
    i                     z    d Z ddlmZmZ ddlmZ ddlmZmZmZm	Z	 ddl
mZ e G d d             Z G d d	      Zy
)z/Token usage tracking module for DevAll project.    )	dataclassfield)datetime)DictListOptionalAny)defaultdictc                       e Zd ZU dZdZeed<   dZeed<   dZeed<    e	e
      Zeeef   ed<    e	ej                         Zeed<   d	Zee   ed
<   d	Zee   ed<   d	Zee   ed<   d	Zee   ed<   d Zy	)
TokenUsagez4Stores token usage metrics for individual API calls.r   input_tokensoutput_tokenstotal_tokens)default_factorymetadata	timestampNnode_id
model_nameworkflow_idproviderc           
          | j                   | j                  | j                  t        | j                        | j
                  j                         | j                  | j                  | j                  | j                  d	S )zConvert to dictionary format.)	r   r   r   r   r   r   r   r   r   )r   r   r   dictr   r   	isoformatr   r   r   r   selfs    @/Users/bowang/.openclaw/workspace/ChatDev/utils/token_tracker.pyto_dictzTokenUsage.to_dict   sc     !--!// --T]]+113||//++

 
	
    )__name__
__module____qualname____doc__r   int__annotations__r   r   r   r   r   r   strr	   r   nowr   r   r   r   r   r   r    r   r   r   r      s    >L#M3L#$T:Hd38n:=Ix=!GXc]! $J$!%K#%"Hhsm"
r   r   c            	           e Zd ZdZdefdZddedededefd	Zd
efdZded
efdZ	ded
efdZ
ded
efdZd
eeef   fdZdefdZy)TokenTrackerz7Singleton class to track token usage across a workflow.r   c                     || _         t               | _        t        t              | _        t        t              | _        g | _        t        t              | _        y N)	r   r   total_usager
   node_usagesmodel_usagescall_historyr#   node_call_counts)r   r   s     r   __init__zTokenTracker.__init__'   sC    &%<&z2'
3 +C 0r   Nr   r   usager   c           	      ,   |r|j                   s||_         | j                  xj                  |j                  z  c_        | j                  xj                  |j                  z  c_        | j                  xj                  |j                  z  c_        | j
                  |   }|xj                  |j                  z  c_        |xj                  |j                  z  c_        |xj                  |j                  z  c_        |r||_         | j                  |   }|xj                  |j                  z  c_        |xj                  |j                  z  c_        |xj                  |j                  z  c_        |r||_         | j                  |xx   dz  cc<   |||j                  |j                  |j                  t        |j                        |j                  j                         | j                  |   d}|r||d<   | j                  j                  |       y)zKRecords token usage for a specific call, handling multiple node executions.   )r   r   r   r   r   r   r   execution_numberr   N)r   r,   r   r   r   r-   r.   r0   r   r   r   r   r/   append)r   r   r   r2   r   
node_usagemodel_usagehistory_entrys           r   record_usagezTokenTracker.record_usage/   s    ENN%EN 	%%););;%&&%*=*==&%%););;% %%g.
5#5#55  E$7$77 5#5#55"*J ''
3  E$6$66 !!U%8%88!  E$6$66 #+K  	g&!+& $!.."00!..U^^,224 $ 5 5g >	
 (0M*%  /r   returnc                     | j                   S )z'Get total token usage for the workflow.)r,   r   s    r   get_total_usagezTokenTracker.get_total_usage_   s    r   c                      | j                   |   S )z@Get token usage for a specific node (across all its executions).)r-   r   r   s     r   get_node_usagezTokenTracker.get_node_usagec   s    ((r   c                      | j                   |   S )z%Get token usage for a specific model.)r.   )r   r   s     r   get_model_usagezTokenTracker.get_model_usageg   s      ,,r   c                      | j                   |   S )z'Get how many times a node was executed.)r0   r?   s     r   get_node_execution_countz%TokenTracker.get_node_execution_countk   s    $$W--r   c                 6   | j                   | j                  j                  | j                  j                  | j                  j                  d| j
                  j                         D ci c]*  \  }}||j                  |j                  |j                  d, c}}| j                  j                         D ci c]*  \  }}||j                  |j                  |j                  d, c}}t        | j                        | j                  d}|S c c}}w c c}}w )N)r   r   r   )r   r,   r-   r.   node_execution_countsr/   )r   r,   r   r   r   r-   itemsr.   r   r0   r/   )r   r   r2   r   datas        r   get_token_usagezTokenTracker.get_token_usageo   s#   ++ $ 0 0 = =!%!1!1!?!? $ 0 0 = = '+&6&6&<&<&> '?NGU $)$6$6%*%8%8$)$6$6 
 '? *.):):)@)@)B *C%J $)$6$6%*%8%8$)$6$6 
 *C &*$*?*?%@ --1
4 's   */D
9/Dfilepathc                     ddl }ddlm}  ||      }|j                  j	                  dd       | j                         }t        |dd      5 }|j                  ||d	d
       ddd       y# 1 sw Y   yxY w)z'Export token usage data to a JSON file.r   N)PathT)parentsexist_okwzutf-8)encodingF   )ensure_asciiindent)jsonpathlibrL   parentmkdirrI   opendump)r   rJ   rT   rL   pathrH   fs          r   export_to_filezTokenTracker.export_to_file   sf      H~$6##%(C'2aIIdAE!I< 322s   A--A6r+   )r   r    r!   r"   r%   r1   r   r:   r=   r@   rB   r#   rD   r   r	   rI   r\   r'   r   r   r)   r)   $   s    A1C 1.0C .0S .0 .0WZ .0`   )c )j )-# -* -. . .c3h :=s =r   r)   N)r"   dataclassesr   r   r   typingr   r   r   r	   collectionsr
   r   r)   r'   r   r   <module>r`      s<    5 (  , , # 
 
 
6t= t=r   