@cloudflare/ai-chat 0.0.8 → 0.1.0
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 +89 -0
- package/README.md +225 -459
- package/dist/ai-chat-v5-migration.d.ts +9 -9
- package/dist/ai-chat-v5-migration.js +25 -4
- package/dist/ai-chat-v5-migration.js.map +1 -1
- package/dist/experimental/forever.d.ts +29 -0
- package/dist/experimental/forever.js +55 -0
- package/dist/experimental/forever.js.map +1 -0
- package/dist/index.d.ts +114 -76
- package/dist/index.js +745 -629
- package/dist/index.js.map +1 -1
- package/dist/message-builder-Bwc_Umye.js +231 -0
- package/dist/message-builder-Bwc_Umye.js.map +1 -0
- package/dist/react.d.ts +27 -9
- package/dist/react.js +244 -258
- package/dist/react.js.map +1 -1
- package/dist/resumable-stream-DqXxQVjf.d.ts +103 -0
- package/dist/types.d.ts +16 -4
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -1
- package/e2e/chat.spec.ts +1703 -0
- package/e2e/client-tools.spec.ts +504 -0
- package/e2e/llm.spec.ts +637 -0
- package/e2e/playwright.config.ts +24 -0
- package/e2e/worker.ts +176 -0
- package/e2e/wrangler.jsonc +23 -0
- package/package.json +12 -5
- package/scripts/build.ts +2 -1
- package/src/ai-chat-v5-migration.ts +32 -5
- package/src/experimental/forever.ts +66 -0
- package/src/index.ts +831 -1137
- package/src/message-builder.ts +384 -0
- package/src/react-tests/use-agent-chat.test.tsx +738 -2
- package/src/react.tsx +385 -461
- package/src/resumable-stream.ts +441 -0
- package/src/tests/chat-context.test.ts +1 -1
- package/src/tests/chat-persistence.test.ts +5 -5
- package/src/tests/clear-history.test.ts +132 -0
- package/src/tests/client-tool-duplicate-message.test.ts +457 -14
- package/src/tests/client-tools-broadcast.test.ts +2 -2
- package/src/tests/client-tools-continuation.test.ts +3 -3
- package/src/tests/client-tools-reconnect.test.ts +143 -0
- package/src/tests/custom-body-continuation.test.ts +309 -0
- package/src/tests/custom-body.test.ts +3 -3
- package/src/tests/destroy.test.ts +57 -0
- package/src/tests/get-messages-endpoint.test.ts +79 -0
- package/src/tests/max-persisted-messages.test.ts +201 -0
- package/src/tests/merge-server-state.test.ts +106 -0
- package/src/tests/message-builder.test.ts +808 -0
- package/src/tests/migration.test.ts +282 -0
- package/src/tests/non-sse-response.test.ts +2 -2
- package/src/tests/onfinish-cleanup.test.ts +362 -0
- package/src/tests/plaintext-response.test.ts +87 -0
- package/src/tests/resumable-streaming.test.ts +220 -14
- package/src/tests/row-size-guard.test.ts +374 -0
- package/src/tests/sanitize-messages.test.ts +215 -0
- package/src/tests/validate-messages.test.ts +269 -0
- package/src/tests/worker.ts +59 -24
- package/src/types.ts +17 -1
- package/src/ws-chat-transport.ts +203 -0
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
import { test, expect } from "@playwright/test";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* E2E tests for client-side tool results and auto-continuation.
|
|
5
|
+
* Uses ClientToolAgent which defines a tool without `execute`,
|
|
6
|
+
* so the LLM calls it but the server waits for CF_AGENT_TOOL_RESULT from the client.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const MessageType = {
|
|
10
|
+
CF_AGENT_USE_CHAT_REQUEST: "cf_agent_use_chat_request",
|
|
11
|
+
CF_AGENT_USE_CHAT_RESPONSE: "cf_agent_use_chat_response",
|
|
12
|
+
CF_AGENT_TOOL_RESULT: "cf_agent_tool_result",
|
|
13
|
+
CF_AGENT_TOOL_APPROVAL: "cf_agent_tool_approval",
|
|
14
|
+
CF_AGENT_MESSAGE_UPDATED: "cf_agent_message_updated"
|
|
15
|
+
} as const;
|
|
16
|
+
|
|
17
|
+
type WSMessage = {
|
|
18
|
+
type: string;
|
|
19
|
+
[key: string]: unknown;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function agentPath(baseURL: string, room: string) {
|
|
23
|
+
return `${baseURL.replace("http", "ws")}/agents/client-tool-agent/${room}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test.describe("Client-side tool results e2e", () => {
|
|
27
|
+
test.setTimeout(30_000);
|
|
28
|
+
|
|
29
|
+
test.beforeEach(async ({ page }) => {
|
|
30
|
+
await page.goto("about:blank");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("client tool round-trip: LLM calls tool, client sends result, server broadcasts update", async ({
|
|
34
|
+
page,
|
|
35
|
+
baseURL
|
|
36
|
+
}) => {
|
|
37
|
+
const room = crypto.randomUUID();
|
|
38
|
+
const wsUrl = agentPath(baseURL!, room);
|
|
39
|
+
|
|
40
|
+
// This test:
|
|
41
|
+
// 1. Sends a message that triggers the LLM to call getUserLocation
|
|
42
|
+
// 2. Waits for tool-input-available in the stream
|
|
43
|
+
// 3. Sends CF_AGENT_TOOL_RESULT with the "location"
|
|
44
|
+
// 4. Verifies CF_AGENT_MESSAGE_UPDATED is received with the output
|
|
45
|
+
const result = await page.evaluate(
|
|
46
|
+
({ url, MT }) => {
|
|
47
|
+
return new Promise<{
|
|
48
|
+
streamMessages: WSMessage[];
|
|
49
|
+
updatedMessages: WSMessage[];
|
|
50
|
+
toolCallId: string | null;
|
|
51
|
+
}>((resolve) => {
|
|
52
|
+
const ws = new WebSocket(url);
|
|
53
|
+
const streamMessages: WSMessage[] = [];
|
|
54
|
+
const updatedMessages: WSMessage[] = [];
|
|
55
|
+
let toolCallId: string | null = null;
|
|
56
|
+
let sentResult = false;
|
|
57
|
+
|
|
58
|
+
ws.onmessage = (e) => {
|
|
59
|
+
try {
|
|
60
|
+
const data = JSON.parse(e.data) as WSMessage;
|
|
61
|
+
|
|
62
|
+
if (data.type === MT.CF_AGENT_USE_CHAT_RESPONSE) {
|
|
63
|
+
streamMessages.push(data);
|
|
64
|
+
|
|
65
|
+
// Look for tool-input-available in the stream body
|
|
66
|
+
if (
|
|
67
|
+
!sentResult &&
|
|
68
|
+
typeof data.body === "string" &&
|
|
69
|
+
data.body.includes("tool-input-available")
|
|
70
|
+
) {
|
|
71
|
+
try {
|
|
72
|
+
const chunk = JSON.parse(data.body as string);
|
|
73
|
+
if (
|
|
74
|
+
chunk.type === "tool-input-available" &&
|
|
75
|
+
chunk.toolCallId
|
|
76
|
+
) {
|
|
77
|
+
toolCallId = chunk.toolCallId;
|
|
78
|
+
// Send tool result back to server
|
|
79
|
+
ws.send(
|
|
80
|
+
JSON.stringify({
|
|
81
|
+
type: MT.CF_AGENT_TOOL_RESULT,
|
|
82
|
+
toolCallId: chunk.toolCallId,
|
|
83
|
+
toolName: "getUserLocation",
|
|
84
|
+
output: {
|
|
85
|
+
lat: 51.5074,
|
|
86
|
+
lng: -0.1278,
|
|
87
|
+
city: "London"
|
|
88
|
+
},
|
|
89
|
+
autoContinue: false
|
|
90
|
+
})
|
|
91
|
+
);
|
|
92
|
+
sentResult = true;
|
|
93
|
+
}
|
|
94
|
+
} catch {
|
|
95
|
+
// not JSON
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Check for done
|
|
100
|
+
if (data.done) {
|
|
101
|
+
// Wait a bit for MESSAGE_UPDATED to arrive
|
|
102
|
+
setTimeout(() => {
|
|
103
|
+
ws.close();
|
|
104
|
+
resolve({ streamMessages, updatedMessages, toolCallId });
|
|
105
|
+
}, 1000);
|
|
106
|
+
}
|
|
107
|
+
} else if (data.type === MT.CF_AGENT_MESSAGE_UPDATED) {
|
|
108
|
+
updatedMessages.push(data);
|
|
109
|
+
}
|
|
110
|
+
} catch {
|
|
111
|
+
// ignore
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
ws.onopen = () => {
|
|
116
|
+
ws.send(
|
|
117
|
+
JSON.stringify({
|
|
118
|
+
type: MT.CF_AGENT_USE_CHAT_REQUEST,
|
|
119
|
+
id: "req-client-tool",
|
|
120
|
+
init: {
|
|
121
|
+
method: "POST",
|
|
122
|
+
body: JSON.stringify({
|
|
123
|
+
messages: [
|
|
124
|
+
{
|
|
125
|
+
id: "msg-ct-1",
|
|
126
|
+
role: "user",
|
|
127
|
+
parts: [
|
|
128
|
+
{
|
|
129
|
+
type: "text",
|
|
130
|
+
text: "What is my current location? Use the getUserLocation tool."
|
|
131
|
+
}
|
|
132
|
+
]
|
|
133
|
+
}
|
|
134
|
+
]
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
setTimeout(() => {
|
|
142
|
+
ws.close();
|
|
143
|
+
resolve({ streamMessages, updatedMessages, toolCallId });
|
|
144
|
+
}, 20000);
|
|
145
|
+
});
|
|
146
|
+
},
|
|
147
|
+
{ url: wsUrl, MT: MessageType }
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
// The LLM should have called the tool
|
|
151
|
+
expect(result.toolCallId).toBeTruthy();
|
|
152
|
+
|
|
153
|
+
// The server now broadcasts CF_AGENT_MESSAGE_UPDATED for streaming
|
|
154
|
+
// messages too, so clients get immediate confirmation.
|
|
155
|
+
expect(result.updatedMessages.length).toBeGreaterThanOrEqual(1);
|
|
156
|
+
|
|
157
|
+
const updateMsg = result.updatedMessages[0];
|
|
158
|
+
expect(updateMsg.type).toBe(MessageType.CF_AGENT_MESSAGE_UPDATED);
|
|
159
|
+
const message = updateMsg.message as {
|
|
160
|
+
parts: Array<{
|
|
161
|
+
toolCallId?: string;
|
|
162
|
+
state?: string;
|
|
163
|
+
output?: unknown;
|
|
164
|
+
}>;
|
|
165
|
+
};
|
|
166
|
+
const toolPart = message.parts.find(
|
|
167
|
+
(p) => p.toolCallId === result.toolCallId
|
|
168
|
+
);
|
|
169
|
+
expect(toolPart).toBeTruthy();
|
|
170
|
+
expect(toolPart!.state).toBe("output-available");
|
|
171
|
+
expect(toolPart!.output).toEqual({
|
|
172
|
+
lat: 51.5074,
|
|
173
|
+
lng: -0.1278,
|
|
174
|
+
city: "London"
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Also verify persistence after stream completes
|
|
178
|
+
const res = await page.request.get(
|
|
179
|
+
`${baseURL}/agents/client-tool-agent/${room}/get-messages`
|
|
180
|
+
);
|
|
181
|
+
expect(res.ok()).toBe(true);
|
|
182
|
+
const persisted = await res.json();
|
|
183
|
+
const assistantMsgs = persisted.filter(
|
|
184
|
+
(m: { role: string }) => m.role === "assistant"
|
|
185
|
+
);
|
|
186
|
+
expect(assistantMsgs.length).toBeGreaterThanOrEqual(1);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("auto-continuation: server continues conversation after receiving tool result", async ({
|
|
190
|
+
page,
|
|
191
|
+
baseURL
|
|
192
|
+
}) => {
|
|
193
|
+
const room = crypto.randomUUID();
|
|
194
|
+
const wsUrl = agentPath(baseURL!, room);
|
|
195
|
+
|
|
196
|
+
const result = await page.evaluate(
|
|
197
|
+
({ url, MT }) => {
|
|
198
|
+
return new Promise<{
|
|
199
|
+
allMessages: WSMessage[];
|
|
200
|
+
continuationReceived: boolean;
|
|
201
|
+
toolCallId: string | null;
|
|
202
|
+
}>((resolve) => {
|
|
203
|
+
const ws = new WebSocket(url);
|
|
204
|
+
const allMessages: WSMessage[] = [];
|
|
205
|
+
let toolCallId: string | null = null;
|
|
206
|
+
let sentResult = false;
|
|
207
|
+
let doneCount = 0;
|
|
208
|
+
let continuationReceived = false;
|
|
209
|
+
|
|
210
|
+
ws.onmessage = (e) => {
|
|
211
|
+
try {
|
|
212
|
+
const data = JSON.parse(e.data) as WSMessage;
|
|
213
|
+
allMessages.push(data);
|
|
214
|
+
|
|
215
|
+
if (data.type === MT.CF_AGENT_USE_CHAT_RESPONSE) {
|
|
216
|
+
// Check for continuation flag
|
|
217
|
+
if (data.continuation === true) {
|
|
218
|
+
continuationReceived = true;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Look for tool-input-available
|
|
222
|
+
if (
|
|
223
|
+
!sentResult &&
|
|
224
|
+
typeof data.body === "string" &&
|
|
225
|
+
data.body.includes("tool-input-available")
|
|
226
|
+
) {
|
|
227
|
+
try {
|
|
228
|
+
const chunk = JSON.parse(data.body as string);
|
|
229
|
+
if (
|
|
230
|
+
chunk.type === "tool-input-available" &&
|
|
231
|
+
chunk.toolCallId
|
|
232
|
+
) {
|
|
233
|
+
toolCallId = chunk.toolCallId;
|
|
234
|
+
// Send tool result with autoContinue=true
|
|
235
|
+
ws.send(
|
|
236
|
+
JSON.stringify({
|
|
237
|
+
type: MT.CF_AGENT_TOOL_RESULT,
|
|
238
|
+
toolCallId: chunk.toolCallId,
|
|
239
|
+
toolName: "getUserLocation",
|
|
240
|
+
output: { city: "Paris", lat: 48.8566, lng: 2.3522 },
|
|
241
|
+
autoContinue: true
|
|
242
|
+
})
|
|
243
|
+
);
|
|
244
|
+
sentResult = true;
|
|
245
|
+
}
|
|
246
|
+
} catch {
|
|
247
|
+
// not JSON
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (data.done) {
|
|
252
|
+
doneCount++;
|
|
253
|
+
// With auto-continuation, we expect 2 done signals:
|
|
254
|
+
// 1st from the original stream, 2nd from the continuation.
|
|
255
|
+
// Wait for both, but also handle the case where continuation
|
|
256
|
+
// arrives in a single stream.
|
|
257
|
+
if (
|
|
258
|
+
doneCount >= 2 ||
|
|
259
|
+
(doneCount >= 1 && continuationReceived)
|
|
260
|
+
) {
|
|
261
|
+
setTimeout(() => {
|
|
262
|
+
ws.close();
|
|
263
|
+
resolve({
|
|
264
|
+
allMessages,
|
|
265
|
+
continuationReceived,
|
|
266
|
+
toolCallId
|
|
267
|
+
});
|
|
268
|
+
}, 500);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
} catch {
|
|
273
|
+
// ignore
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
ws.onopen = () => {
|
|
278
|
+
ws.send(
|
|
279
|
+
JSON.stringify({
|
|
280
|
+
type: MT.CF_AGENT_USE_CHAT_REQUEST,
|
|
281
|
+
id: "req-auto-cont",
|
|
282
|
+
init: {
|
|
283
|
+
method: "POST",
|
|
284
|
+
body: JSON.stringify({
|
|
285
|
+
messages: [
|
|
286
|
+
{
|
|
287
|
+
id: "msg-ac-1",
|
|
288
|
+
role: "user",
|
|
289
|
+
parts: [
|
|
290
|
+
{
|
|
291
|
+
type: "text",
|
|
292
|
+
text: "Where am I? Use the getUserLocation tool."
|
|
293
|
+
}
|
|
294
|
+
]
|
|
295
|
+
}
|
|
296
|
+
]
|
|
297
|
+
})
|
|
298
|
+
}
|
|
299
|
+
})
|
|
300
|
+
);
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
setTimeout(() => {
|
|
304
|
+
ws.close();
|
|
305
|
+
resolve({ allMessages, continuationReceived, toolCallId });
|
|
306
|
+
}, 25000);
|
|
307
|
+
});
|
|
308
|
+
},
|
|
309
|
+
{ url: wsUrl, MT: MessageType }
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
expect(result.toolCallId).toBeTruthy();
|
|
313
|
+
|
|
314
|
+
// With autoContinue=true, the server should have sent a continuation stream
|
|
315
|
+
// The continuation messages have continuation: true flag
|
|
316
|
+
expect(result.continuationReceived).toBe(true);
|
|
317
|
+
|
|
318
|
+
// The continuation should include text from the LLM responding to the tool result
|
|
319
|
+
const continuationChunks = result.allMessages.filter(
|
|
320
|
+
(m) =>
|
|
321
|
+
m.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE &&
|
|
322
|
+
m.continuation === true &&
|
|
323
|
+
typeof m.body === "string" &&
|
|
324
|
+
(m.body as string).trim()
|
|
325
|
+
);
|
|
326
|
+
expect(continuationChunks.length).toBeGreaterThan(0);
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
test.describe("Tool approval auto-continuation e2e", () => {
|
|
331
|
+
test.setTimeout(30_000);
|
|
332
|
+
|
|
333
|
+
test.beforeEach(async ({ page }) => {
|
|
334
|
+
await page.goto("about:blank");
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
test("tool approval with autoContinue triggers continuation stream", async ({
|
|
338
|
+
page,
|
|
339
|
+
baseURL
|
|
340
|
+
}) => {
|
|
341
|
+
const room = crypto.randomUUID();
|
|
342
|
+
const wsUrl = agentPath(baseURL!, room);
|
|
343
|
+
|
|
344
|
+
// This test:
|
|
345
|
+
// 1. Sends a message that triggers the LLM to call getUserLocation
|
|
346
|
+
// 2. Waits for tool-input-available in the stream
|
|
347
|
+
// 3. Sends CF_AGENT_TOOL_APPROVAL (instead of TOOL_RESULT) with autoContinue
|
|
348
|
+
// 4. Verifies continuation messages are received
|
|
349
|
+
const result = await page.evaluate(
|
|
350
|
+
({ url, MT }) => {
|
|
351
|
+
return new Promise<{
|
|
352
|
+
allMessages: WSMessage[];
|
|
353
|
+
continuationReceived: boolean;
|
|
354
|
+
toolCallId: string | null;
|
|
355
|
+
approvalSent: boolean;
|
|
356
|
+
}>((resolve) => {
|
|
357
|
+
const ws = new WebSocket(url);
|
|
358
|
+
const allMessages: WSMessage[] = [];
|
|
359
|
+
let toolCallId: string | null = null;
|
|
360
|
+
let sentApproval = false;
|
|
361
|
+
let doneCount = 0;
|
|
362
|
+
let continuationReceived = false;
|
|
363
|
+
|
|
364
|
+
ws.onmessage = (e) => {
|
|
365
|
+
try {
|
|
366
|
+
const data = JSON.parse(e.data) as WSMessage;
|
|
367
|
+
allMessages.push(data);
|
|
368
|
+
|
|
369
|
+
if (data.type === MT.CF_AGENT_USE_CHAT_RESPONSE) {
|
|
370
|
+
if (data.continuation === true) {
|
|
371
|
+
continuationReceived = true;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Look for tool-input-available
|
|
375
|
+
if (
|
|
376
|
+
!sentApproval &&
|
|
377
|
+
typeof data.body === "string" &&
|
|
378
|
+
data.body.includes("tool-input-available")
|
|
379
|
+
) {
|
|
380
|
+
try {
|
|
381
|
+
const chunk = JSON.parse(data.body as string);
|
|
382
|
+
if (
|
|
383
|
+
chunk.type === "tool-input-available" &&
|
|
384
|
+
chunk.toolCallId
|
|
385
|
+
) {
|
|
386
|
+
toolCallId = chunk.toolCallId;
|
|
387
|
+
// Send tool APPROVAL with autoContinue
|
|
388
|
+
ws.send(
|
|
389
|
+
JSON.stringify({
|
|
390
|
+
type: MT.CF_AGENT_TOOL_APPROVAL,
|
|
391
|
+
toolCallId: chunk.toolCallId,
|
|
392
|
+
approved: true,
|
|
393
|
+
autoContinue: true
|
|
394
|
+
})
|
|
395
|
+
);
|
|
396
|
+
sentApproval = true;
|
|
397
|
+
}
|
|
398
|
+
} catch {
|
|
399
|
+
// not JSON
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (data.done) {
|
|
404
|
+
doneCount++;
|
|
405
|
+
// With auto-continuation, we expect 2 done signals
|
|
406
|
+
if (
|
|
407
|
+
doneCount >= 2 ||
|
|
408
|
+
(doneCount >= 1 && continuationReceived)
|
|
409
|
+
) {
|
|
410
|
+
setTimeout(() => {
|
|
411
|
+
ws.close();
|
|
412
|
+
resolve({
|
|
413
|
+
allMessages,
|
|
414
|
+
continuationReceived,
|
|
415
|
+
toolCallId,
|
|
416
|
+
approvalSent: sentApproval
|
|
417
|
+
});
|
|
418
|
+
}, 500);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
} catch {
|
|
423
|
+
// ignore
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
ws.onopen = () => {
|
|
428
|
+
ws.send(
|
|
429
|
+
JSON.stringify({
|
|
430
|
+
type: MT.CF_AGENT_USE_CHAT_REQUEST,
|
|
431
|
+
id: "req-approval-cont",
|
|
432
|
+
init: {
|
|
433
|
+
method: "POST",
|
|
434
|
+
body: JSON.stringify({
|
|
435
|
+
messages: [
|
|
436
|
+
{
|
|
437
|
+
id: "msg-appr-1",
|
|
438
|
+
role: "user",
|
|
439
|
+
parts: [
|
|
440
|
+
{
|
|
441
|
+
type: "text",
|
|
442
|
+
text: "Where am I? Use the getUserLocation tool."
|
|
443
|
+
}
|
|
444
|
+
]
|
|
445
|
+
}
|
|
446
|
+
]
|
|
447
|
+
})
|
|
448
|
+
}
|
|
449
|
+
})
|
|
450
|
+
);
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
setTimeout(() => {
|
|
454
|
+
ws.close();
|
|
455
|
+
resolve({
|
|
456
|
+
allMessages,
|
|
457
|
+
continuationReceived,
|
|
458
|
+
toolCallId,
|
|
459
|
+
approvalSent: sentApproval
|
|
460
|
+
});
|
|
461
|
+
}, 25000);
|
|
462
|
+
});
|
|
463
|
+
},
|
|
464
|
+
{ url: wsUrl, MT: MessageType }
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
// The LLM should have called the tool
|
|
468
|
+
expect(result.toolCallId).toBeTruthy();
|
|
469
|
+
expect(result.approvalSent).toBe(true);
|
|
470
|
+
|
|
471
|
+
// With autoContinue=true on approval, the server should send continuation
|
|
472
|
+
expect(result.continuationReceived).toBe(true);
|
|
473
|
+
|
|
474
|
+
// The continuation should include response chunks
|
|
475
|
+
const continuationChunks = result.allMessages.filter(
|
|
476
|
+
(m) =>
|
|
477
|
+
m.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE &&
|
|
478
|
+
m.continuation === true &&
|
|
479
|
+
typeof m.body === "string" &&
|
|
480
|
+
(m.body as string).trim()
|
|
481
|
+
);
|
|
482
|
+
expect(continuationChunks.length).toBeGreaterThan(0);
|
|
483
|
+
|
|
484
|
+
// Verify persistence
|
|
485
|
+
const res = await page.request.get(
|
|
486
|
+
`${baseURL}/agents/client-tool-agent/${room}/get-messages`
|
|
487
|
+
);
|
|
488
|
+
expect(res.ok()).toBe(true);
|
|
489
|
+
const persisted = await res.json();
|
|
490
|
+
const assistantMsgs = persisted.filter(
|
|
491
|
+
(m: { role: string }) => m.role === "assistant"
|
|
492
|
+
);
|
|
493
|
+
expect(assistantMsgs.length).toBeGreaterThanOrEqual(1);
|
|
494
|
+
|
|
495
|
+
// The tool part should be in approval-responded state
|
|
496
|
+
const assistantMsg = assistantMsgs[0];
|
|
497
|
+
const toolPart = assistantMsg.parts.find(
|
|
498
|
+
(p: { toolCallId?: string }) => p.toolCallId === result.toolCallId
|
|
499
|
+
);
|
|
500
|
+
expect(toolPart).toBeTruthy();
|
|
501
|
+
expect(toolPart.state).toBe("approval-responded");
|
|
502
|
+
expect(toolPart.approval).toEqual({ approved: true });
|
|
503
|
+
});
|
|
504
|
+
});
|