@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.
Files changed (67) hide show
  1. package/dist/experimental/forever.d.ts +4 -13
  2. package/dist/experimental/forever.js +1 -46
  3. package/dist/experimental/forever.js.map +1 -1
  4. package/dist/index.d.ts +59 -4
  5. package/dist/index.js +170 -28
  6. package/dist/index.js.map +1 -1
  7. package/dist/{message-builder-Bwc_Umye.js → message-builder-uSvexDl2.js} +17 -4
  8. package/dist/message-builder-uSvexDl2.js.map +1 -0
  9. package/dist/react.js +96 -10
  10. package/dist/react.js.map +1 -1
  11. package/dist/{resumable-stream-DqXxQVjf.d.ts → resumable-stream-DTLAk9XC.d.ts} +25 -2
  12. package/dist/types.d.ts +2 -1
  13. package/dist/types.js.map +1 -1
  14. package/package.json +7 -3
  15. package/CHANGELOG.md +0 -251
  16. package/dist/message-builder-Bwc_Umye.js.map +0 -1
  17. package/e2e/chat.spec.ts +0 -1703
  18. package/e2e/client-tools.spec.ts +0 -504
  19. package/e2e/llm.spec.ts +0 -637
  20. package/e2e/playwright.config.ts +0 -27
  21. package/e2e/worker.ts +0 -174
  22. package/e2e/wrangler.jsonc +0 -23
  23. package/scripts/build.ts +0 -32
  24. package/src/ai-chat-v5-migration.ts +0 -403
  25. package/src/experimental/forever.ts +0 -66
  26. package/src/index.ts +0 -2269
  27. package/src/message-builder.ts +0 -384
  28. package/src/react-tests/setup.ts +0 -3
  29. package/src/react-tests/use-agent-chat.test.tsx +0 -1435
  30. package/src/react-tests/vitest.config.ts +0 -20
  31. package/src/react.tsx +0 -1447
  32. package/src/resumable-stream.ts +0 -441
  33. package/src/tests/cancel-request.test.ts +0 -280
  34. package/src/tests/chat-context.test.ts +0 -85
  35. package/src/tests/chat-persistence.test.ts +0 -426
  36. package/src/tests/clear-history.test.ts +0 -132
  37. package/src/tests/client-tool-duplicate-message.test.ts +0 -1957
  38. package/src/tests/client-tools-broadcast.test.ts +0 -138
  39. package/src/tests/client-tools-continuation.test.ts +0 -314
  40. package/src/tests/client-tools-reconnect.test.ts +0 -143
  41. package/src/tests/cloudflare-test.d.ts +0 -5
  42. package/src/tests/custom-body-continuation.test.ts +0 -309
  43. package/src/tests/custom-body.test.ts +0 -186
  44. package/src/tests/destroy.test.ts +0 -57
  45. package/src/tests/get-messages-endpoint.test.ts +0 -153
  46. package/src/tests/max-persisted-messages.test.ts +0 -201
  47. package/src/tests/merge-server-state.test.ts +0 -343
  48. package/src/tests/message-builder.test.ts +0 -808
  49. package/src/tests/migration.test.ts +0 -282
  50. package/src/tests/non-sse-response.test.ts +0 -186
  51. package/src/tests/onfinish-cleanup.test.ts +0 -362
  52. package/src/tests/plaintext-response.test.ts +0 -87
  53. package/src/tests/request-id.test.ts +0 -312
  54. package/src/tests/resumable-streaming.test.ts +0 -758
  55. package/src/tests/row-size-guard.test.ts +0 -374
  56. package/src/tests/sanitize-messages.test.ts +0 -325
  57. package/src/tests/test-utils.ts +0 -39
  58. package/src/tests/tsconfig.json +0 -10
  59. package/src/tests/validate-messages.test.ts +0 -269
  60. package/src/tests/vitest.config.ts +0 -30
  61. package/src/tests/worker.ts +0 -456
  62. package/src/tests/wrangler.jsonc +0 -50
  63. package/src/tests/ws-transport-abort.test.ts +0 -198
  64. package/src/types.ts +0 -152
  65. package/src/ws-chat-transport.ts +0 -227
  66. package/tsconfig.json +0 -4
  67. package/vitest.config.ts +0 -7
