{"version":3,"file":"middleware.mjs","names":[],"sources":["../../../../src/v2/runtime/core/middleware.ts"],"sourcesContent":["/**\n * Middleware support for CopilotKit Runtime.\n *\n * A middleware hook can be provided as either:\n *   1. A **callback function** executed in-process.\n *   2. A **webhook URL** (http/https).  The runtime will `POST` a JSON payload\n *      to the URL and, for *before* hooks, accept an optional modified\n *      `Request` object in the response body.\n *\n * Two lifecycle hooks are available:\n *   • `BEFORE_REQUEST` – runs *before* the request handler.\n *   • `AFTER_REQUEST`  – runs *after* the handler returns a `Response`.\n */\n\nimport type { CopilotRuntimeLike } from \"./runtime\";\nimport type { MaybePromise } from \"@copilotkit/shared\";\nimport { logger } from \"@copilotkit/shared\";\nimport { parseSSEResponse } from \"./middleware-sse-parser\";\nimport type { Message } from \"./middleware-sse-parser\";\n\n/* ------------------------------------------------------------------------------------------------\n * Public types\n * --------------------------------------------------------------------------------------------- */\n\nexport interface BeforeRequestMiddlewareParameters {\n  runtime: CopilotRuntimeLike;\n  request: Request;\n  path: string;\n}\nexport interface AfterRequestMiddlewareParameters {\n  runtime: CopilotRuntimeLike;\n  response: Response;\n  path: string;\n  /** Reconstructed messages from the SSE stream (empty for non-SSE responses). */\n  messages?: Message[];\n  /** Thread ID extracted from the RUN_STARTED event. */\n  threadId?: string;\n  /** Run ID extracted from the RUN_STARTED event. */\n  runId?: string;\n}\n\nexport type BeforeRequestMiddlewareFn = (\n  params: BeforeRequestMiddlewareParameters,\n) => MaybePromise<Request | void>;\nexport type AfterRequestMiddlewareFn = (\n  params: AfterRequestMiddlewareParameters,\n) => MaybePromise<void>;\n\n/**\n * A middleware value can be either a callback function or a webhook URL.\n */\nexport type BeforeRequestMiddleware = BeforeRequestMiddlewareFn;\nexport type AfterRequestMiddleware = AfterRequestMiddlewareFn;\n\n/** Lifecycle events emitted to webhook middleware. */\nexport enum CopilotKitMiddlewareEvent {\n  BeforeRequest = \"BEFORE_REQUEST\",\n  AfterRequest = \"AFTER_REQUEST\",\n}\n\n/** Stages used by the Middleware Webhook Protocol */\n/** Stages used by the CopilotKit webhook protocol */\nexport enum WebhookStage {\n  BeforeRequest = \"before_request\",\n  AfterRequest = \"after_request\",\n}\n\n/* ------------------------------------------------------------------------------------------------\n * Internal helpers – (de)serialisation\n * --------------------------------------------------------------------------------------------- */\n\nexport async function callBeforeRequestMiddleware({\n  runtime,\n  request,\n  path,\n}: BeforeRequestMiddlewareParameters): Promise<Request | void> {\n  const mw = runtime.beforeRequestMiddleware;\n  if (!mw) return;\n\n  // Function-based middleware (in-process)\n  if (typeof mw === \"function\") {\n    return (mw as BeforeRequestMiddlewareFn)({ runtime, request, path });\n  }\n\n  logger.warn({ mw }, \"Unsupported beforeRequestMiddleware value – skipped\");\n  return;\n}\n\nexport async function callAfterRequestMiddleware({\n  runtime,\n  response,\n  path,\n}: {\n  runtime: CopilotRuntimeLike;\n  response: Response;\n  path: string;\n}): Promise<void> {\n  const mw = runtime.afterRequestMiddleware;\n  if (!mw) return;\n\n  const { messages, threadId, runId } = await parseSSEResponse(response);\n\n  if (typeof mw === \"function\") {\n    return (mw as AfterRequestMiddlewareFn)({\n      runtime,\n      response,\n      path,\n      messages,\n      threadId,\n      runId,\n    });\n  }\n\n  logger.warn({ mw }, \"Unsupported afterRequestMiddleware value – skipped\");\n}\n"],"mappings":";;;;;AAuEA,eAAsB,4BAA4B,EAChD,SACA,SACA,QAC6D;CAC7D,MAAM,KAAK,QAAQ;AACnB,KAAI,CAAC,GAAI;AAGT,KAAI,OAAO,OAAO,WAChB,QAAQ,GAAiC;EAAE;EAAS;EAAS;EAAM,CAAC;AAGtE,QAAO,KAAK,EAAE,IAAI,EAAE,sDAAsD;;AAI5E,eAAsB,2BAA2B,EAC/C,SACA,UACA,QAKgB;CAChB,MAAM,KAAK,QAAQ;AACnB,KAAI,CAAC,GAAI;CAET,MAAM,EAAE,UAAU,UAAU,UAAU,MAAM,iBAAiB,SAAS;AAEtE,KAAI,OAAO,OAAO,WAChB,QAAQ,GAAgC;EACtC;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;AAGJ,QAAO,KAAK,EAAE,IAAI,EAAE,qDAAqD"}