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;" />

209 lines (207 loc) 7.96 kB
require("reflect-metadata"); const require_runtime = require('../../../../_virtual/_rolldown/runtime.cjs'); const require_telemetry_client = require('../../telemetry/telemetry-client.cjs'); const require_json_response = require('../shared/json-response.cjs'); const require_thread_names = require('./thread-names.cjs'); const require_resolve_intelligence_user = require('../shared/resolve-intelligence-user.cjs'); let _copilotkit_shared = require("@copilotkit/shared"); let _ag_ui_client = require("@ag-ui/client"); //#region src/v2/runtime/handlers/intelligence/run.ts /** * Builds browser-facing realtime connection metadata owned by the runtime. */ function buildRealtimeConnectionInfo(params) { return { clientUrl: params.clientUrl, topic: `thread:${params.threadId}` }; } function hasRunnerStartupBoundary(runner) { return typeof runner.runWithStartupBoundary === "function" && (Object.prototype.hasOwnProperty.call(runner, "runWithStartupBoundary") || Object.prototype.hasOwnProperty.call(runner, "threads")); } async function handleIntelligenceRun({ runtime, request, agentId, agent, input }) { if (!runtime.intelligence) return Response.json({ error: "Intelligence not configured", message: "Intelligence mode requires a CopilotKitIntelligence" }, { status: 500 }); const user = await require_resolve_intelligence_user.resolveIntelligenceUser({ runtime, request }); if (require_json_response.isHandlerResponse(user)) return user; const userId = user.id; try { const { thread, created } = await runtime.intelligence.getOrCreateThread({ threadId: input.threadId, userId, agentId }); if (created && runtime.generateThreadNames && !thread.name?.trim()) require_thread_names.generateThreadNameForNewThread({ runtime, request, agentId, sourceInput: input, thread, userId }).catch((nameError) => { _copilotkit_shared.logger.error("Failed to generate thread name:", nameError); }); } catch (error) { _copilotkit_shared.logger.error("Failed to get or create thread:", error); return Response.json({ error: "Failed to initialize thread" }, { status: 502 }); } let canonicalThreadId = input.threadId; let canonicalRunId = input.runId; let joinToken; try { const lockResult = await runtime.intelligenceacquireThreadLock({ threadId: input.threadId, runId: input.runId, userId, agentId, ...runtime.lockKeyPrefix !== void 0 ? { lockKeyPrefix: runtime.lockKeyPrefix } : {}, ttlSeconds: runtime.lockTtlSeconds }); canonicalThreadId = lockResult.threadId; canonicalRunId = lockResult.runId; joinToken = lockResult.joinToken; } catch (error) { _copilotkit_shared.logger.error("Thread lock denied:", error); return Response.json({ error: "Thread lock denied" }, { status: 409 }); } const cleanupLock = (reason) => runtime.intelligencecleanupThreadLock({ threadId: canonicalThreadId || input.threadId, runId: canonicalRunId || input.runId }).catch((cleanupError) => { _copilotkit_shared.logger.error({ err: cleanupError, reason }, "Failed to cleanup thread lock"); }); if (!canonicalThreadId || !canonicalRunId || !joinToken) { await cleanupLock("malformed-lock-response"); return Response.json({ error: "Run connection credentials not available", message: "Intelligence platform did not return canonical threadId, runId, and joinToken" }, { status: 502 }); } const upstreamAuth = input.forwardedProps?.auth ?? {}; const copilotkitIntelligenceAuth = runtime.intelligence.ɵisMcpServerEnabled?.() ? { copilotkitIntelligence: { userId, apiKey: runtime.intelligencegetApiKey(), mcpUrl: `${runtime.intelligence.ɵgetApiUrl()}/mcp` } } : {}; const mergedAuth = { ...upstreamAuth, ...copilotkitIntelligenceAuth }; const canonicalInput = { ...input, threadId: canonicalThreadId, runId: canonicalRunId, forwardedProps: { ...input.forwardedProps, ...Object.keys(mergedAuth).length > 0 ? { auth: mergedAuth } : {} } }; let persistedInputMessages; if (Array.isArray(input.messages)) try { const history = await runtime.intelligence.getThreadMessages({ threadId: canonicalThreadId }); const historicMessageIds = new Set(history.messages.map((message) => message.id)); persistedInputMessages = input.messages.filter((message) => !historicMessageIds.has(message.id)); } catch (error) { _copilotkit_shared.logger.error("Thread history lookup failed:", error); await cleanupLock("thread-history-lookup-failed"); return Response.json({ error: "Thread history lookup failed" }, { status: 502 }); } require_telemetry_client.default.capture("oss.runtime.agent_execution_stream_started", {}); let heartbeatTimer; heartbeatTimer = setInterval(() => { runtime.intelligencerenewThreadLock({ threadId: canonicalThreadId, runId: canonicalRunId, ttlSeconds: runtime.lockTtlSeconds, ...runtime.lockKeyPrefix !== void 0 ? { lockKeyPrefix: runtime.lockKeyPrefix } : {} }).catch((err) => { _copilotkit_shared.logger.error("Failed to renew thread lock:", err); clearHeartbeat(); try { agent.abortRun(); } catch (abortError) { _copilotkit_shared.logger.error("Failed to abort agent after lock renewal failure:", abortError); } }); }, runtime.lockHeartbeatIntervalSeconds * 1e3); const clearHeartbeat = () => { if (heartbeatTimer !== void 0) { clearInterval(heartbeatTimer); heartbeatTimer = void 0; } }; const runStarted = { current: false }; let immediateStartupErrorMessage; let immediateStartupCleanup; const runRequest = { threadId: canonicalThreadId, agent, input: canonicalInput, ...persistedInputMessages !== void 0 ? { persistedInputMessages } : {} }; try { const runStart = hasRunnerStartupBoundary(runtime.runner) ? runtime.runner.runWithStartupBoundary(runRequest) : { events: runtime.runner.run(runRequest), startup: Promise.resolve() }; runStart.events.subscribe({ next: (event) => { if (event.type === _ag_ui_client.EventType.RUN_STARTED) runStarted.current = true; if (event.type === _ag_ui_client.EventType.RUN_ERROR && !runStarted.current) { clearHeartbeat(); immediateStartupErrorMessage = "message" in event && typeof event.message === "string" ? event.message : "Runner failed before the run started"; immediateStartupCleanup = cleanupLock("runner-start-failed"); } }, error: (error) => { clearHeartbeat(); if (!runStarted.current) { immediateStartupErrorMessage = error instanceof Error ? error.message : String(error); immediateStartupCleanup = cleanupLock("runner-start-error"); } else cleanupLock("runner-error"); require_telemetry_client.default.capture("oss.runtime.agent_execution_stream_errored", { error: error instanceof Error ? error.message : String(error) }); _copilotkit_shared.logger.error("Error running agent:", error); }, complete: () => { clearHeartbeat(); require_telemetry_client.default.capture("oss.runtime.agent_execution_stream_ended", {}); } }); await runStart.startup; } catch (error) { clearHeartbeat(); await (immediateStartupCleanup ?? cleanupLock("runner-start-threw")); _copilotkit_shared.logger.error("Error starting agent runner:", error); return Response.json({ error: "Failed to start runner", message: error instanceof Error ? error.message : String(error) }, { status: 502 }); } if (immediateStartupErrorMessage) { await immediateStartupCleanup; return Response.json({ error: "Failed to start runner", message: immediateStartupErrorMessage }, { status: 502 }); } return Response.json({ threadId: canonicalThreadId, runId: canonicalRunId, joinToken, realtime: buildRealtimeConnectionInfo({ clientUrl: runtime.intelligencegetClientWsUrl(), threadId: canonicalThreadId }) }, { headers: { "Cache-Control": "no-cache" } }); } //#endregion exports.handleIntelligenceRun = handleIntelligenceRun; //# sourceMappingURL=run.cjs.map