@copilotkit/react-core 1.57.2 → 1.57.4

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 (266) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +12 -13
  3. package/skills/react-core/SKILL.md +108 -0
  4. package/skills/react-core/references/agent-access.md +288 -0
  5. package/skills/react-core/references/attachments.md +291 -0
  6. package/skills/react-core/references/capabilities.md +138 -0
  7. package/skills/react-core/references/chat-components.md +221 -0
  8. package/skills/react-core/references/client-side-tools.md +358 -0
  9. package/skills/react-core/references/custom-message-renderers.md +226 -0
  10. package/skills/react-core/references/debug-mode.md +153 -0
  11. package/skills/react-core/references/human-in-the-loop.md +312 -0
  12. package/skills/react-core/references/provider-setup.md +326 -0
  13. package/skills/react-core/references/rendering-activity-messages.md +207 -0
  14. package/skills/react-core/references/rendering-tool-calls.md +319 -0
  15. package/skills/react-core/references/suggestions.md +211 -0
  16. package/skills/react-core/references/switching-agents-recipes.md +160 -0
  17. package/skills/react-core/references/switching-agents.md +231 -0
  18. package/skills/react-core/references/threads.md +226 -0
  19. package/.attw.json +0 -3
  20. package/CHANGELOG.md +0 -5043
  21. package/scripts/scope-preflight.mjs +0 -100
  22. package/src/components/CopilotListeners.tsx +0 -137
  23. package/src/components/__tests__/CopilotListeners.test.tsx +0 -38
  24. package/src/components/copilot-provider/__tests__/copilot-messages-key.test.tsx +0 -92
  25. package/src/components/copilot-provider/__tests__/copilotkit-error.test.tsx +0 -77
  26. package/src/components/copilot-provider/__tests__/error-visibility-prod.test.tsx +0 -70
  27. package/src/components/copilot-provider/__tests__/v1-explicit-threadid-bridge.test.tsx +0 -107
  28. package/src/components/copilot-provider/copilot-messages.tsx +0 -314
  29. package/src/components/copilot-provider/copilotkit-props.tsx +0 -214
  30. package/src/components/copilot-provider/copilotkit.tsx +0 -853
  31. package/src/components/copilot-provider/index.ts +0 -3
  32. package/src/components/dev-console/console-trigger.tsx +0 -283
  33. package/src/components/dev-console/developer-console-modal.tsx +0 -1016
  34. package/src/components/dev-console/icons.tsx +0 -106
  35. package/src/components/error-boundary/error-boundary.tsx +0 -99
  36. package/src/components/error-boundary/error-utils.tsx +0 -105
  37. package/src/components/index.ts +0 -1
  38. package/src/components/toast/exclamation-mark-icon.tsx +0 -27
  39. package/src/components/toast/toast-provider.tsx +0 -448
  40. package/src/components/usage-banner.tsx +0 -266
  41. package/src/context/__tests__/threads-context.test.tsx +0 -141
  42. package/src/context/coagent-state-renders-context.tsx +0 -89
  43. package/src/context/copilot-context.tsx +0 -365
  44. package/src/context/copilot-messages-context.tsx +0 -35
  45. package/src/context/index.ts +0 -22
  46. package/src/context/threads-context.tsx +0 -69
  47. package/src/hooks/__tests__/use-coagent-config.test.ts +0 -352
  48. package/src/hooks/__tests__/use-coagent-state-render-bridge.helpers.test.ts +0 -107
  49. package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +0 -1209
  50. package/src/hooks/__tests__/use-coagent-state-render.test.tsx +0 -356
  51. package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +0 -241
  52. package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -72
  53. package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +0 -102
  54. package/src/hooks/index.ts +0 -33
  55. package/src/hooks/use-agent-nodename.ts +0 -33
  56. package/src/hooks/use-coagent-state-render-bridge.helpers.ts +0 -345
  57. package/src/hooks/use-coagent-state-render-bridge.tsx +0 -222
  58. package/src/hooks/use-coagent-state-render-registry.ts +0 -230
  59. package/src/hooks/use-coagent-state-render.ts +0 -163
  60. package/src/hooks/use-coagent.ts +0 -377
  61. package/src/hooks/use-configure-chat-suggestions.tsx +0 -96
  62. package/src/hooks/use-copilot-action.ts +0 -245
  63. package/src/hooks/use-copilot-additional-instructions.ts +0 -98
  64. package/src/hooks/use-copilot-authenticated-action.ts +0 -73
  65. package/src/hooks/use-copilot-chat-headless_c.ts +0 -264
  66. package/src/hooks/use-copilot-chat-suggestions.tsx +0 -134
  67. package/src/hooks/use-copilot-chat.ts +0 -132
  68. package/src/hooks/use-copilot-chat_internal.ts +0 -875
  69. package/src/hooks/use-copilot-readable.ts +0 -135
  70. package/src/hooks/use-copilot-runtime-client.ts +0 -178
  71. package/src/hooks/use-default-tool.ts +0 -13
  72. package/src/hooks/use-flat-category-store.ts +0 -109
  73. package/src/hooks/use-frontend-tool.ts +0 -113
  74. package/src/hooks/use-human-in-the-loop.ts +0 -138
  75. package/src/hooks/use-langgraph-interrupt.ts +0 -103
  76. package/src/hooks/use-lazy-tool-renderer.tsx +0 -30
  77. package/src/hooks/use-make-copilot-document-readable.ts +0 -30
  78. package/src/hooks/use-render-tool-call.ts +0 -89
  79. package/src/hooks/use-tree.ts +0 -222
  80. package/src/index.tsx +0 -7
  81. package/src/lib/copilot-task.ts +0 -215
  82. package/src/lib/index.ts +0 -1
  83. package/src/lib/status-checker.ts +0 -67
  84. package/src/setupTests.ts +0 -37
  85. package/src/test-helpers/copilot-context.ts +0 -91
  86. package/src/types/chat-suggestion-configuration.ts +0 -23
  87. package/src/types/coagent-action.ts +0 -35
  88. package/src/types/coagent-state.ts +0 -13
  89. package/src/types/crew.ts +0 -89
  90. package/src/types/document-pointer.ts +0 -7
  91. package/src/types/frontend-action.ts +0 -213
  92. package/src/types/index.ts +0 -17
  93. package/src/types/interrupt-action.ts +0 -58
  94. package/src/types/system-message.ts +0 -4
  95. package/src/utils/dev-console.ts +0 -19
  96. package/src/utils/index.ts +0 -2
  97. package/src/utils/suggestions-constants.ts +0 -8
  98. package/src/utils/utils.test.ts +0 -7
  99. package/src/utils/utils.ts +0 -6
  100. package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +0 -240
  101. package/src/v2/__tests__/globalSetup.ts +0 -14
  102. package/src/v2/__tests__/setup.ts +0 -93
  103. package/src/v2/__tests__/utils/test-helpers.tsx +0 -570
  104. package/src/v2/a2ui/A2UICatalogContext.tsx +0 -79
  105. package/src/v2/a2ui/A2UIMessageRenderer.tsx +0 -294
  106. package/src/v2/a2ui/A2UIToolCallRenderer.tsx +0 -290
  107. package/src/v2/components/CopilotKitInspector.tsx +0 -52
  108. package/src/v2/components/MCPAppsActivityRenderer.tsx +0 -815
  109. package/src/v2/components/OpenGenerativeUIRenderer.tsx +0 -598
  110. package/src/v2/components/WildcardToolCallRender.tsx +0 -86
  111. package/src/v2/components/__tests__/OpenGenerativeUIRenderer.test.tsx +0 -665
  112. package/src/v2/components/chat/CopilotChat.tsx +0 -664
  113. package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +0 -393
  114. package/src/v2/components/chat/CopilotChatAttachmentQueue.tsx +0 -374
  115. package/src/v2/components/chat/CopilotChatAttachmentRenderer.tsx +0 -159
  116. package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +0 -350
  117. package/src/v2/components/chat/CopilotChatInput.tsx +0 -1412
  118. package/src/v2/components/chat/CopilotChatMessageView.tsx +0 -716
  119. package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +0 -265
  120. package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +0 -59
  121. package/src/v2/components/chat/CopilotChatSuggestionView.tsx +0 -134
  122. package/src/v2/components/chat/CopilotChatToggleButton.tsx +0 -171
  123. package/src/v2/components/chat/CopilotChatToolCallsView.tsx +0 -40
  124. package/src/v2/components/chat/CopilotChatUserMessage.tsx +0 -445
  125. package/src/v2/components/chat/CopilotChatView.tsx +0 -890
  126. package/src/v2/components/chat/CopilotModalHeader.tsx +0 -129
  127. package/src/v2/components/chat/CopilotPopup.tsx +0 -81
  128. package/src/v2/components/chat/CopilotPopupView.tsx +0 -317
  129. package/src/v2/components/chat/CopilotSidebar.tsx +0 -80
  130. package/src/v2/components/chat/CopilotSidebarView.tsx +0 -269
  131. package/src/v2/components/chat/Lightbox.tsx +0 -103
  132. package/src/v2/components/chat/__tests__/CopilotChat.absentThreadConnect.test.tsx +0 -66
  133. package/src/v2/components/chat/__tests__/CopilotChat.attachments.test.tsx +0 -168
  134. package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +0 -1239
  135. package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +0 -73
  136. package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +0 -432
  137. package/src/v2/components/chat/__tests__/CopilotChat.suggestionsAlways.test.tsx +0 -183
  138. package/src/v2/components/chat/__tests__/CopilotChat.welcomeGate.test.tsx +0 -184
  139. package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +0 -649
  140. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +0 -624
  141. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +0 -702
  142. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.thumbs.test.tsx +0 -72
  143. package/src/v2/components/chat/__tests__/CopilotChatCopyButton.clipboard.test.tsx +0 -241
  144. package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +0 -107
  145. package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +0 -929
  146. package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +0 -1567
  147. package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +0 -1004
  148. package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +0 -279
  149. package/src/v2/components/chat/__tests__/CopilotChatPerf.e2e.test.tsx +0 -296
  150. package/src/v2/components/chat/__tests__/CopilotChatPropsRerender.e2e.test.tsx +0 -249
  151. package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +0 -530
  152. package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +0 -785
  153. package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +0 -2416
  154. package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +0 -621
  155. package/src/v2/components/chat/__tests__/CopilotChatView.connectingGate.test.tsx +0 -56
  156. package/src/v2/components/chat/__tests__/CopilotChatView.inputOverlay.test.tsx +0 -264
  157. package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +0 -853
  158. package/src/v2/components/chat/__tests__/CopilotChatView.pinToSend.test.tsx +0 -94
  159. package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +0 -1050
  160. package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +0 -484
  161. package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +0 -612
  162. package/src/v2/components/chat/__tests__/CopilotSidebarView.position.test.tsx +0 -159
  163. package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +0 -502
  164. package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +0 -1068
  165. package/src/v2/components/chat/__tests__/MCPAppsProxy.e2e.test.tsx +0 -589
  166. package/src/v2/components/chat/__tests__/MCPAppsUiMessage.e2e.test.tsx +0 -403
  167. package/src/v2/components/chat/__tests__/copilot-chat-throttle.test.tsx +0 -137
  168. package/src/v2/components/chat/__tests__/normalize-auto-scroll.test.ts +0 -37
  169. package/src/v2/components/chat/__tests__/setup.ts +0 -1
  170. package/src/v2/components/chat/index.ts +0 -90
  171. package/src/v2/components/chat/last-user-message-context.ts +0 -21
  172. package/src/v2/components/chat/normalize-auto-scroll.ts +0 -17
  173. package/src/v2/components/chat/scroll-element-context.ts +0 -13
  174. package/src/v2/components/index.ts +0 -8
  175. package/src/v2/components/intelligence-indicator/IntelligenceIndicator.tsx +0 -286
  176. package/src/v2/components/intelligence-indicator/__tests__/IntelligenceIndicator.e2e.test.tsx +0 -464
  177. package/src/v2/components/intelligence-indicator/index.ts +0 -2
  178. package/src/v2/components/license-warning-banner.tsx +0 -217
  179. package/src/v2/components/ui/button.tsx +0 -124
  180. package/src/v2/components/ui/dropdown-menu.tsx +0 -258
  181. package/src/v2/components/ui/tooltip.tsx +0 -60
  182. package/src/v2/context.ts +0 -62
  183. package/src/v2/headless.ts +0 -64
  184. package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +0 -152
  185. package/src/v2/hooks/__tests__/standard-schema.test.tsx +0 -282
  186. package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +0 -140
  187. package/src/v2/hooks/__tests__/use-agent-context.test.tsx +0 -401
  188. package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +0 -44
  189. package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +0 -211
  190. package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +0 -1029
  191. package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +0 -159
  192. package/src/v2/hooks/__tests__/use-attachments.test.tsx +0 -169
  193. package/src/v2/hooks/__tests__/use-capabilities.test.tsx +0 -76
  194. package/src/v2/hooks/__tests__/use-component.test.tsx +0 -126
  195. package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +0 -696
  196. package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +0 -153
  197. package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +0 -167
  198. package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +0 -2148
  199. package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +0 -1261
  200. package/src/v2/hooks/__tests__/use-interrupt.test.tsx +0 -397
  201. package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +0 -56
  202. package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +0 -192
  203. package/src/v2/hooks/__tests__/use-pin-to-send.test.tsx +0 -219
  204. package/src/v2/hooks/__tests__/use-render-custom-messages.test.tsx +0 -55
  205. package/src/v2/hooks/__tests__/use-render-tool.test.tsx +0 -259
  206. package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +0 -524
  207. package/src/v2/hooks/__tests__/use-threads.test.tsx +0 -757
  208. package/src/v2/hooks/__tests__/zod-regression.test.tsx +0 -311
  209. package/src/v2/hooks/index.ts +0 -24
  210. package/src/v2/hooks/use-agent-context.tsx +0 -45
  211. package/src/v2/hooks/use-agent.tsx +0 -227
  212. package/src/v2/hooks/use-attachments.tsx +0 -269
  213. package/src/v2/hooks/use-capabilities.tsx +0 -25
  214. package/src/v2/hooks/use-component.tsx +0 -91
  215. package/src/v2/hooks/use-configure-suggestions.tsx +0 -236
  216. package/src/v2/hooks/use-default-render-tool.tsx +0 -271
  217. package/src/v2/hooks/use-frontend-tool.tsx +0 -46
  218. package/src/v2/hooks/use-human-in-the-loop.tsx +0 -81
  219. package/src/v2/hooks/use-interrupt.tsx +0 -305
  220. package/src/v2/hooks/use-keyboard-height.tsx +0 -67
  221. package/src/v2/hooks/use-pin-to-send.ts +0 -94
  222. package/src/v2/hooks/use-render-activity-message.tsx +0 -72
  223. package/src/v2/hooks/use-render-custom-messages.tsx +0 -93
  224. package/src/v2/hooks/use-render-tool-call.tsx +0 -208
  225. package/src/v2/hooks/use-render-tool.tsx +0 -184
  226. package/src/v2/hooks/use-suggestions.tsx +0 -91
  227. package/src/v2/hooks/use-threads.tsx +0 -325
  228. package/src/v2/hooks/useKatexStyles.ts +0 -27
  229. package/src/v2/index.css +0 -1
  230. package/src/v2/index.ts +0 -27
  231. package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +0 -495
  232. package/src/v2/lib/__tests__/processPartialHtml.test.ts +0 -112
  233. package/src/v2/lib/__tests__/renderSlot.test.tsx +0 -588
  234. package/src/v2/lib/__tests__/slots.test.ts +0 -56
  235. package/src/v2/lib/processPartialHtml.ts +0 -45
  236. package/src/v2/lib/react-core.ts +0 -156
  237. package/src/v2/lib/slots.tsx +0 -184
  238. package/src/v2/lib/transcription-client.ts +0 -184
  239. package/src/v2/lib/utils.ts +0 -8
  240. package/src/v2/providers/CopilotChatConfigurationProvider.tsx +0 -196
  241. package/src/v2/providers/CopilotKitProvider.tsx +0 -800
  242. package/src/v2/providers/SandboxFunctionsContext.ts +0 -10
  243. package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +0 -652
  244. package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +0 -101
  245. package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +0 -69
  246. package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +0 -881
  247. package/src/v2/providers/__tests__/CopilotKitProvider.sandboxFunctions.test.tsx +0 -198
  248. package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +0 -740
  249. package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +0 -713
  250. package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +0 -294
  251. package/src/v2/providers/index.ts +0 -21
  252. package/src/v2/styles/globals.css +0 -349
  253. package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +0 -525
  254. package/src/v2/types/defineToolCallRenderer.ts +0 -68
  255. package/src/v2/types/frontend-tool.ts +0 -8
  256. package/src/v2/types/human-in-the-loop.ts +0 -33
  257. package/src/v2/types/index.ts +0 -8
  258. package/src/v2/types/interrupt.ts +0 -15
  259. package/src/v2/types/react-activity-message-renderer.ts +0 -27
  260. package/src/v2/types/react-custom-message-renderer.ts +0 -17
  261. package/src/v2/types/react-tool-call-renderer.ts +0 -35
  262. package/src/v2/types/sandbox-function.ts +0 -11
  263. package/tsconfig.json +0 -8
  264. package/tsdown.config.ts +0 -193
  265. package/typedoc.json +0 -4
  266. package/vitest.config.mjs +0 -31
