{"version":3,"file":"copilotkit.mjs","names":[],"sources":["../src/copilotkit.ts"],"sourcesContent":["import {\n  CopilotServiceAdapter,\n  ExperimentalEmptyAdapter,\n} from \"@copilotkit/runtime\";\nimport {\n  AgentsConfig,\n  CopilotCorsConfig,\n  CopilotRuntime,\n  CopilotRuntimeOptions,\n  createCopilotRuntimeHandler,\n} from \"@copilotkit/runtime/v2\";\nimport {\n  MASTRA_RESOURCE_ID_KEY,\n  RequestContext,\n} from \"@mastra/core/request-context\";\nimport { ContextWithMastra, registerApiRoute } from \"@mastra/core/server\";\nimport { MastraAgent, MastraTracingOptions } from \"./mastra\";\n\n/**\n * `Omit` that distributes over each member of a union, so a discriminated union\n * (e.g. `CopilotRuntimeOptions`) keeps its discriminant correlation. Plain\n * `Omit<A | B, K>` collapses to `Omit<A & B, K>` because `keyof (A | B)` is only\n * the shared keys, which destroys the union.\n */\ntype DistributiveOmit<T, K extends PropertyKey> = T extends unknown\n  ? Omit<T, K>\n  : never;\n\n/**\n * Registers a CopilotKit endpoint that exposes Mastra agents through the AG-UI protocol.\n * This function creates an API route that handles CopilotKit requests and forwards them to Mastra agents, enabling seamless integration between CopilotKit's UI components and Mastra's agent framework.\n *\n * @example\n * ```ts\n * registerCopilotKit({\n *   path: \"/api/copilotkit\"\n * });\n * ```\n */\nexport function registerCopilotKit({\n  path,\n  resourceId,\n  serviceAdapter = new ExperimentalEmptyAdapter(),\n  setContext,\n  agents,\n  cors,\n  tracingOptions,\n  ...runtimeOptions\n}: {\n  path: string;\n  resourceId: string;\n  /**\n   * Mastra tracing options forwarded to each agent run (default-agent path\n   * only; ignored when `agents` is supplied since those are pre-constructed).\n   * See MastraAgentConfig.tracingOptions.\n   */\n  tracingOptions?: MastraTracingOptions;\n  /**\n   * @deprecated The v2 CopilotKit runtime handler used internally has no\n   * service-adapter slot (AG-UI agents don't use one), so this option is\n   * accepted for backwards compatibility but ignored. Safe to remove.\n   */\n  serviceAdapter?: CopilotServiceAdapter;\n  /**\n   * Hook to populate the request context before agents run. It runs inside the\n   * route handler, i.e. *after* any Mastra server middleware, so the\n   * `requestContext` it receives already holds whatever keys that middleware\n   * set. Be careful not to clobber those keys (e.g. `MASTRA_RESOURCE_ID_KEY`)\n   * unless that is the intent.\n   */\n  setContext?: (\n    c: ContextWithMastra,\n    requestContext: RequestContext,\n  ) => void | Promise<void>;\n  cors?: boolean | CopilotCorsConfig;\n} & DistributiveOmit<CopilotRuntimeOptions, \"agents\"> & {\n    agents?: AgentsConfig;\n  }) {\n  // `serviceAdapter` is deprecated and intentionally unused (see its JSDoc):\n  // the v2 runtime handler has no service-adapter slot. Referenced here to\n  // keep it a supported, non-breaking option without a lint no-unused-vars.\n  void serviceAdapter;\n\n  return registerApiRoute(path, {\n    method: `ALL`,\n    handler: async (c) => {\n      const mastra = c.get(\"mastra\");\n      const requestContext = c.get(\"requestContext\");\n\n      if (setContext) {\n        await setContext(c, requestContext);\n      }\n\n      const aguiAgents =\n        agents ||\n        MastraAgent.getLocalAgents({\n          resourceId:\n            requestContext.get<\n              typeof MASTRA_RESOURCE_ID_KEY,\n              string | undefined\n            >(MASTRA_RESOURCE_ID_KEY) ?? resourceId,\n          mastra,\n          requestContext,\n          tracingOptions,\n        });\n\n      const runtime = new CopilotRuntime({\n        ...runtimeOptions,\n        agents: aguiAgents,\n      });\n\n      const handler = createCopilotRuntimeHandler({\n        runtime,\n        basePath: path,\n        cors,\n        mode: \"single-route\",\n      });\n\n      // CopilotKit forwards `authorization` and `x-*` headers onto the agent\n      // (`configureAgentForRequest` → `extractForwardableHeaders`), and\n      // `@ag-ui/mastra` then passes the agent's headers as\n      // `modelSettings.headers` into the model call. That would forward our\n      // Mastra Authorization to our AI provider and clobber the provider's real\n      // Authorization header. Mastra has already authenticated this request by\n      // now, so drop the header before the runtime sees it.\n      const headers = new Headers(c.req.raw.headers);\n      headers.delete(\"authorization\");\n\n      return handler(new Request(c.req.raw, { headers }));\n    },\n  });\n}\n"],"mappings":"+TAuCA,SAAgB,EAAmB,CACjC,OACA,aACA,iBAAiB,IAAI,EACrB,aACA,SACA,OACA,iBACA,GAAG,GA8BA,CAMH,OAAO,EAAiB,EAAM,CAC5B,OAAQ,MACR,QAAS,KAAO,IAAM,CACpB,IAAM,EAAS,EAAE,IAAI,SAAS,CACxB,EAAiB,EAAE,IAAI,iBAAiB,CAE1C,GACF,MAAM,EAAW,EAAG,EAAe,CAGrC,IAAM,EACJ,GACA,EAAY,eAAe,CACzB,WACE,EAAe,IAGb,EAAuB,EAAI,EAC/B,SACA,iBACA,iBACD,CAAC,CAOE,EAAU,EAA4B,CAC1C,QANc,IAAI,EAAe,CACjC,GAAG,EACH,OAAQ,EACT,CAAC,CAIA,SAAU,EACV,OACA,KAAM,eACP,CAAC,CASI,EAAU,IAAI,QAAQ,EAAE,IAAI,IAAI,QAAQ,CAG9C,OAFA,EAAQ,OAAO,gBAAgB,CAExB,EAAQ,IAAI,QAAQ,EAAE,IAAI,IAAK,CAAE,UAAS,CAAC,CAAC,EAEtD,CAAC"}