26 lines
761 B
Python
26 lines
761 B
Python
from loop_claude.claude_code import content, parse, parts, said, 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:
|
|
if entry.get("type") == "assistant":
|
|
lines += [line for part in parts(entry) if (line := said(part))]
|
|
elif 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)
|