@copilotkit/runtime 1.80.0 → 1.82.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_virtual/_rolldown/runtime.mjs +15 -1
- package/dist/agent/index.cjs +10 -7
- package/dist/agent/index.cjs.map +1 -1
- package/dist/agent/index.d.cts +1 -1
- package/dist/agent/index.d.cts.map +1 -1
- package/dist/agent/index.d.mts.map +1 -1
- package/dist/agent/index.mjs +10 -7
- package/dist/agent/index.mjs.map +1 -1
- package/dist/lib/runtime/copilot-runtime.cjs +17 -2
- package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.cts +2 -2
- package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.mts +2 -2
- package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.mjs +18 -3
- package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
- package/dist/lib/runtime/legacy-service-adapter-agent.cjs +360 -0
- package/dist/lib/runtime/legacy-service-adapter-agent.cjs.map +1 -0
- package/dist/lib/runtime/legacy-service-adapter-agent.mjs +359 -0
- package/dist/lib/runtime/legacy-service-adapter-agent.mjs.map +1 -0
- package/dist/lib/runtime/telemetry-agent-runner.cjs +1 -1
- package/dist/lib/runtime/telemetry-agent-runner.cjs.map +1 -1
- package/dist/lib/runtime/telemetry-agent-runner.d.cts.map +1 -1
- package/dist/lib/runtime/telemetry-agent-runner.d.mts.map +1 -1
- package/dist/lib/runtime/telemetry-agent-runner.mjs +1 -1
- package/dist/lib/runtime/telemetry-agent-runner.mjs.map +1 -1
- package/dist/lib/telemetry-client.cjs +12 -1
- package/dist/lib/telemetry-client.cjs.map +1 -1
- package/dist/lib/telemetry-client.mjs +12 -1
- package/dist/lib/telemetry-client.mjs.map +1 -1
- package/dist/package.cjs +2 -2
- package/dist/package.mjs +2 -2
- package/dist/service-adapters/events.cjs +91 -0
- package/dist/service-adapters/events.cjs.map +1 -1
- package/dist/service-adapters/events.d.cts +6 -1
- package/dist/service-adapters/events.d.cts.map +1 -1
- package/dist/service-adapters/events.d.mts +6 -1
- package/dist/service-adapters/events.d.mts.map +1 -1
- package/dist/service-adapters/events.mjs +90 -1
- package/dist/service-adapters/events.mjs.map +1 -1
- package/dist/service-adapters/langchain/langchain-adapter.cjs +1 -1
- package/dist/service-adapters/langchain/langchain-adapter.cjs.map +1 -1
- package/dist/service-adapters/langchain/langchain-adapter.d.cts.map +1 -1
- package/dist/service-adapters/langchain/langchain-adapter.d.mts.map +1 -1
- package/dist/service-adapters/langchain/langchain-adapter.mjs +2 -2
- package/dist/service-adapters/langchain/langchain-adapter.mjs.map +1 -1
- package/dist/service-adapters/langchain/utils.cjs +53 -15
- package/dist/service-adapters/langchain/utils.cjs.map +1 -1
- package/dist/service-adapters/langchain/utils.mjs +53 -16
- package/dist/service-adapters/langchain/utils.mjs.map +1 -1
- package/dist/v2/index.cjs +5 -0
- package/dist/v2/index.d.cts +5 -2
- package/dist/v2/index.d.mts +5 -2
- package/dist/v2/index.mjs +4 -2
- package/dist/v2/runtime/core/channel-manager.cjs +1 -0
- package/dist/v2/runtime/core/channel-manager.cjs.map +1 -1
- package/dist/v2/runtime/core/channel-manager.mjs +1 -0
- package/dist/v2/runtime/core/channel-manager.mjs.map +1 -1
- package/dist/v2/runtime/core/fetch-handler.cjs +1 -1
- package/dist/v2/runtime/core/fetch-handler.cjs.map +1 -1
- package/dist/v2/runtime/core/fetch-handler.mjs +1 -1
- package/dist/v2/runtime/core/fetch-handler.mjs.map +1 -1
- package/dist/v2/runtime/core/package-info.cjs +1 -1
- package/dist/v2/runtime/core/package-info.mjs +1 -1
- package/dist/v2/runtime/core/runtime.cjs +5 -1
- package/dist/v2/runtime/core/runtime.cjs.map +1 -1
- package/dist/v2/runtime/core/runtime.d.cts +15 -0
- package/dist/v2/runtime/core/runtime.d.cts.map +1 -1
- package/dist/v2/runtime/core/runtime.d.mts +15 -0
- package/dist/v2/runtime/core/runtime.d.mts.map +1 -1
- package/dist/v2/runtime/core/runtime.mjs +5 -1
- package/dist/v2/runtime/core/runtime.mjs.map +1 -1
- package/dist/v2/runtime/endpoints/express.cjs +2 -2
- package/dist/v2/runtime/endpoints/express.cjs.map +1 -1
- package/dist/v2/runtime/endpoints/express.mjs +2 -2
- package/dist/v2/runtime/endpoints/express.mjs.map +1 -1
- package/dist/v2/runtime/endpoints/node-fetch-handler.cjs +51 -1
- package/dist/v2/runtime/endpoints/node-fetch-handler.cjs.map +1 -1
- package/dist/v2/runtime/endpoints/node-fetch-handler.d.cts.map +1 -1
- package/dist/v2/runtime/endpoints/node-fetch-handler.d.mts.map +1 -1
- package/dist/v2/runtime/endpoints/node-fetch-handler.mjs +52 -2
- package/dist/v2/runtime/endpoints/node-fetch-handler.mjs.map +1 -1
- package/dist/v2/runtime/generative-ui/generative-ui-middleware.cjs +394 -0
- package/dist/v2/runtime/generative-ui/generative-ui-middleware.cjs.map +1 -0
- package/dist/v2/runtime/generative-ui/generative-ui-middleware.d.cts +21 -0
- package/dist/v2/runtime/generative-ui/generative-ui-middleware.d.cts.map +1 -0
- package/dist/v2/runtime/generative-ui/generative-ui-middleware.d.mts +21 -0
- package/dist/v2/runtime/generative-ui/generative-ui-middleware.d.mts.map +1 -0
- package/dist/v2/runtime/generative-ui/generative-ui-middleware.mjs +393 -0
- package/dist/v2/runtime/generative-ui/generative-ui-middleware.mjs.map +1 -0
- package/dist/v2/runtime/generative-ui/index.d.cts +3 -0
- package/dist/v2/runtime/generative-ui/index.d.mts +3 -0
- package/dist/v2/runtime/generative-ui/surface-writer.cjs +86 -0
- package/dist/v2/runtime/generative-ui/surface-writer.cjs.map +1 -0
- package/dist/v2/runtime/generative-ui/surface-writer.d.cts +33 -0
- package/dist/v2/runtime/generative-ui/surface-writer.d.cts.map +1 -0
- package/dist/v2/runtime/generative-ui/surface-writer.d.mts +33 -0
- package/dist/v2/runtime/generative-ui/surface-writer.d.mts.map +1 -0
- package/dist/v2/runtime/generative-ui/surface-writer.mjs +85 -0
- package/dist/v2/runtime/generative-ui/surface-writer.mjs.map +1 -0
- package/dist/v2/runtime/handlers/handle-run.cjs +8 -3
- package/dist/v2/runtime/handlers/handle-run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-run.mjs +5 -1
- package/dist/v2/runtime/handlers/handle-run.mjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/run.cjs +3 -0
- package/dist/v2/runtime/handlers/intelligence/run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/run.mjs +3 -0
- package/dist/v2/runtime/handlers/intelligence/run.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/agent-utils.cjs +7 -0
- package/dist/v2/runtime/handlers/shared/agent-utils.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/agent-utils.mjs +7 -0
- package/dist/v2/runtime/handlers/shared/agent-utils.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/mcp-apps-servers.cjs +2 -1
- package/dist/v2/runtime/handlers/shared/mcp-apps-servers.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/mcp-apps-servers.mjs +3 -1
- package/dist/v2/runtime/handlers/shared/mcp-apps-servers.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/sse-response.cjs +1 -1
- package/dist/v2/runtime/handlers/shared/sse-response.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/sse-response.mjs +1 -1
- package/dist/v2/runtime/handlers/shared/sse-response.mjs.map +1 -1
- package/dist/v2/runtime/index.d.cts +5 -1
- package/dist/v2/runtime/index.d.cts.map +1 -1
- package/dist/v2/runtime/index.d.mts +5 -1
- package/dist/v2/runtime/index.d.mts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.cjs +85 -1
- package/dist/v2/runtime/intelligence-platform/client.cjs.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.d.cts +24 -1
- package/dist/v2/runtime/intelligence-platform/client.d.cts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.d.mts +24 -1
- package/dist/v2/runtime/intelligence-platform/client.d.mts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.mjs +85 -1
- package/dist/v2/runtime/intelligence-platform/client.mjs.map +1 -1
- package/dist/v2/runtime/intelligence-platform/experimental.d.cts +27 -0
- package/dist/v2/runtime/intelligence-platform/experimental.d.cts.map +1 -0
- package/dist/v2/runtime/intelligence-platform/experimental.d.mts +27 -0
- package/dist/v2/runtime/intelligence-platform/experimental.d.mts.map +1 -0
- package/dist/v2/runtime/intelligence-platform/index.d.cts +2 -1
- package/dist/v2/runtime/intelligence-platform/index.d.mts +2 -1
- package/dist/v2/runtime/intelligence-platform/learning-management.cjs +204 -0
- package/dist/v2/runtime/intelligence-platform/learning-management.cjs.map +1 -0
- package/dist/v2/runtime/intelligence-platform/learning-management.d.cts +112 -0
- package/dist/v2/runtime/intelligence-platform/learning-management.d.cts.map +1 -0
- package/dist/v2/runtime/intelligence-platform/learning-management.d.mts +112 -0
- package/dist/v2/runtime/intelligence-platform/learning-management.d.mts.map +1 -0
- package/dist/v2/runtime/intelligence-platform/learning-management.mjs +194 -0
- package/dist/v2/runtime/intelligence-platform/learning-management.mjs.map +1 -0
- package/dist/v2/runtime/telemetry/telemetry-client.cjs +1 -1
- package/dist/v2/runtime/telemetry/telemetry-client.cjs.map +1 -1
- package/dist/v2/runtime/telemetry/telemetry-client.mjs +1 -1
- package/dist/v2/runtime/telemetry/telemetry-client.mjs.map +1 -1
- package/package.json +6 -6
- package/dist/v2/runtime/endpoints/express-fetch-bridge.cjs +0 -82
- package/dist/v2/runtime/endpoints/express-fetch-bridge.cjs.map +0 -1
- package/dist/v2/runtime/endpoints/express-fetch-bridge.mjs +0 -81
- package/dist/v2/runtime/endpoints/express-fetch-bridge.mjs.map +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require("reflect-metadata");
|
|
2
2
|
const require_runtime = require('../../../_virtual/_rolldown/runtime.cjs');
|
|
3
3
|
const require_fetch_handler = require('../core/fetch-handler.cjs');
|
|
4
|
-
const
|
|
4
|
+
const require_node_fetch_handler = require('./node-fetch-handler.cjs');
|
|
5
5
|
const require_load_express = require('./load-express.cjs');
|
|
6
6
|
const require_auto_start_channels = require('./auto-start-channels.cjs');
|
|
7
7
|
let cors = require("cors");
|
|
@@ -19,7 +19,7 @@ function createCopilotExpressHandler({ runtime, basePath, mode = "multi-route",
|
|
|
19
19
|
activateChannels,
|
|
20
20
|
__channelEngine
|
|
21
21
|
});
|
|
22
|
-
const nodeHandler =
|
|
22
|
+
const nodeHandler = require_node_fetch_handler.createCopilotNodeHandler(handler);
|
|
23
23
|
const expressHandler = async (req, res, next) => {
|
|
24
24
|
try {
|
|
25
25
|
await nodeHandler(req, res);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"express.cjs","names":["createCopilotRuntimeHandler","createExpressNodeHandler","loadExpress"],"sources":["../../../../src/v2/runtime/endpoints/express.ts"],"sourcesContent":["import type {\n Request as ExpressRequest,\n Response as ExpressResponse,\n NextFunction,\n} from \"express\";\nimport cors from \"cors\";\nimport type { CorsOptions } from \"cors\";\nimport type { CopilotRuntimeLike } from \"../core/runtime\";\nimport { createCopilotRuntimeHandler } from \"../core/fetch-handler\";\nimport type {\n ActivateChannelEngine,\n ChannelsControl,\n} from \"../core/channel-manager\";\nimport { createExpressNodeHandler } from \"./express-fetch-bridge\";\nimport { loadExpress } from \"./load-express\";\nimport { autoStartChannels } from \"./auto-start-channels\";\nimport type { CopilotRuntimeHooks } from \"../core/hooks\";\n\n/**\n * The middleware `createCopilotExpressHandler` returns: an Express router that\n * may also carry an optional {@link ChannelsControl} surface. The router object\n * itself is request-scoped middleware, but an Express app can only run inside a\n * long-running `http.Server` — so this wrapper is a lifecycle-owning host like\n * `createCopilotNodeListener`: it STARTS activation of the runtime's declared\n * managed Channels at creation, and `.channels` is here to observe (`ready()`)\n * or tear down (`stop()`) that activation.\n *\n * Deliberately NOT typed as Express's own `Router`. Pinning that type binds our\n * public surface to a single Express major: `@types/express@4` declares\n * `Request.param()` and `@types/express@5` does not, so a v4-typed router is not\n * assignable to a v5 `app.use()` and an Express 5 app cannot compile against us\n * at all (#7276). The call signature below is what `use()` accepts in both\n * majors, and the methods beside it are the router surface both majors share.\n *\n * The runtime value is still a real `express.Router()`; only the declared type\n * is widened.\n */\nexport type CopilotExpressRouter = ((\n req: any,\n res: any,\n next: (err?: unknown) => void,\n) => void) & {\n channels?: ChannelsControl;\n\n // The common router-configuration surface, declared structurally so that\n // consumers who add their own middleware or routes to the returned value keep\n // compiling. These are NOT taken from either Express major's `Router`, because\n // that is exactly what makes the type un-mountable across majors.\n use(...handlers: any[]): CopilotExpressRouter;\n get(...handlers: any[]): CopilotExpressRouter;\n post(...handlers: any[]): CopilotExpressRouter;\n put(...handlers: any[]): CopilotExpressRouter;\n patch(...handlers: any[]): CopilotExpressRouter;\n delete(...handlers: any[]): CopilotExpressRouter;\n options(...handlers: any[]): CopilotExpressRouter;\n all(...handlers: any[]): CopilotExpressRouter;\n\n // `route()` and `param()` are the remaining Router-configuration methods\n // consumers reach for, and dropping them would break\n // `router.route(\"/x\").get(...)` and `router.param(...)` callers for no gain: a\n // re-pin to either Express major is caught by\n // `scripts/validate-dts-imports.ts`, which fails on any published declaration\n // that names `express` at all.\n //\n // `stack` is deliberately absent. It is Express's own internal middleware\n // array, not a configuration surface, and it is what\n // `express-major-compat.test-d.ts` asserts against to keep this type from\n // quietly becoming a `Router` again.\n route(path: any): any;\n param(...handlers: any[]): CopilotExpressRouter;\n};\n\nexport interface CopilotExpressEndpointParams {\n runtime: CopilotRuntimeLike;\n basePath: string;\n\n /**\n * Endpoint mode.\n * - `\"multi-route\"` (default): separate routes for each operation\n * - `\"single-route\"`: single POST endpoint with JSON envelope dispatch\n */\n mode?: \"multi-route\" | \"single-route\";\n\n /**\n * CORS configuration for the Express router.\n * - `true` (default): permissive CORS (`origin: \"*\"`, all methods, all headers).\n * - `false`: no CORS middleware is applied — handle it yourself.\n * - object: passed directly to the Express `cors()` middleware.\n */\n cors?: boolean | CorsOptions;\n\n /**\n * Lifecycle hooks for request processing.\n */\n hooks?: CopilotRuntimeHooks;\n\n /**\n * Whether the underlying handler builds the control surface for the runtime's\n * declared managed Channels — and, because Express is a long-running host,\n * starts their activation at creation. Defaults to `true`. Set `false` to\n * build no surface and open no socket (tests, short-lived scripts). See\n * `CopilotRuntimeHandlerOptions.activateChannels`.\n */\n activateChannels?: boolean;\n\n /**\n * @internal Test seam: inject a fake Channel activation engine. Forwarded\n * to `createCopilotRuntimeHandler`. Not part of the public API.\n */\n __channelEngine?: ActivateChannelEngine;\n}\n\n/**\n * Creates an Express router that serves the CopilotKit runtime.\n *\n * In **multi-route** mode (default) the router exposes:\n * - `GET {basePath}/info` — runtime info\n * - `POST {basePath}/agent/:agentId/run` — start an agent run\n * - `POST {basePath}/agent/:agentId/connect` — connect to an agent run\n * - `POST {basePath}/agent/:agentId/stop/:threadId` — stop an agent run\n * - `POST {basePath}/transcribe` — transcribe audio\n *\n * In **single-route** mode a single `POST {basePath}` endpoint accepts a JSON\n * envelope `{ method, params, body }` and dispatches to the appropriate handler.\n *\n * @example\n * ```typescript\n * import express from \"express\";\n * import { CopilotRuntime } from \"@copilotkit/runtime/v2\";\n * import { createCopilotExpressHandler } from \"@copilotkit/runtime/v2/express\";\n *\n * const runtime = new CopilotRuntime({\n * agents: { default: new BuiltInAgent({ model: \"openai/gpt-4o-mini\" }) },\n * });\n *\n * const app = express();\n * app.use(createCopilotExpressHandler({\n * runtime,\n * basePath: \"/api/copilotkit\",\n * cors: true,\n * }));\n * app.listen(4000);\n * ```\n *\n * @example Single-route mode with lifecycle hooks\n * ```typescript\n * app.use(createCopilotExpressHandler({\n * runtime,\n * basePath: \"/api/copilotkit\",\n * mode: \"single-route\",\n * hooks: {\n * onRequest: ({ request }) => {\n * if (!request.headers.get(\"authorization\")) {\n * throw new Response(\"Unauthorized\", { status: 401 });\n * }\n * },\n * },\n * }));\n * ```\n */\n/** @deprecated Use `createCopilotExpressHandler` instead. */\nexport { createCopilotExpressHandler as createCopilotEndpointExpress };\n\nexport function createCopilotExpressHandler({\n runtime,\n basePath,\n mode = \"multi-route\",\n cors: corsOption = true,\n hooks,\n activateChannels,\n __channelEngine,\n}: CopilotExpressEndpointParams): CopilotExpressRouter {\n const normalizedBase = normalizeBasePath(basePath);\n\n const handler = createCopilotRuntimeHandler({\n runtime,\n basePath: normalizedBase,\n mode,\n cors: false, // CORS is handled at the Express middleware layer\n hooks,\n activateChannels,\n __channelEngine,\n });\n\n const nodeHandler = createExpressNodeHandler(handler);\n\n const expressHandler = async (\n req: ExpressRequest,\n res: ExpressResponse,\n next: NextFunction,\n ) => {\n try {\n await nodeHandler(req, res);\n } catch (err) {\n next(err);\n }\n };\n\n const router = loadExpress().Router();\n\n // CORS middleware\n if (corsOption) {\n const corsConfig: CorsOptions =\n corsOption === true\n ? {\n origin: \"*\",\n methods: [\n \"GET\",\n \"HEAD\",\n \"PUT\",\n \"POST\",\n \"DELETE\",\n \"PATCH\",\n \"OPTIONS\",\n ],\n allowedHeaders: [\"*\"],\n }\n : corsOption;\n router.use(cors(corsConfig));\n }\n\n // Route mounting\n if (mode === \"single-route\") {\n router.post(normalizedBase, expressHandler);\n router.options(normalizedBase, expressHandler);\n } else if (normalizedBase === \"/\") {\n router.all(/.*/, expressHandler);\n } else {\n router.all(\n new RegExp(`^${escapeRegExp(normalizedBase)}(\\\\/.*)?$`),\n expressHandler,\n );\n }\n\n const exposedRouter: CopilotExpressRouter = router;\n exposedRouter.channels = handler.channels;\n autoStartChannels(exposedRouter.channels);\n return exposedRouter;\n}\n\nfunction escapeRegExp(s: string): string {\n return s.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n}\n\nfunction normalizeBasePath(path: string): string {\n if (!path) {\n throw new Error(\"basePath must be provided for Express endpoint\");\n }\n\n if (!path.startsWith(\"/\")) {\n return `/${path}`;\n }\n\n if (path.length > 1 && path.endsWith(\"/\")) {\n return path.slice(0, -1);\n }\n\n return path;\n}\n"],"mappings":";;;;;;;;;;AAmKA,SAAgB,4BAA4B,EAC1C,SACA,UACA,OAAO,eACP,MAAM,aAAa,MACnB,OACA,kBACA,mBACqD;CACrD,MAAM,iBAAiB,kBAAkB,SAAS;CAElD,MAAM,UAAUA,kDAA4B;EAC1C;EACA,UAAU;EACV;EACA,MAAM;EACN;EACA;EACA;EACD,CAAC;CAEF,MAAM,cAAcC,sDAAyB,QAAQ;CAErD,MAAM,iBAAiB,OACrB,KACA,KACA,SACG;AACH,MAAI;AACF,SAAM,YAAY,KAAK,IAAI;WACpB,KAAK;AACZ,QAAK,IAAI;;;CAIb,MAAM,SAASC,kCAAa,CAAC,QAAQ;AAGrC,KAAI,YAAY;EACd,MAAM,aACJ,eAAe,OACX;GACE,QAAQ;GACR,SAAS;IACP;IACA;IACA;IACA;IACA;IACA;IACA;IACD;GACD,gBAAgB,CAAC,IAAI;GACtB,GACD;AACN,SAAO,sBAAS,WAAW,CAAC;;AAI9B,KAAI,SAAS,gBAAgB;AAC3B,SAAO,KAAK,gBAAgB,eAAe;AAC3C,SAAO,QAAQ,gBAAgB,eAAe;YACrC,mBAAmB,IAC5B,QAAO,IAAI,MAAM,eAAe;KAEhC,QAAO,IACL,IAAI,OAAO,IAAI,aAAa,eAAe,CAAC,WAAW,EACvD,eACD;CAGH,MAAM,gBAAsC;AAC5C,eAAc,WAAW,QAAQ;AACjC,+CAAkB,cAAc,SAAS;AACzC,QAAO;;AAGT,SAAS,aAAa,GAAmB;AACvC,QAAO,EAAE,QAAQ,uBAAuB,OAAO;;AAGjD,SAAS,kBAAkB,MAAsB;AAC/C,KAAI,CAAC,KACH,OAAM,IAAI,MAAM,iDAAiD;AAGnE,KAAI,CAAC,KAAK,WAAW,IAAI,CACvB,QAAO,IAAI;AAGb,KAAI,KAAK,SAAS,KAAK,KAAK,SAAS,IAAI,CACvC,QAAO,KAAK,MAAM,GAAG,GAAG;AAG1B,QAAO"}
|
|
1
|
+
{"version":3,"file":"express.cjs","names":["createCopilotRuntimeHandler","createCopilotNodeHandler","loadExpress"],"sources":["../../../../src/v2/runtime/endpoints/express.ts"],"sourcesContent":["import type {\n Request as ExpressRequest,\n Response as ExpressResponse,\n NextFunction,\n} from \"express\";\nimport cors from \"cors\";\nimport type { CorsOptions } from \"cors\";\nimport type { CopilotRuntimeLike } from \"../core/runtime\";\nimport { createCopilotRuntimeHandler } from \"../core/fetch-handler\";\nimport type {\n ActivateChannelEngine,\n ChannelsControl,\n} from \"../core/channel-manager\";\nimport { createCopilotNodeHandler } from \"./node-fetch-handler\";\nimport { loadExpress } from \"./load-express\";\nimport { autoStartChannels } from \"./auto-start-channels\";\nimport type { CopilotRuntimeHooks } from \"../core/hooks\";\n\n/**\n * The middleware `createCopilotExpressHandler` returns: an Express router that\n * may also carry an optional {@link ChannelsControl} surface. The router object\n * itself is request-scoped middleware, but an Express app can only run inside a\n * long-running `http.Server` — so this wrapper is a lifecycle-owning host like\n * `createCopilotNodeListener`: it STARTS activation of the runtime's declared\n * managed Channels at creation, and `.channels` is here to observe (`ready()`)\n * or tear down (`stop()`) that activation.\n *\n * Deliberately NOT typed as Express's own `Router`. Pinning that type binds our\n * public surface to a single Express major: `@types/express@4` declares\n * `Request.param()` and `@types/express@5` does not, so a v4-typed router is not\n * assignable to a v5 `app.use()` and an Express 5 app cannot compile against us\n * at all (#7276). The call signature below is what `use()` accepts in both\n * majors, and the methods beside it are the router surface both majors share.\n *\n * The runtime value is still a real `express.Router()`; only the declared type\n * is widened.\n */\nexport type CopilotExpressRouter = ((\n req: any,\n res: any,\n next: (err?: unknown) => void,\n) => void) & {\n channels?: ChannelsControl;\n\n // The common router-configuration surface, declared structurally so that\n // consumers who add their own middleware or routes to the returned value keep\n // compiling. These are NOT taken from either Express major's `Router`, because\n // that is exactly what makes the type un-mountable across majors.\n use(...handlers: any[]): CopilotExpressRouter;\n get(...handlers: any[]): CopilotExpressRouter;\n post(...handlers: any[]): CopilotExpressRouter;\n put(...handlers: any[]): CopilotExpressRouter;\n patch(...handlers: any[]): CopilotExpressRouter;\n delete(...handlers: any[]): CopilotExpressRouter;\n options(...handlers: any[]): CopilotExpressRouter;\n all(...handlers: any[]): CopilotExpressRouter;\n\n // `route()` and `param()` are the remaining Router-configuration methods\n // consumers reach for, and dropping them would break\n // `router.route(\"/x\").get(...)` and `router.param(...)` callers for no gain: a\n // re-pin to either Express major is caught by\n // `scripts/validate-dts-imports.ts`, which fails on any published declaration\n // that names `express` at all.\n //\n // `stack` is deliberately absent. It is Express's own internal middleware\n // array, not a configuration surface, and it is what\n // `express-major-compat.test-d.ts` asserts against to keep this type from\n // quietly becoming a `Router` again.\n route(path: any): any;\n param(...handlers: any[]): CopilotExpressRouter;\n};\n\nexport interface CopilotExpressEndpointParams {\n runtime: CopilotRuntimeLike;\n basePath: string;\n\n /**\n * Endpoint mode.\n * - `\"multi-route\"` (default): separate routes for each operation\n * - `\"single-route\"`: single POST endpoint with JSON envelope dispatch\n */\n mode?: \"multi-route\" | \"single-route\";\n\n /**\n * CORS configuration for the Express router.\n * - `true` (default): permissive CORS (`origin: \"*\"`, all methods, all headers).\n * - `false`: no CORS middleware is applied — handle it yourself.\n * - object: passed directly to the Express `cors()` middleware.\n */\n cors?: boolean | CorsOptions;\n\n /**\n * Lifecycle hooks for request processing.\n */\n hooks?: CopilotRuntimeHooks;\n\n /**\n * Whether the underlying handler builds the control surface for the runtime's\n * declared managed Channels — and, because Express is a long-running host,\n * starts their activation at creation. Defaults to `true`. Set `false` to\n * build no surface and open no socket (tests, short-lived scripts). See\n * `CopilotRuntimeHandlerOptions.activateChannels`.\n */\n activateChannels?: boolean;\n\n /**\n * @internal Test seam: inject a fake Channel activation engine. Forwarded\n * to `createCopilotRuntimeHandler`. Not part of the public API.\n */\n __channelEngine?: ActivateChannelEngine;\n}\n\n/**\n * Creates an Express router that serves the CopilotKit runtime.\n *\n * In **multi-route** mode (default) the router exposes:\n * - `GET {basePath}/info` — runtime info\n * - `POST {basePath}/agent/:agentId/run` — start an agent run\n * - `POST {basePath}/agent/:agentId/connect` — connect to an agent run\n * - `POST {basePath}/agent/:agentId/stop/:threadId` — stop an agent run\n * - `POST {basePath}/transcribe` — transcribe audio\n *\n * In **single-route** mode a single `POST {basePath}` endpoint accepts a JSON\n * envelope `{ method, params, body }` and dispatches to the appropriate handler.\n *\n * @example\n * ```typescript\n * import express from \"express\";\n * import { CopilotRuntime } from \"@copilotkit/runtime/v2\";\n * import { createCopilotExpressHandler } from \"@copilotkit/runtime/v2/express\";\n *\n * const runtime = new CopilotRuntime({\n * agents: { default: new BuiltInAgent({ model: \"openai/gpt-4o-mini\" }) },\n * });\n *\n * const app = express();\n * app.use(createCopilotExpressHandler({\n * runtime,\n * basePath: \"/api/copilotkit\",\n * cors: true,\n * }));\n * app.listen(4000);\n * ```\n *\n * @example Single-route mode with lifecycle hooks\n * ```typescript\n * app.use(createCopilotExpressHandler({\n * runtime,\n * basePath: \"/api/copilotkit\",\n * mode: \"single-route\",\n * hooks: {\n * onRequest: ({ request }) => {\n * if (!request.headers.get(\"authorization\")) {\n * throw new Response(\"Unauthorized\", { status: 401 });\n * }\n * },\n * },\n * }));\n * ```\n */\n/** @deprecated Use `createCopilotExpressHandler` instead. */\nexport { createCopilotExpressHandler as createCopilotEndpointExpress };\n\nexport function createCopilotExpressHandler({\n runtime,\n basePath,\n mode = \"multi-route\",\n cors: corsOption = true,\n hooks,\n activateChannels,\n __channelEngine,\n}: CopilotExpressEndpointParams): CopilotExpressRouter {\n const normalizedBase = normalizeBasePath(basePath);\n\n const handler = createCopilotRuntimeHandler({\n runtime,\n basePath: normalizedBase,\n mode,\n cors: false, // CORS is handled at the Express middleware layer\n hooks,\n activateChannels,\n __channelEngine,\n });\n\n const nodeHandler = createCopilotNodeHandler(handler);\n\n const expressHandler = async (\n req: ExpressRequest,\n res: ExpressResponse,\n next: NextFunction,\n ) => {\n try {\n await nodeHandler(req, res);\n } catch (err) {\n next(err);\n }\n };\n\n const router = loadExpress().Router();\n\n // CORS middleware\n if (corsOption) {\n const corsConfig: CorsOptions =\n corsOption === true\n ? {\n origin: \"*\",\n methods: [\n \"GET\",\n \"HEAD\",\n \"PUT\",\n \"POST\",\n \"DELETE\",\n \"PATCH\",\n \"OPTIONS\",\n ],\n allowedHeaders: [\"*\"],\n }\n : corsOption;\n router.use(cors(corsConfig));\n }\n\n // Route mounting\n if (mode === \"single-route\") {\n router.post(normalizedBase, expressHandler);\n router.options(normalizedBase, expressHandler);\n } else if (normalizedBase === \"/\") {\n router.all(/.*/, expressHandler);\n } else {\n router.all(\n new RegExp(`^${escapeRegExp(normalizedBase)}(\\\\/.*)?$`),\n expressHandler,\n );\n }\n\n const exposedRouter: CopilotExpressRouter = router;\n exposedRouter.channels = handler.channels;\n autoStartChannels(exposedRouter.channels);\n return exposedRouter;\n}\n\nfunction escapeRegExp(s: string): string {\n return s.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n}\n\nfunction normalizeBasePath(path: string): string {\n if (!path) {\n throw new Error(\"basePath must be provided for Express endpoint\");\n }\n\n if (!path.startsWith(\"/\")) {\n return `/${path}`;\n }\n\n if (path.length > 1 && path.endsWith(\"/\")) {\n return path.slice(0, -1);\n }\n\n return path;\n}\n"],"mappings":";;;;;;;;;;AAmKA,SAAgB,4BAA4B,EAC1C,SACA,UACA,OAAO,eACP,MAAM,aAAa,MACnB,OACA,kBACA,mBACqD;CACrD,MAAM,iBAAiB,kBAAkB,SAAS;CAElD,MAAM,UAAUA,kDAA4B;EAC1C;EACA,UAAU;EACV;EACA,MAAM;EACN;EACA;EACA;EACD,CAAC;CAEF,MAAM,cAAcC,oDAAyB,QAAQ;CAErD,MAAM,iBAAiB,OACrB,KACA,KACA,SACG;AACH,MAAI;AACF,SAAM,YAAY,KAAK,IAAI;WACpB,KAAK;AACZ,QAAK,IAAI;;;CAIb,MAAM,SAASC,kCAAa,CAAC,QAAQ;AAGrC,KAAI,YAAY;EACd,MAAM,aACJ,eAAe,OACX;GACE,QAAQ;GACR,SAAS;IACP;IACA;IACA;IACA;IACA;IACA;IACA;IACD;GACD,gBAAgB,CAAC,IAAI;GACtB,GACD;AACN,SAAO,sBAAS,WAAW,CAAC;;AAI9B,KAAI,SAAS,gBAAgB;AAC3B,SAAO,KAAK,gBAAgB,eAAe;AAC3C,SAAO,QAAQ,gBAAgB,eAAe;YACrC,mBAAmB,IAC5B,QAAO,IAAI,MAAM,eAAe;KAEhC,QAAO,IACL,IAAI,OAAO,IAAI,aAAa,eAAe,CAAC,WAAW,EACvD,eACD;CAGH,MAAM,gBAAsC;AAC5C,eAAc,WAAW,QAAQ;AACjC,+CAAkB,cAAc,SAAS;AACzC,QAAO;;AAGT,SAAS,aAAa,GAAmB;AACvC,QAAO,EAAE,QAAQ,uBAAuB,OAAO;;AAGjD,SAAS,kBAAkB,MAAsB;AAC/C,KAAI,CAAC,KACH,OAAM,IAAI,MAAM,iDAAiD;AAGnE,KAAI,CAAC,KAAK,WAAW,IAAI,CACvB,QAAO,IAAI;AAGb,KAAI,KAAK,SAAS,KAAK,KAAK,SAAS,IAAI,CACvC,QAAO,KAAK,MAAM,GAAG,GAAG;AAG1B,QAAO"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
2
|
import { createCopilotRuntimeHandler } from "../core/fetch-handler.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { createCopilotNodeHandler } from "./node-fetch-handler.mjs";
|
|
4
4
|
import { loadExpress } from "./load-express.mjs";
|
|
5
5
|
import { autoStartChannels } from "./auto-start-channels.mjs";
|
|
6
6
|
import cors from "cors";
|
|
@@ -17,7 +17,7 @@ function createCopilotExpressHandler({ runtime, basePath, mode = "multi-route",
|
|
|
17
17
|
activateChannels,
|
|
18
18
|
__channelEngine
|
|
19
19
|
});
|
|
20
|
-
const nodeHandler =
|
|
20
|
+
const nodeHandler = createCopilotNodeHandler(handler);
|
|
21
21
|
const expressHandler = async (req, res, next) => {
|
|
22
22
|
try {
|
|
23
23
|
await nodeHandler(req, res);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"express.mjs","names":[],"sources":["../../../../src/v2/runtime/endpoints/express.ts"],"sourcesContent":["import type {\n Request as ExpressRequest,\n Response as ExpressResponse,\n NextFunction,\n} from \"express\";\nimport cors from \"cors\";\nimport type { CorsOptions } from \"cors\";\nimport type { CopilotRuntimeLike } from \"../core/runtime\";\nimport { createCopilotRuntimeHandler } from \"../core/fetch-handler\";\nimport type {\n ActivateChannelEngine,\n ChannelsControl,\n} from \"../core/channel-manager\";\nimport {
|
|
1
|
+
{"version":3,"file":"express.mjs","names":[],"sources":["../../../../src/v2/runtime/endpoints/express.ts"],"sourcesContent":["import type {\n Request as ExpressRequest,\n Response as ExpressResponse,\n NextFunction,\n} from \"express\";\nimport cors from \"cors\";\nimport type { CorsOptions } from \"cors\";\nimport type { CopilotRuntimeLike } from \"../core/runtime\";\nimport { createCopilotRuntimeHandler } from \"../core/fetch-handler\";\nimport type {\n ActivateChannelEngine,\n ChannelsControl,\n} from \"../core/channel-manager\";\nimport { createCopilotNodeHandler } from \"./node-fetch-handler\";\nimport { loadExpress } from \"./load-express\";\nimport { autoStartChannels } from \"./auto-start-channels\";\nimport type { CopilotRuntimeHooks } from \"../core/hooks\";\n\n/**\n * The middleware `createCopilotExpressHandler` returns: an Express router that\n * may also carry an optional {@link ChannelsControl} surface. The router object\n * itself is request-scoped middleware, but an Express app can only run inside a\n * long-running `http.Server` — so this wrapper is a lifecycle-owning host like\n * `createCopilotNodeListener`: it STARTS activation of the runtime's declared\n * managed Channels at creation, and `.channels` is here to observe (`ready()`)\n * or tear down (`stop()`) that activation.\n *\n * Deliberately NOT typed as Express's own `Router`. Pinning that type binds our\n * public surface to a single Express major: `@types/express@4` declares\n * `Request.param()` and `@types/express@5` does not, so a v4-typed router is not\n * assignable to a v5 `app.use()` and an Express 5 app cannot compile against us\n * at all (#7276). The call signature below is what `use()` accepts in both\n * majors, and the methods beside it are the router surface both majors share.\n *\n * The runtime value is still a real `express.Router()`; only the declared type\n * is widened.\n */\nexport type CopilotExpressRouter = ((\n req: any,\n res: any,\n next: (err?: unknown) => void,\n) => void) & {\n channels?: ChannelsControl;\n\n // The common router-configuration surface, declared structurally so that\n // consumers who add their own middleware or routes to the returned value keep\n // compiling. These are NOT taken from either Express major's `Router`, because\n // that is exactly what makes the type un-mountable across majors.\n use(...handlers: any[]): CopilotExpressRouter;\n get(...handlers: any[]): CopilotExpressRouter;\n post(...handlers: any[]): CopilotExpressRouter;\n put(...handlers: any[]): CopilotExpressRouter;\n patch(...handlers: any[]): CopilotExpressRouter;\n delete(...handlers: any[]): CopilotExpressRouter;\n options(...handlers: any[]): CopilotExpressRouter;\n all(...handlers: any[]): CopilotExpressRouter;\n\n // `route()` and `param()` are the remaining Router-configuration methods\n // consumers reach for, and dropping them would break\n // `router.route(\"/x\").get(...)` and `router.param(...)` callers for no gain: a\n // re-pin to either Express major is caught by\n // `scripts/validate-dts-imports.ts`, which fails on any published declaration\n // that names `express` at all.\n //\n // `stack` is deliberately absent. It is Express's own internal middleware\n // array, not a configuration surface, and it is what\n // `express-major-compat.test-d.ts` asserts against to keep this type from\n // quietly becoming a `Router` again.\n route(path: any): any;\n param(...handlers: any[]): CopilotExpressRouter;\n};\n\nexport interface CopilotExpressEndpointParams {\n runtime: CopilotRuntimeLike;\n basePath: string;\n\n /**\n * Endpoint mode.\n * - `\"multi-route\"` (default): separate routes for each operation\n * - `\"single-route\"`: single POST endpoint with JSON envelope dispatch\n */\n mode?: \"multi-route\" | \"single-route\";\n\n /**\n * CORS configuration for the Express router.\n * - `true` (default): permissive CORS (`origin: \"*\"`, all methods, all headers).\n * - `false`: no CORS middleware is applied — handle it yourself.\n * - object: passed directly to the Express `cors()` middleware.\n */\n cors?: boolean | CorsOptions;\n\n /**\n * Lifecycle hooks for request processing.\n */\n hooks?: CopilotRuntimeHooks;\n\n /**\n * Whether the underlying handler builds the control surface for the runtime's\n * declared managed Channels — and, because Express is a long-running host,\n * starts their activation at creation. Defaults to `true`. Set `false` to\n * build no surface and open no socket (tests, short-lived scripts). See\n * `CopilotRuntimeHandlerOptions.activateChannels`.\n */\n activateChannels?: boolean;\n\n /**\n * @internal Test seam: inject a fake Channel activation engine. Forwarded\n * to `createCopilotRuntimeHandler`. Not part of the public API.\n */\n __channelEngine?: ActivateChannelEngine;\n}\n\n/**\n * Creates an Express router that serves the CopilotKit runtime.\n *\n * In **multi-route** mode (default) the router exposes:\n * - `GET {basePath}/info` — runtime info\n * - `POST {basePath}/agent/:agentId/run` — start an agent run\n * - `POST {basePath}/agent/:agentId/connect` — connect to an agent run\n * - `POST {basePath}/agent/:agentId/stop/:threadId` — stop an agent run\n * - `POST {basePath}/transcribe` — transcribe audio\n *\n * In **single-route** mode a single `POST {basePath}` endpoint accepts a JSON\n * envelope `{ method, params, body }` and dispatches to the appropriate handler.\n *\n * @example\n * ```typescript\n * import express from \"express\";\n * import { CopilotRuntime } from \"@copilotkit/runtime/v2\";\n * import { createCopilotExpressHandler } from \"@copilotkit/runtime/v2/express\";\n *\n * const runtime = new CopilotRuntime({\n * agents: { default: new BuiltInAgent({ model: \"openai/gpt-4o-mini\" }) },\n * });\n *\n * const app = express();\n * app.use(createCopilotExpressHandler({\n * runtime,\n * basePath: \"/api/copilotkit\",\n * cors: true,\n * }));\n * app.listen(4000);\n * ```\n *\n * @example Single-route mode with lifecycle hooks\n * ```typescript\n * app.use(createCopilotExpressHandler({\n * runtime,\n * basePath: \"/api/copilotkit\",\n * mode: \"single-route\",\n * hooks: {\n * onRequest: ({ request }) => {\n * if (!request.headers.get(\"authorization\")) {\n * throw new Response(\"Unauthorized\", { status: 401 });\n * }\n * },\n * },\n * }));\n * ```\n */\n/** @deprecated Use `createCopilotExpressHandler` instead. */\nexport { createCopilotExpressHandler as createCopilotEndpointExpress };\n\nexport function createCopilotExpressHandler({\n runtime,\n basePath,\n mode = \"multi-route\",\n cors: corsOption = true,\n hooks,\n activateChannels,\n __channelEngine,\n}: CopilotExpressEndpointParams): CopilotExpressRouter {\n const normalizedBase = normalizeBasePath(basePath);\n\n const handler = createCopilotRuntimeHandler({\n runtime,\n basePath: normalizedBase,\n mode,\n cors: false, // CORS is handled at the Express middleware layer\n hooks,\n activateChannels,\n __channelEngine,\n });\n\n const nodeHandler = createCopilotNodeHandler(handler);\n\n const expressHandler = async (\n req: ExpressRequest,\n res: ExpressResponse,\n next: NextFunction,\n ) => {\n try {\n await nodeHandler(req, res);\n } catch (err) {\n next(err);\n }\n };\n\n const router = loadExpress().Router();\n\n // CORS middleware\n if (corsOption) {\n const corsConfig: CorsOptions =\n corsOption === true\n ? {\n origin: \"*\",\n methods: [\n \"GET\",\n \"HEAD\",\n \"PUT\",\n \"POST\",\n \"DELETE\",\n \"PATCH\",\n \"OPTIONS\",\n ],\n allowedHeaders: [\"*\"],\n }\n : corsOption;\n router.use(cors(corsConfig));\n }\n\n // Route mounting\n if (mode === \"single-route\") {\n router.post(normalizedBase, expressHandler);\n router.options(normalizedBase, expressHandler);\n } else if (normalizedBase === \"/\") {\n router.all(/.*/, expressHandler);\n } else {\n router.all(\n new RegExp(`^${escapeRegExp(normalizedBase)}(\\\\/.*)?$`),\n expressHandler,\n );\n }\n\n const exposedRouter: CopilotExpressRouter = router;\n exposedRouter.channels = handler.channels;\n autoStartChannels(exposedRouter.channels);\n return exposedRouter;\n}\n\nfunction escapeRegExp(s: string): string {\n return s.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n}\n\nfunction normalizeBasePath(path: string): string {\n if (!path) {\n throw new Error(\"basePath must be provided for Express endpoint\");\n }\n\n if (!path.startsWith(\"/\")) {\n return `/${path}`;\n }\n\n if (path.length > 1 && path.endsWith(\"/\")) {\n return path.slice(0, -1);\n }\n\n return path;\n}\n"],"mappings":";;;;;;;;AAmKA,SAAgB,4BAA4B,EAC1C,SACA,UACA,OAAO,eACP,MAAM,aAAa,MACnB,OACA,kBACA,mBACqD;CACrD,MAAM,iBAAiB,kBAAkB,SAAS;CAElD,MAAM,UAAU,4BAA4B;EAC1C;EACA,UAAU;EACV;EACA,MAAM;EACN;EACA;EACA;EACD,CAAC;CAEF,MAAM,cAAc,yBAAyB,QAAQ;CAErD,MAAM,iBAAiB,OACrB,KACA,KACA,SACG;AACH,MAAI;AACF,SAAM,YAAY,KAAK,IAAI;WACpB,KAAK;AACZ,QAAK,IAAI;;;CAIb,MAAM,SAAS,aAAa,CAAC,QAAQ;AAGrC,KAAI,YAAY;EACd,MAAM,aACJ,eAAe,OACX;GACE,QAAQ;GACR,SAAS;IACP;IACA;IACA;IACA;IACA;IACA;IACA;IACD;GACD,gBAAgB,CAAC,IAAI;GACtB,GACD;AACN,SAAO,IAAI,KAAK,WAAW,CAAC;;AAI9B,KAAI,SAAS,gBAAgB;AAC3B,SAAO,KAAK,gBAAgB,eAAe;AAC3C,SAAO,QAAQ,gBAAgB,eAAe;YACrC,mBAAmB,IAC5B,QAAO,IAAI,MAAM,eAAe;KAEhC,QAAO,IACL,IAAI,OAAO,IAAI,aAAa,eAAe,CAAC,WAAW,EACvD,eACD;CAGH,MAAM,gBAAsC;AAC5C,eAAc,WAAW,QAAQ;AACjC,mBAAkB,cAAc,SAAS;AACzC,QAAO;;AAGT,SAAS,aAAa,GAAmB;AACvC,QAAO,EAAE,QAAQ,uBAAuB,OAAO;;AAGjD,SAAS,kBAAkB,MAAsB;AAC/C,KAAI,CAAC,KACH,OAAM,IAAI,MAAM,iDAAiD;AAGnE,KAAI,CAAC,KAAK,WAAW,IAAI,CACvB,QAAO,IAAI;AAGb,KAAI,KAAK,SAAS,KAAK,KAAK,SAAS,IAAI,CACvC,QAAO,KAAK,MAAM,GAAG,GAAG;AAG1B,QAAO"}
|
|
@@ -4,10 +4,16 @@ let _copilotkit_shared = require("@copilotkit/shared");
|
|
|
4
4
|
let _remix_run_node_fetch_server = require("@remix-run/node-fetch-server");
|
|
5
5
|
|
|
6
6
|
//#region src/v2/runtime/endpoints/node-fetch-handler.ts
|
|
7
|
+
const METHODS_WITHOUT_BODY = new Set([
|
|
8
|
+
"GET",
|
|
9
|
+
"HEAD",
|
|
10
|
+
"OPTIONS"
|
|
11
|
+
]);
|
|
7
12
|
function createCopilotNodeHandler(handler) {
|
|
8
13
|
return async (req, res) => {
|
|
9
14
|
try {
|
|
10
|
-
|
|
15
|
+
const method = (req.method ?? "GET").toUpperCase();
|
|
16
|
+
await (0, _remix_run_node_fetch_server.sendResponse)(res, await handler(!METHODS_WITHOUT_BODY.has(method) && hasPreParsedBody(req) ? buildPreParsedRequest(req, res) : (0, _remix_run_node_fetch_server.createRequest)(req, res)));
|
|
11
17
|
} catch (err) {
|
|
12
18
|
_copilotkit_shared.logger.error({ err }, "Error in Node fetch handler");
|
|
13
19
|
if (!res.headersSent) {
|
|
@@ -19,6 +25,50 @@ function createCopilotNodeHandler(handler) {
|
|
|
19
25
|
}
|
|
20
26
|
/** @deprecated Use `createCopilotNodeHandler` instead. */
|
|
21
27
|
const createNodeFetchHandler = createCopilotNodeHandler;
|
|
28
|
+
/**
|
|
29
|
+
* Build a Fetch Request from a Node IncomingMessage whose body stream has
|
|
30
|
+
* already been consumed by a body parser.
|
|
31
|
+
*
|
|
32
|
+
* The URL, headers and abort wiring match `createRequest` (the streaming path),
|
|
33
|
+
* so a request routes the same way whether or not a parser ran first. In
|
|
34
|
+
* particular the URL comes from `req.url`, which is what a mounting router
|
|
35
|
+
* matched against; `originalUrl` still carries the mount prefix and would miss
|
|
36
|
+
* the handler's `basePath`.
|
|
37
|
+
*/
|
|
38
|
+
function buildPreParsedRequest(req, res) {
|
|
39
|
+
const method = (req.method ?? "GET").toUpperCase();
|
|
40
|
+
const headers = (0, _remix_run_node_fetch_server.createHeaders)(req);
|
|
41
|
+
const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
|
|
42
|
+
const host = headers.get("host") ?? "localhost";
|
|
43
|
+
const url = new URL(req.url ?? "/", `${protocol}//${host}`);
|
|
44
|
+
const controller = new AbortController();
|
|
45
|
+
res.on("close", () => {
|
|
46
|
+
if (!res.writableFinished) controller.abort();
|
|
47
|
+
});
|
|
48
|
+
const init = {
|
|
49
|
+
method,
|
|
50
|
+
headers,
|
|
51
|
+
signal: controller.signal
|
|
52
|
+
};
|
|
53
|
+
const { body, contentType } = synthesizeBody(req.body);
|
|
54
|
+
if (contentType) headers.set("content-type", contentType);
|
|
55
|
+
headers.delete("content-length");
|
|
56
|
+
if (body !== void 0) init.body = body;
|
|
57
|
+
return new Request(url, init);
|
|
58
|
+
}
|
|
59
|
+
function hasPreParsedBody(req) {
|
|
60
|
+
if (req.body === void 0 || req.body === null) return false;
|
|
61
|
+
return Boolean(req.readableEnded);
|
|
62
|
+
}
|
|
63
|
+
function synthesizeBody(body) {
|
|
64
|
+
if (Buffer.isBuffer(body) || body instanceof Uint8Array) return { body };
|
|
65
|
+
if (typeof body === "string") return { body };
|
|
66
|
+
if (typeof body === "object" && body !== null) return {
|
|
67
|
+
body: JSON.stringify(body),
|
|
68
|
+
contentType: "application/json"
|
|
69
|
+
};
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
22
72
|
|
|
23
73
|
//#endregion
|
|
24
74
|
exports.createCopilotNodeHandler = createCopilotNodeHandler;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node-fetch-handler.cjs","names":[],"sources":["../../../../src/v2/runtime/endpoints/node-fetch-handler.ts"],"sourcesContent":["/**\n * Generic Node ↔ Fetch bridge for CopilotKit runtime.\n *\n * Wraps a `CopilotRuntimeFetchHandler` as a Node HTTP handler using\n * `@remix-run/node-fetch-server` for reliable streaming and conversion.\n *\n * @example\n * ```typescript\n * import { createServer } from \"node:http\";\n * import { createCopilotRuntimeHandler } from \"@copilotkit/runtime/v2\";\n * import { createCopilotNodeHandler } from \"@copilotkit/runtime/v2/node\";\n *\n * const handler = createCopilotRuntimeHandler({ runtime, basePath: \"/api/copilotkit\", cors: true });\n * const nodeHandler = createCopilotNodeHandler(handler);\n * createServer(nodeHandler).listen(3000);\n * ```\n */\n\nimport type { IncomingMessage, ServerResponse } from \"node:http\";\nimport {
|
|
1
|
+
{"version":3,"file":"node-fetch-handler.cjs","names":[],"sources":["../../../../src/v2/runtime/endpoints/node-fetch-handler.ts"],"sourcesContent":["/**\n * Generic Node ↔ Fetch bridge for CopilotKit runtime.\n *\n * Wraps a `CopilotRuntimeFetchHandler` as a Node HTTP handler using\n * `@remix-run/node-fetch-server` for reliable streaming and conversion.\n *\n * When body-parsing middleware (Express `express.json()`, NestJS's default\n * parser, …) runs first, the Node request stream is already consumed and\n * `req.body` holds the parsed content. Reading the exhausted stream would hang,\n * so that case re-serialises `req.body` into the Fetch `Request` instead.\n *\n * @example\n * ```typescript\n * import { createServer } from \"node:http\";\n * import { createCopilotRuntimeHandler } from \"@copilotkit/runtime/v2\";\n * import { createCopilotNodeHandler } from \"@copilotkit/runtime/v2/node\";\n *\n * const handler = createCopilotRuntimeHandler({ runtime, basePath: \"/api/copilotkit\", cors: true });\n * const nodeHandler = createCopilotNodeHandler(handler);\n * createServer(nodeHandler).listen(3000);\n * ```\n */\n\nimport type { IncomingMessage, ServerResponse } from \"node:http\";\nimport {\n createHeaders,\n createRequest,\n sendResponse,\n} from \"@remix-run/node-fetch-server\";\nimport type { CopilotRuntimeFetchHandler } from \"../core/fetch-handler\";\nimport { logger } from \"@copilotkit/shared\";\n\nconst METHODS_WITHOUT_BODY = new Set([\"GET\", \"HEAD\", \"OPTIONS\"]);\n\nexport type NodeFetchHandler = (\n req: IncomingMessage,\n res: ServerResponse,\n) => Promise<void>;\n\nexport function createCopilotNodeHandler(\n handler: CopilotRuntimeFetchHandler,\n): NodeFetchHandler {\n return async (req: IncomingMessage, res: ServerResponse) => {\n try {\n const method = (req.method ?? \"GET\").toUpperCase();\n const fetchReq =\n !METHODS_WITHOUT_BODY.has(method) && hasPreParsedBody(req)\n ? buildPreParsedRequest(req, res)\n : createRequest(req, res);\n const fetchRes = await handler(fetchReq);\n await sendResponse(res, fetchRes);\n } catch (err: unknown) {\n logger.error({ err }, \"Error in Node fetch handler\");\n if (!res.headersSent) {\n res.statusCode = 500;\n res.end(\"Internal Server Error\");\n }\n }\n };\n}\n\n/** @deprecated Use `createCopilotNodeHandler` instead. */\nexport const createNodeFetchHandler = createCopilotNodeHandler;\n\n/**\n * Build a Fetch Request from a Node IncomingMessage whose body stream has\n * already been consumed by a body parser.\n *\n * The URL, headers and abort wiring match `createRequest` (the streaming path),\n * so a request routes the same way whether or not a parser ran first. In\n * particular the URL comes from `req.url`, which is what a mounting router\n * matched against; `originalUrl` still carries the mount prefix and would miss\n * the handler's `basePath`.\n */\nfunction buildPreParsedRequest(\n req: IncomingMessage & { body?: unknown },\n res: ServerResponse,\n): Request {\n const method = (req.method ?? \"GET\").toUpperCase();\n const headers = createHeaders(req);\n const protocol =\n \"encrypted\" in req.socket && req.socket.encrypted ? \"https:\" : \"http:\";\n const host = headers.get(\"host\") ?? \"localhost\";\n const url = new URL(req.url ?? \"/\", `${protocol}//${host}`);\n\n // Wire an AbortSignal so client disconnects propagate to the fetch handler\n const controller = new AbortController();\n res.on(\"close\", () => {\n if (!res.writableFinished) controller.abort();\n });\n\n const init: RequestInit = {\n method,\n headers,\n signal: controller.signal,\n };\n\n const { body, contentType } = synthesizeBody(req.body);\n if (contentType) {\n headers.set(\"content-type\", contentType);\n }\n headers.delete(\"content-length\");\n if (body !== undefined) {\n init.body = body;\n }\n\n return new Request(url, init);\n}\n\nfunction hasPreParsedBody(req: IncomingMessage & { body?: unknown }): boolean {\n if (req.body === undefined || req.body === null) return false;\n\n // Only `readableEnded` means \"a parser drained this stream to its end\".\n // `req.complete` and the private `_readableState.ended` are set by the HTTP\n // parser once the socket has all the bytes, whether or not anything read them.\n //\n // That distinction matters because `req.body` being set does not prove a parser\n // ran: body-parser 1.x (Express 4) assigns `req.body = req.body || {}` before\n // its own content-type checks, so a request it declines to parse — multipart\n // upload, text/plain — reaches us with `req.body === {}` and a full, unread\n // stream. Treating that as pre-parsed rebuilt the request from `{}` and\n // dropped the real payload.\n return Boolean(req.readableEnded);\n}\n\nfunction synthesizeBody(body: unknown): {\n body?: BodyInit;\n contentType?: string;\n} {\n if (Buffer.isBuffer(body) || body instanceof Uint8Array) {\n // Buffer/Uint8Array<ArrayBufferLike> are valid fetch bodies at runtime,\n // but the DOM lib's BodyInit only admits ArrayBuffer-backed views.\n return { body: body as BodyInit };\n }\n if (typeof body === \"string\") {\n return { body };\n }\n if (typeof body === \"object\" && body !== null) {\n return { body: JSON.stringify(body), contentType: \"application/json\" };\n }\n return {};\n}\n"],"mappings":";;;;;;AAgCA,MAAM,uBAAuB,IAAI,IAAI;CAAC;CAAO;CAAQ;CAAU,CAAC;AAOhE,SAAgB,yBACd,SACkB;AAClB,QAAO,OAAO,KAAsB,QAAwB;AAC1D,MAAI;GACF,MAAM,UAAU,IAAI,UAAU,OAAO,aAAa;AAMlD,wDAAmB,KADF,MAAM,QAHrB,CAAC,qBAAqB,IAAI,OAAO,IAAI,iBAAiB,IAAI,GACtD,sBAAsB,KAAK,IAAI,mDACjB,KAAK,IAAI,CACW,CACP;WAC1B,KAAc;AACrB,6BAAO,MAAM,EAAE,KAAK,EAAE,8BAA8B;AACpD,OAAI,CAAC,IAAI,aAAa;AACpB,QAAI,aAAa;AACjB,QAAI,IAAI,wBAAwB;;;;;;AAOxC,MAAa,yBAAyB;;;;;;;;;;;AAYtC,SAAS,sBACP,KACA,KACS;CACT,MAAM,UAAU,IAAI,UAAU,OAAO,aAAa;CAClD,MAAM,0DAAwB,IAAI;CAClC,MAAM,WACJ,eAAe,IAAI,UAAU,IAAI,OAAO,YAAY,WAAW;CACjE,MAAM,OAAO,QAAQ,IAAI,OAAO,IAAI;CACpC,MAAM,MAAM,IAAI,IAAI,IAAI,OAAO,KAAK,GAAG,SAAS,IAAI,OAAO;CAG3D,MAAM,aAAa,IAAI,iBAAiB;AACxC,KAAI,GAAG,eAAe;AACpB,MAAI,CAAC,IAAI,iBAAkB,YAAW,OAAO;GAC7C;CAEF,MAAM,OAAoB;EACxB;EACA;EACA,QAAQ,WAAW;EACpB;CAED,MAAM,EAAE,MAAM,gBAAgB,eAAe,IAAI,KAAK;AACtD,KAAI,YACF,SAAQ,IAAI,gBAAgB,YAAY;AAE1C,SAAQ,OAAO,iBAAiB;AAChC,KAAI,SAAS,OACX,MAAK,OAAO;AAGd,QAAO,IAAI,QAAQ,KAAK,KAAK;;AAG/B,SAAS,iBAAiB,KAAoD;AAC5E,KAAI,IAAI,SAAS,UAAa,IAAI,SAAS,KAAM,QAAO;AAYxD,QAAO,QAAQ,IAAI,cAAc;;AAGnC,SAAS,eAAe,MAGtB;AACA,KAAI,OAAO,SAAS,KAAK,IAAI,gBAAgB,WAG3C,QAAO,EAAQ,MAAkB;AAEnC,KAAI,OAAO,SAAS,SAClB,QAAO,EAAE,MAAM;AAEjB,KAAI,OAAO,SAAS,YAAY,SAAS,KACvC,QAAO;EAAE,MAAM,KAAK,UAAU,KAAK;EAAE,aAAa;EAAoB;AAExE,QAAO,EAAE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node-fetch-handler.d.cts","names":[],"sources":["../../../../src/v2/runtime/endpoints/node-fetch-handler.ts"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"node-fetch-handler.d.cts","names":[],"sources":["../../../../src/v2/runtime/endpoints/node-fetch-handler.ts"],"mappings":";;;;;KAkCY,gBAAA,IACV,GAAA,EAAK,eAAA,EACL,GAAA,EAAK,cAAA,KACF,OAAA;AAAA,iBAEW,wBAAA,CACd,OAAA,EAAS,0BAAA,GACR,gBAAA;;cAqBU,sBAAA,SAAsB,wBAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node-fetch-handler.d.mts","names":[],"sources":["../../../../src/v2/runtime/endpoints/node-fetch-handler.ts"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"node-fetch-handler.d.mts","names":[],"sources":["../../../../src/v2/runtime/endpoints/node-fetch-handler.ts"],"mappings":";;;;;KAkCY,gBAAA,IACV,GAAA,EAAK,eAAA,EACL,GAAA,EAAK,cAAA,KACF,OAAA;AAAA,iBAEW,wBAAA,CACd,OAAA,EAAS,0BAAA,GACR,gBAAA;;cAqBU,sBAAA,SAAsB,wBAAA"}
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
2
|
import { logger } from "@copilotkit/shared";
|
|
3
|
-
import { createRequest, sendResponse } from "@remix-run/node-fetch-server";
|
|
3
|
+
import { createHeaders, createRequest, sendResponse } from "@remix-run/node-fetch-server";
|
|
4
4
|
|
|
5
5
|
//#region src/v2/runtime/endpoints/node-fetch-handler.ts
|
|
6
|
+
const METHODS_WITHOUT_BODY = new Set([
|
|
7
|
+
"GET",
|
|
8
|
+
"HEAD",
|
|
9
|
+
"OPTIONS"
|
|
10
|
+
]);
|
|
6
11
|
function createCopilotNodeHandler(handler) {
|
|
7
12
|
return async (req, res) => {
|
|
8
13
|
try {
|
|
9
|
-
|
|
14
|
+
const method = (req.method ?? "GET").toUpperCase();
|
|
15
|
+
await sendResponse(res, await handler(!METHODS_WITHOUT_BODY.has(method) && hasPreParsedBody(req) ? buildPreParsedRequest(req, res) : createRequest(req, res)));
|
|
10
16
|
} catch (err) {
|
|
11
17
|
logger.error({ err }, "Error in Node fetch handler");
|
|
12
18
|
if (!res.headersSent) {
|
|
@@ -18,6 +24,50 @@ function createCopilotNodeHandler(handler) {
|
|
|
18
24
|
}
|
|
19
25
|
/** @deprecated Use `createCopilotNodeHandler` instead. */
|
|
20
26
|
const createNodeFetchHandler = createCopilotNodeHandler;
|
|
27
|
+
/**
|
|
28
|
+
* Build a Fetch Request from a Node IncomingMessage whose body stream has
|
|
29
|
+
* already been consumed by a body parser.
|
|
30
|
+
*
|
|
31
|
+
* The URL, headers and abort wiring match `createRequest` (the streaming path),
|
|
32
|
+
* so a request routes the same way whether or not a parser ran first. In
|
|
33
|
+
* particular the URL comes from `req.url`, which is what a mounting router
|
|
34
|
+
* matched against; `originalUrl` still carries the mount prefix and would miss
|
|
35
|
+
* the handler's `basePath`.
|
|
36
|
+
*/
|
|
37
|
+
function buildPreParsedRequest(req, res) {
|
|
38
|
+
const method = (req.method ?? "GET").toUpperCase();
|
|
39
|
+
const headers = createHeaders(req);
|
|
40
|
+
const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
|
|
41
|
+
const host = headers.get("host") ?? "localhost";
|
|
42
|
+
const url = new URL(req.url ?? "/", `${protocol}//${host}`);
|
|
43
|
+
const controller = new AbortController();
|
|
44
|
+
res.on("close", () => {
|
|
45
|
+
if (!res.writableFinished) controller.abort();
|
|
46
|
+
});
|
|
47
|
+
const init = {
|
|
48
|
+
method,
|
|
49
|
+
headers,
|
|
50
|
+
signal: controller.signal
|
|
51
|
+
};
|
|
52
|
+
const { body, contentType } = synthesizeBody(req.body);
|
|
53
|
+
if (contentType) headers.set("content-type", contentType);
|
|
54
|
+
headers.delete("content-length");
|
|
55
|
+
if (body !== void 0) init.body = body;
|
|
56
|
+
return new Request(url, init);
|
|
57
|
+
}
|
|
58
|
+
function hasPreParsedBody(req) {
|
|
59
|
+
if (req.body === void 0 || req.body === null) return false;
|
|
60
|
+
return Boolean(req.readableEnded);
|
|
61
|
+
}
|
|
62
|
+
function synthesizeBody(body) {
|
|
63
|
+
if (Buffer.isBuffer(body) || body instanceof Uint8Array) return { body };
|
|
64
|
+
if (typeof body === "string") return { body };
|
|
65
|
+
if (typeof body === "object" && body !== null) return {
|
|
66
|
+
body: JSON.stringify(body),
|
|
67
|
+
contentType: "application/json"
|
|
68
|
+
};
|
|
69
|
+
return {};
|
|
70
|
+
}
|
|
21
71
|
|
|
22
72
|
//#endregion
|
|
23
73
|
export { createCopilotNodeHandler, createNodeFetchHandler };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node-fetch-handler.mjs","names":[],"sources":["../../../../src/v2/runtime/endpoints/node-fetch-handler.ts"],"sourcesContent":["/**\n * Generic Node ↔ Fetch bridge for CopilotKit runtime.\n *\n * Wraps a `CopilotRuntimeFetchHandler` as a Node HTTP handler using\n * `@remix-run/node-fetch-server` for reliable streaming and conversion.\n *\n * @example\n * ```typescript\n * import { createServer } from \"node:http\";\n * import { createCopilotRuntimeHandler } from \"@copilotkit/runtime/v2\";\n * import { createCopilotNodeHandler } from \"@copilotkit/runtime/v2/node\";\n *\n * const handler = createCopilotRuntimeHandler({ runtime, basePath: \"/api/copilotkit\", cors: true });\n * const nodeHandler = createCopilotNodeHandler(handler);\n * createServer(nodeHandler).listen(3000);\n * ```\n */\n\nimport type { IncomingMessage, ServerResponse } from \"node:http\";\nimport {
|
|
1
|
+
{"version":3,"file":"node-fetch-handler.mjs","names":[],"sources":["../../../../src/v2/runtime/endpoints/node-fetch-handler.ts"],"sourcesContent":["/**\n * Generic Node ↔ Fetch bridge for CopilotKit runtime.\n *\n * Wraps a `CopilotRuntimeFetchHandler` as a Node HTTP handler using\n * `@remix-run/node-fetch-server` for reliable streaming and conversion.\n *\n * When body-parsing middleware (Express `express.json()`, NestJS's default\n * parser, …) runs first, the Node request stream is already consumed and\n * `req.body` holds the parsed content. Reading the exhausted stream would hang,\n * so that case re-serialises `req.body` into the Fetch `Request` instead.\n *\n * @example\n * ```typescript\n * import { createServer } from \"node:http\";\n * import { createCopilotRuntimeHandler } from \"@copilotkit/runtime/v2\";\n * import { createCopilotNodeHandler } from \"@copilotkit/runtime/v2/node\";\n *\n * const handler = createCopilotRuntimeHandler({ runtime, basePath: \"/api/copilotkit\", cors: true });\n * const nodeHandler = createCopilotNodeHandler(handler);\n * createServer(nodeHandler).listen(3000);\n * ```\n */\n\nimport type { IncomingMessage, ServerResponse } from \"node:http\";\nimport {\n createHeaders,\n createRequest,\n sendResponse,\n} from \"@remix-run/node-fetch-server\";\nimport type { CopilotRuntimeFetchHandler } from \"../core/fetch-handler\";\nimport { logger } from \"@copilotkit/shared\";\n\nconst METHODS_WITHOUT_BODY = new Set([\"GET\", \"HEAD\", \"OPTIONS\"]);\n\nexport type NodeFetchHandler = (\n req: IncomingMessage,\n res: ServerResponse,\n) => Promise<void>;\n\nexport function createCopilotNodeHandler(\n handler: CopilotRuntimeFetchHandler,\n): NodeFetchHandler {\n return async (req: IncomingMessage, res: ServerResponse) => {\n try {\n const method = (req.method ?? \"GET\").toUpperCase();\n const fetchReq =\n !METHODS_WITHOUT_BODY.has(method) && hasPreParsedBody(req)\n ? buildPreParsedRequest(req, res)\n : createRequest(req, res);\n const fetchRes = await handler(fetchReq);\n await sendResponse(res, fetchRes);\n } catch (err: unknown) {\n logger.error({ err }, \"Error in Node fetch handler\");\n if (!res.headersSent) {\n res.statusCode = 500;\n res.end(\"Internal Server Error\");\n }\n }\n };\n}\n\n/** @deprecated Use `createCopilotNodeHandler` instead. */\nexport const createNodeFetchHandler = createCopilotNodeHandler;\n\n/**\n * Build a Fetch Request from a Node IncomingMessage whose body stream has\n * already been consumed by a body parser.\n *\n * The URL, headers and abort wiring match `createRequest` (the streaming path),\n * so a request routes the same way whether or not a parser ran first. In\n * particular the URL comes from `req.url`, which is what a mounting router\n * matched against; `originalUrl` still carries the mount prefix and would miss\n * the handler's `basePath`.\n */\nfunction buildPreParsedRequest(\n req: IncomingMessage & { body?: unknown },\n res: ServerResponse,\n): Request {\n const method = (req.method ?? \"GET\").toUpperCase();\n const headers = createHeaders(req);\n const protocol =\n \"encrypted\" in req.socket && req.socket.encrypted ? \"https:\" : \"http:\";\n const host = headers.get(\"host\") ?? \"localhost\";\n const url = new URL(req.url ?? \"/\", `${protocol}//${host}`);\n\n // Wire an AbortSignal so client disconnects propagate to the fetch handler\n const controller = new AbortController();\n res.on(\"close\", () => {\n if (!res.writableFinished) controller.abort();\n });\n\n const init: RequestInit = {\n method,\n headers,\n signal: controller.signal,\n };\n\n const { body, contentType } = synthesizeBody(req.body);\n if (contentType) {\n headers.set(\"content-type\", contentType);\n }\n headers.delete(\"content-length\");\n if (body !== undefined) {\n init.body = body;\n }\n\n return new Request(url, init);\n}\n\nfunction hasPreParsedBody(req: IncomingMessage & { body?: unknown }): boolean {\n if (req.body === undefined || req.body === null) return false;\n\n // Only `readableEnded` means \"a parser drained this stream to its end\".\n // `req.complete` and the private `_readableState.ended` are set by the HTTP\n // parser once the socket has all the bytes, whether or not anything read them.\n //\n // That distinction matters because `req.body` being set does not prove a parser\n // ran: body-parser 1.x (Express 4) assigns `req.body = req.body || {}` before\n // its own content-type checks, so a request it declines to parse — multipart\n // upload, text/plain — reaches us with `req.body === {}` and a full, unread\n // stream. Treating that as pre-parsed rebuilt the request from `{}` and\n // dropped the real payload.\n return Boolean(req.readableEnded);\n}\n\nfunction synthesizeBody(body: unknown): {\n body?: BodyInit;\n contentType?: string;\n} {\n if (Buffer.isBuffer(body) || body instanceof Uint8Array) {\n // Buffer/Uint8Array<ArrayBufferLike> are valid fetch bodies at runtime,\n // but the DOM lib's BodyInit only admits ArrayBuffer-backed views.\n return { body: body as BodyInit };\n }\n if (typeof body === \"string\") {\n return { body };\n }\n if (typeof body === \"object\" && body !== null) {\n return { body: JSON.stringify(body), contentType: \"application/json\" };\n }\n return {};\n}\n"],"mappings":";;;;;AAgCA,MAAM,uBAAuB,IAAI,IAAI;CAAC;CAAO;CAAQ;CAAU,CAAC;AAOhE,SAAgB,yBACd,SACkB;AAClB,QAAO,OAAO,KAAsB,QAAwB;AAC1D,MAAI;GACF,MAAM,UAAU,IAAI,UAAU,OAAO,aAAa;AAMlD,SAAM,aAAa,KADF,MAAM,QAHrB,CAAC,qBAAqB,IAAI,OAAO,IAAI,iBAAiB,IAAI,GACtD,sBAAsB,KAAK,IAAI,GAC/B,cAAc,KAAK,IAAI,CACW,CACP;WAC1B,KAAc;AACrB,UAAO,MAAM,EAAE,KAAK,EAAE,8BAA8B;AACpD,OAAI,CAAC,IAAI,aAAa;AACpB,QAAI,aAAa;AACjB,QAAI,IAAI,wBAAwB;;;;;;AAOxC,MAAa,yBAAyB;;;;;;;;;;;AAYtC,SAAS,sBACP,KACA,KACS;CACT,MAAM,UAAU,IAAI,UAAU,OAAO,aAAa;CAClD,MAAM,UAAU,cAAc,IAAI;CAClC,MAAM,WACJ,eAAe,IAAI,UAAU,IAAI,OAAO,YAAY,WAAW;CACjE,MAAM,OAAO,QAAQ,IAAI,OAAO,IAAI;CACpC,MAAM,MAAM,IAAI,IAAI,IAAI,OAAO,KAAK,GAAG,SAAS,IAAI,OAAO;CAG3D,MAAM,aAAa,IAAI,iBAAiB;AACxC,KAAI,GAAG,eAAe;AACpB,MAAI,CAAC,IAAI,iBAAkB,YAAW,OAAO;GAC7C;CAEF,MAAM,OAAoB;EACxB;EACA;EACA,QAAQ,WAAW;EACpB;CAED,MAAM,EAAE,MAAM,gBAAgB,eAAe,IAAI,KAAK;AACtD,KAAI,YACF,SAAQ,IAAI,gBAAgB,YAAY;AAE1C,SAAQ,OAAO,iBAAiB;AAChC,KAAI,SAAS,OACX,MAAK,OAAO;AAGd,QAAO,IAAI,QAAQ,KAAK,KAAK;;AAG/B,SAAS,iBAAiB,KAAoD;AAC5E,KAAI,IAAI,SAAS,UAAa,IAAI,SAAS,KAAM,QAAO;AAYxD,QAAO,QAAQ,IAAI,cAAc;;AAGnC,SAAS,eAAe,MAGtB;AACA,KAAI,OAAO,SAAS,KAAK,IAAI,gBAAgB,WAG3C,QAAO,EAAQ,MAAkB;AAEnC,KAAI,OAAO,SAAS,SAClB,QAAO,EAAE,MAAM;AAEjB,KAAI,OAAO,SAAS,YAAY,SAAS,KACvC,QAAO;EAAE,MAAM,KAAK,UAAU,KAAK;EAAE,aAAa;EAAoB;AAExE,QAAO,EAAE"}
|