@copilotkit/runtime 1.57.3 → 1.58.0-canary.thread-id-propagation
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/lib/observability.d.cts +1 -1
- package/dist/lib/observability.d.cts.map +1 -1
- package/dist/lib/observability.d.mts +1 -1
- package/dist/lib/observability.d.mts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.cjs +2 -0
- package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.mjs +2 -0
- package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
- package/dist/package.cjs +5 -8
- package/dist/package.mjs +5 -8
- package/dist/v2/runtime/core/runtime.cjs +4 -1
- package/dist/v2/runtime/core/runtime.cjs.map +1 -1
- package/dist/v2/runtime/core/runtime.d.cts.map +1 -1
- package/dist/v2/runtime/core/runtime.d.mts.map +1 -1
- package/dist/v2/runtime/core/runtime.mjs +4 -1
- package/dist/v2/runtime/core/runtime.mjs.map +1 -1
- package/dist/v2/runtime/handlers/get-runtime-info.cjs +1 -1
- package/dist/v2/runtime/handlers/get-runtime-info.mjs +1 -1
- package/dist/v2/runtime/handlers/handle-connect.cjs +1 -1
- package/dist/v2/runtime/handlers/handle-connect.mjs +1 -1
- package/dist/v2/runtime/handlers/handle-run.cjs +1 -1
- package/dist/v2/runtime/handlers/handle-run.mjs +1 -1
- package/dist/v2/runtime/handlers/shared/agent-utils.cjs +1 -1
- package/dist/v2/runtime/handlers/shared/agent-utils.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/agent-utils.mjs +1 -1
- package/dist/v2/runtime/handlers/shared/agent-utils.mjs.map +1 -1
- package/dist/v2/runtime/telemetry/telemetry-client.cjs +22 -6
- package/dist/v2/runtime/telemetry/telemetry-client.cjs.map +1 -1
- package/dist/v2/runtime/telemetry/telemetry-client.mjs +27 -11
- package/dist/v2/runtime/telemetry/telemetry-client.mjs.map +1 -1
- package/package.json +9 -19
- package/skills/runtime/SKILL.md +98 -0
- package/skills/runtime/references/agent-runners-custom.md +161 -0
- package/skills/runtime/references/agent-runners-in-memory.md +64 -0
- package/skills/runtime/references/agent-runners-sqlite.md +90 -0
- package/skills/runtime/references/agent-runners.md +304 -0
- package/skills/runtime/references/built-in-agent-factory-modes.md +232 -0
- package/skills/runtime/references/built-in-agent-helper-utilities.md +123 -0
- package/skills/runtime/references/built-in-agent-model-identifiers.md +59 -0
- package/skills/runtime/references/built-in-agent.md +523 -0
- package/skills/runtime/references/intelligence-mode.md +336 -0
- package/skills/runtime/references/middleware.md +376 -0
- package/skills/runtime/references/server-side-tools.md +414 -0
- package/skills/runtime/references/setup-endpoint.md +503 -0
- package/skills/runtime/references/transcription.md +287 -0
- package/skills/runtime/references/wiring-a2a.md +40 -0
- package/skills/runtime/references/wiring-adk.md +45 -0
- package/skills/runtime/references/wiring-ag2.md +41 -0
- package/skills/runtime/references/wiring-agno.md +39 -0
- package/skills/runtime/references/wiring-aws-strands.md +59 -0
- package/skills/runtime/references/wiring-crewai-crews.md +51 -0
- package/skills/runtime/references/wiring-crewai-flows.md +45 -0
- package/skills/runtime/references/wiring-external-agents.md +348 -0
- package/skills/runtime/references/wiring-langgraph.md +50 -0
- package/skills/runtime/references/wiring-llamaindex.md +39 -0
- package/skills/runtime/references/wiring-mastra.md +70 -0
- package/skills/runtime/references/wiring-mcp-apps-middleware.md +68 -0
- package/skills/runtime/references/wiring-ms-agent-framework.md +41 -0
- package/skills/runtime/references/wiring-pydantic-ai.md +45 -0
- package/CHANGELOG.md +0 -3624
- package/__snapshots__/schema/schema.graphql +0 -371
- package/dist/v2/runtime/telemetry/scarf-client.cjs +0 -32
- package/dist/v2/runtime/telemetry/scarf-client.cjs.map +0 -1
- package/dist/v2/runtime/telemetry/scarf-client.mjs +0 -32
- package/dist/v2/runtime/telemetry/scarf-client.mjs.map +0 -1
- package/scripts/generate-gql-schema.ts +0 -16
- package/src/agent/__tests__/agent-test-helpers.ts +0 -476
- package/src/agent/__tests__/agent.test.ts +0 -593
- package/src/agent/__tests__/ai-sdk-v6-compat.test.ts +0 -116
- package/src/agent/__tests__/basic-agent.test.ts +0 -1698
- package/src/agent/__tests__/capabilities.test.ts +0 -81
- package/src/agent/__tests__/config-tools-execution.test.ts +0 -516
- package/src/agent/__tests__/converter-aisdk.test.ts +0 -692
- package/src/agent/__tests__/converter-custom.test.ts +0 -319
- package/src/agent/__tests__/converter-tanstack-input.test.ts +0 -211
- package/src/agent/__tests__/converter-tanstack.test.ts +0 -594
- package/src/agent/__tests__/mcp-clients.test.ts +0 -246
- package/src/agent/__tests__/mcp-servers-integration.test.ts +0 -373
- package/src/agent/__tests__/multimodal-tanstack.test.ts +0 -284
- package/src/agent/__tests__/multimodal.test.ts +0 -176
- package/src/agent/__tests__/property-overrides.test.ts +0 -598
- package/src/agent/__tests__/provider-id-collision.test.ts +0 -195
- package/src/agent/__tests__/standard-schema-tools.test.ts +0 -313
- package/src/agent/__tests__/standard-schema-types.test.ts +0 -158
- package/src/agent/__tests__/state-tools.test.ts +0 -436
- package/src/agent/__tests__/test-helpers.ts +0 -197
- package/src/agent/__tests__/utils.test.ts +0 -536
- package/src/agent/__tests__/zod-regression.test.ts +0 -350
- package/src/agent/converters/aisdk.ts +0 -326
- package/src/agent/converters/index.ts +0 -7
- package/src/agent/converters/tanstack.ts +0 -451
- package/src/agent/index.ts +0 -1743
- package/src/agents/langgraph/__tests__/event-source.test.ts +0 -256
- package/src/agents/langgraph/event-source.ts +0 -365
- package/src/agents/langgraph/events.ts +0 -394
- package/src/graphql/inputs/action.input.ts +0 -16
- package/src/graphql/inputs/agent-session.input.ts +0 -13
- package/src/graphql/inputs/agent-state.input.ts +0 -13
- package/src/graphql/inputs/cloud-guardrails.input.ts +0 -16
- package/src/graphql/inputs/cloud.input.ts +0 -8
- package/src/graphql/inputs/context-property.input.ts +0 -10
- package/src/graphql/inputs/copilot-context.input.ts +0 -10
- package/src/graphql/inputs/custom-property.input.ts +0 -15
- package/src/graphql/inputs/extensions.input.ts +0 -21
- package/src/graphql/inputs/forwarded-parameters.input.ts +0 -22
- package/src/graphql/inputs/frontend.input.ts +0 -14
- package/src/graphql/inputs/generate-copilot-response.input.ts +0 -59
- package/src/graphql/inputs/load-agent-state.input.ts +0 -10
- package/src/graphql/inputs/message.input.ts +0 -110
- package/src/graphql/inputs/meta-event.input.ts +0 -18
- package/src/graphql/message-conversion/agui-to-gql.test.ts +0 -1384
- package/src/graphql/message-conversion/agui-to-gql.ts +0 -384
- package/src/graphql/message-conversion/gql-to-agui.test.ts +0 -1653
- package/src/graphql/message-conversion/gql-to-agui.ts +0 -297
- package/src/graphql/message-conversion/index.ts +0 -2
- package/src/graphql/message-conversion/roundtrip-conversion.test.ts +0 -561
- package/src/graphql/resolvers/__tests__/resolve-message-id.test.ts +0 -25
- package/src/graphql/resolvers/copilot.resolver.ts +0 -785
- package/src/graphql/resolvers/resolve-message-id.ts +0 -14
- package/src/graphql/resolvers/state.resolver.ts +0 -30
- package/src/graphql/types/agents-response.type.ts +0 -19
- package/src/graphql/types/base/index.ts +0 -10
- package/src/graphql/types/converted/index.ts +0 -183
- package/src/graphql/types/copilot-response.type.ts +0 -141
- package/src/graphql/types/enums.ts +0 -38
- package/src/graphql/types/extensions-response.type.ts +0 -23
- package/src/graphql/types/guardrails-result.type.ts +0 -20
- package/src/graphql/types/load-agent-state-response.type.ts +0 -17
- package/src/graphql/types/message-status.type.ts +0 -48
- package/src/graphql/types/meta-events.type.ts +0 -78
- package/src/graphql/types/response-status.type.ts +0 -77
- package/src/index.ts +0 -3
- package/src/langgraph.ts +0 -1
- package/src/lib/__tests__/telemetry-disclosure.test.ts +0 -55
- package/src/lib/cloud/index.ts +0 -4
- package/src/lib/error-messages.ts +0 -211
- package/src/lib/index.ts +0 -52
- package/src/lib/integrations/index.ts +0 -6
- package/src/lib/integrations/nest/index.ts +0 -21
- package/src/lib/integrations/nextjs/app-router.ts +0 -47
- package/src/lib/integrations/nextjs/pages-router.ts +0 -45
- package/src/lib/integrations/node-express/index.ts +0 -21
- package/src/lib/integrations/node-http/__tests__/request-duck-type.test.ts +0 -66
- package/src/lib/integrations/node-http/index.ts +0 -187
- package/src/lib/integrations/node-http/request-handler.ts +0 -128
- package/src/lib/integrations/shared.ts +0 -112
- package/src/lib/logger.ts +0 -31
- package/src/lib/observability.ts +0 -167
- package/src/lib/runtime/__tests__/copilot-runtime-error.test.ts +0 -183
- package/src/lib/runtime/__tests__/handle-service-adapter.test.ts +0 -108
- package/src/lib/runtime/__tests__/mcp-tools-utils.test.ts +0 -499
- package/src/lib/runtime/__tests__/on-after-request.test.ts +0 -122
- package/src/lib/runtime/__tests__/retry-utils.test.ts +0 -137
- package/src/lib/runtime/__tests__/v1-agent-factory.test.ts +0 -109
- package/src/lib/runtime/agent-integrations/langgraph/__tests__/dispatch-event-filtering.test.ts +0 -345
- package/src/lib/runtime/agent-integrations/langgraph/__tests__/run-message-filtering.test.ts +0 -156
- package/src/lib/runtime/agent-integrations/langgraph/agent.ts +0 -263
- package/src/lib/runtime/agent-integrations/langgraph/consts.ts +0 -37
- package/src/lib/runtime/agent-integrations/langgraph/index.ts +0 -2
- package/src/lib/runtime/copilot-runtime.ts +0 -863
- package/src/lib/runtime/mcp-tools-utils.ts +0 -313
- package/src/lib/runtime/retry-utils.ts +0 -141
- package/src/lib/runtime/telemetry-agent-runner.ts +0 -151
- package/src/lib/runtime/types.ts +0 -48
- package/src/lib/runtime/utils.ts +0 -93
- package/src/lib/streaming.ts +0 -220
- package/src/lib/telemetry-client.ts +0 -66
- package/src/lib/telemetry-disclosure.ts +0 -53
- package/src/service-adapters/anthropic/anthropic-adapter.ts +0 -532
- package/src/service-adapters/anthropic/utils.ts +0 -219
- package/src/service-adapters/bedrock/bedrock-adapter.ts +0 -73
- package/src/service-adapters/conversion.test.ts +0 -56
- package/src/service-adapters/conversion.ts +0 -69
- package/src/service-adapters/empty/empty-adapter.ts +0 -38
- package/src/service-adapters/events.ts +0 -337
- package/src/service-adapters/experimental/ollama/ollama-adapter.ts +0 -84
- package/src/service-adapters/google/google-genai-adapter.test.ts +0 -151
- package/src/service-adapters/google/google-genai-adapter.ts +0 -95
- package/src/service-adapters/groq/groq-adapter.ts +0 -229
- package/src/service-adapters/index.ts +0 -18
- package/src/service-adapters/langchain/langchain-adapter.ts +0 -113
- package/src/service-adapters/langchain/langserve.ts +0 -88
- package/src/service-adapters/langchain/types.ts +0 -20
- package/src/service-adapters/langchain/utils.ts +0 -330
- package/src/service-adapters/openai/__tests__/openai-v5-compat.test.ts +0 -177
- package/src/service-adapters/openai/openai-adapter.ts +0 -324
- package/src/service-adapters/openai/openai-assistant-adapter.ts +0 -385
- package/src/service-adapters/openai/utils.ts +0 -305
- package/src/service-adapters/service-adapter.ts +0 -50
- package/src/service-adapters/shared/error-utils.ts +0 -64
- package/src/service-adapters/shared/index.ts +0 -2
- package/src/service-adapters/shared/sdk-client-utils.ts +0 -19
- package/src/service-adapters/unify/unify-adapter.ts +0 -165
- package/src/utils/failed-response-status-reasons.ts +0 -70
- package/src/utils/index.ts +0 -1
- package/src/v2/express.ts +0 -1
- package/src/v2/hono.ts +0 -1
- package/src/v2/index.ts +0 -5
- package/src/v2/node.ts +0 -1
- package/src/v2/runtime/__tests__/agents-factory.test.ts +0 -136
- package/src/v2/runtime/__tests__/backward-compat.test.ts +0 -261
- package/src/v2/runtime/__tests__/code-review-fixes.test.ts +0 -500
- package/src/v2/runtime/__tests__/cors-credentials.test.ts +0 -320
- package/src/v2/runtime/__tests__/debug-sse-response.test.ts +0 -302
- package/src/v2/runtime/__tests__/express-adapter.test.ts +0 -188
- package/src/v2/runtime/__tests__/express-body-order.test.ts +0 -76
- package/src/v2/runtime/__tests__/express-fetch-bridge.test.ts +0 -344
- package/src/v2/runtime/__tests__/express-single-sse.test.ts +0 -122
- package/src/v2/runtime/__tests__/express-single-telemetry.integration.test.ts +0 -65
- package/src/v2/runtime/__tests__/express-telemetry.integration.test.ts +0 -101
- package/src/v2/runtime/__tests__/fetch-cors.test.ts +0 -205
- package/src/v2/runtime/__tests__/fetch-handler-validation.test.ts +0 -440
- package/src/v2/runtime/__tests__/fetch-handler.test.ts +0 -456
- package/src/v2/runtime/__tests__/fetch-router.test.ts +0 -276
- package/src/v2/runtime/__tests__/get-runtime-info.test.ts +0 -335
- package/src/v2/runtime/__tests__/handle-connect.test.ts +0 -585
- package/src/v2/runtime/__tests__/handle-run.test.ts +0 -1388
- package/src/v2/runtime/__tests__/handle-threads.test.ts +0 -930
- package/src/v2/runtime/__tests__/handle-transcribe.test.ts +0 -301
- package/src/v2/runtime/__tests__/header-utils.test.ts +0 -88
- package/src/v2/runtime/__tests__/hono-adapter.test.ts +0 -150
- package/src/v2/runtime/__tests__/hono-single-telemetry.integration.test.ts +0 -46
- package/src/v2/runtime/__tests__/hono-telemetry.integration.test.ts +0 -99
- package/src/v2/runtime/__tests__/hooks-edge-cases.test.ts +0 -457
- package/src/v2/runtime/__tests__/hooks.test.ts +0 -557
- package/src/v2/runtime/__tests__/in-process-agent-runner-messages.test.ts +0 -230
- package/src/v2/runtime/__tests__/in-process-agent-runner.test.ts +0 -1030
- package/src/v2/runtime/__tests__/integration/bun/bun-servers.integration.test.ts +0 -27
- package/src/v2/runtime/__tests__/integration/bun/elysia-multi.ts +0 -32
- package/src/v2/runtime/__tests__/integration/bun/elysia-single.ts +0 -33
- package/src/v2/runtime/__tests__/integration/bun/hono-bun-multi.ts +0 -25
- package/src/v2/runtime/__tests__/integration/bun/hono-bun-single.ts +0 -32
- package/src/v2/runtime/__tests__/integration/helpers/create-test-runtime.ts +0 -15
- package/src/v2/runtime/__tests__/integration/helpers/sse-reader.ts +0 -45
- package/src/v2/runtime/__tests__/integration/helpers/test-agent.ts +0 -58
- package/src/v2/runtime/__tests__/integration/node-servers.integration.test.ts +0 -58
- package/src/v2/runtime/__tests__/integration/servers/express-multi.ts +0 -35
- package/src/v2/runtime/__tests__/integration/servers/express-single.ts +0 -36
- package/src/v2/runtime/__tests__/integration/servers/fetch-direct.ts +0 -39
- package/src/v2/runtime/__tests__/integration/servers/hono-multi.ts +0 -30
- package/src/v2/runtime/__tests__/integration/servers/hono-single.ts +0 -37
- package/src/v2/runtime/__tests__/integration/servers/node-multi.ts +0 -45
- package/src/v2/runtime/__tests__/integration/servers/node-single.ts +0 -46
- package/src/v2/runtime/__tests__/integration/servers/types.ts +0 -18
- package/src/v2/runtime/__tests__/integration/suites/debug-events.suite.ts +0 -253
- package/src/v2/runtime/__tests__/integration/suites/multi-endpoint.suite.ts +0 -358
- package/src/v2/runtime/__tests__/integration/suites/single-endpoint.suite.ts +0 -363
- package/src/v2/runtime/__tests__/intelligence-run-telemetry.test.ts +0 -194
- package/src/v2/runtime/__tests__/mcp-apps-middleware-integration.test.ts +0 -275
- package/src/v2/runtime/__tests__/middleware-express.test.ts +0 -208
- package/src/v2/runtime/__tests__/middleware-single-express.test.ts +0 -213
- package/src/v2/runtime/__tests__/middleware-single.test.ts +0 -225
- package/src/v2/runtime/__tests__/middleware-sse-parser.test.ts +0 -237
- package/src/v2/runtime/__tests__/middleware.test.ts +0 -250
- package/src/v2/runtime/__tests__/node-fetch-handler.test.ts +0 -157
- package/src/v2/runtime/__tests__/open-generative-ui-middleware.e2e.test.ts +0 -728
- package/src/v2/runtime/__tests__/router-edge-cases.test.ts +0 -217
- package/src/v2/runtime/__tests__/routing-express.test.ts +0 -174
- package/src/v2/runtime/__tests__/routing-single-express.test.ts +0 -168
- package/src/v2/runtime/__tests__/routing-single.test.ts +0 -193
- package/src/v2/runtime/__tests__/routing.test.ts +0 -257
- package/src/v2/runtime/__tests__/runtime.test.ts +0 -234
- package/src/v2/runtime/__tests__/sse-response-telemetry.test.ts +0 -108
- package/src/v2/runtime/__tests__/telemetry.test.ts +0 -167
- package/src/v2/runtime/__tests__/thread-names.test.ts +0 -188
- package/src/v2/runtime/core/__tests__/debug-event-bus.test.ts +0 -156
- package/src/v2/runtime/core/debug-event-bus.ts +0 -45
- package/src/v2/runtime/core/fetch-cors.ts +0 -136
- package/src/v2/runtime/core/fetch-handler.ts +0 -492
- package/src/v2/runtime/core/fetch-router.ts +0 -203
- package/src/v2/runtime/core/hooks.ts +0 -160
- package/src/v2/runtime/core/middleware-sse-parser.ts +0 -210
- package/src/v2/runtime/core/middleware.ts +0 -115
- package/src/v2/runtime/core/runtime.ts +0 -432
- package/src/v2/runtime/endpoints/express-fetch-bridge.ts +0 -137
- package/src/v2/runtime/endpoints/express-single.ts +0 -54
- package/src/v2/runtime/endpoints/express.ts +0 -179
- package/src/v2/runtime/endpoints/hono-single.ts +0 -60
- package/src/v2/runtime/endpoints/hono.ts +0 -89
- package/src/v2/runtime/endpoints/index.ts +0 -4
- package/src/v2/runtime/endpoints/node-fetch-handler.ts +0 -48
- package/src/v2/runtime/endpoints/node.ts +0 -28
- package/src/v2/runtime/endpoints/single-route-helpers.ts +0 -125
- package/src/v2/runtime/express.ts +0 -2
- package/src/v2/runtime/handlers/__tests__/handle-debug-events.test.ts +0 -176
- package/src/v2/runtime/handlers/get-runtime-info.ts +0 -101
- package/src/v2/runtime/handlers/handle-connect.ts +0 -80
- package/src/v2/runtime/handlers/handle-debug-events.ts +0 -52
- package/src/v2/runtime/handlers/handle-run.ts +0 -111
- package/src/v2/runtime/handlers/handle-stop.ts +0 -77
- package/src/v2/runtime/handlers/handle-threads.ts +0 -11
- package/src/v2/runtime/handlers/handle-transcribe.ts +0 -269
- package/src/v2/runtime/handlers/header-utils.ts +0 -24
- package/src/v2/runtime/handlers/intelligence/connect.ts +0 -102
- package/src/v2/runtime/handlers/intelligence/run.ts +0 -351
- package/src/v2/runtime/handlers/intelligence/thread-names.ts +0 -246
- package/src/v2/runtime/handlers/intelligence/threads.ts +0 -420
- package/src/v2/runtime/handlers/shared/agent-utils.ts +0 -154
- package/src/v2/runtime/handlers/shared/intelligence-utils.ts +0 -41
- package/src/v2/runtime/handlers/shared/json-response.ts +0 -9
- package/src/v2/runtime/handlers/shared/resolve-intelligence-user.ts +0 -28
- package/src/v2/runtime/handlers/shared/sse-response.ts +0 -215
- package/src/v2/runtime/handlers/sse/__tests__/sse-connect-agent-id.test.ts +0 -71
- package/src/v2/runtime/handlers/sse/connect.ts +0 -30
- package/src/v2/runtime/handlers/sse/run.ts +0 -40
- package/src/v2/runtime/hono.ts +0 -2
- package/src/v2/runtime/index.ts +0 -51
- package/src/v2/runtime/intelligence-platform/__tests__/client.test.ts +0 -601
- package/src/v2/runtime/intelligence-platform/__tests__/intelligence-mcp-helper.test.ts +0 -246
- package/src/v2/runtime/intelligence-platform/client.ts +0 -818
- package/src/v2/runtime/intelligence-platform/index.ts +0 -10
- package/src/v2/runtime/node.ts +0 -6
- package/src/v2/runtime/open-generative-ui-middleware.ts +0 -373
- package/src/v2/runtime/runner/__tests__/finalize-events.test.ts +0 -109
- package/src/v2/runtime/runner/__tests__/in-memory-runner.e2e.test.ts +0 -775
- package/src/v2/runtime/runner/__tests__/in-memory-runner.test.ts +0 -777
- package/src/v2/runtime/runner/__tests__/intelligence-runner.test.ts +0 -1039
- package/src/v2/runtime/runner/agent-runner.ts +0 -35
- package/src/v2/runtime/runner/in-memory.ts +0 -467
- package/src/v2/runtime/runner/index.ts +0 -4
- package/src/v2/runtime/runner/intelligence.ts +0 -498
- package/src/v2/runtime/telemetry/__tests__/instance-created.test.ts +0 -96
- package/src/v2/runtime/telemetry/events.ts +0 -35
- package/src/v2/runtime/telemetry/index.ts +0 -7
- package/src/v2/runtime/telemetry/instance-created.ts +0 -44
- package/src/v2/runtime/telemetry/scarf-client.ts +0 -39
- package/src/v2/runtime/telemetry/telemetry-client.ts +0 -70
- package/src/v2/runtime/transcription-service/transcription-service.ts +0 -11
- package/tests/global.d.ts +0 -1
- package/tests/service-adapters/anthropic/allowlist-approach.test.ts +0 -259
- package/tests/service-adapters/anthropic/anthropic-adapter-language-model.test.ts +0 -101
- package/tests/service-adapters/anthropic/anthropic-adapter.test.ts +0 -645
- package/tests/service-adapters/anthropic/utils-token-trimming.test.ts +0 -301
- package/tests/service-adapters/groq/groq-adapter-language-model.test.ts +0 -102
- package/tests/service-adapters/openai/allowlist-approach.test.ts +0 -294
- package/tests/service-adapters/openai/openai-adapter-language-model.test.ts +0 -122
- package/tests/service-adapters/openai/openai-adapter.test.ts +0 -291
- package/tests/service-adapters/shared/sdk-client-utils.test.ts +0 -36
- package/tests/setup.vitest.ts +0 -8
- package/tests/tsconfig.json +0 -10
- package/tsconfig.json +0 -20
- package/tsdown.config.ts +0 -45
- package/typedoc.json +0 -4
- package/vitest.config.mjs +0 -13
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
import type OpenAI from "openai";
|
|
2
|
-
import { Message } from "../../graphql/types/converted";
|
|
3
|
-
import { ActionInput } from "../../graphql/inputs/action.input";
|
|
4
|
-
import {
|
|
5
|
-
ChatCompletionAssistantMessageParam,
|
|
6
|
-
ChatCompletionMessageParam,
|
|
7
|
-
ChatCompletionSystemMessageParam,
|
|
8
|
-
ChatCompletionTool,
|
|
9
|
-
ChatCompletionUserMessageParam,
|
|
10
|
-
ChatCompletionDeveloperMessageParam,
|
|
11
|
-
} from "openai/resources/chat";
|
|
12
|
-
import { parseJson } from "@copilotkit/shared";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* OpenAI v4 exposes streaming completions under `beta.chat.completions`.
|
|
16
|
-
* v5 removed `beta.chat` and promoted streaming to `chat.completions`.
|
|
17
|
-
* These interfaces model the v4-specific shape so we can detect and access
|
|
18
|
-
* the beta namespace safely without `as any`.
|
|
19
|
-
*/
|
|
20
|
-
interface OpenAIV4BetaChat {
|
|
21
|
-
chat: {
|
|
22
|
-
completions: OpenAI["chat"]["completions"];
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface OpenAIV4Beta extends OpenAI.Beta {
|
|
27
|
-
chat: OpenAIV4BetaChat["chat"];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Type guard: checks whether the OpenAI client has the v4-era `beta.chat`
|
|
32
|
-
* namespace. Returns `false` for v5+ clients where `beta.chat` was removed.
|
|
33
|
-
*/
|
|
34
|
-
function hasV4BetaChat(beta: OpenAI["beta"] | undefined): beta is OpenAIV4Beta {
|
|
35
|
-
return beta != null && "chat" in beta && (beta as OpenAIV4Beta).chat != null;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Detects whether the provided OpenAI client is v5+ by checking for the
|
|
40
|
-
* removal of the `beta.chat` namespace (which was promoted to `chat` in v5).
|
|
41
|
-
*/
|
|
42
|
-
export function isOpenAIV5(openai: OpenAI): boolean {
|
|
43
|
-
return !hasV4BetaChat(openai.beta);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Returns the chat completions object that supports `.stream()`.
|
|
48
|
-
* In v4 this lives under `openai.beta.chat.completions`;
|
|
49
|
-
* in v5 it was promoted to `openai.chat.completions`.
|
|
50
|
-
*/
|
|
51
|
-
export function getChatCompletionsForStreaming(
|
|
52
|
-
openai: OpenAI,
|
|
53
|
-
): OpenAI["chat"]["completions"] {
|
|
54
|
-
if (hasV4BetaChat(openai.beta)) {
|
|
55
|
-
return openai.beta.chat.completions;
|
|
56
|
-
}
|
|
57
|
-
return openai.chat.completions;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Retrieves a thread run, handling the v4→v5 API signature change.
|
|
62
|
-
* v4: retrieve(threadId, runId)
|
|
63
|
-
* v5: retrieve(runId, { thread_id: threadId })
|
|
64
|
-
*/
|
|
65
|
-
export async function retrieveThreadRun(
|
|
66
|
-
openai: OpenAI,
|
|
67
|
-
threadId: string,
|
|
68
|
-
runId: string,
|
|
69
|
-
): Promise<OpenAI.Beta.Threads.Runs.Run> {
|
|
70
|
-
if (isOpenAIV5(openai)) {
|
|
71
|
-
// v5 switched to named path params. The type definitions from whichever
|
|
72
|
-
// SDK version is installed won't match both signatures, so we call through
|
|
73
|
-
// a generic function reference. This is the one unavoidable boundary
|
|
74
|
-
// between two incompatible SDK type surfaces.
|
|
75
|
-
const retrieve = openai.beta.threads.runs.retrieve as {
|
|
76
|
-
(...args: unknown[]): Promise<OpenAI.Beta.Threads.Runs.Run>;
|
|
77
|
-
};
|
|
78
|
-
return retrieve(runId, { thread_id: threadId });
|
|
79
|
-
}
|
|
80
|
-
return openai.beta.threads.runs.retrieve(threadId, runId);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Submits tool outputs as a stream, handling the v4→v5 API signature change.
|
|
85
|
-
* v4: submitToolOutputsStream(threadId, runId, body)
|
|
86
|
-
* v5: submitToolOutputsStream(runId, { thread_id, ...body })
|
|
87
|
-
*/
|
|
88
|
-
export function submitToolOutputsStream(
|
|
89
|
-
openai: OpenAI,
|
|
90
|
-
threadId: string,
|
|
91
|
-
runId: string,
|
|
92
|
-
body: {
|
|
93
|
-
tool_outputs: Array<{ tool_call_id: string; output: string }>;
|
|
94
|
-
parallel_tool_calls?: false;
|
|
95
|
-
},
|
|
96
|
-
) {
|
|
97
|
-
if (isOpenAIV5(openai)) {
|
|
98
|
-
// Same boundary as retrieveThreadRun — v5 uses named path params.
|
|
99
|
-
const submit = openai.beta.threads.runs.submitToolOutputsStream as {
|
|
100
|
-
(
|
|
101
|
-
...args: unknown[]
|
|
102
|
-
): ReturnType<typeof openai.beta.threads.runs.submitToolOutputsStream>;
|
|
103
|
-
};
|
|
104
|
-
return submit(runId, { thread_id: threadId, ...body });
|
|
105
|
-
}
|
|
106
|
-
return openai.beta.threads.runs.submitToolOutputsStream(
|
|
107
|
-
threadId,
|
|
108
|
-
runId,
|
|
109
|
-
body,
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function limitMessagesToTokenCount(
|
|
114
|
-
messages: any[],
|
|
115
|
-
tools: any[],
|
|
116
|
-
model: string,
|
|
117
|
-
maxTokens?: number,
|
|
118
|
-
): any[] {
|
|
119
|
-
maxTokens ||= maxTokensForOpenAIModel(model);
|
|
120
|
-
|
|
121
|
-
const result: any[] = [];
|
|
122
|
-
const toolsNumTokens = countToolsTokens(model, tools);
|
|
123
|
-
if (toolsNumTokens > maxTokens) {
|
|
124
|
-
throw new Error(
|
|
125
|
-
`Too many tokens in function definitions: ${toolsNumTokens} > ${maxTokens}`,
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
maxTokens -= toolsNumTokens;
|
|
129
|
-
|
|
130
|
-
for (const message of messages) {
|
|
131
|
-
if (["system", "developer"].includes(message.role)) {
|
|
132
|
-
const numTokens = countMessageTokens(model, message);
|
|
133
|
-
maxTokens -= numTokens;
|
|
134
|
-
|
|
135
|
-
if (maxTokens < 0) {
|
|
136
|
-
throw new Error("Not enough tokens for system message.");
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
let cutoff: boolean = false;
|
|
142
|
-
|
|
143
|
-
const reversedMessages = [...messages].toReversed();
|
|
144
|
-
for (const message of reversedMessages) {
|
|
145
|
-
if (["system", "developer"].includes(message.role)) {
|
|
146
|
-
result.unshift(message);
|
|
147
|
-
continue;
|
|
148
|
-
} else if (cutoff) {
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
let numTokens = countMessageTokens(model, message);
|
|
152
|
-
if (maxTokens < numTokens) {
|
|
153
|
-
cutoff = true;
|
|
154
|
-
continue;
|
|
155
|
-
}
|
|
156
|
-
result.unshift(message);
|
|
157
|
-
maxTokens -= numTokens;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return result;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export function maxTokensForOpenAIModel(model: string): number {
|
|
164
|
-
return maxTokensByModel[model] || DEFAULT_MAX_TOKENS;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const DEFAULT_MAX_TOKENS = 128000;
|
|
168
|
-
|
|
169
|
-
const maxTokensByModel: { [key: string]: number } = {
|
|
170
|
-
// o1
|
|
171
|
-
o1: 200000,
|
|
172
|
-
"o1-2024-12-17": 200000,
|
|
173
|
-
"o1-mini": 128000,
|
|
174
|
-
"o1-mini-2024-09-12": 128000,
|
|
175
|
-
"o1-preview": 128000,
|
|
176
|
-
"o1-preview-2024-09-12": 128000,
|
|
177
|
-
// o3-mini
|
|
178
|
-
"o3-mini": 200000,
|
|
179
|
-
"o3-mini-2025-01-31": 200000,
|
|
180
|
-
// GPT-4
|
|
181
|
-
"gpt-4o": 128000,
|
|
182
|
-
"chatgpt-4o-latest": 128000,
|
|
183
|
-
"gpt-4o-2024-08-06": 128000,
|
|
184
|
-
"gpt-4o-2024-05-13": 128000,
|
|
185
|
-
"gpt-4o-mini": 128000,
|
|
186
|
-
"gpt-4o-mini-2024-07-18": 128000,
|
|
187
|
-
"gpt-4-turbo": 128000,
|
|
188
|
-
"gpt-4-turbo-2024-04-09": 128000,
|
|
189
|
-
"gpt-4-0125-preview": 128000,
|
|
190
|
-
"gpt-4-turbo-preview": 128000,
|
|
191
|
-
"gpt-4-1106-preview": 128000,
|
|
192
|
-
"gpt-4-vision-preview": 128000,
|
|
193
|
-
"gpt-4-1106-vision-preview": 128000,
|
|
194
|
-
"gpt-4-32k": 32768,
|
|
195
|
-
"gpt-4-32k-0613": 32768,
|
|
196
|
-
"gpt-4-32k-0314": 32768,
|
|
197
|
-
"gpt-4": 8192,
|
|
198
|
-
"gpt-4-0613": 8192,
|
|
199
|
-
"gpt-4-0314": 8192,
|
|
200
|
-
|
|
201
|
-
// GPT-3.5
|
|
202
|
-
"gpt-3.5-turbo-0125": 16385,
|
|
203
|
-
"gpt-3.5-turbo": 16385,
|
|
204
|
-
"gpt-3.5-turbo-1106": 16385,
|
|
205
|
-
"gpt-3.5-turbo-instruct": 4096,
|
|
206
|
-
"gpt-3.5-turbo-16k": 16385,
|
|
207
|
-
"gpt-3.5-turbo-0613": 4096,
|
|
208
|
-
"gpt-3.5-turbo-16k-0613": 16385,
|
|
209
|
-
"gpt-3.5-turbo-0301": 4097,
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
function countToolsTokens(model: string, tools: any[]): number {
|
|
213
|
-
if (tools.length === 0) {
|
|
214
|
-
return 0;
|
|
215
|
-
}
|
|
216
|
-
const json = JSON.stringify(tools);
|
|
217
|
-
return countTokens(model, json);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function countMessageTokens(model: string, message: any): number {
|
|
221
|
-
return countTokens(model, message.content || "");
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
function countTokens(model: string, text: string): number {
|
|
225
|
-
return text.length / 3;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
export function convertActionInputToOpenAITool(
|
|
229
|
-
action: ActionInput,
|
|
230
|
-
): ChatCompletionTool {
|
|
231
|
-
return {
|
|
232
|
-
type: "function",
|
|
233
|
-
function: {
|
|
234
|
-
name: action.name,
|
|
235
|
-
description: action.description,
|
|
236
|
-
parameters: parseJson(action.jsonSchema, {}),
|
|
237
|
-
},
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
type UsedMessageParams =
|
|
242
|
-
| ChatCompletionUserMessageParam
|
|
243
|
-
| ChatCompletionAssistantMessageParam
|
|
244
|
-
| ChatCompletionDeveloperMessageParam
|
|
245
|
-
| ChatCompletionSystemMessageParam;
|
|
246
|
-
export function convertMessageToOpenAIMessage(
|
|
247
|
-
message: Message,
|
|
248
|
-
options?: { keepSystemRole: boolean },
|
|
249
|
-
): ChatCompletionMessageParam {
|
|
250
|
-
const { keepSystemRole } = options || { keepSystemRole: false };
|
|
251
|
-
if (message.isTextMessage()) {
|
|
252
|
-
let role = message.role as UsedMessageParams["role"];
|
|
253
|
-
if (message.role === "system" && !keepSystemRole) {
|
|
254
|
-
role = "developer";
|
|
255
|
-
}
|
|
256
|
-
return {
|
|
257
|
-
role,
|
|
258
|
-
content: message.content,
|
|
259
|
-
} satisfies UsedMessageParams;
|
|
260
|
-
} else if (message.isImageMessage()) {
|
|
261
|
-
return {
|
|
262
|
-
role: "user",
|
|
263
|
-
content: [
|
|
264
|
-
{
|
|
265
|
-
type: "image_url",
|
|
266
|
-
image_url: {
|
|
267
|
-
url: `data:image/${message.format};base64,${message.bytes}`,
|
|
268
|
-
},
|
|
269
|
-
},
|
|
270
|
-
],
|
|
271
|
-
} satisfies UsedMessageParams;
|
|
272
|
-
} else if (message.isActionExecutionMessage()) {
|
|
273
|
-
return {
|
|
274
|
-
role: "assistant",
|
|
275
|
-
tool_calls: [
|
|
276
|
-
{
|
|
277
|
-
id: message.id,
|
|
278
|
-
type: "function",
|
|
279
|
-
function: {
|
|
280
|
-
name: message.name,
|
|
281
|
-
arguments: JSON.stringify(message.arguments),
|
|
282
|
-
},
|
|
283
|
-
},
|
|
284
|
-
],
|
|
285
|
-
};
|
|
286
|
-
} else if (message.isResultMessage()) {
|
|
287
|
-
return {
|
|
288
|
-
role: "tool",
|
|
289
|
-
content: message.result,
|
|
290
|
-
tool_call_id: message.actionExecutionId,
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
export function convertSystemMessageToAssistantAPI(
|
|
296
|
-
message: ChatCompletionMessageParam,
|
|
297
|
-
) {
|
|
298
|
-
return {
|
|
299
|
-
...message,
|
|
300
|
-
...(["system", "developer"].includes(message.role) && {
|
|
301
|
-
role: "assistant",
|
|
302
|
-
content: "THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: " + message.content,
|
|
303
|
-
}),
|
|
304
|
-
};
|
|
305
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type { LanguageModel } from "ai";
|
|
2
|
-
import { Message } from "../graphql/types/converted";
|
|
3
|
-
import { RuntimeEventSource } from "./events";
|
|
4
|
-
import { ActionInput } from "../graphql/inputs/action.input";
|
|
5
|
-
import { ForwardedParametersInput } from "../graphql/inputs/forwarded-parameters.input";
|
|
6
|
-
import { ExtensionsInput } from "../graphql/inputs/extensions.input";
|
|
7
|
-
import { ExtensionsResponse } from "../graphql/types/extensions-response.type";
|
|
8
|
-
import { AgentSessionInput } from "../graphql/inputs/agent-session.input";
|
|
9
|
-
import { AgentStateInput } from "../graphql/inputs/agent-state.input";
|
|
10
|
-
|
|
11
|
-
export interface CopilotKitResponse {
|
|
12
|
-
stream: ReadableStream;
|
|
13
|
-
headers?: Record<string, string>;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface CopilotRuntimeChatCompletionRequest {
|
|
17
|
-
eventSource: RuntimeEventSource;
|
|
18
|
-
messages: Message[];
|
|
19
|
-
actions: ActionInput[];
|
|
20
|
-
model?: string;
|
|
21
|
-
threadId?: string;
|
|
22
|
-
runId?: string;
|
|
23
|
-
forwardedParameters?: ForwardedParametersInput;
|
|
24
|
-
extensions?: ExtensionsInput;
|
|
25
|
-
agentSession?: AgentSessionInput;
|
|
26
|
-
agentStates?: AgentStateInput[];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface CopilotRuntimeChatCompletionResponse {
|
|
30
|
-
threadId: string;
|
|
31
|
-
runId?: string;
|
|
32
|
-
extensions?: ExtensionsResponse;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface CopilotServiceAdapter {
|
|
36
|
-
provider?: string;
|
|
37
|
-
model?: string;
|
|
38
|
-
process(
|
|
39
|
-
request: CopilotRuntimeChatCompletionRequest,
|
|
40
|
-
): Promise<CopilotRuntimeChatCompletionResponse>;
|
|
41
|
-
name?: string;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Returns a pre-configured LanguageModel for use with BuiltInAgent.
|
|
45
|
-
* Adapters that support custom provider configurations (e.g., Azure OpenAI
|
|
46
|
-
* with custom baseURL/apiKey) should implement this to ensure the
|
|
47
|
-
* configuration is propagated to the agent layer.
|
|
48
|
-
*/
|
|
49
|
-
getLanguageModel?(): LanguageModel;
|
|
50
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CopilotKitLowLevelError,
|
|
3
|
-
CopilotKitErrorCode,
|
|
4
|
-
} from "@copilotkit/shared";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Converts service adapter errors to structured CopilotKitError format using HTTP status codes.
|
|
8
|
-
* This provides consistent error classification across all service adapters.
|
|
9
|
-
*/
|
|
10
|
-
export function convertServiceAdapterError(
|
|
11
|
-
error: any,
|
|
12
|
-
adapterName: string,
|
|
13
|
-
): CopilotKitLowLevelError {
|
|
14
|
-
const errorName = error?.constructor?.name || error.name;
|
|
15
|
-
const errorMessage = error?.message || String(error);
|
|
16
|
-
const statusCode = error.status || error.statusCode || error.response?.status;
|
|
17
|
-
const responseData = error.error || error.response?.data || error.data;
|
|
18
|
-
|
|
19
|
-
// Create the base error with the constructor signature
|
|
20
|
-
const structuredError = new CopilotKitLowLevelError({
|
|
21
|
-
error: error instanceof Error ? error : new Error(errorMessage),
|
|
22
|
-
url: `${adapterName} service adapter`,
|
|
23
|
-
message: `${adapterName} API error: ${errorMessage}`,
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
// Add additional properties after construction
|
|
27
|
-
if (statusCode) {
|
|
28
|
-
(structuredError as any).statusCode = statusCode;
|
|
29
|
-
}
|
|
30
|
-
if (responseData) {
|
|
31
|
-
(structuredError as any).responseData = responseData;
|
|
32
|
-
}
|
|
33
|
-
if (errorName) {
|
|
34
|
-
(structuredError as any).originalErrorType = errorName;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Classify error based on HTTP status codes (reliable and provider-agnostic)
|
|
38
|
-
let newCode: CopilotKitErrorCode;
|
|
39
|
-
|
|
40
|
-
if (statusCode === 401) {
|
|
41
|
-
// 401 = Authentication/API key issues
|
|
42
|
-
newCode = CopilotKitErrorCode.AUTHENTICATION_ERROR;
|
|
43
|
-
} else if (statusCode >= 400 && statusCode < 500) {
|
|
44
|
-
// 4xx = Client errors (bad request, invalid params, etc.) - these are configuration issues
|
|
45
|
-
newCode = CopilotKitErrorCode.CONFIGURATION_ERROR;
|
|
46
|
-
} else if (statusCode >= 500) {
|
|
47
|
-
// 5xx = Server errors - keep as NETWORK_ERROR since it's infrastructure related
|
|
48
|
-
newCode = CopilotKitErrorCode.NETWORK_ERROR;
|
|
49
|
-
} else if (statusCode) {
|
|
50
|
-
// Any other HTTP status with an error - likely configuration
|
|
51
|
-
newCode = CopilotKitErrorCode.CONFIGURATION_ERROR;
|
|
52
|
-
} else {
|
|
53
|
-
// No status code - likely a genuine network/connection error
|
|
54
|
-
newCode = CopilotKitErrorCode.NETWORK_ERROR;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Update both the instance property and the extensions
|
|
58
|
-
(structuredError as any).code = newCode;
|
|
59
|
-
if ((structuredError as any).extensions) {
|
|
60
|
-
(structuredError as any).extensions.code = newCode;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return structuredError;
|
|
64
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SDK clients (OpenAI, Anthropic, Groq) store constructor options like
|
|
3
|
-
* `defaultHeaders` and `fetch` in a private/protected `_options` field
|
|
4
|
-
* with no public accessor. This extracts them with a narrow type assertion.
|
|
5
|
-
*/
|
|
6
|
-
export function getSdkClientOptions(client: object): {
|
|
7
|
-
defaultHeaders?: Record<string, string>;
|
|
8
|
-
fetch?: typeof globalThis.fetch;
|
|
9
|
-
} {
|
|
10
|
-
const rec = client as Record<string, unknown>;
|
|
11
|
-
const options = rec._options;
|
|
12
|
-
if (options != null && typeof options === "object") {
|
|
13
|
-
return options as {
|
|
14
|
-
defaultHeaders?: Record<string, string>;
|
|
15
|
-
fetch?: typeof globalThis.fetch;
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
return {};
|
|
19
|
-
}
|
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CopilotKit Adapter for Unify
|
|
3
|
-
*
|
|
4
|
-
* <RequestExample>
|
|
5
|
-
* ```jsx CopilotRuntime Example
|
|
6
|
-
* const copilotKit = new CopilotRuntime();
|
|
7
|
-
* return copilotKit.response(req, new UnifyAdapter());
|
|
8
|
-
* ```
|
|
9
|
-
* </RequestExample>
|
|
10
|
-
*
|
|
11
|
-
* You can easily set the model to use by passing it to the constructor.
|
|
12
|
-
* ```jsx
|
|
13
|
-
* const copilotKit = new CopilotRuntime();
|
|
14
|
-
* return copilotKit.response(
|
|
15
|
-
* req,
|
|
16
|
-
* new UnifyAdapter({ model: "llama-3-8b-chat@fireworks-ai" }),
|
|
17
|
-
* );
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
import {
|
|
21
|
-
CopilotRuntimeChatCompletionRequest,
|
|
22
|
-
CopilotRuntimeChatCompletionResponse,
|
|
23
|
-
CopilotServiceAdapter,
|
|
24
|
-
} from "../service-adapter";
|
|
25
|
-
import { randomId, randomUUID } from "@copilotkit/shared";
|
|
26
|
-
import {
|
|
27
|
-
convertActionInputToOpenAITool,
|
|
28
|
-
convertMessageToOpenAIMessage,
|
|
29
|
-
} from "../openai/utils";
|
|
30
|
-
|
|
31
|
-
export interface UnifyAdapterParams {
|
|
32
|
-
apiKey?: string;
|
|
33
|
-
model: string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export class UnifyAdapter implements CopilotServiceAdapter {
|
|
37
|
-
private apiKey: string;
|
|
38
|
-
public model: string;
|
|
39
|
-
private start: boolean;
|
|
40
|
-
public provider = "unify";
|
|
41
|
-
|
|
42
|
-
public get name() {
|
|
43
|
-
return "UnifyAdapter";
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
constructor(options?: UnifyAdapterParams) {
|
|
47
|
-
if (options?.apiKey) {
|
|
48
|
-
this.apiKey = options.apiKey;
|
|
49
|
-
} else {
|
|
50
|
-
this.apiKey = "UNIFY_API_KEY";
|
|
51
|
-
}
|
|
52
|
-
this.model = options?.model;
|
|
53
|
-
this.start = true;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async process(
|
|
57
|
-
request: CopilotRuntimeChatCompletionRequest,
|
|
58
|
-
): Promise<CopilotRuntimeChatCompletionResponse> {
|
|
59
|
-
const tools = request.actions.map(convertActionInputToOpenAITool);
|
|
60
|
-
// Lazy require for optional peer dependency
|
|
61
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
62
|
-
const OpenAI = require("openai").default;
|
|
63
|
-
const openai = new OpenAI({
|
|
64
|
-
apiKey: this.apiKey,
|
|
65
|
-
baseURL: "https://api.unify.ai/v0/",
|
|
66
|
-
});
|
|
67
|
-
const forwardedParameters = request.forwardedParameters;
|
|
68
|
-
|
|
69
|
-
const messages = request.messages.map((m) =>
|
|
70
|
-
convertMessageToOpenAIMessage(m),
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
const stream = await openai.chat.completions.create({
|
|
74
|
-
model: this.model,
|
|
75
|
-
messages: messages,
|
|
76
|
-
stream: true,
|
|
77
|
-
...(tools.length > 0 && { tools }),
|
|
78
|
-
...(forwardedParameters?.temperature && {
|
|
79
|
-
temperature: forwardedParameters.temperature,
|
|
80
|
-
}),
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
let model = null;
|
|
84
|
-
let currentMessageId: string;
|
|
85
|
-
let currentToolCallId: string;
|
|
86
|
-
request.eventSource.stream(async (eventStream$) => {
|
|
87
|
-
let mode: "function" | "message" | null = null;
|
|
88
|
-
for await (const chunk of stream) {
|
|
89
|
-
if (this.start) {
|
|
90
|
-
model = chunk.model;
|
|
91
|
-
currentMessageId = randomId();
|
|
92
|
-
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
93
|
-
eventStream$.sendTextMessageContent({
|
|
94
|
-
messageId: currentMessageId,
|
|
95
|
-
content: `Model used: ${model}\n`,
|
|
96
|
-
});
|
|
97
|
-
eventStream$.sendTextMessageEnd({ messageId: currentMessageId });
|
|
98
|
-
this.start = false;
|
|
99
|
-
}
|
|
100
|
-
const toolCall = chunk.choices[0].delta.tool_calls?.[0];
|
|
101
|
-
const content = chunk.choices[0].delta.content;
|
|
102
|
-
|
|
103
|
-
// When switching from message to function or vice versa,
|
|
104
|
-
// send the respective end event.
|
|
105
|
-
// If toolCall?.id is defined, it means a new tool call starts.
|
|
106
|
-
if (mode === "message" && toolCall?.id) {
|
|
107
|
-
mode = null;
|
|
108
|
-
eventStream$.sendTextMessageEnd({ messageId: currentMessageId });
|
|
109
|
-
} else if (
|
|
110
|
-
mode === "function" &&
|
|
111
|
-
(toolCall === undefined || toolCall?.id)
|
|
112
|
-
) {
|
|
113
|
-
mode = null;
|
|
114
|
-
eventStream$.sendActionExecutionEnd({
|
|
115
|
-
actionExecutionId: currentToolCallId,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// If we send a new message type, send the appropriate start event.
|
|
120
|
-
if (mode === null) {
|
|
121
|
-
if (toolCall?.id) {
|
|
122
|
-
mode = "function";
|
|
123
|
-
currentToolCallId = toolCall!.id;
|
|
124
|
-
eventStream$.sendActionExecutionStart({
|
|
125
|
-
actionExecutionId: currentToolCallId,
|
|
126
|
-
actionName: toolCall!.function!.name,
|
|
127
|
-
});
|
|
128
|
-
} else if (content) {
|
|
129
|
-
mode = "message";
|
|
130
|
-
currentMessageId = chunk.id;
|
|
131
|
-
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// send the content events
|
|
136
|
-
if (mode === "message" && content) {
|
|
137
|
-
eventStream$.sendTextMessageContent({
|
|
138
|
-
messageId: currentMessageId,
|
|
139
|
-
content: content,
|
|
140
|
-
});
|
|
141
|
-
} else if (mode === "function" && toolCall?.function?.arguments) {
|
|
142
|
-
eventStream$.sendActionExecutionArgs({
|
|
143
|
-
actionExecutionId: currentToolCallId,
|
|
144
|
-
args: toolCall.function.arguments,
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// send the end events
|
|
150
|
-
if (mode === "message") {
|
|
151
|
-
eventStream$.sendTextMessageEnd({ messageId: currentMessageId });
|
|
152
|
-
} else if (mode === "function") {
|
|
153
|
-
eventStream$.sendActionExecutionEnd({
|
|
154
|
-
actionExecutionId: currentToolCallId,
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
eventStream$.complete();
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
return {
|
|
162
|
-
threadId: request.threadId || randomUUID(),
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
FailedResponseStatus,
|
|
3
|
-
FailedResponseStatusReason,
|
|
4
|
-
} from "../graphql/types/response-status.type";
|
|
5
|
-
|
|
6
|
-
export class GuardrailsValidationFailureResponse extends FailedResponseStatus {
|
|
7
|
-
reason = FailedResponseStatusReason.GUARDRAILS_VALIDATION_FAILED;
|
|
8
|
-
declare details: {
|
|
9
|
-
guardrailsReason: string;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
constructor({ guardrailsReason }) {
|
|
13
|
-
super();
|
|
14
|
-
this.details = {
|
|
15
|
-
guardrailsReason,
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class MessageStreamInterruptedResponse extends FailedResponseStatus {
|
|
21
|
-
reason = FailedResponseStatusReason.MESSAGE_STREAM_INTERRUPTED;
|
|
22
|
-
declare details: {
|
|
23
|
-
messageId: string;
|
|
24
|
-
description: string;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
constructor({ messageId }: { messageId: string }) {
|
|
28
|
-
super();
|
|
29
|
-
this.details = {
|
|
30
|
-
messageId,
|
|
31
|
-
description: "Check the message for mode details",
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export class UnknownErrorResponse extends FailedResponseStatus {
|
|
37
|
-
reason = FailedResponseStatusReason.UNKNOWN_ERROR;
|
|
38
|
-
declare details: {
|
|
39
|
-
description?: string;
|
|
40
|
-
originalError?: {
|
|
41
|
-
code?: string;
|
|
42
|
-
statusCode?: number;
|
|
43
|
-
severity?: string;
|
|
44
|
-
visibility?: string;
|
|
45
|
-
originalErrorType?: string;
|
|
46
|
-
extensions?: any;
|
|
47
|
-
};
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
constructor({
|
|
51
|
-
description,
|
|
52
|
-
originalError,
|
|
53
|
-
}: {
|
|
54
|
-
description?: string;
|
|
55
|
-
originalError?: {
|
|
56
|
-
code?: string;
|
|
57
|
-
statusCode?: number;
|
|
58
|
-
severity?: string;
|
|
59
|
-
visibility?: string;
|
|
60
|
-
originalErrorType?: string;
|
|
61
|
-
extensions?: any;
|
|
62
|
-
};
|
|
63
|
-
}) {
|
|
64
|
-
super();
|
|
65
|
-
this.details = {
|
|
66
|
-
description,
|
|
67
|
-
originalError,
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
}
|
package/src/utils/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./failed-response-status-reasons";
|
package/src/v2/express.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./runtime/express";
|
package/src/v2/hono.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./runtime/hono";
|
package/src/v2/index.ts
DELETED
package/src/v2/node.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./runtime/node";
|