
    $j              '       r   d Z ddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
 ddlZddlmZmZmZmZmZ ddlmZmZmZmZ ddlmZ deeeef                  dz  d	ee         fd
ZerddlmZ ddlm Z   ej!        e"          Z#dZ$ded	efdZ%ded	e&fdZ' ej(        dej)                  Z* ej(        dej)        ej+        z            Z, ej(        dej)                  Z- ej(        dej)        ej+        z            Z.ded	efdZ/ded	efdZ0dededdded	e1eeef         dz  e2e2f         f
dZ3 ej4        d          Z5d eeeef                  d	e2fd!Z6d"e7d	e&fd#Z8de$dddd$d%d%dd&f
ddd'ed(ed)eeef         d*e
ee2ge	eeef                  f         d+e
ee2ge	eeef                  f         d,e
ee2e2ge	eeef                  f         d-e
ee         ege	eeef                  f         d.edz  d/e2d0e2dz  dedz  deeeef                  dz  d1e&d2e&d3e&d4edz  d5e2d	ef&d6Z9d7d8d	eeef         fd9Z:	 	 dMd:d8d;e;e         d<e;e         d=e;e         d>e2d?e2d@ee         dAee         dBedCe
dedDee         ddEdedz  d	efdFZ<	 dNd7d8d*e
ee2ge	eeef                  f         d+e
ee2ge	eeef                  f         d,e
ee2e2ge	eeef                  f         d-e
ee         ege	eeef                  f         dGe=e         dz  d	e1eeef         e2f         fdHZ>	 dNdIedJeeef         d*e
ee2ge	eeef                  f         d+e
ee2ge	eeef                  f         d,e
ee2e2ge	eeef                  f         d-e
ee         ege	eeef                  f         dGe=e         dz  d	eeef         fdKZ?dIedJeeef         d	efdLZ@dS )Oa  
Reflect agent - agentic loop for reflection with native tool calling.

Uses hierarchical retrieval:
1. search_mental_models - User-curated summaries (highest quality)
2. search_observations - Consolidated knowledge with freshness
3. recall - Raw facts as ground truth
    N)TYPE_CHECKINGAny	AwaitableCallable   )DirectiveInfoLLMCallReflectAgentResultTokenUsageSummaryToolCall)_extract_directive_rulesbuild_final_promptbuild_final_system_promptbuild_system_prompt_for_tools)get_reflect_tools
directivesreturnc                 "    | sg S d | D             S )z,Build list of DirectiveInfo from directives.c           
          g | ]P}t          |                    d d          |                    dd          |                    dd                    QS )id namecontent)r   r   r   )r   get).0	directives     j/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/hindsight_api/engine/reflect/agent.py
<listcomp>z-_build_directives_applied.<locals>.<listcomp>"   sl         	}}T2&&vr**MM)R00	
 	
 	
       )r   s    r   _build_directives_appliedr!      s2     	  $   r      )LLMProvider)LLMToolCall
   r   c                     |                      d          r| t          d          d         } |                      d          r| t          d          d         } d| v r|                     d          d         } | S )au  Normalize tool name from various LLM output formats.

    Some LLMs output tool names in non-standard formats:
    - 'functions.done' (OpenAI-style prefix)
    - 'call=functions.done' (some models)
    - 'call=done' (some models)
    - 'done<|channel|>commentary' (malformed special tokens appended)

    Returns the normalized tool name (e.g., 'done', 'recall', etc.)
    zcall=Nz
functions.z<|r   )
startswithlensplitr   s    r   _normalize_tool_namer+   5   s     w $CLLNN# |$$ )C%%''( t||zz$"Kr   c                 (    t          |           dk    S )z2Check if the tool name represents the 'done' tool.done)r+   r*   s    r   _is_done_toolr.   P   s    %%//r   zdone\s*\(\s*\{.*$z[\s*```(?:json)?\s*\{[^}]*(?:"(?:observation_ids|memory_ids|mental_model_ids)"|\})\s*```\s*$zM\s*\{[^{]*"(?:observation_ids|memory_ids|mental_model_ids|answer)"[^}]*\}\s*$zG\s*(?:observation_ids|memory_ids|mental_model_ids)\s*[=:]\s*\[.*?\]\s*$textc                 h    t                               d|                                           }|r|n| S )zClean up answer text by removing any done() tool call syntax.

    Some LLMs output the done() call as text instead of a proper tool call.
    This strips out patterns like: done({"answer": "...", ...})
    r   )_DONE_CALL_PATTERNsubstripr/   cleaneds     r   _clean_answer_textr6   e   s4     !$$R..4466G'774'r   c                 (   | s| S | }t                               d|                                          }t                              d|                                          }t                              d|                                          }|r|n| S )ak  Clean up the answer field from a done() tool call.

    Some LLMs leak structured output patterns into the answer text, such as:
    - JSON code blocks with observation_ids/memory_ids at the end
    - Raw JSON objects with these fields
    - Plain text like "observation_ids: [...]"

    This cleans those patterns while preserving the actual answer content.
    r   )_LEAKED_JSON_SUFFIXr2   r3   _LEAKED_JSON_OBJECT_TRAILING_IDS_PATTERNr4   s     r   _clean_done_answerr;   p   s      G "%%b'2288::G "%%b'2288::G $''G44::<<G'774'r   answerresponse_schema
