@copilotkit/runtime 1.55.0-next.9 → 1.55.0

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 (345) hide show
  1. package/CHANGELOG.md +24 -2
  2. package/dist/agent/index.cjs +101 -12
  3. package/dist/agent/index.cjs.map +1 -1
  4. package/dist/agent/index.d.cts.map +1 -1
  5. package/dist/agent/index.d.mts.map +1 -1
  6. package/dist/agent/index.mjs +102 -13
  7. package/dist/agent/index.mjs.map +1 -1
  8. package/dist/lib/runtime/agent-integrations/langgraph/agent.cjs.map +1 -1
  9. package/dist/lib/runtime/agent-integrations/langgraph/agent.d.cts +4 -841
  10. package/dist/lib/runtime/agent-integrations/langgraph/agent.d.cts.map +1 -1
  11. package/dist/lib/runtime/agent-integrations/langgraph/agent.d.mts +4 -841
  12. package/dist/lib/runtime/agent-integrations/langgraph/agent.d.mts.map +1 -1
  13. package/dist/lib/runtime/agent-integrations/langgraph/agent.mjs.map +1 -1
  14. package/dist/lib/runtime/copilot-runtime.cjs +3 -2
  15. package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
  16. package/dist/lib/runtime/copilot-runtime.d.cts +1 -1
  17. package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
  18. package/dist/lib/runtime/copilot-runtime.d.mts +3 -3
  19. package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
  20. package/dist/lib/runtime/copilot-runtime.mjs +3 -2
  21. package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
  22. package/dist/package.cjs +70 -47
  23. package/dist/package.mjs +70 -47
  24. package/dist/v2/express.cjs +8 -0
  25. package/dist/v2/express.d.cts +5 -0
  26. package/dist/v2/express.d.mts +5 -0
  27. package/dist/v2/express.mjs +5 -0
  28. package/dist/v2/hono.cjs +9 -0
  29. package/dist/v2/hono.d.cts +5 -0
  30. package/dist/v2/hono.d.mts +5 -0
  31. package/dist/v2/hono.mjs +5 -0
  32. package/dist/v2/index.cjs +8 -3
  33. package/dist/v2/index.d.cts +8 -5
  34. package/dist/v2/index.d.mts +8 -5
  35. package/dist/v2/index.mjs +5 -4
  36. package/dist/v2/node.cjs +8 -0
  37. package/dist/v2/node.d.cts +5 -0
  38. package/dist/v2/node.d.mts +5 -0
  39. package/dist/v2/node.mjs +5 -0
  40. package/dist/v2/runtime/core/fetch-cors.cjs +72 -0
  41. package/dist/v2/runtime/core/fetch-cors.cjs.map +1 -0
  42. package/dist/v2/runtime/core/fetch-cors.d.cts +20 -0
  43. package/dist/v2/runtime/core/fetch-cors.d.cts.map +1 -0
  44. package/dist/v2/runtime/core/fetch-cors.d.mts +20 -0
  45. package/dist/v2/runtime/core/fetch-cors.d.mts.map +1 -0
  46. package/dist/v2/runtime/core/fetch-cors.mjs +70 -0
  47. package/dist/v2/runtime/core/fetch-cors.mjs.map +1 -0
  48. package/dist/v2/runtime/core/fetch-handler.cjs +232 -0
  49. package/dist/v2/runtime/core/fetch-handler.cjs.map +1 -0
  50. package/dist/v2/runtime/core/fetch-handler.d.cts +40 -0
  51. package/dist/v2/runtime/core/fetch-handler.d.cts.map +1 -0
  52. package/dist/v2/runtime/core/fetch-handler.d.mts +40 -0
  53. package/dist/v2/runtime/core/fetch-handler.d.mts.map +1 -0
  54. package/dist/v2/runtime/core/fetch-handler.mjs +231 -0
  55. package/dist/v2/runtime/core/fetch-handler.mjs.map +1 -0
  56. package/dist/v2/runtime/core/fetch-router.cjs +68 -0
  57. package/dist/v2/runtime/core/fetch-router.cjs.map +1 -0
  58. package/dist/v2/runtime/core/fetch-router.mjs +67 -0
  59. package/dist/v2/runtime/core/fetch-router.mjs.map +1 -0
  60. package/dist/v2/runtime/core/hooks.cjs +29 -0
  61. package/dist/v2/runtime/core/hooks.cjs.map +1 -0
  62. package/dist/v2/runtime/core/hooks.d.cts +78 -0
  63. package/dist/v2/runtime/core/hooks.d.cts.map +1 -0
  64. package/dist/v2/runtime/core/hooks.d.mts +78 -0
  65. package/dist/v2/runtime/core/hooks.d.mts.map +1 -0
  66. package/dist/v2/runtime/core/hooks.mjs +25 -0
  67. package/dist/v2/runtime/core/hooks.mjs.map +1 -0
  68. package/dist/v2/runtime/{middleware-sse-parser.cjs → core/middleware-sse-parser.cjs} +2 -2
  69. package/dist/v2/runtime/core/middleware-sse-parser.cjs.map +1 -0
  70. package/dist/v2/runtime/{middleware-sse-parser.d.cts → core/middleware-sse-parser.d.cts} +1 -1
  71. package/dist/v2/runtime/core/middleware-sse-parser.d.cts.map +1 -0
  72. package/dist/v2/runtime/{middleware-sse-parser.d.mts → core/middleware-sse-parser.d.mts} +1 -1
  73. package/dist/v2/runtime/core/middleware-sse-parser.d.mts.map +1 -0
  74. package/dist/v2/runtime/{middleware-sse-parser.mjs → core/middleware-sse-parser.mjs} +1 -1
  75. package/dist/v2/runtime/core/middleware-sse-parser.mjs.map +1 -0
  76. package/dist/v2/runtime/{middleware.cjs → core/middleware.cjs} +2 -2
  77. package/dist/v2/runtime/core/middleware.cjs.map +1 -0
  78. package/dist/v2/runtime/{middleware.d.cts → core/middleware.d.cts} +1 -1
  79. package/dist/v2/runtime/core/middleware.d.cts.map +1 -0
  80. package/dist/v2/runtime/{middleware.d.mts → core/middleware.d.mts} +1 -1
  81. package/dist/v2/runtime/core/middleware.d.mts.map +1 -0
  82. package/dist/v2/runtime/{middleware.mjs → core/middleware.mjs} +1 -1
  83. package/dist/v2/runtime/core/middleware.mjs.map +1 -0
  84. package/dist/v2/runtime/{runtime.cjs → core/runtime.cjs} +35 -10
  85. package/dist/v2/runtime/core/runtime.cjs.map +1 -0
  86. package/dist/v2/runtime/{runtime.d.cts → core/runtime.d.cts} +41 -7
  87. package/dist/v2/runtime/core/runtime.d.cts.map +1 -0
  88. package/dist/v2/runtime/{runtime.d.mts → core/runtime.d.mts} +42 -8
  89. package/dist/v2/runtime/core/runtime.d.mts.map +1 -0
  90. package/dist/v2/runtime/{runtime.mjs → core/runtime.mjs} +36 -11
  91. package/dist/v2/runtime/core/runtime.mjs.map +1 -0
  92. package/dist/v2/runtime/endpoints/express-fetch-bridge.cjs +83 -0
  93. package/dist/v2/runtime/endpoints/express-fetch-bridge.cjs.map +1 -0
  94. package/dist/v2/runtime/endpoints/express-fetch-bridge.mjs +82 -0
  95. package/dist/v2/runtime/endpoints/express-fetch-bridge.mjs.map +1 -0
  96. package/dist/v2/runtime/endpoints/express-single.cjs +35 -181
  97. package/dist/v2/runtime/endpoints/express-single.cjs.map +1 -1
  98. package/dist/v2/runtime/endpoints/express-single.d.cts +35 -2
  99. package/dist/v2/runtime/endpoints/express-single.d.cts.map +1 -1
  100. package/dist/v2/runtime/endpoints/express-single.d.mts +35 -2
  101. package/dist/v2/runtime/endpoints/express-single.d.mts.map +1 -1
  102. package/dist/v2/runtime/endpoints/express-single.mjs +35 -178
  103. package/dist/v2/runtime/endpoints/express-single.mjs.map +1 -1
  104. package/dist/v2/runtime/endpoints/express.cjs +41 -195
  105. package/dist/v2/runtime/endpoints/express.cjs.map +1 -1
  106. package/dist/v2/runtime/endpoints/express.d.cts +26 -4
  107. package/dist/v2/runtime/endpoints/express.d.cts.map +1 -1
  108. package/dist/v2/runtime/endpoints/express.d.mts +26 -4
  109. package/dist/v2/runtime/endpoints/express.d.mts.map +1 -1
  110. package/dist/v2/runtime/endpoints/express.mjs +41 -195
  111. package/dist/v2/runtime/endpoints/express.mjs.map +1 -1
  112. package/dist/v2/runtime/endpoints/hono-single.cjs +11 -123
  113. package/dist/v2/runtime/endpoints/hono-single.cjs.map +1 -1
  114. package/dist/v2/runtime/endpoints/hono-single.d.cts +14 -11
  115. package/dist/v2/runtime/endpoints/hono-single.d.cts.map +1 -1
  116. package/dist/v2/runtime/endpoints/hono-single.d.mts +14 -11
  117. package/dist/v2/runtime/endpoints/hono-single.d.mts.map +1 -1
  118. package/dist/v2/runtime/endpoints/hono-single.mjs +11 -123
  119. package/dist/v2/runtime/endpoints/hono-single.mjs.map +1 -1
  120. package/dist/v2/runtime/endpoints/hono.cjs +23 -237
  121. package/dist/v2/runtime/endpoints/hono.cjs.map +1 -1
  122. package/dist/v2/runtime/endpoints/hono.d.cts +29 -120
  123. package/dist/v2/runtime/endpoints/hono.d.cts.map +1 -1
  124. package/dist/v2/runtime/endpoints/hono.d.mts +29 -120
  125. package/dist/v2/runtime/endpoints/hono.d.mts.map +1 -1
  126. package/dist/v2/runtime/endpoints/hono.mjs +22 -238
  127. package/dist/v2/runtime/endpoints/hono.mjs.map +1 -1
  128. package/dist/v2/runtime/endpoints/index.d.cts +2 -2
  129. package/dist/v2/runtime/endpoints/index.d.mts +2 -2
  130. package/dist/v2/runtime/endpoints/node-fetch-handler.cjs +26 -0
  131. package/dist/v2/runtime/endpoints/node-fetch-handler.cjs.map +1 -0
  132. package/dist/v2/runtime/endpoints/node-fetch-handler.d.cts +12 -0
  133. package/dist/v2/runtime/endpoints/node-fetch-handler.d.cts.map +1 -0
  134. package/dist/v2/runtime/endpoints/node-fetch-handler.d.mts +12 -0
  135. package/dist/v2/runtime/endpoints/node-fetch-handler.d.mts.map +1 -0
  136. package/dist/v2/runtime/endpoints/node-fetch-handler.mjs +24 -0
  137. package/dist/v2/runtime/endpoints/node-fetch-handler.mjs.map +1 -0
  138. package/dist/v2/runtime/endpoints/node.cjs +30 -0
  139. package/dist/v2/runtime/endpoints/node.cjs.map +1 -0
  140. package/dist/v2/runtime/endpoints/node.d.cts +27 -0
  141. package/dist/v2/runtime/endpoints/node.d.cts.map +1 -0
  142. package/dist/v2/runtime/endpoints/node.d.mts +27 -0
  143. package/dist/v2/runtime/endpoints/node.d.mts.map +1 -0
  144. package/dist/v2/runtime/endpoints/node.mjs +30 -0
  145. package/dist/v2/runtime/endpoints/node.mjs.map +1 -0
  146. package/dist/v2/runtime/express.d.cts +3 -0
  147. package/dist/v2/runtime/express.d.mts +3 -0
  148. package/dist/v2/runtime/handlers/get-runtime-info.cjs +2 -1
  149. package/dist/v2/runtime/handlers/get-runtime-info.cjs.map +1 -1
  150. package/dist/v2/runtime/handlers/get-runtime-info.mjs +2 -1
  151. package/dist/v2/runtime/handlers/get-runtime-info.mjs.map +1 -1
  152. package/dist/v2/runtime/handlers/handle-connect.cjs +6 -3
  153. package/dist/v2/runtime/handlers/handle-connect.cjs.map +1 -1
  154. package/dist/v2/runtime/handlers/handle-connect.mjs +6 -3
  155. package/dist/v2/runtime/handlers/handle-connect.mjs.map +1 -1
  156. package/dist/v2/runtime/handlers/handle-run.cjs +6 -3
  157. package/dist/v2/runtime/handlers/handle-run.cjs.map +1 -1
  158. package/dist/v2/runtime/handlers/handle-run.mjs +6 -3
  159. package/dist/v2/runtime/handlers/handle-run.mjs.map +1 -1
  160. package/dist/v2/runtime/handlers/handle-stop.cjs.map +1 -1
  161. package/dist/v2/runtime/handlers/handle-stop.mjs.map +1 -1
  162. package/dist/v2/runtime/handlers/handle-transcribe.cjs.map +1 -1
  163. package/dist/v2/runtime/handlers/handle-transcribe.mjs.map +1 -1
  164. package/dist/v2/runtime/handlers/intelligence/connect.cjs.map +1 -1
  165. package/dist/v2/runtime/handlers/intelligence/connect.mjs.map +1 -1
  166. package/dist/v2/runtime/handlers/intelligence/run.cjs +22 -1
  167. package/dist/v2/runtime/handlers/intelligence/run.cjs.map +1 -1
  168. package/dist/v2/runtime/handlers/intelligence/run.mjs +22 -1
  169. package/dist/v2/runtime/handlers/intelligence/run.mjs.map +1 -1
  170. package/dist/v2/runtime/handlers/intelligence/thread-names.cjs +1 -1
  171. package/dist/v2/runtime/handlers/intelligence/thread-names.cjs.map +1 -1
  172. package/dist/v2/runtime/handlers/intelligence/thread-names.mjs +1 -1
  173. package/dist/v2/runtime/handlers/intelligence/thread-names.mjs.map +1 -1
  174. package/dist/v2/runtime/handlers/shared/agent-utils.cjs +21 -6
  175. package/dist/v2/runtime/handlers/shared/agent-utils.cjs.map +1 -1
  176. package/dist/v2/runtime/handlers/shared/agent-utils.mjs +21 -6
  177. package/dist/v2/runtime/handlers/shared/agent-utils.mjs.map +1 -1
  178. package/dist/v2/runtime/handlers/shared/json-response.cjs +4 -1
  179. package/dist/v2/runtime/handlers/shared/json-response.cjs.map +1 -1
  180. package/dist/v2/runtime/handlers/shared/json-response.mjs +4 -1
  181. package/dist/v2/runtime/handlers/shared/json-response.mjs.map +1 -1
  182. package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.cjs.map +1 -1
  183. package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.mjs.map +1 -1
  184. package/dist/v2/runtime/handlers/sse/connect.cjs.map +1 -1
  185. package/dist/v2/runtime/handlers/sse/connect.mjs.map +1 -1
  186. package/dist/v2/runtime/handlers/sse/run.cjs.map +1 -1
  187. package/dist/v2/runtime/handlers/sse/run.mjs.map +1 -1
  188. package/dist/v2/runtime/hono.d.cts +3 -0
  189. package/dist/v2/runtime/hono.d.mts +3 -0
  190. package/dist/v2/runtime/index.d.cts +16 -4
  191. package/dist/v2/runtime/index.d.cts.map +1 -0
  192. package/dist/v2/runtime/index.d.mts +16 -4
  193. package/dist/v2/runtime/index.d.mts.map +1 -0
  194. package/dist/v2/runtime/intelligence-platform/client.cjs +10 -1
  195. package/dist/v2/runtime/intelligence-platform/client.cjs.map +1 -1
  196. package/dist/v2/runtime/intelligence-platform/client.d.cts +22 -0
  197. package/dist/v2/runtime/intelligence-platform/client.d.cts.map +1 -1
  198. package/dist/v2/runtime/intelligence-platform/client.d.mts +22 -0
  199. package/dist/v2/runtime/intelligence-platform/client.d.mts.map +1 -1
  200. package/dist/v2/runtime/intelligence-platform/client.mjs +10 -1
  201. package/dist/v2/runtime/intelligence-platform/client.mjs.map +1 -1
  202. package/dist/v2/runtime/node.d.cts +3 -0
  203. package/dist/v2/runtime/node.d.mts +3 -0
  204. package/dist/v2/runtime/open-generative-ui-middleware.cjs +282 -0
  205. package/dist/v2/runtime/open-generative-ui-middleware.cjs.map +1 -0
  206. package/dist/v2/runtime/open-generative-ui-middleware.mjs +280 -0
  207. package/dist/v2/runtime/open-generative-ui-middleware.mjs.map +1 -0
  208. package/dist/v2/runtime/runner/intelligence.cjs +4 -4
  209. package/dist/v2/runtime/runner/intelligence.cjs.map +1 -1
  210. package/dist/v2/runtime/runner/intelligence.d.cts +6 -2
  211. package/dist/v2/runtime/runner/intelligence.d.cts.map +1 -1
  212. package/dist/v2/runtime/runner/intelligence.d.mts +6 -2
  213. package/dist/v2/runtime/runner/intelligence.d.mts.map +1 -1
  214. package/dist/v2/runtime/runner/intelligence.mjs +4 -4
  215. package/dist/v2/runtime/runner/intelligence.mjs.map +1 -1
  216. package/dist/v2/runtime/telemetry/telemetry-client.cjs +37 -0
  217. package/dist/v2/runtime/telemetry/telemetry-client.cjs.map +1 -1
  218. package/dist/v2/runtime/telemetry/telemetry-client.mjs +36 -0
  219. package/dist/v2/runtime/telemetry/telemetry-client.mjs.map +1 -1
  220. package/dist/v2/runtime/telemetry/utils.cjs +15 -0
  221. package/dist/v2/runtime/telemetry/utils.cjs.map +1 -0
  222. package/dist/v2/runtime/telemetry/utils.mjs +14 -0
  223. package/dist/v2/runtime/telemetry/utils.mjs.map +1 -0
  224. package/package.json +81 -48
  225. package/src/agent/__tests__/multimodal.test.ts +176 -0
  226. package/src/agent/index.ts +130 -19
  227. package/src/lib/runtime/agent-integrations/langgraph/agent.ts +3 -3
  228. package/src/lib/runtime/copilot-runtime.ts +1 -0
  229. package/src/v2/express.ts +1 -0
  230. package/src/v2/hono.ts +1 -0
  231. package/src/v2/node.ts +1 -0
  232. package/src/v2/runtime/__tests__/backward-compat.test.ts +261 -0
  233. package/src/v2/runtime/__tests__/code-review-fixes.test.ts +500 -0
  234. package/src/v2/runtime/__tests__/cors-credentials.test.ts +2 -2
  235. package/src/v2/runtime/__tests__/express-adapter.test.ts +188 -0
  236. package/src/v2/runtime/__tests__/express-body-order.test.ts +1 -1
  237. package/src/v2/runtime/__tests__/express-fetch-bridge.test.ts +344 -0
  238. package/src/v2/runtime/__tests__/express-single-sse.test.ts +1 -1
  239. package/src/v2/runtime/__tests__/fetch-cors.test.ts +205 -0
  240. package/src/v2/runtime/__tests__/fetch-handler-validation.test.ts +372 -0
  241. package/src/v2/runtime/__tests__/fetch-handler.test.ts +456 -0
  242. package/src/v2/runtime/__tests__/fetch-router.test.ts +132 -0
  243. package/src/v2/runtime/__tests__/get-runtime-info.test.ts +4 -1
  244. package/src/v2/runtime/__tests__/handle-connect.test.ts +15 -13
  245. package/src/v2/runtime/__tests__/handle-run.test.ts +21 -17
  246. package/src/v2/runtime/__tests__/handle-threads.test.ts +1 -1
  247. package/src/v2/runtime/__tests__/handle-transcribe.test.ts +1 -1
  248. package/src/v2/runtime/__tests__/hono-adapter.test.ts +150 -0
  249. package/src/v2/runtime/__tests__/hooks-edge-cases.test.ts +457 -0
  250. package/src/v2/runtime/__tests__/hooks.test.ts +557 -0
  251. package/src/v2/runtime/__tests__/integration/bun/bun-servers.integration.test.ts +27 -0
  252. package/src/v2/runtime/__tests__/integration/bun/elysia-multi.ts +32 -0
  253. package/src/v2/runtime/__tests__/integration/bun/elysia-single.ts +33 -0
  254. package/src/v2/runtime/__tests__/integration/bun/hono-bun-multi.ts +25 -0
  255. package/src/v2/runtime/__tests__/integration/bun/hono-bun-single.ts +32 -0
  256. package/src/v2/runtime/__tests__/integration/helpers/create-test-runtime.ts +15 -0
  257. package/src/v2/runtime/__tests__/integration/helpers/sse-reader.ts +45 -0
  258. package/src/v2/runtime/__tests__/integration/helpers/test-agent.ts +58 -0
  259. package/src/v2/runtime/__tests__/integration/node-servers.integration.test.ts +39 -0
  260. package/src/v2/runtime/__tests__/integration/servers/express-multi.ts +35 -0
  261. package/src/v2/runtime/__tests__/integration/servers/express-single.ts +36 -0
  262. package/src/v2/runtime/__tests__/integration/servers/fetch-direct.ts +39 -0
  263. package/src/v2/runtime/__tests__/integration/servers/hono-multi.ts +30 -0
  264. package/src/v2/runtime/__tests__/integration/servers/hono-single.ts +37 -0
  265. package/src/v2/runtime/__tests__/integration/servers/node-multi.ts +45 -0
  266. package/src/v2/runtime/__tests__/integration/servers/node-single.ts +46 -0
  267. package/src/v2/runtime/__tests__/integration/servers/types.ts +18 -0
  268. package/src/v2/runtime/__tests__/integration/suites/multi-endpoint.suite.ts +358 -0
  269. package/src/v2/runtime/__tests__/integration/suites/single-endpoint.suite.ts +363 -0
  270. package/src/v2/runtime/__tests__/middleware-express.test.ts +1 -1
  271. package/src/v2/runtime/__tests__/middleware-single-express.test.ts +1 -1
  272. package/src/v2/runtime/__tests__/middleware-single.test.ts +1 -1
  273. package/src/v2/runtime/__tests__/middleware-sse-parser.test.ts +1 -1
  274. package/src/v2/runtime/__tests__/middleware.test.ts +1 -2
  275. package/src/v2/runtime/__tests__/node-fetch-handler.test.ts +157 -0
  276. package/src/v2/runtime/__tests__/open-generative-ui-middleware.e2e.test.ts +728 -0
  277. package/src/v2/runtime/__tests__/router-edge-cases.test.ts +217 -0
  278. package/src/v2/runtime/__tests__/routing-express.test.ts +1 -1
  279. package/src/v2/runtime/__tests__/routing-single-express.test.ts +1 -1
  280. package/src/v2/runtime/__tests__/routing-single.test.ts +1 -1
  281. package/src/v2/runtime/__tests__/routing.test.ts +1 -1
  282. package/src/v2/runtime/__tests__/runtime.test.ts +110 -1
  283. package/src/v2/runtime/__tests__/telemetry.test.ts +62 -1
  284. package/src/v2/runtime/core/fetch-cors.ts +136 -0
  285. package/src/v2/runtime/core/fetch-handler.ts +415 -0
  286. package/src/v2/runtime/core/fetch-router.ts +112 -0
  287. package/src/v2/runtime/core/hooks.ts +151 -0
  288. package/src/v2/runtime/{runtime.ts → core/runtime.ts} +79 -10
  289. package/src/v2/runtime/endpoints/express-fetch-bridge.ts +137 -0
  290. package/src/v2/runtime/endpoints/express-single.ts +42 -219
  291. package/src/v2/runtime/endpoints/express.ts +128 -230
  292. package/src/v2/runtime/endpoints/hono-single.ts +19 -171
  293. package/src/v2/runtime/endpoints/hono.ts +45 -270
  294. package/src/v2/runtime/endpoints/node-fetch-handler.ts +48 -0
  295. package/src/v2/runtime/endpoints/node.ts +28 -0
  296. package/src/v2/runtime/handlers/get-runtime-info.ts +3 -2
  297. package/src/v2/runtime/handlers/handle-connect.ts +7 -4
  298. package/src/v2/runtime/handlers/handle-run.ts +7 -4
  299. package/src/v2/runtime/handlers/handle-stop.ts +1 -1
  300. package/src/v2/runtime/handlers/handle-transcribe.ts +1 -1
  301. package/src/v2/runtime/handlers/intelligence/connect.ts +1 -1
  302. package/src/v2/runtime/handlers/intelligence/run.ts +31 -1
  303. package/src/v2/runtime/handlers/intelligence/thread-names.ts +2 -2
  304. package/src/v2/runtime/handlers/intelligence/threads.ts +1 -1
  305. package/src/v2/runtime/handlers/shared/agent-utils.ts +29 -10
  306. package/src/v2/runtime/handlers/shared/json-response.ts +4 -1
  307. package/src/v2/runtime/handlers/shared/resolve-intelligence-user.ts +1 -1
  308. package/src/v2/runtime/handlers/sse/connect.ts +1 -1
  309. package/src/v2/runtime/handlers/sse/run.ts +1 -1
  310. package/src/v2/runtime/hono.ts +2 -0
  311. package/src/v2/runtime/index.ts +27 -1
  312. package/src/v2/runtime/intelligence-platform/client.ts +50 -1
  313. package/src/v2/runtime/node.ts +6 -0
  314. package/src/v2/runtime/open-generative-ui-middleware.ts +373 -0
  315. package/src/v2/runtime/runner/intelligence.ts +14 -4
  316. package/src/v2/runtime/telemetry/telemetry-client.ts +56 -0
  317. package/src/v2/runtime/telemetry/utils.ts +15 -0
  318. package/tsdown.config.ts +8 -1
  319. package/vitest.config.mjs +2 -5
  320. package/.eslintrc.js +0 -7
  321. package/dist/v2/runtime/endpoints/express-utils.cjs +0 -119
  322. package/dist/v2/runtime/endpoints/express-utils.cjs.map +0 -1
  323. package/dist/v2/runtime/endpoints/express-utils.mjs +0 -117
  324. package/dist/v2/runtime/endpoints/express-utils.mjs.map +0 -1
  325. package/dist/v2/runtime/handlers/intelligence/threads.cjs +0 -159
  326. package/dist/v2/runtime/handlers/intelligence/threads.cjs.map +0 -1
  327. package/dist/v2/runtime/handlers/intelligence/threads.mjs +0 -154
  328. package/dist/v2/runtime/handlers/intelligence/threads.mjs.map +0 -1
  329. package/dist/v2/runtime/middleware-sse-parser.cjs.map +0 -1
  330. package/dist/v2/runtime/middleware-sse-parser.d.cts.map +0 -1
  331. package/dist/v2/runtime/middleware-sse-parser.d.mts.map +0 -1
  332. package/dist/v2/runtime/middleware-sse-parser.mjs.map +0 -1
  333. package/dist/v2/runtime/middleware.cjs.map +0 -1
  334. package/dist/v2/runtime/middleware.d.cts.map +0 -1
  335. package/dist/v2/runtime/middleware.d.mts.map +0 -1
  336. package/dist/v2/runtime/middleware.mjs.map +0 -1
  337. package/dist/v2/runtime/runtime.cjs.map +0 -1
  338. package/dist/v2/runtime/runtime.d.cts.map +0 -1
  339. package/dist/v2/runtime/runtime.d.mts.map +0 -1
  340. package/dist/v2/runtime/runtime.mjs.map +0 -1
  341. package/src/v2/runtime/__tests__/express-abort-signal.test.ts +0 -25
  342. package/src/v2/runtime/endpoints/express-utils.ts +0 -182
  343. package/src/v2/runtime/handler.ts +0 -3
  344. /package/src/v2/runtime/{middleware-sse-parser.ts → core/middleware-sse-parser.ts} +0 -0
  345. /package/src/v2/runtime/{middleware.ts → core/middleware.ts} +0 -0
