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): 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 human_text(entry) is not None) def render(entries): 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): 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 ""