@copilotkit/runtime 1.55.0-next.8 → 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 +28 -0
  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 +25 -23
  271. package/src/v2/runtime/__tests__/middleware-single-express.test.ts +25 -23
  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
@@ -4,22 +4,24 @@ import {
4
4
  RuntimeMode,
5
5
  RUNTIME_MODE_SSE,
6
6
  RUNTIME_MODE_INTELLIGENCE,
7
+ } from "@copilotkit/shared";
8
+ import {
7
9
  createLicenseChecker,
8
10
  type LicenseChecker,
9
- } from "@copilotkit/shared";
11
+ } from "@copilotkit/license-verifier";
10
12
  import { AbstractAgent } from "@ag-ui/client";
11
13
  import type { MCPClientConfig } from "@ag-ui/mcp-apps-middleware";
12
14
  import { A2UIMiddlewareConfig } from "@ag-ui/a2ui-middleware";
13
- import pkg from "../../../package.json";
15
+ import pkg from "../../../../package.json";
14
16
  import type {
15
17
  BeforeRequestMiddleware,
16
18
  AfterRequestMiddleware,
17
19
  } 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";
20
+ import { TranscriptionService } from "../transcription-service/transcription-service";
21
+ import { AgentRunner } from "../runner/agent-runner";
22
+ import { InMemoryAgentRunner } from "../runner/in-memory";
23
+ import { IntelligenceAgentRunner } from "../runner/intelligence";
24
+ import { CopilotKitIntelligence } from "../intelligence-platform";
23
25
 
24
26
  export const VERSION = pkg.version;
25
27
 
@@ -38,11 +40,20 @@ export interface McpAppsConfig {
38
40
  servers: McpAppsServerConfig[];
39
41
  }
40
42
 
43
+ export interface OpenGenerativeUIOptions extends BaseCopilotRuntimeMiddlewareOptions {}
44
+
45
+ export type OpenGenerativeUIConfig = boolean | OpenGenerativeUIOptions;
46
+
41
47
  interface CopilotRuntimeMiddlewares {
42
- /** Auto-apply A2UIMiddleware to agents at run time. */
48
+ /**
49
+ * Auto-apply A2UIMiddleware to agents at run time.
50
+ * Pass an object to enable and customise behaviour, or omit to disable.
51
+ */
43
52
  a2ui?: BaseCopilotRuntimeMiddlewareOptions & A2UIMiddlewareConfig;
44
53
  /** Auto-apply MCPAppsMiddleware to agents at run time. */
45
54
  mcpApps?: McpAppsConfig;
55
+ /** Auto-apply OpenGenerativeUIMiddleware to agents at run time. */
56
+ openGenerativeUI?: OpenGenerativeUIConfig;
46
57
  }
47
58
 
48
59
  interface BaseCopilotRuntimeOptions extends CopilotRuntimeMiddlewares {
@@ -80,6 +91,16 @@ export interface CopilotIntelligenceRuntimeOptions extends BaseCopilotRuntimeOpt
80
91
  identifyUser: IdentifyUserCallback;
81
92
  /** Auto-generate short names for newly created threads. */
82
93
  generateThreadNames?: boolean;
94
+ /** Max delay (ms) for WebSocket reconnect backoff. @default 10_000 */
95
+ maxReconnectMs?: number;
96
+ /** Max delay (ms) for channel rejoin backoff. @default 30_000 */
97
+ maxRejoinMs?: number;
98
+ /** Lock TTL in seconds. Clamped to a maximum of 3600 (1 hour). @default 20 */
99
+ lockTtlSeconds?: number;
100
+ /** Custom Redis key prefix for the thread lock. */
101
+ lockKeyPrefix?: string;
102
+ /** Interval in seconds at which the runtime renews the thread lock. Clamped to a maximum of 3000 (50 minutes). @default 15 */
103
+ lockHeartbeatIntervalSeconds?: number;
83
104
  }
84
105
 
85
106
  export type CopilotRuntimeOptions =
