@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.
Files changed (346) hide show
  1. package/dist/lib/observability.d.cts +1 -1
  2. package/dist/lib/observability.d.cts.map +1 -1
  3. package/dist/lib/observability.d.mts +1 -1
  4. package/dist/lib/observability.d.mts.map +1 -1
  5. package/dist/lib/runtime/copilot-runtime.cjs +2 -0
  6. package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
  7. package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
  8. package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
  9. package/dist/lib/runtime/copilot-runtime.mjs +2 -0
  10. package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
  11. package/dist/package.cjs +5 -8
  12. package/dist/package.mjs +5 -8
  13. package/dist/v2/runtime/core/runtime.cjs +4 -1
  14. package/dist/v2/runtime/core/runtime.cjs.map +1 -1
  15. package/dist/v2/runtime/core/runtime.d.cts.map +1 -1
  16. package/dist/v2/runtime/core/runtime.d.mts.map +1 -1
  17. package/dist/v2/runtime/core/runtime.mjs +4 -1
  18. package/dist/v2/runtime/core/runtime.mjs.map +1 -1
  19. package/dist/v2/runtime/handlers/get-runtime-info.cjs +1 -1
  20. package/dist/v2/runtime/handlers/get-runtime-info.mjs +1 -1
  21. package/dist/v2/runtime/handlers/handle-connect.cjs +1 -1
  22. package/dist/v2/runtime/handlers/handle-connect.mjs +1 -1
  23. package/dist/v2/runtime/handlers/handle-run.cjs +1 -1
  24. package/dist/v2/runtime/handlers/handle-run.mjs +1 -1
  25. package/dist/v2/runtime/handlers/shared/agent-utils.cjs +1 -1
  26. package/dist/v2/runtime/handlers/shared/agent-utils.cjs.map +1 -1
  27. package/dist/v2/runtime/handlers/shared/agent-utils.mjs +1 -1
  28. package/dist/v2/runtime/handlers/shared/agent-utils.mjs.map +1 -1
  29. package/dist/v2/runtime/telemetry/telemetry-client.cjs +22 -6
  30. package/dist/v2/runtime/telemetry/telemetry-client.cjs.map +1 -1
  31. package/dist/v2/runtime/telemetry/telemetry-client.mjs +27 -11
  32. package/dist/v2/runtime/telemetry/telemetry-client.mjs.map +1 -1
  33. package/package.json +9 -19
  34. package/skills/runtime/SKILL.md +98 -0
  35. package/skills/runtime/references/agent-runners-custom.md +161 -0
  36. package/skills/runtime/references/agent-runners-in-memory.md +64 -0
  37. package/skills/runtime/references/agent-runners-sqlite.md +90 -0
  38. package/skills/runtime/references/agent-runners.md +304 -0
  39. package/skills/runtime/references/built-in-agent-factory-modes.md +232 -0
  40. package/skills/runtime/references/built-in-agent-helper-utilities.md +123 -0
  41. package/skills/runtime/references/built-in-agent-model-identifiers.md +59 -0
  42. package/skills/runtime/references/built-in-agent.md +523 -0
  43. package/skills/runtime/references/intelligence-mode.md +336 -0
  44. package/skills/runtime/references/middleware.md +376 -0
  45. package/skills/runtime/references/server-side-tools.md +414 -0
  46. package/skills/runtime/references/setup-endpoint.md +503 -0
  47. package/skills/runtime/references/transcription.md +287 -0
  48. package/skills/runtime/references/wiring-a2a.md +40 -0
  49. package/skills/runtime/references/wiring-adk.md +45 -0
  50. package/skills/runtime/references/wiring-ag2.md +41 -0
  51. package/skills/runtime/references/wiring-agno.md +39 -0
  52. package/skills/runtime/references/wiring-aws-strands.md +59 -0
  53. package/skills/runtime/references/wiring-crewai-crews.md +51 -0
  54. package/skills/runtime/references/wiring-crewai-flows.md +45 -0
  55. package/skills/runtime/references/wiring-external-agents.md +348 -0
  56. package/skills/runtime/references/wiring-langgraph.md +50 -0
  57. package/skills/runtime/references/wiring-llamaindex.md +39 -0
  58. package/skills/runtime/references/wiring-mastra.md +70 -0
  59. package/skills/runtime/references/wiring-mcp-apps-middleware.md +68 -0
  60. package/skills/runtime/references/wiring-ms-agent-framework.md +41 -0
  61. package/skills/runtime/references/wiring-pydantic-ai.md +45 -0
  62. package/CHANGELOG.md +0 -3624
  63. package/__snapshots__/schema/schema.graphql +0 -371
  64. package/dist/v2/runtime/telemetry/scarf-client.cjs +0 -32
  65. package/dist/v2/runtime/telemetry/scarf-client.cjs.map +0 -1
  66. package/dist/v2/runtime/telemetry/scarf-client.mjs +0 -32
  67. package/dist/v2/runtime/telemetry/scarf-client.mjs.map +0 -1
  68. package/scripts/generate-gql-schema.ts +0 -16
  69. package/src/agent/__tests__/agent-test-helpers.ts +0 -476
  70. package/src/agent/__tests__/agent.test.ts +0 -593
  71. package/src/agent/__tests__/ai-sdk-v6-compat.test.ts +0 -116
  72. package/src/agent/__tests__/basic-agent.test.ts +0 -1698
  73. package/src/agent/__tests__/capabilities.test.ts +0 -81
  74. package/src/agent/__tests__/config-tools-execution.test.ts +0 -516
  75. package/src/agent/__tests__/converter-aisdk.test.ts +0 -692
  76. package/src/agent/__tests__/converter-custom.test.ts +0 -319
  77. package/src/agent/__tests__/converter-tanstack-input.test.ts +0 -211
  78. package/src/agent/__tests__/converter-tanstack.test.ts +0 -594
  79. package/src/agent/__tests__/mcp-clients.test.ts +0 -246
  80. package/src/agent/__tests__/mcp-servers-integration.test.ts +0 -373
  81. package/src/agent/__tests__/multimodal-tanstack.test.ts +0 -284
  82. package/src/agent/__tests__/multimodal.test.ts +0 -176
  83. package/src/agent/__tests__/property-overrides.test.ts +0 -598
  84. package/src/agent/__tests__/provider-id-collision.test.ts +0 -195
  85. package/src/agent/__tests__/standard-schema-tools.test.ts +0 -313
  86. package/src/agent/__tests__/standard-schema-types.test.ts +0 -158
  87. package/src/agent/__tests__/state-tools.test.ts +0 -436
  88. package/src/agent/__tests__/test-helpers.ts +0 -197
  89. package/src/agent/__tests__/utils.test.ts +0 -536
  90. package/src/agent/__tests__/zod-regression.test.ts +0 -350
  91. package/src/agent/converters/aisdk.ts +0 -326
  92. package/src/agent/converters/index.ts +0 -7
  93. package/src/agent/converters/tanstack.ts +0 -451
  94. package/src/agent/index.ts +0 -1743
  95. package/src/agents/langgraph/__tests__/event-source.test.ts +0 -256
  96. package/src/agents/langgraph/event-source.ts +0 -365
  97. package/src/agents/langgraph/events.ts +0 -394
  98. package/src/graphql/inputs/action.input.ts +0 -16
  99. package/src/graphql/inputs/agent-session.input.ts +0 -13
  100. package/src/graphql/inputs/agent-state.input.ts +0 -13
  101. package/src/graphql/inputs/cloud-guardrails.input.ts +0 -16
  102. package/src/graphql/inputs/cloud.input.ts +0 -8
  103. package/src/graphql/inputs/context-property.input.ts +0 -10
  104. package/src/graphql/inputs/copilot-context.input.ts +0 -10
  105. package/src/graphql/inputs/custom-property.input.ts +0 -15
  106. package/src/graphql/inputs/extensions.input.ts +0 -21
  107. package/src/graphql/inputs/forwarded-parameters.input.ts +0 -22
  108. package/src/graphql/inputs/frontend.input.ts +0 -14
  109. package/src/graphql/inputs/generate-copilot-response.input.ts +0 -59
  110. package/src/graphql/inputs/load-agent-state.input.ts +0 -10
  111. package/src/graphql/inputs/message.input.ts +0 -110
  112. package/src/graphql/inputs/meta-event.input.ts +0 -18
  113. package/src/graphql/message-conversion/agui-to-gql.test.ts +0 -1384
  114. package/src/graphql/message-conversion/agui-to-gql.ts +0 -384
  115. package/src/graphql/message-conversion/gql-to-agui.test.ts +0 -1653
  116. package/src/graphql/message-conversion/gql-to-agui.ts +0 -297
  117. package/src/graphql/message-conversion/index.ts +0 -2
  118. package/src/graphql/message-conversion/roundtrip-conversion.test.ts +0 -561
  119. package/src/graphql/resolvers/__tests__/resolve-message-id.test.ts +0 -25
  120. package/src/graphql/resolvers/copilot.resolver.ts +0 -785
  121. package/src/graphql/resolvers/resolve-message-id.ts +0 -14
  122. package/src/graphql/resolvers/state.resolver.ts +0 -30
  123. package/src/graphql/types/agents-response.type.ts +0 -19
  124. package/src/graphql/types/base/index.ts +0 -10
  125. package/src/graphql/types/converted/index.ts +0 -183
  126. package/src/graphql/types/copilot-response.type.ts +0 -141
  127. package/src/graphql/types/enums.ts +0 -38
  128. package/src/graphql/types/extensions-response.type.ts +0 -23
  129. package/src/graphql/types/guardrails-result.type.ts +0 -20
  130. package/src/graphql/types/load-agent-state-response.type.ts +0 -17
  131. package/src/graphql/types/message-status.type.ts +0 -48
  132. package/src/graphql/types/meta-events.type.ts +0 -78
  133. package/src/graphql/types/response-status.type.ts +0 -77
  134. package/src/index.ts +0 -3
  135. package/src/langgraph.ts +0 -1
  136. package/src/lib/__tests__/telemetry-disclosure.test.ts +0 -55
  137. package/src/lib/cloud/index.ts +0 -4
  138. package/src/lib/error-messages.ts +0 -211
  139. package/src/lib/index.ts +0 -52
  140. package/src/lib/integrations/index.ts +0 -6
  141. package/src/lib/integrations/nest/index.ts +0 -21
  142. package/src/lib/integrations/nextjs/app-router.ts +0 -47
  143. package/src/lib/integrations/nextjs/pages-router.ts +0 -45
  144. package/src/lib/integrations/node-express/index.ts +0 -21
  145. package/src/lib/integrations/node-http/__tests__/request-duck-type.test.ts +0 -66
  146. package/src/lib/integrations/node-http/index.ts +0 -187
  147. package/src/lib/integrations/node-http/request-handler.ts +0 -128
  148. package/src/lib/integrations/shared.ts +0 -112
  149. package/src/lib/logger.ts +0 -31
  150. package/src/lib/observability.ts +0 -167
  151. package/src/lib/runtime/__tests__/copilot-runtime-error.test.ts +0 -183
  152. package/src/lib/runtime/__tests__/handle-service-adapter.test.ts +0 -108
  153. package/src/lib/runtime/__tests__/mcp-tools-utils.test.ts +0 -499
  154. package/src/lib/runtime/__tests__/on-after-request.test.ts +0 -122
  155. package/src/lib/runtime/__tests__/retry-utils.test.ts +0 -137
  156. package/src/lib/runtime/__tests__/v1-agent-factory.test.ts +0 -109
  157. package/src/lib/runtime/agent-integrations/langgraph/__tests__/dispatch-event-filtering.test.ts +0 -345
  158. package/src/lib/runtime/agent-integrations/langgraph/__tests__/run-message-filtering.test.ts +0 -156
  159. package/src/lib/runtime/agent-integrations/langgraph/agent.ts +0 -263
  160. package/src/lib/runtime/agent-integrations/langgraph/consts.ts +0 -37
  161. package/src/lib/runtime/agent-integrations/langgraph/index.ts +0 -2
  162. package/src/lib/runtime/copilot-runtime.ts +0 -863
  163. package/src/lib/runtime/mcp-tools-utils.ts +0 -313
  164. package/src/lib/runtime/retry-utils.ts +0 -141
  165. package/src/lib/runtime/telemetry-agent-runner.ts +0 -151
  166. package/src/lib/runtime/types.ts +0 -48
  167. package/src/lib/runtime/utils.ts +0 -93
  168. package/src/lib/streaming.ts +0 -220
  169. package/src/lib/telemetry-client.ts +0 -66
  170. package/src/lib/telemetry-disclosure.ts +0 -53
  171. package/src/service-adapters/anthropic/anthropic-adapter.ts +0 -532
  172. package/src/service-adapters/anthropic/utils.ts +0 -219
  173. package/src/service-adapters/bedrock/bedrock-adapter.ts +0 -73
  174. package/src/service-adapters/conversion.test.ts +0 -56
  175. package/src/service-adapters/conversion.ts +0 -69
  176. package/src/service-adapters/empty/empty-adapter.ts +0 -38
  177. package/src/service-adapters/events.ts +0 -337
  178. package/src/service-adapters/experimental/ollama/ollama-adapter.ts +0 -84
  179. package/src/service-adapters/google/google-genai-adapter.test.ts +0 -151
  180. package/src/service-adapters/google/google-genai-adapter.ts +0 -95
  181. package/src/service-adapters/groq/groq-adapter.ts +0 -229
  182. package/src/service-adapters/index.ts +0 -18
  183. package/src/service-adapters/langchain/langchain-adapter.ts +0 -113
  184. package/src/service-adapters/langchain/langserve.ts +0 -88
  185. package/src/service-adapters/langchain/types.ts +0 -20
  186. package/src/service-adapters/langchain/utils.ts +0 -330
  187. package/src/service-adapters/openai/__tests__/openai-v5-compat.test.ts +0 -177
  188. package/src/service-adapters/openai/openai-adapter.ts +0 -324
  189. package/src/service-adapters/openai/openai-assistant-adapter.ts +0 -385
  190. package/src/service-adapters/openai/utils.ts +0 -305
  191. package/src/service-adapters/service-adapter.ts +0 -50
  192. package/src/service-adapters/shared/error-utils.ts +0 -64
  193. package/src/service-adapters/shared/index.ts +0 -2
  194. package/src/service-adapters/shared/sdk-client-utils.ts +0 -19
  195. package/src/service-adapters/unify/unify-adapter.ts +0 -165
  196. package/src/utils/failed-response-status-reasons.ts +0 -70
  197. package/src/utils/index.ts +0 -1
  198. package/src/v2/express.ts +0 -1
  199. package/src/v2/hono.ts +0 -1
  200. package/src/v2/index.ts +0 -5
  201. package/src/v2/node.ts +0 -1
  202. package/src/v2/runtime/__tests__/agents-factory.test.ts +0 -136
  203. package/src/v2/runtime/__tests__/backward-compat.test.ts +0 -261
  204. package/src/v2/runtime/__tests__/code-review-fixes.test.ts +0 -500
  205. package/src/v2/runtime/__tests__/cors-credentials.test.ts +0 -320
  206. package/src/v2/runtime/__tests__/debug-sse-response.test.ts +0 -302
  207. package/src/v2/runtime/__tests__/express-adapter.test.ts +0 -188
  208. package/src/v2/runtime/__tests__/express-body-order.test.ts +0 -76
  209. package/src/v2/runtime/__tests__/express-fetch-bridge.test.ts +0 -344
  210. package/src/v2/runtime/__tests__/express-single-sse.test.ts +0 -122
  211. package/src/v2/runtime/__tests__/express-single-telemetry.integration.test.ts +0 -65
  212. package/src/v2/runtime/__tests__/express-telemetry.integration.test.ts +0 -101
  213. package/src/v2/runtime/__tests__/fetch-cors.test.ts +0 -205
  214. package/src/v2/runtime/__tests__/fetch-handler-validation.test.ts +0 -440
  215. package/src/v2/runtime/__tests__/fetch-handler.test.ts +0 -456
  216. package/src/v2/runtime/__tests__/fetch-router.test.ts +0 -276
  217. package/src/v2/runtime/__tests__/get-runtime-info.test.ts +0 -335
  218. package/src/v2/runtime/__tests__/handle-connect.test.ts +0 -585
  219. package/src/v2/runtime/__tests__/handle-run.test.ts +0 -1388
  220. package/src/v2/runtime/__tests__/handle-threads.test.ts +0 -930
  221. package/src/v2/runtime/__tests__/handle-transcribe.test.ts +0 -301
  222. package/src/v2/runtime/__tests__/header-utils.test.ts +0 -88
  223. package/src/v2/runtime/__tests__/hono-adapter.test.ts +0 -150
  224. package/src/v2/runtime/__tests__/hono-single-telemetry.integration.test.ts +0 -46
  225. package/src/v2/runtime/__tests__/hono-telemetry.integration.test.ts +0 -99
  226. package/src/v2/runtime/__tests__/hooks-edge-cases.test.ts +0 -457
  227. package/src/v2/runtime/__tests__/hooks.test.ts +0 -557
  228. package/src/v2/runtime/__tests__/in-process-agent-runner-messages.test.ts +0 -230
  229. package/src/v2/runtime/__tests__/in-process-agent-runner.test.ts +0 -1030
  230. package/src/v2/runtime/__tests__/integration/bun/bun-servers.integration.test.ts +0 -27
  231. package/src/v2/runtime/__tests__/integration/bun/elysia-multi.ts +0 -32
  232. package/src/v2/runtime/__tests__/integration/bun/elysia-single.ts +0 -33
  233. package/src/v2/runtime/__tests__/integration/bun/hono-bun-multi.ts +0 -25
  234. package/src/v2/runtime/__tests__/integration/bun/hono-bun-single.ts +0 -32
  235. package/src/v2/runtime/__tests__/integration/helpers/create-test-runtime.ts +0 -15
  236. package/src/v2/runtime/__tests__/integration/helpers/sse-reader.ts +0 -45
  237. package/src/v2/runtime/__tests__/integration/helpers/test-agent.ts +0 -58
  238. package/src/v2/runtime/__tests__/integration/node-servers.integration.test.ts +0 -58
  239. package/src/v2/runtime/__tests__/integration/servers/express-multi.ts +0 -35
  240. package/src/v2/runtime/__tests__/integration/servers/express-single.ts +0 -36
  241. package/src/v2/runtime/__tests__/integration/servers/fetch-direct.ts +0 -39
  242. package/src/v2/runtime/__tests__/integration/servers/hono-multi.ts +0 -30
  243. package/src/v2/runtime/__tests__/integration/servers/hono-single.ts +0 -37
  244. package/src/v2/runtime/__tests__/integration/servers/node-multi.ts +0 -45
  245. package/src/v2/runtime/__tests__/integration/servers/node-single.ts +0 -46
  246. package/src/v2/runtime/__tests__/integration/servers/types.ts +0 -18
  247. package/src/v2/runtime/__tests__/integration/suites/debug-events.suite.ts +0 -253
  248. package/src/v2/runtime/__tests__/integration/suites/multi-endpoint.suite.ts +0 -358
  249. package/src/v2/runtime/__tests__/integration/suites/single-endpoint.suite.ts +0 -363
  250. package/src/v2/runtime/__tests__/intelligence-run-telemetry.test.ts +0 -194
  251. package/src/v2/runtime/__tests__/mcp-apps-middleware-integration.test.ts +0 -275
  252. package/src/v2/runtime/__tests__/middleware-express.test.ts +0 -208
  253. package/src/v2/runtime/__tests__/middleware-single-express.test.ts +0 -213
  254. package/src/v2/runtime/__tests__/middleware-single.test.ts +0 -225
  255. package/src/v2/runtime/__tests__/middleware-sse-parser.test.ts +0 -237
  256. package/src/v2/runtime/__tests__/middleware.test.ts +0 -250
  257. package/src/v2/runtime/__tests__/node-fetch-handler.test.ts +0 -157
  258. package/src/v2/runtime/__tests__/open-generative-ui-middleware.e2e.test.ts +0 -728
  259. package/src/v2/runtime/__tests__/router-edge-cases.test.ts +0 -217
  260. package/src/v2/runtime/__tests__/routing-express.test.ts +0 -174
  261. package/src/v2/runtime/__tests__/routing-single-express.test.ts +0 -168
  262. package/src/v2/runtime/__tests__/routing-single.test.ts +0 -193
  263. package/src/v2/runtime/__tests__/routing.test.ts +0 -257
  264. package/src/v2/runtime/__tests__/runtime.test.ts +0 -234
  265. package/src/v2/runtime/__tests__/sse-response-telemetry.test.ts +0 -108
  266. package/src/v2/runtime/__tests__/telemetry.test.ts +0 -167
  267. package/src/v2/runtime/__tests__/thread-names.test.ts +0 -188
  268. package/src/v2/runtime/core/__tests__/debug-event-bus.test.ts +0 -156
  269. package/src/v2/runtime/core/debug-event-bus.ts +0 -45
  270. package/src/v2/runtime/core/fetch-cors.ts +0 -136
  271. package/src/v2/runtime/core/fetch-handler.ts +0 -492
  272. package/src/v2/runtime/core/fetch-router.ts +0 -203
  273. package/src/v2/runtime/core/hooks.ts +0 -160
  274. package/src/v2/runtime/core/middleware-sse-parser.ts +0 -210
  275. package/src/v2/runtime/core/middleware.ts +0 -115
  276. package/src/v2/runtime/core/runtime.ts +0 -432
  277. package/src/v2/runtime/endpoints/express-fetch-bridge.ts +0 -137
  278. package/src/v2/runtime/endpoints/express-single.ts +0 -54
  279. package/src/v2/runtime/endpoints/express.ts +0 -179
  280. package/src/v2/runtime/endpoints/hono-single.ts +0 -60
  281. package/src/v2/runtime/endpoints/hono.ts +0 -89
  282. package/src/v2/runtime/endpoints/index.ts +0 -4
  283. package/src/v2/runtime/endpoints/node-fetch-handler.ts +0 -48
  284. package/src/v2/runtime/endpoints/node.ts +0 -28
  285. package/src/v2/runtime/endpoints/single-route-helpers.ts +0 -125
  286. package/src/v2/runtime/express.ts +0 -2
  287. package/src/v2/runtime/handlers/__tests__/handle-debug-events.test.ts +0 -176
  288. package/src/v2/runtime/handlers/get-runtime-info.ts +0 -101
  289. package/src/v2/runtime/handlers/handle-connect.ts +0 -80
  290. package/src/v2/runtime/handlers/handle-debug-events.ts +0 -52
  291. package/src/v2/runtime/handlers/handle-run.ts +0 -111
  292. package/src/v2/runtime/handlers/handle-stop.ts +0 -77
  293. package/src/v2/runtime/handlers/handle-threads.ts +0 -11
  294. package/src/v2/runtime/handlers/handle-transcribe.ts +0 -269
  295. package/src/v2/runtime/handlers/header-utils.ts +0 -24
  296. package/src/v2/runtime/handlers/intelligence/connect.ts +0 -102
  297. package/src/v2/runtime/handlers/intelligence/run.ts +0 -351
  298. package/src/v2/runtime/handlers/intelligence/thread-names.ts +0 -246
  299. package/src/v2/runtime/handlers/intelligence/threads.ts +0 -420
  300. package/src/v2/runtime/handlers/shared/agent-utils.ts +0 -154
  301. package/src/v2/runtime/handlers/shared/intelligence-utils.ts +0 -41
  302. package/src/v2/runtime/handlers/shared/json-response.ts +0 -9
  303. package/src/v2/runtime/handlers/shared/resolve-intelligence-user.ts +0 -28
  304. package/src/v2/runtime/handlers/shared/sse-response.ts +0 -215
  305. package/src/v2/runtime/handlers/sse/__tests__/sse-connect-agent-id.test.ts +0 -71
  306. package/src/v2/runtime/handlers/sse/connect.ts +0 -30
  307. package/src/v2/runtime/handlers/sse/run.ts +0 -40
  308. package/src/v2/runtime/hono.ts +0 -2
  309. package/src/v2/runtime/index.ts +0 -51
  310. package/src/v2/runtime/intelligence-platform/__tests__/client.test.ts +0 -601
  311. package/src/v2/runtime/intelligence-platform/__tests__/intelligence-mcp-helper.test.ts +0 -246
  312. package/src/v2/runtime/intelligence-platform/client.ts +0 -818
  313. package/src/v2/runtime/intelligence-platform/index.ts +0 -10
  314. package/src/v2/runtime/node.ts +0 -6
  315. package/src/v2/runtime/open-generative-ui-middleware.ts +0 -373
  316. package/src/v2/runtime/runner/__tests__/finalize-events.test.ts +0 -109
  317. package/src/v2/runtime/runner/__tests__/in-memory-runner.e2e.test.ts +0 -775
  318. package/src/v2/runtime/runner/__tests__/in-memory-runner.test.ts +0 -777
  319. package/src/v2/runtime/runner/__tests__/intelligence-runner.test.ts +0 -1039
  320. package/src/v2/runtime/runner/agent-runner.ts +0 -35
  321. package/src/v2/runtime/runner/in-memory.ts +0 -467
  322. package/src/v2/runtime/runner/index.ts +0 -4
  323. package/src/v2/runtime/runner/intelligence.ts +0 -498
  324. package/src/v2/runtime/telemetry/__tests__/instance-created.test.ts +0 -96
  325. package/src/v2/runtime/telemetry/events.ts +0 -35
  326. package/src/v2/runtime/telemetry/index.ts +0 -7
  327. package/src/v2/runtime/telemetry/instance-created.ts +0 -44
  328. package/src/v2/runtime/telemetry/scarf-client.ts +0 -39
  329. package/src/v2/runtime/telemetry/telemetry-client.ts +0 -70
  330. package/src/v2/runtime/transcription-service/transcription-service.ts +0 -11
  331. package/tests/global.d.ts +0 -1
  332. package/tests/service-adapters/anthropic/allowlist-approach.test.ts +0 -259
  333. package/tests/service-adapters/anthropic/anthropic-adapter-language-model.test.ts +0 -101
  334. package/tests/service-adapters/anthropic/anthropic-adapter.test.ts +0 -645
  335. package/tests/service-adapters/anthropic/utils-token-trimming.test.ts +0 -301
  336. package/tests/service-adapters/groq/groq-adapter-language-model.test.ts +0 -102
  337. package/tests/service-adapters/openai/allowlist-approach.test.ts +0 -294
  338. package/tests/service-adapters/openai/openai-adapter-language-model.test.ts +0 -122
  339. package/tests/service-adapters/openai/openai-adapter.test.ts +0 -291
  340. package/tests/service-adapters/shared/sdk-client-utils.test.ts +0 -36
  341. package/tests/setup.vitest.ts +0 -8
  342. package/tests/tsconfig.json +0 -10
  343. package/tsconfig.json +0 -20
  344. package/tsdown.config.ts +0 -45
  345. package/typedoc.json +0 -4
  346. package/vitest.config.mjs +0 -13