llm_configr#   
reflect_idc                 Z  K   	 ddl m} ddlm} dt          dt
          fd}|                    di           }t          |                    dg                     }i }	|                                D ]\  }
} ||          }|
|v rd	nd
}||f|	|
<    |	s t          
                    d| d           dS  |d.i |	}t          j        |dd          }g }|                                D ]\\  }
}|                    dd          }|                    dd          }|
|v }|rdnd}|                    d|
 d| d| d|            ]d                    |          }d|  d| d| d }|                    d!d"d#d$|d#g|d%d&d&'           d
{V \  }}t!          |d(          r|                                }n9t%          |t                    r|}n!t          j        t)          |                    }|D ]F}
|                    |
          }||dk    s|g k    r!t          
                    d| d)|
 d*           Gt                              d| d+t-          |           d,           ||j        |j        fS # t2          $ r+}t          
                    d| d-|            Y d
}~dS d
}~ww xY w)/a  Generate structured output from an answer using the provided JSON schema.

    Args:
        answer: The text answer to extract structured data from
        response_schema: JSON Schema for the expected output structure
        llm_config: LLM provider for making the extraction call
        reflect_id: Reflect ID for logging

    Returns:
        Tuple of (structured_output, input_tokens, output_tokens).
        structured_output is None if generation fails.
    r   )r   )create_modelfield_schemar   c                     |                      dd          }|dk    rt          S |dk    rt          S |dk    rt          S |dk    rt          S |dk    rt
          S t          S )z<Map JSON schema type to Python type for better LLM guidance.typestringarrayobjectintegernumberboolean)r   listdictintfloatboolstr)rB   	json_types     r   _json_schema_type_to_pythonz@_generate_structured_output.<locals>._json_schema_type_to_python   sl    $((::IG##h&&i''
h&&i''
r   
propertiesrequired.N	[REFLECT z@] No fields found in response_schema, skipping structured output)Nr   r   StructuredResponser"   F)indentensure_asciirD   rE   descriptionr   z (REQUIRED)z (optional)z-  (): 
zwYour task is to extract specific information from the answer below and format it as JSON.

ANSWER TO EXTRACT FROM:
"""
zS
"""

REQUIRED OUTPUT FORMAT - Extract the following fields from the answer above:
z

JSON Schema:
```json
a  
```

INSTRUCTIONS:
1. Read the answer carefully and identify the information that matches each field
2. Extract the ACTUAL content from the answer - do NOT leave fields empty if information is present
3. For string fields: use the exact text or a clear summary from the answer
4. For array fields: return a JSON array (e.g., ["item1", "item2"]), NOT a string
5. For required fields: you MUST provide a value extracted from the answer
6. Return ONLY the JSON object, no explanation

OUTPUT:systemzYou are a precise data extraction assistant. Extract information from text and return it as valid JSON matching the provided schema. Always extract actual content - never return empty strings for required fields if information is available.roler   userreflect_structuredT)messagesresponse_formatscopeskip_validationreturn_usage
model_dumpz] Required field 'z' is empty in structured outputz#] Generated structured output with z fieldsz(] Failed to generate structured output: )rV   )typingr   pydanticrA   rL   rD   r   setitemsloggerwarningjsondumpsappendjoincallhasattrrh   
isinstanceloadsrP   infor(   input_tokensoutput_tokens	Exception)r<   r=   r>   r?   	TypingAnyrA   rR   schema_propsrequired_fieldsfields
field_namerB   
field_typedefaultDynamicModel
schema_strfield_descriptions
field_descis_required
req_markerfields_textstructured_promptstructured_resultusagestructured_outputvaluees                              r   _generate_structured_outputr      s     $m++++++))))))	d 	t 	 	 	 	" '**<<<o11*bAABB')(4(:(:(<(< 	7 	7$J44\BBJ'?::ccG",g!6F: 	NNszsssttt:#|CCFCC ZNNN
  (4(:(:(<(< 	b 	b$J%))&(;;J%))-<<J$7K*5H=J%%&`:&`&`&`&`j&`&`T^&`&`aaaaii 233 	      2 *4 %  R   ,=>> )&  *9 *
 *
 $
 $
 $
 $
 $
 $
 5 $l33 	C 1 < < > >)400 	C 1 !%
3/@+A+A B B * 	v 	vJ%))*55E}u{{t:tttttuuun
nnsSdOeOennnooo %"4e6III   Z:ZZWXZZ[[[zzzzzs   B-I5 3GI5 5
J*? J%%J*cl100k_baserc   c                    d}| D ]d}|                     d          pd}t          |t                    r+|t          t                              |                    z  }nt          |t                    rr|D ]o}t          |t                    rXt          |                     d          t                    r0|t          t                              |d                             z  }p|                     d          pg D ]k}t          |t                    rT|                     di           }|t          t                              |                     dd                              z  }lf|S )zIEstimate the token count of the messages list using cl100k_base encoding.r   r   r   r/   
tool_callsfunction	arguments)r   ru   rP   r(   _TIKTOKEN_ENCODINGencoderK   rL   )rc   totalmsgr   parttcfuncs          r   _count_messages_tokensr     sd   E S S'')$$*gs## 	JS+227;;<<<EE&& 	J J JdD)) Jj&9I9I3.O.O JS!3!:!:4<!H!HIIIE'',''-2 	S 	SB"d## Svvj"--/66txxR7P7PQQRRR	S Lr   excc                 |    t          |                                           t          fddD                       S )zIReturn True if the exception signals the LLM context window was exceeded.c              3       K   | ]}|v V  	d S Nr    )r   phraser   s     r   	<genexpr>z-_is_context_overflow_error.<locals>.<genexpr>&  s;         	#     r   )context_length_exceededzcontext length exceededzmaximum context lengthprompt_too_longzprompt is too longresource_exhaustedzinput is too longztoo many tokens)rP   lowerany)r   r   s    @r   _is_context_overflow_errorr   #  sR    
c((..

C    	
     r   FT順 bank_idquerybank_profilesearch_mental_models_fnsearch_observations_fn	recall_fn	expand_fncontextmax_iterations
max_tokenshas_mental_modelsinclude_observationsinclude_recallbudgetmax_context_tokensc                 !  NOPQRSTK   |dd          dt          t          j                    dz            dz   Pt          j                    Qt          |          }|rt          |          nd}t	          ||||          }t          d |D                       Nt          |||||          }d	|d
dd
g}d}g }g Rg Og }dSdTt                      }t                      }t                      }dt          t                   fOfd}dt          fSTfd}d^dt          dt           dt          fOPQRfd}d} t          |	          D ]}!|!|	dz
  k    }"|"rYt          ||||          }#t          j                    }$|                     d	t!          |                    d                    d
d|#d
gd|
d           d{V \  }%}&t          t          j                    |$z
  dz            }'S|&j        z  ST|&j        z  TO                    d|'|&j        |&j        d           t+          |%                                          }(d})|r(|(r&t/          |(|| P           d{V \  })}*}+S|*z  ST|+z  T ||(|!dz   d           t1          |(|)|!dz   || |             |            |          c S t3          |          },|,|k    rt          |          st          |          st          |          rt4                              dP d |!dz    d!|, d"| d#	           t          ||||          }#t          j                    }$|                     d	t!          |                    d                    d
d|#d
gd|
d           d{V \  }%}&t          t          j                    |$z
  dz            }'S|&j        z  ST|&j        z  TO                    d|'|&j        |&j        d           t+          |%                                          }(d})|r(|(r&t/          |(|| P           d{V \  })}*}+S|*z  ST|+z  T ||(|!dz   d           t1          |(|)|!dz   || |             |            |          c S t          j                    }$g }-|r|-                    d$           |r|-                    d%           |r|-                    d&           |!t9          |-          k     rd'd(|-|!         id)}.nd*}.	 |                     ||d+|.,           d{V }/t          t          j                    |$z
  dz            }'d} S|/j        z  ST|/j        z  TO                    d-|!dz    |'|/j        |/j        d           n]# t<          $ rO}0t          t          j                    |$z
  dz            }1| dz  } t4                              dP d.|!dz    d/|0 d0|1 d1	           O                    d-|!dz    d2|1d3           t          |          pt          |          pt          |          }2t?          |0          r%t4                              dP d4|!dz    d5           n|2s|!|	dz
  k     r| d6k     rY d}0~0+t          ||||          }#t          j                    }$|                     d	t!          |                    d                    d
d|#d
gd|
d           d{V \  }%}&t          t          j                    |$z
  dz            }'S|&j        z  ST|&j        z  TO                    d|'|&j        |&j        d           t+          |%                                          }(d})|r(|(r&t/          |(|| P           d{V \  })}*}+S|*z  ST|+z  T ||(|!dz   d           t1          |(|)|!dz   || |             |            |          cY d}0~0c S d}0~0ww xY w|/j         st          |          pt          |          pt          |          }2|o|2 }3|/j!        r}|3szt+          |/j!                                                  }(|
t9          tD          #                    |(                    |
k    rt          j                    }4|                     d	d7d
dd8|
 d9|( d
gd|
d           d{V \  }5}6S|6j        z  ST|6j        z  TO                    d:t          t          j                    |4z
  dz            |6j        |6j        d           t+          |5                                          }(d})|r(|(r&t/          |(|| P           d{V \  })}*}+S|*z  ST|+z  T ||(|!dz              t1          |(|)|!dz   || |             |            |          c S t          ||||          }#t          j                    }$|                     d	t!          |                    d                    d
d|#d
gd|
d           d{V \  }%}&t          t          j                    |$z
  dz            }'S|&j        z  ST|&j        z  TO                    d|'|&j        |&j        d           t+          |%                                          }(d})|r(|(r&t/          |(|| P           d{V \  })}*}+S|*z  ST|+z  T ||(|!dz   d           t1          |(|)|!dz   || |             |            |          c S tI          d; |/j         D             d          }7|7r=t          |          pt          |          pt          |          }2|2sk|!|	dz
  k     rb|                    d<tK          |7          gd=           |                    d>|7j&        |7j'        tQ          j)        d?d@idA          dB           
eddCl*m+}8  |8            }9dD}:|9,                    |:          5 };|;-                    dEd+           |;-                    dFd+           t]          |7||||!dz   || |             |            |P|| |G           d{V cddd           c S # 1 swxY w Y   dH |/j         D             }<|<rg }=g }>|<D ]K}?t_          |?j'                  }@N|@Nvr|@dIvr|>                    |?           6|=                    |?           L|                    d<dJ |<D             d=           |>D ]R}?|                    d>|?j&        |?j'        tQ          j)        d?dKt_          |?j'                   dLidA          dB           S|=}<NfdM|<D             }Ata          j1        |AdNdi d{V }B|t9          |<          z  }te          |<|B          D ]\  }?}Ctg          |Ct<                    r2t4                              dP dO|?j'         dP|C            d?dQ|C i}Dd}En|C\  }D}Et_          |?j'                  }Ftg          |Dth                    r-d?|Dv r)t4                              dP dO|F dR|Dd?                     |Fd$k    rCtg          |Dth                    r.dS|Dv r*|DdS         D ]!}GdT|Gv r|5                    |GdT                    "|Fd%k    rCtg          |Dth                    r.dU|Dv r*|DdU         D ]!}HdT|Hv r|5                    |HdT                    "|Fd&k    rCtg          |Dth                    r.dV|Dv r*|DdV         D ]!}IdT|Iv r|5                    |IdT                    "|                    d>|?j&        |?j'        tQ          j)        |Dt          dW          dB           d>|?j'        i|?j6        }Jto          |?j'        |?j6                  }K|?j6                            dX          }L|                    tq          |?j'        |L|J|D|E|!dz   Y                     	 t9          tQ          j)        |DdA                    }Mn3# tr          tt          f$ r t9          t          |D                    }MY nw xY wR                    |?j'        |K|E|MdZ           |                    |?j'        |J|Dd[           ̐ d\}( ||(|	d           t1          |(|	|| |             |            |]          S )_a  
    Execute the reflect agent loop using native tool calling.

    The agent uses hierarchical retrieval:
    1. search_mental_models - User-curated summaries (try first)
    2. search_observations - Consolidated knowledge with freshness
    3. recall - Raw facts as ground truth

    Args:
        llm_config: LLM provider for agent calls
        bank_id: Bank identifier
        query: Question to answer
        bank_profile: Bank profile with name and mission
        search_mental_models_fn: Tool callback for searching mental models (query, max_results) -> result
        search_observations_fn: Tool callback for searching observations (query, max_results) -> result
        recall_fn: Tool callback for recall (query, max_tokens) -> result
        expand_fn: Tool callback for expand (memory_ids, depth) -> result
        context: Optional additional context
        max_iterations: Maximum number of iterations before forcing response
        max_tokens: Maximum tokens for the final response
        response_schema: Optional JSON Schema for structured output in final response
        directives: Optional list of directive mental models to inject as hard rules

    Returns:
        ReflectAgentResult with final answer and metadata
    N   -  r   )directive_rulesinclude_mental_modelsr   r   c              3   d   K   | ]+}|                     d           dk    |d         d         V  ,dS )rD   r   r   Nr   r   ts     r   r   z$run_reflect_agent.<locals>.<genexpr>t  sF      -r-rVWV[V[\bVcVcgqVqVqa
mF.CVqVqVqVq-r-rr   )r   r   r   r^   r_   ra   r   r   c                      d  D             S )Nc                     g | ]I}t          |d          |d         |                    dd          |                    dd                    JS )re   duration_msrx   r   ry   re   r   rx   ry   )r	   r   r   cs     r   r   z=run_reflect_agent.<locals>._get_llm_trace.<locals>.<listcomp>  sj     
 
 
  jm,UU>155eeOQ77	  
 
 
r   r    )	llm_traces   r   _get_llm_tracez)run_reflect_agent.<locals>._get_llm_trace  s&    
 
 
 
 
 	
r   c                  .    t            z             S )Nrx   ry   total_tokens)r   )total_input_tokenstotal_output_tokenss   r   
_get_usagez%run_reflect_agent.<locals>._get_usage  s)     +-+.AA
 
 
 	
r   Fr<   
iterationsforcedc                    t          t          j                    z
  dz            }d                    d D                       pd}d                    d 
D                       pd}t          d 
D                       }t          d D                       }t	          |           dk    r| d d         d	z   n| }|rd
nd}	t
                              d d|	 dd d          d| d| d| d| d| d| d| d           d S )Nr   z, c              3   ~   K   | ]8}|d           d|d          d|d          d|                     dd           dV  9d	S )
tool(input_summaryz)=r   zms/output_charsr   r   Nr   r   s     r   r   z=run_reflect_agent.<locals>._log_completion.<locals>.<genexpr>  su         V9eeq1eeQ}5Eee!%%P^`aJbJbeee     r   nonec              3   >   K   | ]}|d           d|d          dV  dS )re   =r   msNr    r   s     r   r   z=run_reflect_agent.<locals>._log_completion.<locals>.<genexpr>  s<      XX1W: D D-0@ D D DXXXXXXr   c              3   &   K   | ]}|d          V  dS r   Nr    r   s     r   r   z=run_reflect_agent.<locals>._log_completion.<locals>.<genexpr>  s'      ??1]+??????r   c              3   &   K   | ]}|d          V  dS r   r    r   s     r   r   z=run_reflect_agent.<locals>._log_completion.<locals>.<genexpr>  s'      JJ!Q}-JJJJJJr   d   z...r   r-   rU   z] z
 | query='2   z...' | iterations=z | llm=[z] (zms) | tools=[zms) | answer='z
