@copilotkit/react-core 1.54.1 → 1.55.0-next.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/CHANGELOG.md +117 -116
  2. package/dist/copilotkit-B3Mb1yVE.cjs +7975 -0
  3. package/dist/copilotkit-B3Mb1yVE.cjs.map +1 -0
  4. package/dist/copilotkit-DBzgOMby.d.cts +2182 -0
  5. package/dist/copilotkit-DBzgOMby.d.cts.map +1 -0
  6. package/dist/copilotkit-DNYSFuz5.mjs +7562 -0
  7. package/dist/copilotkit-DNYSFuz5.mjs.map +1 -0
  8. package/dist/copilotkit-Dy5w3qEV.d.mts +2182 -0
  9. package/dist/copilotkit-Dy5w3qEV.d.mts.map +1 -0
  10. package/dist/index.cjs +27 -28
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.d.cts +3 -3
  13. package/dist/index.d.cts.map +1 -1
  14. package/dist/index.d.mts +3 -3
  15. package/dist/index.d.mts.map +1 -1
  16. package/dist/index.mjs +4 -5
  17. package/dist/index.mjs.map +1 -1
  18. package/dist/index.umd.js +1941 -35
  19. package/dist/index.umd.js.map +1 -1
  20. package/dist/v2/index.cjs +77 -7
  21. package/dist/v2/index.css +1 -2
  22. package/dist/v2/index.d.cts +6 -4
  23. package/dist/v2/index.d.mts +6 -4
  24. package/dist/v2/index.mjs +7 -4
  25. package/dist/v2/index.umd.js +5725 -24
  26. package/dist/v2/index.umd.js.map +1 -1
  27. package/package.json +37 -9
  28. package/scripts/scope-preflight.mjs +101 -0
  29. package/src/components/CopilotListeners.tsx +2 -6
  30. package/src/components/copilot-provider/copilot-messages.tsx +1 -1
  31. package/src/components/copilot-provider/copilotkit-props.tsx +1 -1
  32. package/src/components/copilot-provider/copilotkit.tsx +4 -4
  33. package/src/context/copilot-messages-context.tsx +1 -1
  34. package/src/hooks/__tests__/use-coagent-config.test.ts +2 -2
  35. package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +2 -2
  36. package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +3 -7
  37. package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +1 -1
  38. package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +4 -4
  39. package/src/hooks/use-agent-nodename.ts +1 -1
  40. package/src/hooks/use-coagent-state-render-bridge.tsx +1 -4
  41. package/src/hooks/use-coagent.ts +1 -1
  42. package/src/hooks/use-configure-chat-suggestions.tsx +2 -2
  43. package/src/hooks/use-copilot-chat-suggestions.tsx +2 -2
  44. package/src/hooks/use-copilot-chat_internal.ts +2 -2
  45. package/src/hooks/use-copilot-readable.ts +1 -1
  46. package/src/hooks/use-frontend-tool.ts +2 -2
  47. package/src/hooks/use-human-in-the-loop.ts +2 -2
  48. package/src/hooks/use-langgraph-interrupt.ts +2 -5
  49. package/src/hooks/use-lazy-tool-renderer.tsx +1 -1
  50. package/src/hooks/use-render-tool-call.ts +1 -1
  51. package/src/lib/copilot-task.ts +1 -1
  52. package/src/setupTests.ts +18 -14
  53. package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +176 -0
  54. package/src/v2/__tests__/globalSetup.ts +14 -0
  55. package/src/v2/__tests__/setup.ts +93 -0
  56. package/src/v2/__tests__/utils/test-helpers.tsx +470 -0
  57. package/src/v2/a2ui/A2UIMessageRenderer.tsx +206 -0
  58. package/src/v2/components/CopilotKitInspector.tsx +50 -0
  59. package/src/v2/components/MCPAppsActivityRenderer.tsx +785 -0
  60. package/src/v2/components/WildcardToolCallRender.tsx +86 -0
  61. package/src/v2/components/__tests__/license-warning-banner.test.tsx +46 -0
  62. package/src/v2/components/chat/CopilotChat.tsx +431 -0
  63. package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +375 -0
  64. package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +350 -0
  65. package/src/v2/components/chat/CopilotChatInput.tsx +1302 -0
  66. package/src/v2/components/chat/CopilotChatMessageView.tsx +556 -0
  67. package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +252 -0
  68. package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +59 -0
  69. package/src/v2/components/chat/CopilotChatSuggestionView.tsx +133 -0
  70. package/src/v2/components/chat/CopilotChatToggleButton.tsx +171 -0
  71. package/src/v2/components/chat/CopilotChatToolCallsView.tsx +40 -0
  72. package/src/v2/components/chat/CopilotChatUserMessage.tsx +388 -0
  73. package/src/v2/components/chat/CopilotChatView.tsx +598 -0
  74. package/src/v2/components/chat/CopilotModalHeader.tsx +129 -0
  75. package/src/v2/components/chat/CopilotPopup.tsx +81 -0
  76. package/src/v2/components/chat/CopilotPopupView.tsx +317 -0
  77. package/src/v2/components/chat/CopilotSidebar.tsx +76 -0
  78. package/src/v2/components/chat/CopilotSidebarView.tsx +255 -0
  79. package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +1113 -0
  80. package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +73 -0
  81. package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +432 -0
  82. package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +150 -0
  83. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +624 -0
  84. package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +702 -0
  85. package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +107 -0
  86. package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +929 -0
  87. package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +986 -0
  88. package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +1004 -0
  89. package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +169 -0
  90. package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +530 -0
  91. package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +782 -0
  92. package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +2413 -0
  93. package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +621 -0
  94. package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +853 -0
  95. package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +1050 -0
  96. package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +484 -0
  97. package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +612 -0
  98. package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +502 -0
  99. package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +1011 -0
  100. package/src/v2/components/chat/__tests__/setup.ts +1 -0
  101. package/src/v2/components/chat/index.ts +79 -0
  102. package/src/v2/components/index.ts +7 -0
  103. package/src/v2/components/license-warning-banner.tsx +198 -0
  104. package/src/v2/components/ui/button.tsx +123 -0
  105. package/src/v2/components/ui/dropdown-menu.tsx +258 -0
  106. package/src/v2/components/ui/tooltip.tsx +60 -0
  107. package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +152 -0
  108. package/src/v2/hooks/__tests__/standard-schema.test.tsx +282 -0
  109. package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +132 -0
  110. package/src/v2/hooks/__tests__/use-agent-context.test.tsx +401 -0
  111. package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +44 -0
  112. package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +205 -0
  113. package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +148 -0
  114. package/src/v2/hooks/__tests__/use-component.test.tsx +123 -0
  115. package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +696 -0
  116. package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +153 -0
  117. package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +167 -0
  118. package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +2129 -0
  119. package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +1261 -0
  120. package/src/v2/hooks/__tests__/use-interrupt.test.tsx +397 -0
  121. package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +56 -0
  122. package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +192 -0
  123. package/src/v2/hooks/__tests__/use-render-tool.test.tsx +259 -0
  124. package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +524 -0
  125. package/src/v2/hooks/__tests__/use-threads.test.tsx +433 -0
  126. package/src/v2/hooks/__tests__/zod-regression.test.tsx +311 -0
  127. package/src/v2/hooks/index.ts +18 -0
  128. package/src/v2/hooks/use-agent-context.tsx +45 -0
  129. package/src/v2/hooks/use-agent.tsx +155 -0
  130. package/src/v2/hooks/use-component.tsx +89 -0
  131. package/src/v2/hooks/use-configure-suggestions.tsx +187 -0
  132. package/src/v2/hooks/use-default-render-tool.tsx +254 -0
  133. package/src/v2/hooks/use-frontend-tool.tsx +43 -0
  134. package/src/v2/hooks/use-human-in-the-loop.tsx +81 -0
  135. package/src/v2/hooks/use-interrupt.tsx +305 -0
  136. package/src/v2/hooks/use-keyboard-height.tsx +67 -0
  137. package/src/v2/hooks/use-render-activity-message.tsx +73 -0
  138. package/src/v2/hooks/use-render-custom-messages.tsx +93 -0
  139. package/src/v2/hooks/use-render-tool-call.tsx +175 -0
  140. package/src/v2/hooks/use-render-tool.tsx +181 -0
  141. package/src/v2/hooks/use-suggestions.tsx +91 -0
  142. package/src/v2/hooks/use-threads.tsx +256 -0
  143. package/src/v2/hooks/useKatexStyles.ts +27 -0
  144. package/src/v2/index.css +1 -1
  145. package/src/v2/index.ts +18 -2
  146. package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +495 -0
  147. package/src/v2/lib/__tests__/renderSlot.test.tsx +588 -0
  148. package/src/v2/lib/react-core.ts +156 -0
  149. package/src/v2/lib/slots.tsx +143 -0
  150. package/src/v2/lib/transcription-client.ts +184 -0
  151. package/src/v2/lib/utils.ts +8 -0
  152. package/src/v2/providers/CopilotChatConfigurationProvider.tsx +162 -0
  153. package/src/v2/providers/CopilotKitProvider.tsx +600 -0
  154. package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +546 -0
  155. package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +101 -0
  156. package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +69 -0
  157. package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +881 -0
  158. package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +740 -0
  159. package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +642 -0
  160. package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +294 -0
  161. package/src/v2/providers/index.ts +14 -0
  162. package/src/v2/styles/globals.css +230 -0
  163. package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +525 -0
  164. package/src/v2/types/defineToolCallRenderer.ts +65 -0
  165. package/src/v2/types/frontend-tool.ts +8 -0
  166. package/src/v2/types/human-in-the-loop.ts +33 -0
  167. package/src/v2/types/index.ts +7 -0
  168. package/src/v2/types/interrupt.ts +15 -0
  169. package/src/v2/types/react-activity-message-renderer.ts +27 -0
  170. package/src/v2/types/react-custom-message-renderer.ts +17 -0
  171. package/src/v2/types/react-tool-call-renderer.ts +32 -0
  172. package/tsdown.config.ts +34 -10
  173. package/vitest.config.mjs +4 -3
  174. package/LICENSE +0 -21
  175. package/dist/copilotkit-BRPQ2sqS.d.cts +0 -670
  176. package/dist/copilotkit-BRPQ2sqS.d.cts.map +0 -1
  177. package/dist/copilotkit-C94ayZbs.cjs +0 -2161
  178. package/dist/copilotkit-C94ayZbs.cjs.map +0 -1
  179. package/dist/copilotkit-CwZMFmSK.d.mts +0 -670
  180. package/dist/copilotkit-CwZMFmSK.d.mts.map +0 -1
  181. package/dist/copilotkit-Yh_Ld_FX.mjs +0 -2031
  182. package/dist/copilotkit-Yh_Ld_FX.mjs.map +0 -1
  183. package/dist/v2/index.css.map +0 -1
