UNPKG

ai-patterns

Version:

Production-ready TypeScript patterns to build solid and robust AI applications. Retry logic, circuit breakers, rate limiting, human-in-the-loop escalation, prompt versioning, response validation, context window management, and more—all with complete type

33 lines 1.22 kB
/** * Compose Pattern - Middleware-based composition */ import type { Middleware, ComposeConfig } from "../types/composition"; export type { Middleware, ComposeConfig }; /** * Compose multiple middlewares together * * Middlewares are applied from right to left (innermost first), similar to * function composition in mathematics: compose([f, g, h])(x) = f(g(h(x))) * * @example * ```typescript * import { compose, withTimeout, withRetry, withCircuitBreaker } from 'ai-patterns'; * * const robustAPI = compose([ * withCircuitBreaker({ failureThreshold: 5 }), * withTimeout({ duration: 5000 }), * withRetry({ maxAttempts: 3 }) * ]); * * const result = await robustAPI( * async () => fetch('/api/data').then(r => r.json()), * undefined * ); * ``` * * @param middlewares - Array of middlewares to compose * @param config - Optional configuration * @returns Composed function that applies all middlewares */ export declare function compose<TInput = void, TOutput = unknown>(middlewares: Middleware<TInput, TOutput>[], config?: ComposeConfig<TInput, TOutput>): (execute: (input: TInput) => TOutput | Promise<TOutput>, input?: TInput) => Promise<TOutput>; //# sourceMappingURL=compose.d.ts.map