@cloudflare/ai-chat 0.1.5 → 0.1.6
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/experimental/forever.d.ts +4 -13
- package/dist/experimental/forever.js +1 -46
- package/dist/experimental/forever.js.map +1 -1
- package/dist/index.d.ts +59 -4
- package/dist/index.js +170 -28
- package/dist/index.js.map +1 -1
- package/dist/{message-builder-Bwc_Umye.js → message-builder-uSvexDl2.js} +17 -4
- package/dist/message-builder-uSvexDl2.js.map +1 -0
- package/dist/react.js +96 -10
- package/dist/react.js.map +1 -1
- package/dist/{resumable-stream-DqXxQVjf.d.ts → resumable-stream-DTLAk9XC.d.ts} +25 -2
- package/dist/types.d.ts +2 -1
- package/dist/types.js.map +1 -1
- package/package.json +7 -3
- package/CHANGELOG.md +0 -251
- package/dist/message-builder-Bwc_Umye.js.map +0 -1
- package/e2e/chat.spec.ts +0 -1703
- package/e2e/client-tools.spec.ts +0 -504
- package/e2e/llm.spec.ts +0 -637
- package/e2e/playwright.config.ts +0 -27
- package/e2e/worker.ts +0 -174
- package/e2e/wrangler.jsonc +0 -23
- package/scripts/build.ts +0 -32
- package/src/ai-chat-v5-migration.ts +0 -403
- package/src/experimental/forever.ts +0 -66
- package/src/index.ts +0 -2269
- package/src/message-builder.ts +0 -384
- package/src/react-tests/setup.ts +0 -3
- package/src/react-tests/use-agent-chat.test.tsx +0 -1435
- package/src/react-tests/vitest.config.ts +0 -20
- package/src/react.tsx +0 -1447
- package/src/resumable-stream.ts +0 -441
- package/src/tests/cancel-request.test.ts +0 -280
- package/src/tests/chat-context.test.ts +0 -85
- package/src/tests/chat-persistence.test.ts +0 -426
- package/src/tests/clear-history.test.ts +0 -132
- package/src/tests/client-tool-duplicate-message.test.ts +0 -1957
- package/src/tests/client-tools-broadcast.test.ts +0 -138
- package/src/tests/client-tools-continuation.test.ts +0 -314
- package/src/tests/client-tools-reconnect.test.ts +0 -143
- package/src/tests/cloudflare-test.d.ts +0 -5
- package/src/tests/custom-body-continuation.test.ts +0 -309
- package/src/tests/custom-body.test.ts +0 -186
- package/src/tests/destroy.test.ts +0 -57
- package/src/tests/get-messages-endpoint.test.ts +0 -153
- package/src/tests/max-persisted-messages.test.ts +0 -201
- package/src/tests/merge-server-state.test.ts +0 -343
- package/src/tests/message-builder.test.ts +0 -808
- package/src/tests/migration.test.ts +0 -282
- package/src/tests/non-sse-response.test.ts +0 -186
- package/src/tests/onfinish-cleanup.test.ts +0 -362
- package/src/tests/plaintext-response.test.ts +0 -87
- package/src/tests/request-id.test.ts +0 -312
- package/src/tests/resumable-streaming.test.ts +0 -758
- package/src/tests/row-size-guard.test.ts +0 -374
- package/src/tests/sanitize-messages.test.ts +0 -325
- package/src/tests/test-utils.ts +0 -39
- package/src/tests/tsconfig.json +0 -10
- package/src/tests/validate-messages.test.ts +0 -269
- package/src/tests/vitest.config.ts +0 -30
- package/src/tests/worker.ts +0 -456
- package/src/tests/wrangler.jsonc +0 -50
- package/src/tests/ws-transport-abort.test.ts +0 -198
- package/src/types.ts +0 -152
- package/src/ws-chat-transport.ts +0 -227
- package/tsconfig.json +0 -4
- package/vitest.config.ts +0 -7
|
@@ -1,1435 +0,0 @@
|
|
|
1
|
-
import { StrictMode, Suspense, act } from "react";
|
|
2
|
-
import { describe, expect, it, vi } from "vitest";
|
|
3
|
-
import { render } from "vitest-browser-react";
|
|
4
|
-
import type { UIMessage } from "ai";
|
|
5
|
-
import {
|
|
6
|
-
useAgentChat,
|
|
7
|
-
type PrepareSendMessagesRequestOptions,
|
|
8
|
-
type PrepareSendMessagesRequestResult,
|
|
9
|
-
type AITool
|
|
10
|
-
} from "../react";
|
|
11
|
-
import type { useAgent } from "agents/react";
|
|
12
|
-
|
|
13
|
-
function sleep(ms: number) {
|
|
14
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function createAgent({
|
|
18
|
-
name,
|
|
19
|
-
url,
|
|
20
|
-
send
|
|
21
|
-
}: {
|
|
22
|
-
name: string;
|
|
23
|
-
url: string;
|
|
24
|
-
send?: (data: string) => void;
|
|
25
|
-
}) {
|
|
26
|
-
const target = new EventTarget();
|
|
27
|
-
const baseAgent = {
|
|
28
|
-
_pkurl: url,
|
|
29
|
-
_pk: name, // Use name as pk to distinguish agents
|
|
30
|
-
_url: null as string | null,
|
|
31
|
-
addEventListener: target.addEventListener.bind(target),
|
|
32
|
-
agent: "Chat",
|
|
33
|
-
close: () => {},
|
|
34
|
-
id: "fake-agent",
|
|
35
|
-
name,
|
|
36
|
-
removeEventListener: target.removeEventListener.bind(target),
|
|
37
|
-
send: send ?? (() => {}),
|
|
38
|
-
dispatchEvent: target.dispatchEvent.bind(target)
|
|
39
|
-
};
|
|
40
|
-
return baseAgent as unknown as ReturnType<typeof useAgent>;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
describe("useAgentChat", () => {
|
|
44
|
-
it("should cache initial message responses across re-renders", async () => {
|
|
45
|
-
const agent = createAgent({
|
|
46
|
-
name: "thread-alpha",
|
|
47
|
-
url: "ws://localhost:3000/agents/chat/thread-alpha?_pk=abc"
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
const testMessages = [
|
|
51
|
-
{
|
|
52
|
-
id: "1",
|
|
53
|
-
role: "user" as const,
|
|
54
|
-
parts: [{ type: "text" as const, text: "Hi" }]
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
id: "2",
|
|
58
|
-
role: "assistant" as const,
|
|
59
|
-
parts: [{ type: "text" as const, text: "Hello" }]
|
|
60
|
-
}
|
|
61
|
-
];
|
|
62
|
-
|
|
63
|
-
const getInitialMessages = vi.fn(() => Promise.resolve(testMessages));
|
|
64
|
-
|
|
65
|
-
const TestComponent = () => {
|
|
66
|
-
const chat = useAgentChat({
|
|
67
|
-
agent,
|
|
68
|
-
getInitialMessages
|
|
69
|
-
});
|
|
70
|
-
return <div data-testid="messages">{JSON.stringify(chat.messages)}</div>;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const suspenseRendered = vi.fn();
|
|
74
|
-
const SuspenseObserver = () => {
|
|
75
|
-
suspenseRendered();
|
|
76
|
-
return "Suspended";
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const screen = await act(async () => {
|
|
80
|
-
const screen = render(<TestComponent />, {
|
|
81
|
-
wrapper: ({ children }) => (
|
|
82
|
-
<StrictMode>
|
|
83
|
-
<Suspense fallback={<SuspenseObserver />}>{children}</Suspense>
|
|
84
|
-
</StrictMode>
|
|
85
|
-
)
|
|
86
|
-
});
|
|
87
|
-
await sleep(10);
|
|
88
|
-
return screen;
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
await expect
|
|
92
|
-
.element(screen.getByTestId("messages"))
|
|
93
|
-
.toHaveTextContent(JSON.stringify(testMessages));
|
|
94
|
-
|
|
95
|
-
expect(getInitialMessages).toHaveBeenCalledTimes(1);
|
|
96
|
-
expect(suspenseRendered).toHaveBeenCalled();
|
|
97
|
-
|
|
98
|
-
suspenseRendered.mockClear();
|
|
99
|
-
|
|
100
|
-
await screen.rerender(<TestComponent />);
|
|
101
|
-
|
|
102
|
-
await expect
|
|
103
|
-
.element(screen.getByTestId("messages"))
|
|
104
|
-
.toHaveTextContent(JSON.stringify(testMessages));
|
|
105
|
-
|
|
106
|
-
expect(getInitialMessages).toHaveBeenCalledTimes(1);
|
|
107
|
-
expect(suspenseRendered).not.toHaveBeenCalled();
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it("should refetch initial messages when the agent name changes", async () => {
|
|
111
|
-
const url = "ws://localhost:3000/agents/chat/thread-a?_pk=abc";
|
|
112
|
-
const agentA = createAgent({ name: "thread-a", url });
|
|
113
|
-
const agentB = createAgent({ name: "thread-b", url });
|
|
114
|
-
|
|
115
|
-
const getInitialMessages = vi.fn(async ({ name }: { name: string }) => [
|
|
116
|
-
{
|
|
117
|
-
id: "1",
|
|
118
|
-
role: "assistant" as const,
|
|
119
|
-
parts: [{ type: "text" as const, text: `Hello from ${name}` }]
|
|
120
|
-
}
|
|
121
|
-
]);
|
|
122
|
-
|
|
123
|
-
const TestComponent = ({
|
|
124
|
-
agent
|
|
125
|
-
}: {
|
|
126
|
-
agent: ReturnType<typeof useAgent>;
|
|
127
|
-
}) => {
|
|
128
|
-
const chat = useAgentChat({
|
|
129
|
-
agent,
|
|
130
|
-
getInitialMessages
|
|
131
|
-
});
|
|
132
|
-
return <div data-testid="messages">{JSON.stringify(chat.messages)}</div>;
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
const suspenseRendered = vi.fn();
|
|
136
|
-
const SuspenseObserver = () => {
|
|
137
|
-
suspenseRendered();
|
|
138
|
-
return "Suspended";
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
const screen = await act(async () => {
|
|
142
|
-
const screen = render(<TestComponent agent={agentA} />, {
|
|
143
|
-
wrapper: ({ children }) => (
|
|
144
|
-
<StrictMode>
|
|
145
|
-
<Suspense fallback={<SuspenseObserver />}>{children}</Suspense>
|
|
146
|
-
</StrictMode>
|
|
147
|
-
)
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
await sleep(10);
|
|
151
|
-
return screen;
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
await expect
|
|
155
|
-
.element(screen.getByTestId("messages"))
|
|
156
|
-
.toHaveTextContent("Hello from thread-a");
|
|
157
|
-
|
|
158
|
-
expect(getInitialMessages).toHaveBeenCalledTimes(1);
|
|
159
|
-
expect(getInitialMessages).toHaveBeenNthCalledWith(
|
|
160
|
-
1,
|
|
161
|
-
expect.objectContaining({ name: "thread-a" })
|
|
162
|
-
);
|
|
163
|
-
|
|
164
|
-
suspenseRendered.mockClear();
|
|
165
|
-
|
|
166
|
-
await act(async () => {
|
|
167
|
-
screen.rerender(<TestComponent agent={agentB} />);
|
|
168
|
-
await sleep(10);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
await expect
|
|
172
|
-
.element(screen.getByTestId("messages"))
|
|
173
|
-
.toHaveTextContent("Hello from thread-b");
|
|
174
|
-
|
|
175
|
-
expect(getInitialMessages).toHaveBeenCalledTimes(2);
|
|
176
|
-
expect(getInitialMessages).toHaveBeenNthCalledWith(
|
|
177
|
-
2,
|
|
178
|
-
expect.objectContaining({ name: "thread-b" })
|
|
179
|
-
);
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
it("should accept prepareSendMessagesRequest option without errors", async () => {
|
|
183
|
-
const agent = createAgent({
|
|
184
|
-
name: "thread-with-tools",
|
|
185
|
-
url: "ws://localhost:3000/agents/chat/thread-with-tools?_pk=abc"
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
const prepareSendMessagesRequest = vi.fn(
|
|
189
|
-
(
|
|
190
|
-
_options: PrepareSendMessagesRequestOptions<UIMessage>
|
|
191
|
-
): PrepareSendMessagesRequestResult => ({
|
|
192
|
-
body: {
|
|
193
|
-
clientTools: [
|
|
194
|
-
{
|
|
195
|
-
name: "showAlert",
|
|
196
|
-
description: "Shows an alert to the user",
|
|
197
|
-
parameters: { message: { type: "string" } }
|
|
198
|
-
}
|
|
199
|
-
]
|
|
200
|
-
},
|
|
201
|
-
headers: {
|
|
202
|
-
"X-Client-Tool-Count": "1"
|
|
203
|
-
}
|
|
204
|
-
})
|
|
205
|
-
);
|
|
206
|
-
|
|
207
|
-
const TestComponent = () => {
|
|
208
|
-
const chat = useAgentChat({
|
|
209
|
-
agent,
|
|
210
|
-
getInitialMessages: null, // Skip fetching initial messages
|
|
211
|
-
prepareSendMessagesRequest
|
|
212
|
-
});
|
|
213
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
const screen = await act(() =>
|
|
217
|
-
render(<TestComponent />, {
|
|
218
|
-
wrapper: ({ children }) => (
|
|
219
|
-
<StrictMode>
|
|
220
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
221
|
-
</StrictMode>
|
|
222
|
-
)
|
|
223
|
-
})
|
|
224
|
-
);
|
|
225
|
-
|
|
226
|
-
// Verify component renders without errors
|
|
227
|
-
await expect
|
|
228
|
-
.element(screen.getByTestId("messages-count"))
|
|
229
|
-
.toHaveTextContent("0");
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
it("should handle async prepareSendMessagesRequest", async () => {
|
|
233
|
-
const agent = createAgent({
|
|
234
|
-
name: "thread-async-prepare",
|
|
235
|
-
url: "ws://localhost:3000/agents/chat/thread-async-prepare?_pk=abc"
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
const prepareSendMessagesRequest = vi.fn(
|
|
239
|
-
async (
|
|
240
|
-
_options: PrepareSendMessagesRequestOptions<UIMessage>
|
|
241
|
-
): Promise<PrepareSendMessagesRequestResult> => {
|
|
242
|
-
// Simulate async operation like fetching tool definitions
|
|
243
|
-
await sleep(10);
|
|
244
|
-
return {
|
|
245
|
-
body: {
|
|
246
|
-
clientTools: [
|
|
247
|
-
{ name: "navigateToPage", description: "Navigates to a page" }
|
|
248
|
-
]
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
);
|
|
253
|
-
|
|
254
|
-
const TestComponent = () => {
|
|
255
|
-
const chat = useAgentChat({
|
|
256
|
-
agent,
|
|
257
|
-
getInitialMessages: null,
|
|
258
|
-
prepareSendMessagesRequest
|
|
259
|
-
});
|
|
260
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
const screen = await act(() =>
|
|
264
|
-
render(<TestComponent />, {
|
|
265
|
-
wrapper: ({ children }) => (
|
|
266
|
-
<StrictMode>
|
|
267
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
268
|
-
</StrictMode>
|
|
269
|
-
)
|
|
270
|
-
})
|
|
271
|
-
);
|
|
272
|
-
|
|
273
|
-
// Verify component renders without errors
|
|
274
|
-
await expect
|
|
275
|
-
.element(screen.getByTestId("messages-count"))
|
|
276
|
-
.toHaveTextContent("0");
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
it("should auto-extract schemas from tools with execute functions", async () => {
|
|
280
|
-
const agent = createAgent({
|
|
281
|
-
name: "thread-client-tools",
|
|
282
|
-
url: "ws://localhost:3000/agents/chat/thread-client-tools?_pk=abc"
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
// Tools with execute functions have their schemas auto-extracted and sent to server
|
|
286
|
-
const tools: Record<string, AITool<unknown, unknown>> = {
|
|
287
|
-
showAlert: {
|
|
288
|
-
description: "Shows an alert dialog to the user",
|
|
289
|
-
parameters: {
|
|
290
|
-
type: "object",
|
|
291
|
-
properties: {
|
|
292
|
-
message: { type: "string", description: "The message to display" }
|
|
293
|
-
},
|
|
294
|
-
required: ["message"]
|
|
295
|
-
},
|
|
296
|
-
execute: async (input) => {
|
|
297
|
-
// Client-side execution
|
|
298
|
-
const { message } = input as { message: string };
|
|
299
|
-
return { shown: true, message };
|
|
300
|
-
}
|
|
301
|
-
},
|
|
302
|
-
changeBackgroundColor: {
|
|
303
|
-
description: "Changes the page background color",
|
|
304
|
-
parameters: {
|
|
305
|
-
type: "object",
|
|
306
|
-
properties: {
|
|
307
|
-
color: { type: "string" }
|
|
308
|
-
}
|
|
309
|
-
},
|
|
310
|
-
execute: async (input) => {
|
|
311
|
-
const { color } = input as { color: string };
|
|
312
|
-
return { success: true, color };
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
const TestComponent = () => {
|
|
318
|
-
const chat = useAgentChat({
|
|
319
|
-
agent,
|
|
320
|
-
getInitialMessages: null,
|
|
321
|
-
tools
|
|
322
|
-
});
|
|
323
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
const screen = await act(() =>
|
|
327
|
-
render(<TestComponent />, {
|
|
328
|
-
wrapper: ({ children }) => (
|
|
329
|
-
<StrictMode>
|
|
330
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
331
|
-
</StrictMode>
|
|
332
|
-
)
|
|
333
|
-
})
|
|
334
|
-
);
|
|
335
|
-
|
|
336
|
-
// Verify component renders without errors
|
|
337
|
-
await expect
|
|
338
|
-
.element(screen.getByTestId("messages-count"))
|
|
339
|
-
.toHaveTextContent("0");
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
it("should combine auto-extracted tools with prepareSendMessagesRequest", async () => {
|
|
343
|
-
const agent = createAgent({
|
|
344
|
-
name: "thread-combined",
|
|
345
|
-
url: "ws://localhost:3000/agents/chat/thread-combined?_pk=abc"
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
const tools: Record<string, AITool> = {
|
|
349
|
-
showAlert: {
|
|
350
|
-
description: "Shows an alert",
|
|
351
|
-
execute: async () => ({ shown: true })
|
|
352
|
-
}
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
const prepareSendMessagesRequest = vi.fn(
|
|
356
|
-
(
|
|
357
|
-
_options: PrepareSendMessagesRequestOptions<UIMessage>
|
|
358
|
-
): PrepareSendMessagesRequestResult => ({
|
|
359
|
-
body: {
|
|
360
|
-
customData: "extra-context",
|
|
361
|
-
userTimezone: "America/New_York"
|
|
362
|
-
},
|
|
363
|
-
headers: {
|
|
364
|
-
"X-Custom-Header": "custom-value"
|
|
365
|
-
}
|
|
366
|
-
})
|
|
367
|
-
);
|
|
368
|
-
|
|
369
|
-
const TestComponent = () => {
|
|
370
|
-
const chat = useAgentChat({
|
|
371
|
-
agent,
|
|
372
|
-
getInitialMessages: null,
|
|
373
|
-
tools,
|
|
374
|
-
prepareSendMessagesRequest
|
|
375
|
-
});
|
|
376
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
377
|
-
};
|
|
378
|
-
|
|
379
|
-
const screen = await act(() =>
|
|
380
|
-
render(<TestComponent />, {
|
|
381
|
-
wrapper: ({ children }) => (
|
|
382
|
-
<StrictMode>
|
|
383
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
384
|
-
</StrictMode>
|
|
385
|
-
)
|
|
386
|
-
})
|
|
387
|
-
);
|
|
388
|
-
|
|
389
|
-
// Verify component renders without errors
|
|
390
|
-
await expect
|
|
391
|
-
.element(screen.getByTestId("messages-count"))
|
|
392
|
-
.toHaveTextContent("0");
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
it("should work with tools that have execute functions for client-side execution", async () => {
|
|
396
|
-
const agent = createAgent({
|
|
397
|
-
name: "thread-tools-execution",
|
|
398
|
-
url: "ws://localhost:3000/agents/chat/thread-tools-execution?_pk=abc"
|
|
399
|
-
});
|
|
400
|
-
|
|
401
|
-
const mockExecute = vi.fn().mockResolvedValue({ success: true });
|
|
402
|
-
|
|
403
|
-
// Single unified tools object - schema + execute in one place
|
|
404
|
-
const tools: Record<string, AITool> = {
|
|
405
|
-
showAlert: {
|
|
406
|
-
description: "Shows an alert",
|
|
407
|
-
parameters: {
|
|
408
|
-
type: "object",
|
|
409
|
-
properties: { message: { type: "string" } }
|
|
410
|
-
},
|
|
411
|
-
execute: mockExecute
|
|
412
|
-
}
|
|
413
|
-
};
|
|
414
|
-
|
|
415
|
-
const TestComponent = () => {
|
|
416
|
-
const chat = useAgentChat({
|
|
417
|
-
agent,
|
|
418
|
-
getInitialMessages: null,
|
|
419
|
-
tools
|
|
420
|
-
});
|
|
421
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
422
|
-
};
|
|
423
|
-
|
|
424
|
-
const screen = await act(() =>
|
|
425
|
-
render(<TestComponent />, {
|
|
426
|
-
wrapper: ({ children }) => (
|
|
427
|
-
<StrictMode>
|
|
428
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
429
|
-
</StrictMode>
|
|
430
|
-
)
|
|
431
|
-
})
|
|
432
|
-
);
|
|
433
|
-
|
|
434
|
-
// Verify component renders without errors
|
|
435
|
-
await expect
|
|
436
|
-
.element(screen.getByTestId("messages-count"))
|
|
437
|
-
.toHaveTextContent("0");
|
|
438
|
-
});
|
|
439
|
-
});
|
|
440
|
-
|
|
441
|
-
describe("useAgentChat client-side tool execution (issue #728)", () => {
|
|
442
|
-
it("should update tool part state from input-available to output-available when addToolResult is called", async () => {
|
|
443
|
-
const agent = createAgent({
|
|
444
|
-
name: "tool-state-test",
|
|
445
|
-
url: "ws://localhost:3000/agents/chat/tool-state-test?_pk=abc"
|
|
446
|
-
});
|
|
447
|
-
|
|
448
|
-
const mockExecute = vi.fn().mockResolvedValue({ location: "New York" });
|
|
449
|
-
|
|
450
|
-
// Initial messages with a tool call in input-available state
|
|
451
|
-
const initialMessages: UIMessage[] = [
|
|
452
|
-
{
|
|
453
|
-
id: "msg-1",
|
|
454
|
-
role: "user",
|
|
455
|
-
parts: [{ type: "text", text: "Where am I?" }]
|
|
456
|
-
},
|
|
457
|
-
{
|
|
458
|
-
id: "msg-2",
|
|
459
|
-
role: "assistant",
|
|
460
|
-
parts: [
|
|
461
|
-
{
|
|
462
|
-
type: "tool-getLocation",
|
|
463
|
-
toolCallId: "tool-call-1",
|
|
464
|
-
state: "input-available",
|
|
465
|
-
input: {}
|
|
466
|
-
}
|
|
467
|
-
]
|
|
468
|
-
}
|
|
469
|
-
];
|
|
470
|
-
|
|
471
|
-
const TestComponent = () => {
|
|
472
|
-
const chat = useAgentChat({
|
|
473
|
-
agent,
|
|
474
|
-
getInitialMessages: () => Promise.resolve(initialMessages),
|
|
475
|
-
experimental_automaticToolResolution: true,
|
|
476
|
-
tools: {
|
|
477
|
-
getLocation: {
|
|
478
|
-
execute: mockExecute
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
});
|
|
482
|
-
|
|
483
|
-
// Find the tool part to check its state
|
|
484
|
-
const assistantMsg = chat.messages.find((m) => m.role === "assistant");
|
|
485
|
-
const toolPart = assistantMsg?.parts.find(
|
|
486
|
-
(p) => "toolCallId" in p && p.toolCallId === "tool-call-1"
|
|
487
|
-
);
|
|
488
|
-
const toolState =
|
|
489
|
-
toolPart && "state" in toolPart ? toolPart.state : "not-found";
|
|
490
|
-
|
|
491
|
-
return (
|
|
492
|
-
<div>
|
|
493
|
-
<div data-testid="messages-count">{chat.messages.length}</div>
|
|
494
|
-
<div data-testid="tool-state">{toolState}</div>
|
|
495
|
-
</div>
|
|
496
|
-
);
|
|
497
|
-
};
|
|
498
|
-
|
|
499
|
-
const screen = await act(async () => {
|
|
500
|
-
const screen = render(<TestComponent />, {
|
|
501
|
-
wrapper: ({ children }) => (
|
|
502
|
-
<StrictMode>
|
|
503
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
504
|
-
</StrictMode>
|
|
505
|
-
)
|
|
506
|
-
});
|
|
507
|
-
// The tool should have been automatically executed
|
|
508
|
-
await sleep(10);
|
|
509
|
-
return screen;
|
|
510
|
-
});
|
|
511
|
-
|
|
512
|
-
// Wait for initial messages to load
|
|
513
|
-
await expect
|
|
514
|
-
.element(screen.getByTestId("messages-count"))
|
|
515
|
-
.toHaveTextContent("2");
|
|
516
|
-
|
|
517
|
-
// Verify the tool execute was called
|
|
518
|
-
expect(mockExecute).toHaveBeenCalled();
|
|
519
|
-
|
|
520
|
-
// the tool part should be updated to output-available
|
|
521
|
-
// in the SAME message (msg-2), not in a new message
|
|
522
|
-
await expect
|
|
523
|
-
.element(screen.getByTestId("messages-count"))
|
|
524
|
-
.toHaveTextContent("2"); // Should still be 2 messages, not 3
|
|
525
|
-
|
|
526
|
-
// The tool state should be output-available after addToolResult
|
|
527
|
-
await expect
|
|
528
|
-
.element(screen.getByTestId("tool-state"))
|
|
529
|
-
.toHaveTextContent("output-available");
|
|
530
|
-
});
|
|
531
|
-
|
|
532
|
-
it("should not create duplicate tool parts when client executes tool", async () => {
|
|
533
|
-
const agent = createAgent({
|
|
534
|
-
name: "duplicate-test",
|
|
535
|
-
url: "ws://localhost:3000/agents/chat/duplicate-test?_pk=abc"
|
|
536
|
-
});
|
|
537
|
-
|
|
538
|
-
const mockExecute = vi.fn().mockResolvedValue({ confirmed: true });
|
|
539
|
-
|
|
540
|
-
const initialMessages: UIMessage[] = [
|
|
541
|
-
{
|
|
542
|
-
id: "msg-1",
|
|
543
|
-
role: "assistant",
|
|
544
|
-
parts: [
|
|
545
|
-
{ type: "text", text: "Should I proceed?" },
|
|
546
|
-
{
|
|
547
|
-
type: "tool-askForConfirmation",
|
|
548
|
-
toolCallId: "confirm-1",
|
|
549
|
-
state: "input-available",
|
|
550
|
-
input: { message: "Proceed with action?" }
|
|
551
|
-
}
|
|
552
|
-
]
|
|
553
|
-
}
|
|
554
|
-
];
|
|
555
|
-
|
|
556
|
-
let chatInstance: ReturnType<typeof useAgentChat> | null = null;
|
|
557
|
-
|
|
558
|
-
const TestComponent = () => {
|
|
559
|
-
const chat = useAgentChat({
|
|
560
|
-
agent,
|
|
561
|
-
getInitialMessages: () => Promise.resolve(initialMessages),
|
|
562
|
-
tools: {
|
|
563
|
-
askForConfirmation: {
|
|
564
|
-
execute: mockExecute
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
});
|
|
568
|
-
chatInstance = chat;
|
|
569
|
-
|
|
570
|
-
// Count tool parts with this toolCallId
|
|
571
|
-
const toolPartsCount = chat.messages.reduce((count, msg) => {
|
|
572
|
-
return (
|
|
573
|
-
count +
|
|
574
|
-
msg.parts.filter(
|
|
575
|
-
(p) => "toolCallId" in p && p.toolCallId === "confirm-1"
|
|
576
|
-
).length
|
|
577
|
-
);
|
|
578
|
-
}, 0);
|
|
579
|
-
|
|
580
|
-
// Get the tool state
|
|
581
|
-
const toolPart = chat.messages
|
|
582
|
-
.flatMap((m) => m.parts)
|
|
583
|
-
.find((p) => "toolCallId" in p && p.toolCallId === "confirm-1");
|
|
584
|
-
const toolState =
|
|
585
|
-
toolPart && "state" in toolPart ? toolPart.state : "not-found";
|
|
586
|
-
|
|
587
|
-
return (
|
|
588
|
-
<div>
|
|
589
|
-
<div data-testid="messages-count">{chat.messages.length}</div>
|
|
590
|
-
<div data-testid="tool-parts-count">{toolPartsCount}</div>
|
|
591
|
-
<div data-testid="tool-state">{toolState}</div>
|
|
592
|
-
</div>
|
|
593
|
-
);
|
|
594
|
-
};
|
|
595
|
-
|
|
596
|
-
const screen = await act(async () => {
|
|
597
|
-
const screen = render(<TestComponent />, {
|
|
598
|
-
wrapper: ({ children }) => (
|
|
599
|
-
<StrictMode>
|
|
600
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
601
|
-
</StrictMode>
|
|
602
|
-
)
|
|
603
|
-
});
|
|
604
|
-
await sleep(10);
|
|
605
|
-
return screen;
|
|
606
|
-
});
|
|
607
|
-
|
|
608
|
-
await expect
|
|
609
|
-
.element(screen.getByTestId("messages-count"))
|
|
610
|
-
.toHaveTextContent("1");
|
|
611
|
-
|
|
612
|
-
// Manually trigger addToolResult to simulate user confirming
|
|
613
|
-
await act(async () => {
|
|
614
|
-
if (chatInstance) {
|
|
615
|
-
await chatInstance.addToolResult({
|
|
616
|
-
tool: "askForConfirmation",
|
|
617
|
-
toolCallId: "confirm-1",
|
|
618
|
-
output: { confirmed: true }
|
|
619
|
-
});
|
|
620
|
-
}
|
|
621
|
-
});
|
|
622
|
-
|
|
623
|
-
// There should still be exactly ONE tool part with this toolCallId
|
|
624
|
-
await expect
|
|
625
|
-
.element(screen.getByTestId("tool-parts-count"))
|
|
626
|
-
.toHaveTextContent("1");
|
|
627
|
-
|
|
628
|
-
// The tool state should be updated to output-available
|
|
629
|
-
await expect
|
|
630
|
-
.element(screen.getByTestId("tool-state"))
|
|
631
|
-
.toHaveTextContent("output-available");
|
|
632
|
-
});
|
|
633
|
-
});
|
|
634
|
-
|
|
635
|
-
describe("useAgentChat setMessages", () => {
|
|
636
|
-
it("should handle functional updater and sync resolved messages to server", async () => {
|
|
637
|
-
const sentMessages: string[] = [];
|
|
638
|
-
const agent = createAgent({
|
|
639
|
-
name: "set-messages-test",
|
|
640
|
-
url: "ws://localhost:3000/agents/chat/set-messages-test?_pk=abc",
|
|
641
|
-
send: (data: string) => sentMessages.push(data)
|
|
642
|
-
});
|
|
643
|
-
|
|
644
|
-
const initialMessages: UIMessage[] = [
|
|
645
|
-
{
|
|
646
|
-
id: "msg-1",
|
|
647
|
-
role: "user",
|
|
648
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
649
|
-
},
|
|
650
|
-
{
|
|
651
|
-
id: "msg-2",
|
|
652
|
-
role: "assistant",
|
|
653
|
-
parts: [{ type: "text", text: "Hi there!" }]
|
|
654
|
-
}
|
|
655
|
-
];
|
|
656
|
-
|
|
657
|
-
let chatInstance: ReturnType<typeof useAgentChat> | null = null;
|
|
658
|
-
|
|
659
|
-
const TestComponent = () => {
|
|
660
|
-
const chat = useAgentChat({
|
|
661
|
-
agent,
|
|
662
|
-
getInitialMessages: () => Promise.resolve(initialMessages)
|
|
663
|
-
});
|
|
664
|
-
chatInstance = chat;
|
|
665
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
666
|
-
};
|
|
667
|
-
|
|
668
|
-
const screen = await act(async () => {
|
|
669
|
-
const screen = render(<TestComponent />, {
|
|
670
|
-
wrapper: ({ children }) => (
|
|
671
|
-
<StrictMode>
|
|
672
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
673
|
-
</StrictMode>
|
|
674
|
-
)
|
|
675
|
-
});
|
|
676
|
-
await sleep(10);
|
|
677
|
-
return screen;
|
|
678
|
-
});
|
|
679
|
-
|
|
680
|
-
await expect
|
|
681
|
-
.element(screen.getByTestId("messages-count"))
|
|
682
|
-
.toHaveTextContent("2");
|
|
683
|
-
|
|
684
|
-
// Use functional updater to append a message
|
|
685
|
-
const newMessage: UIMessage = {
|
|
686
|
-
id: "msg-3",
|
|
687
|
-
role: "user",
|
|
688
|
-
parts: [{ type: "text", text: "Follow up" }]
|
|
689
|
-
};
|
|
690
|
-
|
|
691
|
-
await act(async () => {
|
|
692
|
-
chatInstance!.setMessages((prev) => [...prev, newMessage]);
|
|
693
|
-
await sleep(10);
|
|
694
|
-
});
|
|
695
|
-
|
|
696
|
-
await expect
|
|
697
|
-
.element(screen.getByTestId("messages-count"))
|
|
698
|
-
.toHaveTextContent("3");
|
|
699
|
-
|
|
700
|
-
// Verify the server received the RESOLVED messages (not empty array)
|
|
701
|
-
const chatMessagesSent = sentMessages
|
|
702
|
-
.map((m) => JSON.parse(m))
|
|
703
|
-
.filter((m) => m.type === "cf_agent_chat_messages");
|
|
704
|
-
|
|
705
|
-
expect(chatMessagesSent.length).toBeGreaterThan(0);
|
|
706
|
-
const lastSent = chatMessagesSent[chatMessagesSent.length - 1];
|
|
707
|
-
// Should have the full 3 messages, NOT an empty array
|
|
708
|
-
expect(lastSent.messages.length).toBe(3);
|
|
709
|
-
expect(lastSent.messages[2].id).toBe("msg-3");
|
|
710
|
-
});
|
|
711
|
-
|
|
712
|
-
it("should handle array setMessages and sync to server", async () => {
|
|
713
|
-
const sentMessages: string[] = [];
|
|
714
|
-
const agent = createAgent({
|
|
715
|
-
name: "set-messages-array-test",
|
|
716
|
-
url: "ws://localhost:3000/agents/chat/set-messages-array-test?_pk=abc",
|
|
717
|
-
send: (data: string) => sentMessages.push(data)
|
|
718
|
-
});
|
|
719
|
-
|
|
720
|
-
let chatInstance: ReturnType<typeof useAgentChat> | null = null;
|
|
721
|
-
|
|
722
|
-
const TestComponent = () => {
|
|
723
|
-
const chat = useAgentChat({
|
|
724
|
-
agent,
|
|
725
|
-
getInitialMessages: null,
|
|
726
|
-
messages: [] as UIMessage[]
|
|
727
|
-
});
|
|
728
|
-
chatInstance = chat;
|
|
729
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
730
|
-
};
|
|
731
|
-
|
|
732
|
-
await act(async () => {
|
|
733
|
-
render(<TestComponent />, {
|
|
734
|
-
wrapper: ({ children }) => (
|
|
735
|
-
<StrictMode>
|
|
736
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
737
|
-
</StrictMode>
|
|
738
|
-
)
|
|
739
|
-
});
|
|
740
|
-
await sleep(10);
|
|
741
|
-
});
|
|
742
|
-
|
|
743
|
-
// Set messages with an array directly
|
|
744
|
-
const newMessages: UIMessage[] = [
|
|
745
|
-
{
|
|
746
|
-
id: "arr-1",
|
|
747
|
-
role: "user",
|
|
748
|
-
parts: [{ type: "text", text: "Replaced" }]
|
|
749
|
-
}
|
|
750
|
-
];
|
|
751
|
-
|
|
752
|
-
await act(async () => {
|
|
753
|
-
chatInstance!.setMessages(newMessages);
|
|
754
|
-
await sleep(10);
|
|
755
|
-
});
|
|
756
|
-
|
|
757
|
-
// Verify the server received the array
|
|
758
|
-
const chatMessagesSent = sentMessages
|
|
759
|
-
.map((m) => JSON.parse(m))
|
|
760
|
-
.filter((m) => m.type === "cf_agent_chat_messages");
|
|
761
|
-
|
|
762
|
-
expect(chatMessagesSent.length).toBeGreaterThan(0);
|
|
763
|
-
const lastSent = chatMessagesSent[chatMessagesSent.length - 1];
|
|
764
|
-
expect(lastSent.messages.length).toBe(1);
|
|
765
|
-
expect(lastSent.messages[0].id).toBe("arr-1");
|
|
766
|
-
});
|
|
767
|
-
});
|
|
768
|
-
|
|
769
|
-
describe("useAgentChat clearHistory", () => {
|
|
770
|
-
it("should clear local state and send CF_AGENT_CHAT_CLEAR to server", async () => {
|
|
771
|
-
const sentMessages: string[] = [];
|
|
772
|
-
const agent = createAgent({
|
|
773
|
-
name: "clear-test",
|
|
774
|
-
url: "ws://localhost:3000/agents/chat/clear-test?_pk=abc",
|
|
775
|
-
send: (data: string) => sentMessages.push(data)
|
|
776
|
-
});
|
|
777
|
-
|
|
778
|
-
const initialMessages: UIMessage[] = [
|
|
779
|
-
{
|
|
780
|
-
id: "clear-1",
|
|
781
|
-
role: "user",
|
|
782
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
783
|
-
}
|
|
784
|
-
];
|
|
785
|
-
|
|
786
|
-
let chatInstance: ReturnType<typeof useAgentChat> | null = null;
|
|
787
|
-
|
|
788
|
-
const TestComponent = () => {
|
|
789
|
-
const chat = useAgentChat({
|
|
790
|
-
agent,
|
|
791
|
-
getInitialMessages: () => Promise.resolve(initialMessages)
|
|
792
|
-
});
|
|
793
|
-
chatInstance = chat;
|
|
794
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
795
|
-
};
|
|
796
|
-
|
|
797
|
-
const screen = await act(async () => {
|
|
798
|
-
const screen = render(<TestComponent />, {
|
|
799
|
-
wrapper: ({ children }) => (
|
|
800
|
-
<StrictMode>
|
|
801
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
802
|
-
</StrictMode>
|
|
803
|
-
)
|
|
804
|
-
});
|
|
805
|
-
await sleep(10);
|
|
806
|
-
return screen;
|
|
807
|
-
});
|
|
808
|
-
|
|
809
|
-
await expect
|
|
810
|
-
.element(screen.getByTestId("messages-count"))
|
|
811
|
-
.toHaveTextContent("1");
|
|
812
|
-
|
|
813
|
-
await act(async () => {
|
|
814
|
-
chatInstance!.clearHistory();
|
|
815
|
-
await sleep(10);
|
|
816
|
-
});
|
|
817
|
-
|
|
818
|
-
await expect
|
|
819
|
-
.element(screen.getByTestId("messages-count"))
|
|
820
|
-
.toHaveTextContent("0");
|
|
821
|
-
|
|
822
|
-
// Verify CF_AGENT_CHAT_CLEAR was sent
|
|
823
|
-
const clearMessages = sentMessages
|
|
824
|
-
.map((m) => JSON.parse(m))
|
|
825
|
-
.filter((m) => m.type === "cf_agent_chat_clear");
|
|
826
|
-
expect(clearMessages.length).toBe(1);
|
|
827
|
-
});
|
|
828
|
-
});
|
|
829
|
-
|
|
830
|
-
describe("useAgentChat autoContinueAfterToolResult default", () => {
|
|
831
|
-
it("should send autoContinue: true by default with tool results", async () => {
|
|
832
|
-
const sentMessages: string[] = [];
|
|
833
|
-
const agent = createAgent({
|
|
834
|
-
name: "auto-continue-default",
|
|
835
|
-
url: "ws://localhost:3000/agents/chat/auto-continue-default?_pk=abc",
|
|
836
|
-
send: (data: string) => sentMessages.push(data)
|
|
837
|
-
});
|
|
838
|
-
|
|
839
|
-
const initialMessages: UIMessage[] = [
|
|
840
|
-
{
|
|
841
|
-
id: "msg-1",
|
|
842
|
-
role: "assistant",
|
|
843
|
-
parts: [
|
|
844
|
-
{
|
|
845
|
-
type: "tool-getLocation",
|
|
846
|
-
toolCallId: "tc-default-1",
|
|
847
|
-
state: "input-available",
|
|
848
|
-
input: {}
|
|
849
|
-
}
|
|
850
|
-
]
|
|
851
|
-
}
|
|
852
|
-
];
|
|
853
|
-
|
|
854
|
-
const TestComponent = () => {
|
|
855
|
-
const chat = useAgentChat({
|
|
856
|
-
agent,
|
|
857
|
-
getInitialMessages: () => Promise.resolve(initialMessages),
|
|
858
|
-
// No explicit autoContinueAfterToolResult — should default to true
|
|
859
|
-
onToolCall: ({ toolCall, addToolOutput }) => {
|
|
860
|
-
addToolOutput({
|
|
861
|
-
toolCallId: toolCall.toolCallId,
|
|
862
|
-
output: { lat: 51.5, lng: -0.1 }
|
|
863
|
-
});
|
|
864
|
-
}
|
|
865
|
-
});
|
|
866
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
867
|
-
};
|
|
868
|
-
|
|
869
|
-
await act(async () => {
|
|
870
|
-
render(<TestComponent />, {
|
|
871
|
-
wrapper: ({ children }) => (
|
|
872
|
-
<StrictMode>
|
|
873
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
874
|
-
</StrictMode>
|
|
875
|
-
)
|
|
876
|
-
});
|
|
877
|
-
await sleep(50);
|
|
878
|
-
});
|
|
879
|
-
|
|
880
|
-
// Find the CF_AGENT_TOOL_RESULT message
|
|
881
|
-
const toolResultMessages = sentMessages
|
|
882
|
-
.map((m) => JSON.parse(m))
|
|
883
|
-
.filter((m) => m.type === "cf_agent_tool_result");
|
|
884
|
-
|
|
885
|
-
expect(toolResultMessages.length).toBeGreaterThanOrEqual(1);
|
|
886
|
-
// Default should be autoContinue: true
|
|
887
|
-
expect(toolResultMessages[0].autoContinue).toBe(true);
|
|
888
|
-
});
|
|
889
|
-
|
|
890
|
-
it("should send autoContinue: false when explicitly disabled", async () => {
|
|
891
|
-
const sentMessages: string[] = [];
|
|
892
|
-
const agent = createAgent({
|
|
893
|
-
name: "auto-continue-disabled",
|
|
894
|
-
url: "ws://localhost:3000/agents/chat/auto-continue-disabled?_pk=abc",
|
|
895
|
-
send: (data: string) => sentMessages.push(data)
|
|
896
|
-
});
|
|
897
|
-
|
|
898
|
-
const initialMessages: UIMessage[] = [
|
|
899
|
-
{
|
|
900
|
-
id: "msg-1",
|
|
901
|
-
role: "assistant",
|
|
902
|
-
parts: [
|
|
903
|
-
{
|
|
904
|
-
type: "tool-getLocation",
|
|
905
|
-
toolCallId: "tc-disabled-1",
|
|
906
|
-
state: "input-available",
|
|
907
|
-
input: {}
|
|
908
|
-
}
|
|
909
|
-
]
|
|
910
|
-
}
|
|
911
|
-
];
|
|
912
|
-
|
|
913
|
-
const TestComponent = () => {
|
|
914
|
-
const chat = useAgentChat({
|
|
915
|
-
agent,
|
|
916
|
-
getInitialMessages: () => Promise.resolve(initialMessages),
|
|
917
|
-
autoContinueAfterToolResult: false, // Explicitly disabled
|
|
918
|
-
onToolCall: ({ toolCall, addToolOutput }) => {
|
|
919
|
-
addToolOutput({
|
|
920
|
-
toolCallId: toolCall.toolCallId,
|
|
921
|
-
output: { lat: 51.5, lng: -0.1 }
|
|
922
|
-
});
|
|
923
|
-
}
|
|
924
|
-
});
|
|
925
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
926
|
-
};
|
|
927
|
-
|
|
928
|
-
await act(async () => {
|
|
929
|
-
render(<TestComponent />, {
|
|
930
|
-
wrapper: ({ children }) => (
|
|
931
|
-
<StrictMode>
|
|
932
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
933
|
-
</StrictMode>
|
|
934
|
-
)
|
|
935
|
-
});
|
|
936
|
-
await sleep(50);
|
|
937
|
-
});
|
|
938
|
-
|
|
939
|
-
const toolResultMessages = sentMessages
|
|
940
|
-
.map((m) => JSON.parse(m))
|
|
941
|
-
.filter((m) => m.type === "cf_agent_tool_result");
|
|
942
|
-
|
|
943
|
-
expect(toolResultMessages.length).toBeGreaterThanOrEqual(1);
|
|
944
|
-
expect(toolResultMessages[0].autoContinue).toBe(false);
|
|
945
|
-
});
|
|
946
|
-
|
|
947
|
-
it("should send autoContinue: true by default with tool approvals", async () => {
|
|
948
|
-
const sentMessages: string[] = [];
|
|
949
|
-
const agent = createAgent({
|
|
950
|
-
name: "auto-continue-approval",
|
|
951
|
-
url: "ws://localhost:3000/agents/chat/auto-continue-approval?_pk=abc",
|
|
952
|
-
send: (data: string) => sentMessages.push(data)
|
|
953
|
-
});
|
|
954
|
-
|
|
955
|
-
// Tool part must have approval.id so the wrapper can find the toolCallId
|
|
956
|
-
const initialMessages: UIMessage[] = [
|
|
957
|
-
{
|
|
958
|
-
id: "msg-1",
|
|
959
|
-
role: "assistant",
|
|
960
|
-
parts: [
|
|
961
|
-
{
|
|
962
|
-
type: "tool-dangerousAction",
|
|
963
|
-
toolCallId: "tc-approval-1",
|
|
964
|
-
state: "approval-requested",
|
|
965
|
-
input: { action: "delete" },
|
|
966
|
-
approval: { id: "approval-req-1" }
|
|
967
|
-
}
|
|
968
|
-
]
|
|
969
|
-
}
|
|
970
|
-
];
|
|
971
|
-
|
|
972
|
-
let chatInstance: ReturnType<typeof useAgentChat> | null = null;
|
|
973
|
-
|
|
974
|
-
const TestComponent = () => {
|
|
975
|
-
const chat = useAgentChat({
|
|
976
|
-
agent,
|
|
977
|
-
getInitialMessages: () => Promise.resolve(initialMessages)
|
|
978
|
-
// No explicit autoContinueAfterToolResult — should default to true
|
|
979
|
-
});
|
|
980
|
-
chatInstance = chat;
|
|
981
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
982
|
-
};
|
|
983
|
-
|
|
984
|
-
await act(async () => {
|
|
985
|
-
render(<TestComponent />, {
|
|
986
|
-
wrapper: ({ children }) => (
|
|
987
|
-
<StrictMode>
|
|
988
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
989
|
-
</StrictMode>
|
|
990
|
-
)
|
|
991
|
-
});
|
|
992
|
-
await sleep(50);
|
|
993
|
-
});
|
|
994
|
-
|
|
995
|
-
// Send approval via the hook using the approval request ID
|
|
996
|
-
await act(async () => {
|
|
997
|
-
if (chatInstance) {
|
|
998
|
-
chatInstance.addToolApprovalResponse({
|
|
999
|
-
id: "approval-req-1",
|
|
1000
|
-
approved: true
|
|
1001
|
-
});
|
|
1002
|
-
}
|
|
1003
|
-
await sleep(10);
|
|
1004
|
-
});
|
|
1005
|
-
|
|
1006
|
-
// Find the CF_AGENT_TOOL_APPROVAL message
|
|
1007
|
-
const approvalMessages = sentMessages
|
|
1008
|
-
.map((m) => JSON.parse(m))
|
|
1009
|
-
.filter((m) => m.type === "cf_agent_tool_approval");
|
|
1010
|
-
|
|
1011
|
-
expect(approvalMessages.length).toBeGreaterThanOrEqual(1);
|
|
1012
|
-
expect(approvalMessages[0].autoContinue).toBe(true);
|
|
1013
|
-
expect(approvalMessages[0].approved).toBe(true);
|
|
1014
|
-
});
|
|
1015
|
-
});
|
|
1016
|
-
|
|
1017
|
-
describe("useAgentChat onToolCall", () => {
|
|
1018
|
-
it("should fire onToolCall for input-available tool parts", async () => {
|
|
1019
|
-
const agent = createAgent({
|
|
1020
|
-
name: "ontoolcall-test",
|
|
1021
|
-
url: "ws://localhost:3000/agents/chat/ontoolcall-test?_pk=abc",
|
|
1022
|
-
send: () => {}
|
|
1023
|
-
});
|
|
1024
|
-
|
|
1025
|
-
const toolCallReceived = vi.fn();
|
|
1026
|
-
|
|
1027
|
-
const initialMessages: UIMessage[] = [
|
|
1028
|
-
{
|
|
1029
|
-
id: "msg-tool-1",
|
|
1030
|
-
role: "assistant",
|
|
1031
|
-
parts: [
|
|
1032
|
-
{
|
|
1033
|
-
type: "tool-getLocation",
|
|
1034
|
-
toolCallId: "tc-1",
|
|
1035
|
-
state: "input-available",
|
|
1036
|
-
input: { query: "current" }
|
|
1037
|
-
}
|
|
1038
|
-
]
|
|
1039
|
-
}
|
|
1040
|
-
];
|
|
1041
|
-
|
|
1042
|
-
const TestComponent = () => {
|
|
1043
|
-
const chat = useAgentChat({
|
|
1044
|
-
agent,
|
|
1045
|
-
getInitialMessages: () => Promise.resolve(initialMessages),
|
|
1046
|
-
onToolCall: ({ toolCall, addToolOutput }) => {
|
|
1047
|
-
toolCallReceived(toolCall);
|
|
1048
|
-
addToolOutput({
|
|
1049
|
-
toolCallId: toolCall.toolCallId,
|
|
1050
|
-
output: { lat: 40.7, lng: -74.0 }
|
|
1051
|
-
});
|
|
1052
|
-
}
|
|
1053
|
-
});
|
|
1054
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
1055
|
-
};
|
|
1056
|
-
|
|
1057
|
-
await act(async () => {
|
|
1058
|
-
render(<TestComponent />, {
|
|
1059
|
-
wrapper: ({ children }) => (
|
|
1060
|
-
<StrictMode>
|
|
1061
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
1062
|
-
</StrictMode>
|
|
1063
|
-
)
|
|
1064
|
-
});
|
|
1065
|
-
await sleep(50);
|
|
1066
|
-
});
|
|
1067
|
-
|
|
1068
|
-
// onToolCall should have been called with the tool call details
|
|
1069
|
-
expect(toolCallReceived).toHaveBeenCalledWith(
|
|
1070
|
-
expect.objectContaining({
|
|
1071
|
-
toolCallId: "tc-1",
|
|
1072
|
-
toolName: "getLocation",
|
|
1073
|
-
input: { query: "current" }
|
|
1074
|
-
})
|
|
1075
|
-
);
|
|
1076
|
-
});
|
|
1077
|
-
});
|
|
1078
|
-
|
|
1079
|
-
describe("useAgentChat re-render stability", () => {
|
|
1080
|
-
it("should not cause infinite re-renders when idle", async () => {
|
|
1081
|
-
const agent = createAgent({
|
|
1082
|
-
name: "rerender-idle",
|
|
1083
|
-
url: "ws://localhost:3000/agents/chat/rerender-idle?_pk=abc"
|
|
1084
|
-
});
|
|
1085
|
-
|
|
1086
|
-
let renderCount = 0;
|
|
1087
|
-
|
|
1088
|
-
const TestComponent = () => {
|
|
1089
|
-
renderCount++;
|
|
1090
|
-
const chat = useAgentChat({
|
|
1091
|
-
agent,
|
|
1092
|
-
getInitialMessages: null,
|
|
1093
|
-
messages: []
|
|
1094
|
-
});
|
|
1095
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
1096
|
-
};
|
|
1097
|
-
|
|
1098
|
-
await act(async () => {
|
|
1099
|
-
render(<TestComponent />, {
|
|
1100
|
-
wrapper: ({ children }) => (
|
|
1101
|
-
<StrictMode>
|
|
1102
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
1103
|
-
</StrictMode>
|
|
1104
|
-
)
|
|
1105
|
-
});
|
|
1106
|
-
await sleep(10);
|
|
1107
|
-
});
|
|
1108
|
-
|
|
1109
|
-
// Capture render count after initial mount
|
|
1110
|
-
const afterMountCount = renderCount;
|
|
1111
|
-
|
|
1112
|
-
// Wait to see if more renders happen (would indicate an infinite loop)
|
|
1113
|
-
await sleep(200);
|
|
1114
|
-
|
|
1115
|
-
// In Strict Mode, React double-renders. After mount stabilizes,
|
|
1116
|
-
// there should be NO additional renders (no infinite loop).
|
|
1117
|
-
expect(renderCount).toBe(afterMountCount);
|
|
1118
|
-
});
|
|
1119
|
-
|
|
1120
|
-
it("should not re-render excessively when messages are set", async () => {
|
|
1121
|
-
const agent = createAgent({
|
|
1122
|
-
name: "rerender-messages",
|
|
1123
|
-
url: "ws://localhost:3000/agents/chat/rerender-messages?_pk=abc"
|
|
1124
|
-
});
|
|
1125
|
-
|
|
1126
|
-
let renderCount = 0;
|
|
1127
|
-
let chatInstance: ReturnType<typeof useAgentChat> | null = null;
|
|
1128
|
-
|
|
1129
|
-
const TestComponent = () => {
|
|
1130
|
-
renderCount++;
|
|
1131
|
-
const chat = useAgentChat({
|
|
1132
|
-
agent,
|
|
1133
|
-
getInitialMessages: null,
|
|
1134
|
-
messages: [] as UIMessage[]
|
|
1135
|
-
});
|
|
1136
|
-
chatInstance = chat;
|
|
1137
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
1138
|
-
};
|
|
1139
|
-
|
|
1140
|
-
await act(async () => {
|
|
1141
|
-
render(<TestComponent />, {
|
|
1142
|
-
wrapper: ({ children }) => (
|
|
1143
|
-
<StrictMode>
|
|
1144
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
1145
|
-
</StrictMode>
|
|
1146
|
-
)
|
|
1147
|
-
});
|
|
1148
|
-
await sleep(10);
|
|
1149
|
-
});
|
|
1150
|
-
|
|
1151
|
-
const beforeSetMessages = renderCount;
|
|
1152
|
-
|
|
1153
|
-
// Set messages
|
|
1154
|
-
await act(async () => {
|
|
1155
|
-
chatInstance!.setMessages([
|
|
1156
|
-
{
|
|
1157
|
-
id: "msg-1",
|
|
1158
|
-
role: "user",
|
|
1159
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
1160
|
-
}
|
|
1161
|
-
]);
|
|
1162
|
-
await sleep(10);
|
|
1163
|
-
});
|
|
1164
|
-
|
|
1165
|
-
const afterSetMessages = renderCount;
|
|
1166
|
-
|
|
1167
|
-
// Wait to see if renders stabilize
|
|
1168
|
-
await sleep(200);
|
|
1169
|
-
|
|
1170
|
-
// Should have re-rendered for the setMessages call but then stopped.
|
|
1171
|
-
// Allow some re-renders (React batching, state updates) but not infinite.
|
|
1172
|
-
const rendersFromSetMessages = afterSetMessages - beforeSetMessages;
|
|
1173
|
-
expect(rendersFromSetMessages).toBeLessThan(10);
|
|
1174
|
-
|
|
1175
|
-
// No additional renders after stabilizing
|
|
1176
|
-
expect(renderCount).toBe(afterSetMessages);
|
|
1177
|
-
});
|
|
1178
|
-
|
|
1179
|
-
it("should stabilize after receiving a broadcast message", async () => {
|
|
1180
|
-
const target = new EventTarget();
|
|
1181
|
-
const agent = createAgent({
|
|
1182
|
-
name: "rerender-broadcast",
|
|
1183
|
-
url: "ws://localhost:3000/agents/chat/rerender-broadcast?_pk=abc"
|
|
1184
|
-
});
|
|
1185
|
-
// Override addEventListener/removeEventListener to use our target
|
|
1186
|
-
(agent as unknown as Record<string, unknown>).addEventListener =
|
|
1187
|
-
target.addEventListener.bind(target);
|
|
1188
|
-
(agent as unknown as Record<string, unknown>).removeEventListener =
|
|
1189
|
-
target.removeEventListener.bind(target);
|
|
1190
|
-
|
|
1191
|
-
let renderCount = 0;
|
|
1192
|
-
|
|
1193
|
-
const TestComponent = () => {
|
|
1194
|
-
renderCount++;
|
|
1195
|
-
const chat = useAgentChat({
|
|
1196
|
-
agent,
|
|
1197
|
-
getInitialMessages: null,
|
|
1198
|
-
messages: []
|
|
1199
|
-
});
|
|
1200
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
1201
|
-
};
|
|
1202
|
-
|
|
1203
|
-
await act(async () => {
|
|
1204
|
-
render(<TestComponent />, {
|
|
1205
|
-
wrapper: ({ children }) => (
|
|
1206
|
-
<StrictMode>
|
|
1207
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
1208
|
-
</StrictMode>
|
|
1209
|
-
)
|
|
1210
|
-
});
|
|
1211
|
-
await sleep(10);
|
|
1212
|
-
});
|
|
1213
|
-
|
|
1214
|
-
const beforeBroadcast = renderCount;
|
|
1215
|
-
|
|
1216
|
-
// Simulate a server broadcast (CF_AGENT_CHAT_MESSAGES)
|
|
1217
|
-
await act(async () => {
|
|
1218
|
-
target.dispatchEvent(
|
|
1219
|
-
new MessageEvent("message", {
|
|
1220
|
-
data: JSON.stringify({
|
|
1221
|
-
type: "cf_agent_chat_messages",
|
|
1222
|
-
messages: [
|
|
1223
|
-
{
|
|
1224
|
-
id: "broadcast-1",
|
|
1225
|
-
role: "user",
|
|
1226
|
-
parts: [{ type: "text", text: "From other tab" }]
|
|
1227
|
-
}
|
|
1228
|
-
]
|
|
1229
|
-
})
|
|
1230
|
-
})
|
|
1231
|
-
);
|
|
1232
|
-
await sleep(10);
|
|
1233
|
-
});
|
|
1234
|
-
|
|
1235
|
-
const afterBroadcast = renderCount;
|
|
1236
|
-
|
|
1237
|
-
// Wait for stabilization
|
|
1238
|
-
await sleep(200);
|
|
1239
|
-
|
|
1240
|
-
// Should have re-rendered for the broadcast but then stopped
|
|
1241
|
-
const rendersFromBroadcast = afterBroadcast - beforeBroadcast;
|
|
1242
|
-
expect(rendersFromBroadcast).toBeGreaterThan(0); // Must have re-rendered
|
|
1243
|
-
expect(rendersFromBroadcast).toBeLessThan(10); // But not infinitely
|
|
1244
|
-
|
|
1245
|
-
// No additional renders after stabilizing
|
|
1246
|
-
expect(renderCount).toBe(afterBroadcast);
|
|
1247
|
-
});
|
|
1248
|
-
});
|
|
1249
|
-
|
|
1250
|
-
describe("useAgentChat body option", () => {
|
|
1251
|
-
it("should include static body fields in sent messages", async () => {
|
|
1252
|
-
const sentMessages: string[] = [];
|
|
1253
|
-
const agent = createAgent({
|
|
1254
|
-
name: "body-static-test",
|
|
1255
|
-
url: "ws://localhost:3000/agents/chat/body-static-test?_pk=abc",
|
|
1256
|
-
send: (data: string) => sentMessages.push(data)
|
|
1257
|
-
});
|
|
1258
|
-
|
|
1259
|
-
const TestComponent = () => {
|
|
1260
|
-
const chat = useAgentChat({
|
|
1261
|
-
agent,
|
|
1262
|
-
getInitialMessages: null,
|
|
1263
|
-
messages: [],
|
|
1264
|
-
body: { timezone: "America/New_York", userId: "user-123" }
|
|
1265
|
-
});
|
|
1266
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
1267
|
-
};
|
|
1268
|
-
|
|
1269
|
-
await act(async () => {
|
|
1270
|
-
render(<TestComponent />, {
|
|
1271
|
-
wrapper: ({ children }) => (
|
|
1272
|
-
<StrictMode>
|
|
1273
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
1274
|
-
</StrictMode>
|
|
1275
|
-
)
|
|
1276
|
-
});
|
|
1277
|
-
await sleep(10);
|
|
1278
|
-
});
|
|
1279
|
-
|
|
1280
|
-
// The body fields should be included when the transport sends messages
|
|
1281
|
-
// We can verify by checking that the component rendered without errors
|
|
1282
|
-
// (the actual body merging is tested via the sent WS messages)
|
|
1283
|
-
expect(sentMessages).toBeDefined();
|
|
1284
|
-
});
|
|
1285
|
-
|
|
1286
|
-
it("should include dynamic body fields from function", async () => {
|
|
1287
|
-
const sentMessages: string[] = [];
|
|
1288
|
-
let callCount = 0;
|
|
1289
|
-
const agent = createAgent({
|
|
1290
|
-
name: "body-dynamic-test",
|
|
1291
|
-
url: "ws://localhost:3000/agents/chat/body-dynamic-test?_pk=abc",
|
|
1292
|
-
send: (data: string) => sentMessages.push(data)
|
|
1293
|
-
});
|
|
1294
|
-
|
|
1295
|
-
const TestComponent = () => {
|
|
1296
|
-
const chat = useAgentChat({
|
|
1297
|
-
agent,
|
|
1298
|
-
getInitialMessages: null,
|
|
1299
|
-
messages: [],
|
|
1300
|
-
body: () => {
|
|
1301
|
-
callCount++;
|
|
1302
|
-
return { timestamp: Date.now(), requestNumber: callCount };
|
|
1303
|
-
}
|
|
1304
|
-
});
|
|
1305
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
1306
|
-
};
|
|
1307
|
-
|
|
1308
|
-
await act(async () => {
|
|
1309
|
-
render(<TestComponent />, {
|
|
1310
|
-
wrapper: ({ children }) => (
|
|
1311
|
-
<StrictMode>
|
|
1312
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
1313
|
-
</StrictMode>
|
|
1314
|
-
)
|
|
1315
|
-
});
|
|
1316
|
-
await sleep(10);
|
|
1317
|
-
});
|
|
1318
|
-
|
|
1319
|
-
// Component should render without errors with function body
|
|
1320
|
-
expect(callCount).toBeDefined();
|
|
1321
|
-
});
|
|
1322
|
-
|
|
1323
|
-
it("should work alongside prepareSendMessagesRequest", async () => {
|
|
1324
|
-
const sentMessages: string[] = [];
|
|
1325
|
-
const agent = createAgent({
|
|
1326
|
-
name: "body-combined-test",
|
|
1327
|
-
url: "ws://localhost:3000/agents/chat/body-combined-test?_pk=abc",
|
|
1328
|
-
send: (data: string) => sentMessages.push(data)
|
|
1329
|
-
});
|
|
1330
|
-
|
|
1331
|
-
const prepareSendMessagesRequest = vi.fn(() => ({
|
|
1332
|
-
body: { fromPrepare: true }
|
|
1333
|
-
}));
|
|
1334
|
-
|
|
1335
|
-
const TestComponent = () => {
|
|
1336
|
-
const chat = useAgentChat({
|
|
1337
|
-
agent,
|
|
1338
|
-
getInitialMessages: null,
|
|
1339
|
-
messages: [],
|
|
1340
|
-
body: { fromBody: true },
|
|
1341
|
-
prepareSendMessagesRequest
|
|
1342
|
-
});
|
|
1343
|
-
return <div data-testid="messages-count">{chat.messages.length}</div>;
|
|
1344
|
-
};
|
|
1345
|
-
|
|
1346
|
-
await act(async () => {
|
|
1347
|
-
render(<TestComponent />, {
|
|
1348
|
-
wrapper: ({ children }) => (
|
|
1349
|
-
<StrictMode>
|
|
1350
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
1351
|
-
</StrictMode>
|
|
1352
|
-
)
|
|
1353
|
-
});
|
|
1354
|
-
await sleep(10);
|
|
1355
|
-
});
|
|
1356
|
-
|
|
1357
|
-
// Both body and prepareSendMessagesRequest should coexist without errors
|
|
1358
|
-
expect(sentMessages).toBeDefined();
|
|
1359
|
-
});
|
|
1360
|
-
});
|
|
1361
|
-
|
|
1362
|
-
describe("useAgentChat stale agent ref (issue #929)", () => {
|
|
1363
|
-
it("should use the new agent's send method after agent switch, not the old one", async () => {
|
|
1364
|
-
const oldSend = vi.fn();
|
|
1365
|
-
const newSend = vi.fn();
|
|
1366
|
-
|
|
1367
|
-
const agentOld = createAgent({
|
|
1368
|
-
name: "thread-old",
|
|
1369
|
-
url: "ws://localhost:3000/agents/chat/thread-old?_pk=old",
|
|
1370
|
-
send: oldSend
|
|
1371
|
-
});
|
|
1372
|
-
|
|
1373
|
-
const agentNew = createAgent({
|
|
1374
|
-
name: "thread-new",
|
|
1375
|
-
url: "ws://localhost:3000/agents/chat/thread-new?_pk=new",
|
|
1376
|
-
send: newSend
|
|
1377
|
-
});
|
|
1378
|
-
|
|
1379
|
-
let chatInstance: ReturnType<typeof useAgentChat> | null = null;
|
|
1380
|
-
|
|
1381
|
-
const TestComponent = ({
|
|
1382
|
-
agent
|
|
1383
|
-
}: {
|
|
1384
|
-
agent: ReturnType<typeof useAgent>;
|
|
1385
|
-
}) => {
|
|
1386
|
-
const chat = useAgentChat({
|
|
1387
|
-
agent,
|
|
1388
|
-
getInitialMessages: null,
|
|
1389
|
-
messages: [] as UIMessage[]
|
|
1390
|
-
});
|
|
1391
|
-
chatInstance = chat;
|
|
1392
|
-
return <div data-testid="status">{chat.status}</div>;
|
|
1393
|
-
};
|
|
1394
|
-
|
|
1395
|
-
const screen = await act(async () => {
|
|
1396
|
-
const screen = render(<TestComponent agent={agentOld} />, {
|
|
1397
|
-
wrapper: ({ children }) => (
|
|
1398
|
-
<StrictMode>
|
|
1399
|
-
<Suspense fallback="Loading...">{children}</Suspense>
|
|
1400
|
-
</StrictMode>
|
|
1401
|
-
)
|
|
1402
|
-
});
|
|
1403
|
-
await sleep(10);
|
|
1404
|
-
return screen;
|
|
1405
|
-
});
|
|
1406
|
-
|
|
1407
|
-
// Switch to the new agent
|
|
1408
|
-
await act(async () => {
|
|
1409
|
-
screen.rerender(<TestComponent agent={agentNew} />);
|
|
1410
|
-
await sleep(10);
|
|
1411
|
-
});
|
|
1412
|
-
|
|
1413
|
-
// Clear any sends that happened during setup (e.g., stream resume requests)
|
|
1414
|
-
oldSend.mockClear();
|
|
1415
|
-
newSend.mockClear();
|
|
1416
|
-
|
|
1417
|
-
// Clear history triggers agent.send() — this should go to the NEW agent
|
|
1418
|
-
await act(async () => {
|
|
1419
|
-
chatInstance!.clearHistory();
|
|
1420
|
-
await sleep(10);
|
|
1421
|
-
});
|
|
1422
|
-
|
|
1423
|
-
// The clear message should have been sent to the NEW agent, not the old one
|
|
1424
|
-
const newSendCalls = newSend.mock.calls
|
|
1425
|
-
.map((args) => JSON.parse(args[0] as string))
|
|
1426
|
-
.filter((m: Record<string, unknown>) => m.type === "cf_agent_chat_clear");
|
|
1427
|
-
expect(newSendCalls.length).toBe(1);
|
|
1428
|
-
|
|
1429
|
-
// The old agent should NOT have received the clear message
|
|
1430
|
-
const oldSendCalls = oldSend.mock.calls
|
|
1431
|
-
.map((args) => JSON.parse(args[0] as string))
|
|
1432
|
-
.filter((m: Record<string, unknown>) => m.type === "cf_agent_chat_clear");
|
|
1433
|
-
expect(oldSendCalls.length).toBe(0);
|
|
1434
|
-
});
|
|
1435
|
-
});
|