@@ -0,0 +1,1113 @@
1
+ import React, { useEffect } from "react";
2
+ import { screen, fireEvent, waitFor } 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 expand and collapse reasoning content on click", async () => {
1064
+ const agent = new MockStepwiseAgent();
1065
+ renderWithCopilotKit({ agent });
1066
+
1067
+ const input = await screen.findByRole("textbox");
1068
+ fireEvent.change(input, { target: { value: "Toggle test" } });
1069
+ fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
1070
+
1071
+ await waitFor(() => {
1072
+ expect(screen.getByText("Toggle test")).toBeDefined();
1073
+ });
1074
+
1075
+ const reasoningId = testId("reasoning");
1076
+ const textId = testId("text");
1077
+
1078
+ agent.emit(runStartedEvent());
1079
+ emitReasoningSequence(
1080
+ agent,
1081
+ reasoningId,
1082
+ "This is expandable reasoning content",
1083
+ );
1084
+ agent.emit(textChunkEvent(textId, "Done thinking."));
1085
+ agent.emit(runFinishedEvent());
1086
+ agent.complete();
1087
+
1088
+ // After reasoning finishes, it should auto-collapse
1089
+ await waitFor(() => {
1090
+ const header = screen.getByText(/Thought for/);
1091
+ expect(header).toBeDefined();
1092
+ // The header's button should have aria-expanded="false" after collapsing
1093
+ const button = header.closest("button");
1094
+ expect(button?.getAttribute("aria-expanded")).toBe("false");
1095
+ });
1096
+
1097
+ // Click to expand
1098
+ const header = screen.getByText(/Thought for/);
1099
+ const button = header.closest("button");
1100
+ if (button) {
1101
+ fireEvent.click(button);
1102
+ }
1103
+
1104
+ // Should now be expanded
1105
+ await waitFor(() => {
1106
+ const expandedButton = screen
1107
+ .getByText(/Thought for/)
1108
+ .closest("button");
1109
+ expect(expandedButton?.getAttribute("aria-expanded")).toBe("true");
1110
+ });
1111
+ });
1112
+ });
1113
+ });