
    iA                     t   d Z ddlmZmZmZ ddlmZmZmZm	Z	 ddl
mZ ddlmZmZ ddlmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZ e G d d	e             Ze G d
 de             Ze G d de             Z e G d de             Z!e G d de             Z"e G d de             Z#e G d de             Z$y)z)Memory-related configuration dataclasses.    )	dataclassfieldreplace)AnyDictListMapping)AgentExecFlowStage)enum_options_forenum_options_from_values)SchemaLookupErrorget_memory_store_schemaiter_memory_store_schemas)

BaseConfigConfigErrorConfigFieldSpecChildKeyensure_listoptional_dictoptional_strrequire_mappingrequire_strextend_pathc                   &   e Zd ZU eed<   eed<   dZedz  ed<   dZedz  ed<    ee      Z	e
eef   ed<   edeeef   d	ed
d fd       Z edddddd       edddddd       eddddddd       eddddddd       eddddi dd       d!Zy)"EmbeddingConfigprovidermodelNapi_keybase_urldefault_factoryparamsdatapathreturnc                    t        ||      }t        |d|      }t        |d|      }t        |d|      }t        |d|      }t        |d|      xs i } | ||||||      S )Nr   r   r   r   r"   )r   r   r   r   r"   r$   )r   r   r   r   )	clsr#   r$   mappingr   r   r   r   r"   s	            G/Users/bowang/.openclaw/workspace/ChatDev/entity/configs/node/memory.py	from_dictzEmbeddingConfig.from_dict$   sp    !$-w
D9GWd3w	48T:w$7=2HE7X^dkopp    zEmbedding ProviderstrTopenaizEmbedding provider)namedisplay_name	type_hintrequireddefaultdescriptionzEmbedding Modelztext-embedding-3-smallzEmbedding model namezAPI KeyFzAPI keyz
${API_KEY}r.   r/   r0   r1   r3   r2   advancezBase URLzCustom Base URLz${BASE_URL}zCustom Parameterszdict[str, Any]z(Embedding parameters (temperature, etc.)r.   r/   r0   r1   r2   r3   r5   )r   r   r   r   r"   )__name__
__module____qualname__r,   __annotations__r   r   r   dictr"   r   r   classmethodr	   r*   r   FIELD_SPECS r+   r)   r   r      s   MJGS4ZHcDj"48FDcN8qWS#X. q qAR q q $-,
 !*,.
 #"! 
 $#)!
 ",&B
G,Kr+   r   c                       e Zd ZU eed<   dZee   dz  ed<   dZeed<   dZ	eed<   e
deeef   d	ed
d fd       Z ed	dddd       eddddd       eddddddd       eddddddd      dZy)FileSourceConfigsource_pathN
file_typesT	recursiveutf-8encodingr#   r$   r%   c          
         t        ||      }t        |d|      }|j                  d      }d }|]t        |      }g }t	        |      D ]@  \  }	}
t        |
t              st        dt        |d|	 d            |j                  |
       B |}|j                  dd      }t        |t              st        dt        |d            t        |d	|      xs d
} | |||||      S )Nr$   rB   z"file_types entries must be stringszfile_types[]rC   Tzrecursive must be booleanrE   rD   )rA   rB   rC   rE   r$   )r   r   getr   	enumerate
isinstancer,   r   r   appendboolr   )r'   r#   r$   r(   	file_pathfile_types_valuerB   items
normalizedidxitemrecursive_valuerE   s                r)   r*   zFileSourceConfig.from_dictd   s    !$-6	";;|4'+
' 01E$&J&u-	T!$,%&JKX\`klokppq^rLsuu!!$' . $J!++k48/409;t[;YZZT:EgyZ?emtxyyr+   zFile/Directory Pathr,   z$Path to file/directory to be indexedr.   r/   r0   r1   r3   zFile Type Filterz	list[str]Fz4List of file type suffixes to limit (e.g. .md, .txt)zRecursive SubdirectoriesrL   z-Whether to include subdirectories recursivelyr6   zFile EncodingzEncoding used to read files)r$   rB   rC   rE   )r7   r8   r9   r,   r:   rB   r   rC   rL   rE   r<   r	   r   r*   r   r=   r>   r+   r)   r@   r@   ]   s    #'JS	D 'ItHczWS#X. z zAS z z,  .>
 &+!N
 %3G
 $(5
1!Kr+   r@   c            	           e Zd ZU dZedz  ed<   dZedz  ed<   ede	ee
f   dedd fd       Z eddd	d
dd       edddd
de      dZy)SimpleMemoryConfigNmemory_path	embeddingr#   r$   r%   c                    t        ||      }t        |d|      }d }d|v r)|d   $t        j                  |d   t	        |d            } | |||      S )NrW   rX   r$   )rW   rX   r$   )r   r   r   r*   r   )r'   r#   r$   r(   rW   embedding_cfgs         r)   r*   zSimpleMemoryConfig.from_dict   sf    !$-"7M4@'!gk&:&F+55gk6JQ\]acnQo5pM{m$OOr+   zMemory File Pathr,   FzSimple memory file pathTr.   r/   r0   r1   r3   r5   Embedding Configurationr   z Optional embedding configurationr.   r/   r0   r1   r3   child)rW   rX   )r7   r8   r9   rW   r,   r:   rX   r   r<   r	   r   r*   r   r=   r>   r+   r)   rV   rV      s    "Kt"(,I%,PWS#X. P PAU P P '+1
 %2':!