@@ -94,6 +115,7 @@ export interface CopilotRuntimeLike {
94
115
  runner: AgentRunner;
95
116
  a2ui: CopilotRuntimeOptions["a2ui"];
96
117
  mcpApps: CopilotRuntimeOptions["mcpApps"];
118
+ openGenerativeUI: CopilotRuntimeOptions["openGenerativeUI"];
97
119
  intelligence?: CopilotKitIntelligence;
98
120
  identifyUser?: IdentifyUserCallback;
99
121
  mode: RuntimeMode;
@@ -109,6 +131,9 @@ export interface CopilotIntelligenceRuntimeLike extends CopilotRuntimeLike {
109
131
  intelligence: CopilotKitIntelligence;
110
132
  identifyUser: IdentifyUserCallback;
111
133
  generateThreadNames: boolean;
134
+ lockTtlSeconds: number;
135
+ lockKeyPrefix?: string;
136
+ lockHeartbeatIntervalSeconds: number;
112
137
  mode: RUNTIME_MODE_INTELLIGENCE;
113
138
  }
114
139
 
@@ -120,6 +145,7 @@ abstract class BaseCopilotRuntime implements CopilotRuntimeLike {
120
145
  public runner: AgentRunner;
121
146
  public a2ui: CopilotRuntimeOptions["a2ui"];
122
147
  public mcpApps: CopilotRuntimeOptions["mcpApps"];
148
+ public openGenerativeUI: CopilotRuntimeOptions["openGenerativeUI"];
123
149
  public licenseChecker?: LicenseChecker;
124
150
 
125
151
  abstract readonly intelligence?: CopilotKitIntelligence;
@@ -133,16 +159,17 @@ abstract class BaseCopilotRuntime implements CopilotRuntimeLike {
133
159
  afterRequestMiddleware,
134
160
  a2ui,
135
161
  mcpApps,
162
+ openGenerativeUI,
136
163
  } = options;
137
164
 
138
165
  this.agents = agents;
139
166
  this.transcriptionService = transcriptionService;
140
167
  this.beforeRequestMiddleware = beforeRequestMiddleware;
141
168
  this.afterRequestMiddleware = afterRequestMiddleware;
142
- this.a2ui = a2ui;
169
+ this.a2ui = a2ui || undefined;
143
170
  this.mcpApps = mcpApps;
171
+ this.openGenerativeUI = openGenerativeUI;
144
172
  this.runner = runner;
145
- this.licenseChecker = createLicenseChecker(options.licenseToken);
146
173
  }
147
174
  }
148
175
 
@@ -165,19 +192,39 @@ export class CopilotIntelligenceRuntime
165
192
  readonly intelligence: CopilotKitIntelligence;
166
193
  readonly identifyUser: IdentifyUserCallback;
167
194
  readonly generateThreadNames: boolean;
195
+ readonly lockTtlSeconds: number;
196
+ readonly lockKeyPrefix?: string;
197
+ readonly lockHeartbeatIntervalSeconds: number;
168
198
  readonly mode = RUNTIME_MODE_INTELLIGENCE;
169
199
 
200
+ /** Maximum allowed lock TTL in seconds (1 hour). */
201
+ static readonly MAX_LOCK_TTL_SECONDS = 3_600;
202
+ /** Maximum allowed heartbeat interval in seconds (50 minutes). */
203
+ static readonly MAX_HEARTBEAT_INTERVAL_SECONDS = 3_000;
204
+
170
205
  constructor(options: CopilotIntelligenceRuntimeOptions) {
171
206
  super(
172
207
  options,
173
208
  new IntelligenceAgentRunner({
174
209
  url: options.intelligence.ɵgetRunnerWsUrl(),
175
210
  authToken: options.intelligence.ɵgetRunnerAuthToken(),
211
+ maxReconnectMs: options.maxReconnectMs,
212
+ maxRejoinMs: options.maxRejoinMs,
176
213
  }),
177
214
  );
178
215
  this.intelligence = options.intelligence;
179
216
  this.identifyUser = options.identifyUser;
180
217
  this.generateThreadNames = options.generateThreadNames ?? true;
218
+ this.licenseChecker = createLicenseChecker(options.licenseToken);
219
+ this.lockTtlSeconds = Math.min(
220
+ options.lockTtlSeconds ?? 20,
221
+ CopilotIntelligenceRuntime.MAX_LOCK_TTL_SECONDS,
222
+ );
223
+ this.lockKeyPrefix = options.lockKeyPrefix;
224
+ this.lockHeartbeatIntervalSeconds = Math.min(
225
+ options.lockHeartbeatIntervalSeconds ?? 15,
226
+ CopilotIntelligenceRuntime.MAX_HEARTBEAT_INTERVAL_SECONDS,
227
+ );
181
228
  }
