import { accessSync, closeSync, constants, existsSync, openSync, readdirSync, readFileSync, readSync, statSync } from "node:fs"; import { homedir } from "node:os"; import { basename, join } from "node:path"; import { StringDecoder } from "node:string_decoder"; import { readUserText, sessionGoalSchema, stripHiddenContext, type ContentBlock, type PlanEntry, type SessionUpdate, type ToolCallContent, type ToolDetail, type ToolKind } from "@linkshell/wire"; import { inlineImage } from "../images.js"; import { nestUnder } from "../nesting.js"; import { ClaudeChildIndex, ownedFile } from "./children.js"; // Claude Code writes each session to ~/.claude/projects//.jsonl, // one JSON object per line. Assistant lines are one content block each // (thinking / text / tool_use) and share the API message id (`msg_…`); tool // results arrive in later user lines keyed by `tool_use_id`. These are the same // ids the ACP adapter stamps live, so both views produce the same item ids. type Json = Record; const str = (value: unknown): string | undefined => (typeof value === "string" ? value : undefined); const obj = (value: unknown): Json | undefined => value && typeof value === "object" && !Array.isArray(value) ? (value as Json) : undefined; export function claudeConfigDir(env: NodeJS.ProcessEnv = process.env): string { return env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude"); } /** Claude's project directory name for a cwd: every non-alphanumeric character becomes "-". */ export function encodeProjectDir(cwd: string): string { return cwd.replace(/[^a-zA-Z0-9]/g, "-"); } export function findTranscript(configDir: string, sessionId: string, cwd?: string): string | undefined { const projects = join(configDir, "projects"); if (cwd) { const direct = join(projects, encodeProjectDir(cwd), `${sessionId}.jsonl`); if (existsSync(direct)) return direct; } let dirs: string[]; try { dirs = readdirSync(projects); } catch { return undefined; } for (const dir of dirs) { const candidate = join(projects, dir, `${sessionId}.jsonl`); if (existsSync(candidate)) return candidate; } return undefined; } type TranscriptLocation = { status: "available"; path: string } | { status: "missing" | "unknown" }; function inspectTranscript(path: string): TranscriptLocation { try { if (!statSync(path).isFile()) return { status: "unknown" }; accessSync(path, constants.R_OK); return { status: "available", path }; } catch (error) { const code = (error as NodeJS.ErrnoException).code; return { status: code === "ENOENT" ? "missing" : "unknown" }; } } /** One directory scan serves a discovery batch, including sessions moved to another project. */ export class ClaudeTranscriptIndex { private snapshot?: { at: number; paths: Map; complete: boolean }; constructor(private readonly configDir: string) {} invalidate(): void { this.snapshot = undefined; } /** Every transcript: session id → its paths. */ all(): ReadonlyMap { return this.scan().paths; } locate(sessionId: string, cwd?: string): TranscriptLocation { const direct = cwd ? join(this.configDir, "projects", encodeProjectDir(cwd), `${sessionId}.jsonl`) : undefined; const first = direct ? inspectTranscript(direct) : undefined; if (first?.status === "available") { if (this.snapshot) { const paths = this.snapshot.paths.get(sessionId) ?? []; if (!paths.includes(first.path)) this.snapshot.paths.set(sessionId, [...paths, first.path]); } return first; } for (let attempt = 0; ; attempt++) { const snapshot = this.scan(); let uncertain = first?.status === "unknown" || !snapshot.complete; let moved = false; for (const path of snapshot.paths.get(sessionId) ?? []) { const found = path === direct ? first! : inspectTranscript(path); if (found.status === "available") return found; uncertain ||= found.status === "unknown"; moved ||= found.status === "missing"; } // A cached location disappearing may be a move, not a deleted session. if (moved && attempt === 0) { this.invalidate(); continue; } return { status: uncertain ? "unknown" : "missing" }; } } private scan(): NonNullable { if (this.snapshot && Date.now() - this.snapshot.at < 1000) return this.snapshot; const snapshot = { at: Date.now(), paths: new Map(), complete: true }; const projects = join(this.configDir, "projects"); try { for (const dir of readdirSync(projects, { withFileTypes: true })) { if (!dir.isDirectory() && !dir.isSymbolicLink()) continue; try { for (const entry of readdirSync(join(projects, dir.name), { withFileTypes: true })) { if (!entry.name.endsWith(".jsonl")) continue; const id = entry.name.slice(0, -6); const paths = snapshot.paths.get(id) ?? []; paths.push(join(projects, dir.name, entry.name)); snapshot.paths.set(id, paths); } } catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") snapshot.complete = false; } } } catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") snapshot.complete = false; } this.snapshot = snapshot; return snapshot; } } const FILE_TOOLS = new Set(["Read", "Write", "Edit", "MultiEdit", "NotebookEdit", "NotebookRead"]); function fileName(path: string | undefined): string { return path ? basename(path) : "file"; } /** A readable title and kind for a Claude Code tool call. */ export function describeClaudeTool(name: string, input: Json): { title: string; kind: ToolKind; detail?: ToolDetail } { const path = str(input.file_path) ?? str(input.notebook_path) ?? str(input.path); switch (name) { case "Read": case "NotebookRead": return { title: `Read ${fileName(path)}`, kind: "read" }; case "Write": return { title: `Write ${fileName(path)}`, kind: "edit" }; case "Edit": case "MultiEdit": case "NotebookEdit": return { title: `Edit ${fileName(path)}`, kind: "edit" }; case "Bash": return { title: str(input.description) ?? str(input.command) ?? "Run command", kind: "execute" }; case "Grep": return { title: `Search “${str(input.pattern) ?? ""}”${path ? ` in ${fileName(path)}` : ""}`, kind: "search" }; case "Glob": return { title: `Find ${str(input.pattern) ?? "files"}`, kind: "search" }; case "WebFetch": return { title: `Fetch ${str(input.url) ?? ""}`.trim(), kind: "fetch" }; case "WebSearch": return { title: `Search web: ${str(input.query) ?? ""}`.trim(), kind: "fetch", detail: { type: "web_search", query: str(input.query) } }; case "Task": case "Agent": return { title: `Agent: ${str(input.description) ?? str(input.subagent_type) ?? "task"}`, kind: "other", detail: { type: "subagent", action: "spawn", task: str(input.description) ?? str(input.prompt), agentType: str(input.subagent_type), name: str(input.name), model: str(input.model), }, }; case "Workflow": return { title: `Workflow: ${str(input.name) ?? str(input.description) ?? "工作流"}`, kind: "other", detail: { type: "subagent", action: "spawn", agentType: "工作流", task: str(input.description) ?? str(input.name), workflow: {} }, }; case "SendMessage": return { title: `Message ${str(input.to) ?? "agent"}`, kind: "other", detail: { type: "subagent", action: "message", task: str(input.summary) ?? str(input.message) }, }; case "TaskStop": case "KillShell": return { title: name === "TaskStop" ? "Stop agent" : "Stop shell", kind: "other", detail: name === "TaskStop" ? { type: "subagent", action: "stop" } : undefined }; case "AskUserQuestion": { const questions = Array.isArray(input.questions) ? input.questions.flatMap((entry) => str(obj(entry)?.question) ?? []) : []; const [text] = questions; // (Its input arrives after the call is announced: until then there is nothing to say about it.) if (!text) return { title: "Ask a question", kind: "other" }; return { title: `Ask: ${text}`, kind: "other", detail: { type: "question", text, more: questions.length > 1 ? questions.length - 1 : undefined } }; } case "Skill": { const skill = str(input.skill) ?? str(input.command) ?? "skill"; return { title: `Skill: ${skill}`, kind: "other", detail: { type: "skill", name: skill.replace(/^\//, "") } }; } default: { // MCP tools are named mcp____. const mcp = /^mcp__(.+?)__(.+)$/.exec(name); if (mcp) return { title: `${mcp[1]} · ${mcp[2]}`, kind: "other", detail: { type: "mcp", server: mcp[1]!, tool: mcp[2]! } }; return { title: name, kind: FILE_TOOLS.has(name) ? "edit" : "other" }; } } } function toolContent(name: string, input: Json): ToolCallContent[] | undefined { const path = str(input.file_path); if (!path) return undefined; if (name === "Write" && typeof input.content === "string") { return [{ type: "diff", path, oldText: null, newText: input.content }]; } if (name === "Edit" && typeof input.new_string === "string") { return [{ type: "diff", path, oldText: str(input.old_string) ?? null, newText: input.new_string }]; } if (name === "MultiEdit" && Array.isArray(input.edits)) { return input.edits.flatMap((raw): ToolCallContent[] => { const edit = obj(raw); return typeof edit?.new_string === "string" ? [{ type: "diff", path, oldText: str(edit.old_string) ?? null, newText: edit.new_string }] : []; }); } return undefined; } /** The last task list per transcript reader (its `hidden` set), for the updates that only move it on. */ const lastPlans = new WeakMap, { id: string; entries: PlanEntry[] }>(); /** * The Claude desktop app's task list (its `update_plan` tool, under any MCP server prefix): the * whole list (`items`), or a step on from the last one (`advance`, or `done` / `start` by number). * It is one list the app keeps under the latest message, worked through across turns, so it keeps * one id (the call that began it): moved on, or rewritten while it still has steps to do, it is * the same card. A new request's list (`first_task` / `new_task`), or any list after one whose * steps were all done, is a card of its own: the finished one stays where it was finished. */ function desktopPlan(input: Json, reader: Set | undefined, callId: string): { id: string; entries: PlanEntry[] } | undefined { const last = reader && lastPlans.get(reader); if (Array.isArray(input.items)) { const entries = input.items.flatMap((raw): PlanEntry[] => { const item = obj(raw); const content = str(item?.content); if (!content) return []; const status = item?.status === "completed" || item?.status === "in_progress" ? item.status : "pending"; return [{ content, status, priority: "medium" }]; }); const fresh = input.scope === "first_task" || input.scope === "new_task" || !!last?.entries.every((entry) => entry.status === "completed"); const plan = { id: last && !fresh ? last.id : callId, entries }; if (reader) lastPlans.set(reader, plan); return plan; } if (!last) return undefined; const next = last.entries.map((entry) => ({ ...entry })); if (input.advance === true) { const current = next.findIndex((entry) => entry.status === "in_progress"); if (current >= 0) next[current]!.status = "completed"; const following = next.findIndex((entry, index) => index > current && entry.status === "pending"); if (following >= 0) next[following]!.status = "in_progress"; } if (Array.isArray(input.done)) for (const number of input.done) if (typeof number === "number" && next[number - 1]) next[number - 1]!.status = "completed"; if (typeof input.start === "number" && next[input.start - 1]) { for (const entry of next) if (entry.status === "in_progress") entry.status = "pending"; next[input.start - 1]!.status = "in_progress"; } const plan = { id: last.id, entries: next }; lastPlans.set(reader!, plan); return plan; } function todoPlan(input: Json): PlanEntry[] | undefined { if (!Array.isArray(input.todos)) return undefined; return input.todos.flatMap((raw) => { const todo = obj(raw); const content = str(todo?.content); if (!content) return []; const status = todo?.status === "completed" || todo?.status === "in_progress" ? todo.status : "pending"; return [{ content, status, priority: "medium" as const }]; }); } /** Images in a tool result (screenshots, image files the agent read). */ function resultImages(content: unknown): ToolCallContent[] { if (!Array.isArray(content)) return []; return content.flatMap((raw) => { const block = obj(raw); if (block?.type !== "image") return []; const source = obj(block.source); const image = inlineImage(str(source?.data), str(source?.media_type)); return image ? [{ type: "content" as const, content: image }] : []; }); } function resultText(content: unknown): string | undefined { if (typeof content === "string") return content; if (!Array.isArray(content)) return undefined; const text = content .map((block) => (obj(block)?.type === "text" ? str(obj(block)?.text) ?? "" : "")) .filter(Boolean) .join("\n"); return text || undefined; } /** * A slash command the user typed (`/review`, a skill, a plugin command): Claude * records it as tagged text. Returns it as the user wrote it. */ function slashCommand(text: string): string | undefined { const name = /\s*([^<]+?)\s*<\/command-name>/.exec(text)?.[1]; if (!name) return undefined; const args = /([\s\S]*?)<\/command-args>/.exec(text)?.[1]?.trim(); const command = name.startsWith("/") ? name : `/${name}`; // As in the Claude app: a model switch and a compaction show as what they did, not as a message. if (command === "/model" || command === "/compact") return ""; return args ? `${command} ${args}` : command; } /** * Claude tells itself a background task (a sub-agent, a background shell) * finished by injecting `` as a user message. It's the * task's status, not something the user said. */ /** * A background task finished: its call gets its final status. For an agent * started in the background (one of `agents`) that is also the end of its * turn, and its report is what the call returned. */ function taskNotification(text: string, agents?: Set): SessionUpdate[] | undefined { if (!text.trimStart().startsWith("")) return undefined; const toolCallId = /\s*([^<\s]+)\s*<\/tool-use-id>/.exec(text)?.[1]; if (!toolCallId) return undefined; const status = /\s*([^<\s]+)\s*<\/status>/.exec(text)?.[1]; const done = status === "completed"; if (!agents?.delete(toolCallId)) return [{ sessionUpdate: "tool_call_update", toolCallId, status: done ? "completed" : "failed" }]; const report = /([\s\S]*?)<\/result>/.exec(text)?.[1]?.trim(); return [ { sessionUpdate: "tool_call_update", toolCallId, status: done ? "completed" : "failed", content: report ? [{ type: "content", content: { type: "text", text: report } }] : undefined, }, { sessionUpdate: "ls_turn", state: "ended", parentToolCallId: toolCallId, stopReason: done ? "end_turn" : status === "killed" || status === "stopped" ? "cancelled" : "error" }, ]; } /** Slash-command plumbing and interruption markers that aren't real user messages. */ /** * After compacting, Claude writes messages it keeps a second time under the * ids they already had — the ones just before, and what earlier compactions * kept, from anywhere in the transcript. A reader keeps the ids of the lines it * has read (`seen`) so those don't count as said or done again. */ function repeated(seen: Set, uuid: string | undefined): boolean { if (!uuid) return false; if (seen.has(uuid)) return true; seen.add(uuid); return false; } /** The context was compacted (`/compact`, or by itself when it ran out): a card saying so, with how much it freed. */ function compaction(line: Json): SessionUpdate[] { const uuid = str(line.uuid); if (line.subtype !== "compact_boundary" || !uuid || line.isSidechain === true) return []; const meta = obj(line.compactMetadata); return [ { sessionUpdate: "tool_call", toolCallId: `compact:${uuid}`, title: "Context compacted", kind: "think", status: "completed", detail: { type: "compaction" }, rawInput: meta ? { trigger: meta.trigger, preTokens: meta.preTokens, postTokens: meta.postTokens } : undefined, }, ]; } /** * What of a user block to keep: the text without what Claude added for itself (reminders, IDE * state, caveats), wherever in the block it is; nothing when that was all of it. Mail from other * sessions and agents stays: it is a message of its own. */ function keptText(text: string): string | undefined { const kept = stripHiddenContext(text, { keepPeers: true }); if (!kept || kept.startsWith("[Request interrupted by user")) return undefined; if (/^<(?:command-|task-notification>)/.test(kept)) return undefined; const peer = /<(?:cross-session-message|teammate-message|agent-message)\b/.test(kept); return peer || !readUserText(kept).hidden ? kept : undefined; } /** A shell command from the prompt (`!`) or a local command's output: in Claude's context, but not answered. */ const unanswered = (text: string) => /^<(?:bash-(?:input|stdout|stderr)|local-command-(?:stdout|stderr))>/.test(text); /** * A message the user typed while Claude was working. Claude folds it into the * running turn and records it as a `queued_command` attachment rather than a * user line; it's still something the user said. */ function queuedPrompt(line: Json, agents?: Set): TranscriptLineResult { const attachment = obj(line.attachment); if (attachment?.type !== "queued_command" || line.isSidechain === true) return { updates: [] }; const stamp = str(line.timestamp) ? Date.parse(str(line.timestamp)!) : Number.NaN; const ts = Number.isFinite(stamp) ? stamp : undefined; if (attachment.commandMode === "task-notification" && typeof attachment.prompt === "string") { // A background task reporting back; Claude answers it in a turn of its own. const notification = taskNotification(attachment.prompt, agents); return notification ? { updates: [...notification, { sessionUpdate: "ls_turn", state: "started" }], ts } : { updates: [] }; } if (attachment.commandMode !== undefined && attachment.commandMode !== "prompt") return { updates: [] }; // Typed by the person, or mail from another session or a sub-agent handing back its report. const origin = obj(attachment.origin)?.kind; if (origin !== undefined && origin !== "human" && origin !== "peer") return { updates: [] }; const messageId = str(attachment.source_uuid) ?? str(line.uuid); const prompt = attachment.prompt; const blocks: ContentBlock[] = []; if (typeof prompt === "string") { const text = keptText(prompt); if (text) blocks.push({ type: "text", text }); } else if (Array.isArray(prompt)) { for (const raw of prompt) { const block = obj(raw); const text = block?.type === "text" ? keptText(str(block.text) ?? "") : undefined; if (text) blocks.push({ type: "text", text }); if (block?.type === "image") { const source = obj(block.source); const image = inlineImage(str(source?.data), str(source?.media_type)); if (image) blocks.push(image); } } } return { updates: blocks.map((content) => ({ sessionUpdate: "user_message_chunk" as const, messageId, content })), ts }; } export interface TranscriptLineResult { updates: SessionUpdate[]; /** When the line was written (ms). */ ts?: number; /** A title set by /rename or generated by Claude. */ title?: string; } /** * Converts one transcript line to wire updates. Sub-agent files pass * `sidechain`; a reader keeps one `hidden` set per transcript, so a tool that * only feeds another view (TodoWrite → the plan) doesn't also show as a call, * and one `agents` set: the calls that started an agent in the background and * haven't heard back from it. */ export function transcriptLine( raw: string, options: { sidechain?: boolean; hidden?: Set; agents?: Set; seen?: Set } = {}, ): TranscriptLineResult { let line: Json; try { line = JSON.parse(raw) as Json; } catch { return { updates: [] }; } if (options.seen && repeated(options.seen, str(line.uuid))) return { updates: [] }; switch (line.type) { case "custom-title": return { updates: [], title: str(line.customTitle) }; case "ai-title": return { updates: [], title: str(line.aiTitle) }; case "user": case "assistant": break; case "attachment": { const attachment = obj(line.attachment); // Claude persists the same Goal check that the runtime reports live. // Sub-agent goals belong to their own session, never to the parent's card. if (!line.isSidechain && attachment?.type === "goal_status" && typeof attachment.met === "boolean") { if (attachment.met && attachment.sentinel === true) return { updates: [{ sessionUpdate: "ls_goal", goal: null }] }; const goal = sessionGoalSchema.safeParse({ objective: attachment.condition, status: attachment.met ? "complete" : "active", iterations: attachment.iterations, lastReason: attachment.reason, tokensUsed: attachment.tokens, timeUsedSeconds: typeof attachment.durationMs === "number" ? attachment.durationMs / 1000 : undefined, }); if (goal.success) return { updates: [{ sessionUpdate: "ls_goal", goal: goal.data }] }; } return queuedPrompt(line, options.agents); } case "system": return { updates: compaction(line) }; default: return { updates: [] }; } // What Claude writes for itself after compacting (the summary it continues from) isn't something the user said. if ((line.isSidechain === true && !options.sidechain) || line.isMeta === true || line.isCompactSummary === true) return { updates: [] }; const stamp = str(line.timestamp) ? Date.parse(str(line.timestamp)!) : Number.NaN; const ts = Number.isFinite(stamp) ? stamp : undefined; const message = obj(line.message); if (!message) return { updates: [] }; const updates: SessionUpdate[] = []; if (line.type === "user") { const uuid = str(line.uuid); const blocks = typeof message.content === "string" ? [{ type: "text", text: message.content }] : Array.isArray(message.content) ? message.content : []; let userText = false; for (const rawBlock of blocks) { const block = obj(rawBlock); if (!block) continue; if (block.type === "text" && typeof block.text === "string") { const notification = taskNotification(block.text, options.agents); if (notification) { updates.push(...notification); // Claude answers the notification in a turn of its own. userText = true; continue; } const command = slashCommand(block.text); if (command !== undefined) { if (command) { updates.push({ sessionUpdate: "user_message_chunk", messageId: uuid, content: { type: "text", text: command } }); userText = true; } continue; } if (block.text.trimStart().startsWith("[Request interrupted by user")) { // Esc at the desk (or a stop from a device): the turn is over. updates.push({ sessionUpdate: "ls_turn", state: "ended", stopReason: "cancelled" }); continue; } const text = keptText(block.text); if (!text) continue; updates.push({ sessionUpdate: "user_message_chunk", messageId: uuid, content: { type: "text", text } }); if (!unanswered(text)) userText = true; } else if (block.type === "image") { const source = obj(block.source); const content: ContentBlock = inlineImage(str(source?.data), str(source?.media_type)) ?? { type: "image", mimeType: str(source?.media_type) ?? "image/*" }; updates.push({ sessionUpdate: "user_message_chunk", messageId: uuid, content }); userText = true; } else if (block.type === "tool_result" && str(block.tool_use_id)) { if (options.hidden?.delete(str(block.tool_use_id)!)) continue; const launched = obj(line.toolUseResult ?? line.tool_use_result); const workflow = launched?.status === "async_launched" || launched?.status === "remote_launched"; const teammate = launched?.status === "teammate_spawned" && (str(launched.agentId) ?? str(launched.agent_id)); if (!block.is_error && !launched?.error && (teammate || (launched?.isAsync === true && str(launched.agentId)) || (workflow && str(launched?.taskId))) && !options.sidechain) { // An agent started in the background: the call returns at once (with // nothing to show) while the agent works on. Its end is a task notification. options.agents?.add(str(block.tool_use_id)!); updates.push({ sessionUpdate: "ls_turn", state: "started", parentToolCallId: str(block.tool_use_id)! }); continue; } const text = resultText(block.content); const content: ToolCallContent[] = [...(text ? [{ type: "content" as const, content: { type: "text" as const, text } }] : []), ...resultImages(block.content)]; updates.push({ sessionUpdate: "tool_call_update", toolCallId: str(block.tool_use_id)!, status: block.is_error === true ? "failed" : "completed", content: content.length > 0 ? content : undefined, }); } } // A real prompt starts a turn; tool results continue one. if (userText) updates.push({ sessionUpdate: "ls_turn", state: "started" }); return { updates, ts }; } const messageId = str(message.id) ?? str(line.uuid) ?? ""; // A request that failed is written as a reply of Claude's own (model ""): it is the // error, not something the model said, and goes out as one with Claude's code for it. const apiError = line.isApiErrorMessage === true; if (apiError) { const said = (Array.isArray(message.content) ? message.content : []).map((raw) => str(obj(raw)?.text) ?? "").join("").trim(); const code = str(line.error); if (said) updates.push({ sessionUpdate: "ls_error", code: !code || code === "unknown" ? "api_error" : code, message: said }); } for (const rawBlock of apiError ? [] : Array.isArray(message.content) ? message.content : []) { const block = obj(rawBlock); if (!block) continue; if (block.type === "thinking" && typeof block.thinking === "string" && block.thinking) { updates.push({ sessionUpdate: "agent_thought_chunk", messageId, content: { type: "text", text: block.thinking } }); } else if (block.type === "text" && typeof block.text === "string" && block.text) { updates.push({ sessionUpdate: "agent_message_chunk", messageId, content: { type: "text", text: block.text } }); } else if (block.type === "tool_use" && str(block.id) && str(block.name)) { const input = obj(block.input) ?? {}; if (block.name === "TodoWrite" || /(^|__)update_plan$/.test(str(block.name)!)) { const desktop = block.name === "TodoWrite" ? undefined : desktopPlan(input, options.hidden, str(block.id)!); const entries = block.name === "TodoWrite" ? todoPlan(input) : desktop?.entries; if (entries) updates.push({ sessionUpdate: "plan", entries, ...(desktop ? { planId: desktop.id } : {}) }); // The plan card is the view; its result line is skipped by id. Without a // reader's set (a single line) the call still shows, and closes cleanly. if (entries && options.hidden) { options.hidden.add(str(block.id)!); continue; } } const { title, kind, detail } = describeClaudeTool(str(block.name)!, input); updates.push({ sessionUpdate: "tool_call", toolCallId: str(block.id)!, title, // Claude's own name for the tool: Grep and Glob, Write and Edit share a kind. name: str(block.name), kind, detail, status: "in_progress", rawInput: input, content: toolContent(str(block.name)!, input), }); } } if (!apiError && !options.sidechain && message.stop_reason !== null) { const usage = obj(message.usage); if (usage) { const count = (value: unknown) => (typeof value === "number" && Number.isFinite(value) && value >= 0 ? Math.trunc(value) : 0); const inputTokens = count(usage.input_tokens); const outputTokens = count(usage.output_tokens); const cachedReadTokens = typeof usage.cache_read_input_tokens === "number" ? count(usage.cache_read_input_tokens) : undefined; const cachedWriteTokens = typeof usage.cache_creation_input_tokens === "number" ? count(usage.cache_creation_input_tokens) : undefined; const contextTokens = inputTokens + (cachedReadTokens ?? 0) + (cachedWriteTokens ?? 0); const totalTokens = contextTokens + outputTokens; if (totalTokens > 0) { updates.push({ sessionUpdate: "usage_update", usedTokens: contextTokens > 0 ? contextTokens : totalTokens, ...(messageId ? { accounting: { id: `claude:${messageId}`, scope: "turn", ...(str(message.model) ? { model: str(message.model) } : {}), ...(ts !== undefined ? { timestamp: ts } : {}), }, } : {}), tokens: { totalTokens, inputTokens, outputTokens, ...(cachedReadTokens !== undefined ? { cachedReadTokens } : {}), ...(cachedWriteTokens !== undefined ? { cachedWriteTokens } : {}), }, }); } } } // Anything but a tool call (or a reply still being written) is the turn's last word. const stop = message.stop_reason; if (stop === "end_turn" || stop === "stop_sequence") updates.push({ sessionUpdate: "ls_turn", state: "ended", stopReason: "end_turn" }); else if (stop === "max_tokens" || stop === "refusal") updates.push({ sessionUpdate: "ls_turn", state: "ended", stopReason: stop }); return { updates, ts }; } /** Parses a whole transcript file. */ /** When each transcript update happened, for history import. */ export const transcriptTimes = new WeakMap(); /** * Claude Code keeps each sub-agent's transcript next to the session: * `/subagents/agent-.jsonl`. The Agent/Task result links its id * to the spawning call; ordinary agents also carry that call in .meta.json. * Returns the sub-agents' work as * updates nested under those calls (their prompt is the call's own input). */ export function readSubagents(transcriptPath: string, index?: ClaudeChildIndex): SessionUpdate[] { const root = transcriptPath.replace(/\.jsonl$/, ""); const dir = join(root, "subagents"); if (!existsSync(dir)) return []; if (!index) { index = new ClaudeChildIndex(); eachLine(transcriptPath, (raw) => index!.observe(raw)); } const updates: SessionUpdate[] = []; for (const file of readdirSync(dir)) { if (!file.startsWith("agent-") || !file.endsWith(".jsonl")) continue; let parent = index.children.get(file.slice(6, -6))?.call; try { const metaPath = join(dir, file.replace(/\.jsonl$/, ".meta.json")); if (ownedFile(root, metaPath)) parent ??= str(obj(JSON.parse(readFileSync(metaPath, "utf8")))?.toolUseId); } catch { // A launch result can associate the child before its sidecar exists. } const transcript = join(dir, file); if (!parent || !ownedFile(root, transcript)) continue; const hidden = new Set(); const seen = new Set(); for (const raw of readFileSync(transcript, "utf8").split("\n")) { if (!raw.trim()) continue; const result = transcriptLine(raw, { sidechain: true, hidden, seen }); for (const update of result.updates) { const nested = nestUnder(update, parent); if (!nested) continue; if (result.ts !== undefined) transcriptTimes.set(nested, result.ts); updates.push(nested); } } } // Parallel sub-agents: one timeline, in time order (sort is stable within each). return updates.sort((a, b) => (transcriptTimes.get(a) ?? 0) - (transcriptTimes.get(b) ?? 0)); } /** Interleaves sub-agent updates into the session's by time, keeping each list's order. */ export function mergeByTime(main: SessionUpdate[], nested: SessionUpdate[]): SessionUpdate[] { if (nested.length === 0) return main; const out: SessionUpdate[] = []; let j = 0; let lastMain = Number.NEGATIVE_INFINITY; for (const update of main) { const ts = transcriptTimes.get(update) ?? lastMain; while (j < nested.length && (transcriptTimes.get(nested[j]!) ?? Number.POSITIVE_INFINITY) < ts) out.push(nested[j++]!); out.push(update); lastMain = ts; } while (j < nested.length) out.push(nested[j++]!); return out; } /** What a session is set to, as its transcript shows: each reply records the model and effort it ran with, each prompt the permission mode. */ export interface ObservedSettings { model?: string; effort?: string; /** Claude's permission mode id ("default", "acceptEdits", "plan", "bypassPermissions", …). */ mode?: string; fast?: boolean; } /** The settings one transcript line shows, if any (matched on the raw line: quotes inside message text are escaped there). */ export function settingsOf(raw: string): ObservedSettings | undefined { if (raw.includes('"isSidechain":true')) return undefined; if (raw.includes('"type":"assistant"')) { const model = /"model":"([^"]+)"/.exec(raw)?.[1]; if (!model || model === "") return undefined; const speed = /"speed":"([a-z]+)"/.exec(raw)?.[1]; return { model, effort: /"effort":"([a-z]+)"/.exec(raw)?.[1], fast: speed ? speed === "fast" : undefined }; } if (raw.includes('"type":"user"')) { const mode = /"permissionMode":"([A-Za-z]+)"/.exec(raw)?.[1]; return mode ? { mode } : undefined; } return undefined; } /** `next` laid over `current`, leaving out what `next` doesn't say; undefined when nothing changed. */ export function mergeSettings(current: ObservedSettings, next: ObservedSettings | undefined): ObservedSettings | undefined { if (!next) return undefined; let merged: ObservedSettings | undefined; for (const key of ["model", "effort", "mode", "fast"] as const) { if (next[key] !== undefined && next[key] !== current[key]) merged = { ...(merged ?? current), [key]: next[key] }; } return merged; } export function readTranscript(path: string, options: { includeSubagents?: boolean; onLine?: (raw: string) => void } = {}): { updates: SessionUpdate[]; title?: string; size: number; settings: ObservedSettings; agents: Set; /** The ids of its lines, for whoever reads on from `size`. */ seen: Set; } { let title: string | undefined; let settings: ObservedSettings = {}; const updates: SessionUpdate[] = []; const hidden = new Set(); const agents = new Set(); const seen = new Set(); const children = new ClaudeChildIndex(); const size = eachLine(path, (raw) => { if (!raw.trim()) return; options.onLine?.(raw); if (options.includeSubagents !== false) children.observe(raw); const result = transcriptLine(raw, { hidden, agents, seen }); // The latest reply and prompt say what the session is using. settings = mergeSettings(settings, settingsOf(raw)) ?? settings; if (result.ts !== undefined) for (const update of result.updates) transcriptTimes.set(update, result.ts); updates.push(...result.updates); if (result.title) title = result.title; }); return { updates: mergeByTime(updates, options.includeSubagents === false ? [] : readSubagents(path, children)), title, settings, size, agents, seen }; } /** * Calls `onLine` for every complete line of a file, reading it a piece at a * time (a long session's transcript runs to hundreds of MB). Returns how many * bytes those lines take: where a tail picks up. */ function eachLine(path: string, onLine: (line: string) => void): number { const fd = openSync(path, "r"); const buffer = Buffer.alloc(4 * 1024 * 1024); const decoder = new StringDecoder("utf8"); let carry = ""; let consumed = 0; try { for (;;) { const read = readSync(fd, buffer, 0, buffer.length, null); if (read === 0) break; const text = carry + decoder.write(buffer.subarray(0, read)); const end = text.lastIndexOf("\n"); if (end < 0) { carry = text; continue; } carry = text.slice(end + 1); const lines = text.slice(0, end + 1); consumed += Buffer.byteLength(lines); for (const line of lines.split("\n")) onLine(line); } } finally { closeSync(fd); } return consumed; } /** * Where a fork "through the turn of `itemId`" ends: the uuid of the last * message of that turn. The item is a reply's message id or a prompt's uuid; * when it isn't in the transcript, `turn` (counted from 1) names the turn. */ export function turnEndUuid(path: string, itemId: string, turn: number): string | undefined { let current = 0; let picked: number | undefined; const lastOfTurn: (string | undefined)[] = []; eachLine(path, (raw) => { if (!raw.trim() || raw.includes('"isSidechain":true')) return; if (!/"type":"(user|assistant)"/.test(raw)) return; let line: { type?: string; uuid?: string; message?: { id?: string } }; try { line = JSON.parse(raw) as typeof line; } catch { return; } if (line.type !== "user" && line.type !== "assistant") return; const starts = transcriptLine(raw).updates.some((update) => update.sessionUpdate === "ls_turn" && update.state === "started" && !update.parentToolCallId); if (starts) current += 1; if (line.uuid) lastOfTurn[current] = line.uuid; if (line.uuid === itemId || line.message?.id === itemId) picked = current; }); return lastOfTurn[picked ?? turn]; } /** The last `bytes` of a file, as text (the first line may be partial). */ export function readTail(path: string, bytes: number): string { const size = statSync(path).size; return readRange(path, Math.max(0, size - bytes), size); } function readRange(path: string, start: number, end: number): string { return readBytes(path, start, end).toString("utf8"); } function readBytes(path: string, start: number, end: number): Buffer { if (end <= start) return Buffer.alloc(0); const fd = openSync(path, "r"); try { const buffer = Buffer.alloc(end - start); let read = 0; while (read < buffer.length) { const n = readSync(fd, buffer, read, buffer.length - read, start + read); if (n === 0) break; read += n; } return buffer.subarray(0, read); } finally { closeSync(fd); } } /** How much of a transcript's start Claude's session list (the Agent SDK) reads for the session's cwd. */ export const LISTED_HEAD_BYTES = 64 * 1024; /** How far into a transcript to look for its cwd: the SDK's own bound for looking through one. */ const CWD_SCAN_BYTES = 16 * 1024 * 1024; const CWD_KEY = Buffer.from('"cwd":"'); /** The first cwd a transcript names, found as the SDK finds it, and the byte just past it. */ export function firstCwd(path: string, limit = CWD_SCAN_BYTES): { cwd: string; end: number } | undefined { const size = statSync(path).size; // Usually the first message has it; one with a large picture can put it megabytes in. for (const bytes of [LISTED_HEAD_BYTES, 1024 * 1024, limit]) { const start = readBytes(path, 0, Math.min(size, bytes)); const at = start.indexOf(CWD_KEY); if (at >= 0) { let end = at + CWD_KEY.length; // (`"` and `\` never occur inside a multibyte UTF-8 character.) while (end < start.length && start[end] !== 0x22) end += start[end] === 0x5c ? 2 : 1; if (end < start.length) { try { return { cwd: JSON.parse(`"${start.toString("utf8", at + CWD_KEY.length, end)}"`) as string, end: end + 1 }; } catch { return undefined; } } } if (bytes >= size) break; } return undefined; } /** * Transcripts whose cwd lies past what Claude's session list reads, so the * list can't place their sessions and the adapter leaves them out: a first * prompt with a large picture or paste does it. Remembered per file, since a * transcript only grows and what its start says stays. */ export class ClaudeLateCwds { private readonly known = new Map(); /** The cwd of the transcript at `path`, `size` bytes long, when the list can't find it. */ get(path: string, size: number): string | undefined { const known = this.known.get(path); if (known && (known.settled ? size >= known.size : size === known.size)) return known.cwd; let found: ReturnType; try { found = firstCwd(path); } catch { return undefined; } const cwd = found && found.end > LISTED_HEAD_BYTES ? found.cwd : undefined; // None yet can mean its first message is still being written. this.known.set(path, { size, cwd, settled: !!found || size >= CWD_SCAN_BYTES }); return cwd; } } /** * Follows a transcript from a byte offset by polling (Claude only appends). * `locate` is retried until the file exists, for sessions with no messages yet. */ export class TranscriptTail { private offset: number; private carry = ""; private timer?: ReturnType; private paused = false; private path?: string; /** While catching up after a pause: lines not to emit. */ private skip?: (line: string) => boolean; /** While paused: how far `writtenMeanwhile` has looked. */ private looked = 0; constructor( private readonly options: { locate: () => string | undefined; offset: number; onLine: (line: string) => void; pollMs?: number; }, ) { this.offset = options.offset; } start(): void { if (this.timer) return; this.timer = setInterval(() => this.poll(), this.options.pollMs ?? 400); this.timer.unref?.(); } stop(): void { if (this.timer) clearInterval(this.timer); this.timer = undefined; } /** Stops emitting (the remote driver is writing); see `resumeSkipping` and `resumeAtEnd`. */ pause(): void { this.poll(); this.paused = true; this.looked = this.offset; } /** While paused: whether a line `match` recognises was written since the pause (or since the last look). */ writtenMeanwhile(match: (line: string) => boolean): boolean { if (!this.paused) return false; const path = this.resolve(); if (!path) return false; let size: number; try { size = statSync(path).size; } catch { return false; } if (size <= this.looked) return false; const chunk = readRange(path, this.looked, size); const end = chunk.lastIndexOf("\n"); if (end < 0) return false; this.looked += Buffer.byteLength(chunk.slice(0, end + 1)); return chunk.slice(0, end).split("\n").some(match); } /** * Resumes after `pause`: what was written meanwhile is emitted, except the * lines `skip` recognises (the remote driver's own, already shown live). */ resumeSkipping(skip: (line: string) => boolean): void { if (!this.paused) return; this.paused = false; this.skip = skip; this.poll(); // A line still being written when we looked gets the same check once it's whole. this.skip = this.carry ? skip : undefined; } resumeAtEnd(): void { const path = this.resolve(); this.offset = path ? statSync(path).size : this.offset; this.carry = ""; this.paused = false; } /** Reads whatever is new right now. */ poll(): void { if (this.paused) return; const path = this.resolve(); if (!path) return; let size: number; try { size = statSync(path).size; } catch { return; } if (size < this.offset) { // Rewritten (rare): start over. this.offset = 0; this.carry = ""; } if (size === this.offset) return; const chunk = this.carry + readRange(path, this.offset, size); this.offset = size; const end = chunk.lastIndexOf("\n"); if (end < 0) { this.carry = chunk; return; } this.carry = chunk.slice(end + 1); const skip = this.skip; this.skip = undefined; for (const line of chunk.slice(0, end).split("\n")) { if (line.trim() && !skip?.(line)) this.options.onLine(line); } } private resolve(): string | undefined { if (!this.path) this.path = this.options.locate(); return this.path; } }