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

244 lines (242 loc) 9.42 kB
require("reflect-metadata"); const require_runtime = require('../../../../_virtual/_rolldown/runtime.cjs'); const require_telemetry_client = require('../../telemetry/telemetry-client.cjs'); const require_runtime_error_reporter = require('../../core/runtime-error-reporter.cjs'); const require_learning = require('../../core/learning.cjs'); const require_json_response = require('../shared/json-response.cjs'); const require_intelligence_utils = require('../shared/intelligence-utils.cjs'); const require_resolve_intelligence_user = require('../shared/resolve-intelligence-user.cjs'); const require_thread_names = require('./thread-names.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, startTime }) { 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; let learningContainerId; try { learningContainerId = await require_learning.resolveLearningContainerId(runtime.learning, { surface: "web", request, threadId: input.threadId, runId: input.runId, agentId, userId }); } catch (error) { _copilotkit_shared.logger.error("Failed to resolve Learning Container:", error); return Response.json({ error: "Failed to resolve Learning Container" }, { status: 500 }); } try { const { thread, created } = await runtime.intelligence.getOrCreateThread({ threadId: input.threadId, userId, agentId, ...learningContainerId !== void 0 ? { learningContainerId } : {} }); 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); const platformStatus = require_intelligence_utils.getPlatformErrorStatus(error); return Response.json({ error: "Failed to initialize thread" }, { status: platformStatus !== void 0 && platformStatus >= 400 && platformStatus < 500 ? platformStatus : 502 }); } let canonicalThreadId = input.threadId; let canonicalRunId = input.runId; let joinToken; try { const lockResult = await runtime.intelligence.ɵacquireThreadLock({ threadId: input.threadId, runId: input.runId, userId, agentId, ...learningContainerId !== void 0 ? { learningContainerId } : {}, ...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); const platformStatus = require_intelligence_utils.getPlatformErrorStatus(error); return Response.json({ error: "Thread lock denied" }, { status: platformStatus === 409 ? 409 : 502 }); } const cleanupLock = (reason) => runtime.intelligence.ɵcleanupThreadLock({ 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 canonicalInput = { ...input, threadId: canonicalThreadId, runId: canonicalRunId }; let persistedInputMessages; if (Array.isArray(input.messages)) try { const history = await runtime.intelligence.getThreadMessages({ threadId: canonicalThreadId, userId }); 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 heartbeatStopped = false; let heartbeatTimer; heartbeatTimer = setInterval(() => { runtime.intelligence.ɵrenewThreadLock({ threadId: canonicalThreadId, runId: canonicalRunId, ttlSeconds: runtime.lockTtlSeconds, ...runtime.lockKeyPrefix !== void 0 ? { lockKeyPrefix: runtime.lockKeyPrefix } : {} }).catch((err) => { if (heartbeatStopped) return; _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 = () => { heartbeatStopped = true; 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 } : {} }; const runtimeErrorReporter = require_runtime_error_reporter.getRuntimeErrorReporter(runtime); let agentErrorReported = false; const reportAgentError = (error, phase) => { if (agentErrorReported) return; agentErrorReported = true; runtimeErrorReporter?.report({ request, error, operation: "agent.run", agentId, threadId: canonicalThreadId, runId: canonicalRunId, phase, startTime }); }; 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) { const message = "message" in event && typeof event.message === "string" ? event.message : "Runner failed before the run started"; reportAgentError(new Error(message), runStarted.current ? "intelligence.subscription" : "intelligence.startup"); if (!runStarted.current) { clearHeartbeat(); immediateStartupErrorMessage = message; immediateStartupCleanup = cleanupLock("runner-start-failed"); } } }, error: (error) => { reportAgentError(error, runStarted.current ? "intelligence.subscription" : "intelligence.startup"); 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) { reportAgentError(error, "intelligence.startup"); 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.intelligence.ɵgetClientWsUrl(), threadId: canonicalThreadId }) }, { headers: { "Cache-Control": "no-cache" } }); } //#endregion exports.handleIntelligenceRun = handleIntelligenceRun; //# sourceMappingURL=run.cjs.map