o
    jEiS                     @   sB   d dl Z d dlZd dlmZ d dlmZ eeZG dd dZdS )    N)date)	getLoggerc                   @   sB   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Zdd Z	dS )XaiTokenManagerzb
    Manages the daily call quota for the XAI API to prevent rate-limiting and control costs.
    settings.jsonxai_token_state.jsonc                 C   s(   || _ | || _|  | _|   d S )N)
state_path_load_daily_limitdaily_limit_load_statestate_check_and_reset_daily_count)selfsettings_pathr    r   8/var/www/agentarbitrage/keepa_deals/xai_token_manager.py__init__   s   
zXaiTokenManager.__init__c              	   C   s   z,t |d}t|}W d   n1 sw   Y  |dd}td| d |W S  ttjfyB   td| d Y dS w )	zALoads the max_xai_calls_per_day from the main settings.json file.rNmax_xai_calls_per_dayi  z+XAI Token Manager: Daily call limit set to .zCould not load 'z%'. Using default daily limit of 1000.)	openjsonloadgetloggerinfoFileNotFoundErrorJSONDecodeErrorwarning)r   r   fsettingslimitr   r   r   r      s   z!XaiTokenManager._load_daily_limitc              	   C   sz   z&t | jd}t|}W d   n1 sw   Y  td|  |W S  ttjfy<   td ddd Y S w )z"Loads the usage state from a file.r   NzLoaded XAI token state: zDXAI token state file not found or invalid. Initializing fresh state.z
1970-01-01r   )last_reset_datecalls_today)	r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r
      s   
zXaiTokenManager._load_statec              
   C   s   z$t | jd}tj| j|dd W d   W dS 1 sw   Y  W dS  tyC } ztd| j d|  W Y d}~dS d}~ww )z(Saves the current usage state to a file.w   )indentNz#Could not save XAI token state to 'z': )r   r   r   dumpr   IOErrorr   error)r   r   er   r   r   _save_state)   s   &&zXaiTokenManager._save_statec                 C   sX   t t }| jd|kr*td| jd  d || jd< d| jd< |   dS dS )z4Resets the daily call count if the date has changed.r!   z6New day detected. Resetting XAI daily call count from r"   z to 0.r   N)strr   todayr   r   r   r   r*   )r   	today_strr   r   r   r   1   s   

z,XaiTokenManager._check_and_reset_daily_countc                 C   sl   |    | jd | jk r*| jd  d7  < |   td| jd  d| j  dS td| j d dS )	z
        Checks if an XAI API call can be made. If yes, increments the count.
        Returns True if the call is permitted, False otherwise.
        r"      z!XAI call permitted. Usage today: /TzXAI daily call limit reached (z8). Call denied. No further XAI calls will be made today.F)r   r   r	   r*   r   r   r   )r   r   r   r   request_permission:   s   z"XaiTokenManager.request_permissionN)r   r   )
__name__
__module____qualname____doc__r   r   r
   r*   r   r0   r   r   r   r   r      s    
	r   )	r   osdatetimer   loggingr   r1   r   r   r   r   r   r   <module>   s    