@copilotkit/runtime 1.54.1 → 1.55.0-next.8

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 (337) hide show
  1. package/.eslintrc.js +4 -4
  2. package/CHANGELOG.md +125 -113
  3. package/dist/_virtual/_rolldown/runtime.mjs +25 -1
  4. package/dist/agent/index.cjs +654 -0
  5. package/dist/agent/index.cjs.map +1 -0
  6. package/dist/agent/index.d.cts +263 -0
  7. package/dist/agent/index.d.cts.map +1 -0
  8. package/dist/agent/index.d.mts +263 -0
  9. package/dist/agent/index.d.mts.map +1 -0
  10. package/dist/agent/index.mjs +646 -0
  11. package/dist/agent/index.mjs.map +1 -0
  12. package/dist/graphql/message-conversion/agui-to-gql.cjs.map +1 -1
  13. package/dist/graphql/message-conversion/agui-to-gql.mjs.map +1 -1
  14. package/dist/lib/integrations/nextjs/app-router.cjs +2 -2
  15. package/dist/lib/integrations/nextjs/app-router.cjs.map +1 -1
  16. package/dist/lib/integrations/nextjs/app-router.mjs +1 -1
  17. package/dist/lib/integrations/nextjs/app-router.mjs.map +1 -1
  18. package/dist/lib/integrations/node-http/index.cjs +2 -3
  19. package/dist/lib/integrations/node-http/index.cjs.map +1 -1
  20. package/dist/lib/integrations/node-http/index.mjs +1 -1
  21. package/dist/lib/integrations/node-http/index.mjs.map +1 -1
  22. package/dist/lib/runtime/agent-integrations/langgraph/agent.cjs +1 -1
  23. package/dist/lib/runtime/agent-integrations/langgraph/agent.d.cts +2 -2
  24. package/dist/lib/runtime/agent-integrations/langgraph/agent.d.cts.map +1 -1
  25. package/dist/lib/runtime/agent-integrations/langgraph/agent.d.mts +3 -3
  26. package/dist/lib/runtime/agent-integrations/langgraph/agent.d.mts.map +1 -1
  27. package/dist/lib/runtime/agent-integrations/langgraph/agent.mjs +1 -1
  28. package/dist/lib/runtime/copilot-runtime.cjs +7 -5
  29. package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
  30. package/dist/lib/runtime/copilot-runtime.d.cts +10 -8
  31. package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
  32. package/dist/lib/runtime/copilot-runtime.d.mts +10 -8
  33. package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
  34. package/dist/lib/runtime/copilot-runtime.mjs +7 -5
  35. package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
  36. package/dist/lib/runtime/telemetry-agent-runner.cjs +2 -2
  37. package/dist/lib/runtime/telemetry-agent-runner.cjs.map +1 -1
  38. package/dist/lib/runtime/telemetry-agent-runner.d.cts +2 -1
  39. package/dist/lib/runtime/telemetry-agent-runner.d.cts.map +1 -1
  40. package/dist/lib/runtime/telemetry-agent-runner.d.mts +2 -1
  41. package/dist/lib/runtime/telemetry-agent-runner.d.mts.map +1 -1
  42. package/dist/lib/runtime/telemetry-agent-runner.mjs +1 -1
  43. package/dist/lib/runtime/telemetry-agent-runner.mjs.map +1 -1
  44. package/dist/lib/telemetry-client.cjs +1 -1
  45. package/dist/lib/telemetry-client.mjs +1 -1
  46. package/dist/package.cjs +21 -4
  47. package/dist/package.mjs +21 -4
  48. package/dist/service-adapters/anthropic/anthropic-adapter.d.mts +1 -1
  49. package/dist/v2/index.cjs +41 -15
  50. package/dist/v2/index.d.cts +14 -2
  51. package/dist/v2/index.d.mts +14 -2
  52. package/dist/v2/index.mjs +13 -4
  53. package/dist/v2/runtime/endpoints/express-single.cjs +190 -0
  54. package/dist/v2/runtime/endpoints/express-single.cjs.map +1 -0
  55. package/dist/v2/runtime/endpoints/express-single.d.cts +16 -0
  56. package/dist/v2/runtime/endpoints/express-single.d.cts.map +1 -0
  57. package/dist/v2/runtime/endpoints/express-single.d.mts +16 -0
  58. package/dist/v2/runtime/endpoints/express-single.d.mts.map +1 -0
  59. package/dist/v2/runtime/endpoints/express-single.mjs +187 -0
  60. package/dist/v2/runtime/endpoints/express-single.mjs.map +1 -0
  61. package/dist/v2/runtime/endpoints/express-utils.cjs +119 -0
  62. package/dist/v2/runtime/endpoints/express-utils.cjs.map +1 -0
  63. package/dist/v2/runtime/endpoints/express-utils.mjs +117 -0
  64. package/dist/v2/runtime/endpoints/express-utils.mjs.map +1 -0
  65. package/dist/v2/runtime/endpoints/express.cjs +217 -0
  66. package/dist/v2/runtime/endpoints/express.cjs.map +1 -0
  67. package/dist/v2/runtime/endpoints/express.d.cts +16 -0
  68. package/dist/v2/runtime/endpoints/express.d.cts.map +1 -0
  69. package/dist/v2/runtime/endpoints/express.d.mts +16 -0
  70. package/dist/v2/runtime/endpoints/express.d.mts.map +1 -0
  71. package/dist/v2/runtime/endpoints/express.mjs +214 -0
  72. package/dist/v2/runtime/endpoints/express.mjs.map +1 -0
  73. package/dist/v2/runtime/endpoints/hono-single.cjs +141 -0
  74. package/dist/v2/runtime/endpoints/hono-single.cjs.map +1 -0
  75. package/dist/v2/runtime/endpoints/hono-single.d.cts +41 -0
  76. package/dist/v2/runtime/endpoints/hono-single.d.cts.map +1 -0
  77. package/dist/v2/runtime/endpoints/hono-single.d.mts +41 -0
  78. package/dist/v2/runtime/endpoints/hono-single.d.mts.map +1 -0
  79. package/dist/v2/runtime/endpoints/hono-single.mjs +140 -0
  80. package/dist/v2/runtime/endpoints/hono-single.mjs.map +1 -0
  81. package/dist/v2/runtime/endpoints/hono.cjs +248 -0
  82. package/dist/v2/runtime/endpoints/hono.cjs.map +1 -0
  83. package/dist/v2/runtime/endpoints/hono.d.cts +164 -0
  84. package/dist/v2/runtime/endpoints/hono.d.cts.map +1 -0
  85. package/dist/v2/runtime/endpoints/hono.d.mts +164 -0
  86. package/dist/v2/runtime/endpoints/hono.d.mts.map +1 -0
  87. package/dist/v2/runtime/endpoints/hono.mjs +247 -0
  88. package/dist/v2/runtime/endpoints/hono.mjs.map +1 -0
  89. package/dist/v2/runtime/endpoints/index.d.cts +5 -0
  90. package/dist/v2/runtime/endpoints/index.d.mts +5 -0
  91. package/dist/v2/runtime/endpoints/single-route-helpers.cjs +68 -0
  92. package/dist/v2/runtime/endpoints/single-route-helpers.cjs.map +1 -0
  93. package/dist/v2/runtime/endpoints/single-route-helpers.mjs +65 -0
  94. package/dist/v2/runtime/endpoints/single-route-helpers.mjs.map +1 -0
  95. package/dist/v2/runtime/handlers/get-runtime-info.cjs +51 -0
  96. package/dist/v2/runtime/handlers/get-runtime-info.cjs.map +1 -0
  97. package/dist/v2/runtime/handlers/get-runtime-info.mjs +51 -0
  98. package/dist/v2/runtime/handlers/get-runtime-info.mjs.map +1 -0
  99. package/dist/v2/runtime/handlers/handle-connect.cjs +49 -0
  100. package/dist/v2/runtime/handlers/handle-connect.cjs.map +1 -0
  101. package/dist/v2/runtime/handlers/handle-connect.mjs +49 -0
  102. package/dist/v2/runtime/handlers/handle-connect.mjs.map +1 -0
  103. package/dist/v2/runtime/handlers/handle-run.cjs +61 -0
  104. package/dist/v2/runtime/handlers/handle-run.cjs.map +1 -0
  105. package/dist/v2/runtime/handlers/handle-run.mjs +61 -0
  106. package/dist/v2/runtime/handlers/handle-run.mjs.map +1 -0
  107. package/dist/v2/runtime/handlers/handle-stop.cjs +47 -0
  108. package/dist/v2/runtime/handlers/handle-stop.cjs.map +1 -0
  109. package/dist/v2/runtime/handlers/handle-stop.mjs +46 -0
  110. package/dist/v2/runtime/handlers/handle-stop.mjs.map +1 -0
  111. package/dist/v2/runtime/handlers/handle-transcribe.cjs +112 -0
  112. package/dist/v2/runtime/handlers/handle-transcribe.cjs.map +1 -0
  113. package/dist/v2/runtime/handlers/handle-transcribe.mjs +111 -0
  114. package/dist/v2/runtime/handlers/handle-transcribe.mjs.map +1 -0
  115. package/dist/v2/runtime/handlers/header-utils.cjs +26 -0
  116. package/dist/v2/runtime/handlers/header-utils.cjs.map +1 -0
  117. package/dist/v2/runtime/handlers/header-utils.mjs +25 -0
  118. package/dist/v2/runtime/handlers/header-utils.mjs.map +1 -0
  119. package/dist/v2/runtime/handlers/intelligence/connect.cjs +37 -0
  120. package/dist/v2/runtime/handlers/intelligence/connect.cjs.map +1 -0
  121. package/dist/v2/runtime/handlers/intelligence/connect.mjs +37 -0
  122. package/dist/v2/runtime/handlers/intelligence/connect.mjs.map +1 -0
  123. package/dist/v2/runtime/handlers/intelligence/run.cjs +89 -0
  124. package/dist/v2/runtime/handlers/intelligence/run.cjs.map +1 -0
  125. package/dist/v2/runtime/handlers/intelligence/run.mjs +88 -0
  126. package/dist/v2/runtime/handlers/intelligence/run.mjs.map +1 -0
  127. package/dist/v2/runtime/handlers/intelligence/thread-names.cjs +146 -0
  128. package/dist/v2/runtime/handlers/intelligence/thread-names.cjs.map +1 -0
  129. package/dist/v2/runtime/handlers/intelligence/thread-names.mjs +145 -0
  130. package/dist/v2/runtime/handlers/intelligence/thread-names.mjs.map +1 -0
  131. package/dist/v2/runtime/handlers/intelligence/threads.cjs +159 -0
  132. package/dist/v2/runtime/handlers/intelligence/threads.cjs.map +1 -0
  133. package/dist/v2/runtime/handlers/intelligence/threads.mjs +154 -0
  134. package/dist/v2/runtime/handlers/intelligence/threads.mjs.map +1 -0
  135. package/dist/v2/runtime/handlers/shared/agent-utils.cjs +74 -0
  136. package/dist/v2/runtime/handlers/shared/agent-utils.cjs.map +1 -0
  137. package/dist/v2/runtime/handlers/shared/agent-utils.mjs +70 -0
  138. package/dist/v2/runtime/handlers/shared/agent-utils.mjs.map +1 -0
  139. package/dist/v2/runtime/handlers/shared/intelligence-utils.cjs +21 -0
  140. package/dist/v2/runtime/handlers/shared/intelligence-utils.cjs.map +1 -0
  141. package/dist/v2/runtime/handlers/shared/intelligence-utils.mjs +20 -0
  142. package/dist/v2/runtime/handlers/shared/intelligence-utils.mjs.map +1 -0
  143. package/dist/v2/runtime/handlers/shared/json-response.cjs +12 -0
  144. package/dist/v2/runtime/handlers/shared/json-response.cjs.map +1 -0
  145. package/dist/v2/runtime/handlers/shared/json-response.mjs +10 -0
  146. package/dist/v2/runtime/handlers/shared/json-response.mjs.map +1 -0
  147. package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.cjs +20 -0
  148. package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.cjs.map +1 -0
  149. package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.mjs +20 -0
  150. package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.mjs.map +1 -0
  151. package/dist/v2/runtime/handlers/shared/sse-response.cjs +69 -0
  152. package/dist/v2/runtime/handlers/shared/sse-response.cjs.map +1 -0
  153. package/dist/v2/runtime/handlers/shared/sse-response.mjs +68 -0
  154. package/dist/v2/runtime/handlers/shared/sse-response.mjs.map +1 -0
  155. package/dist/v2/runtime/handlers/sse/connect.cjs +18 -0
  156. package/dist/v2/runtime/handlers/sse/connect.cjs.map +1 -0
  157. package/dist/v2/runtime/handlers/sse/connect.mjs +18 -0
  158. package/dist/v2/runtime/handlers/sse/connect.mjs.map +1 -0
  159. package/dist/v2/runtime/handlers/sse/run.cjs +18 -0
  160. package/dist/v2/runtime/handlers/sse/run.cjs.map +1 -0
  161. package/dist/v2/runtime/handlers/sse/run.mjs +18 -0
  162. package/dist/v2/runtime/handlers/sse/run.mjs.map +1 -0
  163. package/dist/v2/runtime/index.d.cts +13 -0
  164. package/dist/v2/runtime/index.d.mts +14 -0
  165. package/dist/v2/runtime/intelligence-platform/client.cjs +333 -0
  166. package/dist/v2/runtime/intelligence-platform/client.cjs.map +1 -0
  167. package/dist/v2/runtime/intelligence-platform/client.d.cts +336 -0
  168. package/dist/v2/runtime/intelligence-platform/client.d.cts.map +1 -0
  169. package/dist/v2/runtime/intelligence-platform/client.d.mts +336 -0
  170. package/dist/v2/runtime/intelligence-platform/client.d.mts.map +1 -0
  171. package/dist/v2/runtime/intelligence-platform/client.mjs +331 -0
  172. package/dist/v2/runtime/intelligence-platform/client.mjs.map +1 -0
  173. package/dist/v2/runtime/intelligence-platform/index.d.mts +2 -0
  174. package/dist/v2/runtime/middleware-sse-parser.cjs +138 -0
  175. package/dist/v2/runtime/middleware-sse-parser.cjs.map +1 -0
  176. package/dist/v2/runtime/middleware-sse-parser.d.cts +22 -0
  177. package/dist/v2/runtime/middleware-sse-parser.d.cts.map +1 -0
  178. package/dist/v2/runtime/middleware-sse-parser.d.mts +22 -0
  179. package/dist/v2/runtime/middleware-sse-parser.d.mts.map +1 -0
  180. package/dist/v2/runtime/middleware-sse-parser.mjs +137 -0
  181. package/dist/v2/runtime/middleware-sse-parser.mjs.map +1 -0
  182. package/dist/v2/runtime/middleware.cjs +35 -0
  183. package/dist/v2/runtime/middleware.cjs.map +1 -0
  184. package/dist/v2/runtime/middleware.d.cts +32 -0
  185. package/dist/v2/runtime/middleware.d.cts.map +1 -0
  186. package/dist/v2/runtime/middleware.d.mts +32 -0
  187. package/dist/v2/runtime/middleware.d.mts.map +1 -0
  188. package/dist/v2/runtime/middleware.mjs +33 -0
  189. package/dist/v2/runtime/middleware.mjs.map +1 -0
  190. package/dist/v2/runtime/runner/agent-runner.cjs +8 -0
  191. package/dist/v2/runtime/runner/agent-runner.cjs.map +1 -0
  192. package/dist/v2/runtime/runner/agent-runner.d.cts +32 -0
  193. package/dist/v2/runtime/runner/agent-runner.d.cts.map +1 -0
  194. package/dist/v2/runtime/runner/agent-runner.d.mts +32 -0
  195. package/dist/v2/runtime/runner/agent-runner.d.mts.map +1 -0
  196. package/dist/v2/runtime/runner/agent-runner.mjs +7 -0
  197. package/dist/v2/runtime/runner/agent-runner.mjs.map +1 -0
  198. package/dist/v2/runtime/runner/in-memory.cjs +223 -0
  199. package/dist/v2/runtime/runner/in-memory.cjs.map +1 -0
  200. package/dist/v2/runtime/runner/in-memory.d.cts +15 -0
  201. package/dist/v2/runtime/runner/in-memory.d.cts.map +1 -0
  202. package/dist/v2/runtime/runner/in-memory.d.mts +15 -0
  203. package/dist/v2/runtime/runner/in-memory.d.mts.map +1 -0
  204. package/dist/v2/runtime/runner/in-memory.mjs +222 -0
  205. package/dist/v2/runtime/runner/in-memory.mjs.map +1 -0
  206. package/dist/v2/runtime/runner/index.d.cts +6 -0
  207. package/dist/v2/runtime/runner/index.d.mts +6 -0
  208. package/dist/v2/runtime/runner/index.mjs +7 -0
  209. package/dist/v2/runtime/runner/intelligence.cjs +246 -0
  210. package/dist/v2/runtime/runner/intelligence.cjs.map +1 -0
  211. package/dist/v2/runtime/runner/intelligence.d.cts +57 -0
  212. package/dist/v2/runtime/runner/intelligence.d.cts.map +1 -0
  213. package/dist/v2/runtime/runner/intelligence.d.mts +57 -0
  214. package/dist/v2/runtime/runner/intelligence.d.mts.map +1 -0
  215. package/dist/v2/runtime/runner/intelligence.mjs +245 -0
  216. package/dist/v2/runtime/runner/intelligence.mjs.map +1 -0
  217. package/dist/v2/runtime/runtime.cjs +101 -0
  218. package/dist/v2/runtime/runtime.cjs.map +1 -0
  219. package/dist/v2/runtime/runtime.d.cts +132 -0
  220. package/dist/v2/runtime/runtime.d.cts.map +1 -0
  221. package/dist/v2/runtime/runtime.d.mts +133 -0
  222. package/dist/v2/runtime/runtime.d.mts.map +1 -0
  223. package/dist/v2/runtime/runtime.mjs +97 -0
  224. package/dist/v2/runtime/runtime.mjs.map +1 -0
  225. package/dist/v2/runtime/telemetry/scarf-client.cjs +32 -0
  226. package/dist/v2/runtime/telemetry/scarf-client.cjs.map +1 -0
  227. package/dist/v2/runtime/telemetry/scarf-client.mjs +32 -0
  228. package/dist/v2/runtime/telemetry/scarf-client.mjs.map +1 -0
  229. package/dist/v2/runtime/telemetry/telemetry-client.cjs +35 -0
  230. package/dist/v2/runtime/telemetry/telemetry-client.cjs.map +1 -0
  231. package/dist/v2/runtime/telemetry/telemetry-client.mjs +35 -0
  232. package/dist/v2/runtime/telemetry/telemetry-client.mjs.map +1 -0
  233. package/dist/v2/runtime/transcription-service/transcription-service.cjs +8 -0
  234. package/dist/v2/runtime/transcription-service/transcription-service.cjs.map +1 -0
  235. package/dist/v2/runtime/transcription-service/transcription-service.d.cts +15 -0
  236. package/dist/v2/runtime/transcription-service/transcription-service.d.cts.map +1 -0
  237. package/dist/v2/runtime/transcription-service/transcription-service.d.mts +15 -0
  238. package/dist/v2/runtime/transcription-service/transcription-service.d.mts.map +1 -0
  239. package/dist/v2/runtime/transcription-service/transcription-service.mjs +7 -0
  240. package/dist/v2/runtime/transcription-service/transcription-service.mjs.map +1 -0
  241. package/package.json +24 -7
  242. package/src/agent/__tests__/ai-sdk-v6-compat.test.ts +116 -0
  243. package/src/agent/__tests__/basic-agent.test.ts +1698 -0
  244. package/src/agent/__tests__/config-tools-execution.test.ts +516 -0
  245. package/src/agent/__tests__/mcp-clients.test.ts +260 -0
  246. package/src/agent/__tests__/property-overrides.test.ts +598 -0
  247. package/src/agent/__tests__/standard-schema-tools.test.ts +313 -0
  248. package/src/agent/__tests__/standard-schema-types.test.ts +158 -0
  249. package/src/agent/__tests__/state-tools.test.ts +436 -0
  250. package/src/agent/__tests__/test-helpers.ts +193 -0
  251. package/src/agent/__tests__/utils.test.ts +536 -0
  252. package/src/agent/__tests__/zod-regression.test.ts +350 -0
  253. package/src/agent/index.ts +1329 -0
  254. package/src/graphql/message-conversion/agui-to-gql.test.ts +1 -1
  255. package/src/graphql/message-conversion/agui-to-gql.ts +1 -1
  256. package/src/graphql/message-conversion/gql-to-agui.ts +1 -1
  257. package/src/graphql/message-conversion/roundtrip-conversion.test.ts +1 -1
  258. package/src/lib/integrations/nextjs/app-router.ts +2 -2
  259. package/src/lib/integrations/node-http/index.ts +2 -2
  260. package/src/lib/runtime/copilot-runtime.ts +3 -5
  261. package/src/lib/runtime/telemetry-agent-runner.ts +1 -1
  262. package/src/service-adapters/conversion.test.ts +1 -1
  263. package/src/service-adapters/conversion.ts +1 -28
  264. package/src/v2/index.ts +5 -2
  265. package/src/v2/runtime/__tests__/cors-credentials.test.ts +320 -0
  266. package/src/v2/runtime/__tests__/express-abort-signal.test.ts +25 -0
  267. package/src/v2/runtime/__tests__/express-body-order.test.ts +76 -0
  268. package/src/v2/runtime/__tests__/express-single-sse.test.ts +122 -0
  269. package/src/v2/runtime/__tests__/get-runtime-info.test.ts +141 -0
  270. package/src/v2/runtime/__tests__/handle-connect.test.ts +423 -0
  271. package/src/v2/runtime/__tests__/handle-run.test.ts +910 -0
  272. package/src/v2/runtime/__tests__/handle-threads.test.ts +388 -0
  273. package/src/v2/runtime/__tests__/handle-transcribe.test.ts +301 -0
  274. package/src/v2/runtime/__tests__/header-utils.test.ts +88 -0
  275. package/src/v2/runtime/__tests__/in-process-agent-runner-messages.test.ts +230 -0
  276. package/src/v2/runtime/__tests__/in-process-agent-runner.test.ts +1030 -0
  277. package/src/v2/runtime/__tests__/middleware-express.test.ts +206 -0
  278. package/src/v2/runtime/__tests__/middleware-single-express.test.ts +211 -0
  279. package/src/v2/runtime/__tests__/middleware-single.test.ts +225 -0
  280. package/src/v2/runtime/__tests__/middleware-sse-parser.test.ts +187 -0
  281. package/src/v2/runtime/__tests__/middleware.test.ts +251 -0
  282. package/src/v2/runtime/__tests__/routing-express.test.ts +174 -0
  283. package/src/v2/runtime/__tests__/routing-single-express.test.ts +168 -0
  284. package/src/v2/runtime/__tests__/routing-single.test.ts +193 -0
  285. package/src/v2/runtime/__tests__/routing.test.ts +257 -0
  286. package/src/v2/runtime/__tests__/runtime.test.ts +123 -0
  287. package/src/v2/runtime/__tests__/telemetry.test.ts +167 -0
  288. package/src/v2/runtime/__tests__/thread-names.test.ts +188 -0
  289. package/src/v2/runtime/endpoints/express-single.ts +231 -0
  290. package/src/v2/runtime/endpoints/express-utils.ts +182 -0
  291. package/src/v2/runtime/endpoints/express.ts +275 -0
  292. package/src/v2/runtime/endpoints/hono-single.ts +212 -0
  293. package/src/v2/runtime/endpoints/hono.ts +314 -0
  294. package/src/v2/runtime/endpoints/index.ts +4 -0
  295. package/src/v2/runtime/endpoints/single-route-helpers.ts +125 -0
  296. package/src/v2/runtime/express.ts +2 -0
  297. package/src/v2/runtime/handler.ts +3 -0
  298. package/src/v2/runtime/handlers/get-runtime-info.ts +79 -0
  299. package/src/v2/runtime/handlers/handle-connect.ts +76 -0
  300. package/src/v2/runtime/handlers/handle-run.ts +89 -0
  301. package/src/v2/runtime/handlers/handle-stop.ts +76 -0
  302. package/src/v2/runtime/handlers/handle-threads.ts +7 -0
  303. package/src/v2/runtime/handlers/handle-transcribe.ts +256 -0
  304. package/src/v2/runtime/handlers/header-utils.ts +24 -0
  305. package/src/v2/runtime/handlers/intelligence/connect.ts +65 -0
  306. package/src/v2/runtime/handlers/intelligence/run.ts +152 -0
  307. package/src/v2/runtime/handlers/intelligence/thread-names.ts +246 -0
  308. package/src/v2/runtime/handlers/intelligence/threads.ts +233 -0
  309. package/src/v2/runtime/handlers/shared/agent-utils.ts +136 -0
  310. package/src/v2/runtime/handlers/shared/intelligence-utils.ts +21 -0
  311. package/src/v2/runtime/handlers/shared/json-response.ts +6 -0
  312. package/src/v2/runtime/handlers/shared/resolve-intelligence-user.ts +25 -0
  313. package/src/v2/runtime/handlers/shared/sse-response.ts +100 -0
  314. package/src/v2/runtime/handlers/sse/connect.ts +24 -0
  315. package/src/v2/runtime/handlers/sse/run.ts +27 -0
  316. package/src/v2/runtime/index.ts +20 -0
  317. package/src/v2/runtime/intelligence-platform/__tests__/client.test.ts +605 -0
  318. package/src/v2/runtime/intelligence-platform/client.ts +659 -0
  319. package/src/v2/runtime/intelligence-platform/index.ts +10 -0
  320. package/src/v2/runtime/middleware-sse-parser.ts +200 -0
  321. package/src/v2/runtime/middleware.ts +115 -0
  322. package/src/v2/runtime/runner/__tests__/finalize-events.test.ts +109 -0
  323. package/src/v2/runtime/runner/__tests__/in-memory-runner.e2e.test.ts +775 -0
  324. package/src/v2/runtime/runner/__tests__/in-memory-runner.test.ts +363 -0
  325. package/src/v2/runtime/runner/__tests__/intelligence-runner.test.ts +981 -0
  326. package/src/v2/runtime/runner/agent-runner.ts +36 -0
  327. package/src/v2/runtime/runner/in-memory.ts +381 -0
  328. package/src/v2/runtime/runner/index.ts +4 -0
  329. package/src/v2/runtime/runner/intelligence.ts +429 -0
  330. package/src/v2/runtime/runtime.ts +260 -0
  331. package/src/v2/runtime/telemetry/events.ts +35 -0
  332. package/src/v2/runtime/telemetry/index.ts +7 -0
  333. package/src/v2/runtime/telemetry/scarf-client.ts +39 -0
  334. package/src/v2/runtime/telemetry/telemetry-client.ts +70 -0
  335. package/src/v2/runtime/transcription-service/transcription-service.ts +11 -0
  336. package/tsconfig.json +9 -2
  337. package/tsdown.config.ts +1 -0
