{"version":3,"file":"copilotkit.mjs","names":[],"sources":["../src/copilotkit.ts"],"sourcesContent":["import { AbstractAgent } from \"@ag-ui/client\";\nimport {\n  CopilotRuntime,\n  copilotRuntimeNodeHttpEndpoint,\n  CopilotServiceAdapter,\n  ExperimentalEmptyAdapter,\n} from \"@copilotkit/runtime\";\nimport { RequestContext } from \"@mastra/core/request-context\";\nimport { registerApiRoute } from \"@mastra/core/server\";\nimport { MastraAgent } from \"./mastra\";\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  T extends Record<string, any> | unknown = unknown,\n>({\n  path,\n  resourceId,\n  serviceAdapter = new ExperimentalEmptyAdapter(),\n  agents,\n  setContext,\n}: {\n  path: string;\n  resourceId: string;\n  serviceAdapter?: CopilotServiceAdapter;\n  agents?: Record<string, AbstractAgent>;\n  setContext?: (\n    c: any,\n    requestContext: RequestContext<T>,\n  ) => void | Promise<void>;\n}) {\n  return registerApiRoute(path, {\n    method: `ALL`,\n    handler: async (c) => {\n      const mastra = c.get(\"mastra\");\n\n      const requestContext = new RequestContext<T>();\n\n      if (setContext) {\n        await setContext(c, requestContext);\n      }\n\n      const aguiAgents =\n        agents ||\n        MastraAgent.getLocalAgents({\n          resourceId,\n          mastra,\n          requestContext: requestContext as RequestContext,\n        });\n\n      const runtime = new CopilotRuntime({\n        agents: aguiAgents as any,\n      });\n\n      const handler = copilotRuntimeNodeHttpEndpoint({\n        endpoint: path,\n        runtime,\n        serviceAdapter,\n      });\n\n      return handler(c.req.raw);\n    },\n  });\n}\n"],"mappings":"sRAsBA,SAAgB,EAEd,CACA,OACA,aACA,iBAAiB,IAAI,EACrB,SACA,cAUC,CACD,OAAO,EAAiB,EAAM,CAC5B,OAAQ,MACR,QAAS,KAAO,IAAM,CACpB,IAAM,EAAS,EAAE,IAAI,SAAS,CAExB,EAAiB,IAAI,EAwB3B,OAtBI,GACF,MAAM,EAAW,EAAG,EAAe,CAerB,EAA+B,CAC7C,SAAU,EACV,QANc,IAAI,EAAe,CACjC,OARA,GACA,EAAY,eAAe,CACzB,aACA,SACgB,iBACjB,CAAC,CAIH,CAAC,CAKA,iBACD,CAAC,CAEa,EAAE,IAAI,IAAI,EAE5B,CAAC"}