182
229
  }
183
230
 
@@ -234,6 +281,10 @@ export class CopilotRuntime implements CopilotRuntimeLike {
234
281
  return this.delegate.mcpApps;
235
282
  }
236
283
 
284
+ get openGenerativeUI(): CopilotRuntimeOptions["openGenerativeUI"] {
285
+ return this.delegate.openGenerativeUI;
286
+ }
287
+
237
288
  get intelligence(): CopilotKitIntelligence | undefined {
238
289
  return this.delegate.intelligence;
239
290
  }
@@ -250,6 +301,24 @@ export class CopilotRuntime implements CopilotRuntimeLike {
250
301
  : undefined;
251
302
  }
252
303
 
304
+ get lockTtlSeconds(): number | undefined {
305
+ return isIntelligenceRuntime(this.delegate)
306
+ ? this.delegate.lockTtlSeconds
307
+ : undefined;
308
+ }
309
+
310
+ get lockKeyPrefix(): string | undefined {
311
+ return isIntelligenceRuntime(this.delegate)
312
+ ? this.delegate.lockKeyPrefix
313
+ : undefined;
314
+ }
315
+
316
+ get lockHeartbeatIntervalSeconds(): number | undefined {
317
+ return isIntelligenceRuntime(this.delegate)
318
+ ? this.delegate.lockHeartbeatIntervalSeconds
319
+ : undefined;
320
+ }
321
+
253
322
  get mode(): RuntimeMode {
254
323
  return this.delegate.mode;
255
324
  }
