@copilotkit/react-core 1.57.0 → 1.57.1
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/dist/{copilotkit-DFaI4j2r.d.mts → copilotkit-BN4I_y1n.d.mts} +64 -8
- package/dist/copilotkit-BN4I_y1n.d.mts.map +1 -0
- package/dist/{copilotkit-DGbvw8n2.cjs → copilotkit-C3k13WZn.cjs} +572 -435
- package/dist/copilotkit-C3k13WZn.cjs.map +1 -0
- package/dist/{copilotkit-CPe2-340.mjs → copilotkit-DjxXMYHG.mjs} +571 -440
- package/dist/copilotkit-DjxXMYHG.mjs.map +1 -0
- package/dist/{copilotkit-Dg4r4Gi_.d.cts → copilotkit-sQWiKtxA.d.cts} +64 -8
- package/dist/copilotkit-sQWiKtxA.d.cts.map +1 -0
- package/dist/index.cjs +2 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +2 -5
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +449 -502
- package/dist/index.umd.js.map +1 -1
- package/dist/v2/context.cjs +135 -0
- package/dist/v2/context.cjs.map +1 -0
- package/dist/v2/context.d.cts +148 -0
- package/dist/v2/context.d.cts.map +1 -0
- package/dist/v2/context.d.mts +148 -0
- package/dist/v2/context.d.mts.map +1 -0
- package/dist/v2/context.mjs +129 -0
- package/dist/v2/context.mjs.map +1 -0
- package/dist/v2/headless.cjs +1043 -0
- package/dist/v2/headless.cjs.map +1 -0
- package/dist/v2/headless.d.cts +605 -0
- package/dist/v2/headless.d.cts.map +1 -0
- package/dist/v2/headless.d.mts +512 -0
- package/dist/v2/headless.d.mts.map +1 -0
- package/dist/v2/headless.mjs +997 -0
- package/dist/v2/headless.mjs.map +1 -0
- package/dist/v2/index.cjs +2 -1
- package/dist/v2/index.css +1 -1
- package/dist/v2/index.d.cts +2 -2
- package/dist/v2/index.d.mts +2 -2
- package/dist/v2/index.mjs +2 -2
- package/dist/v2/index.umd.js +584 -441
- package/dist/v2/index.umd.js.map +1 -1
- package/package.json +14 -6
- package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +5 -6
- package/src/hooks/use-copilot-chat_internal.ts +0 -1
- package/src/v2/components/chat/CopilotChat.tsx +2 -1
- package/src/v2/components/chat/CopilotChatMessageView.tsx +24 -9
- package/src/v2/components/chat/CopilotChatView.tsx +2 -2
- package/src/v2/components/chat/__tests__/CopilotChat.welcomeGate.test.tsx +1 -3
- package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +29 -25
- package/src/v2/components/chat/__tests__/MCPAppsUiMessage.e2e.test.tsx +5 -60
- package/src/v2/components/index.ts +1 -0
- package/src/v2/components/intelligence-indicator/IntelligenceIndicator.tsx +286 -0
- package/src/v2/components/intelligence-indicator/__tests__/IntelligenceIndicator.e2e.test.tsx +464 -0
- package/src/v2/components/intelligence-indicator/index.ts +2 -0
- package/src/v2/context.ts +62 -0
- package/src/v2/headless.ts +42 -0
- package/src/v2/hooks/__tests__/standard-schema.test.tsx +2 -2
- package/src/v2/hooks/__tests__/use-agent-context.test.tsx +3 -3
- package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +3 -3
- package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +85 -85
- package/src/v2/hooks/__tests__/use-interrupt.test.tsx +2 -2
- package/src/v2/hooks/__tests__/use-render-tool.test.tsx +2 -2
- package/src/v2/hooks/__tests__/use-threads.test.tsx +2 -2
- package/src/v2/hooks/__tests__/zod-regression.test.tsx +2 -2
- package/src/v2/hooks/use-agent-context.tsx +1 -1
- package/src/v2/hooks/use-agent.tsx +9 -118
- package/src/v2/hooks/use-configure-suggestions.tsx +1 -1
- package/src/v2/hooks/use-frontend-tool.tsx +2 -2
- package/src/v2/hooks/use-human-in-the-loop.tsx +1 -1
- package/src/v2/hooks/use-interrupt.tsx +1 -1
- package/src/v2/hooks/use-render-activity-message.tsx +3 -11
- package/src/v2/hooks/use-render-custom-messages.tsx +1 -6
- package/src/v2/hooks/use-render-tool-call.tsx +1 -1
- package/src/v2/hooks/use-render-tool.tsx +2 -2
- package/src/v2/hooks/use-suggestions.tsx +1 -1
- package/src/v2/hooks/use-threads.tsx +1 -1
- package/src/v2/providers/CopilotKitProvider.tsx +19 -59
- package/src/v2/styles/globals.css +118 -0
- package/tsdown.config.ts +75 -0
- package/dist/copilotkit-CPe2-340.mjs.map +0 -1
- package/dist/copilotkit-DFaI4j2r.d.mts.map +0 -1
- package/dist/copilotkit-DGbvw8n2.cjs.map +0 -1
- package/dist/copilotkit-Dg4r4Gi_.d.cts.map +0 -1
- package/src/v2/hooks/__tests__/use-agent-thread-isolation.test.tsx +0 -333
|
@@ -2,7 +2,7 @@ import React from "react";
|
|
|
2
2
|
import { render, act, screen } from "@testing-library/react";
|
|
3
3
|
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
4
4
|
import { useAgent, UseAgentUpdate } from "../use-agent";
|
|
5
|
-
import { useCopilotKit } from "../../
|
|
5
|
+
import { useCopilotKit } from "../../context";
|
|
6
6
|
import { MockStepwiseAgent } from "../../__tests__/utils/test-helpers";
|
|
7
7
|
import {
|
|
8
8
|
CopilotKitCore,
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
import type { Message } from "@ag-ui/core";
|
|
12
12
|
import type { RunAgentInput } from "@ag-ui/client";
|
|
13
13
|
|
|
14
|
-
vi.mock("../../
|
|
14
|
+
vi.mock("../../context", () => ({
|
|
15
15
|
useCopilotKit: vi.fn(),
|
|
16
16
|
}));
|
|
17
17
|
|
|
@@ -173,13 +173,13 @@ describe("useAgent throttleMs", () => {
|
|
|
173
173
|
vi.restoreAllMocks();
|
|
174
174
|
});
|
|
175
175
|
|
|
176
|
-
it("without throttleMs, component reflects latest messages after notification", () => {
|
|
176
|
+
it("without throttleMs, component reflects latest messages after notification", async () => {
|
|
177
177
|
const TestComponent = createTestComponent();
|
|
178
178
|
|
|
179
179
|
render(<TestComponent />);
|
|
180
180
|
expect(screen.getByTestId("count").textContent).toBe("0");
|
|
181
181
|
|
|
182
|
-
act(() => {
|
|
182
|
+
await act(async () => {
|
|
183
183
|
mockAgent.messages = [userMsg("1", "hello")];
|
|
184
184
|
notifyMessagesChanged(mockAgent);
|
|
185
185
|
});
|
|
@@ -187,13 +187,13 @@ describe("useAgent throttleMs", () => {
|
|
|
187
187
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
188
188
|
});
|
|
189
189
|
|
|
190
|
-
it("with throttleMs: 0 (explicit), behaves identically to omitting throttleMs", () => {
|
|
190
|
+
it("with throttleMs: 0 (explicit), behaves identically to omitting throttleMs", async () => {
|
|
191
191
|
const TestComponent = createTestComponent({ throttleMs: 0 });
|
|
192
192
|
|
|
193
193
|
render(<TestComponent />);
|
|
194
194
|
expect(screen.getByTestId("count").textContent).toBe("0");
|
|
195
195
|
|
|
196
|
-
act(() => {
|
|
196
|
+
await act(async () => {
|
|
197
197
|
mockAgent.messages = [userMsg("1", "hello")];
|
|
198
198
|
notifyMessagesChanged(mockAgent);
|
|
199
199
|
});
|
|
@@ -201,7 +201,7 @@ describe("useAgent throttleMs", () => {
|
|
|
201
201
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
202
202
|
|
|
203
203
|
// Second notification also fires immediately (no throttle)
|
|
204
|
-
act(() => {
|
|
204
|
+
await act(async () => {
|
|
205
205
|
mockAgent.messages = [userMsg("1", "hello"), assistantMsg("2", "world")];
|
|
206
206
|
notifyMessagesChanged(mockAgent);
|
|
207
207
|
});
|
|
@@ -209,13 +209,13 @@ describe("useAgent throttleMs", () => {
|
|
|
209
209
|
expect(screen.getByTestId("count").textContent).toBe("2");
|
|
210
210
|
});
|
|
211
211
|
|
|
212
|
-
it("with throttleMs, first notification fires immediately (leading edge)", () => {
|
|
212
|
+
it("with throttleMs, first notification fires immediately (leading edge)", async () => {
|
|
213
213
|
const TestComponent = createTestComponent({ throttleMs: 100 });
|
|
214
214
|
|
|
215
215
|
render(<TestComponent />);
|
|
216
216
|
expect(screen.getByTestId("count").textContent).toBe("0");
|
|
217
217
|
|
|
218
|
-
act(() => {
|
|
218
|
+
await act(async () => {
|
|
219
219
|
mockAgent.messages = [userMsg("1", "hello")];
|
|
220
220
|
notifyMessagesChanged(mockAgent);
|
|
221
221
|
});
|
|
@@ -223,20 +223,20 @@ describe("useAgent throttleMs", () => {
|
|
|
223
223
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
224
224
|
});
|
|
225
225
|
|
|
226
|
-
it("with throttleMs, second notification within window is deferred until trailing edge", () => {
|
|
226
|
+
it("with throttleMs, second notification within window is deferred until trailing edge", async () => {
|
|
227
227
|
const TestComponent = createTestComponent({ throttleMs: 100 });
|
|
228
228
|
|
|
229
229
|
render(<TestComponent />);
|
|
230
230
|
|
|
231
231
|
// First notification — leading edge, fires immediately
|
|
232
|
-
act(() => {
|
|
232
|
+
await act(async () => {
|
|
233
233
|
mockAgent.messages = [userMsg("1", "a")];
|
|
234
234
|
notifyMessagesChanged(mockAgent);
|
|
235
235
|
});
|
|
236
236
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
237
237
|
|
|
238
238
|
// Second notification 10ms later — within throttle window
|
|
239
|
-
act(() => {
|
|
239
|
+
await act(async () => {
|
|
240
240
|
vi.advanceTimersByTime(10);
|
|
241
241
|
mockAgent.messages = [userMsg("1", "a"), assistantMsg("2", "b")];
|
|
242
242
|
notifyMessagesChanged(mockAgent);
|
|
@@ -246,13 +246,13 @@ describe("useAgent throttleMs", () => {
|
|
|
246
246
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
247
247
|
|
|
248
248
|
// Advance past the throttle window — trailing edge fires
|
|
249
|
-
act(() => {
|
|
249
|
+
await act(async () => {
|
|
250
250
|
vi.advanceTimersByTime(100);
|
|
251
251
|
});
|
|
252
252
|
expect(screen.getByTestId("count").textContent).toBe("2");
|
|
253
253
|
});
|
|
254
254
|
|
|
255
|
-
it("with throttleMs, rapid burst of many notifications results in exactly 2 renders (leading + trailing)", () => {
|
|
255
|
+
it("with throttleMs, rapid burst of many notifications results in exactly 2 renders (leading + trailing)", async () => {
|
|
256
256
|
const renderCount = { current: 0 };
|
|
257
257
|
const TestComponent = createTestComponent({ throttleMs: 100, renderCount });
|
|
258
258
|
|
|
@@ -260,7 +260,7 @@ describe("useAgent throttleMs", () => {
|
|
|
260
260
|
const rendersAfterMount = renderCount.current;
|
|
261
261
|
|
|
262
262
|
// Leading edge — fires immediately
|
|
263
|
-
act(() => {
|
|
263
|
+
await act(async () => {
|
|
264
264
|
mockAgent.messages = [userMsg("1", "tok1")];
|
|
265
265
|
notifyMessagesChanged(mockAgent);
|
|
266
266
|
});
|
|
@@ -268,7 +268,7 @@ describe("useAgent throttleMs", () => {
|
|
|
268
268
|
|
|
269
269
|
// Fire 10 rapid notifications within the throttle window (1ms apart)
|
|
270
270
|
for (let i = 2; i <= 11; i++) {
|
|
271
|
-
act(() => {
|
|
271
|
+
await act(async () => {
|
|
272
272
|
vi.advanceTimersByTime(1);
|
|
273
273
|
mockAgent.messages = createMessages(i);
|
|
274
274
|
notifyMessagesChanged(mockAgent);
|
|
@@ -280,40 +280,40 @@ describe("useAgent throttleMs", () => {
|
|
|
280
280
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
281
281
|
|
|
282
282
|
// Advance past the throttle window — trailing edge fires once
|
|
283
|
-
act(() => {
|
|
283
|
+
await act(async () => {
|
|
284
284
|
vi.advanceTimersByTime(100);
|
|
285
285
|
});
|
|
286
286
|
expect(renderCount.current).toBe(rendersAfterMount + 2);
|
|
287
287
|
expect(screen.getByTestId("count").textContent).toBe("11");
|
|
288
288
|
});
|
|
289
289
|
|
|
290
|
-
it("with throttleMs, new notification after trailing edge fires immediately (new cycle)", () => {
|
|
290
|
+
it("with throttleMs, new notification after trailing edge fires immediately (new cycle)", async () => {
|
|
291
291
|
const TestComponent = createTestComponent({ throttleMs: 100 });
|
|
292
292
|
|
|
293
293
|
render(<TestComponent />);
|
|
294
294
|
|
|
295
295
|
// Leading edge
|
|
296
|
-
act(() => {
|
|
296
|
+
await act(async () => {
|
|
297
297
|
mockAgent.messages = [userMsg("1", "a")];
|
|
298
298
|
notifyMessagesChanged(mockAgent);
|
|
299
299
|
});
|
|
300
300
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
301
301
|
|
|
302
302
|
// Second notification — deferred
|
|
303
|
-
act(() => {
|
|
303
|
+
await act(async () => {
|
|
304
304
|
vi.advanceTimersByTime(10);
|
|
305
305
|
mockAgent.messages = [userMsg("1", "a"), assistantMsg("2", "b")];
|
|
306
306
|
notifyMessagesChanged(mockAgent);
|
|
307
307
|
});
|
|
308
308
|
|
|
309
309
|
// Trailing edge fires
|
|
310
|
-
act(() => {
|
|
310
|
+
await act(async () => {
|
|
311
311
|
vi.advanceTimersByTime(100);
|
|
312
312
|
});
|
|
313
313
|
expect(screen.getByTestId("count").textContent).toBe("2");
|
|
314
314
|
|
|
315
315
|
// New notification well after the window — should fire immediately as a new leading edge
|
|
316
|
-
act(() => {
|
|
316
|
+
await act(async () => {
|
|
317
317
|
vi.advanceTimersByTime(200);
|
|
318
318
|
mockAgent.messages = [
|
|
319
319
|
userMsg("1", "a"),
|
|
@@ -337,14 +337,14 @@ describe("useAgent throttleMs", () => {
|
|
|
337
337
|
render(<TestComponent />);
|
|
338
338
|
|
|
339
339
|
// Fire onMessagesChanged to start the throttle window (leading edge)
|
|
340
|
-
act(() => {
|
|
340
|
+
await act(async () => {
|
|
341
341
|
mockAgent.messages = [userMsg("1", "a")];
|
|
342
342
|
notifyMessagesChanged(mockAgent);
|
|
343
343
|
});
|
|
344
344
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
345
345
|
|
|
346
346
|
// Fire onStateChanged 10ms later — should be deferred (within throttle window)
|
|
347
|
-
act(() => {
|
|
347
|
+
await act(async () => {
|
|
348
348
|
vi.advanceTimersByTime(10);
|
|
349
349
|
mockAgent.state = { count: 42 };
|
|
350
350
|
notifyStateChanged(mockAgent);
|
|
@@ -362,20 +362,20 @@ describe("useAgent throttleMs", () => {
|
|
|
362
362
|
expect(screen.getByTestId("state").textContent).toBe('{"count":42}');
|
|
363
363
|
});
|
|
364
364
|
|
|
365
|
-
it("with throttleMs, pending trailing timer does not fire after unmount", () => {
|
|
365
|
+
it("with throttleMs, pending trailing timer does not fire after unmount", async () => {
|
|
366
366
|
const renderCount = { current: 0 };
|
|
367
367
|
const TestComponent = createTestComponent({ throttleMs: 100, renderCount });
|
|
368
368
|
|
|
369
369
|
const { unmount } = render(<TestComponent />);
|
|
370
370
|
|
|
371
371
|
// Leading edge — fires immediately
|
|
372
|
-
act(() => {
|
|
372
|
+
await act(async () => {
|
|
373
373
|
mockAgent.messages = [userMsg("1", "a")];
|
|
374
374
|
notifyMessagesChanged(mockAgent);
|
|
375
375
|
});
|
|
376
376
|
|
|
377
377
|
// Second notification — schedules trailing timer
|
|
378
|
-
act(() => {
|
|
378
|
+
await act(async () => {
|
|
379
379
|
vi.advanceTimersByTime(10);
|
|
380
380
|
mockAgent.messages = [userMsg("1", "a"), assistantMsg("2", "b")];
|
|
381
381
|
notifyMessagesChanged(mockAgent);
|
|
@@ -387,7 +387,7 @@ describe("useAgent throttleMs", () => {
|
|
|
387
387
|
unmount();
|
|
388
388
|
|
|
389
389
|
// Advancing past the window should NOT cause additional renders
|
|
390
|
-
act(() => {
|
|
390
|
+
await act(async () => {
|
|
391
391
|
vi.advanceTimersByTime(100);
|
|
392
392
|
});
|
|
393
393
|
|
|
@@ -428,7 +428,7 @@ describe("useAgent throttleMs", () => {
|
|
|
428
428
|
{ label: "-Infinity", value: -Infinity },
|
|
429
429
|
])(
|
|
430
430
|
"with invalid throttleMs ($label), falls back to unthrottled and warns",
|
|
431
|
-
({ value }) => {
|
|
431
|
+
async ({ value }) => {
|
|
432
432
|
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
433
433
|
const TestComponent = createTestComponent({ throttleMs: value });
|
|
434
434
|
|
|
@@ -443,13 +443,13 @@ describe("useAgent throttleMs", () => {
|
|
|
443
443
|
);
|
|
444
444
|
|
|
445
445
|
// Should behave as unthrottled — every notification fires immediately
|
|
446
|
-
act(() => {
|
|
446
|
+
await act(async () => {
|
|
447
447
|
mockAgent.messages = [userMsg("1", "a")];
|
|
448
448
|
notifyMessagesChanged(mockAgent);
|
|
449
449
|
});
|
|
450
450
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
451
451
|
|
|
452
|
-
act(() => {
|
|
452
|
+
await act(async () => {
|
|
453
453
|
mockAgent.messages = [userMsg("1", "a"), assistantMsg("2", "b")];
|
|
454
454
|
notifyMessagesChanged(mockAgent);
|
|
455
455
|
});
|
|
@@ -457,25 +457,25 @@ describe("useAgent throttleMs", () => {
|
|
|
457
457
|
},
|
|
458
458
|
);
|
|
459
459
|
|
|
460
|
-
it("trailing-edge render reflects the latest messages, not stale data", () => {
|
|
460
|
+
it("trailing-edge render reflects the latest messages, not stale data", async () => {
|
|
461
461
|
const TestComponent = createTestComponent({ throttleMs: 100 });
|
|
462
462
|
render(<TestComponent />);
|
|
463
463
|
|
|
464
464
|
// Leading edge
|
|
465
|
-
act(() => {
|
|
465
|
+
await act(async () => {
|
|
466
466
|
mockAgent.messages = [userMsg("1", "A")];
|
|
467
467
|
notifyMessagesChanged(mockAgent);
|
|
468
468
|
});
|
|
469
469
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
470
470
|
|
|
471
471
|
// Multiple deferred notifications with increasing messages
|
|
472
|
-
act(() => {
|
|
472
|
+
await act(async () => {
|
|
473
473
|
vi.advanceTimersByTime(20);
|
|
474
474
|
mockAgent.messages = [userMsg("1", "A"), assistantMsg("2", "B")];
|
|
475
475
|
notifyMessagesChanged(mockAgent);
|
|
476
476
|
});
|
|
477
477
|
|
|
478
|
-
act(() => {
|
|
478
|
+
await act(async () => {
|
|
479
479
|
vi.advanceTimersByTime(20);
|
|
480
480
|
mockAgent.messages = [
|
|
481
481
|
userMsg("1", "A"),
|
|
@@ -489,25 +489,25 @@ describe("useAgent throttleMs", () => {
|
|
|
489
489
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
490
490
|
|
|
491
491
|
// Trailing edge fires — must show all 3 messages (latest state)
|
|
492
|
-
act(() => {
|
|
492
|
+
await act(async () => {
|
|
493
493
|
vi.advanceTimersByTime(100);
|
|
494
494
|
});
|
|
495
495
|
expect(screen.getByTestId("count").textContent).toBe("3");
|
|
496
496
|
});
|
|
497
497
|
|
|
498
|
-
it("trailing edge fires at exactly throttleMs after the leading edge", () => {
|
|
498
|
+
it("trailing edge fires at exactly throttleMs after the leading edge", async () => {
|
|
499
499
|
const TestComponent = createTestComponent({ throttleMs: 100 });
|
|
500
500
|
render(<TestComponent />);
|
|
501
501
|
|
|
502
502
|
// Leading edge at T=0
|
|
503
|
-
act(() => {
|
|
503
|
+
await act(async () => {
|
|
504
504
|
mockAgent.messages = [userMsg("1", "a")];
|
|
505
505
|
notifyMessagesChanged(mockAgent);
|
|
506
506
|
});
|
|
507
507
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
508
508
|
|
|
509
509
|
// Deferred notification at T=40
|
|
510
|
-
act(() => {
|
|
510
|
+
await act(async () => {
|
|
511
511
|
vi.advanceTimersByTime(40);
|
|
512
512
|
mockAgent.messages = [userMsg("1", "a"), assistantMsg("2", "b")];
|
|
513
513
|
notifyMessagesChanged(mockAgent);
|
|
@@ -515,19 +515,19 @@ describe("useAgent throttleMs", () => {
|
|
|
515
515
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
516
516
|
|
|
517
517
|
// At T=99, trailing has NOT fired yet
|
|
518
|
-
act(() => {
|
|
518
|
+
await act(async () => {
|
|
519
519
|
vi.advanceTimersByTime(59);
|
|
520
520
|
});
|
|
521
521
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
522
522
|
|
|
523
523
|
// At T=100, trailing fires
|
|
524
|
-
act(() => {
|
|
524
|
+
await act(async () => {
|
|
525
525
|
vi.advanceTimersByTime(1);
|
|
526
526
|
});
|
|
527
527
|
expect(screen.getByTestId("count").textContent).toBe("2");
|
|
528
528
|
});
|
|
529
529
|
|
|
530
|
-
it("changing throttleMs cleans up pending timers from the previous configuration", () => {
|
|
530
|
+
it("changing throttleMs cleans up pending timers from the previous configuration", async () => {
|
|
531
531
|
function DynamicThrottleComponent({ throttleMs }: { throttleMs: number }) {
|
|
532
532
|
const { agent } = useAgent({
|
|
533
533
|
agentId: "test-agent",
|
|
@@ -540,14 +540,14 @@ describe("useAgent throttleMs", () => {
|
|
|
540
540
|
const { rerender } = render(<DynamicThrottleComponent throttleMs={200} />);
|
|
541
541
|
|
|
542
542
|
// Leading edge
|
|
543
|
-
act(() => {
|
|
543
|
+
await act(async () => {
|
|
544
544
|
mockAgent.messages = [userMsg("1", "a")];
|
|
545
545
|
notifyMessagesChanged(mockAgent);
|
|
546
546
|
});
|
|
547
547
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
548
548
|
|
|
549
549
|
// Deferred notification — pending timer set for 200ms
|
|
550
|
-
act(() => {
|
|
550
|
+
await act(async () => {
|
|
551
551
|
vi.advanceTimersByTime(50);
|
|
552
552
|
mockAgent.messages = [userMsg("1", "a"), assistantMsg("2", "b")];
|
|
553
553
|
notifyMessagesChanged(mockAgent);
|
|
@@ -558,7 +558,7 @@ describe("useAgent throttleMs", () => {
|
|
|
558
558
|
rerender(<DynamicThrottleComponent throttleMs={50} />);
|
|
559
559
|
|
|
560
560
|
// New notification fires as leading edge under the new 50ms throttle
|
|
561
|
-
act(() => {
|
|
561
|
+
await act(async () => {
|
|
562
562
|
mockAgent.messages = [
|
|
563
563
|
userMsg("1", "a"),
|
|
564
564
|
assistantMsg("2", "b"),
|
|
@@ -570,13 +570,13 @@ describe("useAgent throttleMs", () => {
|
|
|
570
570
|
|
|
571
571
|
// Advance past what would have been the old 200ms trailing edge —
|
|
572
572
|
// no ghost render should occur from the old timer
|
|
573
|
-
act(() => {
|
|
573
|
+
await act(async () => {
|
|
574
574
|
vi.advanceTimersByTime(200);
|
|
575
575
|
});
|
|
576
576
|
expect(screen.getByTestId("count").textContent).toBe("3");
|
|
577
577
|
});
|
|
578
578
|
|
|
579
|
-
it("notification immediately after trailing edge is throttled (trailing restarts the window)", () => {
|
|
579
|
+
it("notification immediately after trailing edge is throttled (trailing restarts the window)", async () => {
|
|
580
580
|
const renderCount = { current: 0 };
|
|
581
581
|
const TestComponent = createTestComponent({ throttleMs: 100, renderCount });
|
|
582
582
|
|
|
@@ -584,7 +584,7 @@ describe("useAgent throttleMs", () => {
|
|
|
584
584
|
const rendersAfterMount = renderCount.current;
|
|
585
585
|
|
|
586
586
|
// T=0: Leading edge fires immediately
|
|
587
|
-
act(() => {
|
|
587
|
+
await act(async () => {
|
|
588
588
|
mockAgent.messages = [userMsg("1", "a")];
|
|
589
589
|
notifyMessagesChanged(mockAgent);
|
|
590
590
|
});
|
|
@@ -592,21 +592,21 @@ describe("useAgent throttleMs", () => {
|
|
|
592
592
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
593
593
|
|
|
594
594
|
// T=10: Deferred notification — schedules trailing
|
|
595
|
-
act(() => {
|
|
595
|
+
await act(async () => {
|
|
596
596
|
vi.advanceTimersByTime(10);
|
|
597
597
|
mockAgent.messages = [userMsg("1", "a"), assistantMsg("2", "b")];
|
|
598
598
|
notifyMessagesChanged(mockAgent);
|
|
599
599
|
});
|
|
600
600
|
|
|
601
601
|
// T=100: Trailing fires (render #2) and restarts window
|
|
602
|
-
act(() => {
|
|
602
|
+
await act(async () => {
|
|
603
603
|
vi.advanceTimersByTime(90);
|
|
604
604
|
});
|
|
605
605
|
expect(renderCount.current).toBe(rendersAfterMount + 2);
|
|
606
606
|
expect(screen.getByTestId("count").textContent).toBe("2");
|
|
607
607
|
|
|
608
608
|
// T=101: Notification 1ms after trailing — should be DEFERRED (within new window), not immediate
|
|
609
|
-
act(() => {
|
|
609
|
+
await act(async () => {
|
|
610
610
|
vi.advanceTimersByTime(1);
|
|
611
611
|
mockAgent.messages = [
|
|
612
612
|
userMsg("1", "a"),
|
|
@@ -620,7 +620,7 @@ describe("useAgent throttleMs", () => {
|
|
|
620
620
|
expect(screen.getByTestId("count").textContent).toBe("2");
|
|
621
621
|
|
|
622
622
|
// T=200: New trailing fires (render #3)
|
|
623
|
-
act(() => {
|
|
623
|
+
await act(async () => {
|
|
624
624
|
vi.advanceTimersByTime(99);
|
|
625
625
|
});
|
|
626
626
|
expect(renderCount.current).toBe(rendersAfterMount + 3);
|
|
@@ -648,7 +648,7 @@ describe("useAgent throttleMs", () => {
|
|
|
648
648
|
expect(mockAgent.subscribers.length).toBe(subscriberCountBefore);
|
|
649
649
|
});
|
|
650
650
|
|
|
651
|
-
it("single notification within window does not trigger a trailing re-render", () => {
|
|
651
|
+
it("single notification within window does not trigger a trailing re-render", async () => {
|
|
652
652
|
const renderCount = { current: 0 };
|
|
653
653
|
const TestComponent = createTestComponent({ throttleMs: 100, renderCount });
|
|
654
654
|
|
|
@@ -656,14 +656,14 @@ describe("useAgent throttleMs", () => {
|
|
|
656
656
|
const rendersAfterMount = renderCount.current;
|
|
657
657
|
|
|
658
658
|
// Leading edge — fires immediately
|
|
659
|
-
act(() => {
|
|
659
|
+
await act(async () => {
|
|
660
660
|
mockAgent.messages = [userMsg("1", "a")];
|
|
661
661
|
notifyMessagesChanged(mockAgent);
|
|
662
662
|
});
|
|
663
663
|
expect(renderCount.current).toBe(rendersAfterMount + 1);
|
|
664
664
|
|
|
665
665
|
// Advance well past the throttle window — no trailing should fire
|
|
666
|
-
act(() => {
|
|
666
|
+
await act(async () => {
|
|
667
667
|
vi.advanceTimersByTime(200);
|
|
668
668
|
});
|
|
669
669
|
|
|
@@ -686,7 +686,7 @@ describe("useAgent throttleMs", () => {
|
|
|
686
686
|
const rendersAfterMount = renderCount.current;
|
|
687
687
|
|
|
688
688
|
// Fire onMessagesChanged to start the throttle window
|
|
689
|
-
act(() => {
|
|
689
|
+
await act(async () => {
|
|
690
690
|
mockAgent.messages = [userMsg("1", "a")];
|
|
691
691
|
notifyMessagesChanged(mockAgent);
|
|
692
692
|
});
|
|
@@ -702,7 +702,7 @@ describe("useAgent throttleMs", () => {
|
|
|
702
702
|
expect(renderCount.current).toBe(rendersAfterMount + 2);
|
|
703
703
|
});
|
|
704
704
|
|
|
705
|
-
it("changing throttleMs from positive to 0 disables throttling immediately", () => {
|
|
705
|
+
it("changing throttleMs from positive to 0 disables throttling immediately", async () => {
|
|
706
706
|
function DynamicThrottleComponent({ throttleMs }: { throttleMs: number }) {
|
|
707
707
|
const { agent } = useAgent({
|
|
708
708
|
agentId: "test-agent",
|
|
@@ -715,14 +715,14 @@ describe("useAgent throttleMs", () => {
|
|
|
715
715
|
const { rerender } = render(<DynamicThrottleComponent throttleMs={200} />);
|
|
716
716
|
|
|
717
717
|
// Leading edge with throttle active
|
|
718
|
-
act(() => {
|
|
718
|
+
await act(async () => {
|
|
719
719
|
mockAgent.messages = [userMsg("1", "a")];
|
|
720
720
|
notifyMessagesChanged(mockAgent);
|
|
721
721
|
});
|
|
722
722
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
723
723
|
|
|
724
724
|
// Deferred notification — within throttle window
|
|
725
|
-
act(() => {
|
|
725
|
+
await act(async () => {
|
|
726
726
|
vi.advanceTimersByTime(50);
|
|
727
727
|
mockAgent.messages = [userMsg("1", "a"), assistantMsg("2", "b")];
|
|
728
728
|
notifyMessagesChanged(mockAgent);
|
|
@@ -733,7 +733,7 @@ describe("useAgent throttleMs", () => {
|
|
|
733
733
|
rerender(<DynamicThrottleComponent throttleMs={0} />);
|
|
734
734
|
|
|
735
735
|
// Both notifications should fire immediately now
|
|
736
|
-
act(() => {
|
|
736
|
+
await act(async () => {
|
|
737
737
|
mockAgent.messages = [
|
|
738
738
|
userMsg("1", "a"),
|
|
739
739
|
assistantMsg("2", "b"),
|
|
@@ -744,7 +744,7 @@ describe("useAgent throttleMs", () => {
|
|
|
744
744
|
expect(screen.getByTestId("count").textContent).toBe("3");
|
|
745
745
|
|
|
746
746
|
// Second immediate notification also fires (no coalescing)
|
|
747
|
-
act(() => {
|
|
747
|
+
await act(async () => {
|
|
748
748
|
mockAgent.messages = [
|
|
749
749
|
userMsg("1", "a"),
|
|
750
750
|
assistantMsg("2", "b"),
|
|
@@ -771,7 +771,7 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
771
771
|
vi.restoreAllMocks();
|
|
772
772
|
});
|
|
773
773
|
|
|
774
|
-
it("uses provider defaultThrottleMs when no explicit throttleMs is passed", () => {
|
|
774
|
+
it("uses provider defaultThrottleMs when no explicit throttleMs is passed", async () => {
|
|
775
775
|
mockUseCopilotKit.mockReturnValue(
|
|
776
776
|
createMockContext(mockAgent, { defaultThrottleMs: 100 }),
|
|
777
777
|
);
|
|
@@ -781,14 +781,14 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
781
781
|
render(<TestComponent />);
|
|
782
782
|
|
|
783
783
|
// Leading edge — fires immediately
|
|
784
|
-
act(() => {
|
|
784
|
+
await act(async () => {
|
|
785
785
|
mockAgent.messages = [userMsg("1", "hello")];
|
|
786
786
|
notifyMessagesChanged(mockAgent);
|
|
787
787
|
});
|
|
788
788
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
789
789
|
|
|
790
790
|
// Second notification within 100ms window — should be deferred (throttled)
|
|
791
|
-
act(() => {
|
|
791
|
+
await act(async () => {
|
|
792
792
|
vi.advanceTimersByTime(10);
|
|
793
793
|
mockAgent.messages = [userMsg("1", "hello"), assistantMsg("2", "world")];
|
|
794
794
|
notifyMessagesChanged(mockAgent);
|
|
@@ -796,13 +796,13 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
796
796
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
797
797
|
|
|
798
798
|
// Trailing edge fires after 100ms
|
|
799
|
-
act(() => {
|
|
799
|
+
await act(async () => {
|
|
800
800
|
vi.advanceTimersByTime(100);
|
|
801
801
|
});
|
|
802
802
|
expect(screen.getByTestId("count").textContent).toBe("2");
|
|
803
803
|
});
|
|
804
804
|
|
|
805
|
-
it("explicit throttleMs overrides provider defaultThrottleMs", () => {
|
|
805
|
+
it("explicit throttleMs overrides provider defaultThrottleMs", async () => {
|
|
806
806
|
mockUseCopilotKit.mockReturnValue(
|
|
807
807
|
createMockContext(mockAgent, { defaultThrottleMs: 5000 }),
|
|
808
808
|
);
|
|
@@ -813,14 +813,14 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
813
813
|
render(<TestComponent />);
|
|
814
814
|
|
|
815
815
|
// Leading edge
|
|
816
|
-
act(() => {
|
|
816
|
+
await act(async () => {
|
|
817
817
|
mockAgent.messages = [userMsg("1", "hello")];
|
|
818
818
|
notifyMessagesChanged(mockAgent);
|
|
819
819
|
});
|
|
820
820
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
821
821
|
|
|
822
822
|
// Deferred within 100ms window
|
|
823
|
-
act(() => {
|
|
823
|
+
await act(async () => {
|
|
824
824
|
vi.advanceTimersByTime(10);
|
|
825
825
|
mockAgent.messages = [userMsg("1", "hello"), assistantMsg("2", "world")];
|
|
826
826
|
notifyMessagesChanged(mockAgent);
|
|
@@ -828,34 +828,34 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
828
828
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
829
829
|
|
|
830
830
|
// At 100ms trailing fires (not waiting for provider's 5000ms)
|
|
831
|
-
act(() => {
|
|
831
|
+
await act(async () => {
|
|
832
832
|
vi.advanceTimersByTime(100);
|
|
833
833
|
});
|
|
834
834
|
expect(screen.getByTestId("count").textContent).toBe("2");
|
|
835
835
|
});
|
|
836
836
|
|
|
837
|
-
it("without provider defaultThrottleMs or explicit throttleMs, behaves unthrottled", () => {
|
|
837
|
+
it("without provider defaultThrottleMs or explicit throttleMs, behaves unthrottled", async () => {
|
|
838
838
|
mockUseCopilotKit.mockReturnValue(createMockContext(mockAgent));
|
|
839
839
|
|
|
840
840
|
const TestComponent = createTestComponent({});
|
|
841
841
|
|
|
842
842
|
render(<TestComponent />);
|
|
843
843
|
|
|
844
|
-
act(() => {
|
|
844
|
+
await act(async () => {
|
|
845
845
|
mockAgent.messages = [userMsg("1", "hello")];
|
|
846
846
|
notifyMessagesChanged(mockAgent);
|
|
847
847
|
});
|
|
848
848
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
849
849
|
|
|
850
850
|
// Immediately fires — no throttle
|
|
851
|
-
act(() => {
|
|
851
|
+
await act(async () => {
|
|
852
852
|
mockAgent.messages = [userMsg("1", "hello"), assistantMsg("2", "world")];
|
|
853
853
|
notifyMessagesChanged(mockAgent);
|
|
854
854
|
});
|
|
855
855
|
expect(screen.getByTestId("count").textContent).toBe("2");
|
|
856
856
|
});
|
|
857
857
|
|
|
858
|
-
it("explicit throttleMs: 0 overrides non-zero provider defaultThrottleMs (opt-out)", () => {
|
|
858
|
+
it("explicit throttleMs: 0 overrides non-zero provider defaultThrottleMs (opt-out)", async () => {
|
|
859
859
|
mockUseCopilotKit.mockReturnValue(
|
|
860
860
|
createMockContext(mockAgent, { defaultThrottleMs: 500 }),
|
|
861
861
|
);
|
|
@@ -865,13 +865,13 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
865
865
|
render(<TestComponent />);
|
|
866
866
|
|
|
867
867
|
// Both notifications fire immediately — throttleMs: 0 means no throttle
|
|
868
|
-
act(() => {
|
|
868
|
+
await act(async () => {
|
|
869
869
|
mockAgent.messages = [userMsg("1", "hello")];
|
|
870
870
|
notifyMessagesChanged(mockAgent);
|
|
871
871
|
});
|
|
872
872
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
873
873
|
|
|
874
|
-
act(() => {
|
|
874
|
+
await act(async () => {
|
|
875
875
|
mockAgent.messages = [userMsg("1", "hello"), assistantMsg("2", "world")];
|
|
876
876
|
notifyMessagesChanged(mockAgent);
|
|
877
877
|
});
|
|
@@ -885,7 +885,7 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
885
885
|
{ label: "-Infinity", value: -Infinity },
|
|
886
886
|
])(
|
|
887
887
|
"with invalid provider defaultThrottleMs ($label), falls back to unthrottled and warns",
|
|
888
|
-
({ value }) => {
|
|
888
|
+
async ({ value }) => {
|
|
889
889
|
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
890
890
|
|
|
891
891
|
mockUseCopilotKit.mockReturnValue(
|
|
@@ -903,13 +903,13 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
903
903
|
);
|
|
904
904
|
|
|
905
905
|
// Should behave as unthrottled (setter rejected the value)
|
|
906
|
-
act(() => {
|
|
906
|
+
await act(async () => {
|
|
907
907
|
mockAgent.messages = [userMsg("1", "a")];
|
|
908
908
|
notifyMessagesChanged(mockAgent);
|
|
909
909
|
});
|
|
910
910
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
911
911
|
|
|
912
|
-
act(() => {
|
|
912
|
+
await act(async () => {
|
|
913
913
|
mockAgent.messages = [userMsg("1", "a"), assistantMsg("2", "b")];
|
|
914
914
|
notifyMessagesChanged(mockAgent);
|
|
915
915
|
});
|
|
@@ -917,7 +917,7 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
917
917
|
},
|
|
918
918
|
);
|
|
919
919
|
|
|
920
|
-
it("dynamically changing provider defaultThrottleMs updates throttle behavior", () => {
|
|
920
|
+
it("dynamically changing provider defaultThrottleMs updates throttle behavior", async () => {
|
|
921
921
|
// Start with 200ms throttle from provider
|
|
922
922
|
mockUseCopilotKit.mockReturnValue(
|
|
923
923
|
createMockContext(mockAgent, { defaultThrottleMs: 200 }),
|
|
@@ -927,14 +927,14 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
927
927
|
const { rerender } = render(<TestComponent />);
|
|
928
928
|
|
|
929
929
|
// Leading edge fires immediately
|
|
930
|
-
act(() => {
|
|
930
|
+
await act(async () => {
|
|
931
931
|
mockAgent.messages = [userMsg("1", "hello")];
|
|
932
932
|
notifyMessagesChanged(mockAgent);
|
|
933
933
|
});
|
|
934
934
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
935
935
|
|
|
936
936
|
// Deferred within 200ms window
|
|
937
|
-
act(() => {
|
|
937
|
+
await act(async () => {
|
|
938
938
|
vi.advanceTimersByTime(10);
|
|
939
939
|
mockAgent.messages = [userMsg("1", "hello"), assistantMsg("2", "world")];
|
|
940
940
|
notifyMessagesChanged(mockAgent);
|
|
@@ -942,7 +942,7 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
942
942
|
expect(screen.getByTestId("count").textContent).toBe("1");
|
|
943
943
|
|
|
944
944
|
// Flush trailing edge
|
|
945
|
-
act(() => {
|
|
945
|
+
await act(async () => {
|
|
946
946
|
vi.advanceTimersByTime(200);
|
|
947
947
|
});
|
|
948
948
|
expect(screen.getByTestId("count").textContent).toBe("2");
|
|
@@ -954,7 +954,7 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
954
954
|
rerender(<TestComponent />);
|
|
955
955
|
|
|
956
956
|
// Leading edge fires immediately
|
|
957
|
-
act(() => {
|
|
957
|
+
await act(async () => {
|
|
958
958
|
mockAgent.messages = [
|
|
959
959
|
userMsg("1", "hello"),
|
|
960
960
|
assistantMsg("2", "world"),
|
|
@@ -965,7 +965,7 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
965
965
|
expect(screen.getByTestId("count").textContent).toBe("3");
|
|
966
966
|
|
|
967
967
|
// Deferred within 50ms window
|
|
968
|
-
act(() => {
|
|
968
|
+
await act(async () => {
|
|
969
969
|
vi.advanceTimersByTime(10);
|
|
970
970
|
mockAgent.messages = [
|
|
971
971
|
userMsg("1", "hello"),
|
|
@@ -978,7 +978,7 @@ describe("useAgent defaultThrottleMs from provider", () => {
|
|
|
978
978
|
expect(screen.getByTestId("count").textContent).toBe("3");
|
|
979
979
|
|
|
980
980
|
// Trailing fires after only 50ms (not 200ms)
|
|
981
|
-
act(() => {
|
|
981
|
+
await act(async () => {
|
|
982
982
|
vi.advanceTimersByTime(50);
|
|
983
983
|
});
|
|
984
984
|
expect(screen.getByTestId("count").textContent).toBe("4");
|