
    `gj              	           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y)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                 V    | rd| vr| S | j                  dd      j                  dd      S )z4Convert <REASONING_SCRATCHPAD> tags to <think> tags.<REASONING_SCRATCHPAD>z<think></REASONING_SCRATCHPAD>z</think>)replacer   s    C/root/.hermes/venv/lib/python3.12/site-packages/agent/trajectory.pyconvert_scratchpad_to_thinkr      s3    .g=??3Y?GGHacmnn    c                     | syd| v xr d| vS )zMCheck if content has an opening <REASONING_SCRATCHPAD> without a closing tag.Fr
   r    r   s    r   has_incomplete_scratchpadr      s    #w.[3LT[3[[r   
trajectorymodel	completedfilenamec                    ||rdnd}| t        j                         j                         ||d}	 t        |dd      5 }|j	                  t        j                  |d	      d
z          ddd       t        j                  d|       y# 1 sw Y    xY w# t        $ r }t        j                  d|       Y d}~y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,      s     1:-@[ $\\^--/	E;(C'2 	BaGGDJJu59D@A	B,h7	B 	B  ;6::;s.   B *B)B BB 	B=B88B=)N)__doc__r#   loggingr   typingr   r   r   	getLogger__name__r%   strr   boolr   r,   r   r   r   <module>r4      s       " "			8	$o o o\s \t \ 6:;T#s(^ 4 ;S ;#;/2;r   