"""Trajectory saving utilities and static helpers. _convert_to_trajectory_format stays as an AIAgent method (batch_runner.py calls agent._convert_to_trajectory_format); only static helpers and file-write logic live here. """ import json import logging from datetime import datetime from typing import Any, Dict, List logger = logging.getLogger(__name__) def convert_scratchpad_to_think(content: str) -> str: """Convert tags to tags.""" if not content or "" not in content: return content return content.replace("", "").replace("", "") def has_incomplete_scratchpad(content: str) -> bool: """Whether content has an opening without a closing tag.""" return bool(content) and "" in content and "" not in content def save_trajectory(trajectory: List[Dict[str, Any]], model: str, completed: bool, filename: str = None): """Append a ShareGPT-format trajectory entry to a JSONL file (default trajectory_samples.jsonl / failed_trajectories.jsonl based on ``completed``).""" if filename is None: filename = "trajectory_samples.jsonl" if completed else "failed_trajectories.jsonl" entry = { "conversations": trajectory, "timestamp": datetime.now().isoformat(), "model": model, "completed": completed, } try: with open(filename, "a", encoding="utf-8") as f: f.write(json.dumps(entry, ensure_ascii=False) + "\n") logger.info("Trajectory saved to %s", filename) except Exception as e: logger.warning("Failed to save trajectory: %s", e)