@cloudflare/ai-chat 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 +150 -36
- 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,312 +0,0 @@
|
|
|
1
|
-
import { env } from "cloudflare:test";
|
|
2
|
-
import { describe, it, expect } from "vitest";
|
|
3
|
-
import { MessageType } from "../types";
|
|
4
|
-
import type { UIMessage as ChatMessage } from "ai";
|
|
5
|
-
import { connectChatWS, isUseChatResponseMessage } from "./test-utils";
|
|
6
|
-
import { getAgentByName } from "agents";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Helper: send a chat request and wait for the done response.
|
|
10
|
-
* Returns the request ID that was sent.
|
|
11
|
-
*/
|
|
12
|
-
async function sendChatAndWaitForDone(
|
|
13
|
-
ws: WebSocket,
|
|
14
|
-
requestId: string,
|
|
15
|
-
messages: ChatMessage[],
|
|
16
|
-
extraBody?: Record<string, unknown>
|
|
17
|
-
): Promise<void> {
|
|
18
|
-
let resolvePromise: (value: boolean) => void;
|
|
19
|
-
const donePromise = new Promise<boolean>((res) => {
|
|
20
|
-
resolvePromise = res;
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
const timeout = setTimeout(() => resolvePromise(false), 2000);
|
|
24
|
-
|
|
25
|
-
const handler = (e: MessageEvent) => {
|
|
26
|
-
const data = JSON.parse(e.data as string);
|
|
27
|
-
if (data.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE && data.done) {
|
|
28
|
-
clearTimeout(timeout);
|
|
29
|
-
resolvePromise(true);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
ws.addEventListener("message", handler);
|
|
33
|
-
|
|
34
|
-
ws.send(
|
|
35
|
-
JSON.stringify({
|
|
36
|
-
type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
|
|
37
|
-
id: requestId,
|
|
38
|
-
init: {
|
|
39
|
-
method: "POST",
|
|
40
|
-
body: JSON.stringify({ messages, ...extraBody })
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
const done = await donePromise;
|
|
46
|
-
ws.removeEventListener("message", handler);
|
|
47
|
-
expect(done).toBe(true);
|
|
48
|
-
|
|
49
|
-
// Allow handler to finish
|
|
50
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
describe("requestId in OnChatMessageOptions", () => {
|
|
54
|
-
it("should pass the client-sent request ID on initial chat messages", async () => {
|
|
55
|
-
const room = crypto.randomUUID();
|
|
56
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
57
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
58
|
-
await agentStub.clearCapturedContext();
|
|
59
|
-
|
|
60
|
-
const userMessage: ChatMessage = {
|
|
61
|
-
id: "msg1",
|
|
62
|
-
role: "user",
|
|
63
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
await sendChatAndWaitForDone(ws, "my-request-123", [userMessage]);
|
|
67
|
-
|
|
68
|
-
const capturedId = await agentStub.getCapturedRequestId();
|
|
69
|
-
expect(capturedId).toBe("my-request-123");
|
|
70
|
-
|
|
71
|
-
ws.close(1000);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("should pass a server-generated requestId on tool result continuation", async () => {
|
|
75
|
-
const room = crypto.randomUUID();
|
|
76
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
77
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
78
|
-
|
|
79
|
-
const userMessage: ChatMessage = {
|
|
80
|
-
id: "msg1",
|
|
81
|
-
role: "user",
|
|
82
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
// Step 1: Send initial request so context is established
|
|
86
|
-
await sendChatAndWaitForDone(ws, "initial-req", [userMessage]);
|
|
87
|
-
|
|
88
|
-
// Step 2: Persist a tool call in input-available state
|
|
89
|
-
const toolCallId = "call_reqid_test";
|
|
90
|
-
await agentStub.persistMessages([
|
|
91
|
-
userMessage,
|
|
92
|
-
{
|
|
93
|
-
id: "assistant-1",
|
|
94
|
-
role: "assistant",
|
|
95
|
-
parts: [
|
|
96
|
-
{
|
|
97
|
-
type: "tool-testTool",
|
|
98
|
-
toolCallId,
|
|
99
|
-
state: "input-available",
|
|
100
|
-
input: {}
|
|
101
|
-
}
|
|
102
|
-
] as ChatMessage["parts"]
|
|
103
|
-
}
|
|
104
|
-
]);
|
|
105
|
-
|
|
106
|
-
// Step 3: Clear captured state before continuation
|
|
107
|
-
await agentStub.clearCapturedContext();
|
|
108
|
-
|
|
109
|
-
// Step 4: Send tool result with autoContinue
|
|
110
|
-
ws.send(
|
|
111
|
-
JSON.stringify({
|
|
112
|
-
type: "cf_agent_tool_result",
|
|
113
|
-
toolCallId,
|
|
114
|
-
toolName: "testTool",
|
|
115
|
-
output: { success: true },
|
|
116
|
-
autoContinue: true
|
|
117
|
-
})
|
|
118
|
-
);
|
|
119
|
-
|
|
120
|
-
// Wait for continuation (500ms stream wait + processing)
|
|
121
|
-
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
122
|
-
|
|
123
|
-
// Step 5: Verify a requestId was passed (server-generated, so just check it exists)
|
|
124
|
-
const capturedId = await agentStub.getCapturedRequestId();
|
|
125
|
-
expect(capturedId).toBeDefined();
|
|
126
|
-
expect(typeof capturedId).toBe("string");
|
|
127
|
-
expect(capturedId!.length).toBeGreaterThan(0);
|
|
128
|
-
// Should NOT be the initial request ID — it's a new continuation ID
|
|
129
|
-
expect(capturedId).not.toBe("initial-req");
|
|
130
|
-
|
|
131
|
-
ws.close(1000);
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it("should pass a server-generated requestId on tool approval continuation", async () => {
|
|
135
|
-
const room = crypto.randomUUID();
|
|
136
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
137
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
138
|
-
|
|
139
|
-
const userMessage: ChatMessage = {
|
|
140
|
-
id: "msg1",
|
|
141
|
-
role: "user",
|
|
142
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
// Step 1: Send initial request
|
|
146
|
-
await sendChatAndWaitForDone(ws, "initial-req", [userMessage]);
|
|
147
|
-
|
|
148
|
-
// Step 2: Persist a tool call in approval-required state
|
|
149
|
-
const toolCallId = "call_approval_test";
|
|
150
|
-
await agentStub.persistMessages([
|
|
151
|
-
userMessage,
|
|
152
|
-
{
|
|
153
|
-
id: "assistant-1",
|
|
154
|
-
role: "assistant",
|
|
155
|
-
parts: [
|
|
156
|
-
{
|
|
157
|
-
type: "tool-testTool",
|
|
158
|
-
toolCallId,
|
|
159
|
-
state: "input-available",
|
|
160
|
-
input: {}
|
|
161
|
-
}
|
|
162
|
-
] as ChatMessage["parts"]
|
|
163
|
-
}
|
|
164
|
-
]);
|
|
165
|
-
|
|
166
|
-
// Step 3: Clear captured state
|
|
167
|
-
await agentStub.clearCapturedContext();
|
|
168
|
-
|
|
169
|
-
// Step 4: Send approval with autoContinue
|
|
170
|
-
ws.send(
|
|
171
|
-
JSON.stringify({
|
|
172
|
-
type: "cf_agent_tool_approval",
|
|
173
|
-
toolCallId,
|
|
174
|
-
approved: true,
|
|
175
|
-
autoContinue: true
|
|
176
|
-
})
|
|
177
|
-
);
|
|
178
|
-
|
|
179
|
-
// Wait for continuation
|
|
180
|
-
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
181
|
-
|
|
182
|
-
// Step 5: Verify requestId was passed
|
|
183
|
-
const capturedId = await agentStub.getCapturedRequestId();
|
|
184
|
-
expect(capturedId).toBeDefined();
|
|
185
|
-
expect(typeof capturedId).toBe("string");
|
|
186
|
-
expect(capturedId!.length).toBeGreaterThan(0);
|
|
187
|
-
expect(capturedId).not.toBe("initial-req");
|
|
188
|
-
|
|
189
|
-
ws.close(1000);
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
it("should tag response messages with requestId so the client can match them", async () => {
|
|
193
|
-
const room = crypto.randomUUID();
|
|
194
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
195
|
-
|
|
196
|
-
const receivedMessages: Array<{ id: string; done: boolean }> = [];
|
|
197
|
-
|
|
198
|
-
ws.addEventListener("message", (e: MessageEvent) => {
|
|
199
|
-
const data = JSON.parse(e.data as string);
|
|
200
|
-
if (isUseChatResponseMessage(data)) {
|
|
201
|
-
receivedMessages.push({ id: data.id, done: !!data.done });
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
const userMessage: ChatMessage = {
|
|
206
|
-
id: "msg1",
|
|
207
|
-
role: "user",
|
|
208
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
ws.send(
|
|
212
|
-
JSON.stringify({
|
|
213
|
-
type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
|
|
214
|
-
id: "tagged-req-42",
|
|
215
|
-
init: {
|
|
216
|
-
method: "POST",
|
|
217
|
-
body: JSON.stringify({ messages: [userMessage] })
|
|
218
|
-
}
|
|
219
|
-
})
|
|
220
|
-
);
|
|
221
|
-
|
|
222
|
-
// Wait for the done message
|
|
223
|
-
await new Promise<void>((resolve) => {
|
|
224
|
-
const interval = setInterval(() => {
|
|
225
|
-
if (receivedMessages.some((m) => m.done)) {
|
|
226
|
-
clearInterval(interval);
|
|
227
|
-
resolve();
|
|
228
|
-
}
|
|
229
|
-
}, 50);
|
|
230
|
-
setTimeout(() => {
|
|
231
|
-
clearInterval(interval);
|
|
232
|
-
resolve();
|
|
233
|
-
}, 2000);
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
// ALL response messages should be tagged with the same request ID
|
|
237
|
-
expect(receivedMessages.length).toBeGreaterThan(0);
|
|
238
|
-
for (const msg of receivedMessages) {
|
|
239
|
-
expect(msg.id).toBe("tagged-req-42");
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
ws.close(1000);
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
it("should allow handlers to send error responses using requestId (pre-stream failure use case)", async () => {
|
|
246
|
-
// This is the primary use case from the PR:
|
|
247
|
-
// An onChatMessage handler validates the request body BEFORE starting a stream.
|
|
248
|
-
// If validation fails, it needs the requestId to send a properly-tagged error
|
|
249
|
-
// message that the client transport will accept (it filters by data.id === requestId).
|
|
250
|
-
//
|
|
251
|
-
// We test this by verifying that requestId is available and matches the
|
|
252
|
-
// wire-protocol ID, so a handler *could* send:
|
|
253
|
-
// connection.send(JSON.stringify({
|
|
254
|
-
// type: "cf_agent_use_chat_response",
|
|
255
|
-
// id: options.requestId,
|
|
256
|
-
// body: "validation error",
|
|
257
|
-
// done: true,
|
|
258
|
-
// error: true
|
|
259
|
-
// }));
|
|
260
|
-
|
|
261
|
-
const room = crypto.randomUUID();
|
|
262
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
263
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
264
|
-
await agentStub.clearCapturedContext();
|
|
265
|
-
|
|
266
|
-
const userMessage: ChatMessage = {
|
|
267
|
-
id: "msg1",
|
|
268
|
-
role: "user",
|
|
269
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
const clientRequestId = "client-error-test-id";
|
|
273
|
-
await sendChatAndWaitForDone(ws, clientRequestId, [userMessage]);
|
|
274
|
-
|
|
275
|
-
// Verify the requestId received in onChatMessage matches exactly
|
|
276
|
-
// what the client sent as data.id — this is the contract that lets
|
|
277
|
-
// handlers send tagged error responses
|
|
278
|
-
const capturedId = await agentStub.getCapturedRequestId();
|
|
279
|
-
expect(capturedId).toBe(clientRequestId);
|
|
280
|
-
|
|
281
|
-
ws.close(1000);
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
it("should pass requestId with different values across sequential requests", async () => {
|
|
285
|
-
const room = crypto.randomUUID();
|
|
286
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
287
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
288
|
-
|
|
289
|
-
const userMessage: ChatMessage = {
|
|
290
|
-
id: "msg1",
|
|
291
|
-
role: "user",
|
|
292
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
// First request
|
|
296
|
-
await agentStub.clearCapturedContext();
|
|
297
|
-
await sendChatAndWaitForDone(ws, "req-alpha", [userMessage]);
|
|
298
|
-
const firstId = await agentStub.getCapturedRequestId();
|
|
299
|
-
expect(firstId).toBe("req-alpha");
|
|
300
|
-
|
|
301
|
-
// Second request with different ID
|
|
302
|
-
await agentStub.clearCapturedContext();
|
|
303
|
-
await sendChatAndWaitForDone(ws, "req-beta", [userMessage]);
|
|
304
|
-
const secondId = await agentStub.getCapturedRequestId();
|
|
305
|
-
expect(secondId).toBe("req-beta");
|
|
306
|
-
|
|
307
|
-
// They should be different
|
|
308
|
-
expect(firstId).not.toBe(secondId);
|
|
309
|
-
|
|
310
|
-
ws.close(1000);
|
|
311
|
-
});
|
|
312
|
-
});
|