@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.
- package/CHANGELOG.md +117 -116
- package/dist/copilotkit-B3Mb1yVE.cjs +7975 -0
- package/dist/copilotkit-B3Mb1yVE.cjs.map +1 -0
- package/dist/copilotkit-DBzgOMby.d.cts +2182 -0
- package/dist/copilotkit-DBzgOMby.d.cts.map +1 -0
- package/dist/copilotkit-DNYSFuz5.mjs +7562 -0
- package/dist/copilotkit-DNYSFuz5.mjs.map +1 -0
- package/dist/copilotkit-Dy5w3qEV.d.mts +2182 -0
- package/dist/copilotkit-Dy5w3qEV.d.mts.map +1 -0
- package/dist/index.cjs +27 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4 -5
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1941 -35
- package/dist/index.umd.js.map +1 -1
- package/dist/v2/index.cjs +77 -7
- package/dist/v2/index.css +1 -2
- package/dist/v2/index.d.cts +6 -4
- package/dist/v2/index.d.mts +6 -4
- package/dist/v2/index.mjs +7 -4
- package/dist/v2/index.umd.js +5725 -24
- package/dist/v2/index.umd.js.map +1 -1
- package/package.json +37 -9
- package/scripts/scope-preflight.mjs +101 -0
- package/src/components/CopilotListeners.tsx +2 -6
- package/src/components/copilot-provider/copilot-messages.tsx +1 -1
- package/src/components/copilot-provider/copilotkit-props.tsx +1 -1
- package/src/components/copilot-provider/copilotkit.tsx +4 -4
- package/src/context/copilot-messages-context.tsx +1 -1
- package/src/hooks/__tests__/use-coagent-config.test.ts +2 -2
- package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +2 -2
- package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +3 -7
- package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +1 -1
- package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +4 -4
- package/src/hooks/use-agent-nodename.ts +1 -1
- package/src/hooks/use-coagent-state-render-bridge.tsx +1 -4
- package/src/hooks/use-coagent.ts +1 -1
- package/src/hooks/use-configure-chat-suggestions.tsx +2 -2
- package/src/hooks/use-copilot-chat-suggestions.tsx +2 -2
- package/src/hooks/use-copilot-chat_internal.ts +2 -2
- package/src/hooks/use-copilot-readable.ts +1 -1
- package/src/hooks/use-frontend-tool.ts +2 -2
- package/src/hooks/use-human-in-the-loop.ts +2 -2
- package/src/hooks/use-langgraph-interrupt.ts +2 -5
- package/src/hooks/use-lazy-tool-renderer.tsx +1 -1
- package/src/hooks/use-render-tool-call.ts +1 -1
- package/src/lib/copilot-task.ts +1 -1
- package/src/setupTests.ts +18 -14
- package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +176 -0
- package/src/v2/__tests__/globalSetup.ts +14 -0
- package/src/v2/__tests__/setup.ts +93 -0
- package/src/v2/__tests__/utils/test-helpers.tsx +470 -0
- package/src/v2/a2ui/A2UIMessageRenderer.tsx +206 -0
- package/src/v2/components/CopilotKitInspector.tsx +50 -0
- package/src/v2/components/MCPAppsActivityRenderer.tsx +785 -0
- package/src/v2/components/WildcardToolCallRender.tsx +86 -0
- package/src/v2/components/__tests__/license-warning-banner.test.tsx +46 -0
- package/src/v2/components/chat/CopilotChat.tsx +431 -0
- package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +375 -0
- package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +350 -0
- package/src/v2/components/chat/CopilotChatInput.tsx +1302 -0
- package/src/v2/components/chat/CopilotChatMessageView.tsx +556 -0
- package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +252 -0
- package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +59 -0
- package/src/v2/components/chat/CopilotChatSuggestionView.tsx +133 -0
- package/src/v2/components/chat/CopilotChatToggleButton.tsx +171 -0
- package/src/v2/components/chat/CopilotChatToolCallsView.tsx +40 -0
- package/src/v2/components/chat/CopilotChatUserMessage.tsx +388 -0
- package/src/v2/components/chat/CopilotChatView.tsx +598 -0
- package/src/v2/components/chat/CopilotModalHeader.tsx +129 -0
- package/src/v2/components/chat/CopilotPopup.tsx +81 -0
- package/src/v2/components/chat/CopilotPopupView.tsx +317 -0
- package/src/v2/components/chat/CopilotSidebar.tsx +76 -0
- package/src/v2/components/chat/CopilotSidebarView.tsx +255 -0
- package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +1113 -0
- package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +73 -0
- package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +432 -0
- package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +150 -0
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +624 -0
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +702 -0
- package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +107 -0
- package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +929 -0
- package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +986 -0
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +1004 -0
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +169 -0
- package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +530 -0
- package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +782 -0
- package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +2413 -0
- package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +621 -0
- package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +853 -0
- package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +1050 -0
- package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +484 -0
- package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +612 -0
- package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +502 -0
- package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +1011 -0
- package/src/v2/components/chat/__tests__/setup.ts +1 -0
- package/src/v2/components/chat/index.ts +79 -0
- package/src/v2/components/index.ts +7 -0
- package/src/v2/components/license-warning-banner.tsx +198 -0
- package/src/v2/components/ui/button.tsx +123 -0
- package/src/v2/components/ui/dropdown-menu.tsx +258 -0
- package/src/v2/components/ui/tooltip.tsx +60 -0
- package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +152 -0
- package/src/v2/hooks/__tests__/standard-schema.test.tsx +282 -0
- package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +132 -0
- package/src/v2/hooks/__tests__/use-agent-context.test.tsx +401 -0
- package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +44 -0
- package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +205 -0
- package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +148 -0
- package/src/v2/hooks/__tests__/use-component.test.tsx +123 -0
- package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +696 -0
- package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +153 -0
- package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +167 -0
- package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +2129 -0
- package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +1261 -0
- package/src/v2/hooks/__tests__/use-interrupt.test.tsx +397 -0
- package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +56 -0
- package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +192 -0
- package/src/v2/hooks/__tests__/use-render-tool.test.tsx +259 -0
- package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +524 -0
- package/src/v2/hooks/__tests__/use-threads.test.tsx +433 -0
- package/src/v2/hooks/__tests__/zod-regression.test.tsx +311 -0
- package/src/v2/hooks/index.ts +18 -0
- package/src/v2/hooks/use-agent-context.tsx +45 -0
- package/src/v2/hooks/use-agent.tsx +155 -0
- package/src/v2/hooks/use-component.tsx +89 -0
- package/src/v2/hooks/use-configure-suggestions.tsx +187 -0
- package/src/v2/hooks/use-default-render-tool.tsx +254 -0
- package/src/v2/hooks/use-frontend-tool.tsx +43 -0
- package/src/v2/hooks/use-human-in-the-loop.tsx +81 -0
- package/src/v2/hooks/use-interrupt.tsx +305 -0
- package/src/v2/hooks/use-keyboard-height.tsx +67 -0
- package/src/v2/hooks/use-render-activity-message.tsx +73 -0
- package/src/v2/hooks/use-render-custom-messages.tsx +93 -0
- package/src/v2/hooks/use-render-tool-call.tsx +175 -0
- package/src/v2/hooks/use-render-tool.tsx +181 -0
- package/src/v2/hooks/use-suggestions.tsx +91 -0
- package/src/v2/hooks/use-threads.tsx +256 -0
- package/src/v2/hooks/useKatexStyles.ts +27 -0
- package/src/v2/index.css +1 -1
- package/src/v2/index.ts +18 -2
- package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +495 -0
- package/src/v2/lib/__tests__/renderSlot.test.tsx +588 -0
- package/src/v2/lib/react-core.ts +156 -0
- package/src/v2/lib/slots.tsx +143 -0
- package/src/v2/lib/transcription-client.ts +184 -0
- package/src/v2/lib/utils.ts +8 -0
- package/src/v2/providers/CopilotChatConfigurationProvider.tsx +162 -0
- package/src/v2/providers/CopilotKitProvider.tsx +600 -0
- package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +546 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +101 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +69 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +881 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +740 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +642 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +294 -0
- package/src/v2/providers/index.ts +14 -0
- package/src/v2/styles/globals.css +230 -0
- package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +525 -0
- package/src/v2/types/defineToolCallRenderer.ts +65 -0
- package/src/v2/types/frontend-tool.ts +8 -0
- package/src/v2/types/human-in-the-loop.ts +33 -0
- package/src/v2/types/index.ts +7 -0
- package/src/v2/types/interrupt.ts +15 -0
- package/src/v2/types/react-activity-message-renderer.ts +27 -0
- package/src/v2/types/react-custom-message-renderer.ts +17 -0
- package/src/v2/types/react-tool-call-renderer.ts +32 -0
- package/tsdown.config.ts +34 -10
- package/vitest.config.mjs +4 -3
- package/LICENSE +0 -21
- package/dist/copilotkit-BRPQ2sqS.d.cts +0 -670
- package/dist/copilotkit-BRPQ2sqS.d.cts.map +0 -1
- package/dist/copilotkit-C94ayZbs.cjs +0 -2161
- package/dist/copilotkit-C94ayZbs.cjs.map +0 -1
- package/dist/copilotkit-CwZMFmSK.d.mts +0 -670
- package/dist/copilotkit-CwZMFmSK.d.mts.map +0 -1
- package/dist/copilotkit-Yh_Ld_FX.mjs +0 -2031
- package/dist/copilotkit-Yh_Ld_FX.mjs.map +0 -1
- package/dist/v2/index.css.map +0 -1
|
@@ -0,0 +1,2413 @@
|
|
|
1
|
+
import React, { useRef, useState } from "react";
|
|
2
|
+
import {
|
|
3
|
+
render,
|
|
4
|
+
screen,
|
|
5
|
+
fireEvent,
|
|
6
|
+
waitFor,
|
|
7
|
+
act,
|
|
8
|
+
} from "@testing-library/react";
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
import { CopilotKitProvider } from "../../../providers/CopilotKitProvider";
|
|
11
|
+
import { CopilotChat } from "../CopilotChat";
|
|
12
|
+
import {
|
|
13
|
+
AbstractAgent,
|
|
14
|
+
EventType,
|
|
15
|
+
type BaseEvent,
|
|
16
|
+
type RunAgentInput,
|
|
17
|
+
} from "@ag-ui/client";
|
|
18
|
+
import { Observable, Subject } from "rxjs";
|
|
19
|
+
import { defineToolCallRenderer, ReactToolCallRenderer } from "../../../types";
|
|
20
|
+
import { ToolCallStatus } from "@copilotkit/core";
|
|
21
|
+
import { CopilotChatMessageView } from "../CopilotChatMessageView";
|
|
22
|
+
import { CopilotChatView, CopilotChatViewProps } from "../CopilotChatView";
|
|
23
|
+
import { CopilotChatConfigurationProvider } from "../../../providers/CopilotChatConfigurationProvider";
|
|
24
|
+
import { ActivityMessage, AssistantMessage, Message } from "@ag-ui/core";
|
|
25
|
+
import {
|
|
26
|
+
ReactActivityMessageRenderer,
|
|
27
|
+
ReactCustomMessageRenderer,
|
|
28
|
+
} from "../../../types";
|
|
29
|
+
import CopilotChatInput, { CopilotChatInputProps } from "../CopilotChatInput";
|
|
30
|
+
import { CopilotChatSuggestionView } from "../CopilotChatSuggestionView";
|
|
31
|
+
import { CopilotChatAssistantMessage } from "../CopilotChatAssistantMessage";
|
|
32
|
+
|
|
33
|
+
// A controllable streaming agent to step through events deterministically
|
|
34
|
+
class MockStepwiseAgent extends AbstractAgent {
|
|
35
|
+
private subject = new Subject<BaseEvent>();
|
|
36
|
+
|
|
37
|
+
emit(event: BaseEvent) {
|
|
38
|
+
if (event.type === EventType.RUN_STARTED) {
|
|
39
|
+
this.isRunning = true;
|
|
40
|
+
} else if (
|
|
41
|
+
event.type === EventType.RUN_FINISHED ||
|
|
42
|
+
event.type === EventType.RUN_ERROR
|
|
43
|
+
) {
|
|
44
|
+
this.isRunning = false;
|
|
45
|
+
}
|
|
46
|
+
this.subject.next(event);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
complete() {
|
|
50
|
+
this.isRunning = false;
|
|
51
|
+
this.subject.complete();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
clone(): MockStepwiseAgent {
|
|
55
|
+
// For tests, return same instance so we can keep controlling it.
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async detachActiveRun(): Promise<void> {}
|
|
60
|
+
|
|
61
|
+
run(_input: RunAgentInput): Observable<BaseEvent> {
|
|
62
|
+
return this.subject.asObservable();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
describe("Tool Call Re-render Prevention", () => {
|
|
67
|
+
it("should not re-render a completed tool call when subsequent text is streamed", async () => {
|
|
68
|
+
const agent = new MockStepwiseAgent();
|
|
69
|
+
|
|
70
|
+
// Track render counts for the tool renderer
|
|
71
|
+
let toolRenderCount = 0;
|
|
72
|
+
let lastRenderStatus: string | null = null;
|
|
73
|
+
let lastRenderArgs: Record<string, unknown> | null = null;
|
|
74
|
+
|
|
75
|
+
const renderToolCalls = [
|
|
76
|
+
defineToolCallRenderer({
|
|
77
|
+
name: "getWeather",
|
|
78
|
+
args: z.object({
|
|
79
|
+
location: z.string(),
|
|
80
|
+
}),
|
|
81
|
+
render: ({ status, args, result }) => {
|
|
82
|
+
toolRenderCount++;
|
|
83
|
+
lastRenderStatus = status;
|
|
84
|
+
lastRenderArgs = args as Record<string, unknown>;
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<div data-testid="weather-tool">
|
|
88
|
+
<span data-testid="render-count">{toolRenderCount}</span>
|
|
89
|
+
<span data-testid="status">{status}</span>
|
|
90
|
+
<span data-testid="location">{args.location}</span>
|
|
91
|
+
<span data-testid="result">
|
|
92
|
+
{result ? String(result) : "pending"}
|
|
93
|
+
</span>
|
|
94
|
+
</div>
|
|
95
|
+
);
|
|
96
|
+
},
|
|
97
|
+
}),
|
|
98
|
+
] as unknown as ReactToolCallRenderer<unknown>[];
|
|
99
|
+
|
|
100
|
+
render(
|
|
101
|
+
<CopilotKitProvider
|
|
102
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
103
|
+
renderToolCalls={renderToolCalls}
|
|
104
|
+
>
|
|
105
|
+
<div style={{ height: 400 }}>
|
|
106
|
+
<CopilotChat />
|
|
107
|
+
</div>
|
|
108
|
+
</CopilotKitProvider>,
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
// Submit a user message to trigger runAgent
|
|
112
|
+
const input = await screen.findByRole("textbox");
|
|
113
|
+
fireEvent.change(input, { target: { value: "What's the weather?" } });
|
|
114
|
+
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
|
|
115
|
+
|
|
116
|
+
await waitFor(() => {
|
|
117
|
+
expect(screen.getByText("What's the weather?")).toBeDefined();
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const messageId = "m_rerender_test";
|
|
121
|
+
const toolCallId = "tc_rerender_test";
|
|
122
|
+
|
|
123
|
+
// Start the run
|
|
124
|
+
agent.emit({ type: EventType.RUN_STARTED } as BaseEvent);
|
|
125
|
+
|
|
126
|
+
// Stream the tool call with complete args
|
|
127
|
+
agent.emit({
|
|
128
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
129
|
+
toolCallId,
|
|
130
|
+
toolCallName: "getWeather",
|
|
131
|
+
parentMessageId: messageId,
|
|
132
|
+
delta: '{"location":"Paris"}',
|
|
133
|
+
} as BaseEvent);
|
|
134
|
+
|
|
135
|
+
// Wait for tool to render with InProgress status
|
|
136
|
+
await waitFor(() => {
|
|
137
|
+
const statusEl = screen.getByTestId("status");
|
|
138
|
+
expect(statusEl.textContent).toBe("inProgress");
|
|
139
|
+
expect(screen.getByTestId("location").textContent).toBe("Paris");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const renderCountAfterToolCall = toolRenderCount;
|
|
143
|
+
|
|
144
|
+
// Send the tool result to complete the tool call
|
|
145
|
+
agent.emit({
|
|
146
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
147
|
+
toolCallId,
|
|
148
|
+
messageId: `${messageId}_result`,
|
|
149
|
+
content: JSON.stringify({ temperature: 22, condition: "sunny" }),
|
|
150
|
+
} as BaseEvent);
|
|
151
|
+
|
|
152
|
+
// Wait for tool to show Complete status
|
|
153
|
+
await waitFor(() => {
|
|
154
|
+
const statusEl = screen.getByTestId("status");
|
|
155
|
+
expect(statusEl.textContent).toBe("complete");
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const renderCountAfterComplete = toolRenderCount;
|
|
159
|
+
|
|
160
|
+
// Sanity check: it should have re-rendered at least once to show complete status
|
|
161
|
+
expect(renderCountAfterComplete).toBeGreaterThan(renderCountAfterToolCall);
|
|
162
|
+
|
|
163
|
+
// Now stream additional text AFTER the tool call is complete
|
|
164
|
+
// This should NOT cause the tool call renderer to re-render
|
|
165
|
+
agent.emit({
|
|
166
|
+
type: EventType.TEXT_MESSAGE_CHUNK,
|
|
167
|
+
messageId: "m_followup",
|
|
168
|
+
delta: "The weather in Paris is ",
|
|
169
|
+
} as BaseEvent);
|
|
170
|
+
|
|
171
|
+
// Wait a moment for React to process
|
|
172
|
+
await waitFor(() => {
|
|
173
|
+
expect(screen.getByText(/The weather in Paris is/)).toBeDefined();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
const renderCountAfterFirstTextChunk = toolRenderCount;
|
|
177
|
+
|
|
178
|
+
// Stream more text chunks
|
|
179
|
+
agent.emit({
|
|
180
|
+
type: EventType.TEXT_MESSAGE_CHUNK,
|
|
181
|
+
messageId: "m_followup",
|
|
182
|
+
delta: "currently sunny ",
|
|
183
|
+
} as BaseEvent);
|
|
184
|
+
|
|
185
|
+
await waitFor(() => {
|
|
186
|
+
expect(screen.getByText(/currently sunny/)).toBeDefined();
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
agent.emit({
|
|
190
|
+
type: EventType.TEXT_MESSAGE_CHUNK,
|
|
191
|
+
messageId: "m_followup",
|
|
192
|
+
delta: "with a temperature of 22°C.",
|
|
193
|
+
} as BaseEvent);
|
|
194
|
+
|
|
195
|
+
await waitFor(() => {
|
|
196
|
+
expect(screen.getByText(/22°C/)).toBeDefined();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const renderCountAfterAllText = toolRenderCount;
|
|
200
|
+
|
|
201
|
+
// THE KEY ASSERTION: The tool should NOT have re-rendered after it was complete
|
|
202
|
+
// and we started streaming text
|
|
203
|
+
expect(renderCountAfterAllText).toBe(renderCountAfterComplete);
|
|
204
|
+
|
|
205
|
+
// Verify the tool still shows the correct completed state
|
|
206
|
+
expect(screen.getByTestId("status").textContent).toBe("complete");
|
|
207
|
+
expect(screen.getByTestId("location").textContent).toBe("Paris");
|
|
208
|
+
expect(screen.getByTestId("result").textContent).toContain("temperature");
|
|
209
|
+
|
|
210
|
+
agent.emit({ type: EventType.RUN_FINISHED } as BaseEvent);
|
|
211
|
+
agent.complete();
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("should not re-render a tool call when its arguments have not changed during streaming", async () => {
|
|
215
|
+
const agent = new MockStepwiseAgent();
|
|
216
|
+
|
|
217
|
+
// Track render counts
|
|
218
|
+
let toolRenderCount = 0;
|
|
219
|
+
|
|
220
|
+
const renderToolCalls = [
|
|
221
|
+
defineToolCallRenderer({
|
|
222
|
+
name: "search",
|
|
223
|
+
args: z.object({
|
|
224
|
+
query: z.string(),
|
|
225
|
+
}),
|
|
226
|
+
render: ({ status, args }) => {
|
|
227
|
+
toolRenderCount++;
|
|
228
|
+
|
|
229
|
+
return (
|
|
230
|
+
<div data-testid="search-tool">
|
|
231
|
+
<span data-testid="search-render-count">{toolRenderCount}</span>
|
|
232
|
+
<span data-testid="search-status">{status}</span>
|
|
233
|
+
<span data-testid="search-query">{args.query}</span>
|
|
234
|
+
</div>
|
|
235
|
+
);
|
|
236
|
+
},
|
|
237
|
+
}),
|
|
238
|
+
] as unknown as ReactToolCallRenderer<unknown>[];
|
|
239
|
+
|
|
240
|
+
render(
|
|
241
|
+
<CopilotKitProvider
|
|
242
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
243
|
+
renderToolCalls={renderToolCalls}
|
|
244
|
+
>
|
|
245
|
+
<div style={{ height: 400 }}>
|
|
246
|
+
<CopilotChat />
|
|
247
|
+
</div>
|
|
248
|
+
</CopilotKitProvider>,
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
const input = await screen.findByRole("textbox");
|
|
252
|
+
fireEvent.change(input, { target: { value: "Search for something" } });
|
|
253
|
+
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
|
|
254
|
+
|
|
255
|
+
await waitFor(() => {
|
|
256
|
+
expect(screen.getByText("Search for something")).toBeDefined();
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
const messageId = "m_search";
|
|
260
|
+
const toolCallId = "tc_search";
|
|
261
|
+
|
|
262
|
+
agent.emit({ type: EventType.RUN_STARTED } as BaseEvent);
|
|
263
|
+
|
|
264
|
+
// Stream complete tool call args
|
|
265
|
+
agent.emit({
|
|
266
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
267
|
+
toolCallId,
|
|
268
|
+
toolCallName: "search",
|
|
269
|
+
parentMessageId: messageId,
|
|
270
|
+
delta: '{"query":"React hooks"}',
|
|
271
|
+
} as BaseEvent);
|
|
272
|
+
|
|
273
|
+
await waitFor(() => {
|
|
274
|
+
expect(screen.getByTestId("search-query").textContent).toBe(
|
|
275
|
+
"React hooks",
|
|
276
|
+
);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const renderCountAfterToolCall = toolRenderCount;
|
|
280
|
+
|
|
281
|
+
// Stream text in the same message (before tool result)
|
|
282
|
+
// This simulates the agent adding explanation text while tool is in progress
|
|
283
|
+
agent.emit({
|
|
284
|
+
type: EventType.TEXT_MESSAGE_CHUNK,
|
|
285
|
+
messageId,
|
|
286
|
+
delta: "Let me search for that...",
|
|
287
|
+
} as BaseEvent);
|
|
288
|
+
|
|
289
|
+
await waitFor(() => {
|
|
290
|
+
expect(screen.getByText(/Let me search for that/)).toBeDefined();
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
const renderCountAfterText = toolRenderCount;
|
|
294
|
+
|
|
295
|
+
// The tool call should NOT re-render just because text was added to the message
|
|
296
|
+
// since its arguments haven't changed
|
|
297
|
+
expect(renderCountAfterText).toBe(renderCountAfterToolCall);
|
|
298
|
+
|
|
299
|
+
agent.emit({ type: EventType.RUN_FINISHED } as BaseEvent);
|
|
300
|
+
agent.complete();
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("should re-render a tool call when its arguments change during streaming", async () => {
|
|
304
|
+
const agent = new MockStepwiseAgent();
|
|
305
|
+
|
|
306
|
+
// Track render counts and captured args
|
|
307
|
+
let toolRenderCount = 0;
|
|
308
|
+
const capturedArgs: string[] = [];
|
|
309
|
+
|
|
310
|
+
const renderToolCalls = [
|
|
311
|
+
defineToolCallRenderer({
|
|
312
|
+
name: "search",
|
|
313
|
+
args: z.object({
|
|
314
|
+
query: z.string(),
|
|
315
|
+
}),
|
|
316
|
+
render: ({ args }) => {
|
|
317
|
+
toolRenderCount++;
|
|
318
|
+
capturedArgs.push(args.query!);
|
|
319
|
+
|
|
320
|
+
return (
|
|
321
|
+
<div data-testid="search-tool">
|
|
322
|
+
<span data-testid="search-render-count">{toolRenderCount}</span>
|
|
323
|
+
<span data-testid="search-query">{args.query}</span>
|
|
324
|
+
</div>
|
|
325
|
+
);
|
|
326
|
+
},
|
|
327
|
+
}),
|
|
328
|
+
] as unknown as ReactToolCallRenderer<unknown>[];
|
|
329
|
+
|
|
330
|
+
render(
|
|
331
|
+
<CopilotKitProvider
|
|
332
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
333
|
+
renderToolCalls={renderToolCalls}
|
|
334
|
+
>
|
|
335
|
+
<div style={{ height: 400 }}>
|
|
336
|
+
<CopilotChat />
|
|
337
|
+
</div>
|
|
338
|
+
</CopilotKitProvider>,
|
|
339
|
+
);
|
|
340
|
+
|
|
341
|
+
const input = await screen.findByRole("textbox");
|
|
342
|
+
fireEvent.change(input, { target: { value: "Search for something" } });
|
|
343
|
+
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
|
|
344
|
+
|
|
345
|
+
await waitFor(() => {
|
|
346
|
+
expect(screen.getByText("Search for something")).toBeDefined();
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
const messageId = "m_search_update";
|
|
350
|
+
const toolCallId = "tc_search_update";
|
|
351
|
+
|
|
352
|
+
agent.emit({ type: EventType.RUN_STARTED } as BaseEvent);
|
|
353
|
+
|
|
354
|
+
// Stream partial args first
|
|
355
|
+
agent.emit({
|
|
356
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
357
|
+
toolCallId,
|
|
358
|
+
toolCallName: "search",
|
|
359
|
+
parentMessageId: messageId,
|
|
360
|
+
delta: '{"query":"Rea',
|
|
361
|
+
} as BaseEvent);
|
|
362
|
+
|
|
363
|
+
await waitFor(() => {
|
|
364
|
+
expect(screen.getByTestId("search-query").textContent).toBe("Rea");
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
const renderCountAfterFirstChunk = toolRenderCount;
|
|
368
|
+
|
|
369
|
+
// Stream more args
|
|
370
|
+
agent.emit({
|
|
371
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
372
|
+
toolCallId,
|
|
373
|
+
toolCallName: "search",
|
|
374
|
+
parentMessageId: messageId,
|
|
375
|
+
delta: 'ct hooks"}',
|
|
376
|
+
} as BaseEvent);
|
|
377
|
+
|
|
378
|
+
await waitFor(() => {
|
|
379
|
+
expect(screen.getByTestId("search-query").textContent).toBe(
|
|
380
|
+
"React hooks",
|
|
381
|
+
);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
const renderCountAfterSecondChunk = toolRenderCount;
|
|
385
|
+
|
|
386
|
+
// THE KEY ASSERTION: Tool should re-render when arguments change
|
|
387
|
+
expect(renderCountAfterSecondChunk).toBeGreaterThan(
|
|
388
|
+
renderCountAfterFirstChunk,
|
|
389
|
+
);
|
|
390
|
+
expect(capturedArgs).toContain("Rea");
|
|
391
|
+
expect(capturedArgs).toContain("React hooks");
|
|
392
|
+
|
|
393
|
+
agent.emit({ type: EventType.RUN_FINISHED } as BaseEvent);
|
|
394
|
+
agent.complete();
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
it("should re-render a tool call when status changes to complete", async () => {
|
|
398
|
+
const agent = new MockStepwiseAgent();
|
|
399
|
+
|
|
400
|
+
let toolRenderCount = 0;
|
|
401
|
+
const capturedStatuses: string[] = [];
|
|
402
|
+
|
|
403
|
+
const renderToolCalls = [
|
|
404
|
+
defineToolCallRenderer({
|
|
405
|
+
name: "getData",
|
|
406
|
+
args: z.object({ id: z.string() }),
|
|
407
|
+
render: ({ status, result }) => {
|
|
408
|
+
toolRenderCount++;
|
|
409
|
+
capturedStatuses.push(status);
|
|
410
|
+
|
|
411
|
+
return (
|
|
412
|
+
<div data-testid="data-tool">
|
|
413
|
+
<span data-testid="data-status">{status}</span>
|
|
414
|
+
<span data-testid="data-result">
|
|
415
|
+
{result ? String(result) : "none"}
|
|
416
|
+
</span>
|
|
417
|
+
</div>
|
|
418
|
+
);
|
|
419
|
+
},
|
|
420
|
+
}),
|
|
421
|
+
] as unknown as ReactToolCallRenderer<unknown>[];
|
|
422
|
+
|
|
423
|
+
render(
|
|
424
|
+
<CopilotKitProvider
|
|
425
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
426
|
+
renderToolCalls={renderToolCalls}
|
|
427
|
+
>
|
|
428
|
+
<div style={{ height: 400 }}>
|
|
429
|
+
<CopilotChat />
|
|
430
|
+
</div>
|
|
431
|
+
</CopilotKitProvider>,
|
|
432
|
+
);
|
|
433
|
+
|
|
434
|
+
const input = await screen.findByRole("textbox");
|
|
435
|
+
fireEvent.change(input, { target: { value: "Get data" } });
|
|
436
|
+
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
|
|
437
|
+
|
|
438
|
+
await waitFor(() => {
|
|
439
|
+
expect(screen.getByText("Get data")).toBeDefined();
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
const messageId = "m_data";
|
|
443
|
+
const toolCallId = "tc_data";
|
|
444
|
+
|
|
445
|
+
agent.emit({ type: EventType.RUN_STARTED } as BaseEvent);
|
|
446
|
+
|
|
447
|
+
// Send tool call
|
|
448
|
+
agent.emit({
|
|
449
|
+
type: EventType.TOOL_CALL_CHUNK,
|
|
450
|
+
toolCallId,
|
|
451
|
+
toolCallName: "getData",
|
|
452
|
+
parentMessageId: messageId,
|
|
453
|
+
delta: '{"id":"123"}',
|
|
454
|
+
} as BaseEvent);
|
|
455
|
+
|
|
456
|
+
await waitFor(() => {
|
|
457
|
+
expect(screen.getByTestId("data-status").textContent).toBe("inProgress");
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
const renderCountBeforeResult = toolRenderCount;
|
|
461
|
+
|
|
462
|
+
// Send tool result
|
|
463
|
+
agent.emit({
|
|
464
|
+
type: EventType.TOOL_CALL_RESULT,
|
|
465
|
+
toolCallId,
|
|
466
|
+
messageId: `${messageId}_result`,
|
|
467
|
+
content: JSON.stringify({ data: "found" }),
|
|
468
|
+
} as BaseEvent);
|
|
469
|
+
|
|
470
|
+
await waitFor(() => {
|
|
471
|
+
expect(screen.getByTestId("data-status").textContent).toBe("complete");
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
const renderCountAfterResult = toolRenderCount;
|
|
475
|
+
|
|
476
|
+
// THE KEY ASSERTION: Tool should re-render when status changes
|
|
477
|
+
expect(renderCountAfterResult).toBeGreaterThan(renderCountBeforeResult);
|
|
478
|
+
expect(capturedStatuses).toContain("inProgress");
|
|
479
|
+
expect(capturedStatuses).toContain("complete");
|
|
480
|
+
|
|
481
|
+
agent.emit({ type: EventType.RUN_FINISHED } as BaseEvent);
|
|
482
|
+
agent.complete();
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
describe("Text Message Re-render Prevention", () => {
|
|
487
|
+
it("should not re-render a previous assistant message when a new message streams in", async () => {
|
|
488
|
+
// Track render counts per message ID
|
|
489
|
+
const renderCounts: Record<string, number> = {};
|
|
490
|
+
|
|
491
|
+
// Custom assistant message component that tracks renders
|
|
492
|
+
const TrackedAssistantMessage: React.FC<{
|
|
493
|
+
message: AssistantMessage;
|
|
494
|
+
messages?: Message[];
|
|
495
|
+
isRunning?: boolean;
|
|
496
|
+
}> = ({ message }) => {
|
|
497
|
+
// Increment render count for this message
|
|
498
|
+
renderCounts[message.id] = (renderCounts[message.id] || 0) + 1;
|
|
499
|
+
|
|
500
|
+
return (
|
|
501
|
+
<div data-testid={`assistant-message-${message.id}`}>
|
|
502
|
+
<span data-testid={`content-${message.id}`}>{message.content}</span>
|
|
503
|
+
<span data-testid={`render-count-${message.id}`}>
|
|
504
|
+
{renderCounts[message.id]}
|
|
505
|
+
</span>
|
|
506
|
+
</div>
|
|
507
|
+
);
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
// Initial messages - one complete assistant message
|
|
511
|
+
const initialMessages: Message[] = [
|
|
512
|
+
{
|
|
513
|
+
id: "msg-1",
|
|
514
|
+
role: "assistant",
|
|
515
|
+
content: "Hello! How can I help you today?",
|
|
516
|
+
} as AssistantMessage,
|
|
517
|
+
];
|
|
518
|
+
|
|
519
|
+
const { rerender } = render(
|
|
520
|
+
<CopilotKitProvider>
|
|
521
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
522
|
+
<CopilotChatMessageView
|
|
523
|
+
messages={initialMessages}
|
|
524
|
+
isRunning={false}
|
|
525
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
526
|
+
/>
|
|
527
|
+
</CopilotChatConfigurationProvider>
|
|
528
|
+
</CopilotKitProvider>,
|
|
529
|
+
);
|
|
530
|
+
|
|
531
|
+
// Verify first message rendered
|
|
532
|
+
await waitFor(() => {
|
|
533
|
+
expect(screen.getByTestId("assistant-message-msg-1")).toBeDefined();
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
const firstMessageRenderCountAfterInitial = renderCounts["msg-1"];
|
|
537
|
+
expect(firstMessageRenderCountAfterInitial).toBe(1);
|
|
538
|
+
|
|
539
|
+
// Simulate streaming a second message - first chunk
|
|
540
|
+
const messagesWithSecondPartial: Message[] = [
|
|
541
|
+
...initialMessages,
|
|
542
|
+
{
|
|
543
|
+
id: "msg-2",
|
|
544
|
+
role: "assistant",
|
|
545
|
+
content: "Let me help",
|
|
546
|
+
} as AssistantMessage,
|
|
547
|
+
];
|
|
548
|
+
|
|
549
|
+
rerender(
|
|
550
|
+
<CopilotKitProvider>
|
|
551
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
552
|
+
<CopilotChatMessageView
|
|
553
|
+
messages={messagesWithSecondPartial}
|
|
554
|
+
isRunning={true}
|
|
555
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
556
|
+
/>
|
|
557
|
+
</CopilotChatConfigurationProvider>
|
|
558
|
+
</CopilotKitProvider>,
|
|
559
|
+
);
|
|
560
|
+
|
|
561
|
+
await waitFor(() => {
|
|
562
|
+
expect(screen.getByTestId("assistant-message-msg-2")).toBeDefined();
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
const firstMessageRenderCountAfterSecondMessage = renderCounts["msg-1"];
|
|
566
|
+
|
|
567
|
+
// Continue streaming the second message
|
|
568
|
+
const messagesWithMoreContent: Message[] = [
|
|
569
|
+
...initialMessages,
|
|
570
|
+
{
|
|
571
|
+
id: "msg-2",
|
|
572
|
+
role: "assistant",
|
|
573
|
+
content: "Let me help you with that task.",
|
|
574
|
+
} as AssistantMessage,
|
|
575
|
+
];
|
|
576
|
+
|
|
577
|
+
rerender(
|
|
578
|
+
<CopilotKitProvider>
|
|
579
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
580
|
+
<CopilotChatMessageView
|
|
581
|
+
messages={messagesWithMoreContent}
|
|
582
|
+
isRunning={true}
|
|
583
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
584
|
+
/>
|
|
585
|
+
</CopilotChatConfigurationProvider>
|
|
586
|
+
</CopilotKitProvider>,
|
|
587
|
+
);
|
|
588
|
+
|
|
589
|
+
await waitFor(() => {
|
|
590
|
+
expect(screen.getByTestId("content-msg-2").textContent).toBe(
|
|
591
|
+
"Let me help you with that task.",
|
|
592
|
+
);
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
// Stream even more content
|
|
596
|
+
const messagesWithEvenMoreContent: Message[] = [
|
|
597
|
+
...initialMessages,
|
|
598
|
+
{
|
|
599
|
+
id: "msg-2",
|
|
600
|
+
role: "assistant",
|
|
601
|
+
content: "Let me help you with that task. Here's what I found:",
|
|
602
|
+
} as AssistantMessage,
|
|
603
|
+
];
|
|
604
|
+
|
|
605
|
+
rerender(
|
|
606
|
+
<CopilotKitProvider>
|
|
607
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
608
|
+
<CopilotChatMessageView
|
|
609
|
+
messages={messagesWithEvenMoreContent}
|
|
610
|
+
isRunning={true}
|
|
611
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
612
|
+
/>
|
|
613
|
+
</CopilotChatConfigurationProvider>
|
|
614
|
+
</CopilotKitProvider>,
|
|
615
|
+
);
|
|
616
|
+
|
|
617
|
+
await waitFor(() => {
|
|
618
|
+
expect(screen.getByTestId("content-msg-2").textContent).toContain(
|
|
619
|
+
"Here's what I found",
|
|
620
|
+
);
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
const firstMessageRenderCountAfterAllStreaming = renderCounts["msg-1"];
|
|
624
|
+
|
|
625
|
+
// THE KEY ASSERTION: The first message should NOT have re-rendered
|
|
626
|
+
// when the second message was streaming
|
|
627
|
+
expect(firstMessageRenderCountAfterAllStreaming).toBe(
|
|
628
|
+
firstMessageRenderCountAfterInitial,
|
|
629
|
+
);
|
|
630
|
+
|
|
631
|
+
// Verify the second message did update (it should have rendered multiple times)
|
|
632
|
+
expect(renderCounts["msg-2"]).toBeGreaterThan(1);
|
|
633
|
+
});
|
|
634
|
+
|
|
635
|
+
it("should not re-render a user message when assistant message streams", async () => {
|
|
636
|
+
const renderCounts: Record<string, number> = {};
|
|
637
|
+
|
|
638
|
+
const TrackedAssistantMessage: React.FC<{
|
|
639
|
+
message: AssistantMessage;
|
|
640
|
+
messages?: Message[];
|
|
641
|
+
isRunning?: boolean;
|
|
642
|
+
}> = ({ message }) => {
|
|
643
|
+
renderCounts[message.id] = (renderCounts[message.id] || 0) + 1;
|
|
644
|
+
return (
|
|
645
|
+
<div data-testid={`assistant-message-${message.id}`}>
|
|
646
|
+
<span data-testid={`content-${message.id}`}>{message.content}</span>
|
|
647
|
+
</div>
|
|
648
|
+
);
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
const TrackedUserMessage: React.FC<{
|
|
652
|
+
message: Message;
|
|
653
|
+
}> = ({ message }) => {
|
|
654
|
+
renderCounts[message.id] = (renderCounts[message.id] || 0) + 1;
|
|
655
|
+
return (
|
|
656
|
+
<div data-testid={`user-message-${message.id}`}>
|
|
657
|
+
<span data-testid={`user-content-${message.id}`}>
|
|
658
|
+
{typeof message.content === "string" ? message.content : ""}
|
|
659
|
+
</span>
|
|
660
|
+
<span data-testid={`user-render-count-${message.id}`}>
|
|
661
|
+
{renderCounts[message.id]}
|
|
662
|
+
</span>
|
|
663
|
+
</div>
|
|
664
|
+
);
|
|
665
|
+
};
|
|
666
|
+
|
|
667
|
+
const initialMessages: Message[] = [
|
|
668
|
+
{
|
|
669
|
+
id: "user-1",
|
|
670
|
+
role: "user",
|
|
671
|
+
content: "Hello!",
|
|
672
|
+
},
|
|
673
|
+
];
|
|
674
|
+
|
|
675
|
+
const { rerender } = render(
|
|
676
|
+
<CopilotKitProvider>
|
|
677
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
678
|
+
<CopilotChatMessageView
|
|
679
|
+
messages={initialMessages}
|
|
680
|
+
isRunning={false}
|
|
681
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
682
|
+
userMessage={TrackedUserMessage as any}
|
|
683
|
+
/>
|
|
684
|
+
</CopilotChatConfigurationProvider>
|
|
685
|
+
</CopilotKitProvider>,
|
|
686
|
+
);
|
|
687
|
+
|
|
688
|
+
await waitFor(() => {
|
|
689
|
+
expect(screen.getByTestId("user-message-user-1")).toBeDefined();
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
const userMessageRenderCountInitial = renderCounts["user-1"];
|
|
693
|
+
expect(userMessageRenderCountInitial).toBe(1);
|
|
694
|
+
|
|
695
|
+
// Add assistant response and stream it
|
|
696
|
+
const messagesWithAssistant: Message[] = [
|
|
697
|
+
...initialMessages,
|
|
698
|
+
{
|
|
699
|
+
id: "assistant-1",
|
|
700
|
+
role: "assistant",
|
|
701
|
+
content: "Hi there!",
|
|
702
|
+
} as AssistantMessage,
|
|
703
|
+
];
|
|
704
|
+
|
|
705
|
+
rerender(
|
|
706
|
+
<CopilotKitProvider>
|
|
707
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
708
|
+
<CopilotChatMessageView
|
|
709
|
+
messages={messagesWithAssistant}
|
|
710
|
+
isRunning={true}
|
|
711
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
712
|
+
userMessage={TrackedUserMessage as any}
|
|
713
|
+
/>
|
|
714
|
+
</CopilotChatConfigurationProvider>
|
|
715
|
+
</CopilotKitProvider>,
|
|
716
|
+
);
|
|
717
|
+
|
|
718
|
+
// Stream more content
|
|
719
|
+
const messagesWithMoreAssistant: Message[] = [
|
|
720
|
+
...initialMessages,
|
|
721
|
+
{
|
|
722
|
+
id: "assistant-1",
|
|
723
|
+
role: "assistant",
|
|
724
|
+
content: "Hi there! How can I assist you today?",
|
|
725
|
+
} as AssistantMessage,
|
|
726
|
+
];
|
|
727
|
+
|
|
728
|
+
rerender(
|
|
729
|
+
<CopilotKitProvider>
|
|
730
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
731
|
+
<CopilotChatMessageView
|
|
732
|
+
messages={messagesWithMoreAssistant}
|
|
733
|
+
isRunning={true}
|
|
734
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
735
|
+
userMessage={TrackedUserMessage as any}
|
|
736
|
+
/>
|
|
737
|
+
</CopilotChatConfigurationProvider>
|
|
738
|
+
</CopilotKitProvider>,
|
|
739
|
+
);
|
|
740
|
+
|
|
741
|
+
await waitFor(() => {
|
|
742
|
+
expect(screen.getByTestId("content-assistant-1").textContent).toContain(
|
|
743
|
+
"How can I assist",
|
|
744
|
+
);
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
const userMessageRenderCountAfterStreaming = renderCounts["user-1"];
|
|
748
|
+
|
|
749
|
+
// THE KEY ASSERTION: User message should not re-render when assistant streams
|
|
750
|
+
expect(userMessageRenderCountAfterStreaming).toBe(
|
|
751
|
+
userMessageRenderCountInitial,
|
|
752
|
+
);
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
it("should re-render an assistant message when its content changes", async () => {
|
|
756
|
+
const renderCounts: Record<string, number> = {};
|
|
757
|
+
const capturedContent: string[] = [];
|
|
758
|
+
|
|
759
|
+
const TrackedAssistantMessage: React.FC<{
|
|
760
|
+
message: AssistantMessage;
|
|
761
|
+
messages?: Message[];
|
|
762
|
+
isRunning?: boolean;
|
|
763
|
+
}> = ({ message }) => {
|
|
764
|
+
renderCounts[message.id] = (renderCounts[message.id] || 0) + 1;
|
|
765
|
+
capturedContent.push(message.content ?? "");
|
|
766
|
+
return (
|
|
767
|
+
<div data-testid={`assistant-message-${message.id}`}>
|
|
768
|
+
<span data-testid={`content-${message.id}`}>{message.content}</span>
|
|
769
|
+
</div>
|
|
770
|
+
);
|
|
771
|
+
};
|
|
772
|
+
|
|
773
|
+
const initialMessages: Message[] = [
|
|
774
|
+
{
|
|
775
|
+
id: "msg-1",
|
|
776
|
+
role: "assistant",
|
|
777
|
+
content: "Hello",
|
|
778
|
+
} as AssistantMessage,
|
|
779
|
+
];
|
|
780
|
+
|
|
781
|
+
const { rerender } = render(
|
|
782
|
+
<CopilotKitProvider>
|
|
783
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
784
|
+
<CopilotChatMessageView
|
|
785
|
+
messages={initialMessages}
|
|
786
|
+
isRunning={true}
|
|
787
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
788
|
+
/>
|
|
789
|
+
</CopilotChatConfigurationProvider>
|
|
790
|
+
</CopilotKitProvider>,
|
|
791
|
+
);
|
|
792
|
+
|
|
793
|
+
await waitFor(() => {
|
|
794
|
+
expect(screen.getByTestId("assistant-message-msg-1")).toBeDefined();
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
const renderCountAfterInitial = renderCounts["msg-1"]!;
|
|
798
|
+
expect(renderCountAfterInitial).toBe(1);
|
|
799
|
+
|
|
800
|
+
// Update message content (streaming)
|
|
801
|
+
const updatedMessages: Message[] = [
|
|
802
|
+
{
|
|
803
|
+
id: "msg-1",
|
|
804
|
+
role: "assistant",
|
|
805
|
+
content: "Hello! How can I help",
|
|
806
|
+
} as AssistantMessage,
|
|
807
|
+
];
|
|
808
|
+
|
|
809
|
+
rerender(
|
|
810
|
+
<CopilotKitProvider>
|
|
811
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
812
|
+
<CopilotChatMessageView
|
|
813
|
+
messages={updatedMessages}
|
|
814
|
+
isRunning={true}
|
|
815
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
816
|
+
/>
|
|
817
|
+
</CopilotChatConfigurationProvider>
|
|
818
|
+
</CopilotKitProvider>,
|
|
819
|
+
);
|
|
820
|
+
|
|
821
|
+
await waitFor(() => {
|
|
822
|
+
expect(screen.getByTestId("content-msg-1").textContent).toBe(
|
|
823
|
+
"Hello! How can I help",
|
|
824
|
+
);
|
|
825
|
+
});
|
|
826
|
+
|
|
827
|
+
const renderCountAfterUpdate = renderCounts["msg-1"]!;
|
|
828
|
+
|
|
829
|
+
// THE KEY ASSERTION: Message should re-render when content changes
|
|
830
|
+
expect(renderCountAfterUpdate).toBeGreaterThan(renderCountAfterInitial);
|
|
831
|
+
expect(capturedContent).toContain("Hello");
|
|
832
|
+
expect(capturedContent).toContain("Hello! How can I help");
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
it("should re-render a user message when its content changes", async () => {
|
|
836
|
+
const renderCounts: Record<string, number> = {};
|
|
837
|
+
const capturedContent: string[] = [];
|
|
838
|
+
|
|
839
|
+
const TrackedUserMessage: React.FC<{
|
|
840
|
+
message: Message;
|
|
841
|
+
}> = ({ message }) => {
|
|
842
|
+
renderCounts[message.id] = (renderCounts[message.id] || 0) + 1;
|
|
843
|
+
const content =
|
|
844
|
+
typeof message.content === "string" ? message.content : "";
|
|
845
|
+
capturedContent.push(content);
|
|
846
|
+
return (
|
|
847
|
+
<div data-testid={`user-message-${message.id}`}>
|
|
848
|
+
<span data-testid={`user-content-${message.id}`}>{content}</span>
|
|
849
|
+
</div>
|
|
850
|
+
);
|
|
851
|
+
};
|
|
852
|
+
|
|
853
|
+
const initialMessages: Message[] = [
|
|
854
|
+
{
|
|
855
|
+
id: "user-1",
|
|
856
|
+
role: "user",
|
|
857
|
+
content: "Initial message",
|
|
858
|
+
},
|
|
859
|
+
];
|
|
860
|
+
|
|
861
|
+
const { rerender } = render(
|
|
862
|
+
<CopilotKitProvider>
|
|
863
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
864
|
+
<CopilotChatMessageView
|
|
865
|
+
messages={initialMessages}
|
|
866
|
+
isRunning={false}
|
|
867
|
+
userMessage={TrackedUserMessage as any}
|
|
868
|
+
/>
|
|
869
|
+
</CopilotChatConfigurationProvider>
|
|
870
|
+
</CopilotKitProvider>,
|
|
871
|
+
);
|
|
872
|
+
|
|
873
|
+
await waitFor(() => {
|
|
874
|
+
expect(screen.getByTestId("user-message-user-1")).toBeDefined();
|
|
875
|
+
});
|
|
876
|
+
|
|
877
|
+
const renderCountAfterInitial = renderCounts["user-1"]!;
|
|
878
|
+
expect(renderCountAfterInitial).toBe(1);
|
|
879
|
+
|
|
880
|
+
// Update user message content (e.g., editing)
|
|
881
|
+
const updatedMessages: Message[] = [
|
|
882
|
+
{
|
|
883
|
+
id: "user-1",
|
|
884
|
+
role: "user",
|
|
885
|
+
content: "Updated message",
|
|
886
|
+
},
|
|
887
|
+
];
|
|
888
|
+
|
|
889
|
+
rerender(
|
|
890
|
+
<CopilotKitProvider>
|
|
891
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
892
|
+
<CopilotChatMessageView
|
|
893
|
+
messages={updatedMessages}
|
|
894
|
+
isRunning={false}
|
|
895
|
+
userMessage={TrackedUserMessage as any}
|
|
896
|
+
/>
|
|
897
|
+
</CopilotChatConfigurationProvider>
|
|
898
|
+
</CopilotKitProvider>,
|
|
899
|
+
);
|
|
900
|
+
|
|
901
|
+
await waitFor(() => {
|
|
902
|
+
expect(screen.getByTestId("user-content-user-1").textContent).toBe(
|
|
903
|
+
"Updated message",
|
|
904
|
+
);
|
|
905
|
+
});
|
|
906
|
+
|
|
907
|
+
const renderCountAfterUpdate = renderCounts["user-1"]!;
|
|
908
|
+
|
|
909
|
+
// THE KEY ASSERTION: User message should re-render when content changes
|
|
910
|
+
expect(renderCountAfterUpdate).toBeGreaterThan(renderCountAfterInitial);
|
|
911
|
+
expect(capturedContent).toContain("Initial message");
|
|
912
|
+
expect(capturedContent).toContain("Updated message");
|
|
913
|
+
});
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
describe("Activity Message Re-render Prevention", () => {
|
|
917
|
+
it("should not re-render a previous activity message when a new message streams in", async () => {
|
|
918
|
+
// Track render counts per message ID
|
|
919
|
+
const renderCounts: Record<string, number> = {};
|
|
920
|
+
|
|
921
|
+
// Custom activity renderer that tracks renders
|
|
922
|
+
const activityRenderer: ReactActivityMessageRenderer<{
|
|
923
|
+
status: string;
|
|
924
|
+
percent: number;
|
|
925
|
+
}> = {
|
|
926
|
+
activityType: "search-progress",
|
|
927
|
+
content: z.object({ status: z.string(), percent: z.number() }),
|
|
928
|
+
render: ({ content, message }) => {
|
|
929
|
+
renderCounts[message.id] = (renderCounts[message.id] || 0) + 1;
|
|
930
|
+
return (
|
|
931
|
+
<div data-testid={`activity-${message.id}`}>
|
|
932
|
+
<span data-testid={`activity-content-${message.id}`}>
|
|
933
|
+
{content.status} - {content.percent}%
|
|
934
|
+
</span>
|
|
935
|
+
<span data-testid={`activity-render-count-${message.id}`}>
|
|
936
|
+
{renderCounts[message.id]}
|
|
937
|
+
</span>
|
|
938
|
+
</div>
|
|
939
|
+
);
|
|
940
|
+
},
|
|
941
|
+
};
|
|
942
|
+
|
|
943
|
+
// Initial messages - one activity message
|
|
944
|
+
const initialMessages: Message[] = [
|
|
945
|
+
{
|
|
946
|
+
id: "activity-1",
|
|
947
|
+
role: "activity",
|
|
948
|
+
activityType: "search-progress",
|
|
949
|
+
content: { status: "Searching", percent: 50 },
|
|
950
|
+
} as ActivityMessage,
|
|
951
|
+
];
|
|
952
|
+
|
|
953
|
+
const { rerender } = render(
|
|
954
|
+
<CopilotKitProvider renderActivityMessages={[activityRenderer]}>
|
|
955
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
956
|
+
<CopilotChatMessageView messages={initialMessages} isRunning={true} />
|
|
957
|
+
</CopilotChatConfigurationProvider>
|
|
958
|
+
</CopilotKitProvider>,
|
|
959
|
+
);
|
|
960
|
+
|
|
961
|
+
// Verify first activity rendered
|
|
962
|
+
await waitFor(() => {
|
|
963
|
+
expect(screen.getByTestId("activity-activity-1")).toBeDefined();
|
|
964
|
+
});
|
|
965
|
+
|
|
966
|
+
const firstActivityRenderCountAfterInitial = renderCounts["activity-1"];
|
|
967
|
+
expect(firstActivityRenderCountAfterInitial).toBe(1);
|
|
968
|
+
|
|
969
|
+
// Add a second activity message
|
|
970
|
+
const messagesWithSecondActivity: Message[] = [
|
|
971
|
+
...initialMessages,
|
|
972
|
+
{
|
|
973
|
+
id: "activity-2",
|
|
974
|
+
role: "activity",
|
|
975
|
+
activityType: "search-progress",
|
|
976
|
+
content: { status: "Processing", percent: 75 },
|
|
977
|
+
} as ActivityMessage,
|
|
978
|
+
];
|
|
979
|
+
|
|
980
|
+
rerender(
|
|
981
|
+
<CopilotKitProvider renderActivityMessages={[activityRenderer]}>
|
|
982
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
983
|
+
<CopilotChatMessageView
|
|
984
|
+
messages={messagesWithSecondActivity}
|
|
985
|
+
isRunning={true}
|
|
986
|
+
/>
|
|
987
|
+
</CopilotChatConfigurationProvider>
|
|
988
|
+
</CopilotKitProvider>,
|
|
989
|
+
);
|
|
990
|
+
|
|
991
|
+
await waitFor(() => {
|
|
992
|
+
expect(screen.getByTestId("activity-activity-2")).toBeDefined();
|
|
993
|
+
});
|
|
994
|
+
|
|
995
|
+
// Update the second activity message
|
|
996
|
+
const messagesWithUpdatedSecondActivity: Message[] = [
|
|
997
|
+
initialMessages[0]!,
|
|
998
|
+
{
|
|
999
|
+
id: "activity-2",
|
|
1000
|
+
role: "activity",
|
|
1001
|
+
activityType: "search-progress",
|
|
1002
|
+
content: { status: "Almost done", percent: 90 },
|
|
1003
|
+
} as ActivityMessage,
|
|
1004
|
+
];
|
|
1005
|
+
|
|
1006
|
+
rerender(
|
|
1007
|
+
<CopilotKitProvider renderActivityMessages={[activityRenderer]}>
|
|
1008
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1009
|
+
<CopilotChatMessageView
|
|
1010
|
+
messages={messagesWithUpdatedSecondActivity}
|
|
1011
|
+
isRunning={true}
|
|
1012
|
+
/>
|
|
1013
|
+
</CopilotChatConfigurationProvider>
|
|
1014
|
+
</CopilotKitProvider>,
|
|
1015
|
+
);
|
|
1016
|
+
|
|
1017
|
+
await waitFor(() => {
|
|
1018
|
+
expect(
|
|
1019
|
+
screen.getByTestId("activity-content-activity-2").textContent,
|
|
1020
|
+
).toContain("Almost done");
|
|
1021
|
+
});
|
|
1022
|
+
|
|
1023
|
+
const firstActivityRenderCountAfterAllUpdates = renderCounts["activity-1"];
|
|
1024
|
+
|
|
1025
|
+
// THE KEY ASSERTION: The first activity should NOT have re-rendered
|
|
1026
|
+
// when the second activity was added or updated
|
|
1027
|
+
expect(firstActivityRenderCountAfterAllUpdates).toBe(
|
|
1028
|
+
firstActivityRenderCountAfterInitial,
|
|
1029
|
+
);
|
|
1030
|
+
|
|
1031
|
+
// Verify the second activity did update (it should have rendered multiple times)
|
|
1032
|
+
expect(renderCounts["activity-2"]).toBeGreaterThan(1);
|
|
1033
|
+
});
|
|
1034
|
+
|
|
1035
|
+
it("should not re-render an activity message when an assistant message streams", async () => {
|
|
1036
|
+
const renderCounts: Record<string, number> = {};
|
|
1037
|
+
|
|
1038
|
+
const activityRenderer: ReactActivityMessageRenderer<{ status: string }> = {
|
|
1039
|
+
activityType: "progress",
|
|
1040
|
+
content: z.object({ status: z.string() }),
|
|
1041
|
+
render: ({ content, message }) => {
|
|
1042
|
+
renderCounts[message.id] = (renderCounts[message.id] || 0) + 1;
|
|
1043
|
+
return (
|
|
1044
|
+
<div data-testid={`activity-${message.id}`}>{content.status}</div>
|
|
1045
|
+
);
|
|
1046
|
+
},
|
|
1047
|
+
};
|
|
1048
|
+
|
|
1049
|
+
const TrackedAssistantMessage: React.FC<{
|
|
1050
|
+
message: AssistantMessage;
|
|
1051
|
+
messages?: Message[];
|
|
1052
|
+
isRunning?: boolean;
|
|
1053
|
+
}> = ({ message }) => {
|
|
1054
|
+
renderCounts[message.id] = (renderCounts[message.id] || 0) + 1;
|
|
1055
|
+
return (
|
|
1056
|
+
<div data-testid={`assistant-${message.id}`}>{message.content}</div>
|
|
1057
|
+
);
|
|
1058
|
+
};
|
|
1059
|
+
|
|
1060
|
+
const initialMessages: Message[] = [
|
|
1061
|
+
{
|
|
1062
|
+
id: "activity-1",
|
|
1063
|
+
role: "activity",
|
|
1064
|
+
activityType: "progress",
|
|
1065
|
+
content: { status: "Loading..." },
|
|
1066
|
+
} as ActivityMessage,
|
|
1067
|
+
];
|
|
1068
|
+
|
|
1069
|
+
const { rerender } = render(
|
|
1070
|
+
<CopilotKitProvider renderActivityMessages={[activityRenderer]}>
|
|
1071
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1072
|
+
<CopilotChatMessageView
|
|
1073
|
+
messages={initialMessages}
|
|
1074
|
+
isRunning={true}
|
|
1075
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
1076
|
+
/>
|
|
1077
|
+
</CopilotChatConfigurationProvider>
|
|
1078
|
+
</CopilotKitProvider>,
|
|
1079
|
+
);
|
|
1080
|
+
|
|
1081
|
+
await waitFor(() => {
|
|
1082
|
+
expect(screen.getByTestId("activity-activity-1")).toBeDefined();
|
|
1083
|
+
});
|
|
1084
|
+
|
|
1085
|
+
const activityRenderCountInitial = renderCounts["activity-1"];
|
|
1086
|
+
expect(activityRenderCountInitial).toBe(1);
|
|
1087
|
+
|
|
1088
|
+
// Add an assistant message and stream it
|
|
1089
|
+
const messagesWithAssistant: Message[] = [
|
|
1090
|
+
...initialMessages,
|
|
1091
|
+
{
|
|
1092
|
+
id: "assistant-1",
|
|
1093
|
+
role: "assistant",
|
|
1094
|
+
content: "Here's what I found...",
|
|
1095
|
+
} as AssistantMessage,
|
|
1096
|
+
];
|
|
1097
|
+
|
|
1098
|
+
rerender(
|
|
1099
|
+
<CopilotKitProvider renderActivityMessages={[activityRenderer]}>
|
|
1100
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1101
|
+
<CopilotChatMessageView
|
|
1102
|
+
messages={messagesWithAssistant}
|
|
1103
|
+
isRunning={true}
|
|
1104
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
1105
|
+
/>
|
|
1106
|
+
</CopilotChatConfigurationProvider>
|
|
1107
|
+
</CopilotKitProvider>,
|
|
1108
|
+
);
|
|
1109
|
+
|
|
1110
|
+
// Stream more content
|
|
1111
|
+
const messagesWithMoreAssistant: Message[] = [
|
|
1112
|
+
initialMessages[0]!,
|
|
1113
|
+
{
|
|
1114
|
+
id: "assistant-1",
|
|
1115
|
+
role: "assistant",
|
|
1116
|
+
content: "Here's what I found... The results show that...",
|
|
1117
|
+
} as AssistantMessage,
|
|
1118
|
+
];
|
|
1119
|
+
|
|
1120
|
+
rerender(
|
|
1121
|
+
<CopilotKitProvider renderActivityMessages={[activityRenderer]}>
|
|
1122
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1123
|
+
<CopilotChatMessageView
|
|
1124
|
+
messages={messagesWithMoreAssistant}
|
|
1125
|
+
isRunning={true}
|
|
1126
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
1127
|
+
/>
|
|
1128
|
+
</CopilotChatConfigurationProvider>
|
|
1129
|
+
</CopilotKitProvider>,
|
|
1130
|
+
);
|
|
1131
|
+
|
|
1132
|
+
await waitFor(() => {
|
|
1133
|
+
expect(screen.getByTestId("assistant-assistant-1").textContent).toContain(
|
|
1134
|
+
"The results show",
|
|
1135
|
+
);
|
|
1136
|
+
});
|
|
1137
|
+
|
|
1138
|
+
const activityRenderCountAfterStreaming = renderCounts["activity-1"];
|
|
1139
|
+
|
|
1140
|
+
// THE KEY ASSERTION: Activity message should not re-render when assistant streams
|
|
1141
|
+
expect(activityRenderCountAfterStreaming).toBe(activityRenderCountInitial);
|
|
1142
|
+
});
|
|
1143
|
+
|
|
1144
|
+
it("should re-render an activity message when its content changes", async () => {
|
|
1145
|
+
const renderCounts: Record<string, number> = {};
|
|
1146
|
+
const capturedContent: { status: string; percent: number }[] = [];
|
|
1147
|
+
|
|
1148
|
+
const activityRenderer: ReactActivityMessageRenderer<{
|
|
1149
|
+
status: string;
|
|
1150
|
+
percent: number;
|
|
1151
|
+
}> = {
|
|
1152
|
+
activityType: "progress",
|
|
1153
|
+
content: z.object({ status: z.string(), percent: z.number() }),
|
|
1154
|
+
render: ({ content, message }) => {
|
|
1155
|
+
renderCounts[message.id] = (renderCounts[message.id] || 0) + 1;
|
|
1156
|
+
capturedContent.push({ ...content });
|
|
1157
|
+
return (
|
|
1158
|
+
<div data-testid={`activity-${message.id}`}>
|
|
1159
|
+
<span data-testid={`activity-status-${message.id}`}>
|
|
1160
|
+
{content.status}
|
|
1161
|
+
</span>
|
|
1162
|
+
<span data-testid={`activity-percent-${message.id}`}>
|
|
1163
|
+
{content.percent}
|
|
1164
|
+
</span>
|
|
1165
|
+
</div>
|
|
1166
|
+
);
|
|
1167
|
+
},
|
|
1168
|
+
};
|
|
1169
|
+
|
|
1170
|
+
const initialMessages: Message[] = [
|
|
1171
|
+
{
|
|
1172
|
+
id: "activity-1",
|
|
1173
|
+
role: "activity",
|
|
1174
|
+
activityType: "progress",
|
|
1175
|
+
content: { status: "Starting", percent: 0 },
|
|
1176
|
+
} as ActivityMessage,
|
|
1177
|
+
];
|
|
1178
|
+
|
|
1179
|
+
const { rerender } = render(
|
|
1180
|
+
<CopilotKitProvider renderActivityMessages={[activityRenderer]}>
|
|
1181
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1182
|
+
<CopilotChatMessageView messages={initialMessages} isRunning={true} />
|
|
1183
|
+
</CopilotChatConfigurationProvider>
|
|
1184
|
+
</CopilotKitProvider>,
|
|
1185
|
+
);
|
|
1186
|
+
|
|
1187
|
+
await waitFor(() => {
|
|
1188
|
+
expect(screen.getByTestId("activity-activity-1")).toBeDefined();
|
|
1189
|
+
});
|
|
1190
|
+
|
|
1191
|
+
const renderCountAfterInitial = renderCounts["activity-1"]!;
|
|
1192
|
+
expect(renderCountAfterInitial).toBe(1);
|
|
1193
|
+
|
|
1194
|
+
// Update activity content (progress update)
|
|
1195
|
+
const updatedMessages: Message[] = [
|
|
1196
|
+
{
|
|
1197
|
+
id: "activity-1",
|
|
1198
|
+
role: "activity",
|
|
1199
|
+
activityType: "progress",
|
|
1200
|
+
content: { status: "Processing", percent: 50 },
|
|
1201
|
+
} as ActivityMessage,
|
|
1202
|
+
];
|
|
1203
|
+
|
|
1204
|
+
rerender(
|
|
1205
|
+
<CopilotKitProvider renderActivityMessages={[activityRenderer]}>
|
|
1206
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1207
|
+
<CopilotChatMessageView messages={updatedMessages} isRunning={true} />
|
|
1208
|
+
</CopilotChatConfigurationProvider>
|
|
1209
|
+
</CopilotKitProvider>,
|
|
1210
|
+
);
|
|
1211
|
+
|
|
1212
|
+
await waitFor(() => {
|
|
1213
|
+
expect(screen.getByTestId("activity-status-activity-1").textContent).toBe(
|
|
1214
|
+
"Processing",
|
|
1215
|
+
);
|
|
1216
|
+
expect(
|
|
1217
|
+
screen.getByTestId("activity-percent-activity-1").textContent,
|
|
1218
|
+
).toBe("50");
|
|
1219
|
+
});
|
|
1220
|
+
|
|
1221
|
+
const renderCountAfterUpdate = renderCounts["activity-1"]!;
|
|
1222
|
+
|
|
1223
|
+
// THE KEY ASSERTION: Activity should re-render when content changes
|
|
1224
|
+
expect(renderCountAfterUpdate).toBeGreaterThan(renderCountAfterInitial);
|
|
1225
|
+
expect(capturedContent).toContainEqual({ status: "Starting", percent: 0 });
|
|
1226
|
+
expect(capturedContent).toContainEqual({
|
|
1227
|
+
status: "Processing",
|
|
1228
|
+
percent: 50,
|
|
1229
|
+
});
|
|
1230
|
+
});
|
|
1231
|
+
|
|
1232
|
+
it("should re-render an activity message when its activityType changes", async () => {
|
|
1233
|
+
const renderCounts: Record<string, number> = {};
|
|
1234
|
+
|
|
1235
|
+
const progressRenderer: ReactActivityMessageRenderer<{ status: string }> = {
|
|
1236
|
+
activityType: "progress",
|
|
1237
|
+
content: z.object({ status: z.string() }),
|
|
1238
|
+
render: ({ content, message }) => {
|
|
1239
|
+
renderCounts[message.id] = (renderCounts[message.id] || 0) + 1;
|
|
1240
|
+
return (
|
|
1241
|
+
<div data-testid={`activity-${message.id}`}>
|
|
1242
|
+
<span data-testid={`activity-type-${message.id}`}>progress</span>
|
|
1243
|
+
<span data-testid={`activity-status-${message.id}`}>
|
|
1244
|
+
{content.status}
|
|
1245
|
+
</span>
|
|
1246
|
+
</div>
|
|
1247
|
+
);
|
|
1248
|
+
},
|
|
1249
|
+
};
|
|
1250
|
+
|
|
1251
|
+
const completedRenderer: ReactActivityMessageRenderer<{ result: string }> =
|
|
1252
|
+
{
|
|
1253
|
+
activityType: "completed",
|
|
1254
|
+
content: z.object({ result: z.string() }),
|
|
1255
|
+
render: ({ content, message }) => {
|
|
1256
|
+
renderCounts[message.id] = (renderCounts[message.id] || 0) + 1;
|
|
1257
|
+
return (
|
|
1258
|
+
<div data-testid={`activity-${message.id}`}>
|
|
1259
|
+
<span data-testid={`activity-type-${message.id}`}>completed</span>
|
|
1260
|
+
<span data-testid={`activity-result-${message.id}`}>
|
|
1261
|
+
{content.result}
|
|
1262
|
+
</span>
|
|
1263
|
+
</div>
|
|
1264
|
+
);
|
|
1265
|
+
},
|
|
1266
|
+
};
|
|
1267
|
+
|
|
1268
|
+
const initialMessages: Message[] = [
|
|
1269
|
+
{
|
|
1270
|
+
id: "activity-1",
|
|
1271
|
+
role: "activity",
|
|
1272
|
+
activityType: "progress",
|
|
1273
|
+
content: { status: "Loading..." },
|
|
1274
|
+
} as ActivityMessage,
|
|
1275
|
+
];
|
|
1276
|
+
|
|
1277
|
+
const { rerender } = render(
|
|
1278
|
+
<CopilotKitProvider
|
|
1279
|
+
renderActivityMessages={[progressRenderer, completedRenderer]}
|
|
1280
|
+
>
|
|
1281
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1282
|
+
<CopilotChatMessageView messages={initialMessages} isRunning={true} />
|
|
1283
|
+
</CopilotChatConfigurationProvider>
|
|
1284
|
+
</CopilotKitProvider>,
|
|
1285
|
+
);
|
|
1286
|
+
|
|
1287
|
+
await waitFor(() => {
|
|
1288
|
+
expect(screen.getByTestId("activity-type-activity-1").textContent).toBe(
|
|
1289
|
+
"progress",
|
|
1290
|
+
);
|
|
1291
|
+
});
|
|
1292
|
+
|
|
1293
|
+
const renderCountAfterInitial = renderCounts["activity-1"]!;
|
|
1294
|
+
expect(renderCountAfterInitial).toBe(1);
|
|
1295
|
+
|
|
1296
|
+
// Change activity type
|
|
1297
|
+
const updatedMessages: Message[] = [
|
|
1298
|
+
{
|
|
1299
|
+
id: "activity-1",
|
|
1300
|
+
role: "activity",
|
|
1301
|
+
activityType: "completed",
|
|
1302
|
+
content: { result: "Done!" },
|
|
1303
|
+
} as ActivityMessage,
|
|
1304
|
+
];
|
|
1305
|
+
|
|
1306
|
+
rerender(
|
|
1307
|
+
<CopilotKitProvider
|
|
1308
|
+
renderActivityMessages={[progressRenderer, completedRenderer]}
|
|
1309
|
+
>
|
|
1310
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1311
|
+
<CopilotChatMessageView
|
|
1312
|
+
messages={updatedMessages}
|
|
1313
|
+
isRunning={false}
|
|
1314
|
+
/>
|
|
1315
|
+
</CopilotChatConfigurationProvider>
|
|
1316
|
+
</CopilotKitProvider>,
|
|
1317
|
+
);
|
|
1318
|
+
|
|
1319
|
+
await waitFor(() => {
|
|
1320
|
+
expect(screen.getByTestId("activity-type-activity-1").textContent).toBe(
|
|
1321
|
+
"completed",
|
|
1322
|
+
);
|
|
1323
|
+
});
|
|
1324
|
+
|
|
1325
|
+
const renderCountAfterTypeChange = renderCounts["activity-1"]!;
|
|
1326
|
+
|
|
1327
|
+
// THE KEY ASSERTION: Activity should re-render when activityType changes
|
|
1328
|
+
expect(renderCountAfterTypeChange).toBeGreaterThan(renderCountAfterInitial);
|
|
1329
|
+
});
|
|
1330
|
+
});
|
|
1331
|
+
|
|
1332
|
+
describe("Custom Message Re-render Prevention", () => {
|
|
1333
|
+
it("should not re-render a custom message for a previous message when a new message streams in", async () => {
|
|
1334
|
+
const agent = new MockStepwiseAgent();
|
|
1335
|
+
|
|
1336
|
+
// Track render counts by message ID and position
|
|
1337
|
+
const renderCounts: Record<string, number> = {};
|
|
1338
|
+
|
|
1339
|
+
// Custom message renderer that tracks renders
|
|
1340
|
+
const customRenderer: ReactCustomMessageRenderer = {
|
|
1341
|
+
render: ({ message, position }) => {
|
|
1342
|
+
// Only render for assistant messages in "after" position
|
|
1343
|
+
if (message.role !== "assistant" || position !== "after") {
|
|
1344
|
+
return null;
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
const key = `${message.id}-${position}`;
|
|
1348
|
+
renderCounts[key] = (renderCounts[key] || 0) + 1;
|
|
1349
|
+
|
|
1350
|
+
return (
|
|
1351
|
+
<div data-testid={`custom-${message.id}`}>
|
|
1352
|
+
<span data-testid={`custom-content-${message.id}`}>
|
|
1353
|
+
Custom content for {message.id}
|
|
1354
|
+
</span>
|
|
1355
|
+
<span data-testid={`custom-render-count-${message.id}`}>
|
|
1356
|
+
{renderCounts[key]}
|
|
1357
|
+
</span>
|
|
1358
|
+
</div>
|
|
1359
|
+
);
|
|
1360
|
+
},
|
|
1361
|
+
};
|
|
1362
|
+
|
|
1363
|
+
// Initial messages - one assistant message
|
|
1364
|
+
const initialMessages: Message[] = [
|
|
1365
|
+
{
|
|
1366
|
+
id: "assistant-1",
|
|
1367
|
+
role: "assistant",
|
|
1368
|
+
content: "Hello! How can I help you?",
|
|
1369
|
+
} as AssistantMessage,
|
|
1370
|
+
];
|
|
1371
|
+
|
|
1372
|
+
const { rerender } = render(
|
|
1373
|
+
<CopilotKitProvider
|
|
1374
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
1375
|
+
renderCustomMessages={[customRenderer]}
|
|
1376
|
+
>
|
|
1377
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1378
|
+
<CopilotChatMessageView
|
|
1379
|
+
messages={initialMessages}
|
|
1380
|
+
isRunning={false}
|
|
1381
|
+
/>
|
|
1382
|
+
</CopilotChatConfigurationProvider>
|
|
1383
|
+
</CopilotKitProvider>,
|
|
1384
|
+
);
|
|
1385
|
+
|
|
1386
|
+
// Verify first custom message rendered
|
|
1387
|
+
await waitFor(() => {
|
|
1388
|
+
expect(screen.getByTestId("custom-assistant-1")).toBeDefined();
|
|
1389
|
+
});
|
|
1390
|
+
|
|
1391
|
+
const firstCustomRenderCountAfterInitial =
|
|
1392
|
+
renderCounts["assistant-1-after"];
|
|
1393
|
+
expect(firstCustomRenderCountAfterInitial).toBe(1);
|
|
1394
|
+
|
|
1395
|
+
// Add a second assistant message
|
|
1396
|
+
const messagesWithSecond: Message[] = [
|
|
1397
|
+
...initialMessages,
|
|
1398
|
+
{
|
|
1399
|
+
id: "assistant-2",
|
|
1400
|
+
role: "assistant",
|
|
1401
|
+
content: "Here's some more info...",
|
|
1402
|
+
} as AssistantMessage,
|
|
1403
|
+
];
|
|
1404
|
+
|
|
1405
|
+
rerender(
|
|
1406
|
+
<CopilotKitProvider
|
|
1407
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
1408
|
+
renderCustomMessages={[customRenderer]}
|
|
1409
|
+
>
|
|
1410
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1411
|
+
<CopilotChatMessageView
|
|
1412
|
+
messages={messagesWithSecond}
|
|
1413
|
+
isRunning={true}
|
|
1414
|
+
/>
|
|
1415
|
+
</CopilotChatConfigurationProvider>
|
|
1416
|
+
</CopilotKitProvider>,
|
|
1417
|
+
);
|
|
1418
|
+
|
|
1419
|
+
await waitFor(() => {
|
|
1420
|
+
expect(screen.getByTestId("custom-assistant-2")).toBeDefined();
|
|
1421
|
+
});
|
|
1422
|
+
|
|
1423
|
+
// Update the second message (streaming more content)
|
|
1424
|
+
const messagesWithUpdatedSecond: Message[] = [
|
|
1425
|
+
initialMessages[0]!,
|
|
1426
|
+
{
|
|
1427
|
+
id: "assistant-2",
|
|
1428
|
+
role: "assistant",
|
|
1429
|
+
content: "Here's some more info... Let me explain in detail.",
|
|
1430
|
+
} as AssistantMessage,
|
|
1431
|
+
];
|
|
1432
|
+
|
|
1433
|
+
rerender(
|
|
1434
|
+
<CopilotKitProvider
|
|
1435
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
1436
|
+
renderCustomMessages={[customRenderer]}
|
|
1437
|
+
>
|
|
1438
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1439
|
+
<CopilotChatMessageView
|
|
1440
|
+
messages={messagesWithUpdatedSecond}
|
|
1441
|
+
isRunning={true}
|
|
1442
|
+
/>
|
|
1443
|
+
</CopilotChatConfigurationProvider>
|
|
1444
|
+
</CopilotKitProvider>,
|
|
1445
|
+
);
|
|
1446
|
+
|
|
1447
|
+
// Stream even more content
|
|
1448
|
+
const messagesWithMoreContent: Message[] = [
|
|
1449
|
+
initialMessages[0]!,
|
|
1450
|
+
{
|
|
1451
|
+
id: "assistant-2",
|
|
1452
|
+
role: "assistant",
|
|
1453
|
+
content:
|
|
1454
|
+
"Here's some more info... Let me explain in detail. This is comprehensive.",
|
|
1455
|
+
} as AssistantMessage,
|
|
1456
|
+
];
|
|
1457
|
+
|
|
1458
|
+
rerender(
|
|
1459
|
+
<CopilotKitProvider
|
|
1460
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
1461
|
+
renderCustomMessages={[customRenderer]}
|
|
1462
|
+
>
|
|
1463
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1464
|
+
<CopilotChatMessageView
|
|
1465
|
+
messages={messagesWithMoreContent}
|
|
1466
|
+
isRunning={false}
|
|
1467
|
+
/>
|
|
1468
|
+
</CopilotChatConfigurationProvider>
|
|
1469
|
+
</CopilotKitProvider>,
|
|
1470
|
+
);
|
|
1471
|
+
|
|
1472
|
+
const firstCustomRenderCountAfterAllUpdates =
|
|
1473
|
+
renderCounts["assistant-1-after"];
|
|
1474
|
+
|
|
1475
|
+
// THE KEY ASSERTION: The first custom message should NOT have re-rendered
|
|
1476
|
+
// when the second message was streaming
|
|
1477
|
+
expect(firstCustomRenderCountAfterAllUpdates).toBe(
|
|
1478
|
+
firstCustomRenderCountAfterInitial,
|
|
1479
|
+
);
|
|
1480
|
+
|
|
1481
|
+
// Verify the second custom message did update
|
|
1482
|
+
expect(renderCounts["assistant-2-after"]).toBeGreaterThan(1);
|
|
1483
|
+
});
|
|
1484
|
+
|
|
1485
|
+
it("should not re-render custom messages when isRunning changes but message content is the same", async () => {
|
|
1486
|
+
const agent = new MockStepwiseAgent();
|
|
1487
|
+
const renderCounts: Record<string, number> = {};
|
|
1488
|
+
|
|
1489
|
+
const customRenderer: ReactCustomMessageRenderer = {
|
|
1490
|
+
render: ({ message, position }) => {
|
|
1491
|
+
if (message.role !== "assistant" || position !== "after") {
|
|
1492
|
+
return null;
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
const key = `${message.id}-${position}`;
|
|
1496
|
+
renderCounts[key] = (renderCounts[key] || 0) + 1;
|
|
1497
|
+
|
|
1498
|
+
return (
|
|
1499
|
+
<div data-testid={`custom-${message.id}`}>
|
|
1500
|
+
Render count: {renderCounts[key]}
|
|
1501
|
+
</div>
|
|
1502
|
+
);
|
|
1503
|
+
},
|
|
1504
|
+
};
|
|
1505
|
+
|
|
1506
|
+
const messages: Message[] = [
|
|
1507
|
+
{
|
|
1508
|
+
id: "assistant-1",
|
|
1509
|
+
role: "assistant",
|
|
1510
|
+
content: "Complete message",
|
|
1511
|
+
} as AssistantMessage,
|
|
1512
|
+
];
|
|
1513
|
+
|
|
1514
|
+
const { rerender } = render(
|
|
1515
|
+
<CopilotKitProvider
|
|
1516
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
1517
|
+
renderCustomMessages={[customRenderer]}
|
|
1518
|
+
>
|
|
1519
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1520
|
+
<CopilotChatMessageView messages={messages} isRunning={true} />
|
|
1521
|
+
</CopilotChatConfigurationProvider>
|
|
1522
|
+
</CopilotKitProvider>,
|
|
1523
|
+
);
|
|
1524
|
+
|
|
1525
|
+
await waitFor(() => {
|
|
1526
|
+
expect(screen.getByTestId("custom-assistant-1")).toBeDefined();
|
|
1527
|
+
});
|
|
1528
|
+
|
|
1529
|
+
const renderCountWhileRunning = renderCounts["assistant-1-after"]!;
|
|
1530
|
+
expect(renderCountWhileRunning).toBe(1);
|
|
1531
|
+
|
|
1532
|
+
// Change isRunning to false (but same messages)
|
|
1533
|
+
rerender(
|
|
1534
|
+
<CopilotKitProvider
|
|
1535
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
1536
|
+
renderCustomMessages={[customRenderer]}
|
|
1537
|
+
>
|
|
1538
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1539
|
+
<CopilotChatMessageView messages={messages} isRunning={false} />
|
|
1540
|
+
</CopilotChatConfigurationProvider>
|
|
1541
|
+
</CopilotKitProvider>,
|
|
1542
|
+
);
|
|
1543
|
+
|
|
1544
|
+
const renderCountAfterRunningChanged = renderCounts["assistant-1-after"]!;
|
|
1545
|
+
|
|
1546
|
+
// THE KEY ASSERTION: Custom message should not re-render just because isRunning changed
|
|
1547
|
+
expect(renderCountAfterRunningChanged).toBe(renderCountWhileRunning);
|
|
1548
|
+
});
|
|
1549
|
+
|
|
1550
|
+
it("should re-render a custom message when its message content changes", async () => {
|
|
1551
|
+
const agent = new MockStepwiseAgent();
|
|
1552
|
+
const renderCounts: Record<string, number> = {};
|
|
1553
|
+
const capturedContent: string[] = [];
|
|
1554
|
+
|
|
1555
|
+
const customRenderer: ReactCustomMessageRenderer = {
|
|
1556
|
+
render: ({ message, position }) => {
|
|
1557
|
+
if (message.role !== "assistant" || position !== "after") {
|
|
1558
|
+
return null;
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
const key = `${message.id}-${position}`;
|
|
1562
|
+
renderCounts[key] = (renderCounts[key] || 0) + 1;
|
|
1563
|
+
const content =
|
|
1564
|
+
typeof message.content === "string" ? message.content : "";
|
|
1565
|
+
capturedContent.push(content);
|
|
1566
|
+
|
|
1567
|
+
return (
|
|
1568
|
+
<div data-testid={`custom-${message.id}`}>
|
|
1569
|
+
<span data-testid={`custom-content-${message.id}`}>{content}</span>
|
|
1570
|
+
<span data-testid={`custom-render-count-${message.id}`}>
|
|
1571
|
+
{renderCounts[key]}
|
|
1572
|
+
</span>
|
|
1573
|
+
</div>
|
|
1574
|
+
);
|
|
1575
|
+
},
|
|
1576
|
+
};
|
|
1577
|
+
|
|
1578
|
+
const initialMessages: Message[] = [
|
|
1579
|
+
{
|
|
1580
|
+
id: "assistant-1",
|
|
1581
|
+
role: "assistant",
|
|
1582
|
+
content: "Hello",
|
|
1583
|
+
} as AssistantMessage,
|
|
1584
|
+
];
|
|
1585
|
+
|
|
1586
|
+
const { rerender } = render(
|
|
1587
|
+
<CopilotKitProvider
|
|
1588
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
1589
|
+
renderCustomMessages={[customRenderer]}
|
|
1590
|
+
>
|
|
1591
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1592
|
+
<CopilotChatMessageView messages={initialMessages} isRunning={true} />
|
|
1593
|
+
</CopilotChatConfigurationProvider>
|
|
1594
|
+
</CopilotKitProvider>,
|
|
1595
|
+
);
|
|
1596
|
+
|
|
1597
|
+
await waitFor(() => {
|
|
1598
|
+
expect(screen.getByTestId("custom-assistant-1")).toBeDefined();
|
|
1599
|
+
});
|
|
1600
|
+
|
|
1601
|
+
const renderCountAfterInitial = renderCounts["assistant-1-after"]!;
|
|
1602
|
+
expect(renderCountAfterInitial).toBe(1);
|
|
1603
|
+
|
|
1604
|
+
// Update message content (streaming)
|
|
1605
|
+
const updatedMessages: Message[] = [
|
|
1606
|
+
{
|
|
1607
|
+
id: "assistant-1",
|
|
1608
|
+
role: "assistant",
|
|
1609
|
+
content: "Hello! How can I help you today?",
|
|
1610
|
+
} as AssistantMessage,
|
|
1611
|
+
];
|
|
1612
|
+
|
|
1613
|
+
rerender(
|
|
1614
|
+
<CopilotKitProvider
|
|
1615
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
1616
|
+
renderCustomMessages={[customRenderer]}
|
|
1617
|
+
>
|
|
1618
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1619
|
+
<CopilotChatMessageView messages={updatedMessages} isRunning={true} />
|
|
1620
|
+
</CopilotChatConfigurationProvider>
|
|
1621
|
+
</CopilotKitProvider>,
|
|
1622
|
+
);
|
|
1623
|
+
|
|
1624
|
+
await waitFor(() => {
|
|
1625
|
+
expect(screen.getByTestId("custom-content-assistant-1").textContent).toBe(
|
|
1626
|
+
"Hello! How can I help you today?",
|
|
1627
|
+
);
|
|
1628
|
+
});
|
|
1629
|
+
|
|
1630
|
+
const renderCountAfterUpdate = renderCounts["assistant-1-after"]!;
|
|
1631
|
+
|
|
1632
|
+
// THE KEY ASSERTION: Custom message should re-render when content changes
|
|
1633
|
+
expect(renderCountAfterUpdate).toBeGreaterThan(renderCountAfterInitial);
|
|
1634
|
+
expect(capturedContent).toContain("Hello");
|
|
1635
|
+
expect(capturedContent).toContain("Hello! How can I help you today?");
|
|
1636
|
+
});
|
|
1637
|
+
|
|
1638
|
+
it("should re-render a custom message when its message role changes", async () => {
|
|
1639
|
+
const agent = new MockStepwiseAgent();
|
|
1640
|
+
const renderCounts: Record<string, number> = {};
|
|
1641
|
+
|
|
1642
|
+
const customRenderer: ReactCustomMessageRenderer = {
|
|
1643
|
+
render: ({ message, position }) => {
|
|
1644
|
+
if (position !== "after") {
|
|
1645
|
+
return null;
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
const key = `${message.id}-${position}`;
|
|
1649
|
+
renderCounts[key] = (renderCounts[key] || 0) + 1;
|
|
1650
|
+
|
|
1651
|
+
return (
|
|
1652
|
+
<div data-testid={`custom-${message.id}`}>
|
|
1653
|
+
<span data-testid={`custom-role-${message.id}`}>
|
|
1654
|
+
{message.role}
|
|
1655
|
+
</span>
|
|
1656
|
+
<span data-testid={`custom-render-count-${message.id}`}>
|
|
1657
|
+
{renderCounts[key]}
|
|
1658
|
+
</span>
|
|
1659
|
+
</div>
|
|
1660
|
+
);
|
|
1661
|
+
},
|
|
1662
|
+
};
|
|
1663
|
+
|
|
1664
|
+
const initialMessages: Message[] = [
|
|
1665
|
+
{
|
|
1666
|
+
id: "msg-1",
|
|
1667
|
+
role: "user",
|
|
1668
|
+
content: "Hello",
|
|
1669
|
+
},
|
|
1670
|
+
];
|
|
1671
|
+
|
|
1672
|
+
const { rerender } = render(
|
|
1673
|
+
<CopilotKitProvider
|
|
1674
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
1675
|
+
renderCustomMessages={[customRenderer]}
|
|
1676
|
+
>
|
|
1677
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1678
|
+
<CopilotChatMessageView
|
|
1679
|
+
messages={initialMessages}
|
|
1680
|
+
isRunning={false}
|
|
1681
|
+
/>
|
|
1682
|
+
</CopilotChatConfigurationProvider>
|
|
1683
|
+
</CopilotKitProvider>,
|
|
1684
|
+
);
|
|
1685
|
+
|
|
1686
|
+
await waitFor(() => {
|
|
1687
|
+
expect(screen.getByTestId("custom-role-msg-1").textContent).toBe("user");
|
|
1688
|
+
});
|
|
1689
|
+
|
|
1690
|
+
const renderCountAfterInitial = renderCounts["msg-1-after"]!;
|
|
1691
|
+
expect(renderCountAfterInitial).toBe(1);
|
|
1692
|
+
|
|
1693
|
+
// Change message role (unusual but possible)
|
|
1694
|
+
const updatedMessages: Message[] = [
|
|
1695
|
+
{
|
|
1696
|
+
id: "msg-1",
|
|
1697
|
+
role: "assistant",
|
|
1698
|
+
content: "Hello",
|
|
1699
|
+
} as AssistantMessage,
|
|
1700
|
+
];
|
|
1701
|
+
|
|
1702
|
+
rerender(
|
|
1703
|
+
<CopilotKitProvider
|
|
1704
|
+
agents__unsafe_dev_only={{ default: agent }}
|
|
1705
|
+
renderCustomMessages={[customRenderer]}
|
|
1706
|
+
>
|
|
1707
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1708
|
+
<CopilotChatMessageView
|
|
1709
|
+
messages={updatedMessages}
|
|
1710
|
+
isRunning={false}
|
|
1711
|
+
/>
|
|
1712
|
+
</CopilotChatConfigurationProvider>
|
|
1713
|
+
</CopilotKitProvider>,
|
|
1714
|
+
);
|
|
1715
|
+
|
|
1716
|
+
await waitFor(() => {
|
|
1717
|
+
expect(screen.getByTestId("custom-role-msg-1").textContent).toBe(
|
|
1718
|
+
"assistant",
|
|
1719
|
+
);
|
|
1720
|
+
});
|
|
1721
|
+
|
|
1722
|
+
const renderCountAfterRoleChange = renderCounts["msg-1-after"]!;
|
|
1723
|
+
|
|
1724
|
+
// THE KEY ASSERTION: Custom message should re-render when role changes
|
|
1725
|
+
expect(renderCountAfterRoleChange).toBeGreaterThan(renderCountAfterInitial);
|
|
1726
|
+
});
|
|
1727
|
+
});
|
|
1728
|
+
|
|
1729
|
+
describe("Input Component Re-render Prevention", () => {
|
|
1730
|
+
it("should not re-render the input component when messages stream in", async () => {
|
|
1731
|
+
let inputRenderCount = 0;
|
|
1732
|
+
|
|
1733
|
+
// Custom input component that tracks renders
|
|
1734
|
+
const TrackedInput: React.FC<CopilotChatInputProps> = (props) => {
|
|
1735
|
+
inputRenderCount++;
|
|
1736
|
+
return (
|
|
1737
|
+
<div data-testid="tracked-input">
|
|
1738
|
+
<span data-testid="input-render-count">{inputRenderCount}</span>
|
|
1739
|
+
<CopilotChatInput {...props} />
|
|
1740
|
+
</div>
|
|
1741
|
+
);
|
|
1742
|
+
};
|
|
1743
|
+
|
|
1744
|
+
// Use a stable callback reference to properly test memoization
|
|
1745
|
+
const stableOnSubmit = () => {};
|
|
1746
|
+
|
|
1747
|
+
const initialMessages: Message[] = [
|
|
1748
|
+
{
|
|
1749
|
+
id: "msg-1",
|
|
1750
|
+
role: "assistant",
|
|
1751
|
+
content: "Hello!",
|
|
1752
|
+
} as AssistantMessage,
|
|
1753
|
+
];
|
|
1754
|
+
|
|
1755
|
+
const { rerender } = render(
|
|
1756
|
+
<CopilotKitProvider>
|
|
1757
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1758
|
+
<CopilotChatView
|
|
1759
|
+
messages={initialMessages}
|
|
1760
|
+
isRunning={false}
|
|
1761
|
+
input={TrackedInput as any}
|
|
1762
|
+
onSubmitMessage={stableOnSubmit}
|
|
1763
|
+
/>
|
|
1764
|
+
</CopilotChatConfigurationProvider>
|
|
1765
|
+
</CopilotKitProvider>,
|
|
1766
|
+
);
|
|
1767
|
+
|
|
1768
|
+
await waitFor(() => {
|
|
1769
|
+
expect(screen.getByTestId("tracked-input")).toBeDefined();
|
|
1770
|
+
});
|
|
1771
|
+
|
|
1772
|
+
const renderCountAfterInitial = inputRenderCount;
|
|
1773
|
+
expect(renderCountAfterInitial).toBe(1);
|
|
1774
|
+
|
|
1775
|
+
// Stream a new message (add more content)
|
|
1776
|
+
const updatedMessages: Message[] = [
|
|
1777
|
+
...initialMessages,
|
|
1778
|
+
{
|
|
1779
|
+
id: "msg-2",
|
|
1780
|
+
role: "assistant",
|
|
1781
|
+
content: "How can I help?",
|
|
1782
|
+
} as AssistantMessage,
|
|
1783
|
+
];
|
|
1784
|
+
|
|
1785
|
+
rerender(
|
|
1786
|
+
<CopilotKitProvider>
|
|
1787
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1788
|
+
<CopilotChatView
|
|
1789
|
+
messages={updatedMessages}
|
|
1790
|
+
isRunning={false}
|
|
1791
|
+
input={TrackedInput as any}
|
|
1792
|
+
onSubmitMessage={stableOnSubmit}
|
|
1793
|
+
/>
|
|
1794
|
+
</CopilotChatConfigurationProvider>
|
|
1795
|
+
</CopilotKitProvider>,
|
|
1796
|
+
);
|
|
1797
|
+
|
|
1798
|
+
// Continue streaming
|
|
1799
|
+
const moreMessages: Message[] = [
|
|
1800
|
+
...initialMessages,
|
|
1801
|
+
{
|
|
1802
|
+
id: "msg-2",
|
|
1803
|
+
role: "assistant",
|
|
1804
|
+
content: "How can I help you today?",
|
|
1805
|
+
} as AssistantMessage,
|
|
1806
|
+
];
|
|
1807
|
+
|
|
1808
|
+
rerender(
|
|
1809
|
+
<CopilotKitProvider>
|
|
1810
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1811
|
+
<CopilotChatView
|
|
1812
|
+
messages={moreMessages}
|
|
1813
|
+
isRunning={false}
|
|
1814
|
+
input={TrackedInput as any}
|
|
1815
|
+
onSubmitMessage={stableOnSubmit}
|
|
1816
|
+
/>
|
|
1817
|
+
</CopilotChatConfigurationProvider>
|
|
1818
|
+
</CopilotKitProvider>,
|
|
1819
|
+
);
|
|
1820
|
+
|
|
1821
|
+
// Even more streaming
|
|
1822
|
+
const evenMoreMessages: Message[] = [
|
|
1823
|
+
...initialMessages,
|
|
1824
|
+
{
|
|
1825
|
+
id: "msg-2",
|
|
1826
|
+
role: "assistant",
|
|
1827
|
+
content: "How can I help you today? I'm here to assist.",
|
|
1828
|
+
} as AssistantMessage,
|
|
1829
|
+
];
|
|
1830
|
+
|
|
1831
|
+
rerender(
|
|
1832
|
+
<CopilotKitProvider>
|
|
1833
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1834
|
+
<CopilotChatView
|
|
1835
|
+
messages={evenMoreMessages}
|
|
1836
|
+
isRunning={false}
|
|
1837
|
+
input={TrackedInput as any}
|
|
1838
|
+
onSubmitMessage={stableOnSubmit}
|
|
1839
|
+
/>
|
|
1840
|
+
</CopilotChatConfigurationProvider>
|
|
1841
|
+
</CopilotKitProvider>,
|
|
1842
|
+
);
|
|
1843
|
+
|
|
1844
|
+
const renderCountAfterStreaming = inputRenderCount;
|
|
1845
|
+
|
|
1846
|
+
// THE KEY ASSERTION: Input should NOT re-render when messages change
|
|
1847
|
+
// (since input props haven't changed)
|
|
1848
|
+
expect(renderCountAfterStreaming).toBe(renderCountAfterInitial);
|
|
1849
|
+
});
|
|
1850
|
+
|
|
1851
|
+
it("should re-render a replaced input component when its internal state changes", async () => {
|
|
1852
|
+
let externalRenderCount = 0;
|
|
1853
|
+
|
|
1854
|
+
// Custom input with internal state - uses useState to track clicks
|
|
1855
|
+
const InputWithInternalState: React.FC<CopilotChatInputProps> = (props) => {
|
|
1856
|
+
const [clickCount, setClickCount] = useState(0);
|
|
1857
|
+
externalRenderCount++;
|
|
1858
|
+
|
|
1859
|
+
return (
|
|
1860
|
+
<div data-testid="stateful-input">
|
|
1861
|
+
<span data-testid="external-render-count">{externalRenderCount}</span>
|
|
1862
|
+
<span data-testid="click-count">{clickCount}</span>
|
|
1863
|
+
<button
|
|
1864
|
+
data-testid="increment-button"
|
|
1865
|
+
onClick={() => setClickCount((c) => c + 1)}
|
|
1866
|
+
>
|
|
1867
|
+
Increment
|
|
1868
|
+
</button>
|
|
1869
|
+
<CopilotChatInput {...props} />
|
|
1870
|
+
</div>
|
|
1871
|
+
);
|
|
1872
|
+
};
|
|
1873
|
+
|
|
1874
|
+
const messages: Message[] = [
|
|
1875
|
+
{
|
|
1876
|
+
id: "msg-1",
|
|
1877
|
+
role: "assistant",
|
|
1878
|
+
content: "Hello!",
|
|
1879
|
+
} as AssistantMessage,
|
|
1880
|
+
];
|
|
1881
|
+
|
|
1882
|
+
render(
|
|
1883
|
+
<CopilotKitProvider>
|
|
1884
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1885
|
+
<CopilotChatView
|
|
1886
|
+
messages={messages}
|
|
1887
|
+
isRunning={false}
|
|
1888
|
+
input={InputWithInternalState as any}
|
|
1889
|
+
/>
|
|
1890
|
+
</CopilotChatConfigurationProvider>
|
|
1891
|
+
</CopilotKitProvider>,
|
|
1892
|
+
);
|
|
1893
|
+
|
|
1894
|
+
await waitFor(() => {
|
|
1895
|
+
expect(screen.getByTestId("stateful-input")).toBeDefined();
|
|
1896
|
+
});
|
|
1897
|
+
|
|
1898
|
+
// Initial state
|
|
1899
|
+
expect(screen.getByTestId("click-count").textContent).toBe("0");
|
|
1900
|
+
const initialExternalRenderCount = externalRenderCount;
|
|
1901
|
+
expect(initialExternalRenderCount).toBe(1);
|
|
1902
|
+
|
|
1903
|
+
// Click the button to trigger internal state change
|
|
1904
|
+
const incrementButton = screen.getByTestId("increment-button");
|
|
1905
|
+
fireEvent.click(incrementButton);
|
|
1906
|
+
|
|
1907
|
+
// THE KEY ASSERTION: Internal state changes SHOULD cause re-render
|
|
1908
|
+
await waitFor(() => {
|
|
1909
|
+
expect(screen.getByTestId("click-count").textContent).toBe("1");
|
|
1910
|
+
});
|
|
1911
|
+
|
|
1912
|
+
// Verify the component actually re-rendered (not just DOM updated)
|
|
1913
|
+
expect(externalRenderCount).toBe(2);
|
|
1914
|
+
|
|
1915
|
+
// Click again to confirm consistent behavior
|
|
1916
|
+
fireEvent.click(incrementButton);
|
|
1917
|
+
|
|
1918
|
+
await waitFor(() => {
|
|
1919
|
+
expect(screen.getByTestId("click-count").textContent).toBe("2");
|
|
1920
|
+
});
|
|
1921
|
+
|
|
1922
|
+
expect(externalRenderCount).toBe(3);
|
|
1923
|
+
});
|
|
1924
|
+
|
|
1925
|
+
it("should re-render the input component when its props change", async () => {
|
|
1926
|
+
let inputRenderCount = 0;
|
|
1927
|
+
const capturedModes: string[] = [];
|
|
1928
|
+
|
|
1929
|
+
const TrackedInput: React.FC<CopilotChatInputProps> = (props) => {
|
|
1930
|
+
inputRenderCount++;
|
|
1931
|
+
capturedModes.push(props.mode || "default");
|
|
1932
|
+
return (
|
|
1933
|
+
<div data-testid="tracked-input">
|
|
1934
|
+
<span data-testid="input-mode">{props.mode}</span>
|
|
1935
|
+
<CopilotChatInput {...props} />
|
|
1936
|
+
</div>
|
|
1937
|
+
);
|
|
1938
|
+
};
|
|
1939
|
+
|
|
1940
|
+
const messages: Message[] = [
|
|
1941
|
+
{
|
|
1942
|
+
id: "msg-1",
|
|
1943
|
+
role: "assistant",
|
|
1944
|
+
content: "Hello!",
|
|
1945
|
+
} as AssistantMessage,
|
|
1946
|
+
];
|
|
1947
|
+
|
|
1948
|
+
const { rerender } = render(
|
|
1949
|
+
<CopilotKitProvider>
|
|
1950
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1951
|
+
<CopilotChatView
|
|
1952
|
+
messages={messages}
|
|
1953
|
+
isRunning={false}
|
|
1954
|
+
input={TrackedInput as any}
|
|
1955
|
+
onSubmitMessage={() => {}}
|
|
1956
|
+
inputMode="input"
|
|
1957
|
+
/>
|
|
1958
|
+
</CopilotChatConfigurationProvider>
|
|
1959
|
+
</CopilotKitProvider>,
|
|
1960
|
+
);
|
|
1961
|
+
|
|
1962
|
+
await waitFor(() => {
|
|
1963
|
+
expect(screen.getByTestId("tracked-input")).toBeDefined();
|
|
1964
|
+
});
|
|
1965
|
+
|
|
1966
|
+
const renderCountAfterInitial = inputRenderCount;
|
|
1967
|
+
expect(renderCountAfterInitial).toBe(1);
|
|
1968
|
+
|
|
1969
|
+
// Change the mode prop
|
|
1970
|
+
rerender(
|
|
1971
|
+
<CopilotKitProvider>
|
|
1972
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
1973
|
+
<CopilotChatView
|
|
1974
|
+
messages={messages}
|
|
1975
|
+
isRunning={true}
|
|
1976
|
+
input={TrackedInput as any}
|
|
1977
|
+
onSubmitMessage={() => {}}
|
|
1978
|
+
inputMode="processing"
|
|
1979
|
+
/>
|
|
1980
|
+
</CopilotChatConfigurationProvider>
|
|
1981
|
+
</CopilotKitProvider>,
|
|
1982
|
+
);
|
|
1983
|
+
|
|
1984
|
+
await waitFor(() => {
|
|
1985
|
+
expect(screen.getByTestId("input-mode").textContent).toBe("processing");
|
|
1986
|
+
});
|
|
1987
|
+
|
|
1988
|
+
const renderCountAfterModeChange = inputRenderCount;
|
|
1989
|
+
|
|
1990
|
+
// THE KEY ASSERTION: Input SHOULD re-render when its props change
|
|
1991
|
+
expect(renderCountAfterModeChange).toBeGreaterThan(renderCountAfterInitial);
|
|
1992
|
+
expect(capturedModes).toContain("input");
|
|
1993
|
+
expect(capturedModes).toContain("processing");
|
|
1994
|
+
});
|
|
1995
|
+
});
|
|
1996
|
+
|
|
1997
|
+
describe("Suggestion View Re-render Prevention", () => {
|
|
1998
|
+
it("should re-render a suggestion when its loading state changes", async () => {
|
|
1999
|
+
const suggestionRenderCounts: Record<string, number> = {};
|
|
2000
|
+
|
|
2001
|
+
const TrackedSuggestionPill: React.FC<{
|
|
2002
|
+
children: React.ReactNode;
|
|
2003
|
+
isLoading?: boolean;
|
|
2004
|
+
onClick?: () => void;
|
|
2005
|
+
}> = ({ children, isLoading, onClick }) => {
|
|
2006
|
+
const title = String(children);
|
|
2007
|
+
suggestionRenderCounts[title] = (suggestionRenderCounts[title] || 0) + 1;
|
|
2008
|
+
return (
|
|
2009
|
+
<button
|
|
2010
|
+
data-testid={`suggestion-${title}`}
|
|
2011
|
+
onClick={onClick}
|
|
2012
|
+
disabled={isLoading}
|
|
2013
|
+
>
|
|
2014
|
+
{title}
|
|
2015
|
+
<span data-testid={`suggestion-loading-${title}`}>
|
|
2016
|
+
{isLoading ? "loading" : "ready"}
|
|
2017
|
+
</span>
|
|
2018
|
+
</button>
|
|
2019
|
+
);
|
|
2020
|
+
};
|
|
2021
|
+
|
|
2022
|
+
const suggestions = [
|
|
2023
|
+
{ title: "Tell me a joke", message: "Tell me a joke", isLoading: false },
|
|
2024
|
+
{
|
|
2025
|
+
title: "What's the weather?",
|
|
2026
|
+
message: "What's the weather?",
|
|
2027
|
+
isLoading: false,
|
|
2028
|
+
},
|
|
2029
|
+
];
|
|
2030
|
+
|
|
2031
|
+
const { rerender } = render(
|
|
2032
|
+
<CopilotKitProvider>
|
|
2033
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
2034
|
+
<CopilotChatSuggestionView
|
|
2035
|
+
suggestions={suggestions}
|
|
2036
|
+
suggestion={TrackedSuggestionPill as any}
|
|
2037
|
+
/>
|
|
2038
|
+
</CopilotChatConfigurationProvider>
|
|
2039
|
+
</CopilotKitProvider>,
|
|
2040
|
+
);
|
|
2041
|
+
|
|
2042
|
+
await waitFor(() => {
|
|
2043
|
+
expect(
|
|
2044
|
+
screen.getByTestId("suggestion-loading-Tell me a joke").textContent,
|
|
2045
|
+
).toBe("ready");
|
|
2046
|
+
});
|
|
2047
|
+
|
|
2048
|
+
const initialRenderCount = suggestionRenderCounts["Tell me a joke"]!;
|
|
2049
|
+
|
|
2050
|
+
// Set first suggestion to loading
|
|
2051
|
+
rerender(
|
|
2052
|
+
<CopilotKitProvider>
|
|
2053
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
2054
|
+
<CopilotChatSuggestionView
|
|
2055
|
+
suggestions={suggestions}
|
|
2056
|
+
loadingIndexes={[0]}
|
|
2057
|
+
suggestion={TrackedSuggestionPill as any}
|
|
2058
|
+
/>
|
|
2059
|
+
</CopilotChatConfigurationProvider>
|
|
2060
|
+
</CopilotKitProvider>,
|
|
2061
|
+
);
|
|
2062
|
+
|
|
2063
|
+
await waitFor(() => {
|
|
2064
|
+
expect(
|
|
2065
|
+
screen.getByTestId("suggestion-loading-Tell me a joke").textContent,
|
|
2066
|
+
).toBe("loading");
|
|
2067
|
+
});
|
|
2068
|
+
|
|
2069
|
+
// THE KEY ASSERTION: Suggestion SHOULD re-render when loading state changes
|
|
2070
|
+
expect(suggestionRenderCounts["Tell me a joke"]).toBeGreaterThan(
|
|
2071
|
+
initialRenderCount,
|
|
2072
|
+
);
|
|
2073
|
+
});
|
|
2074
|
+
});
|
|
2075
|
+
|
|
2076
|
+
describe("Markdown Renderer Re-render Prevention", () => {
|
|
2077
|
+
it("should not re-render markdown when other messages change", async () => {
|
|
2078
|
+
const markdownRenderCounts: Record<string, number> = {};
|
|
2079
|
+
|
|
2080
|
+
const TrackedMarkdownRenderer: React.FC<{
|
|
2081
|
+
content: string;
|
|
2082
|
+
}> = ({ content }) => {
|
|
2083
|
+
markdownRenderCounts[content] = (markdownRenderCounts[content] || 0) + 1;
|
|
2084
|
+
return (
|
|
2085
|
+
<div data-testid={`markdown-${content.slice(0, 20)}`}>
|
|
2086
|
+
<span data-testid={`markdown-content-${content.slice(0, 20)}`}>
|
|
2087
|
+
{content}
|
|
2088
|
+
</span>
|
|
2089
|
+
<span data-testid={`markdown-render-count-${content.slice(0, 20)}`}>
|
|
2090
|
+
{markdownRenderCounts[content]}
|
|
2091
|
+
</span>
|
|
2092
|
+
</div>
|
|
2093
|
+
);
|
|
2094
|
+
};
|
|
2095
|
+
|
|
2096
|
+
const TrackedAssistantMessage: React.FC<{
|
|
2097
|
+
message: AssistantMessage;
|
|
2098
|
+
messages?: Message[];
|
|
2099
|
+
isRunning?: boolean;
|
|
2100
|
+
}> = ({ message, messages, isRunning }) => {
|
|
2101
|
+
return (
|
|
2102
|
+
<CopilotChatAssistantMessage
|
|
2103
|
+
message={message}
|
|
2104
|
+
messages={messages}
|
|
2105
|
+
isRunning={isRunning}
|
|
2106
|
+
markdownRenderer={TrackedMarkdownRenderer as any}
|
|
2107
|
+
/>
|
|
2108
|
+
);
|
|
2109
|
+
};
|
|
2110
|
+
|
|
2111
|
+
const initialMessages: Message[] = [
|
|
2112
|
+
{
|
|
2113
|
+
id: "msg-1",
|
|
2114
|
+
role: "assistant",
|
|
2115
|
+
content: "Hello! How can I help?",
|
|
2116
|
+
} as AssistantMessage,
|
|
2117
|
+
];
|
|
2118
|
+
|
|
2119
|
+
const { rerender } = render(
|
|
2120
|
+
<CopilotKitProvider>
|
|
2121
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
2122
|
+
<CopilotChatMessageView
|
|
2123
|
+
messages={initialMessages}
|
|
2124
|
+
isRunning={false}
|
|
2125
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
2126
|
+
/>
|
|
2127
|
+
</CopilotChatConfigurationProvider>
|
|
2128
|
+
</CopilotKitProvider>,
|
|
2129
|
+
);
|
|
2130
|
+
|
|
2131
|
+
await waitFor(() => {
|
|
2132
|
+
expect(screen.getByTestId("markdown-Hello! How can I hel")).toBeDefined();
|
|
2133
|
+
});
|
|
2134
|
+
|
|
2135
|
+
const initialRenderCount = markdownRenderCounts["Hello! How can I help?"]!;
|
|
2136
|
+
|
|
2137
|
+
// Add a new message (simulating streaming)
|
|
2138
|
+
const messagesWithSecond: Message[] = [
|
|
2139
|
+
...initialMessages,
|
|
2140
|
+
{
|
|
2141
|
+
id: "msg-2",
|
|
2142
|
+
role: "assistant",
|
|
2143
|
+
content: "Let me help you with",
|
|
2144
|
+
} as AssistantMessage,
|
|
2145
|
+
];
|
|
2146
|
+
|
|
2147
|
+
rerender(
|
|
2148
|
+
<CopilotKitProvider>
|
|
2149
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
2150
|
+
<CopilotChatMessageView
|
|
2151
|
+
messages={messagesWithSecond}
|
|
2152
|
+
isRunning={true}
|
|
2153
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
2154
|
+
/>
|
|
2155
|
+
</CopilotChatConfigurationProvider>
|
|
2156
|
+
</CopilotKitProvider>,
|
|
2157
|
+
);
|
|
2158
|
+
|
|
2159
|
+
await waitFor(() => {
|
|
2160
|
+
expect(screen.getByTestId("markdown-Let me help you with")).toBeDefined();
|
|
2161
|
+
});
|
|
2162
|
+
|
|
2163
|
+
// THE KEY ASSERTION: First message's markdown should NOT re-render
|
|
2164
|
+
expect(markdownRenderCounts["Hello! How can I help?"]).toBe(
|
|
2165
|
+
initialRenderCount,
|
|
2166
|
+
);
|
|
2167
|
+
});
|
|
2168
|
+
|
|
2169
|
+
it("should re-render markdown when its content changes", async () => {
|
|
2170
|
+
const markdownRenderCounts: Record<string, number> = {};
|
|
2171
|
+
const capturedContent: string[] = [];
|
|
2172
|
+
|
|
2173
|
+
const TrackedMarkdownRenderer: React.FC<{
|
|
2174
|
+
content: string;
|
|
2175
|
+
}> = ({ content }) => {
|
|
2176
|
+
markdownRenderCounts[content] = (markdownRenderCounts[content] || 0) + 1;
|
|
2177
|
+
capturedContent.push(content);
|
|
2178
|
+
return (
|
|
2179
|
+
<div data-testid="markdown">
|
|
2180
|
+
<span data-testid="markdown-content">{content}</span>
|
|
2181
|
+
</div>
|
|
2182
|
+
);
|
|
2183
|
+
};
|
|
2184
|
+
|
|
2185
|
+
const TrackedAssistantMessage: React.FC<{
|
|
2186
|
+
message: AssistantMessage;
|
|
2187
|
+
messages?: Message[];
|
|
2188
|
+
isRunning?: boolean;
|
|
2189
|
+
}> = ({ message, messages, isRunning }) => {
|
|
2190
|
+
return (
|
|
2191
|
+
<CopilotChatAssistantMessage
|
|
2192
|
+
message={message}
|
|
2193
|
+
messages={messages}
|
|
2194
|
+
isRunning={isRunning}
|
|
2195
|
+
markdownRenderer={TrackedMarkdownRenderer as any}
|
|
2196
|
+
/>
|
|
2197
|
+
);
|
|
2198
|
+
};
|
|
2199
|
+
|
|
2200
|
+
const initialMessages: Message[] = [
|
|
2201
|
+
{
|
|
2202
|
+
id: "msg-1",
|
|
2203
|
+
role: "assistant",
|
|
2204
|
+
content: "Hello",
|
|
2205
|
+
} as AssistantMessage,
|
|
2206
|
+
];
|
|
2207
|
+
|
|
2208
|
+
const { rerender } = render(
|
|
2209
|
+
<CopilotKitProvider>
|
|
2210
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
2211
|
+
<CopilotChatMessageView
|
|
2212
|
+
messages={initialMessages}
|
|
2213
|
+
isRunning={true}
|
|
2214
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
2215
|
+
/>
|
|
2216
|
+
</CopilotChatConfigurationProvider>
|
|
2217
|
+
</CopilotKitProvider>,
|
|
2218
|
+
);
|
|
2219
|
+
|
|
2220
|
+
await waitFor(() => {
|
|
2221
|
+
expect(screen.getByTestId("markdown-content").textContent).toBe("Hello");
|
|
2222
|
+
});
|
|
2223
|
+
|
|
2224
|
+
// Stream more content
|
|
2225
|
+
const messagesWithMoreContent: Message[] = [
|
|
2226
|
+
{
|
|
2227
|
+
id: "msg-1",
|
|
2228
|
+
role: "assistant",
|
|
2229
|
+
content: "Hello! How are you today?",
|
|
2230
|
+
} as AssistantMessage,
|
|
2231
|
+
];
|
|
2232
|
+
|
|
2233
|
+
rerender(
|
|
2234
|
+
<CopilotKitProvider>
|
|
2235
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
2236
|
+
<CopilotChatMessageView
|
|
2237
|
+
messages={messagesWithMoreContent}
|
|
2238
|
+
isRunning={true}
|
|
2239
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
2240
|
+
/>
|
|
2241
|
+
</CopilotChatConfigurationProvider>
|
|
2242
|
+
</CopilotKitProvider>,
|
|
2243
|
+
);
|
|
2244
|
+
|
|
2245
|
+
await waitFor(() => {
|
|
2246
|
+
expect(screen.getByTestId("markdown-content").textContent).toBe(
|
|
2247
|
+
"Hello! How are you today?",
|
|
2248
|
+
);
|
|
2249
|
+
});
|
|
2250
|
+
|
|
2251
|
+
// THE KEY ASSERTION: Markdown SHOULD re-render when content changes
|
|
2252
|
+
expect(capturedContent).toContain("Hello");
|
|
2253
|
+
expect(capturedContent).toContain("Hello! How are you today?");
|
|
2254
|
+
});
|
|
2255
|
+
});
|
|
2256
|
+
|
|
2257
|
+
describe("Copy Button Re-render Prevention", () => {
|
|
2258
|
+
it("should not re-render copy button when a new message is added", async () => {
|
|
2259
|
+
let copyButtonRenderCount = 0;
|
|
2260
|
+
|
|
2261
|
+
const TrackedCopyButton: React.FC<{
|
|
2262
|
+
onClick?: () => void;
|
|
2263
|
+
}> = ({ onClick }) => {
|
|
2264
|
+
copyButtonRenderCount++;
|
|
2265
|
+
return (
|
|
2266
|
+
<button data-testid="copy-button" onClick={onClick}>
|
|
2267
|
+
Copy
|
|
2268
|
+
<span data-testid="copy-render-count">{copyButtonRenderCount}</span>
|
|
2269
|
+
</button>
|
|
2270
|
+
);
|
|
2271
|
+
};
|
|
2272
|
+
|
|
2273
|
+
const TrackedAssistantMessage: React.FC<{
|
|
2274
|
+
message: AssistantMessage;
|
|
2275
|
+
messages?: Message[];
|
|
2276
|
+
isRunning?: boolean;
|
|
2277
|
+
}> = ({ message, messages, isRunning }) => {
|
|
2278
|
+
return (
|
|
2279
|
+
<CopilotChatAssistantMessage
|
|
2280
|
+
message={message}
|
|
2281
|
+
messages={messages}
|
|
2282
|
+
isRunning={isRunning}
|
|
2283
|
+
copyButton={TrackedCopyButton as any}
|
|
2284
|
+
/>
|
|
2285
|
+
);
|
|
2286
|
+
};
|
|
2287
|
+
|
|
2288
|
+
// Start with a completed message (isRunning=false so toolbar shows)
|
|
2289
|
+
const initialMessages: Message[] = [
|
|
2290
|
+
{
|
|
2291
|
+
id: "msg-1",
|
|
2292
|
+
role: "assistant",
|
|
2293
|
+
content: "Hello! First message here.",
|
|
2294
|
+
} as AssistantMessage,
|
|
2295
|
+
];
|
|
2296
|
+
|
|
2297
|
+
const { rerender } = render(
|
|
2298
|
+
<CopilotKitProvider>
|
|
2299
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
2300
|
+
<CopilotChatMessageView
|
|
2301
|
+
messages={initialMessages}
|
|
2302
|
+
isRunning={false}
|
|
2303
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
2304
|
+
/>
|
|
2305
|
+
</CopilotChatConfigurationProvider>
|
|
2306
|
+
</CopilotKitProvider>,
|
|
2307
|
+
);
|
|
2308
|
+
|
|
2309
|
+
await waitFor(() => {
|
|
2310
|
+
expect(screen.getByTestId("copy-button")).toBeDefined();
|
|
2311
|
+
});
|
|
2312
|
+
|
|
2313
|
+
const initialRenderCount = copyButtonRenderCount;
|
|
2314
|
+
|
|
2315
|
+
// Add a second message - the first message's copy button should NOT re-render
|
|
2316
|
+
const messagesWithSecond: Message[] = [
|
|
2317
|
+
...initialMessages,
|
|
2318
|
+
{
|
|
2319
|
+
id: "msg-2",
|
|
2320
|
+
role: "assistant",
|
|
2321
|
+
content: "Second message here.",
|
|
2322
|
+
} as AssistantMessage,
|
|
2323
|
+
];
|
|
2324
|
+
|
|
2325
|
+
rerender(
|
|
2326
|
+
<CopilotKitProvider>
|
|
2327
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
2328
|
+
<CopilotChatMessageView
|
|
2329
|
+
messages={messagesWithSecond}
|
|
2330
|
+
isRunning={false}
|
|
2331
|
+
assistantMessage={TrackedAssistantMessage as any}
|
|
2332
|
+
/>
|
|
2333
|
+
</CopilotChatConfigurationProvider>
|
|
2334
|
+
</CopilotKitProvider>,
|
|
2335
|
+
);
|
|
2336
|
+
|
|
2337
|
+
// Wait for second message to render
|
|
2338
|
+
await waitFor(() => {
|
|
2339
|
+
expect(screen.getAllByTestId("copy-button").length).toBe(2);
|
|
2340
|
+
});
|
|
2341
|
+
|
|
2342
|
+
// THE KEY ASSERTION: First message's copy button should NOT re-render when second message is added
|
|
2343
|
+
// We check that the total render count is 2 (one for each message), not 3 (which would mean first re-rendered)
|
|
2344
|
+
expect(copyButtonRenderCount).toBe(2);
|
|
2345
|
+
});
|
|
2346
|
+
|
|
2347
|
+
it("should re-render copy button when its onClick handler changes", async () => {
|
|
2348
|
+
let copyButtonRenderCount = 0;
|
|
2349
|
+
|
|
2350
|
+
const TrackedCopyButton: React.FC<{
|
|
2351
|
+
onClick?: () => void;
|
|
2352
|
+
}> = ({ onClick }) => {
|
|
2353
|
+
copyButtonRenderCount++;
|
|
2354
|
+
return (
|
|
2355
|
+
<button data-testid="copy-button" onClick={onClick}>
|
|
2356
|
+
Copy
|
|
2357
|
+
</button>
|
|
2358
|
+
);
|
|
2359
|
+
};
|
|
2360
|
+
|
|
2361
|
+
// First render with one message (isRunning=false so toolbar shows)
|
|
2362
|
+
const message1: AssistantMessage = {
|
|
2363
|
+
id: "msg-1",
|
|
2364
|
+
role: "assistant",
|
|
2365
|
+
content: "First message",
|
|
2366
|
+
};
|
|
2367
|
+
|
|
2368
|
+
const { rerender } = render(
|
|
2369
|
+
<CopilotKitProvider>
|
|
2370
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
2371
|
+
<CopilotChatAssistantMessage
|
|
2372
|
+
message={message1}
|
|
2373
|
+
isRunning={false}
|
|
2374
|
+
copyButton={TrackedCopyButton as any}
|
|
2375
|
+
/>
|
|
2376
|
+
</CopilotChatConfigurationProvider>
|
|
2377
|
+
</CopilotKitProvider>,
|
|
2378
|
+
);
|
|
2379
|
+
|
|
2380
|
+
await waitFor(() => {
|
|
2381
|
+
expect(screen.getByTestId("copy-button")).toBeDefined();
|
|
2382
|
+
});
|
|
2383
|
+
|
|
2384
|
+
const initialRenderCount = copyButtonRenderCount;
|
|
2385
|
+
|
|
2386
|
+
// Re-render with a completely different message (different ID = different onClick)
|
|
2387
|
+
const message2: AssistantMessage = {
|
|
2388
|
+
id: "msg-2",
|
|
2389
|
+
role: "assistant",
|
|
2390
|
+
content: "Second message",
|
|
2391
|
+
};
|
|
2392
|
+
|
|
2393
|
+
rerender(
|
|
2394
|
+
<CopilotKitProvider>
|
|
2395
|
+
<CopilotChatConfigurationProvider agentId="default" threadId="test">
|
|
2396
|
+
<CopilotChatAssistantMessage
|
|
2397
|
+
message={message2}
|
|
2398
|
+
isRunning={false}
|
|
2399
|
+
copyButton={TrackedCopyButton as any}
|
|
2400
|
+
/>
|
|
2401
|
+
</CopilotChatConfigurationProvider>
|
|
2402
|
+
</CopilotKitProvider>,
|
|
2403
|
+
);
|
|
2404
|
+
|
|
2405
|
+
await waitFor(() => {
|
|
2406
|
+
expect(screen.getByTestId("copy-button")).toBeDefined();
|
|
2407
|
+
});
|
|
2408
|
+
|
|
2409
|
+
// THE KEY ASSERTION: Copy button SHOULD re-render when the message changes
|
|
2410
|
+
// because the onClick handler needs to reference the new message content
|
|
2411
|
+
expect(copyButtonRenderCount).toBeGreaterThan(initialRenderCount);
|
|
2412
|
+
});
|
|
2413
|
+
});
|