@@ -1,5 +1,8 @@
1
1
  export const errorResponse = (message: string, status: number) =>
2
- Response.json({ error: message }, { status });
2
+ new Response(JSON.stringify({ error: message }), {
3
+ status,
4
+ headers: { "Content-Type": "application/json" },
5
+ });
3
6
 
4
7
  export function isHandlerResponse(value: unknown): value is Response {
5
8
  return value instanceof Response;
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  CopilotIntelligenceRuntimeLike,
3
3
  CopilotRuntimeUser,
4
- } from "../../runtime";
4
+ } from "../../core/runtime";
5
5
  import { errorResponse } from "./json-response";
6
6
  import { isValidIdentifier } from "./intelligence-utils";
7
7
 
@@ -1,4 +1,4 @@
1
- import { CopilotRuntimeLike } from "../../runtime";
1
+ import { CopilotRuntimeLike } from "../../core/runtime";
2
2
  import { createSseEventResponse } from "../shared/sse-response";
3
3
  import { extractForwardableHeaders } from "../header-utils";
4
4
 
@@ -1,5 +1,5 @@
1
1
  import { AbstractAgent, RunAgentInput } from "@ag-ui/client";
2
- import { CopilotRuntimeLike } from "../../runtime";
2
+ import { CopilotRuntimeLike } from "../../core/runtime";
3
3
  import { createSseEventResponse } from "../shared/sse-response";
