@copilotkit/runtime 1.57.3 → 1.58.0-canary.thread-id-propagation

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (346) hide show
  1. package/dist/lib/observability.d.cts +1 -1
  2. package/dist/lib/observability.d.cts.map +1 -1
  3. package/dist/lib/observability.d.mts +1 -1
  4. package/dist/lib/observability.d.mts.map +1 -1
  5. package/dist/lib/runtime/copilot-runtime.cjs +2 -0
  6. package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
  7. package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
  8. package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
  9. package/dist/lib/runtime/copilot-runtime.mjs +2 -0
  10. package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
  11. package/dist/package.cjs +5 -8
  12. package/dist/package.mjs +5 -8
  13. package/dist/v2/runtime/core/runtime.cjs +4 -1
  14. package/dist/v2/runtime/core/runtime.cjs.map +1 -1
  15. package/dist/v2/runtime/core/runtime.d.cts.map +1 -1
  16. package/dist/v2/runtime/core/runtime.d.mts.map +1 -1
  17. package/dist/v2/runtime/core/runtime.mjs +4 -1
  18. package/dist/v2/runtime/core/runtime.mjs.map +1 -1
  19. package/dist/v2/runtime/handlers/get-runtime-info.cjs +1 -1
  20. package/dist/v2/runtime/handlers/get-runtime-info.mjs +1 -1
  21. package/dist/v2/runtime/handlers/handle-connect.cjs +1 -1
  22. package/dist/v2/runtime/handlers/handle-connect.mjs +1 -1
  23. package/dist/v2/runtime/handlers/handle-run.cjs +1 -1
  24. package/dist/v2/runtime/handlers/handle-run.mjs +1 -1
  25. package/dist/v2/runtime/handlers/shared/agent-utils.cjs +1 -1
  26. package/dist/v2/runtime/handlers/shared/agent-utils.cjs.map +1 -1
  27. package/dist/v2/runtime/handlers/shared/agent-utils.mjs +1 -1
  28. package/dist/v2/runtime/handlers/shared/agent-utils.mjs.map +1 -1
  29. package/dist/v2/runtime/telemetry/telemetry-client.cjs +22 -6
  30. package/dist/v2/runtime/telemetry/telemetry-client.cjs.map +1 -1
  31. package/dist/v2/runtime/telemetry/telemetry-client.mjs +27 -11
  32. package/dist/v2/runtime/telemetry/telemetry-client.mjs.map +1 -1
  33. package/package.json +9 -19
  34. package/skills/runtime/SKILL.md +98 -0
  35. package/skills/runtime/references/agent-runners-custom.md +161 -0
  36. package/skills/runtime/references/agent-runners-in-memory.md +64 -0
  37. package/skills/runtime/references/agent-runners-sqlite.md +90 -0
  38. package/skills/runtime/references/agent-runners.md +304 -0
  39. package/skills/runtime/references/built-in-agent-factory-modes.md +232 -0
  40. package/skills/runtime/references/built-in-agent-helper-utilities.md +123 -0
  41. package/skills/runtime/references/built-in-agent-model-identifiers.md +59 -0
  42. package/skills/runtime/references/built-in-agent.md +523 -0
  43. package/skills/runtime/references/intelligence-mode.md +336 -0
  44. package/skills/runtime/references/middleware.md +376 -0
  45. package/skills/runtime/references/server-side-tools.md +414 -0
  46. package/skills/runtime/references/setup-endpoint.md +503 -0
  47. package/skills/runtime/references/transcription.md +287 -0
  48. package/skills/runtime/references/wiring-a2a.md +40 -0
  49. package/skills/runtime/references/wiring-adk.md +45 -0
  50. package/skills/runtime/references/wiring-ag2.md +41 -0
  51. package/skills/runtime/references/wiring-agno.md +39 -0
  52. package/skills/runtime/references/wiring-aws-strands.md +59 -0
  53. package/skills/runtime/references/wiring-crewai-crews.md +51 -0
  54. package/skills/runtime/references/wiring-crewai-flows.md +45 -0
  55. package/skills/runtime/references/wiring-external-agents.md +348 -0
  56. package/skills/runtime/references/wiring-langgraph.md +50 -0
  57. package/skills/runtime/references/wiring-llamaindex.md +39 -0
  58. package/skills/runtime/references/wiring-mastra.md +70 -0
  59. package/skills/runtime/references/wiring-mcp-apps-middleware.md +68 -0
  60. package/skills/runtime/references/wiring-ms-agent-framework.md +41 -0
  61. package/skills/runtime/references/wiring-pydantic-ai.md +45 -0
  62. package/CHANGELOG.md +0 -3624
  63. package/__snapshots__/schema/schema.graphql +0 -371
  64. package/dist/v2/runtime/telemetry/scarf-client.cjs +0 -32
  65. package/dist/v2/runtime/telemetry/scarf-client.cjs.map +0 -1
  66. package/dist/v2/runtime/telemetry/scarf-client.mjs +0 -32
  67. package/dist/v2/runtime/telemetry/scarf-client.mjs.map +0 -1
  68. package/scripts/generate-gql-schema.ts +0 -16
  69. package/src/agent/__tests__/agent-test-helpers.ts +0 -476
  70. package/src/agent/__tests__/agent.test.ts +0 -593
  71. package/src/agent/__tests__/ai-sdk-v6-compat.test.ts +0 -116
  72. package/src/agent/__tests__/basic-agent.test.ts +0 -1698
  73. package/src/agent/__tests__/capabilities.test.ts +0 -81
  74. package/src/agent/__tests__/config-tools-execution.test.ts +0 -516
  75. package/src/agent/__tests__/converter-aisdk.test.ts +0 -692
  76. package/src/agent/__tests__/converter-custom.test.ts +0 -319
  77. package/src/agent/__tests__/converter-tanstack-input.test.ts +0 -211
  78. package/src/agent/__tests__/converter-tanstack.test.ts +0 -594
  79. package/src/agent/__tests__/mcp-clients.test.ts +0 -246
  80. package/src/agent/__tests__/mcp-servers-integration.test.ts +0 -373
  81. package/src/agent/__tests__/multimodal-tanstack.test.ts +0 -284
  82. package/src/agent/__tests__/multimodal.test.ts +0 -176
  83. package/src/agent/__tests__/property-overrides.test.ts +0 -598
  84. package/src/agent/__tests__/provider-id-collision.test.ts +0 -195
  85. package/src/agent/__tests__/standard-schema-tools.test.ts +0 -313
  86. package/src/agent/__tests__/standard-schema-types.test.ts +0 -158
  87. package/src/agent/__tests__/state-tools.test.ts +0 -436
  88. package/src/agent/__tests__/test-helpers.ts +0 -197
  89. package/src/agent/__tests__/utils.test.ts +0 -536
  90. package/src/agent/__tests__/zod-regression.test.ts +0 -350
  91. package/src/agent/converters/aisdk.ts +0 -326
  92. package/src/agent/converters/index.ts +0 -7
  93. package/src/agent/converters/tanstack.ts +0 -451
  94. package/src/agent/index.ts +0 -1743
  95. package/src/agents/langgraph/__tests__/event-source.test.ts +0 -256
  96. package/src/agents/langgraph/event-source.ts +0 -365
  97. package/src/agents/langgraph/events.ts +0 -394
  98. package/src/graphql/inputs/action.input.ts +0 -16
  99. package/src/graphql/inputs/agent-session.input.ts +0 -13
  100. package/src/graphql/inputs/agent-state.input.ts +0 -13
  101. package/src/graphql/inputs/cloud-guardrails.input.ts +0 -16
  102. package/src/graphql/inputs/cloud.input.ts +0 -8
  103. package/src/graphql/inputs/context-property.input.ts +0 -10
  104. package/src/graphql/inputs/copilot-context.input.ts +0 -10
  105. package/src/graphql/inputs/custom-property.input.ts +0 -15
  106. package/src/graphql/inputs/extensions.input.ts +0 -21
  107. package/src/graphql/inputs/forwarded-parameters.input.ts +0 -22
  108. package/src/graphql/inputs/frontend.input.ts +0 -14
  109. package/src/graphql/inputs/generate-copilot-response.input.ts +0 -59
  110. package/src/graphql/inputs/load-agent-state.input.ts +0 -10
  111. package/src/graphql/inputs/message.input.ts +0 -110
  112. package/src/graphql/inputs/meta-event.input.ts +0 -18
  113. package/src/graphql/message-conversion/agui-to-gql.test.ts +0 -1384
  114. package/src/graphql/message-conversion/agui-to-gql.ts +0 -384
  115. package/src/graphql/message-conversion/gql-to-agui.test.ts +0 -1653
  116. package/src/graphql/message-conversion/gql-to-agui.ts +0 -297
  117. package/src/graphql/message-conversion/index.ts +0 -2
  118. package/src/graphql/message-conversion/roundtrip-conversion.test.ts +0 -561
  119. package/src/graphql/resolvers/__tests__/resolve-message-id.test.ts +0 -25
  120. package/src/graphql/resolvers/copilot.resolver.ts +0 -785
  121. package/src/graphql/resolvers/resolve-message-id.ts +0 -14
  122. package/src/graphql/resolvers/state.resolver.ts +0 -30
  123. package/src/graphql/types/agents-response.type.ts +0 -19
  124. package/src/graphql/types/base/index.ts +0 -10
  125. package/src/graphql/types/converted/index.ts +0 -183
  126. package/src/graphql/types/copilot-response.type.ts +0 -141
  127. package/src/graphql/types/enums.ts +0 -38
  128. package/src/graphql/types/extensions-response.type.ts +0 -23
  129. package/src/graphql/types/guardrails-result.type.ts +0 -20
  130. package/src/graphql/types/load-agent-state-response.type.ts +0 -17
  131. package/src/graphql/types/message-status.type.ts +0 -48
  132. package/src/graphql/types/meta-events.type.ts +0 -78
  133. package/src/graphql/types/response-status.type.ts +0 -77
  134. package/src/index.ts +0 -3
  135. package/src/langgraph.ts +0 -1
  136. package/src/lib/__tests__/telemetry-disclosure.test.ts +0 -55
  137. package/src/lib/cloud/index.ts +0 -4
  138. package/src/lib/error-messages.ts +0 -211
  139. package/src/lib/index.ts +0 -52
  140. package/src/lib/integrations/index.ts +0 -6
  141. package/src/lib/integrations/nest/index.ts +0 -21
  142. package/src/lib/integrations/nextjs/app-router.ts +0 -47
  143. package/src/lib/integrations/nextjs/pages-router.ts +0 -45
  144. package/src/lib/integrations/node-express/index.ts +0 -21
  145. package/src/lib/integrations/node-http/__tests__/request-duck-type.test.ts +0 -66
  146. package/src/lib/integrations/node-http/index.ts +0 -187
  147. package/src/lib/integrations/node-http/request-handler.ts +0 -128
  148. package/src/lib/integrations/shared.ts +0 -112
  149. package/src/lib/logger.ts +0 -31
  150. package/src/lib/observability.ts +0 -167
  151. package/src/lib/runtime/__tests__/copilot-runtime-error.test.ts +0 -183
  152. package/src/lib/runtime/__tests__/handle-service-adapter.test.ts +0 -108
  153. package/src/lib/runtime/__tests__/mcp-tools-utils.test.ts +0 -499
  154. package/src/lib/runtime/__tests__/on-after-request.test.ts +0 -122
  155. package/src/lib/runtime/__tests__/retry-utils.test.ts +0 -137
  156. package/src/lib/runtime/__tests__/v1-agent-factory.test.ts +0 -109
  157. package/src/lib/runtime/agent-integrations/langgraph/__tests__/dispatch-event-filtering.test.ts +0 -345
  158. package/src/lib/runtime/agent-integrations/langgraph/__tests__/run-message-filtering.test.ts +0 -156
  159. package/src/lib/runtime/agent-integrations/langgraph/agent.ts +0 -263
  160. package/src/lib/runtime/agent-integrations/langgraph/consts.ts +0 -37
  161. package/src/lib/runtime/agent-integrations/langgraph/index.ts +0 -2
  162. package/src/lib/runtime/copilot-runtime.ts +0 -863
  163. package/src/lib/runtime/mcp-tools-utils.ts +0 -313
  164. package/src/lib/runtime/retry-utils.ts +0 -141
  165. package/src/lib/runtime/telemetry-agent-runner.ts +0 -151
  166. package/src/lib/runtime/types.ts +0 -48
  167. package/src/lib/runtime/utils.ts +0 -93
  168. package/src/lib/streaming.ts +0 -220
  169. package/src/lib/telemetry-client.ts +0 -66
  170. package/src/lib/telemetry-disclosure.ts +0 -53
  171. package/src/service-adapters/anthropic/anthropic-adapter.ts +0 -532
  172. package/src/service-adapters/anthropic/utils.ts +0 -219
  173. package/src/service-adapters/bedrock/bedrock-adapter.ts +0 -73
  174. package/src/service-adapters/conversion.test.ts +0 -56
  175. package/src/service-adapters/conversion.ts +0 -69
  176. package/src/service-adapters/empty/empty-adapter.ts +0 -38
  177. package/src/service-adapters/events.ts +0 -337
  178. package/src/service-adapters/experimental/ollama/ollama-adapter.ts +0 -84
  179. package/src/service-adapters/google/google-genai-adapter.test.ts +0 -151
  180. package/src/service-adapters/google/google-genai-adapter.ts +0 -95
  181. package/src/service-adapters/groq/groq-adapter.ts +0 -229
  182. package/src/service-adapters/index.ts +0 -18
  183. package/src/service-adapters/langchain/langchain-adapter.ts +0 -113
  184. package/src/service-adapters/langchain/langserve.ts +0 -88
  185. package/src/service-adapters/langchain/types.ts +0 -20
  186. package/src/service-adapters/langchain/utils.ts +0 -330
  187. package/src/service-adapters/openai/__tests__/openai-v5-compat.test.ts +0 -177
  188. package/src/service-adapters/openai/openai-adapter.ts +0 -324
  189. package/src/service-adapters/openai/openai-assistant-adapter.ts +0 -385
  190. package/src/service-adapters/openai/utils.ts +0 -305
  191. package/src/service-adapters/service-adapter.ts +0 -50
  192. package/src/service-adapters/shared/error-utils.ts +0 -64
  193. package/src/service-adapters/shared/index.ts +0 -2
  194. package/src/service-adapters/shared/sdk-client-utils.ts +0 -19
  195. package/src/service-adapters/unify/unify-adapter.ts +0 -165
  196. package/src/utils/failed-response-status-reasons.ts +0 -70
  197. package/src/utils/index.ts +0 -1
  198. package/src/v2/express.ts +0 -1
  199. package/src/v2/hono.ts +0 -1
  200. package/src/v2/index.ts +0 -5
  201. package/src/v2/node.ts +0 -1
  202. package/src/v2/runtime/__tests__/agents-factory.test.ts +0 -136
  203. package/src/v2/runtime/__tests__/backward-compat.test.ts +0 -261
  204. package/src/v2/runtime/__tests__/code-review-fixes.test.ts +0 -500
  205. package/src/v2/runtime/__tests__/cors-credentials.test.ts +0 -320
  206. package/src/v2/runtime/__tests__/debug-sse-response.test.ts +0 -302
  207. package/src/v2/runtime/__tests__/express-adapter.test.ts +0 -188
  208. package/src/v2/runtime/__tests__/express-body-order.test.ts +0 -76
  209. package/src/v2/runtime/__tests__/express-fetch-bridge.test.ts +0 -344
  210. package/src/v2/runtime/__tests__/express-single-sse.test.ts +0 -122
  211. package/src/v2/runtime/__tests__/express-single-telemetry.integration.test.ts +0 -65
  212. package/src/v2/runtime/__tests__/express-telemetry.integration.test.ts +0 -101
  213. package/src/v2/runtime/__tests__/fetch-cors.test.ts +0 -205
  214. package/src/v2/runtime/__tests__/fetch-handler-validation.test.ts +0 -440
  215. package/src/v2/runtime/__tests__/fetch-handler.test.ts +0 -456
  216. package/src/v2/runtime/__tests__/fetch-router.test.ts +0 -276
  217. package/src/v2/runtime/__tests__/get-runtime-info.test.ts +0 -335
  218. package/src/v2/runtime/__tests__/handle-connect.test.ts +0 -585
  219. package/src/v2/runtime/__tests__/handle-run.test.ts +0 -1388
  220. package/src/v2/runtime/__tests__/handle-threads.test.ts +0 -930
  221. package/src/v2/runtime/__tests__/handle-transcribe.test.ts +0 -301
  222. package/src/v2/runtime/__tests__/header-utils.test.ts +0 -88
  223. package/src/v2/runtime/__tests__/hono-adapter.test.ts +0 -150
  224. package/src/v2/runtime/__tests__/hono-single-telemetry.integration.test.ts +0 -46
  225. package/src/v2/runtime/__tests__/hono-telemetry.integration.test.ts +0 -99
  226. package/src/v2/runtime/__tests__/hooks-edge-cases.test.ts +0 -457
  227. package/src/v2/runtime/__tests__/hooks.test.ts +0 -557
  228. package/src/v2/runtime/__tests__/in-process-agent-runner-messages.test.ts +0 -230
  229. package/src/v2/runtime/__tests__/in-process-agent-runner.test.ts +0 -1030
  230. package/src/v2/runtime/__tests__/integration/bun/bun-servers.integration.test.ts +0 -27
  231. package/src/v2/runtime/__tests__/integration/bun/elysia-multi.ts +0 -32
  232. package/src/v2/runtime/__tests__/integration/bun/elysia-single.ts +0 -33
  233. package/src/v2/runtime/__tests__/integration/bun/hono-bun-multi.ts +0 -25
  234. package/src/v2/runtime/__tests__/integration/bun/hono-bun-single.ts +0 -32
  235. package/src/v2/runtime/__tests__/integration/helpers/create-test-runtime.ts +0 -15
  236. package/src/v2/runtime/__tests__/integration/helpers/sse-reader.ts +0 -45
  237. package/src/v2/runtime/__tests__/integration/helpers/test-agent.ts +0 -58
  238. package/src/v2/runtime/__tests__/integration/node-servers.integration.test.ts +0 -58
  239. package/src/v2/runtime/__tests__/integration/servers/express-multi.ts +0 -35
  240. package/src/v2/runtime/__tests__/integration/servers/express-single.ts +0 -36
  241. package/src/v2/runtime/__tests__/integration/servers/fetch-direct.ts +0 -39
  242. package/src/v2/runtime/__tests__/integration/servers/hono-multi.ts +0 -30
  243. package/src/v2/runtime/__tests__/integration/servers/hono-single.ts +0 -37
  244. package/src/v2/runtime/__tests__/integration/servers/node-multi.ts +0 -45
  245. package/src/v2/runtime/__tests__/integration/servers/node-single.ts +0 -46
  246. package/src/v2/runtime/__tests__/integration/servers/types.ts +0 -18
  247. package/src/v2/runtime/__tests__/integration/suites/debug-events.suite.ts +0 -253
  248. package/src/v2/runtime/__tests__/integration/suites/multi-endpoint.suite.ts +0 -358
  249. package/src/v2/runtime/__tests__/integration/suites/single-endpoint.suite.ts +0 -363
  250. package/src/v2/runtime/__tests__/intelligence-run-telemetry.test.ts +0 -194
  251. package/src/v2/runtime/__tests__/mcp-apps-middleware-integration.test.ts +0 -275
  252. package/src/v2/runtime/__tests__/middleware-express.test.ts +0 -208
  253. package/src/v2/runtime/__tests__/middleware-single-express.test.ts +0 -213
  254. package/src/v2/runtime/__tests__/middleware-single.test.ts +0 -225
  255. package/src/v2/runtime/__tests__/middleware-sse-parser.test.ts +0 -237
  256. package/src/v2/runtime/__tests__/middleware.test.ts +0 -250
  257. package/src/v2/runtime/__tests__/node-fetch-handler.test.ts +0 -157
  258. package/src/v2/runtime/__tests__/open-generative-ui-middleware.e2e.test.ts +0 -728
  259. package/src/v2/runtime/__tests__/router-edge-cases.test.ts +0 -217
  260. package/src/v2/runtime/__tests__/routing-express.test.ts +0 -174
  261. package/src/v2/runtime/__tests__/routing-single-express.test.ts +0 -168
  262. package/src/v2/runtime/__tests__/routing-single.test.ts +0 -193
  263. package/src/v2/runtime/__tests__/routing.test.ts +0 -257
  264. package/src/v2/runtime/__tests__/runtime.test.ts +0 -234
  265. package/src/v2/runtime/__tests__/sse-response-telemetry.test.ts +0 -108
  266. package/src/v2/runtime/__tests__/telemetry.test.ts +0 -167
  267. package/src/v2/runtime/__tests__/thread-names.test.ts +0 -188
  268. package/src/v2/runtime/core/__tests__/debug-event-bus.test.ts +0 -156
  269. package/src/v2/runtime/core/debug-event-bus.ts +0 -45
  270. package/src/v2/runtime/core/fetch-cors.ts +0 -136
  271. package/src/v2/runtime/core/fetch-handler.ts +0 -492
  272. package/src/v2/runtime/core/fetch-router.ts +0 -203
  273. package/src/v2/runtime/core/hooks.ts +0 -160
  274. package/src/v2/runtime/core/middleware-sse-parser.ts +0 -210
  275. package/src/v2/runtime/core/middleware.ts +0 -115
  276. package/src/v2/runtime/core/runtime.ts +0 -432
  277. package/src/v2/runtime/endpoints/express-fetch-bridge.ts +0 -137
  278. package/src/v2/runtime/endpoints/express-single.ts +0 -54
  279. package/src/v2/runtime/endpoints/express.ts +0 -179
  280. package/src/v2/runtime/endpoints/hono-single.ts +0 -60
  281. package/src/v2/runtime/endpoints/hono.ts +0 -89
  282. package/src/v2/runtime/endpoints/index.ts +0 -4
  283. package/src/v2/runtime/endpoints/node-fetch-handler.ts +0 -48
  284. package/src/v2/runtime/endpoints/node.ts +0 -28
  285. package/src/v2/runtime/endpoints/single-route-helpers.ts +0 -125
  286. package/src/v2/runtime/express.ts +0 -2
  287. package/src/v2/runtime/handlers/__tests__/handle-debug-events.test.ts +0 -176
  288. package/src/v2/runtime/handlers/get-runtime-info.ts +0 -101
  289. package/src/v2/runtime/handlers/handle-connect.ts +0 -80
  290. package/src/v2/runtime/handlers/handle-debug-events.ts +0 -52
  291. package/src/v2/runtime/handlers/handle-run.ts +0 -111
  292. package/src/v2/runtime/handlers/handle-stop.ts +0 -77
  293. package/src/v2/runtime/handlers/handle-threads.ts +0 -11
  294. package/src/v2/runtime/handlers/handle-transcribe.ts +0 -269
  295. package/src/v2/runtime/handlers/header-utils.ts +0 -24
  296. package/src/v2/runtime/handlers/intelligence/connect.ts +0 -102
  297. package/src/v2/runtime/handlers/intelligence/run.ts +0 -351
  298. package/src/v2/runtime/handlers/intelligence/thread-names.ts +0 -246
  299. package/src/v2/runtime/handlers/intelligence/threads.ts +0 -420
  300. package/src/v2/runtime/handlers/shared/agent-utils.ts +0 -154
  301. package/src/v2/runtime/handlers/shared/intelligence-utils.ts +0 -41
  302. package/src/v2/runtime/handlers/shared/json-response.ts +0 -9
  303. package/src/v2/runtime/handlers/shared/resolve-intelligence-user.ts +0 -28
  304. package/src/v2/runtime/handlers/shared/sse-response.ts +0 -215
  305. package/src/v2/runtime/handlers/sse/__tests__/sse-connect-agent-id.test.ts +0 -71
  306. package/src/v2/runtime/handlers/sse/connect.ts +0 -30
  307. package/src/v2/runtime/handlers/sse/run.ts +0 -40
  308. package/src/v2/runtime/hono.ts +0 -2
  309. package/src/v2/runtime/index.ts +0 -51
  310. package/src/v2/runtime/intelligence-platform/__tests__/client.test.ts +0 -601
  311. package/src/v2/runtime/intelligence-platform/__tests__/intelligence-mcp-helper.test.ts +0 -246
  312. package/src/v2/runtime/intelligence-platform/client.ts +0 -818
  313. package/src/v2/runtime/intelligence-platform/index.ts +0 -10
  314. package/src/v2/runtime/node.ts +0 -6
  315. package/src/v2/runtime/open-generative-ui-middleware.ts +0 -373
  316. package/src/v2/runtime/runner/__tests__/finalize-events.test.ts +0 -109
  317. package/src/v2/runtime/runner/__tests__/in-memory-runner.e2e.test.ts +0 -775
  318. package/src/v2/runtime/runner/__tests__/in-memory-runner.test.ts +0 -777
  319. package/src/v2/runtime/runner/__tests__/intelligence-runner.test.ts +0 -1039
  320. package/src/v2/runtime/runner/agent-runner.ts +0 -35
  321. package/src/v2/runtime/runner/in-memory.ts +0 -467
  322. package/src/v2/runtime/runner/index.ts +0 -4
  323. package/src/v2/runtime/runner/intelligence.ts +0 -498
  324. package/src/v2/runtime/telemetry/__tests__/instance-created.test.ts +0 -96
  325. package/src/v2/runtime/telemetry/events.ts +0 -35
  326. package/src/v2/runtime/telemetry/index.ts +0 -7
  327. package/src/v2/runtime/telemetry/instance-created.ts +0 -44
  328. package/src/v2/runtime/telemetry/scarf-client.ts +0 -39
  329. package/src/v2/runtime/telemetry/telemetry-client.ts +0 -70
  330. package/src/v2/runtime/transcription-service/transcription-service.ts +0 -11
  331. package/tests/global.d.ts +0 -1
  332. package/tests/service-adapters/anthropic/allowlist-approach.test.ts +0 -259
  333. package/tests/service-adapters/anthropic/anthropic-adapter-language-model.test.ts +0 -101
  334. package/tests/service-adapters/anthropic/anthropic-adapter.test.ts +0 -645
  335. package/tests/service-adapters/anthropic/utils-token-trimming.test.ts +0 -301
  336. package/tests/service-adapters/groq/groq-adapter-language-model.test.ts +0 -102
  337. package/tests/service-adapters/openai/allowlist-approach.test.ts +0 -294
  338. package/tests/service-adapters/openai/openai-adapter-language-model.test.ts +0 -122
  339. package/tests/service-adapters/openai/openai-adapter.test.ts +0 -291
  340. package/tests/service-adapters/shared/sdk-client-utils.test.ts +0 -36
  341. package/tests/setup.vitest.ts +0 -8
  342. package/tests/tsconfig.json +0 -10
  343. package/tsconfig.json +0 -20
  344. package/tsdown.config.ts +0 -45
  345. package/typedoc.json +0 -4
  346. package/vitest.config.mjs +0 -13
