@khalilgharbaoui/opencode-claude-code-plugin 0.6.3 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/claude-code-language-model.ts","../src/logger.ts","../src/todo-ledger.ts","../src/tool-mapping.ts","../src/message-builder.ts","../src/mcp-bridge.ts","../src/tmp.ts","../src/runtime-status.ts","../src/session-manager.ts","../src/cli-version.ts","../src/proxy-mcp.ts","../src/proxy-broker.ts","../src/models.ts","../src/accounts.ts","../src/cleanup-stale.ts","../src/index.ts"],"sourcesContent":["import type {\n LanguageModelV3,\n LanguageModelV3CallOptions,\n LanguageModelV3Content,\n LanguageModelV3FinishReason,\n LanguageModelV3StreamPart,\n LanguageModelV3Usage,\n SharedV3Warning,\n} from \"@ai-sdk/provider\"\nimport { generateId } from \"@ai-sdk/provider-utils\"\nimport type {\n ClaudeCodeConfig,\n ControlRequestBehavior,\n ClaudeStreamMessage,\n ReasoningEffort,\n} from \"./types.js\"\nimport { mapTool } from \"./tool-mapping.js\"\nimport { applyTaskCreateToolResult } from \"./todo-ledger.js\"\nimport { getClaudeUserMessage } from \"./message-builder.js\"\nimport { bridgeOpencodeMcp, type RuntimeMcpStatus } from \"./mcp-bridge.js\"\nimport {\n getRuntimeMcpStatus,\n fetchOpencodeToolList,\n resolveSpawnCwd,\n} from \"./runtime-status.js\"\nimport {\n getActiveProcess,\n spawnClaudeProcess,\n buildCliArgs,\n setClaudeSessionId,\n getClaudeSessionId,\n deleteClaudeSessionId,\n deleteActiveProcess,\n claudeSpawnEnv,\n isClaudeThinkingDisabled,\n sessionKey,\n} from \"./session-manager.js\"\nimport { log } from \"./logger.js\"\nimport { detectCliVersion } from \"./cli-version.js\"\nimport {\n createProxyMcpServer,\n disallowedToolFlags,\n DEFAULT_PROXY_TOOLS,\n PROXY_TOOL_PREFIX,\n type ProxyMcpServer,\n type ProxyToolCall,\n type ProxyToolDef,\n type ProxyToolResult,\n} from \"./proxy-mcp.js\"\nimport {\n getPendingProxyCalls,\n onPendingProxyCall,\n queuePendingProxyCall,\n rejectAllPendingProxyCallsForSession,\n rejectPendingProxyCallById,\n resolvePendingProxyCallById,\n type PendingProxyCall,\n} from \"./proxy-broker.js\"\nimport { readFileSync, writeFileSync } from \"node:fs\"\nimport { unlink } from \"node:fs/promises\"\nimport { homedir, tmpdir } from \"node:os\"\nimport { randomUUID } from \"node:crypto\"\nimport { dirname, join } from \"node:path\"\n\n/**\n * Default model used for opencode `/compact`. Haiku 4.5 is fast\n * (~150 tok/s), has a hard 8k output cap that bounds latency, and is a\n * strong structured summarizer. Override per-project via the\n * `compactionModel` provider setting in opencode.json / opencode.jsonc,\n * or per-run via the `CLAUDE_CODE_COMPACTION_MODEL` env var (env wins).\n */\nexport const DEFAULT_COMPACTION_MODEL = \"claude-haiku-4-5\"\n\n/**\n * Pick the model used to handle /compact. Precedence:\n * 1. `CLAUDE_CODE_COMPACTION_MODEL` env var (per-process override)\n * 2. `configured` argument (the `compactionModel` provider setting)\n * 3. `DEFAULT_COMPACTION_MODEL`\n *\n * Exported as a free function so it can be unit-tested without\n * instantiating the language model class.\n */\nexport function resolveCompactionModel(configured?: string): string {\n const env = process.env.CLAUDE_CODE_COMPACTION_MODEL?.trim()\n if (env) return env\n const trimmed = configured?.trim()\n if (trimmed) return trimmed\n return DEFAULT_COMPACTION_MODEL\n}\n\n/**\n * Resolve the session affinity token for a given LLM call. The affinity\n * token is part of the session key in session-manager so two different\n * opencode sessions sharing the same cwd+model still get separate Claude\n * CLI processes.\n *\n * Priority:\n * 1. `x-session-affinity` request header (primary — opencode sets it for\n * third-party providers in packages/opencode/src/session/llm.ts).\n * 2. `opencodeSessionID` inside `providerOptions` (injected by the\n * `chat.params` hook in index.ts). Covers cases where the header is\n * absent: provider switch mid-session, title synthesis paths, older\n * opencode versions. opencode wraps `output.options` under the\n * providerID before passing it to the language model, so we look up\n * both the configured provider key and the canonical `\"claude-code\"`.\n * 3. `\"default\"` — safe fallback when neither source is available.\n *\n * Exported as a free function so it can be unit-tested without\n * instantiating the language model class.\n */\nexport function resolveSessionAffinity(\n headers: Record<string, string | undefined> | undefined,\n providerOptions: Record<string, unknown> | undefined,\n providerKey: string,\n): string {\n if (headers) {\n for (const key of Object.keys(headers)) {\n if (key.toLowerCase() === \"x-session-affinity\") {\n const v = headers[key]\n if (typeof v === \"string\" && v.length > 0) return v\n }\n }\n }\n if (providerOptions) {\n const bag =\n (providerOptions as any)[providerKey] ??\n (providerOptions as any)[\"claude-code\"]\n const sid = bag?.opencodeSessionID\n if (typeof sid === \"string\" && sid.length > 0) return sid\n }\n return \"default\"\n}\n\n/**\n * Stream delta types we handle explicitly. `signature_delta` is listed as\n * known-and-silent: it carries encrypted thinking-block signatures that\n * are opaque to clients (the server uses them to reconstitute thinking\n * across turns), so there's nothing for us to do but ignore it.\n */\nconst KNOWN_DELTA_TYPES = new Set([\n \"thinking_delta\",\n \"text_delta\",\n \"input_json_delta\",\n \"signature_delta\",\n])\n\n/**\n * True if the prompt has any user-side content after the last assistant\n * message (text, tool_result, or any user role entry). False when the\n * prompt ends with an assistant message and there is nothing for Claude\n * to respond to — opencode sometimes iterates the agent loop one more\n * time after a turn naturally completed; without short-circuiting we'd\n * spawn Claude CLI on an empty turn and the model would reply with a\n * stub like \"Did you mean to send a message?\".\n */\nexport function hasNewUserContent(\n prompt: LanguageModelV3CallOptions[\"prompt\"],\n): boolean {\n for (let i = prompt.length - 1; i >= 0; i--) {\n const msg = prompt[i]\n if (msg.role === \"assistant\") return false\n // Tool-result turns from opencode's outer loop arrive in `tool`-role\n // messages (AI SDK V3 shape). Treat any tool-result part as new\n // content so the short-circuit doesn't drop turns where opencode is\n // delivering the result for a still-pending proxy MCP call — letting\n // that fire `stop` is what was forcing the user to press \"continue\".\n if (msg.role === \"tool\") {\n const content: any = msg.content\n if (Array.isArray(content)) {\n for (const part of content as any[]) {\n if (part?.type === \"tool-result\") return true\n }\n }\n continue\n }\n if (msg.role !== \"user\") continue\n const content: any = msg.content\n if (typeof content === \"string\") {\n if (content.trim()) return true\n continue\n }\n if (Array.isArray(content)) {\n for (const part of content as any[]) {\n if (part.type === \"text\" && part.text && part.text.trim()) return true\n if (part.type === \"tool-result\") return true\n // Image/file-only user turns count as new input — without this the\n // short-circuit drops them as if the turn were empty.\n if (part.type === \"image\" || part.type === \"file\") return true\n }\n }\n }\n return false\n}\n\nconst AUTO_CONTINUE_MAX_ATTEMPTS = 8\nconst AUTO_CONTINUE_MAX_ELAPSED_MS = 10 * 60 * 1000\nconst AUTO_CONTINUE_NO_PROGRESS_LIMIT = 2\n\nconst AUTO_CONTINUE_PROMPT =\n \"Continue the task from where you stopped. Do not summarize; keep working until the requested task is complete, you need clarification, or you hit a real blocker.\"\n\ninterface AutoContinueState {\n enabled: boolean | \"smart\" | undefined\n attempts: number\n startedAt: number\n noProgressCount: number\n lastSignature?: string\n aborted?: boolean\n}\n\ninterface AutoContinueSnapshot {\n text: string\n /**\n * Text of the most recent assistant text block only. Used for final-answer\n * detection so mid-task narration like \"Implementing now. Updated the\n * search index.\" in an earlier block doesn't trip the keyword regex.\n */\n lastVisibleText: string\n hadReasoning: boolean\n hadToolActivity: boolean\n hadProxyActivity: boolean\n isError?: boolean\n /**\n * Protocol-level stop signal from the Claude API (forwarded by Claude\n * CLI). When present and non-empty, we trust it as authoritative — the\n * model itself signaled why the turn ended (`end_turn`, `max_tokens`,\n * `stop_sequence`, `refusal`, `pause_turn`, `tool_use`, etc.) — and stop\n * without running the keyword regex. The heuristic only runs as a\n * fallback when `stop_reason` is missing (older CLI versions, abrupt\n * termination).\n */\n stopReason?: string | null\n now?: number\n}\n\ninterface AutoContinueDecision {\n continue: boolean\n reason: string\n}\n\nfunction normalizeVisibleText(text: string): string {\n return text.replace(/\\s+/g, \" \").trim()\n}\n\n/** Tool names that mean \"ask the human a question\" (CLI casing variants). */\nfunction isAskUserQuestionTool(name: string | undefined): boolean {\n if (!name) return false\n const n = name.toLowerCase()\n return n === \"askuserquestion\" || n === \"ask_user_question\"\n}\n\n/**\n * Render Claude Code's `AskUserQuestion` tool input as visible markdown.\n *\n * opencode has no native structured ask-question executor to proxy this\n * through (unlike bash/task), so the question + every option is rendered\n * as readable assistant text and the user answers in the next turn —\n * same approach as the `ExitPlanMode` handling. The previous behavior\n * collapsed the whole payload to a single faint `_Asking: <q>_` line,\n * dropping all options and any question past the first.\n */\nfunction formatAskUserQuestion(input: Record<string, unknown>): string {\n const anyInput = input as any\n const questions: any[] = Array.isArray(anyInput?.questions)\n ? anyInput.questions\n : []\n\n if (questions.length === 0) {\n const single = anyInput?.question ?? anyInput?.text\n const q =\n typeof single === \"string\" && single.trim() ? single.trim() : \"Question?\"\n return `\\n\\n**${q}**\\n\\n_Reply with your answer to continue._\\n\\n`\n }\n\n const out: string[] = [\"\\n\\n\"]\n const multiQ = questions.length > 1\n questions.forEach((q, i) => {\n const text =\n (typeof q?.question === \"string\" && q.question.trim()) ||\n (typeof q?.text === \"string\" && q.text.trim()) ||\n \"Question?\"\n const header =\n typeof q?.header === \"string\" && q.header.trim() ? q.header.trim() : \"\"\n out.push(`**${multiQ ? `${i + 1}. ` : \"\"}${text}**`)\n if (header) out.push(` _(${header})_`)\n out.push(\"\\n\\n\")\n\n const options: any[] = Array.isArray(q?.options) ? q.options : []\n options.forEach((opt, j) => {\n const label =\n (typeof opt?.label === \"string\" && opt.label.trim()) ||\n (typeof opt === \"string\" && opt.trim()) ||\n `Option ${j + 1}`\n const desc =\n typeof opt?.description === \"string\" && opt.description.trim()\n ? ` — ${opt.description.trim()}`\n : \"\"\n out.push(`${j + 1}. **${label}**${desc}\\n`)\n })\n\n out.push(\n q?.multiSelect === true\n ? \"\\n_Select one or more — reply with the numbers or labels._\\n\\n\"\n : \"\\n_Reply with your choice (the number or label)._\\n\\n\",\n )\n })\n return out.join(\"\")\n}\n\nfunction looksLikeQuestion(text: string): boolean {\n const normalized = normalizeVisibleText(text).toLowerCase()\n if (!normalized) return false\n // v0.4.10 tweak 5a: '?' anywhere in the last block, not just trailing.\n // Catches long answers that pose a question mid-text then list options\n // and end with a period. FP risk on inline code (`result?.value`) is\n // accepted — cost is one extra \"continue\" press, in the safe direction.\n if (normalized.includes(\"?\")) return true\n // v0.4.11 additions: ready when you are / standing by / i'll stand by /\n // let me know when. These are awaiting-input idioms with no '?'. The\n // \"standing by\" addition has historical significance — it's the exact\n // stub phrase Claude CLI emits on empty turns that commit 49345e3 was\n // designed to suppress at the message-builder layer. This adds a second\n // line of defense at the model-output layer for cases where the model\n // organically produces the same idiom.\n //\n // v0.4.12 additions: over to you / your turn / all yours / let me know\n // how / i'm here. Defensive coverage of soft-proceed idioms in the\n // model's vocabulary. \"i'm here\" has the highest FP risk (\"I'm here to\n // help with X\" is a conversational opener) but cost of FP is one extra\n // continue press — safe direction.\n return /\\b(please confirm|can you confirm|should i|would you like|do you want|which option|choose|pick one|need your|need you to|what would you like|let me know if|let me know whether|let me know what|let me know when|let me know how|if you'?d like|if you want to|tell me if|tell me which|tell me whether|say (?:go|yes|no)|push back|sign off|sounds? (?:good|right)|your call|your move|your turn|over to you|all yours|up to you|ready to (?:ship|go|proceed|merge)|ready (?:when|whenever|once|if) you|standing by|i'?ll stand ?by|i'?m here|happy to (?:ship|go|proceed|merge))\\b/.test(normalized)\n}\n\nfunction looksLikeBlocker(text: string): boolean {\n const normalized = normalizeVisibleText(text).toLowerCase()\n if (!normalized) return false\n // v0.4.10 tweak 3: 'needs your' / 'needs you to' / 'action required'\n // are intent-equivalent to 'requires your' but use the verb-with-s form.\n return /\\b(blocked|blocker|cannot proceed|can't proceed|unable to proceed|need clarification|need more information|permission denied|failed and needs|requires your|needs your|needs you to|action required|manual step|required from you)\\b/.test(normalized)\n}\n\nfunction looksLikeFinalAnswer(text: string): boolean {\n const normalized = normalizeVisibleText(text).toLowerCase()\n if (looksLikeQuestion(normalized) || looksLikeBlocker(normalized)) return false\n // v0.4.15: strong-completion phrases bypass the 30-char length floor.\n // These are unambiguous end-of-turn signals at any text length — even\n // a short standalone \"We're done.\" should stop.\n if (/\\b(we'?re done|we are done|all done|all set)\\b/.test(normalized)) {\n return true\n }\n // v0.4.10 tweak 4: floor lowered 40 → 30 chars. Catches short clean\n // completions like \"Task is now completely done. Pushed.\" (36 chars)\n // while keeping a buffer against ambiguous short narration.\n if (normalized.length < 30) return false\n // v0.4.15: keyword list extended with deploy/ship verbs the model\n // routinely uses at turn end (shipped, deployed, merged, tagged, live,\n // pinned). FP risk highest on \"live\" — \"live data\" mid-turn could match\n // — but cost of FP is one extra continue press, safe direction.\n return /\\b(done|completed|fixed|implemented|verified|published|released|sent|delivered|updated|shipped|deployed|merged|tagged|live|pinned)\\b/.test(normalized) ||\n // v0.4.15: also accept present-tense \"tests pass\" / \"checks pass\".\n // Real fire 03:31 ended in \"78/78 tests pass\" — past-tense-only regex\n // missed it.\n /\\b(checks?|tests?) (?:pass|passes|passed)\\b/.test(normalized) ||\n /\\b(summary|what changed|verification)\\b/.test(normalized)\n}\n\nfunction continuationSignature(snapshot: AutoContinueSnapshot): string {\n const text = normalizeVisibleText(snapshot.text).slice(-500)\n return JSON.stringify({\n text,\n reasoning: snapshot.hadReasoning,\n tools: snapshot.hadToolActivity,\n proxy: snapshot.hadProxyActivity,\n })\n}\n\nexport function shouldAutoContinueIncompleteTurn(\n state: AutoContinueState,\n snapshot: AutoContinueSnapshot,\n): AutoContinueDecision {\n if (state.enabled === false) return { continue: false, reason: \"disabled\" }\n if (snapshot.isError) return { continue: false, reason: \"error\" }\n if (state.aborted) return { continue: false, reason: \"aborted\" }\n // v0.4.17: trust ANY protocol-level stop_reason as authoritative. If\n // Claude CLI emitted a stop_reason value at all, the model has signaled\n // a stop — honor it without consulting the keyword heuristic. The\n // heuristic only runs as a fallback when stop_reason is missing (older\n // CLI versions / edge cases). Maps snake_case → kebab-case for reason\n // label consistency with other reasons.\n if (snapshot.stopReason) {\n return {\n continue: false,\n reason: snapshot.stopReason.replace(/_/g, \"-\"),\n }\n }\n if (state.attempts >= AUTO_CONTINUE_MAX_ATTEMPTS) {\n return { continue: false, reason: \"max-attempts\" }\n }\n const now = snapshot.now ?? Date.now()\n if (now - state.startedAt > AUTO_CONTINUE_MAX_ELAPSED_MS) {\n return { continue: false, reason: \"max-elapsed\" }\n }\n\n const text = normalizeVisibleText(snapshot.text)\n const lastText = normalizeVisibleText(snapshot.lastVisibleText)\n if (looksLikeQuestion(text)) return { continue: false, reason: \"question\" }\n if (looksLikeBlocker(text)) return { continue: false, reason: \"blocker\" }\n // Final-answer detection runs on the most recent text block only. Earlier\n // blocks may contain mid-task narration that would false-positive the\n // keyword regex; the model's actual \"I'm done\" sentence is in the last\n // block before result/end_turn.\n if (looksLikeFinalAnswer(lastText)) {\n return { continue: false, reason: \"final-answer\" }\n }\n\n const hadActivity =\n snapshot.hadReasoning || snapshot.hadToolActivity || snapshot.hadProxyActivity\n if (!hadActivity) return { continue: false, reason: \"no-activity\" }\n\n const signature = continuationSignature(snapshot)\n const noProgress = signature === state.lastSignature\n if (noProgress && state.noProgressCount + 1 >= AUTO_CONTINUE_NO_PROGRESS_LIMIT) {\n return { continue: false, reason: \"no-progress\" }\n }\n\n if (!text) {\n return { continue: true, reason: \"activity-without-visible-answer\" }\n }\n\n return { continue: true, reason: \"non-final-progress\" }\n}\n\nfunction makeAutoContinueMessage(): string {\n return JSON.stringify({\n type: \"user\",\n message: {\n role: \"user\",\n content: [{ type: \"text\", text: AUTO_CONTINUE_PROMPT }],\n },\n })\n}\n\nfunction readPromptFileIfPresent(path: string): string | undefined {\n try {\n const content = readFileSync(path, \"utf8\").trim()\n return content || undefined\n } catch {\n return undefined\n }\n}\n\nfunction nearestWorkspaceAgentsPrompt(cwd: string): string | undefined {\n let dir = cwd\n while (true) {\n const content = readPromptFileIfPresent(join(dir, \"AGENTS.md\"))\n if (content) return content\n const parent = dirname(dir)\n if (parent === dir) return undefined\n dir = parent\n }\n}\n\nconst AGENTS_MAINTENANCE_HINT = `## Keeping AGENTS.md up to date\n\nWhen you complete a task, phase, or to-do item that is listed in AGENTS.md, update the file\nimmediately after the work is done — mark it ✅, check it off, or remove it. Do this inside\nthe same turn so the next session does not repeat work that is already finished.`\n\nconst MULTI_STEP_TASK_HINT = `## Continuing through multi-step tasks\n\nopencode requires the user to press \"continue\" after each turn ends. When a\ntask has multiple steps, do them all in one turn — chain tool calls rather\nthan pausing for user confirmation between subtasks. End the turn only\nwhen the task is done, you need clarification on intent, or you hit a real\nblocker. The user can interrupt or abort at any time; turn endings should\nmark meaningful checkpoints, not every completed substep.`\n\n/**\n * Prepended to every appended system prompt so Claude knows which\n * context-management tools exist in the Claude CLI runtime versus a\n * direct API provider. DCP and similar plugins forward compress/distill/\n * prune instructions via system.transform; those reach us through\n * extractSystemMessages, but the tools themselves are not available in\n * the CLI environment. Without this note Claude wastes thinking cycles\n * searching for tools that don't exist.\n */\nconst CLAUDE_CLI_CONTEXT_NOTE = `## Runtime environment: Claude Code CLI\n\nYou are running via the Claude Code CLI (not a direct API call). This affects context management:\n\n- The \\`compress\\` tool is NOT available. Do not attempt to call it.\n- The \\`distill\\`, \\`prune\\`, and \\`extract\\` tools are NOT available.\n- Context window management is handled automatically by Claude CLI's own session history.\n- Ignore any system instructions that tell you to call \\`compress\\` — they are intended for direct API providers, not this environment.\n- DCP context injections (AGENTS.md, dynamic state) arrive via the system prompt and are already applied.`\n\n/**\n * Extract text content from all `system`-role messages in the prompt.\n * Standard API providers forward these as the `system` parameter; for\n * Claude CLI, the only equivalent path is --append-system-prompt-file.\n * Plugins like opencode-dcp inject AGENTS.md and other context via\n * system-role messages and would otherwise be silently dropped.\n */\nfunction extractSystemMessages(\n prompt: LanguageModelV3CallOptions[\"prompt\"],\n): string[] {\n const out: string[] = []\n for (const msg of prompt) {\n if (msg.role !== \"system\") continue\n if (typeof msg.content === \"string\") {\n if (msg.content.trim()) out.push(msg.content.trim())\n } else if (Array.isArray(msg.content)) {\n for (const part of msg.content as any[]) {\n if (\n part?.type === \"text\" &&\n typeof part.text === \"string\" &&\n part.text.trim()\n ) {\n out.push(part.text.trim())\n }\n }\n }\n }\n return out\n}\n\nfunction buildAppendedSystemPrompt(\n cwd: string,\n includeMultiStepHint = true,\n extraSystemContent: string[] = [],\n): string | undefined {\n const parts: string[] = []\n parts.push(CLAUDE_CLI_CONTEXT_NOTE)\n for (const s of extraSystemContent) {\n if (s.trim()) parts.push(s.trim())\n }\n const configRoot =\n process.env.XDG_CONFIG_HOME ?? join(homedir(), \".config\")\n const globalAgents = readPromptFileIfPresent(join(configRoot, \"opencode\", \"AGENTS.md\"))\n const workspaceAgents = nearestWorkspaceAgentsPrompt(cwd)\n\n if (globalAgents) parts.push(globalAgents)\n if (workspaceAgents && workspaceAgents !== globalAgents) parts.push(workspaceAgents)\n if (globalAgents || workspaceAgents) parts.push(AGENTS_MAINTENANCE_HINT)\n if (includeMultiStepHint) parts.push(MULTI_STEP_TASK_HINT)\n\n const content = parts.join(\"\\n\\n\")\n if (!content) return undefined\n\n const path = join(tmpdir(), `opencode-cc-sys-${randomUUID()}.md`)\n try {\n writeFileSync(path, content, \"utf8\")\n return path\n } catch (err) {\n log.warn(\"failed to write system prompt file\", { error: String(err) })\n return undefined\n }\n}\n\nexport class ClaudeCodeLanguageModel implements LanguageModelV3 {\n readonly specificationVersion = \"v3\"\n readonly modelId: string\n private readonly config: ClaudeCodeConfig\n\n constructor(modelId: string, config: ClaudeCodeConfig) {\n this.modelId = modelId\n this.config = config\n }\n\n readonly supportedUrls: Record<string, RegExp[]> = {}\n\n get provider(): string {\n return this.config.provider\n }\n\n private toUsage(rawUsage?: ClaudeStreamMessage[\"usage\"]): LanguageModelV3Usage {\n // Prefer the last iteration's counters over cumulative totals.\n // CLI usage is the sum across all internal tool-use iterations;\n // using it directly inflates context size and triggers premature compaction.\n const iter = rawUsage?.iterations\n const effective = iter?.length ? iter[iter.length - 1] : rawUsage\n // Claude CLI reports input_tokens as non-cached input only.\n // OpenCode expects total = noCache + cacheRead + cacheWrite.\n const noCache = effective?.input_tokens ?? 0\n const cacheRead = effective?.cache_read_input_tokens ?? 0\n const cacheWrite = effective?.cache_creation_input_tokens ?? 0\n return {\n inputTokens: {\n total: noCache + cacheRead + cacheWrite,\n noCache,\n cacheRead: cacheRead || undefined,\n cacheWrite: cacheWrite || undefined,\n },\n outputTokens: {\n total: effective?.output_tokens,\n text: effective?.output_tokens,\n reasoning: undefined,\n },\n raw: rawUsage as any,\n }\n }\n\n private toFinishReason(\n reason: \"stop\" | \"tool-calls\" = \"stop\",\n ): LanguageModelV3FinishReason {\n return {\n unified: reason,\n raw: reason,\n }\n }\n\n private requestScope(options: { tools?: unknown }): \"tools\" | \"no-tools\" {\n const tools = options?.tools\n if (Array.isArray(tools)) return \"tools\"\n if (tools && typeof tools === \"object\") {\n return Object.keys(tools as Record<string, unknown>).length > 0\n ? \"tools\"\n : \"no-tools\"\n }\n return \"no-tools\"\n }\n\n /**\n * Build the combined `--mcp-config` list and return both the list and the\n * hash of the bridged opencode MCP block (or null when bridging is off /\n * yields nothing). The hash is used to detect mid-session config changes\n * and respawn the underlying claude process.\n *\n * `runtimeStatus` is a snapshot of opencode's `client.mcp.status()`. When\n * provided it overlays opencode's UI-toggled state on top of disk config\n * so `/mcps` toggles propagate without a config file write.\n */\n private effectiveMcpConfig(\n cwd: string,\n proxyConfigPath?: string,\n runtimeStatus?: RuntimeMcpStatus,\n excludeServers?: ReadonlySet<string>,\n ): {\n paths: string[]\n bridgedHash: string | null\n allEnabledServerNames: string[]\n } {\n const paths = Array.isArray(this.config.mcpConfig)\n ? this.config.mcpConfig.slice()\n : this.config.mcpConfig\n ? [this.config.mcpConfig]\n : []\n let bridgedHash: string | null = null\n let allEnabledServerNames: string[] = []\n if (this.config.bridgeOpencodeMcp !== false) {\n const bridged = bridgeOpencodeMcp(cwd, runtimeStatus, excludeServers)\n if (bridged) {\n if (bridged.path) paths.push(bridged.path)\n bridgedHash = bridged.hash\n allEnabledServerNames = bridged.allEnabledServerNames\n }\n }\n if (proxyConfigPath) paths.push(proxyConfigPath)\n return { paths, bridgedHash, allEnabledServerNames }\n }\n\n /** Resolve ProxyToolDef[] for the configured proxyTools names. */\n private resolvedProxyTools(): ProxyToolDef[] | null {\n const names = this.config.proxyTools\n if (!names || names.length === 0) return null\n const defsByName = new Map(\n DEFAULT_PROXY_TOOLS.map((t) => [t.name.toLowerCase(), t]),\n )\n const picked: ProxyToolDef[] = []\n for (const n of names) {\n const def = defsByName.get(String(n).toLowerCase())\n if (def) picked.push(def)\n }\n return picked.length > 0 ? picked : null\n }\n\n /**\n * Resolve ProxyToolDef[] for opencode's MCP-bridged tools so they go\n * through the in-process proxy instead of being bridged into Claude CLI's\n * `--mcp-config`. Direct bridging causes double execution because both\n * Claude CLI's own MCP child and opencode hold their own connection to\n * the same server; routing through the proxy keeps a single execution\n * site (opencode). Returns null when the feature is disabled, the SDK\n * client is unavailable, or no MCP servers are configured.\n */\n private async resolvedProxyMcpTools(\n allEnabledServerNames: string[],\n ): Promise<ProxyToolDef[] | null> {\n if (this.config.proxyOpencodeMcpTools === false) return null\n if (this.config.bridgeOpencodeMcp === false) return null\n if (allEnabledServerNames.length === 0) return null\n\n const items = await fetchOpencodeToolList(\n this.config.provider,\n this.modelId,\n this.config.cwd,\n )\n if (!items || items.length === 0) return null\n\n // opencode names MCP tools `<server>_<originalToolName>`. Match the\n // longest server name prefix first so e.g. `slack_intl_*` resolves to\n // server `slack_intl` not `slack`.\n const serversByLengthDesc = [...allEnabledServerNames].sort(\n (a, b) => b.length - a.length,\n )\n const out: ProxyToolDef[] = []\n const seen = new Set<string>()\n for (const item of items) {\n const matchedServer = serversByLengthDesc.find(\n (name) => item.id === name || item.id.startsWith(`${name}_`),\n )\n if (!matchedServer) continue\n if (seen.has(item.id)) continue\n seen.add(item.id)\n out.push({\n name: item.id,\n description: item.description ?? \"\",\n inputSchema:\n item.parameters && typeof item.parameters === \"object\"\n ? item.parameters\n : { type: \"object\", properties: {} },\n })\n }\n return out.length > 0 ? out : null\n }\n\n /**\n * Create a proxy MCP server for a single active Claude process/session.\n * The process lifecycle owns the server lifecycle via session-manager.\n */\n private async ensureProxyServer(\n tools: ProxyToolDef[],\n sessionKeyForCalls: string,\n ): Promise<ProxyMcpServer> {\n const srv = await createProxyMcpServer(tools)\n srv.calls.on(\"call\", (call: ProxyToolCall) => {\n queuePendingProxyCall(sessionKeyForCalls, call)\n })\n return srv\n }\n\n private extractPendingProxyResult(\n prompt: LanguageModelV3CallOptions[\"prompt\"],\n toolCallId: string,\n ): ProxyToolResult | null {\n for (let i = prompt.length - 1; i >= 0; i--) {\n const msg = prompt[i]\n if (msg.role !== \"tool\" || !Array.isArray(msg.content)) continue\n\n for (const part of msg.content) {\n if (part.type !== \"tool-result\" || part.toolCallId !== toolCallId) continue\n\n const output = part.output as any\n if (!output || typeof output !== \"object\") {\n return {\n kind: \"text\",\n text: String(output ?? \"\"),\n }\n }\n\n if (output.type === \"text\") {\n return {\n kind: \"text\",\n text: String(output.value ?? \"\"),\n }\n }\n\n if (output.type === \"json\") {\n return {\n kind: \"text\",\n text: JSON.stringify(output.value),\n }\n }\n\n if (output.type === \"content\" && Array.isArray(output.value)) {\n const text = output.value\n .filter((v: any) => v?.type === \"text\" && typeof v.text === \"string\")\n .map((v: any) => v.text)\n .join(\"\\n\")\n return {\n kind: \"text\",\n text,\n }\n }\n\n return {\n kind: \"text\",\n text: JSON.stringify(output),\n }\n }\n }\n\n return null\n }\n\n /**\n * Resolve the session affinity token for this LLM call. Delegates to the\n * exported `resolveSessionAffinity` helper so the logic is unit-testable.\n * Priority:\n * 1. `x-session-affinity` request header (primary).\n * 2. `opencodeSessionID` in providerOptions (chat.params hook fallback —\n * covers provider switches mid-session and title synthesis paths\n * where the header is absent).\n * 3. `\"default\"`.\n */\n private sessionAffinity(\n options: LanguageModelV3CallOptions,\n ): string {\n const headers = (options as any)?.headers as\n | Record<string, string | undefined>\n | undefined\n return resolveSessionAffinity(\n headers,\n options.providerOptions as Record<string, unknown> | undefined,\n this.config.provider,\n )\n }\n\n private controlRequestBehaviorForTool(toolName: string): ControlRequestBehavior {\n const configured = this.config.controlRequestToolBehaviors\n if (configured && toolName) {\n const direct = configured[toolName] ?? configured[toolName.toLowerCase()]\n if (direct === \"allow\" || direct === \"deny\") return direct\n\n const lower = toolName.toLowerCase()\n for (const [key, behavior] of Object.entries(configured)) {\n if (key.toLowerCase() === lower && (behavior === \"allow\" || behavior === \"deny\")) {\n return behavior\n }\n }\n }\n\n // AskUserQuestion must never be auto-allowed. Allowing it lets the\n // Claude CLI resolve its own question internally — in headless mode\n // there is no TTY, so the CLI fabricates/empties the answer and the\n // model proceeds on a guess. Deny so the CLI cannot self-answer; the\n // tool_use is still streamed and rendered to the opencode user by\n // formatAskUserQuestion, and the turn stops for a real reply. An\n // explicit controlRequestToolBehaviors entry above can still override.\n if (isAskUserQuestionTool(toolName)) return \"deny\"\n\n return this.config.controlRequestBehavior ?? \"allow\"\n }\n\n private writeControlResponse(\n proc: import(\"child_process\").ChildProcess,\n requestId: string,\n response?: Record<string, unknown>,\n ): void {\n const payload = {\n type: \"control_response\",\n response: {\n subtype: \"success\",\n request_id: requestId,\n response,\n },\n }\n\n try {\n proc.stdin?.write(JSON.stringify(payload) + \"\\n\")\n } catch (error) {\n log.warn(\"failed to write control response\", {\n requestId,\n error: error instanceof Error ? error.message : String(error),\n })\n }\n }\n\n /**\n * Handle Claude stream-json control requests (`can_use_tool`, etc.) and\n * respond via stdin with a matching `control_response`.\n */\n private handleControlRequest(\n msg: ClaudeStreamMessage,\n proc: import(\"child_process\").ChildProcess,\n ): boolean {\n if (msg.type !== \"control_request\") return false\n const requestId = msg.request_id\n const request = msg.request\n if (!requestId || !request?.subtype) return false\n\n if (request.subtype === \"can_use_tool\") {\n const toolName = request.tool_name ?? \"unknown\"\n const behavior = this.controlRequestBehaviorForTool(toolName)\n\n if (behavior === \"allow\") {\n this.writeControlResponse(proc, requestId, {\n behavior: \"allow\",\n updatedInput: request.input ?? {},\n toolUseID: request.tool_use_id,\n })\n log.info(\"control request auto-allowed\", {\n requestId,\n toolName,\n })\n } else {\n const denyMessage = isAskUserQuestionTool(toolName)\n ? \"Your question and its options have already been presented to\" +\n \" the operator in full. Prefer to stop here and wait for their\" +\n \" answer in the next message — do not silently guess. But if\" +\n \" this is an automated or otherwise non-interactive run where\" +\n \" no operator will reply, do not stall: proceed with the single\" +\n \" most reasonable option and state, in one line, the assumption\" +\n \" you made so it can be corrected later.\"\n : this.config.controlRequestDenyMessage ??\n `Denied by opencode-claude-code policy for tool ${toolName}`\n this.writeControlResponse(proc, requestId, {\n behavior: \"deny\",\n message: denyMessage,\n toolUseID: request.tool_use_id,\n })\n log.info(\"control request auto-denied\", {\n requestId,\n toolName,\n })\n }\n\n return true\n }\n\n // For control request subtypes we don't actively handle yet, acknowledge\n // with an empty success so the CLI stream does not stall.\n this.writeControlResponse(proc, requestId, {})\n log.debug(\"control request acknowledged\", {\n requestId,\n subtype: request.subtype,\n })\n return true\n }\n\n private getReasoningEffort(\n providerOptions?: LanguageModelV3CallOptions[\"providerOptions\"],\n ): ReasoningEffort | undefined {\n if (!providerOptions) return undefined\n const ownKey = this.config.provider\n const bag =\n (providerOptions as any)[ownKey] ??\n (providerOptions as any)[\"claude-code\"]\n const effort = bag?.reasoningEffort\n const valid: ReasoningEffort[] = [\n \"minimal\",\n \"low\",\n \"medium\",\n \"high\",\n \"xhigh\",\n \"max\",\n ]\n return valid.includes(effort) ? effort : undefined\n }\n\n private getOpencodeAgent(\n providerOptions?: LanguageModelV3CallOptions[\"providerOptions\"],\n ): string | undefined {\n if (!providerOptions) return undefined\n const ownKey = this.config.provider\n const bag =\n (providerOptions as any)[ownKey] ??\n (providerOptions as any)[\"claude-code\"]\n const agent = bag?.opencodeAgent\n return typeof agent === \"string\" ? agent : undefined\n }\n\n private isCompactionCall(\n options: LanguageModelV3CallOptions,\n ): boolean {\n return this.getOpencodeAgent(options.providerOptions) === \"compaction\"\n }\n\n /**\n * Pick the model used to handle /compact. Precedence:\n * 1. `CLAUDE_CODE_COMPACTION_MODEL` env var (per-process override)\n * 2. `compactionModel` provider setting (opencode.json / .jsonc)\n * 3. Built-in default (claude-haiku-4-5)\n */\n private resolveCompactionModel(): string {\n return resolveCompactionModel(this.config.compactionModel)\n }\n\n private thinkingCliOptions(): {\n thinking?: \"enabled\"\n thinkingDisplay?: \"summarized\"\n } {\n if (isClaudeThinkingDisabled()) return {}\n\n return {\n thinking: \"enabled\",\n thinkingDisplay:\n process.env.CLAUDE_CODE_SHOW_THINKING_SUMMARIES === undefined\n ? \"summarized\"\n : undefined,\n }\n }\n\n private latestUserText(\n prompt: LanguageModelV3CallOptions[\"prompt\"],\n ): string {\n for (let i = prompt.length - 1; i >= 0; i--) {\n const msg = prompt[i]\n if (msg.role !== \"user\") continue\n\n if (typeof msg.content === \"string\") {\n return String(msg.content).trim()\n }\n\n if (Array.isArray(msg.content)) {\n const text = (msg.content as any[])\n .filter((part) => part.type === \"text\" && typeof part.text === \"string\")\n .map((part: any) => String(part.text).trim())\n .filter(Boolean)\n .join(\" \")\n if (text) return text\n }\n }\n\n return \"\"\n }\n\n private synthesizeTitle(\n prompt: LanguageModelV3CallOptions[\"prompt\"],\n ): string {\n const source = this.latestUserText(prompt)\n .replace(/\\s+/g, \" \")\n .replace(/[^\\p{L}\\p{N}\\s-]/gu, \" \")\n .trim()\n\n if (!source) return \"New Session\"\n\n const stop = new Set([\n \"a\",\n \"an\",\n \"the\",\n \"and\",\n \"or\",\n \"but\",\n \"to\",\n \"for\",\n \"of\",\n \"in\",\n \"on\",\n \"at\",\n \"with\",\n \"can\",\n \"could\",\n \"would\",\n \"should\",\n \"please\",\n \"hi\",\n \"hello\",\n \"hey\",\n \"there\",\n \"you\",\n \"your\",\n \"this\",\n \"that\",\n \"is\",\n \"are\",\n \"was\",\n \"were\",\n \"be\",\n \"do\",\n \"does\",\n \"did\",\n \"summarize\",\n \"summary\",\n \"project\",\n ])\n\n const words = source\n .split(\" \")\n .map((word) => word.trim())\n .filter(Boolean)\n .filter((word) => !stop.has(word.toLowerCase()))\n\n const picked = (words.length > 0 ? words : source.split(\" \").filter(Boolean))\n .slice(0, 6)\n .map((word) => word.charAt(0).toUpperCase() + word.slice(1))\n .join(\" \")\n\n return picked || \"New Session\"\n }\n\n private async doGenerateViaStream(\n options: LanguageModelV3CallOptions,\n ): Promise<Awaited<ReturnType<LanguageModelV3[\"doGenerate\"]>>> {\n const result = await this.doStream(options)\n const reader = result.stream.getReader()\n\n let text = \"\"\n let reasoning = \"\"\n const toolCalls: LanguageModelV3Content[] = []\n let finishReason = this.toFinishReason(\"stop\")\n let usage: LanguageModelV3Usage = this.toUsage()\n let providerMetadata: any\n\n while (true) {\n const { value, done } = await reader.read()\n if (done) break\n\n switch ((value as any).type) {\n case \"text-delta\":\n text += (value as any).delta ?? \"\"\n break\n case \"reasoning-delta\":\n reasoning += (value as any).delta ?? \"\"\n break\n case \"tool-call\":\n toolCalls.push({\n type: \"tool-call\",\n toolCallId: (value as any).toolCallId,\n toolName: (value as any).toolName,\n input: (value as any).input,\n providerExecuted: (value as any).providerExecuted,\n } as any)\n break\n case \"finish\":\n finishReason = (value as any).finishReason ?? finishReason\n usage = (value as any).usage ?? usage\n providerMetadata = (value as any).providerMetadata ?? providerMetadata\n break\n }\n }\n\n const content: LanguageModelV3Content[] = []\n if (reasoning) {\n content.push({ type: \"reasoning\", text: reasoning } as any)\n }\n if (text) {\n content.push({ type: \"text\", text, providerMetadata } as any)\n }\n content.push(...toolCalls)\n\n return {\n content,\n finishReason,\n usage,\n request: result.request,\n response: {\n id: generateId(),\n timestamp: new Date(),\n modelId: this.modelId,\n },\n providerMetadata,\n warnings: [],\n }\n }\n\n async doGenerate(\n options: LanguageModelV3CallOptions,\n ): Promise<Awaited<ReturnType<LanguageModelV3[\"doGenerate\"]>>> {\n const warnings: SharedV3Warning[] = []\n const cwd = resolveSpawnCwd(this.config.cwd)\n const scope = this.requestScope(options as any)\n const affinity = this.sessionAffinity(options)\n const sk = sessionKey(cwd, `${this.modelId}::${scope}::${affinity}`)\n\n // When selective proxying is enabled, doGenerate must not bypass the\n // proxy path. Reuse doStream and aggregate its events so proxied tools\n // still route through opencode permissions/execution. Same for\n // opencode MCP proxying — doStream is the only path that wires up the\n // proxy server with the dynamically-discovered MCP tool defs.\n const compactionMode = this.isCompactionCall(options)\n\n if (\n scope === \"tools\" &&\n (this.resolvedProxyTools() ||\n (this.config.proxyOpencodeMcpTools !== false &&\n this.config.bridgeOpencodeMcp !== false))\n ) {\n return this.doGenerateViaStream(options)\n }\n\n // Route compaction through doStream so it gets the lean spawn path,\n // model override, and rich transcript handling. Aggregating a stream\n // for doGenerate matches what doGenerateViaStream already does for\n // proxy tools.\n if (compactionMode) {\n return this.doGenerateViaStream(options)\n }\n\n if (scope === \"no-tools\") {\n log.info(\"doGenerate no-tools title stub\", {\n compactionMode,\n opencodeAgent: this.getOpencodeAgent(options.providerOptions),\n providerOptionsKeys: options.providerOptions\n ? Object.keys(options.providerOptions)\n : [],\n })\n const text = this.synthesizeTitle(options.prompt)\n return {\n content: [{ type: \"text\", text }] as any,\n finishReason: this.toFinishReason(\"stop\"),\n usage: this.toUsage({ input_tokens: 0, output_tokens: 0 }),\n request: { body: { text: \"\" } },\n response: {\n id: generateId(),\n timestamp: new Date(),\n modelId: this.modelId,\n },\n providerMetadata: {\n \"claude-code\": {\n synthetic: true,\n path: \"no-tools\",\n },\n },\n warnings,\n }\n }\n\n // Short-circuit when opencode iterates the agent loop one more time\n // after a turn already finished. The prompt ends with an assistant\n // message and has no fresh user input — spawning Claude here would\n // just produce a stub like \"No input received. Standing by\".\n if (!hasNewUserContent(options.prompt)) {\n log.info(\"doGenerate short-circuit: no new user content\")\n return {\n content: [],\n finishReason: this.toFinishReason(\"stop\"),\n usage: this.toUsage({ input_tokens: 0, output_tokens: 0 }),\n request: { body: { text: \"\" } },\n response: {\n id: generateId(),\n timestamp: new Date(),\n modelId: this.modelId,\n },\n providerMetadata: {\n \"claude-code\": { synthetic: true, path: \"no-new-user-content\" },\n },\n warnings,\n }\n }\n\n const hasPriorConversation =\n options.prompt.filter((m) => m.role === \"user\" || m.role === \"assistant\")\n .length > 1\n\n // New session — clear any stale state from a previous session\n if (!hasPriorConversation) {\n deleteClaudeSessionId(sk)\n deleteActiveProcess(sk)\n }\n\n const hasExistingSession = !!getClaudeSessionId(sk)\n const includeHistoryContext = !hasExistingSession && hasPriorConversation\n\n const reasoningEffort = this.getReasoningEffort(options.providerOptions)\n const userMsg = getClaudeUserMessage(\n options.prompt,\n includeHistoryContext,\n reasoningEffort,\n )\n\n // doGenerate always spawns a fresh process, never reuse session ID.\n // Pre-fetch opencode's MCP runtime status so the bridge overlays\n // UI-toggled state on top of disk config.\n const [runtimeStatus, cliVersion] = await Promise.all([\n getRuntimeMcpStatus(),\n detectCliVersion(this.config.cliPath),\n ])\n const systemPromptFile = buildAppendedSystemPrompt(\n cwd,\n this.config.multiStepContinuation !== false,\n extractSystemMessages(options.prompt),\n )\n const cliArgs = buildCliArgs({\n sessionKey: sk,\n skipPermissions: this.config.skipPermissions !== false,\n includeSessionId: false,\n model: this.modelId,\n permissionMode: this.config.permissionMode,\n mcpConfig: this.effectiveMcpConfig(cwd, undefined, runtimeStatus).paths,\n strictMcpConfig: this.config.strictMcpConfig,\n disallowedTools:\n this.config.webSearch === \"disabled\" ? [\"WebSearch\"] : undefined,\n appendSystemPromptFile: systemPromptFile,\n ...this.thinkingCliOptions(),\n cliVersion,\n })\n\n log.info(\"doGenerate starting\", {\n cwd,\n model: this.modelId,\n textLength: userMsg.length,\n includeHistoryContext,\n })\n\n const { spawn } = await import(\"node:child_process\")\n const { createInterface } = await import(\"node:readline\")\n\n const proc = spawn(this.config.cliPath, cliArgs, {\n cwd,\n stdio: [\"pipe\", \"pipe\", \"pipe\"],\n env: claudeSpawnEnv(),\n shell: process.platform === \"win32\",\n })\n\n if (systemPromptFile) {\n proc.on(\"exit\", () => {\n void unlink(systemPromptFile).catch(() => {})\n })\n }\n\n const rl = createInterface({ input: proc.stdout! })\n\n let responseText = \"\"\n let thinkingText = \"\"\n let resultMeta: {\n sessionId?: string\n costUsd?: number\n durationMs?: number\n usage?: ClaudeStreamMessage[\"usage\"]\n } = {}\n const toolCalls: Array<{ id: string; name: string; args: unknown }> = []\n // Streaming tool_use entries keyed by content-block index. We accumulate\n // partial_json chunks here instead of trying to JSON.parse each chunk\n // independently, and flush to `toolCalls` at content_block_stop. The\n // previous code indexed `toolCalls` by `msg.index` directly, which is\n // wrong whenever non-tool blocks (text, thinking) precede a tool_use.\n const toolCallStreams = new Map<\n number,\n { id: string; name: string; inputJson: string }\n >()\n\n // Set true once we observe a `stream_event` envelope. When on, the\n // top-level `assistant` message is a duplicate of content already\n // accumulated via the inner content_block_* events — skip it.\n let gotPartialEvents = false\n\n const result = await new Promise<\n typeof resultMeta & {\n text: string\n thinking: string\n toolCalls: typeof toolCalls\n }\n >((resolve, reject) => {\n const cleanup = () => {\n try {\n if (!proc.killed && proc.exitCode === null) proc.kill()\n } catch {}\n }\n\n rl.on(\"line\", (line) => {\n if (!line.trim()) return\n try {\n const outer: ClaudeStreamMessage = JSON.parse(line)\n\n // Unwrap stream_event envelope (--include-partial-messages).\n // Inner event uses the same content_block_* / message_* shape.\n const msg: ClaudeStreamMessage =\n outer.type === \"stream_event\" && outer.event\n ? { ...outer.event, session_id: outer.session_id }\n : outer\n\n if (outer.type === \"stream_event\") {\n gotPartialEvents = true\n }\n\n if (this.handleControlRequest(msg, proc)) {\n return\n }\n\n if (msg.type === \"system\" && msg.subtype === \"init\") {\n if (msg.session_id) {\n setClaudeSessionId(sk, msg.session_id)\n }\n }\n\n if (\n msg.type === \"assistant\" &&\n msg.message?.content &&\n !gotPartialEvents\n ) {\n for (const block of msg.message.content) {\n if (block.type === \"text\" && block.text) {\n responseText += block.text\n }\n if (block.type === \"thinking\" && block.thinking) {\n thinkingText += block.thinking\n }\n if (block.type === \"tool_use\" && block.id && block.name) {\n if (isAskUserQuestionTool(block.name)) {\n // Render the full question + options as visible text so\n // the user can actually see and answer it.\n const parsedInput = (block.input ?? {}) as Record<\n string,\n unknown\n >\n responseText += formatAskUserQuestion(parsedInput)\n continue\n }\n\n if (block.name === \"ExitPlanMode\") {\n const parsedInput = (block.input ?? {}) as Record<\n string,\n unknown\n >\n const plan = (parsedInput?.plan as string) || \"\"\n responseText += `\\n\\n${plan}\\n\\n---\\n**Do you want to proceed with this plan?** (yes/no)\\n`\n continue\n }\n\n toolCalls.push({\n id: block.id,\n name: block.name,\n args: block.input ?? {},\n })\n }\n }\n }\n\n if (\n msg.type === \"content_block_start\" &&\n msg.content_block &&\n msg.index !== undefined\n ) {\n if (\n msg.content_block.type === \"tool_use\" &&\n msg.content_block.id &&\n msg.content_block.name\n ) {\n toolCallStreams.set(msg.index, {\n id: msg.content_block.id,\n name: msg.content_block.name,\n inputJson: \"\",\n })\n }\n }\n\n if (\n msg.type === \"content_block_delta\" &&\n msg.delta &&\n msg.index !== undefined\n ) {\n if (msg.delta.type === \"text_delta\" && msg.delta.text) {\n responseText += msg.delta.text\n }\n if (msg.delta.type === \"thinking_delta\" && msg.delta.thinking) {\n thinkingText += msg.delta.thinking\n }\n if (\n msg.delta.type === \"input_json_delta\" &&\n msg.delta.partial_json\n ) {\n const tc = toolCallStreams.get(msg.index)\n if (tc) tc.inputJson += msg.delta.partial_json\n }\n }\n\n if (msg.type === \"content_block_stop\" && msg.index !== undefined) {\n const tc = toolCallStreams.get(msg.index)\n if (tc) {\n let args: unknown = {}\n try {\n args = tc.inputJson ? JSON.parse(tc.inputJson) : {}\n } catch (err) {\n log.warn(\"tool input JSON parse failed\", {\n name: tc.name,\n error: String(err),\n })\n }\n toolCalls.push({ id: tc.id, name: tc.name, args })\n toolCallStreams.delete(msg.index)\n }\n }\n\n if (msg.type === \"result\") {\n if (msg.session_id) {\n setClaudeSessionId(sk, msg.session_id)\n }\n\n // Some CLI failures only surface user-readable text on the final\n // `result` message (without prior assistant text blocks). Preserve\n // that so callers don't receive an empty response.\n if (\n !responseText &&\n msg.is_error &&\n typeof msg.result === \"string\" &&\n msg.result.trim().length > 0\n ) {\n responseText = msg.result\n }\n\n resultMeta = {\n sessionId: msg.session_id,\n costUsd: msg.total_cost_usd,\n durationMs: msg.duration_ms,\n usage: msg.usage,\n }\n cleanup()\n resolve({\n ...resultMeta,\n text: responseText,\n thinking: thinkingText,\n toolCalls,\n })\n }\n } catch {\n // Ignore non-JSON lines\n }\n })\n\n rl.on(\"close\", () => {\n cleanup()\n resolve({\n ...resultMeta,\n text: responseText,\n thinking: thinkingText,\n toolCalls,\n })\n })\n\n proc.on(\"error\", (err) => {\n log.error(\"process error\", { error: err.message })\n cleanup()\n reject(err)\n })\n\n proc.stderr?.on(\"data\", (data: Buffer) => {\n log.debug(\"stderr\", { data: data.toString().slice(0, 200) })\n })\n\n proc.stdin?.write(userMsg + \"\\n\")\n })\n\n const content: LanguageModelV3Content[] = []\n\n if (result.thinking) {\n content.push({\n type: \"reasoning\",\n text: result.thinking,\n } as any)\n }\n\n if (result.text) {\n content.push({\n type: \"text\",\n text: result.text,\n providerMetadata: {\n \"claude-code\": {\n sessionId: result.sessionId ?? null,\n costUsd: result.costUsd ?? null,\n durationMs: result.durationMs ?? null,\n },\n ...(typeof result.usage?.cache_creation_input_tokens === \"number\"\n ? {\n anthropic: {\n cacheCreationInputTokens:\n result.usage.cache_creation_input_tokens,\n },\n }\n : {}),\n },\n })\n }\n\n for (const tc of result.toolCalls) {\n const {\n name: mappedName,\n input: mappedInput,\n executed,\n skip,\n } = mapTool(tc.name, tc.args, {\n webSearch: this.config.webSearch,\n sessionId: getClaudeSessionId(sk),\n toolUseId: tc.id,\n })\n if (skip) continue\n content.push({\n type: \"tool-call\",\n toolCallId: tc.id,\n toolName: mappedName,\n input: JSON.stringify(mappedInput),\n providerExecuted: executed,\n } as any)\n }\n\n const usage = this.toUsage(result.usage)\n\n return {\n content,\n // Claude CLI's `result` message signals a fully-completed turn —\n // tools have already been executed internally and final assistant\n // text has been produced. Always report \"stop\" so opencode doesn't\n // loop expecting to run tools itself.\n finishReason: this.toFinishReason(\"stop\"),\n usage,\n request: { body: { text: userMsg } },\n response: {\n id: result.sessionId ?? generateId(),\n timestamp: new Date(),\n modelId: this.modelId,\n },\n providerMetadata: {\n \"claude-code\": {\n sessionId: result.sessionId ?? null,\n costUsd: result.costUsd ?? null,\n durationMs: result.durationMs ?? null,\n },\n ...(typeof result.usage?.cache_creation_input_tokens === \"number\"\n ? {\n anthropic: {\n cacheCreationInputTokens:\n result.usage.cache_creation_input_tokens,\n },\n }\n : {}),\n },\n warnings,\n }\n }\n\n async doStream(\n options: LanguageModelV3CallOptions,\n ): Promise<Awaited<ReturnType<LanguageModelV3[\"doStream\"]>>> {\n const warnings: SharedV3Warning[] = []\n const cwd = resolveSpawnCwd(this.config.cwd)\n const cliPath = this.config.cliPath\n const skipPermissions = this.config.skipPermissions !== false\n const scope = this.requestScope(options as any)\n const affinity = this.sessionAffinity(options)\n const compactionMode = this.isCompactionCall(options)\n // Use a separate session key for compaction so its short-lived spawn\n // never collides with the main conversation's claude process.\n const effectiveModelId = compactionMode\n ? this.resolveCompactionModel()\n : this.modelId\n const sk = compactionMode\n ? sessionKey(cwd, `${effectiveModelId}::compaction::${affinity}`)\n : sessionKey(cwd, `${this.modelId}::${scope}::${affinity}`)\n const toUsage = this.toUsage.bind(this)\n const toFinishReason = this.toFinishReason.bind(this)\n const handleControlRequest = this.handleControlRequest.bind(this)\n\n if (scope === \"no-tools\" && !compactionMode) {\n log.info(\"doStream no-tools title stub\", {\n compactionMode,\n opencodeAgent: this.getOpencodeAgent(options.providerOptions),\n providerOptionsKeys: options.providerOptions\n ? Object.keys(options.providerOptions)\n : [],\n })\n const text = this.synthesizeTitle(options.prompt)\n const textId = generateId()\n const stream = new ReadableStream<LanguageModelV3StreamPart>({\n start(controller) {\n controller.enqueue({ type: \"stream-start\", warnings })\n controller.enqueue({ type: \"text-start\", id: textId } as any)\n controller.enqueue({\n type: \"text-delta\",\n id: textId,\n delta: text,\n })\n controller.enqueue({ type: \"text-end\", id: textId })\n controller.enqueue({\n type: \"finish\",\n finishReason: toFinishReason(\"stop\"),\n usage: toUsage({ input_tokens: 0, output_tokens: 0 }),\n providerMetadata: {\n \"claude-code\": {\n synthetic: true,\n path: \"no-tools\",\n },\n },\n })\n controller.close()\n },\n })\n\n return {\n stream,\n request: { body: { text: \"\" } },\n }\n }\n\n // Short-circuit when opencode iterates the agent loop one more time\n // after a turn already finished. The prompt ends with an assistant\n // message and has no fresh user input — spawning Claude here would\n // just produce a stub like \"No input received. Standing by\".\n if (!hasNewUserContent(options.prompt)) {\n log.info(\"doStream short-circuit: no new user content\")\n const stream = new ReadableStream<LanguageModelV3StreamPart>({\n start(controller) {\n controller.enqueue({ type: \"stream-start\", warnings })\n controller.enqueue({\n type: \"finish\",\n finishReason: toFinishReason(\"stop\"),\n usage: toUsage({ input_tokens: 0, output_tokens: 0 }),\n providerMetadata: {\n \"claude-code\": { synthetic: true, path: \"no-new-user-content\" },\n },\n })\n controller.close()\n },\n })\n return { stream, request: { body: { text: \"\" } } }\n }\n\n const hasPriorConversation =\n options.prompt.filter((m) => m.role === \"user\" || m.role === \"assistant\")\n .length > 1\n\n // New session — clear any stale state from a previous session\n if (!hasPriorConversation) {\n deleteClaudeSessionId(sk)\n deleteActiveProcess(sk)\n }\n\n const hasExistingSession = !!getClaudeSessionId(sk)\n const hasActiveProcess = !!getActiveProcess(sk)\n const includeHistoryContext =\n !hasExistingSession && !hasActiveProcess && hasPriorConversation\n\n const reasoningEffort = this.getReasoningEffort(options.providerOptions)\n const userMsg = getClaudeUserMessage(\n options.prompt,\n includeHistoryContext,\n reasoningEffort,\n { compactionMode },\n )\n const resolvedProxy = compactionMode ? null : this.resolvedProxyTools()\n const self = this\n\n const previousPendingProxyCalls = compactionMode\n ? []\n : getPendingProxyCalls(sk)\n const previousPendingProxyMatches: Array<{\n call: PendingProxyCall\n result: ProxyToolResult | null\n }> = previousPendingProxyCalls.map((call) => ({\n call,\n result: this.extractPendingProxyResult(options.prompt, call.toolCallId),\n }))\n const hasMatchedPendingResults = previousPendingProxyMatches.some(\n (m) => m.result !== null,\n )\n\n // Pre-fetch opencode's MCP runtime status before constructing the\n // ReadableStream so the sync hot-reload check and async setup() see\n // the same overlay snapshot. One in-process call per turn — cheap;\n // the SDK client routes through `Server.app.fetch` (no socket).\n // Detect the Claude CLI version in parallel so the spawn can decide\n // which optional flags it supports without crashing older binaries.\n const [runtimeStatus, cliVersion] = await Promise.all([\n compactionMode ? Promise.resolve(undefined) : getRuntimeMcpStatus(),\n detectCliVersion(this.config.cliPath),\n ])\n\n log.info(\"doStream starting\", {\n cwd,\n model: effectiveModelId,\n textLength: userMsg.length,\n includeHistoryContext,\n hasActiveProcess,\n reasoningEffort,\n proxyTools: resolvedProxy?.map((t) => t.name) ?? null,\n compactionMode,\n scope,\n opencodeAgent: this.getOpencodeAgent(options.providerOptions),\n providerOptionsKeys: options.providerOptions\n ? Object.keys(options.providerOptions)\n : [],\n })\n\n const stream = new ReadableStream<LanguageModelV3StreamPart>({\n start(controller) {\n // Compaction is a one-shot call. Don't reuse any cached process\n // from a prior compaction — each /compact gets a fresh spawn so\n // the new transcript isn't appended to a stale claude session.\n if (compactionMode) {\n deleteActiveProcess(sk)\n deleteClaudeSessionId(sk)\n }\n\n let activeProcess = getActiveProcess(sk)\n let proc: import(\"child_process\").ChildProcess\n let lineEmitter: import(\"events\").EventEmitter\n let proxyServer: ProxyMcpServer | null = activeProcess?.proxyServer ?? null\n\n // Hot reload: evict cached subprocess if the bridged opencode MCP\n // config has drifted since spawn. Only checked between turns (here,\n // before setup() runs), never mid tool-call. The stored claude\n // session id is preserved so the respawn resumes the conversation\n // via `--session-id` (handled by buildCliArgs).\n if (\n !compactionMode &&\n activeProcess &&\n self.config.hotReloadMcp !== false &&\n self.config.bridgeOpencodeMcp !== false\n ) {\n const probe = self.effectiveMcpConfig(cwd, undefined, runtimeStatus!)\n const previousHash = activeProcess.mcpHash ?? null\n if (previousHash !== probe.bridgedHash) {\n log.info(\"opencode MCP config changed, respawning claude\", {\n sk,\n previousHash,\n currentHash: probe.bridgedHash,\n })\n deleteActiveProcess(sk)\n activeProcess = undefined\n proxyServer = null\n }\n }\n\n const setup = async () => {\n let cliArgs: string[]\n let spawnSystemPromptFile: string | undefined\n let spawnProxyServer: ProxyMcpServer | null = null\n let spawnMcpHash: string | null = null\n\n if (compactionMode) {\n // Compaction takes a lean spawn: no MCP servers, no proxy, no\n // appended system prompt, no disallowed-tools list. The model\n // is asked for text output only on a single turn — all the\n // normal tool wiring is pure overhead and adds latency.\n // Explicitly opt out of `--session-id` so a stale id can never\n // resume into the lean spawn.\n cliArgs = buildCliArgs({\n sessionKey: sk,\n skipPermissions,\n includeSessionId: false,\n model: effectiveModelId,\n permissionMode: self.config.permissionMode,\n cliVersion,\n })\n } else {\n // First pass: discover which opencode MCP servers would be\n // bridged. We use this to decide which ones to re-route through\n // the proxy instead. No --mcp-config path is consumed here;\n // it's recomputed below with the exclusion set in place.\n const discovery = self.effectiveMcpConfig(\n cwd,\n undefined,\n runtimeStatus!,\n )\n\n // Fetch the proxy MCP tools (one ProxyToolDef per opencode\n // MCP-bridged tool). If discovery returns nothing or the SDK\n // is unreachable, this is null and we fall back to direct\n // bridging.\n const proxyMcpTools = await self.resolvedProxyMcpTools(\n discovery.allEnabledServerNames,\n )\n const excludeServers: ReadonlySet<string> | undefined = proxyMcpTools\n ? new Set(discovery.allEnabledServerNames)\n : undefined\n\n const combinedProxyTools: ProxyToolDef[] | null =\n resolvedProxy || proxyMcpTools\n ? [...(resolvedProxy ?? []), ...(proxyMcpTools ?? [])]\n : null\n\n if (!proxyServer && combinedProxyTools) {\n proxyServer = await self.ensureProxyServer(combinedProxyTools, sk)\n }\n\n const proxyDisallowed = resolvedProxy ? disallowedToolFlags(resolvedProxy) : []\n const extraDisallowed: string[] = []\n if (self.config.webSearch === \"disabled\") extraDisallowed.push(\"WebSearch\")\n const allDisallowed = [...proxyDisallowed, ...extraDisallowed]\n const mcp = self.effectiveMcpConfig(\n cwd,\n proxyServer?.configPath(),\n runtimeStatus!,\n excludeServers,\n )\n const systemPromptFile = activeProcess\n ? undefined\n : buildAppendedSystemPrompt(\n cwd,\n self.config.multiStepContinuation !== false,\n extractSystemMessages(options.prompt),\n )\n cliArgs = buildCliArgs({\n sessionKey: sk,\n skipPermissions,\n model: self.modelId,\n permissionMode: self.config.permissionMode,\n mcpConfig: mcp.paths,\n strictMcpConfig: self.config.strictMcpConfig,\n disallowedTools: allDisallowed.length > 0 ? allDisallowed : undefined,\n appendSystemPromptFile: systemPromptFile,\n ...self.thinkingCliOptions(),\n cliVersion,\n })\n spawnSystemPromptFile = systemPromptFile\n spawnProxyServer = proxyServer\n spawnMcpHash = mcp.bridgedHash\n }\n\n if (activeProcess && !compactionMode) {\n proc = activeProcess.proc\n lineEmitter = activeProcess.lineEmitter\n log.debug(\"reusing active process\", { sk })\n } else {\n const ap = spawnClaudeProcess(\n cliPath,\n cliArgs,\n cwd,\n sk,\n spawnProxyServer,\n spawnMcpHash,\n spawnSystemPromptFile,\n )\n proc = ap.proc\n lineEmitter = ap.lineEmitter\n activeProcess = ap\n }\n\n controller.enqueue({ type: \"stream-start\", warnings })\n\n let currentTextId: string | null = null\n const textBlockIndices = new Set<number>()\n\n const startTextBlock = (): string => {\n if (currentTextId) {\n controller.enqueue({ type: \"text-end\", id: currentTextId })\n }\n const id = generateId()\n currentTextId = id\n controller.enqueue({ type: \"text-start\", id } as any)\n return id\n }\n\n const endTextBlock = (): void => {\n if (currentTextId) {\n controller.enqueue({ type: \"text-end\", id: currentTextId })\n currentTextId = null\n }\n }\n\n const reasoningIds = new Map<number, string>()\n const reasoningStarted = new Map<number, boolean>()\n let hadThinkingTextFromStream = false\n\n let turnCompleted = false\n let controllerClosed = false\n let pendingProxyUnsubscribe: (() => void) | null = null\n let resultFallbackTimer: ReturnType<typeof setTimeout> | null = null\n let hasReceivedContent = false\n let visibleTextSinceContinue = \"\"\n let lastVisibleTextSinceContinue = \"\"\n let hadReasoningSinceContinue = false\n let hadToolActivitySinceContinue = false\n let hadProxyActivitySinceContinue = false\n // v0.4.16: protocol-level stop signal captured from Claude CLI's\n // stream. Set by either the `message_delta` partial event or the\n // top-level `assistant` message, whichever arrives first.\n let lastStopReason: string | null = null\n const autoContinueState: AutoContinueState = {\n enabled: self.config.autoContinueIncompleteTurns,\n attempts: 0,\n startedAt: Date.now(),\n noProgressCount: 0,\n }\n\n const clearFallbackTimer = () => {\n if (resultFallbackTimer) {\n clearTimeout(resultFallbackTimer)\n resultFallbackTimer = null\n }\n }\n\n // Wire-inactivity watchdog. Resets on every line received from the\n // CLI; only fires if the CLI has emitted content and then gone\n // silent on stdout for `delayMs` without sending a `result`. The\n // previous design armed this on every text content_block_stop,\n // which killed legitimate mid-turn think pauses (most visibly\n // with sonnet between text-end and the next tool_use_start).\n const startResultFallback = (delayMs = 60_000) => {\n clearFallbackTimer()\n if (!hasReceivedContent || controllerClosed) return\n resultFallbackTimer = setTimeout(() => {\n if (controllerClosed) return\n log.warn(\"result fallback timer fired — closing stream without result event\", {\n delayMs,\n })\n closeHandler()\n }, delayMs)\n }\n\n const toolCallMap = new Map<\n number,\n { id: string; name: string; inputJson: string }\n >()\n // Tool calls the plugin reported as providerExecuted:false — opencode\n // will run these itself and emit its own tool-result, so we must NOT\n // forward Claude CLI's tool_result for them (would short-circuit\n // opencode's execute).\n const skipResultForIds = new Set<string>()\n const toolCallsById = new Map<\n string,\n { id: string; name: string; input: unknown }\n >()\n\n let resultMeta: {\n sessionId?: string\n costUsd?: number\n durationMs?: number\n usage?: ClaudeStreamMessage[\"usage\"]\n } = {}\n\n // Batched drain so claude CLI's parallel tool_use blocks (e.g. two\n // bash calls in one assistant message) end up in a single\n // tool-calls finish event. Without this, the broker would reject\n // every overlapping call and claude would see spurious tool errors.\n const drainBuffer: PendingProxyCall[] = []\n let drainTimer: ReturnType<typeof setTimeout> | null = null\n const DRAIN_QUIET_MS = 100\n\n const finishWithToolCalls = (calls: PendingProxyCall[]) => {\n if (controllerClosed) return\n if (calls.length === 0) return\n for (const call of calls) {\n controller.enqueue({\n type: \"tool-input-start\",\n id: call.toolCallId,\n toolName: call.toolName,\n } as any)\n controller.enqueue({\n type: \"tool-call\",\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n input: JSON.stringify(call.input),\n providerExecuted: false,\n } as any)\n skipResultForIds.add(call.toolCallId)\n }\n controller.enqueue({\n type: \"finish\",\n finishReason: toFinishReason(\"tool-calls\"),\n usage: toUsage(resultMeta.usage),\n providerMetadata: {\n \"claude-code\": resultMeta,\n },\n })\n controllerClosed = true\n cleanupTurn()\n try {\n controller.close()\n } catch {}\n }\n\n const drainNow = () => {\n if (drainTimer) {\n clearTimeout(drainTimer)\n drainTimer = null\n }\n if (drainBuffer.length === 0) return\n if (controllerClosed) return\n const batch = drainBuffer.splice(0, drainBuffer.length)\n log.info(\"draining pending proxy calls into stream finish\", {\n sessionKey: sk,\n count: batch.length,\n toolCallIds: batch.map((c) => c.toolCallId),\n })\n finishWithToolCalls(batch)\n }\n\n const noteVisibleText = (text: string) => {\n visibleTextSinceContinue += text\n lastVisibleTextSinceContinue += text\n }\n\n const resetLastVisibleTextBlock = () => {\n lastVisibleTextSinceContinue = \"\"\n }\n\n const noteReasoning = () => {\n hadReasoningSinceContinue = true\n }\n\n const noteToolActivity = () => {\n hadToolActivitySinceContinue = true\n }\n\n const noteProxyActivity = () => {\n hadProxyActivitySinceContinue = true\n }\n\n const resetAutoContinueWindow = () => {\n visibleTextSinceContinue = \"\"\n lastVisibleTextSinceContinue = \"\"\n hadReasoningSinceContinue = false\n hadToolActivitySinceContinue = false\n hadProxyActivitySinceContinue = false\n lastStopReason = null\n }\n\n // Set true once we observe a `stream_event` envelope. When on, the\n // top-level `assistant` message is a duplicate of what we already\n // streamed via content_block_* deltas — skip its content.\n let gotPartialEvents = false\n\n const lineHandler = (line: string) => {\n if (!line.trim()) return\n if (controllerClosed) return\n\n // Any line from the CLI counts as activity — reset the inactivity\n // watchdog so mid-turn pauses between blocks don't get killed.\n startResultFallback()\n\n try {\n const outer: ClaudeStreamMessage = JSON.parse(line)\n\n // Unwrap stream_event envelope (--include-partial-messages).\n // Inner event uses the same content_block_* / message_* shape.\n const msg: ClaudeStreamMessage =\n outer.type === \"stream_event\" && outer.event\n ? { ...outer.event, session_id: outer.session_id }\n : outer\n\n if (outer.type === \"stream_event\") {\n gotPartialEvents = true\n }\n\n if (handleControlRequest(msg, proc)) {\n return\n }\n\n log.debug(\"stream message\", {\n type: msg.type,\n subtype: msg.subtype,\n })\n\n // Handle system init\n if (msg.type === \"system\" && msg.subtype === \"init\") {\n if (msg.session_id) {\n setClaudeSessionId(sk, msg.session_id)\n log.info(\"session initialized\", {\n claudeSessionId: msg.session_id,\n })\n }\n }\n\n // content_block_start\n if (\n msg.type === \"content_block_start\" &&\n msg.content_block &&\n msg.index !== undefined\n ) {\n const block = msg.content_block\n const idx = msg.index\n\n if (block.type === \"thinking\") {\n noteReasoning()\n const reasoningId = generateId()\n reasoningIds.set(idx, reasoningId)\n }\n\n if (block.type === \"text\") {\n textBlockIndices.add(idx)\n // New text block — clear last-block buffer so final-answer\n // detection only considers this block's contents, not earlier\n // mid-task narration.\n resetLastVisibleTextBlock()\n if (block.text) {\n if (!currentTextId) startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: currentTextId!,\n delta: block.text,\n })\n noteVisibleText(block.text)\n hasReceivedContent = true\n }\n }\n\n if (block.type === \"tool_use\" && block.id && block.name) {\n noteToolActivity()\n toolCallMap.set(idx, {\n id: block.id,\n name: block.name,\n inputJson: \"\",\n })\n\n if (\n block.name !== \"AskUserQuestion\" &&\n block.name !== \"ask_user_question\" &&\n block.name !== \"ExitPlanMode\" &&\n !block.name.startsWith(PROXY_TOOL_PREFIX)\n ) {\n const { name: mappedName, skip, executed } = mapTool(\n block.name,\n undefined,\n {\n webSearch: self.config.webSearch,\n sessionId: getClaudeSessionId(sk),\n toolUseId: block.id,\n },\n )\n if (!skip) {\n controller.enqueue({\n type: \"tool-input-start\",\n id: block.id,\n toolName: mappedName,\n providerExecuted: executed,\n } as any)\n log.info(\"tool started\", {\n name: block.name,\n mappedName,\n id: block.id,\n })\n }\n }\n }\n }\n\n // content_block_delta\n if (\n msg.type === \"content_block_delta\" &&\n msg.delta &&\n msg.index !== undefined\n ) {\n const delta = msg.delta\n const idx = msg.index\n\n if (delta.type === \"thinking_delta\" && delta.thinking) {\n noteReasoning()\n hadThinkingTextFromStream = true\n const reasoningId = reasoningIds.get(idx)\n if (reasoningId) {\n if (!reasoningStarted.get(idx)) {\n controller.enqueue({\n type: \"reasoning-start\",\n id: reasoningId,\n } as any)\n reasoningStarted.set(idx, true)\n }\n controller.enqueue({\n type: \"reasoning-delta\",\n id: reasoningId,\n delta: delta.thinking,\n } as any)\n }\n }\n\n if (delta.type === \"text_delta\" && delta.text) {\n if (!currentTextId) startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: currentTextId!,\n delta: delta.text,\n })\n noteVisibleText(delta.text)\n hasReceivedContent = true\n }\n\n if (delta.type === \"input_json_delta\" && delta.partial_json) {\n const tc = toolCallMap.get(idx)\n if (tc) {\n tc.inputJson += delta.partial_json\n controller.enqueue({\n type: \"tool-input-delta\",\n id: tc.id,\n delta: delta.partial_json,\n } as any)\n }\n }\n\n if (!KNOWN_DELTA_TYPES.has(delta.type)) {\n log.debug(\"unrecognized content_block_delta type\", {\n type: delta.type,\n idx,\n keys: Object.keys(delta),\n })\n }\n }\n\n // content_block_stop\n if (\n msg.type === \"content_block_stop\" &&\n msg.index !== undefined\n ) {\n const idx = msg.index\n\n const reasoningId = reasoningIds.get(idx)\n if (reasoningId && reasoningStarted.get(idx)) {\n controller.enqueue({\n type: \"reasoning-end\",\n id: reasoningId,\n } as any)\n reasoningStarted.delete(idx)\n }\n\n if (textBlockIndices.has(idx)) {\n endTextBlock()\n textBlockIndices.delete(idx)\n }\n\n const tc = toolCallMap.get(idx)\n if (tc) {\n let parsedInput: any = {}\n try {\n parsedInput = JSON.parse(tc.inputJson || \"{}\")\n } catch {}\n\n if (isAskUserQuestionTool(tc.name)) {\n const askId = startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: askId,\n delta: formatAskUserQuestion(parsedInput),\n })\n endTextBlock()\n } else if (tc.name === \"ExitPlanMode\") {\n const plan = (parsedInput?.plan as string) || \"\"\n\n const planId = startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: planId,\n delta: `\\n\\n${plan}\\n\\n---\\n**Do you want to proceed with this plan?** (yes/no)\\n`,\n })\n endTextBlock()\n } else if (tc.name.startsWith(PROXY_TOOL_PREFIX)) {\n log.debug(\"ignoring proxy tool_use block; broker handles it\", {\n name: tc.name,\n id: tc.id,\n })\n } else {\n const {\n name: mappedName,\n input: mappedInput,\n executed,\n skip,\n } = mapTool(tc.name, parsedInput, {\n webSearch: self.config.webSearch,\n sessionId: getClaudeSessionId(sk),\n toolUseId: tc.id,\n })\n\n if (!skip) {\n toolCallsById.set(tc.id, {\n id: tc.id,\n name: tc.name,\n input: parsedInput,\n })\n if (!executed) skipResultForIds.add(tc.id)\n\n controller.enqueue({\n type: \"tool-call\",\n toolCallId: tc.id,\n toolName: mappedName,\n input: JSON.stringify(mappedInput),\n providerExecuted: executed,\n } as any)\n }\n log.info(\"tool call complete\", {\n name: tc.name,\n mappedName,\n id: tc.id,\n executed,\n })\n }\n }\n }\n\n // Capture protocol-level stop_reason from the streaming\n // `message_delta` event (sent right before the final\n // `message_stop`). Any non-empty value is the source-of-truth\n // for why the turn ended — used to bypass the keyword heuristic.\n if (\n gotPartialEvents &&\n msg.type === \"message_delta\" &&\n typeof (msg as any).delta?.stop_reason === \"string\"\n ) {\n lastStopReason = (msg as any).delta.stop_reason\n }\n\n // assistant message (complete, not streaming).\n // When --include-partial-messages is on, this is a duplicate of\n // what we already streamed via content_block_* events. Skip it\n // for content, but still capture stop_reason from it for the\n // non-partial path.\n if (\n msg.type === \"assistant\" &&\n msg.message &&\n typeof (msg.message as any).stop_reason === \"string\"\n ) {\n lastStopReason = (msg.message as any).stop_reason\n }\n // Fallback: extract thinking from the complete assistant\n // message. opus-4-7's CLI strips thinking_delta from stream\n // events but may include thinking in the final message.\n if (\n msg.type === \"assistant\" &&\n msg.message?.content &&\n gotPartialEvents\n ) {\n const thinkingBlocks = (msg.message.content as any[]).filter(\n (b) => b.type === \"thinking\",\n )\n if (thinkingBlocks.length > 0) {\n log.info(\"assistant message thinking blocks\", {\n count: thinkingBlocks.length,\n hasText: thinkingBlocks.some(\n (b) => typeof b.thinking === \"string\" && b.thinking.length > 0,\n ),\n hadStreamThinking: hadThinkingTextFromStream,\n })\n if (!hadThinkingTextFromStream) {\n for (const block of thinkingBlocks) {\n if (block.thinking && block.thinking.length > 0) {\n noteReasoning()\n hadThinkingTextFromStream = true\n const thinkingId = generateId()\n controller.enqueue({\n type: \"reasoning-start\",\n id: thinkingId,\n } as any)\n controller.enqueue({\n type: \"reasoning-delta\",\n id: thinkingId,\n delta: block.thinking,\n } as any)\n controller.enqueue({\n type: \"reasoning-end\",\n id: thinkingId,\n } as any)\n }\n }\n }\n }\n }\n if (\n msg.type === \"assistant\" &&\n msg.message?.content &&\n !gotPartialEvents\n ) {\n const hasText = msg.message.content.some(\n (b: any) => b.type === \"text\" && b.text,\n )\n const hasToolUse = msg.message.content.some(\n (b: any) => b.type === \"tool_use\",\n )\n\n if (hasText) {\n hasReceivedContent = true\n }\n\n if (hasText && !hasToolUse) {\n startResultFallback()\n }\n if (hasToolUse) {\n clearFallbackTimer()\n }\n\n for (const block of msg.message.content) {\n if (block.type === \"text\" && block.text) {\n // New text block — keep only this block's text in the\n // last-block buffer for final-answer detection.\n resetLastVisibleTextBlock()\n const blockId = startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: blockId,\n delta: block.text,\n })\n endTextBlock()\n noteVisibleText(block.text)\n hasReceivedContent = true\n }\n\n if (block.type === \"thinking\" && block.thinking) {\n noteReasoning()\n const thinkingId = generateId()\n controller.enqueue({\n type: \"reasoning-start\",\n id: thinkingId,\n } as any)\n controller.enqueue({\n type: \"reasoning-delta\",\n id: thinkingId,\n delta: block.thinking,\n } as any)\n controller.enqueue({\n type: \"reasoning-end\",\n id: thinkingId,\n } as any)\n }\n\n if (block.type === \"tool_use\" && block.id && block.name) {\n noteToolActivity()\n const parsedInput = (block.input ?? {}) as Record<\n string,\n unknown\n >\n toolCallsById.set(block.id, {\n id: block.id,\n name: block.name,\n input: parsedInput,\n })\n\n if (isAskUserQuestionTool(block.name)) {\n const askId = startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: askId,\n delta: formatAskUserQuestion(parsedInput),\n })\n endTextBlock()\n } else if (block.name === \"ExitPlanMode\") {\n const plan = (parsedInput?.plan as string) || \"\"\n\n const planId = startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: planId,\n delta: `\\n\\n${plan}\\n\\n---\\n**Do you want to proceed with this plan?** (yes/no)\\n`,\n })\n endTextBlock()\n } else if (block.name.startsWith(PROXY_TOOL_PREFIX)) {\n log.debug(\"ignoring proxy tool_use from assistant message\", {\n name: block.name,\n id: block.id,\n })\n } else {\n const {\n name: mappedName,\n input: mappedInput,\n executed,\n skip,\n } = mapTool(block.name, parsedInput, {\n webSearch: self.config.webSearch,\n sessionId: getClaudeSessionId(sk),\n toolUseId: block.id,\n })\n\n if (!skip) {\n if (!executed) skipResultForIds.add(block.id)\n controller.enqueue({\n type: \"tool-input-start\",\n id: block.id,\n toolName: mappedName,\n providerExecuted: executed,\n } as any)\n controller.enqueue({\n type: \"tool-call\",\n toolCallId: block.id,\n toolName: mappedName,\n input: JSON.stringify(mappedInput),\n providerExecuted: executed,\n } as any)\n }\n log.info(\"tool_use from assistant message\", {\n name: block.name,\n mappedName,\n id: block.id,\n executed,\n })\n }\n }\n\n if (block.type === \"tool_result\") {\n log.debug(\"tool_result\", {\n toolUseId: block.tool_use_id,\n })\n }\n }\n }\n\n // user message (tool results from Claude CLI)\n if (msg.type === \"user\" && msg.message?.content) {\n for (const block of msg.message.content) {\n if (block.type === \"tool_result\" && block.tool_use_id) {\n if (skipResultForIds.has(block.tool_use_id)) {\n log.debug(\"skipping tool-result (opencode runs it)\", {\n toolUseId: block.tool_use_id,\n })\n continue\n }\n\n let resultText = \"\"\n if (typeof block.content === \"string\") {\n resultText = block.content\n } else if (Array.isArray(block.content)) {\n resultText = block.content\n .filter(\n (\n c,\n ): c is { type: string; text: string } =>\n c.type === \"text\" &&\n typeof c.text === \"string\",\n )\n .map((c) => c.text)\n .join(\"\\n\")\n }\n\n // Ledger hook: commit pending TaskCreate to opencode's todo\n // panel via a synthetic todowrite emission. Pass-through —\n // returns null for non-TaskCreate ids, so cheap and silent.\n const claudeSessionId = getClaudeSessionId(sk)\n if (claudeSessionId) {\n const list = applyTaskCreateToolResult(\n claudeSessionId,\n block.tool_use_id,\n resultText,\n )\n if (list) {\n const synthId = `todowrite_${block.tool_use_id}`\n controller.enqueue({\n type: \"tool-input-start\",\n id: synthId,\n toolName: \"todowrite\",\n providerExecuted: false,\n } as any)\n controller.enqueue({\n type: \"tool-call\",\n toolCallId: synthId,\n toolName: \"todowrite\",\n input: JSON.stringify({\n todos: list.map((t) => ({\n id: t.id,\n content: t.content,\n status: t.status,\n priority: \"medium\",\n })),\n }),\n providerExecuted: false,\n } as any)\n noteToolActivity()\n }\n }\n\n const toolCall = toolCallsById.get(block.tool_use_id)\n if (toolCall) {\n controller.enqueue({\n type: \"tool-result\",\n toolCallId: block.tool_use_id,\n toolName: toolCall.name,\n result: {\n output: resultText,\n title: toolCall.name,\n metadata: {},\n },\n providerExecuted: true,\n } as any)\n noteToolActivity()\n log.info(\"tool result emitted\", {\n toolUseId: block.tool_use_id,\n name: toolCall.name,\n })\n toolCallsById.delete(block.tool_use_id)\n }\n }\n }\n }\n\n // result - end of conversation turn\n if (msg.type === \"result\") {\n clearFallbackTimer()\n\n if (msg.session_id) {\n setClaudeSessionId(sk, msg.session_id)\n }\n\n // Some CLI failures only include user-readable text in\n // `result.result` (no prior assistant text blocks). Emit it so\n // opencode users don't see a blank turn.\n if (\n !currentTextId &&\n msg.is_error &&\n typeof msg.result === \"string\" &&\n msg.result.trim().length > 0\n ) {\n const errId = startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: errId,\n delta: msg.result,\n })\n }\n\n resultMeta = {\n sessionId: msg.session_id,\n costUsd: msg.total_cost_usd,\n durationMs: msg.duration_ms,\n usage: msg.usage,\n }\n\n log.info(\"conversation result\", {\n sessionId: msg.session_id,\n durationMs: msg.duration_ms,\n numTurns: msg.num_turns,\n isError: msg.is_error,\n })\n\n turnCompleted = true\n\n endTextBlock()\n\n // Drain race / abandoned-call guard. If Claude CLI emitted\n // `result` while a proxy tool call is still pending — either\n // because the 100ms drain timer hasn't fired yet, or because\n // Claude CLI gave up on its MCP HTTP request after an internal\n // timeout — drain it through the normal tool-calls flow so\n // opencode executes the tool; otherwise reject any orphan\n // pending calls so proxy-mcp returns to the HTTP caller\n // immediately instead of hanging until the broker's 10-minute\n // timeout (which surfaces as a hard 2-minute \"operation timed\n // out\" on the SDK side).\n if (drainBuffer.length > 0) {\n log.info(\n \"draining pending proxy calls at turn-result boundary\",\n {\n sessionKey: sk,\n count: drainBuffer.length,\n },\n )\n drainNow()\n return\n }\n const orphanPending = getPendingProxyCalls(sk)\n if (orphanPending.length > 0) {\n log.warn(\n \"rejecting orphan pending proxy calls at turn-result boundary\",\n {\n sessionKey: sk,\n count: orphanPending.length,\n },\n )\n rejectAllPendingProxyCallsForSession(\n sk,\n new Error(\n \"Claude CLI emitted result with pending proxy calls not in drain buffer\",\n ),\n )\n }\n\n const autoDecision = shouldAutoContinueIncompleteTurn(\n autoContinueState,\n {\n text: visibleTextSinceContinue,\n lastVisibleText: lastVisibleTextSinceContinue,\n hadReasoning: hadReasoningSinceContinue,\n hadToolActivity: hadToolActivitySinceContinue,\n hadProxyActivity: hadProxyActivitySinceContinue,\n isError: msg.is_error,\n stopReason: lastStopReason,\n },\n )\n if (autoDecision.continue) {\n const signature = continuationSignature({\n text: visibleTextSinceContinue,\n lastVisibleText: lastVisibleTextSinceContinue,\n hadReasoning: hadReasoningSinceContinue,\n hadToolActivity: hadToolActivitySinceContinue,\n hadProxyActivity: hadProxyActivitySinceContinue,\n isError: msg.is_error,\n })\n autoContinueState.noProgressCount =\n signature === autoContinueState.lastSignature\n ? autoContinueState.noProgressCount + 1\n : 0\n autoContinueState.lastSignature = signature\n autoContinueState.attempts++\n log.notice(\"auto-continuing incomplete claude result\", {\n sessionKey: sk,\n reason: autoDecision.reason,\n attempts: autoContinueState.attempts,\n textLength: visibleTextSinceContinue.length,\n lastTextLength: lastVisibleTextSinceContinue.length,\n hadReasoning: hadReasoningSinceContinue,\n hadToolActivity: hadToolActivitySinceContinue,\n hadProxyActivity: hadProxyActivitySinceContinue,\n })\n turnCompleted = false\n resetAutoContinueWindow()\n proc.stdin?.write(makeAutoContinueMessage() + \"\\n\")\n return\n }\n log.notice(\"auto-continuation stopped\", {\n sessionKey: sk,\n reason: autoDecision.reason,\n stopReason: lastStopReason,\n attempts: autoContinueState.attempts,\n textLength: visibleTextSinceContinue.length,\n lastTextLength: lastVisibleTextSinceContinue.length,\n hadReasoning: hadReasoningSinceContinue,\n hadToolActivity: hadToolActivitySinceContinue,\n hadProxyActivity: hadProxyActivitySinceContinue,\n })\n\n for (const [idx, reasoningId] of reasoningIds) {\n if (reasoningStarted.get(idx)) {\n controller.enqueue({\n type: \"reasoning-end\",\n id: reasoningId,\n } as any)\n }\n }\n\n controller.enqueue({\n type: \"finish\",\n finishReason: toFinishReason(\"stop\"),\n usage: toUsage(msg.usage),\n providerMetadata: {\n \"claude-code\": {\n ...resultMeta,\n ...(compactionMode\n ? { compactionModel: effectiveModelId }\n : {}),\n },\n ...(typeof msg.usage?.cache_creation_input_tokens === \"number\"\n ? {\n anthropic: {\n cacheCreationInputTokens:\n msg.usage.cache_creation_input_tokens,\n },\n }\n : {}),\n },\n })\n\n controllerClosed = true\n cleanupTurn()\n\n try {\n controller.close()\n } catch {}\n }\n } catch (e) {\n log.debug(\"failed to parse line\", {\n error:\n e instanceof Error ? e.message : String(e),\n })\n }\n }\n\n const closeHandler = () => {\n log.debug(\"readline closed\")\n if (controllerClosed) return\n // Claude CLI's stdio is gone. The proxy-mcp HTTP requests that\n // backed any pending tool calls have no one to answer them now —\n // reject so the handlers return errors rather than hang.\n if (drainBuffer.length > 0 || getPendingProxyCalls(sk).length > 0) {\n rejectAllPendingProxyCallsForSession(\n sk,\n new Error(\n \"Claude CLI subprocess closed before pending tool calls were resolved\",\n ),\n )\n drainBuffer.length = 0\n }\n controllerClosed = true\n cleanupTurn()\n endTextBlock()\n controller.enqueue({\n type: \"finish\",\n finishReason: toFinishReason(\"stop\"),\n usage: toUsage(),\n providerMetadata: {\n \"claude-code\": {\n ...resultMeta,\n ...(compactionMode\n ? { compactionModel: effectiveModelId }\n : {}),\n },\n },\n })\n try {\n controller.close()\n } catch {}\n }\n\n // Centralised per-turn teardown. Every exit path funnels through here\n // so we don't accumulate listeners across turns on a reused process.\n let cleanedUp = false\n const cleanupTurn = () => {\n if (cleanedUp) return\n cleanedUp = true\n clearFallbackTimer()\n if (drainTimer) {\n clearTimeout(drainTimer)\n drainTimer = null\n }\n lineEmitter.off(\"line\", lineHandler)\n lineEmitter.off(\"close\", closeHandler)\n pendingProxyUnsubscribe?.()\n pendingProxyUnsubscribe = null\n proc.off(\"error\", procErrorHandler)\n }\n\n const procErrorHandler = (err: Error) => {\n log.error(\"process error\", { error: err.message })\n if (controllerClosed) return\n // Subprocess failure invalidates every pending HTTP-bound tool\n // call for this session. Reject them so proxy-mcp returns errors\n // to Claude rather than letting the sockets stall.\n if (drainBuffer.length > 0 || getPendingProxyCalls(sk).length > 0) {\n rejectAllPendingProxyCallsForSession(\n sk,\n new Error(\n `Claude CLI subprocess error: ${err.message}`,\n ),\n )\n drainBuffer.length = 0\n }\n controllerClosed = true\n cleanupTurn()\n controller.enqueue({ type: \"error\", error: err })\n try {\n controller.close()\n } catch {}\n }\n\n lineEmitter.on(\"line\", lineHandler)\n lineEmitter.on(\"close\", closeHandler)\n\n pendingProxyUnsubscribe = onPendingProxyCall(sk, (call) => {\n if (controllerClosed) {\n // Stream already closed (we already drained). Late arrival —\n // reject immediately so the proxy-mcp HTTP request returns\n // instead of hanging until its 10-min timeout.\n log.warn(\n \"pending proxy call arrived after stream close; rejecting\",\n {\n sessionKey: sk,\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n },\n )\n rejectPendingProxyCallById(\n call.toolCallId,\n new Error(\n `Pending proxy call '${call.toolName}' arrived after the stream was already closed`,\n ),\n )\n return\n }\n log.info(\"received pending proxy call for session\", {\n sessionKey: sk,\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n })\n noteProxyActivity()\n noteToolActivity()\n drainBuffer.push(call)\n if (drainTimer) clearTimeout(drainTimer)\n drainTimer = setTimeout(drainNow, DRAIN_QUIET_MS)\n })\n\n proc.on(\"error\", procErrorHandler)\n\n // On abort, keep process alive for next message\n if (options.abortSignal) {\n options.abortSignal.addEventListener(\"abort\", () => {\n autoContinueState.aborted = true\n if (turnCompleted || controllerClosed) return\n\n if (!hasReceivedContent) {\n log.info(\n \"abort signal received before content, closing stream immediately\",\n { cwd },\n )\n controllerClosed = true\n cleanupTurn()\n try {\n controller.close()\n } catch {}\n return\n }\n\n log.info(\n \"abort signal received mid-turn, starting grace period\",\n { cwd },\n )\n // Abort grace period — short, since the user already asked to stop.\n startResultFallback(5_000)\n })\n }\n\n if (hasMatchedPendingResults) {\n // Tool-result turn: the prompt carries opencode's results for the\n // proxy tool calls we drained on the previous turn. Resolve each\n // matched call (claude CLI's HTTP handlers wake up and continue).\n // Any pending calls without a matching tool-result are orphans\n // (rare protocol anomaly); reject them so claude CLI doesn't hang\n // on those HTTP requests.\n for (const { call, result } of previousPendingProxyMatches) {\n if (result) {\n log.info(\"resolving pending proxy call from tool result prompt\", {\n sessionKey: sk,\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n })\n resolvePendingProxyCallById(call.toolCallId, result)\n } else {\n log.notice(\n \"pending proxy call had no matching tool-result; rejecting as orphan\",\n {\n sessionKey: sk,\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n },\n )\n rejectPendingProxyCallById(\n call.toolCallId,\n new Error(\n `Pending proxy call '${call.toolName}' (${call.toolCallId}) was not matched in tool-result turn; rejecting as orphaned`,\n ),\n )\n }\n }\n return\n }\n\n // No pending calls had matching tool-results. If any pending calls\n // are still hanging around from a prior turn, reject them so the\n // HTTP handlers in proxy-mcp don't sit blocked forever while we\n // proceed with a brand new user message.\n if (previousPendingProxyCalls.length > 0) {\n for (const call of previousPendingProxyCalls) {\n rejectPendingProxyCallById(\n call.toolCallId,\n new Error(\n `Pending proxy call '${call.toolName}' (${call.toolCallId}) was orphaned by a new user turn; rejecting`,\n ),\n )\n }\n }\n\n // Send the user message for a fresh turn.\n proc.stdin?.write(userMsg + \"\\n\")\n log.debug(\"sent user message\", { textLength: userMsg.length })\n }\n\n void setup().catch((err) => {\n log.error(\"failed to set up doStream\", {\n error: err instanceof Error ? err.message : String(err),\n })\n controller.enqueue({\n type: \"error\",\n error: err instanceof Error ? err : new Error(String(err)),\n })\n try {\n controller.close()\n } catch {}\n })\n },\n cancel() {\n // Consumer cancelled the stream\n },\n })\n\n return {\n stream,\n request: { body: { text: userMsg } },\n response: { headers: {} },\n }\n }\n}\n","import { appendFileSync, mkdirSync, renameSync, statSync } from \"node:fs\"\nimport { homedir } from \"node:os\"\nimport { dirname, join } from \"node:path\"\n\nexport type LogLevel = \"debug\" | \"info\" | \"notice\" | \"warn\" | \"error\"\nexport type LogMode = \"silent\" | \"debug\"\n\nexport interface LoggerConfig {\n file: boolean\n dir: string | null\n mode: LogMode\n level: LogLevel\n}\n\nconst LEVEL_RANK: Record<LogLevel, number> = {\n debug: 0,\n info: 1,\n notice: 2,\n warn: 3,\n error: 4,\n}\n\nconst MAX_LOG_BYTES = 5 * 1024 * 1024 // 5 MB\nconst DEFAULT_DIR = join(homedir(), \".local\", \"share\", \"opencode-claude-code\")\n\nconst DEFAULT_CONFIG: LoggerConfig = {\n file: false,\n dir: null,\n mode: \"silent\",\n level: \"info\",\n}\n\nfunction parseBoolEnv(v: string | undefined): boolean | undefined {\n if (v == null) return undefined\n const s = v.toLowerCase().trim()\n if (s === \"\") return undefined\n if (s === \"0\" || s === \"false\" || s === \"no\" || s === \"off\") return false\n return true\n}\n\nfunction parseLevelEnv(v: string | undefined): LogLevel | undefined {\n if (v == null) return undefined\n const s = v.toLowerCase().trim()\n if (s === \"\") return undefined\n if (s === \"debug\" || s === \"info\" || s === \"notice\" || s === \"warn\" || s === \"error\") {\n return s\n }\n return undefined\n}\n\nfunction parseModeFromDebugEnv(v: string | undefined): LogMode | undefined {\n if (v == null || v === \"\") return undefined\n return v.includes(\"opencode-claude-code\") ? \"debug\" : undefined\n}\n\nfunction withEnvOverrides(base: LoggerConfig): LoggerConfig {\n const result: LoggerConfig = { ...base }\n const envFile = parseBoolEnv(process.env.OPENCODE_CLAUDE_CODE_LOG_FILE)\n if (envFile !== undefined) result.file = envFile\n const envDir = process.env.OPENCODE_CLAUDE_CODE_LOG_DIR\n if (envDir !== undefined && envDir !== \"\") result.dir = envDir\n const envMode = parseModeFromDebugEnv(process.env.DEBUG)\n if (envMode !== undefined) result.mode = envMode\n const envLevel = parseLevelEnv(process.env.OPENCODE_CLAUDE_CODE_LOG_LEVEL)\n if (envLevel !== undefined) result.level = envLevel\n return result\n}\n\nlet activeConfig: LoggerConfig = withEnvOverrides(DEFAULT_CONFIG)\nlet fileLoggingDisabled = false\n\n/**\n * Configure the logger from plugin settings. Env vars override the supplied\n * config when explicitly set, so a developer can flip behavior for a single\n * process without editing opencode.jsonc.\n *\n * `OPENCODE_CLAUDE_CODE_LOG_FILE` → `file` (1/true/on/yes vs 0/false/no/off)\n * `OPENCODE_CLAUDE_CODE_LOG_DIR` → `dir`\n * `DEBUG=opencode-claude-code` → `mode: \"debug\"`\n * `OPENCODE_CLAUDE_CODE_LOG_LEVEL` → `level` (debug | info | notice | warn | error)\n */\nexport function configureLogger(input: Partial<LoggerConfig>): void {\n const merged: LoggerConfig = { ...DEFAULT_CONFIG, ...input }\n activeConfig = withEnvOverrides(merged)\n fileLoggingDisabled = false\n}\n\nexport function getLoggerConfig(): LoggerConfig {\n return { ...activeConfig }\n}\n\n/** Test-only helper. Resets to defaults+env so tests are deterministic. */\nexport function _resetLoggerForTests(): void {\n activeConfig = withEnvOverrides(DEFAULT_CONFIG)\n fileLoggingDisabled = false\n}\n\nfunction resolvedLogFile(): string {\n return join(activeConfig.dir ?? DEFAULT_DIR, \"plugin.log\")\n}\n\nfunction rotateIfNeeded(logFile: string): void {\n try {\n const stat = statSync(logFile)\n if (stat.size > MAX_LOG_BYTES) {\n renameSync(logFile, `${logFile}.1`)\n }\n } catch {\n // file does not exist yet — nothing to rotate\n }\n}\n\nfunction writeToFile(line: string): void {\n if (!activeConfig.file) return\n if (fileLoggingDisabled) return\n try {\n const logFile = resolvedLogFile()\n mkdirSync(dirname(logFile), { recursive: true })\n rotateIfNeeded(logFile)\n appendFileSync(logFile, line + \"\\n\", \"utf8\")\n } catch {\n // Disable on first failure to avoid spamming errors on a read-only FS.\n fileLoggingDisabled = true\n }\n}\n\nfunction fmt(level: string, msg: string, data?: Record<string, unknown>): string {\n const ts = new Date().toISOString()\n const base = `[${ts}] [opencode-claude-code] ${level}: ${msg}`\n if (data && Object.keys(data).length > 0) {\n return `${base} ${JSON.stringify(data)}`\n }\n return base\n}\n\nfunction shouldEmit(level: LogLevel): boolean {\n return LEVEL_RANK[level] >= LEVEL_RANK[activeConfig.level]\n}\n\nfunction shouldTui(level: LogLevel): boolean {\n // warn/error are alwaysStderr: a developer who passes the level threshold\n // should still see real problems in the TUI regardless of mode. Below-\n // threshold entries are filtered earlier by shouldEmit().\n if (level === \"warn\" || level === \"error\") return true\n return activeConfig.mode === \"debug\"\n}\n\nfunction emit(level: LogLevel, msg: string, data?: Record<string, unknown>): void {\n if (!shouldEmit(level)) return\n const line = fmt(level.toUpperCase(), msg, data)\n if (shouldTui(level)) {\n console.error(line)\n }\n writeToFile(line)\n}\n\nexport const log = {\n debug(msg: string, data?: Record<string, unknown>) {\n emit(\"debug\", msg, data)\n },\n info(msg: string, data?: Record<string, unknown>) {\n emit(\"info\", msg, data)\n },\n notice(msg: string, data?: Record<string, unknown>) {\n emit(\"notice\", msg, data)\n },\n warn(msg: string, data?: Record<string, unknown>) {\n emit(\"warn\", msg, data)\n },\n error(msg: string, data?: Record<string, unknown>) {\n emit(\"error\", msg, data)\n },\n}\n","import { log } from \"./logger.js\"\n\nexport type TodoStatus = \"pending\" | \"in_progress\" | \"completed\"\n\nexport interface TodoEntry {\n id: string\n content: string\n status: TodoStatus\n}\n\ninterface PendingCreate {\n subject: string\n createdAt: number\n}\n\ninterface SessionLedger {\n todos: Map<string, TodoEntry>\n pendingCreates: Map<string, PendingCreate>\n}\n\nconst ledgers = new Map<string, SessionLedger>()\n\nconst PENDING_CREATE_TTL_MS = 60_000\nconst TASK_CREATED_PATTERN = /Task\\s*#?\\s*(\\d+)\\s+created/i\nconst VALID_STATUSES: ReadonlySet<TodoStatus> = new Set([\"pending\", \"in_progress\", \"completed\"])\n\nfunction getOrCreate(sessionId: string): SessionLedger {\n let ledger = ledgers.get(sessionId)\n if (!ledger) {\n ledger = { todos: new Map(), pendingCreates: new Map() }\n ledgers.set(sessionId, ledger)\n }\n return ledger\n}\n\nfunction prunePending(ledger: SessionLedger): void {\n const cutoff = Date.now() - PENDING_CREATE_TTL_MS\n for (const [id, pending] of ledger.pendingCreates) {\n if (pending.createdAt < cutoff) ledger.pendingCreates.delete(id)\n }\n}\n\nfunction materialize(ledger: SessionLedger): TodoEntry[] {\n return Array.from(ledger.todos.values())\n}\n\nfunction resolveSubject(input: { subject?: unknown; description?: unknown } | undefined): string {\n const subject = typeof input?.subject === \"string\" ? input.subject.trim() : \"\"\n if (subject) return subject\n const description = typeof input?.description === \"string\" ? input.description.trim() : \"\"\n if (description) return description\n return \"(no subject)\"\n}\n\nexport function applyTaskCreateToolUse(\n sessionId: string,\n toolUseId: string,\n input: { subject?: unknown; description?: unknown } | undefined,\n): void {\n if (!sessionId || !toolUseId) return\n const ledger = getOrCreate(sessionId)\n prunePending(ledger)\n ledger.pendingCreates.set(toolUseId, {\n subject: resolveSubject(input),\n createdAt: Date.now(),\n })\n}\n\nexport function applyTaskCreateToolResult(\n sessionId: string,\n toolUseId: string,\n resultText: string,\n): TodoEntry[] | null {\n if (!sessionId || !toolUseId) return null\n const ledger = ledgers.get(sessionId)\n if (!ledger) return null\n const pending = ledger.pendingCreates.get(toolUseId)\n if (!pending) return null\n ledger.pendingCreates.delete(toolUseId)\n const match = typeof resultText === \"string\" ? resultText.match(TASK_CREATED_PATTERN) : null\n if (!match) {\n log.debug(\"TaskCreate result did not match expected format\", { sessionId, toolUseId, resultText })\n return null\n }\n const claudeId = match[1]\n if (ledger.todos.has(claudeId)) {\n log.debug(\"TaskCreate result for already-known claude id; overwriting\", { sessionId, claudeId })\n }\n ledger.todos.set(claudeId, { id: claudeId, content: pending.subject, status: \"pending\" })\n return materialize(ledger)\n}\n\nexport function applyTaskUpdate(\n sessionId: string,\n input: { taskId?: unknown; subject?: unknown; status?: unknown } | undefined,\n): TodoEntry[] | null {\n if (!sessionId) return null\n const taskId = typeof input?.taskId === \"string\" ? input.taskId : null\n if (!taskId) return null\n const ledger = ledgers.get(sessionId)\n if (!ledger) return null\n const entry = ledger.todos.get(taskId)\n if (!entry) {\n log.debug(\"TaskUpdate for unknown task id\", { sessionId, taskId })\n return null\n }\n if (input?.status === \"deleted\") {\n ledger.todos.delete(taskId)\n return materialize(ledger)\n }\n if (typeof input?.status === \"string\" && VALID_STATUSES.has(input.status as TodoStatus)) {\n entry.status = input.status as TodoStatus\n }\n if (typeof input?.subject === \"string\" && input.subject.trim().length > 0) {\n entry.content = input.subject.trim()\n }\n return materialize(ledger)\n}\n\nexport function clearLedger(sessionId: string): void {\n if (!sessionId) return\n ledgers.delete(sessionId)\n}\n\nexport function getLedger(sessionId: string): TodoEntry[] {\n const ledger = ledgers.get(sessionId)\n if (!ledger) return []\n return materialize(ledger)\n}\n\nexport function _resetAllLedgersForTests(): void {\n ledgers.clear()\n}\n","import { log } from \"./logger.js\"\nimport { applyTaskCreateToolUse, applyTaskUpdate, type TodoEntry } from \"./todo-ledger.js\"\nimport type { WebSearchRouting } from \"./types.js\"\n\nexport interface MapToolOptions {\n webSearch?: WebSearchRouting\n sessionId?: string\n toolUseId?: string\n}\n\n/**\n * Map Claude CLI tool input (snake_case) to OpenCode tool input (camelCase)\n */\nfunction mapToolInput(name: string, input: any): any {\n if (!input) return input\n\n switch (name) {\n case \"Write\":\n return {\n filePath: input.file_path ?? input.filePath,\n content: input.content,\n }\n case \"Edit\":\n return {\n filePath: input.file_path ?? input.filePath,\n oldString: input.old_string ?? input.oldString,\n newString: input.new_string ?? input.newString,\n replaceAll: input.replace_all ?? input.replaceAll,\n }\n case \"Read\":\n return {\n filePath: input.file_path ?? input.filePath,\n offset: input.offset,\n limit: input.limit,\n }\n case \"Bash\":\n return {\n command: input.command,\n description:\n input.description ||\n `Execute: ${String(input.command || \"\").slice(0, 50)}${String(input.command || \"\").length > 50 ? \"...\" : \"\"}`,\n timeout: input.timeout,\n }\n case \"NotebookEdit\":\n return {\n notebookPath: input.notebook_path ?? input.notebookPath,\n cellNumber: input.cell_number ?? input.cellNumber,\n newSource: input.new_source ?? input.newSource,\n cellType: input.cell_type ?? input.cellType,\n editMode: input.edit_mode ?? input.editMode,\n }\n case \"Glob\":\n return {\n pattern: input.pattern,\n path: input.path,\n }\n case \"Grep\":\n return {\n pattern: input.pattern,\n path: input.path,\n include: input.include,\n }\n case \"TodoWrite\":\n if (Array.isArray(input.todos)) {\n const mappedTodos = input.todos.map((todo: any, index: number) => ({\n content: todo.content,\n status: todo.status || \"pending\",\n priority: todo.priority || \"medium\",\n id: todo.id || `todo_${Date.now()}_${index}`,\n }))\n return { todos: mappedTodos }\n }\n return input\n default:\n return input\n }\n}\n\n// Tools that Claude CLI executes internally but we report to opencode for UI display\nconst OPENCODE_HANDLED_TOOLS = new Set([\n \"Edit\",\n \"Write\",\n \"Bash\",\n \"NotebookEdit\",\n \"Read\",\n \"Glob\",\n \"Grep\",\n])\n\n// Claude CLI internal tools that should not be forwarded to opencode.\n// These are part of Claude Code's own system and have no opencode equivalent.\n// Tools the Claude CLI emits for its own internal bookkeeping (sub-agents,\n// task tracking, search). opencode has no matching tool registry entry, so\n// forwarding them surfaces as `⚙ invalid` rows in the UI. Skip them.\n// TaskOutput is intentionally NOT here — it has an explicit bash-echo mapping\n// below so the result stays visible.\nconst CLAUDE_INTERNAL_TOOLS = new Set([\n \"ToolSearch\",\n \"Agent\",\n \"AskFollowupQuestion\",\n \"TaskList\",\n \"TaskGet\",\n \"TaskStop\",\n])\n\nfunction emitTodoWrite(todos: TodoEntry[]) {\n return {\n name: \"todowrite\",\n input: {\n todos: todos.map((todo) => ({\n id: todo.id,\n content: todo.content,\n status: todo.status,\n priority: \"medium\",\n })),\n },\n executed: false,\n }\n}\n\nexport function mapTool(\n name: string,\n input?: any,\n opts?: MapToolOptions,\n): { name: string; input?: any; executed: boolean; skip?: boolean } {\n // Claude CLI internal tools — skip entirely\n if (CLAUDE_INTERNAL_TOOLS.has(name)) {\n log.debug(\"skipping Claude CLI internal tool\", { name })\n return { name, input, executed: true, skip: true }\n }\n\n // TaskCreate: stash subject keyed by tool_use_id; emission happens on tool_result.\n // Without sessionId+toolUseId we cannot maintain the ledger, so fall back to skip\n // (preserves old behavior for callers that haven't been threaded yet).\n if (name === \"TaskCreate\") {\n if (opts?.sessionId && opts?.toolUseId) {\n applyTaskCreateToolUse(opts.sessionId, opts.toolUseId, input)\n }\n return { name, input, executed: true, skip: true }\n }\n\n // TaskUpdate: mutate ledger and emit full list as opencode todowrite. Without\n // sessionId, fall back to skip. Unknown task ids return null from the ledger\n // and we drop the event.\n if (name === \"TaskUpdate\") {\n if (opts?.sessionId) {\n const list = applyTaskUpdate(opts.sessionId, input)\n if (list !== null) return emitTodoWrite(list)\n }\n return { name, input, executed: true, skip: true }\n }\n\n // Plan mode tools\n if (name === \"EnterPlanMode\") return { name: \"plan_enter\", input: {}, executed: false }\n if (name === \"ExitPlanMode\") return { name: \"plan_exit\", input, executed: false }\n\n // TodoWrite needs opencode to run it locally so Todo.Service (and the UI\n // widget backed by it) gets populated. Reporting as provider-executed would\n // short-circuit opencode's own execute and leave the todo panel empty.\n if (name === \"TodoWrite\") {\n const mappedInput = mapToolInput(name, input)\n return { name: \"todowrite\", input: mappedInput, executed: false }\n }\n\n // WebSearch — routing controlled by config.webSearch\n if (name === \"WebSearch\" || name === \"web_search\") {\n const mappedInput = input?.query ? { query: input.query } : input\n const route = opts?.webSearch\n if (route && route !== \"claude\" && route !== \"disabled\") {\n log.debug(\"routing WebSearch to opencode tool\", { target: route, mappedInput })\n return { name: route, input: mappedInput, executed: false }\n }\n log.debug(\"WebSearch executed by Claude CLI\", { mappedInput })\n return { name: \"WebSearch\", input: mappedInput, executed: true }\n }\n\n // TaskOutput -> bash echo\n if (name === \"TaskOutput\") {\n if (!input) return { name: \"bash\", executed: false }\n const output = input?.content || input?.output || JSON.stringify(input)\n return {\n name: \"bash\",\n input: {\n command: `echo \"TASK OUTPUT: ${String(output).replace(/\"/g, '\\\\\"')}\"`,\n description: \"Displaying task output\",\n },\n executed: false,\n }\n }\n\n // Third-party MCP tools: mcp__<server>__<tool> -> <server>_<tool>.\n // Marked provider-executed because Claude CLI runs these internally via\n // its own --mcp-config; the tool-result is already in the stream. If we\n // reported executed:false, opencode would look up the tool in its own\n // registry, fail to find it, and emit an `invalid` tool error that\n // shadows the real result.\n //\n // Our own proxy tools (`mcp__opencode_proxy__*`) are filtered out by\n // callers before reaching here, so this branch only ever sees user MCP\n // servers configured in Claude CLI's settings.\n if (name.startsWith(\"mcp__\")) {\n const parts = name.slice(5).split(\"__\")\n if (parts.length >= 2) {\n const serverName = parts[0]\n const toolName = parts.slice(1).join(\"_\")\n const openCodeName = `${serverName}_${toolName}`\n log.debug(\"mapping MCP tool\", { original: name, mapped: openCodeName })\n return { name: openCodeName, input, executed: true }\n }\n }\n\n // Tools executed by Claude CLI internally - map to lowercase for opencode\n if (OPENCODE_HANDLED_TOOLS.has(name)) {\n const mappedInput = mapToolInput(name, input)\n const openCodeName = name.toLowerCase()\n log.debug(\"mapping CLI-executed tool\", { name, openCodeName })\n return { name: openCodeName, input: mappedInput, executed: true }\n }\n\n // Unknown tools - treated as provider-executed\n return { name, input, executed: true }\n}\n","import type { LanguageModelV3 } from \"@ai-sdk/provider\"\nimport { log } from \"./logger.js\"\nimport type { ReasoningEffort } from \"./types.js\"\n\ntype Prompt = Parameters<LanguageModelV3[\"doGenerate\"]>[0][\"prompt\"]\n\nconst THINKING_KEYWORDS: Record<ReasoningEffort, string | null> = {\n minimal: null,\n low: \"think\",\n medium: \"think hard\",\n high: \"think harder\",\n xhigh: \"megathink\",\n max: \"ultrathink\",\n}\n\nexport function reasoningKeyword(effort?: ReasoningEffort): string | null {\n if (!effort) return null\n return THINKING_KEYWORDS[effort] ?? null\n}\n\nconst SUPPORTED_IMAGE_TYPES = new Set([\n \"image/jpeg\",\n \"image/png\",\n \"image/gif\",\n \"image/webp\",\n])\n\nfunction toImageBlock(part: any): any | null {\n const raw: unknown = part.data ?? part.url ?? part.source?.data\n if (!raw) {\n log.warn(\"file part without data, skipping\")\n return null\n }\n\n let resolvedMediaType: string = part.mediaType || part.mimeType || part.mime || \"\"\n let base64: string | null = null\n\n if (typeof raw === \"string\") {\n if (raw.startsWith(\"data:\")) {\n const match = /^data:([^;,]+)(?:;[^,]*)*(?:;base64)?,(.*)$/s.exec(raw)\n if (!match) {\n log.warn(\"malformed data URI, skipping file part\")\n return null\n }\n resolvedMediaType = resolvedMediaType || match[1]\n base64 = match[2]\n } else if (/^https?:\\/\\//i.test(raw)) {\n log.warn(\"remote URL images are not supported by Claude CLI, skipping\")\n return null\n } else {\n base64 = raw\n }\n } else if (raw instanceof URL) {\n log.warn(\"remote URL images are not supported by Claude CLI, skipping\")\n return null\n } else if (raw instanceof Uint8Array || Buffer.isBuffer(raw)) {\n base64 = Buffer.from(raw as Uint8Array).toString(\"base64\")\n } else {\n log.warn(\"unsupported file part data type\", { dataType: typeof raw })\n return null\n }\n\n if (!resolvedMediaType || !SUPPORTED_IMAGE_TYPES.has(resolvedMediaType)) {\n log.warn(\"unsupported media type for Claude image block, skipping\", {\n mediaType: resolvedMediaType,\n })\n return null\n }\n\n return {\n type: \"image\",\n source: { type: \"base64\", media_type: resolvedMediaType, data: base64 },\n }\n}\n\nfunction getToolResultText(part: any): string {\n const value = part.output ?? part.result\n\n if (typeof value === \"string\") {\n return value\n }\n\n if (!value || typeof value !== \"object\") {\n return JSON.stringify(value)\n }\n\n switch (value.type) {\n case \"text\":\n case \"error-text\":\n return String(value.value)\n case \"json\":\n case \"error-json\":\n return JSON.stringify(value.value)\n case \"execution-denied\":\n return value.reason ? `Execution denied: ${value.reason}` : \"Execution denied\"\n case \"content\":\n return Array.isArray(value.value)\n ? value.value\n .map((item: any) => {\n if (item?.type === \"text\") return item.text\n return JSON.stringify(item)\n })\n .join(\"\\n\")\n : JSON.stringify(value.value)\n default:\n return JSON.stringify(value)\n }\n}\n\n// Compaction-mode caps. These are the only knobs that affect how much\n// transcript content reaches the model when opencode invokes /compact.\n// 180k chars ≈ 60k tokens worst-case — well under Haiku 4.5's 200k window\n// after accounting for system prompt + output budget.\nconst MAX_HISTORY_CHARS = 180_000\nconst MAX_TOOL_RESULT_CHARS = 10_000\nconst MAX_TOOL_INPUT_CHARS = 2_000\n\nfunction clipWithMarker(text: string, max: number): string {\n if (text.length <= max) return text\n return `${text.slice(0, max)}\\n…[truncated ${text.length - max} chars]`\n}\n\nfunction renderToolInput(input: unknown): string {\n let raw: string\n try {\n raw = typeof input === \"string\" ? input : JSON.stringify(input)\n } catch {\n raw = String(input)\n }\n return clipWithMarker(raw, MAX_TOOL_INPUT_CHARS)\n}\n\nfunction renderMessageContentForCompaction(\n msg: any,\n): { text: string; toolResultCount: number } {\n const lines: string[] = []\n let toolResultCount = 0\n\n if (typeof msg.content === \"string\") {\n return { text: msg.content, toolResultCount: 0 }\n }\n\n if (!Array.isArray(msg.content)) {\n return { text: \"\", toolResultCount: 0 }\n }\n\n for (const part of msg.content as any[]) {\n if (!part) continue\n switch (part.type) {\n case \"text\":\n if (part.text) lines.push(part.text)\n break\n case \"tool-call\":\n lines.push(\n `[tool_use:${part.toolName ?? \"unknown\"}(${renderToolInput(part.input)})]`,\n )\n break\n case \"tool-result\":\n toolResultCount++\n lines.push(\n `[tool_result:${part.toolName ?? part.toolCallId ?? \"unknown\"}]\\n${clipWithMarker(\n getToolResultText(part),\n MAX_TOOL_RESULT_CHARS,\n )}`,\n )\n break\n case \"image\":\n lines.push(\n `[image: ${part.mediaType ?? part.mimeType ?? \"unknown\"}]`,\n )\n break\n case \"file\":\n lines.push(\n `[file: ${part.mediaType ?? part.mimeType ?? \"unknown\"}]`,\n )\n break\n case \"reasoning\":\n // Skip reasoning blocks in compaction — they bloat input without\n // helping the summarizer.\n break\n }\n }\n\n return { text: lines.join(\"\\n\"), toolResultCount }\n}\n\n/**\n * Compact conversation history into a context summary.\n *\n * - mode \"fresh-session\" (default): legacy behavior. Filters to\n * user/assistant only, clips each message at 2000 chars, drops tool\n * payloads to placeholders. Used when starting a fresh CLI session\n * that lost its prior session id.\n * - mode \"compaction\": rich serializer for opencode /compact. Includes\n * tool roles, renders tool_use input and tool_result content (each\n * clipped at MAX_TOOL_RESULT_CHARS), and caps aggregate output at\n * MAX_HISTORY_CHARS by dropping oldest entries first.\n */\nexport function compactConversationHistory(\n prompt: Prompt,\n opts: { mode?: \"fresh-session\" | \"compaction\" } = {},\n): string | null {\n const mode = opts.mode ?? \"fresh-session\"\n\n if (mode === \"compaction\") {\n return buildCompactionHistory(prompt)\n }\n\n const conversationMessages = prompt.filter(\n (m) => m.role === \"user\" || m.role === \"assistant\",\n )\n\n if (conversationMessages.length <= 1) {\n return null\n }\n\n const historyParts: string[] = []\n\n for (let i = 0; i < conversationMessages.length - 1; i++) {\n const msg = conversationMessages[i]\n const role = msg.role === \"user\" ? \"User\" : \"Assistant\"\n\n let text = \"\"\n if (typeof msg.content === \"string\") {\n text = msg.content\n } else if (Array.isArray(msg.content)) {\n const textParts = (msg.content as any[])\n .filter((p) => p.type === \"text\" && p.text)\n .map((p) => p.text)\n text = textParts.join(\"\\n\")\n\n const toolCalls = (msg.content as any[]).filter(\n (p) => p.type === \"tool-call\",\n )\n const toolResults = (msg.content as any[]).filter(\n (p) => p.type === \"tool-result\",\n )\n\n if (toolCalls.length > 0) {\n text += `\\n[Called ${toolCalls.length} tool(s): ${toolCalls.map((t: any) => t.toolName).join(\", \")}]`\n }\n if (toolResults.length > 0) {\n text += `\\n[Received ${toolResults.length} tool result(s)]`\n }\n }\n\n if (text.trim()) {\n const truncated =\n text.length > 2000 ? text.slice(0, 2000) + \"...\" : text\n historyParts.push(`${role}: ${truncated}`)\n }\n }\n\n if (historyParts.length === 0) {\n return null\n }\n\n return historyParts.join(\"\\n\\n\")\n}\n\nfunction buildCompactionHistory(prompt: Prompt): string | null {\n // Iterate newest-first, accumulate up to MAX_HISTORY_CHARS, then reverse\n // to chronological order. Oldest messages get dropped when the budget\n // is exhausted — they are the least relevant for a summary of recent\n // work.\n const entries: string[] = []\n let total = 0\n let totalToolResults = 0\n let droppedOldest = 0\n\n // Skip the trailing user message: opencode's /compact appends the\n // synthesis instruction as the final user turn. The instruction itself\n // is added by getClaudeUserMessage after the transcript block, so we\n // don't want it duplicated inside the transcript.\n const end = prompt.length > 0 && prompt[prompt.length - 1].role === \"user\"\n ? prompt.length - 1\n : prompt.length\n\n for (let i = end - 1; i >= 0; i--) {\n const msg = prompt[i] as any\n const roleLabel =\n msg.role === \"user\"\n ? \"User\"\n : msg.role === \"assistant\"\n ? \"Assistant\"\n : msg.role === \"tool\"\n ? \"Tool\"\n : msg.role\n\n const { text, toolResultCount } = renderMessageContentForCompaction(msg)\n if (!text.trim()) continue\n\n const entry = `${roleLabel}: ${text}`\n if (total + entry.length > MAX_HISTORY_CHARS) {\n droppedOldest = i + 1\n break\n }\n entries.push(entry)\n total += entry.length + 2 // +2 for the \"\\n\\n\" join\n totalToolResults += toolResultCount\n }\n\n if (entries.length === 0) return null\n\n entries.reverse()\n log.info(\"built compaction history\", {\n entries: entries.length,\n chars: total,\n toolResults: totalToolResults,\n droppedOldestBefore: droppedOldest,\n })\n\n return entries.join(\"\\n\\n\")\n}\n\n/**\n * Convert AI SDK prompt into a Claude CLI stream-json user message.\n *\n * `compactionMode` switches behavior for opencode /compact: the prior\n * transcript is rendered with rich tool content (not placeholders), the\n * wrapper framing tells the model this is the authoritative thread, and\n * the reasoning keyword is suppressed so the full output budget goes\n * toward the summary.\n */\nexport function getClaudeUserMessage(\n prompt: Prompt,\n includeHistoryContext: boolean = false,\n reasoningEffort?: ReasoningEffort,\n opts: { compactionMode?: boolean } = {},\n): string {\n const compactionMode = opts.compactionMode === true\n const content: any[] = []\n\n if (compactionMode) {\n const transcript = compactConversationHistory(prompt, {\n mode: \"compaction\",\n })\n if (transcript) {\n log.info(\"including compaction transcript\", {\n historyLength: transcript.length,\n })\n content.push({\n type: \"text\",\n text: `<conversation_transcript>\n${transcript}\n</conversation_transcript>\n\nThe complete prior conversation appears above. The synthesis instructions follow below.\n\n`,\n })\n }\n } else if (includeHistoryContext) {\n const historyContext = compactConversationHistory(prompt)\n if (historyContext) {\n log.info(\"including conversation history context\", {\n historyLength: historyContext.length,\n })\n content.push({\n type: \"text\",\n text: `<conversation_history>\nThe following is a summary of our conversation so far (from a previous session that couldn't be resumed):\n\n${historyContext}\n\n</conversation_history>\n\nNow continuing with the current message:\n\n`,\n })\n }\n }\n\n // Find messages since last assistant message\n const messages: typeof prompt = []\n for (let i = prompt.length - 1; i >= 0; i--) {\n if (prompt[i].role === \"assistant\") break\n messages.unshift(prompt[i])\n }\n\n for (const msg of messages) {\n if (msg.role === \"user\") {\n if (typeof msg.content === \"string\") {\n const str = msg.content as string\n if (str.trim()) {\n content.push({ type: \"text\", text: str })\n }\n } else if (Array.isArray(msg.content)) {\n for (const part of msg.content as any[]) {\n if (part.type === \"text\") {\n if (part.text && part.text.trim()) {\n content.push({ type: \"text\", text: part.text })\n }\n } else if (part.type === \"file\" || part.type === \"image\") {\n const block = toImageBlock(part)\n if (block) {\n content.push(block)\n } else {\n log.debug(\"skipped non-image file part\", {\n mediaType: part.mediaType,\n })\n }\n } else if (part.type === \"tool-result\") {\n const p = part as any\n content.push({\n type: \"tool_result\",\n tool_use_id: p.toolCallId,\n content: getToolResultText(p),\n })\n }\n }\n }\n } else if (msg.role === \"tool\") {\n // AI SDK V3 delivers tool results in `tool`-role messages, not `user`.\n // Without this branch we'd hit the empty-content sentinel path and\n // send \"(empty)\" to Claude CLI instead of the actual tool result —\n // forcing the user to press \"continue\" between proxy tool calls.\n if (Array.isArray(msg.content)) {\n for (const part of msg.content as any[]) {\n if (part?.type === \"tool-result\") {\n const p = part as any\n content.push({\n type: \"tool_result\",\n tool_use_id: p.toolCallId,\n content: getToolResultText(p),\n })\n }\n }\n }\n }\n }\n\n if (content.length === 0) {\n // CLI rejects a zero-block message with 400, and Anthropic rejects\n // whitespace-only text blocks — so we need a non-whitespace sentinel.\n // \"(empty)\" matches the parenthetical meta-note convention this file\n // already uses for reasoning keywords (\"(think)\", \"(megathink)\", etc.),\n // which the model reads as out-of-band metadata rather than a prompt to\n // continue its previous turn.\n log.warn(\"empty user content; sending sentinel to satisfy CLI\")\n return JSON.stringify({\n type: \"user\",\n message: {\n role: \"user\",\n content: [{ type: \"text\", text: \"(empty)\" }],\n },\n })\n }\n\n // Reasoning keyword is a Claude CLI hint that triggers extended thinking.\n // For compaction we want the full output budget to go to the summary\n // itself, not internal reasoning — so skip injection.\n if (!compactionMode) {\n const keyword = reasoningKeyword(reasoningEffort)\n if (keyword) {\n const lastTextPart = [...content].reverse().find((p) => p.type === \"text\")\n if (lastTextPart) {\n lastTextPart.text = lastTextPart.text\n ? `${lastTextPart.text}\\n\\n(${keyword})`\n : `(${keyword})`\n } else {\n content.push({ type: \"text\", text: `(${keyword})` })\n }\n log.debug(\"injected reasoning keyword\", { effort: reasoningEffort, keyword })\n }\n }\n\n return JSON.stringify({\n type: \"user\",\n message: {\n role: \"user\",\n content,\n },\n })\n}\n","import * as fs from \"node:fs\"\nimport * as path from \"node:path\"\nimport * as os from \"node:os\"\nimport * as crypto from \"node:crypto\"\nimport { log } from \"./logger.js\"\nimport { pluginTmpDir } from \"./tmp.js\"\n\n/**\n * Bridge opencode's `mcp` config block into a Claude CLI `--mcp-config` file.\n *\n * Opencode core schema (packages/opencode/src/config/mcp.ts):\n * {\n * \"mcp\": {\n * \"name\": {\n * \"type\": \"local\" | \"remote\",\n * \"command\"?: string[], // local\n * \"environment\"?: Record<string,string>,\n * \"url\"?: string, // remote\n * \"headers\"?: Record<string,string>,\n * \"oauth\"?: object | false, // remote — NOT bridged (Claude --mcp-config has no slot)\n * \"timeout\"?: number, // NOT bridged (Claude --mcp-config has no slot)\n * \"enabled\"?: boolean\n * }\n * }\n * }\n *\n * Claude CLI `--mcp-config` schema:\n * {\n * \"mcpServers\": {\n * \"name\": {\n * \"type\": \"stdio\" | \"http\",\n * \"command\"?: string, \"args\"?: string[], \"env\"?: Record<string,string>,\n * \"url\"?: string, \"headers\"?: Record<string,string>\n * }\n * }\n * }\n *\n * Discovery + merge are aligned with opencode core's `loadInstanceState`\n * (packages/opencode/src/config/config.ts). In merge order (last wins),\n * opencode loads:\n *\n * 1. Auth `.well-known` remote configs ← NOT bridged\n * 2. Global: ~/.config/opencode/{config.json,opencode.json,opencode.jsonc}\n * — all three deep-merged, jsonc highest priority\n * 3. OPENCODE_CONFIG env var (single file)\n * 4. Project walk-up: opencode.json[c] in each dir from cwd up to (not past)\n * worktree, both extensions per dir, parent-most first\n * 5. .opencode/ siblings: from cwd up + home dir + OPENCODE_CONFIG_DIR,\n * both extensions per dir, opencode-iteration order (cwd-most first\n * in walk-up — so parent-most `.opencode/` wins, matching upstream)\n * 6. OPENCODE_CONFIG_CONTENT env var (inline JSON) ← NOT bridged\n * 7. Active org remote config ← NOT bridged\n * 8. Managed config dir / macOS MDM ← NOT bridged\n *\n * Sources marked NOT bridged are niche and would require live opencode\n * runtime state (auth tokens, account context, MDM access). Document them\n * here so the gap is explicit; functionality of the common path is intact.\n *\n * Per-server merge is deep-merge (matching opencode's `mergeConfigConcatArrays`\n * → `mergeDeep`), so a project layer can override one field of a global server\n * spec — e.g. `{ \"linear\": { \"enabled\": true } }` lifts global linear's URL.\n */\n\nconst FILE_NAMES = [\"opencode.jsonc\", \"opencode.json\", \"config.json\"] as const\nconst PROJECT_FILE_NAMES = [\"opencode.json\", \"opencode.jsonc\"] as const\n\nfunction fileExists(p: string): boolean {\n try {\n return fs.statSync(p).isFile()\n } catch {\n return false\n }\n}\n\nfunction dirExists(p: string): boolean {\n try {\n return fs.statSync(p).isDirectory()\n } catch {\n return false\n }\n}\n\n/** Strip `//` and `/* *\\/` comments so JSONC parses via JSON.parse. */\nfunction stripJsonComments(text: string): string {\n let out = \"\"\n let i = 0\n let inString: string | null = null\n while (i < text.length) {\n const c = text[i]\n if (inString) {\n out += c\n if (c === \"\\\\\" && i + 1 < text.length) {\n out += text[i + 1]\n i += 2\n continue\n }\n if (c === inString) inString = null\n i++\n continue\n }\n if (c === '\"' || c === \"'\") {\n inString = c\n out += c\n i++\n continue\n }\n if (c === \"/\" && text[i + 1] === \"/\") {\n while (i < text.length && text[i] !== \"\\n\") i++\n continue\n }\n if (c === \"/\" && text[i + 1] === \"*\") {\n i += 2\n while (\n i < text.length &&\n !(text[i] === \"*\" && text[i + 1] === \"/\")\n )\n i++\n i += 2\n continue\n }\n out += c\n i++\n }\n return out\n}\n\nfunction readAndParse(file: string): Record<string, unknown> | null {\n try {\n const raw = fs.readFileSync(file, \"utf8\")\n return JSON.parse(stripJsonComments(raw)) as Record<string, unknown>\n } catch (e) {\n log.warn(\"failed to parse opencode config\", {\n file,\n error: e instanceof Error ? e.message : String(e),\n })\n return null\n }\n}\n\n/**\n * Deep merge two plain-object trees. Arrays and primitives are replaced\n * (not concatenated). Matches the effective behavior of opencode's\n * `mergeDeep` from `remeda` for the MCP block — opencode does not special\n * case array fields inside `mcp.<server>` (its only special case is\n * `instructions`, which is concat-deduped at the config root).\n */\nfunction isPlainObject(x: unknown): x is Record<string, unknown> {\n return typeof x === \"object\" && x !== null && !Array.isArray(x)\n}\n\nfunction deepMerge(\n target: Record<string, unknown>,\n source: Record<string, unknown>,\n): Record<string, unknown> {\n const out: Record<string, unknown> = { ...target }\n for (const [k, v] of Object.entries(source)) {\n if (v === undefined) continue\n const existing = out[k]\n if (isPlainObject(existing) && isPlainObject(v)) {\n out[k] = deepMerge(existing, v)\n } else {\n out[k] = v\n }\n }\n return out\n}\n\n/**\n * Walk up from `start` toward filesystem root (or `stop` if provided),\n * collecting paths where each `target` exists. Mirrors opencode core's\n * `FileSystem.up` (packages/core/src/filesystem.ts): cwd-most first,\n * parent-most last.\n */\nfunction walkUp(opts: {\n start: string\n stop?: string\n targets: readonly string[]\n predicate: (p: string) => boolean\n}): string[] {\n const out: string[] = []\n let current = path.resolve(opts.start)\n while (true) {\n for (const target of opts.targets) {\n const candidate = path.join(current, target)\n if (opts.predicate(candidate)) out.push(candidate)\n }\n if (opts.stop && current === path.resolve(opts.stop)) break\n const parent = path.dirname(current)\n if (parent === current) break\n current = parent\n }\n return out\n}\n\n/**\n * Find the worktree root by walking up from `cwd` looking for a `.git`\n * entry (file or directory — submodules use a file). If no `.git` is\n * found, walk to filesystem root. Honors OPENCODE_WORKTREE override.\n */\nfunction detectWorktree(cwd: string): string | undefined {\n const override = process.env.OPENCODE_WORKTREE\n if (override) return path.resolve(override)\n let current = path.resolve(cwd)\n while (true) {\n const gitPath = path.join(current, \".git\")\n try {\n if (fs.existsSync(gitPath)) return current\n } catch {\n // ignore\n }\n const parent = path.dirname(current)\n if (parent === current) return undefined\n current = parent\n }\n}\n\nfunction globalConfigDir(): string {\n const xdg = process.env.XDG_CONFIG_HOME ?? path.join(os.homedir(), \".config\")\n return path.join(xdg, \"opencode\")\n}\n\n/**\n * Load the merged global config from `~/.config/opencode/`. Mirrors\n * opencode core's `loadGlobal`: deep-merges config.json → opencode.json\n * → opencode.jsonc in that order (jsonc wins).\n */\nfunction loadGlobalConfig(): Record<string, unknown> {\n const dir = globalConfigDir()\n let merged: Record<string, unknown> = {}\n for (const name of FILE_NAMES.slice().reverse()) {\n // FILE_NAMES is jsonc-first; reverse to get config.json-first order.\n const file = path.join(dir, name)\n if (!fileExists(file)) continue\n const parsed = readAndParse(file)\n if (parsed) merged = deepMerge(merged, parsed)\n }\n return merged\n}\n\n/** Load both `opencode.json` and `opencode.jsonc` in `dir`, deep-merged. */\nfunction loadProjectFilesInDir(dir: string): Record<string, unknown> {\n let merged: Record<string, unknown> = {}\n for (const name of PROJECT_FILE_NAMES) {\n const file = path.join(dir, name)\n if (!fileExists(file)) continue\n const parsed = readAndParse(file)\n if (parsed) merged = deepMerge(merged, parsed)\n }\n return merged\n}\n\n/**\n * Build the list of `.opencode/` directories to consider, in opencode core's\n * order (matching `ConfigPaths.directories`):\n * project walk-up (cwd-most first) → home-dir `.opencode/` → OPENCODE_CONFIG_DIR\n */\nfunction dotOpencodeDirs(cwd: string, worktree?: string): string[] {\n const dirs: string[] = []\n const seen = new Set<string>()\n const push = (p: string) => {\n const abs = path.resolve(p)\n if (!seen.has(abs) && dirExists(abs)) {\n seen.add(abs)\n dirs.push(abs)\n }\n }\n\n for (const dir of walkUp({\n start: cwd,\n stop: worktree,\n targets: [\".opencode\"],\n predicate: dirExists,\n })) {\n push(dir)\n }\n\n const home = os.homedir()\n if (home) {\n const homeDot = path.join(home, \".opencode\")\n if (dirExists(homeDot)) push(homeDot)\n }\n\n const envDir = process.env.OPENCODE_CONFIG_DIR\n if (envDir && dirExists(envDir)) push(envDir)\n\n return dirs\n}\n\ninterface OpencodeLocalServer {\n type?: \"local\"\n command?: string[]\n environment?: Record<string, string>\n enabled?: boolean\n}\n\ninterface OpencodeRemoteServer {\n type?: \"remote\"\n url?: string\n headers?: Record<string, string>\n enabled?: boolean\n}\n\ntype OpencodeServer = OpencodeLocalServer | OpencodeRemoteServer | { enabled?: boolean }\n\n/**\n * Substitute opencode's `{env:VAR}` interpolation in a string-keyed record\n * using values from `process.env`. Returns a new object. If the source is\n * not a flat string-valued record, returns it unchanged.\n *\n * Opencode performs this substitution itself when it spawns MCP servers\n * directly, but the spec we read from disk still contains the literal\n * placeholders. Without substituting them here, Claude CLI hands the\n * literal string `{env:FOO}` to the MCP subprocess as the env value, and\n * any server that validates credentials at startup (e.g. slack-mcp-server)\n * crashes before exposing tools. Servers that defer validation to\n * request time (e.g. github-mcp-server) appear to register but every API\n * call 401s.\n */\nfunction substituteEnvPlaceholders(\n source: Record<string, unknown>,\n): Record<string, string> {\n const out: Record<string, string> = {}\n for (const [k, v] of Object.entries(source)) {\n if (typeof v !== \"string\") continue\n out[k] = v.replace(/\\{env:([A-Za-z_][A-Za-z0-9_]*)\\}/g, (_match, name) => {\n const resolved = process.env[name]\n return typeof resolved === \"string\" ? resolved : \"\"\n })\n }\n return out\n}\n\nfunction translateServer(\n name: string,\n spec: Record<string, unknown>,\n): Record<string, unknown> | null {\n if (spec.enabled === false) return null\n\n const type = spec.type\n if (type === \"local\") {\n const cmd = spec.command\n if (!Array.isArray(cmd) || cmd.length === 0) {\n log.warn(\"skipping local MCP server with no command\", { name })\n return null\n }\n const out: Record<string, unknown> = {\n type: \"stdio\",\n command: String(cmd[0]),\n }\n if (cmd.length > 1) out.args = cmd.slice(1).map((s) => String(s))\n if (spec.environment && typeof spec.environment === \"object\") {\n out.env = substituteEnvPlaceholders(\n spec.environment as Record<string, unknown>,\n )\n }\n return out\n }\n\n if (type === \"remote\") {\n if (typeof spec.url !== \"string\" || !spec.url) {\n log.warn(\"skipping remote MCP server with no url\", { name })\n return null\n }\n const out: Record<string, unknown> = {\n type: \"http\",\n url: spec.url,\n }\n if (spec.headers && typeof spec.headers === \"object\") {\n out.headers = substituteEnvPlaceholders(\n spec.headers as Record<string, unknown>,\n )\n }\n return out\n }\n\n log.warn(\"skipping MCP server with unknown type\", {\n name,\n type: type ?? null,\n })\n return null\n}\n\nfunction extractMcpBlock(\n config: Record<string, unknown>,\n): Record<string, OpencodeServer> {\n const mcp = config.mcp\n if (!mcp || typeof mcp !== \"object\" || Array.isArray(mcp)) return {}\n return mcp as Record<string, OpencodeServer>\n}\n\n/**\n * Deep-merge per-server specs from `source` into `target`. Mirrors opencode's\n * `mergeDeep` semantics for the `mcp` record: each server entry is recursively\n * merged so a partial layer (e.g. `{ \"linear\": { \"enabled\": true } }`) can\n * override one field without dropping the rest.\n */\nfunction mergeMcp(\n target: Record<string, OpencodeServer>,\n source: Record<string, OpencodeServer>,\n): Record<string, OpencodeServer> {\n const out: Record<string, OpencodeServer> = { ...target }\n for (const [name, spec] of Object.entries(source)) {\n if (!spec || typeof spec !== \"object\") continue\n const existing = out[name]\n if (existing && typeof existing === \"object\") {\n out[name] = deepMerge(\n existing as Record<string, unknown>,\n spec as Record<string, unknown>,\n ) as OpencodeServer\n } else {\n out[name] = spec\n }\n }\n return out\n}\n\nexport interface BridgedMcp {\n /** Path to the temp file containing the translated `--mcp-config`. */\n path: string\n /** Stable hash of the merged opencode mcp block (pre-translation). */\n hash: string\n /**\n * Names of opencode MCP servers that were bridged into Claude CLI's\n * `--mcp-config`. Excludes any servers passed in `excludeServers`.\n */\n serverNames: string[]\n /**\n * Names of every enabled opencode MCP server after merge + runtime\n * overlay, regardless of whether they ended up bridged or excluded.\n * Callers (e.g. the proxy-tool builder) use this to decide which\n * `<server>_<tool>` IDs in opencode's tool catalog are MCP-origin.\n */\n allEnabledServerNames: string[]\n}\n\n/** Result of merging opencode's MCP config layers + applying runtime overlay. */\nexport interface MergedMcp {\n /** Server names whose final spec is enabled (or implicitly enabled). */\n enabledServerNames: string[]\n /** Stable hash of the merged (pre-translation) MCP block. */\n hash: string\n}\n\n/**\n * Per-server runtime status from opencode's `client.mcp.status()`. Used as\n * an overlay on top of the on-disk merged config so opencode's UI-toggled\n * state — which lives only in-memory; `connect()`/`disconnect()` never\n * touch disk — propagates to the bridged claude subprocess.\n *\n * Treatment per server:\n * - \"connected\" → force `enabled: true` (mirror opencode)\n * - any other status → force `enabled: false` (don't ship a server\n * opencode can't run; user fixes it in opencode first)\n * - missing entry → leave disk value\n *\n * Omit the overlay and the bridge falls back to disk-only.\n */\nexport type RuntimeMcpStatus = Record<string, string>\n\n/**\n * Read opencode config layers, deep-merge their `mcp` blocks per opencode's\n * own semantics, optionally apply an opencode runtime-status overlay, then\n * translate each server to Claude CLI format, write a scratch file, and\n * return its path + a stable hash. Returns null when no enabled MCP servers\n * remain after the merge + overlay.\n */\nexport function bridgeOpencodeMcp(\n cwd: string,\n runtimeStatus?: RuntimeMcpStatus,\n excludeServers?: ReadonlySet<string>,\n): BridgedMcp | null {\n const worktree = detectWorktree(cwd)\n\n // Layer 1: global merged\n let merged: Record<string, OpencodeServer> = {}\n merged = mergeMcp(merged, extractMcpBlock(loadGlobalConfig()))\n\n // Layer 2: OPENCODE_CONFIG (single file, applied before project walk-up)\n const explicitConfig = process.env.OPENCODE_CONFIG\n if (explicitConfig && fileExists(explicitConfig)) {\n const parsed = readAndParse(explicitConfig)\n if (parsed) merged = mergeMcp(merged, extractMcpBlock(parsed))\n }\n\n // Layer 3: project walk-up — opencode.json[c] in each dir from cwd to\n // (not past) worktree, both extensions per dir. walkUp returns cwd-most\n // first; collect distinct dirs in that order then reverse for merge so\n // cwd-most wins under last-merge-wins.\n const projectFiles = walkUp({\n start: cwd,\n stop: worktree,\n targets: PROJECT_FILE_NAMES,\n predicate: fileExists,\n })\n const projectDirs: string[] = []\n const seenProjectDirs = new Set<string>()\n for (const f of projectFiles) {\n const d = path.dirname(f)\n if (!seenProjectDirs.has(d)) {\n seenProjectDirs.add(d)\n projectDirs.push(d)\n }\n }\n for (const dir of projectDirs.slice().reverse()) {\n merged = mergeMcp(merged, extractMcpBlock(loadProjectFilesInDir(dir)))\n }\n\n // Layer 4: `.opencode/` siblings — project walk-up then home-dir then\n // OPENCODE_CONFIG_DIR, in that order. Iteration order matches opencode's\n // (cwd-most first within walk-up), so under deep-merge \"later wins\"\n // parent-most `.opencode/` overrides cwd-most. This is upstream's\n // behavior, surprising though it is.\n for (const dir of dotOpencodeDirs(cwd, worktree)) {\n merged = mergeMcp(merged, extractMcpBlock(loadProjectFilesInDir(dir)))\n }\n\n // Layer 5: opencode runtime overlay. opencode's `/mcps` UI toggle calls\n // `mcp.connect()` / `mcp.disconnect()` which only mutate in-memory state,\n // never the on-disk config. Without this overlay the bridge can't see\n // those toggles and claude misses servers the user just enabled.\n if (runtimeStatus) {\n for (const name of Object.keys(merged)) {\n const status = runtimeStatus[name]\n if (status === undefined) continue\n const existing = merged[name]\n const base =\n existing && typeof existing === \"object\"\n ? (existing as Record<string, unknown>)\n : {}\n merged[name] = { ...base, enabled: status === \"connected\" } as OpencodeServer\n }\n }\n\n // Compute the set of enabled server names BEFORE exclusion so callers can\n // tell whether a tool ID like `slack_conversations_add_message` came from\n // an opencode MCP server (vs a built-in tool that happens to contain `_`).\n const allEnabledServerNames: string[] = []\n for (const [name, spec] of Object.entries(merged)) {\n if (!spec || typeof spec !== \"object\") continue\n const enabled = (spec as { enabled?: unknown }).enabled\n if (enabled === false) continue\n allEnabledServerNames.push(name)\n }\n\n // Translate every still-enabled server, skipping any caller has asked us\n // to exclude (because they're being routed through the proxy instead).\n const servers: Record<string, unknown> = {}\n const bridgedServerNames: string[] = []\n for (const [name, spec] of Object.entries(merged)) {\n if (!spec || typeof spec !== \"object\") continue\n if (excludeServers?.has(name)) continue\n const translated = translateServer(name, spec as Record<string, unknown>)\n if (translated) {\n servers[name] = translated\n bridgedServerNames.push(name)\n }\n }\n\n // Hash the pre-exclusion merged block so the hot-reload detector picks up\n // upstream config changes even when every server is excluded.\n const mergedBody = JSON.stringify({ mcpServers: merged }, null, 2)\n const hash = crypto\n .createHash(\"sha256\")\n .update(mergedBody)\n .digest(\"hex\")\n .slice(0, 12)\n\n if (Object.keys(servers).length === 0) {\n const allEnabledServersExcluded =\n excludeServers &&\n allEnabledServerNames.length > 0 &&\n allEnabledServerNames.every((name) => excludeServers.has(name))\n\n if (!allEnabledServersExcluded) return null\n\n return {\n path: \"\",\n hash,\n serverNames: [],\n allEnabledServerNames,\n }\n }\n\n const body = JSON.stringify({ mcpServers: servers }, null, 2)\n const outPath = path.join(\n pluginTmpDir(),\n `mcp-${hash}.json`,\n )\n try {\n if (!fileExists(outPath)) {\n fs.writeFileSync(outPath, body, { encoding: \"utf8\", mode: 0o600 })\n }\n } catch (e) {\n log.warn(\"failed to write bridged MCP config\", {\n error: e instanceof Error ? e.message : String(e),\n })\n return null\n }\n\n log.info(\"bridged opencode MCP config\", {\n target: outPath,\n hash,\n servers: bridgedServerNames,\n excluded: excludeServers ? Array.from(excludeServers) : [],\n })\n return {\n path: outPath,\n hash,\n serverNames: bridgedServerNames,\n allEnabledServerNames,\n }\n}\n\n// Internal helpers exported for tests only.\nexport const __test = {\n deepMerge,\n mergeMcp,\n translateServer,\n substituteEnvPlaceholders,\n detectWorktree,\n loadGlobalConfig,\n loadProjectFilesInDir,\n dotOpencodeDirs,\n}\n","import * as fs from \"node:fs\"\nimport * as os from \"node:os\"\nimport * as path from \"node:path\"\n\n/**\n * Per-process scratch directory for plugin tmp files (bridged MCP config,\n * proxy server config, etc.). Created lazily on first use and rm'd on\n * normal process exit so we don't leak across runs. PID-isolated so two\n * concurrent opencode processes don't race on the same files.\n *\n * Caveat: `process.on(\"exit\")` does not fire for SIGKILL or unhandled\n * external signals, so abnormal terminations still leak. OS-level tmpdir\n * cleanup (`systemd-tmpfiles`, macOS periodic) handles those eventually.\n */\nconst PLUGIN_TMP_DIR = path.join(\n os.tmpdir(),\n `opencode-claude-code-${process.pid}`,\n)\n\nlet registered = false\n\nexport function pluginTmpDir(): string {\n if (!fs.existsSync(PLUGIN_TMP_DIR)) {\n fs.mkdirSync(PLUGIN_TMP_DIR, { recursive: true })\n }\n if (!registered) {\n registered = true\n process.on(\"exit\", () => {\n try {\n fs.rmSync(PLUGIN_TMP_DIR, { recursive: true, force: true })\n } catch {}\n })\n }\n return PLUGIN_TMP_DIR\n}\n","import type { RuntimeMcpStatus } from \"./mcp-bridge.js\"\nimport { log } from \"./logger.js\"\n\n/**\n * Captured opencode runtime context (SDK client + project directory) from\n * `PluginInput`. Lives in its own module to break the cycle that would\n * otherwise form between `index.ts` and `claude-code-language-model.ts`.\n * Values are `null`/`undefined` until the plugin's `server` factory runs\n * (e.g. early provider lookups, direct AI-SDK use, tests).\n */\ntype OpencodeClient = {\n mcp?: {\n status?: () => Promise<{ data?: unknown; error?: unknown }>\n }\n tool?: {\n list?: (options: {\n query: { provider: string; model: string; directory?: string }\n }) => Promise<{ data?: unknown; error?: unknown }>\n }\n}\n\nlet opencodeClient: OpencodeClient | null = null\n\nexport function setOpencodeClient(client: unknown): void {\n if (client && typeof client === \"object\") {\n opencodeClient = client as OpencodeClient\n }\n}\n\n/**\n * Captured opencode project directory from `PluginInput.directory` (with\n * `worktree` as secondary signal). Used as a *fallback* at Claude CLI\n * spawn time only when `process.cwd()` is unusable (macOS GUI launches\n * where launchd hands the process `cwd=/`).\n *\n * IMPORTANT: never bake this into provider config (`mergedOptions.cwd`).\n * Doing so freezes the value at plugin init and breaks workspace\n * switching mid-session, because subsequent workspace changes in\n * opencode's UI never get reflected in `this.config.cwd`. See issue #4.\n */\nlet opencodeProjectDirectory: string | undefined\n\nexport function setOpencodeProjectDirectory(dir: string | undefined): void {\n opencodeProjectDirectory = dir\n}\n\nexport function getOpencodeProjectDirectory(): string | undefined {\n return opencodeProjectDirectory\n}\n\nexport function isUsableDirectory(d: unknown): d is string {\n return typeof d === \"string\" && d.length > 1 && d !== \"/\"\n}\n\n/**\n * Resolve the cwd for a Claude CLI subprocess spawn. Priority:\n *\n * 1. Explicit `configured` value (`options.cwd` from `opencode.json`).\n * Users who pinned a directory keep their override unconditionally.\n * 2. Live `process.cwd()` when it's a real directory. Restores the lazy\n * resolution that lets opencode's project-aware behavior (chdir on\n * workspace switch, project-per-shell on terminal launch) flow\n * through without restarting the plugin.\n * 3. Captured project directory from plugin init. Rescues macOS GUI\n * launches where `process.cwd()` is `/`.\n * 4. Final fallback to `process.cwd()` (returns `/` in the pathological\n * case where neither override nor capture is available).\n */\nexport function resolveSpawnCwd(configured: string | undefined): string {\n return resolveSpawnCwdFrom(\n configured,\n process.cwd(),\n opencodeProjectDirectory,\n )\n}\n\nexport function resolveSpawnCwdFrom(\n configured: string | undefined,\n live: string,\n captured: string | undefined,\n): string {\n if (configured) return configured\n if (isUsableDirectory(live)) return live\n return captured ?? live\n}\n\n/**\n * Snapshot opencode's current MCP runtime status so the bridge can overlay\n * UI-toggled state on top of disk config. Returns `undefined` on any\n * failure (no client captured, status call rejected, malformed response)\n * so the bridge falls back to disk-only.\n */\nexport async function getRuntimeMcpStatus(): Promise<\n RuntimeMcpStatus | undefined\n> {\n const client = opencodeClient\n if (!client?.mcp?.status) return undefined\n try {\n const res = await client.mcp.status()\n const data = (res as { data?: unknown }).data\n if (!data || typeof data !== \"object\") return undefined\n const out: RuntimeMcpStatus = {}\n for (const [name, entry] of Object.entries(data as Record<string, unknown>)) {\n if (entry && typeof entry === \"object\") {\n const status = (entry as { status?: unknown }).status\n if (typeof status === \"string\") out[name] = status\n }\n }\n return out\n } catch (err) {\n log.warn(\"failed to fetch opencode MCP runtime status\", {\n error: err instanceof Error ? err.message : String(err),\n })\n return undefined\n }\n}\n\nexport interface OpencodeToolListItem {\n id: string\n description: string\n parameters: Record<string, unknown>\n}\n\n/**\n * Fetch opencode's full tool catalog (built-ins + MCP-bridged) with JSON\n * Schema parameters via `client.tool.list()`. The provider/model query\n * narrows the schema variants opencode returns; in practice MCP-origin\n * tool schemas are model-agnostic, so any registered (provider, model)\n * works as the query target. Returns `undefined` on any failure so callers\n * can fall back to direct-bridge behavior.\n */\nexport async function fetchOpencodeToolList(\n provider: string,\n model: string,\n directory?: string,\n): Promise<OpencodeToolListItem[] | undefined> {\n const client = opencodeClient\n if (!client?.tool?.list) return undefined\n try {\n const res = await client.tool.list({\n query: { provider, model, ...(directory ? { directory } : {}) },\n })\n const data = (res as { data?: unknown }).data\n if (!Array.isArray(data)) return undefined\n const out: OpencodeToolListItem[] = []\n for (const entry of data as unknown[]) {\n if (!entry || typeof entry !== \"object\") continue\n const e = entry as Record<string, unknown>\n const id = typeof e.id === \"string\" ? e.id : null\n const description =\n typeof e.description === \"string\" ? e.description : \"\"\n const parameters =\n e.parameters && typeof e.parameters === \"object\"\n ? (e.parameters as Record<string, unknown>)\n : {}\n if (!id) continue\n out.push({ id, description, parameters })\n }\n return out\n } catch (err) {\n log.warn(\"failed to fetch opencode tool list\", {\n provider,\n model,\n error: err instanceof Error ? err.message : String(err),\n })\n return undefined\n }\n}\n","import { spawn, type ChildProcess } from \"node:child_process\"\nimport { createInterface } from \"node:readline\"\nimport { EventEmitter } from \"node:events\"\nimport { unlink } from \"node:fs/promises\"\nimport { log } from \"./logger.js\"\nimport type { ProxyMcpServer } from \"./proxy-mcp.js\"\nimport { clearLedger } from \"./todo-ledger.js\"\nimport {\n cliSupportsThinking,\n cliSupportsThinkingDisplay,\n type CliVersion,\n} from \"./cli-version.js\"\n\nexport interface ActiveProcess {\n proc: ChildProcess\n lineEmitter: EventEmitter\n proxyServer?: ProxyMcpServer | null\n /**\n * Hash of the bridged opencode MCP config the process was spawned with.\n * `null` when the bridge produced nothing (no MCP servers). `undefined`\n * when the bridge was disabled. Used to detect mid-session config drift\n * and force a respawn.\n */\n mcpHash?: string | null\n /** Temp file holding `--append-system-prompt-file` content; unlinked on exit. */\n systemPromptFile?: string\n}\n\n// One active CLI process per session key. Keyed by a composite\n// (cwd + model + opencode session-affinity) so two chats don't race.\n// Iteration order is insertion order, which we refresh on access to\n// make this a poor-man's LRU; see `touch()` below.\nconst activeProcesses = new Map<string, ActiveProcess>()\nconst claudeSessions = new Map<string, string>()\n\n// Cap on live CLI subprocesses. Session-affinity-keyed entries accumulate\n// one-per-chat, so an unbounded map would leak processes as users open new\n// chats. This caps at a reasonable working-set and evicts the oldest.\nconst MAX_ACTIVE_PROCESSES = 16\n\nfunction envFlagEnabled(value: string | undefined): boolean {\n if (value === undefined) return false\n const normalized = value.trim().toLowerCase()\n if (!normalized) return false\n return ![\"0\", \"false\", \"no\", \"off\"].includes(normalized)\n}\n\nexport function isClaudeThinkingDisabled(): boolean {\n return (\n envFlagEnabled(process.env.CLAUDE_CODE_DISABLE_THINKING) ||\n envFlagEnabled(process.env.CLAUDE_CODE_DISABLE_ADAPTIVE_THINKING)\n )\n}\n\nexport function claudeSpawnEnv(): Record<string, string | undefined> {\n const env: Record<string, string | undefined> = {\n ...process.env,\n TERM: \"xterm-256color\",\n }\n\n // Default-on thinking summaries for opus-4-7 (which omits thinking by\n // default on the CLI side). Any var the user has explicitly set in their\n // shell is passed through untouched; the plugin only fills in the default.\n if (\n !isClaudeThinkingDisabled() &&\n process.env.CLAUDE_CODE_SHOW_THINKING_SUMMARIES === undefined\n ) {\n env.CLAUDE_CODE_SHOW_THINKING_SUMMARIES = \"1\"\n }\n\n return env\n}\n\nfunction touch(key: string): void {\n const existing = activeProcesses.get(key)\n if (existing) {\n activeProcesses.delete(key)\n activeProcesses.set(key, existing)\n }\n}\n\nfunction evictIfNeeded(): void {\n while (activeProcesses.size >= MAX_ACTIVE_PROCESSES) {\n const oldestKey = activeProcesses.keys().next().value\n if (!oldestKey) break\n log.info(\"evicting LRU claude process\", { sessionKey: oldestKey })\n deleteActiveProcess(oldestKey)\n }\n}\n\nexport function getActiveProcess(key: string): ActiveProcess | undefined {\n const ap = activeProcesses.get(key)\n if (ap) touch(key)\n return ap\n}\n\nexport function setActiveProcess(key: string, ap: ActiveProcess): void {\n activeProcesses.set(key, ap)\n}\n\nexport function deleteActiveProcess(key: string): void {\n const ap = activeProcesses.get(key)\n if (ap) {\n void ap.proxyServer?.close()\n ap.proc.kill()\n activeProcesses.delete(key)\n }\n}\n\nexport function getClaudeSessionId(key: string): string | undefined {\n return claudeSessions.get(key)\n}\n\nexport function setClaudeSessionId(key: string, sessionId: string): void {\n claudeSessions.set(key, sessionId)\n}\n\nexport function deleteClaudeSessionId(key: string): void {\n const claudeSessionId = claudeSessions.get(key)\n if (claudeSessionId) clearLedger(claudeSessionId)\n claudeSessions.delete(key)\n}\n\nexport function spawnClaudeProcess(\n cliPath: string,\n cliArgs: string[],\n cwd: string,\n sessionKey: string,\n proxyServer?: ProxyMcpServer | null,\n mcpHash?: string | null,\n systemPromptFile?: string,\n): ActiveProcess {\n evictIfNeeded()\n log.info(\"spawning new claude process\", { cliPath, cliArgs, cwd, sessionKey })\n\n const proc = spawn(cliPath, cliArgs, {\n cwd,\n stdio: [\"pipe\", \"pipe\", \"pipe\"],\n env: claudeSpawnEnv(),\n shell: process.platform === \"win32\",\n })\n\n const lineEmitter = new EventEmitter()\n\n const rl = createInterface({ input: proc.stdout! })\n rl.on(\"line\", (line: string) => {\n lineEmitter.emit(\"line\", line)\n })\n rl.on(\"close\", () => {\n lineEmitter.emit(\"close\")\n })\n\n const ap: ActiveProcess = {\n proc,\n lineEmitter,\n proxyServer: proxyServer ?? null,\n mcpHash,\n systemPromptFile,\n }\n activeProcesses.set(sessionKey, ap)\n\n // Baseline 'error' listener so Node doesn't throw when the process emits\n // an error between stream turns (no per-stream listener attached then).\n proc.on(\"error\", (err) => {\n log.error(\"claude process error\", { sessionKey, error: err.message })\n })\n\n proc.on(\"exit\", (code, signal) => {\n log.info(\"claude process exited\", { code, signal, sessionKey })\n void proxyServer?.close()\n if (systemPromptFile) {\n void unlink(systemPromptFile).catch(() => {})\n }\n activeProcesses.delete(sessionKey)\n if (code !== 0 && code !== null) {\n log.info(\"process exited with error, clearing session\", {\n code,\n sessionKey,\n })\n claudeSessions.delete(sessionKey)\n }\n })\n\n proc.stderr?.on(\"data\", (data: Buffer) => {\n const stderr = data.toString()\n log.debug(\"stderr\", { data: stderr.slice(0, 200) })\n\n if (\n stderr.includes(\"Session ID\") &&\n (stderr.includes(\"already in use\") ||\n stderr.includes(\"not found\") ||\n stderr.includes(\"invalid\"))\n ) {\n log.warn(\"claude session ID error, clearing session\", {\n sessionKey,\n error: stderr.slice(0, 200),\n })\n claudeSessions.delete(sessionKey)\n }\n })\n\n return ap\n}\n\nexport function buildCliArgs(opts: {\n sessionKey: string\n skipPermissions: boolean\n includeSessionId?: boolean\n model?: string\n permissionMode?: string\n mcpConfig?: string | string[]\n strictMcpConfig?: boolean\n disallowedTools?: string[]\n appendSystemPromptFile?: string\n thinking?: \"enabled\" | \"disabled\"\n thinkingDisplay?: \"summarized\" | \"omitted\"\n cliVersion?: CliVersion | null\n}): string[] {\n const {\n sessionKey,\n skipPermissions,\n includeSessionId = true,\n model,\n permissionMode,\n mcpConfig,\n strictMcpConfig,\n disallowedTools,\n appendSystemPromptFile,\n thinking,\n thinkingDisplay,\n cliVersion,\n } = opts\n const args = [\n \"--print\",\n \"--output-format\",\n \"stream-json\",\n \"--input-format\",\n \"stream-json\",\n \"--include-partial-messages\",\n \"--verbose\",\n ]\n\n if (model) {\n args.push(\"--model\", model)\n }\n\n if (permissionMode) {\n args.push(\"--permission-mode\", permissionMode)\n }\n\n if (includeSessionId) {\n const sessionId = claudeSessions.get(sessionKey)\n if (sessionId && !activeProcesses.has(sessionKey)) {\n args.push(\"--session-id\", sessionId)\n }\n }\n\n if (mcpConfig) {\n const configs = Array.isArray(mcpConfig) ? mcpConfig : [mcpConfig]\n const filtered = configs.filter((c) => typeof c === \"string\" && c.length > 0)\n if (filtered.length > 0) {\n args.push(\"--mcp-config\", ...filtered)\n }\n }\n\n if (strictMcpConfig) {\n args.push(\"--strict-mcp-config\")\n }\n\n if (disallowedTools && disallowedTools.length > 0) {\n args.push(\"--disallowedTools\", ...disallowedTools)\n }\n\n // `--thinking` is only present from Claude Code 2.x onward; gate so\n // pre-2.x binaries don't crash with a parse error. Unknown version →\n // skip (the spawn still works, the user just doesn't get extended\n // thinking until they upgrade).\n if (thinking && cliSupportsThinking(cliVersion ?? null)) {\n args.push(\"--thinking\", thinking)\n }\n\n // `--thinking-display` was added in Claude Code 2.1.142. Older CLIs\n // reject it with a parse error, so gate on detected version. When\n // version is unknown (detection failed), be conservative and skip.\n if (thinkingDisplay && cliSupportsThinkingDisplay(cliVersion ?? null)) {\n args.push(\"--thinking-display\", thinkingDisplay)\n }\n\n if (appendSystemPromptFile) {\n args.push(\"--append-system-prompt-file\", appendSystemPromptFile)\n }\n\n if (skipPermissions) {\n args.push(\"--dangerously-skip-permissions\")\n }\n\n return args\n}\n\n/**\n * Build a session key that includes both cwd and model,\n * so different models get separate processes.\n */\nexport function sessionKey(cwd: string, modelId: string): string {\n return `${cwd}::${modelId}`\n}\n","import { execFile } from \"node:child_process\"\nimport { promisify } from \"node:util\"\nimport { log } from \"./logger.js\"\n\nconst execFileAsync = promisify(execFile)\n\nexport interface CliVersion {\n major: number\n minor: number\n patch: number\n raw: string\n}\n\nconst cache = new Map<string, Promise<CliVersion | null>>()\n\n/**\n * Run `claude --version` once per cliPath and parse the leading semver.\n * Returns null on any failure (binary missing, unparseable output, etc.)\n * so callers can fall back to the most conservative flag set.\n */\nexport function detectCliVersion(cliPath: string): Promise<CliVersion | null> {\n const cached = cache.get(cliPath)\n if (cached) return cached\n const promise = (async (): Promise<CliVersion | null> => {\n try {\n const { stdout } = await execFileAsync(cliPath, [\"--version\"], {\n timeout: 5000,\n })\n const match = /(\\d+)\\.(\\d+)\\.(\\d+)/.exec(stdout.trim())\n if (!match) {\n log.warn(\"claude --version output unparseable\", { stdout: stdout.trim() })\n return null\n }\n const v: CliVersion = {\n major: Number(match[1]),\n minor: Number(match[2]),\n patch: Number(match[3]),\n raw: stdout.trim(),\n }\n log.info(\"detected claude cli version\", { cliPath, version: v.raw })\n if (!cliSupportsThinkingDisplay(v)) {\n log.notice(\n \"claude cli < 2.1.142 detected; Opus 4.7 thinking summaries unavailable. Run `npm i -g @anthropic-ai/claude-code` to upgrade.\",\n { version: v.raw },\n )\n }\n return v\n } catch (err) {\n log.warn(\"failed to detect claude cli version\", {\n cliPath,\n error: err instanceof Error ? err.message : String(err),\n })\n return null\n }\n })()\n cache.set(cliPath, promise)\n return promise\n}\n\nfunction gte(v: CliVersion, target: { major: number; minor: number; patch: number }): boolean {\n if (v.major !== target.major) return v.major > target.major\n if (v.minor !== target.minor) return v.minor > target.minor\n return v.patch >= target.patch\n}\n\n/**\n * `--thinking-display` was introduced in Claude Code 2.1.142 alongside\n * Opus 4.7's \"omitted by default\" thinking behavior. Older CLIs reject\n * the flag with a parse error, so we gate it. Unknown version → return\n * false so we don't risk crashing the spawn.\n */\nexport function cliSupportsThinkingDisplay(v: CliVersion | null): boolean {\n if (!v) return false\n return gte(v, { major: 2, minor: 1, patch: 142 })\n}\n\n/**\n * `--thinking` has been part of Claude Code's CLI since the 2.x line.\n * We require a detected 2.0.0+ before passing it; unknown version → skip\n * to avoid crashing a pre-flag binary. Anyone on the 1.x line should\n * upgrade.\n */\nexport function cliSupportsThinking(v: CliVersion | null): boolean {\n if (!v) return false\n return gte(v, { major: 2, minor: 0, patch: 0 })\n}\n\n/** For tests. */\nexport function _clearCache(): void {\n cache.clear()\n}\n","import { createServer, type IncomingMessage, type ServerResponse } from \"node:http\"\nimport type { AddressInfo } from \"node:net\"\nimport * as fs from \"node:fs\"\nimport * as path from \"node:path\"\nimport * as crypto from \"node:crypto\"\nimport { EventEmitter } from \"node:events\"\nimport { log } from \"./logger.js\"\nimport { pluginTmpDir } from \"./tmp.js\"\n\n/**\n * Minimal MCP HTTP server embedded in-process. Exposes a set of \"proxy\"\n * tools (Bash, Edit, Write, etc.) that Claude CLI calls when its built-in\n * equivalents are disabled via --disallowedTools. Our handler blocks until\n * an external broker resolves the call, then responds to Claude.\n *\n * Wire protocol: JSON-RPC 2.0 over plain HTTP POST to `/mcp`. MCP spec\n * also supports SSE streaming, but Claude's HTTP transport accepts single\n * JSON responses for short-lived tool calls, so we keep it simple.\n */\n\nexport interface ProxyMcpServer {\n url: string\n serverName: string\n tools: ProxyToolDef[]\n /** Fires when Claude invokes one of our proxy tools. The handler resolves\n * the returned pending call once a result is available. */\n calls: EventEmitter\n /** Write `--mcp-config <path>`-compatible scratch file and return its path. */\n configPath(): string\n close(): Promise<void>\n}\n\nexport interface ProxyToolDef {\n /** Raw name as seen by Claude once proxied: the MCP exposed tool name. */\n name: string\n description: string\n inputSchema: Record<string, unknown>\n}\n\nexport interface ProxyToolCall {\n id: string\n toolName: string\n input: Record<string, unknown>\n resolve: (result: ProxyToolResult) => void\n reject: (err: Error) => void\n}\n\nexport type ProxyToolResult =\n | { kind: \"text\"; text: string; isError?: boolean }\n | { kind: \"error\"; message: string }\n\nconst PROTOCOL_VERSION = \"2024-11-05\"\nconst SERVER_NAME = \"opencode_proxy\"\nexport const PROXY_TOOL_PREFIX = `mcp__${SERVER_NAME}__`\n\n// Cap on how long a proxy tool call may wait for opencode to resolve it.\n// Matches Claude CLI's hard upper bound for Bash (10 min). Without this the\n// HTTP handler waits forever if the broker chain breaks (listener never\n// attaches, opencode crashes between turns, etc.) and the Claude\n// subprocess sits idle waiting for a tool result that never arrives.\nconst PROXY_CALL_TIMEOUT_MS = 10 * 60 * 1000\n\nexport const DEFAULT_PROXY_TOOLS: ProxyToolDef[] = [\n {\n name: \"bash\",\n description:\n \"Execute a shell command. Routed through opencode's bash tool so\" +\n \" permission prompts flow through opencode's UI.\",\n inputSchema: {\n type: \"object\",\n properties: {\n command: {\n type: \"string\",\n description: \"The shell command to execute.\",\n },\n description: {\n type: \"string\",\n description: \"Short human-readable description of what the command does.\",\n },\n timeout: {\n type: \"number\",\n description: \"Optional timeout in milliseconds.\",\n },\n },\n required: [\"command\"],\n },\n },\n {\n name: \"write\",\n description:\n \"Write a file. Routed through opencode's write tool so permission prompts flow through opencode's UI.\",\n inputSchema: {\n type: \"object\",\n properties: {\n filePath: {\n type: \"string\",\n description: \"The file to write. Absolute paths are preferred.\",\n },\n content: {\n type: \"string\",\n description: \"The full content to write to the file.\",\n },\n },\n required: [\"filePath\", \"content\"],\n },\n },\n {\n name: \"edit\",\n description:\n \"Replace text in an existing file. Routed through opencode's edit tool so permission prompts flow through opencode's UI.\",\n inputSchema: {\n type: \"object\",\n properties: {\n filePath: {\n type: \"string\",\n description: \"The file to edit. Absolute paths are preferred.\",\n },\n oldString: {\n type: \"string\",\n description: \"The exact text to replace.\",\n },\n newString: {\n type: \"string\",\n description: \"The replacement text.\",\n },\n replaceAll: {\n type: \"boolean\",\n description: \"Replace all occurrences instead of just the first one.\",\n },\n },\n required: [\"filePath\", \"oldString\", \"newString\"],\n },\n },\n {\n name: \"webfetch\",\n description:\n \"Fetch content from a URL. Routed through opencode's webfetch tool so\" +\n \" permission prompts flow through opencode's UI. Returns the page\" +\n \" content in the requested format.\",\n inputSchema: {\n type: \"object\",\n properties: {\n url: {\n type: \"string\",\n description: \"The URL to fetch content from. Must start with http:// or https://.\",\n },\n format: {\n type: \"string\",\n enum: [\"text\", \"markdown\", \"html\"],\n description:\n \"The format to return the content in. Defaults to markdown.\",\n },\n timeout: {\n type: \"number\",\n description: \"Optional timeout in seconds (max 120).\",\n },\n },\n required: [\"url\"],\n },\n },\n {\n name: \"task\",\n description:\n \"Launch an opencode subagent to handle a complex multi-step task\" +\n \" autonomously. Routed through opencode's task tool so subagent\" +\n \" orchestration, permission, and lifecycle are handled by opencode.\" +\n \" Use `subagent_type` to pick which configured subagent runs (e.g.\" +\n \" `build`, `general`, `explore`, or any custom subagent declared in\" +\n \" opencode.json). The call blocks until the subagent finishes; the\" +\n \" 10-minute proxy timeout applies.\",\n inputSchema: {\n type: \"object\",\n properties: {\n description: {\n type: \"string\",\n description: \"A short (3-5 words) description of the task\",\n },\n prompt: {\n type: \"string\",\n description: \"The task for the agent to perform\",\n },\n subagent_type: {\n type: \"string\",\n description: \"The type of specialized agent to use for this task\",\n },\n task_id: {\n type: \"string\",\n description:\n \"Set this only if you mean to resume a previous task — pass the\" +\n \" prior task_id to continue the same subagent session instead of\" +\n \" creating a fresh one.\",\n },\n command: {\n type: \"string\",\n description: \"The command that triggered this task\",\n },\n },\n required: [\"description\", \"prompt\", \"subagent_type\"],\n },\n },\n]\n\nexport async function createProxyMcpServer(\n tools: ProxyToolDef[] = DEFAULT_PROXY_TOOLS,\n): Promise<ProxyMcpServer> {\n const calls = new EventEmitter()\n const pending = new Map<string, ProxyToolCall>()\n\n const server = createServer(async (req, res) => {\n if (req.method !== \"POST\" || !req.url?.startsWith(\"/mcp\")) {\n res.statusCode = 404\n res.end()\n return\n }\n try {\n const body = await readBody(req)\n const request = JSON.parse(body) as {\n jsonrpc?: string\n id?: number | string | null\n method?: string\n params?: Record<string, unknown>\n }\n\n if (request?.jsonrpc !== \"2.0\" || typeof request.method !== \"string\") {\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request?.id ?? null,\n error: { code: -32600, message: \"Invalid request\" },\n })\n return\n }\n\n log.debug(\"proxy-mcp request\", {\n method: request.method,\n id: request.id,\n })\n\n if (request.method === \"initialize\") {\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request.id ?? null,\n result: {\n protocolVersion: PROTOCOL_VERSION,\n capabilities: { tools: {} },\n serverInfo: {\n name: SERVER_NAME,\n version: \"0.1.0\",\n },\n },\n })\n return\n }\n\n if (request.method === \"notifications/initialized\") {\n res.statusCode = 204\n res.end()\n return\n }\n\n if (request.method === \"tools/list\") {\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request.id ?? null,\n result: {\n tools: tools.map((t) => ({\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema,\n })),\n },\n })\n return\n }\n\n if (request.method === \"tools/call\") {\n const params = request.params ?? {}\n const toolName = String(params.name ?? \"\")\n const input = (params.arguments ?? {}) as Record<string, unknown>\n\n if (!tools.some((t) => t.name === toolName)) {\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request.id ?? null,\n error: {\n code: -32601,\n message: `Unknown proxy tool: ${toolName}`,\n },\n })\n return\n }\n\n const callId = crypto.randomUUID()\n log.info(\"proxy-mcp tool call received\", {\n callId,\n toolName,\n hasInput: input != null,\n })\n\n let timer: ReturnType<typeof setTimeout> | null = null\n const result = await new Promise<ProxyToolResult>(\n (resolve, reject) => {\n const entry: ProxyToolCall = {\n id: callId,\n toolName,\n input,\n resolve,\n reject,\n }\n pending.set(callId, entry)\n timer = setTimeout(() => {\n if (!pending.has(callId)) return\n pending.delete(callId)\n // v0.4.13: demoted from warn to notice. Timeouts are usually\n // permission-pending while the user is AFK — surfacing each as\n // a yellow UI bubble produces a wall of noise on return. The\n // file log still captures the event for diagnostics.\n log.notice(\"proxy-mcp tool call timed out\", {\n callId,\n toolName,\n timeoutMs: PROXY_CALL_TIMEOUT_MS,\n })\n reject(\n new Error(\n `Proxy tool '${toolName}' timed out after ${PROXY_CALL_TIMEOUT_MS}ms waiting for opencode to resolve the call`,\n ),\n )\n }, PROXY_CALL_TIMEOUT_MS)\n calls.emit(\"call\", entry)\n },\n ).finally(() => {\n if (timer) clearTimeout(timer)\n pending.delete(callId)\n })\n\n if (result.kind === \"error\") {\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request.id ?? null,\n error: {\n code: -32000,\n message: result.message,\n },\n })\n return\n }\n\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request.id ?? null,\n result: {\n content: [{ type: \"text\", text: result.text }],\n isError: result.isError === true,\n },\n })\n return\n }\n\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request.id ?? null,\n error: { code: -32601, message: `Unknown method: ${request.method}` },\n })\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error)\n // v0.4.13 + v0.4.19: cleanup rejections from the broker propagate up\n // here. None are user-actionable — they fire on AFK-permission timeouts,\n // orphan-rejections after a turn boundary, stream closes, etc. File-log\n // them at NOTICE; other error shapes stay as WARN so genuine bugs remain\n // visible in the TUI.\n const isExpectedCleanup =\n (errorMessage.includes(\"timed out after\") &&\n errorMessage.includes(\"waiting for opencode to resolve\")) ||\n errorMessage.includes(\"rejecting as orphaned\") ||\n errorMessage.includes(\"was orphaned by a new user turn\")\n const logFn = isExpectedCleanup ? log.notice : log.warn\n logFn(\"proxy-mcp error handling request\", {\n error: errorMessage,\n })\n try {\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: null,\n error: {\n code: -32603,\n message: error instanceof Error ? error.message : \"Internal error\",\n },\n })\n } catch {\n try {\n res.statusCode = 500\n res.end()\n } catch {}\n }\n }\n })\n\n await new Promise<void>((resolve, reject) => {\n server.once(\"error\", reject)\n server.listen(0, \"127.0.0.1\", () => {\n server.off(\"error\", reject)\n resolve()\n })\n })\n\n const addr = server.address() as AddressInfo | null\n if (!addr) {\n server.close()\n throw new Error(\"Failed to bind proxy MCP server\")\n }\n\n const url = `http://127.0.0.1:${addr.port}/mcp`\n\n log.info(\"proxy-mcp server started\", {\n url,\n tools: tools.map((t) => t.name),\n })\n\n let configFilePath: string | null = null\n\n const api: ProxyMcpServer = {\n url,\n serverName: SERVER_NAME,\n tools,\n calls,\n configPath() {\n if (configFilePath) return configFilePath\n const body = JSON.stringify(\n {\n mcpServers: {\n [SERVER_NAME]: {\n type: \"http\",\n url,\n },\n },\n },\n null,\n 2,\n )\n const hash = crypto\n .createHash(\"sha256\")\n .update(body)\n .digest(\"hex\")\n .slice(0, 12)\n const outPath = path.join(\n pluginTmpDir(),\n `proxy-${hash}.json`,\n )\n fs.writeFileSync(outPath, body, { encoding: \"utf8\", mode: 0o600 })\n configFilePath = outPath\n return outPath\n },\n async close() {\n for (const entry of pending.values()) {\n entry.reject(new Error(\"proxy MCP server closed\"))\n }\n pending.clear()\n await new Promise<void>((resolve) => {\n server.close(() => resolve())\n })\n if (configFilePath) {\n try {\n fs.unlinkSync(configFilePath)\n } catch {}\n configFilePath = null\n }\n },\n }\n\n return api\n}\n\n/** CLI-ready list of Claude tool names to disable, for each proxied tool. */\nexport function disallowedToolFlags(tools: ProxyToolDef[]): string[] {\n // Map our lowercase MCP tool names to the Claude tool name(s) they replace.\n // `edit` covers both `Edit` and `MultiEdit` because opencode has no\n // MultiEdit equivalent; without disabling MultiEdit, Claude can batch\n // file changes through it and bypass opencode's permission UI.\n // `task` disables Claude CLI's `Agent` tool (its built-in subagent\n // dispatcher) so subagent calls flow through opencode's `task` tool\n // instead — which lets opencode's configured subagent set (`build`,\n // `general`, custom subagents in opencode.json) execute the work\n // under opencode's permission/lifecycle, rather than Claude's\n // internal-only general-purpose / Explore / Plan options.\n const nameMap: Record<string, string[]> = {\n bash: [\"Bash\"],\n read: [\"Read\"],\n write: [\"Write\"],\n edit: [\"Edit\", \"MultiEdit\"],\n glob: [\"Glob\"],\n grep: [\"Grep\"],\n webfetch: [\"WebFetch\"],\n task: [\"Agent\"],\n }\n const out: string[] = []\n const seen = new Set<string>()\n for (const t of tools) {\n const mapped = nameMap[t.name.toLowerCase()]\n if (!mapped) continue\n for (const claudeTool of mapped) {\n if (seen.has(claudeTool)) continue\n seen.add(claudeTool)\n out.push(claudeTool)\n }\n }\n return out\n}\n\nfunction readBody(req: IncomingMessage): Promise<string> {\n return new Promise((resolve, reject) => {\n const chunks: Buffer[] = []\n req.on(\"data\", (chunk: Buffer) => chunks.push(chunk))\n req.on(\"end\", () => resolve(Buffer.concat(chunks).toString(\"utf8\")))\n req.on(\"error\", reject)\n })\n}\n\nfunction writeJson(res: ServerResponse, body: unknown): void {\n const payload = JSON.stringify(body)\n res.statusCode = 200\n res.setHeader(\"Content-Type\", \"application/json\")\n res.setHeader(\"Content-Length\", Buffer.byteLength(payload).toString())\n res.end(payload)\n}\n","import { EventEmitter } from \"node:events\"\nimport type { ProxyToolCall, ProxyToolResult } from \"./proxy-mcp.js\"\nimport { log } from \"./logger.js\"\n\nexport interface PendingProxyCall {\n sessionKey: string\n toolCallId: string\n toolName: string\n input: Record<string, unknown>\n}\n\ntype InternalPending = PendingProxyCall & {\n createdAt: number\n timer: ReturnType<typeof setTimeout>\n resolve(result: ProxyToolResult): void\n reject(error: Error): void\n}\n\n// Primary index: callId -> pending. Tool call IDs are UUIDs produced by\n// proxy-mcp, so they are globally unique across sessions.\nconst pendingByCallId = new Map<string, InternalPending>()\n// Reverse index: sessionKey -> set of callIds, so the language model can\n// drain or reject every pending call for one Claude subprocess at once.\nconst callIdsBySession = new Map<string, Set<string>>()\n\nconst emitter = new EventEmitter()\nconst PENDING_PROXY_CALL_TIMEOUT_MS = 10 * 60 * 1000\n\nfunction eventName(sessionKey: string) {\n return `pending:${sessionKey}`\n}\n\nfunction indexAdd(sessionKey: string, callId: string) {\n let s = callIdsBySession.get(sessionKey)\n if (!s) {\n s = new Set()\n callIdsBySession.set(sessionKey, s)\n }\n s.add(callId)\n}\n\nfunction indexRemove(sessionKey: string, callId: string) {\n const s = callIdsBySession.get(sessionKey)\n if (!s) return\n s.delete(callId)\n if (s.size === 0) callIdsBySession.delete(sessionKey)\n}\n\nexport function onPendingProxyCall(\n sessionKey: string,\n handler: (call: PendingProxyCall) => void,\n): () => void {\n const name = eventName(sessionKey)\n emitter.on(name, handler)\n return () => emitter.off(name, handler)\n}\n\nexport function queuePendingProxyCall(\n sessionKey: string,\n call: ProxyToolCall,\n): PendingProxyCall {\n // Defensive: if this exact callId is somehow already pending (UUID\n // collision or retry storm), replace it cleanly so we never leak two\n // entries for the same id.\n const previous = pendingByCallId.get(call.id)\n if (previous) {\n clearTimeout(previous.timer)\n previous.reject(\n new Error(`Replaced pending proxy call ${call.id} with a fresh one`),\n )\n pendingByCallId.delete(call.id)\n indexRemove(previous.sessionKey, call.id)\n }\n\n const timer = setTimeout(() => {\n const current = pendingByCallId.get(call.id)\n if (!current) return\n pendingByCallId.delete(call.id)\n indexRemove(current.sessionKey, call.id)\n current.reject(\n new Error(\n `Proxy tool call '${call.toolName}' timed out after ${PENDING_PROXY_CALL_TIMEOUT_MS}ms waiting for opencode to resolve the call`,\n ),\n )\n // v0.4.13: demoted from warn to notice. AFK-permission-pending\n // sessions can stack many of these; demoting keeps the UI quiet on\n // return while preserving the audit trail in plugin.log.\n log.notice(\"timed out pending proxy call\", {\n sessionKey: current.sessionKey,\n toolCallId: call.id,\n toolName: call.toolName,\n timeoutMs: PENDING_PROXY_CALL_TIMEOUT_MS,\n })\n }, PENDING_PROXY_CALL_TIMEOUT_MS)\n\n const pending: InternalPending = {\n sessionKey,\n toolCallId: call.id,\n toolName: call.toolName,\n input: call.input,\n createdAt: Date.now(),\n timer,\n resolve: call.resolve,\n reject: call.reject,\n }\n pendingByCallId.set(call.id, pending)\n indexAdd(sessionKey, call.id)\n emitter.emit(eventName(sessionKey), pending)\n log.info(\"queued pending proxy call\", {\n sessionKey,\n toolCallId: call.id,\n toolName: call.toolName,\n })\n return pending\n}\n\nexport function getPendingProxyCalls(sessionKey: string): PendingProxyCall[] {\n const s = callIdsBySession.get(sessionKey)\n if (!s || s.size === 0) return []\n const out: PendingProxyCall[] = []\n for (const id of s) {\n const p = pendingByCallId.get(id)\n if (p) out.push(p)\n }\n return out\n}\n\nexport function resolvePendingProxyCallById(\n toolCallId: string,\n result: ProxyToolResult,\n): boolean {\n const pending = pendingByCallId.get(toolCallId)\n if (!pending) return false\n pendingByCallId.delete(toolCallId)\n indexRemove(pending.sessionKey, toolCallId)\n clearTimeout(pending.timer)\n pending.resolve(result)\n log.info(\"resolved pending proxy call\", {\n sessionKey: pending.sessionKey,\n toolCallId: pending.toolCallId,\n toolName: pending.toolName,\n })\n return true\n}\n\nexport function rejectPendingProxyCallById(\n toolCallId: string,\n error: Error,\n): boolean {\n const pending = pendingByCallId.get(toolCallId)\n if (!pending) return false\n pendingByCallId.delete(toolCallId)\n indexRemove(pending.sessionKey, toolCallId)\n clearTimeout(pending.timer)\n pending.reject(error)\n // Rejection is the broker's cleanup mechanism — fires on timeouts, orphans,\n // stream closes, etc. None are user-actionable. File-log them at NOTICE so\n // the audit trail is intact; rely on caller sites to decide TUI visibility.\n log.notice(\"rejected pending proxy call\", {\n sessionKey: pending.sessionKey,\n toolCallId: pending.toolCallId,\n toolName: pending.toolName,\n error: error.message,\n })\n return true\n}\n\nexport function rejectAllPendingProxyCallsForSession(\n sessionKey: string,\n error: Error,\n): number {\n const s = callIdsBySession.get(sessionKey)\n if (!s) return 0\n const ids = [...s]\n let count = 0\n for (const id of ids) {\n if (rejectPendingProxyCallById(id, error)) count++\n }\n return count\n}\n","import type { OpenCodeModel } from \"./opencode-types.js\"\n\nconst PROVIDER_ID = \"claude-code\"\nconst NPM = \"@khalilgharbaoui/opencode-claude-code-plugin\"\n\nconst reasoningVariants: Record<string, Record<string, unknown>> = {\n low: { reasoningEffort: \"low\" },\n medium: { reasoningEffort: \"medium\" },\n high: { reasoningEffort: \"high\" },\n xhigh: { reasoningEffort: \"xhigh\" },\n max: { reasoningEffort: \"max\" },\n}\n\nconst baseCapabilities = {\n temperature: false,\n attachment: true,\n toolcall: true,\n input: { text: true, audio: false, image: true, video: false, pdf: false },\n output: { text: true, audio: false, image: false, video: false, pdf: false },\n interleaved: false as const,\n}\n\nfunction defineModel(opts: {\n id: string\n name: string\n family: string\n reasoning: boolean\n context: number\n output: number\n cost: { input: number; output: number; cacheRead: number; cacheWrite: number }\n releaseDate: string\n status?: OpenCodeModel[\"status\"]\n}): OpenCodeModel {\n return {\n id: opts.id,\n providerID: PROVIDER_ID,\n api: { id: opts.id, url: \"\", npm: NPM },\n name: opts.name,\n family: opts.family,\n capabilities: { ...baseCapabilities, reasoning: opts.reasoning },\n cost: {\n input: opts.cost.input,\n output: opts.cost.output,\n cache: { read: opts.cost.cacheRead, write: opts.cost.cacheWrite },\n },\n limit: { context: opts.context, output: opts.output },\n status: opts.status ?? \"active\",\n options: {},\n headers: {},\n release_date: opts.releaseDate,\n variants: opts.reasoning ? reasoningVariants : undefined,\n }\n}\n\n// Per-token costs derived from Anthropic per-million-token pricing\nconst haikuCost = { input: 1e-6, output: 5e-6, cacheRead: 1e-7, cacheWrite: 1.25e-6 }\nconst sonnetCost = { input: 3e-6, output: 15e-6, cacheRead: 3e-7, cacheWrite: 3.75e-6 }\nconst opusCost = { input: 15e-6, output: 75e-6, cacheRead: 1.5e-6, cacheWrite: 18.75e-6 }\n\n/**\n * Convert an OpenCodeModel to the flat config schema that OpenCode's\n * provider.ts config parser expects (model.temperature, model.reasoning,\n * model.cost.cache_read, model.modalities, etc.).\n */\nexport function toConfigModel(model: OpenCodeModel): Record<string, unknown> {\n const inputMods: string[] = []\n const outputMods: string[] = []\n for (const [k, v] of Object.entries(model.capabilities.input)) {\n if (v) inputMods.push(k)\n }\n for (const [k, v] of Object.entries(model.capabilities.output)) {\n if (v) outputMods.push(k)\n }\n\n return {\n id: model.api.id,\n name: model.name,\n status: model.status,\n family: model.family ?? \"\",\n release_date: model.release_date,\n\n temperature: model.capabilities.temperature,\n reasoning: model.capabilities.reasoning,\n attachment: model.capabilities.attachment,\n tool_call: model.capabilities.toolcall,\n modalities: { input: inputMods, output: outputMods },\n\n cost: {\n input: model.cost.input,\n output: model.cost.output,\n cache_read: model.cost.cache.read,\n cache_write: model.cost.cache.write,\n },\n\n limit: model.limit,\n options: model.options,\n headers: model.headers,\n variants: model.variants,\n }\n}\n\nexport const defaultModels: Record<string, OpenCodeModel> = {\n \"claude-haiku-4-5\": defineModel({\n id: \"claude-haiku-4-5\",\n name: \"Claude Haiku 4.5\",\n family: \"haiku\",\n reasoning: false,\n context: 200_000,\n output: 8_192,\n cost: haikuCost,\n releaseDate: \"2024-10-22\",\n }),\n \"claude-sonnet-4-5\": defineModel({\n id: \"claude-sonnet-4-5\",\n name: \"Claude Sonnet 4.5\",\n family: \"sonnet\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: sonnetCost,\n releaseDate: \"2025-04-14\",\n }),\n \"claude-sonnet-4-6\": defineModel({\n id: \"claude-sonnet-4-6\",\n name: \"Claude Sonnet 4.6\",\n family: \"sonnet\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: sonnetCost,\n releaseDate: \"2025-06-19\",\n }),\n \"claude-opus-4-5\": defineModel({\n id: \"claude-opus-4-5\",\n name: \"Claude Opus 4.5\",\n family: \"opus\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: opusCost,\n releaseDate: \"2025-04-14\",\n }),\n \"claude-opus-4-6\": defineModel({\n id: \"claude-opus-4-6\",\n name: \"Claude Opus 4.6\",\n family: \"opus\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: opusCost,\n releaseDate: \"2025-06-19\",\n }),\n \"claude-opus-4-7\": defineModel({\n id: \"claude-opus-4-7\",\n name: \"Claude Opus 4.7\",\n family: \"opus\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: opusCost,\n releaseDate: \"2025-07-16\",\n }),\n \"claude-opus-4-8\": defineModel({\n id: \"claude-opus-4-8\",\n name: \"Claude Opus 4.8\",\n family: \"opus\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: opusCost,\n releaseDate: \"2026-05-28\",\n }),\n}\n","import { chmod, lstat, mkdir, readlink, symlink, writeFile } from \"node:fs/promises\"\nimport path from \"node:path\"\nimport { log } from \"./logger.js\"\n\nexport const BASE_PROVIDER_ID = \"claude-code\"\nexport const DEFAULT_ACCOUNT = \"default\"\n\nconst SHARED_CAPABILITY_ITEMS = [\n \"CLAUDE.md\",\n \"settings.json\",\n \"skills\",\n \"agents\",\n \"commands\",\n \"plugins\",\n]\n\nexport function normalizeAccountName(account: string): string {\n return account\n .trim()\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, \"-\")\n .replace(/^-+|-+$/g, \"\")\n}\n\nexport function resolveAccounts(value: unknown): string[] | null {\n if (!Array.isArray(value)) return null\n\n const accounts = value\n .map((account) => normalizeAccountName(String(account)))\n .filter(Boolean)\n\n return Array.from(new Set([DEFAULT_ACCOUNT, ...accounts]))\n}\n\nexport function accountProviderId(account: string): string {\n return `${BASE_PROVIDER_ID}-${normalizeAccountName(account)}`\n}\n\nexport function accountDisplayName(account: string): string {\n return `Claude Code (${titleizeAccount(account)})`\n}\n\nexport function accountModelSuffix(account: string): string | undefined {\n const normalized = normalizeAccountName(account)\n return normalized === DEFAULT_ACCOUNT ? undefined : normalized\n}\n\nexport function accountConfigDir(account: string): string | undefined {\n const normalized = normalizeAccountName(account)\n\n if (!normalized || normalized === DEFAULT_ACCOUNT) return undefined\n\n return `~/.claude-${normalized}`\n}\n\nexport function expandHome(value: string): string {\n const home = process.env.HOME ?? process.env.USERPROFILE\n\n if (value === \"~\") return home ?? value\n\n if (value.startsWith(\"~/\") || value.startsWith(\"~\\\\\")) {\n return home ? path.join(home, value.slice(2)) : value\n }\n\n return value\n}\n\nexport async function ensureAccountRuntime(\n account: string,\n baseCliPath: string,\n): Promise<{ cliPath: string; configDir?: string }> {\n const configDir = accountConfigDir(account)\n\n if (!configDir) return { cliPath: baseCliPath }\n\n const expandedConfigDir = expandHome(configDir)\n await mkdir(expandedConfigDir, { recursive: true })\n\n try {\n await ensureSharedCapabilities(expandedConfigDir)\n } catch (err) {\n log.warn(\"failed to symlink shared capabilities; continuing anyway\", {\n account,\n configDir: expandedConfigDir,\n error: String(err),\n })\n }\n\n const cliPath = await writeAccountWrapper(\n normalizeAccountName(account),\n baseCliPath,\n expandedConfigDir,\n )\n\n return { cliPath, configDir }\n}\n\nasync function ensureSharedCapabilities(targetRoot: string): Promise<void> {\n const sourceRoot = expandHome(\"~/.claude\")\n\n for (const item of SHARED_CAPABILITY_ITEMS) {\n await ensureSharedCapabilityItem(sourceRoot, targetRoot, item)\n }\n}\n\nasync function ensureSharedCapabilityItem(\n sourceRoot: string,\n targetRoot: string,\n item: string,\n): Promise<void> {\n const source = path.join(sourceRoot, item)\n const target = path.join(targetRoot, item)\n\n let sourceStat\n try {\n sourceStat = await lstat(source)\n } catch {\n return\n }\n\n try {\n const targetStat = await lstat(target)\n\n if (targetStat.isSymbolicLink()) {\n const current = await readlink(target)\n const resolvedCurrent = path.resolve(path.dirname(target), current)\n const resolvedSource = path.resolve(source)\n\n if (resolvedCurrent === resolvedSource) return\n }\n\n log.warn(\"shared Claude capability already exists; leaving untouched\", {\n item,\n target,\n source,\n })\n\n return\n } catch {\n // Missing target is expected.\n }\n\n const type = sourceStat.isDirectory()\n ? process.platform === \"win32\"\n ? \"junction\"\n : \"dir\"\n : \"file\"\n\n await symlink(source, target, type)\n}\n\nasync function writeAccountWrapper(\n account: string,\n baseCliPath: string,\n configDir: string,\n): Promise<string> {\n const cacheRoot = path.join(\n process.env.XDG_CACHE_HOME ?? expandHome(\"~/.cache\"),\n \"opencode-claude-code-plugin\",\n )\n const wrapperPath = path.join(cacheRoot, `claude-${account}`)\n const suffix = `@${account}`\n\n await mkdir(cacheRoot, { recursive: true })\n\n const script = `#!/usr/bin/env bash\nset -euo pipefail\n\nargs=()\nwhile [[ $# -gt 0 ]]; do\n if [[ \"$1\" == \"--model\" && $# -ge 2 ]]; then\n model=\"$2\"\n if [[ \"$model\" == *${shellDoubleQuote(suffix)} ]]; then\n model=\"\\${model%${shellDoubleQuote(suffix)}}\"\n fi\n args+=(\"$1\" \"$model\")\n shift 2\n else\n args+=(\"$1\")\n shift\n fi\ndone\n\nexport CLAUDE_CONFIG_DIR=${shellSingleQuote(configDir)}\nexec ${shellSingleQuote(baseCliPath)} \"\\${args[@]}\"\n`\n\n await writeFile(wrapperPath, script, \"utf8\")\n await chmod(wrapperPath, 0o755)\n\n return wrapperPath\n}\n\nfunction shellSingleQuote(value: string): string {\n return `'${value.replace(/'/g, `'\"'\"'`)}'`\n}\n\nfunction shellDoubleQuote(value: string): string {\n return value.replace(/[$`\"\\\\]/g, \"\\\\$&\")\n}\n\nfunction titleizeAccount(account: string): string {\n return normalizeAccountName(account)\n .split(\"-\")\n .filter(Boolean)\n .map((part) => part.charAt(0).toUpperCase() + part.slice(1))\n .join(\" \")\n}\n","// Removes a stale unscoped `opencode-claude-code-plugin` install left in\n// opencode's plugin cache by older configs. The unscoped name is a different\n// artifact than this scoped plugin and shadows it when both coexist.\n// Disable with OPENCODE_CLAUDE_CODE_PLUGIN_NO_CLEANUP=1.\n\nimport {\n existsSync,\n readFileSync,\n realpathSync,\n rmSync,\n writeFileSync,\n} from \"node:fs\"\nimport { homedir } from \"node:os\"\nimport { join, resolve } from \"node:path\"\nimport { fileURLToPath } from \"node:url\"\nimport { log } from \"./logger.js\"\n\nconst STALE_PACKAGE_NAME = \"opencode-claude-code-plugin\"\nconst SUSPECT_DESCRIPTION_TOKEN = \"Claude Code\"\n\nlet alreadyRan = false\n\nfunction candidateCacheRoots(): string[] {\n const xdg = process.env.XDG_CACHE_HOME\n return [\n xdg ? join(xdg, \"opencode\") : null,\n join(homedir(), \".cache\", \"opencode\"),\n join(homedir(), \"Library\", \"Caches\", \"opencode\"),\n ].filter((p): p is string => Boolean(p))\n}\n\nfunction userOpencodeJsonPath(): string {\n const xdgConfig = process.env.XDG_CONFIG_HOME ?? join(homedir(), \".config\")\n return join(xdgConfig, \"opencode\", \"opencode.json\")\n}\n\nfunction userIntendsToUseUnscoped(): boolean {\n const cfg = userOpencodeJsonPath()\n if (!existsSync(cfg)) return false\n try {\n const json = JSON.parse(readFileSync(cfg, \"utf8\"))\n const plugins: unknown = json.plugin\n if (!Array.isArray(plugins)) return false\n return plugins.some(\n (entry) =>\n typeof entry === \"string\" &&\n /^opencode-claude-code-plugin(@[^/]+)?$/.test(entry),\n )\n } catch {\n return false\n }\n}\n\nfunction ourLoadedDir(): string | null {\n try {\n const filePath = fileURLToPath(import.meta.url)\n return realpathSync(resolve(filePath, \"..\", \"..\"))\n } catch {\n return null\n }\n}\n\nexport function cleanupStaleUnscopedInstall(): void {\n if (alreadyRan) return\n alreadyRan = true\n\n if (process.env.OPENCODE_CLAUDE_CODE_PLUGIN_NO_CLEANUP === \"1\") return\n if (userIntendsToUseUnscoped()) return\n\n const ourDir = ourLoadedDir()\n\n for (const cacheRoot of candidateCacheRoots()) {\n try {\n cleanupOne(cacheRoot, ourDir)\n } catch (err) {\n log.warn(\"cleanup-stale: error processing cache root\", {\n cacheRoot,\n error: String(err),\n })\n }\n }\n}\n\nfunction cleanupOne(cacheRoot: string, ourDir: string | null): void {\n if (!existsSync(cacheRoot)) return\n\n const stalePath = join(cacheRoot, \"node_modules\", STALE_PACKAGE_NAME)\n if (!existsSync(stalePath)) return\n\n // Don't self-delete if we are the unscoped install.\n let realStalePath = stalePath\n try {\n realStalePath = realpathSync(stalePath)\n } catch {\n // ignore\n }\n if (ourDir && realStalePath === ourDir) return\n\n // Verify identity before removing.\n const pkgJsonPath = join(stalePath, \"package.json\")\n if (!existsSync(pkgJsonPath)) return\n let pkg: { name?: string; description?: string } = {}\n try {\n pkg = JSON.parse(readFileSync(pkgJsonPath, \"utf8\"))\n } catch {\n return\n }\n if (pkg.name !== STALE_PACKAGE_NAME) return\n if (!pkg.description?.includes(SUSPECT_DESCRIPTION_TOKEN)) return\n\n log.info(\"cleanup-stale: removing unscoped install\", { stalePath })\n try {\n rmSync(stalePath, { recursive: true, force: true })\n } catch (err) {\n log.warn(\"cleanup-stale: rmSync failed\", {\n stalePath,\n error: String(err),\n })\n return\n }\n\n // Drop the dep from the cache root's package.json so opencode's installer\n // doesn't reinstate it on its next pass. Lockfile is left alone; bun\n // reconciles against package.json on the next install.\n const cachePkgJson = join(cacheRoot, \"package.json\")\n if (!existsSync(cachePkgJson)) return\n try {\n const cfg = JSON.parse(readFileSync(cachePkgJson, \"utf8\"))\n if (cfg?.dependencies?.[STALE_PACKAGE_NAME]) {\n delete cfg.dependencies[STALE_PACKAGE_NAME]\n writeFileSync(cachePkgJson, JSON.stringify(cfg, null, 2) + \"\\n\")\n log.info(\"cleanup-stale: pruned dep from cache package.json\")\n }\n } catch (err) {\n log.warn(\"cleanup-stale: cache package.json update failed\", {\n error: String(err),\n })\n }\n}\n","import type { LanguageModelV3 } from \"@ai-sdk/provider\"\nimport { ClaudeCodeLanguageModel } from \"./claude-code-language-model.js\"\nimport { defaultModels, toConfigModel } from \"./models.js\"\nimport type { OpenCodeModel, OpenCodePlugin, OpenCodeProvider } from \"./opencode-types.js\"\nimport type { ClaudeCodeProviderSettings } from \"./types.js\"\nimport {\n BASE_PROVIDER_ID,\n accountDisplayName,\n accountModelSuffix,\n accountProviderId,\n ensureAccountRuntime,\n resolveAccounts,\n} from \"./accounts.js\"\nimport { cleanupStaleUnscopedInstall } from \"./cleanup-stale.js\"\nimport { configureLogger, log } from \"./logger.js\"\nimport {\n isUsableDirectory,\n setOpencodeClient,\n setOpencodeProjectDirectory,\n} from \"./runtime-status.js\"\n\nexport interface ClaudeCodeProvider {\n specificationVersion: \"v3\"\n (modelId: string): LanguageModelV3\n languageModel(modelId: string): LanguageModelV3\n}\n\n// Picks the best directory from opencode's plugin context (`directory` /\n// `worktree`). Result is handed to runtime-status so it's available as a\n// *fallback* at spawn time only when `process.cwd()` is unusable (macOS\n// GUI launches at `/`). Never baked into provider config — see #4.\nfunction pickOpencodeDirectory(input: unknown): string | undefined {\n if (!input || typeof input !== \"object\") return undefined\n const ctx = input as { directory?: unknown; worktree?: unknown }\n if (isUsableDirectory(ctx.directory)) return ctx.directory\n if (isUsableDirectory(ctx.worktree)) return ctx.worktree\n return undefined\n}\n\nexport function createClaudeCode(\n settings: ClaudeCodeProviderSettings = {},\n): ClaudeCodeProvider {\n if (settings.logging) {\n configureLogger({\n file: settings.logging.file ?? false,\n dir: settings.logging.dir ?? null,\n mode: settings.logging.mode ?? \"silent\",\n level: settings.logging.level ?? \"info\",\n })\n }\n const cliPath =\n settings.cliPath ?? process.env.CLAUDE_CLI_PATH ?? \"claude\"\n const providerName = settings.providerID ?? settings.name ?? \"claude-code\"\n const proxyTools = settings.proxyTools ?? [\"Bash\", \"Edit\", \"Write\", \"WebFetch\"]\n\n const createModel = (modelId: string): LanguageModelV3 => {\n return new ClaudeCodeLanguageModel(modelId, {\n provider: providerName,\n cliPath,\n cwd: settings.cwd,\n account: settings.account,\n configDir: settings.configDir,\n providerID: settings.providerID,\n skipPermissions: settings.skipPermissions ?? true,\n permissionMode: settings.permissionMode,\n mcpConfig: settings.mcpConfig,\n strictMcpConfig: settings.strictMcpConfig,\n bridgeOpencodeMcp: settings.bridgeOpencodeMcp ?? true,\n controlRequestBehavior: settings.controlRequestBehavior ?? \"allow\",\n controlRequestToolBehaviors: settings.controlRequestToolBehaviors,\n controlRequestDenyMessage: settings.controlRequestDenyMessage,\n proxyTools,\n webSearch: settings.webSearch,\n hotReloadMcp: settings.hotReloadMcp ?? true,\n proxyOpencodeMcpTools: settings.proxyOpencodeMcpTools ?? true,\n multiStepContinuation: settings.multiStepContinuation ?? true,\n autoContinueIncompleteTurns:\n settings.autoContinueIncompleteTurns ?? \"smart\",\n compactionModel: settings.compactionModel,\n })\n }\n\n const provider = function (modelId: string) {\n return createModel(modelId)\n } as ClaudeCodeProvider\n\n provider.specificationVersion = \"v3\"\n provider.languageModel = createModel\n\n return provider\n}\n\n// ---------------------------------------------------------------------------\n// OpenCode plugin interface\n// ---------------------------------------------------------------------------\n\nconst PROVIDER_ID = BASE_PROVIDER_ID\nconst PACKAGE_NPM = \"@khalilgharbaoui/opencode-claude-code-plugin\"\n\nfunction pluginEntrypoint(): string {\n return import.meta.url.startsWith(\"file:\") ? import.meta.url : PACKAGE_NPM\n}\n\nfunction cleanProviderOptions(\n options: Record<string, unknown> = {},\n): Record<string, unknown> {\n const result = { ...options }\n delete result.accounts\n return result\n}\n\nfunction defaultModelsForProvider(\n providerModels: OpenCodeProvider[\"models\"],\n providerID = PROVIDER_ID,\n modelSuffix?: string,\n) {\n const models = Object.fromEntries(\n Object.entries(defaultModels).map(([id, model]) => {\n const modelId = modelSuffix ? `${id}@${modelSuffix}` : id\n const existing = providerModels[id] ?? providerModels[modelId]\n return [\n modelId,\n {\n ...model,\n id: modelId,\n providerID,\n api: {\n ...model.api,\n id: modelId,\n npm: existing?.api?.npm ?? model.api.npm,\n url: existing?.api?.url ?? model.api.url,\n },\n },\n ]\n }),\n )\n\n for (const [id, model] of Object.entries(providerModels)) {\n if (!(id in models)) {\n models[id] = {\n ...model,\n providerID,\n }\n }\n }\n\n return models\n}\n\n/**\n * Build models in OpenCode's config schema format (flat properties like\n * `temperature`, `reasoning`, `cost.cache_read`, `modalities`, etc.)\n * so the config-path provider loader parses them correctly.\n */\nexport function configModelsForProvider(\n providerModels: OpenCodeProvider[\"models\"],\n providerID: string,\n modelSuffix?: string,\n): Record<string, Record<string, unknown>> {\n const models: Record<string, Record<string, unknown>> = {}\n\n for (const [id, model] of Object.entries(defaultModels)) {\n const modelId = modelSuffix ? `${id}@${modelSuffix}` : id\n const existing = providerModels[id] ?? providerModels[modelId]\n const existingVariants =\n existing && typeof (existing as { variants?: unknown }).variants === \"object\"\n ? ((existing as { variants?: Record<string, Record<string, unknown>> }).variants ?? {})\n : {}\n const full: OpenCodeModel = {\n ...model,\n id: modelId,\n providerID,\n api: {\n ...model.api,\n id: modelId,\n npm: existing?.api?.npm ?? model.api.npm,\n url: existing?.api?.url ?? model.api.url,\n },\n variants: {\n ...(model.variants ?? {}),\n ...existingVariants,\n },\n }\n models[modelId] = toConfigModel(full)\n }\n\n for (const [id, model] of Object.entries(providerModels)) {\n if (!(id in models)) {\n models[id] = toConfigModel({ ...model, providerID } as OpenCodeModel)\n }\n }\n\n return models\n}\n\nasync function providerConfig(\n existing: {\n name?: string\n npm?: string\n options?: Record<string, unknown>\n models?: Record<string, unknown>\n } | undefined,\n providerID = PROVIDER_ID,\n optionDefaults: Record<string, unknown> = {},\n displayName?: string,\n) {\n const mergedOptions: Record<string, unknown> = {\n cliPath: \"claude\",\n proxyTools: [\"Bash\", \"Edit\", \"Write\", \"WebFetch\"],\n ...optionDefaults,\n ...cleanProviderOptions(existing?.options),\n providerID,\n }\n\n const cliPath = String(mergedOptions.cliPath ?? \"claude\")\n const account =\n typeof mergedOptions.account === \"string\" ? mergedOptions.account : undefined\n const runtime = account\n ? await ensureAccountRuntime(account, cliPath)\n : { cliPath }\n\n return {\n name: displayName ?? existing?.name,\n npm: existing?.npm ?? pluginEntrypoint(),\n options: {\n ...mergedOptions,\n ...runtime,\n },\n // models is intentionally omitted: both callers overwrite it with\n // configModelsForProvider(), which emits the flat config schema\n // opencode's config-path loader parses (and merges user variants).\n }\n}\n\nasync function expandAccountProviders(config: {\n provider?: Record<\n string,\n {\n name?: string\n npm?: string\n options?: Record<string, unknown>\n models?: Record<string, unknown>\n }\n >\n}): Promise<boolean> {\n const seed = config.provider?.[PROVIDER_ID]\n const accounts = resolveAccounts(seed?.options?.accounts)\n\n if (!accounts) return false\n\n config.provider ??= {}\n\n const seedOptions = cleanProviderOptions(seed?.options)\n let expandedCount = 0\n\n for (const account of accounts) {\n const providerID = accountProviderId(account)\n try {\n const existing = config.provider[providerID]\n const modelSuffix = accountModelSuffix(account)\n\n config.provider[providerID] = {\n ...existing,\n ...(await providerConfig(\n existing,\n providerID,\n {\n ...seedOptions,\n account,\n },\n accountDisplayName(account),\n )),\n models: configModelsForProvider(\n (existing?.models ?? seed?.models ?? {}) as OpenCodeProvider[\"models\"],\n providerID,\n modelSuffix,\n ),\n }\n expandedCount++\n } catch (err) {\n log.error(\"failed to expand account provider\", {\n account,\n providerID,\n error: String(err),\n })\n }\n }\n\n if (expandedCount > 0) {\n delete config.provider[PROVIDER_ID]\n }\n\n return expandedCount > 0\n}\n\nconst server: OpenCodePlugin = async (input) => {\n cleanupStaleUnscopedInstall()\n\n // Capture the SDK client so the language model can query opencode's\n // in-memory MCP state per-turn for the runtime overlay. `input` is\n // `unknown` here (kept loose since opencode adds fields over time);\n // narrow defensively.\n if (input && typeof input === \"object\" && \"client\" in input) {\n setOpencodeClient((input as { client?: unknown }).client)\n }\n\n // Capture opencode's project-aware directory as a *fallback* used at\n // Claude CLI spawn time only when `process.cwd()` is unusable. Rescues\n // macOS GUI launches at `/` without freezing the value into provider\n // config, so opencode workspace switches mid-session still take effect.\n // See `resolveSpawnCwd` in runtime-status.ts and issue #4.\n setOpencodeProjectDirectory(pickOpencodeDirectory(input))\n\n return {\n config: async (config) => {\n config.provider ??= {}\n\n const expanded = await expandAccountProviders(config)\n if (expanded) {\n const registered = Object.entries(config.provider)\n .filter(([id]) => id === PROVIDER_ID || id.startsWith(`${PROVIDER_ID}-`))\n .map(([id, p]) => ({\n id,\n name: p?.name ?? id,\n cwd: (p?.options as { cwd?: unknown } | undefined)?.cwd,\n }))\n log.notice(\"registered claude-code providers\", { providers: registered })\n return\n }\n\n const existing = config.provider[PROVIDER_ID]\n config.provider[PROVIDER_ID] = {\n ...existing,\n ...(await providerConfig(existing)),\n models: configModelsForProvider(\n (existing?.models ?? {}) as OpenCodeProvider[\"models\"],\n PROVIDER_ID,\n ),\n }\n log.notice(\"registered claude-code provider\", {\n id: PROVIDER_ID,\n name: config.provider[PROVIDER_ID]?.name ?? PROVIDER_ID,\n cwd: (config.provider[PROVIDER_ID]?.options as { cwd?: unknown } | undefined)?.cwd,\n })\n },\n // No `event` hook: MCP config drift is detected at turn start by the\n // hot-reload check in `claude-code-language-model.ts`, which respawns\n // claude safely between turns. Eviction on `global.disposed` would kill\n // an in-flight stream and abort the user's current turn.\n provider: {\n id: PROVIDER_ID,\n models: async (provider) => defaultModelsForProvider(provider.models),\n },\n // Inject opencode's agent name into providerOptions so the language\n // model can distinguish /compact (and title) calls from normal turns.\n // Without this, every no-tools call looks like a title request and\n // gets short-circuited to a synthetic stub.\n \"chat.params\": async (input, output) => {\n const providerID = input.model?.providerID ?? input.provider?.info?.id\n // The hook fires for every provider opencode is configured with, not\n // just ours — keep this at debug to avoid log spam on non-claude-code\n // calls.\n log.debug(\"chat.params hook fired\", {\n agent: input.agent,\n providerID,\n sessionID: input.sessionID,\n })\n if (typeof providerID !== \"string\") return\n if (providerID !== PROVIDER_ID && !providerID.startsWith(`${PROVIDER_ID}-`)) return\n\n // Inject sessionID BEFORE the agent guard so session isolation works\n // even when input.agent is absent (older opencode, provider-switch\n // edge paths). resolveSessionAffinity reads this as a fallback when\n // the x-session-affinity header is missing.\n if (typeof input.sessionID === \"string\" && input.sessionID.length > 0) {\n output.options ??= {}\n ;(output.options as Record<string, unknown>).opencodeSessionID = input.sessionID\n }\n\n if (!input.agent) return\n // opencode wraps the entire `output.options` bag under the providerID\n // via ProviderTransform.providerOptions(model, options) → { [providerID]: options }\n // before handing it to the language model as `providerOptions`. So we\n // write fields at the TOP LEVEL of output.options, not nested under\n // providerID — otherwise the model sees providerOptions[id][id].opencodeAgent.\n output.options ??= {}\n ;(output.options as Record<string, unknown>).opencodeAgent = input.agent\n log.debug(\"chat.params tagged providerOptions\", {\n agent: input.agent,\n sessionID: input.sessionID,\n providerID,\n })\n },\n }\n}\n\nexport default {\n id: \"@khalilgharbaoui/opencode-claude-code-plugin\",\n server,\n}\n\n// ---------------------------------------------------------------------------\n// Re-exports\n// ---------------------------------------------------------------------------\n\nexport { ClaudeCodeLanguageModel } from \"./claude-code-language-model.js\"\nexport { bridgeOpencodeMcp } from \"./mcp-bridge.js\"\nexport { defaultModels } from \"./models.js\"\nexport type {\n ClaudeCodeConfig,\n ClaudeCodeProviderSettings,\n ClaudeStreamMessage,\n} from \"./types.js\"\nexport type { OpenCodeHooks, OpenCodeModel, OpenCodePlugin } from \"./opencode-types.js\"\n"],"mappings":";AASA,SAAS,kBAAkB;;;ACT3B,SAAS,gBAAgB,WAAW,YAAY,gBAAgB;AAChE,SAAS,eAAe;AACxB,SAAS,SAAS,YAAY;AAY9B,IAAM,aAAuC;AAAA,EAC3C,OAAO;AAAA,EACP,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,OAAO;AACT;AAEA,IAAM,gBAAgB,IAAI,OAAO;AACjC,IAAM,cAAc,KAAK,QAAQ,GAAG,UAAU,SAAS,sBAAsB;AAE7E,IAAM,iBAA+B;AAAA,EACnC,MAAM;AAAA,EACN,KAAK;AAAA,EACL,MAAM;AAAA,EACN,OAAO;AACT;AAEA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OA,OAAM;AAAA,QACb;AAAA,QACA,WAAWA,OAAM;AAAA,MACnB,CAAC;AACD,UAAI,OAAO,eAAe,SAAU;AACpC,UAAI,eAAeF,gBAAe,CAAC,WAAW,WAAW,GAAGA,YAAW,GAAG,EAAG;AAM7E,UAAI,OAAOE,OAAM,cAAc,YAAYA,OAAM,UAAU,SAAS,GAAG;AACrE,eAAO,YAAY,CAAC;AACnB,QAAC,OAAO,QAAoC,oBAAoBA,OAAM;AAAA,MACzE;AAEA,UAAI,CAACA,OAAM,MAAO;AAMlB,aAAO,YAAY,CAAC;AACnB,MAAC,OAAO,QAAoC,gBAAgBA,OAAM;AACnE,UAAI,MAAM,sCAAsC;AAAA,QAC9C,OAAOA,OAAM;AAAA,QACb,WAAWA,OAAM;AAAA,QACjB;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEA,IAAO,gBAAQ;AAAA,EACb,IAAI;AAAA,EACJ;AACF;","names":["fs","path","os","sessionKey","fs","path","crypto","EventEmitter","EventEmitter","server","resolve","EventEmitter","EventEmitter","sessionKey","readFileSync","writeFileSync","unlink","homedir","tmpdir","randomUUID","dirname","join","content","path","spawn","createInterface","resolve","stream","path","path","existsSync","readFileSync","rmSync","writeFileSync","homedir","join","resolve","join","homedir","existsSync","readFileSync","resolve","rmSync","writeFileSync","PROVIDER_ID","registered","input"]}
1
+ {"version":3,"sources":["../src/claude-code-language-model.ts","../src/logger.ts","../src/todo-ledger.ts","../src/tool-mapping.ts","../src/message-builder.ts","../src/mcp-bridge.ts","../src/tmp.ts","../src/runtime-status.ts","../src/session-manager.ts","../src/cli-version.ts","../src/proxy-mcp.ts","../src/proxy-broker.ts","../src/models.ts","../src/accounts.ts","../src/cleanup-stale.ts","../src/index.ts"],"sourcesContent":["import type {\n LanguageModelV3,\n LanguageModelV3CallOptions,\n LanguageModelV3Content,\n LanguageModelV3FinishReason,\n LanguageModelV3StreamPart,\n LanguageModelV3Usage,\n SharedV3Warning,\n} from \"@ai-sdk/provider\"\nimport { generateId } from \"@ai-sdk/provider-utils\"\nimport type {\n ClaudeCodeConfig,\n ControlRequestBehavior,\n ClaudeStreamMessage,\n ReasoningEffort,\n} from \"./types.js\"\nimport { mapTool } from \"./tool-mapping.js\"\nimport { applyTaskCreateToolResult } from \"./todo-ledger.js\"\nimport { getClaudeUserMessage } from \"./message-builder.js\"\nimport { bridgeOpencodeMcp, type RuntimeMcpStatus } from \"./mcp-bridge.js\"\nimport {\n getRuntimeMcpStatus,\n fetchOpencodeToolList,\n resolveSpawnCwd,\n} from \"./runtime-status.js\"\nimport {\n getActiveProcess,\n spawnClaudeProcess,\n buildCliArgs,\n setClaudeSessionId,\n getClaudeSessionId,\n deleteClaudeSessionId,\n deleteActiveProcess,\n claudeSpawnEnv,\n isClaudeThinkingDisabled,\n sessionKey,\n} from \"./session-manager.js\"\nimport { log } from \"./logger.js\"\nimport { detectCliVersion } from \"./cli-version.js\"\nimport {\n createProxyMcpServer,\n disallowedToolFlags,\n DEFAULT_PROXY_TOOLS,\n PROXY_TOOL_PREFIX,\n type ProxyMcpServer,\n type ProxyToolCall,\n type ProxyToolDef,\n type ProxyToolResult,\n} from \"./proxy-mcp.js\"\nimport {\n getPendingProxyCalls,\n onPendingProxyCall,\n queuePendingProxyCall,\n rejectAllPendingProxyCallsForSession,\n rejectPendingProxyCallById,\n resolvePendingProxyCallById,\n type PendingProxyCall,\n} from \"./proxy-broker.js\"\nimport { readFileSync, writeFileSync } from \"node:fs\"\nimport { unlink } from \"node:fs/promises\"\nimport { homedir, tmpdir } from \"node:os\"\nimport { randomUUID } from \"node:crypto\"\nimport { dirname, join } from \"node:path\"\n\n/**\n * Default model used for opencode `/compact`. Haiku 4.5 is fast\n * (~150 tok/s), has a hard 8k output cap that bounds latency, and is a\n * strong structured summarizer. Override per-project via the\n * `compactionModel` provider setting in opencode.json / opencode.jsonc,\n * or per-run via the `CLAUDE_CODE_COMPACTION_MODEL` env var (env wins).\n */\nexport const DEFAULT_COMPACTION_MODEL = \"claude-haiku-4-5\"\n\n/**\n * Pick the model used to handle /compact. Precedence:\n * 1. `CLAUDE_CODE_COMPACTION_MODEL` env var (per-process override)\n * 2. `configured` argument (the `compactionModel` provider setting)\n * 3. `DEFAULT_COMPACTION_MODEL`\n *\n * Exported as a free function so it can be unit-tested without\n * instantiating the language model class.\n */\nexport function resolveCompactionModel(configured?: string): string {\n const env = process.env.CLAUDE_CODE_COMPACTION_MODEL?.trim()\n if (env) return env\n const trimmed = configured?.trim()\n if (trimmed) return trimmed\n return DEFAULT_COMPACTION_MODEL\n}\n\n/**\n * Resolve the session affinity token for a given LLM call. The affinity\n * token is part of the session key in session-manager so two different\n * opencode sessions sharing the same cwd+model still get separate Claude\n * CLI processes.\n *\n * Priority:\n * 1. `x-session-affinity` request header (primary — opencode sets it for\n * third-party providers in packages/opencode/src/session/llm.ts).\n * 2. `opencodeSessionID` inside `providerOptions` (injected by the\n * `chat.params` hook in index.ts). Covers cases where the header is\n * absent: provider switch mid-session, title synthesis paths, older\n * opencode versions. opencode wraps `output.options` under the\n * providerID before passing it to the language model, so we look up\n * both the configured provider key and the canonical `\"claude-code\"`.\n * 3. `\"default\"` — safe fallback when neither source is available.\n *\n * Exported as a free function so it can be unit-tested without\n * instantiating the language model class.\n */\nexport function resolveSessionAffinity(\n headers: Record<string, string | undefined> | undefined,\n providerOptions: Record<string, unknown> | undefined,\n providerKey: string,\n): string {\n if (headers) {\n for (const key of Object.keys(headers)) {\n if (key.toLowerCase() === \"x-session-affinity\") {\n const v = headers[key]\n if (typeof v === \"string\" && v.length > 0) return v\n }\n }\n }\n if (providerOptions) {\n const bag =\n (providerOptions as any)[providerKey] ??\n (providerOptions as any)[\"claude-code\"]\n const sid = bag?.opencodeSessionID\n if (typeof sid === \"string\" && sid.length > 0) return sid\n }\n return \"default\"\n}\n\n/**\n * Stream delta types we handle explicitly. `signature_delta` is listed as\n * known-and-silent: it carries encrypted thinking-block signatures that\n * are opaque to clients (the server uses them to reconstitute thinking\n * across turns), so there's nothing for us to do but ignore it.\n */\nconst KNOWN_DELTA_TYPES = new Set([\n \"thinking_delta\",\n \"text_delta\",\n \"input_json_delta\",\n \"signature_delta\",\n])\n\n/**\n * True if the prompt has any user-side content after the last assistant\n * message (text, tool_result, or any user role entry). False when the\n * prompt ends with an assistant message and there is nothing for Claude\n * to respond to — opencode sometimes iterates the agent loop one more\n * time after a turn naturally completed; without short-circuiting we'd\n * spawn Claude CLI on an empty turn and the model would reply with a\n * stub like \"Did you mean to send a message?\".\n */\nexport function hasNewUserContent(\n prompt: LanguageModelV3CallOptions[\"prompt\"],\n): boolean {\n for (let i = prompt.length - 1; i >= 0; i--) {\n const msg = prompt[i]\n if (msg.role === \"assistant\") return false\n // Tool-result turns from opencode's outer loop arrive in `tool`-role\n // messages (AI SDK V3 shape). Treat any tool-result part as new\n // content so the short-circuit doesn't drop turns where opencode is\n // delivering the result for a still-pending proxy MCP call — letting\n // that fire `stop` is what was forcing the user to press \"continue\".\n if (msg.role === \"tool\") {\n const content: any = msg.content\n if (Array.isArray(content)) {\n for (const part of content as any[]) {\n if (part?.type === \"tool-result\") return true\n }\n }\n continue\n }\n if (msg.role !== \"user\") continue\n const content: any = msg.content\n if (typeof content === \"string\") {\n if (content.trim()) return true\n continue\n }\n if (Array.isArray(content)) {\n for (const part of content as any[]) {\n if (part.type === \"text\" && part.text && part.text.trim()) return true\n if (part.type === \"tool-result\") return true\n // Image/file-only user turns count as new input — without this the\n // short-circuit drops them as if the turn were empty.\n if (part.type === \"image\" || part.type === \"file\") return true\n }\n }\n }\n return false\n}\n\nconst AUTO_CONTINUE_MAX_ATTEMPTS = 8\nconst AUTO_CONTINUE_MAX_ELAPSED_MS = 10 * 60 * 1000\nconst AUTO_CONTINUE_NO_PROGRESS_LIMIT = 2\n\nconst AUTO_CONTINUE_PROMPT =\n \"Continue the task from where you stopped. Do not summarize; keep working until the requested task is complete, you need clarification, or you hit a real blocker.\"\n\ninterface AutoContinueState {\n enabled: boolean | \"smart\" | undefined\n attempts: number\n startedAt: number\n noProgressCount: number\n lastSignature?: string\n aborted?: boolean\n}\n\ninterface AutoContinueSnapshot {\n text: string\n /**\n * Text of the most recent assistant text block only. Used for final-answer\n * detection so mid-task narration like \"Implementing now. Updated the\n * search index.\" in an earlier block doesn't trip the keyword regex.\n */\n lastVisibleText: string\n hadReasoning: boolean\n hadToolActivity: boolean\n hadProxyActivity: boolean\n isError?: boolean\n /**\n * Protocol-level stop signal from the Claude API (forwarded by Claude\n * CLI). When present and non-empty, we trust it as authoritative — the\n * model itself signaled why the turn ended (`end_turn`, `max_tokens`,\n * `stop_sequence`, `refusal`, `pause_turn`, `tool_use`, etc.) — and stop\n * without running the keyword regex. The heuristic only runs as a\n * fallback when `stop_reason` is missing (older CLI versions, abrupt\n * termination).\n */\n stopReason?: string | null\n now?: number\n}\n\ninterface AutoContinueDecision {\n continue: boolean\n reason: string\n}\n\nfunction normalizeVisibleText(text: string): string {\n return text.replace(/\\s+/g, \" \").trim()\n}\n\n/** Tool names that mean \"ask the human a question\" (CLI casing variants). */\nexport function isAskUserQuestionTool(name: string | undefined): boolean {\n if (!name) return false\n const n = name.toLowerCase()\n return n === \"askuserquestion\" || n === \"ask_user_question\"\n}\n\n/**\n * Deny message returned to the model when it invokes AskUserQuestion.\n *\n * AskUserQuestion is denied (see controlRequestBehaviorForTool) so the\n * headless CLI cannot self-answer against an empty TTY. The question is\n * already rendered to the operator by formatAskUserQuestion, so this text\n * tells the model to stop and wait — unconditionally. Earlier versions\n * offered an \"if this is non-interactive, proceed with a reasonable guess\"\n * escape hatch, but the model could not reliably tell interactive opencode\n * from a headless run and routinely took it, so questions appeared to be\n * skipped (issue #8). Stopping is the correct default for opencode; a\n * headless run simply ends the turn with the question as its final output.\n */\nconst ASK_USER_QUESTION_DENY_MESSAGE =\n \"Your question and its options have already been presented to the\" +\n \" operator verbatim. Stop now: end your turn without calling any more\" +\n \" tools and without answering the question yourself. Wait for the\" +\n \" operator's reply, which arrives as the next user message. Do not\" +\n \" guess, assume, or proceed on their behalf.\"\n\n/** Build the deny message for an auto-denied control request. */\nexport function denyMessageForTool(\n toolName: string | undefined,\n configuredDenyMessage?: string,\n): string {\n if (isAskUserQuestionTool(toolName)) return ASK_USER_QUESTION_DENY_MESSAGE\n return (\n configuredDenyMessage ??\n `Denied by opencode-claude-code policy for tool ${toolName}`\n )\n}\n\n/**\n * Render Claude Code's `AskUserQuestion` tool input as visible markdown.\n *\n * opencode has no native structured ask-question executor to proxy this\n * through (unlike bash/task), so the question + every option is rendered\n * as readable assistant text and the user answers in the next turn —\n * same approach as the `ExitPlanMode` handling. The previous behavior\n * collapsed the whole payload to a single faint `_Asking: <q>_` line,\n * dropping all options and any question past the first.\n */\nfunction formatAskUserQuestion(input: Record<string, unknown>): string {\n const anyInput = input as any\n const questions: any[] = Array.isArray(anyInput?.questions)\n ? anyInput.questions\n : []\n\n if (questions.length === 0) {\n const single = anyInput?.question ?? anyInput?.text\n const q =\n typeof single === \"string\" && single.trim() ? single.trim() : \"Question?\"\n return `\\n\\n**${q}**\\n\\n_Reply with your answer to continue._\\n\\n`\n }\n\n const out: string[] = [\"\\n\\n\"]\n const multiQ = questions.length > 1\n questions.forEach((q, i) => {\n const text =\n (typeof q?.question === \"string\" && q.question.trim()) ||\n (typeof q?.text === \"string\" && q.text.trim()) ||\n \"Question?\"\n const header =\n typeof q?.header === \"string\" && q.header.trim() ? q.header.trim() : \"\"\n out.push(`**${multiQ ? `${i + 1}. ` : \"\"}${text}**`)\n if (header) out.push(` _(${header})_`)\n out.push(\"\\n\\n\")\n\n const options: any[] = Array.isArray(q?.options) ? q.options : []\n options.forEach((opt, j) => {\n const label =\n (typeof opt?.label === \"string\" && opt.label.trim()) ||\n (typeof opt === \"string\" && opt.trim()) ||\n `Option ${j + 1}`\n const desc =\n typeof opt?.description === \"string\" && opt.description.trim()\n ? ` — ${opt.description.trim()}`\n : \"\"\n out.push(`${j + 1}. **${label}**${desc}\\n`)\n })\n\n out.push(\n q?.multiSelect === true\n ? \"\\n_Select one or more — reply with the numbers or labels._\\n\\n\"\n : \"\\n_Reply with your choice (the number or label)._\\n\\n\",\n )\n })\n return out.join(\"\")\n}\n\nfunction looksLikeQuestion(text: string): boolean {\n const normalized = normalizeVisibleText(text).toLowerCase()\n if (!normalized) return false\n // v0.4.10 tweak 5a: '?' anywhere in the last block, not just trailing.\n // Catches long answers that pose a question mid-text then list options\n // and end with a period. FP risk on inline code (`result?.value`) is\n // accepted — cost is one extra \"continue\" press, in the safe direction.\n if (normalized.includes(\"?\")) return true\n // v0.4.11 additions: ready when you are / standing by / i'll stand by /\n // let me know when. These are awaiting-input idioms with no '?'. The\n // \"standing by\" addition has historical significance — it's the exact\n // stub phrase Claude CLI emits on empty turns that commit 49345e3 was\n // designed to suppress at the message-builder layer. This adds a second\n // line of defense at the model-output layer for cases where the model\n // organically produces the same idiom.\n //\n // v0.4.12 additions: over to you / your turn / all yours / let me know\n // how / i'm here. Defensive coverage of soft-proceed idioms in the\n // model's vocabulary. \"i'm here\" has the highest FP risk (\"I'm here to\n // help with X\" is a conversational opener) but cost of FP is one extra\n // continue press — safe direction.\n return /\\b(please confirm|can you confirm|should i|would you like|do you want|which option|choose|pick one|need your|need you to|what would you like|let me know if|let me know whether|let me know what|let me know when|let me know how|if you'?d like|if you want to|tell me if|tell me which|tell me whether|say (?:go|yes|no)|push back|sign off|sounds? (?:good|right)|your call|your move|your turn|over to you|all yours|up to you|ready to (?:ship|go|proceed|merge)|ready (?:when|whenever|once|if) you|standing by|i'?ll stand ?by|i'?m here|happy to (?:ship|go|proceed|merge))\\b/.test(normalized)\n}\n\nfunction looksLikeBlocker(text: string): boolean {\n const normalized = normalizeVisibleText(text).toLowerCase()\n if (!normalized) return false\n // v0.4.10 tweak 3: 'needs your' / 'needs you to' / 'action required'\n // are intent-equivalent to 'requires your' but use the verb-with-s form.\n return /\\b(blocked|blocker|cannot proceed|can't proceed|unable to proceed|need clarification|need more information|permission denied|failed and needs|requires your|needs your|needs you to|action required|manual step|required from you)\\b/.test(normalized)\n}\n\nfunction looksLikeFinalAnswer(text: string): boolean {\n const normalized = normalizeVisibleText(text).toLowerCase()\n if (looksLikeQuestion(normalized) || looksLikeBlocker(normalized)) return false\n // v0.4.15: strong-completion phrases bypass the 30-char length floor.\n // These are unambiguous end-of-turn signals at any text length — even\n // a short standalone \"We're done.\" should stop.\n if (/\\b(we'?re done|we are done|all done|all set)\\b/.test(normalized)) {\n return true\n }\n // v0.4.10 tweak 4: floor lowered 40 → 30 chars. Catches short clean\n // completions like \"Task is now completely done. Pushed.\" (36 chars)\n // while keeping a buffer against ambiguous short narration.\n if (normalized.length < 30) return false\n // v0.4.15: keyword list extended with deploy/ship verbs the model\n // routinely uses at turn end (shipped, deployed, merged, tagged, live,\n // pinned). FP risk highest on \"live\" — \"live data\" mid-turn could match\n // — but cost of FP is one extra continue press, safe direction.\n return /\\b(done|completed|fixed|implemented|verified|published|released|sent|delivered|updated|shipped|deployed|merged|tagged|live|pinned)\\b/.test(normalized) ||\n // v0.4.15: also accept present-tense \"tests pass\" / \"checks pass\".\n // Real fire 03:31 ended in \"78/78 tests pass\" — past-tense-only regex\n // missed it.\n /\\b(checks?|tests?) (?:pass|passes|passed)\\b/.test(normalized) ||\n /\\b(summary|what changed|verification)\\b/.test(normalized)\n}\n\nfunction continuationSignature(snapshot: AutoContinueSnapshot): string {\n const text = normalizeVisibleText(snapshot.text).slice(-500)\n return JSON.stringify({\n text,\n reasoning: snapshot.hadReasoning,\n tools: snapshot.hadToolActivity,\n proxy: snapshot.hadProxyActivity,\n })\n}\n\nexport function shouldAutoContinueIncompleteTurn(\n state: AutoContinueState,\n snapshot: AutoContinueSnapshot,\n): AutoContinueDecision {\n if (state.enabled === false) return { continue: false, reason: \"disabled\" }\n if (snapshot.isError) return { continue: false, reason: \"error\" }\n if (state.aborted) return { continue: false, reason: \"aborted\" }\n // v0.4.17: trust ANY protocol-level stop_reason as authoritative. If\n // Claude CLI emitted a stop_reason value at all, the model has signaled\n // a stop — honor it without consulting the keyword heuristic. The\n // heuristic only runs as a fallback when stop_reason is missing (older\n // CLI versions / edge cases). Maps snake_case → kebab-case for reason\n // label consistency with other reasons.\n if (snapshot.stopReason) {\n return {\n continue: false,\n reason: snapshot.stopReason.replace(/_/g, \"-\"),\n }\n }\n if (state.attempts >= AUTO_CONTINUE_MAX_ATTEMPTS) {\n return { continue: false, reason: \"max-attempts\" }\n }\n const now = snapshot.now ?? Date.now()\n if (now - state.startedAt > AUTO_CONTINUE_MAX_ELAPSED_MS) {\n return { continue: false, reason: \"max-elapsed\" }\n }\n\n const text = normalizeVisibleText(snapshot.text)\n const lastText = normalizeVisibleText(snapshot.lastVisibleText)\n if (looksLikeQuestion(text)) return { continue: false, reason: \"question\" }\n if (looksLikeBlocker(text)) return { continue: false, reason: \"blocker\" }\n // Final-answer detection runs on the most recent text block only. Earlier\n // blocks may contain mid-task narration that would false-positive the\n // keyword regex; the model's actual \"I'm done\" sentence is in the last\n // block before result/end_turn.\n if (looksLikeFinalAnswer(lastText)) {\n return { continue: false, reason: \"final-answer\" }\n }\n\n const hadActivity =\n snapshot.hadReasoning || snapshot.hadToolActivity || snapshot.hadProxyActivity\n if (!hadActivity) return { continue: false, reason: \"no-activity\" }\n\n const signature = continuationSignature(snapshot)\n const noProgress = signature === state.lastSignature\n if (noProgress && state.noProgressCount + 1 >= AUTO_CONTINUE_NO_PROGRESS_LIMIT) {\n return { continue: false, reason: \"no-progress\" }\n }\n\n if (!text) {\n return { continue: true, reason: \"activity-without-visible-answer\" }\n }\n\n return { continue: true, reason: \"non-final-progress\" }\n}\n\nfunction makeAutoContinueMessage(): string {\n return JSON.stringify({\n type: \"user\",\n message: {\n role: \"user\",\n content: [{ type: \"text\", text: AUTO_CONTINUE_PROMPT }],\n },\n })\n}\n\nfunction readPromptFileIfPresent(path: string): string | undefined {\n try {\n const content = readFileSync(path, \"utf8\").trim()\n return content || undefined\n } catch {\n return undefined\n }\n}\n\nfunction nearestWorkspaceAgentsPrompt(cwd: string): string | undefined {\n let dir = cwd\n while (true) {\n const content = readPromptFileIfPresent(join(dir, \"AGENTS.md\"))\n if (content) return content\n const parent = dirname(dir)\n if (parent === dir) return undefined\n dir = parent\n }\n}\n\nconst AGENTS_MAINTENANCE_HINT = `## Keeping AGENTS.md up to date\n\nWhen you complete a task, phase, or to-do item that is listed in AGENTS.md, update the file\nimmediately after the work is done — mark it ✅, check it off, or remove it. Do this inside\nthe same turn so the next session does not repeat work that is already finished.`\n\nconst MULTI_STEP_TASK_HINT = `## Continuing through multi-step tasks\n\nopencode requires the user to press \"continue\" after each turn ends. When a\ntask has multiple steps, do them all in one turn — chain tool calls rather\nthan pausing for user confirmation between subtasks. End the turn only\nwhen the task is done, you need clarification on intent, or you hit a real\nblocker. The user can interrupt or abort at any time; turn endings should\nmark meaningful checkpoints, not every completed substep.`\n\n/**\n * Prepended to every appended system prompt so Claude knows which\n * context-management tools exist in the Claude CLI runtime versus a\n * direct API provider. DCP and similar plugins forward compress/distill/\n * prune instructions via system.transform; those reach us through\n * extractSystemMessages, but the tools themselves are not available in\n * the CLI environment. Without this note Claude wastes thinking cycles\n * searching for tools that don't exist.\n */\nconst CLAUDE_CLI_CONTEXT_NOTE = `## Runtime environment: Claude Code CLI\n\nYou are running via the Claude Code CLI (not a direct API call). This affects context management:\n\n- The \\`compress\\` tool is NOT available. Do not attempt to call it.\n- The \\`distill\\`, \\`prune\\`, and \\`extract\\` tools are NOT available.\n- Context window management is handled automatically by Claude CLI's own session history.\n- Ignore any system instructions that tell you to call \\`compress\\` — they are intended for direct API providers, not this environment.\n- DCP context injections (AGENTS.md, dynamic state) arrive via the system prompt and are already applied.`\n\n/**\n * Extract text content from all `system`-role messages in the prompt.\n * Standard API providers forward these as the `system` parameter; for\n * Claude CLI, the only equivalent path is --append-system-prompt-file.\n * Plugins like opencode-dcp inject AGENTS.md and other context via\n * system-role messages and would otherwise be silently dropped.\n */\nfunction extractSystemMessages(\n prompt: LanguageModelV3CallOptions[\"prompt\"],\n): string[] {\n const out: string[] = []\n for (const msg of prompt) {\n if (msg.role !== \"system\") continue\n if (typeof msg.content === \"string\") {\n if (msg.content.trim()) out.push(msg.content.trim())\n } else if (Array.isArray(msg.content)) {\n for (const part of msg.content as any[]) {\n if (\n part?.type === \"text\" &&\n typeof part.text === \"string\" &&\n part.text.trim()\n ) {\n out.push(part.text.trim())\n }\n }\n }\n }\n return out\n}\n\nfunction buildAppendedSystemPrompt(\n cwd: string,\n includeMultiStepHint = true,\n extraSystemContent: string[] = [],\n): string | undefined {\n const parts: string[] = []\n parts.push(CLAUDE_CLI_CONTEXT_NOTE)\n for (const s of extraSystemContent) {\n if (s.trim()) parts.push(s.trim())\n }\n const configRoot =\n process.env.XDG_CONFIG_HOME ?? join(homedir(), \".config\")\n const globalAgents = readPromptFileIfPresent(join(configRoot, \"opencode\", \"AGENTS.md\"))\n const workspaceAgents = nearestWorkspaceAgentsPrompt(cwd)\n\n if (globalAgents) parts.push(globalAgents)\n if (workspaceAgents && workspaceAgents !== globalAgents) parts.push(workspaceAgents)\n if (globalAgents || workspaceAgents) parts.push(AGENTS_MAINTENANCE_HINT)\n if (includeMultiStepHint) parts.push(MULTI_STEP_TASK_HINT)\n\n const content = parts.join(\"\\n\\n\")\n if (!content) return undefined\n\n const path = join(tmpdir(), `opencode-cc-sys-${randomUUID()}.md`)\n try {\n writeFileSync(path, content, \"utf8\")\n return path\n } catch (err) {\n log.warn(\"failed to write system prompt file\", { error: String(err) })\n return undefined\n }\n}\n\nexport class ClaudeCodeLanguageModel implements LanguageModelV3 {\n readonly specificationVersion = \"v3\"\n readonly modelId: string\n private readonly config: ClaudeCodeConfig\n\n constructor(modelId: string, config: ClaudeCodeConfig) {\n this.modelId = modelId\n this.config = config\n }\n\n readonly supportedUrls: Record<string, RegExp[]> = {}\n\n get provider(): string {\n return this.config.provider\n }\n\n private toUsage(rawUsage?: ClaudeStreamMessage[\"usage\"]): LanguageModelV3Usage {\n // Prefer the last iteration's counters over cumulative totals.\n // CLI usage is the sum across all internal tool-use iterations;\n // using it directly inflates context size and triggers premature compaction.\n const iter = rawUsage?.iterations\n const effective = iter?.length ? iter[iter.length - 1] : rawUsage\n // Claude CLI reports input_tokens as non-cached input only.\n // OpenCode expects total = noCache + cacheRead + cacheWrite.\n const noCache = effective?.input_tokens ?? 0\n const cacheRead = effective?.cache_read_input_tokens ?? 0\n const cacheWrite = effective?.cache_creation_input_tokens ?? 0\n return {\n inputTokens: {\n total: noCache + cacheRead + cacheWrite,\n noCache,\n cacheRead: cacheRead || undefined,\n cacheWrite: cacheWrite || undefined,\n },\n outputTokens: {\n total: effective?.output_tokens,\n text: effective?.output_tokens,\n reasoning: undefined,\n },\n raw: rawUsage as any,\n }\n }\n\n private toFinishReason(\n reason: \"stop\" | \"tool-calls\" = \"stop\",\n ): LanguageModelV3FinishReason {\n return {\n unified: reason,\n raw: reason,\n }\n }\n\n private requestScope(options: { tools?: unknown }): \"tools\" | \"no-tools\" {\n const tools = options?.tools\n if (Array.isArray(tools)) return \"tools\"\n if (tools && typeof tools === \"object\") {\n return Object.keys(tools as Record<string, unknown>).length > 0\n ? \"tools\"\n : \"no-tools\"\n }\n return \"no-tools\"\n }\n\n /**\n * Build the combined `--mcp-config` list and return both the list and the\n * hash of the bridged opencode MCP block (or null when bridging is off /\n * yields nothing). The hash is used to detect mid-session config changes\n * and respawn the underlying claude process.\n *\n * `runtimeStatus` is a snapshot of opencode's `client.mcp.status()`. When\n * provided it overlays opencode's UI-toggled state on top of disk config\n * so `/mcps` toggles propagate without a config file write.\n */\n private effectiveMcpConfig(\n cwd: string,\n proxyConfigPath?: string,\n runtimeStatus?: RuntimeMcpStatus,\n excludeServers?: ReadonlySet<string>,\n ): {\n paths: string[]\n bridgedHash: string | null\n allEnabledServerNames: string[]\n } {\n const paths = Array.isArray(this.config.mcpConfig)\n ? this.config.mcpConfig.slice()\n : this.config.mcpConfig\n ? [this.config.mcpConfig]\n : []\n let bridgedHash: string | null = null\n let allEnabledServerNames: string[] = []\n if (this.config.bridgeOpencodeMcp !== false) {\n const bridged = bridgeOpencodeMcp(cwd, runtimeStatus, excludeServers)\n if (bridged) {\n if (bridged.path) paths.push(bridged.path)\n bridgedHash = bridged.hash\n allEnabledServerNames = bridged.allEnabledServerNames\n }\n }\n if (proxyConfigPath) paths.push(proxyConfigPath)\n return { paths, bridgedHash, allEnabledServerNames }\n }\n\n /** Resolve ProxyToolDef[] for the configured proxyTools names. */\n private resolvedProxyTools(): ProxyToolDef[] | null {\n const names = this.config.proxyTools\n if (!names || names.length === 0) return null\n const defsByName = new Map(\n DEFAULT_PROXY_TOOLS.map((t) => [t.name.toLowerCase(), t]),\n )\n const picked: ProxyToolDef[] = []\n for (const n of names) {\n const def = defsByName.get(String(n).toLowerCase())\n if (def) picked.push(def)\n }\n return picked.length > 0 ? picked : null\n }\n\n /**\n * Resolve ProxyToolDef[] for opencode's MCP-bridged tools so they go\n * through the in-process proxy instead of being bridged into Claude CLI's\n * `--mcp-config`. Direct bridging causes double execution because both\n * Claude CLI's own MCP child and opencode hold their own connection to\n * the same server; routing through the proxy keeps a single execution\n * site (opencode). Returns null when the feature is disabled, the SDK\n * client is unavailable, or no MCP servers are configured.\n */\n private async resolvedProxyMcpTools(\n allEnabledServerNames: string[],\n ): Promise<ProxyToolDef[] | null> {\n if (this.config.proxyOpencodeMcpTools === false) return null\n if (this.config.bridgeOpencodeMcp === false) return null\n if (allEnabledServerNames.length === 0) return null\n\n const items = await fetchOpencodeToolList(\n this.config.provider,\n this.modelId,\n this.config.cwd,\n )\n if (!items || items.length === 0) return null\n\n // opencode names MCP tools `<server>_<originalToolName>`. Match the\n // longest server name prefix first so e.g. `slack_intl_*` resolves to\n // server `slack_intl` not `slack`.\n const serversByLengthDesc = [...allEnabledServerNames].sort(\n (a, b) => b.length - a.length,\n )\n const out: ProxyToolDef[] = []\n const seen = new Set<string>()\n for (const item of items) {\n const matchedServer = serversByLengthDesc.find(\n (name) => item.id === name || item.id.startsWith(`${name}_`),\n )\n if (!matchedServer) continue\n if (seen.has(item.id)) continue\n seen.add(item.id)\n out.push({\n name: item.id,\n description: item.description ?? \"\",\n inputSchema:\n item.parameters && typeof item.parameters === \"object\"\n ? item.parameters\n : { type: \"object\", properties: {} },\n })\n }\n return out.length > 0 ? out : null\n }\n\n /**\n * Create a proxy MCP server for a single active Claude process/session.\n * The process lifecycle owns the server lifecycle via session-manager.\n */\n private async ensureProxyServer(\n tools: ProxyToolDef[],\n sessionKeyForCalls: string,\n ): Promise<ProxyMcpServer> {\n const srv = await createProxyMcpServer(tools)\n srv.calls.on(\"call\", (call: ProxyToolCall) => {\n queuePendingProxyCall(sessionKeyForCalls, call)\n })\n return srv\n }\n\n private extractPendingProxyResult(\n prompt: LanguageModelV3CallOptions[\"prompt\"],\n toolCallId: string,\n ): ProxyToolResult | null {\n for (let i = prompt.length - 1; i >= 0; i--) {\n const msg = prompt[i]\n if (msg.role !== \"tool\" || !Array.isArray(msg.content)) continue\n\n for (const part of msg.content) {\n if (part.type !== \"tool-result\" || part.toolCallId !== toolCallId) continue\n\n const output = part.output as any\n if (!output || typeof output !== \"object\") {\n return {\n kind: \"text\",\n text: String(output ?? \"\"),\n }\n }\n\n if (output.type === \"text\") {\n return {\n kind: \"text\",\n text: String(output.value ?? \"\"),\n }\n }\n\n if (output.type === \"json\") {\n return {\n kind: \"text\",\n text: JSON.stringify(output.value),\n }\n }\n\n if (output.type === \"content\" && Array.isArray(output.value)) {\n const text = output.value\n .filter((v: any) => v?.type === \"text\" && typeof v.text === \"string\")\n .map((v: any) => v.text)\n .join(\"\\n\")\n return {\n kind: \"text\",\n text,\n }\n }\n\n return {\n kind: \"text\",\n text: JSON.stringify(output),\n }\n }\n }\n\n return null\n }\n\n /**\n * Resolve the session affinity token for this LLM call. Delegates to the\n * exported `resolveSessionAffinity` helper so the logic is unit-testable.\n * Priority:\n * 1. `x-session-affinity` request header (primary).\n * 2. `opencodeSessionID` in providerOptions (chat.params hook fallback —\n * covers provider switches mid-session and title synthesis paths\n * where the header is absent).\n * 3. `\"default\"`.\n */\n private sessionAffinity(\n options: LanguageModelV3CallOptions,\n ): string {\n const headers = (options as any)?.headers as\n | Record<string, string | undefined>\n | undefined\n return resolveSessionAffinity(\n headers,\n options.providerOptions as Record<string, unknown> | undefined,\n this.config.provider,\n )\n }\n\n private controlRequestBehaviorForTool(toolName: string): ControlRequestBehavior {\n const configured = this.config.controlRequestToolBehaviors\n if (configured && toolName) {\n const direct = configured[toolName] ?? configured[toolName.toLowerCase()]\n if (direct === \"allow\" || direct === \"deny\") return direct\n\n const lower = toolName.toLowerCase()\n for (const [key, behavior] of Object.entries(configured)) {\n if (key.toLowerCase() === lower && (behavior === \"allow\" || behavior === \"deny\")) {\n return behavior\n }\n }\n }\n\n // AskUserQuestion must never be auto-allowed. Allowing it lets the\n // Claude CLI resolve its own question internally — in headless mode\n // there is no TTY, so the CLI fabricates/empties the answer and the\n // model proceeds on a guess. Deny so the CLI cannot self-answer; the\n // tool_use is still streamed and rendered to the opencode user by\n // formatAskUserQuestion, and the turn stops for a real reply. An\n // explicit controlRequestToolBehaviors entry above can still override.\n if (isAskUserQuestionTool(toolName)) return \"deny\"\n\n return this.config.controlRequestBehavior ?? \"allow\"\n }\n\n private writeControlResponse(\n proc: import(\"child_process\").ChildProcess,\n requestId: string,\n response?: Record<string, unknown>,\n ): void {\n const payload = {\n type: \"control_response\",\n response: {\n subtype: \"success\",\n request_id: requestId,\n response,\n },\n }\n\n try {\n proc.stdin?.write(JSON.stringify(payload) + \"\\n\")\n } catch (error) {\n log.warn(\"failed to write control response\", {\n requestId,\n error: error instanceof Error ? error.message : String(error),\n })\n }\n }\n\n /**\n * Handle Claude stream-json control requests (`can_use_tool`, etc.) and\n * respond via stdin with a matching `control_response`.\n */\n private handleControlRequest(\n msg: ClaudeStreamMessage,\n proc: import(\"child_process\").ChildProcess,\n ): boolean {\n if (msg.type !== \"control_request\") return false\n const requestId = msg.request_id\n const request = msg.request\n if (!requestId || !request?.subtype) return false\n\n if (request.subtype === \"can_use_tool\") {\n const toolName = request.tool_name ?? \"unknown\"\n const behavior = this.controlRequestBehaviorForTool(toolName)\n\n if (behavior === \"allow\") {\n this.writeControlResponse(proc, requestId, {\n behavior: \"allow\",\n updatedInput: request.input ?? {},\n toolUseID: request.tool_use_id,\n })\n log.info(\"control request auto-allowed\", {\n requestId,\n toolName,\n })\n } else {\n const denyMessage = denyMessageForTool(\n toolName,\n this.config.controlRequestDenyMessage,\n )\n this.writeControlResponse(proc, requestId, {\n behavior: \"deny\",\n message: denyMessage,\n toolUseID: request.tool_use_id,\n })\n log.info(\"control request auto-denied\", {\n requestId,\n toolName,\n })\n }\n\n return true\n }\n\n // For control request subtypes we don't actively handle yet, acknowledge\n // with an empty success so the CLI stream does not stall.\n this.writeControlResponse(proc, requestId, {})\n log.debug(\"control request acknowledged\", {\n requestId,\n subtype: request.subtype,\n })\n return true\n }\n\n private getReasoningEffort(\n providerOptions?: LanguageModelV3CallOptions[\"providerOptions\"],\n ): ReasoningEffort | undefined {\n if (!providerOptions) return undefined\n const ownKey = this.config.provider\n const bag =\n (providerOptions as any)[ownKey] ??\n (providerOptions as any)[\"claude-code\"]\n const effort = bag?.reasoningEffort\n const valid: ReasoningEffort[] = [\n \"minimal\",\n \"low\",\n \"medium\",\n \"high\",\n \"xhigh\",\n \"max\",\n ]\n return valid.includes(effort) ? effort : undefined\n }\n\n private getOpencodeAgent(\n providerOptions?: LanguageModelV3CallOptions[\"providerOptions\"],\n ): string | undefined {\n if (!providerOptions) return undefined\n const ownKey = this.config.provider\n const bag =\n (providerOptions as any)[ownKey] ??\n (providerOptions as any)[\"claude-code\"]\n const agent = bag?.opencodeAgent\n return typeof agent === \"string\" ? agent : undefined\n }\n\n private isCompactionCall(\n options: LanguageModelV3CallOptions,\n ): boolean {\n return this.getOpencodeAgent(options.providerOptions) === \"compaction\"\n }\n\n /**\n * Pick the model used to handle /compact. Precedence:\n * 1. `CLAUDE_CODE_COMPACTION_MODEL` env var (per-process override)\n * 2. `compactionModel` provider setting (opencode.json / .jsonc)\n * 3. Built-in default (claude-haiku-4-5)\n */\n private resolveCompactionModel(): string {\n return resolveCompactionModel(this.config.compactionModel)\n }\n\n private thinkingCliOptions(): {\n thinking?: \"enabled\"\n thinkingDisplay?: \"summarized\"\n } {\n if (isClaudeThinkingDisabled()) return {}\n\n return {\n thinking: \"enabled\",\n thinkingDisplay:\n process.env.CLAUDE_CODE_SHOW_THINKING_SUMMARIES === undefined\n ? \"summarized\"\n : undefined,\n }\n }\n\n private latestUserText(\n prompt: LanguageModelV3CallOptions[\"prompt\"],\n ): string {\n for (let i = prompt.length - 1; i >= 0; i--) {\n const msg = prompt[i]\n if (msg.role !== \"user\") continue\n\n if (typeof msg.content === \"string\") {\n return String(msg.content).trim()\n }\n\n if (Array.isArray(msg.content)) {\n const text = (msg.content as any[])\n .filter((part) => part.type === \"text\" && typeof part.text === \"string\")\n .map((part: any) => String(part.text).trim())\n .filter(Boolean)\n .join(\" \")\n if (text) return text\n }\n }\n\n return \"\"\n }\n\n private synthesizeTitle(\n prompt: LanguageModelV3CallOptions[\"prompt\"],\n ): string {\n const source = this.latestUserText(prompt)\n .replace(/\\s+/g, \" \")\n .replace(/[^\\p{L}\\p{N}\\s-]/gu, \" \")\n .trim()\n\n if (!source) return \"New Session\"\n\n const stop = new Set([\n \"a\",\n \"an\",\n \"the\",\n \"and\",\n \"or\",\n \"but\",\n \"to\",\n \"for\",\n \"of\",\n \"in\",\n \"on\",\n \"at\",\n \"with\",\n \"can\",\n \"could\",\n \"would\",\n \"should\",\n \"please\",\n \"hi\",\n \"hello\",\n \"hey\",\n \"there\",\n \"you\",\n \"your\",\n \"this\",\n \"that\",\n \"is\",\n \"are\",\n \"was\",\n \"were\",\n \"be\",\n \"do\",\n \"does\",\n \"did\",\n \"summarize\",\n \"summary\",\n \"project\",\n ])\n\n const words = source\n .split(\" \")\n .map((word) => word.trim())\n .filter(Boolean)\n .filter((word) => !stop.has(word.toLowerCase()))\n\n const picked = (words.length > 0 ? words : source.split(\" \").filter(Boolean))\n .slice(0, 6)\n .map((word) => word.charAt(0).toUpperCase() + word.slice(1))\n .join(\" \")\n\n return picked || \"New Session\"\n }\n\n private async doGenerateViaStream(\n options: LanguageModelV3CallOptions,\n ): Promise<Awaited<ReturnType<LanguageModelV3[\"doGenerate\"]>>> {\n const result = await this.doStream(options)\n const reader = result.stream.getReader()\n\n let text = \"\"\n let reasoning = \"\"\n const toolCalls: LanguageModelV3Content[] = []\n let finishReason = this.toFinishReason(\"stop\")\n let usage: LanguageModelV3Usage = this.toUsage()\n let providerMetadata: any\n\n while (true) {\n const { value, done } = await reader.read()\n if (done) break\n\n switch ((value as any).type) {\n case \"text-delta\":\n text += (value as any).delta ?? \"\"\n break\n case \"reasoning-delta\":\n reasoning += (value as any).delta ?? \"\"\n break\n case \"tool-call\":\n toolCalls.push({\n type: \"tool-call\",\n toolCallId: (value as any).toolCallId,\n toolName: (value as any).toolName,\n input: (value as any).input,\n providerExecuted: (value as any).providerExecuted,\n } as any)\n break\n case \"finish\":\n finishReason = (value as any).finishReason ?? finishReason\n usage = (value as any).usage ?? usage\n providerMetadata = (value as any).providerMetadata ?? providerMetadata\n break\n }\n }\n\n const content: LanguageModelV3Content[] = []\n if (reasoning) {\n content.push({ type: \"reasoning\", text: reasoning } as any)\n }\n if (text) {\n content.push({ type: \"text\", text, providerMetadata } as any)\n }\n content.push(...toolCalls)\n\n return {\n content,\n finishReason,\n usage,\n request: result.request,\n response: {\n id: generateId(),\n timestamp: new Date(),\n modelId: this.modelId,\n },\n providerMetadata,\n warnings: [],\n }\n }\n\n async doGenerate(\n options: LanguageModelV3CallOptions,\n ): Promise<Awaited<ReturnType<LanguageModelV3[\"doGenerate\"]>>> {\n const warnings: SharedV3Warning[] = []\n const cwd = resolveSpawnCwd(this.config.cwd)\n const scope = this.requestScope(options as any)\n const affinity = this.sessionAffinity(options)\n const sk = sessionKey(cwd, `${this.modelId}::${scope}::${affinity}`)\n\n // When selective proxying is enabled, doGenerate must not bypass the\n // proxy path. Reuse doStream and aggregate its events so proxied tools\n // still route through opencode permissions/execution. Same for\n // opencode MCP proxying — doStream is the only path that wires up the\n // proxy server with the dynamically-discovered MCP tool defs.\n const compactionMode = this.isCompactionCall(options)\n\n if (\n scope === \"tools\" &&\n (this.resolvedProxyTools() ||\n (this.config.proxyOpencodeMcpTools !== false &&\n this.config.bridgeOpencodeMcp !== false))\n ) {\n return this.doGenerateViaStream(options)\n }\n\n // Route compaction through doStream so it gets the lean spawn path,\n // model override, and rich transcript handling. Aggregating a stream\n // for doGenerate matches what doGenerateViaStream already does for\n // proxy tools.\n if (compactionMode) {\n return this.doGenerateViaStream(options)\n }\n\n if (scope === \"no-tools\") {\n log.info(\"doGenerate no-tools title stub\", {\n compactionMode,\n opencodeAgent: this.getOpencodeAgent(options.providerOptions),\n providerOptionsKeys: options.providerOptions\n ? Object.keys(options.providerOptions)\n : [],\n })\n const text = this.synthesizeTitle(options.prompt)\n return {\n content: [{ type: \"text\", text }] as any,\n finishReason: this.toFinishReason(\"stop\"),\n usage: this.toUsage({ input_tokens: 0, output_tokens: 0 }),\n request: { body: { text: \"\" } },\n response: {\n id: generateId(),\n timestamp: new Date(),\n modelId: this.modelId,\n },\n providerMetadata: {\n \"claude-code\": {\n synthetic: true,\n path: \"no-tools\",\n },\n },\n warnings,\n }\n }\n\n // Short-circuit when opencode iterates the agent loop one more time\n // after a turn already finished. The prompt ends with an assistant\n // message and has no fresh user input — spawning Claude here would\n // just produce a stub like \"No input received. Standing by\".\n if (!hasNewUserContent(options.prompt)) {\n log.info(\"doGenerate short-circuit: no new user content\")\n return {\n content: [],\n finishReason: this.toFinishReason(\"stop\"),\n usage: this.toUsage({ input_tokens: 0, output_tokens: 0 }),\n request: { body: { text: \"\" } },\n response: {\n id: generateId(),\n timestamp: new Date(),\n modelId: this.modelId,\n },\n providerMetadata: {\n \"claude-code\": { synthetic: true, path: \"no-new-user-content\" },\n },\n warnings,\n }\n }\n\n const hasPriorConversation =\n options.prompt.filter((m) => m.role === \"user\" || m.role === \"assistant\")\n .length > 1\n\n // New session — clear any stale state from a previous session\n if (!hasPriorConversation) {\n deleteClaudeSessionId(sk)\n deleteActiveProcess(sk)\n }\n\n const hasExistingSession = !!getClaudeSessionId(sk)\n const includeHistoryContext = !hasExistingSession && hasPriorConversation\n\n const reasoningEffort = this.getReasoningEffort(options.providerOptions)\n const userMsg = getClaudeUserMessage(\n options.prompt,\n includeHistoryContext,\n reasoningEffort,\n )\n\n // doGenerate always spawns a fresh process, never reuse session ID.\n // Pre-fetch opencode's MCP runtime status so the bridge overlays\n // UI-toggled state on top of disk config.\n const [runtimeStatus, cliVersion] = await Promise.all([\n getRuntimeMcpStatus(),\n detectCliVersion(this.config.cliPath),\n ])\n const systemPromptFile = buildAppendedSystemPrompt(\n cwd,\n this.config.multiStepContinuation !== false,\n extractSystemMessages(options.prompt),\n )\n const cliArgs = buildCliArgs({\n sessionKey: sk,\n skipPermissions: this.config.skipPermissions !== false,\n includeSessionId: false,\n model: this.modelId,\n permissionMode: this.config.permissionMode,\n mcpConfig: this.effectiveMcpConfig(cwd, undefined, runtimeStatus).paths,\n strictMcpConfig: this.config.strictMcpConfig,\n disallowedTools:\n this.config.webSearch === \"disabled\" ? [\"WebSearch\"] : undefined,\n appendSystemPromptFile: systemPromptFile,\n ...this.thinkingCliOptions(),\n cliVersion,\n })\n\n log.info(\"doGenerate starting\", {\n cwd,\n model: this.modelId,\n textLength: userMsg.length,\n includeHistoryContext,\n })\n\n const { spawn } = await import(\"node:child_process\")\n const { createInterface } = await import(\"node:readline\")\n\n const proc = spawn(this.config.cliPath, cliArgs, {\n cwd,\n stdio: [\"pipe\", \"pipe\", \"pipe\"],\n env: claudeSpawnEnv(),\n shell: process.platform === \"win32\",\n })\n\n if (systemPromptFile) {\n proc.on(\"exit\", () => {\n void unlink(systemPromptFile).catch(() => {})\n })\n }\n\n const rl = createInterface({ input: proc.stdout! })\n\n let responseText = \"\"\n let thinkingText = \"\"\n let resultMeta: {\n sessionId?: string\n costUsd?: number\n durationMs?: number\n usage?: ClaudeStreamMessage[\"usage\"]\n } = {}\n const toolCalls: Array<{ id: string; name: string; args: unknown }> = []\n // Streaming tool_use entries keyed by content-block index. We accumulate\n // partial_json chunks here instead of trying to JSON.parse each chunk\n // independently, and flush to `toolCalls` at content_block_stop. The\n // previous code indexed `toolCalls` by `msg.index` directly, which is\n // wrong whenever non-tool blocks (text, thinking) precede a tool_use.\n const toolCallStreams = new Map<\n number,\n { id: string; name: string; inputJson: string }\n >()\n\n // Set true once we observe a `stream_event` envelope. When on, the\n // top-level `assistant` message is a duplicate of content already\n // accumulated via the inner content_block_* events — skip it.\n let gotPartialEvents = false\n\n const result = await new Promise<\n typeof resultMeta & {\n text: string\n thinking: string\n toolCalls: typeof toolCalls\n }\n >((resolve, reject) => {\n const cleanup = () => {\n try {\n if (!proc.killed && proc.exitCode === null) proc.kill()\n } catch {}\n }\n\n rl.on(\"line\", (line) => {\n if (!line.trim()) return\n try {\n const outer: ClaudeStreamMessage = JSON.parse(line)\n\n // Unwrap stream_event envelope (--include-partial-messages).\n // Inner event uses the same content_block_* / message_* shape.\n const msg: ClaudeStreamMessage =\n outer.type === \"stream_event\" && outer.event\n ? { ...outer.event, session_id: outer.session_id }\n : outer\n\n if (outer.type === \"stream_event\") {\n gotPartialEvents = true\n }\n\n if (this.handleControlRequest(msg, proc)) {\n return\n }\n\n if (msg.type === \"system\" && msg.subtype === \"init\") {\n if (msg.session_id) {\n setClaudeSessionId(sk, msg.session_id)\n }\n }\n\n if (\n msg.type === \"assistant\" &&\n msg.message?.content &&\n !gotPartialEvents\n ) {\n for (const block of msg.message.content) {\n if (block.type === \"text\" && block.text) {\n responseText += block.text\n }\n if (block.type === \"thinking\" && block.thinking) {\n thinkingText += block.thinking\n }\n if (block.type === \"tool_use\" && block.id && block.name) {\n if (isAskUserQuestionTool(block.name)) {\n // Render the full question + options as visible text so\n // the user can actually see and answer it.\n const parsedInput = (block.input ?? {}) as Record<\n string,\n unknown\n >\n responseText += formatAskUserQuestion(parsedInput)\n continue\n }\n\n if (block.name === \"ExitPlanMode\") {\n const parsedInput = (block.input ?? {}) as Record<\n string,\n unknown\n >\n const plan = (parsedInput?.plan as string) || \"\"\n responseText += `\\n\\n${plan}\\n\\n---\\n**Do you want to proceed with this plan?** (yes/no)\\n`\n continue\n }\n\n toolCalls.push({\n id: block.id,\n name: block.name,\n args: block.input ?? {},\n })\n }\n }\n }\n\n if (\n msg.type === \"content_block_start\" &&\n msg.content_block &&\n msg.index !== undefined\n ) {\n if (\n msg.content_block.type === \"tool_use\" &&\n msg.content_block.id &&\n msg.content_block.name\n ) {\n toolCallStreams.set(msg.index, {\n id: msg.content_block.id,\n name: msg.content_block.name,\n inputJson: \"\",\n })\n }\n }\n\n if (\n msg.type === \"content_block_delta\" &&\n msg.delta &&\n msg.index !== undefined\n ) {\n if (msg.delta.type === \"text_delta\" && msg.delta.text) {\n responseText += msg.delta.text\n }\n if (msg.delta.type === \"thinking_delta\" && msg.delta.thinking) {\n thinkingText += msg.delta.thinking\n }\n if (\n msg.delta.type === \"input_json_delta\" &&\n msg.delta.partial_json\n ) {\n const tc = toolCallStreams.get(msg.index)\n if (tc) tc.inputJson += msg.delta.partial_json\n }\n }\n\n if (msg.type === \"content_block_stop\" && msg.index !== undefined) {\n const tc = toolCallStreams.get(msg.index)\n if (tc) {\n let args: unknown = {}\n try {\n args = tc.inputJson ? JSON.parse(tc.inputJson) : {}\n } catch (err) {\n log.warn(\"tool input JSON parse failed\", {\n name: tc.name,\n error: String(err),\n })\n }\n toolCalls.push({ id: tc.id, name: tc.name, args })\n toolCallStreams.delete(msg.index)\n }\n }\n\n if (msg.type === \"result\") {\n if (msg.session_id) {\n setClaudeSessionId(sk, msg.session_id)\n }\n\n // Some CLI failures only surface user-readable text on the final\n // `result` message (without prior assistant text blocks). Preserve\n // that so callers don't receive an empty response.\n if (\n !responseText &&\n msg.is_error &&\n typeof msg.result === \"string\" &&\n msg.result.trim().length > 0\n ) {\n responseText = msg.result\n }\n\n resultMeta = {\n sessionId: msg.session_id,\n costUsd: msg.total_cost_usd,\n durationMs: msg.duration_ms,\n usage: msg.usage,\n }\n cleanup()\n resolve({\n ...resultMeta,\n text: responseText,\n thinking: thinkingText,\n toolCalls,\n })\n }\n } catch {\n // Ignore non-JSON lines\n }\n })\n\n rl.on(\"close\", () => {\n cleanup()\n resolve({\n ...resultMeta,\n text: responseText,\n thinking: thinkingText,\n toolCalls,\n })\n })\n\n proc.on(\"error\", (err) => {\n log.error(\"process error\", { error: err.message })\n cleanup()\n reject(err)\n })\n\n proc.stderr?.on(\"data\", (data: Buffer) => {\n log.debug(\"stderr\", { data: data.toString().slice(0, 200) })\n })\n\n proc.stdin?.write(userMsg + \"\\n\")\n })\n\n const content: LanguageModelV3Content[] = []\n\n if (result.thinking) {\n content.push({\n type: \"reasoning\",\n text: result.thinking,\n } as any)\n }\n\n if (result.text) {\n content.push({\n type: \"text\",\n text: result.text,\n providerMetadata: {\n \"claude-code\": {\n sessionId: result.sessionId ?? null,\n costUsd: result.costUsd ?? null,\n durationMs: result.durationMs ?? null,\n },\n ...(typeof result.usage?.cache_creation_input_tokens === \"number\"\n ? {\n anthropic: {\n cacheCreationInputTokens:\n result.usage.cache_creation_input_tokens,\n },\n }\n : {}),\n },\n })\n }\n\n for (const tc of result.toolCalls) {\n const {\n name: mappedName,\n input: mappedInput,\n executed,\n skip,\n } = mapTool(tc.name, tc.args, {\n webSearch: this.config.webSearch,\n sessionId: getClaudeSessionId(sk),\n toolUseId: tc.id,\n })\n if (skip) continue\n content.push({\n type: \"tool-call\",\n toolCallId: tc.id,\n toolName: mappedName,\n input: JSON.stringify(mappedInput),\n providerExecuted: executed,\n } as any)\n }\n\n const usage = this.toUsage(result.usage)\n\n return {\n content,\n // Claude CLI's `result` message signals a fully-completed turn —\n // tools have already been executed internally and final assistant\n // text has been produced. Always report \"stop\" so opencode doesn't\n // loop expecting to run tools itself.\n finishReason: this.toFinishReason(\"stop\"),\n usage,\n request: { body: { text: userMsg } },\n response: {\n id: result.sessionId ?? generateId(),\n timestamp: new Date(),\n modelId: this.modelId,\n },\n providerMetadata: {\n \"claude-code\": {\n sessionId: result.sessionId ?? null,\n costUsd: result.costUsd ?? null,\n durationMs: result.durationMs ?? null,\n },\n ...(typeof result.usage?.cache_creation_input_tokens === \"number\"\n ? {\n anthropic: {\n cacheCreationInputTokens:\n result.usage.cache_creation_input_tokens,\n },\n }\n : {}),\n },\n warnings,\n }\n }\n\n async doStream(\n options: LanguageModelV3CallOptions,\n ): Promise<Awaited<ReturnType<LanguageModelV3[\"doStream\"]>>> {\n const warnings: SharedV3Warning[] = []\n const cwd = resolveSpawnCwd(this.config.cwd)\n const cliPath = this.config.cliPath\n const skipPermissions = this.config.skipPermissions !== false\n const scope = this.requestScope(options as any)\n const affinity = this.sessionAffinity(options)\n const compactionMode = this.isCompactionCall(options)\n // Use a separate session key for compaction so its short-lived spawn\n // never collides with the main conversation's claude process.\n const effectiveModelId = compactionMode\n ? this.resolveCompactionModel()\n : this.modelId\n const sk = compactionMode\n ? sessionKey(cwd, `${effectiveModelId}::compaction::${affinity}`)\n : sessionKey(cwd, `${this.modelId}::${scope}::${affinity}`)\n const toUsage = this.toUsage.bind(this)\n const toFinishReason = this.toFinishReason.bind(this)\n const handleControlRequest = this.handleControlRequest.bind(this)\n\n if (scope === \"no-tools\" && !compactionMode) {\n log.info(\"doStream no-tools title stub\", {\n compactionMode,\n opencodeAgent: this.getOpencodeAgent(options.providerOptions),\n providerOptionsKeys: options.providerOptions\n ? Object.keys(options.providerOptions)\n : [],\n })\n const text = this.synthesizeTitle(options.prompt)\n const textId = generateId()\n const stream = new ReadableStream<LanguageModelV3StreamPart>({\n start(controller) {\n controller.enqueue({ type: \"stream-start\", warnings })\n controller.enqueue({ type: \"text-start\", id: textId } as any)\n controller.enqueue({\n type: \"text-delta\",\n id: textId,\n delta: text,\n })\n controller.enqueue({ type: \"text-end\", id: textId })\n controller.enqueue({\n type: \"finish\",\n finishReason: toFinishReason(\"stop\"),\n usage: toUsage({ input_tokens: 0, output_tokens: 0 }),\n providerMetadata: {\n \"claude-code\": {\n synthetic: true,\n path: \"no-tools\",\n },\n },\n })\n controller.close()\n },\n })\n\n return {\n stream,\n request: { body: { text: \"\" } },\n }\n }\n\n // Short-circuit when opencode iterates the agent loop one more time\n // after a turn already finished. The prompt ends with an assistant\n // message and has no fresh user input — spawning Claude here would\n // just produce a stub like \"No input received. Standing by\".\n if (!hasNewUserContent(options.prompt)) {\n log.info(\"doStream short-circuit: no new user content\")\n const stream = new ReadableStream<LanguageModelV3StreamPart>({\n start(controller) {\n controller.enqueue({ type: \"stream-start\", warnings })\n controller.enqueue({\n type: \"finish\",\n finishReason: toFinishReason(\"stop\"),\n usage: toUsage({ input_tokens: 0, output_tokens: 0 }),\n providerMetadata: {\n \"claude-code\": { synthetic: true, path: \"no-new-user-content\" },\n },\n })\n controller.close()\n },\n })\n return { stream, request: { body: { text: \"\" } } }\n }\n\n const hasPriorConversation =\n options.prompt.filter((m) => m.role === \"user\" || m.role === \"assistant\")\n .length > 1\n\n // New session — clear any stale state from a previous session\n if (!hasPriorConversation) {\n deleteClaudeSessionId(sk)\n deleteActiveProcess(sk)\n }\n\n const hasExistingSession = !!getClaudeSessionId(sk)\n const hasActiveProcess = !!getActiveProcess(sk)\n const includeHistoryContext =\n !hasExistingSession && !hasActiveProcess && hasPriorConversation\n\n const reasoningEffort = this.getReasoningEffort(options.providerOptions)\n const userMsg = getClaudeUserMessage(\n options.prompt,\n includeHistoryContext,\n reasoningEffort,\n { compactionMode },\n )\n const resolvedProxy = compactionMode ? null : this.resolvedProxyTools()\n const self = this\n\n const previousPendingProxyCalls = compactionMode\n ? []\n : getPendingProxyCalls(sk)\n const previousPendingProxyMatches: Array<{\n call: PendingProxyCall\n result: ProxyToolResult | null\n }> = previousPendingProxyCalls.map((call) => ({\n call,\n result: this.extractPendingProxyResult(options.prompt, call.toolCallId),\n }))\n const hasMatchedPendingResults = previousPendingProxyMatches.some(\n (m) => m.result !== null,\n )\n\n // Pre-fetch opencode's MCP runtime status before constructing the\n // ReadableStream so the sync hot-reload check and async setup() see\n // the same overlay snapshot. One in-process call per turn — cheap;\n // the SDK client routes through `Server.app.fetch` (no socket).\n // Detect the Claude CLI version in parallel so the spawn can decide\n // which optional flags it supports without crashing older binaries.\n const [runtimeStatus, cliVersion] = await Promise.all([\n compactionMode ? Promise.resolve(undefined) : getRuntimeMcpStatus(),\n detectCliVersion(this.config.cliPath),\n ])\n\n log.info(\"doStream starting\", {\n cwd,\n model: effectiveModelId,\n textLength: userMsg.length,\n includeHistoryContext,\n hasActiveProcess,\n reasoningEffort,\n proxyTools: resolvedProxy?.map((t) => t.name) ?? null,\n compactionMode,\n scope,\n opencodeAgent: this.getOpencodeAgent(options.providerOptions),\n providerOptionsKeys: options.providerOptions\n ? Object.keys(options.providerOptions)\n : [],\n })\n\n const stream = new ReadableStream<LanguageModelV3StreamPart>({\n start(controller) {\n // Compaction is a one-shot call. Don't reuse any cached process\n // from a prior compaction — each /compact gets a fresh spawn so\n // the new transcript isn't appended to a stale claude session.\n if (compactionMode) {\n deleteActiveProcess(sk)\n deleteClaudeSessionId(sk)\n }\n\n let activeProcess = getActiveProcess(sk)\n let proc: import(\"child_process\").ChildProcess\n let lineEmitter: import(\"events\").EventEmitter\n let proxyServer: ProxyMcpServer | null = activeProcess?.proxyServer ?? null\n\n // Hot reload: evict cached subprocess if the bridged opencode MCP\n // config has drifted since spawn. Only checked between turns (here,\n // before setup() runs), never mid tool-call. The stored claude\n // session id is preserved so the respawn resumes the conversation\n // via `--session-id` (handled by buildCliArgs).\n if (\n !compactionMode &&\n activeProcess &&\n self.config.hotReloadMcp !== false &&\n self.config.bridgeOpencodeMcp !== false\n ) {\n const probe = self.effectiveMcpConfig(cwd, undefined, runtimeStatus!)\n const previousHash = activeProcess.mcpHash ?? null\n if (previousHash !== probe.bridgedHash) {\n log.info(\"opencode MCP config changed, respawning claude\", {\n sk,\n previousHash,\n currentHash: probe.bridgedHash,\n })\n deleteActiveProcess(sk)\n activeProcess = undefined\n proxyServer = null\n }\n }\n\n const setup = async () => {\n let cliArgs: string[]\n let spawnSystemPromptFile: string | undefined\n let spawnProxyServer: ProxyMcpServer | null = null\n let spawnMcpHash: string | null = null\n\n if (compactionMode) {\n // Compaction takes a lean spawn: no MCP servers, no proxy, no\n // appended system prompt, no disallowed-tools list. The model\n // is asked for text output only on a single turn — all the\n // normal tool wiring is pure overhead and adds latency.\n // Explicitly opt out of `--session-id` so a stale id can never\n // resume into the lean spawn.\n cliArgs = buildCliArgs({\n sessionKey: sk,\n skipPermissions,\n includeSessionId: false,\n model: effectiveModelId,\n permissionMode: self.config.permissionMode,\n cliVersion,\n })\n } else {\n // First pass: discover which opencode MCP servers would be\n // bridged. We use this to decide which ones to re-route through\n // the proxy instead. No --mcp-config path is consumed here;\n // it's recomputed below with the exclusion set in place.\n const discovery = self.effectiveMcpConfig(\n cwd,\n undefined,\n runtimeStatus!,\n )\n\n // Fetch the proxy MCP tools (one ProxyToolDef per opencode\n // MCP-bridged tool). If discovery returns nothing or the SDK\n // is unreachable, this is null and we fall back to direct\n // bridging.\n const proxyMcpTools = await self.resolvedProxyMcpTools(\n discovery.allEnabledServerNames,\n )\n const excludeServers: ReadonlySet<string> | undefined = proxyMcpTools\n ? new Set(discovery.allEnabledServerNames)\n : undefined\n\n const combinedProxyTools: ProxyToolDef[] | null =\n resolvedProxy || proxyMcpTools\n ? [...(resolvedProxy ?? []), ...(proxyMcpTools ?? [])]\n : null\n\n if (!proxyServer && combinedProxyTools) {\n proxyServer = await self.ensureProxyServer(combinedProxyTools, sk)\n }\n\n const proxyDisallowed = resolvedProxy ? disallowedToolFlags(resolvedProxy) : []\n const extraDisallowed: string[] = []\n if (self.config.webSearch === \"disabled\") extraDisallowed.push(\"WebSearch\")\n const allDisallowed = [...proxyDisallowed, ...extraDisallowed]\n const mcp = self.effectiveMcpConfig(\n cwd,\n proxyServer?.configPath(),\n runtimeStatus!,\n excludeServers,\n )\n const systemPromptFile = activeProcess\n ? undefined\n : buildAppendedSystemPrompt(\n cwd,\n self.config.multiStepContinuation !== false,\n extractSystemMessages(options.prompt),\n )\n cliArgs = buildCliArgs({\n sessionKey: sk,\n skipPermissions,\n model: self.modelId,\n permissionMode: self.config.permissionMode,\n mcpConfig: mcp.paths,\n strictMcpConfig: self.config.strictMcpConfig,\n disallowedTools: allDisallowed.length > 0 ? allDisallowed : undefined,\n appendSystemPromptFile: systemPromptFile,\n ...self.thinkingCliOptions(),\n cliVersion,\n })\n spawnSystemPromptFile = systemPromptFile\n spawnProxyServer = proxyServer\n spawnMcpHash = mcp.bridgedHash\n }\n\n if (activeProcess && !compactionMode) {\n proc = activeProcess.proc\n lineEmitter = activeProcess.lineEmitter\n log.debug(\"reusing active process\", { sk })\n } else {\n const ap = spawnClaudeProcess(\n cliPath,\n cliArgs,\n cwd,\n sk,\n spawnProxyServer,\n spawnMcpHash,\n spawnSystemPromptFile,\n )\n proc = ap.proc\n lineEmitter = ap.lineEmitter\n activeProcess = ap\n }\n\n controller.enqueue({ type: \"stream-start\", warnings })\n\n let currentTextId: string | null = null\n const textBlockIndices = new Set<number>()\n\n const startTextBlock = (): string => {\n if (currentTextId) {\n controller.enqueue({ type: \"text-end\", id: currentTextId })\n }\n const id = generateId()\n currentTextId = id\n controller.enqueue({ type: \"text-start\", id } as any)\n return id\n }\n\n const endTextBlock = (): void => {\n if (currentTextId) {\n controller.enqueue({ type: \"text-end\", id: currentTextId })\n currentTextId = null\n }\n }\n\n const reasoningIds = new Map<number, string>()\n const reasoningStarted = new Map<number, boolean>()\n let hadThinkingTextFromStream = false\n\n let turnCompleted = false\n let controllerClosed = false\n let pendingProxyUnsubscribe: (() => void) | null = null\n let resultFallbackTimer: ReturnType<typeof setTimeout> | null = null\n let hasReceivedContent = false\n let visibleTextSinceContinue = \"\"\n let lastVisibleTextSinceContinue = \"\"\n let hadReasoningSinceContinue = false\n let hadToolActivitySinceContinue = false\n let hadProxyActivitySinceContinue = false\n // v0.4.16: protocol-level stop signal captured from Claude CLI's\n // stream. Set by either the `message_delta` partial event or the\n // top-level `assistant` message, whichever arrives first.\n let lastStopReason: string | null = null\n const autoContinueState: AutoContinueState = {\n enabled: self.config.autoContinueIncompleteTurns,\n attempts: 0,\n startedAt: Date.now(),\n noProgressCount: 0,\n }\n\n const clearFallbackTimer = () => {\n if (resultFallbackTimer) {\n clearTimeout(resultFallbackTimer)\n resultFallbackTimer = null\n }\n }\n\n // Wire-inactivity watchdog. Resets on every line received from the\n // CLI; only fires if the CLI has emitted content and then gone\n // silent on stdout for `delayMs` without sending a `result`. The\n // previous design armed this on every text content_block_stop,\n // which killed legitimate mid-turn think pauses (most visibly\n // with sonnet between text-end and the next tool_use_start).\n const startResultFallback = (delayMs = 60_000) => {\n clearFallbackTimer()\n if (!hasReceivedContent || controllerClosed) return\n resultFallbackTimer = setTimeout(() => {\n if (controllerClosed) return\n log.warn(\"result fallback timer fired — closing stream without result event\", {\n delayMs,\n })\n closeHandler()\n }, delayMs)\n }\n\n const toolCallMap = new Map<\n number,\n { id: string; name: string; inputJson: string }\n >()\n // Tool calls the plugin reported as providerExecuted:false — opencode\n // will run these itself and emit its own tool-result, so we must NOT\n // forward Claude CLI's tool_result for them (would short-circuit\n // opencode's execute).\n const skipResultForIds = new Set<string>()\n const toolCallsById = new Map<\n string,\n { id: string; name: string; input: unknown }\n >()\n\n let resultMeta: {\n sessionId?: string\n costUsd?: number\n durationMs?: number\n usage?: ClaudeStreamMessage[\"usage\"]\n } = {}\n\n // Batched drain so claude CLI's parallel tool_use blocks (e.g. two\n // bash calls in one assistant message) end up in a single\n // tool-calls finish event. Without this, the broker would reject\n // every overlapping call and claude would see spurious tool errors.\n const drainBuffer: PendingProxyCall[] = []\n let drainTimer: ReturnType<typeof setTimeout> | null = null\n const DRAIN_QUIET_MS = 100\n\n const finishWithToolCalls = (calls: PendingProxyCall[]) => {\n if (controllerClosed) return\n if (calls.length === 0) return\n for (const call of calls) {\n controller.enqueue({\n type: \"tool-input-start\",\n id: call.toolCallId,\n toolName: call.toolName,\n } as any)\n controller.enqueue({\n type: \"tool-call\",\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n input: JSON.stringify(call.input),\n providerExecuted: false,\n } as any)\n skipResultForIds.add(call.toolCallId)\n }\n controller.enqueue({\n type: \"finish\",\n finishReason: toFinishReason(\"tool-calls\"),\n usage: toUsage(resultMeta.usage),\n providerMetadata: {\n \"claude-code\": resultMeta,\n },\n })\n controllerClosed = true\n cleanupTurn()\n try {\n controller.close()\n } catch {}\n }\n\n const drainNow = () => {\n if (drainTimer) {\n clearTimeout(drainTimer)\n drainTimer = null\n }\n if (drainBuffer.length === 0) return\n if (controllerClosed) return\n const batch = drainBuffer.splice(0, drainBuffer.length)\n log.info(\"draining pending proxy calls into stream finish\", {\n sessionKey: sk,\n count: batch.length,\n toolCallIds: batch.map((c) => c.toolCallId),\n })\n finishWithToolCalls(batch)\n }\n\n const noteVisibleText = (text: string) => {\n visibleTextSinceContinue += text\n lastVisibleTextSinceContinue += text\n }\n\n const resetLastVisibleTextBlock = () => {\n lastVisibleTextSinceContinue = \"\"\n }\n\n const noteReasoning = () => {\n hadReasoningSinceContinue = true\n }\n\n const noteToolActivity = () => {\n hadToolActivitySinceContinue = true\n }\n\n const noteProxyActivity = () => {\n hadProxyActivitySinceContinue = true\n }\n\n const resetAutoContinueWindow = () => {\n visibleTextSinceContinue = \"\"\n lastVisibleTextSinceContinue = \"\"\n hadReasoningSinceContinue = false\n hadToolActivitySinceContinue = false\n hadProxyActivitySinceContinue = false\n lastStopReason = null\n }\n\n // Set true once we observe a `stream_event` envelope. When on, the\n // top-level `assistant` message is a duplicate of what we already\n // streamed via content_block_* deltas — skip its content.\n let gotPartialEvents = false\n\n const lineHandler = (line: string) => {\n if (!line.trim()) return\n if (controllerClosed) return\n\n // Any line from the CLI counts as activity — reset the inactivity\n // watchdog so mid-turn pauses between blocks don't get killed.\n startResultFallback()\n\n try {\n const outer: ClaudeStreamMessage = JSON.parse(line)\n\n // Unwrap stream_event envelope (--include-partial-messages).\n // Inner event uses the same content_block_* / message_* shape.\n const msg: ClaudeStreamMessage =\n outer.type === \"stream_event\" && outer.event\n ? { ...outer.event, session_id: outer.session_id }\n : outer\n\n if (outer.type === \"stream_event\") {\n gotPartialEvents = true\n }\n\n if (handleControlRequest(msg, proc)) {\n return\n }\n\n log.debug(\"stream message\", {\n type: msg.type,\n subtype: msg.subtype,\n })\n\n // Handle system init\n if (msg.type === \"system\" && msg.subtype === \"init\") {\n if (msg.session_id) {\n setClaudeSessionId(sk, msg.session_id)\n log.info(\"session initialized\", {\n claudeSessionId: msg.session_id,\n })\n }\n }\n\n // content_block_start\n if (\n msg.type === \"content_block_start\" &&\n msg.content_block &&\n msg.index !== undefined\n ) {\n const block = msg.content_block\n const idx = msg.index\n\n if (block.type === \"thinking\") {\n noteReasoning()\n const reasoningId = generateId()\n reasoningIds.set(idx, reasoningId)\n }\n\n if (block.type === \"text\") {\n textBlockIndices.add(idx)\n // New text block — clear last-block buffer so final-answer\n // detection only considers this block's contents, not earlier\n // mid-task narration.\n resetLastVisibleTextBlock()\n if (block.text) {\n if (!currentTextId) startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: currentTextId!,\n delta: block.text,\n })\n noteVisibleText(block.text)\n hasReceivedContent = true\n }\n }\n\n if (block.type === \"tool_use\" && block.id && block.name) {\n noteToolActivity()\n toolCallMap.set(idx, {\n id: block.id,\n name: block.name,\n inputJson: \"\",\n })\n\n if (\n block.name !== \"AskUserQuestion\" &&\n block.name !== \"ask_user_question\" &&\n block.name !== \"ExitPlanMode\" &&\n !block.name.startsWith(PROXY_TOOL_PREFIX)\n ) {\n const { name: mappedName, skip, executed } = mapTool(\n block.name,\n undefined,\n {\n webSearch: self.config.webSearch,\n sessionId: getClaudeSessionId(sk),\n toolUseId: block.id,\n },\n )\n if (!skip) {\n controller.enqueue({\n type: \"tool-input-start\",\n id: block.id,\n toolName: mappedName,\n providerExecuted: executed,\n } as any)\n log.info(\"tool started\", {\n name: block.name,\n mappedName,\n id: block.id,\n })\n }\n }\n }\n }\n\n // content_block_delta\n if (\n msg.type === \"content_block_delta\" &&\n msg.delta &&\n msg.index !== undefined\n ) {\n const delta = msg.delta\n const idx = msg.index\n\n if (delta.type === \"thinking_delta\" && delta.thinking) {\n noteReasoning()\n hadThinkingTextFromStream = true\n const reasoningId = reasoningIds.get(idx)\n if (reasoningId) {\n if (!reasoningStarted.get(idx)) {\n controller.enqueue({\n type: \"reasoning-start\",\n id: reasoningId,\n } as any)\n reasoningStarted.set(idx, true)\n }\n controller.enqueue({\n type: \"reasoning-delta\",\n id: reasoningId,\n delta: delta.thinking,\n } as any)\n }\n }\n\n if (delta.type === \"text_delta\" && delta.text) {\n if (!currentTextId) startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: currentTextId!,\n delta: delta.text,\n })\n noteVisibleText(delta.text)\n hasReceivedContent = true\n }\n\n if (delta.type === \"input_json_delta\" && delta.partial_json) {\n const tc = toolCallMap.get(idx)\n if (tc) {\n tc.inputJson += delta.partial_json\n controller.enqueue({\n type: \"tool-input-delta\",\n id: tc.id,\n delta: delta.partial_json,\n } as any)\n }\n }\n\n if (!KNOWN_DELTA_TYPES.has(delta.type)) {\n log.debug(\"unrecognized content_block_delta type\", {\n type: delta.type,\n idx,\n keys: Object.keys(delta),\n })\n }\n }\n\n // content_block_stop\n if (\n msg.type === \"content_block_stop\" &&\n msg.index !== undefined\n ) {\n const idx = msg.index\n\n const reasoningId = reasoningIds.get(idx)\n if (reasoningId && reasoningStarted.get(idx)) {\n controller.enqueue({\n type: \"reasoning-end\",\n id: reasoningId,\n } as any)\n reasoningStarted.delete(idx)\n }\n\n if (textBlockIndices.has(idx)) {\n endTextBlock()\n textBlockIndices.delete(idx)\n }\n\n const tc = toolCallMap.get(idx)\n if (tc) {\n let parsedInput: any = {}\n try {\n parsedInput = JSON.parse(tc.inputJson || \"{}\")\n } catch {}\n\n if (isAskUserQuestionTool(tc.name)) {\n const askId = startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: askId,\n delta: formatAskUserQuestion(parsedInput),\n })\n endTextBlock()\n } else if (tc.name === \"ExitPlanMode\") {\n const plan = (parsedInput?.plan as string) || \"\"\n\n const planId = startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: planId,\n delta: `\\n\\n${plan}\\n\\n---\\n**Do you want to proceed with this plan?** (yes/no)\\n`,\n })\n endTextBlock()\n } else if (tc.name.startsWith(PROXY_TOOL_PREFIX)) {\n log.debug(\"ignoring proxy tool_use block; broker handles it\", {\n name: tc.name,\n id: tc.id,\n })\n } else {\n const {\n name: mappedName,\n input: mappedInput,\n executed,\n skip,\n } = mapTool(tc.name, parsedInput, {\n webSearch: self.config.webSearch,\n sessionId: getClaudeSessionId(sk),\n toolUseId: tc.id,\n })\n\n if (!skip) {\n toolCallsById.set(tc.id, {\n id: tc.id,\n name: tc.name,\n input: parsedInput,\n })\n if (!executed) skipResultForIds.add(tc.id)\n\n controller.enqueue({\n type: \"tool-call\",\n toolCallId: tc.id,\n toolName: mappedName,\n input: JSON.stringify(mappedInput),\n providerExecuted: executed,\n } as any)\n }\n log.info(\"tool call complete\", {\n name: tc.name,\n mappedName,\n id: tc.id,\n executed,\n })\n }\n }\n }\n\n // Capture protocol-level stop_reason from the streaming\n // `message_delta` event (sent right before the final\n // `message_stop`). Any non-empty value is the source-of-truth\n // for why the turn ended — used to bypass the keyword heuristic.\n if (\n gotPartialEvents &&\n msg.type === \"message_delta\" &&\n typeof (msg as any).delta?.stop_reason === \"string\"\n ) {\n lastStopReason = (msg as any).delta.stop_reason\n }\n\n // assistant message (complete, not streaming).\n // When --include-partial-messages is on, this is a duplicate of\n // what we already streamed via content_block_* events. Skip it\n // for content, but still capture stop_reason from it for the\n // non-partial path.\n if (\n msg.type === \"assistant\" &&\n msg.message &&\n typeof (msg.message as any).stop_reason === \"string\"\n ) {\n lastStopReason = (msg.message as any).stop_reason\n }\n // Fallback: extract thinking from the complete assistant\n // message. opus-4-7's CLI strips thinking_delta from stream\n // events but may include thinking in the final message.\n if (\n msg.type === \"assistant\" &&\n msg.message?.content &&\n gotPartialEvents\n ) {\n const thinkingBlocks = (msg.message.content as any[]).filter(\n (b) => b.type === \"thinking\",\n )\n if (thinkingBlocks.length > 0) {\n log.info(\"assistant message thinking blocks\", {\n count: thinkingBlocks.length,\n hasText: thinkingBlocks.some(\n (b) => typeof b.thinking === \"string\" && b.thinking.length > 0,\n ),\n hadStreamThinking: hadThinkingTextFromStream,\n })\n if (!hadThinkingTextFromStream) {\n for (const block of thinkingBlocks) {\n if (block.thinking && block.thinking.length > 0) {\n noteReasoning()\n hadThinkingTextFromStream = true\n const thinkingId = generateId()\n controller.enqueue({\n type: \"reasoning-start\",\n id: thinkingId,\n } as any)\n controller.enqueue({\n type: \"reasoning-delta\",\n id: thinkingId,\n delta: block.thinking,\n } as any)\n controller.enqueue({\n type: \"reasoning-end\",\n id: thinkingId,\n } as any)\n }\n }\n }\n }\n }\n if (\n msg.type === \"assistant\" &&\n msg.message?.content &&\n !gotPartialEvents\n ) {\n const hasText = msg.message.content.some(\n (b: any) => b.type === \"text\" && b.text,\n )\n const hasToolUse = msg.message.content.some(\n (b: any) => b.type === \"tool_use\",\n )\n\n if (hasText) {\n hasReceivedContent = true\n }\n\n if (hasText && !hasToolUse) {\n startResultFallback()\n }\n if (hasToolUse) {\n clearFallbackTimer()\n }\n\n for (const block of msg.message.content) {\n if (block.type === \"text\" && block.text) {\n // New text block — keep only this block's text in the\n // last-block buffer for final-answer detection.\n resetLastVisibleTextBlock()\n const blockId = startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: blockId,\n delta: block.text,\n })\n endTextBlock()\n noteVisibleText(block.text)\n hasReceivedContent = true\n }\n\n if (block.type === \"thinking\" && block.thinking) {\n noteReasoning()\n const thinkingId = generateId()\n controller.enqueue({\n type: \"reasoning-start\",\n id: thinkingId,\n } as any)\n controller.enqueue({\n type: \"reasoning-delta\",\n id: thinkingId,\n delta: block.thinking,\n } as any)\n controller.enqueue({\n type: \"reasoning-end\",\n id: thinkingId,\n } as any)\n }\n\n if (block.type === \"tool_use\" && block.id && block.name) {\n noteToolActivity()\n const parsedInput = (block.input ?? {}) as Record<\n string,\n unknown\n >\n toolCallsById.set(block.id, {\n id: block.id,\n name: block.name,\n input: parsedInput,\n })\n\n if (isAskUserQuestionTool(block.name)) {\n const askId = startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: askId,\n delta: formatAskUserQuestion(parsedInput),\n })\n endTextBlock()\n } else if (block.name === \"ExitPlanMode\") {\n const plan = (parsedInput?.plan as string) || \"\"\n\n const planId = startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: planId,\n delta: `\\n\\n${plan}\\n\\n---\\n**Do you want to proceed with this plan?** (yes/no)\\n`,\n })\n endTextBlock()\n } else if (block.name.startsWith(PROXY_TOOL_PREFIX)) {\n log.debug(\"ignoring proxy tool_use from assistant message\", {\n name: block.name,\n id: block.id,\n })\n } else {\n const {\n name: mappedName,\n input: mappedInput,\n executed,\n skip,\n } = mapTool(block.name, parsedInput, {\n webSearch: self.config.webSearch,\n sessionId: getClaudeSessionId(sk),\n toolUseId: block.id,\n })\n\n if (!skip) {\n if (!executed) skipResultForIds.add(block.id)\n controller.enqueue({\n type: \"tool-input-start\",\n id: block.id,\n toolName: mappedName,\n providerExecuted: executed,\n } as any)\n controller.enqueue({\n type: \"tool-call\",\n toolCallId: block.id,\n toolName: mappedName,\n input: JSON.stringify(mappedInput),\n providerExecuted: executed,\n } as any)\n }\n log.info(\"tool_use from assistant message\", {\n name: block.name,\n mappedName,\n id: block.id,\n executed,\n })\n }\n }\n\n if (block.type === \"tool_result\") {\n log.debug(\"tool_result\", {\n toolUseId: block.tool_use_id,\n })\n }\n }\n }\n\n // user message (tool results from Claude CLI)\n if (msg.type === \"user\" && msg.message?.content) {\n for (const block of msg.message.content) {\n if (block.type === \"tool_result\" && block.tool_use_id) {\n if (skipResultForIds.has(block.tool_use_id)) {\n log.debug(\"skipping tool-result (opencode runs it)\", {\n toolUseId: block.tool_use_id,\n })\n continue\n }\n\n let resultText = \"\"\n if (typeof block.content === \"string\") {\n resultText = block.content\n } else if (Array.isArray(block.content)) {\n resultText = block.content\n .filter(\n (\n c,\n ): c is { type: string; text: string } =>\n c.type === \"text\" &&\n typeof c.text === \"string\",\n )\n .map((c) => c.text)\n .join(\"\\n\")\n }\n\n // Ledger hook: commit pending TaskCreate to opencode's todo\n // panel via a synthetic todowrite emission. Pass-through —\n // returns null for non-TaskCreate ids, so cheap and silent.\n const claudeSessionId = getClaudeSessionId(sk)\n if (claudeSessionId) {\n const list = applyTaskCreateToolResult(\n claudeSessionId,\n block.tool_use_id,\n resultText,\n )\n if (list) {\n const synthId = `todowrite_${block.tool_use_id}`\n controller.enqueue({\n type: \"tool-input-start\",\n id: synthId,\n toolName: \"todowrite\",\n providerExecuted: false,\n } as any)\n controller.enqueue({\n type: \"tool-call\",\n toolCallId: synthId,\n toolName: \"todowrite\",\n input: JSON.stringify({\n todos: list.map((t) => ({\n id: t.id,\n content: t.content,\n status: t.status,\n priority: \"medium\",\n })),\n }),\n providerExecuted: false,\n } as any)\n noteToolActivity()\n }\n }\n\n const toolCall = toolCallsById.get(block.tool_use_id)\n if (toolCall) {\n controller.enqueue({\n type: \"tool-result\",\n toolCallId: block.tool_use_id,\n toolName: toolCall.name,\n result: {\n output: resultText,\n title: toolCall.name,\n metadata: {},\n },\n providerExecuted: true,\n } as any)\n noteToolActivity()\n log.info(\"tool result emitted\", {\n toolUseId: block.tool_use_id,\n name: toolCall.name,\n })\n toolCallsById.delete(block.tool_use_id)\n }\n }\n }\n }\n\n // result - end of conversation turn\n if (msg.type === \"result\") {\n clearFallbackTimer()\n\n if (msg.session_id) {\n setClaudeSessionId(sk, msg.session_id)\n }\n\n // Some CLI failures only include user-readable text in\n // `result.result` (no prior assistant text blocks). Emit it so\n // opencode users don't see a blank turn.\n if (\n !currentTextId &&\n msg.is_error &&\n typeof msg.result === \"string\" &&\n msg.result.trim().length > 0\n ) {\n const errId = startTextBlock()\n controller.enqueue({\n type: \"text-delta\",\n id: errId,\n delta: msg.result,\n })\n }\n\n resultMeta = {\n sessionId: msg.session_id,\n costUsd: msg.total_cost_usd,\n durationMs: msg.duration_ms,\n usage: msg.usage,\n }\n\n log.info(\"conversation result\", {\n sessionId: msg.session_id,\n durationMs: msg.duration_ms,\n numTurns: msg.num_turns,\n isError: msg.is_error,\n })\n\n turnCompleted = true\n\n endTextBlock()\n\n // Drain race / abandoned-call guard. If Claude CLI emitted\n // `result` while a proxy tool call is still pending — either\n // because the 100ms drain timer hasn't fired yet, or because\n // Claude CLI gave up on its MCP HTTP request after an internal\n // timeout — drain it through the normal tool-calls flow so\n // opencode executes the tool; otherwise reject any orphan\n // pending calls so proxy-mcp returns to the HTTP caller\n // immediately instead of hanging until the broker's 10-minute\n // timeout (which surfaces as a hard 2-minute \"operation timed\n // out\" on the SDK side).\n if (drainBuffer.length > 0) {\n log.info(\n \"draining pending proxy calls at turn-result boundary\",\n {\n sessionKey: sk,\n count: drainBuffer.length,\n },\n )\n drainNow()\n return\n }\n const orphanPending = getPendingProxyCalls(sk)\n if (orphanPending.length > 0) {\n log.warn(\n \"rejecting orphan pending proxy calls at turn-result boundary\",\n {\n sessionKey: sk,\n count: orphanPending.length,\n },\n )\n rejectAllPendingProxyCallsForSession(\n sk,\n new Error(\n \"Claude CLI emitted result with pending proxy calls not in drain buffer\",\n ),\n )\n }\n\n const autoDecision = shouldAutoContinueIncompleteTurn(\n autoContinueState,\n {\n text: visibleTextSinceContinue,\n lastVisibleText: lastVisibleTextSinceContinue,\n hadReasoning: hadReasoningSinceContinue,\n hadToolActivity: hadToolActivitySinceContinue,\n hadProxyActivity: hadProxyActivitySinceContinue,\n isError: msg.is_error,\n stopReason: lastStopReason,\n },\n )\n if (autoDecision.continue) {\n const signature = continuationSignature({\n text: visibleTextSinceContinue,\n lastVisibleText: lastVisibleTextSinceContinue,\n hadReasoning: hadReasoningSinceContinue,\n hadToolActivity: hadToolActivitySinceContinue,\n hadProxyActivity: hadProxyActivitySinceContinue,\n isError: msg.is_error,\n })\n autoContinueState.noProgressCount =\n signature === autoContinueState.lastSignature\n ? autoContinueState.noProgressCount + 1\n : 0\n autoContinueState.lastSignature = signature\n autoContinueState.attempts++\n log.notice(\"auto-continuing incomplete claude result\", {\n sessionKey: sk,\n reason: autoDecision.reason,\n attempts: autoContinueState.attempts,\n textLength: visibleTextSinceContinue.length,\n lastTextLength: lastVisibleTextSinceContinue.length,\n hadReasoning: hadReasoningSinceContinue,\n hadToolActivity: hadToolActivitySinceContinue,\n hadProxyActivity: hadProxyActivitySinceContinue,\n })\n turnCompleted = false\n resetAutoContinueWindow()\n proc.stdin?.write(makeAutoContinueMessage() + \"\\n\")\n return\n }\n log.notice(\"auto-continuation stopped\", {\n sessionKey: sk,\n reason: autoDecision.reason,\n stopReason: lastStopReason,\n attempts: autoContinueState.attempts,\n textLength: visibleTextSinceContinue.length,\n lastTextLength: lastVisibleTextSinceContinue.length,\n hadReasoning: hadReasoningSinceContinue,\n hadToolActivity: hadToolActivitySinceContinue,\n hadProxyActivity: hadProxyActivitySinceContinue,\n })\n\n for (const [idx, reasoningId] of reasoningIds) {\n if (reasoningStarted.get(idx)) {\n controller.enqueue({\n type: \"reasoning-end\",\n id: reasoningId,\n } as any)\n }\n }\n\n controller.enqueue({\n type: \"finish\",\n finishReason: toFinishReason(\"stop\"),\n usage: toUsage(msg.usage),\n providerMetadata: {\n \"claude-code\": {\n ...resultMeta,\n ...(compactionMode\n ? { compactionModel: effectiveModelId }\n : {}),\n },\n ...(typeof msg.usage?.cache_creation_input_tokens === \"number\"\n ? {\n anthropic: {\n cacheCreationInputTokens:\n msg.usage.cache_creation_input_tokens,\n },\n }\n : {}),\n },\n })\n\n controllerClosed = true\n cleanupTurn()\n\n try {\n controller.close()\n } catch {}\n }\n } catch (e) {\n log.debug(\"failed to parse line\", {\n error:\n e instanceof Error ? e.message : String(e),\n })\n }\n }\n\n const closeHandler = () => {\n log.debug(\"readline closed\")\n if (controllerClosed) return\n // Claude CLI's stdio is gone. The proxy-mcp HTTP requests that\n // backed any pending tool calls have no one to answer them now —\n // reject so the handlers return errors rather than hang.\n if (drainBuffer.length > 0 || getPendingProxyCalls(sk).length > 0) {\n rejectAllPendingProxyCallsForSession(\n sk,\n new Error(\n \"Claude CLI subprocess closed before pending tool calls were resolved\",\n ),\n )\n drainBuffer.length = 0\n }\n controllerClosed = true\n cleanupTurn()\n endTextBlock()\n controller.enqueue({\n type: \"finish\",\n finishReason: toFinishReason(\"stop\"),\n usage: toUsage(),\n providerMetadata: {\n \"claude-code\": {\n ...resultMeta,\n ...(compactionMode\n ? { compactionModel: effectiveModelId }\n : {}),\n },\n },\n })\n try {\n controller.close()\n } catch {}\n }\n\n // Centralised per-turn teardown. Every exit path funnels through here\n // so we don't accumulate listeners across turns on a reused process.\n let cleanedUp = false\n const cleanupTurn = () => {\n if (cleanedUp) return\n cleanedUp = true\n clearFallbackTimer()\n if (drainTimer) {\n clearTimeout(drainTimer)\n drainTimer = null\n }\n lineEmitter.off(\"line\", lineHandler)\n lineEmitter.off(\"close\", closeHandler)\n pendingProxyUnsubscribe?.()\n pendingProxyUnsubscribe = null\n proc.off(\"error\", procErrorHandler)\n }\n\n const procErrorHandler = (err: Error) => {\n log.error(\"process error\", { error: err.message })\n if (controllerClosed) return\n // Subprocess failure invalidates every pending HTTP-bound tool\n // call for this session. Reject them so proxy-mcp returns errors\n // to Claude rather than letting the sockets stall.\n if (drainBuffer.length > 0 || getPendingProxyCalls(sk).length > 0) {\n rejectAllPendingProxyCallsForSession(\n sk,\n new Error(\n `Claude CLI subprocess error: ${err.message}`,\n ),\n )\n drainBuffer.length = 0\n }\n controllerClosed = true\n cleanupTurn()\n controller.enqueue({ type: \"error\", error: err })\n try {\n controller.close()\n } catch {}\n }\n\n lineEmitter.on(\"line\", lineHandler)\n lineEmitter.on(\"close\", closeHandler)\n\n pendingProxyUnsubscribe = onPendingProxyCall(sk, (call) => {\n if (controllerClosed) {\n // Stream already closed (we already drained). Late arrival —\n // reject immediately so the proxy-mcp HTTP request returns\n // instead of hanging until its 10-min timeout.\n log.warn(\n \"pending proxy call arrived after stream close; rejecting\",\n {\n sessionKey: sk,\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n },\n )\n rejectPendingProxyCallById(\n call.toolCallId,\n new Error(\n `Pending proxy call '${call.toolName}' arrived after the stream was already closed`,\n ),\n )\n return\n }\n log.info(\"received pending proxy call for session\", {\n sessionKey: sk,\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n })\n noteProxyActivity()\n noteToolActivity()\n drainBuffer.push(call)\n if (drainTimer) clearTimeout(drainTimer)\n drainTimer = setTimeout(drainNow, DRAIN_QUIET_MS)\n })\n\n proc.on(\"error\", procErrorHandler)\n\n // On abort, keep process alive for next message\n if (options.abortSignal) {\n options.abortSignal.addEventListener(\"abort\", () => {\n autoContinueState.aborted = true\n if (turnCompleted || controllerClosed) return\n\n if (!hasReceivedContent) {\n log.info(\n \"abort signal received before content, closing stream immediately\",\n { cwd },\n )\n controllerClosed = true\n cleanupTurn()\n try {\n controller.close()\n } catch {}\n return\n }\n\n log.info(\n \"abort signal received mid-turn, starting grace period\",\n { cwd },\n )\n // Abort grace period — short, since the user already asked to stop.\n startResultFallback(5_000)\n })\n }\n\n if (hasMatchedPendingResults) {\n // Tool-result turn: the prompt carries opencode's results for the\n // proxy tool calls we drained on the previous turn. Resolve each\n // matched call (claude CLI's HTTP handlers wake up and continue).\n // Any pending calls without a matching tool-result are orphans\n // (rare protocol anomaly); reject them so claude CLI doesn't hang\n // on those HTTP requests.\n for (const { call, result } of previousPendingProxyMatches) {\n if (result) {\n log.info(\"resolving pending proxy call from tool result prompt\", {\n sessionKey: sk,\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n })\n resolvePendingProxyCallById(call.toolCallId, result)\n } else {\n log.notice(\n \"pending proxy call had no matching tool-result; rejecting as orphan\",\n {\n sessionKey: sk,\n toolCallId: call.toolCallId,\n toolName: call.toolName,\n },\n )\n rejectPendingProxyCallById(\n call.toolCallId,\n new Error(\n `Pending proxy call '${call.toolName}' (${call.toolCallId}) was not matched in tool-result turn; rejecting as orphaned`,\n ),\n )\n }\n }\n return\n }\n\n // No pending calls had matching tool-results. If any pending calls\n // are still hanging around from a prior turn, reject them so the\n // HTTP handlers in proxy-mcp don't sit blocked forever while we\n // proceed with a brand new user message.\n if (previousPendingProxyCalls.length > 0) {\n for (const call of previousPendingProxyCalls) {\n rejectPendingProxyCallById(\n call.toolCallId,\n new Error(\n `Pending proxy call '${call.toolName}' (${call.toolCallId}) was orphaned by a new user turn; rejecting`,\n ),\n )\n }\n }\n\n // Send the user message for a fresh turn.\n proc.stdin?.write(userMsg + \"\\n\")\n log.debug(\"sent user message\", { textLength: userMsg.length })\n }\n\n void setup().catch((err) => {\n log.error(\"failed to set up doStream\", {\n error: err instanceof Error ? err.message : String(err),\n })\n controller.enqueue({\n type: \"error\",\n error: err instanceof Error ? err : new Error(String(err)),\n })\n try {\n controller.close()\n } catch {}\n })\n },\n cancel() {\n // Consumer cancelled the stream\n },\n })\n\n return {\n stream,\n request: { body: { text: userMsg } },\n response: { headers: {} },\n }\n }\n}\n","import { appendFileSync, mkdirSync, renameSync, statSync } from \"node:fs\"\nimport { homedir } from \"node:os\"\nimport { dirname, join } from \"node:path\"\n\nexport type LogLevel = \"debug\" | \"info\" | \"notice\" | \"warn\" | \"error\"\nexport type LogMode = \"silent\" | \"debug\"\n\nexport interface LoggerConfig {\n file: boolean\n dir: string | null\n mode: LogMode\n level: LogLevel\n}\n\nconst LEVEL_RANK: Record<LogLevel, number> = {\n debug: 0,\n info: 1,\n notice: 2,\n warn: 3,\n error: 4,\n}\n\nconst MAX_LOG_BYTES = 5 * 1024 * 1024 // 5 MB\nconst DEFAULT_DIR = join(homedir(), \".local\", \"share\", \"opencode-claude-code\")\n\nconst DEFAULT_CONFIG: LoggerConfig = {\n file: false,\n dir: null,\n mode: \"silent\",\n level: \"info\",\n}\n\nfunction parseBoolEnv(v: string | undefined): boolean | undefined {\n if (v == null) return undefined\n const s = v.toLowerCase().trim()\n if (s === \"\") return undefined\n if (s === \"0\" || s === \"false\" || s === \"no\" || s === \"off\") return false\n return true\n}\n\nfunction parseLevelEnv(v: string | undefined): LogLevel | undefined {\n if (v == null) return undefined\n const s = v.toLowerCase().trim()\n if (s === \"\") return undefined\n if (s === \"debug\" || s === \"info\" || s === \"notice\" || s === \"warn\" || s === \"error\") {\n return s\n }\n return undefined\n}\n\nfunction parseModeFromDebugEnv(v: string | undefined): LogMode | undefined {\n if (v == null || v === \"\") return undefined\n return v.includes(\"opencode-claude-code\") ? \"debug\" : undefined\n}\n\nfunction withEnvOverrides(base: LoggerConfig): LoggerConfig {\n const result: LoggerConfig = { ...base }\n const envFile = parseBoolEnv(process.env.OPENCODE_CLAUDE_CODE_LOG_FILE)\n if (envFile !== undefined) result.file = envFile\n const envDir = process.env.OPENCODE_CLAUDE_CODE_LOG_DIR\n if (envDir !== undefined && envDir !== \"\") result.dir = envDir\n const envMode = parseModeFromDebugEnv(process.env.DEBUG)\n if (envMode !== undefined) result.mode = envMode\n const envLevel = parseLevelEnv(process.env.OPENCODE_CLAUDE_CODE_LOG_LEVEL)\n if (envLevel !== undefined) result.level = envLevel\n return result\n}\n\nlet activeConfig: LoggerConfig = withEnvOverrides(DEFAULT_CONFIG)\nlet fileLoggingDisabled = false\n\n/**\n * Configure the logger from plugin settings. Env vars override the supplied\n * config when explicitly set, so a developer can flip behavior for a single\n * process without editing opencode.jsonc.\n *\n * `OPENCODE_CLAUDE_CODE_LOG_FILE` → `file` (1/true/on/yes vs 0/false/no/off)\n * `OPENCODE_CLAUDE_CODE_LOG_DIR` → `dir`\n * `DEBUG=opencode-claude-code` → `mode: \"debug\"`\n * `OPENCODE_CLAUDE_CODE_LOG_LEVEL` → `level` (debug | info | notice | warn | error)\n */\nexport function configureLogger(input: Partial<LoggerConfig>): void {\n const merged: LoggerConfig = { ...DEFAULT_CONFIG, ...input }\n activeConfig = withEnvOverrides(merged)\n fileLoggingDisabled = false\n}\n\nexport function getLoggerConfig(): LoggerConfig {\n return { ...activeConfig }\n}\n\n/** Test-only helper. Resets to defaults+env so tests are deterministic. */\nexport function _resetLoggerForTests(): void {\n activeConfig = withEnvOverrides(DEFAULT_CONFIG)\n fileLoggingDisabled = false\n}\n\nfunction resolvedLogFile(): string {\n return join(activeConfig.dir ?? DEFAULT_DIR, \"plugin.log\")\n}\n\nfunction rotateIfNeeded(logFile: string): void {\n try {\n const stat = statSync(logFile)\n if (stat.size > MAX_LOG_BYTES) {\n renameSync(logFile, `${logFile}.1`)\n }\n } catch {\n // file does not exist yet — nothing to rotate\n }\n}\n\nfunction writeToFile(line: string): void {\n if (!activeConfig.file) return\n if (fileLoggingDisabled) return\n try {\n const logFile = resolvedLogFile()\n mkdirSync(dirname(logFile), { recursive: true })\n rotateIfNeeded(logFile)\n appendFileSync(logFile, line + \"\\n\", \"utf8\")\n } catch {\n // Disable on first failure to avoid spamming errors on a read-only FS.\n fileLoggingDisabled = true\n }\n}\n\nfunction fmt(level: string, msg: string, data?: Record<string, unknown>): string {\n const ts = new Date().toISOString()\n const base = `[${ts}] [opencode-claude-code] ${level}: ${msg}`\n if (data && Object.keys(data).length > 0) {\n return `${base} ${JSON.stringify(data)}`\n }\n return base\n}\n\nfunction shouldEmit(level: LogLevel): boolean {\n return LEVEL_RANK[level] >= LEVEL_RANK[activeConfig.level]\n}\n\nfunction shouldTui(level: LogLevel): boolean {\n // warn/error are alwaysStderr: a developer who passes the level threshold\n // should still see real problems in the TUI regardless of mode. Below-\n // threshold entries are filtered earlier by shouldEmit().\n if (level === \"warn\" || level === \"error\") return true\n return activeConfig.mode === \"debug\"\n}\n\nfunction emit(level: LogLevel, msg: string, data?: Record<string, unknown>): void {\n if (!shouldEmit(level)) return\n const line = fmt(level.toUpperCase(), msg, data)\n if (shouldTui(level)) {\n console.error(line)\n }\n writeToFile(line)\n}\n\nexport const log = {\n debug(msg: string, data?: Record<string, unknown>) {\n emit(\"debug\", msg, data)\n },\n info(msg: string, data?: Record<string, unknown>) {\n emit(\"info\", msg, data)\n },\n notice(msg: string, data?: Record<string, unknown>) {\n emit(\"notice\", msg, data)\n },\n warn(msg: string, data?: Record<string, unknown>) {\n emit(\"warn\", msg, data)\n },\n error(msg: string, data?: Record<string, unknown>) {\n emit(\"error\", msg, data)\n },\n}\n","import { log } from \"./logger.js\"\n\nexport type TodoStatus = \"pending\" | \"in_progress\" | \"completed\"\n\nexport interface TodoEntry {\n id: string\n content: string\n status: TodoStatus\n}\n\ninterface PendingCreate {\n subject: string\n createdAt: number\n}\n\ninterface SessionLedger {\n todos: Map<string, TodoEntry>\n pendingCreates: Map<string, PendingCreate>\n}\n\nconst ledgers = new Map<string, SessionLedger>()\n\nconst PENDING_CREATE_TTL_MS = 60_000\nconst TASK_CREATED_PATTERN = /Task\\s*#?\\s*(\\d+)\\s+created/i\nconst VALID_STATUSES: ReadonlySet<TodoStatus> = new Set([\"pending\", \"in_progress\", \"completed\"])\n\nfunction getOrCreate(sessionId: string): SessionLedger {\n let ledger = ledgers.get(sessionId)\n if (!ledger) {\n ledger = { todos: new Map(), pendingCreates: new Map() }\n ledgers.set(sessionId, ledger)\n }\n return ledger\n}\n\nfunction prunePending(ledger: SessionLedger): void {\n const cutoff = Date.now() - PENDING_CREATE_TTL_MS\n for (const [id, pending] of ledger.pendingCreates) {\n if (pending.createdAt < cutoff) ledger.pendingCreates.delete(id)\n }\n}\n\nfunction materialize(ledger: SessionLedger): TodoEntry[] {\n return Array.from(ledger.todos.values())\n}\n\nfunction resolveSubject(input: { subject?: unknown; description?: unknown } | undefined): string {\n const subject = typeof input?.subject === \"string\" ? input.subject.trim() : \"\"\n if (subject) return subject\n const description = typeof input?.description === \"string\" ? input.description.trim() : \"\"\n if (description) return description\n return \"(no subject)\"\n}\n\nexport function applyTaskCreateToolUse(\n sessionId: string,\n toolUseId: string,\n input: { subject?: unknown; description?: unknown } | undefined,\n): void {\n if (!sessionId || !toolUseId) return\n const ledger = getOrCreate(sessionId)\n prunePending(ledger)\n ledger.pendingCreates.set(toolUseId, {\n subject: resolveSubject(input),\n createdAt: Date.now(),\n })\n}\n\nexport function applyTaskCreateToolResult(\n sessionId: string,\n toolUseId: string,\n resultText: string,\n): TodoEntry[] | null {\n if (!sessionId || !toolUseId) return null\n const ledger = ledgers.get(sessionId)\n if (!ledger) return null\n const pending = ledger.pendingCreates.get(toolUseId)\n if (!pending) return null\n ledger.pendingCreates.delete(toolUseId)\n const match = typeof resultText === \"string\" ? resultText.match(TASK_CREATED_PATTERN) : null\n if (!match) {\n log.debug(\"TaskCreate result did not match expected format\", { sessionId, toolUseId, resultText })\n return null\n }\n const claudeId = match[1]\n if (ledger.todos.has(claudeId)) {\n log.debug(\"TaskCreate result for already-known claude id; overwriting\", { sessionId, claudeId })\n }\n ledger.todos.set(claudeId, { id: claudeId, content: pending.subject, status: \"pending\" })\n return materialize(ledger)\n}\n\nexport function applyTaskUpdate(\n sessionId: string,\n input: { taskId?: unknown; subject?: unknown; status?: unknown } | undefined,\n): TodoEntry[] | null {\n if (!sessionId) return null\n const taskId = typeof input?.taskId === \"string\" ? input.taskId : null\n if (!taskId) return null\n const ledger = ledgers.get(sessionId)\n if (!ledger) return null\n const entry = ledger.todos.get(taskId)\n if (!entry) {\n log.debug(\"TaskUpdate for unknown task id\", { sessionId, taskId })\n return null\n }\n if (input?.status === \"deleted\") {\n ledger.todos.delete(taskId)\n return materialize(ledger)\n }\n if (typeof input?.status === \"string\" && VALID_STATUSES.has(input.status as TodoStatus)) {\n entry.status = input.status as TodoStatus\n }\n if (typeof input?.subject === \"string\" && input.subject.trim().length > 0) {\n entry.content = input.subject.trim()\n }\n return materialize(ledger)\n}\n\nexport function clearLedger(sessionId: string): void {\n if (!sessionId) return\n ledgers.delete(sessionId)\n}\n\nexport function getLedger(sessionId: string): TodoEntry[] {\n const ledger = ledgers.get(sessionId)\n if (!ledger) return []\n return materialize(ledger)\n}\n\nexport function _resetAllLedgersForTests(): void {\n ledgers.clear()\n}\n","import { log } from \"./logger.js\"\nimport { applyTaskCreateToolUse, applyTaskUpdate, type TodoEntry } from \"./todo-ledger.js\"\nimport type { WebSearchRouting } from \"./types.js\"\n\nexport interface MapToolOptions {\n webSearch?: WebSearchRouting\n sessionId?: string\n toolUseId?: string\n}\n\n/**\n * Map Claude CLI tool input (snake_case) to OpenCode tool input (camelCase)\n */\nfunction mapToolInput(name: string, input: any): any {\n if (!input) return input\n\n switch (name) {\n case \"Write\":\n return {\n filePath: input.file_path ?? input.filePath,\n content: input.content,\n }\n case \"Edit\":\n return {\n filePath: input.file_path ?? input.filePath,\n oldString: input.old_string ?? input.oldString,\n newString: input.new_string ?? input.newString,\n replaceAll: input.replace_all ?? input.replaceAll,\n }\n case \"Read\":\n return {\n filePath: input.file_path ?? input.filePath,\n offset: input.offset,\n limit: input.limit,\n }\n case \"Bash\":\n return {\n command: input.command,\n description:\n input.description ||\n `Execute: ${String(input.command || \"\").slice(0, 50)}${String(input.command || \"\").length > 50 ? \"...\" : \"\"}`,\n timeout: input.timeout,\n }\n case \"NotebookEdit\":\n return {\n notebookPath: input.notebook_path ?? input.notebookPath,\n cellNumber: input.cell_number ?? input.cellNumber,\n newSource: input.new_source ?? input.newSource,\n cellType: input.cell_type ?? input.cellType,\n editMode: input.edit_mode ?? input.editMode,\n }\n case \"Glob\":\n return {\n pattern: input.pattern,\n path: input.path,\n }\n case \"Grep\":\n return {\n pattern: input.pattern,\n path: input.path,\n include: input.include,\n }\n case \"TodoWrite\":\n if (Array.isArray(input.todos)) {\n const mappedTodos = input.todos.map((todo: any, index: number) => ({\n content: todo.content,\n status: todo.status || \"pending\",\n priority: todo.priority || \"medium\",\n id: todo.id || `todo_${Date.now()}_${index}`,\n }))\n return { todos: mappedTodos }\n }\n return input\n default:\n return input\n }\n}\n\n// Tools that Claude CLI executes internally but we report to opencode for UI display\nconst OPENCODE_HANDLED_TOOLS = new Set([\n \"Edit\",\n \"Write\",\n \"Bash\",\n \"NotebookEdit\",\n \"Read\",\n \"Glob\",\n \"Grep\",\n])\n\n// Claude CLI internal tools that should not be forwarded to opencode.\n// These are part of Claude Code's own system and have no opencode equivalent.\n// Tools the Claude CLI emits for its own internal bookkeeping (sub-agents,\n// task tracking, search). opencode has no matching tool registry entry, so\n// forwarding them surfaces as `⚙ invalid` rows in the UI. Skip them.\n// TaskOutput is intentionally NOT here — it has an explicit bash-echo mapping\n// below so the result stays visible.\nconst CLAUDE_INTERNAL_TOOLS = new Set([\n \"ToolSearch\",\n \"Agent\",\n \"AskFollowupQuestion\",\n \"TaskList\",\n \"TaskGet\",\n \"TaskStop\",\n])\n\nfunction emitTodoWrite(todos: TodoEntry[]) {\n return {\n name: \"todowrite\",\n input: {\n todos: todos.map((todo) => ({\n id: todo.id,\n content: todo.content,\n status: todo.status,\n priority: \"medium\",\n })),\n },\n executed: false,\n }\n}\n\nexport function mapTool(\n name: string,\n input?: any,\n opts?: MapToolOptions,\n): { name: string; input?: any; executed: boolean; skip?: boolean } {\n // Claude CLI internal tools — skip entirely\n if (CLAUDE_INTERNAL_TOOLS.has(name)) {\n log.debug(\"skipping Claude CLI internal tool\", { name })\n return { name, input, executed: true, skip: true }\n }\n\n // TaskCreate: stash subject keyed by tool_use_id; emission happens on tool_result.\n // Without sessionId+toolUseId we cannot maintain the ledger, so fall back to skip\n // (preserves old behavior for callers that haven't been threaded yet).\n if (name === \"TaskCreate\") {\n if (opts?.sessionId && opts?.toolUseId) {\n applyTaskCreateToolUse(opts.sessionId, opts.toolUseId, input)\n }\n return { name, input, executed: true, skip: true }\n }\n\n // TaskUpdate: mutate ledger and emit full list as opencode todowrite. Without\n // sessionId, fall back to skip. Unknown task ids return null from the ledger\n // and we drop the event.\n if (name === \"TaskUpdate\") {\n if (opts?.sessionId) {\n const list = applyTaskUpdate(opts.sessionId, input)\n if (list !== null) return emitTodoWrite(list)\n }\n return { name, input, executed: true, skip: true }\n }\n\n // Plan mode tools\n if (name === \"EnterPlanMode\") return { name: \"plan_enter\", input: {}, executed: false }\n if (name === \"ExitPlanMode\") return { name: \"plan_exit\", input, executed: false }\n\n // TodoWrite needs opencode to run it locally so Todo.Service (and the UI\n // widget backed by it) gets populated. Reporting as provider-executed would\n // short-circuit opencode's own execute and leave the todo panel empty.\n if (name === \"TodoWrite\") {\n const mappedInput = mapToolInput(name, input)\n return { name: \"todowrite\", input: mappedInput, executed: false }\n }\n\n // WebSearch — routing controlled by config.webSearch\n if (name === \"WebSearch\" || name === \"web_search\") {\n const mappedInput = input?.query ? { query: input.query } : input\n const route = opts?.webSearch\n if (route && route !== \"claude\" && route !== \"disabled\") {\n log.debug(\"routing WebSearch to opencode tool\", { target: route, mappedInput })\n return { name: route, input: mappedInput, executed: false }\n }\n log.debug(\"WebSearch executed by Claude CLI\", { mappedInput })\n return { name: \"WebSearch\", input: mappedInput, executed: true }\n }\n\n // TaskOutput -> bash echo\n if (name === \"TaskOutput\") {\n if (!input) return { name: \"bash\", executed: false }\n const output = input?.content || input?.output || JSON.stringify(input)\n return {\n name: \"bash\",\n input: {\n command: `echo \"TASK OUTPUT: ${String(output).replace(/\"/g, '\\\\\"')}\"`,\n description: \"Displaying task output\",\n },\n executed: false,\n }\n }\n\n // Third-party MCP tools: mcp__<server>__<tool> -> <server>_<tool>.\n // Marked provider-executed because Claude CLI runs these internally via\n // its own --mcp-config; the tool-result is already in the stream. If we\n // reported executed:false, opencode would look up the tool in its own\n // registry, fail to find it, and emit an `invalid` tool error that\n // shadows the real result.\n //\n // Our own proxy tools (`mcp__opencode_proxy__*`) are filtered out by\n // callers before reaching here, so this branch only ever sees user MCP\n // servers configured in Claude CLI's settings.\n if (name.startsWith(\"mcp__\")) {\n const parts = name.slice(5).split(\"__\")\n if (parts.length >= 2) {\n const serverName = parts[0]\n const toolName = parts.slice(1).join(\"_\")\n const openCodeName = `${serverName}_${toolName}`\n log.debug(\"mapping MCP tool\", { original: name, mapped: openCodeName })\n return { name: openCodeName, input, executed: true }\n }\n }\n\n // Tools executed by Claude CLI internally - map to lowercase for opencode\n if (OPENCODE_HANDLED_TOOLS.has(name)) {\n const mappedInput = mapToolInput(name, input)\n const openCodeName = name.toLowerCase()\n log.debug(\"mapping CLI-executed tool\", { name, openCodeName })\n return { name: openCodeName, input: mappedInput, executed: true }\n }\n\n // Unknown tools - treated as provider-executed\n return { name, input, executed: true }\n}\n","import type { LanguageModelV3 } from \"@ai-sdk/provider\"\nimport { log } from \"./logger.js\"\nimport type { ReasoningEffort } from \"./types.js\"\n\ntype Prompt = Parameters<LanguageModelV3[\"doGenerate\"]>[0][\"prompt\"]\n\nconst THINKING_KEYWORDS: Record<ReasoningEffort, string | null> = {\n minimal: null,\n low: \"think\",\n medium: \"think hard\",\n high: \"think harder\",\n xhigh: \"megathink\",\n max: \"ultrathink\",\n}\n\nexport function reasoningKeyword(effort?: ReasoningEffort): string | null {\n if (!effort) return null\n return THINKING_KEYWORDS[effort] ?? null\n}\n\nconst SUPPORTED_IMAGE_TYPES = new Set([\n \"image/jpeg\",\n \"image/png\",\n \"image/gif\",\n \"image/webp\",\n])\n\nfunction toImageBlock(part: any): any | null {\n const raw: unknown = part.data ?? part.url ?? part.source?.data\n if (!raw) {\n log.warn(\"file part without data, skipping\")\n return null\n }\n\n let resolvedMediaType: string = part.mediaType || part.mimeType || part.mime || \"\"\n let base64: string | null = null\n\n if (typeof raw === \"string\") {\n if (raw.startsWith(\"data:\")) {\n const match = /^data:([^;,]+)(?:;[^,]*)*(?:;base64)?,(.*)$/s.exec(raw)\n if (!match) {\n log.warn(\"malformed data URI, skipping file part\")\n return null\n }\n resolvedMediaType = resolvedMediaType || match[1]\n base64 = match[2]\n } else if (/^https?:\\/\\//i.test(raw)) {\n log.warn(\"remote URL images are not supported by Claude CLI, skipping\")\n return null\n } else {\n base64 = raw\n }\n } else if (raw instanceof URL) {\n log.warn(\"remote URL images are not supported by Claude CLI, skipping\")\n return null\n } else if (raw instanceof Uint8Array || Buffer.isBuffer(raw)) {\n base64 = Buffer.from(raw as Uint8Array).toString(\"base64\")\n } else {\n log.warn(\"unsupported file part data type\", { dataType: typeof raw })\n return null\n }\n\n if (!resolvedMediaType || !SUPPORTED_IMAGE_TYPES.has(resolvedMediaType)) {\n log.warn(\"unsupported media type for Claude image block, skipping\", {\n mediaType: resolvedMediaType,\n })\n return null\n }\n\n return {\n type: \"image\",\n source: { type: \"base64\", media_type: resolvedMediaType, data: base64 },\n }\n}\n\nfunction getToolResultText(part: any): string {\n const value = part.output ?? part.result\n\n if (typeof value === \"string\") {\n return value\n }\n\n if (!value || typeof value !== \"object\") {\n return JSON.stringify(value)\n }\n\n switch (value.type) {\n case \"text\":\n case \"error-text\":\n return String(value.value)\n case \"json\":\n case \"error-json\":\n return JSON.stringify(value.value)\n case \"execution-denied\":\n return value.reason ? `Execution denied: ${value.reason}` : \"Execution denied\"\n case \"content\":\n return Array.isArray(value.value)\n ? value.value\n .map((item: any) => {\n if (item?.type === \"text\") return item.text\n return JSON.stringify(item)\n })\n .join(\"\\n\")\n : JSON.stringify(value.value)\n default:\n return JSON.stringify(value)\n }\n}\n\n// Compaction-mode caps. These are the only knobs that affect how much\n// transcript content reaches the model when opencode invokes /compact.\n// 180k chars ≈ 60k tokens worst-case — well under Haiku 4.5's 200k window\n// after accounting for system prompt + output budget.\nconst MAX_HISTORY_CHARS = 180_000\nconst MAX_TOOL_RESULT_CHARS = 10_000\nconst MAX_TOOL_INPUT_CHARS = 2_000\n\nfunction clipWithMarker(text: string, max: number): string {\n if (text.length <= max) return text\n return `${text.slice(0, max)}\\n…[truncated ${text.length - max} chars]`\n}\n\nfunction renderToolInput(input: unknown): string {\n let raw: string\n try {\n raw = typeof input === \"string\" ? input : JSON.stringify(input)\n } catch {\n raw = String(input)\n }\n return clipWithMarker(raw, MAX_TOOL_INPUT_CHARS)\n}\n\nfunction renderMessageContentForCompaction(\n msg: any,\n): { text: string; toolResultCount: number } {\n const lines: string[] = []\n let toolResultCount = 0\n\n if (typeof msg.content === \"string\") {\n return { text: msg.content, toolResultCount: 0 }\n }\n\n if (!Array.isArray(msg.content)) {\n return { text: \"\", toolResultCount: 0 }\n }\n\n for (const part of msg.content as any[]) {\n if (!part) continue\n switch (part.type) {\n case \"text\":\n if (part.text) lines.push(part.text)\n break\n case \"tool-call\":\n lines.push(\n `[tool_use:${part.toolName ?? \"unknown\"}(${renderToolInput(part.input)})]`,\n )\n break\n case \"tool-result\":\n toolResultCount++\n lines.push(\n `[tool_result:${part.toolName ?? part.toolCallId ?? \"unknown\"}]\\n${clipWithMarker(\n getToolResultText(part),\n MAX_TOOL_RESULT_CHARS,\n )}`,\n )\n break\n case \"image\":\n lines.push(\n `[image: ${part.mediaType ?? part.mimeType ?? \"unknown\"}]`,\n )\n break\n case \"file\":\n lines.push(\n `[file: ${part.mediaType ?? part.mimeType ?? \"unknown\"}]`,\n )\n break\n case \"reasoning\":\n // Skip reasoning blocks in compaction — they bloat input without\n // helping the summarizer.\n break\n }\n }\n\n return { text: lines.join(\"\\n\"), toolResultCount }\n}\n\n/**\n * Compact conversation history into a context summary.\n *\n * - mode \"fresh-session\" (default): legacy behavior. Filters to\n * user/assistant only, clips each message at 2000 chars, drops tool\n * payloads to placeholders. Used when starting a fresh CLI session\n * that lost its prior session id.\n * - mode \"compaction\": rich serializer for opencode /compact. Includes\n * tool roles, renders tool_use input and tool_result content (each\n * clipped at MAX_TOOL_RESULT_CHARS), and caps aggregate output at\n * MAX_HISTORY_CHARS by dropping oldest entries first.\n */\nexport function compactConversationHistory(\n prompt: Prompt,\n opts: { mode?: \"fresh-session\" | \"compaction\" } = {},\n): string | null {\n const mode = opts.mode ?? \"fresh-session\"\n\n if (mode === \"compaction\") {\n return buildCompactionHistory(prompt)\n }\n\n const conversationMessages = prompt.filter(\n (m) => m.role === \"user\" || m.role === \"assistant\",\n )\n\n if (conversationMessages.length <= 1) {\n return null\n }\n\n const historyParts: string[] = []\n\n for (let i = 0; i < conversationMessages.length - 1; i++) {\n const msg = conversationMessages[i]\n const role = msg.role === \"user\" ? \"User\" : \"Assistant\"\n\n let text = \"\"\n if (typeof msg.content === \"string\") {\n text = msg.content\n } else if (Array.isArray(msg.content)) {\n const textParts = (msg.content as any[])\n .filter((p) => p.type === \"text\" && p.text)\n .map((p) => p.text)\n text = textParts.join(\"\\n\")\n\n const toolCalls = (msg.content as any[]).filter(\n (p) => p.type === \"tool-call\",\n )\n const toolResults = (msg.content as any[]).filter(\n (p) => p.type === \"tool-result\",\n )\n\n if (toolCalls.length > 0) {\n text += `\\n[Called ${toolCalls.length} tool(s): ${toolCalls.map((t: any) => t.toolName).join(\", \")}]`\n }\n if (toolResults.length > 0) {\n text += `\\n[Received ${toolResults.length} tool result(s)]`\n }\n }\n\n if (text.trim()) {\n const truncated =\n text.length > 2000 ? text.slice(0, 2000) + \"...\" : text\n historyParts.push(`${role}: ${truncated}`)\n }\n }\n\n if (historyParts.length === 0) {\n return null\n }\n\n return historyParts.join(\"\\n\\n\")\n}\n\nfunction buildCompactionHistory(prompt: Prompt): string | null {\n // Iterate newest-first, accumulate up to MAX_HISTORY_CHARS, then reverse\n // to chronological order. Oldest messages get dropped when the budget\n // is exhausted — they are the least relevant for a summary of recent\n // work.\n const entries: string[] = []\n let total = 0\n let totalToolResults = 0\n let droppedOldest = 0\n\n // Skip the trailing user message: opencode's /compact appends the\n // synthesis instruction as the final user turn. The instruction itself\n // is added by getClaudeUserMessage after the transcript block, so we\n // don't want it duplicated inside the transcript.\n const end = prompt.length > 0 && prompt[prompt.length - 1].role === \"user\"\n ? prompt.length - 1\n : prompt.length\n\n for (let i = end - 1; i >= 0; i--) {\n const msg = prompt[i] as any\n const roleLabel =\n msg.role === \"user\"\n ? \"User\"\n : msg.role === \"assistant\"\n ? \"Assistant\"\n : msg.role === \"tool\"\n ? \"Tool\"\n : msg.role\n\n const { text, toolResultCount } = renderMessageContentForCompaction(msg)\n if (!text.trim()) continue\n\n const entry = `${roleLabel}: ${text}`\n if (total + entry.length > MAX_HISTORY_CHARS) {\n droppedOldest = i + 1\n break\n }\n entries.push(entry)\n total += entry.length + 2 // +2 for the \"\\n\\n\" join\n totalToolResults += toolResultCount\n }\n\n if (entries.length === 0) return null\n\n entries.reverse()\n log.info(\"built compaction history\", {\n entries: entries.length,\n chars: total,\n toolResults: totalToolResults,\n droppedOldestBefore: droppedOldest,\n })\n\n return entries.join(\"\\n\\n\")\n}\n\n/**\n * Convert AI SDK prompt into a Claude CLI stream-json user message.\n *\n * `compactionMode` switches behavior for opencode /compact: the prior\n * transcript is rendered with rich tool content (not placeholders), the\n * wrapper framing tells the model this is the authoritative thread, and\n * the reasoning keyword is suppressed so the full output budget goes\n * toward the summary.\n */\nexport function getClaudeUserMessage(\n prompt: Prompt,\n includeHistoryContext: boolean = false,\n reasoningEffort?: ReasoningEffort,\n opts: { compactionMode?: boolean } = {},\n): string {\n const compactionMode = opts.compactionMode === true\n const content: any[] = []\n\n if (compactionMode) {\n const transcript = compactConversationHistory(prompt, {\n mode: \"compaction\",\n })\n if (transcript) {\n log.info(\"including compaction transcript\", {\n historyLength: transcript.length,\n })\n content.push({\n type: \"text\",\n text: `<conversation_transcript>\n${transcript}\n</conversation_transcript>\n\nThe complete prior conversation appears above. The synthesis instructions follow below.\n\n`,\n })\n }\n } else if (includeHistoryContext) {\n const historyContext = compactConversationHistory(prompt)\n if (historyContext) {\n log.info(\"including conversation history context\", {\n historyLength: historyContext.length,\n })\n content.push({\n type: \"text\",\n text: `<conversation_history>\nThe following is a summary of our conversation so far (from a previous session that couldn't be resumed):\n\n${historyContext}\n\n</conversation_history>\n\nNow continuing with the current message:\n\n`,\n })\n }\n }\n\n // Find messages since last assistant message\n const messages: typeof prompt = []\n for (let i = prompt.length - 1; i >= 0; i--) {\n if (prompt[i].role === \"assistant\") break\n messages.unshift(prompt[i])\n }\n\n for (const msg of messages) {\n if (msg.role === \"user\") {\n if (typeof msg.content === \"string\") {\n const str = msg.content as string\n if (str.trim()) {\n content.push({ type: \"text\", text: str })\n }\n } else if (Array.isArray(msg.content)) {\n for (const part of msg.content as any[]) {\n if (part.type === \"text\") {\n if (part.text && part.text.trim()) {\n content.push({ type: \"text\", text: part.text })\n }\n } else if (part.type === \"file\" || part.type === \"image\") {\n const block = toImageBlock(part)\n if (block) {\n content.push(block)\n } else {\n log.debug(\"skipped non-image file part\", {\n mediaType: part.mediaType,\n })\n }\n } else if (part.type === \"tool-result\") {\n const p = part as any\n content.push({\n type: \"tool_result\",\n tool_use_id: p.toolCallId,\n content: getToolResultText(p),\n })\n }\n }\n }\n } else if (msg.role === \"tool\") {\n // AI SDK V3 delivers tool results in `tool`-role messages, not `user`.\n // Without this branch we'd hit the empty-content sentinel path and\n // send \"(empty)\" to Claude CLI instead of the actual tool result —\n // forcing the user to press \"continue\" between proxy tool calls.\n if (Array.isArray(msg.content)) {\n for (const part of msg.content as any[]) {\n if (part?.type === \"tool-result\") {\n const p = part as any\n content.push({\n type: \"tool_result\",\n tool_use_id: p.toolCallId,\n content: getToolResultText(p),\n })\n }\n }\n }\n }\n }\n\n if (content.length === 0) {\n // CLI rejects a zero-block message with 400, and Anthropic rejects\n // whitespace-only text blocks — so we need a non-whitespace sentinel.\n // \"(empty)\" matches the parenthetical meta-note convention this file\n // already uses for reasoning keywords (\"(think)\", \"(megathink)\", etc.),\n // which the model reads as out-of-band metadata rather than a prompt to\n // continue its previous turn.\n log.warn(\"empty user content; sending sentinel to satisfy CLI\")\n return JSON.stringify({\n type: \"user\",\n message: {\n role: \"user\",\n content: [{ type: \"text\", text: \"(empty)\" }],\n },\n })\n }\n\n // Reasoning keyword is a Claude CLI hint that triggers extended thinking.\n // For compaction we want the full output budget to go to the summary\n // itself, not internal reasoning — so skip injection.\n if (!compactionMode) {\n const keyword = reasoningKeyword(reasoningEffort)\n if (keyword) {\n const lastTextPart = [...content].reverse().find((p) => p.type === \"text\")\n if (lastTextPart) {\n lastTextPart.text = lastTextPart.text\n ? `${lastTextPart.text}\\n\\n(${keyword})`\n : `(${keyword})`\n } else {\n content.push({ type: \"text\", text: `(${keyword})` })\n }\n log.debug(\"injected reasoning keyword\", { effort: reasoningEffort, keyword })\n }\n }\n\n return JSON.stringify({\n type: \"user\",\n message: {\n role: \"user\",\n content,\n },\n })\n}\n","import * as fs from \"node:fs\"\nimport * as path from \"node:path\"\nimport * as os from \"node:os\"\nimport * as crypto from \"node:crypto\"\nimport { log } from \"./logger.js\"\nimport { pluginTmpDir } from \"./tmp.js\"\n\n/**\n * Bridge opencode's `mcp` config block into a Claude CLI `--mcp-config` file.\n *\n * Opencode core schema (packages/opencode/src/config/mcp.ts):\n * {\n * \"mcp\": {\n * \"name\": {\n * \"type\": \"local\" | \"remote\",\n * \"command\"?: string[], // local\n * \"environment\"?: Record<string,string>,\n * \"url\"?: string, // remote\n * \"headers\"?: Record<string,string>,\n * \"oauth\"?: object | false, // remote — NOT bridged (Claude --mcp-config has no slot)\n * \"timeout\"?: number, // NOT bridged (Claude --mcp-config has no slot)\n * \"enabled\"?: boolean\n * }\n * }\n * }\n *\n * Claude CLI `--mcp-config` schema:\n * {\n * \"mcpServers\": {\n * \"name\": {\n * \"type\": \"stdio\" | \"http\",\n * \"command\"?: string, \"args\"?: string[], \"env\"?: Record<string,string>,\n * \"url\"?: string, \"headers\"?: Record<string,string>\n * }\n * }\n * }\n *\n * Discovery + merge are aligned with opencode core's `loadInstanceState`\n * (packages/opencode/src/config/config.ts). In merge order (last wins),\n * opencode loads:\n *\n * 1. Auth `.well-known` remote configs ← NOT bridged\n * 2. Global: ~/.config/opencode/{config.json,opencode.json,opencode.jsonc}\n * — all three deep-merged, jsonc highest priority\n * 3. OPENCODE_CONFIG env var (single file)\n * 4. Project walk-up: opencode.json[c] in each dir from cwd up to (not past)\n * worktree, both extensions per dir, parent-most first\n * 5. .opencode/ siblings: from cwd up + home dir + OPENCODE_CONFIG_DIR,\n * both extensions per dir, opencode-iteration order (cwd-most first\n * in walk-up — so parent-most `.opencode/` wins, matching upstream)\n * 6. OPENCODE_CONFIG_CONTENT env var (inline JSON) ← NOT bridged\n * 7. Active org remote config ← NOT bridged\n * 8. Managed config dir / macOS MDM ← NOT bridged\n *\n * Sources marked NOT bridged are niche and would require live opencode\n * runtime state (auth tokens, account context, MDM access). Document them\n * here so the gap is explicit; functionality of the common path is intact.\n *\n * Per-server merge is deep-merge (matching opencode's `mergeConfigConcatArrays`\n * → `mergeDeep`), so a project layer can override one field of a global server\n * spec — e.g. `{ \"linear\": { \"enabled\": true } }` lifts global linear's URL.\n */\n\nconst FILE_NAMES = [\"opencode.jsonc\", \"opencode.json\", \"config.json\"] as const\nconst PROJECT_FILE_NAMES = [\"opencode.json\", \"opencode.jsonc\"] as const\n\nfunction fileExists(p: string): boolean {\n try {\n return fs.statSync(p).isFile()\n } catch {\n return false\n }\n}\n\nfunction dirExists(p: string): boolean {\n try {\n return fs.statSync(p).isDirectory()\n } catch {\n return false\n }\n}\n\n/** Strip `//` and `/* *\\/` comments so JSONC parses via JSON.parse. */\nfunction stripJsonComments(text: string): string {\n let out = \"\"\n let i = 0\n let inString: string | null = null\n while (i < text.length) {\n const c = text[i]\n if (inString) {\n out += c\n if (c === \"\\\\\" && i + 1 < text.length) {\n out += text[i + 1]\n i += 2\n continue\n }\n if (c === inString) inString = null\n i++\n continue\n }\n if (c === '\"' || c === \"'\") {\n inString = c\n out += c\n i++\n continue\n }\n if (c === \"/\" && text[i + 1] === \"/\") {\n while (i < text.length && text[i] !== \"\\n\") i++\n continue\n }\n if (c === \"/\" && text[i + 1] === \"*\") {\n i += 2\n while (\n i < text.length &&\n !(text[i] === \"*\" && text[i + 1] === \"/\")\n )\n i++\n i += 2\n continue\n }\n out += c\n i++\n }\n return out\n}\n\nfunction readAndParse(file: string): Record<string, unknown> | null {\n try {\n const raw = fs.readFileSync(file, \"utf8\")\n return JSON.parse(stripJsonComments(raw)) as Record<string, unknown>\n } catch (e) {\n log.warn(\"failed to parse opencode config\", {\n file,\n error: e instanceof Error ? e.message : String(e),\n })\n return null\n }\n}\n\n/**\n * Deep merge two plain-object trees. Arrays and primitives are replaced\n * (not concatenated). Matches the effective behavior of opencode's\n * `mergeDeep` from `remeda` for the MCP block — opencode does not special\n * case array fields inside `mcp.<server>` (its only special case is\n * `instructions`, which is concat-deduped at the config root).\n */\nfunction isPlainObject(x: unknown): x is Record<string, unknown> {\n return typeof x === \"object\" && x !== null && !Array.isArray(x)\n}\n\nfunction deepMerge(\n target: Record<string, unknown>,\n source: Record<string, unknown>,\n): Record<string, unknown> {\n const out: Record<string, unknown> = { ...target }\n for (const [k, v] of Object.entries(source)) {\n if (v === undefined) continue\n const existing = out[k]\n if (isPlainObject(existing) && isPlainObject(v)) {\n out[k] = deepMerge(existing, v)\n } else {\n out[k] = v\n }\n }\n return out\n}\n\n/**\n * Walk up from `start` toward filesystem root (or `stop` if provided),\n * collecting paths where each `target` exists. Mirrors opencode core's\n * `FileSystem.up` (packages/core/src/filesystem.ts): cwd-most first,\n * parent-most last.\n */\nfunction walkUp(opts: {\n start: string\n stop?: string\n targets: readonly string[]\n predicate: (p: string) => boolean\n}): string[] {\n const out: string[] = []\n let current = path.resolve(opts.start)\n while (true) {\n for (const target of opts.targets) {\n const candidate = path.join(current, target)\n if (opts.predicate(candidate)) out.push(candidate)\n }\n if (opts.stop && current === path.resolve(opts.stop)) break\n const parent = path.dirname(current)\n if (parent === current) break\n current = parent\n }\n return out\n}\n\n/**\n * Find the worktree root by walking up from `cwd` looking for a `.git`\n * entry (file or directory — submodules use a file). If no `.git` is\n * found, walk to filesystem root. Honors OPENCODE_WORKTREE override.\n */\nfunction detectWorktree(cwd: string): string | undefined {\n const override = process.env.OPENCODE_WORKTREE\n if (override) return path.resolve(override)\n let current = path.resolve(cwd)\n while (true) {\n const gitPath = path.join(current, \".git\")\n try {\n if (fs.existsSync(gitPath)) return current\n } catch {\n // ignore\n }\n const parent = path.dirname(current)\n if (parent === current) return undefined\n current = parent\n }\n}\n\nfunction globalConfigDir(): string {\n const xdg = process.env.XDG_CONFIG_HOME ?? path.join(os.homedir(), \".config\")\n return path.join(xdg, \"opencode\")\n}\n\n/**\n * Load the merged global config from `~/.config/opencode/`. Mirrors\n * opencode core's `loadGlobal`: deep-merges config.json → opencode.json\n * → opencode.jsonc in that order (jsonc wins).\n */\nfunction loadGlobalConfig(): Record<string, unknown> {\n const dir = globalConfigDir()\n let merged: Record<string, unknown> = {}\n for (const name of FILE_NAMES.slice().reverse()) {\n // FILE_NAMES is jsonc-first; reverse to get config.json-first order.\n const file = path.join(dir, name)\n if (!fileExists(file)) continue\n const parsed = readAndParse(file)\n if (parsed) merged = deepMerge(merged, parsed)\n }\n return merged\n}\n\n/** Load both `opencode.json` and `opencode.jsonc` in `dir`, deep-merged. */\nfunction loadProjectFilesInDir(dir: string): Record<string, unknown> {\n let merged: Record<string, unknown> = {}\n for (const name of PROJECT_FILE_NAMES) {\n const file = path.join(dir, name)\n if (!fileExists(file)) continue\n const parsed = readAndParse(file)\n if (parsed) merged = deepMerge(merged, parsed)\n }\n return merged\n}\n\n/**\n * Build the list of `.opencode/` directories to consider, in opencode core's\n * order (matching `ConfigPaths.directories`):\n * project walk-up (cwd-most first) → home-dir `.opencode/` → OPENCODE_CONFIG_DIR\n */\nfunction dotOpencodeDirs(cwd: string, worktree?: string): string[] {\n const dirs: string[] = []\n const seen = new Set<string>()\n const push = (p: string) => {\n const abs = path.resolve(p)\n if (!seen.has(abs) && dirExists(abs)) {\n seen.add(abs)\n dirs.push(abs)\n }\n }\n\n for (const dir of walkUp({\n start: cwd,\n stop: worktree,\n targets: [\".opencode\"],\n predicate: dirExists,\n })) {\n push(dir)\n }\n\n const home = os.homedir()\n if (home) {\n const homeDot = path.join(home, \".opencode\")\n if (dirExists(homeDot)) push(homeDot)\n }\n\n const envDir = process.env.OPENCODE_CONFIG_DIR\n if (envDir && dirExists(envDir)) push(envDir)\n\n return dirs\n}\n\ninterface OpencodeLocalServer {\n type?: \"local\"\n command?: string[]\n environment?: Record<string, string>\n enabled?: boolean\n}\n\ninterface OpencodeRemoteServer {\n type?: \"remote\"\n url?: string\n headers?: Record<string, string>\n enabled?: boolean\n}\n\ntype OpencodeServer = OpencodeLocalServer | OpencodeRemoteServer | { enabled?: boolean }\n\n/**\n * Substitute opencode's `{env:VAR}` interpolation in a string-keyed record\n * using values from `process.env`. Returns a new object. If the source is\n * not a flat string-valued record, returns it unchanged.\n *\n * Opencode performs this substitution itself when it spawns MCP servers\n * directly, but the spec we read from disk still contains the literal\n * placeholders. Without substituting them here, Claude CLI hands the\n * literal string `{env:FOO}` to the MCP subprocess as the env value, and\n * any server that validates credentials at startup (e.g. slack-mcp-server)\n * crashes before exposing tools. Servers that defer validation to\n * request time (e.g. github-mcp-server) appear to register but every API\n * call 401s.\n */\nfunction substituteEnvPlaceholders(\n source: Record<string, unknown>,\n): Record<string, string> {\n const out: Record<string, string> = {}\n for (const [k, v] of Object.entries(source)) {\n if (typeof v !== \"string\") continue\n out[k] = v.replace(/\\{env:([A-Za-z_][A-Za-z0-9_]*)\\}/g, (_match, name) => {\n const resolved = process.env[name]\n return typeof resolved === \"string\" ? resolved : \"\"\n })\n }\n return out\n}\n\nfunction translateServer(\n name: string,\n spec: Record<string, unknown>,\n): Record<string, unknown> | null {\n if (spec.enabled === false) return null\n\n const type = spec.type\n if (type === \"local\") {\n const cmd = spec.command\n if (!Array.isArray(cmd) || cmd.length === 0) {\n log.warn(\"skipping local MCP server with no command\", { name })\n return null\n }\n const out: Record<string, unknown> = {\n type: \"stdio\",\n command: String(cmd[0]),\n }\n if (cmd.length > 1) out.args = cmd.slice(1).map((s) => String(s))\n if (spec.environment && typeof spec.environment === \"object\") {\n out.env = substituteEnvPlaceholders(\n spec.environment as Record<string, unknown>,\n )\n }\n return out\n }\n\n if (type === \"remote\") {\n if (typeof spec.url !== \"string\" || !spec.url) {\n log.warn(\"skipping remote MCP server with no url\", { name })\n return null\n }\n const out: Record<string, unknown> = {\n type: \"http\",\n url: spec.url,\n }\n if (spec.headers && typeof spec.headers === \"object\") {\n out.headers = substituteEnvPlaceholders(\n spec.headers as Record<string, unknown>,\n )\n }\n return out\n }\n\n log.warn(\"skipping MCP server with unknown type\", {\n name,\n type: type ?? null,\n })\n return null\n}\n\nfunction extractMcpBlock(\n config: Record<string, unknown>,\n): Record<string, OpencodeServer> {\n const mcp = config.mcp\n if (!mcp || typeof mcp !== \"object\" || Array.isArray(mcp)) return {}\n return mcp as Record<string, OpencodeServer>\n}\n\n/**\n * Deep-merge per-server specs from `source` into `target`. Mirrors opencode's\n * `mergeDeep` semantics for the `mcp` record: each server entry is recursively\n * merged so a partial layer (e.g. `{ \"linear\": { \"enabled\": true } }`) can\n * override one field without dropping the rest.\n */\nfunction mergeMcp(\n target: Record<string, OpencodeServer>,\n source: Record<string, OpencodeServer>,\n): Record<string, OpencodeServer> {\n const out: Record<string, OpencodeServer> = { ...target }\n for (const [name, spec] of Object.entries(source)) {\n if (!spec || typeof spec !== \"object\") continue\n const existing = out[name]\n if (existing && typeof existing === \"object\") {\n out[name] = deepMerge(\n existing as Record<string, unknown>,\n spec as Record<string, unknown>,\n ) as OpencodeServer\n } else {\n out[name] = spec\n }\n }\n return out\n}\n\nexport interface BridgedMcp {\n /** Path to the temp file containing the translated `--mcp-config`. */\n path: string\n /** Stable hash of the merged opencode mcp block (pre-translation). */\n hash: string\n /**\n * Names of opencode MCP servers that were bridged into Claude CLI's\n * `--mcp-config`. Excludes any servers passed in `excludeServers`.\n */\n serverNames: string[]\n /**\n * Names of every enabled opencode MCP server after merge + runtime\n * overlay, regardless of whether they ended up bridged or excluded.\n * Callers (e.g. the proxy-tool builder) use this to decide which\n * `<server>_<tool>` IDs in opencode's tool catalog are MCP-origin.\n */\n allEnabledServerNames: string[]\n}\n\n/** Result of merging opencode's MCP config layers + applying runtime overlay. */\nexport interface MergedMcp {\n /** Server names whose final spec is enabled (or implicitly enabled). */\n enabledServerNames: string[]\n /** Stable hash of the merged (pre-translation) MCP block. */\n hash: string\n}\n\n/**\n * Per-server runtime status from opencode's `client.mcp.status()`. Used as\n * an overlay on top of the on-disk merged config so opencode's UI-toggled\n * state — which lives only in-memory; `connect()`/`disconnect()` never\n * touch disk — propagates to the bridged claude subprocess.\n *\n * Treatment per server:\n * - \"connected\" → force `enabled: true` (mirror opencode)\n * - any other status → force `enabled: false` (don't ship a server\n * opencode can't run; user fixes it in opencode first)\n * - missing entry → leave disk value\n *\n * Omit the overlay and the bridge falls back to disk-only.\n */\nexport type RuntimeMcpStatus = Record<string, string>\n\n/**\n * Read opencode config layers, deep-merge their `mcp` blocks per opencode's\n * own semantics, optionally apply an opencode runtime-status overlay, then\n * translate each server to Claude CLI format, write a scratch file, and\n * return its path + a stable hash. Returns null when no enabled MCP servers\n * remain after the merge + overlay.\n */\nexport function bridgeOpencodeMcp(\n cwd: string,\n runtimeStatus?: RuntimeMcpStatus,\n excludeServers?: ReadonlySet<string>,\n): BridgedMcp | null {\n const worktree = detectWorktree(cwd)\n\n // Layer 1: global merged\n let merged: Record<string, OpencodeServer> = {}\n merged = mergeMcp(merged, extractMcpBlock(loadGlobalConfig()))\n\n // Layer 2: OPENCODE_CONFIG (single file, applied before project walk-up)\n const explicitConfig = process.env.OPENCODE_CONFIG\n if (explicitConfig && fileExists(explicitConfig)) {\n const parsed = readAndParse(explicitConfig)\n if (parsed) merged = mergeMcp(merged, extractMcpBlock(parsed))\n }\n\n // Layer 3: project walk-up — opencode.json[c] in each dir from cwd to\n // (not past) worktree, both extensions per dir. walkUp returns cwd-most\n // first; collect distinct dirs in that order then reverse for merge so\n // cwd-most wins under last-merge-wins.\n const projectFiles = walkUp({\n start: cwd,\n stop: worktree,\n targets: PROJECT_FILE_NAMES,\n predicate: fileExists,\n })\n const projectDirs: string[] = []\n const seenProjectDirs = new Set<string>()\n for (const f of projectFiles) {\n const d = path.dirname(f)\n if (!seenProjectDirs.has(d)) {\n seenProjectDirs.add(d)\n projectDirs.push(d)\n }\n }\n for (const dir of projectDirs.slice().reverse()) {\n merged = mergeMcp(merged, extractMcpBlock(loadProjectFilesInDir(dir)))\n }\n\n // Layer 4: `.opencode/` siblings — project walk-up then home-dir then\n // OPENCODE_CONFIG_DIR, in that order. Iteration order matches opencode's\n // (cwd-most first within walk-up), so under deep-merge \"later wins\"\n // parent-most `.opencode/` overrides cwd-most. This is upstream's\n // behavior, surprising though it is.\n for (const dir of dotOpencodeDirs(cwd, worktree)) {\n merged = mergeMcp(merged, extractMcpBlock(loadProjectFilesInDir(dir)))\n }\n\n // Layer 5: opencode runtime overlay. opencode's `/mcps` UI toggle calls\n // `mcp.connect()` / `mcp.disconnect()` which only mutate in-memory state,\n // never the on-disk config. Without this overlay the bridge can't see\n // those toggles and claude misses servers the user just enabled.\n if (runtimeStatus) {\n for (const name of Object.keys(merged)) {\n const status = runtimeStatus[name]\n if (status === undefined) continue\n const existing = merged[name]\n const base =\n existing && typeof existing === \"object\"\n ? (existing as Record<string, unknown>)\n : {}\n merged[name] = { ...base, enabled: status === \"connected\" } as OpencodeServer\n }\n }\n\n // Compute the set of enabled server names BEFORE exclusion so callers can\n // tell whether a tool ID like `slack_conversations_add_message` came from\n // an opencode MCP server (vs a built-in tool that happens to contain `_`).\n const allEnabledServerNames: string[] = []\n for (const [name, spec] of Object.entries(merged)) {\n if (!spec || typeof spec !== \"object\") continue\n const enabled = (spec as { enabled?: unknown }).enabled\n if (enabled === false) continue\n allEnabledServerNames.push(name)\n }\n\n // Translate every still-enabled server, skipping any caller has asked us\n // to exclude (because they're being routed through the proxy instead).\n const servers: Record<string, unknown> = {}\n const bridgedServerNames: string[] = []\n for (const [name, spec] of Object.entries(merged)) {\n if (!spec || typeof spec !== \"object\") continue\n if (excludeServers?.has(name)) continue\n const translated = translateServer(name, spec as Record<string, unknown>)\n if (translated) {\n servers[name] = translated\n bridgedServerNames.push(name)\n }\n }\n\n // Hash the pre-exclusion merged block so the hot-reload detector picks up\n // upstream config changes even when every server is excluded.\n const mergedBody = JSON.stringify({ mcpServers: merged }, null, 2)\n const hash = crypto\n .createHash(\"sha256\")\n .update(mergedBody)\n .digest(\"hex\")\n .slice(0, 12)\n\n if (Object.keys(servers).length === 0) {\n const allEnabledServersExcluded =\n excludeServers &&\n allEnabledServerNames.length > 0 &&\n allEnabledServerNames.every((name) => excludeServers.has(name))\n\n if (!allEnabledServersExcluded) return null\n\n return {\n path: \"\",\n hash,\n serverNames: [],\n allEnabledServerNames,\n }\n }\n\n const body = JSON.stringify({ mcpServers: servers }, null, 2)\n const outPath = path.join(\n pluginTmpDir(),\n `mcp-${hash}.json`,\n )\n try {\n if (!fileExists(outPath)) {\n fs.writeFileSync(outPath, body, { encoding: \"utf8\", mode: 0o600 })\n }\n } catch (e) {\n log.warn(\"failed to write bridged MCP config\", {\n error: e instanceof Error ? e.message : String(e),\n })\n return null\n }\n\n log.info(\"bridged opencode MCP config\", {\n target: outPath,\n hash,\n servers: bridgedServerNames,\n excluded: excludeServers ? Array.from(excludeServers) : [],\n })\n return {\n path: outPath,\n hash,\n serverNames: bridgedServerNames,\n allEnabledServerNames,\n }\n}\n\n// Internal helpers exported for tests only.\nexport const __test = {\n deepMerge,\n mergeMcp,\n translateServer,\n substituteEnvPlaceholders,\n detectWorktree,\n loadGlobalConfig,\n loadProjectFilesInDir,\n dotOpencodeDirs,\n}\n","import * as fs from \"node:fs\"\nimport * as os from \"node:os\"\nimport * as path from \"node:path\"\n\n/**\n * Per-process scratch directory for plugin tmp files (bridged MCP config,\n * proxy server config, etc.). Created lazily on first use and rm'd on\n * normal process exit so we don't leak across runs. PID-isolated so two\n * concurrent opencode processes don't race on the same files.\n *\n * Caveat: `process.on(\"exit\")` does not fire for SIGKILL or unhandled\n * external signals, so abnormal terminations still leak. OS-level tmpdir\n * cleanup (`systemd-tmpfiles`, macOS periodic) handles those eventually.\n */\nconst PLUGIN_TMP_DIR = path.join(\n os.tmpdir(),\n `opencode-claude-code-${process.pid}`,\n)\n\nlet registered = false\n\nexport function pluginTmpDir(): string {\n if (!fs.existsSync(PLUGIN_TMP_DIR)) {\n fs.mkdirSync(PLUGIN_TMP_DIR, { recursive: true })\n }\n if (!registered) {\n registered = true\n process.on(\"exit\", () => {\n try {\n fs.rmSync(PLUGIN_TMP_DIR, { recursive: true, force: true })\n } catch {}\n })\n }\n return PLUGIN_TMP_DIR\n}\n","import type { RuntimeMcpStatus } from \"./mcp-bridge.js\"\nimport { log } from \"./logger.js\"\n\n/**\n * Captured opencode runtime context (SDK client + project directory) from\n * `PluginInput`. Lives in its own module to break the cycle that would\n * otherwise form between `index.ts` and `claude-code-language-model.ts`.\n * Values are `null`/`undefined` until the plugin's `server` factory runs\n * (e.g. early provider lookups, direct AI-SDK use, tests).\n */\ntype OpencodeClient = {\n mcp?: {\n status?: () => Promise<{ data?: unknown; error?: unknown }>\n }\n tool?: {\n list?: (options: {\n query: { provider: string; model: string; directory?: string }\n }) => Promise<{ data?: unknown; error?: unknown }>\n }\n}\n\nlet opencodeClient: OpencodeClient | null = null\n\nexport function setOpencodeClient(client: unknown): void {\n if (client && typeof client === \"object\") {\n opencodeClient = client as OpencodeClient\n }\n}\n\n/**\n * Captured opencode project directory from `PluginInput.directory` (with\n * `worktree` as secondary signal). Used as a *fallback* at Claude CLI\n * spawn time only when `process.cwd()` is unusable (macOS GUI launches\n * where launchd hands the process `cwd=/`).\n *\n * IMPORTANT: never bake this into provider config (`mergedOptions.cwd`).\n * Doing so freezes the value at plugin init and breaks workspace\n * switching mid-session, because subsequent workspace changes in\n * opencode's UI never get reflected in `this.config.cwd`. See issue #4.\n */\nlet opencodeProjectDirectory: string | undefined\n\nexport function setOpencodeProjectDirectory(dir: string | undefined): void {\n opencodeProjectDirectory = dir\n}\n\nexport function getOpencodeProjectDirectory(): string | undefined {\n return opencodeProjectDirectory\n}\n\nexport function isUsableDirectory(d: unknown): d is string {\n return typeof d === \"string\" && d.length > 1 && d !== \"/\"\n}\n\n/**\n * Resolve the cwd for a Claude CLI subprocess spawn. Priority:\n *\n * 1. Explicit `configured` value (`options.cwd` from `opencode.json`).\n * Users who pinned a directory keep their override unconditionally.\n * 2. Live `process.cwd()` when it's a real directory. Restores the lazy\n * resolution that lets opencode's project-aware behavior (chdir on\n * workspace switch, project-per-shell on terminal launch) flow\n * through without restarting the plugin.\n * 3. Captured project directory from plugin init. Rescues macOS GUI\n * launches where `process.cwd()` is `/`.\n * 4. Final fallback to `process.cwd()` (returns `/` in the pathological\n * case where neither override nor capture is available).\n */\nexport function resolveSpawnCwd(configured: string | undefined): string {\n return resolveSpawnCwdFrom(\n configured,\n process.cwd(),\n opencodeProjectDirectory,\n )\n}\n\nexport function resolveSpawnCwdFrom(\n configured: string | undefined,\n live: string,\n captured: string | undefined,\n): string {\n if (configured) return configured\n if (isUsableDirectory(live)) return live\n return captured ?? live\n}\n\n/**\n * Snapshot opencode's current MCP runtime status so the bridge can overlay\n * UI-toggled state on top of disk config. Returns `undefined` on any\n * failure (no client captured, status call rejected, malformed response)\n * so the bridge falls back to disk-only.\n */\nexport async function getRuntimeMcpStatus(): Promise<\n RuntimeMcpStatus | undefined\n> {\n const client = opencodeClient\n if (!client?.mcp?.status) return undefined\n try {\n const res = await client.mcp.status()\n const data = (res as { data?: unknown }).data\n if (!data || typeof data !== \"object\") return undefined\n const out: RuntimeMcpStatus = {}\n for (const [name, entry] of Object.entries(data as Record<string, unknown>)) {\n if (entry && typeof entry === \"object\") {\n const status = (entry as { status?: unknown }).status\n if (typeof status === \"string\") out[name] = status\n }\n }\n return out\n } catch (err) {\n log.warn(\"failed to fetch opencode MCP runtime status\", {\n error: err instanceof Error ? err.message : String(err),\n })\n return undefined\n }\n}\n\nexport interface OpencodeToolListItem {\n id: string\n description: string\n parameters: Record<string, unknown>\n}\n\n/**\n * Fetch opencode's full tool catalog (built-ins + MCP-bridged) with JSON\n * Schema parameters via `client.tool.list()`. The provider/model query\n * narrows the schema variants opencode returns; in practice MCP-origin\n * tool schemas are model-agnostic, so any registered (provider, model)\n * works as the query target. Returns `undefined` on any failure so callers\n * can fall back to direct-bridge behavior.\n */\nexport async function fetchOpencodeToolList(\n provider: string,\n model: string,\n directory?: string,\n): Promise<OpencodeToolListItem[] | undefined> {\n const client = opencodeClient\n if (!client?.tool?.list) return undefined\n try {\n const res = await client.tool.list({\n query: { provider, model, ...(directory ? { directory } : {}) },\n })\n const data = (res as { data?: unknown }).data\n if (!Array.isArray(data)) return undefined\n const out: OpencodeToolListItem[] = []\n for (const entry of data as unknown[]) {\n if (!entry || typeof entry !== \"object\") continue\n const e = entry as Record<string, unknown>\n const id = typeof e.id === \"string\" ? e.id : null\n const description =\n typeof e.description === \"string\" ? e.description : \"\"\n const parameters =\n e.parameters && typeof e.parameters === \"object\"\n ? (e.parameters as Record<string, unknown>)\n : {}\n if (!id) continue\n out.push({ id, description, parameters })\n }\n return out\n } catch (err) {\n log.warn(\"failed to fetch opencode tool list\", {\n provider,\n model,\n error: err instanceof Error ? err.message : String(err),\n })\n return undefined\n }\n}\n","import { spawn, type ChildProcess } from \"node:child_process\"\nimport { createInterface } from \"node:readline\"\nimport { EventEmitter } from \"node:events\"\nimport { unlink } from \"node:fs/promises\"\nimport { log } from \"./logger.js\"\nimport type { ProxyMcpServer } from \"./proxy-mcp.js\"\nimport { clearLedger } from \"./todo-ledger.js\"\nimport {\n cliSupportsThinking,\n cliSupportsThinkingDisplay,\n type CliVersion,\n} from \"./cli-version.js\"\n\nexport interface ActiveProcess {\n proc: ChildProcess\n lineEmitter: EventEmitter\n proxyServer?: ProxyMcpServer | null\n /**\n * Hash of the bridged opencode MCP config the process was spawned with.\n * `null` when the bridge produced nothing (no MCP servers). `undefined`\n * when the bridge was disabled. Used to detect mid-session config drift\n * and force a respawn.\n */\n mcpHash?: string | null\n /** Temp file holding `--append-system-prompt-file` content; unlinked on exit. */\n systemPromptFile?: string\n}\n\n// One active CLI process per session key. Keyed by a composite\n// (cwd + model + opencode session-affinity) so two chats don't race.\n// Iteration order is insertion order, which we refresh on access to\n// make this a poor-man's LRU; see `touch()` below.\nconst activeProcesses = new Map<string, ActiveProcess>()\nconst claudeSessions = new Map<string, string>()\n\n// Cap on live CLI subprocesses. Session-affinity-keyed entries accumulate\n// one-per-chat, so an unbounded map would leak processes as users open new\n// chats. This caps at a reasonable working-set and evicts the oldest.\nconst MAX_ACTIVE_PROCESSES = 16\n\nfunction envFlagEnabled(value: string | undefined): boolean {\n if (value === undefined) return false\n const normalized = value.trim().toLowerCase()\n if (!normalized) return false\n return ![\"0\", \"false\", \"no\", \"off\"].includes(normalized)\n}\n\nexport function isClaudeThinkingDisabled(): boolean {\n return (\n envFlagEnabled(process.env.CLAUDE_CODE_DISABLE_THINKING) ||\n envFlagEnabled(process.env.CLAUDE_CODE_DISABLE_ADAPTIVE_THINKING)\n )\n}\n\nexport function claudeSpawnEnv(): Record<string, string | undefined> {\n const env: Record<string, string | undefined> = {\n ...process.env,\n TERM: \"xterm-256color\",\n }\n\n // Default-on thinking summaries for opus-4-7 (which omits thinking by\n // default on the CLI side). Any var the user has explicitly set in their\n // shell is passed through untouched; the plugin only fills in the default.\n if (\n !isClaudeThinkingDisabled() &&\n process.env.CLAUDE_CODE_SHOW_THINKING_SUMMARIES === undefined\n ) {\n env.CLAUDE_CODE_SHOW_THINKING_SUMMARIES = \"1\"\n }\n\n return env\n}\n\nfunction touch(key: string): void {\n const existing = activeProcesses.get(key)\n if (existing) {\n activeProcesses.delete(key)\n activeProcesses.set(key, existing)\n }\n}\n\nfunction evictIfNeeded(): void {\n while (activeProcesses.size >= MAX_ACTIVE_PROCESSES) {\n const oldestKey = activeProcesses.keys().next().value\n if (!oldestKey) break\n log.info(\"evicting LRU claude process\", { sessionKey: oldestKey })\n deleteActiveProcess(oldestKey)\n }\n}\n\nexport function getActiveProcess(key: string): ActiveProcess | undefined {\n const ap = activeProcesses.get(key)\n if (ap) touch(key)\n return ap\n}\n\nexport function setActiveProcess(key: string, ap: ActiveProcess): void {\n activeProcesses.set(key, ap)\n}\n\nexport function deleteActiveProcess(key: string): void {\n const ap = activeProcesses.get(key)\n if (ap) {\n void ap.proxyServer?.close()\n ap.proc.kill()\n activeProcesses.delete(key)\n }\n}\n\nexport function getClaudeSessionId(key: string): string | undefined {\n return claudeSessions.get(key)\n}\n\nexport function setClaudeSessionId(key: string, sessionId: string): void {\n claudeSessions.set(key, sessionId)\n}\n\nexport function deleteClaudeSessionId(key: string): void {\n const claudeSessionId = claudeSessions.get(key)\n if (claudeSessionId) clearLedger(claudeSessionId)\n claudeSessions.delete(key)\n}\n\nexport function spawnClaudeProcess(\n cliPath: string,\n cliArgs: string[],\n cwd: string,\n sessionKey: string,\n proxyServer?: ProxyMcpServer | null,\n mcpHash?: string | null,\n systemPromptFile?: string,\n): ActiveProcess {\n evictIfNeeded()\n log.info(\"spawning new claude process\", { cliPath, cliArgs, cwd, sessionKey })\n\n const proc = spawn(cliPath, cliArgs, {\n cwd,\n stdio: [\"pipe\", \"pipe\", \"pipe\"],\n env: claudeSpawnEnv(),\n shell: process.platform === \"win32\",\n })\n\n const lineEmitter = new EventEmitter()\n\n const rl = createInterface({ input: proc.stdout! })\n rl.on(\"line\", (line: string) => {\n lineEmitter.emit(\"line\", line)\n })\n rl.on(\"close\", () => {\n lineEmitter.emit(\"close\")\n })\n\n const ap: ActiveProcess = {\n proc,\n lineEmitter,\n proxyServer: proxyServer ?? null,\n mcpHash,\n systemPromptFile,\n }\n activeProcesses.set(sessionKey, ap)\n\n // Baseline 'error' listener so Node doesn't throw when the process emits\n // an error between stream turns (no per-stream listener attached then).\n proc.on(\"error\", (err) => {\n log.error(\"claude process error\", { sessionKey, error: err.message })\n })\n\n proc.on(\"exit\", (code, signal) => {\n log.info(\"claude process exited\", { code, signal, sessionKey })\n void proxyServer?.close()\n if (systemPromptFile) {\n void unlink(systemPromptFile).catch(() => {})\n }\n activeProcesses.delete(sessionKey)\n if (code !== 0 && code !== null) {\n log.info(\"process exited with error, clearing session\", {\n code,\n sessionKey,\n })\n claudeSessions.delete(sessionKey)\n }\n })\n\n proc.stderr?.on(\"data\", (data: Buffer) => {\n const stderr = data.toString()\n log.debug(\"stderr\", { data: stderr.slice(0, 200) })\n\n if (\n stderr.includes(\"Session ID\") &&\n (stderr.includes(\"already in use\") ||\n stderr.includes(\"not found\") ||\n stderr.includes(\"invalid\"))\n ) {\n log.warn(\"claude session ID error, clearing session\", {\n sessionKey,\n error: stderr.slice(0, 200),\n })\n claudeSessions.delete(sessionKey)\n }\n })\n\n return ap\n}\n\nexport function buildCliArgs(opts: {\n sessionKey: string\n skipPermissions: boolean\n includeSessionId?: boolean\n model?: string\n permissionMode?: string\n mcpConfig?: string | string[]\n strictMcpConfig?: boolean\n disallowedTools?: string[]\n appendSystemPromptFile?: string\n thinking?: \"enabled\" | \"disabled\"\n thinkingDisplay?: \"summarized\" | \"omitted\"\n cliVersion?: CliVersion | null\n}): string[] {\n const {\n sessionKey,\n skipPermissions,\n includeSessionId = true,\n model,\n permissionMode,\n mcpConfig,\n strictMcpConfig,\n disallowedTools,\n appendSystemPromptFile,\n thinking,\n thinkingDisplay,\n cliVersion,\n } = opts\n const args = [\n \"--print\",\n \"--output-format\",\n \"stream-json\",\n \"--input-format\",\n \"stream-json\",\n \"--include-partial-messages\",\n \"--verbose\",\n ]\n\n if (model) {\n args.push(\"--model\", model)\n }\n\n if (permissionMode) {\n args.push(\"--permission-mode\", permissionMode)\n }\n\n if (includeSessionId) {\n const sessionId = claudeSessions.get(sessionKey)\n if (sessionId && !activeProcesses.has(sessionKey)) {\n args.push(\"--session-id\", sessionId)\n }\n }\n\n if (mcpConfig) {\n const configs = Array.isArray(mcpConfig) ? mcpConfig : [mcpConfig]\n const filtered = configs.filter((c) => typeof c === \"string\" && c.length > 0)\n if (filtered.length > 0) {\n args.push(\"--mcp-config\", ...filtered)\n }\n }\n\n if (strictMcpConfig) {\n args.push(\"--strict-mcp-config\")\n }\n\n if (disallowedTools && disallowedTools.length > 0) {\n args.push(\"--disallowedTools\", ...disallowedTools)\n }\n\n // `--thinking` is only present from Claude Code 2.x onward; gate so\n // pre-2.x binaries don't crash with a parse error. Unknown version →\n // skip (the spawn still works, the user just doesn't get extended\n // thinking until they upgrade).\n if (thinking && cliSupportsThinking(cliVersion ?? null)) {\n args.push(\"--thinking\", thinking)\n }\n\n // `--thinking-display` was added in Claude Code 2.1.142. Older CLIs\n // reject it with a parse error, so gate on detected version. When\n // version is unknown (detection failed), be conservative and skip.\n if (thinkingDisplay && cliSupportsThinkingDisplay(cliVersion ?? null)) {\n args.push(\"--thinking-display\", thinkingDisplay)\n }\n\n if (appendSystemPromptFile) {\n args.push(\"--append-system-prompt-file\", appendSystemPromptFile)\n }\n\n if (skipPermissions) {\n args.push(\"--dangerously-skip-permissions\")\n }\n\n return args\n}\n\n/**\n * Build a session key that includes both cwd and model,\n * so different models get separate processes.\n */\nexport function sessionKey(cwd: string, modelId: string): string {\n return `${cwd}::${modelId}`\n}\n","import { execFile } from \"node:child_process\"\nimport { promisify } from \"node:util\"\nimport { log } from \"./logger.js\"\n\nconst execFileAsync = promisify(execFile)\n\nexport interface CliVersion {\n major: number\n minor: number\n patch: number\n raw: string\n}\n\nconst cache = new Map<string, Promise<CliVersion | null>>()\n\n/**\n * Run `claude --version` once per cliPath and parse the leading semver.\n * Returns null on any failure (binary missing, unparseable output, etc.)\n * so callers can fall back to the most conservative flag set.\n */\nexport function detectCliVersion(cliPath: string): Promise<CliVersion | null> {\n const cached = cache.get(cliPath)\n if (cached) return cached\n const promise = (async (): Promise<CliVersion | null> => {\n try {\n const { stdout } = await execFileAsync(cliPath, [\"--version\"], {\n timeout: 5000,\n })\n const match = /(\\d+)\\.(\\d+)\\.(\\d+)/.exec(stdout.trim())\n if (!match) {\n log.warn(\"claude --version output unparseable\", { stdout: stdout.trim() })\n return null\n }\n const v: CliVersion = {\n major: Number(match[1]),\n minor: Number(match[2]),\n patch: Number(match[3]),\n raw: stdout.trim(),\n }\n log.info(\"detected claude cli version\", { cliPath, version: v.raw })\n if (!cliSupportsThinkingDisplay(v)) {\n log.notice(\n \"claude cli < 2.1.142 detected; Opus 4.7 thinking summaries unavailable. Run `npm i -g @anthropic-ai/claude-code` to upgrade.\",\n { version: v.raw },\n )\n }\n return v\n } catch (err) {\n log.warn(\"failed to detect claude cli version\", {\n cliPath,\n error: err instanceof Error ? err.message : String(err),\n })\n return null\n }\n })()\n cache.set(cliPath, promise)\n return promise\n}\n\nfunction gte(v: CliVersion, target: { major: number; minor: number; patch: number }): boolean {\n if (v.major !== target.major) return v.major > target.major\n if (v.minor !== target.minor) return v.minor > target.minor\n return v.patch >= target.patch\n}\n\n/**\n * `--thinking-display` was introduced in Claude Code 2.1.142 alongside\n * Opus 4.7's \"omitted by default\" thinking behavior. Older CLIs reject\n * the flag with a parse error, so we gate it. Unknown version → return\n * false so we don't risk crashing the spawn.\n */\nexport function cliSupportsThinkingDisplay(v: CliVersion | null): boolean {\n if (!v) return false\n return gte(v, { major: 2, minor: 1, patch: 142 })\n}\n\n/**\n * `--thinking` has been part of Claude Code's CLI since the 2.x line.\n * We require a detected 2.0.0+ before passing it; unknown version → skip\n * to avoid crashing a pre-flag binary. Anyone on the 1.x line should\n * upgrade.\n */\nexport function cliSupportsThinking(v: CliVersion | null): boolean {\n if (!v) return false\n return gte(v, { major: 2, minor: 0, patch: 0 })\n}\n\n/** For tests. */\nexport function _clearCache(): void {\n cache.clear()\n}\n","import { createServer, type IncomingMessage, type ServerResponse } from \"node:http\"\nimport type { AddressInfo } from \"node:net\"\nimport * as fs from \"node:fs\"\nimport * as path from \"node:path\"\nimport * as crypto from \"node:crypto\"\nimport { EventEmitter } from \"node:events\"\nimport { log } from \"./logger.js\"\nimport { pluginTmpDir } from \"./tmp.js\"\n\n/**\n * Minimal MCP HTTP server embedded in-process. Exposes a set of \"proxy\"\n * tools (Bash, Edit, Write, etc.) that Claude CLI calls when its built-in\n * equivalents are disabled via --disallowedTools. Our handler blocks until\n * an external broker resolves the call, then responds to Claude.\n *\n * Wire protocol: JSON-RPC 2.0 over plain HTTP POST to `/mcp`. MCP spec\n * also supports SSE streaming, but Claude's HTTP transport accepts single\n * JSON responses for short-lived tool calls, so we keep it simple.\n */\n\nexport interface ProxyMcpServer {\n url: string\n serverName: string\n tools: ProxyToolDef[]\n /** Fires when Claude invokes one of our proxy tools. The handler resolves\n * the returned pending call once a result is available. */\n calls: EventEmitter\n /** Write `--mcp-config <path>`-compatible scratch file and return its path. */\n configPath(): string\n close(): Promise<void>\n}\n\nexport interface ProxyToolDef {\n /** Raw name as seen by Claude once proxied: the MCP exposed tool name. */\n name: string\n description: string\n inputSchema: Record<string, unknown>\n}\n\nexport interface ProxyToolCall {\n id: string\n toolName: string\n input: Record<string, unknown>\n resolve: (result: ProxyToolResult) => void\n reject: (err: Error) => void\n}\n\nexport type ProxyToolResult =\n | { kind: \"text\"; text: string; isError?: boolean }\n | { kind: \"error\"; message: string }\n\nconst PROTOCOL_VERSION = \"2024-11-05\"\nconst SERVER_NAME = \"opencode_proxy\"\nexport const PROXY_TOOL_PREFIX = `mcp__${SERVER_NAME}__`\n\n// Cap on how long a proxy tool call may wait for opencode to resolve it.\n// Matches Claude CLI's hard upper bound for Bash (10 min). Without this the\n// HTTP handler waits forever if the broker chain breaks (listener never\n// attaches, opencode crashes between turns, etc.) and the Claude\n// subprocess sits idle waiting for a tool result that never arrives.\nconst PROXY_CALL_TIMEOUT_MS = 10 * 60 * 1000\n\nexport const DEFAULT_PROXY_TOOLS: ProxyToolDef[] = [\n {\n name: \"bash\",\n description:\n \"Execute a shell command. Routed through opencode's bash tool so\" +\n \" permission prompts flow through opencode's UI.\",\n inputSchema: {\n type: \"object\",\n properties: {\n command: {\n type: \"string\",\n description: \"The shell command to execute.\",\n },\n description: {\n type: \"string\",\n description: \"Short human-readable description of what the command does.\",\n },\n timeout: {\n type: \"number\",\n description: \"Optional timeout in milliseconds.\",\n },\n },\n required: [\"command\"],\n },\n },\n {\n name: \"write\",\n description:\n \"Write a file. Routed through opencode's write tool so permission prompts flow through opencode's UI.\",\n inputSchema: {\n type: \"object\",\n properties: {\n filePath: {\n type: \"string\",\n description: \"The file to write. Absolute paths are preferred.\",\n },\n content: {\n type: \"string\",\n description: \"The full content to write to the file.\",\n },\n },\n required: [\"filePath\", \"content\"],\n },\n },\n {\n name: \"edit\",\n description:\n \"Replace text in an existing file. Routed through opencode's edit tool so permission prompts flow through opencode's UI.\",\n inputSchema: {\n type: \"object\",\n properties: {\n filePath: {\n type: \"string\",\n description: \"The file to edit. Absolute paths are preferred.\",\n },\n oldString: {\n type: \"string\",\n description: \"The exact text to replace.\",\n },\n newString: {\n type: \"string\",\n description: \"The replacement text.\",\n },\n replaceAll: {\n type: \"boolean\",\n description: \"Replace all occurrences instead of just the first one.\",\n },\n },\n required: [\"filePath\", \"oldString\", \"newString\"],\n },\n },\n {\n name: \"webfetch\",\n description:\n \"Fetch content from a URL. Routed through opencode's webfetch tool so\" +\n \" permission prompts flow through opencode's UI. Returns the page\" +\n \" content in the requested format.\",\n inputSchema: {\n type: \"object\",\n properties: {\n url: {\n type: \"string\",\n description: \"The URL to fetch content from. Must start with http:// or https://.\",\n },\n format: {\n type: \"string\",\n enum: [\"text\", \"markdown\", \"html\"],\n description:\n \"The format to return the content in. Defaults to markdown.\",\n },\n timeout: {\n type: \"number\",\n description: \"Optional timeout in seconds (max 120).\",\n },\n },\n required: [\"url\"],\n },\n },\n {\n name: \"task\",\n description:\n \"Launch an opencode subagent to handle a complex multi-step task\" +\n \" autonomously. Routed through opencode's task tool so subagent\" +\n \" orchestration, permission, and lifecycle are handled by opencode.\" +\n \" Use `subagent_type` to pick which configured subagent runs (e.g.\" +\n \" `build`, `general`, `explore`, or any custom subagent declared in\" +\n \" opencode.json). The call blocks until the subagent finishes; the\" +\n \" 10-minute proxy timeout applies.\",\n inputSchema: {\n type: \"object\",\n properties: {\n description: {\n type: \"string\",\n description: \"A short (3-5 words) description of the task\",\n },\n prompt: {\n type: \"string\",\n description: \"The task for the agent to perform\",\n },\n subagent_type: {\n type: \"string\",\n description: \"The type of specialized agent to use for this task\",\n },\n task_id: {\n type: \"string\",\n description:\n \"Set this only if you mean to resume a previous task — pass the\" +\n \" prior task_id to continue the same subagent session instead of\" +\n \" creating a fresh one.\",\n },\n command: {\n type: \"string\",\n description: \"The command that triggered this task\",\n },\n },\n required: [\"description\", \"prompt\", \"subagent_type\"],\n },\n },\n]\n\nexport async function createProxyMcpServer(\n tools: ProxyToolDef[] = DEFAULT_PROXY_TOOLS,\n): Promise<ProxyMcpServer> {\n const calls = new EventEmitter()\n const pending = new Map<string, ProxyToolCall>()\n\n const server = createServer(async (req, res) => {\n if (req.method !== \"POST\" || !req.url?.startsWith(\"/mcp\")) {\n res.statusCode = 404\n res.end()\n return\n }\n try {\n const body = await readBody(req)\n const request = JSON.parse(body) as {\n jsonrpc?: string\n id?: number | string | null\n method?: string\n params?: Record<string, unknown>\n }\n\n if (request?.jsonrpc !== \"2.0\" || typeof request.method !== \"string\") {\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request?.id ?? null,\n error: { code: -32600, message: \"Invalid request\" },\n })\n return\n }\n\n log.debug(\"proxy-mcp request\", {\n method: request.method,\n id: request.id,\n })\n\n if (request.method === \"initialize\") {\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request.id ?? null,\n result: {\n protocolVersion: PROTOCOL_VERSION,\n capabilities: { tools: {} },\n serverInfo: {\n name: SERVER_NAME,\n version: \"0.1.0\",\n },\n },\n })\n return\n }\n\n if (request.method === \"notifications/initialized\") {\n res.statusCode = 204\n res.end()\n return\n }\n\n if (request.method === \"tools/list\") {\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request.id ?? null,\n result: {\n tools: tools.map((t) => ({\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema,\n })),\n },\n })\n return\n }\n\n if (request.method === \"tools/call\") {\n const params = request.params ?? {}\n const toolName = String(params.name ?? \"\")\n const input = (params.arguments ?? {}) as Record<string, unknown>\n\n if (!tools.some((t) => t.name === toolName)) {\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request.id ?? null,\n error: {\n code: -32601,\n message: `Unknown proxy tool: ${toolName}`,\n },\n })\n return\n }\n\n const callId = crypto.randomUUID()\n log.info(\"proxy-mcp tool call received\", {\n callId,\n toolName,\n hasInput: input != null,\n })\n\n let timer: ReturnType<typeof setTimeout> | null = null\n const result = await new Promise<ProxyToolResult>(\n (resolve, reject) => {\n const entry: ProxyToolCall = {\n id: callId,\n toolName,\n input,\n resolve,\n reject,\n }\n pending.set(callId, entry)\n timer = setTimeout(() => {\n if (!pending.has(callId)) return\n pending.delete(callId)\n // v0.4.13: demoted from warn to notice. Timeouts are usually\n // permission-pending while the user is AFK — surfacing each as\n // a yellow UI bubble produces a wall of noise on return. The\n // file log still captures the event for diagnostics.\n log.notice(\"proxy-mcp tool call timed out\", {\n callId,\n toolName,\n timeoutMs: PROXY_CALL_TIMEOUT_MS,\n })\n reject(\n new Error(\n `Proxy tool '${toolName}' timed out after ${PROXY_CALL_TIMEOUT_MS}ms waiting for opencode to resolve the call`,\n ),\n )\n }, PROXY_CALL_TIMEOUT_MS)\n calls.emit(\"call\", entry)\n },\n ).finally(() => {\n if (timer) clearTimeout(timer)\n pending.delete(callId)\n })\n\n if (result.kind === \"error\") {\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request.id ?? null,\n error: {\n code: -32000,\n message: result.message,\n },\n })\n return\n }\n\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request.id ?? null,\n result: {\n content: [{ type: \"text\", text: result.text }],\n isError: result.isError === true,\n },\n })\n return\n }\n\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: request.id ?? null,\n error: { code: -32601, message: `Unknown method: ${request.method}` },\n })\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error)\n // v0.4.13 + v0.4.19: cleanup rejections from the broker propagate up\n // here. None are user-actionable — they fire on AFK-permission timeouts,\n // orphan-rejections after a turn boundary, stream closes, etc. File-log\n // them at NOTICE; other error shapes stay as WARN so genuine bugs remain\n // visible in the TUI.\n const isExpectedCleanup =\n (errorMessage.includes(\"timed out after\") &&\n errorMessage.includes(\"waiting for opencode to resolve\")) ||\n errorMessage.includes(\"rejecting as orphaned\") ||\n errorMessage.includes(\"was orphaned by a new user turn\")\n const logFn = isExpectedCleanup ? log.notice : log.warn\n logFn(\"proxy-mcp error handling request\", {\n error: errorMessage,\n })\n try {\n writeJson(res, {\n jsonrpc: \"2.0\",\n id: null,\n error: {\n code: -32603,\n message: error instanceof Error ? error.message : \"Internal error\",\n },\n })\n } catch {\n try {\n res.statusCode = 500\n res.end()\n } catch {}\n }\n }\n })\n\n await new Promise<void>((resolve, reject) => {\n server.once(\"error\", reject)\n server.listen(0, \"127.0.0.1\", () => {\n server.off(\"error\", reject)\n resolve()\n })\n })\n\n const addr = server.address() as AddressInfo | null\n if (!addr) {\n server.close()\n throw new Error(\"Failed to bind proxy MCP server\")\n }\n\n const url = `http://127.0.0.1:${addr.port}/mcp`\n\n log.info(\"proxy-mcp server started\", {\n url,\n tools: tools.map((t) => t.name),\n })\n\n let configFilePath: string | null = null\n\n const api: ProxyMcpServer = {\n url,\n serverName: SERVER_NAME,\n tools,\n calls,\n configPath() {\n if (configFilePath) return configFilePath\n const body = JSON.stringify(\n {\n mcpServers: {\n [SERVER_NAME]: {\n type: \"http\",\n url,\n },\n },\n },\n null,\n 2,\n )\n const hash = crypto\n .createHash(\"sha256\")\n .update(body)\n .digest(\"hex\")\n .slice(0, 12)\n const outPath = path.join(\n pluginTmpDir(),\n `proxy-${hash}.json`,\n )\n fs.writeFileSync(outPath, body, { encoding: \"utf8\", mode: 0o600 })\n configFilePath = outPath\n return outPath\n },\n async close() {\n for (const entry of pending.values()) {\n entry.reject(new Error(\"proxy MCP server closed\"))\n }\n pending.clear()\n await new Promise<void>((resolve) => {\n server.close(() => resolve())\n })\n if (configFilePath) {\n try {\n fs.unlinkSync(configFilePath)\n } catch {}\n configFilePath = null\n }\n },\n }\n\n return api\n}\n\n/** CLI-ready list of Claude tool names to disable, for each proxied tool. */\nexport function disallowedToolFlags(tools: ProxyToolDef[]): string[] {\n // Map our lowercase MCP tool names to the Claude tool name(s) they replace.\n // `edit` covers both `Edit` and `MultiEdit` because opencode has no\n // MultiEdit equivalent; without disabling MultiEdit, Claude can batch\n // file changes through it and bypass opencode's permission UI.\n // `task` disables Claude CLI's `Agent` tool (its built-in subagent\n // dispatcher) so subagent calls flow through opencode's `task` tool\n // instead — which lets opencode's configured subagent set (`build`,\n // `general`, custom subagents in opencode.json) execute the work\n // under opencode's permission/lifecycle, rather than Claude's\n // internal-only general-purpose / Explore / Plan options.\n const nameMap: Record<string, string[]> = {\n bash: [\"Bash\"],\n read: [\"Read\"],\n write: [\"Write\"],\n edit: [\"Edit\", \"MultiEdit\"],\n glob: [\"Glob\"],\n grep: [\"Grep\"],\n webfetch: [\"WebFetch\"],\n task: [\"Agent\"],\n }\n const out: string[] = []\n const seen = new Set<string>()\n for (const t of tools) {\n const mapped = nameMap[t.name.toLowerCase()]\n if (!mapped) continue\n for (const claudeTool of mapped) {\n if (seen.has(claudeTool)) continue\n seen.add(claudeTool)\n out.push(claudeTool)\n }\n }\n return out\n}\n\nfunction readBody(req: IncomingMessage): Promise<string> {\n return new Promise((resolve, reject) => {\n const chunks: Buffer[] = []\n req.on(\"data\", (chunk: Buffer) => chunks.push(chunk))\n req.on(\"end\", () => resolve(Buffer.concat(chunks).toString(\"utf8\")))\n req.on(\"error\", reject)\n })\n}\n\nfunction writeJson(res: ServerResponse, body: unknown): void {\n const payload = JSON.stringify(body)\n res.statusCode = 200\n res.setHeader(\"Content-Type\", \"application/json\")\n res.setHeader(\"Content-Length\", Buffer.byteLength(payload).toString())\n res.end(payload)\n}\n","import { EventEmitter } from \"node:events\"\nimport type { ProxyToolCall, ProxyToolResult } from \"./proxy-mcp.js\"\nimport { log } from \"./logger.js\"\n\nexport interface PendingProxyCall {\n sessionKey: string\n toolCallId: string\n toolName: string\n input: Record<string, unknown>\n}\n\ntype InternalPending = PendingProxyCall & {\n createdAt: number\n timer: ReturnType<typeof setTimeout>\n resolve(result: ProxyToolResult): void\n reject(error: Error): void\n}\n\n// Primary index: callId -> pending. Tool call IDs are UUIDs produced by\n// proxy-mcp, so they are globally unique across sessions.\nconst pendingByCallId = new Map<string, InternalPending>()\n// Reverse index: sessionKey -> set of callIds, so the language model can\n// drain or reject every pending call for one Claude subprocess at once.\nconst callIdsBySession = new Map<string, Set<string>>()\n\nconst emitter = new EventEmitter()\nconst PENDING_PROXY_CALL_TIMEOUT_MS = 10 * 60 * 1000\n\nfunction eventName(sessionKey: string) {\n return `pending:${sessionKey}`\n}\n\nfunction indexAdd(sessionKey: string, callId: string) {\n let s = callIdsBySession.get(sessionKey)\n if (!s) {\n s = new Set()\n callIdsBySession.set(sessionKey, s)\n }\n s.add(callId)\n}\n\nfunction indexRemove(sessionKey: string, callId: string) {\n const s = callIdsBySession.get(sessionKey)\n if (!s) return\n s.delete(callId)\n if (s.size === 0) callIdsBySession.delete(sessionKey)\n}\n\nexport function onPendingProxyCall(\n sessionKey: string,\n handler: (call: PendingProxyCall) => void,\n): () => void {\n const name = eventName(sessionKey)\n emitter.on(name, handler)\n return () => emitter.off(name, handler)\n}\n\nexport function queuePendingProxyCall(\n sessionKey: string,\n call: ProxyToolCall,\n): PendingProxyCall {\n // Defensive: if this exact callId is somehow already pending (UUID\n // collision or retry storm), replace it cleanly so we never leak two\n // entries for the same id.\n const previous = pendingByCallId.get(call.id)\n if (previous) {\n clearTimeout(previous.timer)\n previous.reject(\n new Error(`Replaced pending proxy call ${call.id} with a fresh one`),\n )\n pendingByCallId.delete(call.id)\n indexRemove(previous.sessionKey, call.id)\n }\n\n const timer = setTimeout(() => {\n const current = pendingByCallId.get(call.id)\n if (!current) return\n pendingByCallId.delete(call.id)\n indexRemove(current.sessionKey, call.id)\n current.reject(\n new Error(\n `Proxy tool call '${call.toolName}' timed out after ${PENDING_PROXY_CALL_TIMEOUT_MS}ms waiting for opencode to resolve the call`,\n ),\n )\n // v0.4.13: demoted from warn to notice. AFK-permission-pending\n // sessions can stack many of these; demoting keeps the UI quiet on\n // return while preserving the audit trail in plugin.log.\n log.notice(\"timed out pending proxy call\", {\n sessionKey: current.sessionKey,\n toolCallId: call.id,\n toolName: call.toolName,\n timeoutMs: PENDING_PROXY_CALL_TIMEOUT_MS,\n })\n }, PENDING_PROXY_CALL_TIMEOUT_MS)\n\n const pending: InternalPending = {\n sessionKey,\n toolCallId: call.id,\n toolName: call.toolName,\n input: call.input,\n createdAt: Date.now(),\n timer,\n resolve: call.resolve,\n reject: call.reject,\n }\n pendingByCallId.set(call.id, pending)\n indexAdd(sessionKey, call.id)\n emitter.emit(eventName(sessionKey), pending)\n log.info(\"queued pending proxy call\", {\n sessionKey,\n toolCallId: call.id,\n toolName: call.toolName,\n })\n return pending\n}\n\nexport function getPendingProxyCalls(sessionKey: string): PendingProxyCall[] {\n const s = callIdsBySession.get(sessionKey)\n if (!s || s.size === 0) return []\n const out: PendingProxyCall[] = []\n for (const id of s) {\n const p = pendingByCallId.get(id)\n if (p) out.push(p)\n }\n return out\n}\n\nexport function resolvePendingProxyCallById(\n toolCallId: string,\n result: ProxyToolResult,\n): boolean {\n const pending = pendingByCallId.get(toolCallId)\n if (!pending) return false\n pendingByCallId.delete(toolCallId)\n indexRemove(pending.sessionKey, toolCallId)\n clearTimeout(pending.timer)\n pending.resolve(result)\n log.info(\"resolved pending proxy call\", {\n sessionKey: pending.sessionKey,\n toolCallId: pending.toolCallId,\n toolName: pending.toolName,\n })\n return true\n}\n\nexport function rejectPendingProxyCallById(\n toolCallId: string,\n error: Error,\n): boolean {\n const pending = pendingByCallId.get(toolCallId)\n if (!pending) return false\n pendingByCallId.delete(toolCallId)\n indexRemove(pending.sessionKey, toolCallId)\n clearTimeout(pending.timer)\n pending.reject(error)\n // Rejection is the broker's cleanup mechanism — fires on timeouts, orphans,\n // stream closes, etc. None are user-actionable. File-log them at NOTICE so\n // the audit trail is intact; rely on caller sites to decide TUI visibility.\n log.notice(\"rejected pending proxy call\", {\n sessionKey: pending.sessionKey,\n toolCallId: pending.toolCallId,\n toolName: pending.toolName,\n error: error.message,\n })\n return true\n}\n\nexport function rejectAllPendingProxyCallsForSession(\n sessionKey: string,\n error: Error,\n): number {\n const s = callIdsBySession.get(sessionKey)\n if (!s) return 0\n const ids = [...s]\n let count = 0\n for (const id of ids) {\n if (rejectPendingProxyCallById(id, error)) count++\n }\n return count\n}\n","import type { OpenCodeModel } from \"./opencode-types.js\"\n\nconst PROVIDER_ID = \"claude-code\"\nconst NPM = \"@khalilgharbaoui/opencode-claude-code-plugin\"\n\nconst reasoningVariants: Record<string, Record<string, unknown>> = {\n low: { reasoningEffort: \"low\" },\n medium: { reasoningEffort: \"medium\" },\n high: { reasoningEffort: \"high\" },\n xhigh: { reasoningEffort: \"xhigh\" },\n max: { reasoningEffort: \"max\" },\n}\n\nconst baseCapabilities = {\n temperature: false,\n attachment: true,\n toolcall: true,\n input: { text: true, audio: false, image: true, video: false, pdf: false },\n output: { text: true, audio: false, image: false, video: false, pdf: false },\n interleaved: false as const,\n}\n\nfunction defineModel(opts: {\n id: string\n name: string\n family: string\n reasoning: boolean\n context: number\n output: number\n cost: { input: number; output: number; cacheRead: number; cacheWrite: number }\n releaseDate: string\n // List-price multiplier relative to Haiku (the cheapest model). Derived\n // exactly from published per-token pricing: input AND output ratios both come\n // out to haiku 1, sonnet 3, opus 5, fable/mythos 10 — so Fable/Mythos are 2×\n // Opus 4.8. Rendered as a `(N×)` suffix on the display name so it surfaces in\n // opencode's model picker, which has no dedicated multiplier field.\n // Display-only: model resolution keys off `id`.\n multiplier: number\n status?: OpenCodeModel[\"status\"]\n}): OpenCodeModel {\n return {\n id: opts.id,\n providerID: PROVIDER_ID,\n api: { id: opts.id, url: \"\", npm: NPM },\n name: `${opts.name} (${opts.multiplier}×)`,\n family: opts.family,\n capabilities: { ...baseCapabilities, reasoning: opts.reasoning },\n cost: {\n input: opts.cost.input,\n output: opts.cost.output,\n cache: { read: opts.cost.cacheRead, write: opts.cost.cacheWrite },\n },\n limit: { context: opts.context, output: opts.output },\n status: opts.status ?? \"active\",\n options: {},\n headers: {},\n release_date: opts.releaseDate,\n variants: opts.reasoning ? reasoningVariants : undefined,\n }\n}\n\n// Per-token costs derived from Anthropic per-million-token pricing\nconst haikuCost = { input: 1e-6, output: 5e-6, cacheRead: 1e-7, cacheWrite: 1.25e-6 }\nconst sonnetCost = { input: 3e-6, output: 15e-6, cacheRead: 3e-7, cacheWrite: 3.75e-6 }\n// Opus 4.5+ standard pricing is $5/M in, $25/M out (the price cut at 4.5; held\n// through 4.6/4.7/4.8). Cache read 0.1x input, cache write 1.25x input.\nconst opusCost = { input: 5e-6, output: 25e-6, cacheRead: 0.5e-6, cacheWrite: 6.25e-6 }\n// Fable 5 and Mythos 5 are the Mythos-class tier above Opus and share pricing\n// ($10/M in, $50/M out). Cache read/write follow Anthropic's standard 0.1x / 1.25x\n// input ratios (not separately published).\nconst fableCost = { input: 10e-6, output: 50e-6, cacheRead: 1e-6, cacheWrite: 12.5e-6 }\n\n/**\n * Convert an OpenCodeModel to the flat config schema that OpenCode's\n * provider.ts config parser expects (model.temperature, model.reasoning,\n * model.cost.cache_read, model.modalities, etc.).\n */\nexport function toConfigModel(model: OpenCodeModel): Record<string, unknown> {\n const inputMods: string[] = []\n const outputMods: string[] = []\n for (const [k, v] of Object.entries(model.capabilities.input)) {\n if (v) inputMods.push(k)\n }\n for (const [k, v] of Object.entries(model.capabilities.output)) {\n if (v) outputMods.push(k)\n }\n\n return {\n id: model.api.id,\n name: model.name,\n status: model.status,\n family: model.family ?? \"\",\n release_date: model.release_date,\n\n temperature: model.capabilities.temperature,\n reasoning: model.capabilities.reasoning,\n attachment: model.capabilities.attachment,\n tool_call: model.capabilities.toolcall,\n modalities: { input: inputMods, output: outputMods },\n\n cost: {\n input: model.cost.input,\n output: model.cost.output,\n cache_read: model.cost.cache.read,\n cache_write: model.cost.cache.write,\n },\n\n limit: model.limit,\n options: model.options,\n headers: model.headers,\n variants: model.variants,\n }\n}\n\nexport const defaultModels: Record<string, OpenCodeModel> = {\n \"claude-haiku-4-5\": defineModel({\n id: \"claude-haiku-4-5\",\n name: \"Claude Haiku 4.5\",\n family: \"haiku\",\n reasoning: false,\n context: 200_000,\n output: 8_192,\n cost: haikuCost,\n multiplier: 1,\n releaseDate: \"2024-10-22\",\n }),\n \"claude-sonnet-4-5\": defineModel({\n id: \"claude-sonnet-4-5\",\n name: \"Claude Sonnet 4.5\",\n family: \"sonnet\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: sonnetCost,\n multiplier: 3,\n releaseDate: \"2025-04-14\",\n }),\n \"claude-sonnet-4-6\": defineModel({\n id: \"claude-sonnet-4-6\",\n name: \"Claude Sonnet 4.6\",\n family: \"sonnet\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: sonnetCost,\n multiplier: 3,\n releaseDate: \"2025-06-19\",\n }),\n \"claude-opus-4-5\": defineModel({\n id: \"claude-opus-4-5\",\n name: \"Claude Opus 4.5\",\n family: \"opus\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: opusCost,\n multiplier: 5,\n releaseDate: \"2025-04-14\",\n }),\n \"claude-opus-4-6\": defineModel({\n id: \"claude-opus-4-6\",\n name: \"Claude Opus 4.6\",\n family: \"opus\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: opusCost,\n multiplier: 5,\n releaseDate: \"2025-06-19\",\n }),\n \"claude-opus-4-7\": defineModel({\n id: \"claude-opus-4-7\",\n name: \"Claude Opus 4.7\",\n family: \"opus\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: opusCost,\n multiplier: 5,\n releaseDate: \"2025-07-16\",\n }),\n \"claude-opus-4-8\": defineModel({\n id: \"claude-opus-4-8\",\n name: \"Claude Opus 4.8\",\n family: \"opus\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: opusCost,\n multiplier: 5,\n releaseDate: \"2026-05-28\",\n }),\n \"claude-fable-5\": defineModel({\n id: \"claude-fable-5\",\n name: \"Claude Fable 5\",\n family: \"fable\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: fableCost,\n multiplier: 10,\n releaseDate: \"2026-06-09\",\n }),\n // Mythos 5 shares Fable 5's capabilities and pricing without the safety\n // classifiers; limited availability via Project Glasswing. `claude --model\n // claude-mythos-5` simply errors for accounts without access, so it's safe to\n // register unconditionally.\n \"claude-mythos-5\": defineModel({\n id: \"claude-mythos-5\",\n name: \"Claude Mythos 5\",\n family: \"mythos\",\n reasoning: true,\n context: 1_000_000,\n output: 16_384,\n cost: fableCost,\n multiplier: 10,\n releaseDate: \"2026-06-09\",\n }),\n}\n","import { chmod, lstat, mkdir, readlink, symlink, writeFile } from \"node:fs/promises\"\nimport path from \"node:path\"\nimport { log } from \"./logger.js\"\n\nexport const BASE_PROVIDER_ID = \"claude-code\"\nexport const DEFAULT_ACCOUNT = \"default\"\n\nconst SHARED_CAPABILITY_ITEMS = [\n \"CLAUDE.md\",\n \"settings.json\",\n \"skills\",\n \"agents\",\n \"commands\",\n \"plugins\",\n]\n\nexport function normalizeAccountName(account: string): string {\n return account\n .trim()\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, \"-\")\n .replace(/^-+|-+$/g, \"\")\n}\n\nexport function resolveAccounts(value: unknown): string[] | null {\n if (!Array.isArray(value)) return null\n\n const accounts = value\n .map((account) => normalizeAccountName(String(account)))\n .filter(Boolean)\n\n return Array.from(new Set([DEFAULT_ACCOUNT, ...accounts]))\n}\n\nexport function accountProviderId(account: string): string {\n return `${BASE_PROVIDER_ID}-${normalizeAccountName(account)}`\n}\n\nexport function accountDisplayName(account: string): string {\n return `Claude Code (${titleizeAccount(account)})`\n}\n\nexport function accountModelSuffix(account: string): string | undefined {\n const normalized = normalizeAccountName(account)\n return normalized === DEFAULT_ACCOUNT ? undefined : normalized\n}\n\nexport function accountConfigDir(account: string): string | undefined {\n const normalized = normalizeAccountName(account)\n\n if (!normalized || normalized === DEFAULT_ACCOUNT) return undefined\n\n return `~/.claude-${normalized}`\n}\n\nexport function expandHome(value: string): string {\n const home = process.env.HOME ?? process.env.USERPROFILE\n\n if (value === \"~\") return home ?? value\n\n if (value.startsWith(\"~/\") || value.startsWith(\"~\\\\\")) {\n return home ? path.join(home, value.slice(2)) : value\n }\n\n return value\n}\n\nexport async function ensureAccountRuntime(\n account: string,\n baseCliPath: string,\n): Promise<{ cliPath: string; configDir?: string }> {\n const configDir = accountConfigDir(account)\n\n if (!configDir) return { cliPath: baseCliPath }\n\n const expandedConfigDir = expandHome(configDir)\n await mkdir(expandedConfigDir, { recursive: true })\n\n try {\n await ensureSharedCapabilities(expandedConfigDir)\n } catch (err) {\n log.warn(\"failed to symlink shared capabilities; continuing anyway\", {\n account,\n configDir: expandedConfigDir,\n error: String(err),\n })\n }\n\n const cliPath = await writeAccountWrapper(\n normalizeAccountName(account),\n baseCliPath,\n expandedConfigDir,\n )\n\n return { cliPath, configDir }\n}\n\nasync function ensureSharedCapabilities(targetRoot: string): Promise<void> {\n const sourceRoot = expandHome(\"~/.claude\")\n\n for (const item of SHARED_CAPABILITY_ITEMS) {\n await ensureSharedCapabilityItem(sourceRoot, targetRoot, item)\n }\n}\n\nasync function ensureSharedCapabilityItem(\n sourceRoot: string,\n targetRoot: string,\n item: string,\n): Promise<void> {\n const source = path.join(sourceRoot, item)\n const target = path.join(targetRoot, item)\n\n let sourceStat\n try {\n sourceStat = await lstat(source)\n } catch {\n return\n }\n\n try {\n const targetStat = await lstat(target)\n\n if (targetStat.isSymbolicLink()) {\n const current = await readlink(target)\n const resolvedCurrent = path.resolve(path.dirname(target), current)\n const resolvedSource = path.resolve(source)\n\n if (resolvedCurrent === resolvedSource) return\n }\n\n log.warn(\"shared Claude capability already exists; leaving untouched\", {\n item,\n target,\n source,\n })\n\n return\n } catch {\n // Missing target is expected.\n }\n\n const type = sourceStat.isDirectory()\n ? process.platform === \"win32\"\n ? \"junction\"\n : \"dir\"\n : \"file\"\n\n await symlink(source, target, type)\n}\n\nasync function writeAccountWrapper(\n account: string,\n baseCliPath: string,\n configDir: string,\n): Promise<string> {\n const cacheRoot = path.join(\n process.env.XDG_CACHE_HOME ?? expandHome(\"~/.cache\"),\n \"opencode-claude-code-plugin\",\n )\n const wrapperPath = path.join(cacheRoot, `claude-${account}`)\n const suffix = `@${account}`\n\n await mkdir(cacheRoot, { recursive: true })\n\n const script = `#!/usr/bin/env bash\nset -euo pipefail\n\nargs=()\nwhile [[ $# -gt 0 ]]; do\n if [[ \"$1\" == \"--model\" && $# -ge 2 ]]; then\n model=\"$2\"\n if [[ \"$model\" == *${shellDoubleQuote(suffix)} ]]; then\n model=\"\\${model%${shellDoubleQuote(suffix)}}\"\n fi\n args+=(\"$1\" \"$model\")\n shift 2\n else\n args+=(\"$1\")\n shift\n fi\ndone\n\nexport CLAUDE_CONFIG_DIR=${shellSingleQuote(configDir)}\nexec ${shellSingleQuote(baseCliPath)} \"\\${args[@]}\"\n`\n\n await writeFile(wrapperPath, script, \"utf8\")\n await chmod(wrapperPath, 0o755)\n\n return wrapperPath\n}\n\nfunction shellSingleQuote(value: string): string {\n return `'${value.replace(/'/g, `'\"'\"'`)}'`\n}\n\nfunction shellDoubleQuote(value: string): string {\n return value.replace(/[$`\"\\\\]/g, \"\\\\$&\")\n}\n\nfunction titleizeAccount(account: string): string {\n return normalizeAccountName(account)\n .split(\"-\")\n .filter(Boolean)\n .map((part) => part.charAt(0).toUpperCase() + part.slice(1))\n .join(\" \")\n}\n","// Removes a stale unscoped `opencode-claude-code-plugin` install left in\n// opencode's plugin cache by older configs. The unscoped name is a different\n// artifact than this scoped plugin and shadows it when both coexist.\n// Disable with OPENCODE_CLAUDE_CODE_PLUGIN_NO_CLEANUP=1.\n\nimport {\n existsSync,\n readFileSync,\n realpathSync,\n rmSync,\n writeFileSync,\n} from \"node:fs\"\nimport { homedir } from \"node:os\"\nimport { join, resolve } from \"node:path\"\nimport { fileURLToPath } from \"node:url\"\nimport { log } from \"./logger.js\"\n\nconst STALE_PACKAGE_NAME = \"opencode-claude-code-plugin\"\nconst SUSPECT_DESCRIPTION_TOKEN = \"Claude Code\"\n\nlet alreadyRan = false\n\nfunction candidateCacheRoots(): string[] {\n const xdg = process.env.XDG_CACHE_HOME\n return [\n xdg ? join(xdg, \"opencode\") : null,\n join(homedir(), \".cache\", \"opencode\"),\n join(homedir(), \"Library\", \"Caches\", \"opencode\"),\n ].filter((p): p is string => Boolean(p))\n}\n\nfunction userOpencodeJsonPath(): string {\n const xdgConfig = process.env.XDG_CONFIG_HOME ?? join(homedir(), \".config\")\n return join(xdgConfig, \"opencode\", \"opencode.json\")\n}\n\nfunction userIntendsToUseUnscoped(): boolean {\n const cfg = userOpencodeJsonPath()\n if (!existsSync(cfg)) return false\n try {\n const json = JSON.parse(readFileSync(cfg, \"utf8\"))\n const plugins: unknown = json.plugin\n if (!Array.isArray(plugins)) return false\n return plugins.some(\n (entry) =>\n typeof entry === \"string\" &&\n /^opencode-claude-code-plugin(@[^/]+)?$/.test(entry),\n )\n } catch {\n return false\n }\n}\n\nfunction ourLoadedDir(): string | null {\n try {\n const filePath = fileURLToPath(import.meta.url)\n return realpathSync(resolve(filePath, \"..\", \"..\"))\n } catch {\n return null\n }\n}\n\nexport function cleanupStaleUnscopedInstall(): void {\n if (alreadyRan) return\n alreadyRan = true\n\n if (process.env.OPENCODE_CLAUDE_CODE_PLUGIN_NO_CLEANUP === \"1\") return\n if (userIntendsToUseUnscoped()) return\n\n const ourDir = ourLoadedDir()\n\n for (const cacheRoot of candidateCacheRoots()) {\n try {\n cleanupOne(cacheRoot, ourDir)\n } catch (err) {\n log.warn(\"cleanup-stale: error processing cache root\", {\n cacheRoot,\n error: String(err),\n })\n }\n }\n}\n\nfunction cleanupOne(cacheRoot: string, ourDir: string | null): void {\n if (!existsSync(cacheRoot)) return\n\n const stalePath = join(cacheRoot, \"node_modules\", STALE_PACKAGE_NAME)\n if (!existsSync(stalePath)) return\n\n // Don't self-delete if we are the unscoped install.\n let realStalePath = stalePath\n try {\n realStalePath = realpathSync(stalePath)\n } catch {\n // ignore\n }\n if (ourDir && realStalePath === ourDir) return\n\n // Verify identity before removing.\n const pkgJsonPath = join(stalePath, \"package.json\")\n if (!existsSync(pkgJsonPath)) return\n let pkg: { name?: string; description?: string } = {}\n try {\n pkg = JSON.parse(readFileSync(pkgJsonPath, \"utf8\"))\n } catch {\n return\n }\n if (pkg.name !== STALE_PACKAGE_NAME) return\n if (!pkg.description?.includes(SUSPECT_DESCRIPTION_TOKEN)) return\n\n log.info(\"cleanup-stale: removing unscoped install\", { stalePath })\n try {\n rmSync(stalePath, { recursive: true, force: true })\n } catch (err) {\n log.warn(\"cleanup-stale: rmSync failed\", {\n stalePath,\n error: String(err),\n })\n return\n }\n\n // Drop the dep from the cache root's package.json so opencode's installer\n // doesn't reinstate it on its next pass. Lockfile is left alone; bun\n // reconciles against package.json on the next install.\n const cachePkgJson = join(cacheRoot, \"package.json\")\n if (!existsSync(cachePkgJson)) return\n try {\n const cfg = JSON.parse(readFileSync(cachePkgJson, \"utf8\"))\n if (cfg?.dependencies?.[STALE_PACKAGE_NAME]) {\n delete cfg.dependencies[STALE_PACKAGE_NAME]\n writeFileSync(cachePkgJson, JSON.stringify(cfg, null, 2) + \"\\n\")\n log.info(\"cleanup-stale: pruned dep from cache package.json\")\n }\n } catch (err) {\n log.warn(\"cleanup-stale: cache package.json update failed\", {\n error: String(err),\n })\n }\n}\n","import type { LanguageModelV3 } from \"@ai-sdk/provider\"\nimport { ClaudeCodeLanguageModel } from \"./claude-code-language-model.js\"\nimport { defaultModels, toConfigModel } from \"./models.js\"\nimport type { OpenCodeModel, OpenCodePlugin, OpenCodeProvider } from \"./opencode-types.js\"\nimport type { ClaudeCodeProviderSettings } from \"./types.js\"\nimport {\n BASE_PROVIDER_ID,\n accountDisplayName,\n accountModelSuffix,\n accountProviderId,\n ensureAccountRuntime,\n resolveAccounts,\n} from \"./accounts.js\"\nimport { cleanupStaleUnscopedInstall } from \"./cleanup-stale.js\"\nimport { configureLogger, log } from \"./logger.js\"\nimport {\n isUsableDirectory,\n setOpencodeClient,\n setOpencodeProjectDirectory,\n} from \"./runtime-status.js\"\n\nexport interface ClaudeCodeProvider {\n specificationVersion: \"v3\"\n (modelId: string): LanguageModelV3\n languageModel(modelId: string): LanguageModelV3\n}\n\n// Picks the best directory from opencode's plugin context (`directory` /\n// `worktree`). Result is handed to runtime-status so it's available as a\n// *fallback* at spawn time only when `process.cwd()` is unusable (macOS\n// GUI launches at `/`). Never baked into provider config — see #4.\nfunction pickOpencodeDirectory(input: unknown): string | undefined {\n if (!input || typeof input !== \"object\") return undefined\n const ctx = input as { directory?: unknown; worktree?: unknown }\n if (isUsableDirectory(ctx.directory)) return ctx.directory\n if (isUsableDirectory(ctx.worktree)) return ctx.worktree\n return undefined\n}\n\nexport function createClaudeCode(\n settings: ClaudeCodeProviderSettings = {},\n): ClaudeCodeProvider {\n if (settings.logging) {\n configureLogger({\n file: settings.logging.file ?? false,\n dir: settings.logging.dir ?? null,\n mode: settings.logging.mode ?? \"silent\",\n level: settings.logging.level ?? \"info\",\n })\n }\n const cliPath =\n settings.cliPath ?? process.env.CLAUDE_CLI_PATH ?? \"claude\"\n const providerName = settings.providerID ?? settings.name ?? \"claude-code\"\n const proxyTools = settings.proxyTools ?? [\"Bash\", \"Edit\", \"Write\", \"WebFetch\"]\n\n const createModel = (modelId: string): LanguageModelV3 => {\n return new ClaudeCodeLanguageModel(modelId, {\n provider: providerName,\n cliPath,\n cwd: settings.cwd,\n account: settings.account,\n configDir: settings.configDir,\n providerID: settings.providerID,\n skipPermissions: settings.skipPermissions ?? true,\n permissionMode: settings.permissionMode,\n mcpConfig: settings.mcpConfig,\n strictMcpConfig: settings.strictMcpConfig,\n bridgeOpencodeMcp: settings.bridgeOpencodeMcp ?? true,\n controlRequestBehavior: settings.controlRequestBehavior ?? \"allow\",\n controlRequestToolBehaviors: settings.controlRequestToolBehaviors,\n controlRequestDenyMessage: settings.controlRequestDenyMessage,\n proxyTools,\n webSearch: settings.webSearch,\n hotReloadMcp: settings.hotReloadMcp ?? true,\n proxyOpencodeMcpTools: settings.proxyOpencodeMcpTools ?? true,\n multiStepContinuation: settings.multiStepContinuation ?? true,\n autoContinueIncompleteTurns:\n settings.autoContinueIncompleteTurns ?? \"smart\",\n compactionModel: settings.compactionModel,\n })\n }\n\n const provider = function (modelId: string) {\n return createModel(modelId)\n } as ClaudeCodeProvider\n\n provider.specificationVersion = \"v3\"\n provider.languageModel = createModel\n\n return provider\n}\n\n// ---------------------------------------------------------------------------\n// OpenCode plugin interface\n// ---------------------------------------------------------------------------\n\nconst PROVIDER_ID = BASE_PROVIDER_ID\nconst PACKAGE_NPM = \"@khalilgharbaoui/opencode-claude-code-plugin\"\n\nfunction pluginEntrypoint(): string {\n return import.meta.url.startsWith(\"file:\") ? import.meta.url : PACKAGE_NPM\n}\n\nfunction cleanProviderOptions(\n options: Record<string, unknown> = {},\n): Record<string, unknown> {\n const result = { ...options }\n delete result.accounts\n return result\n}\n\nfunction defaultModelsForProvider(\n providerModels: OpenCodeProvider[\"models\"],\n providerID = PROVIDER_ID,\n modelSuffix?: string,\n) {\n const models = Object.fromEntries(\n Object.entries(defaultModels).map(([id, model]) => {\n const modelId = modelSuffix ? `${id}@${modelSuffix}` : id\n const existing = providerModels[id] ?? providerModels[modelId]\n return [\n modelId,\n {\n ...model,\n id: modelId,\n providerID,\n api: {\n ...model.api,\n id: modelId,\n npm: existing?.api?.npm ?? model.api.npm,\n url: existing?.api?.url ?? model.api.url,\n },\n },\n ]\n }),\n )\n\n for (const [id, model] of Object.entries(providerModels)) {\n if (!(id in models)) {\n models[id] = {\n ...model,\n providerID,\n }\n }\n }\n\n return models\n}\n\n/**\n * Build models in OpenCode's config schema format (flat properties like\n * `temperature`, `reasoning`, `cost.cache_read`, `modalities`, etc.)\n * so the config-path provider loader parses them correctly.\n */\nexport function configModelsForProvider(\n providerModels: OpenCodeProvider[\"models\"],\n providerID: string,\n modelSuffix?: string,\n): Record<string, Record<string, unknown>> {\n const models: Record<string, Record<string, unknown>> = {}\n\n for (const [id, model] of Object.entries(defaultModels)) {\n const modelId = modelSuffix ? `${id}@${modelSuffix}` : id\n const existing = providerModels[id] ?? providerModels[modelId]\n const existingVariants =\n existing && typeof (existing as { variants?: unknown }).variants === \"object\"\n ? ((existing as { variants?: Record<string, Record<string, unknown>> }).variants ?? {})\n : {}\n const full: OpenCodeModel = {\n ...model,\n id: modelId,\n providerID,\n api: {\n ...model.api,\n id: modelId,\n npm: existing?.api?.npm ?? model.api.npm,\n url: existing?.api?.url ?? model.api.url,\n },\n variants: {\n ...(model.variants ?? {}),\n ...existingVariants,\n },\n }\n models[modelId] = toConfigModel(full)\n }\n\n for (const [id, model] of Object.entries(providerModels)) {\n if (!(id in models)) {\n models[id] = toConfigModel({ ...model, providerID } as OpenCodeModel)\n }\n }\n\n return models\n}\n\nasync function providerConfig(\n existing: {\n name?: string\n npm?: string\n options?: Record<string, unknown>\n models?: Record<string, unknown>\n } | undefined,\n providerID = PROVIDER_ID,\n optionDefaults: Record<string, unknown> = {},\n displayName?: string,\n) {\n const mergedOptions: Record<string, unknown> = {\n cliPath: \"claude\",\n proxyTools: [\"Bash\", \"Edit\", \"Write\", \"WebFetch\"],\n ...optionDefaults,\n ...cleanProviderOptions(existing?.options),\n providerID,\n }\n\n const cliPath = String(mergedOptions.cliPath ?? \"claude\")\n const account =\n typeof mergedOptions.account === \"string\" ? mergedOptions.account : undefined\n const runtime = account\n ? await ensureAccountRuntime(account, cliPath)\n : { cliPath }\n\n return {\n name: displayName ?? existing?.name,\n npm: existing?.npm ?? pluginEntrypoint(),\n options: {\n ...mergedOptions,\n ...runtime,\n },\n // models is intentionally omitted: both callers overwrite it with\n // configModelsForProvider(), which emits the flat config schema\n // opencode's config-path loader parses (and merges user variants).\n }\n}\n\nasync function expandAccountProviders(config: {\n provider?: Record<\n string,\n {\n name?: string\n npm?: string\n options?: Record<string, unknown>\n models?: Record<string, unknown>\n }\n >\n}): Promise<boolean> {\n const seed = config.provider?.[PROVIDER_ID]\n const accounts = resolveAccounts(seed?.options?.accounts)\n\n if (!accounts) return false\n\n config.provider ??= {}\n\n const seedOptions = cleanProviderOptions(seed?.options)\n let expandedCount = 0\n\n for (const account of accounts) {\n const providerID = accountProviderId(account)\n try {\n const existing = config.provider[providerID]\n const modelSuffix = accountModelSuffix(account)\n\n config.provider[providerID] = {\n ...existing,\n ...(await providerConfig(\n existing,\n providerID,\n {\n ...seedOptions,\n account,\n },\n accountDisplayName(account),\n )),\n models: configModelsForProvider(\n (existing?.models ?? seed?.models ?? {}) as OpenCodeProvider[\"models\"],\n providerID,\n modelSuffix,\n ),\n }\n expandedCount++\n } catch (err) {\n log.error(\"failed to expand account provider\", {\n account,\n providerID,\n error: String(err),\n })\n }\n }\n\n if (expandedCount > 0) {\n delete config.provider[PROVIDER_ID]\n }\n\n return expandedCount > 0\n}\n\nconst server: OpenCodePlugin = async (input) => {\n cleanupStaleUnscopedInstall()\n\n // Capture the SDK client so the language model can query opencode's\n // in-memory MCP state per-turn for the runtime overlay. `input` is\n // `unknown` here (kept loose since opencode adds fields over time);\n // narrow defensively.\n if (input && typeof input === \"object\" && \"client\" in input) {\n setOpencodeClient((input as { client?: unknown }).client)\n }\n\n // Capture opencode's project-aware directory as a *fallback* used at\n // Claude CLI spawn time only when `process.cwd()` is unusable. Rescues\n // macOS GUI launches at `/` without freezing the value into provider\n // config, so opencode workspace switches mid-session still take effect.\n // See `resolveSpawnCwd` in runtime-status.ts and issue #4.\n setOpencodeProjectDirectory(pickOpencodeDirectory(input))\n\n return {\n config: async (config) => {\n config.provider ??= {}\n\n const expanded = await expandAccountProviders(config)\n if (expanded) {\n const registered = Object.entries(config.provider)\n .filter(([id]) => id === PROVIDER_ID || id.startsWith(`${PROVIDER_ID}-`))\n .map(([id, p]) => ({\n id,\n name: p?.name ?? id,\n cwd: (p?.options as { cwd?: unknown } | undefined)?.cwd,\n }))\n log.notice(\"registered claude-code providers\", { providers: registered })\n return\n }\n\n const existing = config.provider[PROVIDER_ID]\n config.provider[PROVIDER_ID] = {\n ...existing,\n ...(await providerConfig(existing)),\n models: configModelsForProvider(\n (existing?.models ?? {}) as OpenCodeProvider[\"models\"],\n PROVIDER_ID,\n ),\n }\n log.notice(\"registered claude-code provider\", {\n id: PROVIDER_ID,\n name: config.provider[PROVIDER_ID]?.name ?? PROVIDER_ID,\n cwd: (config.provider[PROVIDER_ID]?.options as { cwd?: unknown } | undefined)?.cwd,\n })\n },\n // No `event` hook: MCP config drift is detected at turn start by the\n // hot-reload check in `claude-code-language-model.ts`, which respawns\n // claude safely between turns. Eviction on `global.disposed` would kill\n // an in-flight stream and abort the user's current turn.\n provider: {\n id: PROVIDER_ID,\n models: async (provider) => defaultModelsForProvider(provider.models),\n },\n // Inject opencode's agent name into providerOptions so the language\n // model can distinguish /compact (and title) calls from normal turns.\n // Without this, every no-tools call looks like a title request and\n // gets short-circuited to a synthetic stub.\n \"chat.params\": async (input, output) => {\n const providerID = input.model?.providerID ?? input.provider?.info?.id\n // The hook fires for every provider opencode is configured with, not\n // just ours — keep this at debug to avoid log spam on non-claude-code\n // calls.\n log.debug(\"chat.params hook fired\", {\n agent: input.agent,\n providerID,\n sessionID: input.sessionID,\n })\n if (typeof providerID !== \"string\") return\n if (providerID !== PROVIDER_ID && !providerID.startsWith(`${PROVIDER_ID}-`)) return\n\n // Inject sessionID BEFORE the agent guard so session isolation works\n // even when input.agent is absent (older opencode, provider-switch\n // edge paths). resolveSessionAffinity reads this as a fallback when\n // the x-session-affinity header is missing.\n if (typeof input.sessionID === \"string\" && input.sessionID.length > 0) {\n output.options ??= {}\n ;(output.options as Record<string, unknown>).opencodeSessionID = input.sessionID\n }\n\n if (!input.agent) return\n // opencode wraps the entire `output.options` bag under the providerID\n // via ProviderTransform.providerOptions(model, options) → { [providerID]: options }\n // before handing it to the language model as `providerOptions`. So we\n // write fields at the TOP LEVEL of output.options, not nested under\n // providerID — otherwise the model sees providerOptions[id][id].opencodeAgent.\n output.options ??= {}\n ;(output.options as Record<string, unknown>).opencodeAgent = input.agent\n log.debug(\"chat.params tagged providerOptions\", {\n agent: input.agent,\n sessionID: input.sessionID,\n providerID,\n })\n },\n }\n}\n\nexport default {\n id: \"@khalilgharbaoui/opencode-claude-code-plugin\",\n server,\n}\n\n// ---------------------------------------------------------------------------\n// Re-exports\n// ---------------------------------------------------------------------------\n\nexport { ClaudeCodeLanguageModel } from \"./claude-code-language-model.js\"\nexport { bridgeOpencodeMcp } from \"./mcp-bridge.js\"\nexport { defaultModels } from \"./models.js\"\nexport type {\n ClaudeCodeConfig,\n ClaudeCodeProviderSettings,\n ClaudeStreamMessage,\n} from \"./types.js\"\nexport type { OpenCodeHooks, OpenCodeModel, OpenCodePlugin } from \"./opencode-types.js\"\n"],"mappings":";AASA,SAAS,kBAAkB;;;ACT3B,SAAS,gBAAgB,WAAW,YAAY,gBAAgB;AAChE,SAAS,eAAe;AACxB,SAAS,SAAS,YAAY;AAY9B,IAAM,aAAuC;AAAA,EAC3C,OAAO;AAAA,EACP,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,OAAO;AACT;AAEA,IAAM,gBAAgB,IAAI,OAAO;AACjC,IAAM,cAAc,KAAK,QAAQ,GAAG,UAAU,SAAS,sBAAsB;AAE7E,IAAM,iBAA+B;AAAA,EACnC,MAAM;AAAA,EACN,KAAK;AAAA,EACL,MAAM;AAAA,EACN,OAAO;AACT;AAEA,SAAS,aAAa,GAA4C;AAChE,MAAI,KAAK,KAAM,QAAO;AACtB,QAAM,IAAI,EAAE,YAAY,EAAE,KAAK;AAC/B,MAAI,MAAM,GAAI,QAAO;AACrB,MAAI,MAAM,OAAO,MAAM,WAAW,MAAM,QAAQ,MAAM,MAAO,QAAO;AACpE,SAAO;AACT;AAEA,SAAS,cAAc,GAA6C;AAClE,MAAI,KAAK,KAAM,QAAO;AACtB,QAAM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AAAA,UACtBA;AAAA,QACF;AAAA,MACF;AACA,UAAI,OAAO,mCAAmC;AAAA,QAC5C,IAAIA;AAAA,QACJ,MAAM,OAAO,SAASA,YAAW,GAAG,QAAQA;AAAA,QAC5C,KAAM,OAAO,SAASA,YAAW,GAAG,SAA2C;AAAA,MACjF,CAAC;AAAA,IACH;AAAA;AAAA;AAAA;AAAA;AAAA,IAKA,UAAU;AAAA,MACR,IAAIA;AAAA,MACJ,QAAQ,OAAO,aAAa,yBAAyB,SAAS,MAAM;AAAA,IACtE;AAAA;AAAA;AAAA;AAAA;AAAA,IAKA,eAAe,OAAOE,QAAO,WAAW;AACtC,YAAM,aAAaA,OAAM,OAAO,cAAcA,OAAM,UAAU,MAAM;AAIpE,UAAI,MAAM,0BAA0B;AAAA,QAClC,OAAOA,OAAM;AAAA,QACb;AAAA,QACA,WAAWA,OAAM;AAAA,MACnB,CAAC;AACD,UAAI,OAAO,eAAe,SAAU;AACpC,UAAI,eAAeF,gBAAe,CAAC,WAAW,WAAW,GAAGA,YAAW,GAAG,EAAG;AAM7E,UAAI,OAAOE,OAAM,cAAc,YAAYA,OAAM,UAAU,SAAS,GAAG;AACrE,eAAO,YAAY,CAAC;AACnB,QAAC,OAAO,QAAoC,oBAAoBA,OAAM;AAAA,MACzE;AAEA,UAAI,CAACA,OAAM,MAAO;AAMlB,aAAO,YAAY,CAAC;AACnB,MAAC,OAAO,QAAoC,gBAAgBA,OAAM;AACnE,UAAI,MAAM,sCAAsC;AAAA,QAC9C,OAAOA,OAAM;AAAA,QACb,WAAWA,OAAM;AAAA,QACjB;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEA,IAAO,gBAAQ;AAAA,EACb,IAAI;AAAA,EACJ;AACF;","names":["fs","path","os","sessionKey","fs","path","crypto","EventEmitter","EventEmitter","server","resolve","EventEmitter","EventEmitter","sessionKey","readFileSync","writeFileSync","unlink","homedir","tmpdir","randomUUID","dirname","join","content","path","spawn","createInterface","resolve","stream","path","path","existsSync","readFileSync","rmSync","writeFileSync","homedir","join","resolve","join","homedir","existsSync","readFileSync","resolve","rmSync","writeFileSync","PROVIDER_ID","registered","input"]}