@copilotkit/runtime 1.55.0-next.9 → 1.55.1-next.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/CHANGELOG.md +31 -2
- package/dist/agent/index.cjs +101 -12
- package/dist/agent/index.cjs.map +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 +102 -13
- package/dist/agent/index.mjs.map +1 -1
- package/dist/lib/runtime/agent-integrations/langgraph/agent.cjs.map +1 -1
- package/dist/lib/runtime/agent-integrations/langgraph/agent.d.cts +4 -841
- package/dist/lib/runtime/agent-integrations/langgraph/agent.d.cts.map +1 -1
- package/dist/lib/runtime/agent-integrations/langgraph/agent.d.mts +4 -841
- package/dist/lib/runtime/agent-integrations/langgraph/agent.d.mts.map +1 -1
- package/dist/lib/runtime/agent-integrations/langgraph/agent.mjs.map +1 -1
- package/dist/lib/runtime/copilot-runtime.cjs +3 -2
- package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.cts +1 -1
- package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.mts +3 -3
- package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.mjs +3 -2
- package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
- package/dist/package.cjs +70 -47
- package/dist/package.mjs +70 -47
- package/dist/v2/express.cjs +8 -0
- package/dist/v2/express.d.cts +5 -0
- package/dist/v2/express.d.mts +5 -0
- package/dist/v2/express.mjs +5 -0
- package/dist/v2/hono.cjs +9 -0
- package/dist/v2/hono.d.cts +5 -0
- package/dist/v2/hono.d.mts +5 -0
- package/dist/v2/hono.mjs +5 -0
- package/dist/v2/index.cjs +8 -3
- package/dist/v2/index.d.cts +8 -5
- package/dist/v2/index.d.mts +8 -5
- package/dist/v2/index.mjs +5 -4
- package/dist/v2/node.cjs +8 -0
- package/dist/v2/node.d.cts +5 -0
- package/dist/v2/node.d.mts +5 -0
- package/dist/v2/node.mjs +5 -0
- package/dist/v2/runtime/core/fetch-cors.cjs +72 -0
- package/dist/v2/runtime/core/fetch-cors.cjs.map +1 -0
- package/dist/v2/runtime/core/fetch-cors.d.cts +20 -0
- package/dist/v2/runtime/core/fetch-cors.d.cts.map +1 -0
- package/dist/v2/runtime/core/fetch-cors.d.mts +20 -0
- package/dist/v2/runtime/core/fetch-cors.d.mts.map +1 -0
- package/dist/v2/runtime/core/fetch-cors.mjs +70 -0
- package/dist/v2/runtime/core/fetch-cors.mjs.map +1 -0
- package/dist/v2/runtime/core/fetch-handler.cjs +232 -0
- package/dist/v2/runtime/core/fetch-handler.cjs.map +1 -0
- package/dist/v2/runtime/core/fetch-handler.d.cts +40 -0
- package/dist/v2/runtime/core/fetch-handler.d.cts.map +1 -0
- package/dist/v2/runtime/core/fetch-handler.d.mts +40 -0
- package/dist/v2/runtime/core/fetch-handler.d.mts.map +1 -0
- package/dist/v2/runtime/core/fetch-handler.mjs +231 -0
- package/dist/v2/runtime/core/fetch-handler.mjs.map +1 -0
- package/dist/v2/runtime/core/fetch-router.cjs +68 -0
- package/dist/v2/runtime/core/fetch-router.cjs.map +1 -0
- package/dist/v2/runtime/core/fetch-router.mjs +67 -0
- package/dist/v2/runtime/core/fetch-router.mjs.map +1 -0
- package/dist/v2/runtime/core/hooks.cjs +29 -0
- package/dist/v2/runtime/core/hooks.cjs.map +1 -0
- package/dist/v2/runtime/core/hooks.d.cts +78 -0
- package/dist/v2/runtime/core/hooks.d.cts.map +1 -0
- package/dist/v2/runtime/core/hooks.d.mts +78 -0
- package/dist/v2/runtime/core/hooks.d.mts.map +1 -0
- package/dist/v2/runtime/core/hooks.mjs +25 -0
- package/dist/v2/runtime/core/hooks.mjs.map +1 -0
- package/dist/v2/runtime/{middleware-sse-parser.cjs → core/middleware-sse-parser.cjs} +2 -2
- package/dist/v2/runtime/core/middleware-sse-parser.cjs.map +1 -0
- package/dist/v2/runtime/{middleware-sse-parser.d.cts → core/middleware-sse-parser.d.cts} +1 -1
- package/dist/v2/runtime/core/middleware-sse-parser.d.cts.map +1 -0
- package/dist/v2/runtime/{middleware-sse-parser.d.mts → core/middleware-sse-parser.d.mts} +1 -1
- package/dist/v2/runtime/core/middleware-sse-parser.d.mts.map +1 -0
- package/dist/v2/runtime/{middleware-sse-parser.mjs → core/middleware-sse-parser.mjs} +1 -1
- package/dist/v2/runtime/core/middleware-sse-parser.mjs.map +1 -0
- package/dist/v2/runtime/{middleware.cjs → core/middleware.cjs} +2 -2
- package/dist/v2/runtime/core/middleware.cjs.map +1 -0
- package/dist/v2/runtime/{middleware.d.cts → core/middleware.d.cts} +1 -1
- package/dist/v2/runtime/core/middleware.d.cts.map +1 -0
- package/dist/v2/runtime/{middleware.d.mts → core/middleware.d.mts} +1 -1
- package/dist/v2/runtime/core/middleware.d.mts.map +1 -0
- package/dist/v2/runtime/{middleware.mjs → core/middleware.mjs} +1 -1
- package/dist/v2/runtime/core/middleware.mjs.map +1 -0
- package/dist/v2/runtime/{runtime.cjs → core/runtime.cjs} +35 -10
- package/dist/v2/runtime/core/runtime.cjs.map +1 -0
- package/dist/v2/runtime/{runtime.d.cts → core/runtime.d.cts} +41 -7
- package/dist/v2/runtime/core/runtime.d.cts.map +1 -0
- package/dist/v2/runtime/{runtime.d.mts → core/runtime.d.mts} +42 -8
- package/dist/v2/runtime/core/runtime.d.mts.map +1 -0
- package/dist/v2/runtime/{runtime.mjs → core/runtime.mjs} +36 -11
- package/dist/v2/runtime/core/runtime.mjs.map +1 -0
- package/dist/v2/runtime/endpoints/express-fetch-bridge.cjs +83 -0
- package/dist/v2/runtime/endpoints/express-fetch-bridge.cjs.map +1 -0
- package/dist/v2/runtime/endpoints/express-fetch-bridge.mjs +82 -0
- package/dist/v2/runtime/endpoints/express-fetch-bridge.mjs.map +1 -0
- package/dist/v2/runtime/endpoints/express-single.cjs +35 -181
- package/dist/v2/runtime/endpoints/express-single.cjs.map +1 -1
- package/dist/v2/runtime/endpoints/express-single.d.cts +35 -2
- package/dist/v2/runtime/endpoints/express-single.d.cts.map +1 -1
- package/dist/v2/runtime/endpoints/express-single.d.mts +35 -2
- package/dist/v2/runtime/endpoints/express-single.d.mts.map +1 -1
- package/dist/v2/runtime/endpoints/express-single.mjs +35 -178
- package/dist/v2/runtime/endpoints/express-single.mjs.map +1 -1
- package/dist/v2/runtime/endpoints/express.cjs +41 -195
- package/dist/v2/runtime/endpoints/express.cjs.map +1 -1
- package/dist/v2/runtime/endpoints/express.d.cts +26 -4
- package/dist/v2/runtime/endpoints/express.d.cts.map +1 -1
- package/dist/v2/runtime/endpoints/express.d.mts +26 -4
- package/dist/v2/runtime/endpoints/express.d.mts.map +1 -1
- package/dist/v2/runtime/endpoints/express.mjs +41 -195
- package/dist/v2/runtime/endpoints/express.mjs.map +1 -1
- package/dist/v2/runtime/endpoints/hono-single.cjs +11 -123
- package/dist/v2/runtime/endpoints/hono-single.cjs.map +1 -1
- package/dist/v2/runtime/endpoints/hono-single.d.cts +14 -11
- package/dist/v2/runtime/endpoints/hono-single.d.cts.map +1 -1
- package/dist/v2/runtime/endpoints/hono-single.d.mts +14 -11
- package/dist/v2/runtime/endpoints/hono-single.d.mts.map +1 -1
- package/dist/v2/runtime/endpoints/hono-single.mjs +11 -123
- package/dist/v2/runtime/endpoints/hono-single.mjs.map +1 -1
- package/dist/v2/runtime/endpoints/hono.cjs +23 -237
- package/dist/v2/runtime/endpoints/hono.cjs.map +1 -1
- package/dist/v2/runtime/endpoints/hono.d.cts +29 -120
- package/dist/v2/runtime/endpoints/hono.d.cts.map +1 -1
- package/dist/v2/runtime/endpoints/hono.d.mts +29 -120
- package/dist/v2/runtime/endpoints/hono.d.mts.map +1 -1
- package/dist/v2/runtime/endpoints/hono.mjs +22 -238
- package/dist/v2/runtime/endpoints/hono.mjs.map +1 -1
- package/dist/v2/runtime/endpoints/index.d.cts +2 -2
- package/dist/v2/runtime/endpoints/index.d.mts +2 -2
- package/dist/v2/runtime/endpoints/node-fetch-handler.cjs +26 -0
- package/dist/v2/runtime/endpoints/node-fetch-handler.cjs.map +1 -0
- package/dist/v2/runtime/endpoints/node-fetch-handler.d.cts +12 -0
- package/dist/v2/runtime/endpoints/node-fetch-handler.d.cts.map +1 -0
- package/dist/v2/runtime/endpoints/node-fetch-handler.d.mts +12 -0
- package/dist/v2/runtime/endpoints/node-fetch-handler.d.mts.map +1 -0
- package/dist/v2/runtime/endpoints/node-fetch-handler.mjs +24 -0
- package/dist/v2/runtime/endpoints/node-fetch-handler.mjs.map +1 -0
- package/dist/v2/runtime/endpoints/node.cjs +30 -0
- package/dist/v2/runtime/endpoints/node.cjs.map +1 -0
- package/dist/v2/runtime/endpoints/node.d.cts +27 -0
- package/dist/v2/runtime/endpoints/node.d.cts.map +1 -0
- package/dist/v2/runtime/endpoints/node.d.mts +27 -0
- package/dist/v2/runtime/endpoints/node.d.mts.map +1 -0
- package/dist/v2/runtime/endpoints/node.mjs +30 -0
- package/dist/v2/runtime/endpoints/node.mjs.map +1 -0
- package/dist/v2/runtime/express.d.cts +3 -0
- package/dist/v2/runtime/express.d.mts +3 -0
- package/dist/v2/runtime/handlers/get-runtime-info.cjs +2 -1
- package/dist/v2/runtime/handlers/get-runtime-info.cjs.map +1 -1
- package/dist/v2/runtime/handlers/get-runtime-info.mjs +2 -1
- package/dist/v2/runtime/handlers/get-runtime-info.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-connect.cjs +6 -3
- package/dist/v2/runtime/handlers/handle-connect.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-connect.mjs +6 -3
- package/dist/v2/runtime/handlers/handle-connect.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-run.cjs +6 -3
- package/dist/v2/runtime/handlers/handle-run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-run.mjs +6 -3
- package/dist/v2/runtime/handlers/handle-run.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-stop.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-stop.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-transcribe.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-transcribe.mjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/connect.cjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/connect.mjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/run.cjs +22 -1
- package/dist/v2/runtime/handlers/intelligence/run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/run.mjs +22 -1
- package/dist/v2/runtime/handlers/intelligence/run.mjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/thread-names.cjs +1 -1
- package/dist/v2/runtime/handlers/intelligence/thread-names.cjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/thread-names.mjs +1 -1
- package/dist/v2/runtime/handlers/intelligence/thread-names.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/agent-utils.cjs +21 -6
- package/dist/v2/runtime/handlers/shared/agent-utils.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/agent-utils.mjs +21 -6
- package/dist/v2/runtime/handlers/shared/agent-utils.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/json-response.cjs +4 -1
- package/dist/v2/runtime/handlers/shared/json-response.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/json-response.mjs +4 -1
- package/dist/v2/runtime/handlers/shared/json-response.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.mjs.map +1 -1
- package/dist/v2/runtime/handlers/sse/connect.cjs.map +1 -1
- package/dist/v2/runtime/handlers/sse/connect.mjs.map +1 -1
- package/dist/v2/runtime/handlers/sse/run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/sse/run.mjs.map +1 -1
- package/dist/v2/runtime/hono.d.cts +3 -0
- package/dist/v2/runtime/hono.d.mts +3 -0
- package/dist/v2/runtime/index.d.cts +16 -4
- package/dist/v2/runtime/index.d.cts.map +1 -0
- package/dist/v2/runtime/index.d.mts +16 -4
- package/dist/v2/runtime/index.d.mts.map +1 -0
- package/dist/v2/runtime/intelligence-platform/client.cjs +10 -1
- package/dist/v2/runtime/intelligence-platform/client.cjs.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.d.cts +22 -0
- package/dist/v2/runtime/intelligence-platform/client.d.cts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.d.mts +22 -0
- package/dist/v2/runtime/intelligence-platform/client.d.mts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.mjs +10 -1
- package/dist/v2/runtime/intelligence-platform/client.mjs.map +1 -1
- package/dist/v2/runtime/node.d.cts +3 -0
- package/dist/v2/runtime/node.d.mts +3 -0
- package/dist/v2/runtime/open-generative-ui-middleware.cjs +282 -0
- package/dist/v2/runtime/open-generative-ui-middleware.cjs.map +1 -0
- package/dist/v2/runtime/open-generative-ui-middleware.mjs +280 -0
- package/dist/v2/runtime/open-generative-ui-middleware.mjs.map +1 -0
- package/dist/v2/runtime/runner/intelligence.cjs +4 -4
- package/dist/v2/runtime/runner/intelligence.cjs.map +1 -1
- package/dist/v2/runtime/runner/intelligence.d.cts +6 -2
- package/dist/v2/runtime/runner/intelligence.d.cts.map +1 -1
- package/dist/v2/runtime/runner/intelligence.d.mts +6 -2
- package/dist/v2/runtime/runner/intelligence.d.mts.map +1 -1
- package/dist/v2/runtime/runner/intelligence.mjs +4 -4
- package/dist/v2/runtime/runner/intelligence.mjs.map +1 -1
- package/dist/v2/runtime/telemetry/telemetry-client.cjs +37 -0
- package/dist/v2/runtime/telemetry/telemetry-client.cjs.map +1 -1
- package/dist/v2/runtime/telemetry/telemetry-client.mjs +36 -0
- package/dist/v2/runtime/telemetry/telemetry-client.mjs.map +1 -1
- package/dist/v2/runtime/telemetry/utils.cjs +15 -0
- package/dist/v2/runtime/telemetry/utils.cjs.map +1 -0
- package/dist/v2/runtime/telemetry/utils.mjs +14 -0
- package/dist/v2/runtime/telemetry/utils.mjs.map +1 -0
- package/package.json +81 -48
- package/src/agent/__tests__/multimodal.test.ts +176 -0
- package/src/agent/index.ts +130 -19
- package/src/lib/runtime/agent-integrations/langgraph/agent.ts +3 -3
- package/src/lib/runtime/copilot-runtime.ts +1 -0
- package/src/v2/express.ts +1 -0
- package/src/v2/hono.ts +1 -0
- package/src/v2/node.ts +1 -0
- package/src/v2/runtime/__tests__/backward-compat.test.ts +261 -0
- package/src/v2/runtime/__tests__/code-review-fixes.test.ts +500 -0
- package/src/v2/runtime/__tests__/cors-credentials.test.ts +2 -2
- package/src/v2/runtime/__tests__/express-adapter.test.ts +188 -0
- package/src/v2/runtime/__tests__/express-body-order.test.ts +1 -1
- package/src/v2/runtime/__tests__/express-fetch-bridge.test.ts +344 -0
- package/src/v2/runtime/__tests__/express-single-sse.test.ts +1 -1
- package/src/v2/runtime/__tests__/fetch-cors.test.ts +205 -0
- package/src/v2/runtime/__tests__/fetch-handler-validation.test.ts +372 -0
- package/src/v2/runtime/__tests__/fetch-handler.test.ts +456 -0
- package/src/v2/runtime/__tests__/fetch-router.test.ts +132 -0
- package/src/v2/runtime/__tests__/get-runtime-info.test.ts +4 -1
- package/src/v2/runtime/__tests__/handle-connect.test.ts +15 -13
- package/src/v2/runtime/__tests__/handle-run.test.ts +21 -17
- package/src/v2/runtime/__tests__/handle-threads.test.ts +1 -1
- package/src/v2/runtime/__tests__/handle-transcribe.test.ts +1 -1
- package/src/v2/runtime/__tests__/hono-adapter.test.ts +150 -0
- package/src/v2/runtime/__tests__/hooks-edge-cases.test.ts +457 -0
- package/src/v2/runtime/__tests__/hooks.test.ts +557 -0
- package/src/v2/runtime/__tests__/integration/bun/bun-servers.integration.test.ts +27 -0
- package/src/v2/runtime/__tests__/integration/bun/elysia-multi.ts +32 -0
- package/src/v2/runtime/__tests__/integration/bun/elysia-single.ts +33 -0
- package/src/v2/runtime/__tests__/integration/bun/hono-bun-multi.ts +25 -0
- package/src/v2/runtime/__tests__/integration/bun/hono-bun-single.ts +32 -0
- package/src/v2/runtime/__tests__/integration/helpers/create-test-runtime.ts +15 -0
- package/src/v2/runtime/__tests__/integration/helpers/sse-reader.ts +45 -0
- package/src/v2/runtime/__tests__/integration/helpers/test-agent.ts +58 -0
- package/src/v2/runtime/__tests__/integration/node-servers.integration.test.ts +39 -0
- package/src/v2/runtime/__tests__/integration/servers/express-multi.ts +35 -0
- package/src/v2/runtime/__tests__/integration/servers/express-single.ts +36 -0
- package/src/v2/runtime/__tests__/integration/servers/fetch-direct.ts +39 -0
- package/src/v2/runtime/__tests__/integration/servers/hono-multi.ts +30 -0
- package/src/v2/runtime/__tests__/integration/servers/hono-single.ts +37 -0
- package/src/v2/runtime/__tests__/integration/servers/node-multi.ts +45 -0
- package/src/v2/runtime/__tests__/integration/servers/node-single.ts +46 -0
- package/src/v2/runtime/__tests__/integration/servers/types.ts +18 -0
- package/src/v2/runtime/__tests__/integration/suites/multi-endpoint.suite.ts +358 -0
- package/src/v2/runtime/__tests__/integration/suites/single-endpoint.suite.ts +363 -0
- package/src/v2/runtime/__tests__/middleware-express.test.ts +1 -1
- package/src/v2/runtime/__tests__/middleware-single-express.test.ts +1 -1
- package/src/v2/runtime/__tests__/middleware-single.test.ts +1 -1
- package/src/v2/runtime/__tests__/middleware-sse-parser.test.ts +1 -1
- package/src/v2/runtime/__tests__/middleware.test.ts +1 -2
- package/src/v2/runtime/__tests__/node-fetch-handler.test.ts +157 -0
- package/src/v2/runtime/__tests__/open-generative-ui-middleware.e2e.test.ts +728 -0
- package/src/v2/runtime/__tests__/router-edge-cases.test.ts +217 -0
- package/src/v2/runtime/__tests__/routing-express.test.ts +1 -1
- package/src/v2/runtime/__tests__/routing-single-express.test.ts +1 -1
- package/src/v2/runtime/__tests__/routing-single.test.ts +1 -1
- package/src/v2/runtime/__tests__/routing.test.ts +1 -1
- package/src/v2/runtime/__tests__/runtime.test.ts +110 -1
- package/src/v2/runtime/__tests__/telemetry.test.ts +62 -1
- package/src/v2/runtime/core/fetch-cors.ts +136 -0
- package/src/v2/runtime/core/fetch-handler.ts +415 -0
- package/src/v2/runtime/core/fetch-router.ts +112 -0
- package/src/v2/runtime/core/hooks.ts +151 -0
- package/src/v2/runtime/{runtime.ts → core/runtime.ts} +79 -10
- package/src/v2/runtime/endpoints/express-fetch-bridge.ts +137 -0
- package/src/v2/runtime/endpoints/express-single.ts +42 -219
- package/src/v2/runtime/endpoints/express.ts +128 -230
- package/src/v2/runtime/endpoints/hono-single.ts +19 -171
- package/src/v2/runtime/endpoints/hono.ts +45 -270
- package/src/v2/runtime/endpoints/node-fetch-handler.ts +48 -0
- package/src/v2/runtime/endpoints/node.ts +28 -0
- package/src/v2/runtime/handlers/get-runtime-info.ts +3 -2
- package/src/v2/runtime/handlers/handle-connect.ts +7 -4
- package/src/v2/runtime/handlers/handle-run.ts +7 -4
- package/src/v2/runtime/handlers/handle-stop.ts +1 -1
- package/src/v2/runtime/handlers/handle-transcribe.ts +1 -1
- package/src/v2/runtime/handlers/intelligence/connect.ts +1 -1
- package/src/v2/runtime/handlers/intelligence/run.ts +31 -1
- package/src/v2/runtime/handlers/intelligence/thread-names.ts +2 -2
- package/src/v2/runtime/handlers/intelligence/threads.ts +1 -1
- package/src/v2/runtime/handlers/shared/agent-utils.ts +29 -10
- package/src/v2/runtime/handlers/shared/json-response.ts +4 -1
- package/src/v2/runtime/handlers/shared/resolve-intelligence-user.ts +1 -1
- package/src/v2/runtime/handlers/sse/connect.ts +1 -1
- package/src/v2/runtime/handlers/sse/run.ts +1 -1
- package/src/v2/runtime/hono.ts +2 -0
- package/src/v2/runtime/index.ts +27 -1
- package/src/v2/runtime/intelligence-platform/client.ts +50 -1
- package/src/v2/runtime/node.ts +6 -0
- package/src/v2/runtime/open-generative-ui-middleware.ts +373 -0
- package/src/v2/runtime/runner/intelligence.ts +14 -4
- package/src/v2/runtime/telemetry/telemetry-client.ts +56 -0
- package/src/v2/runtime/telemetry/utils.ts +15 -0
- package/tsdown.config.ts +8 -1
- package/vitest.config.mjs +2 -5
- package/.eslintrc.js +0 -7
- package/dist/v2/runtime/endpoints/express-utils.cjs +0 -119
- package/dist/v2/runtime/endpoints/express-utils.cjs.map +0 -1
- package/dist/v2/runtime/endpoints/express-utils.mjs +0 -117
- package/dist/v2/runtime/endpoints/express-utils.mjs.map +0 -1
- package/dist/v2/runtime/handlers/intelligence/threads.cjs +0 -159
- package/dist/v2/runtime/handlers/intelligence/threads.cjs.map +0 -1
- package/dist/v2/runtime/handlers/intelligence/threads.mjs +0 -154
- package/dist/v2/runtime/handlers/intelligence/threads.mjs.map +0 -1
- package/dist/v2/runtime/middleware-sse-parser.cjs.map +0 -1
- package/dist/v2/runtime/middleware-sse-parser.d.cts.map +0 -1
- package/dist/v2/runtime/middleware-sse-parser.d.mts.map +0 -1
- package/dist/v2/runtime/middleware-sse-parser.mjs.map +0 -1
- package/dist/v2/runtime/middleware.cjs.map +0 -1
- package/dist/v2/runtime/middleware.d.cts.map +0 -1
- package/dist/v2/runtime/middleware.d.mts.map +0 -1
- package/dist/v2/runtime/middleware.mjs.map +0 -1
- package/dist/v2/runtime/runtime.cjs.map +0 -1
- package/dist/v2/runtime/runtime.d.cts.map +0 -1
- package/dist/v2/runtime/runtime.d.mts.map +0 -1
- package/dist/v2/runtime/runtime.mjs.map +0 -1
- package/src/v2/runtime/__tests__/express-abort-signal.test.ts +0 -25
- package/src/v2/runtime/endpoints/express-utils.ts +0 -182
- package/src/v2/runtime/handler.ts +0 -3
- /package/src/v2/runtime/{middleware-sse-parser.ts → core/middleware-sse-parser.ts} +0 -0
- /package/src/v2/runtime/{middleware.ts → core/middleware.ts} +0 -0
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic CopilotKit runtime handler.
|
|
3
|
+
*
|
|
4
|
+
* Returns a pure `(Request) => Promise<Response>` function that can be used
|
|
5
|
+
* directly with Bun, Deno, Cloudflare Workers, Next.js App Router, or any
|
|
6
|
+
* Fetch-native runtime — no framework dependency required.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { CopilotRuntime, createCopilotRuntimeHandler } from "@copilotkit/runtime/v2";
|
|
11
|
+
*
|
|
12
|
+
* const handler = createCopilotRuntimeHandler({
|
|
13
|
+
* runtime: new CopilotRuntime({ agents: { ... } }),
|
|
14
|
+
* basePath: "/api/copilotkit",
|
|
15
|
+
* cors: true,
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // Bun
|
|
19
|
+
* Bun.serve({ fetch: handler });
|
|
20
|
+
*
|
|
21
|
+
* // Deno
|
|
22
|
+
* Deno.serve(handler);
|
|
23
|
+
*
|
|
24
|
+
* // Cloudflare Workers
|
|
25
|
+
* export default { fetch: handler };
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import type { CopilotRuntimeLike } from "./runtime";
|
|
30
|
+
import type { CopilotRuntimeHooks, RouteInfo, HookContext } from "./hooks";
|
|
31
|
+
import {
|
|
32
|
+
runOnRequest,
|
|
33
|
+
runOnBeforeHandler,
|
|
34
|
+
runOnResponse,
|
|
35
|
+
runOnError,
|
|
36
|
+
} from "./hooks";
|
|
37
|
+
import type { CopilotCorsConfig } from "./fetch-cors";
|
|
38
|
+
import { handleCors, addCorsHeaders } from "./fetch-cors";
|
|
39
|
+
import { matchRoute } from "./fetch-router";
|
|
40
|
+
import {
|
|
41
|
+
callBeforeRequestMiddleware,
|
|
42
|
+
callAfterRequestMiddleware,
|
|
43
|
+
} from "./middleware";
|
|
44
|
+
import { handleRunAgent } from "../handlers/handle-run";
|
|
45
|
+
import { handleConnectAgent } from "../handlers/handle-connect";
|
|
46
|
+
import { handleStopAgent } from "../handlers/handle-stop";
|
|
47
|
+
import { handleGetRuntimeInfo } from "../handlers/get-runtime-info";
|
|
48
|
+
import { handleTranscribe } from "../handlers/handle-transcribe";
|
|
49
|
+
import {
|
|
50
|
+
parseMethodCall,
|
|
51
|
+
createJsonRequest,
|
|
52
|
+
expectString,
|
|
53
|
+
type MethodCall,
|
|
54
|
+
} from "../endpoints/single-route-helpers";
|
|
55
|
+
import { logger } from "@copilotkit/shared";
|
|
56
|
+
|
|
57
|
+
/* ------------------------------------------------------------------------------------------------
|
|
58
|
+
* Public types
|
|
59
|
+
* --------------------------------------------------------------------------------------------- */
|
|
60
|
+
|
|
61
|
+
export interface CopilotRuntimeHandlerOptions {
|
|
62
|
+
runtime: CopilotRuntimeLike;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Optional base path for routing.
|
|
66
|
+
*
|
|
67
|
+
* When provided: strict prefix stripping. The handler strips this prefix from the
|
|
68
|
+
* URL pathname and matches the remainder against known routes.
|
|
69
|
+
*
|
|
70
|
+
* When omitted: suffix matching. The handler matches known route patterns as
|
|
71
|
+
* suffixes of the URL pathname.
|
|
72
|
+
*/
|
|
73
|
+
basePath?: string;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Endpoint mode:
|
|
77
|
+
* - "multi-route" (default): Routes like POST /agent/:agentId/run, GET /info, etc.
|
|
78
|
+
* - "single-route": Single POST endpoint with JSON envelope { method, params, body }
|
|
79
|
+
*/
|
|
80
|
+
mode?: "multi-route" | "single-route";
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Optional CORS configuration.
|
|
84
|
+
* When not provided, no CORS headers are added (let the framework handle it).
|
|
85
|
+
* Set to true for permissive defaults, or provide an object.
|
|
86
|
+
*/
|
|
87
|
+
cors?: boolean | CopilotCorsConfig;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Lifecycle hooks for request processing.
|
|
91
|
+
*/
|
|
92
|
+
hooks?: CopilotRuntimeHooks;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export type CopilotRuntimeFetchHandler = (
|
|
96
|
+
request: Request,
|
|
97
|
+
) => Promise<Response>;
|
|
98
|
+
|
|
99
|
+
/* ------------------------------------------------------------------------------------------------
|
|
100
|
+
* Handler factory
|
|
101
|
+
* --------------------------------------------------------------------------------------------- */
|
|
102
|
+
|
|
103
|
+
export function createCopilotRuntimeHandler(
|
|
104
|
+
options: CopilotRuntimeHandlerOptions,
|
|
105
|
+
): CopilotRuntimeFetchHandler {
|
|
106
|
+
const { runtime, basePath, mode = "multi-route", cors, hooks } = options;
|
|
107
|
+
|
|
108
|
+
const corsConfig = resolveCorsConfig(cors);
|
|
109
|
+
|
|
110
|
+
return async (request: Request): Promise<Response> => {
|
|
111
|
+
const url = new URL(request.url, "http://localhost");
|
|
112
|
+
const path = url.pathname;
|
|
113
|
+
const requestOrigin = request.headers.get("origin");
|
|
114
|
+
|
|
115
|
+
// Base hook context (route not yet known)
|
|
116
|
+
const baseCtx: HookContext = { request, path, runtime };
|
|
117
|
+
|
|
118
|
+
let route: RouteInfo | undefined;
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
// 1. CORS preflight
|
|
122
|
+
if (corsConfig) {
|
|
123
|
+
const preflight = handleCors(request, corsConfig);
|
|
124
|
+
if (preflight) return preflight;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 2. onRequest hook
|
|
128
|
+
request = await runOnRequest(hooks, { ...baseCtx, request });
|
|
129
|
+
|
|
130
|
+
// 3. Legacy beforeRequestMiddleware
|
|
131
|
+
try {
|
|
132
|
+
const maybeModified = await callBeforeRequestMiddleware({
|
|
133
|
+
runtime,
|
|
134
|
+
request,
|
|
135
|
+
path,
|
|
136
|
+
});
|
|
137
|
+
if (maybeModified) {
|
|
138
|
+
request = maybeModified;
|
|
139
|
+
}
|
|
140
|
+
} catch (mwError: unknown) {
|
|
141
|
+
logger.error(
|
|
142
|
+
{ err: mwError, url: request.url, path },
|
|
143
|
+
"Error running before request middleware",
|
|
144
|
+
);
|
|
145
|
+
if (mwError instanceof Response) {
|
|
146
|
+
return maybeAddCors(mwError, corsConfig, requestOrigin);
|
|
147
|
+
}
|
|
148
|
+
throw mwError;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 4. Route matching
|
|
152
|
+
let response: Response;
|
|
153
|
+
|
|
154
|
+
if (mode === "single-route") {
|
|
155
|
+
const resolved = await resolveSingleRoute(request, basePath, path);
|
|
156
|
+
route = resolved.route;
|
|
157
|
+
const { methodCall } = resolved;
|
|
158
|
+
// 5. onBeforeHandler hook
|
|
159
|
+
request = await runOnBeforeHandler(hooks, {
|
|
160
|
+
request,
|
|
161
|
+
path,
|
|
162
|
+
runtime,
|
|
163
|
+
route,
|
|
164
|
+
});
|
|
165
|
+
// 6. Wrap body for methods that need it, then dispatch
|
|
166
|
+
if (
|
|
167
|
+
route.method === "agent/run" ||
|
|
168
|
+
route.method === "agent/connect" ||
|
|
169
|
+
route.method === "transcribe"
|
|
170
|
+
) {
|
|
171
|
+
request = createJsonRequest(request, methodCall.body);
|
|
172
|
+
}
|
|
173
|
+
response = await dispatchRoute(runtime, request, route);
|
|
174
|
+
} else {
|
|
175
|
+
// Multi-route: match URL pattern
|
|
176
|
+
const matched = matchRoute(path, basePath);
|
|
177
|
+
if (!matched) {
|
|
178
|
+
throw jsonResponse({ error: "Not found" }, 404);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Validate HTTP method
|
|
182
|
+
const methodError = validateHttpMethod(request.method, matched);
|
|
183
|
+
if (methodError) {
|
|
184
|
+
route = matched;
|
|
185
|
+
throw methodError;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
route = matched;
|
|
189
|
+
|
|
190
|
+
// 5. onBeforeHandler hook
|
|
191
|
+
request = await runOnBeforeHandler(hooks, {
|
|
192
|
+
request,
|
|
193
|
+
path,
|
|
194
|
+
runtime,
|
|
195
|
+
route,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// 6. Handler dispatch
|
|
199
|
+
response = await dispatchRoute(runtime, request, route);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// 7. onResponse hook
|
|
203
|
+
response = await runOnResponse(hooks, {
|
|
204
|
+
request,
|
|
205
|
+
response,
|
|
206
|
+
path,
|
|
207
|
+
runtime,
|
|
208
|
+
route,
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// 8. CORS headers on response
|
|
212
|
+
response = maybeAddCors(response, corsConfig, requestOrigin);
|
|
213
|
+
|
|
214
|
+
// 9. Legacy afterRequestMiddleware (non-blocking)
|
|
215
|
+
// Clone the response so middleware can read the body without consuming
|
|
216
|
+
// the original stream that will be sent to the client.
|
|
217
|
+
callAfterRequestMiddleware({
|
|
218
|
+
runtime,
|
|
219
|
+
response: response.clone(),
|
|
220
|
+
path,
|
|
221
|
+
}).catch((error: unknown) => {
|
|
222
|
+
logger.error(
|
|
223
|
+
{ err: error, url: request.url, path },
|
|
224
|
+
"Error running after request middleware",
|
|
225
|
+
);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
return response;
|
|
229
|
+
} catch (error) {
|
|
230
|
+
// Short-circuit with thrown Response
|
|
231
|
+
if (error instanceof Response) {
|
|
232
|
+
const finalResponse = await runOnResponse(hooks, {
|
|
233
|
+
request,
|
|
234
|
+
response: error,
|
|
235
|
+
path,
|
|
236
|
+
runtime,
|
|
237
|
+
route: route ?? { method: "info" },
|
|
238
|
+
});
|
|
239
|
+
return maybeAddCors(finalResponse, corsConfig, requestOrigin);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Run onError hook — wrapped so a throwing hook doesn't escape
|
|
243
|
+
try {
|
|
244
|
+
const errorResponse = await runOnError(hooks, {
|
|
245
|
+
request,
|
|
246
|
+
error,
|
|
247
|
+
path,
|
|
248
|
+
runtime,
|
|
249
|
+
route,
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
if (errorResponse) {
|
|
253
|
+
return maybeAddCors(errorResponse, corsConfig, requestOrigin);
|
|
254
|
+
}
|
|
255
|
+
} catch (hookError: unknown) {
|
|
256
|
+
logger.error(
|
|
257
|
+
{ err: hookError, originalErr: error, url: request.url, path },
|
|
258
|
+
"onError hook threw",
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
logger.error(
|
|
263
|
+
{ err: error, url: request.url, path },
|
|
264
|
+
"Unhandled error in CopilotKit runtime handler",
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
return maybeAddCors(
|
|
268
|
+
jsonResponse({ error: "internal_error" }, 500),
|
|
269
|
+
corsConfig,
|
|
270
|
+
requestOrigin,
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/* ------------------------------------------------------------------------------------------------
|
|
277
|
+
* Route dispatch
|
|
278
|
+
* --------------------------------------------------------------------------------------------- */
|
|
279
|
+
|
|
280
|
+
function dispatchRoute(
|
|
281
|
+
runtime: CopilotRuntimeLike,
|
|
282
|
+
request: Request,
|
|
283
|
+
route: RouteInfo,
|
|
284
|
+
): Promise<Response> {
|
|
285
|
+
switch (route.method) {
|
|
286
|
+
case "agent/run":
|
|
287
|
+
return handleRunAgent({
|
|
288
|
+
runtime,
|
|
289
|
+
request,
|
|
290
|
+
agentId: route.agentId,
|
|
291
|
+
});
|
|
292
|
+
case "agent/connect":
|
|
293
|
+
return handleConnectAgent({
|
|
294
|
+
runtime,
|
|
295
|
+
request,
|
|
296
|
+
agentId: route.agentId,
|
|
297
|
+
});
|
|
298
|
+
case "agent/stop":
|
|
299
|
+
return handleStopAgent({
|
|
300
|
+
runtime,
|
|
301
|
+
request,
|
|
302
|
+
agentId: route.agentId,
|
|
303
|
+
threadId: route.threadId,
|
|
304
|
+
});
|
|
305
|
+
case "info":
|
|
306
|
+
return handleGetRuntimeInfo({ runtime, request });
|
|
307
|
+
case "transcribe":
|
|
308
|
+
return handleTranscribe({ runtime, request });
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
interface SingleRouteResolution {
|
|
313
|
+
route: RouteInfo;
|
|
314
|
+
methodCall: MethodCall;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async function resolveSingleRoute(
|
|
318
|
+
request: Request,
|
|
319
|
+
basePath: string | undefined,
|
|
320
|
+
pathname: string,
|
|
321
|
+
): Promise<SingleRouteResolution> {
|
|
322
|
+
if (basePath) {
|
|
323
|
+
const normalizedBase =
|
|
324
|
+
basePath.length > 1 && basePath.endsWith("/")
|
|
325
|
+
? basePath.slice(0, -1)
|
|
326
|
+
: basePath;
|
|
327
|
+
if (!pathname.startsWith(normalizedBase)) {
|
|
328
|
+
throw jsonResponse({ error: "Not found" }, 404);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (request.method !== "POST") {
|
|
333
|
+
throw jsonResponse({ error: "Method not allowed" }, 405, { Allow: "POST" });
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const methodCall = await parseMethodCall(request);
|
|
337
|
+
|
|
338
|
+
let route: RouteInfo;
|
|
339
|
+
switch (methodCall.method) {
|
|
340
|
+
case "agent/run":
|
|
341
|
+
route = {
|
|
342
|
+
method: "agent/run",
|
|
343
|
+
agentId: expectString(methodCall.params, "agentId"),
|
|
344
|
+
};
|
|
345
|
+
break;
|
|
346
|
+
case "agent/connect":
|
|
347
|
+
route = {
|
|
348
|
+
method: "agent/connect",
|
|
349
|
+
agentId: expectString(methodCall.params, "agentId"),
|
|
350
|
+
};
|
|
351
|
+
break;
|
|
352
|
+
case "agent/stop":
|
|
353
|
+
route = {
|
|
354
|
+
method: "agent/stop",
|
|
355
|
+
agentId: expectString(methodCall.params, "agentId"),
|
|
356
|
+
threadId: expectString(methodCall.params, "threadId"),
|
|
357
|
+
};
|
|
358
|
+
break;
|
|
359
|
+
case "info":
|
|
360
|
+
route = { method: "info" };
|
|
361
|
+
break;
|
|
362
|
+
case "transcribe":
|
|
363
|
+
route = { method: "transcribe" };
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return { route, methodCall };
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/* ------------------------------------------------------------------------------------------------
|
|
371
|
+
* HTTP method validation
|
|
372
|
+
* --------------------------------------------------------------------------------------------- */
|
|
373
|
+
|
|
374
|
+
function validateHttpMethod(
|
|
375
|
+
httpMethod: string,
|
|
376
|
+
route: RouteInfo,
|
|
377
|
+
): Response | null {
|
|
378
|
+
const method = httpMethod.toUpperCase();
|
|
379
|
+
if (route.method === "info" && method === "GET") return null;
|
|
380
|
+
if (route.method !== "info" && method === "POST") return null;
|
|
381
|
+
const allowed = route.method === "info" ? "GET" : "POST";
|
|
382
|
+
return jsonResponse({ error: "Method not allowed" }, 405, { Allow: allowed });
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/* ------------------------------------------------------------------------------------------------
|
|
386
|
+
* Helpers
|
|
387
|
+
* --------------------------------------------------------------------------------------------- */
|
|
388
|
+
|
|
389
|
+
function resolveCorsConfig(
|
|
390
|
+
cors: boolean | CopilotCorsConfig | undefined,
|
|
391
|
+
): CopilotCorsConfig | null {
|
|
392
|
+
if (!cors) return null;
|
|
393
|
+
if (cors === true) return {};
|
|
394
|
+
return cors;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function maybeAddCors(
|
|
398
|
+
response: Response,
|
|
399
|
+
config: CopilotCorsConfig | null,
|
|
400
|
+
requestOrigin: string | null,
|
|
401
|
+
): Response {
|
|
402
|
+
if (!config) return response;
|
|
403
|
+
return addCorsHeaders(response, config, requestOrigin);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function jsonResponse(
|
|
407
|
+
body: unknown,
|
|
408
|
+
status: number,
|
|
409
|
+
extraHeaders?: Record<string, string>,
|
|
410
|
+
): Response {
|
|
411
|
+
return new Response(JSON.stringify(body), {
|
|
412
|
+
status,
|
|
413
|
+
headers: { "Content-Type": "application/json", ...extraHeaders },
|
|
414
|
+
});
|
|
415
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightweight URL router for framework-agnostic CopilotKit runtime handler.
|
|
3
|
+
*
|
|
4
|
+
* Two strategies:
|
|
5
|
+
* - With `basePath`: strict prefix strip → match remainder
|
|
6
|
+
* - Without `basePath`: suffix matching on known patterns
|
|
7
|
+
*
|
|
8
|
+
* Single-route mode: delegates to `parseMethodCall` for JSON envelope dispatch.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { RouteInfo } from "./hooks";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Match a request URL against known CopilotKit route patterns.
|
|
15
|
+
*
|
|
16
|
+
* @param pathname - The URL pathname to match
|
|
17
|
+
* @param basePath - Optional base path prefix to strip first
|
|
18
|
+
* @returns RouteInfo if matched, null otherwise
|
|
19
|
+
*/
|
|
20
|
+
export function matchRoute(
|
|
21
|
+
pathname: string,
|
|
22
|
+
basePath?: string,
|
|
23
|
+
): RouteInfo | null {
|
|
24
|
+
let remainder: string;
|
|
25
|
+
|
|
26
|
+
if (basePath) {
|
|
27
|
+
// Normalize: ensure basePath doesn't end with /
|
|
28
|
+
const normalizedBase =
|
|
29
|
+
basePath.length > 1 && basePath.endsWith("/")
|
|
30
|
+
? basePath.slice(0, -1)
|
|
31
|
+
: basePath;
|
|
32
|
+
|
|
33
|
+
// Special case: basePath === "/" matches everything
|
|
34
|
+
if (normalizedBase === "/") {
|
|
35
|
+
remainder = pathname;
|
|
36
|
+
} else {
|
|
37
|
+
if (!pathname.startsWith(normalizedBase)) return null;
|
|
38
|
+
|
|
39
|
+
// The character after basePath must be "/" or end of string
|
|
40
|
+
const afterBase = pathname.slice(normalizedBase.length);
|
|
41
|
+
if (afterBase.length > 0 && !afterBase.startsWith("/")) return null;
|
|
42
|
+
|
|
43
|
+
remainder = afterBase || "/";
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
// Suffix matching: find known patterns at the end of the pathname
|
|
47
|
+
remainder = pathname;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return matchSegments(remainder);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function safeDecodeURIComponent(value: string): string | null {
|
|
54
|
+
try {
|
|
55
|
+
return decodeURIComponent(value);
|
|
56
|
+
} catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function matchSegments(path: string): RouteInfo | null {
|
|
62
|
+
const segments = path.split("/").filter(Boolean);
|
|
63
|
+
const len = segments.length;
|
|
64
|
+
|
|
65
|
+
// Try suffix matching — scan from the end for known patterns
|
|
66
|
+
|
|
67
|
+
// /info (1 segment)
|
|
68
|
+
if (len >= 1 && segments[len - 1] === "info") {
|
|
69
|
+
return { method: "info" };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// /transcribe (1 segment)
|
|
73
|
+
if (len >= 1 && segments[len - 1] === "transcribe") {
|
|
74
|
+
return { method: "transcribe" };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// /agent/:agentId/run (3 segments)
|
|
78
|
+
if (
|
|
79
|
+
len >= 3 &&
|
|
80
|
+
segments[len - 3] === "agent" &&
|
|
81
|
+
segments[len - 1] === "run"
|
|
82
|
+
) {
|
|
83
|
+
const agentId = safeDecodeURIComponent(segments[len - 2]!);
|
|
84
|
+
if (!agentId) return null;
|
|
85
|
+
return { method: "agent/run", agentId };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// /agent/:agentId/connect (3 segments)
|
|
89
|
+
if (
|
|
90
|
+
len >= 3 &&
|
|
91
|
+
segments[len - 3] === "agent" &&
|
|
92
|
+
segments[len - 1] === "connect"
|
|
93
|
+
) {
|
|
94
|
+
const agentId = safeDecodeURIComponent(segments[len - 2]!);
|
|
95
|
+
if (!agentId) return null;
|
|
96
|
+
return { method: "agent/connect", agentId };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// /agent/:agentId/stop/:threadId (4 segments)
|
|
100
|
+
if (
|
|
101
|
+
len >= 4 &&
|
|
102
|
+
segments[len - 4] === "agent" &&
|
|
103
|
+
segments[len - 2] === "stop"
|
|
104
|
+
) {
|
|
105
|
+
const agentId = safeDecodeURIComponent(segments[len - 3]!);
|
|
106
|
+
const threadId = safeDecodeURIComponent(segments[len - 1]!);
|
|
107
|
+
if (!agentId || !threadId) return null;
|
|
108
|
+
return { method: "agent/stop", agentId, threadId };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lifecycle hooks for CopilotKit runtime request processing.
|
|
3
|
+
*
|
|
4
|
+
* Hooks let you intercept requests at various stages of the pipeline:
|
|
5
|
+
* - `onRequest`: Before routing — auth, correlation IDs, header injection
|
|
6
|
+
* - `onBeforeHandler`: After routing — route-specific authorization
|
|
7
|
+
* - `onResponse`: After handler — add headers, log, set cookies
|
|
8
|
+
* - `onError`: On error — custom error responses
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const handler = createCopilotRuntimeHandler({
|
|
13
|
+
* runtime,
|
|
14
|
+
* hooks: {
|
|
15
|
+
* onRequest: async ({ request }) => {
|
|
16
|
+
* const token = request.headers.get("authorization");
|
|
17
|
+
* if (!token) throw new Response("Unauthorized", { status: 401 });
|
|
18
|
+
* },
|
|
19
|
+
* onResponse: async ({ response }) => {
|
|
20
|
+
* const headers = new Headers(response.headers);
|
|
21
|
+
* headers.set("x-copilot-version", "2.0");
|
|
22
|
+
* return new Response(response.body, { ...response, headers });
|
|
23
|
+
* },
|
|
24
|
+
* },
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import type { MaybePromise } from "@copilotkit/shared";
|
|
30
|
+
import type { CopilotRuntimeLike } from "./runtime";
|
|
31
|
+
|
|
32
|
+
/* ------------------------------------------------------------------------------------------------
|
|
33
|
+
* Route info
|
|
34
|
+
* --------------------------------------------------------------------------------------------- */
|
|
35
|
+
|
|
36
|
+
export type RouteInfo =
|
|
37
|
+
| { method: "agent/run"; agentId: string }
|
|
38
|
+
| { method: "agent/connect"; agentId: string }
|
|
39
|
+
| { method: "agent/stop"; agentId: string; threadId: string }
|
|
40
|
+
| { method: "info" }
|
|
41
|
+
| { method: "transcribe" };
|
|
42
|
+
|
|
43
|
+
/* ------------------------------------------------------------------------------------------------
|
|
44
|
+
* Hook contexts
|
|
45
|
+
* --------------------------------------------------------------------------------------------- */
|
|
46
|
+
|
|
47
|
+
export interface HookContext {
|
|
48
|
+
/** The incoming Fetch Request (possibly modified by prior hooks). */
|
|
49
|
+
request: Request;
|
|
50
|
+
/** The resolved URL pathname. */
|
|
51
|
+
path: string;
|
|
52
|
+
/** The CopilotRuntimeLike instance. */
|
|
53
|
+
runtime: CopilotRuntimeLike;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface HandlerHookContext extends HookContext {
|
|
57
|
+
/** The resolved route information. */
|
|
58
|
+
route: RouteInfo;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface ResponseHookContext extends HookContext {
|
|
62
|
+
/** The Response produced by the handler. */
|
|
63
|
+
response: Response;
|
|
64
|
+
/** The resolved route information. */
|
|
65
|
+
route: RouteInfo;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface ErrorHookContext extends HookContext {
|
|
69
|
+
/** The error that occurred. */
|
|
70
|
+
error: unknown;
|
|
71
|
+
/** The route info, if routing had already succeeded. */
|
|
72
|
+
route?: RouteInfo;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/* ------------------------------------------------------------------------------------------------
|
|
76
|
+
* Hooks interface
|
|
77
|
+
* --------------------------------------------------------------------------------------------- */
|
|
78
|
+
|
|
79
|
+
export interface CopilotRuntimeHooks {
|
|
80
|
+
/**
|
|
81
|
+
* Called at the start of every request, before routing.
|
|
82
|
+
* Use to validate auth, attach headers, initialize correlation IDs, etc.
|
|
83
|
+
*
|
|
84
|
+
* Return a modified Request to replace the original, or void to continue.
|
|
85
|
+
* Throw a Response to short-circuit with an early response.
|
|
86
|
+
*/
|
|
87
|
+
onRequest?: (ctx: HookContext) => MaybePromise<Request | void>;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Called after routing is resolved but before the handler executes.
|
|
91
|
+
* Receives the resolved route info (method, agentId, threadId).
|
|
92
|
+
*
|
|
93
|
+
* Use to do route-specific authorization, attach headers for agent calls, etc.
|
|
94
|
+
* Return a modified Request or void.
|
|
95
|
+
* Throw a Response to short-circuit.
|
|
96
|
+
*/
|
|
97
|
+
onBeforeHandler?: (ctx: HandlerHookContext) => MaybePromise<Request | void>;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Called after the handler produces a Response, before it's sent to the client.
|
|
101
|
+
* Use to set cookies, add debugging headers, log, etc.
|
|
102
|
+
*
|
|
103
|
+
* Return a modified Response to replace the original, or void.
|
|
104
|
+
*/
|
|
105
|
+
onResponse?: (ctx: ResponseHookContext) => MaybePromise<Response | void>;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Called when an error occurs during request processing.
|
|
109
|
+
* Return a Response to override the default error response, or void to use the default.
|
|
110
|
+
*/
|
|
111
|
+
onError?: (ctx: ErrorHookContext) => MaybePromise<Response | void>;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/* ------------------------------------------------------------------------------------------------
|
|
115
|
+
* Internal hook runners
|
|
116
|
+
* --------------------------------------------------------------------------------------------- */
|
|
117
|
+
|
|
118
|
+
export async function runOnRequest(
|
|
119
|
+
hooks: CopilotRuntimeHooks | undefined,
|
|
120
|
+
ctx: HookContext,
|
|
121
|
+
): Promise<Request> {
|
|
122
|
+
if (!hooks?.onRequest) return ctx.request;
|
|
123
|
+
const result = await hooks.onRequest(ctx);
|
|
124
|
+
return result instanceof Request ? result : ctx.request;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function runOnBeforeHandler(
|
|
128
|
+
hooks: CopilotRuntimeHooks | undefined,
|
|
129
|
+
ctx: HandlerHookContext,
|
|
130
|
+
): Promise<Request> {
|
|
131
|
+
if (!hooks?.onBeforeHandler) return ctx.request;
|
|
132
|
+
const result = await hooks.onBeforeHandler(ctx);
|
|
133
|
+
return result instanceof Request ? result : ctx.request;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export async function runOnResponse(
|
|
137
|
+
hooks: CopilotRuntimeHooks | undefined,
|
|
138
|
+
ctx: ResponseHookContext,
|
|
139
|
+
): Promise<Response> {
|
|
140
|
+
if (!hooks?.onResponse) return ctx.response;
|
|
141
|
+
const result = await hooks.onResponse(ctx);
|
|
142
|
+
return result instanceof Response ? result : ctx.response;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export async function runOnError(
|
|
146
|
+
hooks: CopilotRuntimeHooks | undefined,
|
|
147
|
+
ctx: ErrorHookContext,
|
|
148
|
+
): Promise<Response | void> {
|
|
149
|
+
if (!hooks?.onError) return;
|
|
150
|
+
return hooks.onError(ctx);
|
|
151
|
+
}
|