@@ -1,362 +0,0 @@
1
- import { env } from "cloudflare:test";
2
- import { describe, it, expect } from "vitest";
3
- import { MessageType } from "../types";
4
- import { connectChatWS } from "./test-utils";
5
- import { getAgentByName } from "agents";
6
-
7
- describe("onFinish cleanup (framework-managed)", () => {
8
- it("cleans up abort controller after stream completes even without user passing onFinish", async () => {
9
- const room = crypto.randomUUID();
10
- const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
11
-
12
- let resolvePromise: (value: boolean) => void;
13
- const donePromise = new Promise<boolean>((res) => {
14
- resolvePromise = res;
15
- });
16
- const timeout = setTimeout(() => resolvePromise(false), 3000);
17
-
18
- ws.addEventListener("message", (e: MessageEvent) => {
19
- const data = JSON.parse(e.data as string);
20
- if (data.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE && data.done) {
21
- clearTimeout(timeout);
22
- resolvePromise(true);
23
- }
24
- });
25
-
26
- // Send first request
27
- ws.send(
28
- JSON.stringify({
29
- type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
30
- id: "req-cleanup-1",
31
- init: {
32
- method: "POST",
33
- body: JSON.stringify({
34
- messages: [
35
- {
36
- id: "msg-cleanup-1",
37
- role: "user",
38
- parts: [{ type: "text", text: "Hello" }]
39
- }
40
- ]
41
- })
42
- }
43
- })
44
- );
45
-
46
- const done = await donePromise;
47
- expect(done).toBe(true);
48
-
49
- // Send a second request to prove the agent is still healthy
50
- // (no leaked abort controllers causing issues)
51
- let resolveSecond: (value: boolean) => void;
52
- const secondDone = new Promise<boolean>((res) => {
53
- resolveSecond = res;
54
- });
55
- const timeout2 = setTimeout(() => resolveSecond(false), 3000);
56
-
57
- ws.addEventListener("message", (e: MessageEvent) => {
58
- const data = JSON.parse(e.data as string);
59
- if (
60
- data.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE &&
61
- data.done &&
62
- data.id === "req-cleanup-2"
63
- ) {
64
- clearTimeout(timeout2);
65
- resolveSecond(true);
66
- }
67
- });
68
-
69
- ws.send(
70
- JSON.stringify({
71
- type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
72
- id: "req-cleanup-2",
73
- init: {
74
- method: "POST",
75
- body: JSON.stringify({
76
- messages: [
77
- {
78
- id: "msg-cleanup-1",
79
- role: "user",
80
- parts: [{ type: "text", text: "Hello" }]
81
- },
82
- {
83
- id: "msg-cleanup-2",
84
- role: "user",
85
- parts: [{ type: "text", text: "Second message" }]
86
- }
87
- ]
88
- })
89
- }
90
- })
91
- );
92
-
93
- const second = await secondDone;
94
- expect(second).toBe(true);
95
-
96
- ws.close(1000);
97
- });
98
-
99
- it("cancellation still works after cleanup is moved to _reply", async () => {
100
- const room = crypto.randomUUID();
101
- const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
102
- await new Promise((r) => setTimeout(r, 50));
103
-
104
- // Send a request and immediately cancel it
105
- ws.send(
106
- JSON.stringify({
107
- type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
108
- id: "req-cancel-test",
109
- init: {
110
- method: "POST",
111
- body: JSON.stringify({
112
- messages: [
113
- {
114
- id: "cancel-1",
115
- role: "user",
116
- parts: [{ type: "text", text: "Cancel me" }]
117
- }
118
- ]
119
- })
120
- }
121
- })
122
- );
123
-
124
- ws.send(
125
- JSON.stringify({
126
- type: MessageType.CF_AGENT_CHAT_REQUEST_CANCEL,
127
- id: "req-cancel-test"
128
- })
129
- );
130
-
131
- // Wait a moment for processing
132
- await new Promise((r) => setTimeout(r, 500));
133
-
134
- // Agent should still be alive -- send another request
135
- let resolvePromise: (value: boolean) => void;
136
- const donePromise = new Promise<boolean>((res) => {
137
- resolvePromise = res;
138
- });
139
- const timeout = setTimeout(() => resolvePromise(false), 3000);
140
-
141
- ws.addEventListener("message", (e: MessageEvent) => {
142
- const data = JSON.parse(e.data as string);
143
- if (
144
- data.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE &&
145
- data.done &&
146
- data.id === "req-after-cancel"
147
- ) {
148
- clearTimeout(timeout);
149
- resolvePromise(true);
150
- }
151
- });
152
-
153
- ws.send(
154
- JSON.stringify({
155
- type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
156
- id: "req-after-cancel",
157
- init: {
158
- method: "POST",
159
- body: JSON.stringify({
160
- messages: [
161
- {
162
- id: "after-cancel-1",
163
- role: "user",
164
- parts: [{ type: "text", text: "Still alive?" }]
165
- }
166
- ]
167
- })
168
- }
169
- })
170
- );
171
-
172
- const done = await donePromise;
173
- expect(done).toBe(true);
174
-
175
- ws.close(1000);
176
- });
177
-
178
- it("multiple concurrent requests each get cleaned up", async () => {
179
- const room = crypto.randomUUID();
180
- const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
181
-
182
- const received: string[] = [];
183
- ws.addEventListener("message", (e: MessageEvent) => {
184
- try {
185
- const data = JSON.parse(e.data as string);
186
- if (data.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE && data.done) {
187
- received.push(data.id);
188
- }
189
- } catch {
190
- // ignore
191
- }
192
- });
193
-
194
- // Send two requests simultaneously
195
- ws.send(
196
- JSON.stringify({
197
- type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
198
- id: "req-concurrent-1",
199
- init: {
200
- method: "POST",
201
- body: JSON.stringify({
202
- messages: [
203
- {
204
- id: "conc-1",
205
- role: "user",
206
- parts: [{ type: "text", text: "First" }]
207
- }
208
- ]
209
- })
210
- }
211
- })
212
- );
213
-
214
- ws.send(
215
- JSON.stringify({
216
- type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
217
- id: "req-concurrent-2",
218
- init: {
219
- method: "POST",
220
- body: JSON.stringify({
221
- messages: [
222
- {
223
- id: "conc-1",
224
- role: "user",
225
- parts: [{ type: "text", text: "First" }]
226
- },
227
- {
228
- id: "conc-2",
229
- role: "user",
230
- parts: [{ type: "text", text: "Second" }]
231
- }
232
- ]
233
- })
234
- }
235
- })
236
- );
237
-
238
- // Wait for both to complete
239
- await new Promise((r) => setTimeout(r, 2000));
240
-
241
- // Both requests should have completed
242
- expect(received).toContain("req-concurrent-1");
243
- expect(received).toContain("req-concurrent-2");
244
-
245
- ws.close(1000);
246
- });
247
-
248
- it("abort controllers are cleaned up after stream completion (count returns to 0)", async () => {
249
- const room = crypto.randomUUID();
250
- const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
251
-
252
- const agentStub = await getAgentByName(env.TestChatAgent, room);
253
-
254
- // Initially no abort controllers
255
- expect(await agentStub.getAbortControllerCount()).toBe(0);
256
-
257
- let resolvePromise: (value: boolean) => void;
258
- const donePromise = new Promise<boolean>((res) => {
259
- resolvePromise = res;
260
- });
261
- const timeout = setTimeout(() => resolvePromise(false), 3000);
262
-
263
- ws.addEventListener("message", (e: MessageEvent) => {
264
- const data = JSON.parse(e.data as string);
265
- if (data.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE && data.done) {
266
- clearTimeout(timeout);
267
- resolvePromise(true);
268
- }
269
- });
270
-
271
- // Send a request
272
- ws.send(
273
- JSON.stringify({
274
- type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
275
- id: "req-ac-check",
276
- init: {
277
- method: "POST",
278
- body: JSON.stringify({
279
- messages: [
280
- {
281
- id: "ac-1",
282
- role: "user",
283
- parts: [{ type: "text", text: "Hello" }]
284
- }
285
- ]
286
- })
287
- }
288
- })
289
- );
290
-
291
- const done = await donePromise;
292
- expect(done).toBe(true);
293
-
294
- // Give _reply a moment to finish its cleanup after the stream completes
295
- await new Promise((r) => setTimeout(r, 100));
296
-
297
- // Abort controller should have been cleaned up by _reply
298
- // even though our test worker's onChatMessage does NOT pass onFinish to streamText
299
- expect(await agentStub.getAbortControllerCount()).toBe(0);
300
-
301
- ws.close(1000);
302
- });
303
-
304
- it("abort controllers are cleaned up after multiple sequential requests", async () => {
305
- const room = crypto.randomUUID();
306
- const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
307
-
308
- const agentStub = await getAgentByName(env.TestChatAgent, room);
309
-
310
- // Send 3 sequential requests
311
- for (let i = 0; i < 3; i++) {
312
- let resolvePromise: (value: boolean) => void;
313
- const donePromise = new Promise<boolean>((res) => {
314
- resolvePromise = res;
315
- });
316
- const timeout = setTimeout(() => resolvePromise(false), 3000);
317
-
318
- const listener = (e: MessageEvent) => {
319
- const data = JSON.parse(e.data as string);
320
- if (
321
- data.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE &&
322
- data.done &&
323
- data.id === `req-seq-${i}`
324
- ) {
325
- clearTimeout(timeout);
326
- resolvePromise(true);
327
- }
328
- };
329
- ws.addEventListener("message", listener);
330
-
331
- ws.send(
332
- JSON.stringify({
333
- type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
334
- id: `req-seq-${i}`,
335
- init: {
336
- method: "POST",
337
- body: JSON.stringify({
338
- messages: [
339
- {
340
- id: `seq-${i}`,
341
- role: "user",
342
- parts: [{ type: "text", text: `Message ${i}` }]
343
- }
344
- ]
345
- })
346
- }
347
- })
348
- );
349
-
350
- await donePromise;
351
- ws.removeEventListener("message", listener);
352
- }
353
-
354
- // Wait for cleanup
355
- await new Promise((r) => setTimeout(r, 100));
356
-
357
- // All 3 abort controllers should be cleaned up
358
- expect(await agentStub.getAbortControllerCount()).toBe(0);
359
-
360
- ws.close(1000);
361
- });
362
- });
@@ -1,87 +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
- describe("Plain text response handling", () => {
9
- it("produces a single text part for plain text responses", async () => {
10
- const room = crypto.randomUUID();
11
- const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
12
-
13
- const messages: unknown[] = [];
14
- let resolvePromise: (value: boolean) => void;
15
- const donePromise = new Promise<boolean>((res) => {
16
- resolvePromise = res;
17
- });
18
-
19
- const timeout = setTimeout(() => resolvePromise(false), 2000);
20
-
21
- ws.addEventListener("message", (e: MessageEvent) => {
22
- const data = JSON.parse(e.data as string);
23
- messages.push(data);
24
-
25
- if (data.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE && data.done) {
26
- clearTimeout(timeout);
27
- resolvePromise(true);
28
- }
29
- });
30
-
31
- // Send a chat request - the test worker returns plain text "Hello from chat agent!"
32
- const userMessage: ChatMessage = {
33
- id: "msg-plain-1",
34
- role: "user",
35
- parts: [{ type: "text", text: "Test" }]
36
- };
37
-
38
- ws.send(
39
- JSON.stringify({
40
- type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
41
- id: "req-plain-1",
42
- init: {
43
- method: "POST",
44
- body: JSON.stringify({ messages: [userMessage] })
45
- }
46
- })
47
- );
48
-
49
- const done = await donePromise;
50
- expect(done).toBe(true);
51
-
52
- // Verify the persisted message has a single text part (not multiple)
53
- const agentStub = await getAgentByName(env.TestChatAgent, room);
54
- const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
55
-
56
- const assistantMessages = persisted.filter((m) => m.role === "assistant");
57
- expect(assistantMessages.length).toBeGreaterThanOrEqual(1);
58
-
59
- // The last assistant message should have exactly 1 text part
60
- const lastAssistant = assistantMessages[assistantMessages.length - 1];
61
- const textParts = lastAssistant.parts.filter((p) => p.type === "text");
62
- expect(textParts.length).toBe(1);
63
-
64
- // The text should be the full response content
65
- const textPart = textParts[0] as { text: string };
66
- expect(textPart.text).toBe("Hello from chat agent!");
67
-
68
- // Verify the stream protocol events: text-start, text-delta(s), text-end
69
- const streamResponses = messages.filter(isUseChatResponseMessage);
70
- const nonEmptyResponses = streamResponses.filter(
71
- (m) =>
72
- "body" in m && typeof m.body === "string" && m.body.trim().length > 0
73
- );
74
-
75
- // Should have text-start, text-delta(s), text-end events
76
- const bodies = nonEmptyResponses.map((m) => JSON.parse(m.body as string));
77
- const types = bodies.map((b) => b.type);
78
-
79
- expect(types[0]).toBe("text-start");
80
- expect(types[types.length - 1]).toBe("text-end");
81
- expect(
82
- types.filter((t: string) => t === "text-delta").length
83
- ).toBeGreaterThanOrEqual(1);
84
-
85
- ws.close(1000);
86
- });
87
- });