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

36 lines
982 B
Python

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