UNPKG

@copilotkit/runtime

Version:

<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />

86 lines (84 loc) 3.29 kB
import "reflect-metadata"; import { resolveAgents } from "../../core/runtime.mjs"; import { OpenGenerativeUIMiddleware } from "../../open-generative-ui-middleware.mjs"; import { extractForwardableHeaders } from "../header-utils.mjs"; import { logger } from "@copilotkit/shared"; import { RunAgentInputSchema } from "@ag-ui/client"; import { A2UIMiddleware } from "@ag-ui/a2ui-middleware"; import { MCPAppsMiddleware } from "@ag-ui/mcp-apps-middleware"; //#region src/v2/runtime/handlers/shared/agent-utils.ts async function cloneAgentForRequest(runtime, agentId, request) { const agents = await resolveAgents(runtime.agents, request); if (!agents[agentId]) return new Response(JSON.stringify({ error: "Agent not found", message: `Agent '${agentId}' does not exist` }), { status: 404, headers: { "Content-Type": "application/json" } }); return agents[agentId].clone(); } function configureAgentForRequest(params) { const { runtime, request, agentId } = params; const agent = params.agent; if (runtime.a2ui) { const { agents: targetAgents, ...a2uiOptions } = runtime.a2ui; if ((!targetAgents || targetAgents.includes(agentId)) && typeof agent.use === "function") agent.use(new A2UIMiddleware(a2uiOptions)); } if (runtime.mcpApps?.servers?.length) { const mcpServers = runtime.mcpApps.servers.filter((server) => !server.agentId || server.agentId === agentId).map((server) => { const mcpServer = { ...server }; delete mcpServer.agentId; return mcpServer; }); if (mcpServers.length > 0 && typeof agent.use === "function") agent.use(new MCPAppsMiddleware({ mcpServers })); } if (runtime.openGenerativeUI) { const config = runtime.openGenerativeUI; const targetAgents = typeof config === "object" ? config.agents : void 0; if ((!targetAgents || targetAgents.includes(agentId)) && typeof agent.use === "function") agent.use(new OpenGenerativeUIMiddleware()); } agent.headers = { ...agent.headers, ...extractForwardableHeaders(request) }; } async function parseRunRequest(request) { try { const requestBody = await request.json(); return RunAgentInputSchema.parse(requestBody); } catch (error) { logger.error("Invalid run request body:", error); return new Response(JSON.stringify({ error: "Invalid request body", details: error instanceof Error ? error.message : String(error) }), { status: 400, headers: { "Content-Type": "application/json" } }); } } async function parseConnectRequest(request) { try { const requestBody = await request.json(); const input = RunAgentInputSchema.parse(requestBody); let lastSeenEventId = null; if ("lastSeenEventId" in requestBody && (typeof requestBody.lastSeenEventId === "string" || requestBody.lastSeenEventId === null)) lastSeenEventId = requestBody.lastSeenEventId ?? null; return { input, lastSeenEventId }; } catch (error) { logger.error("Invalid connect request body:", error); return new Response(JSON.stringify({ error: "Invalid request body", details: error instanceof Error ? error.message : String(error) }), { status: 400, headers: { "Content-Type": "application/json" } }); } } //#endregion export { cloneAgentForRequest, configureAgentForRequest, parseConnectRequest, parseRunRequest }; //# sourceMappingURL=agent-utils.mjs.map