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

84 lines (82 loc) 3.64 kB
require("reflect-metadata"); const require_runtime = require('../../../../_virtual/_rolldown/runtime.cjs'); const require_runtime$1 = require('../../core/runtime.cjs'); const require_client = require('../../intelligence-platform/client.cjs'); const require_json_response = require('../shared/json-response.cjs'); const require_resolve_intelligence_user = require('../shared/resolve-intelligence-user.cjs'); let _copilotkit_shared = require("@copilotkit/shared"); //#region src/v2/runtime/handlers/intelligence/annotate.ts async function parseJsonBody(request) { try { return await request.json(); } catch (error) { _copilotkit_shared.logger.error({ err: error }, "Malformed JSON in annotate body"); return require_json_response.errorResponse("Invalid request body", 400); } } function requireIntelligenceRuntime(runtime) { if (!require_runtime$1.isIntelligenceRuntime(runtime)) return require_json_response.errorResponse("Missing CopilotKitIntelligence configuration. annotate requires a CopilotKitIntelligence instance to be provided in CopilotRuntime options.", 422); return runtime; } function isNonEmptyString(value) { return typeof value === "string" && value.length > 0; } /** * `POST /annotate` handler. * * Three-tier flow: * recordAnnotation() (frontend lib; called by useLearnFromUserAction / useLearningContainers) * → POST ${runtimeUrl}/annotate * → this handler resolves the Intel user from BFF auth * → intelligence.annotate(...) * → PUT ${apiUrl}/connector/annotate/:clientEventId * * The frontend hook may auto-generate a UUID `clientEventId` per call * so retries are idempotent end-to-end (the platform collapses to the * original row). */ async function handleAnnotate({ runtime, request }) { const intelligenceRuntime = requireIntelligenceRuntime(runtime); if (require_json_response.isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime; const body = await parseJsonBody(request); if (require_json_response.isHandlerResponse(body)) return body; const user = await require_resolve_intelligence_user.resolveIntelligenceUser({ runtime: intelligenceRuntime, request }); if (require_json_response.isHandlerResponse(user)) return user; const parsed = parseAnnotateBody(body); if (require_json_response.isHandlerResponse(parsed)) return parsed; try { const result = await intelligenceRuntime.intelligence.annotate({ userId: user.id, threadId: parsed.threadId, type: parsed.type, payload: parsed.payload, clientEventId: parsed.clientEventId, occurredAt: parsed.occurredAt }); return new Response(JSON.stringify(result), { status: 200, headers: { "Content-Type": "application/json" } }); } catch (err) { _copilotkit_shared.logger.error({ err }, "annotate: platform call failed"); if (err instanceof require_client.PlatformRequestError && err.status >= 400 && err.status < 500) return require_json_response.errorResponse(err.message, err.status); return require_json_response.errorResponse("Failed to annotate", 502); } } function parseAnnotateBody(body) { if (!isNonEmptyString(body.threadId)) return require_json_response.errorResponse("Valid threadId is required", 400); if (!isNonEmptyString(body.type)) return require_json_response.errorResponse("Valid type is required", 400); return { type: body.type, payload: body.payload, threadId: body.threadId, clientEventId: isNonEmptyString(body.clientEventId) ? body.clientEventId : void 0, occurredAt: isNonEmptyString(body.occurredAt) ? body.occurredAt : void 0 }; } //#endregion exports.handleAnnotate = handleAnnotate; //# sourceMappingURL=annotate.cjs.map