Kr+   rV   c            
           e Zd ZU dZedz  ed<    ee      Ze	e
   ed<   dZedz  ed<   edeeef   dedd fd	       Z edd
dddd       eddddde
       eddddde      dZy)FileMemoryConfigN
index_pathr    file_sourcesrX   r#   r$   r%   c                   t        ||      }t        |j                  d            }|st        dt	        |d            g }t        |      D ]9  \  }}|j                  t        j                  |t	        |d| d                   ; t        |d|      }|t        |d|      }d }	d|v r)|d   $t        j                  |d   t	        |d            }	 | |||	|	      S )
Nrc   z,file_sources must contain at least one entryzfile_sources[rG   rZ   rb   rW   rX   )rb   rc   rX   r$   )r   r   rH   r   r   rI   rK   r@   r*   r   r   )
r'   r#   r$   r(   sources_rawsourcesrQ   rR   rb   r[   s
             r)   r*   zFileMemoryConfig.from_dict   s    !$-!'++n"=>LkZ^`nNopp*,";/ICNN+55dTUbcfbgghSiAj5kl 0 "'<>
%g}dCJ'!gk&:&F+55gk6JQ\]acnQo5pMjw-^bccr+   z
Index Pathr,   FzVector index storage pathTr\   zFile Source Listzlist[FileSourceConfig]zList of file sources to ingestr^   r]   r   zEmbedding used for file memory)rb   rc   rX   )r7   r8   r9   rb   r,   r:   r   listrc   r   r@   rX   r   r<   r	   r   r*   r   r=   r>   r+   r)   ra   ra      s    !Jd
!+0+FL$'(F(,I%,dWS#X. d dAS d d( &%3
 (+.8"
 %2'8!
#Kr+   ra   c            
           e Zd ZU dZedz  ed<   dZeed<   ede	ee
f   dedd fd       Z edd	d
dddd       eddddddd      dZy)BlackboardMemoryConfigNrW     	max_itemsr#   r$   r%   c                    t        ||      }t        |d|      }|j                  dd      }t        |t              r|dk  rt        dt        |d             | |||      S )NrW   rk   rj   r   z$max_items must be a positive integer)rW   rk   r$   )r   r   rH   rJ   intr   r   )r'   r#   r$   r(   rW   max_items_values         r)   r*   z BlackboardMemoryConfig.from_dict   sc    !$-"7M4@!++k48/3/?a3GDkRVXcFdee{oDQQr+   zBlackboard Pathr,   FzqJSON path for blackboard memory writing. Pass 'auto' to auto-create in working directory, valid for this run onlyautoTr4   zMaximum Itemsrm   z:Maximum number of memory items to retain (trimmed by time)r6   )rW   rk   )r7   r8   r9   rW   r,   r:   rk   rm   r<   r	   r   r*   r   r=   r>   r+   r)   ri   ri      s    "Kt"IsRWS#X. R RAY R R '* L
 %(T
Kr+   ri   c            	       
    e Zd ZU eed<   eed<   dZedz  ed<   edeee	f   dedd fd       Z
defd	Z edd
ddd       eddddd       eddddd      dZedeeee   f   fd       Zedeeef   f fd       Z xZS )MemoryStoreConfigr.   typeNconfigr#   r$   r%   c                h   t        ||      }t        |d|      }t        |d|      }	 t        |      }d|vs|d   t	        dt        |d            |j                  j                  |d   t        |d            } | ||||      S # t        $ r }t	        d| dt        |d            |d }~ww xY w)	Nr.   rr   zunsupported memory store type ''rs   z"memory store requires config blockrZ   )r.   rr   rs   r$   )r   r   r   r   r   r   
config_clsr*   )	r'   r#   r$   r(   r.   
store_typeschemaexc
config_objs	            r)   r*   zMemoryStoreConfig.from_dict   s    !$-7FD1 &$7
	s,Z8F 7"gh&7&?BKPTV^D_``&&001BUY[cId0e
