@tanstack/ai 0.46.0 → 0.47.2
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/esm/activities/chat/index.d.ts +36 -11
- package/dist/esm/activities/chat/index.js +440 -79
- package/dist/esm/activities/chat/index.js.map +1 -1
- package/dist/esm/activities/chat/messages.d.ts +1 -0
- package/dist/esm/activities/chat/messages.js +12 -7
- package/dist/esm/activities/chat/messages.js.map +1 -1
- package/dist/esm/activities/chat/middleware/builder.d.ts +7 -2
- package/dist/esm/activities/chat/middleware/builder.js.map +1 -1
- package/dist/esm/activities/chat/middleware/compose.d.ts +10 -3
- package/dist/esm/activities/chat/middleware/compose.js +55 -0
- package/dist/esm/activities/chat/middleware/compose.js.map +1 -1
- package/dist/esm/activities/chat/middleware/define.d.ts +6 -3
- package/dist/esm/activities/chat/middleware/define.js.map +1 -1
- package/dist/esm/activities/chat/middleware/generic-interrupts.d.ts +13 -0
- package/dist/esm/activities/chat/middleware/generic-interrupts.js +8 -0
- package/dist/esm/activities/chat/middleware/generic-interrupts.js.map +1 -0
- package/dist/esm/activities/chat/middleware/index.d.ts +4 -1
- package/dist/esm/activities/chat/middleware/types.d.ts +54 -3
- package/dist/esm/activities/chat/middleware/types.js +16 -0
- package/dist/esm/activities/chat/middleware/types.js.map +1 -0
- package/dist/esm/activities/chat/stream/processor.js +18 -5
- package/dist/esm/activities/chat/stream/processor.js.map +1 -1
- package/dist/esm/adapter-internals.d.ts +6 -0
- package/dist/esm/adapter-internals.js +4 -1
- package/dist/esm/client.d.ts +4 -0
- package/dist/esm/client.js +3 -1
- package/dist/esm/client.js.map +1 -1
- package/dist/esm/generic-interrupt-continuation.d.ts +45 -0
- package/dist/esm/generic-interrupt-continuation.js +80 -0
- package/dist/esm/generic-interrupt-continuation.js.map +1 -0
- package/dist/esm/index.d.ts +6 -1
- package/dist/esm/index.js +4 -1
- package/dist/esm/interrupt-definition.d.ts +113 -0
- package/dist/esm/interrupt-definition.js +169 -0
- package/dist/esm/interrupt-definition.js.map +1 -0
- package/dist/esm/interrupt-resume.d.ts +3 -0
- package/dist/esm/interrupt-resume.js +77 -16
- package/dist/esm/interrupt-resume.js.map +1 -1
- package/dist/esm/interrupts.d.ts +12 -3
- package/dist/esm/interrupts.js.map +1 -1
- package/dist/esm/types.d.ts +11 -3
- package/dist/esm/utilities/chat-params.js +10 -1
- package/dist/esm/utilities/chat-params.js.map +1 -1
- package/package.json +3 -3
- package/skills/ai-core/media-generation/SKILL.md +4 -1
- package/skills/ai-core/middleware/SKILL.md +53 -44
- package/skills/ai-core/structured-outputs/SKILL.md +59 -55
- package/skills/ai-core/tool-calling/SKILL.md +54 -1
- package/src/activities/chat/index.ts +1030 -211
- package/src/activities/chat/messages.ts +11 -3
- package/src/activities/chat/middleware/builder.ts +29 -4
- package/src/activities/chat/middleware/compose.ts +95 -5
- package/src/activities/chat/middleware/define.ts +13 -3
- package/src/activities/chat/middleware/generic-interrupts.ts +26 -0
- package/src/activities/chat/middleware/index.ts +15 -0
- package/src/activities/chat/middleware/types.ts +127 -2
- package/src/activities/chat/stream/processor.ts +21 -0
- package/src/adapter-internals.ts +20 -0
- package/src/client.ts +20 -0
- package/src/generic-interrupt-continuation.ts +162 -0
- package/src/index.ts +34 -0
- package/src/interrupt-definition.ts +581 -0
- package/src/interrupt-resume.ts +156 -25
- package/src/interrupts.ts +13 -3
- package/src/types.ts +11 -3
- package/src/utilities/chat-params.ts +16 -3
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { InterruptDefinition } from '../../../interrupt-definition.js';
|
|
2
|
+
/**
|
|
3
|
+
* Internal per-run registry of first-party generic interrupt definitions.
|
|
4
|
+
*
|
|
5
|
+
* Persistence uses this bridge while restoring a durable interrupt. It never
|
|
6
|
+
* receives a definition from the stored record; it can only look up one that
|
|
7
|
+
* the current chat call registered.
|
|
8
|
+
*/
|
|
9
|
+
export interface GenericInterruptDefinitionRegistry {
|
|
10
|
+
readonly definitions: ReadonlyMap<string, InterruptDefinition<any, any, any, any>>;
|
|
11
|
+
}
|
|
12
|
+
export declare const GenericInterruptDefinitionRegistryCapability: import('./capabilities.js').Capability<GenericInterruptDefinitionRegistry, "generic-interrupt-definition-registry">;
|
|
13
|
+
export declare const getGenericInterruptDefinitionRegistry: import('./capabilities.js').CapabilityGetter<GenericInterruptDefinitionRegistry>, provideGenericInterruptDefinitionRegistry: import('./capabilities.js').CapabilityProvider<GenericInterruptDefinitionRegistry>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { createCapability } from "./capabilities.js";
|
|
2
|
+
//#region src/activities/chat/middleware/generic-interrupts.ts
|
|
3
|
+
var GenericInterruptDefinitionRegistryCapability = createCapability()("generic-interrupt-definition-registry");
|
|
4
|
+
var [getGenericInterruptDefinitionRegistry, provideGenericInterruptDefinitionRegistry] = GenericInterruptDefinitionRegistryCapability;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { GenericInterruptDefinitionRegistryCapability, getGenericInterruptDefinitionRegistry, provideGenericInterruptDefinitionRegistry };
|
|
7
|
+
|
|
8
|
+
//# sourceMappingURL=generic-interrupts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generic-interrupts.js","names":[],"sources":["../../../../../src/activities/chat/middleware/generic-interrupts.ts"],"sourcesContent":["import { createCapability } from './capabilities'\nimport type { InterruptDefinition } from '../../../interrupt-definition'\n\n/**\n * Internal per-run registry of first-party generic interrupt definitions.\n *\n * Persistence uses this bridge while restoring a durable interrupt. It never\n * receives a definition from the stored record; it can only look up one that\n * the current chat call registered.\n */\nexport interface GenericInterruptDefinitionRegistry {\n readonly definitions: ReadonlyMap<\n string,\n InterruptDefinition<any, any, any, any>\n >\n}\n\nexport const GenericInterruptDefinitionRegistryCapability =\n createCapability<GenericInterruptDefinitionRegistry>()(\n 'generic-interrupt-definition-registry',\n )\n\nexport const [\n getGenericInterruptDefinitionRegistry,\n provideGenericInterruptDefinitionRegistry,\n] = GenericInterruptDefinitionRegistryCapability\n"],"mappings":";;AAiBA,IAAa,+CACX,iBAAqD,CAAC,CACpD,uCACF;AAEF,IAAa,CACX,uCACA,6CACE"}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
export type { ChatMiddleware, ChatMiddlewareContext, ChatMiddlewarePhase, ChatMiddlewareConfig, ChatResumeToolState, ChatResumeGenericResolution, StructuredOutputMiddlewareConfig, ToolCallHookContext, BeforeToolCallDecision, AfterToolCallInfo, IterationInfo, ToolPhaseCompleteInfo, UsageInfo, FinishInfo, AbortInfo, ErrorInfo, SandboxFileEvent, SandboxFileHookEvent, ChatSandboxHooks, } from './types.js';
|
|
1
|
+
export type { ChatMiddleware, ChatMiddlewareContext, ChatMiddlewarePhase, ChatMiddlewareConfig, ChatResumeToolState, ChatResumeGenericResolution, StructuredOutputMiddlewareConfig, ToolCallHookContext, BeforeToolCallDecision, AfterToolCallInfo, IterationInfo, ToolPhaseCompleteInfo, UsageInfo, FinishInfo, AbortInfo, ErrorInfo, SandboxFileEvent, SandboxFileHookEvent, ChatSandboxHooks, InterruptBoundaryPhase, InterruptToolResume, InterruptResolutionCollection, GenericInterruptResolution, InterruptBoundaryResult, InterruptResolutionResult, } from './types.js';
|
|
2
|
+
export { INTERRUPT_BOUNDARY_PHASES, INTERRUPT_TOOL_RESUMES } from './types.js';
|
|
3
|
+
export { GenericInterruptDefinitionRegistryCapability, getGenericInterruptDefinitionRegistry, provideGenericInterruptDefinitionRegistry, } from './generic-interrupts.js';
|
|
4
|
+
export type { GenericInterruptDefinitionRegistry } from './generic-interrupts.js';
|
|
2
5
|
export { MiddlewareRunner } from './compose.js';
|
|
3
6
|
export { createCapability, CapabilityRegistry } from './capabilities.js';
|
|
4
7
|
export type { Capability, CapabilityHandle, CapabilityContext, CapabilityGetter, CapabilityProvider, CapabilityGetOptions, } from './capabilities.js';
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { StandardJSONSchemaV1, StandardSchemaV1 } from '@standard-schema/spec';
|
|
1
2
|
import { AgentLoopState, JSONSchema, ModelMessage, RunAgentResumeItem, StreamChunk, TokenUsage, Tool, ToolCall } from '../../../types.js';
|
|
2
3
|
import { SystemPrompt } from '../../../system-prompts.js';
|
|
3
4
|
import { ToolApprovalResolution } from '../../../interrupts.js';
|
|
5
|
+
import { GenericInterruptRequest, InterruptDefinition } from '../../../interrupt-definition.js';
|
|
4
6
|
import { Capability, CapabilityHandle, CapabilityRegistry } from './capabilities.js';
|
|
5
7
|
/** A file change observed inside a sandbox during a chat run. */
|
|
6
8
|
export interface SandboxFileEvent {
|
|
@@ -35,13 +37,46 @@ export interface ChatSandboxHooks<TContext = unknown> {
|
|
|
35
37
|
* Phase of the chat middleware lifecycle.
|
|
36
38
|
* - 'init': Initial config transform before the chat engine starts
|
|
37
39
|
* - 'beforeModel': Before each adapter chatStream call (per agent iteration)
|
|
40
|
+
* - 'afterModel': After each adapter chatStream call (per agent iteration)
|
|
38
41
|
* - 'modelStream': During model streaming
|
|
39
42
|
* - 'beforeTools': Before tool execution phase
|
|
40
43
|
* - 'afterTools': After tool execution phase
|
|
41
44
|
* - 'structuredOutput': During the final structured-output adapter call (set
|
|
42
45
|
* for chunks from adapter.structuredOutputStream or the synthesized fallback)
|
|
43
46
|
*/
|
|
44
|
-
export type ChatMiddlewarePhase = 'init' | 'beforeModel' | 'modelStream' | 'beforeTools' | 'afterTools' | 'structuredOutput';
|
|
47
|
+
export type ChatMiddlewarePhase = 'init' | 'beforeModel' | 'afterModel' | 'modelStream' | 'beforeTools' | 'afterTools' | 'structuredOutput';
|
|
48
|
+
export declare const INTERRUPT_BOUNDARY_PHASES: readonly ["beforeModel", "afterModel", "beforeTools", "afterTools"];
|
|
49
|
+
export type InterruptBoundaryPhase = (typeof INTERRUPT_BOUNDARY_PHASES)[number];
|
|
50
|
+
export declare const INTERRUPT_TOOL_RESUMES: readonly ["continue", "cancel", "stop"];
|
|
51
|
+
export type InterruptToolResume = (typeof INTERRUPT_TOOL_RESUMES)[number];
|
|
52
|
+
type AnyInterruptDefinition = InterruptDefinition<any, any, any, any>;
|
|
53
|
+
type InterruptResponse<TDefinition> = TDefinition extends InterruptDefinition<any, any, infer TResponseSchema, any> ? TResponseSchema extends StandardSchemaV1<any, infer TResponse> ? TResponse : TResponseSchema extends StandardJSONSchemaV1<any, infer TResponse> ? TResponse : unknown : unknown;
|
|
54
|
+
export type GenericInterruptResolution<TDefinition extends AnyInterruptDefinition> = TDefinition extends AnyInterruptDefinition ? {
|
|
55
|
+
readonly request: GenericInterruptRequest<TDefinition>;
|
|
56
|
+
readonly status: 'resolved';
|
|
57
|
+
readonly response: InterruptResponse<TDefinition>;
|
|
58
|
+
} | {
|
|
59
|
+
readonly request: GenericInterruptRequest<TDefinition>;
|
|
60
|
+
readonly status: 'cancelled';
|
|
61
|
+
readonly response?: never;
|
|
62
|
+
} : never;
|
|
63
|
+
export interface InterruptResolutionCollection<TDefinitions extends AnyInterruptDefinition = AnyInterruptDefinition> {
|
|
64
|
+
for: <TDefinition extends ([TDefinitions] extends [never] ? AnyInterruptDefinition : TDefinitions)>(definition: TDefinition) => ReadonlyArray<GenericInterruptResolution<TDefinition>>;
|
|
65
|
+
all: {
|
|
66
|
+
(): ReadonlyArray<GenericInterruptResolution<TDefinitions>>;
|
|
67
|
+
<const TSelected extends ReadonlyArray<TDefinitions>>(...definitions: TSelected): ReadonlyArray<GenericInterruptResolution<TSelected[number]>>;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
type BivariantInterruptResolutionHook<TContext, TDefinitions extends AnyInterruptDefinition> = InterruptResolutionHookSignature<TContext, TDefinitions>['call'];
|
|
71
|
+
declare abstract class InterruptResolutionHookSignature<TContext, TDefinitions extends AnyInterruptDefinition> {
|
|
72
|
+
abstract call(ctx: ChatMiddlewareContext<TContext>, resolutions: InterruptResolutionCollection<TDefinitions>): InterruptResolutionResult | Promise<InterruptResolutionResult>;
|
|
73
|
+
}
|
|
74
|
+
export type InterruptBoundaryResult<TDefinitions extends AnyInterruptDefinition = AnyInterruptDefinition> = undefined | {
|
|
75
|
+
readonly interrupts: ReadonlyArray<GenericInterruptRequest<TDefinitions>>;
|
|
76
|
+
};
|
|
77
|
+
export type InterruptResolutionResult = void | {
|
|
78
|
+
readonly toolResume: InterruptToolResume;
|
|
79
|
+
};
|
|
45
80
|
/**
|
|
46
81
|
* Stable context object passed to all middleware hooks.
|
|
47
82
|
* Created once per chat() invocation and shared across all hooks.
|
|
@@ -167,6 +202,8 @@ export interface ChatResumeToolState {
|
|
|
167
202
|
approvals?: ReadonlyMap<string, ToolApprovalResolution> | undefined;
|
|
168
203
|
clientToolResults?: ReadonlyMap<string, unknown> | undefined;
|
|
169
204
|
genericInterrupts?: ReadonlyMap<string, ChatResumeGenericResolution> | undefined;
|
|
205
|
+
/** Durable generic requests reconstructed by server middleware. */
|
|
206
|
+
genericInterruptRequests?: ReadonlyMap<string, GenericInterruptRequest<InterruptDefinition<any, any, any, any>>> | undefined;
|
|
170
207
|
deniedToolResults?: ReadonlyMap<string, unknown> | undefined;
|
|
171
208
|
cancelledToolCallIds?: ReadonlySet<string> | undefined;
|
|
172
209
|
}
|
|
@@ -368,9 +405,21 @@ export interface ErrorInfo {
|
|
|
368
405
|
* }
|
|
369
406
|
* ```
|
|
370
407
|
*/
|
|
371
|
-
export interface ChatMiddleware<TContext = unknown> {
|
|
408
|
+
export interface ChatMiddleware<TContext = unknown, TInterruptDefinitions extends AnyInterruptDefinition = never> {
|
|
372
409
|
/** Optional name for debugging and identification */
|
|
373
410
|
name?: string;
|
|
411
|
+
/**
|
|
412
|
+
* Called at a lifecycle boundary. Return interrupt requests to pause the run.
|
|
413
|
+
* Requests from every middleware in the same boundary form one batch.
|
|
414
|
+
*/
|
|
415
|
+
onInterruptBoundary?: (ctx: ChatMiddlewareContext<TContext> & {
|
|
416
|
+
phase: InterruptBoundaryPhase;
|
|
417
|
+
}) => InterruptBoundaryResult<TInterruptDefinitions> | Promise<InterruptBoundaryResult<TInterruptDefinitions>>;
|
|
418
|
+
/**
|
|
419
|
+
* Called on a continuation run after the client answers registered interrupts.
|
|
420
|
+
* Return `toolResume` to decide whether pending tools continue, cancel, or stop.
|
|
421
|
+
*/
|
|
422
|
+
onInterruptResolution?: BivariantInterruptResolutionHook<TContext, TInterruptDefinitions>;
|
|
374
423
|
/**
|
|
375
424
|
* Capabilities this middleware requires. `chat()` validates that some
|
|
376
425
|
* middleware (or the adapter) provides each one; unsatisfied requirements are
|
|
@@ -493,4 +542,6 @@ export interface ChatMiddleware<TContext = unknown> {
|
|
|
493
542
|
sandbox?: ChatSandboxHooks<TContext>;
|
|
494
543
|
}
|
|
495
544
|
/** A `ChatMiddleware` with a permissive context — for use as a constraint. */
|
|
496
|
-
|
|
545
|
+
/** A permissive middleware constraint that retains the definition parameter. */
|
|
546
|
+
export type AnyChatMiddleware = ChatMiddleware<any, any>;
|
|
547
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/activities/chat/middleware/types.ts
|
|
2
|
+
var INTERRUPT_BOUNDARY_PHASES = [
|
|
3
|
+
"beforeModel",
|
|
4
|
+
"afterModel",
|
|
5
|
+
"beforeTools",
|
|
6
|
+
"afterTools"
|
|
7
|
+
];
|
|
8
|
+
var INTERRUPT_TOOL_RESUMES = [
|
|
9
|
+
"continue",
|
|
10
|
+
"cancel",
|
|
11
|
+
"stop"
|
|
12
|
+
];
|
|
13
|
+
//#endregion
|
|
14
|
+
export { INTERRUPT_BOUNDARY_PHASES, INTERRUPT_TOOL_RESUMES };
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","names":[],"sources":["../../../../../src/activities/chat/middleware/types.ts"],"sourcesContent":["import type {\n StandardJSONSchemaV1,\n StandardSchemaV1,\n} from '@standard-schema/spec'\nimport type {\n AgentLoopState,\n JSONSchema,\n ModelMessage,\n RunAgentResumeItem,\n StreamChunk,\n TokenUsage,\n Tool,\n ToolCall,\n} from '../../../types'\nimport type { SystemPrompt } from '../../../system-prompts'\nimport type { ToolApprovalResolution } from '../../../interrupts'\nimport type {\n GenericInterruptRequest,\n InterruptDefinition,\n} from '../../../interrupt-definition'\nimport type {\n Capability,\n CapabilityHandle,\n CapabilityRegistry,\n} from './capabilities'\n\n/** A file change observed inside a sandbox during a chat run. */\nexport interface SandboxFileEvent {\n type: 'create' | 'change' | 'delete'\n /** Absolute path inside the sandbox (under the workspace root). */\n path: string\n timestamp: number\n}\n\n/** The file event a sandbox hook receives: the serializable {@link SandboxFileEvent}\n * plus lazy, git-backed content accessors. Accessors compute on call, so a hook\n * that only reads `path`/`type` pays nothing. Never present on the serialized\n * `sandbox.file` CUSTOM chunk. */\nexport interface SandboxFileHookEvent extends SandboxFileEvent {\n /** Content at the session baseline (`''` for a new file or non-git workspace). */\n before: () => Promise<string>\n /** Current content (`''` when the event is a delete). */\n after: () => Promise<string>\n /** Unified patch vs the session baseline (synthesized add-patch when non-git). */\n diff: () => Promise<string>\n}\n\n/**\n * Sandbox file-event hooks a chat middleware can declare. Fire server-side for\n * every file create/change/delete observed in the sandbox during the run.\n */\nexport interface ChatSandboxHooks<TContext = unknown> {\n onFile?: (\n ctx: ChatMiddlewareContext<TContext>,\n e: SandboxFileHookEvent,\n ) => void | Promise<void>\n onFileCreate?: (\n ctx: ChatMiddlewareContext<TContext>,\n e: SandboxFileHookEvent,\n ) => void | Promise<void>\n onFileChange?: (\n ctx: ChatMiddlewareContext<TContext>,\n e: SandboxFileHookEvent,\n ) => void | Promise<void>\n onFileDelete?: (\n ctx: ChatMiddlewareContext<TContext>,\n e: SandboxFileHookEvent,\n ) => void | Promise<void>\n}\n\n// ===========================\n// Middleware Context\n// ===========================\n\n/**\n * Phase of the chat middleware lifecycle.\n * - 'init': Initial config transform before the chat engine starts\n * - 'beforeModel': Before each adapter chatStream call (per agent iteration)\n * - 'afterModel': After each adapter chatStream call (per agent iteration)\n * - 'modelStream': During model streaming\n * - 'beforeTools': Before tool execution phase\n * - 'afterTools': After tool execution phase\n * - 'structuredOutput': During the final structured-output adapter call (set\n * for chunks from adapter.structuredOutputStream or the synthesized fallback)\n */\nexport type ChatMiddlewarePhase =\n | 'init'\n | 'beforeModel'\n | 'afterModel'\n | 'modelStream'\n | 'beforeTools'\n | 'afterTools'\n | 'structuredOutput'\n\nexport const INTERRUPT_BOUNDARY_PHASES = [\n 'beforeModel',\n 'afterModel',\n 'beforeTools',\n 'afterTools',\n] as const\n\nexport type InterruptBoundaryPhase = (typeof INTERRUPT_BOUNDARY_PHASES)[number]\n\nexport const INTERRUPT_TOOL_RESUMES = ['continue', 'cancel', 'stop'] as const\n\nexport type InterruptToolResume = (typeof INTERRUPT_TOOL_RESUMES)[number]\n\ntype AnyInterruptDefinition = InterruptDefinition<any, any, any, any>\n\ntype InterruptResponse<TDefinition> =\n TDefinition extends InterruptDefinition<any, any, infer TResponseSchema, any>\n ? TResponseSchema extends StandardSchemaV1<any, infer TResponse>\n ? TResponse\n : TResponseSchema extends StandardJSONSchemaV1<any, infer TResponse>\n ? TResponse\n : unknown\n : unknown\n\nexport type GenericInterruptResolution<\n TDefinition extends AnyInterruptDefinition,\n> = TDefinition extends AnyInterruptDefinition\n ?\n | {\n readonly request: GenericInterruptRequest<TDefinition>\n readonly status: 'resolved'\n readonly response: InterruptResponse<TDefinition>\n }\n | {\n readonly request: GenericInterruptRequest<TDefinition>\n readonly status: 'cancelled'\n readonly response?: never\n }\n : never\n\nexport interface InterruptResolutionCollection<\n TDefinitions extends AnyInterruptDefinition = AnyInterruptDefinition,\n> {\n for: <\n TDefinition extends ([TDefinitions] extends [never]\n ? AnyInterruptDefinition\n : TDefinitions),\n >(\n definition: TDefinition,\n ) => ReadonlyArray<GenericInterruptResolution<TDefinition>>\n all: {\n (): ReadonlyArray<GenericInterruptResolution<TDefinitions>>\n <const TSelected extends ReadonlyArray<TDefinitions>>(\n ...definitions: TSelected\n ): ReadonlyArray<GenericInterruptResolution<TSelected[number]>>\n }\n}\n\ntype BivariantInterruptResolutionHook<\n TContext,\n TDefinitions extends AnyInterruptDefinition,\n> = InterruptResolutionHookSignature<TContext, TDefinitions>['call']\n\ndeclare abstract class InterruptResolutionHookSignature<\n TContext,\n TDefinitions extends AnyInterruptDefinition,\n> {\n abstract call(\n ctx: ChatMiddlewareContext<TContext>,\n resolutions: InterruptResolutionCollection<TDefinitions>,\n ): InterruptResolutionResult | Promise<InterruptResolutionResult>\n}\n\nexport type InterruptBoundaryResult<\n TDefinitions extends AnyInterruptDefinition = AnyInterruptDefinition,\n> =\n | undefined\n | {\n readonly interrupts: ReadonlyArray<GenericInterruptRequest<TDefinitions>>\n }\n\nexport type InterruptResolutionResult = void | {\n readonly toolResume: InterruptToolResume\n}\n\n/**\n * Stable context object passed to all middleware hooks.\n * Created once per chat() invocation and shared across all hooks.\n */\nexport interface ChatMiddlewareContext<TContext = unknown> {\n /** Unique identifier for this chat request */\n requestId: string\n /** Unique identifier for this stream */\n streamId: string\n /** AG-UI run identifier for correlating client and server events */\n runId: string\n /** Interrupted or parent run correlated with this continuation. */\n parentRunId?: string\n /**\n * AG-UI thread identifier — a stable per-conversation ID used to\n * correlate client and server devtools events. Resolves to the\n * caller-provided `threadId` (or legacy `conversationId`), or an\n * auto-generated value when neither is supplied.\n */\n threadId: string\n /**\n * @deprecated Use `threadId` instead. Retained as an alias of\n * `threadId` so middleware written before the AG-UI rename keeps\n * working unchanged. Will be removed in a future major release.\n */\n conversationId?: string\n /** Current lifecycle phase */\n phase: ChatMiddlewarePhase\n /** Current agent loop iteration (0-indexed) */\n iteration: number\n /** Running count of chunks yielded so far */\n chunkIndex: number\n /** Abort signal from the chat request */\n signal?: AbortSignal\n /** Abort the chat run with a reason */\n abort: (reason?: string) => void\n /** Runtime context provided by chat() options */\n context: TContext\n /**\n * Defer a non-blocking side-effect promise.\n * Deferred promises do not block streaming and are awaited\n * after the terminal hook (onFinish/onAbort/onError).\n */\n defer: (promise: Promise<unknown>) => void\n\n // --- Provider / adapter info (immutable for the lifetime of the request) ---\n\n /**\n * Which activity this context describes — always `'chat'`. Present so the\n * chat context structurally satisfies the base `GenerationMiddlewareContext`,\n * letting an observe-only middleware authored against the base (e.g.\n * `otelMiddleware`) run on both chat and media activities.\n */\n activity: 'chat'\n /** Provider name (e.g., 'openai', 'anthropic') */\n provider: string\n /** Model identifier (e.g., 'gpt-5.5') */\n model: string\n /** Source of the chat invocation — always 'server' for server-side chat */\n source: 'client' | 'server'\n /** Whether the chat is streaming */\n streaming: boolean\n\n // --- Config-derived info (may update per-iteration via onConfig) ---\n\n /** System prompts configured for this chat */\n systemPrompts: Array<SystemPrompt>\n /** Names of configured tools, if any */\n toolNames?: Array<string>\n /** Flattened generation options (metadata) */\n options?: Record<string, unknown> | undefined\n /** Provider-specific model options */\n modelOptions?: Record<string, unknown> | undefined\n\n // --- Computed info ---\n\n /** Number of messages at the start of the request */\n messageCount: number\n /** Whether tools are configured */\n hasTools: boolean\n\n // --- Mutable per-iteration state ---\n\n /** Current assistant message ID (changes per iteration) */\n currentMessageId: string | null\n /** Accumulated text content for the current iteration */\n accumulatedContent: string\n\n // --- References ---\n\n /** Current messages array (read-only view) */\n messages: ReadonlyArray<ModelMessage>\n /** Generate a unique ID with the given prefix */\n createId: (prefix: string) => string\n /**\n * Capability bookkeeping for this request. Populated by middleware `setup`\n * hooks (via `provide` accessors) and read by later middleware (via `get`\n * accessors). Prefer the accessors returned by `createCapability` over using\n * this directly. Orthogonal to `context` (the user runtime context).\n */\n capabilities: CapabilityRegistry\n /**\n * Read a provided capability by its handle. Equivalent to the handle's own\n * `get` accessor (`getX(ctx)`); throws if the capability was never provided.\n */\n get: <TValue>(capability: Capability<TValue>) => TValue\n /**\n * Read a capability by its handle, returning `undefined` if it was never\n * provided (never throws).\n */\n getOptional: <TValue>(capability: Capability<TValue>) => TValue | undefined\n /**\n * Provide a capability value. Equivalent to the handle's own `provide`\n * accessor (`provideX(ctx, value)`). Typically called from `setup`.\n */\n provide: <TValue>(capability: Capability<TValue>, value: TValue) => void\n}\n\n// ===========================\n// Config passed to onConfig\n// ===========================\n\n/**\n * Chat configuration that middleware can observe or transform.\n * This is a subset of the chat engine's effective configuration\n * that middleware is allowed to modify.\n */\nexport interface ChatMiddlewareConfig {\n messages: Array<ModelMessage>\n systemPrompts: Array<SystemPrompt>\n tools: Array<Tool>\n resume?: Array<RunAgentResumeItem> | undefined\n resumeToolState?: ChatResumeToolState | undefined\n metadata?: Record<string, unknown> | undefined\n modelOptions?: Record<string, unknown> | undefined\n}\n\n/**\n * Tool decisions reconstructed by server-side middleware from validated resume\n * entries. This lets empty-message interrupt resumes continue tool execution\n * without relying on client message history.\n */\nexport interface ChatResumeToolState {\n approvals?: ReadonlyMap<string, ToolApprovalResolution> | undefined\n clientToolResults?: ReadonlyMap<string, unknown> | undefined\n genericInterrupts?:\n | ReadonlyMap<string, ChatResumeGenericResolution>\n | undefined\n /** Durable generic requests reconstructed by server middleware. */\n genericInterruptRequests?:\n | ReadonlyMap<\n string,\n GenericInterruptRequest<InterruptDefinition<any, any, any, any>>\n >\n | undefined\n deniedToolResults?: ReadonlyMap<string, unknown> | undefined\n cancelledToolCallIds?: ReadonlySet<string> | undefined\n}\n\nexport type ChatResumeGenericResolution =\n | { interruptId: string; status: 'resolved'; payload: unknown }\n | { interruptId: string; status: 'cancelled'; payload?: never }\n\n/**\n * Config passed to onStructuredOutputConfig.\n *\n * Mirrors ChatMiddlewareConfig minus `tools` (the final structured-output call\n * is a single typed-response request, not an agentic loop — tools cannot be\n * forwarded to it), plus the `outputSchema` being sent to the provider.\n * Middleware may transform the schema (e.g., inject $defs, strip\n * vendor-incompatible keywords) by returning a partial that includes\n * `outputSchema`.\n */\nexport interface StructuredOutputMiddlewareConfig extends Omit<\n ChatMiddlewareConfig,\n 'tools'\n> {\n /** JSON Schema being sent to the provider for structured output. */\n outputSchema: JSONSchema\n}\n\n// ===========================\n// Tool Call Hook Context\n// ===========================\n\n/**\n * Context provided to tool call hooks (onBeforeToolCall / onAfterToolCall).\n */\nexport interface ToolCallHookContext {\n /** The tool call being executed */\n toolCall: ToolCall\n /** The resolved tool definition, if found */\n tool: Tool | undefined\n /** Parsed arguments for the tool call */\n args: unknown\n /** Name of the tool */\n toolName: string\n /** ID of the tool call */\n toolCallId: string\n}\n\n/**\n * Decision returned from onBeforeToolCall.\n * - undefined/void: continue with normal execution\n * - { type: 'transformArgs', args }: replace args used for execution\n * - { type: 'skip', result }: skip execution, use provided result\n * - { type: 'abort', reason }: abort the entire chat run\n */\nexport type BeforeToolCallDecision =\n | void\n | undefined\n | null\n | { type: 'transformArgs'; args: unknown }\n | { type: 'skip'; result: unknown }\n | { type: 'abort'; reason?: string }\n\n/**\n * Outcome information provided to onAfterToolCall.\n */\nexport interface AfterToolCallInfo {\n /** The tool call that was executed */\n toolCall: ToolCall\n /** The resolved tool definition */\n tool: Tool | undefined\n /** Name of the tool */\n toolName: string\n /** ID of the tool call */\n toolCallId: string\n /** Whether the execution succeeded */\n ok: boolean\n /** Duration of tool execution in milliseconds */\n duration: number\n /** The result (if ok) or error (if not ok) */\n result?: unknown\n error?: unknown\n}\n\n// ===========================\n// Iteration Info\n// ===========================\n\n/**\n * Information passed to onIteration at the start of each agent loop iteration.\n */\nexport interface IterationInfo {\n /** 0-based iteration index */\n iteration: number\n /** The assistant message ID created for this iteration */\n messageId: string\n}\n\n// ===========================\n// Tool Phase Complete Info\n// ===========================\n\n/**\n * Aggregate information passed to onToolPhaseComplete after all tool calls\n * in an iteration have been processed.\n */\nexport interface ToolPhaseCompleteInfo {\n /** Tool calls that were assigned to the assistant message */\n toolCalls: Array<ToolCall>\n /** Completed tool results */\n results: Array<{\n toolCallId: string\n toolName: string\n result: unknown\n duration?: number\n }>\n /** Tools that need user approval */\n needsApproval: Array<{\n toolCallId: string\n toolName: string\n input: unknown\n approvalId: string\n }>\n /** Tools that need client-side execution */\n needsClientExecution: Array<{\n toolCallId: string\n toolName: string\n input: unknown\n }>\n}\n\n// ===========================\n// Usage Info\n// ===========================\n\n/**\n * Token usage statistics passed to the onUsage hook.\n * Extracted from the RUN_FINISHED chunk when usage data is present.\n *\n * Includes optional provider-reported `cost`/`costDetails` (see {@link TokenUsage}).\n * Kept as an interface extending `TokenUsage` to preserve declaration merging for\n * this publicly exported type.\n */\nexport interface UsageInfo extends TokenUsage {}\n\n// ===========================\n// Terminal Hook Info\n// ===========================\n\n/**\n * Information passed to onFinish.\n */\nexport interface FinishInfo {\n /** The finish reason from the last model response */\n finishReason: string | null\n /** Total duration of the chat run in milliseconds */\n duration: number\n /** Final accumulated text content */\n content: string\n /** Final usage totals, if available (optionally including provider-reported cost) */\n usage?: TokenUsage | undefined\n}\n\n/**\n * Information passed to onAbort.\n */\nexport interface AbortInfo {\n /** The reason for the abort, if provided */\n reason?: string\n /** Duration until abort in milliseconds */\n duration: number\n /**\n * True only when the abort came from an explicit, out-of-band cancel (e.g. a\n * cancel endpoint setting `RunRecord.cancelRequested`), never from a mere\n * client disconnect.\n *\n * A disconnect and a user pressing \"stop\" are the SAME connection close on\n * the wire, so consumers must not infer intent from an abort alone. Middleware\n * that tears down expensive resources reads this to distinguish \"the viewer\n * left, keep going\" from \"the user wants this stopped\". Populated from the\n * abort reason: `true` exactly when the run was aborted with `RUN_CANCEL_REASON`\n * (matched with `===`, so an arbitrary error message can never be read as a\n * deliberate cancel), `false` for every other abort. The durable channel is\n * separate — middleware that must also catch a cancel recorded on a different\n * host reads `RunRecord.cancelRequested` in addition to this flag.\n */\n cancelRequested?: boolean\n}\n\n/**\n * Information passed to onError.\n */\nexport interface ErrorInfo {\n /** The error that caused the failure */\n error: unknown\n /** Duration until error in milliseconds */\n duration: number\n}\n\n// ===========================\n// Middleware Interface\n// ===========================\n\n/**\n * Chat middleware interface.\n *\n * All hooks are optional. Middleware is composed in array order:\n * - `onConfig`: config piped through middlewares in order (first transform influences later)\n * - `onChunk`: each output chunk is fed into the next middleware in order\n *\n * @example Logging middleware\n * ```ts\n * const loggingMiddleware: ChatMiddleware = {\n * name: 'logging',\n * onStart(ctx) { console.log('Chat started', ctx.requestId) },\n * onChunk(ctx, chunk) { console.log('Chunk:', chunk.type) },\n * onFinish(ctx, info) { console.log('Done:', info.duration, 'ms') },\n * }\n * ```\n *\n * @example Redaction middleware\n * ```ts\n * const redactionMiddleware: ChatMiddleware = {\n * name: 'redaction',\n * onChunk(ctx, chunk) {\n * if (chunk.type === 'TEXT_MESSAGE_CONTENT') {\n * return { ...chunk, delta: redact(chunk.delta) }\n * }\n * },\n * }\n * ```\n */\nexport interface ChatMiddleware<\n TContext = unknown,\n TInterruptDefinitions extends AnyInterruptDefinition = never,\n> {\n /** Optional name for debugging and identification */\n name?: string\n\n /**\n * Called at a lifecycle boundary. Return interrupt requests to pause the run.\n * Requests from every middleware in the same boundary form one batch.\n */\n onInterruptBoundary?: (\n ctx: ChatMiddlewareContext<TContext> & { phase: InterruptBoundaryPhase },\n ) =>\n | InterruptBoundaryResult<TInterruptDefinitions>\n | Promise<InterruptBoundaryResult<TInterruptDefinitions>>\n\n /**\n * Called on a continuation run after the client answers registered interrupts.\n * Return `toolResume` to decide whether pending tools continue, cancel, or stop.\n */\n onInterruptResolution?: BivariantInterruptResolutionHook<\n TContext,\n TInterruptDefinitions\n >\n\n /**\n * Capabilities this middleware requires. `chat()` validates that some\n * middleware (or the adapter) provides each one; unsatisfied requirements are\n * a compile-time error (array coverage / builder) and a runtime error before\n * the adapter runs.\n */\n requires?: ReadonlyArray<CapabilityHandle>\n\n /**\n * Capabilities this middleware provides. Each declared capability MUST be\n * provided (via its `provide` accessor) inside `setup`, or `chat()` throws\n * after the setup phase.\n */\n provides?: ReadonlyArray<CapabilityHandle>\n\n /**\n * Capabilities this middleware uses if present but does not require.\n * Non-gating: never causes a validation error. Read with\n * `getX(ctx, { optional: true })`.\n */\n optionalRequires?: ReadonlyArray<CapabilityHandle>\n\n /**\n * Provisioning hook. Runs FIRST — before `onConfig` (init) — across all\n * middleware in array order. Use it to call `provide` accessors so later\n * middleware (`onConfig` onward) can consume the capabilities. Receives the\n * stable context; does NOT receive the mutable config.\n */\n setup?: (ctx: ChatMiddlewareContext<TContext>) => void | Promise<void>\n\n /**\n * Called to observe or transform the chat configuration.\n * Called at init and at the beginning of each agent iteration.\n *\n * Return a partial config to merge with the current config, or void to pass through.\n * Only the fields you return are overwritten — everything else is preserved.\n */\n onConfig?: (\n ctx: ChatMiddlewareContext<TContext>,\n config: ChatMiddlewareConfig,\n ) =>\n | void\n | null\n | Partial<ChatMiddlewareConfig>\n | Promise<void | null | Partial<ChatMiddlewareConfig>>\n\n /**\n * Called at the start of the final structured-output call (when the chat\n * was invoked with outputSchema). Pipes through middleware in order, like\n * onConfig, but with access to the JSON Schema being sent to the provider.\n *\n * Return a partial to shallow-merge into the current config, or void to\n * pass through.\n *\n * Fires BEFORE onConfig at the structured-output boundary. onConfig also\n * re-fires at the same boundary with ctx.phase === 'structuredOutput',\n * receiving the post-onStructuredOutputConfig view of the config (minus\n * outputSchema). Use onConfig for general-purpose transforms that apply\n * to every adapter call; use this hook when you need to transform the\n * outputSchema or apply structured-output-specific behavior.\n */\n onStructuredOutputConfig?: (\n ctx: ChatMiddlewareContext<TContext>,\n config: StructuredOutputMiddlewareConfig,\n ) =>\n | void\n | null\n | Partial<StructuredOutputMiddlewareConfig>\n | Promise<void | null | Partial<StructuredOutputMiddlewareConfig>>\n\n /**\n * Called when the chat run starts (after initial onConfig).\n */\n onStart?: (ctx: ChatMiddlewareContext<TContext>) => void | Promise<void>\n\n /**\n * Called at the start of each agent loop iteration, after a new assistant message ID\n * is created. Use this to observe iteration boundaries.\n */\n onIteration?: (\n ctx: ChatMiddlewareContext<TContext>,\n info: IterationInfo,\n ) => void | Promise<void>\n\n /**\n * Called when the engine is deciding whether to start another agent-loop\n * iteration (after a tool phase or between model turns).\n *\n * Return `false` to stop further iterations. Return `true`, `void`, or\n * `undefined` to allow continuation. Combined with AND semantics across\n * middleware and with `agentLoopStrategy` — any `false` stops the loop.\n *\n * Does not abort the run: the stream finishes normally with the current\n * messages. Use `ctx.abort()` only when you need a hard abort.\n *\n * Receives the same {@link AgentLoopState} passed to strategies\n * (`iterationCount`, `toolCallCount`, `lastTurnToolCallCount`, etc.).\n */\n onShouldContinue?: (\n ctx: ChatMiddlewareContext<TContext>,\n state: AgentLoopState,\n ) => boolean | void | Promise<boolean | void>\n\n /**\n * Called for every chunk yielded by chat().\n * Can observe, transform, expand, or drop chunks.\n *\n * @returns void (pass through), chunk (replace), chunk[] (expand), null (drop)\n */\n onChunk?: (\n ctx: ChatMiddlewareContext<TContext>,\n chunk: StreamChunk,\n ) =>\n | void\n | StreamChunk\n | Array<StreamChunk>\n | null\n | Promise<void | StreamChunk | Array<StreamChunk> | null>\n\n /**\n * Called before a tool is executed.\n * Can observe, transform args, skip execution, or abort the run.\n */\n onBeforeToolCall?: (\n ctx: ChatMiddlewareContext<TContext>,\n hookCtx: ToolCallHookContext,\n ) => BeforeToolCallDecision | Promise<BeforeToolCallDecision>\n\n /**\n * Called after a tool execution completes (success or failure).\n */\n onAfterToolCall?: (\n ctx: ChatMiddlewareContext<TContext>,\n info: AfterToolCallInfo,\n ) => void | Promise<void>\n\n /**\n * Called after all tool calls in an iteration have been processed.\n * Provides aggregate data about tool execution results, approvals, and client tools.\n */\n onToolPhaseComplete?: (\n ctx: ChatMiddlewareContext<TContext>,\n info: ToolPhaseCompleteInfo,\n ) => void | Promise<void>\n\n /**\n * Called when usage data is available from a RUN_FINISHED chunk.\n * Called once per model iteration that reports usage.\n */\n onUsage?: (\n ctx: ChatMiddlewareContext<TContext>,\n usage: UsageInfo,\n ) => void | Promise<void>\n\n /**\n * Called when the chat run completes normally.\n * Exactly one of onFinish/onAbort/onError will be called per run.\n */\n onFinish?: (\n ctx: ChatMiddlewareContext<TContext>,\n info: FinishInfo,\n ) => void | Promise<void>\n\n /**\n * Called when the chat run is aborted.\n * Exactly one of onFinish/onAbort/onError will be called per run.\n */\n onAbort?: (\n ctx: ChatMiddlewareContext<TContext>,\n info: AbortInfo,\n ) => void | Promise<void>\n\n /**\n * Called when the chat run encounters an unhandled error.\n * Exactly one of onFinish/onAbort/onError will be called per run.\n */\n onError?: (\n ctx: ChatMiddlewareContext<TContext>,\n info: ErrorInfo,\n ) => void | Promise<void>\n\n /**\n * Sandbox file-event hooks. Fire when a sandbox provided by `withSandbox` is\n * active during the run and a file is created/changed/deleted. Server-side.\n */\n sandbox?: ChatSandboxHooks<TContext>\n}\n\n/** A `ChatMiddleware` with a permissive context — for use as a constraint. */\n/** A permissive middleware constraint that retains the definition parameter. */\nexport type AnyChatMiddleware = ChatMiddleware<any, any>\n"],"mappings":";AA8FA,IAAa,4BAA4B;CACvC;CACA;CACA;CACA;AACF;AAIA,IAAa,yBAAyB;CAAC;CAAY;CAAU;AAAM"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { INTERRUPT_BINDING_METADATA_KEY } from "../../../interrupt-resume.js";
|
|
1
2
|
import { normalizeToolResult } from "../../../utilities/tool-result.js";
|
|
2
3
|
import { isProviderExecutedToolCall } from "../../../utilities/provider-executed.js";
|
|
3
4
|
import { aguiSnapshotMessageToUIMessage, generateMessageId, uiMessageToModelMessages } from "../messages.js";
|
|
@@ -25,6 +26,14 @@ import { ImmediateStrategy } from "./strategies.js";
|
|
|
25
26
|
* adapter contract, single-shot flows, and expected UIMessage output.
|
|
26
27
|
*/
|
|
27
28
|
var STRUCTURED_OUTPUT_UPDATE_BATCH_SIZE = 12;
|
|
29
|
+
function interruptBatchHasGeneric(interrupts) {
|
|
30
|
+
return interrupts.some((interrupt) => {
|
|
31
|
+
const metadata = interrupt.metadata;
|
|
32
|
+
if (!metadata || typeof metadata !== "object" || Array.isArray(metadata)) return false;
|
|
33
|
+
const binding = metadata[INTERRUPT_BINDING_METADATA_KEY];
|
|
34
|
+
return binding !== null && typeof binding === "object" && !Array.isArray(binding) && binding.kind === "generic";
|
|
35
|
+
});
|
|
36
|
+
}
|
|
28
37
|
/**
|
|
29
38
|
* StreamProcessor - State machine for processing AI response streams
|
|
30
39
|
*
|
|
@@ -823,6 +832,7 @@ var StreamProcessor = class StreamProcessor {
|
|
|
823
832
|
}
|
|
824
833
|
}
|
|
825
834
|
handleInterrupts(interrupts) {
|
|
835
|
+
const hasGeneric = interruptBatchHasGeneric(interrupts);
|
|
826
836
|
for (const interrupt of interrupts) {
|
|
827
837
|
const metadata = interrupt.metadata && typeof interrupt.metadata === "object" ? interrupt.metadata : {};
|
|
828
838
|
const kind = typeof metadata.kind === "string" ? metadata.kind : void 0;
|
|
@@ -844,11 +854,14 @@ var StreamProcessor = class StreamProcessor {
|
|
|
844
854
|
});
|
|
845
855
|
continue;
|
|
846
856
|
}
|
|
847
|
-
if (kind === "client_tool" || interrupt.reason === "client_tool_input")
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
857
|
+
if (kind === "client_tool" || interrupt.reason === "client_tool_input") {
|
|
858
|
+
if (hasGeneric) continue;
|
|
859
|
+
this.events.onToolCall?.({
|
|
860
|
+
toolCallId,
|
|
861
|
+
toolName,
|
|
862
|
+
input
|
|
863
|
+
});
|
|
864
|
+
}
|
|
852
865
|
}
|
|
853
866
|
}
|
|
854
867
|
findToolCallName(toolCallId) {
|