Annotate the return type of every function in loop_claude
This commit is contained in:
@@ -3,7 +3,7 @@ import json
|
||||
from loop_claude.claude_code.formats import models_in
|
||||
|
||||
|
||||
def offered_models(folder):
|
||||
def offered_models(folder) -> list[tuple[str, object]]:
|
||||
files = []
|
||||
for path in folder.glob("*.json"):
|
||||
try:
|
||||
|
||||
@@ -4,7 +4,7 @@ import json
|
||||
from loop_claude.outcome import Failure
|
||||
|
||||
|
||||
def parse(raw):
|
||||
def parse(raw) -> dict | None:
|
||||
try:
|
||||
entry = json.loads(raw)
|
||||
except ValueError:
|
||||
@@ -12,22 +12,22 @@ def parse(raw):
|
||||
return entry if isinstance(entry, dict) else None
|
||||
|
||||
|
||||
def content(entry):
|
||||
def content(entry) -> object:
|
||||
message = entry.get("message")
|
||||
return message.get("content") if isinstance(message, dict) else None
|
||||
|
||||
|
||||
def parts(entry):
|
||||
def parts(entry) -> list:
|
||||
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):
|
||||
def assistant_parts(entry) -> list:
|
||||
return parts(entry) if entry.get("type") == "assistant" else []
|
||||
|
||||
|
||||
def written_by_human(entry):
|
||||
def written_by_human(entry) -> bool:
|
||||
"""Whether the human wrote a "user" entry; Claude Code writes the rest."""
|
||||
# {"type": "user", "isMeta": true, "message": {"content": "<local-command-caveat>Caveat: ..."}}
|
||||
# {"type": "user", "promptSource": "sdk", "message": {"content": "Continue the task; ..."}}
|
||||
@@ -46,7 +46,7 @@ def written_by_human(entry):
|
||||
return bool(texts) and None not in texts and not any(text.startswith("[Request interrupted") for text in texts)
|
||||
|
||||
|
||||
def human_text(entry):
|
||||
def human_text(entry) -> str | None:
|
||||
"""What the human wrote in the entry, or None where Claude Code wrote it."""
|
||||
if (queued := queued_human_text(entry)) is not None:
|
||||
return queued
|
||||
@@ -58,7 +58,7 @@ def human_text(entry):
|
||||
|
||||
# What the human types while claude works, written only as this entry:
|
||||
# {"type": "attachment", "attachment": {"type": "queued_command", "commandMode": "prompt", "origin": {"kind": "human"}, "prompt": "B"}}
|
||||
def queued_human_text(entry):
|
||||
def queued_human_text(entry) -> str | None:
|
||||
attachment = entry.get("attachment") if entry.get("type") == "attachment" else None
|
||||
if not isinstance(attachment, dict) or attachment.get("type") != "queued_command" or not isinstance(origin := attachment.get("origin"), dict) or origin.get("kind") != "human":
|
||||
return None
|
||||
@@ -66,27 +66,27 @@ def queued_human_text(entry):
|
||||
|
||||
|
||||
# {"type": "text", "text": "ok"}
|
||||
def part_text(part):
|
||||
def part_text(part) -> str | None:
|
||||
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):
|
||||
def tool_call(part) -> tuple[object, object] | None:
|
||||
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", ...}
|
||||
def ends_stream(event):
|
||||
def ends_stream(event) -> bool:
|
||||
return event.get("type") == "result"
|
||||
|
||||
|
||||
# {"type": "result", "structured_output": {"evidence": "...", ...}, ...}
|
||||
def structured_output(stdout):
|
||||
def structured_output(stdout) -> object:
|
||||
return (parse(stdout) or {}).get("structured_output")
|
||||
|
||||
|
||||
# Claude Code's config: {"projects": {"/home/me/code/log": {"hasTrustDialogAccepted": true, ...}}, ...}
|
||||
def mark_trusted(config, roots):
|
||||
def mark_trusted(config, roots) -> tuple[dict, list[str]] | Failure:
|
||||
projects = config.get("projects", {}) if isinstance(config, dict) else None
|
||||
if not isinstance(projects, dict):
|
||||
return Failure('not an object whose "projects" is an object')
|
||||
@@ -98,7 +98,7 @@ def mark_trusted(config, roots):
|
||||
|
||||
|
||||
# A model catalog file: {"fetchedAt": 1790618085238, "catalog": {"config": {"models": [{"id": "claude-opus-5-5", "name": "Opus 5.5", ...}]}}}
|
||||
def models_in(files):
|
||||
def models_in(files) -> list[tuple[str, object]]:
|
||||
"""The (id, name) of each model in the newest of the parsed catalog files."""
|
||||
newest = {}
|
||||
for data in files:
|
||||
|
||||
@@ -17,19 +17,19 @@ class Session:
|
||||
name: str
|
||||
|
||||
|
||||
def session_for(project, paths):
|
||||
def session_for(project, paths) -> Session:
|
||||
# "A " is the human's cue in the session picker that the session is the loop's.
|
||||
name = f"A {project} loop-claude"
|
||||
return Session(id=uuid.uuid5(NAMESPACE, name), claude_code_transcripts=paths.claude_code_transcripts, name=name)
|
||||
|
||||
|
||||
# <claude_code_transcripts>/-home-me-code-log/<session id>.jsonl
|
||||
def transcript_file(session):
|
||||
def transcript_file(session) -> str:
|
||||
hit = glob.glob(os.path.join(glob.escape(str(session.claude_code_transcripts)), "*", f"{session.id}.jsonl"))
|
||||
return hit[0] if hit else ""
|
||||
|
||||
|
||||
def delete_session(session):
|
||||
def delete_session(session) -> None:
|
||||
path = transcript_file(session)
|
||||
if not path:
|
||||
return
|
||||
|
||||
@@ -4,7 +4,7 @@ from loop_claude.claude_code.formats import assistant_parts, human_text, parse,
|
||||
from loop_claude.outcome import Failure
|
||||
|
||||
|
||||
def read(path):
|
||||
def read(path) -> list[dict] | Failure:
|
||||
try:
|
||||
with open(path, "rb") as handle:
|
||||
return [entry for raw in handle if (entry := parse(raw))]
|
||||
@@ -12,11 +12,11 @@ def read(path):
|
||||
return Failure(f"could not read the transcript {path}: {failure.strerror}")
|
||||
|
||||
|
||||
def human_turns(entries):
|
||||
def human_turns(entries) -> int:
|
||||
return sum(1 for entry in entries if human_text(entry) is not None)
|
||||
|
||||
|
||||
def render(entries):
|
||||
def render(entries) -> str:
|
||||
lines = []
|
||||
for entry in entries:
|
||||
lines += [line for part in assistant_parts(entry) if (line := part_line(part))]
|
||||
@@ -25,7 +25,7 @@ def render(entries):
|
||||
return "\n\n".join(lines)
|
||||
|
||||
|
||||
def part_line(part, limit=400):
|
||||
def part_line(part, limit=400) -> str:
|
||||
if (text := part_text(part)) and text.strip():
|
||||
return text
|
||||
if call := tool_call(part):
|
||||
|
||||
@@ -7,7 +7,7 @@ from loop_claude.outcome import Failure
|
||||
|
||||
|
||||
# A failure only warns: the interrupt still runs, and the human answers Claude Code's trust dialog.
|
||||
def trust(config, roots):
|
||||
def trust(config, roots) -> None:
|
||||
try:
|
||||
with open(config, encoding="utf-8") as handle:
|
||||
data = json.load(handle)
|
||||
|
||||
Reference in New Issue
Block a user