@@ -1,863 +0,0 @@
1
- /**
2
- * <Callout type="info">
3
- * This is the reference for the `CopilotRuntime` class. For more information and example code snippets, please see [Concept: Copilot Runtime](/concepts/copilot-runtime).
4
- * </Callout>
5
- *
6
- * ## Usage
7
- *
8
- * ```tsx
9
- * import { CopilotRuntime } from "@copilotkit/runtime";
10
- *
11
- * const copilotKit = new CopilotRuntime();
12
- * ```
13
- */
14
-
15
- import {
16
- type Action,
17
- type CopilotErrorHandler,
18
- CopilotKitMisuseError,
19
- type MaybePromise,
20
- type NonEmptyRecord,
21
- type Parameter,
22
- readBody,
23
- getZodParameters,
24
- type PartialBy,
25
- isTelemetryDisabled,
26
- type DebugConfig,
27
- } from "@copilotkit/shared";
28
- import type { RunAgentInput } from "@ag-ui/core";
29
- import { aguiToGQL } from "../../graphql/message-conversion/agui-to-gql";
30
- import type {
31
- CopilotServiceAdapter,
32
- RemoteChainParameters,
33
- } from "../../service-adapters";
34
- import {
35
- CopilotRuntime as CopilotRuntimeVNext,
36
- type CopilotRuntimeOptions,
37
- type CopilotRuntimeOptions as CopilotRuntimeOptionsVNext,
38
- type AgentRunner,
39
- type AgentsConfig,
40
- type AgentsFactory,
41
- type AgentFactoryContext,
42
- InMemoryAgentRunner,
43
- } from "../../v2/runtime";
44
-
45
- export type { AgentsConfig, AgentsFactory, AgentFactoryContext };
46
- import { TelemetryAgentRunner } from "./telemetry-agent-runner";
47
- import telemetry from "../telemetry-client";
48
- import { logRuntimeTelemetryDisclosure } from "../telemetry-disclosure";
49
-
50
- import type { MessageInput } from "../../graphql/inputs/message.input";
51
- import type { Message } from "../../graphql/types/converted";
52
-
53
- import {
54
- EndpointType,
55
- type EndpointDefinition,
56
- type CopilotKitEndpoint,
57
- type LangGraphPlatformEndpoint,
58
- } from "./types";
59
-
60
- import type {
61
- CopilotObservabilityConfig,
62
- LLMRequestData,
63
- LLMResponseData,
64
- } from "../observability";
65
- import type { AbstractAgent } from "@ag-ui/client";
66
-
67
- // +++ MCP Imports +++
68
- import {
69
- type MCPClient,
70
- type MCPEndpointConfig,
71
- type MCPTool,
72
- extractParametersFromSchema,
73
- } from "./mcp-tools-utils";
74
- import { BuiltInAgent, type BuiltInAgentClassicConfig } from "../../agent";
75
- // Define the function type alias here or import if defined elsewhere
76
- type CreateMCPClientFunction = (
77
- config: MCPEndpointConfig,
78
- ) => Promise<MCPClient>;
79
-
80
- type ActionsConfiguration<T extends Parameter[] | [] = []> =
81
- | Action<T>[]
82
- | ((ctx: { properties: any; url?: string }) => Action<T>[]);
83
-
84
- interface OnBeforeRequestOptions {
85
- threadId?: string;
86
- runId?: string;
87
- inputMessages: Message[];
88
- properties: any;
89
- url?: string;
90
- }
91
-
92
- type OnBeforeRequestHandler = (
93
- options: OnBeforeRequestOptions,
94
- ) => void | Promise<void>;
95
-
96
- interface OnAfterRequestOptions {
97
- threadId: string;
98
- runId?: string;
99
- inputMessages: Message[];
100
- outputMessages: Message[];
101
- properties: any;
102
- url?: string;
103
- }
104
-
105
- type OnAfterRequestHandler = (
106
- options: OnAfterRequestOptions,
107
- ) => void | Promise<void>;
108
-
109
- interface OnStopGenerationOptions {
110
- threadId: string;
111
- runId?: string;
112
- url?: string;
113
- agentName?: string;
114
- lastMessage: MessageInput;
115
- }
116
- type OnStopGenerationHandler = (
117
- options: OnStopGenerationOptions,
118
- ) => void | Promise<void>;
119
-
120
- interface Middleware {
121
- /**
122
- * A function that is called before the request is processed.
123
- */
124
- /**
125
- * @deprecated This middleware hook is deprecated and will be removed in a future version.
126
- * Use updated middleware integration methods in CopilotRuntimeVNext instead.
127
- */
128
- onBeforeRequest?: OnBeforeRequestHandler;
129
-
130
- /**
131
- * A function that is called after the request is processed.
132
- */
133
- /**
134
- * @deprecated This middleware hook is deprecated and will be removed in a future version.
135
- * Use updated middleware integration methods in CopilotRuntimeVNext instead.
136
- */
137
- onAfterRequest?: OnAfterRequestHandler;
138
- }
139
-
140
- export interface CopilotRuntimeConstructorParams_BASE<
141
- T extends Parameter[] | [] = [],
142
- > {
143
- /**
144
- * Middleware to be used by the runtime.
145
- *
146
- * ```ts
147
- * onBeforeRequest: (options: {
148
- * threadId?: string;
149
- * runId?: string;
150
- * inputMessages: Message[];
151
- * properties: any;
152
- * }) => void | Promise<void>;
153
- * ```
154
- *
155
- * ```ts
156
- * onAfterRequest: (options: {
157
- * threadId?: string;
158
- * runId?: string;
159
- * inputMessages: Message[];
160
- * outputMessages: Message[];
161
- * properties: any;
162
- * }) => void | Promise<void>;
163
- * ```
164
- */
165
- /**
166
- * @deprecated This middleware hook is deprecated and will be removed in a future version.
167
- * Use updated middleware integration methods in CopilotRuntimeVNext instead.
168
- */
169
- middleware?: Middleware;
170
-
171
- /*
172
- * A list of server side actions that can be executed. Will be ignored when remoteActions are set
173
- */
174
- actions?: ActionsConfiguration<T>;
175
-
176
- /*
177
- * Deprecated: Use `remoteEndpoints`.
178
- */
179
- remoteActions?: CopilotKitEndpoint[];
180
-
181
- /*
182
- * A list of remote actions that can be executed.
183
- */
184
- remoteEndpoints?: EndpointDefinition[];
185
-
186
- /*
187
- * An array of LangServer URLs.
188
- */
189
- langserve?: RemoteChainParameters[];
190
-
191
- /**
192
- * Optional agent runner to use for SSE runtime.
193
- */
194
- runner?: AgentRunner;
195
-
196
- /*
197
- * A map of agent names to AGUI agents.
198
- * Example agent config:
199
- * ```ts
200
- * import { AbstractAgent } from "@ag-ui/client";
201
- * // ...
202
- * agents: {
203
- * "support": new CustomerSupportAgent(),
204
- * "technical": new TechnicalAgent()
205
- * }
206
- * ```
207
- */
208
- agents?: Record<string, AbstractAgent>;
209
-
210
- /*
211
- * Delegates agent state processing to the service adapter.
212
- *
213
- * When enabled, individual agent state requests will not be processed by the agent itself.
214
- * Instead, all processing will be handled by the service adapter.
215
- */
216
- delegateAgentProcessingToServiceAdapter?: boolean;
217
-
218
- /**
219
- * Configuration for LLM request/response logging.
220
- * Requires publicApiKey from CopilotKit component to be set:
221
- *
222
- * ```tsx
223
- * <CopilotKit publicApiKey="ck_pub_..." />
224
- * ```
225
- *
226
- * Example logging config:
227
- * ```ts
228
- * logging: {
229
- * enabled: true, // Enable or disable logging
230
- * progressive: true, // Set to false for buffered logging
231
- * logger: {
232
- * logRequest: (data) => langfuse.trace({ name: "LLM Request", input: data }),
233
- * logResponse: (data) => langfuse.trace({ name: "LLM Response", output: data }),
234
- * logError: (errorData) => langfuse.trace({ name: "LLM Error", metadata: errorData }),
235
- * },
236
- * }
237
- * ```
238
- */
239
- observability_c?: CopilotObservabilityConfig;
240
-
241
- /**
242
- * Configuration for connecting to Model Context Protocol (MCP) servers.
243
- * Allows fetching and using tools defined on external MCP-compliant servers.
244
- * Requires providing the `createMCPClient` function during instantiation.
245
- * @experimental
246
- */
247
- mcpServers?: MCPEndpointConfig[];
248
-
249
- /**
250
- * A function that creates an MCP client instance for a given endpoint configuration.
251
- * This function is responsible for using the appropriate MCP client library
252
- * (e.g., `@copilotkit/runtime`, `ai`) to establish a connection.
253
- * Required if `mcpServers` is provided.
254
- *
255
- * ```typescript
256
- * import { experimental_createMCPClient } from "ai"; // Import from vercel ai library
257
- * // ...
258
- * const runtime = new CopilotRuntime({
259
- * mcpServers: [{ endpoint: "..." }],
260
- * async createMCPClient(config) {
261
- * return await experimental_createMCPClient({
262
- * transport: {
263
- * type: "sse",
264
- * url: config.endpoint,
265
- * headers: config.apiKey
266
- * ? { Authorization: `Bearer ${config.apiKey}` }
267
- * : undefined,
268
- * },
269
- * });
270
- * }
271
- * });
272
- * ```
273
- */
274
- createMCPClient?: CreateMCPClientFunction;
275
-
276
- /**
277
- * Optional error handler for comprehensive debugging and observability.
278
- *
279
- * **Requires publicApiKey**: Error handling only works when requests include a valid publicApiKey.
280
- * This is a premium Copilot Cloud feature.
281
- *
282
- * @param errorEvent - Structured error event with rich debugging context
283
- *
284
- * @example
285
- * ```typescript
286
- * const runtime = new CopilotRuntime({
287
- * onError: (errorEvent) => {
288
- * debugDashboard.capture(errorEvent);
289
- * }
290
- * });
291
- * ```
292
- */
293
- onError?: CopilotErrorHandler;
294
-
295
- onStopGeneration?: OnStopGenerationHandler;
296
-
297
- /**
298
- * Enable debug logging for the runtime event pipeline.
299
- * Pass `true` for full output, or an object for granular control:
300
- *
301
- * ```ts
302
- * const runtime = new CopilotRuntime({
303
- * debug: true,
304
- * // or: debug: { events: true, lifecycle: true, verbose: false }
305
- * });
306
- * ```
307
- */
308
- debug?: DebugConfig;
309
-
310
- // /** Optional transcription service for audio processing. */
311
- // transcriptionService?: CopilotRuntimeOptionsVNext["transcriptionService"];
312
- // /** Optional *before* middleware – callback function or webhook URL. */
313
- // beforeRequestMiddleware?: CopilotRuntimeOptionsVNext["beforeRequestMiddleware"];
314
- // /** Optional *after* middleware – callback function or webhook URL. */
315
- // afterRequestMiddleware?: CopilotRuntimeOptionsVNext["afterRequestMiddleware"];
316
- }
317
-
318
- type BeforeRequestMiddleware =
319
- CopilotRuntimeOptionsVNext["beforeRequestMiddleware"];
320
- type AfterRequestMiddleware =
321
- CopilotRuntimeOptionsVNext["afterRequestMiddleware"];
322
- type BeforeRequestMiddlewareFn = Exclude<BeforeRequestMiddleware, string>;
323
- type BeforeRequestMiddlewareFnParameters =
324
- Parameters<BeforeRequestMiddlewareFn>;
325
- type BeforeRequestMiddlewareFnResult = ReturnType<BeforeRequestMiddlewareFn>;
326
- type AfterRequestMiddlewareFn = Exclude<AfterRequestMiddleware, string>;
327
- type AfterRequestMiddlewareFnParameters = Parameters<AfterRequestMiddlewareFn>;
328
-
329
- interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []>
330
- extends
331
- Omit<CopilotRuntimeConstructorParams_BASE<T>, "agents">,
332
- Omit<CopilotRuntimeOptionsVNext, "agents" | "transcriptionService"> {
333
- /**
334
- * TODO: un-omit `transcriptionService` above once it's supported
335
- *
336
- * This satisfies...
337
- * – the optional constraint in `CopilotRuntimeConstructorParams_BASE`
338
- * – the `MaybePromise<NonEmptyRecord<T>>` constraint in `CopilotRuntimeOptionsVNext`
339
- * – the `Record<string, AbstractAgent>` constraint in `both
340
- */
341
- agents?: AgentsConfig;
342
- }
343
-
344
- /**
345
- * Central runtime object passed to all request handlers.
346
- */
347
- export class CopilotRuntime<const T extends Parameter[] | [] = []> {
348
- params?: CopilotRuntimeConstructorParams<T>;
349
- private observability?: CopilotObservabilityConfig;
350
- // Cache MCP tools per endpoint to avoid re-fetching repeatedly
351
- private mcpToolsCache: Map<string, BuiltInAgentClassicConfig["tools"]> =
352
- new Map();
353
- private runtimeArgs: CopilotRuntimeOptions;
354
- private _instance: CopilotRuntimeVNext;
355
-
356
- constructor(
357
- params?: CopilotRuntimeConstructorParams<T> &
358
- PartialBy<CopilotRuntimeOptions, "agents">,
359
- ) {
360
- logRuntimeTelemetryDisclosure();
361
-
362
- const agents = params?.agents ?? {};
363
- const endpointAgents = this.assignEndpointsToAgents(
364
- params?.remoteEndpoints ?? [],
365
- );
366
-
367
- // Merge endpoint agents with user-provided agents.
368
- // When agents is a factory function, wrap it so endpoint agents are merged
369
- // at resolution time (spreading a function produces {} — silent data loss).
370
- let mergedAgents: AgentsConfig;
371
- if (typeof agents === "function") {
372
- mergedAgents = async (ctx) => {
373
- const resolved = await agents(ctx);
374
- return { ...endpointAgents, ...resolved };
375
- };
376
- } else {
377
- mergedAgents = Promise.resolve(agents).then((resolved) => ({
378
- ...endpointAgents,
379
- ...resolved,
380
- }));
381
- }
382
-
383
- // Determine the base runner (user-provided or default)
384
- const baseRunner = params?.runner ?? new InMemoryAgentRunner();
385
-
386
- // Wrap with TelemetryAgentRunner unless telemetry is disabled
387
- // This ensures we always capture agent execution telemetry when enabled,
388
- // even if the user provides their own custom runner
389
- const runner = isTelemetryDisabled()
390
- ? baseRunner
391
- : new TelemetryAgentRunner({ runner: baseRunner });
392
-
393
- this.runtimeArgs = {
394
- agents: mergedAgents,
395
- runner,
396
- licenseToken: params?.licenseToken,
397
- debug: params?.debug,
398
- // TODO: add support for transcriptionService from CopilotRuntimeOptionsVNext once it is ready
399
- // transcriptionService: params?.transcriptionService,
400
-
401
- beforeRequestMiddleware:
402
- this.createOnBeforeRequestHandler(params).bind(this),
403
- ...(params?.afterRequestMiddleware || params?.middleware?.onAfterRequest
404
- ? {
405
- afterRequestMiddleware:
406
- this.createOnAfterRequestHandler(params).bind(this),
407
- }
408
- : {}),
409
- a2ui: params?.a2ui,
410
- mcpApps: params?.mcpApps,
411
- openGenerativeUI: params?.openGenerativeUI,
412
- };
413
- this.params = params;
414
- this.observability = params?.observability_c;
415
- }
416
-
417
- get instance() {
418
- if (!this._instance) {
419
- this._instance = new CopilotRuntimeVNext(this.runtimeArgs);
420
- }
421
-
422
- return this._instance;
423
- }
424
-
425
- private assignEndpointsToAgents(
426
- endpoints: CopilotRuntimeConstructorParams<T>["remoteEndpoints"],
427
- ): Record<string, AbstractAgent> {
428
- let result: Record<string, AbstractAgent> = {};
429
-
430
- if (
431
- endpoints.some(
432
- (endpoint) =>
433
- resolveEndpointType(endpoint) == EndpointType.LangGraphPlatform,
434
- )
435
- ) {
436
- throw new CopilotKitMisuseError({
437
- message:
438
- "LangGraphPlatformEndpoint in remoteEndpoints is deprecated. " +
439
- 'Please use the "agents" option instead with LangGraphAgent from "@copilotkit/runtime/langgraph". ' +
440
- 'Example: agents: { myAgent: new LangGraphAgent({ deploymentUrl: "...", graphId: "..." }) }',
441
- });
442
- }
443
-
444
- return result;
445
- }
446
-
447
- handleServiceAdapter(serviceAdapter: CopilotServiceAdapter) {
448
- this.runtimeArgs.agents = Promise.resolve(
449
- this.runtimeArgs.agents ?? {},
450
- ).then(async (agents) => {
451
- let agentsList = agents;
452
- const isAgentsListEmpty = !Object.keys(agents).length;
453
- const hasServiceAdapter = Boolean(serviceAdapter);
454
- const illegalServiceAdapterNames = ["EmptyAdapter"];
455
- const serviceAdapterCanBeUsedForAgent =
456
- !illegalServiceAdapterNames.includes(serviceAdapter.name);
457
-
458
- if (
459
- isAgentsListEmpty &&
460
- (!hasServiceAdapter || !serviceAdapterCanBeUsedForAgent)
461
- ) {
462
- throw new CopilotKitMisuseError({
463
- message:
464
- "No default agent provided. Please provide a default agent in the runtime config.",
465
- });
466
- }
467
-
468
- if (isAgentsListEmpty) {
469
- const languageModel = serviceAdapter.getLanguageModel?.();
470
- if (languageModel) {
471
- // Adapter exposes a pre-configured LanguageModel (e.g. OpenAI/Anthropic adapters)
472
- agentsList.default = new BuiltInAgent({ model: languageModel });
473
- } else if (serviceAdapter.provider && serviceAdapter.model) {
474
- // Adapter exposes provider/model strings
475
- agentsList.default = new BuiltInAgent({
476
- model: `${serviceAdapter.provider}/${serviceAdapter.model}`,
477
- });
478
- } else {
479
- throw new CopilotKitMisuseError({
480
- message:
481
- `Service adapter "${serviceAdapter.name ?? "unknown"}" does not provide model information. ` +
482
- `When using adapters like LangChainAdapter without an explicit agents list, ` +
483
- `please provide a default agent in the runtime config. Example:\n` +
484
- ` new CopilotRuntime({\n` +
485
- ` agents: { default: new BuiltInAgent({ model: "openai/gpt-4o" }) }\n` +
486
- ` })`,
487
- });
488
- }
489
- }
490
-
491
- const actions = this.params?.actions;
492
- if (actions) {
493
- const mcpTools = await this.getToolsFromMCP();
494
- agentsList = this.assignToolsToAgents(agents, [
495
- ...this.getToolsFromActions(actions),
496
- ...mcpTools,
497
- ]);
498
- }
499
-
500
- return agentsList;
501
- });
502
- }
503
-
504
- // Receive this.params.action and turn it into the AbstractAgent tools
505
- private getToolsFromActions(
506
- actions: ActionsConfiguration<any>,
507
- ): BuiltInAgentClassicConfig["tools"] {
508
- // Resolve actions to an array (handle function case)
509
- const actionsArray =
510
- typeof actions === "function"
511
- ? actions({ properties: {}, url: undefined })
512
- : actions;
513
-
514
- // Convert each Action to a ToolDefinition
515
- return actionsArray.map((action) => {
516
- // Convert JSON schema to Zod schema
517
- const zodSchema = getZodParameters(action.parameters || []);
518
-
519
- return {
520
- name: action.name,
521
- description: action.description || "",
522
- parameters: zodSchema,
523
- execute: () => Promise.resolve(),
524
- };
525
- });
526
- }
527
-
528
- private assignToolsToAgents(
529
- agents: Record<string, AbstractAgent>,
530
- tools: BuiltInAgentClassicConfig["tools"],
531
- ): Record<string, AbstractAgent> {
532
- if (!tools?.length) {
533
- return agents;
534
- }
535
-
536
- const enrichedAgents: Record<string, AbstractAgent> = { ...agents };
537
-
538
- for (const [agentId, agent] of Object.entries(enrichedAgents)) {
539
- const existingConfig = (Reflect.get(agent, "config") ?? {}) as Record<
540
- string,
541
- unknown
542
- >;
543
-
544
- // Skip factory-mode agents — they don't have a tools property
545
- if ("factory" in existingConfig) {
546
- continue;
547
- }
548
-
549
- const classicConfig = existingConfig as BuiltInAgentClassicConfig;
550
- const existingTools = classicConfig.tools ?? [];
551
-
552
- const updatedConfig: BuiltInAgentClassicConfig = {
553
- ...classicConfig,
554
- tools: [...existingTools, ...tools],
555
- };
556
-
557
- Reflect.set(agent, "config", updatedConfig);
558
- enrichedAgents[agentId] = agent;
559
- }
560
-
561
- return enrichedAgents;
562
- }
563
-
564
- private createOnBeforeRequestHandler(
565
- params?: CopilotRuntimeConstructorParams<T> &
566
- PartialBy<CopilotRuntimeOptions, "agents">,
567
- ) {
568
- return async (hookParams: BeforeRequestMiddlewareFnParameters[0]) => {
569
- const { request } = hookParams;
570
-
571
- // Capture telemetry for copilot request creation
572
- const publicApiKey = request.headers.get("x-copilotcloud-public-api-key");
573
- const body = (await readBody(request)) as RunAgentInput;
574
-
575
- const forwardedProps = body?.forwardedProps as
576
- | {
577
- cloud?: { guardrails?: unknown };
578
- metadata?: { requestType?: string };
579
- }
580
- | undefined;
581
-
582
- // Get cloud base URL from environment or default
583
- const cloudBaseUrl =
584
- process.env.COPILOT_CLOUD_BASE_URL || "https://api.cloud.copilotkit.ai";
585
-
586
- telemetry.capture("oss.runtime.copilot_request_created", {
587
- "cloud.guardrails.enabled":
588
- forwardedProps?.cloud?.guardrails !== undefined,
589
- requestType: forwardedProps?.metadata?.requestType ?? "unknown",
590
- "cloud.api_key_provided": !!publicApiKey,
591
- ...(publicApiKey ? { "cloud.public_api_key": publicApiKey } : {}),
592
- "cloud.base_url": cloudBaseUrl,
593
- });
594
-
595
- // We do not process middleware for the internal GET requests
596
- if (request.method === "GET" || !body) return;
597
-
598
- // TODO: get public api key and run with expected data
599
- // if (this.observability?.enabled && this.params.publicApiKey) {
600
- // this.logObservabilityBeforeRequest()
601
- // }
602
-
603
- // TODO: replace hooksParams top argument type with BeforeRequestMiddlewareParameters when exported
604
- const middlewareResult =
605
- await params?.beforeRequestMiddleware?.(hookParams);
606
-
607
- if (params?.middleware?.onBeforeRequest) {
608
- const { request, runtime, path } = hookParams;
609
- const gqlMessages = (aguiToGQL(body.messages) as Message[]).reduce(
610
- (acc, msg) => {
611
- if ("role" in msg && msg.role === "user") {
612
- acc.inputMessages.push(msg);
613
- } else {
614
- acc.outputMessages.push(msg);
615
- }
616
- return acc;
617
- },
618
- { inputMessages: [] as Message[], outputMessages: [] as Message[] },
619
- );
620
- const { inputMessages, outputMessages } = gqlMessages;
621
- params.middleware.onBeforeRequest({
622
- threadId: body.threadId,
623
- runId: body.runId,
624
- inputMessages,
625
- properties: body.forwardedProps,
626
- url: request.url,
627
- } satisfies OnBeforeRequestOptions);
628
- }
629
-
630
- return middlewareResult;
631
- };
632
- }
633
-
634
- private createOnAfterRequestHandler(
635
- params?: CopilotRuntimeConstructorParams<T> &
636
- PartialBy<CopilotRuntimeOptions, "agents">,
637
- ) {
638
- return async (hookParams: AfterRequestMiddlewareFnParameters[0]) => {
639
- // TODO: get public api key and run with expected data
640
- // if (this.observability?.enabled && publicApiKey) {
641
- // this.logObservabilityAfterRequest()
642
- // }
643
-
644
- // TODO: replace hooksParams top argument type with AfterRequestMiddlewareParameters when exported
645
- params?.afterRequestMiddleware?.(hookParams);
646
-
647
- if (params?.middleware?.onAfterRequest) {
648
- const messages = hookParams.messages ?? [];
649
- params.middleware.onAfterRequest({
650
- threadId: hookParams.threadId ?? "",
651
- runId: hookParams.runId,
652
- inputMessages: messages.filter(
653
- (m): m is typeof m & { role: string } =>
654
- "role" in m && m.role === "user",
655
- ) as unknown as Message[],
656
- outputMessages: messages.filter(
657
- (m): m is typeof m & { role: string } =>
658
- "role" in m && m.role !== "user",
659
- ) as unknown as Message[],
660
- // TODO: forward actual properties once the after-request hook has access to the request body
661
- properties: {},
662
- url: hookParams.path,
663
- } satisfies OnAfterRequestOptions);
664
- }
665
- };
666
- }
667
-
668
- // Observability Methods
669
-
670
- /**
671
- * Log LLM request if observability is enabled
672
- */
673
- private async logObservabilityBeforeRequest(
674
- requestData: LLMRequestData,
675
- ): Promise<void> {
676
- try {
677
- await this.observability.hooks.handleRequest(requestData);
678
- } catch (error) {
679
- console.error("Error logging LLM request:", error);
680
- }
681
- }
682
-
683
- /**
684
- * Log final LLM response after request completes
685
- */
686
- private logObservabilityAfterRequest(
687
- outputMessagesPromise: Promise<Message[]>,
688
- baseData: {
689
- threadId: string;
690
- runId?: string;
691
- model?: string;
692
- provider?: string;
693
- agentName?: string;
694
- nodeName?: string;
695
- },
696
- streamedChunks: any[],
697
- requestStartTime: number,
698
- publicApiKey?: string,
699
- ): void {
700
- try {
701
- outputMessagesPromise
702
- .then((outputMessages) => {
703
- const responseData: LLMResponseData = {
704
- threadId: baseData.threadId,
705
- runId: baseData.runId,
706
- model: baseData.model,
707
- // Use collected chunks for progressive mode or outputMessages for regular mode
708
- output: this.observability.progressive
709
- ? streamedChunks
710
- : outputMessages,
711
- latency: Date.now() - requestStartTime,
712
- timestamp: Date.now(),
713
- provider: baseData.provider,
714
- isFinalResponse: true,
715
- agentName: baseData.agentName,
716
- nodeName: baseData.nodeName,
717
- };
718
-
719
- try {
720
- this.observability.hooks.handleResponse(responseData);
721
- } catch (logError) {
722
- console.error("Error logging LLM response:", logError);
723
- }
724
- })
725
- .catch((error) => {
726
- console.error("Failed to get output messages for logging:", error);
727
- });
728
- } catch (error) {
729
- console.error("Error setting up logging for LLM response:", error);
730
- }
731
- }
732
-
733
- // Resolve MCP tools to BuiltInAgent tool definitions
734
- // Optionally accepts request-scoped properties to merge request-provided mcpServers
735
- private async getToolsFromMCP(options?: {
736
- properties?: Record<string, unknown>;
737
- }): Promise<BuiltInAgentClassicConfig["tools"]> {
738
- const runtimeMcpServers = (this.params?.mcpServers ??
739
- []) as MCPEndpointConfig[];
740
- const createMCPClient = this.params?.createMCPClient as
741
- | CreateMCPClientFunction
742
- | undefined;
743
-
744
- // If no runtime config and no request overrides, nothing to do
745
- const requestMcpServers = ((
746
- options?.properties as { mcpServers?: MCPEndpointConfig[] } | undefined
747
- )?.mcpServers ??
748
- (
749
- options?.properties as
750
- | { mcpEndpoints?: MCPEndpointConfig[] }
751
- | undefined
752
- )?.mcpEndpoints ??
753
- []) as MCPEndpointConfig[];
754
-
755
- const hasAnyServers =
756
- (runtimeMcpServers?.length ?? 0) > 0 ||
757
- (requestMcpServers?.length ?? 0) > 0;
758
- if (!hasAnyServers) {
759
- return [];
760
- }
761
-
762
- if (!createMCPClient) {
763
- // Mirror legacy behavior: when servers are provided without a factory, treat as misconfiguration
764
- throw new CopilotKitMisuseError({
765
- message:
766
- "MCP Integration Error: `mcpServers` were provided, but the `createMCPClient` function was not passed to the CopilotRuntime constructor. Please provide an implementation for `createMCPClient`.",
767
- });
768
- }
769
-
770
- // Merge and dedupe endpoints by URL; request-level overrides take precedence
771
- const effectiveEndpoints = (() => {
772
- const byUrl = new Map<string, MCPEndpointConfig>();
773
- for (const ep of runtimeMcpServers) {
774
- if (ep?.endpoint) byUrl.set(ep.endpoint, ep);
775
- }
776
- for (const ep of requestMcpServers) {
777
- if (ep?.endpoint) byUrl.set(ep.endpoint, ep);
778
- }
779
- return Array.from(byUrl.values());
780
- })();
781
-
782
- const allTools: BuiltInAgentClassicConfig["tools"] = [];
783
-
784
- for (const config of effectiveEndpoints) {
785
- const endpointUrl = config.endpoint;
786
- // Return cached tool definitions when available
787
- const cached = this.mcpToolsCache.get(endpointUrl);
788
- if (cached) {
789
- allTools.push(...cached);
790
- continue;
791
- }
792
-
793
- try {
794
- const client = await createMCPClient(config);
795
- const toolsMap = await client.tools();
796
-
797
- const toolDefs: BuiltInAgentClassicConfig["tools"] = Object.entries(
798
- toolsMap,
799
- ).map(([toolName, tool]: [string, MCPTool]) => {
800
- const params: Parameter[] = extractParametersFromSchema(tool);
801
- const zodSchema = getZodParameters(params);
802
- return {
803
- name: toolName,
804
- description:
805
- tool.description || `MCP tool: ${toolName} (from ${endpointUrl})`,
806
- parameters: zodSchema,
807
- execute: () => Promise.resolve(),
808
- };
809
- });
810
-
811
- // Cache per endpoint and add to aggregate
812
- this.mcpToolsCache.set(endpointUrl, toolDefs);
813
- allTools.push(...toolDefs);
814
- } catch (error) {
815
- console.error(
816
- `MCP: Failed to fetch tools from endpoint ${endpointUrl}. Skipping. Error:`,
817
- error,
818
- );
819
- // Cache empty to prevent repeated attempts within lifecycle
820
- this.mcpToolsCache.set(endpointUrl, []);
821
- }
822
- }
823
-
824
- // Dedupe tools by name while preserving last-in wins (request overrides)
825
- const dedupedByName = new Map<string, (typeof allTools)[number]>();
826
- for (const tool of allTools) {
827
- dedupedByName.set(tool.name, tool);
828
- }
829
-
830
- return Array.from(dedupedByName.values());
831
- }
832
- }
833
-
834
- // The two functions below are "factory functions", meant to create the action objects that adhere to the expected interfaces
835
- export function copilotKitEndpoint(
836
- config: Omit<CopilotKitEndpoint, "type">,
837
- ): CopilotKitEndpoint {
838
- return {
839
- ...config,
840
- type: EndpointType.CopilotKit,
841
- };
842
- }
843
-
844
- export function langGraphPlatformEndpoint(
845
- config: Omit<LangGraphPlatformEndpoint, "type">,
846
- ): LangGraphPlatformEndpoint {
847
- return {
848
- ...config,
849
- type: EndpointType.LangGraphPlatform,
850
- };
851
- }
852
-
853
- export function resolveEndpointType(endpoint: EndpointDefinition) {
854
- if (!endpoint.type) {
855
- if ("deploymentUrl" in endpoint && "agents" in endpoint) {
856
- return EndpointType.LangGraphPlatform;
857
- } else {
858
- return EndpointType.CopilotKit;
859
- }
860
- }
861
-
862
- return endpoint.type;
863
- }