' | total=r   )rM   timerr   sumr(   rm   rw   )r<   r   r   
elapsed_mstools_summaryllm_summarytotal_llm_mstotal_tools_msanswer_previewmoder   r   r?   
start_timetool_trace_summarys             r   _log_completionz*run_reflect_agent.<locals>._log_completion  s   $)++
2d:;;
II  +       	 iiXXiXXXXXb\b??Y?????JJ7IJJJJJ14Vs1B1B--!-xxv$
 $ $d $ $CRCj$ $$$ $  $ $ %1$ $ $	$ $ )7	$ $
 &$ $  $ $ $	
 	
 	
 	
 	
r   r   )r   missionreflectT)rc   re   max_completion_tokensrg   finalr   )r   )r/   r   r   tools_called
tool_tracer   r   directives_appliedrU   z'] Context budget exceeded on iteration z: ~z tokens >= z  limit. Forcing final synthesis.search_mental_modelssearch_observationsrecallr   r   )rD   r   autoreflect_tool_call)rc   toolsre   tool_choiceagent_z] LLM error on iteration r\   rZ   zms)_err)re   r   z'] Context window exceeded on iteration z1, forcing final synthesis from gathered evidence.r"   zRewrite the user's text so it fits within the requested token budget. Preserve the key facts and structure; drop lower-priority detail. Respond with the rewritten text only, no preamble.zTarget budget: z tokens.

