@copilotkit/runtime 1.54.1-next.6 → 1.55.0-next.7

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 +117 -106
  3. package/dist/_virtual/_rolldown/runtime.mjs +25 -1
  4. package/dist/agent/index.cjs +643 -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 +635 -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 +22 -5
  242. package/src/agent/__tests__/ai-sdk-v6-compat.test.ts +116 -0
  243. package/src/agent/__tests__/basic-agent.test.ts +1248 -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 +178 -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 +1305 -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,429 @@
1
+ import {
2
+ AgentRunner,
3
+ AgentRunnerConnectRequest,
4
+ AgentRunnerIsRunningRequest,
5
+ AgentRunnerRunRequest,
6
+ type AgentRunnerStopRequest,
7
+ } from "./agent-runner";
8
+ import { EMPTY, Observable, from } from "rxjs";
9
+ import { catchError, finalize } from "rxjs/operators";
10
+ import {
11
+ AbstractAgent,
12
+ BaseEvent,
13
+ EventType,
14
+ RunStartedEvent,
15
+ } from "@ag-ui/client";
16
+ import {
17
+ finalizeRunEvents,
18
+ AG_UI_CHANNEL_EVENT,
19
+ phoenixExponentialBackoff,
20
+ } from "@copilotkit/shared";
21
+ import { Socket, Channel } from "phoenix";
22
+ import { randomUUID } from "node:crypto";
23
+
24
+ export interface IntelligenceAgentRunnerOptions {
25
+ /** Phoenix runner websocket URL, e.g. "ws://localhost:4000/runner" */
26
+ url: string;
27
+ /** Optional Phoenix socket auth token used during websocket connect. */
28
+ authToken?: string;
29
+ }
30
+
31
+ interface ThreadState {
32
+ socket: Socket;
33
+ channel: Channel;
34
+ isRunning: boolean;
35
+ stopRequested: boolean;
36
+ agent: AbstractAgent | null;
37
+ currentEvents: BaseEvent[];
38
+ nextEventSeq: number;
39
+ hasRunStarted: boolean;
40
+ }
41
+
42
+ export class IntelligenceAgentRunner extends AgentRunner {
43
+ private options: IntelligenceAgentRunnerOptions;
44
+ private threads = new Map<string, ThreadState>();
45
+
46
+ constructor(options: IntelligenceAgentRunnerOptions) {
47
+ super();
48
+ // Store config — sockets are created per-run, not eagerly.
49
+ this.options = options;
50
+ }
51
+
52
+ /**
53
+ * Create a new Phoenix socket with explicit exponential backoff.
54
+ *
55
+ * Each run/connect gets its own socket so that:
56
+ * - A socket failure only affects a single thread, not all threads.
57
+ * - Cleanup is simple: channel.leave() + socket.disconnect() tears
58
+ * down everything for that run with no shared-state concerns.
59
+ * - Each run gets its own independent retry budget.
60
+ *
61
+ * reconnectAfterMs — delay before Phoenix reconnects the WebSocket
62
+ * after an unclean close. 100ms base, doubling up to a 10s cap.
63
+ *
64
+ * rejoinAfterMs — delay before Phoenix re-joins a channel that
65
+ * entered the "errored" state. 1s base, doubling up to 30s cap.
66
+ *
67
+ * These are set explicitly because Phoenix's default schedule is a
68
+ * fixed stepped array (not exponential), and any code that calls
69
+ * socket.disconnect() in an onError handler will set
70
+ * closeWasClean = true and reset the reconnect timer — permanently
71
+ * killing retries.
72
+ */
73
+ private createSocket(): Socket {
74
+ const socket = new Socket(this.options.url, {
75
+ ...(this.options.authToken ? { authToken: this.options.authToken } : {}),
76
+ reconnectAfterMs: phoenixExponentialBackoff(100, 10_000),
77
+ rejoinAfterMs: phoenixExponentialBackoff(1_000, 30_000),
78
+ });
79
+ socket.connect();
80
+ return socket;
81
+ }
82
+
83
+ private createRunnerEventPayload(
84
+ event: BaseEvent,
85
+ request: AgentRunnerRunRequest,
86
+ state: ThreadState,
87
+ ): Record<string, unknown> {
88
+ const canonicalEvent = this.stampRunnerMetadata(event, state);
89
+ const payload = {
90
+ ...(canonicalEvent as Record<string, unknown>),
91
+ };
92
+
93
+ payload.thread_id ??= request.threadId;
94
+
95
+ const runId = payload.runId ?? payload.run_id ?? request.input.runId;
96
+
97
+ if (runId) {
98
+ payload.run_id = runId;
99
+ }
100
+
101
+ return payload;
102
+ }
103
+
104
+ private stampRunnerMetadata(event: BaseEvent, state: ThreadState): BaseEvent {
105
+ const eventRecord = event as BaseEvent & {
106
+ metadata?: Record<string, unknown>;
107
+ };
108
+
109
+ const existingMetadata = eventRecord.metadata ?? {};
110
+ const hasEventId = typeof existingMetadata.cpki_event_id === "string";
111
+ const hasEventSeq = typeof existingMetadata.cpki_event_seq === "number";
112
+
113
+ if (hasEventId && hasEventSeq) {
114
+ const eventSeq = existingMetadata.cpki_event_seq as number;
115
+ state.nextEventSeq = Math.max(state.nextEventSeq, eventSeq + 1);
116
+ return eventRecord;
117
+ }
118
+
119
+ const eventSeq = state.nextEventSeq++;
120
+
121
+ return {
122
+ ...eventRecord,
123
+ metadata: {
124
+ ...existingMetadata,
125
+ cpki_event_id:
126
+ typeof existingMetadata.cpki_event_id === "string"
127
+ ? existingMetadata.cpki_event_id
128
+ : randomUUID(),
129
+ cpki_event_seq: eventSeq,
130
+ },
131
+ };
132
+ }
133
+
134
+ run(request: AgentRunnerRunRequest): Observable<BaseEvent> {
135
+ const { threadId, agent, input, joinCode } = request;
136
+
137
+ const existing = this.threads.get(threadId);
138
+ if (existing?.isRunning) {
139
+ throw new Error("Thread already running");
140
+ }
141
+
142
+ return new Observable((observer) => {
143
+ const socket = this.createSocket();
144
+
145
+ const channelTopic = joinCode ?? threadId;
146
+ const channel = socket.channel(`ingestion:${channelTopic}`, {
147
+ runId: input.runId,
148
+ });
149
+
150
+ const state: ThreadState = {
151
+ socket,
152
+ channel,
153
+ isRunning: true,
154
+ stopRequested: false,
155
+ agent,
156
+ currentEvents: [],
157
+ nextEventSeq: 1,
158
+ hasRunStarted: false,
159
+ };
160
+ this.threads.set(threadId, state);
161
+
162
+ // Track consecutive socket errors for this run. Phoenix retries
163
+ // automatically via reconnectAfterMs, but if the connection fails
164
+ // repeatedly we abort the agent — otherwise runAgent() completes
165
+ // normally, finalization events buffer silently on the dead
166
+ // channel, and the client never receives them.
167
+ //
168
+ // Aborting the agent is the single trigger that cascades through
169
+ // the existing error pipeline: runAgent() rejects → catchError
170
+ // pushes RUN_ERROR → finalize calls finalizeRunEvents +
171
+ // removeThread → channel.leave() + socket.disconnect().
172
+ const MAX_CONSECUTIVE_ERRORS = 5;
173
+ let consecutiveErrors = 0;
174
+
175
+ socket.onError(() => {
176
+ consecutiveErrors++;
177
+ if (consecutiveErrors >= MAX_CONSECUTIVE_ERRORS && state.agent) {
178
+ try {
179
+ state.agent.abortRun();
180
+ } catch {
181
+ // Ignore abort errors.
182
+ }
183
+ }
184
+ // Otherwise: Phoenix retries automatically using the exponential
185
+ // backoff schedule configured in createSocket().
186
+ });
187
+
188
+ socket.onOpen(() => {
189
+ // A successful (re)connection resets the counter so transient
190
+ // network blips don't accumulate across recoveries.
191
+ consecutiveErrors = 0;
192
+ });
193
+
194
+ // Listen for custom "stop" events pushed by the client over the
195
+ // channel. This must be registered before channel.join() so the
196
+ // handler is in place by the time the server starts relaying messages.
197
+ // The client sends the stop event before leaving the channel, so the
198
+ // runner is guaranteed to receive it while still joined.
199
+ channel.on(AG_UI_CHANNEL_EVENT, (payload: BaseEvent) => {
200
+ if (
201
+ payload.type === EventType.CUSTOM &&
202
+ (payload as BaseEvent & { name?: string }).name === "stop"
203
+ ) {
204
+ this.stop({ threadId });
205
+ }
206
+ });
207
+
208
+ channel
209
+ .join()
210
+ .receive("ok", () => {
211
+ this.executeAgentRun(request, state, threadId).subscribe({
212
+ complete: () => observer.complete(),
213
+ });
214
+ })
215
+ .receive("error", (resp) => {
216
+ const errorEvent = {
217
+ type: EventType.RUN_ERROR,
218
+ message: `Failed to join channel: ${JSON.stringify(resp)}`,
219
+ code: "CHANNEL_JOIN_ERROR",
220
+ } as BaseEvent;
221
+ observer.next(errorEvent);
222
+ state.currentEvents.push(errorEvent);
223
+ this.removeThread(threadId);
224
+ observer.complete();
225
+ })
226
+ .receive("timeout", () => {
227
+ const errorEvent = {
228
+ type: EventType.RUN_ERROR,
229
+ message: "Timed out joining channel",
230
+ code: "CHANNEL_JOIN_TIMEOUT",
231
+ } as BaseEvent;
232
+ observer.next(errorEvent);
233
+ state.currentEvents.push(errorEvent);
234
+ this.removeThread(threadId);
235
+ observer.complete();
236
+ });
237
+
238
+ return () => {
239
+ this.removeThread(threadId);
240
+ };
241
+ });
242
+ }
243
+
244
+ connect(request: AgentRunnerConnectRequest): Observable<BaseEvent> {
245
+ const { threadId } = request;
246
+
247
+ return new Observable((observer) => {
248
+ const socket = this.createSocket();
249
+
250
+ const channel = socket.channel(`thread:${threadId}`);
251
+
252
+ channel.on("ag_ui_event", (payload: BaseEvent) => {
253
+ observer.next(payload);
254
+
255
+ if (
256
+ payload.type === EventType.RUN_FINISHED ||
257
+ payload.type === EventType.RUN_ERROR
258
+ ) {
259
+ observer.complete();
260
+ }
261
+ });
262
+
263
+ const cleanup = () => {
264
+ channel.leave();
265
+ socket.disconnect();
266
+ };
267
+
268
+ channel
269
+ .join()
270
+ .receive("ok", () => undefined)
271
+ .receive("error", (resp) => {
272
+ observer.error(
273
+ new Error(`Failed to join channel: ${JSON.stringify(resp)}`),
274
+ );
275
+ cleanup();
276
+ })
277
+ .receive("timeout", () => {
278
+ observer.error(new Error("Timed out joining channel"));
279
+ cleanup();
280
+ });
281
+
282
+ return () => {
283
+ cleanup();
284
+ };
285
+ });
286
+ }
287
+
288
+ isRunning(request: AgentRunnerIsRunningRequest): Promise<boolean> {
289
+ const state = this.threads.get(request.threadId);
290
+ return Promise.resolve(state?.isRunning ?? false);
291
+ }
292
+
293
+ stop(request: AgentRunnerStopRequest): Promise<boolean | undefined> {
294
+ const state = this.threads.get(request.threadId);
295
+ if (!state || !state.isRunning || state.stopRequested) {
296
+ return Promise.resolve(false);
297
+ }
298
+
299
+ state.stopRequested = true;
300
+
301
+ // Direct local abort — the runtime is the authority.
302
+ if (state.agent) {
303
+ try {
304
+ state.agent.abortRun();
305
+ } catch {
306
+ // Ignore abort errors.
307
+ }
308
+ }
309
+
310
+ return Promise.resolve(true);
311
+ }
312
+
313
+ private executeAgentRun(
314
+ request: AgentRunnerRunRequest,
315
+ state: ThreadState,
316
+ threadId: string,
317
+ ): Observable<void> {
318
+ const { currentEvents, channel } = state;
319
+ const pushCanonicalEvent = (event: BaseEvent): void => {
320
+ const canonicalEvent = this.stampRunnerMetadata(event, state);
321
+ currentEvents.push(canonicalEvent);
322
+
323
+ if (canonicalEvent.type === EventType.RUN_STARTED) {
324
+ state.hasRunStarted = true;
325
+ }
326
+
327
+ channel.push(
328
+ "event",
329
+ this.createRunnerEventPayload(canonicalEvent, request, state),
330
+ );
331
+ };
332
+
333
+ const getPersistedInputMessages = () =>
334
+ request.persistedInputMessages ?? request.input.messages;
335
+
336
+ const buildRunStartedEvent = (
337
+ source?: RunStartedEvent,
338
+ ): RunStartedEvent => {
339
+ const baseInput = source?.input ?? request.input;
340
+ const persistedInputMessages = getPersistedInputMessages();
341
+
342
+ return {
343
+ ...(source ?? {
344
+ type: EventType.RUN_STARTED,
345
+ threadId: request.threadId,
346
+ runId: request.input.runId,
347
+ }),
348
+ input: {
349
+ ...baseInput,
350
+ ...(persistedInputMessages !== undefined
351
+ ? { messages: persistedInputMessages }
352
+ : {}),
353
+ },
354
+ } as RunStartedEvent;
355
+ };
356
+
357
+ const ensureRunStarted = (): void => {
358
+ if (!state.hasRunStarted) {
359
+ state.hasRunStarted = true;
360
+ pushCanonicalEvent(buildRunStartedEvent());
361
+ }
362
+ };
363
+
364
+ return from(
365
+ request.agent.runAgent(request.input, {
366
+ onEvent: ({ event }: { event: BaseEvent }) => {
367
+ if (event.type === EventType.RUN_STARTED) {
368
+ pushCanonicalEvent(buildRunStartedEvent(event as RunStartedEvent));
369
+ return;
370
+ }
371
+
372
+ ensureRunStarted();
373
+ pushCanonicalEvent(event);
374
+ },
375
+ }),
376
+ ).pipe(
377
+ catchError((error) => {
378
+ ensureRunStarted();
379
+ const errorEvent = {
380
+ type: EventType.RUN_ERROR,
381
+ message: error instanceof Error ? error.message : String(error),
382
+ } as BaseEvent;
383
+ pushCanonicalEvent(errorEvent);
384
+ return EMPTY;
385
+ }),
386
+ finalize(() => {
387
+ ensureRunStarted();
388
+ const appended = finalizeRunEvents(currentEvents, {
389
+ stopRequested: state.stopRequested,
390
+ });
391
+ for (const event of appended) {
392
+ channel.push(
393
+ "event",
394
+ this.createRunnerEventPayload(event, request, state),
395
+ );
396
+ }
397
+ this.removeThread(threadId);
398
+ }),
399
+ );
400
+ }
401
+
402
+ /**
403
+ * Tear down all resources for a thread: leave the channel,
404
+ * disconnect the per-run socket, and remove the thread state.
405
+ *
406
+ * Idempotent — safe to call multiple times for the same threadId
407
+ * (e.g. from join error handlers, finalize, and Observable teardown).
408
+ */
409
+ private removeThread(threadId: string): void {
410
+ const state = this.threads.get(threadId);
411
+ if (!state) {
412
+ return;
413
+ }
414
+
415
+ // Delete first so concurrent calls see the entry as already removed.
416
+ this.threads.delete(threadId);
417
+
418
+ try {
419
+ state.channel.leave();
420
+ } catch {
421
+ // Channel may already be closed/left.
422
+ }
423
+ try {
424
+ state.socket.disconnect();
425
+ } catch {
426
+ // Socket may already be disconnected.
427
+ }
428
+ }
429
+ }
@@ -0,0 +1,260 @@
1
+ import {
2
+ MaybePromise,
3
+ NonEmptyRecord,
4
+ RuntimeMode,
5
+ RUNTIME_MODE_SSE,
6
+ RUNTIME_MODE_INTELLIGENCE,
7
+ createLicenseChecker,
8
+ type LicenseChecker,
9
+ } from "@copilotkit/shared";
10
+ import { AbstractAgent } from "@ag-ui/client";
11
+ import type { MCPClientConfig } from "@ag-ui/mcp-apps-middleware";
12
+ import { A2UIMiddlewareConfig } from "@ag-ui/a2ui-middleware";
13
+ import pkg from "../../../package.json";
14
+ import type {
15
+ BeforeRequestMiddleware,
16
+ AfterRequestMiddleware,
17
+ } from "./middleware";
18
+ import { TranscriptionService } from "./transcription-service/transcription-service";
19
+ import { AgentRunner } from "./runner/agent-runner";
20
+ import { InMemoryAgentRunner } from "./runner/in-memory";
21
+ import { IntelligenceAgentRunner } from "./runner/intelligence";
22
+ import { CopilotKitIntelligence } from "./intelligence-platform";
23
+
24
+ export const VERSION = pkg.version;
25
+
26
+ interface BaseCopilotRuntimeMiddlewareOptions {
27
+ /** If set, middleware only applies to these named agents. Applies to all agents if omitted. */
28
+ agents?: string[];
29
+ }
30
+
31
+ export type McpAppsServerConfig = MCPClientConfig & {
32
+ /** Agent to bind this server to. If omitted, the server is available to all agents. */
33
+ agentId?: string;
34
+ };
35
+
36
+ export interface McpAppsConfig {
37
+ /** List of MCP server configurations. */
38
+ servers: McpAppsServerConfig[];
39
+ }
40
+
41
+ interface CopilotRuntimeMiddlewares {
42
+ /** Auto-apply A2UIMiddleware to agents at run time. */
43
+ a2ui?: BaseCopilotRuntimeMiddlewareOptions & A2UIMiddlewareConfig;
44
+ /** Auto-apply MCPAppsMiddleware to agents at run time. */
45
+ mcpApps?: McpAppsConfig;
46
+ }
47
+
48
+ interface BaseCopilotRuntimeOptions extends CopilotRuntimeMiddlewares {
49
+ /** Map of available agents (loaded lazily is fine). */
50
+ agents: MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;
51
+ /** Optional transcription service for audio processing. */
52
+ transcriptionService?: TranscriptionService;
53
+ /** Optional *before* middleware – callback function or webhook URL. */
54
+ beforeRequestMiddleware?: BeforeRequestMiddleware;
55
+ /** Optional *after* middleware – callback function or webhook URL. */
56
+ afterRequestMiddleware?: AfterRequestMiddleware;
57
+ /** Signed license token for server-side feature verification. Falls back to COPILOTKIT_LICENSE_TOKEN env var. */
58
+ licenseToken?: string;
59
+ }
60
+
61
+ export interface CopilotRuntimeUser {
62
+ id: string;
63
+ }
64
+
65
+ export type IdentifyUserCallback = (
66
+ request: Request,
67
+ ) => MaybePromise<CopilotRuntimeUser>;
68
+
69
+ export interface CopilotSseRuntimeOptions extends BaseCopilotRuntimeOptions {
70
+ /** The runner to use for running agents in SSE mode. */
71
+ runner?: AgentRunner;
72
+ intelligence?: undefined;
73
+ generateThreadNames?: undefined;
74
+ }
75
+
76
+ export interface CopilotIntelligenceRuntimeOptions extends BaseCopilotRuntimeOptions {
77
+ /** Configures Intelligence mode for durable threads and realtime events. */
78
+ intelligence: CopilotKitIntelligence;
79
+ /** Resolves the authenticated user for intelligence requests. */
80
+ identifyUser: IdentifyUserCallback;
81
+ /** Auto-generate short names for newly created threads. */
82
+ generateThreadNames?: boolean;
83
+ }
84
+
85
+ export type CopilotRuntimeOptions =
86
+ | CopilotSseRuntimeOptions
87
+ | CopilotIntelligenceRuntimeOptions;
88
+
89
+ export interface CopilotRuntimeLike {
90
+ agents: CopilotRuntimeOptions["agents"];
91
+ transcriptionService: CopilotRuntimeOptions["transcriptionService"];
92
+ beforeRequestMiddleware: CopilotRuntimeOptions["beforeRequestMiddleware"];
93
+ afterRequestMiddleware: CopilotRuntimeOptions["afterRequestMiddleware"];
94
+ runner: AgentRunner;
95
+ a2ui: CopilotRuntimeOptions["a2ui"];
96
+ mcpApps: CopilotRuntimeOptions["mcpApps"];
97
+ intelligence?: CopilotKitIntelligence;
98
+ identifyUser?: IdentifyUserCallback;
99
+ mode: RuntimeMode;
100
+ licenseChecker?: LicenseChecker;
101
+ }
102
+
103
+ export interface CopilotSseRuntimeLike extends CopilotRuntimeLike {
104
+ intelligence?: undefined;
105
+ mode: RUNTIME_MODE_SSE;
106
+ }
107
+
108
+ export interface CopilotIntelligenceRuntimeLike extends CopilotRuntimeLike {
109
+ intelligence: CopilotKitIntelligence;
110
+ identifyUser: IdentifyUserCallback;
111
+ generateThreadNames: boolean;
112
+ mode: RUNTIME_MODE_INTELLIGENCE;
113
+ }
114
+
115
+ abstract class BaseCopilotRuntime implements CopilotRuntimeLike {
116
+ public agents: CopilotRuntimeOptions["agents"];
117
+ public transcriptionService: CopilotRuntimeOptions["transcriptionService"];
118
+ public beforeRequestMiddleware: CopilotRuntimeOptions["beforeRequestMiddleware"];
119
+ public afterRequestMiddleware: CopilotRuntimeOptions["afterRequestMiddleware"];
120
+ public runner: AgentRunner;
121
+ public a2ui: CopilotRuntimeOptions["a2ui"];
122
+ public mcpApps: CopilotRuntimeOptions["mcpApps"];
123
+ public licenseChecker?: LicenseChecker;
124
+
125
+ abstract readonly intelligence?: CopilotKitIntelligence;
126
+ abstract readonly mode: RuntimeMode;
127
+
128
+ constructor(options: BaseCopilotRuntimeOptions, runner: AgentRunner) {
129
+ const {
130
+ agents,
131
+ transcriptionService,
132
+ beforeRequestMiddleware,
133
+ afterRequestMiddleware,
134
+ a2ui,
135
+ mcpApps,
136
+ } = options;
137
+
138
+ this.agents = agents;
139
+ this.transcriptionService = transcriptionService;
140
+ this.beforeRequestMiddleware = beforeRequestMiddleware;
141
+ this.afterRequestMiddleware = afterRequestMiddleware;
142
+ this.a2ui = a2ui;
143
+ this.mcpApps = mcpApps;
144
+ this.runner = runner;
145
+ this.licenseChecker = createLicenseChecker(options.licenseToken);
146
+ }
147
+ }
148
+
149
+ export class CopilotSseRuntime
150
+ extends BaseCopilotRuntime
151
+ implements CopilotSseRuntimeLike
152
+ {
153
+ readonly intelligence = undefined;
154
+ readonly mode = RUNTIME_MODE_SSE;
155
+
156
+ constructor(options: CopilotSseRuntimeOptions) {
157
+ super(options, options.runner ?? new InMemoryAgentRunner());
158
+ }
159
+ }
160
+
161
+ export class CopilotIntelligenceRuntime
162
+ extends BaseCopilotRuntime
163
+ implements CopilotIntelligenceRuntimeLike
164
+ {
165
+ readonly intelligence: CopilotKitIntelligence;
166
+ readonly identifyUser: IdentifyUserCallback;
167
+ readonly generateThreadNames: boolean;
168
+ readonly mode = RUNTIME_MODE_INTELLIGENCE;
169
+
170
+ constructor(options: CopilotIntelligenceRuntimeOptions) {
171
+ super(
172
+ options,
173
+ new IntelligenceAgentRunner({
174
+ url: options.intelligence.ɵgetRunnerWsUrl(),
175
+ authToken: options.intelligence.ɵgetRunnerAuthToken(),
176
+ }),
177
+ );
178
+ this.intelligence = options.intelligence;
179
+ this.identifyUser = options.identifyUser;
180
+ this.generateThreadNames = options.generateThreadNames ?? true;
181
+ }
182
+ }
183
+
184
+ function hasIntelligenceOptions(
185
+ options: CopilotRuntimeOptions,
186
+ ): options is CopilotIntelligenceRuntimeOptions {
187
+ return "intelligence" in options && !!options.intelligence;
188
+ }
189
+
190
+ export function isIntelligenceRuntime(
191
+ runtime: CopilotRuntimeLike,
192
+ ): runtime is CopilotIntelligenceRuntimeLike {
193
+ return runtime.mode === RUNTIME_MODE_INTELLIGENCE && !!runtime.intelligence;
194
+ }
195
+
196
+ /**
197
+ * Compatibility shim that preserves the legacy `CopilotRuntime` entrypoint.
198
+ * New code should prefer `CopilotSseRuntime` or `CopilotIntelligenceRuntime`.
199
+ */
200
+ export class CopilotRuntime implements CopilotRuntimeLike {
201
+ private delegate: CopilotRuntimeLike;
202
+
203
+ constructor(options: CopilotRuntimeOptions) {
204
+ this.delegate = hasIntelligenceOptions(options)
205
+ ? new CopilotIntelligenceRuntime(options)
206
+ : new CopilotSseRuntime(options);
207
+ }
208
+
209
+ get agents(): CopilotRuntimeOptions["agents"] {
210
+ return this.delegate.agents;
211
+ }
212
+
213
+ get transcriptionService(): CopilotRuntimeOptions["transcriptionService"] {
214
+ return this.delegate.transcriptionService;
215
+ }
216
+
217
+ get beforeRequestMiddleware(): CopilotRuntimeOptions["beforeRequestMiddleware"] {
218
+ return this.delegate.beforeRequestMiddleware;
219
+ }
220
+
221
+ get afterRequestMiddleware(): CopilotRuntimeOptions["afterRequestMiddleware"] {
222
+ return this.delegate.afterRequestMiddleware;
223
+ }
224
+
225
+ get runner(): AgentRunner {
226
+ return this.delegate.runner;
227
+ }
228
+
229
+ get a2ui(): CopilotRuntimeOptions["a2ui"] {
230
+ return this.delegate.a2ui;
231
+ }
232
+
233
+ get mcpApps(): CopilotRuntimeOptions["mcpApps"] {
234
+ return this.delegate.mcpApps;
235
+ }
236
+
237
+ get intelligence(): CopilotKitIntelligence | undefined {
238
+ return this.delegate.intelligence;
239
+ }
240
+
241
+ get generateThreadNames(): boolean | undefined {
242
+ return isIntelligenceRuntime(this.delegate)
243
+ ? this.delegate.generateThreadNames
244
+ : undefined;
245
+ }
246
+
247
+ get identifyUser(): IdentifyUserCallback | undefined {
248
+ return isIntelligenceRuntime(this.delegate)
249
+ ? this.delegate.identifyUser
250
+ : undefined;
251
+ }
252
+
253
+ get mode(): RuntimeMode {
254
+ return this.delegate.mode;
255
+ }
256
+
257
+ get licenseChecker() {
258
+ return this.delegate.licenseChecker;
259
+ }
260
+ }