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

60 lines (58 loc) 1.75 kB
import "reflect-metadata"; import { createCopilotRuntimeHandler } from "../core/fetch-handler.mjs"; import { createExpressNodeHandler } from "./express-fetch-bridge.mjs"; import express from "express"; import cors from "cors"; //#region src/v2/runtime/endpoints/express.ts function createCopilotExpressHandler({ runtime, basePath, mode = "multi-route", cors: corsOption = true, hooks }) { const normalizedBase = normalizeBasePath(basePath); const nodeHandler = createExpressNodeHandler(createCopilotRuntimeHandler({ runtime, basePath: normalizedBase, mode, cors: false, hooks })); const expressHandler = async (req, res, next) => { try { await nodeHandler(req, res); } catch (err) { next(err); } }; const router = express.Router(); if (corsOption) { const corsConfig = corsOption === true ? { origin: "*", methods: [ "GET", "HEAD", "PUT", "POST", "DELETE", "PATCH", "OPTIONS" ], allowedHeaders: ["*"] } : corsOption; router.use(cors(corsConfig)); } if (mode === "single-route") { router.post(normalizedBase, expressHandler); router.options(normalizedBase, expressHandler); } else if (normalizedBase === "/") router.all(/.*/, expressHandler); else router.all(new RegExp(`^${escapeRegExp(normalizedBase)}(\\/.*)?$`), expressHandler); return router; } function escapeRegExp(s) { return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } function normalizeBasePath(path) { if (!path) throw new Error("basePath must be provided for Express endpoint"); if (!path.startsWith("/")) return `/${path}`; if (path.length > 1 && path.endsWith("/")) return path.slice(0, -1); return path; } //#endregion export { createCopilotExpressHandler }; //# sourceMappingURL=express.mjs.map