Files
claude-config/bin/loop_claude/claude_code/transcript.py
T

37 lines
1.1 KiB
Python

import json
from loop_claude.claude_code.entries import assistant_parts, human_text, parse, part_text, tool_call
from loop_claude.failure import Failure
JUDGED_TOOL_INPUT = 400
def read(path: str) -> list[dict] | Failure:
try:
with open(path, "rb") as handle:
return [entry for raw in handle if (entry := parse(raw))]
except OSError as failure:
return Failure(f"could not read the transcript {path}: {failure.strerror}")
def human_turns(entries: list[dict]) -> int:
return sum(1 for entry in entries if human_text(entry) is not None)
def render(entries: list[dict]) -> str:
lines = []
for entry in entries:
lines += [line for part in assistant_parts(entry) if (line := part_line(part, JUDGED_TOOL_INPUT))]
if (text := human_text(entry)) is not None:
lines.append("[human] " + text)
return "\n\n".join(lines)
def part_line(part: object, tool_input_chars: int) -> str:
if (text := part_text(part)) and text.strip():
return text
if call := tool_call(part):
name, arguments = call
return f"[tool {name}] {json.dumps(arguments)[:tool_input_chars]}"
return ""