Text to rewrite:
final_rewritec              3   B   K   | ]}t          |j                  |V  d S r   r.   r   r   r   s     r   r   z$run_reflect_agent.<locals>.<genexpr>	  s1      SSM"'<R<RS"SSSSSSr   	assistant)r`   r   r   errorzYou must search for information first. Use search_mental_models(), search_observations(), or recall() before providing your final answer.rX   )r`   tool_call_idr   r   
get_tracerzhindsight.reflect_tool_callzhindsight.scopezhindsight.operation)r   r>   r=   c                 :    g | ]}t          |j                  |S r    r   r  s     r   r   z%run_reflect_agent.<locals>.<listcomp>@  s'    TTTb]27=S=STrTTTr   r-   expandc                 ,    g | ]}t          |          S r    )_tool_call_to_dictr  s     r   r   z%run_reflect_agent.<locals>.<listcomp>P  s!    "P"P"Pb#5b#9#9"P"P"Pr   Tool '7' is not available. Use only the tools provided to you.c           
      :    g | ]}t          |           S )enabled_tools)_execute_tool_with_timing)r   r   r  r   r   r   r   s     r   r   z%run_reflect_agent.<locals>.<listcomp>g  sL     
 
 
  *+*"/  
 
 
r   return_exceptionsz] Tool z failed with exception: zTool execution failed: z returned error: mental_modelsr   observationsmemories)r   rX   reason)r   r  inputoutputr   	iteration)r   r   r   r   )r   r  r  zGI was unable to formulate a complete answer within the iteration limit.)r/   r   r   r   r   r   r   )F);rM   r   r!   r   r   	frozensetr   rk   rK   r	   r   rP   rO   ranger   rs   r   r   rx   ry   rq   r6   r3   r   r
   r   rm   rn   r(   call_with_toolsrz   r   r   r   r   r   nextr  r   r   ro   rp   hindsight_api.tracingr  start_as_current_spanset_attribute_process_done_toolr+   asynciogatherzipru   rL   addr   _summarize_inputr   	TypeError
ValueError)Ur>   r   r   r   r   r   r   r   r   r   r   r=   r   r   r   r   r   r   r   r   r   system_promptrc   total_tools_calledr   context_historyavailable_memory_idsavailable_mental_model_idsavailable_observation_idsr   r   r   consecutive_errorsr  is_lastprompt	llm_startresponser   llm_durationr<   r   	struct_in
struct_outestimated_tokensforced_sequenceiter_tool_choiceresultr   err_durationhas_gathered_evidencedirective_leak_riskrewrite_start	rewrittenrewrite_usage	done_callr  tracer	span_namespanother_toolsallowed_toolshallucinated_toolsr   norm
tool_taskstool_resultsresult_datar  r   normalized_tool_namemmobsmemory
input_dictr   tool_reasonr   r  r   r?   r   r   r   r   sU     ` ````                                                                      @@@@@@@r   run_reflect_agentrS  5  sX     \ BQBKDD#dikkD&8"9"9F"BDDJJ 3:>> ?IR.z:::dO '/1%	  E %.-r-rU-r-r-r$r$rM 2g*HYbh  M m44E**&H !#J/1&(I,.O  &)UU+.55*-%%	
DM 	
 	
 	
 	
 	
 	

) 
 
 
 
 
 
 

 
 
 
d 
 
 
 
 
 
 
 
 
 
2 >** Pa Pa	~11 /	'gRd  F 	I$.OO%2KLL\L\]fLgLg2h2hii#77  &0! %4 % %      OHe 	i 74?@@L%"445#66$#/$)$6%*%8	    ((8(899F !% 26 2A\OZB B < < < < < <8!9j #i/"#z1#OFIM$????%"3$q=/%(.** jll#5	 	 	 	 	 	 2(;;111%&& 2*./I*J*J 2NRSlNmNm 2 NNfJ f fy[\} f f$f f1Cf f f   (gRd  F 	I$.OO%2KLL\L\]fLgLg2h2hii#77  &0! %4 % %      OHe 	i 74?@@L%"445#66$#/$)$6%*%8	    ((8(899F $ 26 2A\OZB B < < < < < <8!9j #i/"#z1#OFIM$????%"3$q=/%(.** jll#5	 	 	 	 	 	 IKK	
  	;""#9::: 	:""#8999 	-""8,,,s?++++4>VUdenUoLp+q+q%S	%55!),	 6        F 	i 74?@@L!"&"556#775i!m55#/$*$7%+%9	      ?	 ?	 ?		i 74?@@L!#NNszssIXYMss]^ssbnsssttt'C	A'C'C'CT`aabbb)**qd3M.N.NqRVWpRqRq "
 *!,, F
 F FS\_`S` F F F   
 + y>A;M/M/MRdghRhRh'gRd  F 	I$.OO%2KLL\L\]fLgLg2h2hii#77  &0! %4 % %      OHe 	i 74?@@L%"445#66$#/$)$6%*%8	    ((8(899F !% 26 2A\OZB B < < < < < <8!9j #i/"#z1#OFIM$????%"3$q=/%(.** jll#5	 	 	 	 	 	 	 	 	 	 	m?	D   v	 )**qd3M.N.NqRVWpRqRq " #-"J5J1J~ >&9 >+FN,@,@,B,BCC )c2D2K2KF2S2S.T.TWa.a.a$(IKKM5?__ )1%a  )/+oZ+o+ogm+o+o " (.8%)# 6E 6 6 0 0 0 0 0 0,I}& '-*DD&'=+FF'$$%4+.	m0Kt/S+T+T,9,F-:-H	    0	0A0ABBF %)!" 6v 6E`ZF F @ @ @ @ @ @<%y* ')3&':5'	A666)&7(1}!3),n..$*,,'9	 	 	 	 	 	 (gRd  F 	I$.OO%2KLL\L\]fLgLg2h2hii#77  &0! %4 % %      OHe 	i 74?@@L%"445#66$#/$)$6%*%8	    ((8(899F !% 26 2A\OZB B < < < < < <8!9j #i/"#z1#OFIM$????%"3$q=/%(.** jll#5	 	 	 	 	 	 SSv'8SSSUYZZ	 3	 )**qd3M.N.NqRVWpRqRq " ) Y!9K-K-K +'9)'D'D&E     &(1 )#': '  *u */	$ $ $	
 
    988888Z\\F5I--i88 D""#46IJJJ""#8:MNNN/(.-M&"N$$JLL#'9)$3                         * UTF$5TTT J	aM!#! - -+BG44 ,]1J1Jt[mOmOm&--b1111!((,,,, OO'"P"PK"P"P"P    )   &(* "#': '  *I2Frw2O2O  *I  *I  *I */	$ $ $	
 
    (K
 
 
 
 
 
 
 
 &
 
 
J ")!Tt!T!TTTTTTTL#k"2"22 $'{L#A#A Ua UaKk955 6NN#pz#p#p"'#p#pcn#p#pqqq%'N'N'NOF"#KK*5'FK (<BG'D'D$ fd++ 60A0ANNoJoo7Koo^del^moo   ),BBB"6400 C'611$_5 E E2::6::2d8DDD ),AAA"6400 B&&00%n5 E E3;;599#d)DDD'833
648P8P3U_ciUiUi"("4 C C6>>044VD\BBB  &(* "#':fcPU#V#V#V	    %bg>>
 0", G G !l..x88!!W*(%$/"+a-  	 	 	4#&tz&u'M'M'M#N#NLL!:. 4 4 4#&s6{{#3#3LLL4 #)) ")6'2(4	     &&*X^'_'_```` WFOFN48888!' .""jll-   sO   =BV_C&_;E___A"qq	q	8#-A@@A@r   r$   c                     | j         d| j        t          j        | j        d          dd}| j        
| j        |d<   |S )z-Convert LLMToolCall to OpenAI message format.r   Fr  )r   r   )r   rD   r   Nthought_signature)r   r   ro   rp   r   rU  )r   ds     r   r  r    sY     eGBLuEEE
 
 A 
