Keep loop-claude's rendering out of the adapter and Claude Code's locations in it
This commit is contained in:
@@ -19,13 +19,17 @@ class UserEntryKind(enum.Enum):
|
|||||||
SDK_PROMPT = enum.auto()
|
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
|
# <projects>/-home-me-code-log/<session id>.jsonl
|
||||||
def transcript_path(projects, session_id):
|
def transcript_path(projects, session_id):
|
||||||
hit = glob.glob(os.path.join(glob.escape(str(projects)), "*", f"{session_id}.jsonl"))
|
hit = glob.glob(os.path.join(glob.escape(str(projects)), "*", f"{session_id}.jsonl"))
|
||||||
return hit[0] if hit else ""
|
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):
|
def session_files(transcript):
|
||||||
return glob.glob(glob.escape(transcript.removesuffix(".jsonl")) + "*")
|
return glob.glob(glob.escape(transcript.removesuffix(".jsonl")) + "*")
|
||||||
|
|
||||||
@@ -39,18 +43,21 @@ def parse(raw):
|
|||||||
return entry if isinstance(entry, dict) else None
|
return entry if isinstance(entry, dict) else None
|
||||||
|
|
||||||
|
|
||||||
# {"type": "user", "message": {"role": "user", "content": "merge it"}}
|
|
||||||
def content(entry):
|
def content(entry):
|
||||||
message = entry.get("message")
|
message = entry.get("message")
|
||||||
return message.get("content") if isinstance(message, dict) else None
|
return message.get("content") if isinstance(message, dict) else None
|
||||||
|
|
||||||
|
|
||||||
# {"type": "assistant", "message": {"content": [{"type": "text", "text": "ok"}]}}
|
|
||||||
def parts(entry):
|
def parts(entry):
|
||||||
found = content(entry)
|
found = content(entry)
|
||||||
return found if isinstance(found, list) else []
|
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):
|
def user_entry_kind(entry):
|
||||||
# {"type": "user", "isMeta": true, "message": {"content": "<local-command-caveat>Caveat: ..."}}
|
# {"type": "user", "isMeta": true, "message": {"content": "<local-command-caveat>Caveat: ..."}}
|
||||||
if entry.get("isMeta"):
|
if entry.get("isMeta"):
|
||||||
@@ -77,15 +84,14 @@ def typed(entry):
|
|||||||
return entry.get("type") == "user" and user_entry_kind(entry) is UserEntryKind.HUMAN
|
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": "text", "text": "ok"}, {"type": "tool_use", "name": "Bash", "input": {"command": "echo hi"}}
|
def part_text(part):
|
||||||
def said(part, limit=400):
|
return part["text"] if isinstance(part, dict) and part.get("type") == "text" and isinstance(part.get("text"), str) else None
|
||||||
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"]
|
# {"type": "tool_use", "name": "Bash", "input": {"command": "echo hi"}}
|
||||||
if kind == "tool_use":
|
def tool_call(part):
|
||||||
return f"[tool {part.get('name')}] {json.dumps(part.get('input'))[:limit]}"
|
return (part.get("name"), part.get("input")) if isinstance(part, dict) and part.get("type") == "tool_use" else None
|
||||||
return ""
|
|
||||||
|
|
||||||
|
|
||||||
# stream-json's last line: {"type": "result", "subtype": "success", ...}
|
# 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", ...}]}}}
|
# <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):
|
def catalog(folder):
|
||||||
newest = {}
|
newest = {}
|
||||||
for path in folder.glob("*.json"):
|
for path in folder.glob("*.json"):
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import dataclasses
|
import dataclasses
|
||||||
import pathlib
|
import pathlib
|
||||||
|
|
||||||
|
from loop_claude.claude_code import locations
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True)
|
@dataclasses.dataclass(frozen=True)
|
||||||
class Paths:
|
class Paths:
|
||||||
@@ -16,9 +18,7 @@ def paths_from(home, env):
|
|||||||
claude = home / ".claude"
|
claude = home / ".claude"
|
||||||
return Paths(
|
return Paths(
|
||||||
archive=pathlib.Path(env["LOOP_CLAUDE_TRANSCRIPTS"]).expanduser() if env.get("LOOP_CLAUDE_TRANSCRIPTS") else claude / "loop-claude-transcripts",
|
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",
|
code=home / "code",
|
||||||
model_catalog=claude / "cache" / "model-catalog",
|
|
||||||
models_file=claude / "loop-claude-models.json",
|
models_file=claude / "loop-claude-models.json",
|
||||||
projects=claude / "projects",
|
**locations(home),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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
|
from loop_claude.outcome import Failure
|
||||||
|
|
||||||
|
|
||||||
@@ -17,9 +19,17 @@ def human_turns(entries):
|
|||||||
def render(entries):
|
def render(entries):
|
||||||
lines = []
|
lines = []
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
if entry.get("type") == "assistant":
|
lines += [line for part in assistant_parts(entry) if (line := said(part))]
|
||||||
lines += [line for part in parts(entry) if (line := said(part))]
|
if typed(entry):
|
||||||
elif typed(entry):
|
|
||||||
text = content(entry)
|
text = content(entry)
|
||||||
lines.append("[human] " + (text if isinstance(text, str) else "".join(part["text"] for part in text)))
|
lines.append("[human] " + (text if isinstance(text, str) else "".join(part["text"] for part in text)))
|
||||||
return "\n\n".join(lines)
|
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 ""
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
from loop_claude.claude_code import assistant_parts, ends_stream, parse
|
||||||
from loop_claude.outcome import Failure
|
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):
|
def run_worker(root, args):
|
||||||
@@ -13,12 +14,10 @@ def run_worker(root, args):
|
|||||||
done = False
|
done = False
|
||||||
for raw in proc.stdout:
|
for raw in proc.stdout:
|
||||||
event = parse(raw) or {}
|
event = parse(raw) or {}
|
||||||
if ends_stream(event):
|
done = done or ends_stream(event)
|
||||||
done = True
|
for part in assistant_parts(event):
|
||||||
elif event.get("type") == "assistant":
|
if line := said(part, 160):
|
||||||
for part in parts(event):
|
print(line, flush=True)
|
||||||
if line := said(part, 160):
|
|
||||||
print(line, flush=True)
|
|
||||||
code = proc.wait()
|
code = proc.wait()
|
||||||
if code < 0:
|
if code < 0:
|
||||||
return Failure(f"claude killed by signal {-code}")
|
return Failure(f"claude killed by signal {-code}")
|
||||||
|
|||||||
@@ -58,10 +58,10 @@ class HumanTurns(unittest.TestCase):
|
|||||||
|
|
||||||
class Said(unittest.TestCase):
|
class Said(unittest.TestCase):
|
||||||
def test_renders_text_and_tool_calls_and_nothing_else(self):
|
def test_renders_text_and_tool_calls_and_nothing_else(self):
|
||||||
self.assertEqual(claude_code.said({"type": "text", "text": "hi"}), "hi")
|
self.assertEqual(transcript.said({"type": "text", "text": "hi"}), "hi")
|
||||||
self.assertEqual(claude_code.said({"type": "text", "text": " \n"}), "")
|
self.assertEqual(transcript.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(transcript.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("not a part"), "")
|
||||||
|
|
||||||
|
|
||||||
class Parse(unittest.TestCase):
|
class Parse(unittest.TestCase):
|
||||||
|
|||||||
Reference in New Issue
Block a user