UNPKG

@tanstack/ai

Version:

Core TanStack AI library - Open source AI SDK

67 lines (66 loc) 2.91 kB
import { StreamChunk } from '../../../types.js'; import { AbortInfo, AfterToolCallInfo, BeforeToolCallDecision, ChatMiddleware, ChatMiddlewareConfig, ChatMiddlewareContext, ErrorInfo, FinishInfo, IterationInfo, 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 { private readonly middlewares; constructor(middlewares: ReadonlyArray<ChatMiddleware>); 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, config: ChatMiddlewareConfig): Promise<ChatMiddlewareConfig>; /** * Call onStart on all middleware in order. */ runOnStart(ctx: ChatMiddlewareContext): 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, 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, hookCtx: ToolCallHookContext): Promise<BeforeToolCallDecision>; /** * Run onAfterToolCall on all middleware in order. */ runOnAfterToolCall(ctx: ChatMiddlewareContext, info: AfterToolCallInfo): Promise<void>; /** * Run onUsage on all middleware in order. */ runOnUsage(ctx: ChatMiddlewareContext, usage: UsageInfo): Promise<void>; /** * Run onFinish on all middleware in order. */ runOnFinish(ctx: ChatMiddlewareContext, info: FinishInfo): Promise<void>; /** * Run onAbort on all middleware in order. */ runOnAbort(ctx: ChatMiddlewareContext, info: AbortInfo): Promise<void>; /** * Run onError on all middleware in order. */ runOnError(ctx: ChatMiddlewareContext, info: ErrorInfo): Promise<void>; /** * Run onIteration on all middleware in order. * Called at the start of each agent loop iteration. */ runOnIteration(ctx: ChatMiddlewareContext, 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, info: ToolPhaseCompleteInfo): Promise<void>; }