import { MiddlewareExecuteContext, MiddlewareSupervisorContext, MiddlewareToolContext, MiddlewareTripContext } from "../contracts/middleware/middleware-context.type.mjs"; import { AgentMiddleware } from "../contracts/middleware/middleware.contract.mjs"; import { Logger } from "@warlock.js/logger"; //#region ../ai/src/middleware/pipeline.d.ts /** * The four levels at which middleware can hook — mirrors * `AgentMiddleware`'s optional `execute` / `trip` / `tool` / * `supervisor` keys. Kept as a single named union so callers can pass * it around without inline-duplicating the literals. The first three * fire on the agent pipeline; `supervisor` fires once around a whole * `supervisor.execute()` run. */ type MiddlewareLevel = "execute" | "trip" | "tool" | "supervisor"; /** * Shape of the context object for each level. The pipeline is * level-parameterized on the ctx type via this mapping so callers * get compile-time narrowing when they instantiate `runPipeline`. */ type MiddlewareContextByLevel = { execute: MiddlewareExecuteContext; trip: MiddlewareTripContext; tool: MiddlewareToolContext; supervisor: MiddlewareSupervisorContext; }; /** * Run an inner async operation through a stack of agent middlewares * at a single level, applying the onion-model before/after/onError * semantics documented on `AgentMiddleware`. * * **Semantics.** * - `before` hooks run in registration order (top-down). * Returning a defined value from a `before` hook short-circuits the * pipeline with that value as the result, skipping `inner()` and * all deeper `before` / `after` hooks — but outer middleware * `after` hooks (registered earlier) still run on the synthetic * value. * - `after` hooks run in reverse registration order (bottom-up). * Returning a defined value replaces the result before it * propagates further out. Returning `void` / `undefined` keeps the * existing result. * - `onError` hooks also run in reverse (bottom-up) — any error * thrown by `inner()`, by a `before` hook, or by an `after` hook * unwinds through each frame's `onError` in turn. Returning a * defined value from `onError` recovers: the error is cleared and * the returned value becomes the new result (which then flows * through outer `after` hooks). Returning `void` propagates the * error to the next outer frame. * * **Implementation.** Built by folding the middleware array from the * end inward: each middleware produces a closure that wraps the * previous closure (the deeper pipeline). The outermost wrap is * middleware index 0 — so registration order matches onion order * without any reverse iteration at call time. * * **No magic.** The pipeline does not swallow, retry, or translate * errors. Hooks that throw propagate unchanged (subject to `onError` * recovery). Pipeline-level logging is debug-only and respects each * middleware's `log: false` kill-switch. * * @example * const response = await runPipeline( * middlewares, * "trip", * tripContext, * () => model.complete(messages, callOptions), * logger, * ); */ declare function runPipeline(middlewares: ReadonlyArray, level: Level, context: MiddlewareContextByLevel[Level], inner: () => Promise, logger?: Logger): Promise; //#endregion export { MiddlewareContextByLevel, MiddlewareLevel, runPipeline }; //# sourceMappingURL=pipeline.d.mts.map