/** * Wire-format-agnostic Koa-style middleware chain for adapter proxies. * * Each adapter's proxy detects `tool_use` blocks in the LLM's response (in * its own wire format), then calls `startChain` to run the framework's * middleware exactly once per call. The chain races to one of two outcomes: * * - `'short-circuit'` — middleware returned a `ToolResult` without * calling `handler`. The proxy redirects the tool_use to a benign * equivalent (Bash echo + base64 in the claude adapter) and replaces * the result the LLM sees on the next request with `outcome.result`. * * - `'descended'` — middleware (somewhere in the chain) called * `handler(args)`, possibly with mutated args. The chain pauses on * the inner handler's promise; the proxy emits a `tool_use` with * `finalArgs` to the agent, lets it run, then on the next request * calls `resolveBackend(realResult)`. The chain finishes by walking * back up; the final result lives in `chainComplete`. * * This file knows nothing about HTTP, SSE, or any specific provider. */ import type { Config, Data, Middleware, ToolResult } from '../types.js'; export type ChainCtx = { evalName: string; config: Readonly; data: Data; abort: (reason?: unknown) => void; /** Wire-format per-call id (Anthropic `tool_use.id`, Responses `call_id`). */ toolUseId: string; server: string; tool: string; input: unknown; }; export type ChainOutcome = { kind: 'short-circuit'; /** Final ToolResult to feed to the LLM as the synthetic tool_result. */ result: ToolResult; } | { kind: 'descended'; /** Args the proxy should send the agent — possibly middleware-mutated. */ finalArgs: unknown; /** Resolve when the agent's real tool_result arrives in the next request. */ resolveBackend: (real: ToolResult) => void; /** Reject when the agent fails to produce a result (timeout, abort, etc.). */ rejectBackend: (err: Error) => void; /** Resolves with the chain's final ToolResult once `resolveBackend` is called and middleware finishes transforming. */ chainComplete: Promise; }; /** * Run the `onToolCall` chain Koa-style for a single tool call. Returns as * soon as the chain either completes (short-circuit) or descends to the * synthetic backend at the bottom (need-backend); the caller handles each * shape differently. */ export declare function startChain(middleware: readonly Middleware[], ctx: ChainCtx): Promise; /** Flatten a `ToolResult.content` array to a plain string. */ export declare function stringifyToolResult(r: ToolResult): string; //# sourceMappingURL=middleware.d.ts.map