/** * Human-in-the-loop interrupt system for agent workflows. * * Interrupt Flow: * 1. Hook or tool calls `event.interrupt()` or `context.interrupt()` * 2. If resuming (response exists), the response is returned * 3. Otherwise, agent execution halts with `stopReason: 'interrupt'` * 4. User resumes by invoking agent with `interruptResponse` content blocks * 5. On resume, `interrupt()` returns the user's response */ import { InterruptResponseContent, type InterruptResponseContentData, type InterruptParams } from './types/interrupt.js'; import type { JSONValue } from './types/json.js'; import type { LocalAgent } from './types/agent.js'; import { Message, ToolResultBlock, type MessageData, type ToolResultBlockData } from './types/messages.js'; /** * Origin of an interrupt: * - `'tool'` — raised by a tool callback via `ToolContext.interrupt()`. * - `'hook'` — raised by an agent-level hook (e.g. `BeforeToolCallEvent.interrupt()`). * - `'multiagent-hook'` — raised by a multi-agent hook (e.g. `BeforeNodeCallEvent.interrupt()`). */ export type InterruptSource = 'tool' | 'hook' | 'middleware' | 'multiagent-hook'; /** * Represents an interrupt that can pause agent execution for human-in-the-loop workflows. */ export declare class Interrupt { /** * Unique identifier for this interrupt. */ readonly id: string; /** * User-defined name for the interrupt. */ readonly name: string; /** * User-provided reason for raising the interrupt. */ readonly reason?: JSONValue; /** * Human response provided when resuming the agent after an interrupt. */ response?: JSONValue; /** * Where this interrupt was raised from — a tool callback, an agent-level hook, or * a multi-agent orchestrator hook. Always populated. When deserializing a snapshot * produced by an older SDK that did not record this field, defaults to `'hook'`. */ readonly source: InterruptSource; constructor(data: { id: string; name: string; reason?: JSONValue; response?: JSONValue; source?: InterruptSource; }); /** * Serializes the interrupt to a JSON-compatible object. */ toJSON(): { id: string; name: string; reason?: JSONValue; response?: JSONValue; source: InterruptSource; }; /** * Creates an Interrupt instance from a JSON object. * * @param data - JSON data to deserialize * @returns Interrupt instance */ static fromJSON(data: { id: string; name: string; reason?: JSONValue; response?: JSONValue; source?: InterruptSource; }): Interrupt; } /** * Error thrown when human input is required to continue agent execution. * Caught by the agent loop to trigger an interrupt stop. */ export declare class InterruptError extends Error { /** * The interrupts that caused this error. */ readonly interrupts: Interrupt[]; constructor(interrupt: Interrupt | Interrupt[]); } /** * Data format for serialized interrupt state. */ export interface InterruptStateData { /** * Map of interrupt IDs to interrupt data. */ interrupts: Record; /** * Resume responses that were provided when resuming from an interrupt. */ resumeResponses?: InterruptResponseContentData[] | undefined; /** * Whether the agent is in an interrupted state. */ activated: boolean; /** * Pending tool execution state for resume after interrupt. */ pendingToolExecution?: PendingToolExecution | undefined; } /** * Pending tool execution state stored when an interrupt occurs mid-execution. * Contains all data needed to resume tool execution without re-calling the model. */ export interface PendingToolExecution { /** * The assistant message containing tool use blocks, serialized as MessageData. */ assistantMessageData: MessageData; /** * Tool results that were completed before the interrupt. * Maps toolUseId to serialized ToolResultBlock data. */ completedToolResults: Record; } /** * Tracks the state of interrupt events raised during agent execution. * * Interrupt state is cleared after resuming. */ export declare class InterruptState implements InterruptStateData { /** Record of interrupt IDs to Interrupt instances. */ interrupts: Record; /** Resume responses provided when resuming from an interrupt. */ resumeResponses?: InterruptResponseContent[] | undefined; /** Whether the agent is in an interrupted state. */ activated: boolean; /** Pending tool execution state for resume. */ pendingToolExecution?: PendingToolExecution | undefined; constructor(); /** * Gets the pending tool execution state with reconstructed Message and ToolResultBlock objects. * Returns undefined if there is no pending execution. */ getPendingExecution(): { assistantMessage: Message; completedToolResults: Map; } | undefined; /** * Sets the pending tool execution state. */ setPendingToolExecution(pending: PendingToolExecution): void; /** * Clears the pending tool execution state. */ clearPendingToolExecution(): void; /** * Returns the list of interrupts as an array. */ getInterruptsList(): Interrupt[]; /** * Returns all interrupts that have no response (i.e., were raised but not yet answered). */ getUnansweredInterrupts(): Interrupt[]; /** * Returns the first interrupt that has no response (i.e., was raised but not yet answered). */ getUnansweredInterrupt(): Interrupt | undefined; /** * Activates the interrupt state. */ activate(): void; /** * Deactivates the interrupt state and clears all interrupts and context. */ deactivate(): void; /** * Configures the interrupt state for resuming from an interrupt. * Populates interrupt responses from the provided content blocks. * * @param responses - Array of interrupt response content blocks * @throws Error if an interrupt ID is not found */ resume(responses: InterruptResponseContent[]): void; /** * Gets or creates an interrupt with the given ID. * If the interrupt already exists, returns it (potentially with a response). * If a preemptive response is provided and the interrupt is new, the response * is stored on the interrupt so it returns immediately without halting execution. * * @param id - Unique identifier for the interrupt * @param name - User-defined name for the interrupt * @param reason - Optional reason for the interrupt * @param response - Optional preemptive response to skip the interrupt * @param source - Where the interrupt was raised from (tool or hook callback) * @returns The interrupt (may have a response if resuming or preemptive) */ getOrCreateInterrupt(id: string, name: string, reason?: JSONValue, response?: JSONValue, source?: InterruptSource): Interrupt; /** * Register an existing Interrupt instance in the state, or return the already-registered one. */ registerInterrupt(interrupt: Interrupt): Interrupt; /** * Serializes the interrupt state to a JSON-compatible object. */ toJSON(): InterruptStateData; /** * Creates an InterruptState instance from a JSON object. * * @param data - JSON data to deserialize * @returns InterruptState instance */ static fromJSON(data: InterruptStateData): InterruptState; } /** * Interface for objects that support human-in-the-loop interrupts. * Implemented by hook events and tool contexts that can pause agent execution. */ export interface Interruptible { interrupt(params: InterruptParams): T; } /** * Shared interrupt logic that accesses the agent's interrupt state to register or resume an interrupt. * * @param agent - The agent whose interrupt state to access * @param interruptId - Unique identifier for this interrupt instance * @param params - Interrupt parameters including name and optional reason * @param source - Where the interrupt was raised from (tool callback vs hook callback) * @returns The user's response when resuming from an interrupt * @throws InterruptError when no response is available (first invocation) * * @internal */ export declare function interruptFromAgent(agent: LocalAgent, interruptId: string, params: InterruptParams, source: InterruptSource): T; /** * Interrupt-or-resume helper for multi-agent hooks where interrupts live on a per-node * `Interrupt[]` list rather than on an agent's `InterruptState`. Mirrors the * {@link interruptFromAgent} contract: returns the response if the interrupt already * has one (resume path), otherwise records a new interrupt and throws `InterruptError`. * * @internal */ export declare function interruptFromMultiAgentNode(interrupts: Interrupt[], interruptId: string, params: InterruptParams, source: InterruptSource): T; //# sourceMappingURL=interrupt.d.ts.map