UNPKG

@tanstack/ai

Version:

Type-safe TypeScript AI SDK for streaming chat, tool calling, agents, structured outputs, and multimodal generation.

88 lines (87 loc) 4.32 kB
import { StreamChunk } from '../../../types.js'; import { InternalLogger } from '../../../logger/internal-logger.js'; import { AbortInfo, AfterToolCallInfo, BeforeToolCallDecision, ChatMiddleware, ChatMiddlewareConfig, ChatMiddlewareContext, ErrorInfo, FinishInfo, IterationInfo, StructuredOutputMiddlewareConfig, ToolCallHookContext, ToolPhaseCompleteInfo, UsageInfo } from './types.js'; /** * Internal middleware runner that manages composed execution of middleware hooks. * Created once per chat() invocation. */ export declare class MiddlewareRunner<TContext = unknown> { private readonly middlewares; private readonly logger; constructor(middlewares: ReadonlyArray<ChatMiddleware<TContext>>, logger: InternalLogger); get hasMiddleware(): boolean; /** * Pipe config through all middleware onConfig hooks in order. * Each middleware receives the merged config from previous middleware. * Partial returns are shallow-merged with the current config. */ runOnConfig(ctx: ChatMiddlewareContext<TContext>, config: ChatMiddlewareConfig): Promise<ChatMiddlewareConfig>; /** * Pipe config through all middleware onStructuredOutputConfig hooks in order. * Each middleware receives the merged config from previous middleware. * Partial returns are shallow-merged with the current config. * * Called once at the structured-output boundary, before runOnConfig at the * same boundary (which receives a ChatMiddlewareConfig view, no outputSchema). */ runOnStructuredOutputConfig(ctx: ChatMiddlewareContext<TContext>, config: StructuredOutputMiddlewareConfig): Promise<StructuredOutputMiddlewareConfig>; /** * Run all `setup` hooks in array order, then assert every declared `provides` * capability was actually provided. Wires the last-wins duplicate-provide * warning into the registry. Runs before init `onConfig`. * * Takes the full `ChatMiddlewareContext` — the same stable context the engine * threads through every other hook — because it both forwards `ctx` to each * `setup` hook and emits instrumentation events from it. */ runSetup(ctx: ChatMiddlewareContext<TContext>): Promise<void>; /** * Call onStart on all middleware in order. */ runOnStart(ctx: ChatMiddlewareContext<TContext>): Promise<void>; /** * Pipe a single chunk through all middleware onChunk hooks in order. * Returns the resulting chunks (0..N) to yield to the consumer. * * - void: pass through unchanged * - chunk: replace with this chunk * - chunk[]: expand to multiple chunks * - null: drop the chunk entirely */ runOnChunk(ctx: ChatMiddlewareContext<TContext>, chunk: StreamChunk): Promise<Array<StreamChunk>>; /** * Run onBeforeToolCall through middleware in order. * Returns the first non-void decision, or undefined to continue normally. */ runOnBeforeToolCall(ctx: ChatMiddlewareContext<TContext>, hookCtx: ToolCallHookContext): Promise<BeforeToolCallDecision>; /** * Run onAfterToolCall on all middleware in order. */ runOnAfterToolCall(ctx: ChatMiddlewareContext<TContext>, info: AfterToolCallInfo): Promise<void>; /** * Run onUsage on all middleware in order. */ runOnUsage(ctx: ChatMiddlewareContext<TContext>, usage: UsageInfo): Promise<void>; /** * Run onFinish on all middleware in order. */ runOnFinish(ctx: ChatMiddlewareContext<TContext>, info: FinishInfo): Promise<void>; /** * Run onAbort on all middleware in order. */ runOnAbort(ctx: ChatMiddlewareContext<TContext>, info: AbortInfo): Promise<void>; /** * Run onError on all middleware in order. */ runOnError(ctx: ChatMiddlewareContext<TContext>, info: ErrorInfo): Promise<void>; /** * Run onIteration on all middleware in order. * Called at the start of each agent loop iteration. */ runOnIteration(ctx: ChatMiddlewareContext<TContext>, info: IterationInfo): Promise<void>; /** * Run onToolPhaseComplete on all middleware in order. * Called after all tool calls in an iteration have been processed. */ runOnToolPhaseComplete(ctx: ChatMiddlewareContext<TContext>, info: ToolPhaseCompleteInfo): Promise<void>; }