
    i              	           d Z ddlZddlZddlmZ ddlmZmZmZ  ej        e	          Z
dedefdZdedefdZ	 dd	eeeef                  d
ededefdZdS )zTrajectory saving utilities and static helpers.

_convert_to_trajectory_format stays as an AIAgent method (batch_runner.py
calls agent._convert_to_trajectory_format). Only the static helpers and
the file-write logic live here.
    N)datetime)AnyDictListcontentreturnc                 f    | rd| vr| S |                      dd                               dd          S )z4Convert <REASONING_SCRATCHPAD> tags to <think> tags.<REASONING_SCRATCHPAD>z<think></REASONING_SCRATCHPAD>z</think>)replacer   s    5/home/ubuntu/.hermes/hermes-agent/agent/trajectory.pyconvert_scratchpad_to_thinkr      sA     .g==??3Y??GGHacmnnn    c                     | sdS d| v od| vS )zMCheck if content has an opening <REASONING_SCRATCHPAD> without a closing tag.Fr
   r    r   s    r   has_incomplete_scratchpadr      s&     u#w.[3LT[3[[r   
trajectorymodel	completedfilenamec                    ||rdnd}| t          j                                                    ||d}	 t          |dd          5 }|                    t          j        |d	          d
z              ddd           n# 1 swxY w Y   t                              d|           dS # t          $ r&}t          
                    d|           Y d}~dS d}~ww xY w)ay  Append a trajectory entry to a JSONL file.

    Args:
        trajectory: The ShareGPT-format conversation list.
        model: Model name for metadata.
        completed: Whether the conversation completed successfully.
        filename: Override output filename. Defaults to trajectory_samples.jsonl
                  or failed_trajectories.jsonl based on ``completed``.
    Nztrajectory_samples.jsonlzfailed_trajectories.jsonl)conversations	timestampr   r   azutf-8)encodingF)ensure_ascii
zTrajectory saved to %szFailed to save trajectory: %s)r   now	isoformatopenwritejsondumpsloggerinfo	Exceptionwarning)r   r   r   r   entryfes          r   save_trajectoryr,      sJ    1:[--@[ $\^^--//	 E;(C'222 	BaGGDJu5999D@AAA	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B,h77777 ; ; ;6:::::::::;s;   B' -A?3B' ?BB' BB' '
C1CC)N)__doc__r#   loggingr   typingr   r   r   	getLogger__name__r%   strr   boolr   r,   r   r   r   <module>r4      s            " " " " " " " " " "		8	$	$o o o o o o\s \t \ \ \ \ 6:; ;T#s(^ 4 ;S ;#;/2; ; ; ; ; ;r   