@copilotkitnext/runtime 1.52.2-next.2 → 1.52.2-next.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,8 @@
1
1
  const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
2
2
  const require_header_utils = require('./header-utils.cjs');
3
3
  let _ag_ui_client = require("@ag-ui/client");
4
+ let _ag_ui_a2ui_middleware = require("@ag-ui/a2ui-middleware");
5
+ let _ag_ui_mcp_apps_middleware = require("@ag-ui/mcp-apps-middleware");
4
6
  let _ag_ui_encoder = require("@ag-ui/encoder");
5
7
 
6
8
  //#region src/handlers/handle-run.ts
@@ -15,6 +17,14 @@ async function handleRunAgent({ runtime, request, agentId }) {
15
17
  headers: { "Content-Type": "application/json" }
16
18
  });
17
19
  const agent = agents[agentId].clone();
20
+ if (runtime.a2ui) {
21
+ const { agents: targetAgents, ...a2uiOptions } = runtime.a2ui;
22
+ if ((!targetAgents || targetAgents.includes(agentId)) && "use" in agent && typeof agent.use === "function") agent.use(new _ag_ui_a2ui_middleware.A2UIMiddleware(a2uiOptions));
23
+ }
24
+ if (runtime.mcp) {
25
+ const { agents: targetAgents, ...mcpOptions } = runtime.mcp;
26
+ if ((!targetAgents || targetAgents.includes(agentId)) && "use" in agent && typeof agent.use === "function") agent.use(new _ag_ui_mcp_apps_middleware.MCPAppsMiddleware(mcpOptions));
27
+ }
18
28
  if (agent && "headers" in agent) {
19
29
  const forwardableHeaders = require_header_utils.extractForwardableHeaders(request);
20
30
  agent.headers = {
@@ -1 +1 @@
1
- {"version":3,"file":"handle-run.cjs","names":["extractForwardableHeaders","RunAgentInputSchema","EventEncoder"],"sources":["../../src/handlers/handle-run.ts"],"sourcesContent":["import {\n AbstractAgent,\n RunAgentInput,\n RunAgentInputSchema,\n} from \"@ag-ui/client\";\nimport { EventEncoder } from \"@ag-ui/encoder\";\nimport { CopilotRuntime } from \"../runtime\";\nimport { extractForwardableHeaders } from \"./header-utils\";\n\ninterface RunAgentParameters {\n request: Request;\n runtime: CopilotRuntime;\n agentId: string;\n}\n\nexport async function handleRunAgent({\n runtime,\n request,\n agentId,\n}: RunAgentParameters) {\n try {\n const agents = await runtime.agents;\n\n // Check if the requested agent exists\n if (!agents[agentId]) {\n return new Response(\n JSON.stringify({\n error: \"Agent not found\",\n message: `Agent '${agentId}' does not exist`,\n }),\n {\n status: 404,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n\n const registeredAgent = agents[agentId] as AbstractAgent;\n const agent = registeredAgent.clone() as AbstractAgent;\n\n if (agent && \"headers\" in agent) {\n const forwardableHeaders = extractForwardableHeaders(request);\n agent.headers = {\n ...(agent.headers as Record<string, string>),\n ...forwardableHeaders,\n };\n }\n\n // Parse and validate input BEFORE creating the stream\n // so we can return a proper error response\n let input: RunAgentInput;\n try {\n const requestBody = await request.json();\n input = RunAgentInputSchema.parse(requestBody);\n } catch (error) {\n console.error(\"Invalid run request body:\", error);\n return new Response(\n JSON.stringify({\n error: \"Invalid request body\",\n details: error instanceof Error ? error.message : String(error),\n }),\n {\n status: 400,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n\n const stream = new TransformStream();\n const writer = stream.writable.getWriter();\n const encoder = new EventEncoder();\n let streamClosed = false;\n\n agent.setMessages(input.messages);\n agent.setState(input.state);\n agent.threadId = input.threadId;\n\n // Process the agent run in the background\n (async () => {\n runtime.runner\n .run({\n threadId: input.threadId,\n agent,\n input,\n })\n .subscribe({\n next: async (event) => {\n if (!request.signal.aborted && !streamClosed) {\n try {\n await writer.write(encoder.encode(event));\n } catch (error) {\n if (error instanceof Error && error.name === \"AbortError\") {\n streamClosed = true;\n }\n }\n }\n },\n error: async (error) => {\n console.error(\"Error running agent:\", error);\n if (!streamClosed) {\n try {\n await writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n },\n complete: async () => {\n if (!streamClosed) {\n try {\n await writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n },\n });\n })().catch((error) => {\n console.error(\"Error running agent:\", error);\n console.error(\n \"Error stack:\",\n error instanceof Error ? error.stack : \"No stack trace\",\n );\n console.error(\"Error details:\", {\n name: error instanceof Error ? error.name : \"Unknown\",\n message: error instanceof Error ? error.message : String(error),\n cause: error instanceof Error ? error.cause : undefined,\n });\n if (!streamClosed) {\n try {\n writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n });\n\n // Return the SSE response\n return new Response(stream.readable, {\n status: 200,\n headers: {\n \"Content-Type\": \"text/event-stream\",\n \"Cache-Control\": \"no-cache\",\n Connection: \"keep-alive\",\n },\n });\n } catch (error) {\n console.error(\"Error running agent:\", error);\n console.error(\n \"Error stack:\",\n error instanceof Error ? error.stack : \"No stack trace\",\n );\n console.error(\"Error details:\", {\n name: error instanceof Error ? error.name : \"Unknown\",\n message: error instanceof Error ? error.message : String(error),\n cause: error instanceof Error ? error.cause : undefined,\n });\n\n return new Response(\n JSON.stringify({\n error: \"Failed to run agent\",\n message: error instanceof Error ? error.message : \"Unknown error\",\n }),\n {\n status: 500,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n}\n"],"mappings":";;;;;;AAeA,eAAsB,eAAe,EACnC,SACA,SACA,WACqB;AACrB,KAAI;EACF,MAAM,SAAS,MAAM,QAAQ;AAG7B,MAAI,CAAC,OAAO,SACV,QAAO,IAAI,SACT,KAAK,UAAU;GACb,OAAO;GACP,SAAS,UAAU,QAAQ;GAC5B,CAAC,EACF;GACE,QAAQ;GACR,SAAS,EAAE,gBAAgB,oBAAoB;GAChD,CACF;EAIH,MAAM,QADkB,OAAO,SACD,OAAO;AAErC,MAAI,SAAS,aAAa,OAAO;GAC/B,MAAM,qBAAqBA,+CAA0B,QAAQ;AAC7D,SAAM,UAAU;IACd,GAAI,MAAM;IACV,GAAG;IACJ;;EAKH,IAAI;AACJ,MAAI;GACF,MAAM,cAAc,MAAM,QAAQ,MAAM;AACxC,WAAQC,kCAAoB,MA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AC/C,CAAC;AACF,OAAI,CAAC,aACH,KAAI;AACF,WAAO,OAAO;AACd,mBAAe;WACT;IAIV;AAGF,SAAO,IAAI,SAAS,OAAO,UAAU;GACnC,QAAQ;GACR,SAAS;IACP,gBAAgB;IAChB,iBAAiB;IACjB,YAAY;IACb;GACF,CAAC;UACK,OAAO;AACd,UAAQ,MAAM,wBAAwB,MAAM;AAC5C,UAAQ,MACN,gBACA,iBAAiB,QAAQ,MAAM,QAAQ,iBACxC;AACD,UAAQ,MAAM,kBAAkB;GAC9B,MAAM,iBAAiB,QAAQ,MAAM,OAAO;GAC5C,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;GAC/D,OAAO,iBAAiB,QAAQ,MAAM,QAAQ;GAC/C,CAAC;AAEF,SAAO,IAAI,SACT,KAAK,UAAU;GACb,OAAO;GACP,SAAS,iBAAiB,QAAQ,MAAM,UAAU;GACnD,CAAC,EACF;GACE,QAAQ;GACR,SAAS,EAAE,gBAAgB,oBAAoB;GAChD,CACF"}
1
+ {"version":3,"file":"handle-run.cjs","names":["A2UIMiddleware","MCPAppsMiddleware","extractForwardableHeaders","RunAgentInputSchema","EventEncoder"],"sources":["../../src/handlers/handle-run.ts"],"sourcesContent":["import {\n AbstractAgent,\n RunAgentInput,\n RunAgentInputSchema,\n} from \"@ag-ui/client\";\nimport { A2UIMiddleware } from \"@ag-ui/a2ui-middleware\";\nimport { MCPAppsMiddleware } from \"@ag-ui/mcp-apps-middleware\";\nimport { EventEncoder } from \"@ag-ui/encoder\";\nimport { CopilotRuntime } from \"../runtime\";\nimport { extractForwardableHeaders } from \"./header-utils\";\n\ninterface RunAgentParameters {\n request: Request;\n runtime: CopilotRuntime;\n agentId: string;\n}\n\nexport async function handleRunAgent({\n runtime,\n request,\n agentId,\n}: RunAgentParameters) {\n try {\n const agents = await runtime.agents;\n\n // Check if the requested agent exists\n if (!agents[agentId]) {\n return new Response(\n JSON.stringify({\n error: \"Agent not found\",\n message: `Agent '${agentId}' does not exist`,\n }),\n {\n status: 404,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n\n const registeredAgent = agents[agentId] as AbstractAgent;\n const agent = registeredAgent.clone() as AbstractAgent;\n\n // Apply runtime-level A2UI middleware if configured\n if (runtime.a2ui) {\n const { agents: targetAgents, ...a2uiOptions } = runtime.a2ui;\n const shouldApply = !targetAgents || targetAgents.includes(agentId);\n if (\n shouldApply &&\n \"use\" in agent &&\n typeof (agent as any).use === \"function\"\n ) {\n (agent as any).use(new A2UIMiddleware(a2uiOptions));\n }\n }\n\n if (runtime.mcp) {\n const { agents: targetAgents, ...mcpOptions } = runtime.mcp;\n const shouldApply = !targetAgents || targetAgents.includes(agentId);\n if (\n shouldApply &&\n \"use\" in agent &&\n typeof (agent as any).use === \"function\"\n ) {\n (agent as any).use(new MCPAppsMiddleware(mcpOptions));\n }\n }\n\n if (agent && \"headers\" in agent) {\n const forwardableHeaders = extractForwardableHeaders(request);\n agent.headers = {\n ...(agent.headers as Record<string, string>),\n ...forwardableHeaders,\n };\n }\n\n // Parse and validate input BEFORE creating the stream\n // so we can return a proper error response\n let input: RunAgentInput;\n try {\n const requestBody = await request.json();\n input = RunAgentInputSchema.parse(requestBody);\n } catch (error) {\n console.error(\"Invalid run request body:\", error);\n return new Response(\n JSON.stringify({\n error: \"Invalid request body\",\n details: error instanceof Error ? error.message : String(error),\n }),\n {\n status: 400,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n\n const stream = new TransformStream();\n const writer = stream.writable.getWriter();\n const encoder = new EventEncoder();\n let streamClosed = false;\n\n agent.setMessages(input.messages);\n agent.setState(input.state);\n agent.threadId = input.threadId;\n\n // Process the agent run in the background\n (async () => {\n runtime.runner\n .run({\n threadId: input.threadId,\n agent,\n input,\n })\n .subscribe({\n next: async (event) => {\n if (!request.signal.aborted && !streamClosed) {\n try {\n await writer.write(encoder.encode(event));\n } catch (error) {\n if (error instanceof Error && error.name === \"AbortError\") {\n streamClosed = true;\n }\n }\n }\n },\n error: async (error) => {\n console.error(\"Error running agent:\", error);\n if (!streamClosed) {\n try {\n await writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n },\n complete: async () => {\n if (!streamClosed) {\n try {\n await writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n },\n });\n })().catch((error) => {\n console.error(\"Error running agent:\", error);\n console.error(\n \"Error stack:\",\n error instanceof Error ? error.stack : \"No stack trace\",\n );\n console.error(\"Error details:\", {\n name: error instanceof Error ? error.name : \"Unknown\",\n message: error instanceof Error ? error.message : String(error),\n cause: error instanceof Error ? error.cause : undefined,\n });\n if (!streamClosed) {\n try {\n writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n });\n\n // Return the SSE response\n return new Response(stream.readable, {\n status: 200,\n headers: {\n \"Content-Type\": \"text/event-stream\",\n \"Cache-Control\": \"no-cache\",\n Connection: \"keep-alive\",\n },\n });\n } catch (error) {\n console.error(\"Error running agent:\", error);\n console.error(\n \"Error stack:\",\n error instanceof Error ? error.stack : \"No stack trace\",\n );\n console.error(\"Error details:\", {\n name: error instanceof Error ? error.name : \"Unknown\",\n message: error instanceof Error ? error.message : String(error),\n cause: error instanceof Error ? error.cause : undefined,\n });\n\n return new Response(\n JSON.stringify({\n error: \"Failed to run agent\",\n message: error instanceof Error ? error.message : \"Unknown error\",\n }),\n {\n status: 500,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n}\n"],"mappings":";;;;;;;;AAiBA,eAAsB,eAAe,EACnC,SACA,SACA,WACqB;AACrB,KAAI;EACF,MAAM,SAAS,MAAM,QAAQ;AAG7B,MAAI,CAAC,OAAO,SACV,QAAO,IAAI,SACT,KAAK,UAAU;GACb,OAAO;GACP,SAAS,UAAU,QAAQ;GAC5B,CAAC,EACF;GACE,QAAQ;GACR,SAAS,EAAE,gBAAgB,oBAAoB;GAChD,CACF;EAIH,MAAM,QADkB,OAAO,SACD,OAAO;AAGrC,MAAI,QAAQ,MA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BAAoB;GAChD,CACF"}
@@ -1,5 +1,7 @@
1
1
  import { extractForwardableHeaders } from "./header-utils.mjs";
2
2
  import { RunAgentInputSchema } from "@ag-ui/client";
3
+ import { A2UIMiddleware } from "@ag-ui/a2ui-middleware";
4
+ import { MCPAppsMiddleware } from "@ag-ui/mcp-apps-middleware";
3
5
  import { EventEncoder } from "@ag-ui/encoder";
4
6
 
5
7
  //#region src/handlers/handle-run.ts
@@ -14,6 +16,14 @@ async function handleRunAgent({ runtime, request, agentId }) {
14
16
  headers: { "Content-Type": "application/json" }
15
17
  });
16
18
  const agent = agents[agentId].clone();
19
+ if (runtime.a2ui) {
20
+ const { agents: targetAgents, ...a2uiOptions } = runtime.a2ui;
21
+ if ((!targetAgents || targetAgents.includes(agentId)) && "use" in agent && typeof agent.use === "function") agent.use(new A2UIMiddleware(a2uiOptions));
22
+ }
23
+ if (runtime.mcp) {
24
+ const { agents: targetAgents, ...mcpOptions } = runtime.mcp;
25
+ if ((!targetAgents || targetAgents.includes(agentId)) && "use" in agent && typeof agent.use === "function") agent.use(new MCPAppsMiddleware(mcpOptions));
26
+ }
17
27
  if (agent && "headers" in agent) {
18
28
  const forwardableHeaders = extractForwardableHeaders(request);
19
29
  agent.headers = {
@@ -1 +1 @@
1
- {"version":3,"file":"handle-run.mjs","names":[],"sources":["../../src/handlers/handle-run.ts"],"sourcesContent":["import {\n AbstractAgent,\n RunAgentInput,\n RunAgentInputSchema,\n} from \"@ag-ui/client\";\nimport { EventEncoder } from \"@ag-ui/encoder\";\nimport { CopilotRuntime } from \"../runtime\";\nimport { extractForwardableHeaders } from \"./header-utils\";\n\ninterface RunAgentParameters {\n request: Request;\n runtime: CopilotRuntime;\n agentId: string;\n}\n\nexport async function handleRunAgent({\n runtime,\n request,\n agentId,\n}: RunAgentParameters) {\n try {\n const agents = await runtime.agents;\n\n // Check if the requested agent exists\n if (!agents[agentId]) {\n return new Response(\n JSON.stringify({\n error: \"Agent not found\",\n message: `Agent '${agentId}' does not exist`,\n }),\n {\n status: 404,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n\n const registeredAgent = agents[agentId] as AbstractAgent;\n const agent = registeredAgent.clone() as AbstractAgent;\n\n if (agent && \"headers\" in agent) {\n const forwardableHeaders = extractForwardableHeaders(request);\n agent.headers = {\n ...(agent.headers as Record<string, string>),\n ...forwardableHeaders,\n };\n }\n\n // Parse and validate input BEFORE creating the stream\n // so we can return a proper error response\n let input: RunAgentInput;\n try {\n const requestBody = await request.json();\n input = RunAgentInputSchema.parse(requestBody);\n } catch (error) {\n console.error(\"Invalid run request body:\", error);\n return new Response(\n JSON.stringify({\n error: \"Invalid request body\",\n details: error instanceof Error ? error.message : String(error),\n }),\n {\n status: 400,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n\n const stream = new TransformStream();\n const writer = stream.writable.getWriter();\n const encoder = new EventEncoder();\n let streamClosed = false;\n\n agent.setMessages(input.messages);\n agent.setState(input.state);\n agent.threadId = input.threadId;\n\n // Process the agent run in the background\n (async () => {\n runtime.runner\n .run({\n threadId: input.threadId,\n agent,\n input,\n })\n .subscribe({\n next: async (event) => {\n if (!request.signal.aborted && !streamClosed) {\n try {\n await writer.write(encoder.encode(event));\n } catch (error) {\n if (error instanceof Error && error.name === \"AbortError\") {\n streamClosed = true;\n }\n }\n }\n },\n error: async (error) => {\n console.error(\"Error running agent:\", error);\n if (!streamClosed) {\n try {\n await writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n },\n complete: async () => {\n if (!streamClosed) {\n try {\n await writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n },\n });\n })().catch((error) => {\n console.error(\"Error running agent:\", error);\n console.error(\n \"Error stack:\",\n error instanceof Error ? error.stack : \"No stack trace\",\n );\n console.error(\"Error details:\", {\n name: error instanceof Error ? error.name : \"Unknown\",\n message: error instanceof Error ? error.message : String(error),\n cause: error instanceof Error ? error.cause : undefined,\n });\n if (!streamClosed) {\n try {\n writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n });\n\n // Return the SSE response\n return new Response(stream.readable, {\n status: 200,\n headers: {\n \"Content-Type\": \"text/event-stream\",\n \"Cache-Control\": \"no-cache\",\n Connection: \"keep-alive\",\n },\n });\n } catch (error) {\n console.error(\"Error running agent:\", error);\n console.error(\n \"Error stack:\",\n error instanceof Error ? error.stack : \"No stack trace\",\n );\n console.error(\"Error details:\", {\n name: error instanceof Error ? error.name : \"Unknown\",\n message: error instanceof Error ? error.message : String(error),\n cause: error instanceof Error ? error.cause : undefined,\n });\n\n return new Response(\n JSON.stringify({\n error: \"Failed to run agent\",\n message: error instanceof Error ? error.message : \"Unknown error\",\n }),\n {\n status: 500,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n}\n"],"mappings":";;;;;AAeA,eAAsB,eAAe,EACnC,SACA,SACA,WACqB;AACrB,KAAI;EACF,MAAM,SAAS,MAAM,QAAQ;AAG7B,MAAI,CAAC,OAAO,SACV,QAAO,IAAI,SACT,KAAK,UAAU;GACb,OAAO;GACP,SAAS,UAAU,QAAQ;GAC5B,CAAC,EACF;GACE,QAAQ;GACR,SAAS,EAAE,gBAAgB,oBAAoB;GAChD,CACF;EAIH,MAAM,QADkB,OAAO,SACD,OAAO;AAErC,MAAI,SAAS,aAAa,OAAO;GAC/B,MAAM,qBAAqB,0BAA0B,QAAQ;AAC7D,SAAM,UAAU;IACd,GAAI,MAAM;IACV,GAAG;IACJ;;EAKH,IAAI;AACJ,MAAI;GACF,MAAM,cAAc,MAAM,QAAQ,MAAM;AACxC,WAAQ,oBAAoB,MAAM,YAAY;WACvC,OAAO;AACd,WAAQ,MAAM,6BAA6B,MAAM;AACjD,UAAO,IAAI,SACT,KAAK,UAAU;IACb,OAAO;IACP,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;IAChE,CAAC,EACF;IACE,QAAQ;IACR,SAAS,EAAE,gBAAgB,oBAAoB;IAChD,CACF;;EAGH,MAAM,SA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5C,UAAQ,MACN,gBACA,iBAAiB,QAAQ,MAAM,QAAQ,iBACxC;AACD,UAAQ,MAAM,kBAAkB;GAC9B,MAAM,iBAAiB,QAAQ,MAAM,OAAO;GAC5C,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;GAC/D,OAAO,iBAAiB,QAAQ,MAAM,QAAQ;GAC/C,CAAC;AAEF,SAAO,IAAI,SACT,KAAK,UAAU;GACb,OAAO;GACP,SAAS,iBAAiB,QAAQ,MAAM,UAAU;GACnD,CAAC,EACF;GACE,QAAQ;GACR,SAAS,EAAE,gBAAgB,oBAAoB;GAChD,CACF"}
1
+ {"version":3,"file":"handle-run.mjs","names":[],"sources":["../../src/handlers/handle-run.ts"],"sourcesContent":["import {\n AbstractAgent,\n RunAgentInput,\n RunAgentInputSchema,\n} from \"@ag-ui/client\";\nimport { A2UIMiddleware } from \"@ag-ui/a2ui-middleware\";\nimport { MCPAppsMiddleware } from \"@ag-ui/mcp-apps-middleware\";\nimport { EventEncoder } from \"@ag-ui/encoder\";\nimport { CopilotRuntime } from \"../runtime\";\nimport { extractForwardableHeaders } from \"./header-utils\";\n\ninterface RunAgentParameters {\n request: Request;\n runtime: CopilotRuntime;\n agentId: string;\n}\n\nexport async function handleRunAgent({\n runtime,\n request,\n agentId,\n}: RunAgentParameters) {\n try {\n const agents = await runtime.agents;\n\n // Check if the requested agent exists\n if (!agents[agentId]) {\n return new Response(\n JSON.stringify({\n error: \"Agent not found\",\n message: `Agent '${agentId}' does not exist`,\n }),\n {\n status: 404,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n\n const registeredAgent = agents[agentId] as AbstractAgent;\n const agent = registeredAgent.clone() as AbstractAgent;\n\n // Apply runtime-level A2UI middleware if configured\n if (runtime.a2ui) {\n const { agents: targetAgents, ...a2uiOptions } = runtime.a2ui;\n const shouldApply = !targetAgents || targetAgents.includes(agentId);\n if (\n shouldApply &&\n \"use\" in agent &&\n typeof (agent as any).use === \"function\"\n ) {\n (agent as any).use(new A2UIMiddleware(a2uiOptions));\n }\n }\n\n if (runtime.mcp) {\n const { agents: targetAgents, ...mcpOptions } = runtime.mcp;\n const shouldApply = !targetAgents || targetAgents.includes(agentId);\n if (\n shouldApply &&\n \"use\" in agent &&\n typeof (agent as any).use === \"function\"\n ) {\n (agent as any).use(new MCPAppsMiddleware(mcpOptions));\n }\n }\n\n if (agent && \"headers\" in agent) {\n const forwardableHeaders = extractForwardableHeaders(request);\n agent.headers = {\n ...(agent.headers as Record<string, string>),\n ...forwardableHeaders,\n };\n }\n\n // Parse and validate input BEFORE creating the stream\n // so we can return a proper error response\n let input: RunAgentInput;\n try {\n const requestBody = await request.json();\n input = RunAgentInputSchema.parse(requestBody);\n } catch (error) {\n console.error(\"Invalid run request body:\", error);\n return new Response(\n JSON.stringify({\n error: \"Invalid request body\",\n details: error instanceof Error ? error.message : String(error),\n }),\n {\n status: 400,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n\n const stream = new TransformStream();\n const writer = stream.writable.getWriter();\n const encoder = new EventEncoder();\n let streamClosed = false;\n\n agent.setMessages(input.messages);\n agent.setState(input.state);\n agent.threadId = input.threadId;\n\n // Process the agent run in the background\n (async () => {\n runtime.runner\n .run({\n threadId: input.threadId,\n agent,\n input,\n })\n .subscribe({\n next: async (event) => {\n if (!request.signal.aborted && !streamClosed) {\n try {\n await writer.write(encoder.encode(event));\n } catch (error) {\n if (error instanceof Error && error.name === \"AbortError\") {\n streamClosed = true;\n }\n }\n }\n },\n error: async (error) => {\n console.error(\"Error running agent:\", error);\n if (!streamClosed) {\n try {\n await writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n },\n complete: async () => {\n if (!streamClosed) {\n try {\n await writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n },\n });\n })().catch((error) => {\n console.error(\"Error running agent:\", error);\n console.error(\n \"Error stack:\",\n error instanceof Error ? error.stack : \"No stack trace\",\n );\n console.error(\"Error details:\", {\n name: error instanceof Error ? error.name : \"Unknown\",\n message: error instanceof Error ? error.message : String(error),\n cause: error instanceof Error ? error.cause : undefined,\n });\n if (!streamClosed) {\n try {\n writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n });\n\n // Return the SSE response\n return new Response(stream.readable, {\n status: 200,\n headers: {\n \"Content-Type\": \"text/event-stream\",\n \"Cache-Control\": \"no-cache\",\n Connection: \"keep-alive\",\n },\n });\n } catch (error) {\n console.error(\"Error running agent:\", error);\n console.error(\n \"Error stack:\",\n error instanceof Error ? error.stack : \"No stack trace\",\n );\n console.error(\"Error details:\", {\n name: error instanceof Error ? error.name : \"Unknown\",\n message: error instanceof Error ? error.message : String(error),\n cause: error instanceof Error ? error.cause : undefined,\n });\n\n return new Response(\n JSON.stringify({\n error: \"Failed to run agent\",\n message: error instanceof Error ? error.message : \"Unknown error\",\n }),\n {\n status: 500,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n}\n"],"mappings":";;;;;;;AAiBA,eAAsB,eAAe,EACnC,SACA,SACA,WACqB;AACrB,KAAI;EACF,MAAM,SAAS,MAAM,QAAQ;AAG7B,MAAI,CAAC,OAAO,SACV,QAAO,IAAI,SACT,KAAK,UAAU;GACb,OAAO;GACP,SAAS,UAAU,QAAQ;GAC5B,CAAC,EACF;GACE,QAAQ;GACR,SAAS,EAAE,gBAAgB,oBAAoB;GAChD,CACF;EAIH,MAAM,QADkB,OAAO,SACD,OAAO;AAGrC,MAAI,QAAQ,MAAM;GAChB,MAAM,EAAE,QAAQ,cAAc,GAAG,gBAAgB,QAAQ;AAEzD,QADoB,CAAC,gBAAgB,aAAa,SAAS,QAAQ,KAGjE,SAAS,SACT,OAAQ,MAAc,QAAQ,WAE9B,CAAC,MAAc,IAAI,IAAI,eAAe,YAAY,CAAC;;AAIvD,MAAI,QAAQ,KAAK;GACf,MAAM,EAAE,QAAQ,cAAc,GAAG,eAAe,QAAQ;AAExD,QADoB,CAAC,gBAAgB,aAAa,SAA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BAAe;aACT;;IAKb,CAAC;MACF,CAAC,OAAO,UAAU;AACpB,WAAQ,MAAM,wBAAwB,MAAM;AAC5C,WAAQ,MACN,gBACA,iBAAiB,QAAQ,MAAM,QAAQ,iBACxC;AACD,WAAQ,MAAM,kBAAkB;IAC9B,MAAM,iBAAiB,QAAQ,MAAM,OAAO;IAC5C,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;IAC/D,OAAO,iBAAiB,QAAQ,MAAM,QAAQ;IAC/C,CAAC;AACF,OAAI,CAAC,aACH,KAAI;AACF,WAAO,OAAO;AACd,mBAAe;WACT;IAIV;AAGF,SAAO,IAAI,SAAS,OAAO,UAAU;GACnC,QAAQ;GACR,SAAS;IACP,gBAAgB;IAChB,iBAAiB;IACjB,YAAY;IACb;GACF,CAAC;UACK,OAAO;AACd,UAAQ,MAAM,wBAAwB,MAAM;AAC5C,UAAQ,MACN,gBACA,iBAAiB,QAAQ,MAAM,QAAQ,iBACxC;AACD,UAAQ,MAAM,kBAAkB;GAC9B,MAAM,iBAAiB,QAAQ,MAAM,OAAO;GAC5C,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;GAC/D,OAAO,iBAAiB,QAAQ,MAAM,QAAQ;GAC/C,CAAC;AAEF,SAAO,IAAI,SACT,KAAK,UAAU;GACb,OAAO;GACP,SAAS,iBAAiB,QAAQ,MAAM,UAAU;GACnD,CAAC,EACF;GACE,QAAQ;GACR,SAAS,EAAE,gBAAgB,oBAAoB;GAChD,CACF"}
package/dist/package.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  //#region package.json
3
- var version = "1.52.2-next.2";
3
+ var version = "1.52.2-next.3";
4
4
 
5
5
  //#endregion
6
6
  Object.defineProperty(exports, 'version', {
package/dist/package.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "1.52.2-next.2";
2
+ var version = "1.52.2-next.3";
3
3
 
4
4
  //#endregion
5
5
  export { version };
package/dist/runtime.cjs CHANGED
@@ -12,12 +12,16 @@ var CopilotRuntime = class {
12
12
  beforeRequestMiddleware;
13
13
  afterRequestMiddleware;
14
14
  runner;
15
- constructor({ agents, transcriptionService, beforeRequestMiddleware, afterRequestMiddleware, runner }) {
15
+ a2ui;
16
+ mcp;
17
+ constructor({ agents, transcriptionService, beforeRequestMiddleware, afterRequestMiddleware, runner, a2ui, mcp }) {
16
18
  this.agents = agents;
17
19
  this.transcriptionService = transcriptionService;
18
20
  this.beforeRequestMiddleware = beforeRequestMiddleware;
19
21
  this.afterRequestMiddleware = afterRequestMiddleware;
20
22
  this.runner = runner ?? new require_in_memory.InMemoryAgentRunner();
23
+ this.a2ui = a2ui;
24
+ this.mcp = mcp;
21
25
  }
22
26
  };
23
27
 
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.cjs","names":["InMemoryAgentRunner"],"sources":["../src/runtime.ts"],"sourcesContent":["import { MaybePromise, NonEmptyRecord } from \"@copilotkitnext/shared\";\nimport { AbstractAgent } from \"@ag-ui/client\";\nimport pkg from \"../package.json\";\nimport type {\n BeforeRequestMiddleware,\n AfterRequestMiddleware,\n} from \"./middleware\";\nimport { TranscriptionService } from \"./transcription-service/transcription-service\";\nimport { AgentRunner } from \"./runner/agent-runner\";\nimport { InMemoryAgentRunner } from \"./runner/in-memory\";\n\nexport const VERSION = pkg.version;\n\n/**\n * Options used to construct a `CopilotRuntime` instance.\n */\nexport interface CopilotRuntimeOptions {\n /** Map of available agents (loaded lazily is fine). */\n agents: MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;\n /** The runner to use for running agents. */\n runner?: AgentRunner;\n /** Optional transcription service for audio processing. */\n transcriptionService?: TranscriptionService;\n /** Optional *before* middleware – callback function or webhook URL. */\n beforeRequestMiddleware?: BeforeRequestMiddleware;\n /** Optional *after* middleware – callback function or webhook URL. */\n afterRequestMiddleware?: AfterRequestMiddleware;\n}\n\n/**\n * Central runtime object passed to all request handlers.\n */\nexport class CopilotRuntime {\n public agents: CopilotRuntimeOptions[\"agents\"];\n public transcriptionService: CopilotRuntimeOptions[\"transcriptionService\"];\n public beforeRequestMiddleware: CopilotRuntimeOptions[\"beforeRequestMiddleware\"];\n public afterRequestMiddleware: CopilotRuntimeOptions[\"afterRequestMiddleware\"];\n public runner: AgentRunner;\n\n constructor({\n agents,\n transcriptionService,\n beforeRequestMiddleware,\n afterRequestMiddleware,\n runner,\n }: CopilotRuntimeOptions) {\n this.agents = agents;\n this.transcriptionService = transcriptionService;\n this.beforeRequestMiddleware = beforeRequestMiddleware;\n this.afterRequestMiddleware = afterRequestMiddleware;\n this.runner = runner ?? new InMemoryAgentRunner();\n }\n}\n"],"mappings":";;;;AAWA,MAAa;;;;AAqBb,IAAa,iBAAb,MAA4B;CAC1B,AAAO;CACP,AAAO;CACP,AAAO;CACP,AAAO;CACP,AAAO;CAEP,YAAY,EACV,QACA,sBACA,yBACA,wBACA,UACwB;AACxB,OAAK,SAAS;AACd,OAAK,uBAAuB;AAC5B,OAAK,0BAA0B;AAC/B,OAAK,yBAAyB;AAC9B,OAAK,SAAS,UAAU,IAAIA,uCAAqB"}
1
+ {"version":3,"file":"runtime.cjs","names":["InMemoryAgentRunner"],"sources":["../src/runtime.ts"],"sourcesContent":["import { MaybePromise, NonEmptyRecord } from \"@copilotkitnext/shared\";\nimport { AbstractAgent } from \"@ag-ui/client\";\nimport { MCPAppsMiddlewareConfig } from \"@ag-ui/mcp-apps-middleware\";\nimport { A2UIMiddlewareConfig } from \"@ag-ui/a2ui-middleware\";\nimport pkg from \"../package.json\";\nimport type {\n BeforeRequestMiddleware,\n AfterRequestMiddleware,\n} from \"./middleware\";\nimport { TranscriptionService } from \"./transcription-service/transcription-service\";\nimport { AgentRunner } from \"./runner/agent-runner\";\nimport { InMemoryAgentRunner } from \"./runner/in-memory\";\n\nexport const VERSION = pkg.version;\n\ninterface BaseCopilotRuntimeMiddlewareOptions {\n /** If set, middleware only applies to these named agents. Applies to all agents if omitted. */\n agents?: string[];\n}\n\ninterface CopilotRuntimeMiddlewares {\n /** Auto-apply A2UIMiddleware to agents at run time. */\n a2ui?: BaseCopilotRuntimeMiddlewareOptions & A2UIMiddlewareConfig;\n /** Auto-apply MCPAppsMiddleware to agents at run time. */\n mcp?: BaseCopilotRuntimeMiddlewareOptions & MCPAppsMiddlewareConfig;\n}\n\n/**\n * Options used to construct a `CopilotRuntime` instance.\n */\nexport interface CopilotRuntimeOptions extends CopilotRuntimeMiddlewares {\n /** Map of available agents (loaded lazily is fine). */\n agents: MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;\n /** The runner to use for running agents. */\n runner?: AgentRunner;\n /** Optional transcription service for audio processing. */\n transcriptionService?: TranscriptionService;\n /** Optional *before* middleware – callback function or webhook URL. */\n beforeRequestMiddleware?: BeforeRequestMiddleware;\n /** Optional *after* middleware – callback function or webhook URL. */\n afterRequestMiddleware?: AfterRequestMiddleware;\n}\n\n/**\n * Central runtime object passed to all request handlers.\n */\nexport class CopilotRuntime {\n public agents: CopilotRuntimeOptions[\"agents\"];\n public transcriptionService: CopilotRuntimeOptions[\"transcriptionService\"];\n public beforeRequestMiddleware: CopilotRuntimeOptions[\"beforeRequestMiddleware\"];\n public afterRequestMiddleware: CopilotRuntimeOptions[\"afterRequestMiddleware\"];\n public runner: AgentRunner;\n public a2ui: CopilotRuntimeOptions[\"a2ui\"];\n public mcp: CopilotRuntimeOptions[\"mcp\"];\n\n constructor({\n agents,\n transcriptionService,\n beforeRequestMiddleware,\n afterRequestMiddleware,\n runner,\n a2ui,\n mcp,\n }: CopilotRuntimeOptions) {\n this.agents = agents;\n this.transcriptionService = transcriptionService;\n this.beforeRequestMiddleware = beforeRequestMiddleware;\n this.afterRequestMiddleware = afterRequestMiddleware;\n this.runner = runner ?? new InMemoryAgentRunner();\n this.a2ui = a2ui;\n this.mcp = mcp;\n }\n}\n"],"mappings":";;;;AAaA,MAAa;;;;AAiCb,IAAa,iBAAb,MAA4B;CAC1B,AAAO;CACP,AAAO;CACP,AAAO;CACP,AAAO;CACP,AAAO;CACP,AAAO;CACP,AAAO;CAEP,YAAY,EACV,QACA,sBACA,yBACA,wBACA,QACA,MACA,OACwB;AACxB,OAAK,SAAS;AACd,OAAK,uBAAuB;AAC5B,OAAK,0BAA0B;AAC/B,OAAK,yBAAyB;AAC9B,OAAK,SAAS,UAAU,IAAIA,uCAAqB;AACjD,OAAK,OAAO;AACZ,OAAK,MAAM"}
@@ -3,13 +3,25 @@ import { TranscriptionService } from "./transcription-service/transcription-serv
3
3
  import { AgentRunner } from "./runner/agent-runner.cjs";
4
4
  import { MaybePromise, NonEmptyRecord } from "@copilotkitnext/shared";
5
5
  import { AbstractAgent } from "@ag-ui/client";
6
+ import { MCPAppsMiddlewareConfig } from "@ag-ui/mcp-apps-middleware";
7
+ import { A2UIMiddlewareConfig } from "@ag-ui/a2ui-middleware";
6
8
 
7
9
  //#region src/runtime.d.ts
8
10
  declare const VERSION: string;
11
+ interface BaseCopilotRuntimeMiddlewareOptions {
12
+ /** If set, middleware only applies to these named agents. Applies to all agents if omitted. */
13
+ agents?: string[];
14
+ }
15
+ interface CopilotRuntimeMiddlewares {
16
+ /** Auto-apply A2UIMiddleware to agents at run time. */
17
+ a2ui?: BaseCopilotRuntimeMiddlewareOptions & A2UIMiddlewareConfig;
18
+ /** Auto-apply MCPAppsMiddleware to agents at run time. */
19
+ mcp?: BaseCopilotRuntimeMiddlewareOptions & MCPAppsMiddlewareConfig;
20
+ }
9
21
  /**
10
22
  * Options used to construct a `CopilotRuntime` instance.
11
23
  */
12
- interface CopilotRuntimeOptions {
24
+ interface CopilotRuntimeOptions extends CopilotRuntimeMiddlewares {
13
25
  /** Map of available agents (loaded lazily is fine). */
14
26
  agents: MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;
15
27
  /** The runner to use for running agents. */
@@ -30,12 +42,16 @@ declare class CopilotRuntime {
30
42
  beforeRequestMiddleware: CopilotRuntimeOptions["beforeRequestMiddleware"];
31
43
  afterRequestMiddleware: CopilotRuntimeOptions["afterRequestMiddleware"];
32
44
  runner: AgentRunner;
45
+ a2ui: CopilotRuntimeOptions["a2ui"];
46
+ mcp: CopilotRuntimeOptions["mcp"];
33
47
  constructor({
34
48
  agents,
35
49
  transcriptionService,
36
50
  beforeRequestMiddleware,
37
51
  afterRequestMiddleware,
38
- runner
52
+ runner,
53
+ a2ui,
54
+ mcp
39
55
  }: CopilotRuntimeOptions);
40
56
  }
41
57
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.cts","names":[],"sources":["../src/runtime.ts"],"mappings":";;;;;;;cAWa,OAAA;;AAAb;;UAKiB,qBAAA;EALiB;EAOhC,MAAA,EAAQ,YAAA,CAAa,cAAA,CAAe,MAAA,SAAe,aAAA;EAFpC;EAIf,MAAA,GAAS,WAAA;;EAET,oBAAA,GAAuB,oBAAA;EAJa;EAMpC,uBAAA,GAA0B,uBAAA;EANlB;EAQR,sBAAA,GAAyB,sBAAA;AAAA;;;;cAMd,cAAA;EACJ,MAAA,EAAQ,qBAAA;EACR,oBAAA,EAAsB,qBAAA;EACtB,uBAAA,EAAyB,qBAAA;EACzB,sBAAA,EAAwB,qBAAA;EACxB,MAAA,EAAQ,WAAA;;IAGb,MAAA;IACA,oBAAA;IACA,uBAAA;IACA,sBAAA;IACA;EAAA,GACC,qBAAA;AAAA"}
1
+ {"version":3,"file":"runtime.d.cts","names":[],"sources":["../src/runtime.ts"],"mappings":";;;;;;;;;cAaa,OAAA;AAAA,UAEH,mCAAA;EAFG;EAIX,MAAA;AAAA;AAAA,UAGQ,yBAAA;EAPwB;EAShC,IAAA,GAAO,mCAAA,GAAsC,oBAAA;EAPF;EAS3C,GAAA,GAAM,mCAAA,GAAsC,uBAAA;AAAA;;AAPtC;;UAaS,qBAAA,SAA8B,yBAAA;EARtC;EAUP,MAAA,EAAQ,YAAA,CAAa,cAAA,CAAe,MAAA,SAAe,aAAA;EAR7C;EAUN,MAAA,GAAS,WAAA;EAV0D;EAYnE,oBAAA,GAAuB,oBAAA;EAdvB;EAgBA,uBAAA,GAA0B,uBAAA;EAhBmB;EAkB7C,sBAAA,GAAyB,sBAAA;AAAA;;;;cAMd,cAAA;EACJ,MAAA,EAAQ,qBAAA;EACR,oBAAA,EAAsB,qBAAA;EACtB,uBAAA,EAAyB,qBAAA;EACzB,sBAAA,EAAwB,qBAAA;EACxB,MAAA,EAAQ,WAAA;EACR,IAAA,EAAM,qBAAA;EACN,GAAA,EAAK,qBAAA;;IAGV,MAAA;IACA,oBAAA;IACA,uBAAA;IACA,sBAAA;IACA,MAAA;IACA,IAAA;IACA;EAAA,GACC,qBAAA;AAAA"}
@@ -3,13 +3,25 @@ import { TranscriptionService } from "./transcription-service/transcription-serv
3
3
  import { AgentRunner } from "./runner/agent-runner.mjs";
4
4
  import { AbstractAgent } from "@ag-ui/client";
5
5
  import { MaybePromise, NonEmptyRecord } from "@copilotkitnext/shared";
6
+ import { A2UIMiddlewareConfig } from "@ag-ui/a2ui-middleware";
7
+ import { MCPAppsMiddlewareConfig } from "@ag-ui/mcp-apps-middleware";
6
8
 
7
9
  //#region src/runtime.d.ts
8
10
  declare const VERSION: string;
11
+ interface BaseCopilotRuntimeMiddlewareOptions {
12
+ /** If set, middleware only applies to these named agents. Applies to all agents if omitted. */
13
+ agents?: string[];
14
+ }
15
+ interface CopilotRuntimeMiddlewares {
16
+ /** Auto-apply A2UIMiddleware to agents at run time. */
17
+ a2ui?: BaseCopilotRuntimeMiddlewareOptions & A2UIMiddlewareConfig;
18
+ /** Auto-apply MCPAppsMiddleware to agents at run time. */
19
+ mcp?: BaseCopilotRuntimeMiddlewareOptions & MCPAppsMiddlewareConfig;
20
+ }
9
21
  /**
10
22
  * Options used to construct a `CopilotRuntime` instance.
11
23
  */
12
- interface CopilotRuntimeOptions {
24
+ interface CopilotRuntimeOptions extends CopilotRuntimeMiddlewares {
13
25
  /** Map of available agents (loaded lazily is fine). */
14
26
  agents: MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;
15
27
  /** The runner to use for running agents. */
@@ -30,12 +42,16 @@ declare class CopilotRuntime {
30
42
  beforeRequestMiddleware: CopilotRuntimeOptions["beforeRequestMiddleware"];
31
43
  afterRequestMiddleware: CopilotRuntimeOptions["afterRequestMiddleware"];
32
44
  runner: AgentRunner;
45
+ a2ui: CopilotRuntimeOptions["a2ui"];
46
+ mcp: CopilotRuntimeOptions["mcp"];
33
47
  constructor({
34
48
  agents,
35
49
  transcriptionService,
36
50
  beforeRequestMiddleware,
37
51
  afterRequestMiddleware,
38
- runner
52
+ runner,
53
+ a2ui,
54
+ mcp
39
55
  }: CopilotRuntimeOptions);
40
56
  }
41
57
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/runtime.ts"],"mappings":";;;;;;;cAWa,OAAA;;AAAb;;UAKiB,qBAAA;EALiB;EAOhC,MAAA,EAAQ,YAAA,CAAa,cAAA,CAAe,MAAA,SAAe,aAAA;EAFpC;EAIf,MAAA,GAAS,WAAA;;EAET,oBAAA,GAAuB,oBAAA;EAJa;EAMpC,uBAAA,GAA0B,uBAAA;EANlB;EAQR,sBAAA,GAAyB,sBAAA;AAAA;;;;cAMd,cAAA;EACJ,MAAA,EAAQ,qBAAA;EACR,oBAAA,EAAsB,qBAAA;EACtB,uBAAA,EAAyB,qBAAA;EACzB,sBAAA,EAAwB,qBAAA;EACxB,MAAA,EAAQ,WAAA;;IAGb,MAAA;IACA,oBAAA;IACA,uBAAA;IACA,sBAAA;IACA;EAAA,GACC,qBAAA;AAAA"}
1
+ {"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/runtime.ts"],"mappings":";;;;;;;;;cAaa,OAAA;AAAA,UAEH,mCAAA;EAFG;EAIX,MAAA;AAAA;AAAA,UAGQ,yBAAA;EAPwB;EAShC,IAAA,GAAO,mCAAA,GAAsC,oBAAA;EAPF;EAS3C,GAAA,GAAM,mCAAA,GAAsC,uBAAA;AAAA;;AAPtC;;UAaS,qBAAA,SAA8B,yBAAA;EARtC;EAUP,MAAA,EAAQ,YAAA,CAAa,cAAA,CAAe,MAAA,SAAe,aAAA;EAR7C;EAUN,MAAA,GAAS,WAAA;EAV0D;EAYnE,oBAAA,GAAuB,oBAAA;EAdvB;EAgBA,uBAAA,GAA0B,uBAAA;EAhBmB;EAkB7C,sBAAA,GAAyB,sBAAA;AAAA;;;;cAMd,cAAA;EACJ,MAAA,EAAQ,qBAAA;EACR,oBAAA,EAAsB,qBAAA;EACtB,uBAAA,EAAyB,qBAAA;EACzB,sBAAA,EAAwB,qBAAA;EACxB,MAAA,EAAQ,WAAA;EACR,IAAA,EAAM,qBAAA;EACN,GAAA,EAAK,qBAAA;;IAGV,MAAA;IACA,oBAAA;IACA,uBAAA;IACA,sBAAA;IACA,MAAA;IACA,IAAA;IACA;EAAA,GACC,qBAAA;AAAA"}
package/dist/runtime.mjs CHANGED
@@ -12,12 +12,16 @@ var CopilotRuntime = class {
12
12
  beforeRequestMiddleware;
13
13
  afterRequestMiddleware;
14
14
  runner;
15
- constructor({ agents, transcriptionService, beforeRequestMiddleware, afterRequestMiddleware, runner }) {
15
+ a2ui;
16
+ mcp;
17
+ constructor({ agents, transcriptionService, beforeRequestMiddleware, afterRequestMiddleware, runner, a2ui, mcp }) {
16
18
  this.agents = agents;
17
19
  this.transcriptionService = transcriptionService;
18
20
  this.beforeRequestMiddleware = beforeRequestMiddleware;
19
21
  this.afterRequestMiddleware = afterRequestMiddleware;
20
22
  this.runner = runner ?? new InMemoryAgentRunner();
23
+ this.a2ui = a2ui;
24
+ this.mcp = mcp;
21
25
  }
22
26
  };
23
27
 
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.mjs","names":["pkg.version"],"sources":["../src/runtime.ts"],"sourcesContent":["import { MaybePromise, NonEmptyRecord } from \"@copilotkitnext/shared\";\nimport { AbstractAgent } from \"@ag-ui/client\";\nimport pkg from \"../package.json\";\nimport type {\n BeforeRequestMiddleware,\n AfterRequestMiddleware,\n} from \"./middleware\";\nimport { TranscriptionService } from \"./transcription-service/transcription-service\";\nimport { AgentRunner } from \"./runner/agent-runner\";\nimport { InMemoryAgentRunner } from \"./runner/in-memory\";\n\nexport const VERSION = pkg.version;\n\n/**\n * Options used to construct a `CopilotRuntime` instance.\n */\nexport interface CopilotRuntimeOptions {\n /** Map of available agents (loaded lazily is fine). */\n agents: MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;\n /** The runner to use for running agents. */\n runner?: AgentRunner;\n /** Optional transcription service for audio processing. */\n transcriptionService?: TranscriptionService;\n /** Optional *before* middleware – callback function or webhook URL. */\n beforeRequestMiddleware?: BeforeRequestMiddleware;\n /** Optional *after* middleware – callback function or webhook URL. */\n afterRequestMiddleware?: AfterRequestMiddleware;\n}\n\n/**\n * Central runtime object passed to all request handlers.\n */\nexport class CopilotRuntime {\n public agents: CopilotRuntimeOptions[\"agents\"];\n public transcriptionService: CopilotRuntimeOptions[\"transcriptionService\"];\n public beforeRequestMiddleware: CopilotRuntimeOptions[\"beforeRequestMiddleware\"];\n public afterRequestMiddleware: CopilotRuntimeOptions[\"afterRequestMiddleware\"];\n public runner: AgentRunner;\n\n constructor({\n agents,\n transcriptionService,\n beforeRequestMiddleware,\n afterRequestMiddleware,\n runner,\n }: CopilotRuntimeOptions) {\n this.agents = agents;\n this.transcriptionService = transcriptionService;\n this.beforeRequestMiddleware = beforeRequestMiddleware;\n this.afterRequestMiddleware = afterRequestMiddleware;\n this.runner = runner ?? new InMemoryAgentRunner();\n }\n}\n"],"mappings":";;;;AAWA,MAAa,UAAUA;;;;AAqBvB,IAAa,iBAAb,MAA4B;CAC1B,AAAO;CACP,AAAO;CACP,AAAO;CACP,AAAO;CACP,AAAO;CAEP,YAAY,EACV,QACA,sBACA,yBACA,wBACA,UACwB;AACxB,OAAK,SAAS;AACd,OAAK,uBAAuB;AAC5B,OAAK,0BAA0B;AAC/B,OAAK,yBAAyB;AAC9B,OAAK,SAAS,UAAU,IAAI,qBAAqB"}
1
+ {"version":3,"file":"runtime.mjs","names":["pkg.version"],"sources":["../src/runtime.ts"],"sourcesContent":["import { MaybePromise, NonEmptyRecord } from \"@copilotkitnext/shared\";\nimport { AbstractAgent } from \"@ag-ui/client\";\nimport { MCPAppsMiddlewareConfig } from \"@ag-ui/mcp-apps-middleware\";\nimport { A2UIMiddlewareConfig } from \"@ag-ui/a2ui-middleware\";\nimport pkg from \"../package.json\";\nimport type {\n BeforeRequestMiddleware,\n AfterRequestMiddleware,\n} from \"./middleware\";\nimport { TranscriptionService } from \"./transcription-service/transcription-service\";\nimport { AgentRunner } from \"./runner/agent-runner\";\nimport { InMemoryAgentRunner } from \"./runner/in-memory\";\n\nexport const VERSION = pkg.version;\n\ninterface BaseCopilotRuntimeMiddlewareOptions {\n /** If set, middleware only applies to these named agents. Applies to all agents if omitted. */\n agents?: string[];\n}\n\ninterface CopilotRuntimeMiddlewares {\n /** Auto-apply A2UIMiddleware to agents at run time. */\n a2ui?: BaseCopilotRuntimeMiddlewareOptions & A2UIMiddlewareConfig;\n /** Auto-apply MCPAppsMiddleware to agents at run time. */\n mcp?: BaseCopilotRuntimeMiddlewareOptions & MCPAppsMiddlewareConfig;\n}\n\n/**\n * Options used to construct a `CopilotRuntime` instance.\n */\nexport interface CopilotRuntimeOptions extends CopilotRuntimeMiddlewares {\n /** Map of available agents (loaded lazily is fine). */\n agents: MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;\n /** The runner to use for running agents. */\n runner?: AgentRunner;\n /** Optional transcription service for audio processing. */\n transcriptionService?: TranscriptionService;\n /** Optional *before* middleware – callback function or webhook URL. */\n beforeRequestMiddleware?: BeforeRequestMiddleware;\n /** Optional *after* middleware – callback function or webhook URL. */\n afterRequestMiddleware?: AfterRequestMiddleware;\n}\n\n/**\n * Central runtime object passed to all request handlers.\n */\nexport class CopilotRuntime {\n public agents: CopilotRuntimeOptions[\"agents\"];\n public transcriptionService: CopilotRuntimeOptions[\"transcriptionService\"];\n public beforeRequestMiddleware: CopilotRuntimeOptions[\"beforeRequestMiddleware\"];\n public afterRequestMiddleware: CopilotRuntimeOptions[\"afterRequestMiddleware\"];\n public runner: AgentRunner;\n public a2ui: CopilotRuntimeOptions[\"a2ui\"];\n public mcp: CopilotRuntimeOptions[\"mcp\"];\n\n constructor({\n agents,\n transcriptionService,\n beforeRequestMiddleware,\n afterRequestMiddleware,\n runner,\n a2ui,\n mcp,\n }: CopilotRuntimeOptions) {\n this.agents = agents;\n this.transcriptionService = transcriptionService;\n this.beforeRequestMiddleware = beforeRequestMiddleware;\n this.afterRequestMiddleware = afterRequestMiddleware;\n this.runner = runner ?? new InMemoryAgentRunner();\n this.a2ui = a2ui;\n this.mcp = mcp;\n }\n}\n"],"mappings":";;;;AAaA,MAAa,UAAUA;;;;AAiCvB,IAAa,iBAAb,MAA4B;CAC1B,AAAO;CACP,AAAO;CACP,AAAO;CACP,AAAO;CACP,AAAO;CACP,AAAO;CACP,AAAO;CAEP,YAAY,EACV,QACA,sBACA,yBACA,wBACA,QACA,MACA,OACwB;AACxB,OAAK,SAAS;AACd,OAAK,uBAAuB;AAC5B,OAAK,0BAA0B;AAC/B,OAAK,yBAAyB;AAC9B,OAAK,SAAS,UAAU,IAAI,qBAAqB;AACjD,OAAK,OAAO;AACZ,OAAK,MAAM"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@copilotkitnext/runtime",
3
- "version": "1.52.2-next.2",
3
+ "version": "1.52.2-next.3",
4
4
  "description": "Server-side runtime package for CopilotKit2",
5
5
  "main": "./dist/index.cjs",
6
6
  "types": "./dist/index.d.cts",
@@ -29,26 +29,28 @@
29
29
  "tsdown": "^0.20.3",
30
30
  "typescript": "5.8.2",
31
31
  "vitest": "^3.0.5",
32
- "@copilotkitnext/eslint-config": "1.52.2-next.2",
33
- "@copilotkitnext/typescript-config": "1.52.2-next.2"
32
+ "@copilotkitnext/eslint-config": "1.52.2-next.3",
33
+ "@copilotkitnext/typescript-config": "1.52.2-next.3"
34
34
  },
35
35
  "dependencies": {
36
- "@ag-ui/client": "0.0.46",
37
- "@ag-ui/core": "0.0.46",
38
- "@ag-ui/encoder": "0.0.46",
36
+ "@ag-ui/a2ui-middleware": "0.0.2",
37
+ "@ag-ui/client": "0.0.47",
38
+ "@ag-ui/core": "0.0.47",
39
+ "@ag-ui/encoder": "0.0.47",
40
+ "@ag-ui/mcp-apps-middleware": "0.0.2",
39
41
  "cors": "^2.8.5",
40
42
  "phoenix": "^1.8.4",
41
43
  "ws": "^8.18.0",
42
44
  "express": "^4.21.2",
43
45
  "hono": "^4.11.4",
44
46
  "rxjs": "7.8.1",
45
- "@copilotkitnext/shared": "1.52.2-next.2"
47
+ "@copilotkitnext/shared": "1.52.2-next.3"
46
48
  },
47
49
  "peerDependencies": {
48
- "@ag-ui/client": "0.0.46",
49
- "@ag-ui/core": "0.0.46",
50
- "@ag-ui/encoder": "0.0.46",
51
- "@copilotkitnext/shared": "1.52.2-next.2"
50
+ "@ag-ui/client": "0.0.47",
51
+ "@ag-ui/core": "0.0.47",
52
+ "@ag-ui/encoder": "0.0.47",
53
+ "@copilotkitnext/shared": "1.52.2-next.3"
52
54
  },
53
55
  "peerDependenciesMeta": {},
54
56
  "engines": {