gsd-pi 2.75.0-dev.a44b82572 → 2.75.0-dev.e41b70b10
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/resources/extensions/gsd/auto/phases.js +2 -0
- package/dist/resources/extensions/gsd/auto-dashboard.js +22 -1
- package/dist/resources/extensions/gsd/auto-direct-dispatch.js +8 -2
- package/dist/resources/extensions/gsd/auto-dispatch.js +11 -11
- package/dist/resources/extensions/gsd/auto-model-selection.js +3 -1
- package/dist/resources/extensions/gsd/auto-prompts.js +19 -9
- package/dist/resources/extensions/gsd/auto-worktree.js +16 -1
- package/dist/resources/extensions/gsd/doctor-git-checks.js +22 -2
- package/dist/resources/extensions/gsd/pre-execution-checks.js +12 -8
- package/dist/resources/extensions/gsd/prompts/add-tests.md +1 -0
- package/dist/resources/extensions/gsd/prompts/execute-task.md +1 -1
- package/dist/resources/extensions/gsd/prompts/plan-slice.md +1 -0
- package/dist/resources/extensions/gsd/tools/workflow-tool-executors.js +14 -0
- package/dist/resources/extensions/search-the-web/command-search-provider.js +4 -1
- package/dist/resources/extensions/search-the-web/native-search.js +13 -2
- package/dist/tsconfig.extensions.tsbuildinfo +1 -1
- package/dist/web/standalone/.next/BUILD_ID +1 -1
- package/dist/web/standalone/.next/app-path-routes-manifest.json +15 -15
- package/dist/web/standalone/.next/build-manifest.json +2 -2
- package/dist/web/standalone/.next/prerender-manifest.json +3 -3
- package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.html +1 -1
- package/dist/web/standalone/.next/server/app/index.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app-paths-manifest.json +15 -15
- package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
- package/dist/web/standalone/.next/server/middleware-manifest.json +5 -5
- package/dist/web/standalone/.next/server/pages/404.html +1 -1
- package/dist/web/standalone/.next/server/pages/500.html +1 -1
- package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
- package/package.json +1 -1
- package/packages/mcp-server/dist/workflow-tools.d.ts.map +1 -1
- package/packages/mcp-server/dist/workflow-tools.js +102 -65
- package/packages/mcp-server/dist/workflow-tools.js.map +1 -1
- package/packages/mcp-server/src/workflow-tools.test.ts +255 -0
- package/packages/mcp-server/src/workflow-tools.ts +108 -65
- package/packages/mcp-server/tsconfig.tsbuildinfo +1 -1
- package/packages/pi-agent-core/tsconfig.tsbuildinfo +1 -1
- package/packages/pi-ai/dist/index.d.ts +1 -0
- package/packages/pi-ai/dist/index.d.ts.map +1 -1
- package/packages/pi-ai/dist/index.js +1 -0
- package/packages/pi-ai/dist/index.js.map +1 -1
- package/packages/pi-ai/dist/providers/api-family.d.ts +27 -0
- package/packages/pi-ai/dist/providers/api-family.d.ts.map +1 -0
- package/packages/pi-ai/dist/providers/api-family.js +47 -0
- package/packages/pi-ai/dist/providers/api-family.js.map +1 -0
- package/packages/pi-ai/dist/providers/api-family.test.d.ts +2 -0
- package/packages/pi-ai/dist/providers/api-family.test.d.ts.map +1 -0
- package/packages/pi-ai/dist/providers/api-family.test.js +101 -0
- package/packages/pi-ai/dist/providers/api-family.test.js.map +1 -0
- package/packages/pi-ai/src/index.ts +1 -0
- package/packages/pi-ai/src/providers/api-family.test.ts +129 -0
- package/packages/pi-ai/src/providers/api-family.ts +57 -0
- package/packages/pi-ai/tsconfig.tsbuildinfo +1 -1
- package/packages/pi-coding-agent/dist/core/extensions/runner.d.ts +1 -0
- package/packages/pi-coding-agent/dist/core/extensions/runner.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/core/extensions/runner.js.map +1 -1
- package/packages/pi-coding-agent/dist/core/extensions/types.d.ts +2 -1
- package/packages/pi-coding-agent/dist/core/extensions/types.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/core/extensions/types.js.map +1 -1
- package/packages/pi-coding-agent/dist/core/retry-handler.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/core/retry-handler.js +4 -1
- package/packages/pi-coding-agent/dist/core/retry-handler.js.map +1 -1
- package/packages/pi-coding-agent/src/core/extensions/runner.ts +4 -1
- package/packages/pi-coding-agent/src/core/extensions/types.ts +2 -2
- package/packages/pi-coding-agent/src/core/retry-handler.ts +4 -1
- package/packages/pi-coding-agent/tsconfig.tsbuildinfo +1 -1
- package/src/resources/extensions/gsd/auto/loop-deps.ts +2 -10
- package/src/resources/extensions/gsd/auto/phases.ts +3 -0
- package/src/resources/extensions/gsd/auto-dashboard.ts +25 -1
- package/src/resources/extensions/gsd/auto-direct-dispatch.ts +15 -2
- package/src/resources/extensions/gsd/auto-dispatch.ts +21 -7
- package/src/resources/extensions/gsd/auto-model-selection.ts +3 -1
- package/src/resources/extensions/gsd/auto-prompts.ts +33 -9
- package/src/resources/extensions/gsd/auto-worktree.ts +16 -1
- package/src/resources/extensions/gsd/doctor-git-checks.ts +23 -2
- package/src/resources/extensions/gsd/pre-execution-checks.ts +12 -8
- package/src/resources/extensions/gsd/prompts/add-tests.md +1 -0
- package/src/resources/extensions/gsd/prompts/execute-task.md +1 -1
- package/src/resources/extensions/gsd/prompts/plan-slice.md +1 -0
- package/src/resources/extensions/gsd/tests/auto-dashboard.test.ts +49 -0
- package/src/resources/extensions/gsd/tests/integration/doctor-git-symlink-cwd.test.ts +79 -0
- package/src/resources/extensions/gsd/tests/pre-execution-checks.test.ts +66 -0
- package/src/resources/extensions/gsd/tests/prompt-budget-enforcement.test.ts +132 -8
- package/src/resources/extensions/gsd/tests/prompts-no-gitignored-test-refs.test.ts +56 -0
- package/src/resources/extensions/gsd/tests/stash-queued-context-files.test.ts +54 -0
- package/src/resources/extensions/gsd/tests/workflow-tool-executors.test.ts +97 -0
- package/src/resources/extensions/gsd/tools/workflow-tool-executors.ts +14 -0
- package/src/resources/extensions/search-the-web/command-search-provider.ts +4 -1
- package/src/resources/extensions/search-the-web/native-search.ts +13 -3
- /package/dist/web/standalone/.next/static/{iBwPQUj73sn8jxegTo320 → By_yegSJ-AA1OP0QjYbSl}/_buildManifest.js +0 -0
- /package/dist/web/standalone/.next/static/{iBwPQUj73sn8jxegTo320 → By_yegSJ-AA1OP0QjYbSl}/_ssgManifest.js +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/core/extensions/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AA21BH,MAAM,UAAU,qBAAqB,CAAC,QAAgB,EAAE,KAAsB;IAC7E,OAAO,KAAK,CAAC,QAAQ,KAAK,QAAQ,CAAC;AACpC,CAAC;AAiCD,MAAM,UAAU,mBAAmB,CAAC,QAAgB,EAAE,KAAoB;IACzE,OAAO,KAAK,CAAC,QAAQ,KAAK,QAAQ,CAAC;AACpC,CAAC","sourcesContent":["/**\n * Extension system types.\n *\n * Extensions are TypeScript modules that can:\n * - Subscribe to agent lifecycle events\n * - Register LLM-callable tools\n * - Register commands, keyboard shortcuts, and CLI flags\n * - Interact with the user via UI primitives\n */\n\nimport type {\n\tAgentMessage,\n\tAgentToolResult,\n\tAgentToolUpdateCallback,\n\tThinkingLevel,\n} from \"@gsd/pi-agent-core\";\nimport type {\n\tApi,\n\tAssistantMessageEvent,\n\tAssistantMessageEventStream,\n\tContext,\n\tImageContent,\n\tModel,\n\tOAuthCredentials,\n\tOAuthLoginCallbacks,\n\tSimpleStreamOptions,\n\tTextContent,\n\tToolResultMessage,\n} from \"@gsd/pi-ai\";\nimport type {\n\tAutocompleteItem,\n\tComponent,\n\tEditorComponent,\n\tEditorTheme,\n\tKeyId,\n\tOverlayHandle,\n\tOverlayOptions,\n\tTUI,\n} from \"@gsd/pi-tui\";\nimport type { Static, TSchema } from \"@sinclair/typebox\";\nimport type { Theme } from \"../../modes/interactive/theme/theme.js\";\nimport type { BashResult } from \"../bash-executor.js\";\nimport type { CompactionPreparation, CompactionResult } from \"../compaction/index.js\";\nimport type { EventBus } from \"../event-bus.js\";\nimport type { ExecOptions, ExecResult } from \"../exec.js\";\nimport type { ReadonlyFooterDataProvider } from \"../footer-data-provider.js\";\nimport type { KeybindingsManager } from \"../keybindings.js\";\nimport type { CustomMessage } from \"../messages.js\";\nimport type { ModelRegistry } from \"../model-registry.js\";\nimport type {\n\tBranchSummaryEntry,\n\tCompactionEntry,\n\tReadonlySessionManager,\n\tSessionEntry,\n\tSessionManager,\n} from \"../session-manager.js\";\nimport type { SlashCommandInfo } from \"../slash-commands.js\";\nimport type { BashOperations } from \"../tools/bash.js\";\nimport type { EditToolDetails } from \"../tools/edit.js\";\nimport type {\n\tBashToolDetails,\n\tBashToolInput,\n\tEditToolInput,\n\tFindToolDetails,\n\tFindToolInput,\n\tGrepToolDetails,\n\tGrepToolInput,\n\tLsToolDetails,\n\tLsToolInput,\n\tReadToolDetails,\n\tReadToolInput,\n\tWriteToolInput,\n} from \"../tools/index.js\";\n\nexport type { ExecOptions, ExecResult } from \"../exec.js\";\nexport type { AgentToolResult, AgentToolUpdateCallback };\nexport type { AppAction, KeybindingsManager } from \"../keybindings.js\";\n\n// ============================================================================\n// UI Context\n// ============================================================================\n\n/** Options for extension UI dialogs. */\nexport interface ExtensionUIDialogOptions {\n\t/** AbortSignal to programmatically dismiss the dialog. */\n\tsignal?: AbortSignal;\n\t/** Timeout in milliseconds. Dialog auto-dismisses with live countdown display. */\n\ttimeout?: number;\n\t/** When true, the user can select multiple options. The return type becomes `string[]`. */\n\tallowMultiple?: boolean;\n\t/** When true, text input dialogs should hide typed characters if supported by the client surface. */\n\tsecure?: boolean;\n}\n\n/** Placement for extension widgets. */\nexport type WidgetPlacement = \"aboveEditor\" | \"belowEditor\";\n\n/** Options for extension widgets. */\nexport interface ExtensionWidgetOptions {\n\t/** Where the widget is rendered. Defaults to \"aboveEditor\". */\n\tplacement?: WidgetPlacement;\n}\n\n/** Raw terminal input listener for extensions. */\nexport type TerminalInputHandler = (data: string) => { consume?: boolean; data?: string } | undefined;\n\n/**\n * UI context for extensions to request interactive UI.\n * Each mode (interactive, RPC, print) provides its own implementation.\n */\nexport interface ExtensionUIContext {\n\t/** Show a selector and return the user's choice. When `opts.allowMultiple` is true, returns an array. */\n\tselect(title: string, options: string[], opts?: ExtensionUIDialogOptions): Promise<string | string[] | undefined>;\n\n\t/** Show a confirmation dialog. */\n\tconfirm(title: string, message: string, opts?: ExtensionUIDialogOptions): Promise<boolean>;\n\n\t/** Show a text input dialog. */\n\tinput(title: string, placeholder?: string, opts?: ExtensionUIDialogOptions): Promise<string | undefined>;\n\n\t/** Show a notification to the user. */\n\tnotify(message: string, type?: \"info\" | \"warning\" | \"error\" | \"success\"): void;\n\n\t/** Listen to raw terminal input (interactive mode only). Returns an unsubscribe function. */\n\tonTerminalInput(handler: TerminalInputHandler): () => void;\n\n\t/** Set status text in the footer/status bar. Pass undefined to clear. */\n\tsetStatus(key: string, text: string | undefined): void;\n\n\t/** Set the working/loading message shown during streaming. Call with no argument to restore default. */\n\tsetWorkingMessage(message?: string): void;\n\n\t/** Set a widget to display above or below the editor. Accepts string array or component factory. */\n\tsetWidget(key: string, content: string[] | undefined, options?: ExtensionWidgetOptions): void;\n\tsetWidget(\n\t\tkey: string,\n\t\tcontent: ((tui: TUI, theme: Theme) => Component & { dispose?(): void }) | undefined,\n\t\toptions?: ExtensionWidgetOptions,\n\t): void;\n\n\t/** Set a custom footer component, or undefined to restore the built-in footer.\n\t *\n\t * The factory receives a FooterDataProvider for data not otherwise accessible:\n\t * git branch and extension statuses from setStatus(). Token stats, model info,\n\t * etc. are available via ctx.sessionManager and ctx.model.\n\t */\n\tsetFooter(\n\t\tfactory:\n\t\t\t| ((tui: TUI, theme: Theme, footerData: ReadonlyFooterDataProvider) => Component & { dispose?(): void })\n\t\t\t| undefined,\n\t): void;\n\n\t/** Set a custom header component (shown at startup, above chat), or undefined to restore the built-in header. */\n\tsetHeader(factory: ((tui: TUI, theme: Theme) => Component & { dispose?(): void }) | undefined): void;\n\n\t/** Set the terminal window/tab title. */\n\tsetTitle(title: string): void;\n\n\t/** Show a custom component with keyboard focus. */\n\tcustom<T>(\n\t\tfactory: (\n\t\t\ttui: TUI,\n\t\t\ttheme: Theme,\n\t\t\tkeybindings: KeybindingsManager,\n\t\t\tdone: (result: T) => void,\n\t\t) => (Component & { dispose?(): void }) | Promise<Component & { dispose?(): void }>,\n\t\toptions?: {\n\t\t\toverlay?: boolean;\n\t\t\t/** Overlay positioning/sizing options. Can be static or a function for dynamic updates. */\n\t\t\toverlayOptions?: OverlayOptions | (() => OverlayOptions);\n\t\t\t/** Called with the overlay handle after the overlay is shown. Use to control visibility. */\n\t\t\tonHandle?: (handle: OverlayHandle) => void;\n\t\t},\n\t): Promise<T>;\n\n\t/** Paste text into the editor, triggering paste handling (collapse for large content). */\n\tpasteToEditor(text: string): void;\n\n\t/** Set the text in the core input editor. */\n\tsetEditorText(text: string): void;\n\n\t/** Get the current text from the core input editor. */\n\tgetEditorText(): string;\n\n\t/** Show a multi-line editor for text editing. */\n\teditor(title: string, prefill?: string): Promise<string | undefined>;\n\n\t/**\n\t * Set a custom editor component via factory function.\n\t * Pass undefined to restore the default editor.\n\t *\n\t * The factory receives:\n\t * - `theme`: EditorTheme for styling borders and autocomplete\n\t * - `keybindings`: KeybindingsManager for app-level keybindings\n\t *\n\t * For full app keybinding support (escape, ctrl+d, model switching, etc.),\n\t * extend `CustomEditor` from `@gsd/pi-coding-agent` and call\n\t * `super.handleInput(data)` for keys you don't handle.\n\t *\n\t * @example\n\t * ```ts\n\t * import { CustomEditor } from \"@gsd/pi-coding-agent\";\n\t *\n\t * class VimEditor extends CustomEditor {\n\t * private mode: \"normal\" | \"insert\" = \"insert\";\n\t *\n\t * handleInput(data: string): void {\n\t * if (this.mode === \"normal\") {\n\t * // Handle vim normal mode keys...\n\t * if (data === \"i\") { this.mode = \"insert\"; return; }\n\t * }\n\t * super.handleInput(data); // App keybindings + text editing\n\t * }\n\t * }\n\t *\n\t * ctx.ui.setEditorComponent((tui, theme, keybindings) =>\n\t * new VimEditor(tui, theme, keybindings)\n\t * );\n\t * ```\n\t */\n\tsetEditorComponent(\n\t\tfactory: ((tui: TUI, theme: EditorTheme, keybindings: KeybindingsManager) => EditorComponent) | undefined,\n\t): void;\n\n\t/** Get the current theme for styling. */\n\treadonly theme: Theme;\n\n\t/** Get all available themes with their names and file paths. */\n\tgetAllThemes(): { name: string; path: string | undefined }[];\n\n\t/** Load a theme by name without switching to it. Returns undefined if not found. */\n\tgetTheme(name: string): Theme | undefined;\n\n\t/** Set the current theme by name or Theme object. */\n\tsetTheme(theme: string | Theme): { success: boolean; error?: string };\n\n\t/** Get current tool output expansion state. */\n\tgetToolsExpanded(): boolean;\n\n\t/** Set tool output expansion state. */\n\tsetToolsExpanded(expanded: boolean): void;\n}\n\n// ============================================================================\n// Extension Context\n// ============================================================================\n\nexport interface ContextUsage {\n\t/** Estimated context tokens, or null if unknown (e.g. right after compaction, before next LLM response). */\n\ttokens: number | null;\n\tcontextWindow: number;\n\t/** Context usage as percentage of context window, or null if tokens is unknown. */\n\tpercent: number | null;\n}\n\nexport interface CompactOptions {\n\tcustomInstructions?: string;\n\tonComplete?: (result: CompactionResult) => void;\n\tonError?: (error: Error) => void;\n}\n\n/**\n * Context passed to extension event handlers.\n */\nexport interface ExtensionContext {\n\t/** UI methods for user interaction */\n\tui: ExtensionUIContext;\n\t/** Whether UI is available (false in print/RPC mode) */\n\thasUI: boolean;\n\t/** Current working directory */\n\tcwd: string;\n\t/** Session manager (read-only) */\n\tsessionManager: ReadonlySessionManager;\n\t/** Model registry for API key resolution */\n\tmodelRegistry: ModelRegistry;\n\t/** Current model (may be undefined) */\n\tmodel: Model<any> | undefined;\n\t/** Whether the agent is idle (not streaming) */\n\tisIdle(): boolean;\n\t/** Abort the current agent operation */\n\tabort(): void;\n\t/** Whether there are queued messages waiting */\n\thasPendingMessages(): boolean;\n\t/** Gracefully shutdown pi and exit. Available in all contexts. */\n\tshutdown(): void;\n\t/** Get current context usage for the active model. */\n\tgetContextUsage(): ContextUsage | undefined;\n\t/** Trigger compaction without awaiting completion. */\n\tcompact(options?: CompactOptions): void;\n\t/** Get the current effective system prompt. */\n\tgetSystemPrompt(): string;\n}\n\n/**\n * Extended context for command handlers.\n * Includes session control methods only safe in user-initiated commands.\n */\nexport interface ExtensionCommandContext extends ExtensionContext {\n\t/** Wait for the agent to finish streaming */\n\twaitForIdle(): Promise<void>;\n\n\t/** Start a new session, optionally with initialization. */\n\tnewSession(options?: {\n\t\tparentSession?: string;\n\t\tsetup?: (sessionManager: SessionManager) => Promise<void>;\n\t}): Promise<{ cancelled: boolean }>;\n\n\t/** Fork from a specific entry, creating a new session file. */\n\tfork(entryId: string): Promise<{ cancelled: boolean }>;\n\n\t/** Navigate to a different point in the session tree. */\n\tnavigateTree(\n\t\ttargetId: string,\n\t\toptions?: { summarize?: boolean; customInstructions?: string; replaceInstructions?: boolean; label?: string },\n\t): Promise<{ cancelled: boolean }>;\n\n\t/** Switch to a different session file. */\n\tswitchSession(sessionPath: string): Promise<{ cancelled: boolean }>;\n\n\t/** Reload extensions, skills, prompts, and themes. */\n\treload(): Promise<void>;\n}\n\n// ============================================================================\n// Tool Types\n// ============================================================================\n\n/** Rendering options for tool results */\nexport interface ToolRenderResultOptions {\n\t/** Whether the result view is expanded */\n\texpanded: boolean;\n\t/** Whether this is a partial/streaming result */\n\tisPartial: boolean;\n}\n\n/**\n * Tool compatibility metadata for provider-aware tool filtering (ADR-005 Phase 2).\n * Tools without compatibility metadata are assumed universally compatible.\n */\nexport interface ToolCompatibility {\n\t/** Tool produces image content in results (filtered for providers without imageToolResults) */\n\tproducesImages?: boolean;\n\t/** Tool requires schema features that some providers don't support (e.g., [\"patternProperties\"]) */\n\tschemaFeatures?: string[];\n\t/** Tool is effective only with models above a minimum capability threshold */\n\tminCapabilityTier?: \"light\" | \"standard\" | \"heavy\";\n}\n\n/**\n * Tool definition for registerTool().\n */\nexport interface ToolDefinition<TParams extends TSchema = TSchema, TDetails = unknown> {\n\t/** Tool name (used in LLM tool calls) */\n\tname: string;\n\t/** Human-readable label for UI */\n\tlabel: string;\n\t/** Description for LLM */\n\tdescription: string;\n\t/** Optional one-line snippet for the Available tools section in the default system prompt. Falls back to description when omitted. */\n\tpromptSnippet?: string;\n\t/** Optional guideline bullets appended to the default system prompt Guidelines section when this tool is active. */\n\tpromptGuidelines?: string[];\n\t/** Parameter schema (TypeBox) */\n\tparameters: TParams;\n\t/** Provider compatibility metadata (ADR-005). Omit for universally compatible tools. */\n\tcompatibility?: ToolCompatibility;\n\n\t/** Execute the tool. */\n\texecute(\n\t\ttoolCallId: string,\n\t\tparams: Static<TParams>,\n\t\tsignal: AbortSignal | undefined,\n\t\tonUpdate: AgentToolUpdateCallback<TDetails> | undefined,\n\t\tctx: ExtensionContext,\n\t): Promise<AgentToolResult<TDetails>>;\n\n\t/** Custom rendering for tool call display */\n\trenderCall?: (args: Static<TParams>, theme: Theme) => Component | undefined;\n\n\t/** Custom rendering for tool result display */\n\trenderResult?: (\n\t\tresult: AgentToolResult<TDetails>,\n\t\toptions: ToolRenderResultOptions,\n\t\ttheme: Theme,\n\t) => Component | undefined;\n}\n\n// ============================================================================\n// Resource Events\n// ============================================================================\n\n/** Fired after session_start to allow extensions to provide additional resource paths. */\nexport interface ResourcesDiscoverEvent {\n\ttype: \"resources_discover\";\n\tcwd: string;\n\treason: \"startup\" | \"reload\";\n}\n\n/** Result from resources_discover event handler */\nexport interface ResourcesDiscoverResult {\n\tskillPaths?: string[];\n\tpromptPaths?: string[];\n\tthemePaths?: string[];\n}\n\n// ============================================================================\n// Session Events\n// ============================================================================\n\n/** Fired before session manager creation to allow custom session directory resolution */\nexport interface SessionDirectoryEvent {\n\ttype: \"session_directory\";\n\tcwd: string;\n}\n\n/** Fired on initial session load */\nexport interface SessionStartEvent {\n\ttype: \"session_start\";\n}\n\n/** Fired before switching to another session (can be cancelled) */\nexport interface SessionBeforeSwitchEvent {\n\ttype: \"session_before_switch\";\n\treason: \"new\" | \"resume\";\n\ttargetSessionFile?: string;\n}\n\n/** Fired after switching to another session */\nexport interface SessionSwitchEvent {\n\ttype: \"session_switch\";\n\treason: \"new\" | \"resume\";\n\tpreviousSessionFile: string | undefined;\n}\n\n/** Fired before forking a session (can be cancelled) */\nexport interface SessionBeforeForkEvent {\n\ttype: \"session_before_fork\";\n\tentryId: string;\n}\n\n/** Fired after forking a session */\nexport interface SessionForkEvent {\n\ttype: \"session_fork\";\n\tpreviousSessionFile: string | undefined;\n}\n\n/** Fired before context compaction (can be cancelled or customized) */\nexport interface SessionBeforeCompactEvent {\n\ttype: \"session_before_compact\";\n\tpreparation: CompactionPreparation;\n\tbranchEntries: SessionEntry[];\n\tcustomInstructions?: string;\n\tsignal: AbortSignal;\n}\n\n/** Fired after context compaction */\nexport interface SessionCompactEvent {\n\ttype: \"session_compact\";\n\tcompactionEntry: CompactionEntry;\n\tfromExtension: boolean;\n}\n\n/** Fired on process exit */\nexport interface SessionShutdownEvent {\n\ttype: \"session_shutdown\";\n}\n\n/** Preparation data for tree navigation */\nexport interface TreePreparation {\n\ttargetId: string;\n\toldLeafId: string | null;\n\tcommonAncestorId: string | null;\n\tentriesToSummarize: SessionEntry[];\n\tuserWantsSummary: boolean;\n\t/** Custom instructions for summarization */\n\tcustomInstructions?: string;\n\t/** If true, customInstructions replaces the default prompt instead of being appended */\n\treplaceInstructions?: boolean;\n\t/** Label to attach to the branch summary entry */\n\tlabel?: string;\n}\n\n/** Fired before navigating in the session tree (can be cancelled) */\nexport interface SessionBeforeTreeEvent {\n\ttype: \"session_before_tree\";\n\tpreparation: TreePreparation;\n\tsignal: AbortSignal;\n}\n\n/** Fired after navigating in the session tree */\nexport interface SessionTreeEvent {\n\ttype: \"session_tree\";\n\tnewLeafId: string | null;\n\toldLeafId: string | null;\n\tsummaryEntry?: BranchSummaryEntry;\n\tfromExtension?: boolean;\n}\n\nexport type SessionEvent =\n\t| SessionDirectoryEvent\n\t| SessionStartEvent\n\t| SessionBeforeSwitchEvent\n\t| SessionSwitchEvent\n\t| SessionBeforeForkEvent\n\t| SessionForkEvent\n\t| SessionBeforeCompactEvent\n\t| SessionCompactEvent\n\t| SessionShutdownEvent\n\t| SessionBeforeTreeEvent\n\t| SessionTreeEvent;\n\n// ============================================================================\n// Agent Events\n// ============================================================================\n\n/** Fired before each LLM call. Can modify messages. */\nexport interface ContextEvent {\n\ttype: \"context\";\n\tmessages: AgentMessage[];\n}\n\n/** Fired before a provider request is sent. Can replace the payload. */\nexport interface BeforeProviderRequestEvent {\n\ttype: \"before_provider_request\";\n\tpayload: unknown;\n\t/** The resolved model for this request (provider, id, etc.) */\n\tmodel?: { provider: string; id: string };\n}\n\n/** Fired after user submits prompt but before agent loop. */\nexport interface BeforeAgentStartEvent {\n\ttype: \"before_agent_start\";\n\tprompt: string;\n\timages?: ImageContent[];\n\tsystemPrompt: string;\n}\n\n/** Fired when an agent loop starts */\nexport interface AgentStartEvent {\n\ttype: \"agent_start\";\n}\n\n/** Fired when an agent loop ends */\nexport interface AgentEndEvent {\n\ttype: \"agent_end\";\n\tmessages: AgentMessage[];\n}\n\n/** Fired at the start of each turn */\nexport interface TurnStartEvent {\n\ttype: \"turn_start\";\n\tturnIndex: number;\n\ttimestamp: number;\n}\n\n/** Fired at the end of each turn */\nexport interface TurnEndEvent {\n\ttype: \"turn_end\";\n\tturnIndex: number;\n\tmessage: AgentMessage;\n\ttoolResults: ToolResultMessage[];\n}\n\n/** Fired when a message starts (user, assistant, or toolResult) */\nexport interface MessageStartEvent {\n\ttype: \"message_start\";\n\tmessage: AgentMessage;\n}\n\n/** Fired during assistant message streaming with token-by-token updates */\nexport interface MessageUpdateEvent {\n\ttype: \"message_update\";\n\tmessage: AgentMessage;\n\tassistantMessageEvent: AssistantMessageEvent;\n}\n\n/** Fired when a message ends */\nexport interface MessageEndEvent {\n\ttype: \"message_end\";\n\tmessage: AgentMessage;\n}\n\n/** Fired when a tool starts executing */\nexport interface ToolExecutionStartEvent {\n\ttype: \"tool_execution_start\";\n\ttoolCallId: string;\n\ttoolName: string;\n\targs: any;\n}\n\n/** Fired during tool execution with partial/streaming output */\nexport interface ToolExecutionUpdateEvent {\n\ttype: \"tool_execution_update\";\n\ttoolCallId: string;\n\ttoolName: string;\n\targs: any;\n\tpartialResult: any;\n}\n\n/** Fired when a tool finishes executing */\nexport interface ToolExecutionEndEvent {\n\ttype: \"tool_execution_end\";\n\ttoolCallId: string;\n\ttoolName: string;\n\tresult: any;\n\tisError: boolean;\n}\n\n// ============================================================================\n// Model Events\n// ============================================================================\n\nexport type ModelSelectSource = \"set\" | \"cycle\" | \"restore\";\n\n/** Fired when a new model is selected */\nexport interface ModelSelectEvent {\n\ttype: \"model_select\";\n\tmodel: Model<any>;\n\tpreviousModel: Model<any> | undefined;\n\tsource: ModelSelectSource;\n}\n\n/** Fired before model selection runs capability scoring. Extensions can override the selected model. */\nexport interface BeforeModelSelectEvent {\n\ttype: \"before_model_select\";\n\tunitType: string;\n\tunitId: string;\n\tclassification: { tier: string; reason: string; downgraded: boolean };\n\ttaskMetadata?: Record<string, unknown>;\n\teligibleModels: string[];\n\tphaseConfig?: { primary: string; fallbacks: string[] };\n}\n\n/** Result from before_model_select event handler. Return { modelId } to override selection. */\nexport interface BeforeModelSelectResult {\n\tmodelId: string;\n}\n\n/**\n * Fired after model selection to allow extensions to adjust the active tool set (ADR-005 Phase 4).\n * Extensions can add, remove, or reorder tools based on the selected model's provider capabilities.\n */\nexport interface AdjustToolSetEvent {\n\ttype: \"adjust_tool_set\";\n\t/** The selected model's API type */\n\tselectedModelApi: string;\n\t/** The selected model's provider */\n\tselectedModelProvider: string;\n\t/** The selected model ID */\n\tselectedModelId: string;\n\t/** Current active tool names */\n\tactiveToolNames: string[];\n\t/** Tools already filtered by provider compatibility */\n\tfilteredTools: string[];\n}\n\n/** Result from adjust_tool_set event handler. Return { toolNames } to override tool set. */\nexport interface AdjustToolSetResult {\n\t/** Replacement tool names. If omitted, the default filtering is used. */\n\ttoolNames?: string[];\n}\n\n// ============================================================================\n// User Bash Events\n// ============================================================================\n\n/**\n * Fired before the bash tool executes a shell command.\n * Extensions can return a transformed command string.\n * All registered handlers are called in order; each receives the output of the previous.\n */\nexport interface BashTransformEvent {\n\ttype: \"bash_transform\";\n\t/** The command string about to be executed */\n\tcommand: string;\n\t/** Current working directory */\n\tcwd: string;\n}\n\n/** Result from bash_transform event handler */\nexport interface BashTransformEventResult {\n\t/** Replacement command string. If omitted or empty, the original command is used. */\n\tcommand?: string;\n}\n\n/** Fired when user executes a bash command via ! or !! prefix */\nexport interface UserBashEvent {\n\ttype: \"user_bash\";\n\t/** The command to execute */\n\tcommand: string;\n\t/** True if !! prefix was used (excluded from LLM context) */\n\texcludeFromContext: boolean;\n\t/** Current working directory */\n\tcwd: string;\n}\n\n// ============================================================================\n// Input Events\n// ============================================================================\n\n/** Source of user input */\nexport type InputSource = \"interactive\" | \"rpc\" | \"extension\";\n\n/** Fired when user input is received, before agent processing */\nexport interface InputEvent {\n\ttype: \"input\";\n\t/** The input text */\n\ttext: string;\n\t/** Attached images, if any */\n\timages?: ImageContent[];\n\t/** Where the input came from */\n\tsource: InputSource;\n}\n\n/** Result from input event handler */\nexport type InputEventResult =\n\t| { action: \"continue\" }\n\t| { action: \"transform\"; text: string; images?: ImageContent[] }\n\t| { action: \"handled\" };\n\n// ============================================================================\n// Tool Events\n// ============================================================================\n\ninterface ToolCallEventBase {\n\ttype: \"tool_call\";\n\ttoolCallId: string;\n}\n\nexport interface BashToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"bash\";\n\tinput: BashToolInput;\n}\n\nexport interface ReadToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"read\";\n\tinput: ReadToolInput;\n}\n\nexport interface EditToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"edit\";\n\tinput: EditToolInput;\n}\n\nexport interface WriteToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"write\";\n\tinput: WriteToolInput;\n}\n\nexport interface GrepToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"grep\";\n\tinput: GrepToolInput;\n}\n\nexport interface FindToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"find\";\n\tinput: FindToolInput;\n}\n\nexport interface LsToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"ls\";\n\tinput: LsToolInput;\n}\n\nexport interface CustomToolCallEvent extends ToolCallEventBase {\n\ttoolName: string;\n\tinput: Record<string, unknown>;\n}\n\n/** Fired before a tool executes. Can block. */\nexport type ToolCallEvent =\n\t| BashToolCallEvent\n\t| ReadToolCallEvent\n\t| EditToolCallEvent\n\t| WriteToolCallEvent\n\t| GrepToolCallEvent\n\t| FindToolCallEvent\n\t| LsToolCallEvent\n\t| CustomToolCallEvent;\n\ninterface ToolResultEventBase {\n\ttype: \"tool_result\";\n\ttoolCallId: string;\n\tinput: Record<string, unknown>;\n\tcontent: (TextContent | ImageContent)[];\n\tisError: boolean;\n}\n\nexport interface BashToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"bash\";\n\tdetails: BashToolDetails | undefined;\n}\n\nexport interface ReadToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"read\";\n\tdetails: ReadToolDetails | undefined;\n}\n\nexport interface EditToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"edit\";\n\tdetails: EditToolDetails | undefined;\n}\n\nexport interface WriteToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"write\";\n\tdetails: undefined;\n}\n\nexport interface GrepToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"grep\";\n\tdetails: GrepToolDetails | undefined;\n}\n\nexport interface FindToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"find\";\n\tdetails: FindToolDetails | undefined;\n}\n\nexport interface LsToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"ls\";\n\tdetails: LsToolDetails | undefined;\n}\n\nexport interface CustomToolResultEvent extends ToolResultEventBase {\n\ttoolName: string;\n\tdetails: unknown;\n}\n\n/** Fired after a tool executes. Can modify result. */\nexport type ToolResultEvent =\n\t| BashToolResultEvent\n\t| ReadToolResultEvent\n\t| EditToolResultEvent\n\t| WriteToolResultEvent\n\t| GrepToolResultEvent\n\t| FindToolResultEvent\n\t| LsToolResultEvent\n\t| CustomToolResultEvent;\n\n/**\n * Type guard for narrowing ToolResultEvent by tool name.\n *\n * Built-in tools narrow automatically (no type params needed):\n * ```ts\n * if (isToolResultEventType(\"bash\", event)) {\n * event.details; // BashToolDetails | undefined\n * }\n * ```\n *\n * Custom tools require explicit type parameters:\n * ```ts\n * if (isToolResultEventType<\"my_tool\", MyDetails>(\"my_tool\", event)) {\n * event.details; // typed\n * }\n * ```\n */\nexport function isToolResultEventType(toolName: \"bash\", event: ToolResultEvent): event is BashToolResultEvent;\nexport function isToolResultEventType(toolName: \"read\", event: ToolResultEvent): event is ReadToolResultEvent;\nexport function isToolResultEventType(toolName: \"edit\", event: ToolResultEvent): event is EditToolResultEvent;\nexport function isToolResultEventType(toolName: \"write\", event: ToolResultEvent): event is WriteToolResultEvent;\nexport function isToolResultEventType(toolName: \"grep\", event: ToolResultEvent): event is GrepToolResultEvent;\nexport function isToolResultEventType(toolName: \"find\", event: ToolResultEvent): event is FindToolResultEvent;\nexport function isToolResultEventType(toolName: \"ls\", event: ToolResultEvent): event is LsToolResultEvent;\nexport function isToolResultEventType<TName extends string, TDetails = unknown>(\n\ttoolName: TName,\n\tevent: ToolResultEvent,\n): event is ToolResultEvent & { toolName: TName; details: TDetails };\nexport function isToolResultEventType(toolName: string, event: ToolResultEvent): boolean {\n\treturn event.toolName === toolName;\n}\n\n/**\n * Type guard for narrowing ToolCallEvent by tool name.\n *\n * Built-in tools narrow automatically (no type params needed):\n * ```ts\n * if (isToolCallEventType(\"bash\", event)) {\n * event.input.command; // string\n * }\n * ```\n *\n * Custom tools require explicit type parameters:\n * ```ts\n * if (isToolCallEventType<\"my_tool\", MyToolInput>(\"my_tool\", event)) {\n * event.input.action; // typed\n * }\n * ```\n *\n * Note: Direct narrowing via `event.toolName === \"bash\"` doesn't work because\n * CustomToolCallEvent.toolName is `string` which overlaps with all literals.\n */\nexport function isToolCallEventType(toolName: \"bash\", event: ToolCallEvent): event is BashToolCallEvent;\nexport function isToolCallEventType(toolName: \"read\", event: ToolCallEvent): event is ReadToolCallEvent;\nexport function isToolCallEventType(toolName: \"edit\", event: ToolCallEvent): event is EditToolCallEvent;\nexport function isToolCallEventType(toolName: \"write\", event: ToolCallEvent): event is WriteToolCallEvent;\nexport function isToolCallEventType(toolName: \"grep\", event: ToolCallEvent): event is GrepToolCallEvent;\nexport function isToolCallEventType(toolName: \"find\", event: ToolCallEvent): event is FindToolCallEvent;\nexport function isToolCallEventType(toolName: \"ls\", event: ToolCallEvent): event is LsToolCallEvent;\nexport function isToolCallEventType<TName extends string, TInput extends Record<string, unknown>>(\n\ttoolName: TName,\n\tevent: ToolCallEvent,\n): event is ToolCallEvent & { toolName: TName; input: TInput };\nexport function isToolCallEventType(toolName: string, event: ToolCallEvent): boolean {\n\treturn event.toolName === toolName;\n}\n\n/** Union of all event types */\nexport type ExtensionEvent =\n\t| ResourcesDiscoverEvent\n\t| SessionEvent\n\t| ContextEvent\n\t| BeforeProviderRequestEvent\n\t| BeforeAgentStartEvent\n\t| AgentStartEvent\n\t| AgentEndEvent\n\t| TurnStartEvent\n\t| TurnEndEvent\n\t| MessageStartEvent\n\t| MessageUpdateEvent\n\t| MessageEndEvent\n\t| ToolExecutionStartEvent\n\t| ToolExecutionUpdateEvent\n\t| ToolExecutionEndEvent\n\t| ModelSelectEvent\n\t| BashTransformEvent\n\t| UserBashEvent\n\t| InputEvent\n\t| ToolCallEvent\n\t| ToolResultEvent;\n\n// ============================================================================\n// Event Results\n// ============================================================================\n\nexport interface ContextEventResult {\n\tmessages?: AgentMessage[];\n}\n\nexport type BeforeProviderRequestEventResult = unknown;\n\nexport interface ToolCallEventResult {\n\tblock?: boolean;\n\treason?: string;\n}\n\n/** Result from user_bash event handler */\nexport interface UserBashEventResult {\n\t/** Custom operations to use for execution */\n\toperations?: BashOperations;\n\t/** Full replacement: extension handled execution, use this result */\n\tresult?: BashResult;\n}\n\nexport interface ToolResultEventResult {\n\tcontent?: (TextContent | ImageContent)[];\n\tdetails?: unknown;\n\tisError?: boolean;\n}\n\nexport interface BeforeAgentStartEventResult {\n\tmessage?: Pick<CustomMessage, \"customType\" | \"content\" | \"display\" | \"details\">;\n\t/** Replace the system prompt for this turn. If multiple extensions return this, they are chained. */\n\tsystemPrompt?: string;\n}\n\nexport interface SessionDirectoryResult {\n\t/** Custom session directory path. If multiple extensions return this, the last one wins. */\n\tsessionDir?: string;\n}\n\n/** Special startup-only handler. Unlike other events, this receives no ExtensionContext. */\nexport type SessionDirectoryHandler = (\n\tevent: SessionDirectoryEvent,\n) => Promise<SessionDirectoryResult | undefined> | SessionDirectoryResult | undefined;\n\nexport interface SessionBeforeSwitchResult {\n\tcancel?: boolean;\n}\n\nexport interface SessionBeforeForkResult {\n\tcancel?: boolean;\n\tskipConversationRestore?: boolean;\n}\n\nexport interface SessionBeforeCompactResult {\n\tcancel?: boolean;\n\tcompaction?: CompactionResult;\n}\n\nexport interface SessionBeforeTreeResult {\n\tcancel?: boolean;\n\tsummary?: {\n\t\tsummary: string;\n\t\tdetails?: unknown;\n\t};\n\t/** Override custom instructions for summarization */\n\tcustomInstructions?: string;\n\t/** Override whether customInstructions replaces the default prompt */\n\treplaceInstructions?: boolean;\n\t/** Override label to attach to the branch summary entry */\n\tlabel?: string;\n}\n\n// ============================================================================\n// Message Rendering\n// ============================================================================\n\nexport interface MessageRenderOptions {\n\texpanded: boolean;\n}\n\nexport type MessageRenderer<T = unknown> = (\n\tmessage: CustomMessage<T>,\n\toptions: MessageRenderOptions,\n\ttheme: Theme,\n) => Component | undefined;\n\n// ============================================================================\n// Command Registration\n// ============================================================================\n\nexport interface RegisteredCommand {\n\tname: string;\n\tdescription?: string;\n\tgetArgumentCompletions?: (argumentPrefix: string) => AutocompleteItem[] | null;\n\thandler: (args: string, ctx: ExtensionCommandContext) => Promise<void>;\n}\n\nexport type LifecycleHookScope = \"user\" | \"project\";\nexport type LifecycleHookPhase = \"beforeInstall\" | \"afterInstall\" | \"beforeRemove\" | \"afterRemove\";\n\nexport interface LifecycleHookContext {\n\t/** Lifecycle phase currently being executed. */\n\tphase: LifecycleHookPhase;\n\t/** Package source string passed to install (npm:, git:, https://, local path). */\n\tsource: string;\n\t/** Resolved installed package path (or resolved local path), when available for this phase. */\n\tinstalledPath?: string;\n\t/** Where the package was installed. */\n\tscope: LifecycleHookScope;\n\t/** Current working directory for the install invocation. */\n\tcwd: string;\n\t/** Whether install is running in an interactive TTY. */\n\tinteractive: boolean;\n\t/** Info-level logging sink for install output. */\n\tlog(message: string): void;\n\t/** Warning-level logging sink for install output. */\n\twarn(message: string): void;\n\t/** Error-level logging sink for install output. */\n\terror(message: string): void;\n}\n\nexport type LifecycleHookHandler = (ctx: LifecycleHookContext) => Promise<void> | void;\nexport type LifecycleHookMap = Record<LifecycleHookPhase, LifecycleHookHandler[]>;\n\n// ============================================================================\n// Extension API\n// ============================================================================\n\n/** Handler function type for events */\n// biome-ignore lint/suspicious/noConfusingVoidType: void allows bare return statements\nexport type ExtensionHandler<E, R = undefined> = (event: E, ctx: ExtensionContext) => Promise<R | void> | R | void;\n\n/**\n * ExtensionAPI passed to extension factory functions.\n */\nexport interface ExtensionAPI {\n\t// =========================================================================\n\t// Event Subscription\n\t// =========================================================================\n\n\ton(event: \"resources_discover\", handler: ExtensionHandler<ResourcesDiscoverEvent, ResourcesDiscoverResult>): void;\n\ton(event: \"session_directory\", handler: SessionDirectoryHandler): void;\n\ton(event: \"session_start\", handler: ExtensionHandler<SessionStartEvent>): void;\n\ton(\n\t\tevent: \"session_before_switch\",\n\t\thandler: ExtensionHandler<SessionBeforeSwitchEvent, SessionBeforeSwitchResult>,\n\t): void;\n\ton(event: \"session_switch\", handler: ExtensionHandler<SessionSwitchEvent>): void;\n\ton(event: \"session_before_fork\", handler: ExtensionHandler<SessionBeforeForkEvent, SessionBeforeForkResult>): void;\n\ton(event: \"session_fork\", handler: ExtensionHandler<SessionForkEvent>): void;\n\ton(\n\t\tevent: \"session_before_compact\",\n\t\thandler: ExtensionHandler<SessionBeforeCompactEvent, SessionBeforeCompactResult>,\n\t): void;\n\ton(event: \"session_compact\", handler: ExtensionHandler<SessionCompactEvent>): void;\n\ton(event: \"session_shutdown\", handler: ExtensionHandler<SessionShutdownEvent>): void;\n\ton(event: \"session_before_tree\", handler: ExtensionHandler<SessionBeforeTreeEvent, SessionBeforeTreeResult>): void;\n\ton(event: \"session_tree\", handler: ExtensionHandler<SessionTreeEvent>): void;\n\ton(event: \"context\", handler: ExtensionHandler<ContextEvent, ContextEventResult>): void;\n\ton(\n\t\tevent: \"before_provider_request\",\n\t\thandler: ExtensionHandler<BeforeProviderRequestEvent, BeforeProviderRequestEventResult>,\n\t): void;\n\ton(event: \"before_agent_start\", handler: ExtensionHandler<BeforeAgentStartEvent, BeforeAgentStartEventResult>): void;\n\ton(event: \"agent_start\", handler: ExtensionHandler<AgentStartEvent>): void;\n\ton(event: \"agent_end\", handler: ExtensionHandler<AgentEndEvent>): void;\n\ton(event: \"turn_start\", handler: ExtensionHandler<TurnStartEvent>): void;\n\ton(event: \"turn_end\", handler: ExtensionHandler<TurnEndEvent>): void;\n\ton(event: \"message_start\", handler: ExtensionHandler<MessageStartEvent>): void;\n\ton(event: \"message_update\", handler: ExtensionHandler<MessageUpdateEvent>): void;\n\ton(event: \"message_end\", handler: ExtensionHandler<MessageEndEvent>): void;\n\ton(event: \"tool_execution_start\", handler: ExtensionHandler<ToolExecutionStartEvent>): void;\n\ton(event: \"tool_execution_update\", handler: ExtensionHandler<ToolExecutionUpdateEvent>): void;\n\ton(event: \"tool_execution_end\", handler: ExtensionHandler<ToolExecutionEndEvent>): void;\n\ton(event: \"model_select\", handler: ExtensionHandler<ModelSelectEvent>): void;\n\ton(event: \"bash_transform\", handler: ExtensionHandler<BashTransformEvent, BashTransformEventResult>): void;\n\ton(event: \"tool_call\", handler: ExtensionHandler<ToolCallEvent, ToolCallEventResult>): void;\n\ton(event: \"tool_result\", handler: ExtensionHandler<ToolResultEvent, ToolResultEventResult>): void;\n\ton(event: \"user_bash\", handler: ExtensionHandler<UserBashEvent, UserBashEventResult>): void;\n\ton(event: \"input\", handler: ExtensionHandler<InputEvent, InputEventResult>): void;\n\ton(event: \"before_model_select\", handler: ExtensionHandler<BeforeModelSelectEvent, BeforeModelSelectResult>): void;\n\ton(event: \"adjust_tool_set\", handler: ExtensionHandler<AdjustToolSetEvent, AdjustToolSetResult>): void;\n\n\t// =========================================================================\n\t// Event Emission (for host extensions that orchestrate model selection)\n\t// =========================================================================\n\n\t/** Emit before_model_select event. Returns override model ID or undefined. */\n\temitBeforeModelSelect(event: Omit<BeforeModelSelectEvent, \"type\">): Promise<BeforeModelSelectResult | undefined>;\n\n\t/** Emit adjust_tool_set event (ADR-005). Returns override tool names or undefined. */\n\temitAdjustToolSet(event: Omit<AdjustToolSetEvent, \"type\">): Promise<AdjustToolSetResult | undefined>;\n\n\t// =========================================================================\n\t// Tool Registration\n\t// =========================================================================\n\n\t/** Register a tool that the LLM can call. */\n\tregisterTool<TParams extends TSchema = TSchema, TDetails = unknown>(tool: ToolDefinition<TParams, TDetails>): void;\n\n\t// =========================================================================\n\t// Command, Shortcut, Flag Registration\n\t// =========================================================================\n\n\t/** Register a custom command. */\n\tregisterCommand(name: string, options: Omit<RegisteredCommand, \"name\">): void;\n\n\t/** Register a lifecycle hook run before package installation starts. */\n\tregisterBeforeInstall(handler: LifecycleHookHandler): void;\n\n\t/** Register a lifecycle hook run after package installation completes. */\n\tregisterAfterInstall(handler: LifecycleHookHandler): void;\n\n\t/** Register a lifecycle hook run before package removal starts. */\n\tregisterBeforeRemove(handler: LifecycleHookHandler): void;\n\n\t/** Register a lifecycle hook run after package removal completes. */\n\tregisterAfterRemove(handler: LifecycleHookHandler): void;\n\n\t/** Register a keyboard shortcut. */\n\tregisterShortcut(\n\t\tshortcut: KeyId,\n\t\toptions: {\n\t\t\tdescription?: string;\n\t\t\thandler: (ctx: ExtensionContext) => Promise<void> | void;\n\t\t},\n\t): void;\n\n\t/** Register a CLI flag. */\n\tregisterFlag(\n\t\tname: string,\n\t\toptions: {\n\t\t\tdescription?: string;\n\t\t\ttype: \"boolean\" | \"string\";\n\t\t\tdefault?: boolean | string;\n\t\t},\n\t): void;\n\n\t/** Get the value of a registered CLI flag. */\n\tgetFlag(name: string): boolean | string | undefined;\n\n\t// =========================================================================\n\t// Message Rendering\n\t// =========================================================================\n\n\t/** Register a custom renderer for CustomMessageEntry. */\n\tregisterMessageRenderer<T = unknown>(customType: string, renderer: MessageRenderer<T>): void;\n\n\t// =========================================================================\n\t// Actions\n\t// =========================================================================\n\n\t/** Send a custom message to the session. */\n\tsendMessage<T = unknown>(\n\t\tmessage: Pick<CustomMessage<T>, \"customType\" | \"content\" | \"display\" | \"details\">,\n\t\toptions?: { triggerTurn?: boolean; deliverAs?: \"steer\" | \"followUp\" | \"nextTurn\" },\n\t): void;\n\n\t/**\n\t * Send a user message to the agent. Always triggers a turn.\n\t * When the agent is streaming, use deliverAs to specify how to queue the message.\n\t */\n\tsendUserMessage(\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\toptions?: { deliverAs?: \"steer\" | \"followUp\" },\n\t): void;\n\n\t/**\n\t * Retry the last turn by removing the failed assistant response and\n\t * re-running the agent from the last user message. No-op if the last\n\t * message is not an assistant error.\n\t */\n\tretryLastTurn(): void;\n\n\t/** Append a custom entry to the session for state persistence (not sent to LLM). */\n\tappendEntry<T = unknown>(customType: string, data?: T): void;\n\n\t// =========================================================================\n\t// Session Metadata\n\t// =========================================================================\n\n\t/** Set the session display name (shown in session selector). */\n\tsetSessionName(name: string): void;\n\n\t/** Get the current session name, if set. */\n\tgetSessionName(): string | undefined;\n\n\t/** Set or clear a label on an entry. Labels are user-defined markers for bookmarking/navigation. */\n\tsetLabel(entryId: string, label: string | undefined): void;\n\n\t/** Execute a shell command. */\n\texec(command: string, args: string[], options?: ExecOptions): Promise<ExecResult>;\n\n\t/** Get the list of currently active tool names. */\n\tgetActiveTools(): string[];\n\n\t/** Get all configured tools with name and description. */\n\tgetAllTools(): ToolInfo[];\n\n\t/** Set the active tools by name. */\n\tsetActiveTools(toolNames: string[]): void;\n\n\t/** Get available slash commands in the current session. */\n\tgetCommands(): SlashCommandInfo[];\n\n\t// =========================================================================\n\t// Model and Thinking Level\n\t// =========================================================================\n\n\t/** Set the current model. Returns false if no API key available. */\n\tsetModel(model: Model<any>, options?: { persist?: boolean }): Promise<boolean>;\n\n\t/** Get current thinking level. */\n\tgetThinkingLevel(): ThinkingLevel;\n\n\t/** Set thinking level (clamped to model capabilities). */\n\tsetThinkingLevel(level: ThinkingLevel): void;\n\n\t// =========================================================================\n\t// Provider Registration\n\t// =========================================================================\n\n\t/**\n\t * Register or override a model provider.\n\t *\n\t * If `models` is provided: replaces all existing models for this provider.\n\t * If only `baseUrl` is provided: overrides the URL for existing models.\n\t * If `oauth` is provided: registers OAuth provider for /login support.\n\t * If `streamSimple` is provided: registers a custom API stream handler.\n\t *\n\t * During initial extension load this call is queued and applied once the\n\t * runner has bound its context. After that it takes effect immediately, so\n\t * it is safe to call from command handlers or event callbacks without\n\t * requiring a `/reload`.\n\t *\n\t * @example\n\t * // Register a new provider with custom models\n\t * pi.registerProvider(\"my-proxy\", {\n\t * baseUrl: \"https://proxy.example.com\",\n\t * apiKey: \"PROXY_API_KEY\",\n\t * api: \"anthropic-messages\",\n\t * models: [\n\t * {\n\t * id: \"claude-sonnet-4-20250514\",\n\t * name: \"Claude 4 Sonnet (proxy)\",\n\t * reasoning: false,\n\t * input: [\"text\", \"image\"],\n\t * cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },\n\t * contextWindow: 200000,\n\t * maxTokens: 16384\n\t * }\n\t * ]\n\t * });\n\t *\n\t * @example\n\t * // Override baseUrl for an existing provider\n\t * pi.registerProvider(\"anthropic\", {\n\t * baseUrl: \"https://proxy.example.com\"\n\t * });\n\t *\n\t * @example\n\t * // Register provider with OAuth support\n\t * pi.registerProvider(\"corporate-ai\", {\n\t * baseUrl: \"https://ai.corp.com\",\n\t * api: \"openai-responses\",\n\t * models: [...],\n\t * oauth: {\n\t * name: \"Corporate AI (SSO)\",\n\t * async login(callbacks) { ... },\n\t * async refreshToken(credentials) { ... },\n\t * getApiKey(credentials) { return credentials.access; }\n\t * }\n\t * });\n\t */\n\tregisterProvider(name: string, config: ProviderConfig): void;\n\n\t/**\n\t * Unregister a previously registered provider.\n\t *\n\t * Removes all models belonging to the named provider and restores any\n\t * built-in models that were overridden by it. Has no effect if the provider\n\t * is not currently registered.\n\t *\n\t * Like `registerProvider`, this takes effect immediately when called after\n\t * the initial load phase.\n\t *\n\t * @example\n\t * pi.unregisterProvider(\"my-proxy\");\n\t */\n\tunregisterProvider(name: string): void;\n\n\t/** Shared event bus for extension communication. */\n\tevents: EventBus;\n}\n\n// ============================================================================\n// Provider Registration Types\n// ============================================================================\n\n/** Configuration for registering a provider via pi.registerProvider(). */\nexport interface ProviderConfig {\n\t/** Auth behavior for provider availability and request key handling. Defaults to \"apiKey\". */\n\tauthMode?: \"apiKey\" | \"oauth\" | \"externalCli\" | \"none\";\n\t/** Optional readiness check. Return false if the provider cannot accept requests (e.g., CLI not authenticated, API key invalid).\n\t * Called before default auth checks. Trusted at the same level as extension code — extensions already have arbitrary code execution. */\n\tisReady?: () => boolean;\n\t/** Base URL for the API endpoint. Required when defining models. */\n\tbaseUrl?: string;\n\t/** API key or environment variable name. Required when defining models (unless oauth provided). */\n\tapiKey?: string;\n\t/** API type. Required at provider or model level when defining models. */\n\tapi?: Api;\n\t/** Optional streamSimple handler for custom APIs. */\n\tstreamSimple?: (model: Model<Api>, context: Context, options?: SimpleStreamOptions) => AssistantMessageEventStream;\n\t/** Custom headers to include in requests. */\n\theaders?: Record<string, string>;\n\t/** If true, adds Authorization: Bearer header with the resolved API key. */\n\tauthHeader?: boolean;\n\t/** Models to register. If provided, replaces all existing models for this provider. */\n\tmodels?: ProviderModelConfig[];\n\t/** OAuth provider for /login support. The `id` is set automatically from the provider name. */\n\toauth?: {\n\t\t/** Display name for the provider in login UI. */\n\t\tname: string;\n\t\t/** Run the login flow, return credentials to persist. */\n\t\tlogin(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials>;\n\t\t/** Refresh expired credentials, return updated credentials to persist. */\n\t\trefreshToken(credentials: OAuthCredentials): Promise<OAuthCredentials>;\n\t\t/** Convert credentials to API key string for the provider. */\n\t\tgetApiKey(credentials: OAuthCredentials): string;\n\t\t/** Optional: modify models for this provider (e.g., update baseUrl based on credentials). */\n\t\tmodifyModels?(models: Model<Api>[], credentials: OAuthCredentials): Model<Api>[];\n\t};\n}\n\n/** Configuration for a model within a provider. */\nexport interface ProviderModelConfig {\n\t/** Model ID (e.g., \"claude-sonnet-4-20250514\"). */\n\tid: string;\n\t/** Display name (e.g., \"Claude 4 Sonnet\"). */\n\tname: string;\n\t/** API type override for this model. */\n\tapi?: Api;\n\t/** Whether the model supports extended thinking. */\n\treasoning: boolean;\n\t/** Supported input types. */\n\tinput: (\"text\" | \"image\")[];\n\t/** Cost per token (for tracking, can be 0). */\n\tcost: { input: number; output: number; cacheRead: number; cacheWrite: number };\n\t/** Maximum context window size in tokens. */\n\tcontextWindow: number;\n\t/** Maximum output tokens. */\n\tmaxTokens: number;\n\t/** Custom headers for this model. */\n\theaders?: Record<string, string>;\n\t/** OpenAI compatibility settings. */\n\tcompat?: Model<Api>[\"compat\"];\n\t/** Opaque provider-specific options (e.g. Ollama keep_alive, num_gpu). */\n\tproviderOptions?: Record<string, unknown>;\n}\n\n/** Extension factory function type. Supports both sync and async initialization. */\nexport type ExtensionFactory = (pi: ExtensionAPI) => void | Promise<void>;\n\n// ============================================================================\n// Loaded Extension Types\n// ============================================================================\n\nexport interface RegisteredTool {\n\tdefinition: ToolDefinition;\n\textensionPath: string;\n}\n\nexport interface ExtensionFlag {\n\tname: string;\n\tdescription?: string;\n\ttype: \"boolean\" | \"string\";\n\tdefault?: boolean | string;\n\textensionPath: string;\n}\n\nexport interface ExtensionShortcut {\n\tshortcut: KeyId;\n\tdescription?: string;\n\thandler: (ctx: ExtensionContext) => Promise<void> | void;\n\textensionPath: string;\n}\n\ntype HandlerFn = (...args: unknown[]) => Promise<unknown>;\n\n/** Tool info with name, description, and parameter schema */\nexport type ToolInfo = Pick<ToolDefinition, \"name\" | \"description\" | \"parameters\">;\n\n/**\n * Shared state created by loader, used during registration and runtime.\n * Contains flag values (defaults set during registration, CLI values set after).\n */\nexport interface ExtensionRuntimeState {\n\tflagValues: Map<string, boolean | string>;\n\t/** Provider registrations queued during extension loading, processed when runner binds */\n\tpendingProviderRegistrations: Array<{ name: string; config: ProviderConfig }>;\n\t/**\n\t * Register or unregister a provider.\n\t *\n\t * Before bindCore(): queues registrations / removes from queue.\n\t * After bindCore(): calls ModelRegistry directly for immediate effect.\n\t */\n\tregisterProvider: (name: string, config: ProviderConfig) => void;\n\tunregisterProvider: (name: string) => void;\n\t/** Emit before_model_select event to all registered handlers. Bound by ExtensionRunner. */\n\temitBeforeModelSelect: (event: Omit<BeforeModelSelectEvent, \"type\">) => Promise<BeforeModelSelectResult | undefined>;\n\t/** Emit adjust_tool_set event to all registered handlers. Bound by ExtensionRunner (ADR-005). */\n\temitAdjustToolSet: (event: Omit<AdjustToolSetEvent, \"type\">) => Promise<AdjustToolSetResult | undefined>;\n}\n\n/**\n * Action implementations for pi.* API methods.\n * Provided to runner.initialize(), copied into the shared runtime.\n */\nexport interface ExtensionActions {\n\tsendMessage: <T = unknown>(\n\t\tmessage: Pick<CustomMessage<T>, \"customType\" | \"content\" | \"display\" | \"details\">,\n\t\toptions?: { triggerTurn?: boolean; deliverAs?: \"steer\" | \"followUp\" | \"nextTurn\" },\n\t) => void;\n\tsendUserMessage: (\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\toptions?: { deliverAs?: \"steer\" | \"followUp\" },\n\t) => void;\n\tretryLastTurn: () => void;\n\tappendEntry: <T = unknown>(customType: string, data?: T) => void;\n\tsetSessionName: (name: string) => void;\n\tgetSessionName: () => string | undefined;\n\tsetLabel: (entryId: string, label: string | undefined) => void;\n\tgetActiveTools: () => string[];\n\tgetAllTools: () => ToolInfo[];\n\tsetActiveTools: (toolNames: string[]) => void;\n\trefreshTools: () => void;\n\tgetCommands: () => SlashCommandInfo[];\n\tsetModel: (model: Model<any>, options?: { persist?: boolean }) => Promise<boolean>;\n\tgetThinkingLevel: () => ThinkingLevel;\n\tsetThinkingLevel: (level: ThinkingLevel) => void;\n}\n\n/**\n * Actions for ExtensionContext (ctx.* in event handlers).\n * Required by all modes.\n */\nexport interface ExtensionContextActions {\n\tgetModel: () => Model<any> | undefined;\n\tisIdle: () => boolean;\n\tabort: () => void;\n\thasPendingMessages: () => boolean;\n\tshutdown: () => void;\n\tgetContextUsage: () => ContextUsage | undefined;\n\tcompact: (options?: CompactOptions) => void;\n\tgetSystemPrompt: () => string;\n}\n\n/**\n * Actions for ExtensionCommandContext (ctx.* in command handlers).\n * Only needed for interactive mode where extension commands are invokable.\n */\nexport interface ExtensionCommandContextActions {\n\twaitForIdle: () => Promise<void>;\n\tnewSession: (options?: {\n\t\tparentSession?: string;\n\t\tsetup?: (sessionManager: SessionManager) => Promise<void>;\n\t}) => Promise<{ cancelled: boolean }>;\n\tfork: (entryId: string) => Promise<{ cancelled: boolean }>;\n\tnavigateTree: (\n\t\ttargetId: string,\n\t\toptions?: { summarize?: boolean; customInstructions?: string; replaceInstructions?: boolean; label?: string },\n\t) => Promise<{ cancelled: boolean }>;\n\tswitchSession: (sessionPath: string) => Promise<{ cancelled: boolean }>;\n\treload: () => Promise<void>;\n}\n\n/**\n * Full runtime = state + actions.\n * Created by loader with throwing action stubs, completed by runner.initialize().\n */\nexport interface ExtensionRuntime extends ExtensionRuntimeState, ExtensionActions {}\n\n/** Loaded extension with all registered items. */\nexport interface Extension {\n\tpath: string;\n\tresolvedPath: string;\n\thandlers: Map<string, HandlerFn[]>;\n\ttools: Map<string, RegisteredTool>;\n\tmessageRenderers: Map<string, MessageRenderer>;\n\tcommands: Map<string, RegisteredCommand>;\n\tflags: Map<string, ExtensionFlag>;\n\tshortcuts: Map<KeyId, ExtensionShortcut>;\n\tlifecycleHooks: LifecycleHookMap;\n}\n\n/** Result of loading extensions. */\nexport interface LoadExtensionsResult {\n\textensions: Extension[];\n\terrors: Array<{ path: string; error: string }>;\n\t/** Shared runtime - actions are throwing stubs until runner.initialize() */\n\truntime: ExtensionRuntime;\n}\n\n// ============================================================================\n// Extension Error\n// ============================================================================\n\nexport interface ExtensionError {\n\textensionPath: string;\n\tevent: string;\n\terror: string;\n\tstack?: string;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/core/extensions/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AA21BH,MAAM,UAAU,qBAAqB,CAAC,QAAgB,EAAE,KAAsB;IAC7E,OAAO,KAAK,CAAC,QAAQ,KAAK,QAAQ,CAAC;AACpC,CAAC;AAiCD,MAAM,UAAU,mBAAmB,CAAC,QAAgB,EAAE,KAAoB;IACzE,OAAO,KAAK,CAAC,QAAQ,KAAK,QAAQ,CAAC;AACpC,CAAC","sourcesContent":["/**\n * Extension system types.\n *\n * Extensions are TypeScript modules that can:\n * - Subscribe to agent lifecycle events\n * - Register LLM-callable tools\n * - Register commands, keyboard shortcuts, and CLI flags\n * - Interact with the user via UI primitives\n */\n\nimport type {\n\tAgentMessage,\n\tAgentToolResult,\n\tAgentToolUpdateCallback,\n\tThinkingLevel,\n} from \"@gsd/pi-agent-core\";\nimport type {\n\tApi,\n\tAssistantMessageEvent,\n\tAssistantMessageEventStream,\n\tContext,\n\tImageContent,\n\tModel,\n\tOAuthCredentials,\n\tOAuthLoginCallbacks,\n\tSimpleStreamOptions,\n\tTextContent,\n\tToolResultMessage,\n} from \"@gsd/pi-ai\";\nimport type {\n\tAutocompleteItem,\n\tComponent,\n\tEditorComponent,\n\tEditorTheme,\n\tKeyId,\n\tOverlayHandle,\n\tOverlayOptions,\n\tTUI,\n} from \"@gsd/pi-tui\";\nimport type { Static, TSchema } from \"@sinclair/typebox\";\nimport type { Theme } from \"../../modes/interactive/theme/theme.js\";\nimport type { BashResult } from \"../bash-executor.js\";\nimport type { CompactionPreparation, CompactionResult } from \"../compaction/index.js\";\nimport type { EventBus } from \"../event-bus.js\";\nimport type { ExecOptions, ExecResult } from \"../exec.js\";\nimport type { ReadonlyFooterDataProvider } from \"../footer-data-provider.js\";\nimport type { KeybindingsManager } from \"../keybindings.js\";\nimport type { CustomMessage } from \"../messages.js\";\nimport type { ModelRegistry } from \"../model-registry.js\";\nimport type {\n\tBranchSummaryEntry,\n\tCompactionEntry,\n\tReadonlySessionManager,\n\tSessionEntry,\n\tSessionManager,\n} from \"../session-manager.js\";\nimport type { SlashCommandInfo } from \"../slash-commands.js\";\nimport type { BashOperations } from \"../tools/bash.js\";\nimport type { EditToolDetails } from \"../tools/edit.js\";\nimport type {\n\tBashToolDetails,\n\tBashToolInput,\n\tEditToolInput,\n\tFindToolDetails,\n\tFindToolInput,\n\tGrepToolDetails,\n\tGrepToolInput,\n\tLsToolDetails,\n\tLsToolInput,\n\tReadToolDetails,\n\tReadToolInput,\n\tWriteToolInput,\n} from \"../tools/index.js\";\n\nexport type { ExecOptions, ExecResult } from \"../exec.js\";\nexport type { AgentToolResult, AgentToolUpdateCallback };\nexport type { AppAction, KeybindingsManager } from \"../keybindings.js\";\n\n// ============================================================================\n// UI Context\n// ============================================================================\n\n/** Options for extension UI dialogs. */\nexport interface ExtensionUIDialogOptions {\n\t/** AbortSignal to programmatically dismiss the dialog. */\n\tsignal?: AbortSignal;\n\t/** Timeout in milliseconds. Dialog auto-dismisses with live countdown display. */\n\ttimeout?: number;\n\t/** When true, the user can select multiple options. The return type becomes `string[]`. */\n\tallowMultiple?: boolean;\n\t/** When true, text input dialogs should hide typed characters if supported by the client surface. */\n\tsecure?: boolean;\n}\n\n/** Placement for extension widgets. */\nexport type WidgetPlacement = \"aboveEditor\" | \"belowEditor\";\n\n/** Options for extension widgets. */\nexport interface ExtensionWidgetOptions {\n\t/** Where the widget is rendered. Defaults to \"aboveEditor\". */\n\tplacement?: WidgetPlacement;\n}\n\n/** Raw terminal input listener for extensions. */\nexport type TerminalInputHandler = (data: string) => { consume?: boolean; data?: string } | undefined;\n\n/**\n * UI context for extensions to request interactive UI.\n * Each mode (interactive, RPC, print) provides its own implementation.\n */\nexport interface ExtensionUIContext {\n\t/** Show a selector and return the user's choice. When `opts.allowMultiple` is true, returns an array. */\n\tselect(title: string, options: string[], opts?: ExtensionUIDialogOptions): Promise<string | string[] | undefined>;\n\n\t/** Show a confirmation dialog. */\n\tconfirm(title: string, message: string, opts?: ExtensionUIDialogOptions): Promise<boolean>;\n\n\t/** Show a text input dialog. */\n\tinput(title: string, placeholder?: string, opts?: ExtensionUIDialogOptions): Promise<string | undefined>;\n\n\t/** Show a notification to the user. */\n\tnotify(message: string, type?: \"info\" | \"warning\" | \"error\" | \"success\"): void;\n\n\t/** Listen to raw terminal input (interactive mode only). Returns an unsubscribe function. */\n\tonTerminalInput(handler: TerminalInputHandler): () => void;\n\n\t/** Set status text in the footer/status bar. Pass undefined to clear. */\n\tsetStatus(key: string, text: string | undefined): void;\n\n\t/** Set the working/loading message shown during streaming. Call with no argument to restore default. */\n\tsetWorkingMessage(message?: string): void;\n\n\t/** Set a widget to display above or below the editor. Accepts string array or component factory. */\n\tsetWidget(key: string, content: string[] | undefined, options?: ExtensionWidgetOptions): void;\n\tsetWidget(\n\t\tkey: string,\n\t\tcontent: ((tui: TUI, theme: Theme) => Component & { dispose?(): void }) | undefined,\n\t\toptions?: ExtensionWidgetOptions,\n\t): void;\n\n\t/** Set a custom footer component, or undefined to restore the built-in footer.\n\t *\n\t * The factory receives a FooterDataProvider for data not otherwise accessible:\n\t * git branch and extension statuses from setStatus(). Token stats, model info,\n\t * etc. are available via ctx.sessionManager and ctx.model.\n\t */\n\tsetFooter(\n\t\tfactory:\n\t\t\t| ((tui: TUI, theme: Theme, footerData: ReadonlyFooterDataProvider) => Component & { dispose?(): void })\n\t\t\t| undefined,\n\t): void;\n\n\t/** Set a custom header component (shown at startup, above chat), or undefined to restore the built-in header. */\n\tsetHeader(factory: ((tui: TUI, theme: Theme) => Component & { dispose?(): void }) | undefined): void;\n\n\t/** Set the terminal window/tab title. */\n\tsetTitle(title: string): void;\n\n\t/** Show a custom component with keyboard focus. */\n\tcustom<T>(\n\t\tfactory: (\n\t\t\ttui: TUI,\n\t\t\ttheme: Theme,\n\t\t\tkeybindings: KeybindingsManager,\n\t\t\tdone: (result: T) => void,\n\t\t) => (Component & { dispose?(): void }) | Promise<Component & { dispose?(): void }>,\n\t\toptions?: {\n\t\t\toverlay?: boolean;\n\t\t\t/** Overlay positioning/sizing options. Can be static or a function for dynamic updates. */\n\t\t\toverlayOptions?: OverlayOptions | (() => OverlayOptions);\n\t\t\t/** Called with the overlay handle after the overlay is shown. Use to control visibility. */\n\t\t\tonHandle?: (handle: OverlayHandle) => void;\n\t\t},\n\t): Promise<T>;\n\n\t/** Paste text into the editor, triggering paste handling (collapse for large content). */\n\tpasteToEditor(text: string): void;\n\n\t/** Set the text in the core input editor. */\n\tsetEditorText(text: string): void;\n\n\t/** Get the current text from the core input editor. */\n\tgetEditorText(): string;\n\n\t/** Show a multi-line editor for text editing. */\n\teditor(title: string, prefill?: string): Promise<string | undefined>;\n\n\t/**\n\t * Set a custom editor component via factory function.\n\t * Pass undefined to restore the default editor.\n\t *\n\t * The factory receives:\n\t * - `theme`: EditorTheme for styling borders and autocomplete\n\t * - `keybindings`: KeybindingsManager for app-level keybindings\n\t *\n\t * For full app keybinding support (escape, ctrl+d, model switching, etc.),\n\t * extend `CustomEditor` from `@gsd/pi-coding-agent` and call\n\t * `super.handleInput(data)` for keys you don't handle.\n\t *\n\t * @example\n\t * ```ts\n\t * import { CustomEditor } from \"@gsd/pi-coding-agent\";\n\t *\n\t * class VimEditor extends CustomEditor {\n\t * private mode: \"normal\" | \"insert\" = \"insert\";\n\t *\n\t * handleInput(data: string): void {\n\t * if (this.mode === \"normal\") {\n\t * // Handle vim normal mode keys...\n\t * if (data === \"i\") { this.mode = \"insert\"; return; }\n\t * }\n\t * super.handleInput(data); // App keybindings + text editing\n\t * }\n\t * }\n\t *\n\t * ctx.ui.setEditorComponent((tui, theme, keybindings) =>\n\t * new VimEditor(tui, theme, keybindings)\n\t * );\n\t * ```\n\t */\n\tsetEditorComponent(\n\t\tfactory: ((tui: TUI, theme: EditorTheme, keybindings: KeybindingsManager) => EditorComponent) | undefined,\n\t): void;\n\n\t/** Get the current theme for styling. */\n\treadonly theme: Theme;\n\n\t/** Get all available themes with their names and file paths. */\n\tgetAllThemes(): { name: string; path: string | undefined }[];\n\n\t/** Load a theme by name without switching to it. Returns undefined if not found. */\n\tgetTheme(name: string): Theme | undefined;\n\n\t/** Set the current theme by name or Theme object. */\n\tsetTheme(theme: string | Theme): { success: boolean; error?: string };\n\n\t/** Get current tool output expansion state. */\n\tgetToolsExpanded(): boolean;\n\n\t/** Set tool output expansion state. */\n\tsetToolsExpanded(expanded: boolean): void;\n}\n\n// ============================================================================\n// Extension Context\n// ============================================================================\n\nexport interface ContextUsage {\n\t/** Estimated context tokens, or null if unknown (e.g. right after compaction, before next LLM response). */\n\ttokens: number | null;\n\tcontextWindow: number;\n\t/** Context usage as percentage of context window, or null if tokens is unknown. */\n\tpercent: number | null;\n}\n\nexport interface CompactOptions {\n\tcustomInstructions?: string;\n\tonComplete?: (result: CompactionResult) => void;\n\tonError?: (error: Error) => void;\n}\n\n/**\n * Context passed to extension event handlers.\n */\nexport interface ExtensionContext {\n\t/** UI methods for user interaction */\n\tui: ExtensionUIContext;\n\t/** Whether UI is available (false in print/RPC mode) */\n\thasUI: boolean;\n\t/** Current working directory */\n\tcwd: string;\n\t/** Session manager (read-only) */\n\tsessionManager: ReadonlySessionManager;\n\t/** Model registry for API key resolution */\n\tmodelRegistry: ModelRegistry;\n\t/** Current model (may be undefined) */\n\tmodel: Model<any> | undefined;\n\t/** Whether the agent is idle (not streaming) */\n\tisIdle(): boolean;\n\t/** Abort the current agent operation */\n\tabort(): void;\n\t/** Whether there are queued messages waiting */\n\thasPendingMessages(): boolean;\n\t/** Gracefully shutdown pi and exit. Available in all contexts. */\n\tshutdown(): void;\n\t/** Get current context usage for the active model. */\n\tgetContextUsage(): ContextUsage | undefined;\n\t/** Trigger compaction without awaiting completion. */\n\tcompact(options?: CompactOptions): void;\n\t/** Get the current effective system prompt. */\n\tgetSystemPrompt(): string;\n}\n\n/**\n * Extended context for command handlers.\n * Includes session control methods only safe in user-initiated commands.\n */\nexport interface ExtensionCommandContext extends ExtensionContext {\n\t/** Wait for the agent to finish streaming */\n\twaitForIdle(): Promise<void>;\n\n\t/** Start a new session, optionally with initialization. */\n\tnewSession(options?: {\n\t\tparentSession?: string;\n\t\tsetup?: (sessionManager: SessionManager) => Promise<void>;\n\t}): Promise<{ cancelled: boolean }>;\n\n\t/** Fork from a specific entry, creating a new session file. */\n\tfork(entryId: string): Promise<{ cancelled: boolean }>;\n\n\t/** Navigate to a different point in the session tree. */\n\tnavigateTree(\n\t\ttargetId: string,\n\t\toptions?: { summarize?: boolean; customInstructions?: string; replaceInstructions?: boolean; label?: string },\n\t): Promise<{ cancelled: boolean }>;\n\n\t/** Switch to a different session file. */\n\tswitchSession(sessionPath: string): Promise<{ cancelled: boolean }>;\n\n\t/** Reload extensions, skills, prompts, and themes. */\n\treload(): Promise<void>;\n}\n\n// ============================================================================\n// Tool Types\n// ============================================================================\n\n/** Rendering options for tool results */\nexport interface ToolRenderResultOptions {\n\t/** Whether the result view is expanded */\n\texpanded: boolean;\n\t/** Whether this is a partial/streaming result */\n\tisPartial: boolean;\n}\n\n/**\n * Tool compatibility metadata for provider-aware tool filtering (ADR-005 Phase 2).\n * Tools without compatibility metadata are assumed universally compatible.\n */\nexport interface ToolCompatibility {\n\t/** Tool produces image content in results (filtered for providers without imageToolResults) */\n\tproducesImages?: boolean;\n\t/** Tool requires schema features that some providers don't support (e.g., [\"patternProperties\"]) */\n\tschemaFeatures?: string[];\n\t/** Tool is effective only with models above a minimum capability threshold */\n\tminCapabilityTier?: \"light\" | \"standard\" | \"heavy\";\n}\n\n/**\n * Tool definition for registerTool().\n */\nexport interface ToolDefinition<TParams extends TSchema = TSchema, TDetails = unknown> {\n\t/** Tool name (used in LLM tool calls) */\n\tname: string;\n\t/** Human-readable label for UI */\n\tlabel: string;\n\t/** Description for LLM */\n\tdescription: string;\n\t/** Optional one-line snippet for the Available tools section in the default system prompt. Falls back to description when omitted. */\n\tpromptSnippet?: string;\n\t/** Optional guideline bullets appended to the default system prompt Guidelines section when this tool is active. */\n\tpromptGuidelines?: string[];\n\t/** Parameter schema (TypeBox) */\n\tparameters: TParams;\n\t/** Provider compatibility metadata (ADR-005). Omit for universally compatible tools. */\n\tcompatibility?: ToolCompatibility;\n\n\t/** Execute the tool. */\n\texecute(\n\t\ttoolCallId: string,\n\t\tparams: Static<TParams>,\n\t\tsignal: AbortSignal | undefined,\n\t\tonUpdate: AgentToolUpdateCallback<TDetails> | undefined,\n\t\tctx: ExtensionContext,\n\t): Promise<AgentToolResult<TDetails>>;\n\n\t/** Custom rendering for tool call display */\n\trenderCall?: (args: Static<TParams>, theme: Theme) => Component | undefined;\n\n\t/** Custom rendering for tool result display */\n\trenderResult?: (\n\t\tresult: AgentToolResult<TDetails>,\n\t\toptions: ToolRenderResultOptions,\n\t\ttheme: Theme,\n\t) => Component | undefined;\n}\n\n// ============================================================================\n// Resource Events\n// ============================================================================\n\n/** Fired after session_start to allow extensions to provide additional resource paths. */\nexport interface ResourcesDiscoverEvent {\n\ttype: \"resources_discover\";\n\tcwd: string;\n\treason: \"startup\" | \"reload\";\n}\n\n/** Result from resources_discover event handler */\nexport interface ResourcesDiscoverResult {\n\tskillPaths?: string[];\n\tpromptPaths?: string[];\n\tthemePaths?: string[];\n}\n\n// ============================================================================\n// Session Events\n// ============================================================================\n\n/** Fired before session manager creation to allow custom session directory resolution */\nexport interface SessionDirectoryEvent {\n\ttype: \"session_directory\";\n\tcwd: string;\n}\n\n/** Fired on initial session load */\nexport interface SessionStartEvent {\n\ttype: \"session_start\";\n}\n\n/** Fired before switching to another session (can be cancelled) */\nexport interface SessionBeforeSwitchEvent {\n\ttype: \"session_before_switch\";\n\treason: \"new\" | \"resume\";\n\ttargetSessionFile?: string;\n}\n\n/** Fired after switching to another session */\nexport interface SessionSwitchEvent {\n\ttype: \"session_switch\";\n\treason: \"new\" | \"resume\";\n\tpreviousSessionFile: string | undefined;\n}\n\n/** Fired before forking a session (can be cancelled) */\nexport interface SessionBeforeForkEvent {\n\ttype: \"session_before_fork\";\n\tentryId: string;\n}\n\n/** Fired after forking a session */\nexport interface SessionForkEvent {\n\ttype: \"session_fork\";\n\tpreviousSessionFile: string | undefined;\n}\n\n/** Fired before context compaction (can be cancelled or customized) */\nexport interface SessionBeforeCompactEvent {\n\ttype: \"session_before_compact\";\n\tpreparation: CompactionPreparation;\n\tbranchEntries: SessionEntry[];\n\tcustomInstructions?: string;\n\tsignal: AbortSignal;\n}\n\n/** Fired after context compaction */\nexport interface SessionCompactEvent {\n\ttype: \"session_compact\";\n\tcompactionEntry: CompactionEntry;\n\tfromExtension: boolean;\n}\n\n/** Fired on process exit */\nexport interface SessionShutdownEvent {\n\ttype: \"session_shutdown\";\n}\n\n/** Preparation data for tree navigation */\nexport interface TreePreparation {\n\ttargetId: string;\n\toldLeafId: string | null;\n\tcommonAncestorId: string | null;\n\tentriesToSummarize: SessionEntry[];\n\tuserWantsSummary: boolean;\n\t/** Custom instructions for summarization */\n\tcustomInstructions?: string;\n\t/** If true, customInstructions replaces the default prompt instead of being appended */\n\treplaceInstructions?: boolean;\n\t/** Label to attach to the branch summary entry */\n\tlabel?: string;\n}\n\n/** Fired before navigating in the session tree (can be cancelled) */\nexport interface SessionBeforeTreeEvent {\n\ttype: \"session_before_tree\";\n\tpreparation: TreePreparation;\n\tsignal: AbortSignal;\n}\n\n/** Fired after navigating in the session tree */\nexport interface SessionTreeEvent {\n\ttype: \"session_tree\";\n\tnewLeafId: string | null;\n\toldLeafId: string | null;\n\tsummaryEntry?: BranchSummaryEntry;\n\tfromExtension?: boolean;\n}\n\nexport type SessionEvent =\n\t| SessionDirectoryEvent\n\t| SessionStartEvent\n\t| SessionBeforeSwitchEvent\n\t| SessionSwitchEvent\n\t| SessionBeforeForkEvent\n\t| SessionForkEvent\n\t| SessionBeforeCompactEvent\n\t| SessionCompactEvent\n\t| SessionShutdownEvent\n\t| SessionBeforeTreeEvent\n\t| SessionTreeEvent;\n\n// ============================================================================\n// Agent Events\n// ============================================================================\n\n/** Fired before each LLM call. Can modify messages. */\nexport interface ContextEvent {\n\ttype: \"context\";\n\tmessages: AgentMessage[];\n}\n\n/** Fired before a provider request is sent. Can replace the payload. */\nexport interface BeforeProviderRequestEvent {\n\ttype: \"before_provider_request\";\n\tpayload: unknown;\n\t/** The resolved model for this request (provider, id, api, etc.) */\n\tmodel?: { provider: string; id: string; api?: string };\n}\n\n/** Fired after user submits prompt but before agent loop. */\nexport interface BeforeAgentStartEvent {\n\ttype: \"before_agent_start\";\n\tprompt: string;\n\timages?: ImageContent[];\n\tsystemPrompt: string;\n}\n\n/** Fired when an agent loop starts */\nexport interface AgentStartEvent {\n\ttype: \"agent_start\";\n}\n\n/** Fired when an agent loop ends */\nexport interface AgentEndEvent {\n\ttype: \"agent_end\";\n\tmessages: AgentMessage[];\n}\n\n/** Fired at the start of each turn */\nexport interface TurnStartEvent {\n\ttype: \"turn_start\";\n\tturnIndex: number;\n\ttimestamp: number;\n}\n\n/** Fired at the end of each turn */\nexport interface TurnEndEvent {\n\ttype: \"turn_end\";\n\tturnIndex: number;\n\tmessage: AgentMessage;\n\ttoolResults: ToolResultMessage[];\n}\n\n/** Fired when a message starts (user, assistant, or toolResult) */\nexport interface MessageStartEvent {\n\ttype: \"message_start\";\n\tmessage: AgentMessage;\n}\n\n/** Fired during assistant message streaming with token-by-token updates */\nexport interface MessageUpdateEvent {\n\ttype: \"message_update\";\n\tmessage: AgentMessage;\n\tassistantMessageEvent: AssistantMessageEvent;\n}\n\n/** Fired when a message ends */\nexport interface MessageEndEvent {\n\ttype: \"message_end\";\n\tmessage: AgentMessage;\n}\n\n/** Fired when a tool starts executing */\nexport interface ToolExecutionStartEvent {\n\ttype: \"tool_execution_start\";\n\ttoolCallId: string;\n\ttoolName: string;\n\targs: any;\n}\n\n/** Fired during tool execution with partial/streaming output */\nexport interface ToolExecutionUpdateEvent {\n\ttype: \"tool_execution_update\";\n\ttoolCallId: string;\n\ttoolName: string;\n\targs: any;\n\tpartialResult: any;\n}\n\n/** Fired when a tool finishes executing */\nexport interface ToolExecutionEndEvent {\n\ttype: \"tool_execution_end\";\n\ttoolCallId: string;\n\ttoolName: string;\n\tresult: any;\n\tisError: boolean;\n}\n\n// ============================================================================\n// Model Events\n// ============================================================================\n\nexport type ModelSelectSource = \"set\" | \"cycle\" | \"restore\";\n\n/** Fired when a new model is selected */\nexport interface ModelSelectEvent {\n\ttype: \"model_select\";\n\tmodel: Model<any>;\n\tpreviousModel: Model<any> | undefined;\n\tsource: ModelSelectSource;\n}\n\n/** Fired before model selection runs capability scoring. Extensions can override the selected model. */\nexport interface BeforeModelSelectEvent {\n\ttype: \"before_model_select\";\n\tunitType: string;\n\tunitId: string;\n\tclassification: { tier: string; reason: string; downgraded: boolean };\n\ttaskMetadata?: Record<string, unknown>;\n\teligibleModels: string[];\n\tphaseConfig?: { primary: string; fallbacks: string[] };\n}\n\n/** Result from before_model_select event handler. Return { modelId } to override selection. */\nexport interface BeforeModelSelectResult {\n\tmodelId: string;\n}\n\n/**\n * Fired after model selection to allow extensions to adjust the active tool set (ADR-005 Phase 4).\n * Extensions can add, remove, or reorder tools based on the selected model's provider capabilities.\n */\nexport interface AdjustToolSetEvent {\n\ttype: \"adjust_tool_set\";\n\t/** The selected model's API type */\n\tselectedModelApi: string;\n\t/** The selected model's provider */\n\tselectedModelProvider: string;\n\t/** The selected model ID */\n\tselectedModelId: string;\n\t/** Current active tool names */\n\tactiveToolNames: string[];\n\t/** Tools already filtered by provider compatibility */\n\tfilteredTools: string[];\n}\n\n/** Result from adjust_tool_set event handler. Return { toolNames } to override tool set. */\nexport interface AdjustToolSetResult {\n\t/** Replacement tool names. If omitted, the default filtering is used. */\n\ttoolNames?: string[];\n}\n\n// ============================================================================\n// User Bash Events\n// ============================================================================\n\n/**\n * Fired before the bash tool executes a shell command.\n * Extensions can return a transformed command string.\n * All registered handlers are called in order; each receives the output of the previous.\n */\nexport interface BashTransformEvent {\n\ttype: \"bash_transform\";\n\t/** The command string about to be executed */\n\tcommand: string;\n\t/** Current working directory */\n\tcwd: string;\n}\n\n/** Result from bash_transform event handler */\nexport interface BashTransformEventResult {\n\t/** Replacement command string. If omitted or empty, the original command is used. */\n\tcommand?: string;\n}\n\n/** Fired when user executes a bash command via ! or !! prefix */\nexport interface UserBashEvent {\n\ttype: \"user_bash\";\n\t/** The command to execute */\n\tcommand: string;\n\t/** True if !! prefix was used (excluded from LLM context) */\n\texcludeFromContext: boolean;\n\t/** Current working directory */\n\tcwd: string;\n}\n\n// ============================================================================\n// Input Events\n// ============================================================================\n\n/** Source of user input */\nexport type InputSource = \"interactive\" | \"rpc\" | \"extension\";\n\n/** Fired when user input is received, before agent processing */\nexport interface InputEvent {\n\ttype: \"input\";\n\t/** The input text */\n\ttext: string;\n\t/** Attached images, if any */\n\timages?: ImageContent[];\n\t/** Where the input came from */\n\tsource: InputSource;\n}\n\n/** Result from input event handler */\nexport type InputEventResult =\n\t| { action: \"continue\" }\n\t| { action: \"transform\"; text: string; images?: ImageContent[] }\n\t| { action: \"handled\" };\n\n// ============================================================================\n// Tool Events\n// ============================================================================\n\ninterface ToolCallEventBase {\n\ttype: \"tool_call\";\n\ttoolCallId: string;\n}\n\nexport interface BashToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"bash\";\n\tinput: BashToolInput;\n}\n\nexport interface ReadToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"read\";\n\tinput: ReadToolInput;\n}\n\nexport interface EditToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"edit\";\n\tinput: EditToolInput;\n}\n\nexport interface WriteToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"write\";\n\tinput: WriteToolInput;\n}\n\nexport interface GrepToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"grep\";\n\tinput: GrepToolInput;\n}\n\nexport interface FindToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"find\";\n\tinput: FindToolInput;\n}\n\nexport interface LsToolCallEvent extends ToolCallEventBase {\n\ttoolName: \"ls\";\n\tinput: LsToolInput;\n}\n\nexport interface CustomToolCallEvent extends ToolCallEventBase {\n\ttoolName: string;\n\tinput: Record<string, unknown>;\n}\n\n/** Fired before a tool executes. Can block. */\nexport type ToolCallEvent =\n\t| BashToolCallEvent\n\t| ReadToolCallEvent\n\t| EditToolCallEvent\n\t| WriteToolCallEvent\n\t| GrepToolCallEvent\n\t| FindToolCallEvent\n\t| LsToolCallEvent\n\t| CustomToolCallEvent;\n\ninterface ToolResultEventBase {\n\ttype: \"tool_result\";\n\ttoolCallId: string;\n\tinput: Record<string, unknown>;\n\tcontent: (TextContent | ImageContent)[];\n\tisError: boolean;\n}\n\nexport interface BashToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"bash\";\n\tdetails: BashToolDetails | undefined;\n}\n\nexport interface ReadToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"read\";\n\tdetails: ReadToolDetails | undefined;\n}\n\nexport interface EditToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"edit\";\n\tdetails: EditToolDetails | undefined;\n}\n\nexport interface WriteToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"write\";\n\tdetails: undefined;\n}\n\nexport interface GrepToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"grep\";\n\tdetails: GrepToolDetails | undefined;\n}\n\nexport interface FindToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"find\";\n\tdetails: FindToolDetails | undefined;\n}\n\nexport interface LsToolResultEvent extends ToolResultEventBase {\n\ttoolName: \"ls\";\n\tdetails: LsToolDetails | undefined;\n}\n\nexport interface CustomToolResultEvent extends ToolResultEventBase {\n\ttoolName: string;\n\tdetails: unknown;\n}\n\n/** Fired after a tool executes. Can modify result. */\nexport type ToolResultEvent =\n\t| BashToolResultEvent\n\t| ReadToolResultEvent\n\t| EditToolResultEvent\n\t| WriteToolResultEvent\n\t| GrepToolResultEvent\n\t| FindToolResultEvent\n\t| LsToolResultEvent\n\t| CustomToolResultEvent;\n\n/**\n * Type guard for narrowing ToolResultEvent by tool name.\n *\n * Built-in tools narrow automatically (no type params needed):\n * ```ts\n * if (isToolResultEventType(\"bash\", event)) {\n * event.details; // BashToolDetails | undefined\n * }\n * ```\n *\n * Custom tools require explicit type parameters:\n * ```ts\n * if (isToolResultEventType<\"my_tool\", MyDetails>(\"my_tool\", event)) {\n * event.details; // typed\n * }\n * ```\n */\nexport function isToolResultEventType(toolName: \"bash\", event: ToolResultEvent): event is BashToolResultEvent;\nexport function isToolResultEventType(toolName: \"read\", event: ToolResultEvent): event is ReadToolResultEvent;\nexport function isToolResultEventType(toolName: \"edit\", event: ToolResultEvent): event is EditToolResultEvent;\nexport function isToolResultEventType(toolName: \"write\", event: ToolResultEvent): event is WriteToolResultEvent;\nexport function isToolResultEventType(toolName: \"grep\", event: ToolResultEvent): event is GrepToolResultEvent;\nexport function isToolResultEventType(toolName: \"find\", event: ToolResultEvent): event is FindToolResultEvent;\nexport function isToolResultEventType(toolName: \"ls\", event: ToolResultEvent): event is LsToolResultEvent;\nexport function isToolResultEventType<TName extends string, TDetails = unknown>(\n\ttoolName: TName,\n\tevent: ToolResultEvent,\n): event is ToolResultEvent & { toolName: TName; details: TDetails };\nexport function isToolResultEventType(toolName: string, event: ToolResultEvent): boolean {\n\treturn event.toolName === toolName;\n}\n\n/**\n * Type guard for narrowing ToolCallEvent by tool name.\n *\n * Built-in tools narrow automatically (no type params needed):\n * ```ts\n * if (isToolCallEventType(\"bash\", event)) {\n * event.input.command; // string\n * }\n * ```\n *\n * Custom tools require explicit type parameters:\n * ```ts\n * if (isToolCallEventType<\"my_tool\", MyToolInput>(\"my_tool\", event)) {\n * event.input.action; // typed\n * }\n * ```\n *\n * Note: Direct narrowing via `event.toolName === \"bash\"` doesn't work because\n * CustomToolCallEvent.toolName is `string` which overlaps with all literals.\n */\nexport function isToolCallEventType(toolName: \"bash\", event: ToolCallEvent): event is BashToolCallEvent;\nexport function isToolCallEventType(toolName: \"read\", event: ToolCallEvent): event is ReadToolCallEvent;\nexport function isToolCallEventType(toolName: \"edit\", event: ToolCallEvent): event is EditToolCallEvent;\nexport function isToolCallEventType(toolName: \"write\", event: ToolCallEvent): event is WriteToolCallEvent;\nexport function isToolCallEventType(toolName: \"grep\", event: ToolCallEvent): event is GrepToolCallEvent;\nexport function isToolCallEventType(toolName: \"find\", event: ToolCallEvent): event is FindToolCallEvent;\nexport function isToolCallEventType(toolName: \"ls\", event: ToolCallEvent): event is LsToolCallEvent;\nexport function isToolCallEventType<TName extends string, TInput extends Record<string, unknown>>(\n\ttoolName: TName,\n\tevent: ToolCallEvent,\n): event is ToolCallEvent & { toolName: TName; input: TInput };\nexport function isToolCallEventType(toolName: string, event: ToolCallEvent): boolean {\n\treturn event.toolName === toolName;\n}\n\n/** Union of all event types */\nexport type ExtensionEvent =\n\t| ResourcesDiscoverEvent\n\t| SessionEvent\n\t| ContextEvent\n\t| BeforeProviderRequestEvent\n\t| BeforeAgentStartEvent\n\t| AgentStartEvent\n\t| AgentEndEvent\n\t| TurnStartEvent\n\t| TurnEndEvent\n\t| MessageStartEvent\n\t| MessageUpdateEvent\n\t| MessageEndEvent\n\t| ToolExecutionStartEvent\n\t| ToolExecutionUpdateEvent\n\t| ToolExecutionEndEvent\n\t| ModelSelectEvent\n\t| BashTransformEvent\n\t| UserBashEvent\n\t| InputEvent\n\t| ToolCallEvent\n\t| ToolResultEvent;\n\n// ============================================================================\n// Event Results\n// ============================================================================\n\nexport interface ContextEventResult {\n\tmessages?: AgentMessage[];\n}\n\nexport type BeforeProviderRequestEventResult = unknown;\n\nexport interface ToolCallEventResult {\n\tblock?: boolean;\n\treason?: string;\n}\n\n/** Result from user_bash event handler */\nexport interface UserBashEventResult {\n\t/** Custom operations to use for execution */\n\toperations?: BashOperations;\n\t/** Full replacement: extension handled execution, use this result */\n\tresult?: BashResult;\n}\n\nexport interface ToolResultEventResult {\n\tcontent?: (TextContent | ImageContent)[];\n\tdetails?: unknown;\n\tisError?: boolean;\n}\n\nexport interface BeforeAgentStartEventResult {\n\tmessage?: Pick<CustomMessage, \"customType\" | \"content\" | \"display\" | \"details\">;\n\t/** Replace the system prompt for this turn. If multiple extensions return this, they are chained. */\n\tsystemPrompt?: string;\n}\n\nexport interface SessionDirectoryResult {\n\t/** Custom session directory path. If multiple extensions return this, the last one wins. */\n\tsessionDir?: string;\n}\n\n/** Special startup-only handler. Unlike other events, this receives no ExtensionContext. */\nexport type SessionDirectoryHandler = (\n\tevent: SessionDirectoryEvent,\n) => Promise<SessionDirectoryResult | undefined> | SessionDirectoryResult | undefined;\n\nexport interface SessionBeforeSwitchResult {\n\tcancel?: boolean;\n}\n\nexport interface SessionBeforeForkResult {\n\tcancel?: boolean;\n\tskipConversationRestore?: boolean;\n}\n\nexport interface SessionBeforeCompactResult {\n\tcancel?: boolean;\n\tcompaction?: CompactionResult;\n}\n\nexport interface SessionBeforeTreeResult {\n\tcancel?: boolean;\n\tsummary?: {\n\t\tsummary: string;\n\t\tdetails?: unknown;\n\t};\n\t/** Override custom instructions for summarization */\n\tcustomInstructions?: string;\n\t/** Override whether customInstructions replaces the default prompt */\n\treplaceInstructions?: boolean;\n\t/** Override label to attach to the branch summary entry */\n\tlabel?: string;\n}\n\n// ============================================================================\n// Message Rendering\n// ============================================================================\n\nexport interface MessageRenderOptions {\n\texpanded: boolean;\n}\n\nexport type MessageRenderer<T = unknown> = (\n\tmessage: CustomMessage<T>,\n\toptions: MessageRenderOptions,\n\ttheme: Theme,\n) => Component | undefined;\n\n// ============================================================================\n// Command Registration\n// ============================================================================\n\nexport interface RegisteredCommand {\n\tname: string;\n\tdescription?: string;\n\tgetArgumentCompletions?: (argumentPrefix: string) => AutocompleteItem[] | null;\n\thandler: (args: string, ctx: ExtensionCommandContext) => Promise<void>;\n}\n\nexport type LifecycleHookScope = \"user\" | \"project\";\nexport type LifecycleHookPhase = \"beforeInstall\" | \"afterInstall\" | \"beforeRemove\" | \"afterRemove\";\n\nexport interface LifecycleHookContext {\n\t/** Lifecycle phase currently being executed. */\n\tphase: LifecycleHookPhase;\n\t/** Package source string passed to install (npm:, git:, https://, local path). */\n\tsource: string;\n\t/** Resolved installed package path (or resolved local path), when available for this phase. */\n\tinstalledPath?: string;\n\t/** Where the package was installed. */\n\tscope: LifecycleHookScope;\n\t/** Current working directory for the install invocation. */\n\tcwd: string;\n\t/** Whether install is running in an interactive TTY. */\n\tinteractive: boolean;\n\t/** Info-level logging sink for install output. */\n\tlog(message: string): void;\n\t/** Warning-level logging sink for install output. */\n\twarn(message: string): void;\n\t/** Error-level logging sink for install output. */\n\terror(message: string): void;\n}\n\nexport type LifecycleHookHandler = (ctx: LifecycleHookContext) => Promise<void> | void;\nexport type LifecycleHookMap = Record<LifecycleHookPhase, LifecycleHookHandler[]>;\n\n// ============================================================================\n// Extension API\n// ============================================================================\n\n/** Handler function type for events */\n// biome-ignore lint/suspicious/noConfusingVoidType: void allows bare return statements\nexport type ExtensionHandler<E, R = undefined> = (event: E, ctx: ExtensionContext) => Promise<R | void> | R | void;\n\n/**\n * ExtensionAPI passed to extension factory functions.\n */\nexport interface ExtensionAPI {\n\t// =========================================================================\n\t// Event Subscription\n\t// =========================================================================\n\n\ton(event: \"resources_discover\", handler: ExtensionHandler<ResourcesDiscoverEvent, ResourcesDiscoverResult>): void;\n\ton(event: \"session_directory\", handler: SessionDirectoryHandler): void;\n\ton(event: \"session_start\", handler: ExtensionHandler<SessionStartEvent>): void;\n\ton(\n\t\tevent: \"session_before_switch\",\n\t\thandler: ExtensionHandler<SessionBeforeSwitchEvent, SessionBeforeSwitchResult>,\n\t): void;\n\ton(event: \"session_switch\", handler: ExtensionHandler<SessionSwitchEvent>): void;\n\ton(event: \"session_before_fork\", handler: ExtensionHandler<SessionBeforeForkEvent, SessionBeforeForkResult>): void;\n\ton(event: \"session_fork\", handler: ExtensionHandler<SessionForkEvent>): void;\n\ton(\n\t\tevent: \"session_before_compact\",\n\t\thandler: ExtensionHandler<SessionBeforeCompactEvent, SessionBeforeCompactResult>,\n\t): void;\n\ton(event: \"session_compact\", handler: ExtensionHandler<SessionCompactEvent>): void;\n\ton(event: \"session_shutdown\", handler: ExtensionHandler<SessionShutdownEvent>): void;\n\ton(event: \"session_before_tree\", handler: ExtensionHandler<SessionBeforeTreeEvent, SessionBeforeTreeResult>): void;\n\ton(event: \"session_tree\", handler: ExtensionHandler<SessionTreeEvent>): void;\n\ton(event: \"context\", handler: ExtensionHandler<ContextEvent, ContextEventResult>): void;\n\ton(\n\t\tevent: \"before_provider_request\",\n\t\thandler: ExtensionHandler<BeforeProviderRequestEvent, BeforeProviderRequestEventResult>,\n\t): void;\n\ton(event: \"before_agent_start\", handler: ExtensionHandler<BeforeAgentStartEvent, BeforeAgentStartEventResult>): void;\n\ton(event: \"agent_start\", handler: ExtensionHandler<AgentStartEvent>): void;\n\ton(event: \"agent_end\", handler: ExtensionHandler<AgentEndEvent>): void;\n\ton(event: \"turn_start\", handler: ExtensionHandler<TurnStartEvent>): void;\n\ton(event: \"turn_end\", handler: ExtensionHandler<TurnEndEvent>): void;\n\ton(event: \"message_start\", handler: ExtensionHandler<MessageStartEvent>): void;\n\ton(event: \"message_update\", handler: ExtensionHandler<MessageUpdateEvent>): void;\n\ton(event: \"message_end\", handler: ExtensionHandler<MessageEndEvent>): void;\n\ton(event: \"tool_execution_start\", handler: ExtensionHandler<ToolExecutionStartEvent>): void;\n\ton(event: \"tool_execution_update\", handler: ExtensionHandler<ToolExecutionUpdateEvent>): void;\n\ton(event: \"tool_execution_end\", handler: ExtensionHandler<ToolExecutionEndEvent>): void;\n\ton(event: \"model_select\", handler: ExtensionHandler<ModelSelectEvent>): void;\n\ton(event: \"bash_transform\", handler: ExtensionHandler<BashTransformEvent, BashTransformEventResult>): void;\n\ton(event: \"tool_call\", handler: ExtensionHandler<ToolCallEvent, ToolCallEventResult>): void;\n\ton(event: \"tool_result\", handler: ExtensionHandler<ToolResultEvent, ToolResultEventResult>): void;\n\ton(event: \"user_bash\", handler: ExtensionHandler<UserBashEvent, UserBashEventResult>): void;\n\ton(event: \"input\", handler: ExtensionHandler<InputEvent, InputEventResult>): void;\n\ton(event: \"before_model_select\", handler: ExtensionHandler<BeforeModelSelectEvent, BeforeModelSelectResult>): void;\n\ton(event: \"adjust_tool_set\", handler: ExtensionHandler<AdjustToolSetEvent, AdjustToolSetResult>): void;\n\n\t// =========================================================================\n\t// Event Emission (for host extensions that orchestrate model selection)\n\t// =========================================================================\n\n\t/** Emit before_model_select event. Returns override model ID or undefined. */\n\temitBeforeModelSelect(event: Omit<BeforeModelSelectEvent, \"type\">): Promise<BeforeModelSelectResult | undefined>;\n\n\t/** Emit adjust_tool_set event (ADR-005). Returns override tool names or undefined. */\n\temitAdjustToolSet(event: Omit<AdjustToolSetEvent, \"type\">): Promise<AdjustToolSetResult | undefined>;\n\n\t// =========================================================================\n\t// Tool Registration\n\t// =========================================================================\n\n\t/** Register a tool that the LLM can call. */\n\tregisterTool<TParams extends TSchema = TSchema, TDetails = unknown>(tool: ToolDefinition<TParams, TDetails>): void;\n\n\t// =========================================================================\n\t// Command, Shortcut, Flag Registration\n\t// =========================================================================\n\n\t/** Register a custom command. */\n\tregisterCommand(name: string, options: Omit<RegisteredCommand, \"name\">): void;\n\n\t/** Register a lifecycle hook run before package installation starts. */\n\tregisterBeforeInstall(handler: LifecycleHookHandler): void;\n\n\t/** Register a lifecycle hook run after package installation completes. */\n\tregisterAfterInstall(handler: LifecycleHookHandler): void;\n\n\t/** Register a lifecycle hook run before package removal starts. */\n\tregisterBeforeRemove(handler: LifecycleHookHandler): void;\n\n\t/** Register a lifecycle hook run after package removal completes. */\n\tregisterAfterRemove(handler: LifecycleHookHandler): void;\n\n\t/** Register a keyboard shortcut. */\n\tregisterShortcut(\n\t\tshortcut: KeyId,\n\t\toptions: {\n\t\t\tdescription?: string;\n\t\t\thandler: (ctx: ExtensionContext) => Promise<void> | void;\n\t\t},\n\t): void;\n\n\t/** Register a CLI flag. */\n\tregisterFlag(\n\t\tname: string,\n\t\toptions: {\n\t\t\tdescription?: string;\n\t\t\ttype: \"boolean\" | \"string\";\n\t\t\tdefault?: boolean | string;\n\t\t},\n\t): void;\n\n\t/** Get the value of a registered CLI flag. */\n\tgetFlag(name: string): boolean | string | undefined;\n\n\t// =========================================================================\n\t// Message Rendering\n\t// =========================================================================\n\n\t/** Register a custom renderer for CustomMessageEntry. */\n\tregisterMessageRenderer<T = unknown>(customType: string, renderer: MessageRenderer<T>): void;\n\n\t// =========================================================================\n\t// Actions\n\t// =========================================================================\n\n\t/** Send a custom message to the session. */\n\tsendMessage<T = unknown>(\n\t\tmessage: Pick<CustomMessage<T>, \"customType\" | \"content\" | \"display\" | \"details\">,\n\t\toptions?: { triggerTurn?: boolean; deliverAs?: \"steer\" | \"followUp\" | \"nextTurn\" },\n\t): void;\n\n\t/**\n\t * Send a user message to the agent. Always triggers a turn.\n\t * When the agent is streaming, use deliverAs to specify how to queue the message.\n\t */\n\tsendUserMessage(\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\toptions?: { deliverAs?: \"steer\" | \"followUp\" },\n\t): void;\n\n\t/**\n\t * Retry the last turn by removing the failed assistant response and\n\t * re-running the agent from the last user message. No-op if the last\n\t * message is not an assistant error.\n\t */\n\tretryLastTurn(): void;\n\n\t/** Append a custom entry to the session for state persistence (not sent to LLM). */\n\tappendEntry<T = unknown>(customType: string, data?: T): void;\n\n\t// =========================================================================\n\t// Session Metadata\n\t// =========================================================================\n\n\t/** Set the session display name (shown in session selector). */\n\tsetSessionName(name: string): void;\n\n\t/** Get the current session name, if set. */\n\tgetSessionName(): string | undefined;\n\n\t/** Set or clear a label on an entry. Labels are user-defined markers for bookmarking/navigation. */\n\tsetLabel(entryId: string, label: string | undefined): void;\n\n\t/** Execute a shell command. */\n\texec(command: string, args: string[], options?: ExecOptions): Promise<ExecResult>;\n\n\t/** Get the list of currently active tool names. */\n\tgetActiveTools(): string[];\n\n\t/** Get all configured tools with name and description. */\n\tgetAllTools(): ToolInfo[];\n\n\t/** Set the active tools by name. */\n\tsetActiveTools(toolNames: string[]): void;\n\n\t/** Get available slash commands in the current session. */\n\tgetCommands(): SlashCommandInfo[];\n\n\t// =========================================================================\n\t// Model and Thinking Level\n\t// =========================================================================\n\n\t/** Set the current model. Returns false if no API key available. */\n\tsetModel(model: Model<any>, options?: { persist?: boolean }): Promise<boolean>;\n\n\t/** Get current thinking level. */\n\tgetThinkingLevel(): ThinkingLevel;\n\n\t/** Set thinking level (clamped to model capabilities). */\n\tsetThinkingLevel(level: ThinkingLevel): void;\n\n\t// =========================================================================\n\t// Provider Registration\n\t// =========================================================================\n\n\t/**\n\t * Register or override a model provider.\n\t *\n\t * If `models` is provided: replaces all existing models for this provider.\n\t * If only `baseUrl` is provided: overrides the URL for existing models.\n\t * If `oauth` is provided: registers OAuth provider for /login support.\n\t * If `streamSimple` is provided: registers a custom API stream handler.\n\t *\n\t * During initial extension load this call is queued and applied once the\n\t * runner has bound its context. After that it takes effect immediately, so\n\t * it is safe to call from command handlers or event callbacks without\n\t * requiring a `/reload`.\n\t *\n\t * @example\n\t * // Register a new provider with custom models\n\t * pi.registerProvider(\"my-proxy\", {\n\t * baseUrl: \"https://proxy.example.com\",\n\t * apiKey: \"PROXY_API_KEY\",\n\t * api: \"anthropic-messages\",\n\t * models: [\n\t * {\n\t * id: \"claude-sonnet-4-20250514\",\n\t * name: \"Claude 4 Sonnet (proxy)\",\n\t * reasoning: false,\n\t * input: [\"text\", \"image\"],\n\t * cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },\n\t * contextWindow: 200000,\n\t * maxTokens: 16384\n\t * }\n\t * ]\n\t * });\n\t *\n\t * @example\n\t * // Override baseUrl for an existing provider\n\t * pi.registerProvider(\"anthropic\", {\n\t * baseUrl: \"https://proxy.example.com\"\n\t * });\n\t *\n\t * @example\n\t * // Register provider with OAuth support\n\t * pi.registerProvider(\"corporate-ai\", {\n\t * baseUrl: \"https://ai.corp.com\",\n\t * api: \"openai-responses\",\n\t * models: [...],\n\t * oauth: {\n\t * name: \"Corporate AI (SSO)\",\n\t * async login(callbacks) { ... },\n\t * async refreshToken(credentials) { ... },\n\t * getApiKey(credentials) { return credentials.access; }\n\t * }\n\t * });\n\t */\n\tregisterProvider(name: string, config: ProviderConfig): void;\n\n\t/**\n\t * Unregister a previously registered provider.\n\t *\n\t * Removes all models belonging to the named provider and restores any\n\t * built-in models that were overridden by it. Has no effect if the provider\n\t * is not currently registered.\n\t *\n\t * Like `registerProvider`, this takes effect immediately when called after\n\t * the initial load phase.\n\t *\n\t * @example\n\t * pi.unregisterProvider(\"my-proxy\");\n\t */\n\tunregisterProvider(name: string): void;\n\n\t/** Shared event bus for extension communication. */\n\tevents: EventBus;\n}\n\n// ============================================================================\n// Provider Registration Types\n// ============================================================================\n\n/** Configuration for registering a provider via pi.registerProvider(). */\nexport interface ProviderConfig {\n\t/** Auth behavior for provider availability and request key handling. Defaults to \"apiKey\". */\n\tauthMode?: \"apiKey\" | \"oauth\" | \"externalCli\" | \"none\";\n\t/** Optional readiness check. Return false if the provider cannot accept requests (e.g., CLI not authenticated, API key invalid).\n\t * Called before default auth checks. Trusted at the same level as extension code — extensions already have arbitrary code execution. */\n\tisReady?: () => boolean;\n\t/** Base URL for the API endpoint. Required when defining models. */\n\tbaseUrl?: string;\n\t/** API key or environment variable name. Required when defining models (unless oauth provided). */\n\tapiKey?: string;\n\t/** API type. Required at provider or model level when defining models. */\n\tapi?: Api;\n\t/** Optional streamSimple handler for custom APIs. */\n\tstreamSimple?: (model: Model<Api>, context: Context, options?: SimpleStreamOptions) => AssistantMessageEventStream;\n\t/** Custom headers to include in requests. */\n\theaders?: Record<string, string>;\n\t/** If true, adds Authorization: Bearer header with the resolved API key. */\n\tauthHeader?: boolean;\n\t/** Models to register. If provided, replaces all existing models for this provider. */\n\tmodels?: ProviderModelConfig[];\n\t/** OAuth provider for /login support. The `id` is set automatically from the provider name. */\n\toauth?: {\n\t\t/** Display name for the provider in login UI. */\n\t\tname: string;\n\t\t/** Run the login flow, return credentials to persist. */\n\t\tlogin(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials>;\n\t\t/** Refresh expired credentials, return updated credentials to persist. */\n\t\trefreshToken(credentials: OAuthCredentials): Promise<OAuthCredentials>;\n\t\t/** Convert credentials to API key string for the provider. */\n\t\tgetApiKey(credentials: OAuthCredentials): string;\n\t\t/** Optional: modify models for this provider (e.g., update baseUrl based on credentials). */\n\t\tmodifyModels?(models: Model<Api>[], credentials: OAuthCredentials): Model<Api>[];\n\t};\n}\n\n/** Configuration for a model within a provider. */\nexport interface ProviderModelConfig {\n\t/** Model ID (e.g., \"claude-sonnet-4-20250514\"). */\n\tid: string;\n\t/** Display name (e.g., \"Claude 4 Sonnet\"). */\n\tname: string;\n\t/** API type override for this model. */\n\tapi?: Api;\n\t/** Whether the model supports extended thinking. */\n\treasoning: boolean;\n\t/** Supported input types. */\n\tinput: (\"text\" | \"image\")[];\n\t/** Cost per token (for tracking, can be 0). */\n\tcost: { input: number; output: number; cacheRead: number; cacheWrite: number };\n\t/** Maximum context window size in tokens. */\n\tcontextWindow: number;\n\t/** Maximum output tokens. */\n\tmaxTokens: number;\n\t/** Custom headers for this model. */\n\theaders?: Record<string, string>;\n\t/** OpenAI compatibility settings. */\n\tcompat?: Model<Api>[\"compat\"];\n\t/** Opaque provider-specific options (e.g. Ollama keep_alive, num_gpu). */\n\tproviderOptions?: Record<string, unknown>;\n}\n\n/** Extension factory function type. Supports both sync and async initialization. */\nexport type ExtensionFactory = (pi: ExtensionAPI) => void | Promise<void>;\n\n// ============================================================================\n// Loaded Extension Types\n// ============================================================================\n\nexport interface RegisteredTool {\n\tdefinition: ToolDefinition;\n\textensionPath: string;\n}\n\nexport interface ExtensionFlag {\n\tname: string;\n\tdescription?: string;\n\ttype: \"boolean\" | \"string\";\n\tdefault?: boolean | string;\n\textensionPath: string;\n}\n\nexport interface ExtensionShortcut {\n\tshortcut: KeyId;\n\tdescription?: string;\n\thandler: (ctx: ExtensionContext) => Promise<void> | void;\n\textensionPath: string;\n}\n\ntype HandlerFn = (...args: unknown[]) => Promise<unknown>;\n\n/** Tool info with name, description, and parameter schema */\nexport type ToolInfo = Pick<ToolDefinition, \"name\" | \"description\" | \"parameters\">;\n\n/**\n * Shared state created by loader, used during registration and runtime.\n * Contains flag values (defaults set during registration, CLI values set after).\n */\nexport interface ExtensionRuntimeState {\n\tflagValues: Map<string, boolean | string>;\n\t/** Provider registrations queued during extension loading, processed when runner binds */\n\tpendingProviderRegistrations: Array<{ name: string; config: ProviderConfig }>;\n\t/**\n\t * Register or unregister a provider.\n\t *\n\t * Before bindCore(): queues registrations / removes from queue.\n\t * After bindCore(): calls ModelRegistry directly for immediate effect.\n\t */\n\tregisterProvider: (name: string, config: ProviderConfig) => void;\n\tunregisterProvider: (name: string) => void;\n\t/** Emit before_model_select event to all registered handlers. Bound by ExtensionRunner. */\n\temitBeforeModelSelect: (event: Omit<BeforeModelSelectEvent, \"type\">) => Promise<BeforeModelSelectResult | undefined>;\n\t/** Emit adjust_tool_set event to all registered handlers. Bound by ExtensionRunner (ADR-005). */\n\temitAdjustToolSet: (event: Omit<AdjustToolSetEvent, \"type\">) => Promise<AdjustToolSetResult | undefined>;\n}\n\n/**\n * Action implementations for pi.* API methods.\n * Provided to runner.initialize(), copied into the shared runtime.\n */\nexport interface ExtensionActions {\n\tsendMessage: <T = unknown>(\n\t\tmessage: Pick<CustomMessage<T>, \"customType\" | \"content\" | \"display\" | \"details\">,\n\t\toptions?: { triggerTurn?: boolean; deliverAs?: \"steer\" | \"followUp\" | \"nextTurn\" },\n\t) => void;\n\tsendUserMessage: (\n\t\tcontent: string | (TextContent | ImageContent)[],\n\t\toptions?: { deliverAs?: \"steer\" | \"followUp\" },\n\t) => void;\n\tretryLastTurn: () => void;\n\tappendEntry: <T = unknown>(customType: string, data?: T) => void;\n\tsetSessionName: (name: string) => void;\n\tgetSessionName: () => string | undefined;\n\tsetLabel: (entryId: string, label: string | undefined) => void;\n\tgetActiveTools: () => string[];\n\tgetAllTools: () => ToolInfo[];\n\tsetActiveTools: (toolNames: string[]) => void;\n\trefreshTools: () => void;\n\tgetCommands: () => SlashCommandInfo[];\n\tsetModel: (model: Model<any>, options?: { persist?: boolean }) => Promise<boolean>;\n\tgetThinkingLevel: () => ThinkingLevel;\n\tsetThinkingLevel: (level: ThinkingLevel) => void;\n}\n\n/**\n * Actions for ExtensionContext (ctx.* in event handlers).\n * Required by all modes.\n */\nexport interface ExtensionContextActions {\n\tgetModel: () => Model<any> | undefined;\n\tisIdle: () => boolean;\n\tabort: () => void;\n\thasPendingMessages: () => boolean;\n\tshutdown: () => void;\n\tgetContextUsage: () => ContextUsage | undefined;\n\tcompact: (options?: CompactOptions) => void;\n\tgetSystemPrompt: () => string;\n}\n\n/**\n * Actions for ExtensionCommandContext (ctx.* in command handlers).\n * Only needed for interactive mode where extension commands are invokable.\n */\nexport interface ExtensionCommandContextActions {\n\twaitForIdle: () => Promise<void>;\n\tnewSession: (options?: {\n\t\tparentSession?: string;\n\t\tsetup?: (sessionManager: SessionManager) => Promise<void>;\n\t}) => Promise<{ cancelled: boolean }>;\n\tfork: (entryId: string) => Promise<{ cancelled: boolean }>;\n\tnavigateTree: (\n\t\ttargetId: string,\n\t\toptions?: { summarize?: boolean; customInstructions?: string; replaceInstructions?: boolean; label?: string },\n\t) => Promise<{ cancelled: boolean }>;\n\tswitchSession: (sessionPath: string) => Promise<{ cancelled: boolean }>;\n\treload: () => Promise<void>;\n}\n\n/**\n * Full runtime = state + actions.\n * Created by loader with throwing action stubs, completed by runner.initialize().\n */\nexport interface ExtensionRuntime extends ExtensionRuntimeState, ExtensionActions {}\n\n/** Loaded extension with all registered items. */\nexport interface Extension {\n\tpath: string;\n\tresolvedPath: string;\n\thandlers: Map<string, HandlerFn[]>;\n\ttools: Map<string, RegisteredTool>;\n\tmessageRenderers: Map<string, MessageRenderer>;\n\tcommands: Map<string, RegisteredCommand>;\n\tflags: Map<string, ExtensionFlag>;\n\tshortcuts: Map<KeyId, ExtensionShortcut>;\n\tlifecycleHooks: LifecycleHookMap;\n}\n\n/** Result of loading extensions. */\nexport interface LoadExtensionsResult {\n\textensions: Extension[];\n\terrors: Array<{ path: string; error: string }>;\n\t/** Shared runtime - actions are throwing stubs until runner.initialize() */\n\truntime: ExtensionRuntime;\n}\n\n// ============================================================================\n// Extension Error\n// ============================================================================\n\nexport interface ExtensionError {\n\textensionPath: string;\n\tevent: string;\n\terror: string;\n\tstack?: string;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry-handler.d.ts","sourceRoot":"","sources":["../../src/core/retry-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,KAAK,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAG1D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAE7D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAE5D,gEAAgE;AAChE,MAAM,WAAW,gBAAgB;IAChC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,eAAe,EAAE,eAAe,CAAC;IAC1C,QAAQ,CAAC,aAAa,EAAE,aAAa,CAAC;IACtC,QAAQ,CAAC,gBAAgB,EAAE,gBAAgB,CAAC;IAC5C,QAAQ,EAAE,MAAM,KAAK,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;IACvC,YAAY,EAAE,MAAM,MAAM,CAAC;IAC3B,IAAI,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAA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,CAAC,kBAAkB;IAI1B;;;;OAIG;IACH,OAAO,CAAC,sBAAsB;IAyC9B,+DAA+D;IAC/D,OAAO,CAAC,yBAAyB;CAMjC"}
|
|
@@ -458,7 +458,10 @@ export class RetryHandler {
|
|
|
458
458
|
if (!currentModel)
|
|
459
459
|
return false;
|
|
460
460
|
// Only attempt claude-code fallback when the current provider is anthropic.
|
|
461
|
-
//
|
|
461
|
+
// Transport-specific (ADR-012): intentionally keys on provider, not api —
|
|
462
|
+
// the fallback specifically reroutes the plain `anthropic` transport to
|
|
463
|
+
// the `claude-code` transport. Other Anthropic-fronting transports
|
|
464
|
+
// (anthropic-vertex, amazon-bedrock) must not be rerouted.
|
|
462
465
|
if (currentModel.provider !== "anthropic")
|
|
463
466
|
return false;
|
|
464
467
|
// Find the same model ID under the claude-code provider
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry-handler.js","sourceRoot":"","sources":["../../src/core/retry-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAK/C,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAmB1C,MAAM,OAAO,YAAY;IAQxB,YAA6B,KAAuB;QAAvB,UAAK,GAAL,KAAK,CAAkB;QAP5C,0BAAqB,GAAgC,SAAS,CAAC;QAC/D,kBAAa,GAAG,CAAC,CAAC;QAClB,kBAAa,GAA8B,SAAS,CAAC;QACrD,kBAAa,GAA6B,SAAS,CAAC;QACpD,qBAAgB,GAAG,CAAC,CAAC;QACrB,qBAAgB,GAA8C,SAAS,CAAC;IAEzB,CAAC;IAExD,gDAAgD;IAChD,IAAI,YAAY;QACf,OAAO,IAAI,CAAC,aAAa,CAAC;IAC3B,CAAC;IAED,kDAAkD;IAClD,IAAI,UAAU;QACb,OAAO,IAAI,CAAC,aAAa,KAAK,SAAS,CAAC;IACzC,CAAC;IAED,oCAAoC;IACpC,IAAI,gBAAgB;QAC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AAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QACzD,CAAC;IACF,CAAC;CACD","sourcesContent":["/**\n * RetryHandler - Automatic retry logic with exponential backoff and credential/provider fallback.\n *\n * Handles retryable errors (overloaded, rate limit, server errors) by:\n * 1. Trying alternate credentials for the same provider\n * 2. Falling back to other providers via FallbackResolver\n * 3. Exponential backoff with configurable max retries\n *\n * Context overflow errors are NOT handled here (see compaction).\n */\n\nimport type { Agent } from \"@gsd/pi-agent-core\";\nimport type { AssistantMessage, Model } from \"@gsd/pi-ai\";\nimport { isContextOverflow } from \"@gsd/pi-ai\";\nimport type { UsageLimitErrorType } from \"./auth-storage.js\";\nimport type { FallbackResolver } from \"./fallback-resolver.js\";\nimport type { ModelRegistry } from \"./model-registry.js\";\nimport type { SettingsManager } from \"./settings-manager.js\";\nimport { sleep } from \"../utils/sleep.js\";\nimport type { AgentSessionEvent } from \"./agent-session.js\";\n\n/** Dependencies injected from AgentSession into RetryHandler */\nexport interface RetryHandlerDeps {\n\treadonly agent: Agent;\n\treadonly settingsManager: SettingsManager;\n\treadonly modelRegistry: ModelRegistry;\n\treadonly fallbackResolver: FallbackResolver;\n\tgetModel: () => Model<any> | undefined;\n\tgetSessionId: () => string;\n\temit: (event: AgentSessionEvent) => void;\n\t/** Called when the retry handler switches to a fallback model */\n\tonModelChange: (model: Model<any>) => void;\n\t/** Optional: check if the claude-code CLI provider is ready (installed + authed).\n\t * Injected from the app layer to preserve package boundary. */\n\tisClaudeCodeReady?: () => boolean;\n}\n\nexport class RetryHandler {\n\tprivate _retryAbortController: AbortController | undefined = undefined;\n\tprivate _retryAttempt = 0;\n\tprivate _retryPromise: Promise<void> | undefined = undefined;\n\tprivate _retryResolve: (() => void) | undefined = undefined;\n\tprivate _retryGeneration = 0;\n\tprivate _continueTimeout: ReturnType<typeof setTimeout> | undefined = undefined;\n\n\tconstructor(private readonly _deps: RetryHandlerDeps) {}\n\n\t/** Current retry attempt (0 if not retrying) */\n\tget retryAttempt(): number {\n\t\treturn this._retryAttempt;\n\t}\n\n\t/** Whether auto-retry is currently in progress */\n\tget isRetrying(): boolean {\n\t\treturn this._retryPromise !== undefined;\n\t}\n\n\t/** Whether auto-retry is enabled */\n\tget autoRetryEnabled(): boolean {\n\t\treturn this._deps.settingsManager.getRetryEnabled();\n\t}\n\n\t/** Toggle auto-retry setting */\n\tsetAutoRetryEnabled(enabled: boolean): void {\n\t\tthis._deps.settingsManager.setRetryEnabled(enabled);\n\t}\n\n\t/**\n\t * Create a retry promise synchronously for agent_end events.\n\t * Must be called synchronously from the agent event handler before\n\t * any async processing, so that waitForRetry() doesn't miss in-flight retries.\n\t */\n\tcreateRetryPromiseForAgentEnd(messages: Array<{ role: string } & Record<string, any>>): void {\n\t\tif (this._retryPromise) return;\n\n\t\tconst settings = this._deps.settingsManager.getRetrySettings();\n\t\tif (!settings.enabled) return;\n\n\t\tconst lastAssistant = this._findLastAssistantInMessages(messages);\n\t\tif (!lastAssistant || !this.isRetryableError(lastAssistant)) return;\n\n\t\tthis._retryPromise = new Promise((resolve) => {\n\t\t\tthis._retryResolve = resolve;\n\t\t});\n\t}\n\n\t/**\n\t * Handle a successful assistant response by resetting retry state.\n\t * Call this when an assistant message completes without error.\n\t */\n\thandleSuccessfulResponse(): void {\n\t\tif (this._retryAttempt > 0) {\n\t\t\tthis._deps.emit({\n\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\tsuccess: true,\n\t\t\t\tattempt: this._retryAttempt,\n\t\t\t});\n\t\t\tthis._retryAttempt = 0;\n\t\t\tthis._resolveRetry();\n\t\t}\n\t}\n\n\t/**\n\t * Check if an error is retryable (overloaded, rate limit, server errors).\n\t * Context overflow errors are NOT retryable (handled by compaction instead).\n\t */\n\tisRetryableError(message: AssistantMessage): boolean {\n\t\tif (message.stopReason !== \"error\" || !message.errorMessage) return false;\n\n\t\t// Context overflow is handled by compaction, not retry\n\t\tconst contextWindow = this._deps.getModel()?.contextWindow ?? 0;\n\t\tif (isContextOverflow(message, contextWindow)) return false;\n\n\t\tconst err = message.errorMessage;\n\t\t// \"temporarily backed off\" is intentionally excluded: it is an internally-\n\t\t// generated error from getApiKey() when credentials are in a backoff window.\n\t\t// Re-entering the retry handler for that message creates a cascade of empty\n\t\t// error entries in the session file, breaking resume (#3429).\n\t\treturn /overloaded|rate.?limit|too many requests|402|429|500|502|503|504|service.?unavailable|server.?error|internal.?error|connection.?error|connection.?refused|other side closed|fetch failed|upstream.?connect|reset before headers|terminated|retry delay|network.?(?:is\\s+)?unavailable|credentials.*expired|requires more credits|can only afford|insufficient credits|not enough credits|extra usage is required|(?:out of|no) extra usage|third.party.*draw from extra|third.party.*not.*available/i.test(\n\t\t\terr,\n\t\t);\n\t}\n\n\t/**\n\t * Handle retryable errors with exponential backoff.\n\t * When multiple credentials are available, marks the failing credential\n\t * as backed off and retries immediately with the next one.\n\t * @returns true if retry was initiated, false if max retries exceeded or disabled\n\t */\n\tasync handleRetryableError(message: AssistantMessage): Promise<boolean> {\n\t\tconst settings = this._deps.settingsManager.getRetrySettings();\n\t\tif (!settings.enabled) {\n\t\t\tthis._resolveRetry();\n\t\t\treturn false;\n\t\t}\n\n\t\t// Retry promise is created synchronously in createRetryPromiseForAgentEnd.\n\t\t// Keep a defensive fallback here in case a future refactor bypasses that path.\n\t\tif (!this._retryPromise) {\n\t\t\tthis._retryPromise = new Promise((resolve) => {\n\t\t\t\tthis._retryResolve = resolve;\n\t\t\t});\n\t\t}\n\n\t\t// Try credential fallback before counting against retry budget.\n\t\tconst retryGeneration = this._retryGeneration;\n\t\tif (this._deps.getModel() && message.errorMessage) {\n\t\t\t// Third-party subscription block (#3772): Anthropic blocks third-party apps\n\t\t\t// from using Pro/Max subscription quotas. If the claude-code CLI provider is\n\t\t\t// available, switch to it immediately — credential rotation won't help.\n\t\t\tif (this._isThirdPartyBlock(message.errorMessage)) {\n\t\t\t\tconst switched = this._tryClaudeCodeFallback(message, retryGeneration);\n\t\t\t\tif (switched) return true;\n\t\t\t\t// CLI not available — fall through to standard error handling\n\t\t\t}\n\n\t\t\tconst errorType = this._classifyErrorType(message.errorMessage);\n\t\t\tconst isRateLimit = errorType === \"rate_limit\";\n\t\t\tconst isQuotaError = errorType === \"quota_exhausted\";\n\n\t\t\t// Credit-aware retry (OpenRouter-style 402 affordability errors):\n\t\t\t// when provider reports \"can only afford N\", lower maxTokens and retry\n\t\t\t// on the same model before rotating credentials/providers.\n\t\t\tif (isQuotaError) {\n\t\t\t\tconst adjusted = this._tryAffordableMaxTokensRetry(message, retryGeneration);\n\t\t\t\tif (adjusted) return true;\n\t\t\t}\n\n\t\t\t// Credential rotation — only for transient rate limits (#3430).\n\t\t\t// Quota errors (\"Extra usage is required\") are account-level billing\n\t\t\t// gates; rotating to another credential on the same account won't help\n\t\t\t// and the 30-minute backoff blocks all provider requests needlessly.\n\t\t\tif (isRateLimit) {\n\t\t\t\tconst hasAlternate =\n\t\t\t\t\tthis._deps.modelRegistry.authStorage.markUsageLimitReached(\n\t\t\t\t\t\tthis._deps.getModel()!.provider,\n\t\t\t\t\t\tthis._deps.getSessionId(),\n\t\t\t\t\t\t{ errorType },\n\t\t\t\t\t);\n\n\t\t\t\tif (hasAlternate) {\n\t\t\t\t\tthis._removeLastAssistantError();\n\n\t\t\t\t\tthis._deps.emit({\n\t\t\t\t\t\ttype: \"auto_retry_start\",\n\t\t\t\t\t\tattempt: this._retryAttempt + 1,\n\t\t\t\t\t\tmaxAttempts: settings.maxRetries,\n\t\t\t\t\t\tdelayMs: 0,\n\t\t\t\t\t\terrorMessage: `${message.errorMessage} (switching credential)`,\n\t\t\t\t\t});\n\n\t\t\t\t\t// Retry immediately with the next credential - don't increment _retryAttempt\n\t\t\t\t\tthis._scheduleContinue(retryGeneration);\n\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Cross-provider fallback — for rate limits with all creds backed off,\n\t\t\t// or quota errors (which skip credential backoff entirely).\n\t\t\tif (isRateLimit || isQuotaError) {\n\t\t\t\tconst fallbackResult = await this._deps.fallbackResolver.findFallback(\n\t\t\t\t\tthis._deps.getModel()!,\n\t\t\t\t\terrorType,\n\t\t\t\t);\n\n\t\t\t\tif (fallbackResult) {\n\t\t\t\t\tconst previousProvider = this._deps.getModel()!.provider;\n\t\t\t\t\tthis._deps.agent.setModel(fallbackResult.model);\n\t\t\t\t\tthis._deps.onModelChange(fallbackResult.model);\n\t\t\t\t\tthis._removeLastAssistantError();\n\n\t\t\t\t\tthis._deps.emit({\n\t\t\t\t\t\ttype: \"fallback_provider_switch\",\n\t\t\t\t\t\tfrom: `${previousProvider}/${this._deps.getModel()?.id}`,\n\t\t\t\t\t\tto: `${fallbackResult.model.provider}/${fallbackResult.model.id}`,\n\t\t\t\t\t\treason: fallbackResult.reason,\n\t\t\t\t\t});\n\n\t\t\t\t\tthis._deps.emit({\n\t\t\t\t\t\ttype: \"auto_retry_start\",\n\t\t\t\t\t\tattempt: this._retryAttempt + 1,\n\t\t\t\t\t\tmaxAttempts: settings.maxRetries,\n\t\t\t\t\t\tdelayMs: 0,\n\t\t\t\t\t\terrorMessage: `${message.errorMessage} (${fallbackResult.reason})`,\n\t\t\t\t\t});\n\n\t\t\t\t\t// Retry immediately with fallback provider - don't increment _retryAttempt\n\t\t\t\t\tthis._scheduleContinue(retryGeneration);\n\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\n\t\t\t\t// No fallback available either\n\t\t\t\tif (isQuotaError) {\n\t\t\t\t\t// Try long-context model downgrade ([1m] → base) before giving up\n\t\t\t\t\tconst downgraded = this._tryLongContextDowngrade(message, retryGeneration);\n\t\t\t\t\tif (downgraded) return true;\n\n\t\t\t\t\tthis._deps.emit({\n\t\t\t\t\t\ttype: \"fallback_chain_exhausted\",\n\t\t\t\t\t\treason: `All providers exhausted for ${this._deps.getModel()!.provider}/${this._deps.getModel()!.id}`,\n\t\t\t\t\t});\n\t\t\t\t\tthis._deps.emit({\n\t\t\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\t\t\tsuccess: false,\n\t\t\t\t\t\tattempt: this._retryAttempt,\n\t\t\t\t\t\tfinalError: message.errorMessage,\n\t\t\t\t\t});\n\t\t\t\t\tthis._retryAttempt = 0;\n\t\t\t\t\tthis._resolveRetry();\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tthis._retryAttempt++;\n\n\t\tif (this._retryAttempt > settings.maxRetries) {\n\t\t\tthis._deps.emit({\n\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\tsuccess: false,\n\t\t\t\tattempt: this._retryAttempt - 1,\n\t\t\t\tfinalError: message.errorMessage,\n\t\t\t});\n\t\t\tthis._retryAttempt = 0;\n\t\t\tthis._resolveRetry();\n\t\t\treturn false;\n\t\t}\n\n\t\t// Use server-requested delay when available, capped by maxDelayMs.\n\t\t// Fall back to exponential backoff when no server hint is present.\n\t\tconst exponentialDelayMs = settings.baseDelayMs * 2 ** (this._retryAttempt - 1);\n\t\tlet delayMs: number;\n\t\tif (message.retryAfterMs !== undefined) {\n\t\t\tconst cap = settings.maxDelayMs > 0 ? settings.maxDelayMs : Infinity;\n\t\t\tif (message.retryAfterMs > cap) {\n\t\t\t\tthis._deps.emit({\n\t\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\tattempt: this._retryAttempt - 1,\n\t\t\t\t\tfinalError: `Rate limit reset in ${Math.ceil(message.retryAfterMs / 1000)}s (max: ${Math.ceil(cap / 1000)}s). ${message.errorMessage || \"\"}`.trim(),\n\t\t\t\t});\n\t\t\t\tthis._retryAttempt = 0;\n\t\t\t\tthis._resolveRetry();\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tdelayMs = message.retryAfterMs;\n\t\t} else {\n\t\t\tdelayMs = exponentialDelayMs;\n\t\t}\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"auto_retry_start\",\n\t\t\tattempt: this._retryAttempt,\n\t\t\tmaxAttempts: settings.maxRetries,\n\t\t\tdelayMs,\n\t\t\terrorMessage: message.errorMessage || \"Unknown error\",\n\t\t});\n\n\t\tthis._removeLastAssistantError();\n\n\t\t// Wait with exponential backoff (abortable)\n\t\tthis._retryAbortController = new AbortController();\n\t\ttry {\n\t\t\tawait sleep(delayMs, this._retryAbortController.signal);\n\t\t} catch {\n\t\t\t// Aborted during sleep. If the retry generation already advanced, this\n\t\t\t// cancellation was handled externally (e.g. explicit model switch).\n\t\t\tif (retryGeneration !== this._retryGeneration) {\n\t\t\t\tthis._retryAbortController = undefined;\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tconst attempt = this._retryAttempt;\n\t\t\tthis._retryAttempt = 0;\n\t\t\tthis._retryAbortController = undefined;\n\t\t\tthis._deps.emit({\n\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\tsuccess: false,\n\t\t\t\tattempt,\n\t\t\t\tfinalError: \"Retry cancelled\",\n\t\t\t});\n\t\t\tthis._resolveRetry();\n\t\t\treturn false;\n\t\t}\n\t\tthis._retryAbortController = undefined;\n\n\t\t// Retry via continue() - use setTimeout to break out of event handler chain\n\t\tthis._scheduleContinue(retryGeneration);\n\n\t\treturn true;\n\t}\n\n\t/** Cancel in-progress retry */\n\tabortRetry(): void {\n\t\tconst hadRetry =\n\t\t\tthis._retryPromise !== undefined\n\t\t\t|| this._retryAbortController !== undefined\n\t\t\t|| this._continueTimeout !== undefined;\n\t\tif (!hadRetry) return;\n\n\t\tconst attempt = this._retryAttempt > 0 ? this._retryAttempt : 1;\n\t\tthis._retryGeneration++;\n\t\tif (this._continueTimeout) {\n\t\t\tclearTimeout(this._continueTimeout);\n\t\t\tthis._continueTimeout = undefined;\n\t\t}\n\t\tif (this._retryAbortController) {\n\t\t\tthis._retryAbortController.abort();\n\t\t\tthis._retryAbortController = undefined;\n\t\t}\n\t\tthis._retryAttempt = 0;\n\t\tthis._deps.emit({\n\t\t\ttype: \"auto_retry_end\",\n\t\t\tsuccess: false,\n\t\t\tattempt,\n\t\t\tfinalError: \"Retry cancelled\",\n\t\t});\n\t\tthis._resolveRetry();\n\t}\n\n\t/**\n\t * Wait for any in-progress retry to complete.\n\t * Returns immediately if no retry is in progress.\n\t */\n\tasync waitForRetry(): Promise<void> {\n\t\tif (this._retryPromise) {\n\t\t\tawait this._retryPromise;\n\t\t}\n\t}\n\n\t/** Resolve the pending retry promise */\n\tresolveRetry(): void {\n\t\tthis._resolveRetry();\n\t}\n\n\t// =========================================================================\n\t// Private helpers\n\t// =========================================================================\n\n\tprivate _resolveRetry(): void {\n\t\tif (this._retryResolve) {\n\t\t\tthis._retryResolve();\n\t\t\tthis._retryResolve = undefined;\n\t\t\tthis._retryPromise = undefined;\n\t\t}\n\t}\n\n\tprivate _scheduleContinue(retryGeneration: number): void {\n\t\tif (this._continueTimeout) {\n\t\t\tclearTimeout(this._continueTimeout);\n\t\t}\n\t\tthis._continueTimeout = setTimeout(() => {\n\t\t\tthis._continueTimeout = undefined;\n\t\t\tif (retryGeneration !== this._retryGeneration) return;\n\t\t\tthis._deps.agent.continue().catch(() => {});\n\t\t}, 0);\n\t}\n\n\tprivate _findLastAssistantInMessages(\n\t\tmessages: Array<{ role: string } & Record<string, any>>,\n\t): AssistantMessage | undefined {\n\t\tfor (let i = messages.length - 1; i >= 0; i--) {\n\t\t\tconst message = messages[i];\n\t\t\tif (message.role === \"assistant\") {\n\t\t\t\treturn message as AssistantMessage;\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Classify an error message into a usage-limit error type for credential backoff.\n\t */\n\tprivate _classifyErrorType(errorMessage: string): UsageLimitErrorType {\n\t\tconst err = errorMessage.toLowerCase();\n\t\t// Long-context entitlement errors are billing gates, not transient rate limits.\n\t\t// Must be checked before the generic 429/rate_limit regex.\n\t\tif (/extra usage is required|long context required/i.test(err)) return \"quota_exhausted\";\n\t\tif (/requires more credits|can only afford|insufficient credits|not enough credits|credit balance/i.test(err))\n\t\t\treturn \"quota_exhausted\";\n\t\tif (/quota|billing|exceeded.*limit|usage.*limit/i.test(err)) return \"quota_exhausted\";\n\t\tif (/rate.?limit|too many requests|429/i.test(err)) return \"rate_limit\";\n\t\tif (/500|502|503|504|server.?error|internal.?error|service.?unavailable/i.test(err)) return \"server_error\";\n\t\treturn \"unknown\";\n\t}\n\n\t/**\n\t * Attempt a same-model retry by reducing maxTokens when provider reports\n\t * an affordability cap (e.g., \"can only afford 329\").\n\t */\n\tprivate _tryAffordableMaxTokensRetry(message: AssistantMessage, retryGeneration: number): boolean {\n\t\tconst currentModel = this._deps.getModel();\n\t\tif (!currentModel || !message.errorMessage) return false;\n\n\t\t// Example: \"can only afford 329\"\n\t\tconst match = message.errorMessage.match(/can only afford\\s+([\\d,]+)/i);\n\t\tif (!match?.[1]) return false;\n\n\t\tconst affordable = Number.parseInt(match[1].replace(/,/g, \"\"), 10);\n\t\tif (!Number.isFinite(affordable) || affordable <= 0) return false;\n\n\t\t// Leave a small buffer so slight input variance doesn't immediately re-fail.\n\t\tconst safetyBuffer = Math.min(64, Math.max(16, Math.floor(affordable * 0.1)));\n\t\tconst targetMaxTokens = Math.max(64, affordable - safetyBuffer);\n\t\tconst downgradedMaxTokens = Math.min(currentModel.maxTokens, targetMaxTokens);\n\t\tif (downgradedMaxTokens >= currentModel.maxTokens) return false;\n\n\t\tconst downgradedModel = {\n\t\t\t...currentModel,\n\t\t\tmaxTokens: downgradedMaxTokens,\n\t\t};\n\n\t\tthis._deps.agent.setModel(downgradedModel);\n\t\tthis._deps.onModelChange(downgradedModel);\n\t\tthis._removeLastAssistantError();\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"fallback_provider_switch\",\n\t\t\tfrom: `${currentModel.provider}/${currentModel.id} (maxTokens=${currentModel.maxTokens})`,\n\t\t\tto: `${downgradedModel.provider}/${downgradedModel.id} (maxTokens=${downgradedModel.maxTokens})`,\n\t\t\treason: `credit-aware retry: provider affordable cap ${affordable} tokens`,\n\t\t});\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"auto_retry_start\",\n\t\t\tattempt: this._retryAttempt + 1,\n\t\t\tmaxAttempts: this._deps.settingsManager.getRetrySettings().maxRetries,\n\t\t\tdelayMs: 0,\n\t\t\terrorMessage: `${message.errorMessage} (reducing max tokens)`,\n\t\t});\n\n\t\tthis._scheduleContinue(retryGeneration);\n\t\treturn true;\n\t}\n\n\t/**\n\t * Attempt to downgrade a long-context model (e.g. claude-opus-4-6[1m]) to its\n\t * base model (claude-opus-4-6) when the account lacks the long-context billing\n\t * entitlement. Returns true if the downgrade was initiated.\n\t */\n\tprivate _tryLongContextDowngrade(message: AssistantMessage, retryGeneration: number): boolean {\n\t\tconst currentModel = this._deps.getModel();\n\t\tif (!currentModel) return false;\n\n\t\t// Only attempt downgrade for [1m] (or similar long-context) model IDs\n\t\tconst match = currentModel.id.match(/^(.+)\\[\\d+m\\]$/);\n\t\tif (!match) return false;\n\n\t\tconst baseModelId = match[1];\n\t\tconst baseModel = this._deps.modelRegistry.find(currentModel.provider, baseModelId);\n\t\tif (!baseModel) return false;\n\n\t\tconst previousId = currentModel.id;\n\t\tthis._deps.agent.setModel(baseModel);\n\t\tthis._deps.onModelChange(baseModel);\n\t\tthis._removeLastAssistantError();\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"fallback_provider_switch\",\n\t\t\tfrom: `${currentModel.provider}/${previousId}`,\n\t\t\tto: `${baseModel.provider}/${baseModel.id}`,\n\t\t\treason: `long context downgrade: ${previousId} → ${baseModel.id}`,\n\t\t});\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"auto_retry_start\",\n\t\t\tattempt: this._retryAttempt + 1,\n\t\t\tmaxAttempts: this._deps.settingsManager.getRetrySettings().maxRetries,\n\t\t\tdelayMs: 0,\n\t\t\terrorMessage: `${message.errorMessage} (long context downgrade)`,\n\t\t});\n\n\t\tthis._scheduleContinue(retryGeneration);\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Detect Anthropic subscription block errors (#3772).\n\t * These are hard policy blocks, not transient rate limits — credential\n\t * rotation will not help. Matches both the explicit \"third-party\" message\n\t * and the \"out of extra usage\" variant that subscription users receive.\n\t */\n\tprivate _isThirdPartyBlock(errorMessage: string): boolean {\n\t\treturn /third[- .]party.*(?:draw from extra|not.*available|plan limits|not permitted|cannot be used|not supported)|(?:out of|no) extra usage/i.test(errorMessage);\n\t}\n\n\t/**\n\t * Attempt to switch to the claude-code CLI provider when the current\n\t * Anthropic provider is blocked by the third-party policy (#3772).\n\t * Returns true if the switch was made and retry scheduled.\n\t */\n\tprivate _tryClaudeCodeFallback(message: AssistantMessage, retryGeneration: number): boolean {\n\t\tif (!this._deps.isClaudeCodeReady?.()) return false;\n\n\t\tconst currentModel = this._deps.getModel();\n\t\tif (!currentModel) return false;\n\n\t\t// Only attempt claude-code fallback when the current provider is anthropic.\n\t\t// Other providers may produce similar error text but should not be rerouted.\n\t\tif (currentModel.provider !== \"anthropic\") return false;\n\n\t\t// Find the same model ID under the claude-code provider\n\t\tconst ccModel = this._deps.modelRegistry.find(\"claude-code\", currentModel.id);\n\t\tif (!ccModel) return false;\n\n\t\tconst previousProvider = currentModel.provider;\n\t\tthis._deps.agent.setModel(ccModel);\n\t\tthis._deps.onModelChange(ccModel);\n\t\tthis._removeLastAssistantError();\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"fallback_provider_switch\",\n\t\t\tfrom: `${previousProvider}/${currentModel.id}`,\n\t\t\tto: `claude-code/${ccModel.id}`,\n\t\t\treason: \"Anthropic subscription blocked for third-party apps — routing through Claude Code CLI\",\n\t\t});\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"auto_retry_start\",\n\t\t\tattempt: this._retryAttempt + 1,\n\t\t\tmaxAttempts: this._deps.settingsManager.getRetrySettings().maxRetries,\n\t\t\tdelayMs: 0,\n\t\t\terrorMessage: `${message.errorMessage} (switching to Claude Code CLI)`,\n\t\t});\n\n\t\tthis._scheduleContinue(retryGeneration);\n\t\treturn true;\n\t}\n\n\t/** Remove the last assistant error message from agent state */\n\tprivate _removeLastAssistantError(): void {\n\t\tconst messages = this._deps.agent.state.messages;\n\t\tif (messages.length > 0 && messages[messages.length - 1].role === \"assistant\") {\n\t\t\tthis._deps.agent.replaceMessages(messages.slice(0, -1));\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"retry-handler.js","sourceRoot":"","sources":["../../src/core/retry-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAK/C,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAmB1C,MAAM,OAAO,YAAY;IAQxB,YAA6B,KAAuB;QAAvB,UAAK,GAAL,KAAK,CAAkB;QAP5C,0BAAqB,GAAgC,SAAS,CAAC;QAC/D,kBAAa,GAAG,CAAC,CAAC;QAClB,kBAAa,GAA8B,SAAS,CAAC;QACrD,kBAAa,GAA6B,SAAS,CAAC;QACpD,qBAAgB,GAAG,CAAC,CAAC;QACrB,qBAAgB,GAA8C,SAAS,CAAC;IAEzB,CAAC;IAExD,gDAAgD;IAChD,IAAI,YAAY;QACf,OAAO,IAAI,CAAC,aAAa,CAAC;IAC3B,CAAC;IAED,kDAAkD;IAClD,IAAI,UAAU;QACb,OAAO,IAAI,CAAC,aAAa,KAAK,SAAS,CAAC;IACzC,CAAC;IAED,oCAAoC;IACpC,IAAI,gBAAgB;QACnB,OAAO,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,eAAe,EAAE,CAAC;IACrD,CAAC;IAED,gCAAgC;IAChC,mBAAmB,CAAC,OAAgB;QACnC,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;IACrD,CAAC;IAED;;;;OAIG;IACH,6BAA6B,CAAC,QAAuD;QACpF,IAAI,IAAI,CAAC,aAAa;YAAE,OAAO;QAE/B,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,gBAAgB,EAAE,CAAC;QA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AAI,CAAC,KAAK,CAAC,IAAI,CAAC;YACf,IAAI,EAAE,0BAA0B;YAChC,IAAI,EAAE,GAAG,gBAAgB,IAAI,YAAY,CAAC,EAAE,EAAE;YAC9C,EAAE,EAAE,eAAe,OAAO,CAAC,EAAE,EAAE;YAC/B,MAAM,EAAE,uFAAuF;SAC/F,CAAC,CAAC;QAEH,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;YACf,IAAI,EAAE,kBAAkB;YACxB,OAAO,EAAE,IAAI,CAAC,aAAa,GAAG,CAAC;YAC/B,WAAW,EAAE,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,gBAAgB,EAAE,CAAC,UAAU;YACrE,OAAO,EAAE,CAAC;YACV,YAAY,EAAE,GAAG,OAAO,CAAC,YAAY,iCAAiC;SACtE,CAAC,CAAC;QAEH,IAAI,CAAC,iBAAiB,CAAC,eAAe,CAAC,CAAC;QACxC,OAAO,IAAI,CAAC;IACb,CAAC;IAED,+DAA+D;IACvD,yBAAyB;QAChC,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC;QACjD,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,IAAI,QAAQ,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;YAC/E,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,eAAe,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QACzD,CAAC;IACF,CAAC;CACD","sourcesContent":["/**\n * RetryHandler - Automatic retry logic with exponential backoff and credential/provider fallback.\n *\n * Handles retryable errors (overloaded, rate limit, server errors) by:\n * 1. Trying alternate credentials for the same provider\n * 2. Falling back to other providers via FallbackResolver\n * 3. Exponential backoff with configurable max retries\n *\n * Context overflow errors are NOT handled here (see compaction).\n */\n\nimport type { Agent } from \"@gsd/pi-agent-core\";\nimport type { AssistantMessage, Model } from \"@gsd/pi-ai\";\nimport { isContextOverflow } from \"@gsd/pi-ai\";\nimport type { UsageLimitErrorType } from \"./auth-storage.js\";\nimport type { FallbackResolver } from \"./fallback-resolver.js\";\nimport type { ModelRegistry } from \"./model-registry.js\";\nimport type { SettingsManager } from \"./settings-manager.js\";\nimport { sleep } from \"../utils/sleep.js\";\nimport type { AgentSessionEvent } from \"./agent-session.js\";\n\n/** Dependencies injected from AgentSession into RetryHandler */\nexport interface RetryHandlerDeps {\n\treadonly agent: Agent;\n\treadonly settingsManager: SettingsManager;\n\treadonly modelRegistry: ModelRegistry;\n\treadonly fallbackResolver: FallbackResolver;\n\tgetModel: () => Model<any> | undefined;\n\tgetSessionId: () => string;\n\temit: (event: AgentSessionEvent) => void;\n\t/** Called when the retry handler switches to a fallback model */\n\tonModelChange: (model: Model<any>) => void;\n\t/** Optional: check if the claude-code CLI provider is ready (installed + authed).\n\t * Injected from the app layer to preserve package boundary. */\n\tisClaudeCodeReady?: () => boolean;\n}\n\nexport class RetryHandler {\n\tprivate _retryAbortController: AbortController | undefined = undefined;\n\tprivate _retryAttempt = 0;\n\tprivate _retryPromise: Promise<void> | undefined = undefined;\n\tprivate _retryResolve: (() => void) | undefined = undefined;\n\tprivate _retryGeneration = 0;\n\tprivate _continueTimeout: ReturnType<typeof setTimeout> | undefined = undefined;\n\n\tconstructor(private readonly _deps: RetryHandlerDeps) {}\n\n\t/** Current retry attempt (0 if not retrying) */\n\tget retryAttempt(): number {\n\t\treturn this._retryAttempt;\n\t}\n\n\t/** Whether auto-retry is currently in progress */\n\tget isRetrying(): boolean {\n\t\treturn this._retryPromise !== undefined;\n\t}\n\n\t/** Whether auto-retry is enabled */\n\tget autoRetryEnabled(): boolean {\n\t\treturn this._deps.settingsManager.getRetryEnabled();\n\t}\n\n\t/** Toggle auto-retry setting */\n\tsetAutoRetryEnabled(enabled: boolean): void {\n\t\tthis._deps.settingsManager.setRetryEnabled(enabled);\n\t}\n\n\t/**\n\t * Create a retry promise synchronously for agent_end events.\n\t * Must be called synchronously from the agent event handler before\n\t * any async processing, so that waitForRetry() doesn't miss in-flight retries.\n\t */\n\tcreateRetryPromiseForAgentEnd(messages: Array<{ role: string } & Record<string, any>>): void {\n\t\tif (this._retryPromise) return;\n\n\t\tconst settings = this._deps.settingsManager.getRetrySettings();\n\t\tif (!settings.enabled) return;\n\n\t\tconst lastAssistant = this._findLastAssistantInMessages(messages);\n\t\tif (!lastAssistant || !this.isRetryableError(lastAssistant)) return;\n\n\t\tthis._retryPromise = new Promise((resolve) => {\n\t\t\tthis._retryResolve = resolve;\n\t\t});\n\t}\n\n\t/**\n\t * Handle a successful assistant response by resetting retry state.\n\t * Call this when an assistant message completes without error.\n\t */\n\thandleSuccessfulResponse(): void {\n\t\tif (this._retryAttempt > 0) {\n\t\t\tthis._deps.emit({\n\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\tsuccess: true,\n\t\t\t\tattempt: this._retryAttempt,\n\t\t\t});\n\t\t\tthis._retryAttempt = 0;\n\t\t\tthis._resolveRetry();\n\t\t}\n\t}\n\n\t/**\n\t * Check if an error is retryable (overloaded, rate limit, server errors).\n\t * Context overflow errors are NOT retryable (handled by compaction instead).\n\t */\n\tisRetryableError(message: AssistantMessage): boolean {\n\t\tif (message.stopReason !== \"error\" || !message.errorMessage) return false;\n\n\t\t// Context overflow is handled by compaction, not retry\n\t\tconst contextWindow = this._deps.getModel()?.contextWindow ?? 0;\n\t\tif (isContextOverflow(message, contextWindow)) return false;\n\n\t\tconst err = message.errorMessage;\n\t\t// \"temporarily backed off\" is intentionally excluded: it is an internally-\n\t\t// generated error from getApiKey() when credentials are in a backoff window.\n\t\t// Re-entering the retry handler for that message creates a cascade of empty\n\t\t// error entries in the session file, breaking resume (#3429).\n\t\treturn /overloaded|rate.?limit|too many requests|402|429|500|502|503|504|service.?unavailable|server.?error|internal.?error|connection.?error|connection.?refused|other side closed|fetch failed|upstream.?connect|reset before headers|terminated|retry delay|network.?(?:is\\s+)?unavailable|credentials.*expired|requires more credits|can only afford|insufficient credits|not enough credits|extra usage is required|(?:out of|no) extra usage|third.party.*draw from extra|third.party.*not.*available/i.test(\n\t\t\terr,\n\t\t);\n\t}\n\n\t/**\n\t * Handle retryable errors with exponential backoff.\n\t * When multiple credentials are available, marks the failing credential\n\t * as backed off and retries immediately with the next one.\n\t * @returns true if retry was initiated, false if max retries exceeded or disabled\n\t */\n\tasync handleRetryableError(message: AssistantMessage): Promise<boolean> {\n\t\tconst settings = this._deps.settingsManager.getRetrySettings();\n\t\tif (!settings.enabled) {\n\t\t\tthis._resolveRetry();\n\t\t\treturn false;\n\t\t}\n\n\t\t// Retry promise is created synchronously in createRetryPromiseForAgentEnd.\n\t\t// Keep a defensive fallback here in case a future refactor bypasses that path.\n\t\tif (!this._retryPromise) {\n\t\t\tthis._retryPromise = new Promise((resolve) => {\n\t\t\t\tthis._retryResolve = resolve;\n\t\t\t});\n\t\t}\n\n\t\t// Try credential fallback before counting against retry budget.\n\t\tconst retryGeneration = this._retryGeneration;\n\t\tif (this._deps.getModel() && message.errorMessage) {\n\t\t\t// Third-party subscription block (#3772): Anthropic blocks third-party apps\n\t\t\t// from using Pro/Max subscription quotas. If the claude-code CLI provider is\n\t\t\t// available, switch to it immediately — credential rotation won't help.\n\t\t\tif (this._isThirdPartyBlock(message.errorMessage)) {\n\t\t\t\tconst switched = this._tryClaudeCodeFallback(message, retryGeneration);\n\t\t\t\tif (switched) return true;\n\t\t\t\t// CLI not available — fall through to standard error handling\n\t\t\t}\n\n\t\t\tconst errorType = this._classifyErrorType(message.errorMessage);\n\t\t\tconst isRateLimit = errorType === \"rate_limit\";\n\t\t\tconst isQuotaError = errorType === \"quota_exhausted\";\n\n\t\t\t// Credit-aware retry (OpenRouter-style 402 affordability errors):\n\t\t\t// when provider reports \"can only afford N\", lower maxTokens and retry\n\t\t\t// on the same model before rotating credentials/providers.\n\t\t\tif (isQuotaError) {\n\t\t\t\tconst adjusted = this._tryAffordableMaxTokensRetry(message, retryGeneration);\n\t\t\t\tif (adjusted) return true;\n\t\t\t}\n\n\t\t\t// Credential rotation — only for transient rate limits (#3430).\n\t\t\t// Quota errors (\"Extra usage is required\") are account-level billing\n\t\t\t// gates; rotating to another credential on the same account won't help\n\t\t\t// and the 30-minute backoff blocks all provider requests needlessly.\n\t\t\tif (isRateLimit) {\n\t\t\t\tconst hasAlternate =\n\t\t\t\t\tthis._deps.modelRegistry.authStorage.markUsageLimitReached(\n\t\t\t\t\t\tthis._deps.getModel()!.provider,\n\t\t\t\t\t\tthis._deps.getSessionId(),\n\t\t\t\t\t\t{ errorType },\n\t\t\t\t\t);\n\n\t\t\t\tif (hasAlternate) {\n\t\t\t\t\tthis._removeLastAssistantError();\n\n\t\t\t\t\tthis._deps.emit({\n\t\t\t\t\t\ttype: \"auto_retry_start\",\n\t\t\t\t\t\tattempt: this._retryAttempt + 1,\n\t\t\t\t\t\tmaxAttempts: settings.maxRetries,\n\t\t\t\t\t\tdelayMs: 0,\n\t\t\t\t\t\terrorMessage: `${message.errorMessage} (switching credential)`,\n\t\t\t\t\t});\n\n\t\t\t\t\t// Retry immediately with the next credential - don't increment _retryAttempt\n\t\t\t\t\tthis._scheduleContinue(retryGeneration);\n\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Cross-provider fallback — for rate limits with all creds backed off,\n\t\t\t// or quota errors (which skip credential backoff entirely).\n\t\t\tif (isRateLimit || isQuotaError) {\n\t\t\t\tconst fallbackResult = await this._deps.fallbackResolver.findFallback(\n\t\t\t\t\tthis._deps.getModel()!,\n\t\t\t\t\terrorType,\n\t\t\t\t);\n\n\t\t\t\tif (fallbackResult) {\n\t\t\t\t\tconst previousProvider = this._deps.getModel()!.provider;\n\t\t\t\t\tthis._deps.agent.setModel(fallbackResult.model);\n\t\t\t\t\tthis._deps.onModelChange(fallbackResult.model);\n\t\t\t\t\tthis._removeLastAssistantError();\n\n\t\t\t\t\tthis._deps.emit({\n\t\t\t\t\t\ttype: \"fallback_provider_switch\",\n\t\t\t\t\t\tfrom: `${previousProvider}/${this._deps.getModel()?.id}`,\n\t\t\t\t\t\tto: `${fallbackResult.model.provider}/${fallbackResult.model.id}`,\n\t\t\t\t\t\treason: fallbackResult.reason,\n\t\t\t\t\t});\n\n\t\t\t\t\tthis._deps.emit({\n\t\t\t\t\t\ttype: \"auto_retry_start\",\n\t\t\t\t\t\tattempt: this._retryAttempt + 1,\n\t\t\t\t\t\tmaxAttempts: settings.maxRetries,\n\t\t\t\t\t\tdelayMs: 0,\n\t\t\t\t\t\terrorMessage: `${message.errorMessage} (${fallbackResult.reason})`,\n\t\t\t\t\t});\n\n\t\t\t\t\t// Retry immediately with fallback provider - don't increment _retryAttempt\n\t\t\t\t\tthis._scheduleContinue(retryGeneration);\n\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\n\t\t\t\t// No fallback available either\n\t\t\t\tif (isQuotaError) {\n\t\t\t\t\t// Try long-context model downgrade ([1m] → base) before giving up\n\t\t\t\t\tconst downgraded = this._tryLongContextDowngrade(message, retryGeneration);\n\t\t\t\t\tif (downgraded) return true;\n\n\t\t\t\t\tthis._deps.emit({\n\t\t\t\t\t\ttype: \"fallback_chain_exhausted\",\n\t\t\t\t\t\treason: `All providers exhausted for ${this._deps.getModel()!.provider}/${this._deps.getModel()!.id}`,\n\t\t\t\t\t});\n\t\t\t\t\tthis._deps.emit({\n\t\t\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\t\t\tsuccess: false,\n\t\t\t\t\t\tattempt: this._retryAttempt,\n\t\t\t\t\t\tfinalError: message.errorMessage,\n\t\t\t\t\t});\n\t\t\t\t\tthis._retryAttempt = 0;\n\t\t\t\t\tthis._resolveRetry();\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tthis._retryAttempt++;\n\n\t\tif (this._retryAttempt > settings.maxRetries) {\n\t\t\tthis._deps.emit({\n\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\tsuccess: false,\n\t\t\t\tattempt: this._retryAttempt - 1,\n\t\t\t\tfinalError: message.errorMessage,\n\t\t\t});\n\t\t\tthis._retryAttempt = 0;\n\t\t\tthis._resolveRetry();\n\t\t\treturn false;\n\t\t}\n\n\t\t// Use server-requested delay when available, capped by maxDelayMs.\n\t\t// Fall back to exponential backoff when no server hint is present.\n\t\tconst exponentialDelayMs = settings.baseDelayMs * 2 ** (this._retryAttempt - 1);\n\t\tlet delayMs: number;\n\t\tif (message.retryAfterMs !== undefined) {\n\t\t\tconst cap = settings.maxDelayMs > 0 ? settings.maxDelayMs : Infinity;\n\t\t\tif (message.retryAfterMs > cap) {\n\t\t\t\tthis._deps.emit({\n\t\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\tattempt: this._retryAttempt - 1,\n\t\t\t\t\tfinalError: `Rate limit reset in ${Math.ceil(message.retryAfterMs / 1000)}s (max: ${Math.ceil(cap / 1000)}s). ${message.errorMessage || \"\"}`.trim(),\n\t\t\t\t});\n\t\t\t\tthis._retryAttempt = 0;\n\t\t\t\tthis._resolveRetry();\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tdelayMs = message.retryAfterMs;\n\t\t} else {\n\t\t\tdelayMs = exponentialDelayMs;\n\t\t}\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"auto_retry_start\",\n\t\t\tattempt: this._retryAttempt,\n\t\t\tmaxAttempts: settings.maxRetries,\n\t\t\tdelayMs,\n\t\t\terrorMessage: message.errorMessage || \"Unknown error\",\n\t\t});\n\n\t\tthis._removeLastAssistantError();\n\n\t\t// Wait with exponential backoff (abortable)\n\t\tthis._retryAbortController = new AbortController();\n\t\ttry {\n\t\t\tawait sleep(delayMs, this._retryAbortController.signal);\n\t\t} catch {\n\t\t\t// Aborted during sleep. If the retry generation already advanced, this\n\t\t\t// cancellation was handled externally (e.g. explicit model switch).\n\t\t\tif (retryGeneration !== this._retryGeneration) {\n\t\t\t\tthis._retryAbortController = undefined;\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tconst attempt = this._retryAttempt;\n\t\t\tthis._retryAttempt = 0;\n\t\t\tthis._retryAbortController = undefined;\n\t\t\tthis._deps.emit({\n\t\t\t\ttype: \"auto_retry_end\",\n\t\t\t\tsuccess: false,\n\t\t\t\tattempt,\n\t\t\t\tfinalError: \"Retry cancelled\",\n\t\t\t});\n\t\t\tthis._resolveRetry();\n\t\t\treturn false;\n\t\t}\n\t\tthis._retryAbortController = undefined;\n\n\t\t// Retry via continue() - use setTimeout to break out of event handler chain\n\t\tthis._scheduleContinue(retryGeneration);\n\n\t\treturn true;\n\t}\n\n\t/** Cancel in-progress retry */\n\tabortRetry(): void {\n\t\tconst hadRetry =\n\t\t\tthis._retryPromise !== undefined\n\t\t\t|| this._retryAbortController !== undefined\n\t\t\t|| this._continueTimeout !== undefined;\n\t\tif (!hadRetry) return;\n\n\t\tconst attempt = this._retryAttempt > 0 ? this._retryAttempt : 1;\n\t\tthis._retryGeneration++;\n\t\tif (this._continueTimeout) {\n\t\t\tclearTimeout(this._continueTimeout);\n\t\t\tthis._continueTimeout = undefined;\n\t\t}\n\t\tif (this._retryAbortController) {\n\t\t\tthis._retryAbortController.abort();\n\t\t\tthis._retryAbortController = undefined;\n\t\t}\n\t\tthis._retryAttempt = 0;\n\t\tthis._deps.emit({\n\t\t\ttype: \"auto_retry_end\",\n\t\t\tsuccess: false,\n\t\t\tattempt,\n\t\t\tfinalError: \"Retry cancelled\",\n\t\t});\n\t\tthis._resolveRetry();\n\t}\n\n\t/**\n\t * Wait for any in-progress retry to complete.\n\t * Returns immediately if no retry is in progress.\n\t */\n\tasync waitForRetry(): Promise<void> {\n\t\tif (this._retryPromise) {\n\t\t\tawait this._retryPromise;\n\t\t}\n\t}\n\n\t/** Resolve the pending retry promise */\n\tresolveRetry(): void {\n\t\tthis._resolveRetry();\n\t}\n\n\t// =========================================================================\n\t// Private helpers\n\t// =========================================================================\n\n\tprivate _resolveRetry(): void {\n\t\tif (this._retryResolve) {\n\t\t\tthis._retryResolve();\n\t\t\tthis._retryResolve = undefined;\n\t\t\tthis._retryPromise = undefined;\n\t\t}\n\t}\n\n\tprivate _scheduleContinue(retryGeneration: number): void {\n\t\tif (this._continueTimeout) {\n\t\t\tclearTimeout(this._continueTimeout);\n\t\t}\n\t\tthis._continueTimeout = setTimeout(() => {\n\t\t\tthis._continueTimeout = undefined;\n\t\t\tif (retryGeneration !== this._retryGeneration) return;\n\t\t\tthis._deps.agent.continue().catch(() => {});\n\t\t}, 0);\n\t}\n\n\tprivate _findLastAssistantInMessages(\n\t\tmessages: Array<{ role: string } & Record<string, any>>,\n\t): AssistantMessage | undefined {\n\t\tfor (let i = messages.length - 1; i >= 0; i--) {\n\t\t\tconst message = messages[i];\n\t\t\tif (message.role === \"assistant\") {\n\t\t\t\treturn message as AssistantMessage;\n\t\t\t}\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Classify an error message into a usage-limit error type for credential backoff.\n\t */\n\tprivate _classifyErrorType(errorMessage: string): UsageLimitErrorType {\n\t\tconst err = errorMessage.toLowerCase();\n\t\t// Long-context entitlement errors are billing gates, not transient rate limits.\n\t\t// Must be checked before the generic 429/rate_limit regex.\n\t\tif (/extra usage is required|long context required/i.test(err)) return \"quota_exhausted\";\n\t\tif (/requires more credits|can only afford|insufficient credits|not enough credits|credit balance/i.test(err))\n\t\t\treturn \"quota_exhausted\";\n\t\tif (/quota|billing|exceeded.*limit|usage.*limit/i.test(err)) return \"quota_exhausted\";\n\t\tif (/rate.?limit|too many requests|429/i.test(err)) return \"rate_limit\";\n\t\tif (/500|502|503|504|server.?error|internal.?error|service.?unavailable/i.test(err)) return \"server_error\";\n\t\treturn \"unknown\";\n\t}\n\n\t/**\n\t * Attempt a same-model retry by reducing maxTokens when provider reports\n\t * an affordability cap (e.g., \"can only afford 329\").\n\t */\n\tprivate _tryAffordableMaxTokensRetry(message: AssistantMessage, retryGeneration: number): boolean {\n\t\tconst currentModel = this._deps.getModel();\n\t\tif (!currentModel || !message.errorMessage) return false;\n\n\t\t// Example: \"can only afford 329\"\n\t\tconst match = message.errorMessage.match(/can only afford\\s+([\\d,]+)/i);\n\t\tif (!match?.[1]) return false;\n\n\t\tconst affordable = Number.parseInt(match[1].replace(/,/g, \"\"), 10);\n\t\tif (!Number.isFinite(affordable) || affordable <= 0) return false;\n\n\t\t// Leave a small buffer so slight input variance doesn't immediately re-fail.\n\t\tconst safetyBuffer = Math.min(64, Math.max(16, Math.floor(affordable * 0.1)));\n\t\tconst targetMaxTokens = Math.max(64, affordable - safetyBuffer);\n\t\tconst downgradedMaxTokens = Math.min(currentModel.maxTokens, targetMaxTokens);\n\t\tif (downgradedMaxTokens >= currentModel.maxTokens) return false;\n\n\t\tconst downgradedModel = {\n\t\t\t...currentModel,\n\t\t\tmaxTokens: downgradedMaxTokens,\n\t\t};\n\n\t\tthis._deps.agent.setModel(downgradedModel);\n\t\tthis._deps.onModelChange(downgradedModel);\n\t\tthis._removeLastAssistantError();\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"fallback_provider_switch\",\n\t\t\tfrom: `${currentModel.provider}/${currentModel.id} (maxTokens=${currentModel.maxTokens})`,\n\t\t\tto: `${downgradedModel.provider}/${downgradedModel.id} (maxTokens=${downgradedModel.maxTokens})`,\n\t\t\treason: `credit-aware retry: provider affordable cap ${affordable} tokens`,\n\t\t});\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"auto_retry_start\",\n\t\t\tattempt: this._retryAttempt + 1,\n\t\t\tmaxAttempts: this._deps.settingsManager.getRetrySettings().maxRetries,\n\t\t\tdelayMs: 0,\n\t\t\terrorMessage: `${message.errorMessage} (reducing max tokens)`,\n\t\t});\n\n\t\tthis._scheduleContinue(retryGeneration);\n\t\treturn true;\n\t}\n\n\t/**\n\t * Attempt to downgrade a long-context model (e.g. claude-opus-4-6[1m]) to its\n\t * base model (claude-opus-4-6) when the account lacks the long-context billing\n\t * entitlement. Returns true if the downgrade was initiated.\n\t */\n\tprivate _tryLongContextDowngrade(message: AssistantMessage, retryGeneration: number): boolean {\n\t\tconst currentModel = this._deps.getModel();\n\t\tif (!currentModel) return false;\n\n\t\t// Only attempt downgrade for [1m] (or similar long-context) model IDs\n\t\tconst match = currentModel.id.match(/^(.+)\\[\\d+m\\]$/);\n\t\tif (!match) return false;\n\n\t\tconst baseModelId = match[1];\n\t\tconst baseModel = this._deps.modelRegistry.find(currentModel.provider, baseModelId);\n\t\tif (!baseModel) return false;\n\n\t\tconst previousId = currentModel.id;\n\t\tthis._deps.agent.setModel(baseModel);\n\t\tthis._deps.onModelChange(baseModel);\n\t\tthis._removeLastAssistantError();\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"fallback_provider_switch\",\n\t\t\tfrom: `${currentModel.provider}/${previousId}`,\n\t\t\tto: `${baseModel.provider}/${baseModel.id}`,\n\t\t\treason: `long context downgrade: ${previousId} → ${baseModel.id}`,\n\t\t});\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"auto_retry_start\",\n\t\t\tattempt: this._retryAttempt + 1,\n\t\t\tmaxAttempts: this._deps.settingsManager.getRetrySettings().maxRetries,\n\t\t\tdelayMs: 0,\n\t\t\terrorMessage: `${message.errorMessage} (long context downgrade)`,\n\t\t});\n\n\t\tthis._scheduleContinue(retryGeneration);\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Detect Anthropic subscription block errors (#3772).\n\t * These are hard policy blocks, not transient rate limits — credential\n\t * rotation will not help. Matches both the explicit \"third-party\" message\n\t * and the \"out of extra usage\" variant that subscription users receive.\n\t */\n\tprivate _isThirdPartyBlock(errorMessage: string): boolean {\n\t\treturn /third[- .]party.*(?:draw from extra|not.*available|plan limits|not permitted|cannot be used|not supported)|(?:out of|no) extra usage/i.test(errorMessage);\n\t}\n\n\t/**\n\t * Attempt to switch to the claude-code CLI provider when the current\n\t * Anthropic provider is blocked by the third-party policy (#3772).\n\t * Returns true if the switch was made and retry scheduled.\n\t */\n\tprivate _tryClaudeCodeFallback(message: AssistantMessage, retryGeneration: number): boolean {\n\t\tif (!this._deps.isClaudeCodeReady?.()) return false;\n\n\t\tconst currentModel = this._deps.getModel();\n\t\tif (!currentModel) return false;\n\n\t\t// Only attempt claude-code fallback when the current provider is anthropic.\n\t\t// Transport-specific (ADR-012): intentionally keys on provider, not api —\n\t\t// the fallback specifically reroutes the plain `anthropic` transport to\n\t\t// the `claude-code` transport. Other Anthropic-fronting transports\n\t\t// (anthropic-vertex, amazon-bedrock) must not be rerouted.\n\t\tif (currentModel.provider !== \"anthropic\") return false;\n\n\t\t// Find the same model ID under the claude-code provider\n\t\tconst ccModel = this._deps.modelRegistry.find(\"claude-code\", currentModel.id);\n\t\tif (!ccModel) return false;\n\n\t\tconst previousProvider = currentModel.provider;\n\t\tthis._deps.agent.setModel(ccModel);\n\t\tthis._deps.onModelChange(ccModel);\n\t\tthis._removeLastAssistantError();\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"fallback_provider_switch\",\n\t\t\tfrom: `${previousProvider}/${currentModel.id}`,\n\t\t\tto: `claude-code/${ccModel.id}`,\n\t\t\treason: \"Anthropic subscription blocked for third-party apps — routing through Claude Code CLI\",\n\t\t});\n\n\t\tthis._deps.emit({\n\t\t\ttype: \"auto_retry_start\",\n\t\t\tattempt: this._retryAttempt + 1,\n\t\t\tmaxAttempts: this._deps.settingsManager.getRetrySettings().maxRetries,\n\t\t\tdelayMs: 0,\n\t\t\terrorMessage: `${message.errorMessage} (switching to Claude Code CLI)`,\n\t\t});\n\n\t\tthis._scheduleContinue(retryGeneration);\n\t\treturn true;\n\t}\n\n\t/** Remove the last assistant error message from agent state */\n\tprivate _removeLastAssistantError(): void {\n\t\tconst messages = this._deps.agent.state.messages;\n\t\tif (messages.length > 0 && messages[messages.length - 1].role === \"assistant\") {\n\t\t\tthis._deps.agent.replaceMessages(messages.slice(0, -1));\n\t\t}\n\t}\n}\n"]}
|
|
@@ -686,7 +686,10 @@ export class ExtensionRunner {
|
|
|
686
686
|
return currentMessages;
|
|
687
687
|
}
|
|
688
688
|
|
|
689
|
-
async emitBeforeProviderRequest(
|
|
689
|
+
async emitBeforeProviderRequest(
|
|
690
|
+
payload: unknown,
|
|
691
|
+
model?: { provider: string; id: string; api?: string },
|
|
692
|
+
): Promise<unknown> {
|
|
690
693
|
let currentPayload = payload;
|
|
691
694
|
|
|
692
695
|
await this.invokeHandlers("before_provider_request", () => ({
|
|
@@ -523,8 +523,8 @@ export interface ContextEvent {
|
|
|
523
523
|
export interface BeforeProviderRequestEvent {
|
|
524
524
|
type: "before_provider_request";
|
|
525
525
|
payload: unknown;
|
|
526
|
-
/** The resolved model for this request (provider, id, etc.) */
|
|
527
|
-
model?: { provider: string; id: string };
|
|
526
|
+
/** The resolved model for this request (provider, id, api, etc.) */
|
|
527
|
+
model?: { provider: string; id: string; api?: string };
|
|
528
528
|
}
|
|
529
529
|
|
|
530
530
|
/** Fired after user submits prompt but before agent loop. */
|
|
@@ -538,7 +538,10 @@ export class RetryHandler {
|
|
|
538
538
|
if (!currentModel) return false;
|
|
539
539
|
|
|
540
540
|
// Only attempt claude-code fallback when the current provider is anthropic.
|
|
541
|
-
//
|
|
541
|
+
// Transport-specific (ADR-012): intentionally keys on provider, not api —
|
|
542
|
+
// the fallback specifically reroutes the plain `anthropic` transport to
|
|
543
|
+
// the `claude-code` transport. Other Anthropic-fronting transports
|
|
544
|
+
// (anthropic-vertex, amazon-bedrock) must not be rerouted.
|
|
542
545
|
if (currentModel.provider !== "anthropic") return false;
|
|
543
546
|
|
|
544
547
|
// Find the same model ID under the claude-code provider
|