'!#!5
Hr   rB  r-  r.  r/  r   r+  r   r   r   log_completionr   zLLMProvider | Nonec                 \  K   | j         }|                    dd                                          }|rt          |          nd}|sd}fd|                    d          pg D             }fd|                    d          pg D             }fd|                    d	          pg D             }d
}|}|rM|rK|rIt	          ||||
           d
{V \  }}}t          |j        |z   |j        |z   |j        |z   |z             } |	||           t          |||||||||||          S )z1Process the done tool call and return the result.r<   r   zNo answer provided.c                     g | ]}|v |	S r    r    )r   midr-  s     r   r   z&_process_done_tool.<locals>.<listcomp>  s$    dddsOcHcHcsHcHcHcr   
memory_idsc                     g | ]}|v |	S r    r    )r   rZ  r.  s     r   r   z&_process_done_tool.<locals>.<listcomp>  s%    vvvSTW[uTuTuSTuTuTur   mental_model_idsc                     g | ]}|v |	S r    r    )r   oidr/  s     r   r   z&_process_done_tool.<locals>.<listcomp>  s%    sssCRUYrRrRrCRrRrRrr   observation_idsNr   )r/   r   r   r   r   r   r   used_memory_idsused_mental_model_idsused_observation_idsr   )
r   r   r3   r;   r   r   rx   ry   r   r
   )rB  r-  r.  r/  r   r+  r   r   r   rW  r?   r   r>   r=   args
