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

83 lines (81 loc) 3.22 kB
import "reflect-metadata"; import { isIntelligenceRuntime } from "../../core/runtime.mjs"; import { PlatformRequestError } from "../../intelligence-platform/client.mjs"; import { errorResponse, isHandlerResponse } from "../shared/json-response.mjs"; import { resolveIntelligenceUser } from "../shared/resolve-intelligence-user.mjs"; import { logger } from "@copilotkit/shared"; //#region src/v2/runtime/handlers/intelligence/annotate.ts async function parseJsonBody(request) { try { return await request.json(); } catch (error) { logger.error({ err: error }, "Malformed JSON in annotate body"); return errorResponse("Invalid request body", 400); } } function requireIntelligenceRuntime(runtime) { if (!isIntelligenceRuntime(runtime)) return 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 (isHandlerResponse(intelligenceRuntime)) return intelligenceRuntime; const body = await parseJsonBody(request); if (isHandlerResponse(body)) return body; const user = await resolveIntelligenceUser({ runtime: intelligenceRuntime, request }); if (isHandlerResponse(user)) return user; const parsed = parseAnnotateBody(body); if (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) { logger.error({ err }, "annotate: platform call failed"); if (err instanceof PlatformRequestError && err.status >= 400 && err.status < 500) return errorResponse(err.message, err.status); return errorResponse("Failed to annotate", 502); } } function parseAnnotateBody(body) { if (!isNonEmptyString(body.threadId)) return errorResponse("Valid threadId is required", 400); if (!isNonEmptyString(body.type)) return 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 export { handleAnnotate }; //# sourceMappingURL=annotate.mjs.map