:jtLL ! 	s ?
|1M{[_agOhiorr	ss   B 	B1B,,B1c                 r    | j                   s t        dt        | j                  d            | j                   S )Nzmemory store payload missingrs   )rs   r   r   r$   )selfs    r)   require_payloadz!MemoryStoreConfig.require_payload0  s-    {{<k$))U]>^__{{r+   z
Store Namer,   TzUnique name of the memory storerT   z
Store TypezMemory store typezStore ConfigurationobjectznSchema required by the selected store type (simple/file/blackboard/etc.), following that type's required keys.)r.   rr   rs   c                     t               j                         D ci c]  \  }}t        d|      |j                   c}}S c c}}w )Nrs   )r   value)r   rO   r   rv   )r'   r.   rx   s      r)   child_routeszMemoryStoreConfig.child_routesM  sK     !: ; A A C
 Cf 840&2C2CC C
 	
 
s   "Ac           	      0   t         |          }|j                  d      }|rmt               }t	        |j                               }|j                         D ci c]  \  }}||j                   }}}t        ||t        ||d            |d<   |S c c}}w )Nrr   T)preserve_label_case)enumenum_options)
superfield_specsrH   r   rg   keysrO   summaryr   r   )	r'   specs	type_specregistrationsnamesr.   rx   descriptions	__class__s	           r)   r   zMemoryStoreConfig.field_specsT  s    #%IIf%	57M++-.EEREXEXEZ[EZ\T6D&..0EZL[#5e\_cdE&M
  \s   B)r7   r8   r9   r,   r:   rs   r   r<   r	   r   r*   r}   r   r=   r   r   rr   r   r   __classcell__)r   s   @r)   rq   rq     s   
I
I $FJ$MWS#X. M MAT M M   %9
  %+
 ". I
K0 
T(D,<"<= 
 
 Do!56  r+   rq   c                      e Zd ZU eed<   dZee   dz  ed<   dZe	ed<   dZ
eed<   dZeed	<   dZeed
<   edeeef   dedd fd       Z eddddd       edddddeD  cg c]  }|j(                   c}}  ee             eddddddd       eddddddd       ed	dd ddd!d       ed
d"d ddd#d      d$Zyc c}} w )%MemoryAttachmentConfigr.   Nretrieve_stage   top_k      similarity_thresholdTreadwriter#   r$   r%   c                   t        ||      }t        |d|      }|j                  d      }d }|=t        |      }g }t	        |      D ]   \  }	}
	 |j                  t        |
             " |}|j                  dd      }t        |t              r|dk  rt        d	t        |d            |j                  d
d      }t        |t        t        f      st        dt        |d
            |j                  dd      }t        |t              st        dt        |d            |j                  dd      }t        |t              st        dt        |d             | |||t        |      |||      S # t        $ rE}t        dt        D cg c]  }|j                   nc c}w c} t        |d|	 d            |d }~ww xY w)Nr.   r   z&retrieve_stage entries must be one of zretrieve_stage[rG   r   r   r   z top_k must be a positive integerr   r   z$similarity_threshold must be numericr   Tzread must be booleanr   zwrite must be boolean)r.   r   r   r   r   r   r$   )r   r   rH   r   rI   rK   r
   
ValueErrorr   r   r   rJ   rm   floatrL   )r'   r#   r$   r(   r.   
stages_rawstages
stage_listparsedrQ   rR   ry   stagetop_k_valuethreshold_value
read_valuewrite_values                    r)   r*   z MemoryAttachmentConfig.from_dictm  s   !$-7FD1[[!12
26!$Z0J/1F&z2	TMM"4T":; 3 Fkk'1-+s+{a/?@+dT[B\]]!++&<dC/C<8DkRVXnFopp[[.
*d+4k$6OPPkk'40+t,5{47QRR!!&!7
 	
/ " %@[mAn[mRW%++[mAn@op#DOC5*BC s$   E//	F=8F8FF88F=zMemory Namer,   z#Name of the referenced memory storerT   zRetrieve Stagezlist[AgentExecFlowStage]FzExecution stages when memory retrieval occurs. If not set, defaults to all stages. NOTE: this config is related to thinking, if the thinking module is not used, this config has only effect on `gen` stage.)r.   r/   r0   r1   r3   r   r   zTop Krm   zNumber of items to retriever6   zSimilarity Thresholdr   z>Similarity threshold (-1 means no similarity threshold filter)z
Allow ReadrL   z,Whether to read this memory during executionzAllow Writez4Whether to write back to this memory after execution)r.   r   r   r   r   r   )r7   r8   r9   r,   r:   r   r   r
   r   rm   r   r   r   rL   r   r<   r	   r   r*   r   r   r   r=   ).0r   s   00r)   r   r   d  sU   
I6:ND+,t3:E3N"&%&D$E4+
WS#X. +
 +
AY +
 +
\  &=
 *!)0 g+=>+=%%+++=>)*<=
 ! 5
 !0'/X!
  %F
 !&N
Y5K ?s    B>r   N)%__doc__dataclassesr   r   r   typingr   r   r   r	   entity.enumsr
   entity.enum_optionsr   r   schema_registryr   r   r   entity.configs.baser   r   r   r   r   r   r   r   r   r   r   r@   rV   ra   ri   rq   r   r>   r+   r)   <module>r      s    / 1 1 + + + J    =j = =@ =z = =@   B 2z 2 2j  Z    F F
 F FR kZ k kr+   