Gather every Claude Code format loop-claude reads into claude_code.py, verified on 2.1.284
This commit is contained in:
@@ -1,21 +1,7 @@
|
||||
import enum
|
||||
import json
|
||||
|
||||
from loop_claude.claude_code import content, parse, parts, said, typed
|
||||
from loop_claude.outcome import Failure
|
||||
|
||||
|
||||
class UserEntryKind(enum.Enum):
|
||||
"""Who wrote a "user" entry: the human wrote HUMAN, Claude Code every other kind."""
|
||||
|
||||
COMMAND = enum.auto()
|
||||
CTRL_C = enum.auto()
|
||||
EMPTY = enum.auto()
|
||||
HUMAN = enum.auto()
|
||||
META = enum.auto()
|
||||
NON_TEXT = enum.auto()
|
||||
SDK_PROMPT = enum.auto()
|
||||
|
||||
|
||||
def read(path):
|
||||
try:
|
||||
with open(path, "rb") as handle:
|
||||
@@ -24,45 +10,6 @@ def read(path):
|
||||
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 []
|
||||
|
||||
|
||||
# Claude Code 2.1.282 writes "user" entries of its own, a claude -p prompt among them.
|
||||
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))
|
||||
|
||||
@@ -76,13 +23,3 @@ def render(entries):
|
||||
text = content(entry)
|
||||
lines.append("[human] " + (text if isinstance(text, str) else "".join(part["text"] for part in text)))
|
||||
return "\n\n".join(lines)
|
||||
|
||||
|
||||
# An "assistant" entry's parts, alike in the transcript and in stream-json on Claude Code 2.1.282.
|
||||
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