@@ -0,0 +1,137 @@
1
+ /**
2
+ * Express-aware Node ↔ Fetch bridge.
3
+ *
4
+ * When Express body-parsing middleware (e.g. `express.json()`) runs before the
5
+ * CopilotKit router, the Node request stream is already consumed and `req.body`
6
+ * holds the parsed content. The generic `createCopilotNodeHandler` (which uses
7
+ * `@remix-run/node-fetch-server`) would hang because it tries to read from the
8
+ * exhausted stream.
9
+ *
10
+ * This module detects the pre-parsed case and re-serialises `req.body` into the
11
+ * Fetch `Request`, falling back to the generic `createCopilotNodeHandler` when the
12
+ * stream is still available.
13
+ */
14
+
15
+ import type { IncomingMessage, ServerResponse } from "node:http";
16
+ import { sendResponse } from "@remix-run/node-fetch-server";
17
+ import { createCopilotNodeHandler } from "./node-fetch-handler";
18
+ import type { CopilotRuntimeFetchHandler } from "../core/fetch-handler";
19
+ import { logger } from "@copilotkit/shared";
20
+
21
+ const METHODS_WITHOUT_BODY = new Set(["GET", "HEAD", "OPTIONS"]);
22
+
23
+ export type ExpressNodeHandler = (
24
+ req: IncomingMessage,
25
+ res: ServerResponse,
26
+ ) => Promise<void>;
27
+
28
+ /**
29
+ * Creates a Node HTTP handler from a fetch handler, with Express body-parser
30
+ * compatibility. Use this instead of `createNodeFetchHandler` in Express adapters.
31
+ *
32
+ * When the body stream hasn't been consumed, delegates to the generic
33
+ * `createCopilotNodeHandler`. Only intercepts when Express middleware has
34
+ * pre-parsed the body.
35
+ */
36
+ export function createExpressNodeHandler(
37
+ handler: CopilotRuntimeFetchHandler,
38
+ ): ExpressNodeHandler {
39
+ const nodeHandler = createCopilotNodeHandler(handler);
40
+
41
+ return async (req: IncomingMessage, res: ServerResponse) => {
42
+ const method = (req.method ?? "GET").toUpperCase();
43
+
44
+ // Fast path: if no body parser consumed the stream, use the generic handler.
45
+ if (METHODS_WITHOUT_BODY.has(method) || !hasPreParsedBody(req)) {
46
+ return nodeHandler(req, res);
47
+ }
48
+
49
+ // Slow path: body was consumed by Express middleware — rebuild the Request.
50
+ try {
51
+ const fetchReq = buildPreParsedRequest(req, res);
52
+ const fetchRes = await handler(fetchReq);
53
+ await sendResponse(res, fetchRes);
54
+ } catch (err: unknown) {
55
+ logger.error({ err }, "Error in Express fetch bridge (pre-parsed path)");
56
+ if (!res.headersSent) {
57
+ res.statusCode = 500;
58
+ res.end("Internal Server Error");
59
+ }
60
+ }
61
+ };
62
+ }
63
+
64
+ /**
65
+ * Build a Fetch Request from a Node IncomingMessage whose body stream has
66
+ * already been consumed by an Express body parser.
67
+ */
68
+ function buildPreParsedRequest(
69
+ req: IncomingMessage,
70
+ res: ServerResponse,
71
+ ): Request {
72
+ const expressReq = req as IncomingMessage & { body?: unknown };
73
+ const method = (req.method ?? "GET").toUpperCase();
74
+
75
+ const protocol = (req as any).protocol || "http";
76
+ const host = req.headers.host ?? "localhost";
77
+ const url = `${protocol}://${host}${(req as any).originalUrl ?? req.url ?? ""}`;
78
+
79
+ const headers = new Headers();
80
+ for (const [key, value] of Object.entries(req.headers)) {
81
+ if (value === undefined) continue;
82
+ if (Array.isArray(value)) {
83
+ for (const v of value) headers.append(key, v);
84
+ } else {
85
+ headers.set(key, value);
86
+ }
87
+ }
88
+
89
+ // Wire an AbortSignal so client disconnects propagate to the fetch handler
90
+ const controller = new AbortController();
91
+ res.on("close", () => {
92
+ if (!res.writableFinished) controller.abort();
93
+ });
94
+
95
+ const init: RequestInit & { duplex?: "half" } = {
96
+ method,
97
+ headers,
98
+ signal: controller.signal,
99
+ };
100
+
101
+ const { body, contentType } = synthesizeBody(expressReq.body);
102
+ if (contentType) {
103
+ headers.set("content-type", contentType);
104
+ }
105
+ headers.delete("content-length");
106
+ if (body !== undefined) {
107
+ init.body = body;
108
+ }
109
+
110
+ return new Request(url, init);
111
+ }
112
+
113
+ function hasPreParsedBody(req: IncomingMessage & { body?: unknown }): boolean {
114
+ if (req.body === undefined || req.body === null) return false;
115
+
116
+ // Check if the stream has already been consumed.
117
+ const state = (req as any)._readableState;
118
+ return Boolean(
119
+ req.readableEnded || req.complete || state?.ended || state?.endEmitted,
120
+ );
121
+ }
122
+
123
+ function synthesizeBody(body: unknown): {
124
+ body?: BodyInit;
125
+ contentType?: string;
126
+ } {
127
+ if (Buffer.isBuffer(body) || body instanceof Uint8Array) {
128
+ return { body };
129
+ }
130
+ if (typeof body === "string") {
131
+ return { body };
132
+ }
133
+ if (typeof body === "object" && body !== null) {
134
+ return { body: JSON.stringify(body), contentType: "application/json" };
135
+ }
136
+ return {};
137
+ }
@@ -1,231 +1,54 @@
1
- import express from "express";
2
- import type {
3
- Request as ExpressRequest,
4
- Response as ExpressResponse,
5
- NextFunction,
6
- Router,
7
- } from "express";
8
- import cors from "cors";
9
-
10
- import { CopilotRuntimeLike } from "../runtime";
11
- import { handleRunAgent } from "../handlers/handle-run";
12
- import { handleConnectAgent } from "../handlers/handle-connect";
13
- import { handleStopAgent } from "../handlers/handle-stop";
14
- import { handleGetRuntimeInfo } from "../handlers/get-runtime-info";
15
- import { handleTranscribe } from "../handlers/handle-transcribe";
16
- import { logger } from "@copilotkit/shared";
17
- import {
18
- callBeforeRequestMiddleware,
19
- callAfterRequestMiddleware,
20
- } from "../middleware";
21
- import {
22
- createFetchRequestFromExpress,
23
- sendFetchResponse,
24
- } from "./express-utils";
25
- import {
26
- createJsonRequest,
27
- expectString,
28
- MethodCall,
29
- parseMethodCall,
30
- } from "./single-route-helpers";
1
+ import type { Router } from "express";
2
+ import { createCopilotExpressHandler } from "./express";
3
+ import type { CopilotRuntimeLike } from "../core/runtime";
4
+ import type { CopilotRuntimeHooks } from "../core/hooks";
31
5
 
