95 lines
4.0 KiB
Python
95 lines
4.0 KiB
Python
"""Entries of Claude Code's transcripts and of its stream-json and json output."""
|
|
import json
|
|
|
|
|
|
def parse(raw: str | bytes) -> dict | None:
|
|
try:
|
|
entry = json.loads(raw)
|
|
except ValueError:
|
|
return None
|
|
return entry if isinstance(entry, dict) else None
|
|
|
|
|
|
def content(entry: dict) -> object:
|
|
message = entry.get("message")
|
|
return message.get("content") if isinstance(message, dict) else None
|
|
|
|
|
|
def parts(entry: dict) -> list:
|
|
found = content(entry)
|
|
return found if isinstance(found, list) else []
|
|
|
|
|
|
# Alike in the transcript and in stream-json: {"type": "assistant", "message": {"content": [{"type": "text", "text": "ok"}]}}
|
|
def assistant_parts(entry: dict) -> list:
|
|
return parts(entry) if entry.get("type") == "assistant" else []
|
|
|
|
|
|
def written_by_human(entry: dict) -> bool:
|
|
"""Whether the human wrote a "user" entry; Claude Code writes the rest."""
|
|
if tagged_as_injected(entry):
|
|
return False
|
|
text = content(entry)
|
|
return string_by_human(entry, text) if isinstance(text, str) else blocks_by_human(parts(entry))
|
|
|
|
|
|
# {"type": "user", "isMeta": true, "message": {"content": "<local-command-caveat>Caveat: ..."}}
|
|
# {"type": "user", "promptSource": "sdk", "message": {"content": "Continue the task; ..."}}
|
|
# {"type": "user", "promptSource": "system", "message": {"content": "<task-notification>..."}}
|
|
def tagged_as_injected(entry: dict) -> bool:
|
|
return bool(entry.get("isMeta")) or entry.get("promptSource") in ("sdk", "system")
|
|
|
|
|
|
# {"type": "user", "promptSource": "typed", "message": {"content": "merge it"}}
|
|
# {"type": "user", "message": {"content": "<command-name>/model</command-name>\n..."}}
|
|
def string_by_human(entry: dict, text: str) -> bool:
|
|
# Without promptSource, nothing else says who wrote the entry, so a leading < counts as Claude Code's.
|
|
return entry.get("promptSource") == "typed" or not text.startswith("<")
|
|
|
|
|
|
# {"type": "user", "message": {"content": [{"type": "text", "text": "merge it"}]}}
|
|
# {"type": "user", "message": {"content": [{"tool_use_id": "toolu_01…", "type": "tool_result", "content": "hi"}]}}
|
|
# {"type": "user", "message": {"content": [{"type": "text", "text": "[Request interrupted by user]"}]}}
|
|
def blocks_by_human(blocks: list) -> bool:
|
|
texts = [part_text(block) for block in blocks]
|
|
return bool(texts) and all(text is not None and not text.startswith("[Request interrupted") for text in texts)
|
|
|
|
|
|
def human_text(entry: dict) -> str | None:
|
|
"""What the human wrote in the entry, or None where Claude Code wrote it."""
|
|
if (queued := queued_human_text(entry)) is not None:
|
|
return queued
|
|
if entry.get("type") != "user" or not written_by_human(entry):
|
|
return None
|
|
text = content(entry)
|
|
return text if isinstance(text, str) else "".join(part_text(part) or "" for part in parts(entry))
|
|
|
|
|
|
# What the human types while claude works, written only as this entry:
|
|
# {"type": "attachment", "attachment": {"type": "queued_command", "commandMode": "prompt", "origin": {"kind": "human"}, "prompt": "B"}}
|
|
def queued_human_text(entry: dict) -> str | None:
|
|
attachment = entry.get("attachment") if entry.get("type") == "attachment" else None
|
|
if not isinstance(attachment, dict) or attachment.get("type") != "queued_command" or not isinstance(origin := attachment.get("origin"), dict) or origin.get("kind") != "human":
|
|
return None
|
|
return prompt if isinstance(prompt := attachment.get("prompt"), str) else None
|
|
|
|
|
|
# {"type": "text", "text": "ok"}
|
|
def part_text(part: object) -> str | None:
|
|
return part["text"] if isinstance(part, dict) and part.get("type") == "text" and isinstance(part.get("text"), str) else None
|
|
|
|
|
|
# {"type": "tool_use", "name": "Bash", "input": {"command": "echo hi"}}
|
|
def tool_call(part: object) -> tuple[object, object] | None:
|
|
return (part.get("name"), part.get("input")) if isinstance(part, dict) and part.get("type") == "tool_use" else None
|
|
|
|
|
|
# stream-json's last line: {"type": "result", "subtype": "success", ...}
|
|
def ends_stream(event: dict) -> bool:
|
|
return event.get("type") == "result"
|
|
|
|
|
|
# {"type": "result", "structured_output": {"evidence": "...", ...}, ...}
|
|
def structured_output(stdout: str) -> object:
|
|
return (parse(stdout) or {}).get("structured_output")
|