@copilotkit/runtime 1.54.1 → 1.55.0-next.7
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/.eslintrc.js +4 -4
- package/CHANGELOG.md +113 -113
- package/dist/_virtual/_rolldown/runtime.mjs +25 -1
- package/dist/agent/index.cjs +643 -0
- package/dist/agent/index.cjs.map +1 -0
- package/dist/agent/index.d.cts +263 -0
- package/dist/agent/index.d.cts.map +1 -0
- package/dist/agent/index.d.mts +263 -0
- package/dist/agent/index.d.mts.map +1 -0
- package/dist/agent/index.mjs +635 -0
- package/dist/agent/index.mjs.map +1 -0
- package/dist/graphql/message-conversion/agui-to-gql.cjs.map +1 -1
- package/dist/graphql/message-conversion/agui-to-gql.mjs.map +1 -1
- package/dist/lib/integrations/nextjs/app-router.cjs +2 -2
- package/dist/lib/integrations/nextjs/app-router.cjs.map +1 -1
- package/dist/lib/integrations/nextjs/app-router.mjs +1 -1
- package/dist/lib/integrations/nextjs/app-router.mjs.map +1 -1
- package/dist/lib/integrations/node-http/index.cjs +2 -3
- package/dist/lib/integrations/node-http/index.cjs.map +1 -1
- package/dist/lib/integrations/node-http/index.mjs +1 -1
- package/dist/lib/integrations/node-http/index.mjs.map +1 -1
- package/dist/lib/runtime/agent-integrations/langgraph/agent.cjs +1 -1
- package/dist/lib/runtime/agent-integrations/langgraph/agent.d.cts +2 -2
- package/dist/lib/runtime/agent-integrations/langgraph/agent.d.cts.map +1 -1
- package/dist/lib/runtime/agent-integrations/langgraph/agent.d.mts +3 -3
- package/dist/lib/runtime/agent-integrations/langgraph/agent.d.mts.map +1 -1
- package/dist/lib/runtime/agent-integrations/langgraph/agent.mjs +1 -1
- package/dist/lib/runtime/copilot-runtime.cjs +7 -5
- package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.cts +10 -8
- package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.mts +10 -8
- package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.mjs +7 -5
- package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
- package/dist/lib/runtime/telemetry-agent-runner.cjs +2 -2
- package/dist/lib/runtime/telemetry-agent-runner.cjs.map +1 -1
- package/dist/lib/runtime/telemetry-agent-runner.d.cts +2 -1
- package/dist/lib/runtime/telemetry-agent-runner.d.cts.map +1 -1
- package/dist/lib/runtime/telemetry-agent-runner.d.mts +2 -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 +1 -1
- package/dist/lib/telemetry-client.mjs +1 -1
- package/dist/package.cjs +21 -4
- package/dist/package.mjs +21 -4
- package/dist/service-adapters/anthropic/anthropic-adapter.d.mts +1 -1
- package/dist/v2/index.cjs +41 -15
- package/dist/v2/index.d.cts +14 -2
- package/dist/v2/index.d.mts +14 -2
- package/dist/v2/index.mjs +13 -4
- package/dist/v2/runtime/endpoints/express-single.cjs +190 -0
- package/dist/v2/runtime/endpoints/express-single.cjs.map +1 -0
- package/dist/v2/runtime/endpoints/express-single.d.cts +16 -0
- package/dist/v2/runtime/endpoints/express-single.d.cts.map +1 -0
- package/dist/v2/runtime/endpoints/express-single.d.mts +16 -0
- package/dist/v2/runtime/endpoints/express-single.d.mts.map +1 -0
- package/dist/v2/runtime/endpoints/express-single.mjs +187 -0
- package/dist/v2/runtime/endpoints/express-single.mjs.map +1 -0
- package/dist/v2/runtime/endpoints/express-utils.cjs +119 -0
- package/dist/v2/runtime/endpoints/express-utils.cjs.map +1 -0
- package/dist/v2/runtime/endpoints/express-utils.mjs +117 -0
- package/dist/v2/runtime/endpoints/express-utils.mjs.map +1 -0
- package/dist/v2/runtime/endpoints/express.cjs +217 -0
- package/dist/v2/runtime/endpoints/express.cjs.map +1 -0
- package/dist/v2/runtime/endpoints/express.d.cts +16 -0
- package/dist/v2/runtime/endpoints/express.d.cts.map +1 -0
- package/dist/v2/runtime/endpoints/express.d.mts +16 -0
- package/dist/v2/runtime/endpoints/express.d.mts.map +1 -0
- package/dist/v2/runtime/endpoints/express.mjs +214 -0
- package/dist/v2/runtime/endpoints/express.mjs.map +1 -0
- package/dist/v2/runtime/endpoints/hono-single.cjs +141 -0
- package/dist/v2/runtime/endpoints/hono-single.cjs.map +1 -0
- package/dist/v2/runtime/endpoints/hono-single.d.cts +41 -0
- package/dist/v2/runtime/endpoints/hono-single.d.cts.map +1 -0
- package/dist/v2/runtime/endpoints/hono-single.d.mts +41 -0
- package/dist/v2/runtime/endpoints/hono-single.d.mts.map +1 -0
- package/dist/v2/runtime/endpoints/hono-single.mjs +140 -0
- package/dist/v2/runtime/endpoints/hono-single.mjs.map +1 -0
- package/dist/v2/runtime/endpoints/hono.cjs +248 -0
- package/dist/v2/runtime/endpoints/hono.cjs.map +1 -0
- package/dist/v2/runtime/endpoints/hono.d.cts +164 -0
- package/dist/v2/runtime/endpoints/hono.d.cts.map +1 -0
- package/dist/v2/runtime/endpoints/hono.d.mts +164 -0
- package/dist/v2/runtime/endpoints/hono.d.mts.map +1 -0
- package/dist/v2/runtime/endpoints/hono.mjs +247 -0
- package/dist/v2/runtime/endpoints/hono.mjs.map +1 -0
- package/dist/v2/runtime/endpoints/index.d.cts +5 -0
- package/dist/v2/runtime/endpoints/index.d.mts +5 -0
- package/dist/v2/runtime/endpoints/single-route-helpers.cjs +68 -0
- package/dist/v2/runtime/endpoints/single-route-helpers.cjs.map +1 -0
- package/dist/v2/runtime/endpoints/single-route-helpers.mjs +65 -0
- package/dist/v2/runtime/endpoints/single-route-helpers.mjs.map +1 -0
- package/dist/v2/runtime/handlers/get-runtime-info.cjs +51 -0
- package/dist/v2/runtime/handlers/get-runtime-info.cjs.map +1 -0
- package/dist/v2/runtime/handlers/get-runtime-info.mjs +51 -0
- package/dist/v2/runtime/handlers/get-runtime-info.mjs.map +1 -0
- package/dist/v2/runtime/handlers/handle-connect.cjs +49 -0
- package/dist/v2/runtime/handlers/handle-connect.cjs.map +1 -0
- package/dist/v2/runtime/handlers/handle-connect.mjs +49 -0
- package/dist/v2/runtime/handlers/handle-connect.mjs.map +1 -0
- package/dist/v2/runtime/handlers/handle-run.cjs +61 -0
- package/dist/v2/runtime/handlers/handle-run.cjs.map +1 -0
- package/dist/v2/runtime/handlers/handle-run.mjs +61 -0
- package/dist/v2/runtime/handlers/handle-run.mjs.map +1 -0
- package/dist/v2/runtime/handlers/handle-stop.cjs +47 -0
- package/dist/v2/runtime/handlers/handle-stop.cjs.map +1 -0
- package/dist/v2/runtime/handlers/handle-stop.mjs +46 -0
- package/dist/v2/runtime/handlers/handle-stop.mjs.map +1 -0
- package/dist/v2/runtime/handlers/handle-transcribe.cjs +112 -0
- package/dist/v2/runtime/handlers/handle-transcribe.cjs.map +1 -0
- package/dist/v2/runtime/handlers/handle-transcribe.mjs +111 -0
- package/dist/v2/runtime/handlers/handle-transcribe.mjs.map +1 -0
- package/dist/v2/runtime/handlers/header-utils.cjs +26 -0
- package/dist/v2/runtime/handlers/header-utils.cjs.map +1 -0
- package/dist/v2/runtime/handlers/header-utils.mjs +25 -0
- package/dist/v2/runtime/handlers/header-utils.mjs.map +1 -0
- package/dist/v2/runtime/handlers/intelligence/connect.cjs +37 -0
- package/dist/v2/runtime/handlers/intelligence/connect.cjs.map +1 -0
- package/dist/v2/runtime/handlers/intelligence/connect.mjs +37 -0
- package/dist/v2/runtime/handlers/intelligence/connect.mjs.map +1 -0
- package/dist/v2/runtime/handlers/intelligence/run.cjs +89 -0
- package/dist/v2/runtime/handlers/intelligence/run.cjs.map +1 -0
- package/dist/v2/runtime/handlers/intelligence/run.mjs +88 -0
- package/dist/v2/runtime/handlers/intelligence/run.mjs.map +1 -0
- package/dist/v2/runtime/handlers/intelligence/thread-names.cjs +146 -0
- package/dist/v2/runtime/handlers/intelligence/thread-names.cjs.map +1 -0
- package/dist/v2/runtime/handlers/intelligence/thread-names.mjs +145 -0
- package/dist/v2/runtime/handlers/intelligence/thread-names.mjs.map +1 -0
- package/dist/v2/runtime/handlers/intelligence/threads.cjs +159 -0
- package/dist/v2/runtime/handlers/intelligence/threads.cjs.map +1 -0
- package/dist/v2/runtime/handlers/intelligence/threads.mjs +154 -0
- package/dist/v2/runtime/handlers/intelligence/threads.mjs.map +1 -0
- package/dist/v2/runtime/handlers/shared/agent-utils.cjs +74 -0
- package/dist/v2/runtime/handlers/shared/agent-utils.cjs.map +1 -0
- package/dist/v2/runtime/handlers/shared/agent-utils.mjs +70 -0
- package/dist/v2/runtime/handlers/shared/agent-utils.mjs.map +1 -0
- package/dist/v2/runtime/handlers/shared/intelligence-utils.cjs +21 -0
- package/dist/v2/runtime/handlers/shared/intelligence-utils.cjs.map +1 -0
- package/dist/v2/runtime/handlers/shared/intelligence-utils.mjs +20 -0
- package/dist/v2/runtime/handlers/shared/intelligence-utils.mjs.map +1 -0
- package/dist/v2/runtime/handlers/shared/json-response.cjs +12 -0
- package/dist/v2/runtime/handlers/shared/json-response.cjs.map +1 -0
- package/dist/v2/runtime/handlers/shared/json-response.mjs +10 -0
- package/dist/v2/runtime/handlers/shared/json-response.mjs.map +1 -0
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.cjs +20 -0
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.cjs.map +1 -0
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.mjs +20 -0
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.mjs.map +1 -0
- package/dist/v2/runtime/handlers/shared/sse-response.cjs +69 -0
- package/dist/v2/runtime/handlers/shared/sse-response.cjs.map +1 -0
- package/dist/v2/runtime/handlers/shared/sse-response.mjs +68 -0
- package/dist/v2/runtime/handlers/shared/sse-response.mjs.map +1 -0
- package/dist/v2/runtime/handlers/sse/connect.cjs +18 -0
- package/dist/v2/runtime/handlers/sse/connect.cjs.map +1 -0
- package/dist/v2/runtime/handlers/sse/connect.mjs +18 -0
- package/dist/v2/runtime/handlers/sse/connect.mjs.map +1 -0
- package/dist/v2/runtime/handlers/sse/run.cjs +18 -0
- package/dist/v2/runtime/handlers/sse/run.cjs.map +1 -0
- package/dist/v2/runtime/handlers/sse/run.mjs +18 -0
- package/dist/v2/runtime/handlers/sse/run.mjs.map +1 -0
- package/dist/v2/runtime/index.d.cts +13 -0
- package/dist/v2/runtime/index.d.mts +14 -0
- package/dist/v2/runtime/intelligence-platform/client.cjs +333 -0
- package/dist/v2/runtime/intelligence-platform/client.cjs.map +1 -0
- package/dist/v2/runtime/intelligence-platform/client.d.cts +336 -0
- package/dist/v2/runtime/intelligence-platform/client.d.cts.map +1 -0
- package/dist/v2/runtime/intelligence-platform/client.d.mts +336 -0
- package/dist/v2/runtime/intelligence-platform/client.d.mts.map +1 -0
- package/dist/v2/runtime/intelligence-platform/client.mjs +331 -0
- package/dist/v2/runtime/intelligence-platform/client.mjs.map +1 -0
- package/dist/v2/runtime/intelligence-platform/index.d.mts +2 -0
- package/dist/v2/runtime/middleware-sse-parser.cjs +138 -0
- package/dist/v2/runtime/middleware-sse-parser.cjs.map +1 -0
- package/dist/v2/runtime/middleware-sse-parser.d.cts +22 -0
- package/dist/v2/runtime/middleware-sse-parser.d.cts.map +1 -0
- package/dist/v2/runtime/middleware-sse-parser.d.mts +22 -0
- package/dist/v2/runtime/middleware-sse-parser.d.mts.map +1 -0
- package/dist/v2/runtime/middleware-sse-parser.mjs +137 -0
- package/dist/v2/runtime/middleware-sse-parser.mjs.map +1 -0
- package/dist/v2/runtime/middleware.cjs +35 -0
- package/dist/v2/runtime/middleware.cjs.map +1 -0
- package/dist/v2/runtime/middleware.d.cts +32 -0
- package/dist/v2/runtime/middleware.d.cts.map +1 -0
- package/dist/v2/runtime/middleware.d.mts +32 -0
- package/dist/v2/runtime/middleware.d.mts.map +1 -0
- package/dist/v2/runtime/middleware.mjs +33 -0
- package/dist/v2/runtime/middleware.mjs.map +1 -0
- package/dist/v2/runtime/runner/agent-runner.cjs +8 -0
- package/dist/v2/runtime/runner/agent-runner.cjs.map +1 -0
- package/dist/v2/runtime/runner/agent-runner.d.cts +32 -0
- package/dist/v2/runtime/runner/agent-runner.d.cts.map +1 -0
- package/dist/v2/runtime/runner/agent-runner.d.mts +32 -0
- package/dist/v2/runtime/runner/agent-runner.d.mts.map +1 -0
- package/dist/v2/runtime/runner/agent-runner.mjs +7 -0
- package/dist/v2/runtime/runner/agent-runner.mjs.map +1 -0
- package/dist/v2/runtime/runner/in-memory.cjs +223 -0
- package/dist/v2/runtime/runner/in-memory.cjs.map +1 -0
- package/dist/v2/runtime/runner/in-memory.d.cts +15 -0
- package/dist/v2/runtime/runner/in-memory.d.cts.map +1 -0
- package/dist/v2/runtime/runner/in-memory.d.mts +15 -0
- package/dist/v2/runtime/runner/in-memory.d.mts.map +1 -0
- package/dist/v2/runtime/runner/in-memory.mjs +222 -0
- package/dist/v2/runtime/runner/in-memory.mjs.map +1 -0
- package/dist/v2/runtime/runner/index.d.cts +6 -0
- package/dist/v2/runtime/runner/index.d.mts +6 -0
- package/dist/v2/runtime/runner/index.mjs +7 -0
- package/dist/v2/runtime/runner/intelligence.cjs +246 -0
- package/dist/v2/runtime/runner/intelligence.cjs.map +1 -0
- package/dist/v2/runtime/runner/intelligence.d.cts +57 -0
- package/dist/v2/runtime/runner/intelligence.d.cts.map +1 -0
- package/dist/v2/runtime/runner/intelligence.d.mts +57 -0
- package/dist/v2/runtime/runner/intelligence.d.mts.map +1 -0
- package/dist/v2/runtime/runner/intelligence.mjs +245 -0
- package/dist/v2/runtime/runner/intelligence.mjs.map +1 -0
- package/dist/v2/runtime/runtime.cjs +101 -0
- package/dist/v2/runtime/runtime.cjs.map +1 -0
- package/dist/v2/runtime/runtime.d.cts +132 -0
- package/dist/v2/runtime/runtime.d.cts.map +1 -0
- package/dist/v2/runtime/runtime.d.mts +133 -0
- package/dist/v2/runtime/runtime.d.mts.map +1 -0
- package/dist/v2/runtime/runtime.mjs +97 -0
- package/dist/v2/runtime/runtime.mjs.map +1 -0
- package/dist/v2/runtime/telemetry/scarf-client.cjs +32 -0
- package/dist/v2/runtime/telemetry/scarf-client.cjs.map +1 -0
- package/dist/v2/runtime/telemetry/scarf-client.mjs +32 -0
- package/dist/v2/runtime/telemetry/scarf-client.mjs.map +1 -0
- package/dist/v2/runtime/telemetry/telemetry-client.cjs +35 -0
- package/dist/v2/runtime/telemetry/telemetry-client.cjs.map +1 -0
- package/dist/v2/runtime/telemetry/telemetry-client.mjs +35 -0
- package/dist/v2/runtime/telemetry/telemetry-client.mjs.map +1 -0
- package/dist/v2/runtime/transcription-service/transcription-service.cjs +8 -0
- package/dist/v2/runtime/transcription-service/transcription-service.cjs.map +1 -0
- package/dist/v2/runtime/transcription-service/transcription-service.d.cts +15 -0
- package/dist/v2/runtime/transcription-service/transcription-service.d.cts.map +1 -0
- package/dist/v2/runtime/transcription-service/transcription-service.d.mts +15 -0
- package/dist/v2/runtime/transcription-service/transcription-service.d.mts.map +1 -0
- package/dist/v2/runtime/transcription-service/transcription-service.mjs +7 -0
- package/dist/v2/runtime/transcription-service/transcription-service.mjs.map +1 -0
- package/package.json +24 -7
- package/src/agent/__tests__/ai-sdk-v6-compat.test.ts +116 -0
- package/src/agent/__tests__/basic-agent.test.ts +1248 -0
- package/src/agent/__tests__/config-tools-execution.test.ts +516 -0
- package/src/agent/__tests__/mcp-clients.test.ts +260 -0
- package/src/agent/__tests__/property-overrides.test.ts +598 -0
- package/src/agent/__tests__/standard-schema-tools.test.ts +313 -0
- package/src/agent/__tests__/standard-schema-types.test.ts +158 -0
- package/src/agent/__tests__/state-tools.test.ts +436 -0
- package/src/agent/__tests__/test-helpers.ts +178 -0
- package/src/agent/__tests__/utils.test.ts +536 -0
- package/src/agent/__tests__/zod-regression.test.ts +350 -0
- package/src/agent/index.ts +1305 -0
- package/src/graphql/message-conversion/agui-to-gql.test.ts +1 -1
- package/src/graphql/message-conversion/agui-to-gql.ts +1 -1
- package/src/graphql/message-conversion/gql-to-agui.ts +1 -1
- package/src/graphql/message-conversion/roundtrip-conversion.test.ts +1 -1
- package/src/lib/integrations/nextjs/app-router.ts +2 -2
- package/src/lib/integrations/node-http/index.ts +2 -2
- package/src/lib/runtime/copilot-runtime.ts +3 -5
- package/src/lib/runtime/telemetry-agent-runner.ts +1 -1
- package/src/service-adapters/conversion.test.ts +1 -1
- package/src/service-adapters/conversion.ts +1 -28
- package/src/v2/index.ts +5 -2
- package/src/v2/runtime/__tests__/cors-credentials.test.ts +320 -0
- package/src/v2/runtime/__tests__/express-abort-signal.test.ts +25 -0
- package/src/v2/runtime/__tests__/express-body-order.test.ts +76 -0
- package/src/v2/runtime/__tests__/express-single-sse.test.ts +122 -0
- package/src/v2/runtime/__tests__/get-runtime-info.test.ts +141 -0
- package/src/v2/runtime/__tests__/handle-connect.test.ts +423 -0
- package/src/v2/runtime/__tests__/handle-run.test.ts +910 -0
- package/src/v2/runtime/__tests__/handle-threads.test.ts +388 -0
- package/src/v2/runtime/__tests__/handle-transcribe.test.ts +301 -0
- package/src/v2/runtime/__tests__/header-utils.test.ts +88 -0
- package/src/v2/runtime/__tests__/in-process-agent-runner-messages.test.ts +230 -0
- package/src/v2/runtime/__tests__/in-process-agent-runner.test.ts +1030 -0
- package/src/v2/runtime/__tests__/middleware-express.test.ts +206 -0
- package/src/v2/runtime/__tests__/middleware-single-express.test.ts +211 -0
- package/src/v2/runtime/__tests__/middleware-single.test.ts +225 -0
- package/src/v2/runtime/__tests__/middleware-sse-parser.test.ts +187 -0
- package/src/v2/runtime/__tests__/middleware.test.ts +251 -0
- package/src/v2/runtime/__tests__/routing-express.test.ts +174 -0
- package/src/v2/runtime/__tests__/routing-single-express.test.ts +168 -0
- package/src/v2/runtime/__tests__/routing-single.test.ts +193 -0
- package/src/v2/runtime/__tests__/routing.test.ts +257 -0
- package/src/v2/runtime/__tests__/runtime.test.ts +123 -0
- package/src/v2/runtime/__tests__/telemetry.test.ts +167 -0
- package/src/v2/runtime/__tests__/thread-names.test.ts +188 -0
- package/src/v2/runtime/endpoints/express-single.ts +231 -0
- package/src/v2/runtime/endpoints/express-utils.ts +182 -0
- package/src/v2/runtime/endpoints/express.ts +275 -0
- package/src/v2/runtime/endpoints/hono-single.ts +212 -0
- package/src/v2/runtime/endpoints/hono.ts +314 -0
- package/src/v2/runtime/endpoints/index.ts +4 -0
- package/src/v2/runtime/endpoints/single-route-helpers.ts +125 -0
- package/src/v2/runtime/express.ts +2 -0
- package/src/v2/runtime/handler.ts +3 -0
- package/src/v2/runtime/handlers/get-runtime-info.ts +79 -0
- package/src/v2/runtime/handlers/handle-connect.ts +76 -0
- package/src/v2/runtime/handlers/handle-run.ts +89 -0
- package/src/v2/runtime/handlers/handle-stop.ts +76 -0
- package/src/v2/runtime/handlers/handle-threads.ts +7 -0
- package/src/v2/runtime/handlers/handle-transcribe.ts +256 -0
- package/src/v2/runtime/handlers/header-utils.ts +24 -0
- package/src/v2/runtime/handlers/intelligence/connect.ts +65 -0
- package/src/v2/runtime/handlers/intelligence/run.ts +152 -0
- package/src/v2/runtime/handlers/intelligence/thread-names.ts +246 -0
- package/src/v2/runtime/handlers/intelligence/threads.ts +233 -0
- package/src/v2/runtime/handlers/shared/agent-utils.ts +136 -0
- package/src/v2/runtime/handlers/shared/intelligence-utils.ts +21 -0
- package/src/v2/runtime/handlers/shared/json-response.ts +6 -0
- package/src/v2/runtime/handlers/shared/resolve-intelligence-user.ts +25 -0
- package/src/v2/runtime/handlers/shared/sse-response.ts +100 -0
- package/src/v2/runtime/handlers/sse/connect.ts +24 -0
- package/src/v2/runtime/handlers/sse/run.ts +27 -0
- package/src/v2/runtime/index.ts +20 -0
- package/src/v2/runtime/intelligence-platform/__tests__/client.test.ts +605 -0
- package/src/v2/runtime/intelligence-platform/client.ts +659 -0
- package/src/v2/runtime/intelligence-platform/index.ts +10 -0
- package/src/v2/runtime/middleware-sse-parser.ts +200 -0
- package/src/v2/runtime/middleware.ts +115 -0
- package/src/v2/runtime/runner/__tests__/finalize-events.test.ts +109 -0
- package/src/v2/runtime/runner/__tests__/in-memory-runner.e2e.test.ts +775 -0
- package/src/v2/runtime/runner/__tests__/in-memory-runner.test.ts +363 -0
- package/src/v2/runtime/runner/__tests__/intelligence-runner.test.ts +981 -0
- package/src/v2/runtime/runner/agent-runner.ts +36 -0
- package/src/v2/runtime/runner/in-memory.ts +381 -0
- package/src/v2/runtime/runner/index.ts +4 -0
- package/src/v2/runtime/runner/intelligence.ts +429 -0
- package/src/v2/runtime/runtime.ts +260 -0
- package/src/v2/runtime/telemetry/events.ts +35 -0
- package/src/v2/runtime/telemetry/index.ts +7 -0
- package/src/v2/runtime/telemetry/scarf-client.ts +39 -0
- package/src/v2/runtime/telemetry/telemetry-client.ts +70 -0
- package/src/v2/runtime/transcription-service/transcription-service.ts +11 -0
- package/tsconfig.json +9 -2
- package/tsdown.config.ts +1 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Request as ExpressRequest,
|
|
3
|
+
Response as ExpressResponse,
|
|
4
|
+
} from "express";
|
|
5
|
+
import { Readable } from "node:stream";
|
|
6
|
+
import { pipeline } from "node:stream";
|
|
7
|
+
import { promisify } from "node:util";
|
|
8
|
+
import { logger } from "@copilotkit/shared";
|
|
9
|
+
|
|
10
|
+
const streamPipeline = promisify(pipeline);
|
|
11
|
+
|
|
12
|
+
const METHODS_WITHOUT_BODY = new Set(["GET", "HEAD"]);
|
|
13
|
+
|
|
14
|
+
export function createFetchRequestFromExpress(req: ExpressRequest): Request {
|
|
15
|
+
const method = req.method?.toUpperCase() ?? "GET";
|
|
16
|
+
const origin = buildOrigin(req);
|
|
17
|
+
const url = `${origin}${req.originalUrl ?? req.url ?? ""}`;
|
|
18
|
+
|
|
19
|
+
const headers = new Headers();
|
|
20
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
21
|
+
if (value === undefined) continue;
|
|
22
|
+
if (Array.isArray(value)) {
|
|
23
|
+
value.forEach((v) => headers.append(key, v));
|
|
24
|
+
} else {
|
|
25
|
+
headers.set(key, value);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const init: RequestInit & { duplex?: "half" } = {
|
|
30
|
+
method,
|
|
31
|
+
headers,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const hasParsedBody = req.body !== undefined && req.body !== null;
|
|
35
|
+
const streamConsumed = isStreamConsumed(req, hasParsedBody);
|
|
36
|
+
|
|
37
|
+
if (!METHODS_WITHOUT_BODY.has(method)) {
|
|
38
|
+
const canStreamBody = req.readable !== false && !streamConsumed;
|
|
39
|
+
|
|
40
|
+
if (canStreamBody) {
|
|
41
|
+
init.body = Readable.toWeb(req) as unknown as BodyInit;
|
|
42
|
+
init.duplex = "half";
|
|
43
|
+
} else if (hasParsedBody) {
|
|
44
|
+
const { body, contentType } = synthesizeBody(req.body);
|
|
45
|
+
if (contentType) {
|
|
46
|
+
headers.set("content-type", contentType);
|
|
47
|
+
}
|
|
48
|
+
headers.delete("content-length");
|
|
49
|
+
if (body !== undefined) {
|
|
50
|
+
init.body = body;
|
|
51
|
+
}
|
|
52
|
+
logger.info(
|
|
53
|
+
{
|
|
54
|
+
url,
|
|
55
|
+
method,
|
|
56
|
+
readable: req.readable,
|
|
57
|
+
readableEnded: req.readableEnded,
|
|
58
|
+
complete: req.complete,
|
|
59
|
+
},
|
|
60
|
+
"Express request stream already consumed; synthesized body from parsed content",
|
|
61
|
+
);
|
|
62
|
+
} else {
|
|
63
|
+
headers.delete("content-length");
|
|
64
|
+
logger.warn(
|
|
65
|
+
{ url, method },
|
|
66
|
+
"Request stream already consumed but no body was available; sending empty body",
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const controller = new AbortController();
|
|
72
|
+
const abort = () => controller.abort();
|
|
73
|
+
req.on("aborted", abort);
|
|
74
|
+
req.on("error", abort);
|
|
75
|
+
req.on("close", () => {
|
|
76
|
+
if (req.aborted) {
|
|
77
|
+
abort();
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
init.signal = controller.signal;
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
return new Request(url, init);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (error instanceof TypeError && /disturbed|locked/i.test(error.message)) {
|
|
86
|
+
// Fallback to synthesized/empty body when the stream was already consumed.
|
|
87
|
+
headers.delete("content-length");
|
|
88
|
+
delete init.duplex;
|
|
89
|
+
|
|
90
|
+
if (hasParsedBody) {
|
|
91
|
+
const { body, contentType } = synthesizeBody(req.body);
|
|
92
|
+
if (contentType) {
|
|
93
|
+
headers.set("content-type", contentType);
|
|
94
|
+
}
|
|
95
|
+
init.body = body;
|
|
96
|
+
logger.info(
|
|
97
|
+
{ url, method },
|
|
98
|
+
"Request stream disturbed while constructing Request; reused parsed body",
|
|
99
|
+
);
|
|
100
|
+
} else {
|
|
101
|
+
init.body = undefined;
|
|
102
|
+
logger.warn(
|
|
103
|
+
{ url, method },
|
|
104
|
+
"Request stream was disturbed; falling back to empty body",
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return new Request(url, init);
|
|
109
|
+
}
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export async function sendFetchResponse(
|
|
115
|
+
res: ExpressResponse,
|
|
116
|
+
response: Response,
|
|
117
|
+
): Promise<void> {
|
|
118
|
+
res.status(response.status);
|
|
119
|
+
|
|
120
|
+
response.headers.forEach((value, key) => {
|
|
121
|
+
if (key.toLowerCase() === "content-length" && response.body !== null) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
res.setHeader(key, value);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
if (!response.body) {
|
|
128
|
+
res.end();
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const nodeStream = Readable.fromWeb(response.body as any);
|
|
133
|
+
try {
|
|
134
|
+
await streamPipeline(nodeStream, res);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
res.destroy(error as Error);
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function buildOrigin(req: ExpressRequest): string {
|
|
142
|
+
const protocol = req.protocol || (req.secure ? "https" : "http");
|
|
143
|
+
const host = req.get("host") ?? "localhost";
|
|
144
|
+
return `${protocol}://${host}`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function isStreamConsumed(
|
|
148
|
+
req: ExpressRequest,
|
|
149
|
+
hasParsedBody: boolean,
|
|
150
|
+
): boolean {
|
|
151
|
+
const state = (
|
|
152
|
+
req as unknown as {
|
|
153
|
+
_readableState?: { ended?: boolean; endEmitted?: boolean };
|
|
154
|
+
}
|
|
155
|
+
)._readableState;
|
|
156
|
+
return Boolean(
|
|
157
|
+
hasParsedBody ||
|
|
158
|
+
req.readableEnded ||
|
|
159
|
+
req.complete ||
|
|
160
|
+
state?.ended ||
|
|
161
|
+
state?.endEmitted,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function synthesizeBody(body: unknown): {
|
|
166
|
+
body?: BodyInit;
|
|
167
|
+
contentType?: string;
|
|
168
|
+
} {
|
|
169
|
+
if (Buffer.isBuffer(body) || body instanceof Uint8Array) {
|
|
170
|
+
return { body };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (typeof body === "string") {
|
|
174
|
+
return { body };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (typeof body === "object" && body !== undefined) {
|
|
178
|
+
return { body: JSON.stringify(body), contentType: "application/json" };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return {};
|
|
182
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import type {
|
|
3
|
+
Request as ExpressRequest,
|
|
4
|
+
Response as ExpressResponse,
|
|
5
|
+
NextFunction,
|
|
6
|
+
Router,
|
|
7
|
+
} from "express";
|
|
8
|
+
import cors from "cors";
|
|
9
|
+
|
|
10
|
+
import { CopilotRuntimeLike } from "../runtime";
|
|
11
|
+
import { telemetry } from "../telemetry";
|
|
12
|
+
import { handleRunAgent } from "../handlers/handle-run";
|
|
13
|
+
import { handleConnectAgent } from "../handlers/handle-connect";
|
|
14
|
+
import { handleStopAgent } from "../handlers/handle-stop";
|
|
15
|
+
import { handleGetRuntimeInfo } from "../handlers/get-runtime-info";
|
|
16
|
+
import { handleTranscribe } from "../handlers/handle-transcribe";
|
|
17
|
+
import {
|
|
18
|
+
handleListThreads,
|
|
19
|
+
handleSubscribeToThreads,
|
|
20
|
+
handleUpdateThread,
|
|
21
|
+
handleArchiveThread,
|
|
22
|
+
handleDeleteThread,
|
|
23
|
+
} from "../handlers/handle-threads";
|
|
24
|
+
import { logger, getLicenseWarningHeader } from "@copilotkit/shared";
|
|
25
|
+
import {
|
|
26
|
+
callBeforeRequestMiddleware,
|
|
27
|
+
callAfterRequestMiddleware,
|
|
28
|
+
} from "../middleware";
|
|
29
|
+
import {
|
|
30
|
+
createFetchRequestFromExpress,
|
|
31
|
+
sendFetchResponse,
|
|
32
|
+
} from "./express-utils";
|
|
33
|
+
|
|
34
|
+
interface CopilotExpressEndpointParams {
|
|
35
|
+
runtime: CopilotRuntimeLike;
|
|
36
|
+
basePath: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function createCopilotEndpointExpress({
|
|
40
|
+
runtime,
|
|
41
|
+
basePath,
|
|
42
|
+
}: CopilotExpressEndpointParams): Router {
|
|
43
|
+
const router = express.Router();
|
|
44
|
+
const normalizedBase = normalizeBasePath(basePath);
|
|
45
|
+
|
|
46
|
+
// Fire instance_created telemetry - resolve agents if needed
|
|
47
|
+
Promise.resolve(runtime.agents)
|
|
48
|
+
.then((agents) => {
|
|
49
|
+
telemetry.capture("oss.runtime.instance_created", {
|
|
50
|
+
actionsAmount: 0,
|
|
51
|
+
endpointTypes: [],
|
|
52
|
+
endpointsAmount: 0,
|
|
53
|
+
agentsAmount: Object.keys(agents).length,
|
|
54
|
+
"cloud.api_key_provided": false,
|
|
55
|
+
});
|
|
56
|
+
})
|
|
57
|
+
.catch(() => {
|
|
58
|
+
// Silently fail - telemetry should not break the application
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
router.use(
|
|
62
|
+
cors({
|
|
63
|
+
origin: "*",
|
|
64
|
+
methods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH", "OPTIONS"],
|
|
65
|
+
allowedHeaders: ["*"],
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
router.post(
|
|
70
|
+
joinPath(normalizedBase, "/agent/:agentId/run"),
|
|
71
|
+
createRouteHandler(runtime, async ({ request, req }) => {
|
|
72
|
+
const agentId = req.params.agentId as string;
|
|
73
|
+
return handleRunAgent({ runtime, request, agentId });
|
|
74
|
+
}),
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
router.post(
|
|
78
|
+
joinPath(normalizedBase, "/agent/:agentId/connect"),
|
|
79
|
+
createRouteHandler(runtime, async ({ request, req }) => {
|
|
80
|
+
const agentId = req.params.agentId as string;
|
|
81
|
+
return handleConnectAgent({ runtime, request, agentId });
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
router.post(
|
|
86
|
+
joinPath(normalizedBase, "/agent/:agentId/stop/:threadId"),
|
|
87
|
+
createRouteHandler(runtime, async ({ request, req }) => {
|
|
88
|
+
const agentId = req.params.agentId as string;
|
|
89
|
+
const threadId = req.params.threadId as string;
|
|
90
|
+
return handleStopAgent({ runtime, request, agentId, threadId });
|
|
91
|
+
}),
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
router.get(
|
|
95
|
+
joinPath(normalizedBase, "/info"),
|
|
96
|
+
createRouteHandler(runtime, async ({ request }) => {
|
|
97
|
+
return handleGetRuntimeInfo({ runtime, request });
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
router.post(
|
|
102
|
+
joinPath(normalizedBase, "/transcribe"),
|
|
103
|
+
createRouteHandler(runtime, async ({ request }) => {
|
|
104
|
+
return handleTranscribe({ runtime, request });
|
|
105
|
+
}),
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
router.get(
|
|
109
|
+
joinPath(normalizedBase, "/threads"),
|
|
110
|
+
createRouteHandler(runtime, async ({ request }) => {
|
|
111
|
+
return handleListThreads({ runtime, request });
|
|
112
|
+
}),
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
router.post(
|
|
116
|
+
joinPath(normalizedBase, "/threads/subscribe"),
|
|
117
|
+
createRouteHandler(runtime, async ({ request }) => {
|
|
118
|
+
return handleSubscribeToThreads({ runtime, request });
|
|
119
|
+
}),
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
router.patch(
|
|
123
|
+
joinPath(normalizedBase, "/threads/:threadId"),
|
|
124
|
+
createRouteHandler(runtime, async ({ request, req }) => {
|
|
125
|
+
const threadId = req.params.threadId as string;
|
|
126
|
+
return handleUpdateThread({ runtime, request, threadId });
|
|
127
|
+
}),
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
router.post(
|
|
131
|
+
joinPath(normalizedBase, "/threads/:threadId/archive"),
|
|
132
|
+
createRouteHandler(runtime, async ({ request, req }) => {
|
|
133
|
+
const threadId = req.params.threadId as string;
|
|
134
|
+
return handleArchiveThread({ runtime, request, threadId });
|
|
135
|
+
}),
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
router.delete(
|
|
139
|
+
joinPath(normalizedBase, "/threads/:threadId"),
|
|
140
|
+
createRouteHandler(runtime, async ({ request, req }) => {
|
|
141
|
+
const threadId = req.params.threadId as string;
|
|
142
|
+
return handleDeleteThread({ runtime, request, threadId });
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
router.use(joinPath(normalizedBase, "*"), (req, res) => {
|
|
147
|
+
res.status(404).json({ error: "Not found" });
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
return router;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
type RouteHandlerContext = {
|
|
154
|
+
request: Request;
|
|
155
|
+
req: ExpressRequest;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
type RouteHandlerFactory = (ctx: RouteHandlerContext) => Promise<Response>;
|
|
159
|
+
|
|
160
|
+
function createRouteHandler(
|
|
161
|
+
runtime: CopilotRuntimeLike,
|
|
162
|
+
factory: RouteHandlerFactory,
|
|
163
|
+
) {
|
|
164
|
+
return async (
|
|
165
|
+
req: ExpressRequest,
|
|
166
|
+
res: ExpressResponse,
|
|
167
|
+
next: NextFunction,
|
|
168
|
+
) => {
|
|
169
|
+
const path = req.originalUrl ?? req.path;
|
|
170
|
+
let request = createFetchRequestFromExpress(req);
|
|
171
|
+
|
|
172
|
+
const warning = getLicenseWarningHeader(runtime.licenseChecker);
|
|
173
|
+
if (warning) res.setHeader(warning.key, warning.value);
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
const maybeModifiedRequest = await callBeforeRequestMiddleware({
|
|
177
|
+
runtime,
|
|
178
|
+
request,
|
|
179
|
+
path,
|
|
180
|
+
});
|
|
181
|
+
if (maybeModifiedRequest) {
|
|
182
|
+
request = maybeModifiedRequest;
|
|
183
|
+
}
|
|
184
|
+
} catch (error) {
|
|
185
|
+
logger.error(
|
|
186
|
+
{ err: error, url: request.url, path },
|
|
187
|
+
"Error running before request middleware",
|
|
188
|
+
);
|
|
189
|
+
if (error instanceof Response) {
|
|
190
|
+
try {
|
|
191
|
+
await sendFetchResponse(res, error);
|
|
192
|
+
} catch (streamError) {
|
|
193
|
+
next(streamError);
|
|
194
|
+
}
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
next(error);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
try {
|
|
202
|
+
const response = await factory({ request, req });
|
|
203
|
+
const responseForMiddleware = response.clone();
|
|
204
|
+
await sendFetchResponse(res, response);
|
|
205
|
+
callAfterRequestMiddleware({
|
|
206
|
+
runtime,
|
|
207
|
+
response: responseForMiddleware,
|
|
208
|
+
path,
|
|
209
|
+
}).catch((error) => {
|
|
210
|
+
logger.error(
|
|
211
|
+
{ err: error, url: req.originalUrl ?? req.url, path },
|
|
212
|
+
"Error running after request middleware",
|
|
213
|
+
);
|
|
214
|
+
});
|
|
215
|
+
} catch (error) {
|
|
216
|
+
if (error instanceof Response) {
|
|
217
|
+
const errorResponseForMiddleware = error.clone();
|
|
218
|
+
try {
|
|
219
|
+
await sendFetchResponse(res, error);
|
|
220
|
+
} catch (streamError) {
|
|
221
|
+
next(streamError);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
callAfterRequestMiddleware({
|
|
225
|
+
runtime,
|
|
226
|
+
response: errorResponseForMiddleware,
|
|
227
|
+
path,
|
|
228
|
+
}).catch((mwError) => {
|
|
229
|
+
logger.error(
|
|
230
|
+
{ err: mwError, url: req.originalUrl ?? req.url, path },
|
|
231
|
+
"Error running after request middleware",
|
|
232
|
+
);
|
|
233
|
+
});
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
logger.error(
|
|
237
|
+
{ err: error, url: request.url, path },
|
|
238
|
+
"Error running request handler",
|
|
239
|
+
);
|
|
240
|
+
next(error);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function normalizeBasePath(path: string): string {
|
|
246
|
+
if (!path) {
|
|
247
|
+
throw new Error("basePath must be provided for Express endpoint");
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (!path.startsWith("/")) {
|
|
251
|
+
return `/${path}`;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (path.length > 1 && path.endsWith("/")) {
|
|
255
|
+
return path.slice(0, -1);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return path;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function joinPath(basePath: string, suffix: string): string {
|
|
262
|
+
if (basePath === "/") {
|
|
263
|
+
return suffix.startsWith("/") ? suffix : `/${suffix}`;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (!suffix) {
|
|
267
|
+
return basePath;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (suffix === "*") {
|
|
271
|
+
return `${basePath}/*`;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return `${basePath}${suffix.startsWith("/") ? suffix : `/${suffix}`}`;
|
|
275
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { cors } from "hono/cors";
|
|
3
|
+
|
|
4
|
+
import { CopilotRuntimeLike } from "../runtime";
|
|
5
|
+
import { handleRunAgent } from "../handlers/handle-run";
|
|
6
|
+
import { handleConnectAgent } from "../handlers/handle-connect";
|
|
7
|
+
import { handleStopAgent } from "../handlers/handle-stop";
|
|
8
|
+
import { handleGetRuntimeInfo } from "../handlers/get-runtime-info";
|
|
9
|
+
import { handleTranscribe } from "../handlers/handle-transcribe";
|
|
10
|
+
import { logger } from "@copilotkit/shared";
|
|
11
|
+
import {
|
|
12
|
+
callBeforeRequestMiddleware,
|
|
13
|
+
callAfterRequestMiddleware,
|
|
14
|
+
} from "../middleware";
|
|
15
|
+
import {
|
|
16
|
+
createJsonRequest,
|
|
17
|
+
expectString,
|
|
18
|
+
MethodCall,
|
|
19
|
+
parseMethodCall,
|
|
20
|
+
} from "./single-route-helpers";
|
|
21
|
+
|
|
22
|
+
import { CopilotEndpointCorsConfig } from "./hono";
|
|
23
|
+
|
|
24
|
+
interface CopilotSingleEndpointParams {
|
|
25
|
+
runtime: CopilotRuntimeLike;
|
|
26
|
+
/**
|
|
27
|
+
* Absolute path at which to mount the single-route endpoint (e.g. "/api/copilotkit").
|
|
28
|
+
*/
|
|
29
|
+
basePath: string;
|
|
30
|
+
/**
|
|
31
|
+
* Optional CORS configuration. When not provided, defaults to allowing all origins without credentials.
|
|
32
|
+
* To support HTTP-only cookies, provide cors config with credentials: true and explicit origin.
|
|
33
|
+
*/
|
|
34
|
+
cors?: CopilotEndpointCorsConfig;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type CopilotEndpointContext = {
|
|
38
|
+
Variables: {
|
|
39
|
+
modifiedRequest?: Request;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export function createCopilotEndpointSingleRoute({
|
|
44
|
+
runtime,
|
|
45
|
+
basePath,
|
|
46
|
+
cors: corsConfig,
|
|
47
|
+
}: CopilotSingleEndpointParams) {
|
|
48
|
+
const app = new Hono<CopilotEndpointContext>();
|
|
49
|
+
const routePath = normalizePath(basePath);
|
|
50
|
+
|
|
51
|
+
return app
|
|
52
|
+
.basePath(routePath)
|
|
53
|
+
.use(
|
|
54
|
+
"*",
|
|
55
|
+
cors({
|
|
56
|
+
origin: corsConfig?.origin ?? "*",
|
|
57
|
+
allowMethods: [
|
|
58
|
+
"GET",
|
|
59
|
+
"HEAD",
|
|
60
|
+
"PUT",
|
|
61
|
+
"POST",
|
|
62
|
+
"DELETE",
|
|
63
|
+
"PATCH",
|
|
64
|
+
"OPTIONS",
|
|
65
|
+
],
|
|
66
|
+
allowHeaders: ["*"],
|
|
67
|
+
credentials: corsConfig?.credentials ?? false,
|
|
68
|
+
}),
|
|
69
|
+
)
|
|
70
|
+
.use("*", async (c, next) => {
|
|
71
|
+
const request = c.req.raw;
|
|
72
|
+
const path = c.req.path;
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
const maybeModifiedRequest = await callBeforeRequestMiddleware({
|
|
76
|
+
runtime,
|
|
77
|
+
request,
|
|
78
|
+
path,
|
|
79
|
+
});
|
|
80
|
+
if (maybeModifiedRequest) {
|
|
81
|
+
c.set("modifiedRequest", maybeModifiedRequest);
|
|
82
|
+
}
|
|
83
|
+
} catch (error) {
|
|
84
|
+
logger.error(
|
|
85
|
+
{ err: error, url: request.url, path },
|
|
86
|
+
"Error running before request middleware",
|
|
87
|
+
);
|
|
88
|
+
if (error instanceof Response) {
|
|
89
|
+
return error;
|
|
90
|
+
}
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
await next();
|
|
95
|
+
})
|
|
96
|
+
.use("*", async (c, next) => {
|
|
97
|
+
await next();
|
|
98
|
+
|
|
99
|
+
const response = c.res.clone();
|
|
100
|
+
const path = c.req.path;
|
|
101
|
+
|
|
102
|
+
callAfterRequestMiddleware({
|
|
103
|
+
runtime,
|
|
104
|
+
response,
|
|
105
|
+
path,
|
|
106
|
+
}).catch((error) => {
|
|
107
|
+
logger.error(
|
|
108
|
+
{ err: error, url: c.req.url, path },
|
|
109
|
+
"Error running after request middleware",
|
|
110
|
+
);
|
|
111
|
+
});
|
|
112
|
+
})
|
|
113
|
+
.post("/", async (c) => {
|
|
114
|
+
const request = c.get("modifiedRequest") || c.req.raw;
|
|
115
|
+
|
|
116
|
+
let methodCall: MethodCall;
|
|
117
|
+
try {
|
|
118
|
+
methodCall = await parseMethodCall(request);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
if (error instanceof Response) {
|
|
121
|
+
logger.warn({ url: request.url }, "Invalid single-route payload");
|
|
122
|
+
return error;
|
|
123
|
+
}
|
|
124
|
+
logger.warn(
|
|
125
|
+
{ err: error, url: request.url },
|
|
126
|
+
"Invalid single-route payload",
|
|
127
|
+
);
|
|
128
|
+
return c.json(
|
|
129
|
+
{
|
|
130
|
+
error: "invalid_request",
|
|
131
|
+
message:
|
|
132
|
+
error instanceof Error
|
|
133
|
+
? error.message
|
|
134
|
+
: "Invalid request payload",
|
|
135
|
+
},
|
|
136
|
+
400,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
switch (methodCall.method) {
|
|
142
|
+
case "agent/run": {
|
|
143
|
+
const agentId = expectString(methodCall.params, "agentId");
|
|
144
|
+
const handlerRequest = createJsonRequest(request, methodCall.body);
|
|
145
|
+
return await handleRunAgent({
|
|
146
|
+
runtime,
|
|
147
|
+
request: handlerRequest,
|
|
148
|
+
agentId,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
case "agent/connect": {
|
|
152
|
+
const agentId = expectString(methodCall.params, "agentId");
|
|
153
|
+
const handlerRequest = createJsonRequest(request, methodCall.body);
|
|
154
|
+
return await handleConnectAgent({
|
|
155
|
+
runtime,
|
|
156
|
+
request: handlerRequest,
|
|
157
|
+
agentId,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
case "agent/stop": {
|
|
161
|
+
const agentId = expectString(methodCall.params, "agentId");
|
|
162
|
+
const threadId = expectString(methodCall.params, "threadId");
|
|
163
|
+
return await handleStopAgent({
|
|
164
|
+
runtime,
|
|
165
|
+
request,
|
|
166
|
+
agentId,
|
|
167
|
+
threadId,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
case "info": {
|
|
171
|
+
return await handleGetRuntimeInfo({ runtime, request });
|
|
172
|
+
}
|
|
173
|
+
case "transcribe": {
|
|
174
|
+
const handlerRequest = createJsonRequest(request, methodCall.body);
|
|
175
|
+
return await handleTranscribe({ runtime, request: handlerRequest });
|
|
176
|
+
}
|
|
177
|
+
default: {
|
|
178
|
+
const exhaustiveCheck: never = methodCall.method;
|
|
179
|
+
return exhaustiveCheck;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
} catch (error) {
|
|
183
|
+
if (error instanceof Response) {
|
|
184
|
+
return error;
|
|
185
|
+
}
|
|
186
|
+
logger.error(
|
|
187
|
+
{ err: error, url: request.url, method: methodCall.method },
|
|
188
|
+
"Error running single-route handler",
|
|
189
|
+
);
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
})
|
|
193
|
+
.notFound((c) => {
|
|
194
|
+
return c.json({ error: "Not found" }, 404);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function normalizePath(path: string): string {
|
|
199
|
+
if (!path) {
|
|
200
|
+
throw new Error("basePath must be provided for single-route endpoint");
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (!path.startsWith("/")) {
|
|
204
|
+
return `/${path}`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (path.length > 1 && path.endsWith("/")) {
|
|
208
|
+
return path.slice(0, -1);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return path;
|
|
212
|
+
}
|