@@ -1,1039 +0,0 @@
1
- import { describe, it, expect, beforeEach, vi } from "vitest";
2
- import {
3
- AbstractAgent,
4
- BaseEvent,
5
- EventType,
6
- RunAgentInput,
7
- RunStartedEvent,
8
- RunFinishedEvent,
9
- RunErrorEvent,
10
- TextMessageStartEvent,
11
- TextMessageContentEvent,
12
- TextMessageEndEvent,
13
- } from "@ag-ui/client";
14
- import { EMPTY, firstValueFrom } from "rxjs";
15
- import { toArray } from "rxjs/operators";
16
- import {
17
- MockChannel,
18
- MockSocket,
19
- } from "../../../../../../core/src/__tests__/test-utils";
20
-
21
- let mockChannels: MockChannel[] = [];
22
- let mockSockets: MockSocket[] = [];
23
-
24
- class MockSocketImpl extends MockSocket {
25
- constructor(url: string, opts?: any) {
26
- super(url, opts);
27
- mockSockets.push(this);
28
- }
29
-
30
- channel(topic: string, params: Record<string, any> = {}): MockChannel {
31
- const ch = super.channel(topic, params);
32
- mockChannels.push(ch);
33
- return ch;
34
- }
35
- }
36
-
37
- vi.mock("phoenix", () => ({
38
- Socket: MockSocketImpl,
39
- Channel: MockChannel,
40
- }));
41
-
42
- vi.mock("ws", () => ({ default: class MockWebSocket {} }));
43
-
44
- class MockAgent extends AbstractAgent {
45
- aborted = false;
46
- private events: BaseEvent[];
47
-
48
- constructor(events: BaseEvent[] = []) {
49
- super();
50
- this.events = events;
51
- }
52
-
53
- async runAgent(
54
- _input: RunAgentInput,
55
- subscriber?: { onEvent?: (arg: { event: BaseEvent }) => void },
56
- ): Promise<void> {
57
- for (const event of this.events) {
58
- subscriber?.onEvent?.({ event });
59
- }
60
- }
61
-
62
- abortRun(): void {
63
- this.aborted = true;
64
- }
65
-
66
- clone(): AbstractAgent {
67
- return new MockAgent(this.events);
68
- }
69
-
70
- protected run(): ReturnType<AbstractAgent["run"]> {
71
- return EMPTY;
72
- }
73
-
74
- protected connect(): ReturnType<AbstractAgent["connect"]> {
75
- return EMPTY;
76
- }
77
- }
78
-
79
- class ThrowingMockAgent extends AbstractAgent {
80
- aborted = false;
81
- private errorMessage: string;
82
-
83
- constructor(errorMessage = "Agent exploded") {
84
- super();
85
- this.errorMessage = errorMessage;
86
- }
87
-
88
- async runAgent(): Promise<void> {
89
- throw new Error(this.errorMessage);
90
- }
91
-
92
- abortRun(): void {
93
- this.aborted = true;
94
- }
95
-
96
- clone(): AbstractAgent {
97
- return new ThrowingMockAgent(this.errorMessage);
98
- }
99
-
100
- protected run(): ReturnType<AbstractAgent["run"]> {
101
- return EMPTY;
102
- }
103
-
104
- protected connect(): ReturnType<AbstractAgent["connect"]> {
105
- return EMPTY;
106
- }
107
- }
108
-
109
- /**
110
- * An agent whose runAgent() blocks until abortRun() is called,
111
- * then rejects — simulating how a real agent's AbortController
112
- * would cause in-flight work to fail on abort.
113
- */
114
- class BlockingMockAgent extends AbstractAgent {
115
- aborted = false;
116
- private rejectFn: ((reason: Error) => void) | null = null;
117
-
118
- async runAgent(): Promise<void> {
119
- return new Promise<void>((_resolve, reject) => {
120
- this.rejectFn = reject;
121
- });
122
- }
123
-
124
- abortRun(): void {
125
- this.aborted = true;
126
- this.rejectFn?.(new Error("Aborted"));
127
- }
128
-
129
- clone(): AbstractAgent {
130
- return new BlockingMockAgent();
131
- }
132
-
133
- protected run(): ReturnType<AbstractAgent["run"]> {
134
- return EMPTY;
135
- }
136
-
137
- protected connect(): ReturnType<AbstractAgent["connect"]> {
138
- return EMPTY;
139
- }
140
- }
141
-
142
- function createRunInput(
143
- overrides: Partial<RunAgentInput> & { threadId: string; runId: string },
144
- ): RunAgentInput {
145
- return {
146
- messages: [],
147
- tools: [],
148
- context: [],
149
- state: {},
150
- forwardedProps: undefined,
151
- ...overrides,
152
- };
153
- }
154
-
155
- async function collectEvents(
156
- observable: ReturnType<
157
- import("../intelligence").IntelligenceAgentRunner["run"]
158
- >,
159
- ) {
160
- return firstValueFrom(observable.pipe(toArray()));
161
- }
162
-
163
- // Must come after vi.mock so phoenix/ws are mocked when the module is loaded.
164
- const { IntelligenceAgentRunner } = await import("../intelligence");
165
-
166
- describe("IntelligenceAgentRunner", () => {
167
- let runner: InstanceType<typeof IntelligenceAgentRunner>;
168
-
169
- beforeEach(() => {
170
- mockChannels = [];
171
- mockSockets = [];
172
- runner = new IntelligenceAgentRunner({ url: "ws://localhost:4000/runner" });
173
- });
174
-
175
- it("passes Phoenix authToken to the runner socket when configured", () => {
176
- runner = new IntelligenceAgentRunner({
177
- url: "ws://localhost:4000/runner",
178
- authToken: "cpk_test_key",
179
- });
180
-
181
- const threadId = "t-auth";
182
- const input = createRunInput({ threadId, runId: "r-auth" });
183
- const agent = new MockAgent();
184
-
185
- const sub = runner.run({ threadId, agent, input }).subscribe();
186
-
187
- expect(mockSockets[0]?.opts).toMatchObject({
188
- authToken: "cpk_test_key",
189
- });
190
-
191
- sub.unsubscribe();
192
- });
193
-
194
- describe("run", () => {
195
- it("calls runAgent() and completes the Observable (events go to channel only)", async () => {
196
- const threadId = "t-1";
197
- const input = createRunInput({ threadId, runId: "r-1" });
198
-
199
- const agentEvents: BaseEvent[] = [
200
- {
201
- type: EventType.RUN_STARTED,
202
- threadId,
203
- runId: "r-1",
204
- } as RunStartedEvent,
205
- {
206
- type: EventType.TEXT_MESSAGE_START,
207
- messageId: "msg-1",
208
- role: "assistant",
209
- } as TextMessageStartEvent,
210
- {
211
- type: EventType.TEXT_MESSAGE_CONTENT,
212
- messageId: "msg-1",
213
- delta: "Hello",
214
- } as TextMessageContentEvent,
215
- {
216
- type: EventType.TEXT_MESSAGE_END,
217
- messageId: "msg-1",
218
- } as TextMessageEndEvent,
219
- {
220
- type: EventType.RUN_FINISHED,
221
- threadId,
222
- runId: "r-1",
223
- } as RunFinishedEvent,
224
- ];
225
- const agent = new MockAgent(agentEvents);
226
-
227
- const eventsPromise = collectEvents(
228
- runner.run({ threadId, agent, input }),
229
- );
230
- const ch = mockChannels[0];
231
- ch.triggerJoin("ok");
232
-
233
- const events = await eventsPromise;
234
-
235
- // Agent events are NOT emitted to the Observable — only to the channel.
236
- // The Observable only receives finalization events (none needed here).
237
- expect(events).toHaveLength(0);
238
- });
239
-
240
- it("pushes agent events to the Phoenix channel", async () => {
241
- const threadId = "t-push";
242
- const input = createRunInput({ threadId, runId: "r-push" });
243
-
244
- const agentEvents: BaseEvent[] = [
245
- {
246
- type: EventType.RUN_STARTED,
247
- threadId,
248
- runId: "r-push",
249
- } as RunStartedEvent,
250
- {
251
- type: EventType.TEXT_MESSAGE_CONTENT,
252
- messageId: "msg-1",
253
- delta: "hi",
254
- } as TextMessageContentEvent,
255
- {
256
- type: EventType.RUN_FINISHED,
257
- threadId,
258
- runId: "r-push",
259
- } as RunFinishedEvent,
260
- ];
261
- const agent = new MockAgent(agentEvents);
262
-
263
- const eventsPromise = collectEvents(
264
- runner.run({ threadId, agent, input }),
265
- );
266
- const ch = mockChannels[0];
267
- ch.triggerJoin("ok");
268
-
269
- await eventsPromise;
270
-
271
- // Agent events should be pushed to the ingestion channel under "event".
272
- expect(ch.pushLog.every((p) => p.event === "event")).toBe(true);
273
- const payloadTypes = ch.pushLog.map((p) => p.payload.type);
274
- expect(payloadTypes).toContain(EventType.RUN_STARTED);
275
- expect(payloadTypes).toContain(EventType.TEXT_MESSAGE_CONTENT);
276
- expect(payloadTypes).toContain(EventType.RUN_FINISHED);
277
- expect(ch.pushLog[0].payload).toMatchObject({
278
- thread_id: threadId,
279
- run_id: "r-push",
280
- });
281
- expect(ch.pushLog[0].payload.metadata.cpki_event_id).toEqual(
282
- expect.any(String),
283
- );
284
- expect(ch.pushLog[0].payload.metadata.cpki_event_seq).toBe(1);
285
- expect(ch.pushLog[1].payload.metadata.cpki_event_seq).toBe(2);
286
- expect(ch.pushLog[2].payload.metadata.cpki_event_seq).toBe(3);
287
- });
288
-
289
- it("overrides conflicting event thread and run ownership before pushing to the channel", async () => {
290
- const threadId = "t-canonical";
291
- const input = createRunInput({ threadId, runId: "r-canonical" });
292
-
293
- const agent = new MockAgent([
294
- {
295
- type: EventType.RUN_STARTED,
296
- threadId: "backend-thread",
297
- runId: "backend-run",
298
- } as RunStartedEvent,
299
- {
300
- type: EventType.RUN_FINISHED,
301
- threadId: "backend-thread",
302
- runId: "backend-run",
303
- } as RunFinishedEvent,
304
- ]);
305
-
306
- const eventsPromise = collectEvents(
307
- runner.run({ threadId, agent, input }),
308
- );
309
- const ch = mockChannels[0];
310
- ch.triggerJoin("ok");
311
-
312
- await eventsPromise;
313
-
314
- expect(ch.pushLog.map((entry) => entry.payload)).toEqual([
315
- expect.objectContaining({
316
- type: EventType.RUN_STARTED,
317
- threadId,
318
- runId: input.runId,
319
- thread_id: threadId,
320
- run_id: input.runId,
321
- }),
322
- expect.objectContaining({
323
- type: EventType.RUN_FINISHED,
324
- threadId,
325
- runId: input.runId,
326
- thread_id: threadId,
327
- run_id: input.runId,
328
- }),
329
- ]);
330
- });
331
-
332
- it("rewrites RUN_STARTED input.messages to the unseen persisted subset", async () => {
333
- const threadId = "t-persisted-input";
334
- const input = createRunInput({
335
- threadId,
336
- runId: "r-persisted-input",
337
- messages: [
338
- {
339
- id: "msg-existing",
340
- role: "user",
341
- content: "Existing",
342
- },
343
- {
344
- id: "msg-new",
345
- role: "user",
346
- content: "New",
347
- },
348
- ],
349
- });
350
-
351
- const agent = new MockAgent([
352
- {
353
- type: EventType.RUN_STARTED,
354
- threadId,
355
- runId: "r-persisted-input",
356
- input,
357
- } as RunStartedEvent,
358
- {
359
- type: EventType.RUN_FINISHED,
360
- threadId,
361
- runId: "r-persisted-input",
362
- } as RunFinishedEvent,
363
- ]);
364
-
365
- const eventsPromise = collectEvents(
366
- runner.run({
367
- threadId,
368
- agent,
369
- input,
370
- persistedInputMessages: [
371
- {
372
- id: "msg-new",
373
- role: "user",
374
- content: "New",
375
- },
376
- ],
377
- }),
378
- );
379
- const ch = mockChannels[0];
380
- ch.triggerJoin("ok");
381
-
382
- await eventsPromise;
383
-
384
- expect(ch.pushLog[0].payload.type).toBe(EventType.RUN_STARTED);
385
- expect(ch.pushLog[0].payload.input.messages).toEqual([
386
- {
387
- id: "msg-new",
388
- role: "user",
389
- content: "New",
390
- },
391
- ]);
392
- });
393
-
394
- it("synthesizes RUN_STARTED before other events when the agent omits it", async () => {
395
- const threadId = "t-synth-run-started";
396
- const input = createRunInput({
397
- threadId,
398
- runId: "r-synth-run-started",
399
- messages: [
400
- {
401
- id: "msg-new",
402
- role: "user",
403
- content: "Persist me",
404
- },
405
- ],
406
- });
407
-
408
- const agent = new MockAgent([
409
- {
410
- type: EventType.TEXT_MESSAGE_CONTENT,
411
- messageId: "msg-1",
412
- delta: "hello",
413
- } as TextMessageContentEvent,
414
- {
415
- type: EventType.RUN_FINISHED,
416
- threadId,
417
- runId: "r-synth-run-started",
418
- } as RunFinishedEvent,
419
- ]);
420
-
421
- const eventsPromise = collectEvents(
422
- runner.run({
423
- threadId,
424
- agent,
425
- input,
426
- persistedInputMessages: input.messages,
427
- }),
428
- );
429
- const ch = mockChannels[0];
430
- ch.triggerJoin("ok");
431
-
432
- await eventsPromise;
433
-
434
- expect(ch.pushLog[0].payload.type).toBe(EventType.RUN_STARTED);
435
- expect(ch.pushLog[0].payload.input.messages).toEqual(input.messages);
436
- expect(ch.pushLog[1].payload.type).toBe(EventType.TEXT_MESSAGE_CONTENT);
437
- });
438
-
439
- it("does not push any CUSTOM run event to the channel", async () => {
440
- const threadId = "t-no-custom";
441
- const input = createRunInput({ threadId, runId: "r-no-custom" });
442
-
443
- const agentEvents: BaseEvent[] = [
444
- {
445
- type: EventType.RUN_STARTED,
446
- threadId,
447
- runId: "r-no-custom",
448
- } as RunStartedEvent,
449
- {
450
- type: EventType.RUN_FINISHED,
451
- threadId,
452
- runId: "r-no-custom",
453
- } as RunFinishedEvent,
454
- ];
455
- const agent = new MockAgent(agentEvents);
456
-
457
- const eventsPromise = collectEvents(
458
- runner.run({ threadId, agent, input }),
459
- );
460
- const ch = mockChannels[0];
461
- ch.triggerJoin("ok");
462
-
463
- await eventsPromise;
464
-
465
- const customRunPush = ch.pushLog.find(
466
- (p) => p.event === EventType.CUSTOM && p.payload?.name === "run",
467
- );
468
- expect(customRunPush).toBeUndefined();
469
- });
470
-
471
- it("pushes RUN_ERROR to the channel when agent throws", async () => {
472
- const threadId = "t-err";
473
- const input = createRunInput({ threadId, runId: "r-err" });
474
- const agent = new ThrowingMockAgent("Something went wrong");
475
-
476
- const eventsPromise = collectEvents(
477
- runner.run({ threadId, agent, input }),
478
- );
479
- const ch = mockChannels[0];
480
- ch.triggerJoin("ok");
481
-
482
- await eventsPromise;
483
-
484
- const errorPush = ch.pushLog.find(
485
- (p) => p.payload?.type === EventType.RUN_ERROR,
486
- );
487
- expect(errorPush).toBeDefined();
488
- expect(errorPush!.payload).toMatchObject({
489
- type: EventType.RUN_ERROR,
490
- message: "Something went wrong",
491
- threadId,
492
- runId: input.runId,
493
- thread_id: threadId,
494
- run_id: input.runId,
495
- });
496
- });
497
-
498
- it("finalizes open message streams before completing", async () => {
499
- const threadId = "t-finalize";
500
- const input = createRunInput({ threadId, runId: "r-fin" });
501
-
502
- // Emit an unclosed text message, then RUN_FINISHED.
503
- const agentEvents: BaseEvent[] = [
504
- {
505
- type: EventType.TEXT_MESSAGE_START,
506
- messageId: "open-msg",
507
- role: "assistant",
508
- } as BaseEvent,
509
- { type: EventType.RUN_FINISHED, threadId, runId: "r-fin" } as BaseEvent,
510
- ];
511
- const agent = new MockAgent(agentEvents);
512
-
513
- const eventsPromise = collectEvents(
514
- runner.run({ threadId, agent, input }),
515
- );
516
- const ch = mockChannels[0];
517
- ch.triggerJoin("ok");
518
-
519
- await eventsPromise;
520
-
521
- // finalizeRunEvents appends TEXT_MESSAGE_END for the unclosed message.
522
- // Verify the channel received both agent and finalization events.
523
- const chPayloadTypes = ch.pushLog.map((p) => p.payload.type);
524
- expect(chPayloadTypes).toContain(EventType.RUN_STARTED);
525
- expect(chPayloadTypes).toContain(EventType.TEXT_MESSAGE_START);
526
- expect(chPayloadTypes).toContain(EventType.TEXT_MESSAGE_END);
527
- expect(ch.pushLog.map((p) => p.payload.metadata.cpki_event_seq)).toEqual([
528
- 1, 2, 3, 4,
529
- ]);
530
- });
531
-
532
- it("preserves runner event order with increasing cpki_event_seq", async () => {
533
- const threadId = "t-seq";
534
- const input = createRunInput({ threadId, runId: "r-seq" });
535
-
536
- const agent = new MockAgent([
537
- {
538
- type: EventType.TEXT_MESSAGE_CONTENT,
539
- messageId: "msg-1",
540
- delta: "first",
541
- } as TextMessageContentEvent,
542
- {
543
- type: EventType.TEXT_MESSAGE_CONTENT,
544
- messageId: "msg-1",
545
- delta: "second",
546
- } as TextMessageContentEvent,
547
- {
548
- type: EventType.TEXT_MESSAGE_CONTENT,
549
- messageId: "msg-1",
550
- delta: "third",
551
- } as TextMessageContentEvent,
552
- ]);
553
-
554
- const eventsPromise = collectEvents(
555
- runner.run({ threadId, agent, input }),
556
- );
557
- const ch = mockChannels[0];
558
- ch.triggerJoin("ok");
559
-
560
- await eventsPromise;
561
-
562
- expect(
563
- ch.pushLog.map((entry) => entry.payload.metadata.cpki_event_seq),
564
- ).toEqual([1, 2, 3, 4, 5]);
565
- });
566
-
567
- it("throws when the thread is already running", () => {
568
- const threadId = "t-dup";
569
- const input = createRunInput({ threadId, runId: "r-dup" });
570
- const agent = new MockAgent();
571
-
572
- // Start a run and subscribe so the Observable body executes.
573
- const sub = runner.run({ threadId, agent, input }).subscribe();
574
-
575
- expect(() => runner.run({ threadId, agent, input })).toThrow(
576
- "Thread already running",
577
- );
578
- sub.unsubscribe();
579
- });
580
-
581
- it("emits RUN_ERROR and completes when channel join fails", async () => {
582
- const threadId = "t-join-err";
583
- const input = createRunInput({ threadId, runId: "r-join-err" });
584
- const agent = new MockAgent();
585
-
586
- const eventsPromise = collectEvents(
587
- runner.run({ threadId, agent, input }),
588
- );
589
- const ch = mockChannels[0];
590
-
591
- ch.triggerJoin("error", { reason: "unauthorized" });
592
-
593
- const events = await eventsPromise;
594
-
595
- expect(events).toHaveLength(1);
596
- expect(events[0].type).toBe(EventType.RUN_ERROR);
597
- expect((events[0] as RunErrorEvent).message).toContain("unauthorized");
598
- });
599
-
600
- it("emits RUN_ERROR and completes when channel join times out", async () => {
601
- const threadId = "t-join-timeout";
602
- const input = createRunInput({ threadId, runId: "r-join-timeout" });
603
- const agent = new MockAgent();
604
-
605
- const eventsPromise = collectEvents(
606
- runner.run({ threadId, agent, input }),
607
- );
608
- const ch = mockChannels[0];
609
-
610
- ch.triggerJoin("timeout");
611
-
612
- const events = await eventsPromise;
613
-
614
- expect(events).toHaveLength(1);
615
- expect(events[0].type).toBe(EventType.RUN_ERROR);
616
- expect((events[0] as RunErrorEvent).message).toBe(
617
- "Timed out joining channel",
618
- );
619
- });
620
- });
621
-
622
- describe("run channel ownership", () => {
623
- it("uses runId for the ingestion channel topic", async () => {
624
- const threadId = "t-jc";
625
- const input = createRunInput({ threadId, runId: "r-jc" });
626
- const agent = new MockAgent([
627
- {
628
- type: EventType.RUN_FINISHED,
629
- threadId,
630
- runId: "r-jc",
631
- } as RunFinishedEvent,
632
- ]);
633
-
634
- const eventsPromise = collectEvents(
635
- runner.run({ threadId, agent, input }),
636
- );
637
- const ch = mockChannels[0];
638
- expect(ch.topic).toBe("ingestion:r-jc");
639
- expect(ch.params).toEqual({ thread_id: threadId, run_id: "r-jc" });
640
- ch.triggerJoin("ok");
641
- await eventsPromise;
642
- });
643
-
644
- it("keeps pushed event payload ownership on canonical threadId and runId", async () => {
645
- const threadId = "t-no-jc";
646
- const input = createRunInput({ threadId, runId: "r-no-jc" });
647
- const agent = new MockAgent([
648
- {
649
- type: EventType.RUN_FINISHED,
650
- threadId,
651
- runId: "r-no-jc",
652
- } as RunFinishedEvent,
653
- ]);
654
-
655
- const eventsPromise = collectEvents(
656
- runner.run({ threadId, agent, input }),
657
- );
658
- const ch = mockChannels[0];
659
- ch.triggerJoin("ok");
660
- await eventsPromise;
661
-
662
- expect(ch.pushLog[0].payload).toEqual(
663
- expect.objectContaining({
664
- threadId,
665
- runId: "r-no-jc",
666
- thread_id: threadId,
667
- run_id: "r-no-jc",
668
- }),
669
- );
670
- });
671
- });
672
-
673
- describe("connect", () => {
674
- it("forwards thread channel events and completes on RUN_FINISHED", async () => {
675
- const threadId = "t-connect";
676
-
677
- const eventsPromise = collectEvents(runner.connect({ threadId }));
678
- const ch = mockChannels[0];
679
- ch.triggerJoin("ok");
680
-
681
- ch.serverPush("ag_ui_event", {
682
- type: EventType.RUN_STARTED,
683
- threadId,
684
- runId: "r-hist",
685
- } as BaseEvent);
686
- ch.serverPush("ag_ui_event", {
687
- type: EventType.RUN_FINISHED,
688
- threadId,
689
- runId: "r-hist",
690
- } as BaseEvent);
691
-
692
- const events = await eventsPromise;
693
-
694
- expect(events.map((e) => e.type)).toEqual([
695
- EventType.RUN_STARTED,
696
- EventType.RUN_FINISHED,
697
- ]);
698
- });
699
-
700
- it("does not push a CUSTOM connect event after joining", () => {
701
- const threadId = "t-connect-no-push";
702
- const sub = runner.connect({ threadId }).subscribe();
703
- const ch = mockChannels[0];
704
- ch.triggerJoin("ok");
705
-
706
- expect(ch.pushLog).toHaveLength(0);
707
- sub.unsubscribe();
708
- });
709
-
710
- it("joins the thread topic when joinCode is provided", () => {
711
- const threadId = "t-connect-jc";
712
- const sub = runner
713
- .connect({ threadId, joinCode: "join-connect-456" })
714
- .subscribe();
715
- const ch = mockChannels[0];
716
-
717
- expect(ch.topic).toBe(`thread:${threadId}`);
718
- sub.unsubscribe();
719
- });
720
-
721
- it("joins the thread topic when joinCode is not provided", () => {
722
- const threadId = "t-connect-no-jc";
723
- const sub = runner.connect({ threadId }).subscribe();
724
- const ch = mockChannels[0];
725
-
726
- expect(ch.topic).toBe(`thread:${threadId}`);
727
- sub.unsubscribe();
728
- });
729
-
730
- it("errors the observable on channel join failure", async () => {
731
- let error: Error | null = null;
732
- const promise = new Promise<void>((resolve) => {
733
- runner.connect({ threadId: "t-connect-err" }).subscribe({
734
- error: (err) => {
735
- error = err;
736
- resolve();
737
- },
738
- complete: () => resolve(),
739
- });
740
- });
741
- const ch = mockChannels[0];
742
- ch.triggerJoin("error", { reason: "unauthorized" });
743
-
744
- await promise;
745
- expect(error).toBeInstanceOf(Error);
746
- expect(error!.message).toContain("Failed to join channel");
747
- });
748
-
749
- it("errors the observable on channel join timeout", async () => {
750
- let error: Error | null = null;
751
- const promise = new Promise<void>((resolve) => {
752
- runner.connect({ threadId: "t-connect-timeout" }).subscribe({
753
- error: (err) => {
754
- error = err;
755
- resolve();
756
- },
757
- complete: () => resolve(),
758
- });
759
- });
760
- const ch = mockChannels[0];
761
- ch.triggerJoin("timeout");
762
-
763
- await promise;
764
- expect(error).toBeInstanceOf(Error);
765
- expect(error!.message).toBe("Timed out joining channel");
766
- });
767
- });
768
-
769
- describe("isRunning", () => {
770
- it("returns false for unknown threads", async () => {
771
- expect(await runner.isRunning({ threadId: "nope" })).toBe(false);
772
- });
773
-
774
- it("returns true while a run is active", async () => {
775
- const threadId = "t-running";
776
- const input = createRunInput({ threadId, runId: "r-running" });
777
- const agent = new MockAgent();
778
- const sub = runner.run({ threadId, agent, input }).subscribe();
779
-
780
- expect(await runner.isRunning({ threadId })).toBe(true);
781
- sub.unsubscribe();
782
- });
783
-
784
- it("returns false after a run completes", async () => {
785
- const threadId = "t-done";
786
- const input = createRunInput({ threadId, runId: "r-done" });
787
-
788
- const agentEvents: BaseEvent[] = [
789
- {
790
- type: EventType.RUN_FINISHED,
791
- threadId,
792
- runId: "r-done",
793
- } as RunFinishedEvent,
794
- ];
795
- const agent = new MockAgent(agentEvents);
796
-
797
- const eventsPromise = collectEvents(
798
- runner.run({ threadId, agent, input }),
799
- );
800
- const ch = mockChannels[0];
801
- ch.triggerJoin("ok");
802
- await eventsPromise;
803
-
804
- expect(await runner.isRunning({ threadId })).toBe(false);
805
- });
806
- });
807
-
808
- describe("stop", () => {
809
- it("calls abortRun on the agent directly, no CUSTOM stop push", async () => {
810
- const threadId = "t-stop";
811
- const input = createRunInput({ threadId, runId: "r-stop" });
812
- const agent = new MockAgent();
813
- const sub = runner.run({ threadId, agent, input }).subscribe();
814
-
815
- const result = await runner.stop({ threadId });
816
-
817
- expect(result).toBe(true);
818
- expect(agent.aborted).toBe(true);
819
-
820
- const ch = mockChannels[0];
821
- const stopPush = ch.pushLog.find((p) => p.payload?.name === "stop");
822
- expect(stopPush).toBeUndefined();
823
- sub.unsubscribe();
824
- });
825
-
826
- it("returns false when the thread is not running", async () => {
827
- expect(await runner.stop({ threadId: "nope" })).toBe(false);
828
- });
829
-
830
- it("returns false when stop has already been requested", async () => {
831
- const threadId = "t-stop-twice";
832
- const input = createRunInput({ threadId, runId: "r-stop2" });
833
- const agent = new MockAgent();
834
- const sub = runner.run({ threadId, agent, input }).subscribe();
835
-
836
- expect(await runner.stop({ threadId })).toBe(true);
837
- expect(await runner.stop({ threadId })).toBe(false);
838
- sub.unsubscribe();
839
- });
840
- });
841
-
842
- describe("cleanup", () => {
843
- it("leaves the channel and disconnects the socket after the run completes", async () => {
844
- const threadId = "t-cleanup";
845
- const input = createRunInput({ threadId, runId: "r-cleanup" });
846
-
847
- const agentEvents: BaseEvent[] = [
848
- {
849
- type: EventType.RUN_FINISHED,
850
- threadId,
851
- runId: "r-cleanup",
852
- } as RunFinishedEvent,
853
- ];
854
- const agent = new MockAgent(agentEvents);
855
-
856
- const eventsPromise = collectEvents(
857
- runner.run({ threadId, agent, input }),
858
- );
859
- const ch = mockChannels[0];
860
- ch.triggerJoin("ok");
861
- await eventsPromise;
862
-
863
- expect(ch.left).toBe(true);
864
- // Per-run socket should also be disconnected.
865
- // mockSockets[0] is the socket created for this run.
866
- expect(mockSockets[0].disconnected).toBe(true);
867
- });
868
-
869
- it("leaves the channel and disconnects the socket after a join failure", async () => {
870
- const threadId = "t-cleanup-err";
871
- const input = createRunInput({ threadId, runId: "r-cleanup-err" });
872
- const agent = new MockAgent();
873
-
874
- const eventsPromise = collectEvents(
875
- runner.run({ threadId, agent, input }),
876
- );
877
- const ch = mockChannels[0];
878
- ch.triggerJoin("error", { reason: "denied" });
879
- await eventsPromise;
880
-
881
- expect(ch.left).toBe(true);
882
- expect(mockSockets[0].disconnected).toBe(true);
883
- });
884
- });
885
-
886
- describe("per-run socket isolation", () => {
887
- it("creates a separate socket for each run", () => {
888
- const agent = new MockAgent();
889
- const sub1 = runner
890
- .run({
891
- threadId: "t-iso-1",
892
- agent,
893
- input: createRunInput({ threadId: "t-iso-1", runId: "r-1" }),
894
- })
895
- .subscribe();
896
- const sub2 = runner
897
- .run({
898
- threadId: "t-iso-2",
899
- agent,
900
- input: createRunInput({ threadId: "t-iso-2", runId: "r-2" }),
901
- })
902
- .subscribe();
903
-
904
- // Each run should create its own socket (no shared socket).
905
- expect(mockSockets.length).toBe(2);
906
- expect(mockSockets[0]).not.toBe(mockSockets[1]);
907
-
908
- sub1.unsubscribe();
909
- sub2.unsubscribe();
910
- });
911
-
912
- it("disconnecting one run's socket does not affect another", async () => {
913
- const agent1 = new MockAgent([
914
- {
915
- type: EventType.RUN_FINISHED,
916
- threadId: "t-a",
917
- runId: "r-a",
918
- } as RunFinishedEvent,
919
- ]);
920
- const agent2 = new MockAgent();
921
-
922
- // Start two runs
923
- const promise1 = collectEvents(
924
- runner.run({
925
- threadId: "t-a",
926
- agent: agent1,
927
- input: createRunInput({ threadId: "t-a", runId: "r-a" }),
928
- }),
929
- );
930
- const sub2 = runner
931
- .run({
932
- threadId: "t-b",
933
- agent: agent2,
934
- input: createRunInput({ threadId: "t-b", runId: "r-b" }),
935
- })
936
- .subscribe();
937
-
938
- // Complete run 1
939
- mockChannels[0].triggerJoin("ok");
940
- await promise1;
941
-
942
- // Run 1's socket is disconnected, run 2's socket is untouched.
943
- expect(mockSockets[0].disconnected).toBe(true);
944
- expect(mockSockets[1].disconnected).toBe(false);
945
-
946
- sub2.unsubscribe();
947
- });
948
- });
949
-
950
- describe("socket error exhaustion", () => {
951
- it("does not abort the agent on a single socket error", () => {
952
- const threadId = "t-single-err";
953
- const input = createRunInput({ threadId, runId: "r-single-err" });
954
- const agent = new BlockingMockAgent();
955
-
956
- const sub = runner.run({ threadId, agent, input }).subscribe();
957
- const socket = mockSockets[0];
958
- mockChannels[0].triggerJoin("ok");
959
-
960
- socket.triggerError(new Error("network blip"));
961
-
962
- expect(agent.aborted).toBe(false);
963
- sub.unsubscribe();
964
- });
965
-
966
- it("aborts the agent after 5 consecutive socket errors", async () => {
967
- const threadId = "t-exhaust";
968
- const input = createRunInput({ threadId, runId: "r-exhaust" });
969
- const agent = new BlockingMockAgent();
970
-
971
- const eventsPromise = collectEvents(
972
- runner.run({ threadId, agent, input }),
973
- );
974
- const socket = mockSockets[0];
975
- mockChannels[0].triggerJoin("ok");
976
-
977
- // Fire 5 consecutive errors — should trigger abortRun()
978
- for (let i = 0; i < 5; i++) {
979
- socket.triggerError(new Error("connection lost"));
980
- }
981
-
982
- // The abort causes runAgent() to reject, which cascades through
983
- // catchError → finalize → removeThread → Observable completes.
984
- await eventsPromise;
985
-
986
- expect(agent.aborted).toBe(true);
987
- });
988
-
989
- it("resets the error counter on successful reconnection", () => {
990
- const threadId = "t-reset";
991
- const input = createRunInput({ threadId, runId: "r-reset" });
992
- const agent = new BlockingMockAgent();
993
-
994
- const sub = runner.run({ threadId, agent, input }).subscribe();
995
- const socket = mockSockets[0];
996
- mockChannels[0].triggerJoin("ok");
997
-
998
- // 4 errors (just below threshold)
999
- for (let i = 0; i < 4; i++) {
1000
- socket.triggerError();
1001
- }
1002
- expect(agent.aborted).toBe(false);
1003
-
1004
- // Successful reconnect resets the counter
1005
- socket.triggerOpen();
1006
-
1007
- // 4 more errors — still below threshold because counter was reset
1008
- for (let i = 0; i < 4; i++) {
1009
- socket.triggerError();
1010
- }
1011
- expect(agent.aborted).toBe(false);
1012
-
1013
- sub.unsubscribe();
1014
- });
1015
-
1016
- it("fully cleans up after socket error exhaustion", async () => {
1017
- const threadId = "t-exhaust-cleanup";
1018
- const input = createRunInput({ threadId, runId: "r-exhaust-cleanup" });
1019
- const agent = new BlockingMockAgent();
1020
-
1021
- const eventsPromise = collectEvents(
1022
- runner.run({ threadId, agent, input }),
1023
- );
1024
- const socket = mockSockets[0];
1025
- const ch = mockChannels[0];
1026
- ch.triggerJoin("ok");
1027
-
1028
- for (let i = 0; i < 5; i++) {
1029
- socket.triggerError();
1030
- }
1031
-
1032
- await eventsPromise;
1033
-
1034
- expect(ch.left).toBe(true);
1035
- expect(socket.disconnected).toBe(true);
1036
- expect(await runner.isRunning({ threadId })).toBe(false);
1037
- });
1038
- });
1039
- });