From 711d13b71bd6a20fef53b2864c3f8893da95cdee Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Mon, 28 Sep 2026 20:21:01 +0200 Subject: [PATCH] Keep loop-claude's rendering out of the adapter and Claude Code's locations in it --- bin/loop_claude/claude_code.py | 31 ++++++++++++++++++------------- bin/loop_claude/paths.py | 6 +++--- bin/loop_claude/transcript.py | 18 ++++++++++++++---- bin/loop_claude/worker.py | 13 ++++++------- bin/test_loop_claude.py | 8 ++++---- 5 files changed, 45 insertions(+), 31 deletions(-) diff --git a/bin/loop_claude/claude_code.py b/bin/loop_claude/claude_code.py index 129600ce..0cba4f73 100644 --- a/bin/loop_claude/claude_code.py +++ b/bin/loop_claude/claude_code.py @@ -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"} + + # /-home-me-code-log/.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 "" -# .jsonl, /subagents/, .superseded-1789290815002/ +# .jsonl, /subagents/, and from older versions .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": "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): # /*.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"): diff --git a/bin/loop_claude/paths.py b/bin/loop_claude/paths.py index cdf05a48..ee6ef17e 100644 --- a/bin/loop_claude/paths.py +++ b/bin/loop_claude/paths.py @@ -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), ) diff --git a/bin/loop_claude/transcript.py b/bin/loop_claude/transcript.py index 4155d526..50cb7b99 100644 --- a/bin/loop_claude/transcript.py +++ b/bin/loop_claude/transcript.py @@ -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 "" diff --git a/bin/loop_claude/worker.py b/bin/loop_claude/worker.py index c86988d4..2688e1b8 100644 --- a/bin/loop_claude/worker.py +++ b/bin/loop_claude/worker.py @@ -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}") diff --git a/bin/test_loop_claude.py b/bin/test_loop_claude.py index 01f385f8..9ed8006f 100644 --- a/bin/test_loop_claude.py +++ b/bin/test_loop_claude.py @@ -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):