@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,818 +0,0 @@
1
- import { logger } from "@copilotkit/shared";
2
-
3
- /**
4
- * Header name carrying the per-call end-user identity that the CopilotKit
5
- * Intelligence `/mcp` endpoint requires. Internal CopilotKit machinery — the
6
- * runtime stamps this onto `agent.headers` after `identifyUser` resolves,
7
- * and the auto-attach in `configureAgentForRequest` reads it back to gate
8
- * MCP-server attachment and to populate the outbound `X-Cpki-User-Id`
9
- * header on every MCP request. Not part of the public user API.
10
- *
11
- * @internal
12
- */
13
- export const INTELLIGENCE_USER_ID_HEADER = "x-cpki-user-id";
14
-
15
- /**
16
- * Error thrown when an Intelligence platform HTTP request returns a non-2xx
17
- * status. Carries the HTTP {@link status} code so callers can branch on
18
- * specific failures (e.g. 404 for "not found", 409 for "conflict") without
19
- * parsing the error message string.
20
- *
21
- * @example
22
- * ```ts
23
- * try {
24
- * await intelligence.getThread({ threadId });
25
- * } catch (error) {
26
- * if (error instanceof PlatformRequestError && error.status === 404) {
27
- * // thread does not exist yet
28
- * }
29
- * }
30
- * ```
31
- */
32
- export class PlatformRequestError extends Error {
33
- constructor(
34
- message: string,
35
- /** The HTTP status code returned by the platform (e.g. 404, 409, 500). */
36
- public readonly status: number,
37
- ) {
38
- super(message);
39
- this.name = "PlatformRequestError";
40
- }
41
- }
42
-
43
- /**
44
- * Client for the CopilotKit Intelligence Platform REST API.
45
- *
46
- * Construct the client once and pass it to any consumers that need it
47
- * (e.g. `CopilotRuntime`, `IntelligenceAgentRunner`):
48
- *
49
- * ```ts
50
- * import { CopilotKitIntelligence, CopilotRuntime } from "@copilotkit/runtime";
51
- *
52
- * const intelligence = new CopilotKitIntelligence({
53
- * apiUrl: "https://api.copilotkit.ai",
54
- * wsUrl: "wss://api.copilotkit.ai",
55
- * apiKey: process.env.COPILOTKIT_API_KEY!,
56
- * });
57
- *
58
- * const runtime = new CopilotRuntime({
59
- * agents,
60
- * intelligence,
61
- * });
62
- * ```
63
- */
64
-
65
- /** Payload passed to `onThreadDeleted` listeners. */
66
- export interface ThreadDeletedPayload {
67
- threadId: string;
68
- userId: string;
69
- agentId: string;
70
- }
71
-
72
- export interface CopilotKitIntelligenceConfig {
73
- /** Base URL of the intelligence platform API, e.g. "https://api.copilotkit.ai" */
74
- apiUrl: string;
75
- /** Intelligence websocket base URL. Runner and client socket URLs are derived from this. */
76
- wsUrl: string;
77
- /** API key for authenticating with the intelligence platform */
78
- apiKey: string;
79
- /**
80
- * Enable the Intelligence platform's MCP server (bash + thread tools) on
81
- * every `BuiltInAgent` run that resolves a user. The auto-attach is
82
- * implemented in `configureAgentForRequest`: when this flag is `true`
83
- * AND the runtime's `identifyUser` callback has placed a user-id onto
84
- * the agent's forwarded headers AND the user has not already configured
85
- * an MCP server pointing at the same URL, the server is appended to the
86
- * agent's effective MCP server list for that run.
87
- *
88
- * Defaults to `false` — opt-in. Existing intelligence setups continue to
89
- * work without the bash MCP server unless they flip this flag.
90
- */
91
- mcpServer?: boolean;
92
- /**
93
- * Initial listener invoked after a thread is created.
94
- * Prefer {@link CopilotKitIntelligence.onThreadCreated} for multiple listeners.
95
- */
96
- onThreadCreated?: (thread: ThreadSummary) => void;
97
- /**
98
- * Initial listener invoked after a thread is updated.
99
- * Prefer {@link CopilotKitIntelligence.onThreadUpdated} for multiple listeners.
100
- */
101
- onThreadUpdated?: (thread: ThreadSummary) => void;
102
- /**
103
- * Initial listener invoked after a thread is deleted.
104
- * Prefer {@link CopilotKitIntelligence.onThreadDeleted} for multiple listeners.
105
- */
106
- onThreadDeleted?: (params: ThreadDeletedPayload) => void;
107
- }
108
-
109
- /**
110
- * Summary metadata for a single thread returned by the platform.
111
- *
112
- * This is the shape returned by list, get, create, and update operations.
113
- * It does not include the thread's message history — use
114
- * {@link CopilotKitIntelligence.getThreadMessages} for that.
115
- */
116
- export interface ThreadSummary {
117
- /** Platform-assigned unique identifier. */
118
- id: string;
119
- /** Human-readable display name, or `null` if the thread has not been named. */
120
- name: string | null;
121
- /** ISO-8601 timestamp of the most recent agent run on this thread. */
122
- lastRunAt?: string;
123
- /** ISO-8601 timestamp of the most recent metadata update. */
124
- lastUpdatedAt?: string;
125
- /** ISO-8601 timestamp when the thread was created. */
126
- createdAt?: string;
127
- /** ISO-8601 timestamp when the thread was last updated. */
128
- updatedAt?: string;
129
- /** Whether the thread has been archived. Archived threads are excluded from default list results. */
130
- archived?: boolean;
131
- /** The agent that owns this thread. */
132
- agentId?: string;
133
- /** The user who created this thread. */
134
- createdById?: string;
135
- /** The organization this thread belongs to. */
136
- organizationId?: string;
137
- }
138
-
139
- /** Response from listing threads for a user/agent pair. */
140
- export interface ListThreadsResponse {
141
- /** The matching threads, sorted by the platform's default ordering. */
142
- threads: ThreadSummary[];
143
- /** Join code for subscribing to realtime metadata updates for these threads. */
144
- joinCode: string;
145
- /** Short-lived token for authenticating the realtime subscription. */
146
- joinToken?: string;
147
- /** Opaque cursor for fetching the next page. `null` or absent when there are no more pages. */
148
- nextCursor?: string | null;
149
- }
150
-
151
- /**
152
- * Fields that can be updated on a thread via {@link CopilotKitIntelligence.updateThread}.
153
- *
154
- * Additional platform-specific fields can be passed as extra keys and will be
155
- * forwarded to the PATCH request body.
156
- */
157
- export interface UpdateThreadRequest {
158
- /** New human-readable display name for the thread. */
159
- name?: string;
160
- [key: string]: unknown;
161
- }
162
-
163
- /** Parameters for creating a new thread via {@link CopilotKitIntelligence.createThread}. */
164
- export interface CreateThreadRequest {
165
- /** Client-generated unique identifier for the new thread. */
166
- threadId: string;
167
- /** The user creating the thread. Used for authorization and scoping. */
168
- userId: string;
169
- /** The agent this thread belongs to. */
170
- agentId: string;
171
- /** Optional initial display name. If omitted, the thread is unnamed until explicitly renamed. */
172
- name?: string;
173
- }
174
-
175
- /** Credentials returned when locking or joining a thread's realtime channel. */
176
- export interface ThreadConnectionResponse {
177
- /** Canonical platform thread identifier for the run or connection. */
178
- threadId: string;
179
- /** Canonical platform run identifier for an active run lock. */
180
- runId?: string;
181
- /** Short-lived token for authenticating the Phoenix channel join. */
182
- joinToken: string;
183
- /** Lock metadata echoed back by the platform. */
184
- lock?: ThreadLockInfo;
185
- }
186
-
187
- export interface SubscribeToThreadsRequest {
188
- userId: string;
189
- }
190
-
191
- export interface SubscribeToThreadsResponse {
192
- joinToken: string;
193
- }
194
-
195
- export type ConnectThreadResponse = ThreadConnectionResponse | null;
196
-
197
- export interface AcquireThreadLockResponse extends ThreadConnectionResponse {
198
- /** Canonical platform run identifier for the acquired lock. */
199
- runId: string;
200
- }
201
-
202
- /** A single message within a thread's persisted history. */
203
- export interface ThreadMessage {
204
- /** Unique identifier for this message. */
205
- id: string;
206
- /** Message role, e.g. `"user"`, `"assistant"`, `"tool"`. */
207
- role: string;
208
- /** Text content of the message. May be absent for tool-call-only messages. */
209
- content?: string;
210
- /** Tool calls initiated by this message (assistant role only). */
211
- toolCalls?: Array<{
212
- id: string;
213
- name: string;
214
- /** JSON-encoded arguments passed to the tool. */
215
- args: string;
216
- }>;
217
- /** For tool-result messages, the ID of the tool call this message responds to. */
218
- toolCallId?: string;
219
- }
220
-
221
- /** Response from {@link CopilotKitIntelligence.getThreadMessages}. */
222
- export interface ThreadMessagesResponse {
223
- messages: ThreadMessage[];
224
- }
225
-
226
- /**
227
- * Persisted AG-UI event for the inspector's debugging views. The platform
228
- * stores raw events keyed by run; the `_inspect` route returns them in
229
- * replay order (oldest first) across every run that targeted the thread.
230
- */
231
- export interface ThreadInspectEvent {
232
- type: string;
233
- [key: string]: unknown;
234
- }
235
-
236
- /**
237
- * Response from {@link CopilotKitIntelligence.getThreadEvents}. Mirrors the
238
- * `ThreadEventsResult` shape returned by the platform's
239
- * `GET /api/_inspect/threads/:id/events` endpoint.
240
- */
241
- export interface ThreadEventsResponse {
242
- events: ThreadInspectEvent[];
243
- /** Row IDs the platform failed to decode (raw column corrupted). */
244
- decodeErrorRowIds: string[];
245
- /** True when the platform hit its per-thread event cap. */
246
- truncated: boolean;
247
- }
248
-
249
- /**
250
- * Response from {@link CopilotKitIntelligence.getThreadState}. Mirrors the
251
- * discriminated `ThreadStateResult` returned by the platform's
252
- * `GET /api/_inspect/threads/:id/state` endpoint, which folds RFC 6902
253
- * STATE_DELTA events on top of the latest STATE_SNAPSHOT.
254
- */
255
- export type ThreadStateResponse =
256
- | { kind: "no-snapshot" }
257
- | { kind: "snapshot-decode-error" }
258
- | { kind: "snapshot"; state: unknown; skippedDeltas: number };
259
-
260
- export interface AcquireThreadLockRequest {
261
- threadId: string;
262
- runId: string;
263
- userId: string;
264
- agentId: string;
265
- /** Custom Redis key prefix for the lock (default: "thread"). */
266
- lockKeyPrefix?: string;
267
- /** Lock TTL in seconds. When set, the lock auto-expires after this duration. */
268
- ttlSeconds?: number;
269
- }
270
-
271
- export interface RenewThreadLockRequest {
272
- threadId: string;
273
- runId: string;
274
- /** New TTL to set on the lock in seconds. */
275
- ttlSeconds: number;
276
- /** Must match the prefix used when acquiring. */
277
- lockKeyPrefix?: string;
278
- }
279
-
280
- export interface CleanupThreadLockRequest {
281
- threadId: string;
282
- runId: string;
283
- }
284
-
285
- export interface RenewThreadLockResponse {
286
- ttlSeconds: number;
287
- }
288
-
289
- export interface ThreadLockInfo {
290
- key: string;
291
- ttlSeconds: number | null;
292
- }
293
-
294
- interface ThreadEnvelope {
295
- thread: ThreadSummary;
296
- }
297
-
298
- export class CopilotKitIntelligence {
299
- #apiUrl: string;
300
- #runnerWsUrl: string;
301
- #clientWsUrl: string;
302
- #apiKey: string;
303
- #mcpServerEnabled: boolean;
304
- #threadCreatedListeners = new Set<(thread: ThreadSummary) => void>();
305
- #threadUpdatedListeners = new Set<(thread: ThreadSummary) => void>();
306
- #threadDeletedListeners = new Set<(params: ThreadDeletedPayload) => void>();
307
-
308
- constructor(config: CopilotKitIntelligenceConfig) {
309
- const intelligenceWsUrl = normalizeIntelligenceWsUrl(config.wsUrl);
310
-
311
- this.#apiUrl = config.apiUrl.replace(/\/$/, "");
312
- this.#runnerWsUrl = deriveRunnerWsUrl(intelligenceWsUrl);
313
- this.#clientWsUrl = deriveClientWsUrl(intelligenceWsUrl);
314
- this.#apiKey = config.apiKey;
315
- this.#mcpServerEnabled = config.mcpServer ?? false;
316
-
317
- if (config.onThreadCreated) {
318
- this.onThreadCreated(config.onThreadCreated);
319
- }
320
- if (config.onThreadUpdated) {
321
- this.onThreadUpdated(config.onThreadUpdated);
322
- }
323
- if (config.onThreadDeleted) {
324
- this.onThreadDeleted(config.onThreadDeleted);
325
- }
326
- }
327
-
328
- /**
329
- * Register a listener invoked whenever a thread is created.
330
- *
331
- * Multiple listeners can be registered. Each call returns an unsubscribe
332
- * function that removes the listener when called.
333
- *
334
- * @param callback - Receives the newly created {@link ThreadSummary}.
335
- * @returns A function that removes this listener when called.
336
- *
337
- * @example
338
- * ```ts
339
- * const unsubscribe = intelligence.onThreadCreated((thread) => {
340
- * console.log("Thread created:", thread.id);
341
- * });
342
- * // later…
343
- * unsubscribe();
344
- * ```
345
- */
346
- onThreadCreated(callback: (thread: ThreadSummary) => void): () => void {
347
- this.#threadCreatedListeners.add(callback);
348
- return () => {
349
- this.#threadCreatedListeners.delete(callback);
350
- };
351
- }
352
-
353
- /**
354
- * Register a listener invoked whenever a thread is updated (including archive).
355
- *
356
- * Multiple listeners can be registered. Each call returns an unsubscribe
357
- * function that removes the listener when called.
358
- *
359
- * @param callback - Receives the updated {@link ThreadSummary}.
360
- * @returns A function that removes this listener when called.
361
- */
362
- onThreadUpdated(callback: (thread: ThreadSummary) => void): () => void {
363
- this.#threadUpdatedListeners.add(callback);
364
- return () => {
365
- this.#threadUpdatedListeners.delete(callback);
366
- };
367
- }
368
-
369
- /**
370
- * Register a listener invoked whenever a thread is deleted.
371
- *
372
- * Multiple listeners can be registered. Each call returns an unsubscribe
373
- * function that removes the listener when called.
374
- *
375
- * @param callback - Receives the {@link ThreadDeletedPayload} identifying
376
- * the deleted thread.
377
- * @returns A function that removes this listener when called.
378
- */
379
- onThreadDeleted(
380
- callback: (params: ThreadDeletedPayload) => void,
381
- ): () => void {
382
- this.#threadDeletedListeners.add(callback);
383
- return () => {
384
- this.#threadDeletedListeners.delete(callback);
385
- };
386
- }
387
-
388
- ɵgetApiUrl(): string {
389
- return this.#apiUrl;
390
- }
391
-
392
- ɵgetRunnerWsUrl(): string {
393
- return this.#runnerWsUrl;
394
- }
395
-
396
- ɵgetClientWsUrl(): string {
397
- return this.#clientWsUrl;
398
- }
399
-
400
- ɵgetRunnerAuthToken(): string {
401
- return this.#apiKey;
402
- }
403
-
404
- /** @internal Used by the runtime's auto-attach to populate `Authorization`. */
405
- ɵgetApiKey(): string {
406
- return this.#apiKey;
407
- }
408
-
409
- /** @internal Used by the runtime's auto-attach to gate MCP attachment. */
410
- ɵisMcpServerEnabled(): boolean {
411
- return this.#mcpServerEnabled;
412
- }
413
-
414
- async #request<T>(method: string, path: string, body?: unknown): Promise<T> {
415
- const url = `${this.#apiUrl}${path}`;
416
-
417
- const headers: Record<string, string> = {
418
- Authorization: `Bearer ${this.#apiKey}`,
419
- "Content-Type": "application/json",
420
- };
421
-
422
- const response = await fetch(url, {
423
- method,
424
- headers,
425
- body: body ? JSON.stringify(body) : undefined,
426
- });
427
-
428
- if (!response.ok) {
429
- const text = await response.text().catch(() => "");
430
- logger.error(
431
- { status: response.status, body: text, path },
432
- "Intelligence platform request failed",
433
- );
434
- throw new PlatformRequestError(
435
- `Intelligence platform error ${response.status}: ${text || response.statusText}`,
436
- response.status,
437
- );
438
- }
439
-
440
- const text = await response.text();
441
- if (!text) {
442
- return undefined as T;
443
- }
444
- return JSON.parse(text) as T;
445
- }
446
-
447
- #invokeLifecycleCallback(
448
- callbackName: "onThreadCreated" | "onThreadUpdated" | "onThreadDeleted",
449
- payload: ThreadSummary | ThreadDeletedPayload,
450
- ): void {
451
- const listeners =
452
- callbackName === "onThreadCreated"
453
- ? this.#threadCreatedListeners
454
- : callbackName === "onThreadUpdated"
455
- ? this.#threadUpdatedListeners
456
- : this.#threadDeletedListeners;
457
-
458
- for (const callback of listeners) {
459
- try {
460
- (callback as (p: typeof payload) => void)(payload);
461
- } catch (error) {
462
- logger.error(
463
- { err: error, callbackName, payload },
464
- "Intelligence lifecycle callback failed",
465
- );
466
- }
467
- }
468
- }
469
-
470
- /**
471
- * List all non-archived threads for a given user and agent.
472
- *
473
- * @param params.userId - User whose threads to list.
474
- * @param params.agentId - Agent whose threads to list.
475
- * @returns The thread list along with realtime subscription credentials.
476
- * @throws {@link PlatformRequestError} on non-2xx responses.
477
- */
478
- async listThreads(params: {
479
- userId: string;
480
- agentId: string;
481
- includeArchived?: boolean;
482
- limit?: number;
483
- cursor?: string;
484
- }): Promise<ListThreadsResponse> {
485
- const query: Record<string, string> = {
486
- userId: params.userId,
487
- agentId: params.agentId,
488
- };
489
- if (params.includeArchived) query.includeArchived = "true";
490
- if (params.limit != null) query.limit = String(params.limit);
491
- if (params.cursor) query.cursor = params.cursor;
492
-
493
- const qs = new URLSearchParams(query).toString();
494
- return this.#request<ListThreadsResponse>("GET", `/api/threads?${qs}`);
495
- }
496
-
497
- async ɵsubscribeToThreads(
498
- params: SubscribeToThreadsRequest,
499
- ): Promise<SubscribeToThreadsResponse> {
500
- return this.#request<SubscribeToThreadsResponse>(
501
- "POST",
502
- "/api/threads/subscribe",
503
- {
504
- userId: params.userId,
505
- },
506
- );
507
- }
508
-
509
- /**
510
- * Update thread metadata (e.g. name).
511
- *
512
- * Triggers the `onThreadUpdated` lifecycle callback on success.
513
- *
514
- * @returns The updated thread summary.
515
- * @throws {@link PlatformRequestError} on non-2xx responses.
516
- */
517
- async updateThread(params: {
518
- threadId: string;
519
- userId: string;
520
- agentId: string;
521
- updates: UpdateThreadRequest;
522
- }): Promise<ThreadSummary> {
523
- const response = await this.#request<ThreadEnvelope>(
524
- "PATCH",
525
- `/api/threads/${encodeURIComponent(params.threadId)}`,
526
- {
527
- userId: params.userId,
528
- agentId: params.agentId,
529
- ...params.updates,
530
- },
531
- );
532
- this.#invokeLifecycleCallback("onThreadUpdated", response.thread);
533
- return response.thread;
534
- }
535
-
536
- /**
537
- * Create a new thread on the platform.
538
- *
539
- * Triggers the `onThreadCreated` lifecycle callback on success.
540
- *
541
- * @returns The newly created thread summary.
542
- * @throws {@link PlatformRequestError} with status 409 if a thread with the
543
- * same `threadId` already exists.
544
- */
545
- async createThread(params: CreateThreadRequest): Promise<ThreadSummary> {
546
- const response = await this.#request<ThreadEnvelope>(
547
- "POST",
548
- `/api/threads`,
549
- {
550
- threadId: params.threadId,
551
- userId: params.userId,
552
- agentId: params.agentId,
553
- ...(params.name !== undefined ? { name: params.name } : {}),
554
- },
555
- );
556
- this.#invokeLifecycleCallback("onThreadCreated", response.thread);
557
- return response.thread;
558
- }
559
-
560
- /**
561
- * Fetch a single thread by ID.
562
- *
563
- * @returns The thread summary.
564
- * @throws {@link PlatformRequestError} with status 404 if the thread does
565
- * not exist.
566
- */
567
- async getThread(params: { threadId: string }): Promise<ThreadSummary> {
568
- const response = await this.#request<ThreadEnvelope>(
569
- "GET",
570
- `/api/threads/${encodeURIComponent(params.threadId)}`,
571
- );
572
- return response.thread;
573
- }
574
-
575
- /**
576
- * Get an existing thread or create it if it does not exist.
577
- *
578
- * Handles the race where a concurrent request creates the thread between
579
- * the initial 404 and the subsequent `createThread` call by catching the
580
- * 409 Conflict and retrying the get.
581
- *
582
- * Triggers the `onThreadCreated` lifecycle callback when a new thread is
583
- * created.
584
- *
585
- * @returns An object containing the thread and a `created` flag indicating
586
- * whether the thread was newly created (`true`) or already existed (`false`).
587
- * @throws {@link PlatformRequestError} on non-2xx responses other than
588
- * 404 (get) and 409 (create race).
589
- */
590
- async getOrCreateThread(
591
- params: CreateThreadRequest,
592
- ): Promise<{ thread: ThreadSummary; created: boolean }> {
593
- try {
594
- const thread = await this.getThread({ threadId: params.threadId });
595
- return { thread, created: false };
596
- } catch (error) {
597
- if (!(error instanceof PlatformRequestError && error.status === 404)) {
598
- throw error;
599
- }
600
- }
601
-
602
- try {
603
- const thread = await this.createThread(params);
604
- return { thread, created: true };
605
- } catch (error) {
606
- // Another request created the thread between our get and create — retry get.
607
- if (error instanceof PlatformRequestError && error.status === 409) {
608
- const thread = await this.getThread({ threadId: params.threadId });
609
- return { thread, created: false };
610
- }
611
- throw error;
612
- }
613
- }
614
-
615
- /**
616
- * Fetch the full message history for a thread.
617
- *
618
- * @returns All persisted messages in chronological order.
619
- * @throws {@link PlatformRequestError} on non-2xx responses.
620
- */
621
- async getThreadMessages(params: {
622
- threadId: string;
623
- }): Promise<ThreadMessagesResponse> {
624
- return this.#request<ThreadMessagesResponse>(
625
- "GET",
626
- `/api/threads/${encodeURIComponent(params.threadId)}/messages`,
627
- );
628
- }
629
-
630
- /**
631
- * Fetch the persisted AG-UI event stream for a thread.
632
- *
633
- * Backed by the platform's `GET /api/_inspect/threads/:id/events`
634
- * introspection endpoint (see Intelligence PR #144). Events are returned
635
- * in replay order across every run that targeted the thread. The
636
- * `_inspect/` prefix flags this as debug-only — production code paths
637
- * must not depend on it.
638
- *
639
- * @throws {@link PlatformRequestError} on non-2xx responses.
640
- */
641
- async getThreadEvents(params: {
642
- threadId: string;
643
- }): Promise<ThreadEventsResponse> {
644
- return this.#request<ThreadEventsResponse>(
645
- "GET",
646
- `/api/_inspect/threads/${encodeURIComponent(params.threadId)}/events`,
647
- );
648
- }
649
-
650
- /**
651
- * Fetch the current agent state for a thread.
652
- *
653
- * Backed by the platform's `GET /api/_inspect/threads/:id/state`
654
- * introspection endpoint (see Intelligence PR #144). The platform folds
655
- * RFC 6902 STATE_DELTA events on top of the latest STATE_SNAPSHOT, so
656
- * the returned state reflects the thread's current state — not just the
657
- * last snapshot. The discriminated response distinguishes "no snapshot
658
- * persisted yet" from "snapshot present" so consumers can render the
659
- * correct empty state.
660
- *
661
- * @throws {@link PlatformRequestError} on non-2xx responses.
662
- */
663
- async getThreadState(params: {
664
- threadId: string;
665
- }): Promise<ThreadStateResponse> {
666
- return this.#request<ThreadStateResponse>(
667
- "GET",
668
- `/api/_inspect/threads/${encodeURIComponent(params.threadId)}/state`,
669
- );
670
- }
671
-
672
- /**
673
- * Mark a thread as archived.
674
- *
675
- * Archived threads are excluded from {@link listThreads} results.
676
- * Triggers the `onThreadUpdated` lifecycle callback on success.
677
- *
678
- * @throws {@link PlatformRequestError} on non-2xx responses.
679
- */
680
- async archiveThread(params: {
681
- threadId: string;
682
- userId: string;
683
- agentId: string;
684
- }): Promise<void> {
685
- const response = await this.#request<ThreadEnvelope>(
686
- "PATCH",
687
- `/api/threads/${encodeURIComponent(params.threadId)}`,
688
- { userId: params.userId, agentId: params.agentId, archived: true },
689
- );
690
- this.#invokeLifecycleCallback("onThreadUpdated", response.thread);
691
- }
692
-
693
- /**
694
- * Permanently delete a thread and its message history.
695
- *
696
- * This is irreversible. Triggers the `onThreadDeleted` lifecycle callback
697
- * on success.
698
- *
699
- * @throws {@link PlatformRequestError} on non-2xx responses.
700
- */
701
- async deleteThread(params: {
702
- threadId: string;
703
- userId: string;
704
- agentId: string;
705
- }): Promise<void> {
706
- await this.#request<void>(
707
- "DELETE",
708
- `/api/threads/${encodeURIComponent(params.threadId)}`,
709
- {
710
- reason: `Deleted via CopilotKit runtime (userId=${params.userId}, agentId=${params.agentId})`,
711
- },
712
- );
713
- this.#invokeLifecycleCallback("onThreadDeleted", params);
714
- }
715
-
716
- async ɵacquireThreadLock(
717
- params: AcquireThreadLockRequest,
718
- ): Promise<AcquireThreadLockResponse> {
719
- return this.#request<AcquireThreadLockResponse>(
720
- "POST",
721
- `/api/threads/${encodeURIComponent(params.threadId)}/lock`,
722
- {
723
- runId: params.runId,
724
- userId: params.userId,
725
- agentId: params.agentId,
726
- ...(params.lockKeyPrefix !== undefined
727
- ? { lockKeyPrefix: params.lockKeyPrefix }
728
- : {}),
729
- ...(params.ttlSeconds !== undefined
730
- ? { ttlSeconds: params.ttlSeconds }
731
- : {}),
732
- },
733
- );
734
- }
735
-
736
- async ɵcleanupThreadLock(params: CleanupThreadLockRequest): Promise<void> {
737
- return this.#request<void>(
738
- "DELETE",
739
- `/api/threads/${encodeURIComponent(params.threadId)}/lock`,
740
- {
741
- runId: params.runId,
742
- },
743
- );
744
- }
745
-
746
- async ɵrenewThreadLock(
747
- params: RenewThreadLockRequest,
748
- ): Promise<RenewThreadLockResponse> {
749
- return this.#request<RenewThreadLockResponse>(
750
- "PATCH",
751
- `/api/threads/${encodeURIComponent(params.threadId)}/lock`,
752
- {
753
- runId: params.runId,
754
- ttlSeconds: params.ttlSeconds,
755
- ...(params.lockKeyPrefix !== undefined
756
- ? { lockKeyPrefix: params.lockKeyPrefix }
757
- : {}),
758
- },
759
- );
760
- }
761
-
762
- async ɵgetActiveJoinCode(params: {
763
- threadId: string;
764
- userId: string;
765
- }): Promise<ThreadConnectionResponse> {
766
- const qs = new URLSearchParams({ userId: params.userId }).toString();
767
- return this.#request<ThreadConnectionResponse>(
768
- "GET",
769
- `/api/threads/${encodeURIComponent(params.threadId)}/join-code?${qs}`,
770
- );
771
- }
772
-
773
- async ɵconnectThread(params: {
774
- threadId: string;
775
- userId: string;
776
- agentId: string;
777
- }): Promise<ConnectThreadResponse> {
778
- const result = await this.#request<ThreadConnectionResponse>(
779
- "POST",
780
- `/api/threads/${encodeURIComponent(params.threadId)}/connect`,
781
- {
782
- userId: params.userId,
783
- agentId: params.agentId,
784
- },
785
- );
786
-
787
- // request() returns undefined for empty/204 responses
788
- return result ?? null;
789
- }
790
- }
791
-
792
- function normalizeIntelligenceWsUrl(wsUrl: string): string {
793
- return wsUrl.replace(/\/$/, "");
794
- }
795
-
796
- function deriveRunnerWsUrl(wsUrl: string): string {
797
- if (wsUrl.endsWith("/runner")) {
798
- return wsUrl;
799
- }
800
-
801
- if (wsUrl.endsWith("/client")) {
802
- return `${wsUrl.slice(0, -"/client".length)}/runner`;
803
- }
804
-
805
- return `${wsUrl}/runner`;
806
- }
807
-
808
- function deriveClientWsUrl(wsUrl: string): string {
809
- if (wsUrl.endsWith("/client")) {
810
- return wsUrl;
811
- }
812
-
813
- if (wsUrl.endsWith("/runner")) {
814
- return `${wsUrl.slice(0, -"/runner".length)}/client`;
815
- }
816
-
817
- return `${wsUrl}/client`;
818
- }