@@ -1,1209 +0,0 @@
1
- import { vi } from "vitest";
2
- import React from "react";
3
- import { act, render, screen, waitFor, within } from "@testing-library/react";
4
- import { useCoAgentStateRender } from "../use-coagent-state-render";
5
- import { CoAgentStateRenderBridge } from "../use-coagent-state-render-bridge";
6
- import { useCopilotChatInternal } from "../use-copilot-chat_internal";
7
- import {
8
- CoAgentStateRendersProvider,
9
- CopilotContext,
10
- useCoAgentStateRenders,
11
- } from "../../context";
12
- import type { Claim } from "../use-coagent-state-render-bridge.helpers";
13
- import { createTestCopilotContext } from "../../test-helpers/copilot-context";
14
- import { useRenderCustomMessages } from "../../v2";
15
-
16
- type TestMessage = {
17
- id: string;
18
- role: "user" | "assistant" | "system" | string;
19
- content?: string;
20
- };
21
-
22
- type TestAgentSubscriber = {
23
- onStateChanged?: (args?: { state?: Record<string, unknown> }) => void;
24
- onStepStartedEvent?: (args: { event: { stepName: string } }) => void;
25
- onStepFinishedEvent?: (args: { event: { stepName: string } }) => void;
26
- };
27
-
28
- const mockAgent = {
29
- messages: [] as TestMessage[],
30
- state: {},
31
- isRunning: true,
32
- subscribe: vi.fn(),
33
- setMessages: vi.fn(),
34
- setState: vi.fn(),
35
- addMessage: vi.fn(),
36
- abortRun: vi.fn(),
37
- runAgent: vi.fn(),
38
- detachActiveRun: vi.fn().mockResolvedValue(undefined),
39
- };
40
-
41
- let lastSubscriber: TestAgentSubscriber | null = null;
42
-
43
- vi.mock("../../v2", () => ({
44
- useAgent: vi.fn(() => ({ agent: mockAgent })),
45
- useCopilotKit: vi.fn(() => ({
46
- copilotkit: {
47
- connectAgent: vi.fn(),
48
- getRunIdForMessage: vi.fn(),
49
- runAgent: vi.fn(),
50
- clearSuggestions: vi.fn(),
51
- addSuggestionsConfig: vi.fn(),
52
- reloadSuggestions: vi.fn(),
53
- interruptElement: null,
54
- subscribe: vi.fn(() => ({ unsubscribe: vi.fn() })),
55
- },
56
- })),
57
- useCopilotChatConfiguration: vi.fn(() => ({ agentId: "test-agent" })),
58
- useRenderCustomMessages: vi.fn(() => undefined),
59
- useSuggestions: vi.fn(() => ({ suggestions: [], isLoading: false })),
60
- }));
61
-
62
- vi.mock("../../components/toast/toast-provider", () => ({
63
- useToast: () => ({
64
- setBannerError: vi.fn(),
65
- addToast: vi.fn(),
66
- }),
67
- }));
68
-
69
- vi.mock("../../components/error-boundary/error-utils", () => ({
70
- useAsyncCallback: <T extends (...args: unknown[]) => unknown>(fn: T) => fn,
71
- }));
72
-
73
- vi.mock("../use-lazy-tool-renderer", () => ({
74
- useLazyToolRenderer: vi.fn(() => () => null),
75
- }));
76
-
77
- function TestHarness({ snapshot }: { snapshot: string }) {
78
- useCoAgentStateRender<{ current_step?: string }>({
79
- name: "test-agent",
80
- render: ({ state }) => (
81
- <div data-testid="state">{state.current_step ?? "none"}</div>
82
- ),
83
- });
84
-
85
- return (
86
- <CoAgentStateRenderBridge
87
- agentId="test-agent"
88
- message={{ id: "msg-1", role: "assistant" }}
89
- position="after"
90
- runId="run-1"
91
- messageIndex={0}
92
- messageIndexInRun={0}
93
- numberOfMessagesInRun={1}
94
- stateSnapshot={snapshot}
95
- />
96
- );
97
- }
98
-
99
- function SnapshotHarness({
100
- snapshot,
101
- message,
102
- }: {
103
- snapshot: string;
104
- message: TestMessage;
105
- }) {
106
- useCoAgentStateRender<{ current_step?: string }>({
107
- name: "test-agent",
108
- render: ({ state }) => (
109
- <div data-testid="state">{state.current_step ?? "none"}</div>
110
- ),
111
- });
112
-
113
- return (
114
- <CoAgentStateRenderBridge
115
- agentId="test-agent"
116
- message={message}
117
- position="after"
118
- runId="run-early"
119
- messageIndex={0}
120
- messageIndexInRun={0}
121
- numberOfMessagesInRun={1}
122
- stateSnapshot={snapshot}
123
- />
124
- );
125
- }
126
-
127
- function LiveStateHarness({
128
- message,
129
- }: {
130
- message: Pick<TestMessage, "id" | "role">;
131
- }) {
132
- useCoAgentStateRender<{ current_step?: string }>({
133
- name: "test-agent",
134
- render: ({ state }) => (
135
- <div data-testid="state">{state.current_step ?? "none"}</div>
136
- ),
137
- });
138
-
139
- return (
140
- <CoAgentStateRenderBridge
141
- agentId="test-agent"
142
- message={message}
143
- position="after"
144
- runId="run-live"
145
- messageIndex={0}
146
- messageIndexInRun={0}
147
- numberOfMessagesInRun={1}
148
- stateSnapshot={undefined}
149
- />
150
- );
151
- }
152
-
153
- function NonFirstMessageHarness({ snapshot }: { snapshot: string }) {
154
- useCoAgentStateRender<{ current_step?: string }>({
155
- name: "test-agent",
156
- render: ({ state }) => (
157
- <div data-testid="state">{state.current_step ?? "none"}</div>
158
- ),
159
- });
160
-
161
- return (
162
- <CoAgentStateRenderBridge
163
- agentId="test-agent"
164
- message={{ id: "msg-second", role: "assistant", content: "" }}
165
- position="after"
166
- runId="run-non-first"
167
- messageIndex={1}
168
- messageIndexInRun={1}
169
- numberOfMessagesInRun={2}
170
- stateSnapshot={snapshot}
171
- />
172
- );
173
- }
174
-
175
- function MultiRunHarness({
176
- snapshot,
177
- runId,
178
- messageId,
179
- messageIndex,
180
- }: {
181
- snapshot: string;
182
- runId: string;
183
- messageId: string;
184
- messageIndex: number;
185
- }) {
186
- useCoAgentStateRender<{ current_step?: string }>({
187
- name: "test-agent",
188
- render: ({ state }) => (
189
- <div data-testid="state">{state.current_step ?? "none"}</div>
190
- ),
191
- });
192
-
193
- return (
194
- <CoAgentStateRenderBridge
195
- agentId="test-agent"
196
- message={{ id: messageId, role: "assistant", content: "" }}
197
- position="after"
198
- runId={runId}
199
- messageIndex={messageIndex}
200
- messageIndexInRun={0}
201
- numberOfMessagesInRun={1}
202
- stateSnapshot={snapshot}
203
- />
204
- );
205
- }
206
-
207
- function ClaimsObserver({
208
- onChange,
209
- }: {
210
- onChange: (claims: Record<string, Claim>) => void;
211
- }) {
212
- const { claimsRef } = useCoAgentStateRenders();
213
- React.useEffect(() => {
214
- onChange(claimsRef.current as Record<string, Claim>);
215
- });
216
- return null;
217
- }
218
-
219
- function ChatHarness({ tick }: { tick: number }) {
220
- useCoAgentStateRender<{ current_step?: string }>({
221
- name: "test-agent",
222
- render: ({ state }) => (
223
- <div data-testid="state">{state.current_step ?? "none"}</div>
224
- ),
225
- });
226
-
227
- const { messages } = useCopilotChatInternal();
228
-
229
- return (
230
- <>
231
- {messages.map((message) =>
232
- message.generativeUI ? (
233
- <div key={message.id} data-testid={`message-${message.id}`}>
234
- {message.generativeUI()}
235
- </div>
236
- ) : null,
237
- )}
238
- <div data-testid="tick">{tick}</div>
239
- </>
240
- );
241
- }
242
-
243
- describe("useCoAgentStateRender", () => {
244
- beforeEach(() => {
245
- lastSubscriber = null;
246
- mockAgent.state = {};
247
- mockAgent.messages = [];
248
- mockAgent.subscribe.mockImplementation(
249
- (subscriber: TestAgentSubscriber) => {
250
- lastSubscriber = subscriber;
251
- return { unsubscribe: vi.fn() };
252
- },
253
- );
254
- });
255
-
256
- it("re-renders when state snapshots change", async () => {
257
- const copilotContextValue = createTestCopilotContext();
258
-
259
- const { rerender } = render(
260
- <CopilotContext.Provider value={copilotContextValue}>
261
- <CoAgentStateRendersProvider>
262
- <TestHarness
263
- snapshot={JSON.stringify({ current_step: "Processing..." })}
264
- />
265
- </CoAgentStateRendersProvider>
266
- </CopilotContext.Provider>,
267
- );
268
-
269
- await waitFor(() => {
270
- expect(screen.getByTestId("state").textContent).toBe("Processing...");
271
- });
272
-
273
- rerender(
274
- <CopilotContext.Provider value={copilotContextValue}>
275
- <CoAgentStateRendersProvider>
276
- <TestHarness
277
- snapshot={JSON.stringify({ current_step: "Thinking..." })}
278
- />
279
- </CoAgentStateRendersProvider>
280
- </CopilotContext.Provider>,
281
- );
282
-
283
- await waitFor(() => {
284
- expect(screen.getByTestId("state").textContent).toBe("Thinking...");
285
- });
286
- });
287
-
288
- it("renders snapshots that arrive before assistant text", async () => {
289
- const copilotContextValue = createTestCopilotContext();
290
-
291
- const baseMessage = { id: "msg-early", role: "assistant", content: "" };
292
-
293
- const { rerender } = render(
294
- <CopilotContext.Provider value={copilotContextValue}>
295
- <CoAgentStateRendersProvider>
296
- <SnapshotHarness
297
- message={baseMessage}
298
- snapshot={JSON.stringify({ current_step: "Processing..." })}
299
- />
300
- </CoAgentStateRendersProvider>
301
- </CopilotContext.Provider>,
302
- );
303
-
304
- await waitFor(() => {
305
- expect(screen.getByTestId("state").textContent).toBe("Processing...");
306
- });
307
-
308
- rerender(
309
- <CopilotContext.Provider value={copilotContextValue}>
310
- <CoAgentStateRendersProvider>
311
- <SnapshotHarness
312
- message={baseMessage}
313
- snapshot={JSON.stringify({ current_step: "Thinking..." })}
314
- />
315
- </CoAgentStateRendersProvider>
316
- </CopilotContext.Provider>,
317
- );
318
-
319
- await waitFor(() => {
320
- expect(screen.getByTestId("state").textContent).toBe("Thinking...");
321
- });
322
-
323
- rerender(
324
- <CopilotContext.Provider value={copilotContextValue}>
325
- <CoAgentStateRendersProvider>
326
- <SnapshotHarness
327
- message={{ ...baseMessage, content: "Hello" }}
328
- snapshot={JSON.stringify({ current_step: "Finalizing..." })}
329
- />
330
- </CoAgentStateRendersProvider>
331
- </CopilotContext.Provider>,
332
- );
333
-
334
- await waitFor(() => {
335
- expect(screen.getByTestId("state").textContent).toBe("Finalizing...");
336
- });
337
- });
338
-
339
- it("re-renders from live agent state updates", async () => {
340
- const copilotContextValue = createTestCopilotContext();
341
-
342
- render(
343
- <CopilotContext.Provider value={copilotContextValue}>
344
- <CoAgentStateRendersProvider>
345
- <LiveStateHarness message={{ id: "msg-live", role: "assistant" }} />
346
- </CoAgentStateRendersProvider>
347
- </CopilotContext.Provider>,
348
- );
349
-
350
- mockAgent.state = { current_step: "Processing..." };
351
- act(() => {
352
- lastSubscriber?.onStateChanged?.({ state: mockAgent.state });
353
- });
354
-
355
- await waitFor(() => {
356
- expect(screen.getByTestId("state").textContent).toBe("Processing...");
357
- });
358
-
359
- mockAgent.state = { current_step: "Thinking..." };
360
- act(() => {
361
- lastSubscriber?.onStateChanged?.({ state: mockAgent.state });
362
- });
363
-
364
- await waitFor(() => {
365
- expect(screen.getByTestId("state").textContent).toBe("Thinking...");
366
- });
367
- });
368
-
369
- it("renders state snapshots before any assistant message exists", async () => {
370
- const copilotContextValue = createTestCopilotContext({
371
- threadId: "thread-1",
372
- agentSession: null,
373
- });
374
-
375
- mockAgent.messages = [{ id: "msg-user-1", role: "user", content: "Hi" }];
376
- mockAgent.isRunning = false;
377
- mockAgent.state = {};
378
-
379
- const { rerender } = render(
380
- <CopilotContext.Provider value={copilotContextValue}>
381
- <CoAgentStateRendersProvider>
382
- <ChatHarness tick={0} />
383
- </CoAgentStateRendersProvider>
384
- </CopilotContext.Provider>,
385
- );
386
-
387
- const placeholderTestId =
388
- "message-coagent-state-render-test-agent-pending:msg-user-1";
389
- expect(screen.queryByTestId(placeholderTestId)).toBeNull();
390
-
391
- mockAgent.isRunning = true;
392
- mockAgent.state = { current_step: "Processing..." };
393
-
394
- rerender(
395
- <CopilotContext.Provider value={copilotContextValue}>
396
- <CoAgentStateRendersProvider>
397
- <ChatHarness tick={1} />
398
- </CoAgentStateRendersProvider>
399
- </CopilotContext.Provider>,
400
- );
401
-
402
- await waitFor(() => {
403
- expect(screen.getByTestId("state").textContent).toBe("Processing...");
404
- });
405
-
406
- expect(screen.getByTestId(placeholderTestId)).toBeTruthy();
407
-
408
- const placeholderStep = screen.getByTestId("state").textContent;
409
-
410
- mockAgent.isRunning = false;
411
- mockAgent.state = {};
412
- mockAgent.messages = [
413
- { id: "msg-user-1", role: "user", content: "Hi" },
414
- { id: "msg-assistant-1", role: "assistant", content: "" },
415
- ];
416
-
417
- rerender(
418
- <CopilotContext.Provider value={copilotContextValue}>
419
- <CoAgentStateRendersProvider>
420
- <ChatHarness tick={2} />
421
- </CoAgentStateRendersProvider>
422
- </CopilotContext.Provider>,
423
- );
424
-
425
- await waitFor(() => {
426
- expect(screen.queryByTestId(placeholderTestId)).toBeNull();
427
- expect(screen.getByTestId("message-msg-assistant-1")).toBeTruthy();
428
- expect(screen.getByTestId("state").textContent).toBe("none");
429
- });
430
- });
431
-
432
- it("does not render placeholder until the agent is running or has state", async () => {
433
- const copilotContextValue = createTestCopilotContext({
434
- threadId: "thread-1",
435
- agentSession: null,
436
- });
437
-
438
- mockAgent.messages = [{ id: "msg-user-1", role: "user", content: "Hi" }];
439
- mockAgent.isRunning = false;
440
- mockAgent.state = {};
441
-
442
- render(
443
- <CopilotContext.Provider value={copilotContextValue}>
444
- <CoAgentStateRendersProvider>
445
- <ChatHarness tick={0} />
446
- </CoAgentStateRendersProvider>
447
- </CopilotContext.Provider>,
448
- );
449
-
450
- await waitFor(() => {
451
- expect(
452
- screen.queryByTestId(
453
- "message-coagent-state-render-test-agent-pending:msg-user-1",
454
- ),
455
- ).toBeNull();
456
- });
457
- });
458
-
459
- it("renders for non-first messages in a run", async () => {
460
- const copilotContextValue = createTestCopilotContext();
461
-
462
- render(
463
- <CopilotContext.Provider value={copilotContextValue}>
464
- <CoAgentStateRendersProvider>
465
- <NonFirstMessageHarness
466
- snapshot={JSON.stringify({ current_step: "Processing..." })}
467
- />
468
- </CoAgentStateRendersProvider>
469
- </CopilotContext.Provider>,
470
- );
471
-
472
- await waitFor(() => {
473
- expect(screen.getByTestId("state").textContent).toBe("Processing...");
474
- });
475
- });
476
-
477
- it("falls back to legacy renderer when renderCustomMessages throws", async () => {
478
- vi.mocked(useRenderCustomMessages).mockImplementationOnce(() => () => {
479
- throw new Error("boom");
480
- });
481
-
482
- const copilotContextValue = createTestCopilotContext({
483
- threadId: "thread-1",
484
- agentSession: null,
485
- });
486
-
487
- mockAgent.messages = [
488
- { id: "msg-user-1", role: "user", content: "Hi" },
489
- { id: "msg-assistant-1", role: "assistant", content: "" },
490
- ];
491
- mockAgent.isRunning = true;
492
- mockAgent.state = { current_step: "Processing..." };
493
-
494
- render(
495
- <CopilotContext.Provider value={copilotContextValue}>
496
- <CoAgentStateRendersProvider>
497
- <ChatHarness tick={0} />
498
- </CoAgentStateRendersProvider>
499
- </CopilotContext.Provider>,
500
- );
501
-
502
- await waitFor(() => {
503
- expect(screen.getByTestId("state").textContent).toBe("Processing...");
504
- });
505
- });
506
-
507
- it("prefers legacy renderer over renderCustomMessages when both exist", async () => {
508
- const renderCustomSpy = vi.fn(() => null);
509
- vi.mocked(useRenderCustomMessages).mockImplementationOnce(
510
- () => renderCustomSpy,
511
- );
512
-
513
- const copilotContextValue = createTestCopilotContext({
514
- threadId: "thread-1",
515
- agentSession: null,
516
- });
517
-
518
- mockAgent.messages = [
519
- { id: "msg-user-1", role: "user", content: "Hi" },
520
- { id: "msg-assistant-1", role: "assistant", content: "" },
521
- ];
522
- mockAgent.isRunning = true;
523
- mockAgent.state = { current_step: "Processing..." };
524
-
525
- render(
526
- <CopilotContext.Provider value={copilotContextValue}>
527
- <CoAgentStateRendersProvider>
528
- <ChatHarness tick={0} />
529
- </CoAgentStateRendersProvider>
530
- </CopilotContext.Provider>,
531
- );
532
-
533
- await waitFor(() => {
534
- expect(screen.getByTestId("state").textContent).toBe("Processing...");
535
- });
536
-
537
- expect(renderCustomSpy).not.toHaveBeenCalled();
538
- });
539
-
540
- it("renders empty state when agent state clears after completion", async () => {
541
- const copilotContextValue = createTestCopilotContext({
542
- threadId: "thread-1",
543
- agentSession: null,
544
- });
545
-
546
- mockAgent.messages = [
547
- { id: "msg-user-1", role: "user", content: "Hi" },
548
- { id: "msg-assistant-1", role: "assistant", content: "" },
549
- ];
550
- mockAgent.isRunning = true;
551
- mockAgent.state = { current_step: "Processing..." };
552
-
553
- const { rerender } = render(
554
- <CopilotContext.Provider value={copilotContextValue}>
555
- <CoAgentStateRendersProvider>
556
- <ChatHarness tick={0} />
557
- </CoAgentStateRendersProvider>
558
- </CopilotContext.Provider>,
559
- );
560
-
561
- await waitFor(() => {
562
- expect(screen.getByTestId("state").textContent).toBe("Processing...");
563
- });
564
-
565
- mockAgent.isRunning = false;
566
- mockAgent.state = {};
567
-
568
- rerender(
569
- <CopilotContext.Provider value={copilotContextValue}>
570
- <CoAgentStateRendersProvider>
571
- <ChatHarness tick={1} />
572
- </CoAgentStateRendersProvider>
573
- </CopilotContext.Provider>,
574
- );
575
-
576
- await waitFor(() => {
577
- expect(screen.getByTestId("state").textContent).toBe("none");
578
- });
579
- });
580
-
581
- it("allows independent renders across different runs with the same snapshot", async () => {
582
- const copilotContextValue = createTestCopilotContext();
583
-
584
- const snapshot = JSON.stringify({ current_step: "Processing..." });
585
-
586
- render(
587
- <CopilotContext.Provider value={copilotContextValue}>
588
- <CoAgentStateRendersProvider>
589
- <MultiRunHarness
590
- snapshot={snapshot}
591
- runId="run-1"
592
- messageId="msg-1"
593
- messageIndex={0}
594
- />
595
- <MultiRunHarness
596
- snapshot={snapshot}
597
- runId="run-2"
598
- messageId="msg-2"
599
- messageIndex={1}
600
- />
601
- </CoAgentStateRendersProvider>
602
- </CopilotContext.Provider>,
603
- );
604
-
605
- await waitFor(() => {
606
- const nodes = screen.getAllByTestId("state");
607
- expect(nodes).toHaveLength(2);
608
- expect(nodes[0].textContent).toBe("Processing...");
609
- expect(nodes[1].textContent).toBe("Processing...");
610
- });
611
- });
612
-
613
- it("lets newer assistant messages claim even if snapshot matches", async () => {
614
- const copilotContextValue = createTestCopilotContext();
615
-
616
- const snapshot = JSON.stringify({ current_step: "Processing..." });
617
-
618
- render(
619
- <CopilotContext.Provider value={copilotContextValue}>
620
- <CoAgentStateRendersProvider>
621
- <MultiRunHarness
622
- snapshot={snapshot}
623
- runId="run-1"
624
- messageId="msg-1"
625
- messageIndex={0}
626
- />
627
- <MultiRunHarness
628
- snapshot={snapshot}
629
- runId="run-1"
630
- messageId="msg-2"
631
- messageIndex={1}
632
- />
633
- </CoAgentStateRendersProvider>
634
- </CopilotContext.Provider>,
635
- );
636
-
637
- await waitFor(() => {
638
- const nodes = screen.getAllByTestId("state");
639
- expect(nodes).toHaveLength(2);
640
- expect(nodes[0].textContent).toBe("Processing...");
641
- expect(nodes[1].textContent).toBe("Processing...");
642
- });
643
- });
644
-
645
- it("locks older claims when a newer message claims", async () => {
646
- const copilotContextValue = createTestCopilotContext();
647
-
648
- const snapshot = JSON.stringify({ current_step: "Processing..." });
649
- let latestClaims: Record<string, Claim> = {};
650
-
651
- render(
652
- <CopilotContext.Provider value={copilotContextValue}>
653
- <CoAgentStateRendersProvider>
654
- <ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
655
- <MultiRunHarness
656
- snapshot={snapshot}
657
- runId="run-1"
658
- messageId="msg-1"
659
- messageIndex={0}
660
- />
661
- <MultiRunHarness
662
- snapshot={snapshot}
663
- runId="run-1"
664
- messageId="msg-2"
665
- messageIndex={1}
666
- />
667
- </CoAgentStateRendersProvider>
668
- </CopilotContext.Provider>,
669
- );
670
-
671
- await waitFor(() => {
672
- expect(latestClaims["msg-1"]).toBeTruthy();
673
- expect(latestClaims["msg-2"]).toBeTruthy();
674
- expect(latestClaims["msg-1"].locked).toBe(true);
675
- });
676
- });
677
-
678
- it("keeps older message snapshots stable when a new run starts", async () => {
679
- const copilotContextValue = createTestCopilotContext();
680
-
681
- const snapshot = JSON.stringify({ current_step: "Processing..." });
682
- let latestClaims: Record<string, Claim> = {};
683
-
684
- const { rerender } = render(
685
- <CopilotContext.Provider value={copilotContextValue}>
686
- <CoAgentStateRendersProvider>
687
- <ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
688
- <MultiRunHarness
689
- snapshot={snapshot}
690
- runId="run-1"
691
- messageId="msg-1"
692
- messageIndex={0}
693
- />
694
- </CoAgentStateRendersProvider>
695
- </CopilotContext.Provider>,
696
- );
697
-
698
- await waitFor(() => {
699
- expect(latestClaims["msg-1"]?.stateSnapshot?.current_step).toBe(
700
- "Processing...",
701
- );
702
- });
703
-
704
- rerender(
705
- <CopilotContext.Provider value={copilotContextValue}>
706
- <CoAgentStateRendersProvider>
707
- <ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
708
- <MultiRunHarness
709
- snapshot={snapshot}
710
- runId="run-1"
711
- messageId="msg-1"
712
- messageIndex={0}
713
- />
714
- <MultiRunHarness
715
- snapshot={snapshot}
716
- runId="run-2"
717
- messageId="msg-2"
718
- messageIndex={1}
719
- />
720
- </CoAgentStateRendersProvider>
721
- </CopilotContext.Provider>,
722
- );
723
-
724
- await waitFor(() => {
725
- expect(latestClaims["msg-1"]?.stateSnapshot?.current_step).toBe(
726
- "Processing...",
727
- );
728
- expect(latestClaims["msg-2"]?.stateSnapshot?.current_step).toBe(
729
- "Processing...",
730
- );
731
- });
732
- });
733
-
734
- it("does not overwrite a previous run snapshot when a new run has different state", async () => {
735
- const copilotContextValue = createTestCopilotContext();
736
-
737
- const snapshotRun1 = JSON.stringify({ current_step: "Processing..." });
738
- const snapshotRun2 = JSON.stringify({ current_step: "Finalizing..." });
739
- let latestClaims: Record<string, Claim> = {};
740
-
741
- const { rerender } = render(
742
- <CopilotContext.Provider value={copilotContextValue}>
743
- <CoAgentStateRendersProvider>
744
- <ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
745
- <MultiRunHarness
746
- snapshot={snapshotRun1}
747
- runId="run-1"
748
- messageId="msg-1"
749
- messageIndex={0}
750
- />
751
- </CoAgentStateRendersProvider>
752
- </CopilotContext.Provider>,
753
- );
754
-
755
- await waitFor(() => {
756
- expect(latestClaims["msg-1"]?.stateSnapshot?.current_step).toBe(
757
- "Processing...",
758
- );
759
- });
760
-
761
- rerender(
762
- <CopilotContext.Provider value={copilotContextValue}>
763
- <CoAgentStateRendersProvider>
764
- <ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
765
- <MultiRunHarness
766
- snapshot={snapshotRun1}
767
- runId="run-1"
768
- messageId="msg-1"
769
- messageIndex={0}
770
- />
771
- <MultiRunHarness
772
- snapshot={snapshotRun2}
773
- runId="run-2"
774
- messageId="msg-2"
775
- messageIndex={1}
776
- />
777
- </CoAgentStateRendersProvider>
778
- </CopilotContext.Provider>,
779
- );
780
-
781
- await waitFor(() => {
782
- expect(latestClaims["msg-1"]?.stateSnapshot?.current_step).toBe(
783
- "Processing...",
784
- );
785
- expect(latestClaims["msg-2"]?.stateSnapshot?.current_step).toBe(
786
- "Finalizing...",
787
- );
788
- });
789
- });
790
-
791
- it("locks a claim when newer agent messages exist", async () => {
792
- const copilotContextValue = createTestCopilotContext();
793
-
794
- const snapshot = JSON.stringify({ current_step: "Processing..." });
795
- let latestClaims: Record<string, Claim> = {};
796
-
797
- mockAgent.messages = [
798
- { id: "msg-1", role: "assistant", content: "" },
799
- { id: "msg-2", role: "assistant", content: "" },
800
- ];
801
-
802
- render(
803
- <CopilotContext.Provider value={copilotContextValue}>
804
- <CoAgentStateRendersProvider>
805
- <ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
806
- <MultiRunHarness
807
- snapshot={snapshot}
808
- runId="run-1"
809
- messageId="msg-1"
810
- messageIndex={0}
811
- />
812
- </CoAgentStateRendersProvider>
813
- </CopilotContext.Provider>,
814
- );
815
-
816
- await waitFor(() => {
817
- expect(latestClaims["msg-1"]).toBeTruthy();
818
- expect(latestClaims["msg-1"].locked).toBe(true);
819
- });
820
- });
821
-
822
- it("does not update a locked claim from new agent state", async () => {
823
- const copilotContextValue = createTestCopilotContext({
824
- threadId: "thread-1",
825
- agentSession: null,
826
- });
827
-
828
- let latestClaims: Record<string, Claim> = {};
829
- mockAgent.messages = [
830
- { id: "msg-user-1", role: "user", content: "Hi" },
831
- { id: "msg-assistant-1", role: "assistant", content: "" },
832
- ];
833
- mockAgent.isRunning = true;
834
- mockAgent.state = { current_step: "First" };
835
-
836
- const { rerender } = render(
837
- <CopilotContext.Provider value={copilotContextValue}>
838
- <CoAgentStateRendersProvider>
839
- <ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
840
- <ChatHarness tick={0} />
841
- </CoAgentStateRendersProvider>
842
- </CopilotContext.Provider>,
843
- );
844
-
845
- await waitFor(() => {
846
- expect(latestClaims["msg-assistant-1"]?.stateSnapshot?.current_step).toBe(
847
- "First",
848
- );
849
- });
850
-
851
- mockAgent.messages = [
852
- { id: "msg-user-1", role: "user", content: "Hi" },
853
- { id: "msg-assistant-1", role: "assistant", content: "" },
854
- { id: "msg-user-2", role: "user", content: "Next" },
855
- ];
856
- mockAgent.state = { current_step: "Second" };
857
-
858
- rerender(
859
- <CopilotContext.Provider value={copilotContextValue}>
860
- <CoAgentStateRendersProvider>
861
- <ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
862
- <ChatHarness tick={1} />
863
- </CoAgentStateRendersProvider>
864
- </CopilotContext.Provider>,
865
- );
866
-
867
- await waitFor(() => {
868
- expect(latestClaims["msg-assistant-1"]?.stateSnapshot?.current_step).toBe(
869
- "First",
870
- );
871
- });
872
- });
873
-
874
- it("renders new run snapshots instead of reusing the previous run state", async () => {
875
- const copilotContextValue = createTestCopilotContext({
876
- threadId: "thread-1",
877
- agentSession: null,
878
- });
879
-
880
- mockAgent.messages = [
881
- { id: "msg-user-1", role: "user", content: "Hi" },
882
- { id: "msg-assistant-1", role: "assistant", content: "" },
883
- ];
884
- mockAgent.isRunning = true;
885
- mockAgent.state = { current_step: "First run" };
886
-
887
- const { rerender } = render(
888
- <CopilotContext.Provider value={copilotContextValue}>
889
- <CoAgentStateRendersProvider>
890
- <ChatHarness tick={0} />
891
- </CoAgentStateRendersProvider>
892
- </CopilotContext.Provider>,
893
- );
894
-
895
- await waitFor(() => {
896
- expect(screen.getByTestId("state").textContent).toBe("First run");
897
- });
898
-
899
- mockAgent.messages = [
900
- { id: "msg-user-1", role: "user", content: "Hi" },
901
- { id: "msg-assistant-1", role: "assistant", content: "" },
902
- { id: "msg-user-2", role: "user", content: "Next" },
903
- ];
904
- mockAgent.isRunning = true;
905
- mockAgent.state = { current_step: "Second run" };
906
-
907
- rerender(
908
- <CopilotContext.Provider value={copilotContextValue}>
909
- <CoAgentStateRendersProvider>
910
- <ChatHarness tick={1} />
911
- </CoAgentStateRendersProvider>
912
- </CopilotContext.Provider>,
913
- );
914
-
915
- await waitFor(() => {
916
- const stateNodes = screen.getAllByTestId("state");
917
- const values = stateNodes.map((node) => node.textContent);
918
- expect(values).toContain("Second run");
919
- });
920
- });
921
-
922
- it("does not reuse latest cached snapshot for a new run placeholder before snapshots arrive", async () => {
923
- const copilotContextValue = createTestCopilotContext({
924
- threadId: "thread-1",
925
- agentSession: null,
926
- });
927
-
928
- mockAgent.messages = [
929
- { id: "msg-user-1", role: "user", content: "Hi" },
930
- { id: "msg-assistant-1", role: "assistant", content: "" },
931
- ];
932
- mockAgent.isRunning = true;
933
- mockAgent.state = { current_step: "First run" };
934
-
935
- const { rerender } = render(
936
- <CopilotContext.Provider value={copilotContextValue}>
937
- <CoAgentStateRendersProvider>
938
- <ChatHarness tick={0} />
939
- </CoAgentStateRendersProvider>
940
- </CopilotContext.Provider>,
941
- );
942
-
943
- await waitFor(() => {
944
- expect(screen.getByTestId("state").textContent).toBe("First run");
945
- });
946
-
947
- mockAgent.messages = [
948
- { id: "msg-user-1", role: "user", content: "Hi" },
949
- { id: "msg-assistant-1", role: "assistant", content: "" },
950
- { id: "msg-user-2", role: "user", content: "Next" },
951
- ];
952
- mockAgent.isRunning = true;
953
- mockAgent.state = {};
954
-
955
- rerender(
956
- <CopilotContext.Provider value={copilotContextValue}>
957
- <CoAgentStateRendersProvider>
958
- <ChatHarness tick={1} />
959
- </CoAgentStateRendersProvider>
960
- </CopilotContext.Provider>,
961
- );
962
-
963
- await waitFor(() => {
964
- const placeholder = screen.getByTestId(
965
- "message-coagent-state-render-test-agent-pending:msg-user-2",
966
- );
967
- expect(placeholder.textContent).toContain("none");
968
- });
969
- });
970
-
971
- it("does not show the previous snapshot for a new assistant message before its snapshot arrives", async () => {
972
- const copilotContextValue = createTestCopilotContext({
973
- threadId: "thread-1",
974
- agentSession: null,
975
- });
976
-
977
- mockAgent.messages = [
978
- { id: "msg-user-1", role: "user", content: "Hi" },
979
- { id: "msg-assistant-1", role: "assistant", content: "" },
980
- ];
981
- mockAgent.isRunning = true;
982
- mockAgent.state = { current_step: "First run" };
983
-
984
- const { rerender } = render(
985
- <CopilotContext.Provider value={copilotContextValue}>
986
- <CoAgentStateRendersProvider>
987
- <ChatHarness tick={0} />
988
- </CoAgentStateRendersProvider>
989
- </CopilotContext.Provider>,
990
- );
991
-
992
- await waitFor(() => {
993
- const message = screen.getByTestId("message-msg-assistant-1");
994
- expect(within(message).getByTestId("state").textContent).toBe(
995
- "First run",
996
- );
997
- });
998
-
999
- mockAgent.messages = [
1000
- { id: "msg-user-1", role: "user", content: "Hi" },
1001
- { id: "msg-assistant-1", role: "assistant", content: "" },
1002
- { id: "msg-user-2", role: "user", content: "Next" },
1003
- { id: "msg-assistant-2", role: "assistant", content: "" },
1004
- ];
1005
- mockAgent.isRunning = true;
1006
- mockAgent.state = {};
1007
-
1008
- rerender(
1009
- <CopilotContext.Provider value={copilotContextValue}>
1010
- <CoAgentStateRendersProvider>
1011
- <ChatHarness tick={1} />
1012
- </CoAgentStateRendersProvider>
1013
- </CopilotContext.Provider>,
1014
- );
1015
-
1016
- await waitFor(() => {
1017
- const message = screen.getByTestId("message-msg-assistant-2");
1018
- expect(within(message).getByTestId("state").textContent).toBe("none");
1019
- });
1020
-
1021
- mockAgent.messages = [
1022
- { id: "msg-user-1", role: "user", content: "Hi" },
1023
- { id: "msg-assistant-1", role: "assistant", content: "" },
1024
- { id: "msg-user-2", role: "user", content: "Next" },
1025
- {
1026
- id: "msg-assistant-2",
1027
- role: "assistant",
1028
- content: "",
1029
- state: '{"current_step":"Second run"}',
1030
- },
1031
- ];
1032
-
1033
- rerender(
1034
- <CopilotContext.Provider value={copilotContextValue}>
1035
- <CoAgentStateRendersProvider>
1036
- <ChatHarness tick={2} />
1037
- </CoAgentStateRendersProvider>
1038
- </CopilotContext.Provider>,
1039
- );
1040
-
1041
- await waitFor(() => {
1042
- const message = screen.getByTestId("message-msg-assistant-2");
1043
- expect(within(message).getByTestId("state").textContent).toBe(
1044
- "Second run",
1045
- );
1046
- });
1047
- });
1048
-
1049
- it("does not show previous live state for a new run before the run updates state", async () => {
1050
- const copilotContextValue = createTestCopilotContext({
1051
- threadId: "thread-1",
1052
- agentSession: null,
1053
- });
1054
-
1055
- mockAgent.messages = [
1056
- { id: "msg-user-1", role: "user", content: "Hi" },
1057
- { id: "msg-assistant-1", role: "assistant", content: "" },
1058
- ];
1059
- mockAgent.isRunning = true;
1060
- mockAgent.state = { current_step: "First run" };
1061
-
1062
- const { rerender } = render(
1063
- <CopilotContext.Provider value={copilotContextValue}>
1064
- <CoAgentStateRendersProvider>
1065
- <ChatHarness tick={0} />
1066
- </CoAgentStateRendersProvider>
1067
- </CopilotContext.Provider>,
1068
- );
1069
-
1070
- await waitFor(() => {
1071
- const message = screen.getByTestId("message-msg-assistant-1");
1072
- expect(within(message).getByTestId("state").textContent).toBe(
1073
- "First run",
1074
- );
1075
- });
1076
-
1077
- mockAgent.messages = [
1078
- { id: "msg-user-1", role: "user", content: "Hi" },
1079
- { id: "msg-assistant-1", role: "assistant", content: "" },
1080
- { id: "msg-user-2", role: "user", content: "Next" },
1081
- { id: "msg-assistant-2", role: "assistant", content: "" },
1082
- ];
1083
- mockAgent.isRunning = true;
1084
- // live state still shows the previous run
1085
- mockAgent.state = { current_step: "First run" };
1086
-
1087
- rerender(
1088
- <CopilotContext.Provider value={copilotContextValue}>
1089
- <CoAgentStateRendersProvider>
1090
- <ChatHarness tick={1} />
1091
- </CoAgentStateRendersProvider>
1092
- </CopilotContext.Provider>,
1093
- );
1094
-
1095
- await waitFor(() => {
1096
- const message = screen.getByTestId("message-msg-assistant-2");
1097
- expect(within(message).getByTestId("state").textContent).toBe("none");
1098
- });
1099
-
1100
- mockAgent.state = { current_step: "Second run" };
1101
- act(() => {
1102
- lastSubscriber?.onStateChanged?.({ state: mockAgent.state });
1103
- });
1104
-
1105
- await waitFor(() => {
1106
- const message = screen.getByTestId("message-msg-assistant-2");
1107
- expect(within(message).getByTestId("state").textContent).toBe(
1108
- "Second run",
1109
- );
1110
- });
1111
- });
1112
-
1113
- it("renders an explicit empty state snapshot", async () => {
1114
- const copilotContextValue = createTestCopilotContext({
1115
- threadId: "thread-1",
1116
- agentSession: null,
1117
- });
1118
-
1119
- mockAgent.messages = [
1120
- {
1121
- id: "msg-user-1",
1122
- role: "user",
1123
- content: "Hi",
1124
- },
1125
- {
1126
- id: "msg-assistant-1",
1127
- role: "assistant",
1128
- content: "",
1129
- state: '{"current_step":"Processing..."}',
1130
- },
1131
- ];
1132
- mockAgent.isRunning = true;
1133
- mockAgent.state = { current_step: "Processing..." };
1134
-
1135
- const { rerender } = render(
1136
- <CopilotContext.Provider value={copilotContextValue}>
1137
- <CoAgentStateRendersProvider>
1138
- <ChatHarness tick={0} />
1139
- </CoAgentStateRendersProvider>
1140
- </CopilotContext.Provider>,
1141
- );
1142
-
1143
- await waitFor(() => {
1144
- const message = screen.getByTestId("message-msg-assistant-1");
1145
- expect(within(message).getByTestId("state").textContent).toBe(
1146
- "Processing...",
1147
- );
1148
- });
1149
-
1150
- mockAgent.messages = [
1151
- {
1152
- id: "msg-user-1",
1153
- role: "user",
1154
- content: "Hi",
1155
- },
1156
- {
1157
- id: "msg-assistant-1",
1158
- role: "assistant",
1159
- content: "",
1160
- state: "{}",
1161
- },
1162
- ];
1163
-
1164
- rerender(
1165
- <CopilotContext.Provider value={copilotContextValue}>
1166
- <CoAgentStateRendersProvider>
1167
- <ChatHarness tick={1} />
1168
- </CoAgentStateRendersProvider>
1169
- </CopilotContext.Provider>,
1170
- );
1171
-
1172
- await waitFor(() => {
1173
- const message = screen.getByTestId("message-msg-assistant-1");
1174
- expect(within(message).getByTestId("state").textContent).toBe("none");
1175
- });
1176
- });
1177
-
1178
- it("renders an empty live state update", async () => {
1179
- const copilotContextValue = createTestCopilotContext();
1180
-
1181
- render(
1182
- <CopilotContext.Provider value={copilotContextValue}>
1183
- <CoAgentStateRendersProvider>
1184
- <LiveStateHarness
1185
- message={{ id: "msg-live-empty", role: "assistant" }}
1186
- />
1187
- </CoAgentStateRendersProvider>
1188
- </CopilotContext.Provider>,
1189
- );
1190
-
1191
- mockAgent.state = { current_step: "Processing..." };
1192
- act(() => {
1193
- lastSubscriber?.onStateChanged?.({ state: mockAgent.state });
1194
- });
1195
-
1196
- await waitFor(() => {
1197
- expect(screen.getByTestId("state").textContent).toBe("Processing...");
1198
- });
1199
-
1200
- mockAgent.state = {};
1201
- act(() => {
1202
- lastSubscriber?.onStateChanged?.({ state: mockAgent.state });
1203
- });
1204
-
1205
- await waitFor(() => {
1206
- expect(screen.getByTestId("state").textContent).toBe("none");
1207
- });
1208
- });
1209
- });