borderless / ui /agent /traces.py
spagestic's picture
The UI folder restructured
f793029
Raw
History Blame Contribute Delete
1.45 kB
"""Optional JSONL trace logging for sharing Borderless agent sessions."""
from __future__ import annotations
import json
import os
import time
from pathlib import Path
from typing import Any
TRACE_DIR = Path(os.environ.get("BORDERLESS_TRACE_DIR", "agent_traces"))
TRACE_RESULT_LIMIT = 6000
def _compact_result(result: str) -> str:
if len(result) <= TRACE_RESULT_LIMIT:
return result
return result[:TRACE_RESULT_LIMIT] + "\n... (truncated)"
def record_tool_trace(
*,
tool_name: str,
arguments: str,
result: str,
duration: float,
) -> None:
"""Write one tool call to JSONL so sessions can be shared after a demo."""
if os.environ.get("BORDERLESS_DISABLE_TRACE_LOGS") == "1":
return
try:
TRACE_DIR.mkdir(parents=True, exist_ok=True)
path = TRACE_DIR / time.strftime("borderless-%Y%m%d.jsonl")
payload: dict[str, Any] = {
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"tool_name": tool_name,
"arguments": arguments,
"duration_seconds": round(duration, 3),
"result": _compact_result(result),
}
with path.open("a", encoding="utf-8") as handle:
handle.write(json.dumps(payload, ensure_ascii=False) + "\n")
except OSError:
# Trace export should never break the live research experience.
return