{"version":3,"file":"compose.mjs","names":[],"sources":["../../../../../../../../ai/src/middleware/helpers/compose.ts"],"sourcesContent":["import type { AgentMiddleware } from \"../../contracts/middleware\";\n\n/**\n * Flatten one or more middleware sources into a single ordered\n * array suitable for `agent({ middleware: [...] })`.\n *\n * **Role.** As middleware catalogs grow, agent configs accumulate\n * long arrays that mix \"always-on\" stacks (cache + budget + guardrail)\n * with per-concern extras (per-tool rate-limits, audit hooks). A\n * single `compose` call lets callers keep those sources as named\n * variables and flatten at the registration site.\n *\n * **Semantics.** Registration order is preserved across sources —\n * `compose(a, b, c)` produces `[...a, ...b, ...c]`. Because the\n * pipeline's onion is strictly registration-ordered, the flattened\n * order is the execution order. No de-duplication, no sorting, no\n * priority logic — that would hide bugs, not fix them.\n *\n * **Accepts arrays OR individual middlewares.** Both forms are\n * common in callsite code; the helper flattens either.\n *\n * @example\n * const standardStack = [\n *   ai.middleware.semanticCache({ ... }),\n *   ai.middleware.budget({ maxTokens: 20_000 }),\n *   ai.middleware.guardrail({ ... }),\n * ];\n *\n * const toolRateLimits = [\n *   toolRateLimit({ tool: \"search_web\", maxCalls: 3 }),\n *   toolRateLimit({ tool: \"expensive_api\", maxCalls: 1 }),\n * ];\n *\n * const myAgent = ai.agent({\n *   model,\n *   middleware: ai.middleware.compose(standardStack, toolRateLimits, auditMiddleware),\n * });\n */\nexport function composeMiddleware(\n  ...sources: ReadonlyArray<AgentMiddleware | ReadonlyArray<AgentMiddleware>>\n): AgentMiddleware[] {\n  const out: AgentMiddleware[] = [];\n\n  for (const source of sources) {\n    if (Array.isArray(source)) {\n      out.push(...source);\n      continue;\n    }\n\n    out.push(source as AgentMiddleware);\n  }\n\n  return out;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,kBACd,GAAG,SACgB;CACnB,MAAM,MAAyB,CAAC;CAEhC,KAAK,MAAM,UAAU,SAAS;EAC5B,IAAI,MAAM,QAAQ,MAAM,GAAG;GACzB,IAAI,KAAK,GAAG,MAAM;GAClB;EACF;EAEA,IAAI,KAAK,MAAyB;CACpC;CAEA,OAAO;AACT"}