@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;" />
56 lines (54 loc) • 2.44 kB
JavaScript
import "reflect-metadata";
import { hasLearningContainerConfiguration } from "../core/learning.mjs";
import { isIntelligenceRuntime } from "../core/runtime.mjs";
import { PlatformRequestError } from "../intelligence-platform/client.mjs";
import { resolveIntelligenceUser } from "./shared/resolve-intelligence-user.mjs";
import { parseInspectorLearningRequestV1, parseInspectorLearningSnapshotV1 } from "@copilotkit/shared";
//#region src/v2/runtime/handlers/handle-inspector-learning.ts
const headers = {
"Cache-Control": "no-store, private",
"Content-Type": "application/json"
};
const errorResponse = (status, message) => new Response(JSON.stringify({ error: message }), {
status,
headers
});
const queryPage = (value) => value === null ? void 0 : Number(value);
/** Proxies a bounded Learning read through the runtime's server-held credential. */
async function handleInspectorLearning({ runtime, request }) {
if (!isIntelligenceRuntime(runtime) || !hasLearningContainerConfiguration(runtime)) return errorResponse(404, "Not found");
const user = await resolveIntelligenceUser({
runtime,
request
});
if (user instanceof Response) return user;
const url = new URL(request.url);
if ([...url.searchParams.keys()].some((key) => ![
"agentId",
"skillsPage",
"insightsPage"
].includes(key))) return errorResponse(400, "Invalid Inspector Learning request");
const parsedRequest = parseInspectorLearningRequestV1({
agentId: url.searchParams.get("agentId") ?? void 0,
skillsPage: queryPage(url.searchParams.get("skillsPage")),
insightsPage: queryPage(url.searchParams.get("insightsPage"))
});
if (!parsedRequest) return errorResponse(400, "Invalid Inspector Learning request");
try {
const snapshot = parseInspectorLearningSnapshotV1(await runtime.intelligence.getInspectorLearning({
...parsedRequest,
...typeof runtime.learning?.containerId === "string" ? { runtimeContainerId: runtime.learning.containerId } : {}
}));
if (!snapshot) return errorResponse(502, "Invalid Inspector Learning response");
return new Response(JSON.stringify(snapshot), {
status: 200,
headers
});
} catch (error) {
if (error instanceof PlatformRequestError && error.status === 404) return errorResponse(404, "Not found");
return errorResponse(503, "Inspector Learning is temporarily unavailable");
}
}
//#endregion
export { handleInspectorLearning };
//# sourceMappingURL=handle-inspector-learning.mjs.map