4
4
 
5
5
  interface HandleSseRunParams {
@@ -0,0 +1,2 @@
1
+ export * from "./endpoints/hono";
2
+ export * from "./endpoints/hono-single";
@@ -1,4 +1,4 @@
1
- export * from "./runtime";
1
+ export * from "./core/runtime";
2
2
  export * from "./endpoints";
3
3
 
4
4
  // Export agent runners and base types
@@ -18,3 +18,29 @@ export {
18
18
  type SubscribeToThreadsResponse,
19
19
  type UpdateThreadRequest,
20
20
  } from "./intelligence-platform";
21
+
22
+ // Export framework-agnostic fetch handler
23
+ export { createCopilotRuntimeHandler } from "./core/fetch-handler";
24
+ export type {
25
+ CopilotRuntimeHandlerOptions,
26
+ CopilotRuntimeFetchHandler,
27
+ } from "./core/fetch-handler";
28
+
29
+ // Export hook types
30
+ export type {
31
+ CopilotRuntimeHooks,
32
+ HookContext,
33
+ HandlerHookContext,
34
+ ResponseHookContext,
35
+ ErrorHookContext,
36
+ RouteInfo,
37
+ } from "./core/hooks";
38
+
39
+ // Export CORS config type
40
+ export type { CopilotCorsConfig } from "./core/fetch-cors";
41
+
42
+ // Deprecated type aliases for backward compatibility
43
+ /** @deprecated Use `CopilotRuntimeFetchHandler` instead. Note: the new type takes `Request` directly, not `{ request: Request }`. */
44
+ export type CopilotKitRequestHandler = (params: {
45
+ request: Request;
46
+ }) => Promise<Response>;
@@ -157,6 +157,8 @@ export interface ThreadConnectionResponse {
157
157
  joinToken: string;
158
158
  /** Optional join code that can be shared with other clients to join the same channel. */
159
159
  joinCode?: string;
160
+ /** Lock metadata echoed back by the platform. */
161
+ lock?: ThreadLockInfo;
160
162
  }
161
163
 
162
164
  export interface SubscribeToThreadsRequest {
@@ -213,6 +215,28 @@ export interface AcquireThreadLockRequest {
213
215
  threadId: string;
214
216
  runId: string;
215
217
  userId: string;
218
+ /** Custom Redis key prefix for the lock (default: "thread"). */
219
+ lockKeyPrefix?: string;
220
+ /** Lock TTL in seconds. When set, the lock auto-expires after this duration. */
221
+ ttlSeconds?: number;
222
+ }
223
+
224
+ export interface RenewThreadLockRequest {
225
+ threadId: string;
226
+ runId: string;
227
+ /** New TTL to set on the lock in seconds. */
228
+ ttlSeconds: number;
229
+ /** Must match the prefix used when acquiring. */
230
+ lockKeyPrefix?: string;
231
+ }
232
+
233
+ export interface RenewThreadLockResponse {
234
+ ttlSeconds: number;
235
+ }
236
+
237
+ export interface ThreadLockInfo {
238
+ key: string;
239
+ ttlSeconds: number | null;
216
240
  }
217
241
 
218
242
  interface ThreadEnvelope {
@@ -596,7 +620,32 @@ export class CopilotKitIntelligence {
596
620
  return this.#request<ThreadConnectionResponse>(
597
621
  "POST",
598
622
  `/api/threads/${encodeURIComponent(params.threadId)}/lock`,
599
- { runId: params.runId, userId: params.userId },
623
+ {
624
+ runId: params.runId,
625
+ userId: params.userId,
626
+ ...(params.lockKeyPrefix !== undefined
627
+ ? { lockKeyPrefix: params.lockKeyPrefix }
628
+ : {}),
629
+ ...(params.ttlSeconds !== undefined
630
+ ? { ttlSeconds: params.ttlSeconds }
631
+ : {}),
632
+ },
633
+ );
634
+ }
635
+
636
+ async ɵrenewThreadLock(
637
+ params: RenewThreadLockRequest,
638
+ ): Promise<RenewThreadLockResponse> {
639
+ return this.#request<RenewThreadLockResponse>(
640
+ "PATCH",
641
+ `/api/threads/${encodeURIComponent(params.threadId)}/lock`,
642
+ {
643
+ runId: params.runId,
644
+ ttlSeconds: params.ttlSeconds,
645
+ ...(params.lockKeyPrefix !== undefined
646
+ ? { lockKeyPrefix: params.lockKeyPrefix }
647
+ : {}),
648
+ },
600
649
  );
601
650
  }
602
651
 
@@ -0,0 +1,6 @@
1
+ export { createCopilotNodeListener } from "./endpoints/node";
2
+ export {
3
+ createCopilotNodeHandler,
4
+ createNodeFetchHandler,
5
+ } from "./endpoints/node-fetch-handler";
6
+ export type { NodeFetchHandler } from "./endpoints/node-fetch-handler";
@@ -0,0 +1,373 @@
1
+ import {
2
+ Middleware,
3
+ RunAgentInput,
4
+ AbstractAgent,
5
+ BaseEvent,
6
+ EventType,
7
+ ToolCallStartEvent,
8
+ ToolCallArgsEvent,
9
+ ActivitySnapshotEvent,
10
+ ActivityDeltaEvent,
11
+ } from "@ag-ui/client";
12
+ import { Observable } from "rxjs";
13
+ import * as clarinet from "clarinet";
14
+
15
+ const TOOL_NAME = "generateSandboxedUi";
16
+ const ACTIVITY_TYPE = "open-generative-ui";
17
+
18
+ /**
19
+ * Parsed parameters from the generateSandboxedUi tool call.
20
+ */
21
+ export interface GenerateSandboxedUIParams {
22
+ initialHeight?: number;
23
+ placeholderMessages?: string[];
24
+ css?: string;
25
+ html?: string;
26
+ jsFunctions?: string;
27
+ jsExpressions?: string[];
28
+ }
29
+
30
+ /**
31
+ * Callback invoked by ArgsParser whenever a parameter (or array item) finishes parsing.
32
+ */
33
+ export type OnParamEvent = (event: BaseEvent) => void;
34
+
35
+ /**
36
+ * Tracks incremental JSON parsing state for a single tool call's arguments.
37
+ * Emits activity events via the onEvent callback as parameters complete.
38
+ */
39
+ export class ArgsParser {
40
+ private parser: ReturnType<typeof clarinet.parser>;
41
+ private currentKey: string | null = null;
42
+ private depth = 0;
43
+ private currentArrayKey: string | null = null;
44
+ private snapshotEmitted = false;
45
+
46
+ // Streaming html state — reads parser.textNode to emit incremental chunks
47
+ private streamingHtmlKey = false;
48
+ private htmlEmittedLength = 0;
49
+ private htmlArrayEmitted = false;
50
+
51
+ public readonly params: GenerateSandboxedUIParams = {};
52
+ public readonly messageId: string;
53
+ private readonly onEvent: OnParamEvent;
54
+
55
+ constructor(toolCallId: string, onEvent: OnParamEvent) {
56
+ this.messageId = `${toolCallId}-activity`;
57
+ this.onEvent = onEvent;
58
+ this.parser = clarinet.parser();
59
+
60
+ this.parser.onopenobject = (key: string | undefined) => {
61
+ this.depth++;
62
+ if (key !== undefined && this.depth === 1) {
63
+ this.currentKey = key;
64
+ this.initHtmlStreaming(key);
65
+ }
66
+ };
67
+
68
+ this.parser.onkey = (key: string) => {
69
+ if (this.depth === 1) {
70
+ this.currentKey = key;
71
+ this.initHtmlStreaming(key);
72
+ }
73
+ };
74
+
75
+ this.parser.onvalue = (value: string | boolean | number | null) => {
76
+ if (this.depth === 1 && this.currentKey) {
77
+ if (this.currentArrayKey) {
78
+ const strValue = String(value);
79
+ if (this.currentArrayKey === "jsExpressions") {
80
+ if (!this.params.jsExpressions) this.params.jsExpressions = [];
81
+ this.params.jsExpressions.push(strValue);
82
+ } else if (this.currentArrayKey === "placeholderMessages") {
83
+ if (!this.params.placeholderMessages)
84
+ this.params.placeholderMessages = [];
85
+ this.params.placeholderMessages.push(strValue);
86
+ }
87
+ this.emitArrayItemDelta(this.currentArrayKey, strValue);
88
+ } else if (this.streamingHtmlKey) {
89
+ // HTML string completed — flush any remaining content immediately + htmlComplete
90
+ const fullHtml = value != null ? String(value) : "";
91
+ this.params.html = fullHtml || undefined;
92
+ this.emitPendingHtml(fullHtml);
93
+ this.emitParamDelta("htmlComplete", true);
94
+ this.streamingHtmlKey = false;
95
+ } else {
96
+ this.setParam(this.currentKey, value);
97
+ }
98
+ }
99
+ };
100
+
101
+ this.parser.onopenarray = () => {
102
+ if (this.depth === 1 && this.currentKey) {
103
+ const key = this.currentKey;
104
+ if (key === "jsExpressions" || key === "placeholderMessages") {
105
+ this.currentArrayKey = key;
106
+ if (key === "jsExpressions") this.params.jsExpressions = [];
107
+ else this.params.placeholderMessages = [];
108
+ // Emit a delta to create the array in the activity content.
109
+ // Subsequent "add" ops with path "/<key>/-" append to this array.
110
+ this.emitParamDelta(key, []);
111
+ }
112
+ }
113
+ };
114
+
115
+ this.parser.onclosearray = () => {
116
+ if (this.depth === 1) {
117
+ if (this.currentArrayKey === "jsExpressions") {
118
+ this.emitParamDelta("jsExpressionsComplete", true);
119
+ }
120
+ this.currentArrayKey = null;
121
+ }
122
+ };
123
+
124
+ this.parser.oncloseobject = () => {
125
+ this.depth--;
126
+ };
127
+
128
+ this.parser.onerror = (err: Error) => {
129
+ console.warn(
130
+ "[OpenGenerativeUI] JSON parse error in streaming args, resuming:",
131
+ err?.message ?? err,
132
+ );
133
+ // Reset error state so parsing can continue with the next chunk
134
+ this.parser.error = null;
135
+ this.parser.resume();
136
+ };
137
+ }
138
+
139
+ write(chunk: string): void {
140
+ this.parser.write(chunk);
141
+ this.flushHtmlChunks();
142
+ }
143
+
144
+ private initHtmlStreaming(key: string): void {
145
+ if (key === "html") {
146
+ this.streamingHtmlKey = true;
147
+ this.htmlEmittedLength = 0;
148
+ this.htmlArrayEmitted = false;
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Read clarinet's internal textNode buffer to emit html chunks incrementally.
154
+ * Called after every write() so partial string content is emitted as it streams in.
155
+ */
156
+ private flushHtmlChunks(): void {
157
+ if (!this.streamingHtmlKey) return;
158
+ const textNode = (this.parser as any).textNode;
159
+ if (typeof textNode !== "string") return;
160
+ if (textNode.length === this.htmlEmittedLength) return;
161
+
162
+ this.emitPendingHtml(textNode);
163
+ }
164
+
165
+ /**
166
+ * Emit accumulated html content since the last emission.
167
+ * Called by flushHtmlChunks and directly when html completes.
168
+ */
169
+ private emitPendingHtml(textNode: string): void {
170
+ const newContent = textNode.slice(this.htmlEmittedLength);
171
+ if (newContent.length === 0) return;
172
+
173
+ if (!this.htmlArrayEmitted) {
174
+ this.htmlArrayEmitted = true;
175
+ this.emitParamDelta("html", []);
176
+ }
177
+ this.emitArrayItemDelta("html", newContent);
178
+ this.htmlEmittedLength = textNode.length;
179
+ }
180
+
181
+ private setParam(key: string, value: string | boolean | number | null): void {
182
+ switch (key) {
183
+ case "initialHeight":
184
+ this.params.initialHeight =
185
+ typeof value === "number" ? value : undefined;
186
+ this.emitSnapshot();
187
+ break;
188
+ case "css":
189
+ this.params.css = value != null ? String(value) : undefined;
190
+ this.emitParamDelta("css", this.params.css);
191
+ this.emitParamDelta("cssComplete", true);
192
+ break;
193
+ case "jsFunctions":
194
+ this.params.jsFunctions = value != null ? String(value) : undefined;
195
+ this.emitParamDelta("jsFunctions", this.params.jsFunctions);
196
+ this.emitParamDelta("jsFunctionsComplete", true);
197
+ break;
198
+ }
199
+ }
200
+
201
+ private emitSnapshot(): void {
202
+ if (this.snapshotEmitted) return;
203
+ this.snapshotEmitted = true;
204
+
205
+ const event: ActivitySnapshotEvent = {
206
+ type: EventType.ACTIVITY_SNAPSHOT,
207
+ messageId: this.messageId,
208
+ activityType: ACTIVITY_TYPE,
209
+ content: { initialHeight: this.params.initialHeight, generating: true },
210
+ };
211
+ this.onEvent(event);
212
+ }
213
+
214
+ private emitParamDelta(key: string, value: unknown): void {
215
+ const event: ActivityDeltaEvent = {
216
+ type: EventType.ACTIVITY_DELTA,
217
+ messageId: this.messageId,
218
+ activityType: ACTIVITY_TYPE,
219
+ patch: [{ op: "add", path: `/${key}`, value }],
220
+ };
221
+ this.onEvent(event);
222
+ }
223
+
224
+ private emitArrayItemDelta(arrayKey: string, value: string): void {
225
+ const event: ActivityDeltaEvent = {
226
+ type: EventType.ACTIVITY_DELTA,
227
+ messageId: this.messageId,
228
+ activityType: ACTIVITY_TYPE,
229
+ patch: [{ op: "add", path: `/${arrayKey}/-`, value }],
230
+ };
231
+ this.onEvent(event);
232
+ }
233
+ }
234
+
235
+ /**
236
+ * Extract EventWithState type from Middleware.runNextWithState return type
237
+ */
238
+ type ExtractObservableType<T> = T extends Observable<infer U> ? U : never;
239
+ type RunNextWithStateReturn = ReturnType<Middleware["runNextWithState"]>;
240
+ type EventWithState = ExtractObservableType<RunNextWithStateReturn>;
241
+
242
+ export class OpenGenerativeUIMiddleware extends Middleware {
243
+ run(input: RunAgentInput, next: AbstractAgent): Observable<BaseEvent> {
244
+ return this.processStream(this.runNextWithState(input, next));
245
+ }
246
+
247
+ private processStream(
248
+ source: Observable<EventWithState>,
249
+ ): Observable<BaseEvent> {
250
+ return new Observable<BaseEvent>((subscriber) => {
251
+ let heldRunFinished: EventWithState | null = null;
252
+ // Track active generateSandboxedUi tool call IDs → their streaming parser
253
+ const activeParsers = new Map<string, ArgsParser>();
254
+ // Hold genui tool call events until the first activity event is emitted
255
+ const heldToolCallEvents = new Map<string, BaseEvent[]>();
256
+ const flushedToolCalls = new Set<string>();
257
+
258
+ const flushHeldEvents = (toolCallId: string) => {
259
+ if (flushedToolCalls.has(toolCallId)) return;
260
+ flushedToolCalls.add(toolCallId);
261
+ const held = heldToolCallEvents.get(toolCallId);
262
+ if (held) {
263
+ for (const e of held) {
264
+ subscriber.next(e);
265
+ }
266
+ heldToolCallEvents.delete(toolCallId);
267
+ }
268
+ };
269
+
270
+ const subscription = source.subscribe({
271
+ next: (eventWithState) => {
272
+ const event = eventWithState.event;
273
+
274
+ if (heldRunFinished) {
275
+ subscriber.next(heldRunFinished.event);
276
+ heldRunFinished = null;
277
+ }
278
+
279
+ if (event.type === EventType.RUN_FINISHED) {
280
+ heldRunFinished = eventWithState;
281
+ return;
282
+ }
283
+
284
+ // Hold TOOL_CALL_START for genui until the first activity event
285
+ if (event.type === EventType.TOOL_CALL_START) {
286
+ const startEvent = event as ToolCallStartEvent;
287
+ if (startEvent.toolCallName === TOOL_NAME) {
288
+ heldToolCallEvents.set(startEvent.toolCallId, [event]);
289
+ activeParsers.set(
290
+ startEvent.toolCallId,
291
+ new ArgsParser(startEvent.toolCallId, (activityEvent) => {
292
+ subscriber.next(activityEvent);
293
+ flushHeldEvents(startEvent.toolCallId);
294
+ }),
295
+ );
296
+ return;
297
+ }
298
+ }
299
+
300
+ // Hold or emit TOOL_CALL_ARGS for genui tool calls
301
+ if (event.type === EventType.TOOL_CALL_ARGS) {
302
+ const argsEvent = event as ToolCallArgsEvent;
303
+ const parser = activeParsers.get(argsEvent.toolCallId);
304
+ if (parser) {
305
+ if (!flushedToolCalls.has(argsEvent.toolCallId)) {
306
+ heldToolCallEvents.get(argsEvent.toolCallId)!.push(event);
307
+ } else {
308
+ subscriber.next(event);
309
+ }
310
+ parser.write(argsEvent.delta);
311
+ return;
312
+ }
313
+ }
314
+
315
+ // Hold or emit TOOL_CALL_END for genui tool calls
316
+ if (event.type === EventType.TOOL_CALL_END) {
317
+ const endEvent = event as { toolCallId: string } & BaseEvent;
318
+ const parser = activeParsers.get(endEvent.toolCallId);
319
+ if (parser) {
320
+ // Mark generation complete
321
+ const completeEvent: ActivityDeltaEvent = {
322
+ type: EventType.ACTIVITY_DELTA,
323
+ messageId: parser.messageId,
324
+ activityType: ACTIVITY_TYPE,
325
+ patch: [{ op: "add", path: "/generating", value: false }],
326
+ };
327
+ subscriber.next(completeEvent);
328
+
329
+ if (!flushedToolCalls.has(endEvent.toolCallId)) {
330
+ heldToolCallEvents.get(endEvent.toolCallId)!.push(event);
331
+ } else {
332
+ subscriber.next(event);
333
+ }
334
+ return;
335
+ }
336
+ }
337
+
338
+ subscriber.next(event);
339
+ },
340
+ error: (err) => {
341
+ // Flush any held tool call events so downstream sees them before the error
342
+ for (const [, events] of heldToolCallEvents) {
343
+ for (const event of events) {
344
+ subscriber.next(event);
345
+ }
346
+ }
347
+ heldToolCallEvents.clear();
348
+
349
+ if (heldRunFinished) {
350
+ subscriber.next(heldRunFinished.event);
351
+ heldRunFinished = null;
352
+ }
353
+ subscriber.error(err);
354
+ },
355
+ complete: () => {
356
+ // Flush any remaining held tool call events (e.g. parser never emitted)
357
+ heldToolCallEvents.forEach((_, toolCallId) => {
358
+ flushHeldEvents(toolCallId);
359
+ });
360
+
361
+ if (heldRunFinished) {
362
+ subscriber.next(heldRunFinished.event);
363
+ heldRunFinished = null;
364
+ }
365
+ activeParsers.clear();
366
+ subscriber.complete();
367
+ },
368
+ });
369
+
370
+ return () => subscription.unsubscribe();
371
+ });
372
+ }
373
+ }
@@ -26,6 +26,10 @@ export interface IntelligenceAgentRunnerOptions {
26
26
  url: string;
27
27
  /** Optional Phoenix socket auth token used during websocket connect. */
28
28
  authToken?: string;
29
+ /** Max delay (ms) for WebSocket reconnect backoff. @default 10_000 */
30
+ maxReconnectMs?: number;
31
+ /** Max delay (ms) for channel rejoin backoff. @default 30_000 */
32
+ maxRejoinMs?: number;
29
33
  }
30
34
 
31
35
  interface ThreadState {
@@ -59,10 +63,10 @@ export class IntelligenceAgentRunner extends AgentRunner {
59
63
  * - Each run gets its own independent retry budget.
60
64
  *
61
65
  * reconnectAfterMs — delay before Phoenix reconnects the WebSocket
62
- * after an unclean close. 100ms base, doubling up to a 10s cap.
66
+ * after an unclean close. 100ms base, doubling up to maxReconnectMs (default 10s).
63
67
  *
64
68
  * rejoinAfterMs — delay before Phoenix re-joins a channel that
65
- * entered the "errored" state. 1s base, doubling up to 30s cap.
69
+ * entered the "errored" state. 1s base, doubling up to maxRejoinMs (default 30s).
66
70
  *
67
71
  * These are set explicitly because Phoenix's default schedule is a
68
72
  * fixed stepped array (not exponential), and any code that calls
@@ -73,8 +77,14 @@ export class IntelligenceAgentRunner extends AgentRunner {
73
77
  private createSocket(): Socket {
74
78
  const socket = new Socket(this.options.url, {
75
79
  ...(this.options.authToken ? { authToken: this.options.authToken } : {}),
76
- reconnectAfterMs: phoenixExponentialBackoff(100, 10_000),
77
- rejoinAfterMs: phoenixExponentialBackoff(1_000, 30_000),
80
+ reconnectAfterMs: phoenixExponentialBackoff(
81
+ 100,
82
+ this.options.maxReconnectMs ?? 10_000,
83
+ ),
84
+ rejoinAfterMs: phoenixExponentialBackoff(
85
+ 1_000,
86
+ this.options.maxRejoinMs ?? 30_000,
87
+ ),
78
88
  });
79
89
  socket.connect();
80
90
  return socket;
@@ -1,4 +1,7 @@
1
+ import { Analytics } from "@segment/analytics-node";
1
2
  import { AnalyticsEvents } from "./events";
3
+ import { flattenObject } from "./utils";
4
+ import { v4 as uuidv4 } from "uuid";
2
5
  import scarfClient from "./scarf-client";
3
6
 
4
7
  export function isTelemetryDisabled(): boolean {
@@ -14,8 +17,11 @@ export function isTelemetryDisabled(): boolean {
14
17
  }
15
18
 
16
19
  export class TelemetryClient {
20
+ segment: Analytics | undefined;
21
+ globalProperties: Record<string, any> = {};
17
22
  private telemetryDisabled: boolean = false;
18
23
  private sampleRate: number = 0.05;
24
+ private anonymousId = `anon_${uuidv4()}`;
19
25
 
20
26
  constructor({
21
27
  telemetryDisabled,
@@ -25,7 +31,21 @@ export class TelemetryClient {
25
31
  sampleRate?: number;
26
32
  } = {}) {
27
33
  this.telemetryDisabled = telemetryDisabled ?? isTelemetryDisabled();
34
+
35
+ if (this.telemetryDisabled) {
36
+ this.setSampleRate(sampleRate);
37
+ return;
38
+ }
39
+
28
40
  this.setSampleRate(sampleRate);
41
+
42
+ const writeKey =
43
+ process.env.COPILOTKIT_SEGMENT_WRITE_KEY ||
44
+ "n7XAZtQCGS2v1vvBy3LgBCv2h3Y8whja";
45
+
46
+ this.segment = new Analytics({
47
+ writeKey,
48
+ });
29
49
  }
30
50
 
31
51
  private shouldSendEvent() {
@@ -44,11 +64,42 @@ export class TelemetryClient {
44
64
  return;
45
65
  }
46
66
 
67
+ const flattenedProperties = flattenObject(properties);
68
+ const propertiesWithGlobal = {
69
+ ...this.globalProperties,
70
+ ...flattenedProperties,
71
+ };
72
+ const orderedPropertiesWithGlobal = Object.keys(propertiesWithGlobal)
73
+ .sort()
74
+ .reduce(
75
+ (obj, key) => {
76
+ obj[key] = propertiesWithGlobal[key];
77
+ return obj;
78
+ },
79
+ {} as Record<string, any>,
80
+ );
81
+
82
+ if (this.segment) {
83
+ this.segment.track({
84
+ anonymousId: this.anonymousId,
85
+ event,
86
+ properties: { ...orderedPropertiesWithGlobal },
87
+ });
88
+ }
89
+
47
90
  await scarfClient.logEvent({
48
91
  event,
49
92
  });
50
93
  }
51
94
 
95
+ setGlobalProperties(properties: Record<string, any>) {
96
+ const flattenedProperties = flattenObject(properties);
97
+ this.globalProperties = {
98
+ ...this.globalProperties,
99
+ ...flattenedProperties,
100
+ };
101
+ }
102
+
52
103
  private setSampleRate(sampleRate: number | undefined) {
53
104
  let _sampleRate: number;
54
105
 
@@ -63,6 +114,11 @@ export class TelemetryClient {
63
114
  }
64
115
 
65
116
  this.sampleRate = _sampleRate;
117
+ this.setGlobalProperties({
118
+ sampleRate: this.sampleRate,
119
+ sampleRateAdjustmentFactor: 1 - this.sampleRate,
120
+ sampleWeight: 1 / this.sampleRate,
121
+ });
66
122
  }
67
123
  }
68
124
 
@@ -0,0 +1,15 @@
1
+ export function flattenObject(
2
+ obj: Record<string, any>,
3
+ parentKey = "",
4
+ res: Record<string, any> = {},
5
+ ): Record<string, any> {
6
+ for (const key in obj) {
7
+ const propName = parentKey ? `${parentKey}.${key}` : key;
8
+ if (typeof obj[key] === "object" && obj[key] !== null) {
9
+ flattenObject(obj[key], propName, res);
10
+ } else {
11
+ res[propName] = obj[key];
12
+ }
13
+ }
14
+ return res;
15
+ }
package/tsdown.config.ts CHANGED
@@ -1,7 +1,14 @@
1
1
  import { defineConfig } from "tsdown";
2
2
 
3
3
  export default defineConfig({
4
- entry: ["src/index.ts", "src/v2/index.ts", "src/langgraph.ts"],
4
+ entry: [
5
+ "src/index.ts",
6
+ "src/v2/index.ts",
7
+ "src/v2/express.ts",
8
+ "src/v2/hono.ts",
9
+ "src/v2/node.ts",
10
+ "src/langgraph.ts",
11
+ ],
5
12
  format: ["esm", "cjs"],
6
13
  dts: true,
7
14
  sourcemap: true,