Keep loop-claude's rendering out of the adapter and Claude Code's locations in it

This commit is contained in:
2026-09-28 20:21:01 +02:00
parent 938eaf69fc
commit 711d13b71b
5 changed files with 45 additions and 31 deletions
+18 -13
View File
@@ -19,13 +19,17 @@ class UserEntryKind(enum.Enum):
SDK_PROMPT = enum.auto()
def locations(home):
return {"claude_json": home / ".claude.json", "model_catalog": home / ".claude" / "cache" / "model-catalog", "projects": home / ".claude" / "projects"}
# <projects>/-home-me-code-log/<session id>.jsonl
def transcript_path(projects, session_id):
hit = glob.glob(os.path.join(glob.escape(str(projects)), "*", f"{session_id}.jsonl"))
return hit[0] if hit else ""
# <session id>.jsonl, <session id>/subagents/, <session id>.superseded-1789290815002/
# <session id>.jsonl, <session id>/subagents/, and from older versions <session id>.superseded-1789290815002/
def session_files(transcript):
return glob.glob(glob.escape(transcript.removesuffix(".jsonl")) + "*")
@@ -39,18 +43,21 @@ def parse(raw):
return entry if isinstance(entry, dict) else None
# {"type": "user", "message": {"role": "user", "content": "merge it"}}
def content(entry):
message = entry.get("message")
return message.get("content") if isinstance(message, dict) else None
# {"type": "assistant", "message": {"content": [{"type": "text", "text": "ok"}]}}
def parts(entry):
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):
return parts(entry) if entry.get("type") == "assistant" else []
def user_entry_kind(entry):
# {"type": "user", "isMeta": true, "message": {"content": "<local-command-caveat>Caveat: ..."}}
if entry.get("isMeta"):
@@ -77,15 +84,14 @@ def typed(entry):
return entry.get("type") == "user" and user_entry_kind(entry) is UserEntryKind.HUMAN
# An "assistant" entry's part, alike in the transcript and in stream-json:
# {"type": "text", "text": "ok"}, {"type": "tool_use", "name": "Bash", "input": {"command": "echo hi"}}
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 ""
# {"type": "text", "text": "ok"}
def part_text(part):
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):
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", ...}
@@ -116,7 +122,6 @@ def mark_trusted(projects, roots):
# <model catalog>/*.json: {"fetchedAt": 1790618085238, "catalog": {"config": {"models": [{"id": "claude-opus-5-5", "name": "Opus 5.5", ...}]}}}
# The newest file has the highest fetchedAt.
def catalog(folder):
newest = {}
for path in folder.glob("*.json"):
+3 -3
View File
@@ -1,6 +1,8 @@
import dataclasses
import pathlib
from loop_claude.claude_code import locations
@dataclasses.dataclass(frozen=True)
class Paths:
@@ -16,9 +18,7 @@ def paths_from(home, env):
claude = home / ".claude"
return Paths(
archive=pathlib.Path(env["LOOP_CLAUDE_TRANSCRIPTS"]).expanduser() if env.get("LOOP_CLAUDE_TRANSCRIPTS") else claude / "loop-claude-transcripts",
claude_json=home / ".claude.json",
code=home / "code",
model_catalog=claude / "cache" / "model-catalog",
models_file=claude / "loop-claude-models.json",
projects=claude / "projects",
**locations(home),
)
+14 -4
View File
@@ -1,4 +1,6 @@
from loop_claude.claude_code import content, parse, parts, said, typed
import json
from loop_claude.claude_code import assistant_parts, content, parse, part_text, tool_call, typed
from loop_claude.outcome import Failure
@@ -17,9 +19,17 @@ def human_turns(entries):
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):
lines += [line for part in assistant_parts(entry) if (line := said(part))]
if 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):
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 ""
+6 -7
View File
@@ -1,7 +1,8 @@
import subprocess
from loop_claude.claude_code import assistant_parts, ends_stream, parse
from loop_claude.outcome import Failure
from loop_claude.claude_code import ends_stream, parse, parts, said
from loop_claude.transcript import said
def run_worker(root, args):
@@ -13,12 +14,10 @@ def run_worker(root, args):
done = False
for raw in proc.stdout:
event = parse(raw) or {}
if ends_stream(event):
done = True
elif event.get("type") == "assistant":
for part in parts(event):
if line := said(part, 160):
print(line, flush=True)
done = done or ends_stream(event)
for part in assistant_parts(event):
if line := said(part, 160):
print(line, flush=True)
code = proc.wait()
if code < 0:
return Failure(f"claude killed by signal {-code}")
+4 -4
View File
@@ -58,10 +58,10 @@ class HumanTurns(unittest.TestCase):
class Said(unittest.TestCase):
def test_renders_text_and_tool_calls_and_nothing_else(self):
self.assertEqual(claude_code.said({"type": "text", "text": "hi"}), "hi")
self.assertEqual(claude_code.said({"type": "text", "text": " \n"}), "")
self.assertEqual(claude_code.said({"type": "tool_use", "name": "Bash", "input": {"command": "x" * 500}}, 20), '[tool Bash] {"command": "xxxxxxx')
self.assertEqual(claude_code.said("not a part"), "")
self.assertEqual(transcript.said({"type": "text", "text": "hi"}), "hi")
self.assertEqual(transcript.said({"type": "text", "text": " \n"}), "")
self.assertEqual(transcript.said({"type": "tool_use", "name": "Bash", "input": {"command": "x" * 500}}, 20), '[tool Bash] {"command": "xxxxxxx')
self.assertEqual(transcript.said("not a part"), "")
class Parse(unittest.TestCase):