to0p's picture israellaguan's picture
Duplicate from israellaguan/codex-tool-use-trajectories
7bdf36e
Raw History Blame Contribute Delete
1.81 kB
"""Compact view of a trajectory row: role, tool names, first N chars of each message."""
import json
def one_line(s, n=150):
s = (s or "").replace("\r", "").replace("\n", "⏎ ")
return s[:n] + ("…" if len(s) > n else "")
def compact_view(row, n=150, head=8, tail=5):
msgs = row["messages"]
lines = [f"id={row['id']} session={row['session_id']} role={row['agent_role']} thread={row['thread_source']} model={row['model']}/{row['effort']} "
f"task_source={row['task_source']} seg={row['segment']}/{row['segments_total']} calls={row['n_tool_calls']} failed={row['n_failed_calls']} flags={row['flags']} msgs={len(msgs)}"]
idx = list(range(len(msgs)))
if len(msgs) > head + tail:
show = idx[:head] + [None] + idx[-tail:]
else:
show = idx
for i in show:
if i is None:
lines.append(f" … {len(msgs) - head - tail} more messages …")
continue
m = msgs[i]
r = m["role"]
if r == "assistant":
tcs = m.get("tool_calls") or []
names = ",".join(tc["function"]["name"] for tc in tcs)
body = one_line(m.get("content"), n) if m.get("content") else "(no text)"
tag = f"assistant[calls: {names}]" if tcs else "assistant"
lines.append(f" [{i}] {tag}: {body}")
for tc in tcs[:2]:
lines.append(f" ↳ {tc['function']['name']}({one_line(tc['function']['arguments'], n)})")
if len(tcs) > 2:
lines.append(f" ↳ … +{len(tcs)-2} more calls")
elif r == "tool":
lines.append(f" [{i}] tool({m['tool_call_id'][-8:]}): {one_line(m['content'], n)}")
else:
lines.append(f" [{i}] {r}: {one_line(m['content'], n)}")
return "\n".join(lines)