@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;" />
68 lines (66 loc) • 2.22 kB
JavaScript
require("reflect-metadata");
//#region src/v2/runtime/endpoints/single-route-helpers.ts
const METHOD_NAMES = [
"agent/run",
"agent/connect",
"agent/stop",
"info",
"transcribe"
];
async function parseMethodCall(request) {
if (!(request.headers.get("content-type") || "").includes("application/json")) throw createResponseError("Single-route endpoint expects JSON payloads", 415);
let jsonEnvelope;
try {
jsonEnvelope = await request.clone().json();
} catch (error) {
throw createResponseError("Invalid JSON payload", 400);
}
return {
method: validateMethod(jsonEnvelope.method),
params: jsonEnvelope.params,
body: jsonEnvelope.body
};
}
function expectString(params, key) {
const value = params?.[key];
if (typeof value === "string" && value.trim().length > 0) return value;
throw createResponseError(`Missing or invalid parameter '${key}'`, 400);
}
function createJsonRequest(base, body) {
if (body === void 0 || body === null) throw createResponseError("Missing request body for JSON handler", 400);
const headers = new Headers(base.headers);
headers.set("content-type", "application/json");
headers.delete("content-length");
const serializedBody = serializeJsonBody(body);
return new Request(base.url, {
method: "POST",
headers,
body: serializedBody,
signal: base.signal
});
}
function createResponseError(message, status) {
return new Response(JSON.stringify({
error: "invalid_request",
message
}), {
status,
headers: { "Content-Type": "application/json" }
});
}
function validateMethod(method) {
if (!method) throw createResponseError("Missing method field", 400);
if (METHOD_NAMES.includes(method)) return method;
throw createResponseError(`Unsupported method '${method}'`, 400);
}
function serializeJsonBody(body) {
if (typeof body === "string") return body;
if (body instanceof Blob || body instanceof ArrayBuffer || body instanceof Uint8Array) return body;
if (body instanceof FormData || body instanceof URLSearchParams) return body;
return JSON.stringify(body);
}
//#endregion
exports.createJsonRequest = createJsonRequest;
exports.expectString = expectString;
exports.parseMethodCall = parseMethodCall;
//# sourceMappingURL=single-route-helpers.cjs.map