32
6
  interface CopilotSingleRouteExpressParams {
33
7
  runtime: CopilotRuntimeLike;
34
8
  basePath: string;
9
+ /**
10
+ * Lifecycle hooks for request processing.
11
+ */
12
+ hooks?: CopilotRuntimeHooks;
35
13
  }
36
14
 
15
+ /**
16
+ * Creates an Express router that serves the CopilotKit runtime as a single
17
+ * POST endpoint. Clients send a JSON envelope with `{ method, params, body }`
18
+ * to dispatch to the appropriate handler.
19
+ *
20
+ * This is a convenience wrapper around {@link createCopilotExpressHandler}
21
+ * with `mode: "single-route"` and `cors: true`.
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * import express from "express";
26
+ * import { CopilotRuntime } from "@copilotkit/runtime/v2";
27
+ * import { createCopilotEndpointSingleRouteExpress } from "@copilotkit/runtime/v2/express";
28
+ *
29
+ * const runtime = new CopilotRuntime({
30
+ * agents: { default: new BuiltInAgent({ model: "openai/gpt-4o-mini" }) },
31
+ * });
32
+ *
33
+ * const app = express();
34
+ * app.use(createCopilotEndpointSingleRouteExpress({
35
+ * runtime,
36
+ * basePath: "/api/copilotkit",
37
+ * }));
38
+ * app.listen(4000);
39
+ * ```
40
+ */
41
+ /** @deprecated Use `createCopilotExpressHandler` with `mode: "single-route"` instead. */
37
42
  export function createCopilotEndpointSingleRouteExpress({
38
43
  runtime,
39
44
  basePath,
45
+ hooks,
40
46
  }: CopilotSingleRouteExpressParams): Router {
41
- const router = express.Router();
42
- const routePath = normalizeSingleRoutePath(basePath);
43
-
44
- router.use(
45
- cors({
46
- origin: "*",
47
- methods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH", "OPTIONS"],
48
- allowedHeaders: ["*"],
49
- }),
50
- );
51
-
52
- router.post(routePath, createSingleRouteHandler(runtime));
53
-
54
- router.use((req, res) => {
55
- res.status(404).json({ error: "Not found" });
47
+ return createCopilotExpressHandler({
48
+ runtime,
49
+ basePath,
50
+ mode: "single-route",
51
+ cors: true,
52
+ hooks,
56
53
  });
57
-
58
- return router;
59
- }
60
-
61
- function createSingleRouteHandler(runtime: CopilotRuntimeLike) {
62
- return async (
63
- req: ExpressRequest,
64
- res: ExpressResponse,
65
- next: NextFunction,
66
- ) => {
67
- const path = req.originalUrl ?? req.path;
68
- let request = createFetchRequestFromExpress(req);
69
-
70
- try {
71
- const maybeModifiedRequest = await callBeforeRequestMiddleware({
72
- runtime,
73
- request,
74
- path,
75
- });
76
- if (maybeModifiedRequest) {
77
- request = maybeModifiedRequest;
78
- }
79
- } catch (error) {
80
- logger.error(
81
- { err: error, url: request.url, path },
82
- "Error running before request middleware",
83
- );
84
- if (error instanceof Response) {
85
- try {
86
- await sendFetchResponse(res, error);
87
- } catch (streamError) {
88
- next(streamError);
89
- }
90
- return;
91
- }
92
- next(error);
93
- return;
94
- }
95
-
96
- let methodCall: MethodCall;
97
- try {
98
- methodCall = await parseMethodCall(request);
99
- } catch (error) {
100
- if (error instanceof Response) {
101
- logger.warn({ url: request.url }, "Invalid single-route payload");
102
- try {
103
- await sendFetchResponse(res, error);
104
- } catch (streamError) {
105
- next(streamError);
106
- }
107
- return;
108
- }
109
- logger.warn(
110
- { err: error, url: request.url },
111
- "Invalid single-route payload",
112
- );
113
- res.status(400).json({
114
- error: "invalid_request",
115
- message:
116
- error instanceof Error ? error.message : "Invalid request payload",
117
- });
118
- return;
119
- }
120
-
121
- try {
122
- let response: Response;
123
- switch (methodCall.method) {
124
- case "agent/run": {
125
- const agentId = expectString(methodCall.params, "agentId");
126
- const handlerRequest = createJsonRequest(request, methodCall.body);
127
- response = await handleRunAgent({
128
- runtime,
129
- request: handlerRequest,
130
- agentId,
131
- });
132
- break;
133
- }
134
- case "agent/connect": {
135
- const agentId = expectString(methodCall.params, "agentId");
136
- const handlerRequest = createJsonRequest(request, methodCall.body);
137
- response = await handleConnectAgent({
138
- runtime,
139
- request: handlerRequest,
140
- agentId,
141
- });
142
- break;
143
- }
144
- case "agent/stop": {
145
- const agentId = expectString(methodCall.params, "agentId");
146
- const threadId = expectString(methodCall.params, "threadId");
147
- response = await handleStopAgent({
148
- runtime,
149
- request,
150
- agentId,
151
- threadId,
152
- });
153
- break;
154
- }
155
- case "info": {
156
- response = await handleGetRuntimeInfo({ runtime, request });
157
- break;
158
- }
159
- case "transcribe": {
160
- const handlerRequest = createJsonRequest(request, methodCall.body);
161
- response = await handleTranscribe({
162
- runtime,
163
- request: handlerRequest,
164
- });
165
- break;
166
- }
167
- default: {
168
- const exhaustive: never = methodCall.method;
169
- return exhaustive;
170
- }
171
- }
172
-
173
- const responseForMiddleware = response.clone();
174
- await sendFetchResponse(res, response);
175
- callAfterRequestMiddleware({
176
- runtime,
177
- response: responseForMiddleware,
178
- path,
179
- }).catch((error) => {
180
- logger.error(
181
- { err: error, url: req.originalUrl ?? req.url, path },
182
- "Error running after request middleware",
183
- );
184
- });
185
- } catch (error) {
186
- if (error instanceof Response) {
187
- const errorResponseForMiddleware = error.clone();
188
- try {
189
- await sendFetchResponse(res, error);
190
- } catch (streamError) {
191
- next(streamError);
192
- return;
193
- }
194
- callAfterRequestMiddleware({
195
- runtime,
196
- response: errorResponseForMiddleware,
197
- path,
198
- }).catch((mwError) => {
199
- logger.error(
200
- { err: mwError, url: req.originalUrl ?? req.url, path },
201
- "Error running after request middleware",
202
- );
203
- });
204
- return;
205
- }
206
- logger.error(
207
- { err: error, url: request.url, path },
208
- "Error running single-route handler",
209
- );
210
- next(error);
211
- }
212
- };
213
- }
214
-
215
- function normalizeSingleRoutePath(path: string): string {
216
- if (!path) {
217
- throw new Error(
218
- "basePath must be provided for Express single-route endpoint",
219
- );
220
- }
221
-
222
- if (!path.startsWith("/")) {
223
- return `/${path}`;
224
- }
225
-
226
- if (path.length > 1 && path.endsWith("/")) {
227
- return path.slice(0, -1);
228
- }
229
-
230
- return path;
231
54
  }