@copilotkit/react-core 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 (292) hide show
  1. package/LICENSE +21 -0
  2. package/dist/{copilotkit-CtXcs1ea.cjs → copilotkit-B4ouY7qC.cjs} +14 -3
  3. package/dist/copilotkit-B4ouY7qC.cjs.map +1 -0
  4. package/dist/copilotkit-BK9CVq9A.d.cts.map +1 -1
  5. package/dist/{copilotkit-CC8DjOiC.mjs → copilotkit-L4mM_JqG.mjs} +14 -3
  6. package/dist/copilotkit-L4mM_JqG.mjs.map +1 -0
  7. package/dist/copilotkit-WlmeVijs.d.mts.map +1 -1
  8. package/dist/index.cjs +3 -77
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.cts +2 -2
  11. package/dist/index.d.mts +2 -2
  12. package/dist/index.mjs +3 -77
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/index.umd.js +15 -78
  15. package/dist/index.umd.js.map +1 -1
  16. package/dist/v2/headless.cjs +11 -0
  17. package/dist/v2/headless.cjs.map +1 -1
  18. package/dist/v2/headless.d.cts.map +1 -1
  19. package/dist/v2/headless.d.mts.map +1 -1
  20. package/dist/v2/headless.mjs +11 -0
  21. package/dist/v2/headless.mjs.map +1 -1
  22. package/dist/v2/index.cjs +1 -1
  23. package/dist/v2/index.mjs +1 -1
  24. package/dist/v2/index.umd.js +13 -2
  25. package/dist/v2/index.umd.js.map +1 -1
  26. package/package.json +12 -13
  27. package/skills/react-core/SKILL.md +108 -0
  28. package/skills/react-core/references/agent-access.md +288 -0
  29. package/skills/react-core/references/attachments.md +291 -0
  30. package/skills/react-core/references/capabilities.md +138 -0
  31. package/skills/react-core/references/chat-components.md +221 -0
  32. package/skills/react-core/references/client-side-tools.md +358 -0
  33. package/skills/react-core/references/custom-message-renderers.md +226 -0
  34. package/skills/react-core/references/debug-mode.md +153 -0
  35. package/skills/react-core/references/human-in-the-loop.md +312 -0
  36. package/skills/react-core/references/provider-setup.md +326 -0
  37. package/skills/react-core/references/rendering-activity-messages.md +207 -0
  38. package/skills/react-core/references/rendering-tool-calls.md +319 -0
  39. package/skills/react-core/references/suggestions.md +211 -0
  40. package/skills/react-core/references/switching-agents-recipes.md +160 -0
  41. package/skills/react-core/references/switching-agents.md +231 -0
  42. package/skills/react-core/references/threads.md +226 -0
  43. package/.attw.json +0 -3
  44. package/CHANGELOG.md +0 -5043
  45. package/dist/copilotkit-CC8DjOiC.mjs.map +0 -1
  46. package/dist/copilotkit-CtXcs1ea.cjs.map +0 -1
  47. package/scripts/scope-preflight.mjs +0 -100
  48. package/src/components/CopilotListeners.tsx +0 -137
  49. package/src/components/__tests__/CopilotListeners.test.tsx +0 -38
  50. package/src/components/copilot-provider/__tests__/copilot-messages-key.test.tsx +0 -92
  51. package/src/components/copilot-provider/__tests__/copilotkit-error.test.tsx +0 -77
  52. package/src/components/copilot-provider/__tests__/error-visibility-prod.test.tsx +0 -70
  53. package/src/components/copilot-provider/__tests__/v1-explicit-threadid-bridge.test.tsx +0 -107
  54. package/src/components/copilot-provider/copilot-messages.tsx +0 -314
  55. package/src/components/copilot-provider/copilotkit-props.tsx +0 -214
  56. package/src/components/copilot-provider/copilotkit.tsx +0 -853
  57. package/src/components/copilot-provider/index.ts +0 -3
  58. package/src/components/dev-console/console-trigger.tsx +0 -283
  59. package/src/components/dev-console/developer-console-modal.tsx +0 -1016
  60. package/src/components/dev-console/icons.tsx +0 -106
  61. package/src/components/error-boundary/error-boundary.tsx +0 -99
  62. package/src/components/error-boundary/error-utils.tsx +0 -105
  63. package/src/components/index.ts +0 -1
  64. package/src/components/toast/exclamation-mark-icon.tsx +0 -27
  65. package/src/components/toast/toast-provider.tsx +0 -448
  66. package/src/components/usage-banner.tsx +0 -266
  67. package/src/context/__tests__/threads-context.test.tsx +0 -141
  68. package/src/context/coagent-state-renders-context.tsx +0 -89
  69. package/src/context/copilot-context.tsx +0 -365
  70. package/src/context/copilot-messages-context.tsx +0 -35
  71. package/src/context/index.ts +0 -22
  72. package/src/context/threads-context.tsx +0 -69
  73. package/src/hooks/__tests__/use-coagent-config.test.ts +0 -352
  74. package/src/hooks/__tests__/use-coagent-state-render-bridge.helpers.test.ts +0 -107
  75. package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +0 -1209
  76. package/src/hooks/__tests__/use-coagent-state-render.test.tsx +0 -356
  77. package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +0 -241
  78. package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -72
  79. package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +0 -102
  80. package/src/hooks/index.ts +0 -33
  81. package/src/hooks/use-agent-nodename.ts +0 -33
  82. package/src/hooks/use-coagent-state-render-bridge.helpers.ts +0 -345
  83. package/src/hooks/use-coagent-state-render-bridge.tsx +0 -222
  84. package/src/hooks/use-coagent-state-render-registry.ts +0 -230
  85. package/src/hooks/use-coagent-state-render.ts +0 -163
  86. package/src/hooks/use-coagent.ts +0 -377
  87. package/src/hooks/use-configure-chat-suggestions.tsx +0 -96
  88. package/src/hooks/use-copilot-action.ts +0 -245
  89. package/src/hooks/use-copilot-additional-instructions.ts +0 -98
  90. package/src/hooks/use-copilot-authenticated-action.ts +0 -73
  91. package/src/hooks/use-copilot-chat-headless_c.ts +0 -264
  92. package/src/hooks/use-copilot-chat-suggestions.tsx +0 -134
  93. package/src/hooks/use-copilot-chat.ts +0 -132
  94. package/src/hooks/use-copilot-chat_internal.ts +0 -875
  95. package/src/hooks/use-copilot-readable.ts +0 -135
  96. package/src/hooks/use-copilot-runtime-client.ts +0 -178
  97. package/src/hooks/use-default-tool.ts +0 -13
  98. package/src/hooks/use-flat-category-store.ts +0 -109
  99. package/src/hooks/use-frontend-tool.ts +0 -113
  100. package/src/hooks/use-human-in-the-loop.ts +0 -138
  101. package/src/hooks/use-langgraph-interrupt.ts +0 -103
  102. package/src/hooks/use-lazy-tool-renderer.tsx +0 -30
  103. package/src/hooks/use-make-copilot-document-readable.ts +0 -30
  104. package/src/hooks/use-render-tool-call.ts +0 -89
  105. package/src/hooks/use-tree.ts +0 -222
  106. package/src/index.tsx +0 -7
  107. package/src/lib/copilot-task.ts +0 -215
  108. package/src/lib/index.ts +0 -1
  109. package/src/lib/status-checker.ts +0 -67
  110. package/src/setupTests.ts +0 -37
  111. package/src/test-helpers/copilot-context.ts +0 -91
  112. package/src/types/chat-suggestion-configuration.ts +0 -23
  113. package/src/types/coagent-action.ts +0 -35
  114. package/src/types/coagent-state.ts +0 -13
  115. package/src/types/crew.ts +0 -89
  116. package/src/types/document-pointer.ts +0 -7
  117. package/src/types/frontend-action.ts +0 -213
  118. package/src/types/index.ts +0 -17
  119. package/src/types/interrupt-action.ts +0 -58
  120. package/src/types/system-message.ts +0 -4
  121. package/src/utils/dev-console.ts +0 -19
  122. package/src/utils/index.ts +0 -2
  123. package/src/utils/suggestions-constants.ts +0 -8
  124. package/src/utils/utils.test.ts +0 -7
  125. package/src/utils/utils.ts +0 -6
  126. package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +0 -240
  127. package/src/v2/__tests__/globalSetup.ts +0 -14
  128. package/src/v2/__tests__/setup.ts +0 -93
  129. package/src/v2/__tests__/utils/test-helpers.tsx +0 -570
  130. package/src/v2/a2ui/A2UICatalogContext.tsx +0 -79
  131. package/src/v2/a2ui/A2UIMessageRenderer.tsx +0 -294
  132. package/src/v2/a2ui/A2UIToolCallRenderer.tsx +0 -290
  133. package/src/v2/components/CopilotKitInspector.tsx +0 -52
  134. package/src/v2/components/MCPAppsActivityRenderer.tsx +0 -815
  135. package/src/v2/components/OpenGenerativeUIRenderer.tsx +0 -598
  136. package/src/v2/components/WildcardToolCallRender.tsx +0 -86
  137. package/src/v2/components/__tests__/OpenGenerativeUIRenderer.test.tsx +0 -665
  138. package/src/v2/components/chat/CopilotChat.tsx +0 -664
  139. package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +0 -393
  140. package/src/v2/components/chat/CopilotChatAttachmentQueue.tsx +0 -374
  141. package/src/v2/components/chat/CopilotChatAttachmentRenderer.tsx +0 -159
  142. package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +0 -350
  143. package/src/v2/components/chat/CopilotChatInput.tsx +0 -1412
  144. package/src/v2/components/chat/CopilotChatMessageView.tsx +0 -716
  145. package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +0 -265
  146. package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +0 -59
  147. package/src/v2/components/chat/CopilotChatSuggestionView.tsx +0 -134
  148. package/src/v2/components/chat/CopilotChatToggleButton.tsx +0 -171
  149. package/src/v2/components/chat/CopilotChatToolCallsView.tsx +0 -40
  150. package/src/v2/components/chat/CopilotChatUserMessage.tsx +0 -445
  151. package/src/v2/components/chat/CopilotChatView.tsx +0 -890
  152. package/src/v2/components/chat/CopilotModalHeader.tsx +0 -129
  153. package/src/v2/components/chat/CopilotPopup.tsx +0 -81
  154. package/src/v2/components/chat/CopilotPopupView.tsx +0 -317
  155. package/src/v2/components/chat/CopilotSidebar.tsx +0 -80
  156. package/src/v2/components/chat/CopilotSidebarView.tsx +0 -269
  157. package/src/v2/components/chat/Lightbox.tsx +0 -103
  158. package/src/v2/components/chat/__tests__/CopilotChat.absentThreadConnect.test.tsx +0 -66
  159. package/src/v2/components/chat/__tests__/CopilotChat.attachments.test.tsx +0 -168
  160. package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +0 -1239
  161. package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +0 -73
  162. package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +0 -432
  163. package/src/v2/components/chat/__tests__/CopilotChat.suggestionsAlways.test.tsx +0 -183
  164. package/src/v2/components/chat/__tests__/CopilotChat.welcomeGate.test.tsx +0 -184
  165. package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +0 -649
  166. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +0 -624
  167. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +0 -702
  168. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.thumbs.test.tsx +0 -72
  169. package/src/v2/components/chat/__tests__/CopilotChatCopyButton.clipboard.test.tsx +0 -241
  170. package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +0 -107
  171. package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +0 -929
  172. package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +0 -1567
  173. package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +0 -1004
  174. package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +0 -279
  175. package/src/v2/components/chat/__tests__/CopilotChatPerf.e2e.test.tsx +0 -336
  176. package/src/v2/components/chat/__tests__/CopilotChatPropsRerender.e2e.test.tsx +0 -249
  177. package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +0 -530
  178. package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +0 -785
  179. package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +0 -2416
  180. package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +0 -621
  181. package/src/v2/components/chat/__tests__/CopilotChatView.connectingGate.test.tsx +0 -56
  182. package/src/v2/components/chat/__tests__/CopilotChatView.inputOverlay.test.tsx +0 -264
  183. package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +0 -853
  184. package/src/v2/components/chat/__tests__/CopilotChatView.pinToSend.test.tsx +0 -94
  185. package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +0 -1050
  186. package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +0 -484
  187. package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +0 -612
  188. package/src/v2/components/chat/__tests__/CopilotSidebarView.position.test.tsx +0 -159
  189. package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +0 -502
  190. package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +0 -1068
  191. package/src/v2/components/chat/__tests__/MCPAppsProxy.e2e.test.tsx +0 -589
  192. package/src/v2/components/chat/__tests__/MCPAppsUiMessage.e2e.test.tsx +0 -403
  193. package/src/v2/components/chat/__tests__/copilot-chat-throttle.test.tsx +0 -137
  194. package/src/v2/components/chat/__tests__/normalize-auto-scroll.test.ts +0 -37
  195. package/src/v2/components/chat/__tests__/setup.ts +0 -1
  196. package/src/v2/components/chat/index.ts +0 -90
  197. package/src/v2/components/chat/last-user-message-context.ts +0 -21
  198. package/src/v2/components/chat/normalize-auto-scroll.ts +0 -17
  199. package/src/v2/components/chat/scroll-element-context.ts +0 -13
  200. package/src/v2/components/index.ts +0 -8
  201. package/src/v2/components/intelligence-indicator/IntelligenceIndicator.tsx +0 -286
  202. package/src/v2/components/intelligence-indicator/__tests__/IntelligenceIndicator.e2e.test.tsx +0 -464
  203. package/src/v2/components/intelligence-indicator/index.ts +0 -2
  204. package/src/v2/components/license-warning-banner.tsx +0 -217
  205. package/src/v2/components/ui/button.tsx +0 -124
  206. package/src/v2/components/ui/dropdown-menu.tsx +0 -258
  207. package/src/v2/components/ui/tooltip.tsx +0 -60
  208. package/src/v2/context.ts +0 -62
  209. package/src/v2/headless.ts +0 -64
  210. package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +0 -152
  211. package/src/v2/hooks/__tests__/standard-schema.test.tsx +0 -282
  212. package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +0 -140
  213. package/src/v2/hooks/__tests__/use-agent-context.test.tsx +0 -401
  214. package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +0 -44
  215. package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +0 -211
  216. package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +0 -1029
  217. package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +0 -159
  218. package/src/v2/hooks/__tests__/use-attachments.test.tsx +0 -169
  219. package/src/v2/hooks/__tests__/use-capabilities.test.tsx +0 -76
  220. package/src/v2/hooks/__tests__/use-component.test.tsx +0 -126
  221. package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +0 -696
  222. package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +0 -153
  223. package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -167
  224. package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +0 -2148
  225. package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +0 -1261
  226. package/src/v2/hooks/__tests__/use-interrupt.test.tsx +0 -397
  227. package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +0 -56
  228. package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +0 -192
  229. package/src/v2/hooks/__tests__/use-pin-to-send.test.tsx +0 -219
  230. package/src/v2/hooks/__tests__/use-render-custom-messages.test.tsx +0 -55
  231. package/src/v2/hooks/__tests__/use-render-tool.test.tsx +0 -259
  232. package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +0 -524
  233. package/src/v2/hooks/__tests__/use-threads.test.tsx +0 -757
  234. package/src/v2/hooks/__tests__/zod-regression.test.tsx +0 -311
  235. package/src/v2/hooks/index.ts +0 -24
  236. package/src/v2/hooks/use-agent-context.tsx +0 -45
  237. package/src/v2/hooks/use-agent.tsx +0 -227
  238. package/src/v2/hooks/use-attachments.tsx +0 -269
  239. package/src/v2/hooks/use-capabilities.tsx +0 -25
  240. package/src/v2/hooks/use-component.tsx +0 -91
  241. package/src/v2/hooks/use-configure-suggestions.tsx +0 -236
  242. package/src/v2/hooks/use-default-render-tool.tsx +0 -271
  243. package/src/v2/hooks/use-frontend-tool.tsx +0 -46
  244. package/src/v2/hooks/use-human-in-the-loop.tsx +0 -81
  245. package/src/v2/hooks/use-interrupt.tsx +0 -305
  246. package/src/v2/hooks/use-keyboard-height.tsx +0 -67
  247. package/src/v2/hooks/use-pin-to-send.ts +0 -94
  248. package/src/v2/hooks/use-render-activity-message.tsx +0 -72
  249. package/src/v2/hooks/use-render-custom-messages.tsx +0 -93
  250. package/src/v2/hooks/use-render-tool-call.tsx +0 -208
  251. package/src/v2/hooks/use-render-tool.tsx +0 -184
  252. package/src/v2/hooks/use-suggestions.tsx +0 -91
  253. package/src/v2/hooks/use-threads.tsx +0 -325
  254. package/src/v2/hooks/useKatexStyles.ts +0 -27
  255. package/src/v2/index.css +0 -1
  256. package/src/v2/index.ts +0 -27
  257. package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +0 -495
  258. package/src/v2/lib/__tests__/processPartialHtml.test.ts +0 -112
  259. package/src/v2/lib/__tests__/renderSlot.test.tsx +0 -588
  260. package/src/v2/lib/__tests__/slots.test.ts +0 -56
  261. package/src/v2/lib/processPartialHtml.ts +0 -45
  262. package/src/v2/lib/react-core.ts +0 -156
  263. package/src/v2/lib/slots.tsx +0 -184
  264. package/src/v2/lib/transcription-client.ts +0 -184
  265. package/src/v2/lib/utils.ts +0 -8
  266. package/src/v2/providers/CopilotChatConfigurationProvider.tsx +0 -196
  267. package/src/v2/providers/CopilotKitProvider.tsx +0 -800
  268. package/src/v2/providers/SandboxFunctionsContext.ts +0 -10
  269. package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +0 -652
  270. package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +0 -101
  271. package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +0 -69
  272. package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +0 -881
  273. package/src/v2/providers/__tests__/CopilotKitProvider.sandboxFunctions.test.tsx +0 -198
  274. package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +0 -740
  275. package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +0 -713
  276. package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +0 -294
  277. package/src/v2/providers/index.ts +0 -21
  278. package/src/v2/styles/globals.css +0 -349
  279. package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +0 -525
  280. package/src/v2/types/defineToolCallRenderer.ts +0 -68
  281. package/src/v2/types/frontend-tool.ts +0 -8
  282. package/src/v2/types/human-in-the-loop.ts +0 -33
  283. package/src/v2/types/index.ts +0 -8
  284. package/src/v2/types/interrupt.ts +0 -15
  285. package/src/v2/types/react-activity-message-renderer.ts +0 -27
  286. package/src/v2/types/react-custom-message-renderer.ts +0 -17
  287. package/src/v2/types/react-tool-call-renderer.ts +0 -35
  288. package/src/v2/types/sandbox-function.ts +0 -11
  289. package/tsconfig.json +0 -8
  290. package/tsdown.config.ts +0 -193
  291. package/typedoc.json +0 -4
  292. package/vitest.config.mjs +0 -31
