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

45 lines
1.5 KiB
Python

import json
from loop_claude.claude_code.entries import assistant_parts, human_text, loop_text, parse_entry, part_text, tool_call
from failure import Failure
JUDGED_TOOL_INPUT = 400
def read_transcript(path: str) -> list[dict] | Failure:
try:
with open(path, "rb") as handle:
return [entry for raw in handle if (entry := parse_entry(raw))]
except OSError as failure:
return Failure(f"could not read the transcript {path}: {failure.strerror}")
# The lines before it, such as the session's title, are the same for every chunk of a project.
def first_timestamped_line(raw: bytes) -> bytes:
lines = raw.splitlines()
return next((line for line in lines if "timestamp" in (parse_entry(line) or {})), lines[0] if lines else b"")
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)
elif (text := loop_text(entry)) is not None:
lines.append("[loop] " + 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, ensure_ascii=False)[:tool_input_chars]}"
return ""