raw_answerr<   ra  rb  rc  r   final_usager6  r7  s    ```                    r   r"  r"    s     " D (B''--//J/9A
+++rF '& edddtxx'='='CdddOvvvvTXX6H-I-I-ORvvvssssDHH5F,G,G,M2sss K 	
: 	
& 	
9TOZ:
 :
 4
 4
 4
 4
 4
 4
09j (+i7-
:+i7*D
 
 
 N6:&&&+''31-   r   r  c           
      b  K   ddl m} t          j                    } |            }t          | j                  }	d|	 }
t          j                    }|                    |
|d          5 }|                    d|	           |                    d| j                   |                    dt          j
        | j        d	                     	 t          | j        | j        |||||
           d{V }t          |t                    r7d|v r3ddlm}m} |                     ||j        |d                              n+ddlm}m} |                     ||j                             t+          t          j                    |z
  dz            }|                    d|           t          j                    }|                    |           ||fcddd           S # t.          $ r}ddlm}m} |                     ||j        t1          |                               |                    |           t+          t          j                    |z
  dz            }|                    d|           t          j                    }|                    |            d}~ww xY w# 1 swxY w Y   dS )z2Execute a tool call and return result with timing.r   r  zhindsight.reflect_tool_exec.F)r   end_on_exitzhindsight.tool.namezhindsight.tool.idzhindsight.tool.argumentsr  r  Nr  )Status
StatusCoder   zhindsight.tool.duration_ms)end_time)r  r  r   r+   r   time_nsr   r!  r   ro   rp   r   _execute_toolru   rL   opentelemetry.traceri  rj  
set_statusERROROKrM   endrz   rP   record_exception)r   r   r   r   r   r  r  r   rC  normalized_namerD  start_time_nsrE  r;  ri  rj  r   end_time_nsr   s                      r   r  r  &  sJ      100000J Z\\F*2733O@@@I LNNM		%	%  
& 
 
 0 
0/BBB.6665tz",]b7c7c7cddd&	('&+        F &$'' 7Gv,=,=BBBBBBBBz'7 I IJJJJBBBBBBBBz} 5 5666ty{{Z74?@@K;[III ,..KHHkH***;&M0 0 0 0 0 0 0 0N  		 		 		>>>>>>>>OOFF:#3SVV<<===!!!$$$ty{{Z74?@@K;[III,..KHHkH***		O0 0 0 0 0 0 0 0 0 0s3   *A!J$DG
J!(B4JJ!!J$$J(+J(	tool_namerd  c                   K   t          |           } || |vr| dvrdd|  diS | dk    rQ|                    d          }|sddiS t          |                    d	          pd
          } |||           d{V S | dk    r_|                    d          }|sddiS t          t          |                    d          pd          d          }	 |||	           d{V S | dk    r|                    d          }|sddiS t          t          |                    d          pd          d          }	t          t          |                    d          pd          d          }
 |||	|
           d{V S | dk    rD|                    dg           }|sddiS |                    dd          } |||           d{V S dd|  iS )zExecute a single tool by name.Nr	  r  r  r  r   r   z/search_mental_models requires a query parametermax_results   r   z.search_observations requires a query parameterr     r   r   z!recall requires a query parameter   max_chunk_tokensr
  r[  zexpand requires memory_idsdepthchunkzUnknown tool: )r+   r   rM   max)rw  rd  r   r   r   r   r  r   ry  r   r}  r[  r~  s                r   rm  rm  o  sf      %Y//I  Ym%C%C	YkHkHkd)dddee***!! 	PNOO$((=116Q77,,UK@@@@@@@@@	+	+	+!! 	OMNNTXXl33;t<<dCC
++E:>>>>>>>>>	h		!! 	B@AATXXl33;t<<dCC
s488,>#?#?#G4HH$OOYuj2BCCCCCCCCC	h		XXlB//
 	;9::'**Yz5111111111 5)5566r   c           
      h   | dk    rj|                     dd          }t          |          dk    rd|dd          dnd| d}t          |                     d          pd	          }d
| d| dS | dk    rx|                     dd          }t          |          dk    rd|dd          dnd| d}t          t          |                     d          pd          d          }d
| d| dS | dk    r|                     dd          }t          |          dk    rd|dd          dnd| d}t          t          |                     d          pd          d          }t          t          |                     d          pd          d          }d
| d| d| dS | dk    rB|                     dg           }|                     dd          }dt          |           d| dS | dk    r|                     dd          }	t          |	          dk    rd|	dd          dnd|	 d}
|                     dg           }|                     dg           }|                     dg           }d |
 d!t          |           d"t          |           d#t          |           d	S t	          |          S )$z?Create a summary of tool input for logging, showing all params.r   r   r      'Nz...'ry  rz  z(query=z, max_results=r[   r   r   r{  r   z, max_tokens=r   r|  r}  z, max_chunk_tokens=r
  r[  r~  r  z(memory_ids=[z ids], depth=r-   r<   r]  r`  z(answer=z, mem=z, mm=z, obs=)r   r(   rM   r  rP   )rw  rd  r   query_previewry  r   r}  r[  r~  r<   r   r]  r`  s                r   r'  r'    s   ***"%%03E

R,E#2#J,,,,\\\\$((=116Q77DDDkDDDD	+	+	+"%%03E

R,E#2#J,,,,\\\\TXXl33;t<<dCC
BBBZBBBB	h		"%%03E

R,E#2#J,,,,\\\\TXXl33;t<<dCC
s488,>#?#?#G4HH$OOgggZggTdgggg	h		XXlB//
'**Es:EEUEEEE	f		(B''25f++2B2B.VCRC[....FXXlB//
88$6;;((#4b99w~wwS__ww3GWCXCXww`cds`t`twww	
 t99r   )NNr   )A__doc__r#  ro   loggingrer   ri   r   r   r   r   tiktokenmodelsr   r	   r
   r   r   promptsr   r   r   r   tools_schemar   rK   rL   rP   r!   llm_wrapperr#   response_modelsr$   	getLogger__name__rm   DEFAULT_MAX_ITERATIONSr+   rO   r.   compileDOTALLr1   
