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

35 lines
989 B
Python

import json
from loop_claude.claude_code.formats import assistant_parts, human_text, parse, part_text, tool_call
from loop_claude.outcome import Failure
def read(path) -> 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) -> int:
return sum(1 for entry in entries if human_text(entry) is not None)
def render(entries) -> str:
lines = []
for entry in entries:
lines += [line for part in assistant_parts(entry) if (line := part_line(part))]
if (text := human_text(entry)) is not None:
lines.append("[human] " + text)
return "\n\n".join(lines)
def part_line(part, limit=400) -> 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)[:limit]}"
return ""