@copilotkit/react-core 1.51.3-next.6 → 1.51.3-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 +10 -0
- package/dist/chunk-77IVITG3.mjs +158 -0
- package/dist/chunk-77IVITG3.mjs.map +1 -0
- package/dist/chunk-BKMJ4LC7.mjs +119 -0
- package/dist/chunk-BKMJ4LC7.mjs.map +1 -0
- package/dist/chunk-C3YJYDK4.mjs +189 -0
- package/dist/chunk-C3YJYDK4.mjs.map +1 -0
- package/dist/{chunk-GIU66J37.mjs → chunk-DQXCQWSG.mjs} +47 -5
- package/dist/chunk-DQXCQWSG.mjs.map +1 -0
- package/dist/{chunk-HBMPXNW2.mjs → chunk-LO4RRITI.mjs} +71 -18
- package/dist/chunk-LO4RRITI.mjs.map +1 -0
- package/dist/{chunk-3G4VFRVV.mjs → chunk-NXHQDCZF.mjs} +2 -2
- package/dist/{chunk-FDOMAPJY.mjs → chunk-QD7EID4N.mjs} +1 -1
- package/dist/chunk-QD7EID4N.mjs.map +1 -0
- package/dist/{chunk-YTQHRJUA.mjs → chunk-VKNLTZJE.mjs} +2 -2
- package/dist/{chunk-4RRUJHCI.mjs → chunk-VP43SLSZ.mjs} +2 -2
- package/dist/{chunk-MF2ZSLBV.mjs → chunk-XZFIJ7XF.mjs} +2 -2
- package/dist/components/copilot-provider/copilotkit.js +437 -150
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +5 -3
- package/dist/components/copilot-provider/index.js +437 -150
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +5 -3
- package/dist/components/index.js +437 -150
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +5 -3
- package/dist/context/coagent-state-renders-context.d.ts +1 -0
- package/dist/context/coagent-state-renders-context.js.map +1 -1
- package/dist/context/coagent-state-renders-context.mjs +1 -1
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.js +512 -212
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +19 -17
- package/dist/hooks/use-coagent-state-render-bridge.helpers.d.ts +92 -0
- package/dist/hooks/use-coagent-state-render-bridge.helpers.js +231 -0
- package/dist/hooks/use-coagent-state-render-bridge.helpers.js.map +1 -0
- package/dist/hooks/use-coagent-state-render-bridge.helpers.mjs +24 -0
- package/dist/hooks/use-coagent-state-render-bridge.helpers.mjs.map +1 -0
- package/dist/hooks/use-coagent-state-render-bridge.js +334 -72
- package/dist/hooks/use-coagent-state-render-bridge.js.map +1 -1
- package/dist/hooks/use-coagent-state-render-bridge.mjs +4 -2
- package/dist/hooks/use-coagent-state-render-registry.d.ts +25 -0
- package/dist/hooks/use-coagent-state-render-registry.js +358 -0
- package/dist/hooks/use-coagent-state-render-registry.js.map +1 -0
- package/dist/hooks/use-coagent-state-render-registry.mjs +9 -0
- package/dist/hooks/use-coagent-state-render-registry.mjs.map +1 -0
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +2 -2
- package/dist/hooks/use-copilot-chat-headless_c.js +414 -114
- package/dist/hooks/use-copilot-chat-headless_c.js.map +1 -1
- package/dist/hooks/use-copilot-chat-headless_c.mjs +7 -5
- package/dist/hooks/use-copilot-chat.js +406 -106
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +7 -5
- package/dist/hooks/use-copilot-chat_internal.js +406 -106
- package/dist/hooks/use-copilot-chat_internal.js.map +1 -1
- package/dist/hooks/use-copilot-chat_internal.mjs +6 -4
- package/dist/hooks/use-langgraph-interrupt-render.mjs +1 -1
- package/dist/index.js +651 -311
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +22 -20
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +6 -4
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +6 -4
- package/dist/setupTests.js +1 -0
- package/dist/setupTests.js.map +1 -1
- package/dist/setupTests.mjs +1 -0
- package/dist/setupTests.mjs.map +1 -1
- package/dist/test-helpers/copilot-context.d.ts +14 -0
- package/dist/test-helpers/copilot-context.js +128 -0
- package/dist/test-helpers/copilot-context.js.map +1 -0
- package/dist/test-helpers/copilot-context.mjs +74 -0
- package/dist/test-helpers/copilot-context.mjs.map +1 -0
- package/dist/types/index.mjs +1 -1
- package/package.json +5 -5
- package/src/components/copilot-provider/copilotkit.tsx +56 -0
- package/src/context/coagent-state-renders-context.tsx +1 -0
- package/src/hooks/__tests__/use-coagent-state-render-bridge.helpers.test.ts +100 -0
- package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +892 -37
- package/src/hooks/__tests__/use-coagent-state-render.test.tsx +334 -0
- package/src/hooks/use-coagent-state-render-bridge.helpers.ts +311 -0
- package/src/hooks/use-coagent-state-render-bridge.tsx +25 -120
- package/src/hooks/use-coagent-state-render-registry.ts +215 -0
- package/src/hooks/use-copilot-chat_internal.ts +93 -34
- package/src/setupTests.ts +1 -0
- package/src/test-helpers/copilot-context.ts +91 -0
- package/dist/chunk-3X3I7OJV.mjs +0 -172
- package/dist/chunk-3X3I7OJV.mjs.map +0 -1
- package/dist/chunk-FDOMAPJY.mjs.map +0 -1
- package/dist/chunk-GIU66J37.mjs.map +0 -1
- package/dist/chunk-HBMPXNW2.mjs.map +0 -1
- /package/dist/{chunk-3G4VFRVV.mjs.map → chunk-NXHQDCZF.mjs.map} +0 -0
- /package/dist/{chunk-YTQHRJUA.mjs.map → chunk-VKNLTZJE.mjs.map} +0 -0
- /package/dist/{chunk-4RRUJHCI.mjs.map → chunk-VP43SLSZ.mjs.map} +0 -0
- /package/dist/{chunk-MF2ZSLBV.mjs.map → chunk-XZFIJ7XF.mjs.map} +0 -0
|
@@ -1,23 +1,53 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import { act, render, screen, waitFor } from "@testing-library/react";
|
|
2
|
+
import { act, render, screen, waitFor, within } from "@testing-library/react";
|
|
3
3
|
import { useCoAgentStateRender } from "../use-coagent-state-render";
|
|
4
4
|
import { CoAgentStateRenderBridge } from "../use-coagent-state-render-bridge";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
import { useCopilotChatInternal } from "../use-copilot-chat_internal";
|
|
6
|
+
import { CoAgentStateRendersProvider, CopilotContext, useCoAgentStateRenders } from "../../context";
|
|
7
|
+
import type { Claim } from "../use-coagent-state-render-bridge.helpers";
|
|
8
|
+
import { createTestCopilotContext } from "../../test-helpers/copilot-context";
|
|
9
|
+
|
|
10
|
+
type TestMessage = {
|
|
11
|
+
id: string;
|
|
12
|
+
role: "user" | "assistant" | "system" | string;
|
|
13
|
+
content?: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type TestAgentSubscriber = {
|
|
17
|
+
onStateChanged?: (args?: { state?: Record<string, unknown> }) => void;
|
|
18
|
+
onStepStartedEvent?: (args: { event: { stepName: string } }) => void;
|
|
19
|
+
onStepFinishedEvent?: (args: { event: { stepName: string } }) => void;
|
|
20
|
+
};
|
|
10
21
|
|
|
11
22
|
const mockAgent = {
|
|
23
|
+
messages: [] as TestMessage[],
|
|
12
24
|
state: {},
|
|
13
25
|
isRunning: true,
|
|
14
26
|
subscribe: jest.fn(),
|
|
27
|
+
setMessages: jest.fn(),
|
|
28
|
+
setState: jest.fn(),
|
|
29
|
+
addMessage: jest.fn(),
|
|
30
|
+
abortRun: jest.fn(),
|
|
31
|
+
runAgent: jest.fn(),
|
|
15
32
|
};
|
|
16
33
|
|
|
17
|
-
let lastSubscriber:
|
|
34
|
+
let lastSubscriber: TestAgentSubscriber | null = null;
|
|
18
35
|
|
|
19
36
|
jest.mock("@copilotkitnext/react", () => ({
|
|
20
37
|
useAgent: jest.fn(() => ({ agent: mockAgent })),
|
|
38
|
+
useCopilotKit: jest.fn(() => ({
|
|
39
|
+
copilotkit: {
|
|
40
|
+
connectAgent: jest.fn(),
|
|
41
|
+
getRunIdForMessage: jest.fn(),
|
|
42
|
+
runAgent: jest.fn(),
|
|
43
|
+
clearSuggestions: jest.fn(),
|
|
44
|
+
addSuggestionsConfig: jest.fn(),
|
|
45
|
+
reloadSuggestions: jest.fn(),
|
|
46
|
+
},
|
|
47
|
+
})),
|
|
48
|
+
useCopilotChatConfiguration: jest.fn(() => ({ agentId: "test-agent" })),
|
|
49
|
+
useRenderCustomMessages: jest.fn(() => undefined),
|
|
50
|
+
useSuggestions: jest.fn(() => ({ suggestions: [], isLoading: false })),
|
|
21
51
|
}));
|
|
22
52
|
|
|
23
53
|
jest.mock("../../components/toast/toast-provider", () => ({
|
|
@@ -27,12 +57,22 @@ jest.mock("../../components/toast/toast-provider", () => ({
|
|
|
27
57
|
}),
|
|
28
58
|
}));
|
|
29
59
|
|
|
60
|
+
jest.mock("../../components/error-boundary/error-utils", () => ({
|
|
61
|
+
useAsyncCallback: <T extends (...args: unknown[]) => unknown>(fn: T) => fn,
|
|
62
|
+
}));
|
|
63
|
+
|
|
64
|
+
jest.mock("../use-langgraph-interrupt-render", () => ({
|
|
65
|
+
useLangGraphInterruptRender: jest.fn(() => null),
|
|
66
|
+
}));
|
|
67
|
+
|
|
68
|
+
jest.mock("../use-lazy-tool-renderer", () => ({
|
|
69
|
+
useLazyToolRenderer: jest.fn(() => () => null),
|
|
70
|
+
}));
|
|
71
|
+
|
|
30
72
|
function TestHarness({ snapshot }: { snapshot: string }) {
|
|
31
73
|
useCoAgentStateRender<{ current_step?: string }>({
|
|
32
74
|
name: "test-agent",
|
|
33
|
-
render: ({ state }) =>
|
|
34
|
-
<div data-testid="state">{state.current_step ?? "none"}</div>
|
|
35
|
-
),
|
|
75
|
+
render: ({ state }) => <div data-testid="state">{state.current_step ?? "none"}</div>,
|
|
36
76
|
});
|
|
37
77
|
|
|
38
78
|
return (
|
|
@@ -49,18 +89,10 @@ function TestHarness({ snapshot }: { snapshot: string }) {
|
|
|
49
89
|
);
|
|
50
90
|
}
|
|
51
91
|
|
|
52
|
-
function SnapshotHarness({
|
|
53
|
-
snapshot,
|
|
54
|
-
message,
|
|
55
|
-
}: {
|
|
56
|
-
snapshot: string;
|
|
57
|
-
message: { id: string; role: string; content?: string };
|
|
58
|
-
}) {
|
|
92
|
+
function SnapshotHarness({ snapshot, message }: { snapshot: string; message: TestMessage }) {
|
|
59
93
|
useCoAgentStateRender<{ current_step?: string }>({
|
|
60
94
|
name: "test-agent",
|
|
61
|
-
render: ({ state }) =>
|
|
62
|
-
<div data-testid="state">{state.current_step ?? "none"}</div>
|
|
63
|
-
),
|
|
95
|
+
render: ({ state }) => <div data-testid="state">{state.current_step ?? "none"}</div>,
|
|
64
96
|
});
|
|
65
97
|
|
|
66
98
|
return (
|
|
@@ -77,12 +109,10 @@ function SnapshotHarness({
|
|
|
77
109
|
);
|
|
78
110
|
}
|
|
79
111
|
|
|
80
|
-
function LiveStateHarness({ message }: { message:
|
|
112
|
+
function LiveStateHarness({ message }: { message: Pick<TestMessage, "id" | "role"> }) {
|
|
81
113
|
useCoAgentStateRender<{ current_step?: string }>({
|
|
82
114
|
name: "test-agent",
|
|
83
|
-
render: ({ state }) =>
|
|
84
|
-
<div data-testid="state">{state.current_step ?? "none"}</div>
|
|
85
|
-
),
|
|
115
|
+
render: ({ state }) => <div data-testid="state">{state.current_step ?? "none"}</div>,
|
|
86
116
|
});
|
|
87
117
|
|
|
88
118
|
return (
|
|
@@ -99,21 +129,99 @@ function LiveStateHarness({ message }: { message: { id: string; role: string } }
|
|
|
99
129
|
);
|
|
100
130
|
}
|
|
101
131
|
|
|
132
|
+
function NonFirstMessageHarness({ snapshot }: { snapshot: string }) {
|
|
133
|
+
useCoAgentStateRender<{ current_step?: string }>({
|
|
134
|
+
name: "test-agent",
|
|
135
|
+
render: ({ state }) => <div data-testid="state">{state.current_step ?? "none"}</div>,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
return (
|
|
139
|
+
<CoAgentStateRenderBridge
|
|
140
|
+
agentId="test-agent"
|
|
141
|
+
message={{ id: "msg-second", role: "assistant", content: "" }}
|
|
142
|
+
position="after"
|
|
143
|
+
runId="run-non-first"
|
|
144
|
+
messageIndex={1}
|
|
145
|
+
messageIndexInRun={1}
|
|
146
|
+
numberOfMessagesInRun={2}
|
|
147
|
+
stateSnapshot={snapshot}
|
|
148
|
+
/>
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function MultiRunHarness({
|
|
153
|
+
snapshot,
|
|
154
|
+
runId,
|
|
155
|
+
messageId,
|
|
156
|
+
messageIndex,
|
|
157
|
+
}: {
|
|
158
|
+
snapshot: string;
|
|
159
|
+
runId: string;
|
|
160
|
+
messageId: string;
|
|
161
|
+
messageIndex: number;
|
|
162
|
+
}) {
|
|
163
|
+
useCoAgentStateRender<{ current_step?: string }>({
|
|
164
|
+
name: "test-agent",
|
|
165
|
+
render: ({ state }) => <div data-testid="state">{state.current_step ?? "none"}</div>,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
return (
|
|
169
|
+
<CoAgentStateRenderBridge
|
|
170
|
+
agentId="test-agent"
|
|
171
|
+
message={{ id: messageId, role: "assistant", content: "" }}
|
|
172
|
+
position="after"
|
|
173
|
+
runId={runId}
|
|
174
|
+
messageIndex={messageIndex}
|
|
175
|
+
messageIndexInRun={0}
|
|
176
|
+
numberOfMessagesInRun={1}
|
|
177
|
+
stateSnapshot={snapshot}
|
|
178
|
+
/>
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function ClaimsObserver({ onChange }: { onChange: (claims: Record<string, Claim>) => void }) {
|
|
183
|
+
const { claimsRef } = useCoAgentStateRenders();
|
|
184
|
+
React.useEffect(() => {
|
|
185
|
+
onChange(claimsRef.current as Record<string, Claim>);
|
|
186
|
+
});
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function ChatHarness({ tick }: { tick: number }) {
|
|
191
|
+
useCoAgentStateRender<{ current_step?: string }>({
|
|
192
|
+
name: "test-agent",
|
|
193
|
+
render: ({ state }) => <div data-testid="state">{state.current_step ?? "none"}</div>,
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const { messages } = useCopilotChatInternal();
|
|
197
|
+
|
|
198
|
+
return (
|
|
199
|
+
<>
|
|
200
|
+
{messages.map((message) =>
|
|
201
|
+
message.generativeUI ? (
|
|
202
|
+
<div key={message.id} data-testid={`message-${message.id}`}>
|
|
203
|
+
{message.generativeUI()}
|
|
204
|
+
</div>
|
|
205
|
+
) : null,
|
|
206
|
+
)}
|
|
207
|
+
<div data-testid="tick">{tick}</div>
|
|
208
|
+
</>
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
102
212
|
describe("useCoAgentStateRender", () => {
|
|
103
213
|
beforeEach(() => {
|
|
104
214
|
lastSubscriber = null;
|
|
105
215
|
mockAgent.state = {};
|
|
106
|
-
mockAgent.
|
|
216
|
+
mockAgent.messages = [];
|
|
217
|
+
mockAgent.subscribe.mockImplementation((subscriber: TestAgentSubscriber) => {
|
|
107
218
|
lastSubscriber = subscriber;
|
|
108
219
|
return { unsubscribe: jest.fn() };
|
|
109
220
|
});
|
|
110
221
|
});
|
|
111
222
|
|
|
112
223
|
it("re-renders when state snapshots change", async () => {
|
|
113
|
-
const copilotContextValue =
|
|
114
|
-
chatComponentsCache: { current: { actions: {}, coAgentStateRenders: {} } },
|
|
115
|
-
availableAgents: [],
|
|
116
|
-
} as CopilotContextParams;
|
|
224
|
+
const copilotContextValue = createTestCopilotContext();
|
|
117
225
|
|
|
118
226
|
const { rerender } = render(
|
|
119
227
|
<CopilotContext.Provider value={copilotContextValue}>
|
|
@@ -141,10 +249,7 @@ describe("useCoAgentStateRender", () => {
|
|
|
141
249
|
});
|
|
142
250
|
|
|
143
251
|
it("renders snapshots that arrive before assistant text", async () => {
|
|
144
|
-
const copilotContextValue =
|
|
145
|
-
chatComponentsCache: { current: { actions: {}, coAgentStateRenders: {} } },
|
|
146
|
-
availableAgents: [],
|
|
147
|
-
} as CopilotContextParams;
|
|
252
|
+
const copilotContextValue = createTestCopilotContext();
|
|
148
253
|
|
|
149
254
|
const baseMessage = { id: "msg-early", role: "assistant", content: "" };
|
|
150
255
|
|
|
@@ -195,10 +300,7 @@ describe("useCoAgentStateRender", () => {
|
|
|
195
300
|
});
|
|
196
301
|
|
|
197
302
|
it("re-renders from live agent state updates", async () => {
|
|
198
|
-
const copilotContextValue =
|
|
199
|
-
chatComponentsCache: { current: { actions: {}, coAgentStateRenders: {} } },
|
|
200
|
-
availableAgents: [],
|
|
201
|
-
} as CopilotContextParams;
|
|
303
|
+
const copilotContextValue = createTestCopilotContext();
|
|
202
304
|
|
|
203
305
|
render(
|
|
204
306
|
<CopilotContext.Provider value={copilotContextValue}>
|
|
@@ -226,4 +328,757 @@ describe("useCoAgentStateRender", () => {
|
|
|
226
328
|
expect(screen.getByTestId("state").textContent).toBe("Thinking...");
|
|
227
329
|
});
|
|
228
330
|
});
|
|
331
|
+
|
|
332
|
+
it("renders state snapshots before any assistant message exists", async () => {
|
|
333
|
+
const copilotContextValue = createTestCopilotContext({
|
|
334
|
+
threadId: "thread-1",
|
|
335
|
+
agentSession: null,
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
mockAgent.messages = [{ id: "msg-user-1", role: "user", content: "Hi" }];
|
|
339
|
+
mockAgent.isRunning = false;
|
|
340
|
+
mockAgent.state = {};
|
|
341
|
+
|
|
342
|
+
const { rerender } = render(
|
|
343
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
344
|
+
<CoAgentStateRendersProvider>
|
|
345
|
+
<ChatHarness tick={0} />
|
|
346
|
+
</CoAgentStateRendersProvider>
|
|
347
|
+
</CopilotContext.Provider>,
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
const placeholderTestId = "message-coagent-state-render-test-agent-pending:msg-user-1";
|
|
351
|
+
expect(screen.queryByTestId(placeholderTestId)).toBeNull();
|
|
352
|
+
|
|
353
|
+
mockAgent.isRunning = true;
|
|
354
|
+
mockAgent.state = { current_step: "Processing..." };
|
|
355
|
+
|
|
356
|
+
rerender(
|
|
357
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
358
|
+
<CoAgentStateRendersProvider>
|
|
359
|
+
<ChatHarness tick={1} />
|
|
360
|
+
</CoAgentStateRendersProvider>
|
|
361
|
+
</CopilotContext.Provider>,
|
|
362
|
+
);
|
|
363
|
+
|
|
364
|
+
await waitFor(() => {
|
|
365
|
+
expect(screen.getByTestId("state").textContent).toBe("Processing...");
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
expect(screen.getByTestId(placeholderTestId)).toBeTruthy();
|
|
369
|
+
|
|
370
|
+
const placeholderStep = screen.getByTestId("state").textContent;
|
|
371
|
+
|
|
372
|
+
mockAgent.isRunning = false;
|
|
373
|
+
mockAgent.state = {};
|
|
374
|
+
mockAgent.messages = [
|
|
375
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
376
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
377
|
+
];
|
|
378
|
+
|
|
379
|
+
rerender(
|
|
380
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
381
|
+
<CoAgentStateRendersProvider>
|
|
382
|
+
<ChatHarness tick={2} />
|
|
383
|
+
</CoAgentStateRendersProvider>
|
|
384
|
+
</CopilotContext.Provider>,
|
|
385
|
+
);
|
|
386
|
+
|
|
387
|
+
await waitFor(() => {
|
|
388
|
+
expect(screen.queryByTestId(placeholderTestId)).toBeNull();
|
|
389
|
+
expect(screen.getByTestId("message-msg-assistant-1")).toBeTruthy();
|
|
390
|
+
expect(screen.getByTestId("state").textContent).toBe("none");
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it("does not render placeholder until the agent is running or has state", async () => {
|
|
395
|
+
const copilotContextValue = createTestCopilotContext({
|
|
396
|
+
threadId: "thread-1",
|
|
397
|
+
agentSession: null,
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
mockAgent.messages = [{ id: "msg-user-1", role: "user", content: "Hi" }];
|
|
401
|
+
mockAgent.isRunning = false;
|
|
402
|
+
mockAgent.state = {};
|
|
403
|
+
|
|
404
|
+
render(
|
|
405
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
406
|
+
<CoAgentStateRendersProvider>
|
|
407
|
+
<ChatHarness tick={0} />
|
|
408
|
+
</CoAgentStateRendersProvider>
|
|
409
|
+
</CopilotContext.Provider>,
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
await waitFor(() => {
|
|
413
|
+
expect(
|
|
414
|
+
screen.queryByTestId("message-coagent-state-render-test-agent-pending:msg-user-1"),
|
|
415
|
+
).toBeNull();
|
|
416
|
+
});
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it("renders for non-first messages in a run", async () => {
|
|
420
|
+
const copilotContextValue = createTestCopilotContext();
|
|
421
|
+
|
|
422
|
+
render(
|
|
423
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
424
|
+
<CoAgentStateRendersProvider>
|
|
425
|
+
<NonFirstMessageHarness snapshot={JSON.stringify({ current_step: "Processing..." })} />
|
|
426
|
+
</CoAgentStateRendersProvider>
|
|
427
|
+
</CopilotContext.Provider>,
|
|
428
|
+
);
|
|
429
|
+
|
|
430
|
+
await waitFor(() => {
|
|
431
|
+
expect(screen.getByTestId("state").textContent).toBe("Processing...");
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it("falls back to legacy renderer when renderCustomMessages throws", async () => {
|
|
436
|
+
const { useRenderCustomMessages } = jest.requireMock("@copilotkitnext/react");
|
|
437
|
+
useRenderCustomMessages.mockImplementationOnce(() => () => {
|
|
438
|
+
throw new Error("boom");
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
const copilotContextValue = createTestCopilotContext({
|
|
442
|
+
threadId: "thread-1",
|
|
443
|
+
agentSession: null,
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
mockAgent.messages = [
|
|
447
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
448
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
449
|
+
];
|
|
450
|
+
mockAgent.isRunning = true;
|
|
451
|
+
mockAgent.state = { current_step: "Processing..." };
|
|
452
|
+
|
|
453
|
+
render(
|
|
454
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
455
|
+
<CoAgentStateRendersProvider>
|
|
456
|
+
<ChatHarness tick={0} />
|
|
457
|
+
</CoAgentStateRendersProvider>
|
|
458
|
+
</CopilotContext.Provider>,
|
|
459
|
+
);
|
|
460
|
+
|
|
461
|
+
await waitFor(() => {
|
|
462
|
+
expect(screen.getByTestId("state").textContent).toBe("Processing...");
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it("prefers legacy renderer over renderCustomMessages when both exist", async () => {
|
|
467
|
+
const { useRenderCustomMessages } = jest.requireMock("@copilotkitnext/react");
|
|
468
|
+
const renderCustomSpy = jest.fn(() => null);
|
|
469
|
+
useRenderCustomMessages.mockImplementationOnce(() => renderCustomSpy);
|
|
470
|
+
|
|
471
|
+
const copilotContextValue = createTestCopilotContext({
|
|
472
|
+
threadId: "thread-1",
|
|
473
|
+
agentSession: null,
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
mockAgent.messages = [
|
|
477
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
478
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
479
|
+
];
|
|
480
|
+
mockAgent.isRunning = true;
|
|
481
|
+
mockAgent.state = { current_step: "Processing..." };
|
|
482
|
+
|
|
483
|
+
render(
|
|
484
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
485
|
+
<CoAgentStateRendersProvider>
|
|
486
|
+
<ChatHarness tick={0} />
|
|
487
|
+
</CoAgentStateRendersProvider>
|
|
488
|
+
</CopilotContext.Provider>,
|
|
489
|
+
);
|
|
490
|
+
|
|
491
|
+
await waitFor(() => {
|
|
492
|
+
expect(screen.getByTestId("state").textContent).toBe("Processing...");
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
expect(renderCustomSpy).not.toHaveBeenCalled();
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
it("renders empty state when agent state clears after completion", async () => {
|
|
499
|
+
const copilotContextValue = createTestCopilotContext({
|
|
500
|
+
threadId: "thread-1",
|
|
501
|
+
agentSession: null,
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
mockAgent.messages = [
|
|
505
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
506
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
507
|
+
];
|
|
508
|
+
mockAgent.isRunning = true;
|
|
509
|
+
mockAgent.state = { current_step: "Processing..." };
|
|
510
|
+
|
|
511
|
+
const { rerender } = render(
|
|
512
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
513
|
+
<CoAgentStateRendersProvider>
|
|
514
|
+
<ChatHarness tick={0} />
|
|
515
|
+
</CoAgentStateRendersProvider>
|
|
516
|
+
</CopilotContext.Provider>,
|
|
517
|
+
);
|
|
518
|
+
|
|
519
|
+
await waitFor(() => {
|
|
520
|
+
expect(screen.getByTestId("state").textContent).toBe("Processing...");
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
mockAgent.isRunning = false;
|
|
524
|
+
mockAgent.state = {};
|
|
525
|
+
|
|
526
|
+
rerender(
|
|
527
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
528
|
+
<CoAgentStateRendersProvider>
|
|
529
|
+
<ChatHarness tick={1} />
|
|
530
|
+
</CoAgentStateRendersProvider>
|
|
531
|
+
</CopilotContext.Provider>,
|
|
532
|
+
);
|
|
533
|
+
|
|
534
|
+
await waitFor(() => {
|
|
535
|
+
expect(screen.getByTestId("state").textContent).toBe("none");
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
it("allows independent renders across different runs with the same snapshot", async () => {
|
|
540
|
+
const copilotContextValue = createTestCopilotContext();
|
|
541
|
+
|
|
542
|
+
const snapshot = JSON.stringify({ current_step: "Processing..." });
|
|
543
|
+
|
|
544
|
+
render(
|
|
545
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
546
|
+
<CoAgentStateRendersProvider>
|
|
547
|
+
<MultiRunHarness snapshot={snapshot} runId="run-1" messageId="msg-1" messageIndex={0} />
|
|
548
|
+
<MultiRunHarness snapshot={snapshot} runId="run-2" messageId="msg-2" messageIndex={1} />
|
|
549
|
+
</CoAgentStateRendersProvider>
|
|
550
|
+
</CopilotContext.Provider>,
|
|
551
|
+
);
|
|
552
|
+
|
|
553
|
+
await waitFor(() => {
|
|
554
|
+
const nodes = screen.getAllByTestId("state");
|
|
555
|
+
expect(nodes).toHaveLength(2);
|
|
556
|
+
expect(nodes[0].textContent).toBe("Processing...");
|
|
557
|
+
expect(nodes[1].textContent).toBe("Processing...");
|
|
558
|
+
});
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
it("lets newer assistant messages claim even if snapshot matches", async () => {
|
|
562
|
+
const copilotContextValue = createTestCopilotContext();
|
|
563
|
+
|
|
564
|
+
const snapshot = JSON.stringify({ current_step: "Processing..." });
|
|
565
|
+
|
|
566
|
+
render(
|
|
567
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
568
|
+
<CoAgentStateRendersProvider>
|
|
569
|
+
<MultiRunHarness snapshot={snapshot} runId="run-1" messageId="msg-1" messageIndex={0} />
|
|
570
|
+
<MultiRunHarness snapshot={snapshot} runId="run-1" messageId="msg-2" messageIndex={1} />
|
|
571
|
+
</CoAgentStateRendersProvider>
|
|
572
|
+
</CopilotContext.Provider>,
|
|
573
|
+
);
|
|
574
|
+
|
|
575
|
+
await waitFor(() => {
|
|
576
|
+
const nodes = screen.getAllByTestId("state");
|
|
577
|
+
expect(nodes).toHaveLength(2);
|
|
578
|
+
expect(nodes[0].textContent).toBe("Processing...");
|
|
579
|
+
expect(nodes[1].textContent).toBe("Processing...");
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it("locks older claims when a newer message claims", async () => {
|
|
584
|
+
const copilotContextValue = createTestCopilotContext();
|
|
585
|
+
|
|
586
|
+
const snapshot = JSON.stringify({ current_step: "Processing..." });
|
|
587
|
+
let latestClaims: Record<string, Claim> = {};
|
|
588
|
+
|
|
589
|
+
render(
|
|
590
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
591
|
+
<CoAgentStateRendersProvider>
|
|
592
|
+
<ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
|
|
593
|
+
<MultiRunHarness snapshot={snapshot} runId="run-1" messageId="msg-1" messageIndex={0} />
|
|
594
|
+
<MultiRunHarness snapshot={snapshot} runId="run-1" messageId="msg-2" messageIndex={1} />
|
|
595
|
+
</CoAgentStateRendersProvider>
|
|
596
|
+
</CopilotContext.Provider>,
|
|
597
|
+
);
|
|
598
|
+
|
|
599
|
+
await waitFor(() => {
|
|
600
|
+
expect(latestClaims["msg-1"]).toBeTruthy();
|
|
601
|
+
expect(latestClaims["msg-2"]).toBeTruthy();
|
|
602
|
+
expect(latestClaims["msg-1"].locked).toBe(true);
|
|
603
|
+
});
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
it("keeps older message snapshots stable when a new run starts", async () => {
|
|
607
|
+
const copilotContextValue = createTestCopilotContext();
|
|
608
|
+
|
|
609
|
+
const snapshot = JSON.stringify({ current_step: "Processing..." });
|
|
610
|
+
let latestClaims: Record<string, Claim> = {};
|
|
611
|
+
|
|
612
|
+
const { rerender } = render(
|
|
613
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
614
|
+
<CoAgentStateRendersProvider>
|
|
615
|
+
<ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
|
|
616
|
+
<MultiRunHarness snapshot={snapshot} runId="run-1" messageId="msg-1" messageIndex={0} />
|
|
617
|
+
</CoAgentStateRendersProvider>
|
|
618
|
+
</CopilotContext.Provider>,
|
|
619
|
+
);
|
|
620
|
+
|
|
621
|
+
await waitFor(() => {
|
|
622
|
+
expect(latestClaims["msg-1"]?.stateSnapshot?.current_step).toBe("Processing...");
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
rerender(
|
|
626
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
627
|
+
<CoAgentStateRendersProvider>
|
|
628
|
+
<ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
|
|
629
|
+
<MultiRunHarness snapshot={snapshot} runId="run-1" messageId="msg-1" messageIndex={0} />
|
|
630
|
+
<MultiRunHarness snapshot={snapshot} runId="run-2" messageId="msg-2" messageIndex={1} />
|
|
631
|
+
</CoAgentStateRendersProvider>
|
|
632
|
+
</CopilotContext.Provider>,
|
|
633
|
+
);
|
|
634
|
+
|
|
635
|
+
await waitFor(() => {
|
|
636
|
+
expect(latestClaims["msg-1"]?.stateSnapshot?.current_step).toBe("Processing...");
|
|
637
|
+
expect(latestClaims["msg-2"]?.stateSnapshot?.current_step).toBe("Processing...");
|
|
638
|
+
});
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
it("does not overwrite a previous run snapshot when a new run has different state", async () => {
|
|
642
|
+
const copilotContextValue = createTestCopilotContext();
|
|
643
|
+
|
|
644
|
+
const snapshotRun1 = JSON.stringify({ current_step: "Processing..." });
|
|
645
|
+
const snapshotRun2 = JSON.stringify({ current_step: "Finalizing..." });
|
|
646
|
+
let latestClaims: Record<string, Claim> = {};
|
|
647
|
+
|
|
648
|
+
const { rerender } = render(
|
|
649
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
650
|
+
<CoAgentStateRendersProvider>
|
|
651
|
+
<ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
|
|
652
|
+
<MultiRunHarness
|
|
653
|
+
snapshot={snapshotRun1}
|
|
654
|
+
runId="run-1"
|
|
655
|
+
messageId="msg-1"
|
|
656
|
+
messageIndex={0}
|
|
657
|
+
/>
|
|
658
|
+
</CoAgentStateRendersProvider>
|
|
659
|
+
</CopilotContext.Provider>,
|
|
660
|
+
);
|
|
661
|
+
|
|
662
|
+
await waitFor(() => {
|
|
663
|
+
expect(latestClaims["msg-1"]?.stateSnapshot?.current_step).toBe("Processing...");
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
rerender(
|
|
667
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
668
|
+
<CoAgentStateRendersProvider>
|
|
669
|
+
<ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
|
|
670
|
+
<MultiRunHarness
|
|
671
|
+
snapshot={snapshotRun1}
|
|
672
|
+
runId="run-1"
|
|
673
|
+
messageId="msg-1"
|
|
674
|
+
messageIndex={0}
|
|
675
|
+
/>
|
|
676
|
+
<MultiRunHarness
|
|
677
|
+
snapshot={snapshotRun2}
|
|
678
|
+
runId="run-2"
|
|
679
|
+
messageId="msg-2"
|
|
680
|
+
messageIndex={1}
|
|
681
|
+
/>
|
|
682
|
+
</CoAgentStateRendersProvider>
|
|
683
|
+
</CopilotContext.Provider>,
|
|
684
|
+
);
|
|
685
|
+
|
|
686
|
+
await waitFor(() => {
|
|
687
|
+
expect(latestClaims["msg-1"]?.stateSnapshot?.current_step).toBe("Processing...");
|
|
688
|
+
expect(latestClaims["msg-2"]?.stateSnapshot?.current_step).toBe("Finalizing...");
|
|
689
|
+
});
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
it("locks a claim when newer agent messages exist", async () => {
|
|
693
|
+
const copilotContextValue = createTestCopilotContext();
|
|
694
|
+
|
|
695
|
+
const snapshot = JSON.stringify({ current_step: "Processing..." });
|
|
696
|
+
let latestClaims: Record<string, Claim> = {};
|
|
697
|
+
|
|
698
|
+
mockAgent.messages = [
|
|
699
|
+
{ id: "msg-1", role: "assistant", content: "" },
|
|
700
|
+
{ id: "msg-2", role: "assistant", content: "" },
|
|
701
|
+
];
|
|
702
|
+
|
|
703
|
+
render(
|
|
704
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
705
|
+
<CoAgentStateRendersProvider>
|
|
706
|
+
<ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
|
|
707
|
+
<MultiRunHarness snapshot={snapshot} runId="run-1" messageId="msg-1" messageIndex={0} />
|
|
708
|
+
</CoAgentStateRendersProvider>
|
|
709
|
+
</CopilotContext.Provider>,
|
|
710
|
+
);
|
|
711
|
+
|
|
712
|
+
await waitFor(() => {
|
|
713
|
+
expect(latestClaims["msg-1"]).toBeTruthy();
|
|
714
|
+
expect(latestClaims["msg-1"].locked).toBe(true);
|
|
715
|
+
});
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
it("does not update a locked claim from new agent state", async () => {
|
|
719
|
+
const copilotContextValue = createTestCopilotContext({
|
|
720
|
+
threadId: "thread-1",
|
|
721
|
+
agentSession: null,
|
|
722
|
+
});
|
|
723
|
+
|
|
724
|
+
let latestClaims: Record<string, Claim> = {};
|
|
725
|
+
mockAgent.messages = [
|
|
726
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
727
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
728
|
+
];
|
|
729
|
+
mockAgent.isRunning = true;
|
|
730
|
+
mockAgent.state = { current_step: "First" };
|
|
731
|
+
|
|
732
|
+
const { rerender } = render(
|
|
733
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
734
|
+
<CoAgentStateRendersProvider>
|
|
735
|
+
<ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
|
|
736
|
+
<ChatHarness tick={0} />
|
|
737
|
+
</CoAgentStateRendersProvider>
|
|
738
|
+
</CopilotContext.Provider>,
|
|
739
|
+
);
|
|
740
|
+
|
|
741
|
+
await waitFor(() => {
|
|
742
|
+
expect(latestClaims["msg-assistant-1"]?.stateSnapshot?.current_step).toBe("First");
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
mockAgent.messages = [
|
|
746
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
747
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
748
|
+
{ id: "msg-user-2", role: "user", content: "Next" },
|
|
749
|
+
];
|
|
750
|
+
mockAgent.state = { current_step: "Second" };
|
|
751
|
+
|
|
752
|
+
rerender(
|
|
753
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
754
|
+
<CoAgentStateRendersProvider>
|
|
755
|
+
<ClaimsObserver onChange={(claims) => (latestClaims = claims)} />
|
|
756
|
+
<ChatHarness tick={1} />
|
|
757
|
+
</CoAgentStateRendersProvider>
|
|
758
|
+
</CopilotContext.Provider>,
|
|
759
|
+
);
|
|
760
|
+
|
|
761
|
+
await waitFor(() => {
|
|
762
|
+
expect(latestClaims["msg-assistant-1"]?.stateSnapshot?.current_step).toBe("First");
|
|
763
|
+
});
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
it("renders new run snapshots instead of reusing the previous run state", async () => {
|
|
767
|
+
const copilotContextValue = createTestCopilotContext({
|
|
768
|
+
threadId: "thread-1",
|
|
769
|
+
agentSession: null,
|
|
770
|
+
});
|
|
771
|
+
|
|
772
|
+
mockAgent.messages = [
|
|
773
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
774
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
775
|
+
];
|
|
776
|
+
mockAgent.isRunning = true;
|
|
777
|
+
mockAgent.state = { current_step: "First run" };
|
|
778
|
+
|
|
779
|
+
const { rerender } = render(
|
|
780
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
781
|
+
<CoAgentStateRendersProvider>
|
|
782
|
+
<ChatHarness tick={0} />
|
|
783
|
+
</CoAgentStateRendersProvider>
|
|
784
|
+
</CopilotContext.Provider>,
|
|
785
|
+
);
|
|
786
|
+
|
|
787
|
+
await waitFor(() => {
|
|
788
|
+
expect(screen.getByTestId("state").textContent).toBe("First run");
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
mockAgent.messages = [
|
|
792
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
793
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
794
|
+
{ id: "msg-user-2", role: "user", content: "Next" },
|
|
795
|
+
];
|
|
796
|
+
mockAgent.isRunning = true;
|
|
797
|
+
mockAgent.state = { current_step: "Second run" };
|
|
798
|
+
|
|
799
|
+
rerender(
|
|
800
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
801
|
+
<CoAgentStateRendersProvider>
|
|
802
|
+
<ChatHarness tick={1} />
|
|
803
|
+
</CoAgentStateRendersProvider>
|
|
804
|
+
</CopilotContext.Provider>,
|
|
805
|
+
);
|
|
806
|
+
|
|
807
|
+
await waitFor(() => {
|
|
808
|
+
const stateNodes = screen.getAllByTestId("state");
|
|
809
|
+
const values = stateNodes.map((node) => node.textContent);
|
|
810
|
+
expect(values).toContain("Second run");
|
|
811
|
+
});
|
|
812
|
+
});
|
|
813
|
+
|
|
814
|
+
it("does not reuse latest cached snapshot for a new run placeholder before snapshots arrive", async () => {
|
|
815
|
+
const copilotContextValue = createTestCopilotContext({
|
|
816
|
+
threadId: "thread-1",
|
|
817
|
+
agentSession: null,
|
|
818
|
+
});
|
|
819
|
+
|
|
820
|
+
mockAgent.messages = [
|
|
821
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
822
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
823
|
+
];
|
|
824
|
+
mockAgent.isRunning = true;
|
|
825
|
+
mockAgent.state = { current_step: "First run" };
|
|
826
|
+
|
|
827
|
+
const { rerender } = render(
|
|
828
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
829
|
+
<CoAgentStateRendersProvider>
|
|
830
|
+
<ChatHarness tick={0} />
|
|
831
|
+
</CoAgentStateRendersProvider>
|
|
832
|
+
</CopilotContext.Provider>,
|
|
833
|
+
);
|
|
834
|
+
|
|
835
|
+
await waitFor(() => {
|
|
836
|
+
expect(screen.getByTestId("state").textContent).toBe("First run");
|
|
837
|
+
});
|
|
838
|
+
|
|
839
|
+
mockAgent.messages = [
|
|
840
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
841
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
842
|
+
{ id: "msg-user-2", role: "user", content: "Next" },
|
|
843
|
+
];
|
|
844
|
+
mockAgent.isRunning = true;
|
|
845
|
+
mockAgent.state = {};
|
|
846
|
+
|
|
847
|
+
rerender(
|
|
848
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
849
|
+
<CoAgentStateRendersProvider>
|
|
850
|
+
<ChatHarness tick={1} />
|
|
851
|
+
</CoAgentStateRendersProvider>
|
|
852
|
+
</CopilotContext.Provider>,
|
|
853
|
+
);
|
|
854
|
+
|
|
855
|
+
await waitFor(() => {
|
|
856
|
+
const placeholder = screen.getByTestId(
|
|
857
|
+
"message-coagent-state-render-test-agent-pending:msg-user-2",
|
|
858
|
+
);
|
|
859
|
+
expect(placeholder.textContent).toContain("none");
|
|
860
|
+
});
|
|
861
|
+
});
|
|
862
|
+
|
|
863
|
+
it("does not show the previous snapshot for a new assistant message before its snapshot arrives", async () => {
|
|
864
|
+
const copilotContextValue = createTestCopilotContext({
|
|
865
|
+
threadId: "thread-1",
|
|
866
|
+
agentSession: null,
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
mockAgent.messages = [
|
|
870
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
871
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
872
|
+
];
|
|
873
|
+
mockAgent.isRunning = true;
|
|
874
|
+
mockAgent.state = { current_step: "First run" };
|
|
875
|
+
|
|
876
|
+
const { rerender } = render(
|
|
877
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
878
|
+
<CoAgentStateRendersProvider>
|
|
879
|
+
<ChatHarness tick={0} />
|
|
880
|
+
</CoAgentStateRendersProvider>
|
|
881
|
+
</CopilotContext.Provider>,
|
|
882
|
+
);
|
|
883
|
+
|
|
884
|
+
await waitFor(() => {
|
|
885
|
+
const message = screen.getByTestId("message-msg-assistant-1");
|
|
886
|
+
expect(within(message).getByTestId("state").textContent).toBe("First run");
|
|
887
|
+
});
|
|
888
|
+
|
|
889
|
+
mockAgent.messages = [
|
|
890
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
891
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
892
|
+
{ id: "msg-user-2", role: "user", content: "Next" },
|
|
893
|
+
{ id: "msg-assistant-2", role: "assistant", content: "" },
|
|
894
|
+
];
|
|
895
|
+
mockAgent.isRunning = true;
|
|
896
|
+
mockAgent.state = {};
|
|
897
|
+
|
|
898
|
+
rerender(
|
|
899
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
900
|
+
<CoAgentStateRendersProvider>
|
|
901
|
+
<ChatHarness tick={1} />
|
|
902
|
+
</CoAgentStateRendersProvider>
|
|
903
|
+
</CopilotContext.Provider>,
|
|
904
|
+
);
|
|
905
|
+
|
|
906
|
+
await waitFor(() => {
|
|
907
|
+
const message = screen.getByTestId("message-msg-assistant-2");
|
|
908
|
+
expect(within(message).getByTestId("state").textContent).toBe("none");
|
|
909
|
+
});
|
|
910
|
+
|
|
911
|
+
mockAgent.messages = [
|
|
912
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
913
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
914
|
+
{ id: "msg-user-2", role: "user", content: "Next" },
|
|
915
|
+
{ id: "msg-assistant-2", role: "assistant", content: "", state: "{\"current_step\":\"Second run\"}" },
|
|
916
|
+
];
|
|
917
|
+
|
|
918
|
+
rerender(
|
|
919
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
920
|
+
<CoAgentStateRendersProvider>
|
|
921
|
+
<ChatHarness tick={2} />
|
|
922
|
+
</CoAgentStateRendersProvider>
|
|
923
|
+
</CopilotContext.Provider>,
|
|
924
|
+
);
|
|
925
|
+
|
|
926
|
+
await waitFor(() => {
|
|
927
|
+
const message = screen.getByTestId("message-msg-assistant-2");
|
|
928
|
+
expect(within(message).getByTestId("state").textContent).toBe("Second run");
|
|
929
|
+
});
|
|
930
|
+
});
|
|
931
|
+
|
|
932
|
+
it("does not show previous live state for a new run before the run updates state", async () => {
|
|
933
|
+
const copilotContextValue = createTestCopilotContext({
|
|
934
|
+
threadId: "thread-1",
|
|
935
|
+
agentSession: null,
|
|
936
|
+
});
|
|
937
|
+
|
|
938
|
+
mockAgent.messages = [
|
|
939
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
940
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
941
|
+
];
|
|
942
|
+
mockAgent.isRunning = true;
|
|
943
|
+
mockAgent.state = { current_step: "First run" };
|
|
944
|
+
|
|
945
|
+
const { rerender } = render(
|
|
946
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
947
|
+
<CoAgentStateRendersProvider>
|
|
948
|
+
<ChatHarness tick={0} />
|
|
949
|
+
</CoAgentStateRendersProvider>
|
|
950
|
+
</CopilotContext.Provider>,
|
|
951
|
+
);
|
|
952
|
+
|
|
953
|
+
await waitFor(() => {
|
|
954
|
+
const message = screen.getByTestId("message-msg-assistant-1");
|
|
955
|
+
expect(within(message).getByTestId("state").textContent).toBe("First run");
|
|
956
|
+
});
|
|
957
|
+
|
|
958
|
+
mockAgent.messages = [
|
|
959
|
+
{ id: "msg-user-1", role: "user", content: "Hi" },
|
|
960
|
+
{ id: "msg-assistant-1", role: "assistant", content: "" },
|
|
961
|
+
{ id: "msg-user-2", role: "user", content: "Next" },
|
|
962
|
+
{ id: "msg-assistant-2", role: "assistant", content: "" },
|
|
963
|
+
];
|
|
964
|
+
mockAgent.isRunning = true;
|
|
965
|
+
// live state still shows the previous run
|
|
966
|
+
mockAgent.state = { current_step: "First run" };
|
|
967
|
+
|
|
968
|
+
rerender(
|
|
969
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
970
|
+
<CoAgentStateRendersProvider>
|
|
971
|
+
<ChatHarness tick={1} />
|
|
972
|
+
</CoAgentStateRendersProvider>
|
|
973
|
+
</CopilotContext.Provider>,
|
|
974
|
+
);
|
|
975
|
+
|
|
976
|
+
await waitFor(() => {
|
|
977
|
+
const message = screen.getByTestId("message-msg-assistant-2");
|
|
978
|
+
expect(within(message).getByTestId("state").textContent).toBe("none");
|
|
979
|
+
});
|
|
980
|
+
|
|
981
|
+
mockAgent.state = { current_step: "Second run" };
|
|
982
|
+
act(() => {
|
|
983
|
+
lastSubscriber?.onStateChanged?.({ state: mockAgent.state });
|
|
984
|
+
});
|
|
985
|
+
|
|
986
|
+
await waitFor(() => {
|
|
987
|
+
const message = screen.getByTestId("message-msg-assistant-2");
|
|
988
|
+
expect(within(message).getByTestId("state").textContent).toBe("Second run");
|
|
989
|
+
});
|
|
990
|
+
});
|
|
991
|
+
|
|
992
|
+
it("renders an explicit empty state snapshot", async () => {
|
|
993
|
+
const copilotContextValue = createTestCopilotContext({
|
|
994
|
+
threadId: "thread-1",
|
|
995
|
+
agentSession: null,
|
|
996
|
+
});
|
|
997
|
+
|
|
998
|
+
mockAgent.messages = [
|
|
999
|
+
{
|
|
1000
|
+
id: "msg-user-1",
|
|
1001
|
+
role: "user",
|
|
1002
|
+
content: "Hi",
|
|
1003
|
+
},
|
|
1004
|
+
{
|
|
1005
|
+
id: "msg-assistant-1",
|
|
1006
|
+
role: "assistant",
|
|
1007
|
+
content: "",
|
|
1008
|
+
state: "{\"current_step\":\"Processing...\"}",
|
|
1009
|
+
},
|
|
1010
|
+
];
|
|
1011
|
+
mockAgent.isRunning = true;
|
|
1012
|
+
mockAgent.state = { current_step: "Processing..." };
|
|
1013
|
+
|
|
1014
|
+
const { rerender } = render(
|
|
1015
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
1016
|
+
<CoAgentStateRendersProvider>
|
|
1017
|
+
<ChatHarness tick={0} />
|
|
1018
|
+
</CoAgentStateRendersProvider>
|
|
1019
|
+
</CopilotContext.Provider>,
|
|
1020
|
+
);
|
|
1021
|
+
|
|
1022
|
+
await waitFor(() => {
|
|
1023
|
+
const message = screen.getByTestId("message-msg-assistant-1");
|
|
1024
|
+
expect(within(message).getByTestId("state").textContent).toBe("Processing...");
|
|
1025
|
+
});
|
|
1026
|
+
|
|
1027
|
+
mockAgent.messages = [
|
|
1028
|
+
{
|
|
1029
|
+
id: "msg-user-1",
|
|
1030
|
+
role: "user",
|
|
1031
|
+
content: "Hi",
|
|
1032
|
+
},
|
|
1033
|
+
{
|
|
1034
|
+
id: "msg-assistant-1",
|
|
1035
|
+
role: "assistant",
|
|
1036
|
+
content: "",
|
|
1037
|
+
state: "{}",
|
|
1038
|
+
},
|
|
1039
|
+
];
|
|
1040
|
+
|
|
1041
|
+
rerender(
|
|
1042
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
1043
|
+
<CoAgentStateRendersProvider>
|
|
1044
|
+
<ChatHarness tick={1} />
|
|
1045
|
+
</CoAgentStateRendersProvider>
|
|
1046
|
+
</CopilotContext.Provider>,
|
|
1047
|
+
);
|
|
1048
|
+
|
|
1049
|
+
await waitFor(() => {
|
|
1050
|
+
const message = screen.getByTestId("message-msg-assistant-1");
|
|
1051
|
+
expect(within(message).getByTestId("state").textContent).toBe("none");
|
|
1052
|
+
});
|
|
1053
|
+
});
|
|
1054
|
+
|
|
1055
|
+
it("renders an empty live state update", async () => {
|
|
1056
|
+
const copilotContextValue = createTestCopilotContext();
|
|
1057
|
+
|
|
1058
|
+
render(
|
|
1059
|
+
<CopilotContext.Provider value={copilotContextValue}>
|
|
1060
|
+
<CoAgentStateRendersProvider>
|
|
1061
|
+
<LiveStateHarness message={{ id: "msg-live-empty", role: "assistant" }} />
|
|
1062
|
+
</CoAgentStateRendersProvider>
|
|
1063
|
+
</CopilotContext.Provider>,
|
|
1064
|
+
);
|
|
1065
|
+
|
|
1066
|
+
mockAgent.state = { current_step: "Processing..." };
|
|
1067
|
+
act(() => {
|
|
1068
|
+
lastSubscriber?.onStateChanged?.({ state: mockAgent.state });
|
|
1069
|
+
});
|
|
1070
|
+
|
|
1071
|
+
await waitFor(() => {
|
|
1072
|
+
expect(screen.getByTestId("state").textContent).toBe("Processing...");
|
|
1073
|
+
});
|
|
1074
|
+
|
|
1075
|
+
mockAgent.state = {};
|
|
1076
|
+
act(() => {
|
|
1077
|
+
lastSubscriber?.onStateChanged?.({ state: mockAgent.state });
|
|
1078
|
+
});
|
|
1079
|
+
|
|
1080
|
+
await waitFor(() => {
|
|
1081
|
+
expect(screen.getByTestId("state").textContent).toBe("none");
|
|
1082
|
+
});
|
|
1083
|
+
});
|
|
229
1084
|
});
|