IGNORECASEr8   r9   r:   r6   r;   tuplerM   r   get_encodingr   r   rz   r   rS  r  rk   r"  r  r  rm  r'  r    r   r   <module>r     s       				  : : : : : : : : : : : :  [ [ [ [ [ [ [ [ [ [ [ [ [ [            , + + + + +$tCH~*>*E $}J]      .))))))------		8	$	$ s s    60 0 0 0 0 0  RZ 4bi@@  !bjbI   !bjTVXV_   #
NPRPY\^\iPi  
(S (S ( ( ( ((S (S ( ( ( (6  	
 4S>D #s*+   D +X*=99 T$sCx.%9 c    &I $    6 0!#'.2#!%%%c
 c
c
c
 c
 sCx.	c

 &sCj)DcN2K&KLc
 %c3Z4S>1J%JKc
 c34S>)BBCc
 cC()DcN*CCDc
 4Zc
 c
 d
c
 D[c
 T#s(^$t+c
 c
 c
  !c
" $J#c
$ %c
& 'c
 c
 c
 c
L= T#s(^    8 (,#'9 99c(9 !$C9  #3x	9
 9 9 X9 G}9 9 9 9 ]+9 %9 D[9 9 9 9 9D ,0F FF%sCj)DcN2K&KLF %c3Z4S>1J%JKF c34S>)BBC	F
 cC()DcN*CCDF S>D(F 4S>3F F F F` ,0/7 /7/7
sCx./7 &sCj)DcN2K&KL/7 %c3Z4S>1J%JK	/7
 c34S>)BBC/7 cC()DcN*CCD/7 S>D(/7 
#s(^/7 /7 /7 /7d 4S> c      r   