"""Reads Claude Code session transcripts (.jsonl), as written under ~/.claude/projects//. No model call.""" from __future__ import annotations import json from pathlib import Path from typing import Any USAGE_FIELDS = ("input_tokens", "cache_read_input_tokens", "cache_creation_input_tokens", "output_tokens") def is_agent_type(recorded: object, name: str) -> bool: """Whether a subagent's recorded agentType is the agent `name`, started bare ("okf-wiki-reader") or from a plugin, which Claude Code records with the plugin's namespace (":okf-wiki-reader").""" if not isinstance(recorded, str): return False plugin, sep, bare = recorded.rpartition(":") return bare == name and (not sep or bool(plugin)) def read_transcript(path: Path) -> list[dict[str, Any]]: """Every JSON object line of a transcript; a line that does not parse is skipped.""" lines = [] for raw in path.read_text(encoding="utf-8").splitlines(): try: line = json.loads(raw) except json.JSONDecodeError: continue if isinstance(line, dict): lines.append(line) return lines