@@ -1,1239 +0,0 @@
1
- import React, { useEffect } from "react";
2
- import { screen, fireEvent, waitFor, act } from "@testing-library/react";
3
- import { z } from "zod";
4
- import { defineToolCallRenderer, ReactToolCallRenderer } from "../../../types";
5
- import {
6
- MockStepwiseAgent,
7
- SuggestionsProviderAgent,
8
- renderWithCopilotKit,
9
- runStartedEvent,
10
- runFinishedEvent,
11
- textChunkEvent,
12
- toolCallChunkEvent,
13
- toolCallResultEvent,
14
- testId,
15
- emitSuggestionToolCall,
16
- emitReasoningSequence,
17
- reasoningStartEvent,
18
- reasoningMessageStartEvent,
19
- reasoningMessageContentEvent,
20
- reasoningMessageEndEvent,
21
- reasoningEndEvent,
22
- } from "../../../__tests__/utils/test-helpers";
23
- import { useConfigureSuggestions } from "../../../hooks/use-configure-suggestions";
24
- import { CopilotChat } from "../CopilotChat";
25
-
26
- describe("CopilotChat E2E - Chat Basics and Streaming Patterns", () => {
27
- describe("Chat Basics: text input + run", () => {
28
- it("should display user message and start agent run when Enter is pressed", async () => {
29
- const agent = new MockStepwiseAgent();
30
- renderWithCopilotKit({ agent });
31
-
32
- // Type a message and press Enter
33
- const input = await screen.findByRole("textbox");
34
- fireEvent.change(input, { target: { value: "Hello AI!" } });
35
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
36
-
37
- // User message should appear
38
- await waitFor(() => {
39
- const userMessage = screen.getByText("Hello AI!");
40
- expect(userMessage).toBeDefined();
41
- });
42
-
43
- // Agent starts running
44
- const messageId = testId("msg");
45
- agent.emit(runStartedEvent());
46
- agent.emit(textChunkEvent(messageId, "Hello! "));
47
- agent.emit(textChunkEvent(messageId, "How can I help you today?"));
48
- agent.emit(runFinishedEvent());
49
- agent.complete();
50
-
51
- // Assistant message should accumulate
52
- await waitFor(() => {
53
- const assistantMessage = screen.getByText(
54
- "Hello! How can I help you today?",
55
- );
56
- expect(assistantMessage).toBeDefined();
57
- });
58
- });
59
-
60
- it("should accumulate text chunks progressively", async () => {
61
- const agent = new MockStepwiseAgent();
62
- renderWithCopilotKit({ agent });
63
-
64
- // Submit a message
65
- const input = await screen.findByRole("textbox");
66
- fireEvent.change(input, { target: { value: "Tell me a story" } });
67
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
68
-
69
- // Wait for user message to appear
70
- await waitFor(() => {
71
- expect(screen.getByText("Tell me a story")).toBeDefined();
72
- });
73
-
74
- const messageId = testId("msg");
75
- agent.emit(runStartedEvent());
76
-
77
- // Stream text progressively
78
- agent.emit(textChunkEvent(messageId, "Once upon"));
79
-
80
- await waitFor(() => {
81
- expect(screen.getByText(/Once upon/)).toBeDefined();
82
- });
83
-
84
- agent.emit(textChunkEvent(messageId, " a time"));
85
-
86
- await waitFor(() => {
87
- expect(screen.getByText(/Once upon a time/)).toBeDefined();
88
- });
89
-
90
- agent.emit(textChunkEvent(messageId, " there was a robot."));
91
-
92
- await waitFor(() => {
93
- expect(
94
- screen.getByText(/Once upon a time there was a robot\./),
95
- ).toBeDefined();
96
- });
97
-
98
- agent.emit(runFinishedEvent());
99
- agent.complete();
100
- });
101
- });
102
-
103
- describe("Single Tool Flow", () => {
104
- it("should handle complete tool call lifecycle", async () => {
105
- const agent = new MockStepwiseAgent();
106
- const renderToolCalls = [
107
- defineToolCallRenderer({
108
- name: "getWeather",
109
- args: z.object({
110
- location: z.string(),
111
- unit: z.string().optional(),
112
- }),
113
- render: ({ name, args, result, status }) => (
114
- <div data-testid="weather-tool">
115
- Tool: {name} | Status: {status} | Location: {args.location} |
116
- {result && ` Result: ${JSON.stringify(result)}`}
117
- </div>
118
- ),
119
- }),
120
- ] as unknown as ReactToolCallRenderer<unknown>[];
121
-
122
- renderWithCopilotKit({ agent, renderToolCalls });
123
-
124
- // Submit message
125
- const input = await screen.findByRole("textbox");
126
- fireEvent.change(input, { target: { value: "What's the weather?" } });
127
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
128
-
129
- // Wait for user message to appear
130
- await waitFor(() => {
131
- expect(screen.getByText("What's the weather?")).toBeDefined();
132
- });
133
-
134
- const messageId = testId("msg");
135
- const toolCallId = testId("tc");
136
-
137
- // Stream: RUN_STARTED → TEXT_MESSAGE_CHUNK → TOOL_CALL_CHUNK → TOOL_CALL_RESULT → RUN_FINISHED
138
- agent.emit(runStartedEvent());
139
- agent.emit(
140
- textChunkEvent(messageId, "Let me check the weather for you."),
141
- );
142
-
143
- // Start tool call with partial args
144
- agent.emit(
145
- toolCallChunkEvent({
146
- toolCallId,
147
- toolCallName: "getWeather",
148
- parentMessageId: messageId,
149
- delta: '{"location":"Paris"',
150
- }),
151
- );
152
-
153
- // Continue streaming args
154
- agent.emit(
155
- toolCallChunkEvent({
156
- toolCallId,
157
- parentMessageId: messageId,
158
- delta: ',"unit":"celsius"}',
159
- }),
160
- );
161
-
162
- // Wait for tool to render with complete args and verify name is provided
163
- await waitFor(() => {
164
- const tool = screen.getByTestId("weather-tool");
165
- expect(tool.textContent).toContain("Tool: getWeather");
166
- expect(tool.textContent).toContain("Location: Paris");
167
- });
168
-
169
- // Send tool result
170
- agent.emit(
171
- toolCallResultEvent({
172
- toolCallId,
173
- messageId: `${messageId}_result`,
174
- content: JSON.stringify({ temperature: 22, condition: "Sunny" }),
175
- }),
176
- );
177
-
178
- // Check result appears
179
- await waitFor(() => {
180
- const tool = screen.getByTestId("weather-tool");
181
- expect(tool.textContent).toContain("temperature");
182
- expect(tool.textContent).toContain("22");
183
- expect(tool.textContent).toContain("Sunny");
184
- });
185
-
186
- agent.emit(runFinishedEvent());
187
- agent.complete();
188
- });
189
- });
190
-
191
- describe("Multiple Tools Interleaved", () => {
192
- it("should handle multiple tool calls in one assistant message", async () => {
193
- const agent = new MockStepwiseAgent();
194
- const renderToolCalls = [
195
- defineToolCallRenderer({
196
- name: "getWeather",
197
- args: z.object({ location: z.string() }),
198
- render: ({ name, args, result }) => (
199
- <div data-testid={`weather-${args.location}`}>
200
- [{name}] Weather for {args.location}:{" "}
201
- {result ? JSON.stringify(result) : "Loading..."}
202
- </div>
203
- ),
204
- }),
205
- defineToolCallRenderer({
206
- name: "getTime",
207
- args: z.object({ timezone: z.string() }),
208
- render: ({ name, args, result }) => (
209
- <div data-testid={`time-${args.timezone}`}>
210
- [{name}] Time in {args.timezone}:{" "}
211
- {result ? JSON.stringify(result) : "Loading..."}
212
- </div>
213
- ),
214
- }),
215
- ] as unknown as ReactToolCallRenderer<unknown>[];
216
-
217
- renderWithCopilotKit({ agent, renderToolCalls });
218
-
219
- // Submit message
220
- const input = await screen.findByRole("textbox");
221
- fireEvent.change(input, { target: { value: "Weather and time please" } });
222
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
223
-
224
- // Wait for user message to appear
225
- await waitFor(() => {
226
- expect(screen.getByText("Weather and time please")).toBeDefined();
227
- });
228
-
229
- const messageId = testId("msg");
230
- const toolCallId1 = testId("tc1");
231
- const toolCallId2 = testId("tc2");
232
-
233
- agent.emit(runStartedEvent());
234
- agent.emit(textChunkEvent(messageId, "I'll check both for you."));
235
-
236
- // Start first tool call (weather) with complete JSON in one chunk
237
- agent.emit(
238
- toolCallChunkEvent({
239
- toolCallId: toolCallId1,
240
- toolCallName: "getWeather",
241
- parentMessageId: messageId,
242
- delta: '{"location":"London"}',
243
- }),
244
- );
245
-
246
- // Start second tool call (time) with complete JSON in one chunk
247
- agent.emit(
248
- toolCallChunkEvent({
249
- toolCallId: toolCallId2,
250
- toolCallName: "getTime",
251
- parentMessageId: messageId,
252
- delta: '{"timezone":"UTC"}',
253
- }),
254
- );
255
-
256
- // Both tools should render with partial/complete args
257
- await waitFor(() => {
258
- expect(screen.getByTestId("weather-London")).toBeDefined();
259
- expect(screen.getByTestId("time-UTC")).toBeDefined();
260
- });
261
-
262
- // Send results in different order
263
- agent.emit(
264
- toolCallResultEvent({
265
- toolCallId: toolCallId2,
266
- messageId: `${messageId}_result2`,
267
- content: JSON.stringify({ time: "12:00 PM" }),
268
- }),
269
- );
270
-
271
- agent.emit(
272
- toolCallResultEvent({
273
- toolCallId: toolCallId1,
274
- messageId: `${messageId}_result1`,
275
- content: JSON.stringify({ temp: 18, condition: "Cloudy" }),
276
- }),
277
- );
278
-
279
- // Both results should appear with correct names
280
- await waitFor(() => {
281
- const weatherTool = screen.getByTestId("weather-London");
282
- const timeTool = screen.getByTestId("time-UTC");
283
-
284
- expect(weatherTool.textContent).toContain("[getWeather]");
285
- expect(weatherTool.textContent).toContain("18");
286
- expect(weatherTool.textContent).toContain("Cloudy");
287
- expect(timeTool.textContent).toContain("[getTime]");
288
- expect(timeTool.textContent).toContain("12:00 PM");
289
- });
290
-
291
- agent.emit(runFinishedEvent());
292
- agent.complete();
293
- });
294
- });
295
-
296
- describe("Wildcard Fallback", () => {
297
- it("should use wildcard renderer when no specific renderer exists", async () => {
298
- const agent = new MockStepwiseAgent();
299
- const renderToolCalls = [
300
- defineToolCallRenderer({
301
- name: "*",
302
- args: z.any(),
303
- render: ({ name, args }) => (
304
- <div data-testid="wildcard-renderer">
305
- Unknown tool: {name} with args: {JSON.stringify(args)}
306
- </div>
307
- ),
308
- }),
309
- ] as unknown as ReactToolCallRenderer<unknown>[];
310
-
311
- renderWithCopilotKit({ agent, renderToolCalls });
312
-
313
- // Submit message
314
- const input = await screen.findByRole("textbox");
315
- fireEvent.change(input, { target: { value: "Do something unknown" } });
316
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
317
-
318
- // Wait for user message to appear
319
- await waitFor(() => {
320
- expect(screen.getByText("Do something unknown")).toBeDefined();
321
- });
322
-
323
- const messageId = testId("msg");
324
- const toolCallId = testId("tc");
325
-
326
- agent.emit(runStartedEvent());
327
-
328
- // Call an undefined tool
329
- agent.emit(
330
- toolCallChunkEvent({
331
- toolCallId,
332
- toolCallName: "unknownTool",
333
- parentMessageId: messageId,
334
- delta: '{"param":"value"}',
335
- }),
336
- );
337
-
338
- // Wildcard renderer should handle it
339
- await waitFor(() => {
340
- const wildcard = screen.getByTestId("wildcard-renderer");
341
- expect(wildcard).toBeDefined();
342
- // Check that the wildcard renders with the tool name
343
- expect(wildcard.textContent).toContain("Unknown tool: unknownTool");
344
- expect(wildcard.textContent).toContain("value");
345
- });
346
-
347
- agent.emit(runFinishedEvent());
348
- agent.complete();
349
- });
350
-
351
- it("should use wildcard renderer without args definition", async () => {
352
- const agent = new MockStepwiseAgent();
353
- // Test that wildcard tool works without explicit args definition
354
- const renderToolCalls = [
355
- defineToolCallRenderer({
356
- name: "*",
357
- // No args field - should default to z.any()
358
- render: ({ name, args }) => (
359
- <div data-testid="wildcard-no-args">
360
- <span data-testid="tool-name">{name}</span>
361
- <span data-testid="tool-args">{JSON.stringify(args)}</span>
362
- </div>
363
- ),
364
- }),
365
- ] as unknown as ReactToolCallRenderer<unknown>[];
366
-
367
- renderWithCopilotKit({ agent, renderToolCalls });
368
-
369
- // Submit message
370
- const input = await screen.findByRole("textbox");
371
- fireEvent.change(input, { target: { value: "Do something" } });
372
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
373
-
374
- // Wait for user message to appear
375
- await waitFor(() => {
376
- expect(screen.getByText("Do something")).toBeDefined();
377
- });
378
-
379
- const messageId = testId("msg");
380
- const toolCallId = testId("tc");
381
-
382
- agent.emit(runStartedEvent());
383
-
384
- // Call an undefined tool with a specific name
385
- agent.emit(
386
- toolCallChunkEvent({
387
- toolCallId,
388
- toolCallName: "myCustomTool",
389
- parentMessageId: messageId,
390
- delta: '{"param":"test","value":123}',
391
- }),
392
- );
393
-
394
- // Wildcard renderer should receive the actual tool name, not "*"
395
- await waitFor(() => {
396
- const wildcard = screen.getByTestId("wildcard-no-args");
397
- expect(wildcard).toBeDefined();
398
-
399
- // Verify the actual tool name is passed, not "*"
400
- const toolName = screen.getByTestId("tool-name");
401
- expect(toolName.textContent).toBe("myCustomTool");
402
- expect(toolName.textContent).not.toBe("*");
403
-
404
- // Verify args are passed correctly
405
- const toolArgs = screen.getByTestId("tool-args");
406
- const parsedArgs = JSON.parse(toolArgs.textContent || "{}");
407
- expect(parsedArgs.param).toBe("test");
408
- expect(parsedArgs.value).toBe(123);
409
- });
410
-
411
- agent.emit(runFinishedEvent());
412
- agent.complete();
413
- });
414
-
415
- it("should not show toolbar for messages with only tool calls and no content", async () => {
416
- const agent = new MockStepwiseAgent();
417
- const renderToolCalls = [
418
- defineToolCallRenderer({
419
- name: "testTool",
420
- args: z.object({ value: z.string() }),
421
- render: ({ args }) => (
422
- <div data-testid="test-tool">Tool: {args.value}</div>
423
- ),
424
- }),
425
- ] as unknown as ReactToolCallRenderer<unknown>[];
426
-
427
- renderWithCopilotKit({ agent, renderToolCalls });
428
-
429
- // Submit message
430
- const input = await screen.findByRole("textbox");
431
- fireEvent.change(input, { target: { value: "Use test tool" } });
432
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
433
-
434
- // Wait for user message to appear
435
- await waitFor(() => {
436
- expect(screen.getByText("Use test tool")).toBeDefined();
437
- });
438
-
439
- const messageId = testId("msg");
440
- const toolCallId = testId("tc");
441
-
442
- agent.emit(runStartedEvent());
443
-
444
- // Emit tool call WITHOUT any text content
445
- agent.emit(
446
- toolCallChunkEvent({
447
- toolCallId,
448
- toolCallName: "testTool",
449
- parentMessageId: messageId,
450
- delta: '{"value":"test"}',
451
- }),
452
- );
453
-
454
- // Tool call should be rendered
455
- await waitFor(() => {
456
- const toolRender = screen.getByTestId("test-tool");
457
- expect(toolRender).toBeDefined();
458
- expect(toolRender.textContent).toContain("Tool: test");
459
- });
460
-
461
- // Toolbar should NOT be visible for assistant message since it has no text content
462
- await waitFor(() => {
463
- // Find the assistant message container (it should have the tool render)
464
- const assistantMessageDiv = screen
465
- .getByTestId("test-tool")
466
- .closest("[data-message-id]");
467
-
468
- if (assistantMessageDiv) {
469
- // Check that within the assistant message, there's no copy button
470
- const copyButtonsInAssistant = assistantMessageDiv.querySelectorAll(
471
- "button[aria-label*='Copy' i], button[aria-label*='copy' i]",
472
- );
473
- expect(copyButtonsInAssistant.length).toBe(0);
474
- }
475
- });
476
-
477
- // Now emit a NEW message WITH text content
478
- const messageWithContentId = testId("msg2");
479
- agent.emit(
480
- textChunkEvent(
481
- messageWithContentId,
482
- "Here is some actual text content",
483
- ),
484
- );
485
-
486
- // Toolbar SHOULD be visible now for the message with content
487
- await waitFor(() => {
488
- const allMessages = screen.getAllByText(
489
- /Here is some actual text content/,
490
- );
491
- expect(allMessages.length).toBeGreaterThan(0);
492
-
493
- // Should now have copy button
494
- const toolbarButtons = screen.getAllByRole("button");
495
- const copyButton = toolbarButtons.find((btn) =>
496
- btn.getAttribute("aria-label")?.toLowerCase().includes("copy"),
497
- );
498
- expect(copyButton).toBeDefined();
499
- });
500
-
501
- agent.emit(runFinishedEvent());
502
- agent.complete();
503
- });
504
-
505
- it("should prefer specific renderer over wildcard when both exist", async () => {
506
- const agent = new MockStepwiseAgent();
507
- const renderToolCalls = [
508
- defineToolCallRenderer({
509
- name: "specificTool",
510
- args: z.object({ value: z.string() }),
511
- render: ({ args }) => (
512
- <div data-testid="specific-renderer">Specific: {args.value}</div>
513
- ),
514
- }),
515
- defineToolCallRenderer({
516
- name: "*",
517
- args: z.any(),
518
- render: ({ name }) => (
519
- <div data-testid="wildcard-renderer">Wildcard: {name}</div>
520
- ),
521
- }),
522
- ] as unknown as ReactToolCallRenderer<unknown>[];
523
-
524
- renderWithCopilotKit({ agent, renderToolCalls });
525
-
526
- // Submit message
527
- const input = await screen.findByRole("textbox");
528
- fireEvent.change(input, { target: { value: "Test specific" } });
529
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
530
-
531
- // Wait for user message to appear
532
- await waitFor(() => {
533
- expect(screen.getByText("Test specific")).toBeDefined();
534
- });
535
-
536
- const messageId = testId("msg");
537
- const toolCallId1 = testId("tc1");
538
- const toolCallId2 = testId("tc2");
539
-
540
- agent.emit(runStartedEvent());
541
-
542
- // Call the specific tool
543
- agent.emit(
544
- toolCallChunkEvent({
545
- toolCallId: toolCallId1,
546
- toolCallName: "specificTool",
547
- parentMessageId: messageId,
548
- delta: '{"value":"test123"}',
549
- }),
550
- );
551
-
552
- // Call an unknown tool
553
- agent.emit(
554
- toolCallChunkEvent({
555
- toolCallId: toolCallId2,
556
- toolCallName: "unknownTool",
557
- parentMessageId: messageId,
558
- delta: '{"data":"xyz"}',
559
- }),
560
- );
561
-
562
- // Specific renderer should be used for specificTool
563
- await waitFor(() => {
564
- const specific = screen.getByTestId("specific-renderer");
565
- expect(specific).toBeDefined();
566
- expect(specific.textContent).toContain("test123");
567
- });
568
-
569
- // Wildcard should be used for unknownTool
570
- await waitFor(() => {
571
- const wildcard = screen.getByTestId("wildcard-renderer");
572
- expect(wildcard).toBeDefined();
573
- expect(wildcard.textContent).toContain("Wildcard: unknownTool");
574
- });
575
-
576
- agent.emit(runFinishedEvent());
577
- agent.complete();
578
- });
579
- });
580
-
581
- describe("Suggestions Flow", () => {
582
- // Helper component to configure suggestions
583
- const ChatWithSuggestions: React.FC<{
584
- consumerAgentId: string;
585
- providerAgentId: string;
586
- instructions?: string;
587
- minSuggestions?: number;
588
- maxSuggestions?: number;
589
- onReady?: () => void;
590
- }> = ({
591
- consumerAgentId,
592
- providerAgentId,
593
- instructions,
594
- minSuggestions,
595
- maxSuggestions,
596
- onReady,
597
- }) => {
598
- useConfigureSuggestions({
599
- instructions: instructions || "Suggest helpful next actions",
600
- providerAgentId,
601
- consumerAgentId,
602
- minSuggestions: minSuggestions || 2,
603
- maxSuggestions: maxSuggestions || 4,
604
- });
605
-
606
- useEffect(() => {
607
- if (onReady) {
608
- onReady();
609
- }
610
- }, [onReady]);
611
-
612
- return <CopilotChat />;
613
- };
614
-
615
- it("should display suggestions when configured", async () => {
616
- const consumerAgent = new MockStepwiseAgent();
617
- const providerAgent = new SuggestionsProviderAgent();
618
-
619
- // Configure provider agent with suggestions
620
- providerAgent.setSuggestions([
621
- { title: "Option A", message: "Take action A" },
622
- { title: "Option B", message: "Take action B" },
623
- ]);
624
-
625
- let suggestionsReady = false;
626
-
627
- renderWithCopilotKit({
628
- agents: {
629
- default: consumerAgent,
630
- "suggestions-provider": providerAgent,
631
- },
632
- agentId: "default",
633
- children: (
634
- <div style={{ height: 400 }}>
635
- <ChatWithSuggestions
636
- consumerAgentId="default"
637
- providerAgentId="suggestions-provider"
638
- onReady={() => {
639
- suggestionsReady = true;
640
- }}
641
- />
642
- </div>
643
- ),
644
- });
645
-
646
- // Wait for suggestions config to be ready
647
- await waitFor(() => {
648
- expect(suggestionsReady).toBe(true);
649
- });
650
-
651
- // Submit a message to trigger suggestions
652
- const input = await screen.findByRole("textbox");
653
- fireEvent.change(input, { target: { value: "Help me" } });
654
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
655
-
656
- // Wait for user message
657
- await waitFor(() => {
658
- expect(screen.getByText("Help me")).toBeDefined();
659
- });
660
-
661
- // Consumer agent responds
662
- const messageId = testId("msg");
663
- consumerAgent.emit(runStartedEvent());
664
- consumerAgent.emit(textChunkEvent(messageId, "I can help with that."));
665
- consumerAgent.emit(runFinishedEvent());
666
- consumerAgent.complete();
667
-
668
- // Wait for assistant message
669
- await waitFor(() => {
670
- expect(screen.getByText(/I can help with that/)).toBeDefined();
671
- });
672
-
673
- // Verify suggestions appear (provider agent's run() method will be called automatically)
674
- await waitFor(
675
- () => {
676
- expect(screen.getByText("Option A")).toBeDefined();
677
- expect(screen.getByText("Option B")).toBeDefined();
678
- },
679
- { timeout: 5000 },
680
- );
681
-
682
- // Click on a suggestion
683
- const suggestionA = screen.getByText("Option A");
684
- fireEvent.click(suggestionA);
685
-
686
- // Verify the suggestion message is added
687
- await waitFor(() => {
688
- const messages = screen.getAllByText(/Take action A/);
689
- expect(messages.length).toBeGreaterThan(0);
690
- });
691
- });
692
-
693
- it("should stream suggestion titles token by token", async () => {
694
- const consumerAgent = new MockStepwiseAgent();
695
- const providerAgent = new SuggestionsProviderAgent();
696
-
697
- // Configure provider agent with suggestions
698
- providerAgent.setSuggestions([
699
- { title: "First Action", message: "Do first action" },
700
- { title: "Second Action", message: "Do second action" },
701
- ]);
702
-
703
- let suggestionsReady = false;
704
-
705
- renderWithCopilotKit({
706
- agents: {
707
- default: consumerAgent,
708
- "suggestions-provider": providerAgent,
709
- },
710
- agentId: "default",
711
- children: (
712
- <div style={{ height: 400 }}>
713
- <ChatWithSuggestions
714
- consumerAgentId="default"
715
- providerAgentId="suggestions-provider"
716
- onReady={() => {
717
- suggestionsReady = true;
718
- }}
719
- />
720
- </div>
721
- ),
722
- });
723
-
724
- await waitFor(() => {
725
- expect(suggestionsReady).toBe(true);
726
- });
727
-
728
- // Submit a message
729
- const input = await screen.findByRole("textbox");
730
- fireEvent.change(input, { target: { value: "What can I do?" } });
731
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
732
-
733
- await waitFor(() => {
734
- expect(screen.getByText("What can I do?")).toBeDefined();
735
- });
736
-
737
- // Consumer agent responds
738
- const messageId = testId("msg");
739
- consumerAgent.emit(runStartedEvent());
740
- consumerAgent.emit(textChunkEvent(messageId, "Here are some options."));
741
- consumerAgent.emit(runFinishedEvent());
742
- consumerAgent.complete();
743
-
744
- // Verify both suggestions are visible after streaming completes
745
- await waitFor(
746
- () => {
747
- expect(screen.getByText("First Action")).toBeDefined();
748
- expect(screen.getByText("Second Action")).toBeDefined();
749
- },
750
- { timeout: 5000 },
751
- );
752
- });
753
-
754
- it("should handle multiple suggestions streaming concurrently", async () => {
755
- const consumerAgent = new MockStepwiseAgent();
756
- const providerAgent = new SuggestionsProviderAgent();
757
-
758
- // Configure provider agent with suggestions
759
- providerAgent.setSuggestions([
760
- { title: "Alpha", message: "Do alpha" },
761
- { title: "Beta", message: "Do beta" },
762
- { title: "Gamma", message: "Do gamma" },
763
- ]);
764
-
765
- let suggestionsReady = false;
766
-
767
- renderWithCopilotKit({
768
- agents: {
769
- default: consumerAgent,
770
- "suggestions-provider": providerAgent,
771
- },
772
- agentId: "default",
773
- children: (
774
- <div style={{ height: 400 }}>
775
- <ChatWithSuggestions
776
- consumerAgentId="default"
777
- providerAgentId="suggestions-provider"
778
- minSuggestions={3}
779
- maxSuggestions={5}
780
- onReady={() => {
781
- suggestionsReady = true;
782
- }}
783
- />
784
- </div>
785
- ),
786
- });
787
-
788
- await waitFor(() => {
789
- expect(suggestionsReady).toBe(true);
790
- });
791
-
792
- // Submit message
793
- const input = await screen.findByRole("textbox");
794
- fireEvent.change(input, { target: { value: "Show me options" } });
795
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
796
-
797
- await waitFor(() => {
798
- expect(screen.getByText("Show me options")).toBeDefined();
799
- });
800
-
801
- // Consumer agent responds
802
- const messageId = testId("msg");
803
- consumerAgent.emit(runStartedEvent());
804
- consumerAgent.emit(textChunkEvent(messageId, "Here you go."));
805
- consumerAgent.emit(runFinishedEvent());
806
- consumerAgent.complete();
807
-
808
- // Verify all suggestions appear
809
- await waitFor(
810
- () => {
811
- expect(screen.getByText("Alpha")).toBeDefined();
812
- expect(screen.getByText("Beta")).toBeDefined();
813
- expect(screen.getByText("Gamma")).toBeDefined();
814
- },
815
- { timeout: 5000 },
816
- );
817
- });
818
- });
819
-
820
- describe("Reasoning Message Flow", () => {
821
- it("should display reasoning message with 'Thinking...' label while streaming", async () => {
822
- const agent = new MockStepwiseAgent();
823
- renderWithCopilotKit({ agent });
824
-
825
- // Submit message
826
- const input = await screen.findByRole("textbox");
827
- fireEvent.change(input, { target: { value: "Think about this" } });
828
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
829
-
830
- await waitFor(() => {
831
- expect(screen.getByText("Think about this")).toBeDefined();
832
- });
833
-
834
- const reasoningId = testId("reasoning");
835
-
836
- agent.emit(runStartedEvent());
837
- agent.emit(reasoningStartEvent(reasoningId));
838
- agent.emit(reasoningMessageStartEvent(reasoningId));
839
- agent.emit(
840
- reasoningMessageContentEvent(reasoningId, "Let me analyze..."),
841
- );
842
-
843
- // "Thinking..." label should appear while streaming
844
- await waitFor(() => {
845
- expect(screen.getByText("Thinking…")).toBeDefined();
846
- });
847
-
848
- // Clean up
849
- agent.emit(reasoningMessageEndEvent(reasoningId));
850
- agent.emit(reasoningEndEvent(reasoningId));
851
- agent.emit(runFinishedEvent());
852
- agent.complete();
853
- });
854
-
855
- it("should display 'Thought for X seconds' after reasoning completes", async () => {
856
- const agent = new MockStepwiseAgent();
857
- renderWithCopilotKit({ agent });
858
-
859
- const input = await screen.findByRole("textbox");
860
- fireEvent.change(input, { target: { value: "Reason please" } });
861
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
862
-
863
- await waitFor(() => {
864
- expect(screen.getByText("Reason please")).toBeDefined();
865
- });
866
-
867
- const reasoningId = testId("reasoning");
868
- const textId = testId("text");
869
-
870
- agent.emit(runStartedEvent());
871
- emitReasoningSequence(agent, reasoningId, "Some deep thought");
872
- agent.emit(textChunkEvent(textId, "Here is my answer."));
873
- agent.emit(runFinishedEvent());
874
- agent.complete();
875
-
876
- // After reasoning finishes, should show elapsed time label
877
- await waitFor(() => {
878
- expect(screen.getByText(/Thought for/)).toBeDefined();
879
- });
880
- });
881
-
882
- it("should accumulate content from multiple delta events", async () => {
883
- const agent = new MockStepwiseAgent();
884
- renderWithCopilotKit({ agent });
885
-
886
- const input = await screen.findByRole("textbox");
887
- fireEvent.change(input, { target: { value: "Elaborate" } });
888
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
889
-
890
- await waitFor(() => {
891
- expect(screen.getByText("Elaborate")).toBeDefined();
892
- });
893
-
894
- const reasoningId = testId("reasoning");
895
-
896
- agent.emit(runStartedEvent());
897
- agent.emit(reasoningStartEvent(reasoningId));
898
- agent.emit(reasoningMessageStartEvent(reasoningId));
899
- agent.emit(reasoningMessageContentEvent(reasoningId, "Part 1"));
900
- agent.emit(reasoningMessageContentEvent(reasoningId, " Part 2"));
901
- agent.emit(reasoningMessageContentEvent(reasoningId, " Part 3"));
902
- agent.emit(reasoningMessageEndEvent(reasoningId));
903
- agent.emit(reasoningEndEvent(reasoningId));
904
-
905
- // The accumulated content should be present
906
- await waitFor(() => {
907
- expect(screen.getByText(/Part 1 Part 2 Part 3/)).toBeDefined();
908
- });
909
-
910
- agent.emit(runFinishedEvent());
911
- agent.complete();
912
- });
913
-
914
- it("should render reasoning before text in a single agent run", async () => {
915
- const agent = new MockStepwiseAgent();
916
- renderWithCopilotKit({ agent });
917
-
918
- const input = await screen.findByRole("textbox");
919
- fireEvent.change(input, { target: { value: "Answer with thought" } });
920
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
921
-
922
- await waitFor(() => {
923
- expect(screen.getByText("Answer with thought")).toBeDefined();
924
- });
925
-
926
- const reasoningId = testId("reasoning");
927
- const textId = testId("text");
928
-
929
- agent.emit(runStartedEvent());
930
- emitReasoningSequence(agent, reasoningId, "Thinking about the answer");
931
- agent.emit(textChunkEvent(textId, "The answer is 42."));
932
- agent.emit(runFinishedEvent());
933
- agent.complete();
934
-
935
- // Both should appear
936
- await waitFor(() => {
937
- expect(screen.getByText(/Thought for/)).toBeDefined();
938
- expect(screen.getByText("The answer is 42.")).toBeDefined();
939
- });
940
-
941
- // Reasoning should come before text in the DOM
942
- const reasoningEl = screen
943
- .getByText(/Thought for/)
944
- .closest("[data-message-id]");
945
- const textEl = screen
946
- .getByText("The answer is 42.")
947
- .closest("[data-message-id]");
948
-
949
- if (reasoningEl && textEl) {
950
- // Use compareDocumentPosition to verify ordering
951
- const position = reasoningEl.compareDocumentPosition(textEl);
952
- // DOCUMENT_POSITION_FOLLOWING means textEl comes after reasoningEl
953
- expect(position & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
954
- }
955
- });
956
-
957
- it("should handle reasoning-only response (no text output)", async () => {
958
- const agent = new MockStepwiseAgent();
959
- renderWithCopilotKit({ agent });
960
-
961
- const input = await screen.findByRole("textbox");
962
- fireEvent.change(input, { target: { value: "Just think" } });
963
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
964
-
965
- await waitFor(() => {
966
- expect(screen.getByText("Just think")).toBeDefined();
967
- });
968
-
969
- const reasoningId = testId("reasoning");
970
-
971
- agent.emit(runStartedEvent());
972
- emitReasoningSequence(agent, reasoningId, "Only reasoning, no text");
973
- agent.emit(runFinishedEvent());
974
- agent.complete();
975
-
976
- // Reasoning message should appear
977
- await waitFor(() => {
978
- expect(screen.getByText(/Thought for/)).toBeDefined();
979
- });
980
- });
981
-
982
- it("should not show cursor when last message is reasoning", async () => {
983
- const agent = new MockStepwiseAgent();
984
- renderWithCopilotKit({ agent });
985
-
986
- const input = await screen.findByRole("textbox");
987
- fireEvent.change(input, { target: { value: "Think deeply" } });
988
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
989
-
990
- await waitFor(() => {
991
- expect(screen.getByText("Think deeply")).toBeDefined();
992
- });
993
-
994
- const reasoningId = testId("reasoning");
995
-
996
- agent.emit(runStartedEvent());
997
- agent.emit(reasoningStartEvent(reasoningId));
998
- agent.emit(reasoningMessageStartEvent(reasoningId));
999
- agent.emit(
1000
- reasoningMessageContentEvent(reasoningId, "Deep reasoning..."),
1001
- );
1002
-
1003
- // "Thinking..." should appear (reasoning is streaming)
1004
- await waitFor(() => {
1005
- expect(screen.getByText("Thinking…")).toBeDefined();
1006
- });
1007
-
1008
- // Chat-level cursor (pulsing dot) should NOT be visible when last message is reasoning
1009
- // The cursor has animate-pulse-cursor class
1010
- const cursors = document.querySelectorAll(".cpk\\:animate-pulse-cursor");
1011
- // Only the reasoning indicator should be present, not the chat-level cursor
1012
- // The chat-level cursor is a direct child of the message view container
1013
- const chatLevelCursor = document.querySelector(
1014
- ".cpk\\:flex.cpk\\:flex-col > .cpk\\:mt-2 .cpk\\:animate-pulse-cursor",
1015
- );
1016
- expect(chatLevelCursor).toBeNull();
1017
-
1018
- // Clean up
1019
- agent.emit(reasoningMessageEndEvent(reasoningId));
1020
- agent.emit(reasoningEndEvent(reasoningId));
1021
- agent.emit(runFinishedEvent());
1022
- agent.complete();
1023
- });
1024
-
1025
- it("should show cursor after reasoning when text message follows", async () => {
1026
- const agent = new MockStepwiseAgent();
1027
- renderWithCopilotKit({ agent });
1028
-
1029
- const input = await screen.findByRole("textbox");
1030
- fireEvent.change(input, { target: { value: "Think then answer" } });
1031
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
1032
-
1033
- await waitFor(() => {
1034
- expect(screen.getByText("Think then answer")).toBeDefined();
1035
- });
1036
-
1037
- const reasoningId = testId("reasoning");
1038
- const textId = testId("text");
1039
-
1040
- agent.emit(runStartedEvent());
1041
- emitReasoningSequence(agent, reasoningId, "Let me think first");
1042
-
1043
- // Now start streaming text - text is now the last message, cursor should show
1044
- agent.emit(textChunkEvent(textId, "Starting answer..."));
1045
-
1046
- await waitFor(() => {
1047
- expect(screen.getByText(/Starting answer/)).toBeDefined();
1048
- });
1049
-
1050
- // Chat-level cursor should now be visible since last message is text (not reasoning)
1051
- // Note: The cursor shows while isRunning=true and last message is not reasoning
1052
- await waitFor(() => {
1053
- const chatLevelCursor = document.querySelector(
1054
- ".cpk\\:animate-pulse-cursor",
1055
- );
1056
- expect(chatLevelCursor).not.toBeNull();
1057
- });
1058
-
1059
- agent.emit(runFinishedEvent());
1060
- agent.complete();
1061
- });
1062
-
1063
- it("should not auto-collapse when user manually toggled during streaming", async () => {
1064
- const agent = new MockStepwiseAgent();
1065
- renderWithCopilotKit({ agent });
1066
-
1067
- const input = await screen.findByRole("textbox");
1068
- fireEvent.change(input, { target: { value: "User toggle test" } });
1069
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
1070
-
1071
- await waitFor(() => {
1072
- expect(screen.getByText("User toggle test")).toBeDefined();
1073
- });
1074
-
1075
- const reasoningId = testId("reasoning");
1076
- const textId = testId("text");
1077
-
1078
- // Start streaming reasoning — panel should auto-open
1079
- agent.emit(runStartedEvent());
1080
- agent.emit(reasoningStartEvent(reasoningId));
1081
- agent.emit(reasoningMessageStartEvent(reasoningId));
1082
- agent.emit(
1083
- reasoningMessageContentEvent(reasoningId, "Deep analysis in progress"),
1084
- );
1085
-
1086
- await waitFor(() => {
1087
- expect(screen.getByText("Thinking…")).toBeDefined();
1088
- });
1089
-
1090
- // Panel should be open (aria-expanded="true") while streaming
1091
- await waitFor(() => {
1092
- const header = screen.getByText("Thinking…");
1093
- const button = header.closest("button");
1094
- expect(button?.getAttribute("aria-expanded")).toBe("true");
1095
- });
1096
-
1097
- // User manually collapses during streaming — this sets userToggledRef
1098
- const header = screen.getByText("Thinking…");
1099
- const button = header.closest("button");
1100
- act(() => {
1101
- if (button) {
1102
- fireEvent.click(button);
1103
- }
1104
- });
1105
-
1106
- // Should now be collapsed by user action
1107
- await waitFor(() => {
1108
- const btn = screen.getByText("Thinking…").closest("button");
1109
- expect(btn?.getAttribute("aria-expanded")).toBe("false");
1110
- });
1111
-
1112
- // Now streaming ends — because userToggledRef is true, the panel
1113
- // should stay in whatever state the user set (collapsed).
1114
- agent.emit(reasoningMessageEndEvent(reasoningId));
1115
- agent.emit(reasoningEndEvent(reasoningId));
1116
- agent.emit(textChunkEvent(textId, "Done."));
1117
- agent.emit(runFinishedEvent());
1118
- agent.complete();
1119
-
1120
- // Panel should remain collapsed (not flash open then closed)
1121
- await waitFor(() => {
1122
- const btn = screen.getByText(/Thought for/).closest("button");
1123
- expect(btn?.getAttribute("aria-expanded")).toBe("false");
1124
- });
1125
- });
1126
-
1127
- it("should keep panel open when user re-expands during streaming", async () => {
1128
- const agent = new MockStepwiseAgent();
1129
- renderWithCopilotKit({ agent });
1130
-
1131
- const input = await screen.findByRole("textbox");
1132
- fireEvent.change(input, {
1133
- target: { value: "Re-expand toggle test" },
1134
- });
1135
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
1136
-
1137
- await waitFor(() => {
1138
- expect(screen.getByText("Re-expand toggle test")).toBeDefined();
1139
- });
1140
-
1141
- const reasoningId = testId("reasoning");
1142
- const textId = testId("text");
1143
-
1144
- // Start streaming reasoning — panel auto-opens
1145
- agent.emit(runStartedEvent());
1146
- agent.emit(reasoningStartEvent(reasoningId));
1147
- agent.emit(reasoningMessageStartEvent(reasoningId));
1148
- agent.emit(reasoningMessageContentEvent(reasoningId, "Thinking hard"));
1149
-
1150
- await waitFor(() => {
1151
- const btn = screen.getByText("Thinking…").closest("button");
1152
- expect(btn?.getAttribute("aria-expanded")).toBe("true");
1153
- });
1154
-
1155
- // User collapses, then re-expands (both set userToggledRef = true)
1156
- const headerEl = screen.getByText("Thinking…");
1157
- const btn = headerEl.closest("button");
1158
- act(() => {
1159
- if (btn) {
1160
- fireEvent.click(btn); // collapse
1161
- fireEvent.click(btn); // re-expand
1162
- }
1163
- });
1164
-
1165
- await waitFor(() => {
1166
- const b = screen.getByText("Thinking…").closest("button");
1167
- expect(b?.getAttribute("aria-expanded")).toBe("true");
1168
- });
1169
-
1170
- // Streaming ends — because userToggledRef is true, panel should
1171
- // stay in the user's chosen state (open).
1172
- agent.emit(reasoningMessageEndEvent(reasoningId));
1173
- agent.emit(reasoningEndEvent(reasoningId));
1174
- agent.emit(textChunkEvent(textId, "All done."));
1175
- agent.emit(runFinishedEvent());
1176
- agent.complete();
1177
-
1178
- // Panel should remain open (not auto-collapse)
1179
- await waitFor(() => {
1180
- const b = screen.getByText(/Thought for/).closest("button");
1181
- expect(b?.getAttribute("aria-expanded")).toBe("true");
1182
- });
1183
- });
1184
-
1185
- it("should expand and collapse reasoning content on click", async () => {
1186
- const agent = new MockStepwiseAgent();
1187
- renderWithCopilotKit({ agent });
1188
-
1189
- const input = await screen.findByRole("textbox");
1190
- fireEvent.change(input, { target: { value: "Toggle test" } });
1191
- fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
1192
-
1193
- await waitFor(() => {
1194
- expect(screen.getByText("Toggle test")).toBeDefined();
1195
- });
1196
-
1197
- const reasoningId = testId("reasoning");
1198
- const textId = testId("text");
1199
-
1200
- agent.emit(runStartedEvent());
1201
- emitReasoningSequence(
1202
- agent,
1203
- reasoningId,
1204
- "This is expandable reasoning content",
1205
- );
1206
- agent.emit(textChunkEvent(textId, "Done thinking."));
1207
- agent.emit(runFinishedEvent());
1208
- agent.complete();
1209
-
1210
- // After reasoning finishes, it should auto-collapse
1211
- await waitFor(() => {
1212
- const header = screen.getByText(/Thought for/);
1213
- expect(header).toBeDefined();
1214
- // The header's button should have aria-expanded="false" after collapsing
1215
- const button = header.closest("button");
1216
- expect(button?.getAttribute("aria-expanded")).toBe("false");
1217
- });
1218
-
1219
- // Click to expand — wrap in act() so React 18 flushes the state
1220
- // update synchronously instead of deferring it through the scheduler,
1221
- // which can race with waitFor polling on slow CI runners.
1222
- const header = screen.getByText(/Thought for/);
1223
- const button = header.closest("button");
1224
- act(() => {
1225
- if (button) {
1226
- fireEvent.click(button);
1227
- }
1228
- });
1229
-
1230
- // Should now be expanded
1231
- await waitFor(() => {
1232
- const expandedButton = screen
1233
- .getByText(/Thought for/)
1234
- .closest("button");
1235
- expect(expandedButton?.getAttribute("aria-expanded")).toBe("true");
1236
- });
1237
- });
1238
- });
1239
- });