Move the transcript, the session and the worker into their own modules
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import enum
|
||||
import json
|
||||
|
||||
from loop_claude.outcome import Failure
|
||||
|
||||
|
||||
class UserEntryKind(enum.Enum):
|
||||
"""Who wrote a "user" entry: the human wrote HUMAN, Claude Code every other kind; the value is the tell."""
|
||||
|
||||
COMMAND = "string content starting with <"
|
||||
CTRL_C = "a text part starting with [Request interrupted"
|
||||
EMPTY = "no content"
|
||||
HUMAN = "typed by the human"
|
||||
META = "isMeta"
|
||||
NON_TEXT = "a part that is not text, such as a tool result"
|
||||
SDK_PROMPT = "promptSource sdk: the claude -p prompt"
|
||||
|
||||
|
||||
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 parse(raw):
|
||||
try:
|
||||
entry = json.loads(raw)
|
||||
except ValueError:
|
||||
return None
|
||||
return entry if isinstance(entry, dict) else None
|
||||
|
||||
|
||||
def content(entry):
|
||||
message = entry.get("message")
|
||||
return message.get("content") if isinstance(message, dict) else None
|
||||
|
||||
|
||||
def parts(entry):
|
||||
found = content(entry)
|
||||
return found if isinstance(found, list) else []
|
||||
|
||||
|
||||
def user_entry_kind(entry):
|
||||
if entry.get("isMeta"):
|
||||
return UserEntryKind.META
|
||||
if entry.get("promptSource") == "sdk":
|
||||
return UserEntryKind.SDK_PROMPT
|
||||
if isinstance(text := content(entry), str):
|
||||
return UserEntryKind.COMMAND if text.startswith("<") else UserEntryKind.HUMAN
|
||||
if not parts(entry):
|
||||
return UserEntryKind.EMPTY
|
||||
if any(not isinstance(part, dict) or part.get("type") != "text" or not isinstance(part.get("text"), str) for part in parts(entry)):
|
||||
return UserEntryKind.NON_TEXT
|
||||
if any(part["text"].startswith("[Request interrupted") for part in parts(entry)):
|
||||
return UserEntryKind.CTRL_C
|
||||
return UserEntryKind.HUMAN
|
||||
|
||||
|
||||
def typed(entry):
|
||||
return entry.get("type") == "user" and user_entry_kind(entry) is UserEntryKind.HUMAN
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
def said(part, limit=400):
|
||||
kind = part.get("type") if isinstance(part, dict) else None
|
||||
if kind == "text" and isinstance(part.get("text"), str) and part["text"].strip():
|
||||
return part["text"]
|
||||
if kind == "tool_use":
|
||||
return f"[tool {part.get('name')}] {json.dumps(part.get('input'))[:limit]}"
|
||||
return ""
|
||||
Reference in New Issue
Block a user