@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;" />
72 lines (70 loc) • 2.57 kB
JavaScript
import "reflect-metadata";
import telemetry from "../telemetry/telemetry-client.mjs";
import { isIntelligenceRuntime } from "../core/runtime.mjs";
import { cloneAgentForRequest, configureAgentForRequest, parseRunRequest } from "./shared/agent-utils.mjs";
import { handleIntelligenceRun } from "./intelligence/run.mjs";
import { handleSseRun } from "./sse/run.mjs";
//#region src/v2/runtime/handlers/handle-run.ts
async function handleRunAgent({ runtime, request, agentId }) {
telemetry.capture("oss.runtime.copilot_request_created", {
"cloud.guardrails.enabled": false,
requestType: "run",
"cloud.api_key_provided": !!request.headers.get("x-copilotcloud-public-api-key"),
...request.headers.get("x-copilotcloud-public-api-key") ? { "cloud.public_api_key": request.headers.get("x-copilotcloud-public-api-key") } : {}
});
try {
const agent = await cloneAgentForRequest(runtime, agentId, request);
if (agent instanceof Response) return agent;
agent.agentId = agentId;
configureAgentForRequest({
runtime,
request,
agentId,
agent
});
if (runtime.licenseChecker && !runtime.licenseChecker.checkFeature("agents")) console.warn("[CopilotKit Runtime] Warning: \"agents\" feature is not licensed. Visit copilotkit.ai/pricing");
const input = await parseRunRequest(request);
if (input instanceof Response) return input;
agent.setMessages(input.messages);
agent.setState(input.state);
agent.threadId = input.threadId;
if (runtime.debug?.lifecycle && runtime.debugLogger) runtime.debugLogger.debug({
agentName: agentId,
threadId: input.threadId
}, "Agent run started");
if (isIntelligenceRuntime(runtime)) return handleIntelligenceRun({
runtime,
request,
agentId,
agent,
input
});
return handleSseRun({
runtime,
request,
agent,
input,
agentId,
debug: runtime.debug,
logger: runtime.debugLogger
});
} catch (error) {
console.error("Error running agent:", error);
console.error("Error stack:", error instanceof Error ? error.stack : "No stack trace");
console.error("Error details:", {
name: error instanceof Error ? error.name : "Unknown",
message: error instanceof Error ? error.message : String(error),
cause: error instanceof Error ? error.cause : void 0
});
return new Response(JSON.stringify({
error: "Failed to run agent",
message: error instanceof Error ? error.message : "Unknown error"
}), {
status: 500,
headers: { "Content-Type": "application/json" }
});
}
}
//#endregion
export { handleRunAgent };
//# sourceMappingURL=handle-run.mjs.map