/** * Session and tool call/result event types for the extension system. */ import type { ImageContent, TextContent, VideoContent } from "../../../ai/index.js"; import type { CompactionPreparation } from "../compaction/index.js"; import type { BranchSummaryEntry, CompactionEntry, SessionEntry } from "../session-manager.js"; import type { BashToolDetails, BashToolInput, ReadToolDetails, ReadToolInput } from "../tools/index.js"; /** Fired when a session is started, loaded, or reloaded */ export interface SessionStartEvent { type: "session_start"; /** Why this session start happened. */ reason: "startup" | "reload" | "new" | "resume" | "fork"; /** Previously active session file. Present for "new", "resume", and "fork". */ previousSessionFile?: string; } /** Fired before switching to another session (can be cancelled) */ export interface SessionBeforeSwitchEvent { type: "session_before_switch"; reason: "new" | "resume"; targetSessionFile?: string; } /** Fired before forking a session (can be cancelled) */ export interface SessionBeforeForkEvent { type: "session_before_fork"; entryId: string; position: "before" | "at"; } /** Fired before context compaction (can be cancelled or customized) */ export interface SessionBeforeCompactEvent { type: "session_before_compact"; preparation: CompactionPreparation; branchEntries: SessionEntry[]; customInstructions?: string; signal: AbortSignal; /** * Emit live compaction progress to the host UI. Extensions that provide a * custom compaction result should call this while building it so the browser * does not appear idle until the final compaction object is returned. */ emitProgress?: (delta: string, content?: string) => void; } /** Fired after context compaction */ export interface SessionCompactEvent { type: "session_compact"; compactionEntry: CompactionEntry; fromExtension: boolean; } /** Fired before an extension runtime is torn down due to quit, reload, or session replacement. */ export interface SessionShutdownEvent { type: "session_shutdown"; reason: "quit" | "reload" | "new" | "resume" | "fork"; /** Destination session file when shutting down due to session replacement. */ targetSessionFile?: string; } /** Preparation data for tree navigation */ export interface TreePreparation { targetId: string; oldLeafId: string | null; commonAncestorId: string | null; entriesToSummarize: SessionEntry[]; userWantsSummary: boolean; /** Custom instructions for summarization */ customInstructions?: string; /** If true, customInstructions replaces the default prompt instead of being appended */ replaceInstructions?: boolean; /** Label to attach to the branch summary entry */ label?: string; } /** Fired before navigating in the session tree (can be cancelled) */ export interface SessionBeforeTreeEvent { type: "session_before_tree"; preparation: TreePreparation; signal: AbortSignal; } /** Fired after navigating in the session tree */ export interface SessionTreeEvent { type: "session_tree"; newLeafId: string | null; oldLeafId: string | null; summaryEntry?: BranchSummaryEntry; fromExtension?: boolean; } export type SessionEvent = SessionStartEvent | SessionBeforeSwitchEvent | SessionBeforeForkEvent | SessionBeforeCompactEvent | SessionCompactEvent | SessionShutdownEvent | SessionBeforeTreeEvent | SessionTreeEvent; interface ToolCallEventBase { type: "tool_call"; toolCallId: string; } export interface BashToolCallEvent extends ToolCallEventBase { toolName: "bash"; input: BashToolInput; } export interface ReadToolCallEvent extends ToolCallEventBase { toolName: "read"; input: ReadToolInput; } export interface CustomToolCallEvent extends ToolCallEventBase { toolName: string; input: Record; } /** * Fired before a tool executes. Can block. * * `event.input` is mutable. Mutate it in place to patch tool arguments before execution. * Later `tool_call` handlers see earlier mutations. No re-validation is performed after mutation. */ export type ToolCallEvent = BashToolCallEvent | ReadToolCallEvent | CustomToolCallEvent; interface ToolResultEventBase { type: "tool_result"; toolCallId: string; input: Record; content: (TextContent | ImageContent | VideoContent)[]; isError: boolean; } export interface BashToolResultEvent extends ToolResultEventBase { toolName: "bash"; details: BashToolDetails | undefined; } export interface ReadToolResultEvent extends ToolResultEventBase { toolName: "read"; details: ReadToolDetails | undefined; } export interface CustomToolResultEvent extends ToolResultEventBase { toolName: string; details: unknown; } /** Fired after a tool executes. Can modify result. */ export type ToolResultEvent = BashToolResultEvent | ReadToolResultEvent | CustomToolResultEvent; export declare function isBashToolResult(e: ToolResultEvent): e is BashToolResultEvent; export declare function isReadToolResult(e: ToolResultEvent): e is ReadToolResultEvent; /** * Type guard for narrowing ToolCallEvent by tool name. * * Built-in tools narrow automatically (no type params needed): * ```ts * if (isToolCallEventType("bash", event)) { * event.input.command; // string * } * ``` * * Custom tools require explicit type parameters: * ```ts * if (isToolCallEventType<"my_tool", MyToolInput>("my_tool", event)) { * event.input.action; // typed * } * ``` * * Note: Direct narrowing via `event.toolName === "bash"` doesn't work because * CustomToolCallEvent.toolName is `string` which overlaps with all literals. */ export declare function isToolCallEventType(toolName: "bash", event: ToolCallEvent): event is BashToolCallEvent; export declare function isToolCallEventType(toolName: "read", event: ToolCallEvent): event is ReadToolCallEvent; export declare function isToolCallEventType>(toolName: TName, event: ToolCallEvent): event is ToolCallEvent & { toolName: TName; input: TInput; }; export {}; //# sourceMappingURL=session-events.d.ts.map