@@ -0,0 +1,659 @@
1
+ import { logger } from "@copilotkit/shared";
2
+ import type { BaseEvent } from "@ag-ui/client";
3
+
4
+ /**
5
+ * Error thrown when an Intelligence platform HTTP request returns a non-2xx
6
+ * status. Carries the HTTP {@link status} code so callers can branch on
7
+ * specific failures (e.g. 404 for "not found", 409 for "conflict") without
8
+ * parsing the error message string.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * try {
13
+ * await intelligence.getThread({ threadId });
14
+ * } catch (error) {
15
+ * if (error instanceof PlatformRequestError && error.status === 404) {
16
+ * // thread does not exist yet
17
+ * }
18
+ * }
19
+ * ```
20
+ */
21
+ export class PlatformRequestError extends Error {
22
+ constructor(
23
+ message: string,
24
+ /** The HTTP status code returned by the platform (e.g. 404, 409, 500). */
25
+ public readonly status: number,
26
+ ) {
27
+ super(message);
28
+ this.name = "PlatformRequestError";
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Client for the CopilotKit Intelligence Platform REST API.
34
+ *
35
+ * Construct the client once and pass it to any consumers that need it
36
+ * (e.g. `CopilotRuntime`, `IntelligenceAgentRunner`):
37
+ *
38
+ * ```ts
39
+ * import { CopilotKitIntelligence, CopilotRuntime } from "@copilotkit/runtime";
40
+ *
41
+ * const intelligence = new CopilotKitIntelligence({
42
+ * apiUrl: "https://api.copilotkit.ai",
43
+ * wsUrl: "wss://api.copilotkit.ai",
44
+ * apiKey: process.env.COPILOTKIT_API_KEY!,
45
+ * organizationId: process.env.COPILOTKIT_ORGANIZATION_ID!,
46
+ * });
47
+ *
48
+ * const runtime = new CopilotRuntime({
49
+ * agents,
50
+ * intelligence,
51
+ * });
52
+ * ```
53
+ */
54
+
55
+ /** Payload passed to `onThreadDeleted` listeners. */
56
+ export interface ThreadDeletedPayload {
57
+ threadId: string;
58
+ userId: string;
59
+ agentId: string;
60
+ }
61
+
62
+ export interface CopilotKitIntelligenceConfig {
63
+ /** Base URL of the intelligence platform API, e.g. "https://api.copilotkit.ai" */
64
+ apiUrl: string;
65
+ /** Intelligence websocket base URL. Runner and client socket URLs are derived from this. */
66
+ wsUrl: string;
67
+ /** API key for authenticating with the intelligence platform */
68
+ apiKey: string;
69
+ /** Organization identifier used for self-hosted Intelligence instances */
70
+ organizationId: string;
71
+ /**
72
+ * Initial listener invoked after a thread is created.
73
+ * Prefer {@link CopilotKitIntelligence.onThreadCreated} for multiple listeners.
74
+ */
75
+ onThreadCreated?: (thread: ThreadSummary) => void;
76
+ /**
77
+ * Initial listener invoked after a thread is updated.
78
+ * Prefer {@link CopilotKitIntelligence.onThreadUpdated} for multiple listeners.
79
+ */
80
+ onThreadUpdated?: (thread: ThreadSummary) => void;
81
+ /**
82
+ * Initial listener invoked after a thread is deleted.
83
+ * Prefer {@link CopilotKitIntelligence.onThreadDeleted} for multiple listeners.
84
+ */
85
+ onThreadDeleted?: (params: ThreadDeletedPayload) => void;
86
+ }
87
+
88
+ /**
89
+ * Summary metadata for a single thread returned by the platform.
90
+ *
91
+ * This is the shape returned by list, get, create, and update operations.
92
+ * It does not include the thread's message history — use
93
+ * {@link CopilotKitIntelligence.getThreadMessages} for that.
94
+ */
95
+ export interface ThreadSummary {
96
+ /** Platform-assigned unique identifier. */
97
+ id: string;
98
+ /** Human-readable display name, or `null` if the thread has not been named. */
99
+ name: string | null;
100
+ /** ISO-8601 timestamp of the most recent agent run on this thread. */
101
+ lastRunAt?: string;
102
+ /** ISO-8601 timestamp of the most recent metadata update. */
103
+ lastUpdatedAt?: string;
104
+ /** ISO-8601 timestamp when the thread was created. */
105
+ createdAt?: string;
106
+ /** ISO-8601 timestamp when the thread was last updated. */
107
+ updatedAt?: string;
108
+ /** Whether the thread has been archived. Archived threads are excluded from default list results. */
109
+ archived?: boolean;
110
+ /** The agent that owns this thread. */
111
+ agentId?: string;
112
+ /** The user who created this thread. */
113
+ createdById?: string;
114
+ /** The organization this thread belongs to. */
115
+ organizationId?: string;
116
+ }
117
+
118
+ /** Response from listing threads for a user/agent pair. */
119
+ export interface ListThreadsResponse {
120
+ /** The matching threads, sorted by the platform's default ordering. */
121
+ threads: ThreadSummary[];
122
+ /** Join code for subscribing to realtime metadata updates for these threads. */
123
+ joinCode: string;
124
+ /** Short-lived token for authenticating the realtime subscription. */
125
+ joinToken?: string;
126
+ /** Opaque cursor for fetching the next page. `null` or absent when there are no more pages. */
127
+ nextCursor?: string | null;
128
+ }
129
+
130
+ /**
131
+ * Fields that can be updated on a thread via {@link CopilotKitIntelligence.updateThread}.
132
+ *
133
+ * Additional platform-specific fields can be passed as extra keys and will be
134
+ * forwarded to the PATCH request body.
135
+ */
136
+ export interface UpdateThreadRequest {
137
+ /** New human-readable display name for the thread. */
138
+ name?: string;
139
+ [key: string]: unknown;
140
+ }
141
+
142
+ /** Parameters for creating a new thread via {@link CopilotKitIntelligence.createThread}. */
143
+ export interface CreateThreadRequest {
144
+ /** Client-generated unique identifier for the new thread. */
145
+ threadId: string;
146
+ /** The user creating the thread. Used for authorization and scoping. */
147
+ userId: string;
148
+ /** The agent this thread belongs to. */
149
+ agentId: string;
150
+ /** Optional initial display name. If omitted, the thread is unnamed until explicitly renamed. */
151
+ name?: string;
152
+ }
153
+
154
+ /** Credentials returned when locking or joining a thread's realtime channel. */
155
+ export interface ThreadConnectionResponse {
156
+ /** Short-lived token for authenticating the Phoenix channel join. */
157
+ joinToken: string;
158
+ /** Optional join code that can be shared with other clients to join the same channel. */
159
+ joinCode?: string;
160
+ }
161
+
162
+ export interface SubscribeToThreadsRequest {
163
+ userId: string;
164
+ }
165
+
166
+ export interface SubscribeToThreadsResponse {
167
+ joinToken: string;
168
+ }
169
+
170
+ export interface ConnectThreadBootstrapResponse {
171
+ mode: "bootstrap";
172
+ latestEventId: string | null;
173
+ events: BaseEvent[];
174
+ }
175
+
176
+ export interface ConnectThreadLiveResponse {
177
+ mode: "live";
178
+ joinToken: string;
179
+ joinFromEventId: string | null;
180
+ events: BaseEvent[];
181
+ }
182
+
183
+ export type ConnectThreadResponse =
184
+ | ConnectThreadBootstrapResponse
185
+ | ConnectThreadLiveResponse
186
+ | null;
187
+
188
+ /** A single message within a thread's persisted history. */
189
+ export interface ThreadMessage {
190
+ /** Unique identifier for this message. */
191
+ id: string;
192
+ /** Message role, e.g. `"user"`, `"assistant"`, `"tool"`. */
193
+ role: string;
194
+ /** Text content of the message. May be absent for tool-call-only messages. */
195
+ content?: string;
196
+ /** Tool calls initiated by this message (assistant role only). */
197
+ toolCalls?: Array<{
198
+ id: string;
199
+ name: string;
200
+ /** JSON-encoded arguments passed to the tool. */
201
+ args: string;
202
+ }>;
203
+ /** For tool-result messages, the ID of the tool call this message responds to. */
204
+ toolCallId?: string;
205
+ }
206
+
207
+ /** Response from {@link CopilotKitIntelligence.getThreadMessages}. */
208
+ export interface ThreadMessagesResponse {
209
+ messages: ThreadMessage[];
210
+ }
211
+
212
+ export interface AcquireThreadLockRequest {
213
+ threadId: string;
214
+ runId: string;
215
+ userId: string;
216
+ }
217
+
218
+ interface ThreadEnvelope {
219
+ thread: ThreadSummary;
220
+ }
221
+
222
+ export class CopilotKitIntelligence {
223
+ #apiUrl: string;
224
+ #runnerWsUrl: string;
225
+ #clientWsUrl: string;
226
+ #apiKey: string;
227
+ #organizationId: string;
228
+ #threadCreatedListeners = new Set<(thread: ThreadSummary) => void>();
229
+ #threadUpdatedListeners = new Set<(thread: ThreadSummary) => void>();
230
+ #threadDeletedListeners = new Set<(params: ThreadDeletedPayload) => void>();
231
+
232
+ constructor(config: CopilotKitIntelligenceConfig) {
233
+ const intelligenceWsUrl = normalizeIntelligenceWsUrl(config.wsUrl);
234
+
235
+ this.#apiUrl = config.apiUrl.replace(/\/$/, "");
236
+ this.#runnerWsUrl = deriveRunnerWsUrl(intelligenceWsUrl);
237
+ this.#clientWsUrl = deriveClientWsUrl(intelligenceWsUrl);
238
+ this.#apiKey = config.apiKey;
239
+ this.#organizationId = config.organizationId;
240
+
241
+ if (config.onThreadCreated) {
242
+ this.onThreadCreated(config.onThreadCreated);
243
+ }
244
+ if (config.onThreadUpdated) {
245
+ this.onThreadUpdated(config.onThreadUpdated);
246
+ }
247
+ if (config.onThreadDeleted) {
248
+ this.onThreadDeleted(config.onThreadDeleted);
249
+ }
250
+ }
251
+
252
+ /**
253
+ * Register a listener invoked whenever a thread is created.
254
+ *
255
+ * Multiple listeners can be registered. Each call returns an unsubscribe
256
+ * function that removes the listener when called.
257
+ *
258
+ * @param callback - Receives the newly created {@link ThreadSummary}.
259
+ * @returns A function that removes this listener when called.
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * const unsubscribe = intelligence.onThreadCreated((thread) => {
264
+ * console.log("Thread created:", thread.id);
265
+ * });
266
+ * // later…
267
+ * unsubscribe();
268
+ * ```
269
+ */
270
+ onThreadCreated(callback: (thread: ThreadSummary) => void): () => void {
271
+ this.#threadCreatedListeners.add(callback);
272
+ return () => {
273
+ this.#threadCreatedListeners.delete(callback);
274
+ };
275
+ }
276
+
277
+ /**
278
+ * Register a listener invoked whenever a thread is updated (including archive).
279
+ *
280
+ * Multiple listeners can be registered. Each call returns an unsubscribe
281
+ * function that removes the listener when called.
282
+ *
283
+ * @param callback - Receives the updated {@link ThreadSummary}.
284
+ * @returns A function that removes this listener when called.
285
+ */
286
+ onThreadUpdated(callback: (thread: ThreadSummary) => void): () => void {
287
+ this.#threadUpdatedListeners.add(callback);
288
+ return () => {
289
+ this.#threadUpdatedListeners.delete(callback);
290
+ };
291
+ }
292
+
293
+ /**
294
+ * Register a listener invoked whenever a thread is deleted.
295
+ *
296
+ * Multiple listeners can be registered. Each call returns an unsubscribe
297
+ * function that removes the listener when called.
298
+ *
299
+ * @param callback - Receives the {@link ThreadDeletedPayload} identifying
300
+ * the deleted thread.
301
+ * @returns A function that removes this listener when called.
302
+ */
303
+ onThreadDeleted(
304
+ callback: (params: ThreadDeletedPayload) => void,
305
+ ): () => void {
306
+ this.#threadDeletedListeners.add(callback);
307
+ return () => {
308
+ this.#threadDeletedListeners.delete(callback);
309
+ };
310
+ }
311
+
312
+ ɵgetApiUrl(): string {
313
+ return this.#apiUrl;
314
+ }
315
+
316
+ ɵgetRunnerWsUrl(): string {
317
+ return this.#runnerWsUrl;
318
+ }
319
+
320
+ ɵgetClientWsUrl(): string {
321
+ return this.#clientWsUrl;
322
+ }
323
+
324
+ ɵgetOrganizationId(): string {
325
+ return this.#organizationId;
326
+ }
327
+
328
+ ɵgetRunnerAuthToken(): string {
329
+ return this.#apiKey;
330
+ }
331
+
332
+ async #request<T>(method: string, path: string, body?: unknown): Promise<T> {
333
+ const url = `${this.#apiUrl}${path}`;
334
+
335
+ const headers: Record<string, string> = {
336
+ Authorization: `Bearer ${this.#apiKey}`,
337
+ "Content-Type": "application/json",
338
+ "X-Organization-Id": this.#organizationId,
339
+ };
340
+
341
+ const response = await fetch(url, {
342
+ method,
343
+ headers,
344
+ body: body ? JSON.stringify(body) : undefined,
345
+ });
346
+
347
+ if (!response.ok) {
348
+ const text = await response.text().catch(() => "");
349
+ logger.error(
350
+ { status: response.status, body: text, path },
351
+ "Intelligence platform request failed",
352
+ );
353
+ throw new PlatformRequestError(
354
+ `Intelligence platform error ${response.status}: ${text || response.statusText}`,
355
+ response.status,
356
+ );
357
+ }
358
+
359
+ const text = await response.text();
360
+ if (!text) {
361
+ return undefined as T;
362
+ }
363
+ return JSON.parse(text) as T;
364
+ }
365
+
366
+ #invokeLifecycleCallback(
367
+ callbackName: "onThreadCreated" | "onThreadUpdated" | "onThreadDeleted",
368
+ payload: ThreadSummary | ThreadDeletedPayload,
369
+ ): void {
370
+ const listeners =
371
+ callbackName === "onThreadCreated"
372
+ ? this.#threadCreatedListeners
373
+ : callbackName === "onThreadUpdated"
374
+ ? this.#threadUpdatedListeners
375
+ : this.#threadDeletedListeners;
376
+
377
+ for (const callback of listeners) {
378
+ try {
379
+ void (callback as (p: typeof payload) => void)(payload);
380
+ } catch (error) {
381
+ logger.error(
382
+ { err: error, callbackName, payload },
383
+ "Intelligence lifecycle callback failed",
384
+ );
385
+ }
386
+ }
387
+ }
388
+
389
+ /**
390
+ * List all non-archived threads for a given user and agent.
391
+ *
392
+ * @param params.userId - User whose threads to list.
393
+ * @param params.agentId - Agent whose threads to list.
394
+ * @returns The thread list along with realtime subscription credentials.
395
+ * @throws {@link PlatformRequestError} on non-2xx responses.
396
+ */
397
+ async listThreads(params: {
398
+ userId: string;
399
+ agentId: string;
400
+ includeArchived?: boolean;
401
+ limit?: number;
402
+ cursor?: string;
403
+ }): Promise<ListThreadsResponse> {
404
+ const query: Record<string, string> = {
405
+ userId: params.userId,
406
+ agentId: params.agentId,
407
+ };
408
+ if (params.includeArchived) query.includeArchived = "true";
409
+ if (params.limit != null) query.limit = String(params.limit);
410
+ if (params.cursor) query.cursor = params.cursor;
411
+
412
+ const qs = new URLSearchParams(query).toString();
413
+ return this.#request<ListThreadsResponse>("GET", `/api/threads?${qs}`);
414
+ }
415
+
416
+ async ɵsubscribeToThreads(
417
+ params: SubscribeToThreadsRequest,
418
+ ): Promise<SubscribeToThreadsResponse> {
419
+ return this.#request<SubscribeToThreadsResponse>(
420
+ "POST",
421
+ "/api/threads/subscribe",
422
+ {
423
+ userId: params.userId,
424
+ },
425
+ );
426
+ }
427
+
428
+ /**
429
+ * Update thread metadata (e.g. name).
430
+ *
431
+ * Triggers the `onThreadUpdated` lifecycle callback on success.
432
+ *
433
+ * @returns The updated thread summary.
434
+ * @throws {@link PlatformRequestError} on non-2xx responses.
435
+ */
436
+ async updateThread(params: {
437
+ threadId: string;
438
+ userId: string;
439
+ agentId: string;
440
+ updates: UpdateThreadRequest;
441
+ }): Promise<ThreadSummary> {
442
+ const response = await this.#request<ThreadEnvelope>(
443
+ "PATCH",
444
+ `/api/threads/${encodeURIComponent(params.threadId)}`,
445
+ {
446
+ userId: params.userId,
447
+ agentId: params.agentId,
448
+ ...params.updates,
449
+ },
450
+ );
451
+ this.#invokeLifecycleCallback("onThreadUpdated", response.thread);
452
+ return response.thread;
453
+ }
454
+
455
+ /**
456
+ * Create a new thread on the platform.
457
+ *
458
+ * Triggers the `onThreadCreated` lifecycle callback on success.
459
+ *
460
+ * @returns The newly created thread summary.
461
+ * @throws {@link PlatformRequestError} with status 409 if a thread with the
462
+ * same `threadId` already exists.
463
+ */
464
+ async createThread(params: CreateThreadRequest): Promise<ThreadSummary> {
465
+ const response = await this.#request<ThreadEnvelope>(
466
+ "POST",
467
+ `/api/threads`,
468
+ {
469
+ threadId: params.threadId,
470
+ userId: params.userId,
471
+ agentId: params.agentId,
472
+ ...(params.name !== undefined ? { name: params.name } : {}),
473
+ },
474
+ );
475
+ this.#invokeLifecycleCallback("onThreadCreated", response.thread);
476
+ return response.thread;
477
+ }
478
+
479
+ /**
480
+ * Fetch a single thread by ID.
481
+ *
482
+ * @returns The thread summary.
483
+ * @throws {@link PlatformRequestError} with status 404 if the thread does
484
+ * not exist.
485
+ */
486
+ async getThread(params: { threadId: string }): Promise<ThreadSummary> {
487
+ const response = await this.#request<ThreadEnvelope>(
488
+ "GET",
489
+ `/api/threads/${encodeURIComponent(params.threadId)}`,
490
+ );
491
+ return response.thread;
492
+ }
493
+
494
+ /**
495
+ * Get an existing thread or create it if it does not exist.
496
+ *
497
+ * Handles the race where a concurrent request creates the thread between
498
+ * the initial 404 and the subsequent `createThread` call by catching the
499
+ * 409 Conflict and retrying the get.
500
+ *
501
+ * Triggers the `onThreadCreated` lifecycle callback when a new thread is
502
+ * created.
503
+ *
504
+ * @returns An object containing the thread and a `created` flag indicating
505
+ * whether the thread was newly created (`true`) or already existed (`false`).
506
+ * @throws {@link PlatformRequestError} on non-2xx responses other than
507
+ * 404 (get) and 409 (create race).
508
+ */
509
+ async getOrCreateThread(
510
+ params: CreateThreadRequest,
511
+ ): Promise<{ thread: ThreadSummary; created: boolean }> {
512
+ try {
513
+ const thread = await this.getThread({ threadId: params.threadId });
514
+ return { thread, created: false };
515
+ } catch (error) {
516
+ if (!(error instanceof PlatformRequestError && error.status === 404)) {
517
+ throw error;
518
+ }
519
+ }
520
+
521
+ try {
522
+ const thread = await this.createThread(params);
523
+ return { thread, created: true };
524
+ } catch (error) {
525
+ // Another request created the thread between our get and create — retry get.
526
+ if (error instanceof PlatformRequestError && error.status === 409) {
527
+ const thread = await this.getThread({ threadId: params.threadId });
528
+ return { thread, created: false };
529
+ }
530
+ throw error;
531
+ }
532
+ }
533
+
534
+ /**
535
+ * Fetch the full message history for a thread.
536
+ *
537
+ * @returns All persisted messages in chronological order.
538
+ * @throws {@link PlatformRequestError} on non-2xx responses.
539
+ */
540
+ async getThreadMessages(params: {
541
+ threadId: string;
542
+ }): Promise<ThreadMessagesResponse> {
543
+ return this.#request<ThreadMessagesResponse>(
544
+ "GET",
545
+ `/api/threads/${encodeURIComponent(params.threadId)}/messages`,
546
+ );
547
+ }
548
+
549
+ /**
550
+ * Mark a thread as archived.
551
+ *
552
+ * Archived threads are excluded from {@link listThreads} results.
553
+ * Triggers the `onThreadUpdated` lifecycle callback on success.
554
+ *
555
+ * @throws {@link PlatformRequestError} on non-2xx responses.
556
+ */
557
+ async archiveThread(params: {
558
+ threadId: string;
559
+ userId: string;
560
+ agentId: string;
561
+ }): Promise<void> {
562
+ const response = await this.#request<ThreadEnvelope>(
563
+ "PATCH",
564
+ `/api/threads/${encodeURIComponent(params.threadId)}`,
565
+ { userId: params.userId, agentId: params.agentId, archived: true },
566
+ );
567
+ this.#invokeLifecycleCallback("onThreadUpdated", response.thread);
568
+ }
569
+
570
+ /**
571
+ * Permanently delete a thread and its message history.
572
+ *
573
+ * This is irreversible. Triggers the `onThreadDeleted` lifecycle callback
574
+ * on success.
575
+ *
576
+ * @throws {@link PlatformRequestError} on non-2xx responses.
577
+ */
578
+ async deleteThread(params: {
579
+ threadId: string;
580
+ userId: string;
581
+ agentId: string;
582
+ }): Promise<void> {
583
+ await this.#request<void>(
584
+ "DELETE",
585
+ `/api/threads/${encodeURIComponent(params.threadId)}`,
586
+ {
587
+ reason: `Deleted via CopilotKit runtime (userId=${params.userId}, agentId=${params.agentId})`,
588
+ },
589
+ );
590
+ this.#invokeLifecycleCallback("onThreadDeleted", params);
591
+ }
592
+
593
+ async ɵacquireThreadLock(
594
+ params: AcquireThreadLockRequest,
595
+ ): Promise<ThreadConnectionResponse> {
596
+ return this.#request<ThreadConnectionResponse>(
597
+ "POST",
598
+ `/api/threads/${encodeURIComponent(params.threadId)}/lock`,
599
+ { runId: params.runId, userId: params.userId },
600
+ );
601
+ }
602
+
603
+ async ɵgetActiveJoinCode(params: {
604
+ threadId: string;
605
+ userId: string;
606
+ }): Promise<ThreadConnectionResponse> {
607
+ const qs = new URLSearchParams({ userId: params.userId }).toString();
608
+ return this.#request<ThreadConnectionResponse>(
609
+ "GET",
610
+ `/api/threads/${encodeURIComponent(params.threadId)}/join-code?${qs}`,
611
+ );
612
+ }
613
+
614
+ async ɵconnectThread(params: {
615
+ threadId: string;
616
+ userId: string;
617
+ lastSeenEventId?: string | null;
618
+ }): Promise<ConnectThreadResponse> {
619
+ const result = await this.#request<
620
+ ConnectThreadBootstrapResponse | ConnectThreadLiveResponse
621
+ >("POST", `/api/threads/${encodeURIComponent(params.threadId)}/connect`, {
622
+ userId: params.userId,
623
+ ...(params.lastSeenEventId !== undefined
624
+ ? { lastSeenEventId: params.lastSeenEventId }
625
+ : {}),
626
+ });
627
+
628
+ // request() returns undefined for empty/204 responses
629
+ return result ?? null;
630
+ }
631
+ }
632
+
633
+ function normalizeIntelligenceWsUrl(wsUrl: string): string {
634
+ return wsUrl.replace(/\/$/, "");
635
+ }
636
+
637
+ function deriveRunnerWsUrl(wsUrl: string): string {
638
+ if (wsUrl.endsWith("/runner")) {
639
+ return wsUrl;
640
+ }
641
+
642
+ if (wsUrl.endsWith("/client")) {
643
+ return `${wsUrl.slice(0, -"/client".length)}/runner`;
644
+ }
645
+
646
+ return `${wsUrl}/runner`;
647
+ }
648
+
649
+ function deriveClientWsUrl(wsUrl: string): string {
650
+ if (wsUrl.endsWith("/client")) {
651
+ return wsUrl;
652
+ }
653
+
654
+ if (wsUrl.endsWith("/runner")) {
655
+ return `${wsUrl.slice(0, -"/runner".length)}/client`;
656
+ }
657
+
658
+ return `${wsUrl}/client`;
659
+ }
@@ -0,0 +1,10 @@
1
+ export {
2
+ CopilotKitIntelligence,
3
+ type CopilotKitIntelligenceConfig,
4
+ type CreateThreadRequest,
5
+ type ThreadSummary,
6
+ type ListThreadsResponse,
7
+ type SubscribeToThreadsRequest,
8
+ type SubscribeToThreadsResponse,
9
+ type UpdateThreadRequest,
10
+ } from "./client";