@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,808 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import {
3
- applyChunkToParts,
4
- type MessageParts,
5
- type StreamChunkData
6
- } from "../message-builder";
7
-
8
- function makeParts(): MessageParts {
9
- return [];
10
- }
11
-
12
- describe("applyChunkToParts", () => {
13
- describe("text chunks", () => {
14
- it("text-start creates a streaming text part", () => {
15
- const parts = makeParts();
16
- const handled = applyChunkToParts(parts, {
17
- type: "text-start",
18
- id: "t1"
19
- });
20
- expect(handled).toBe(true);
21
- expect(parts.length).toBe(1);
22
- expect(parts[0]).toEqual({
23
- type: "text",
24
- text: "",
25
- state: "streaming"
26
- });
27
- });
28
-
29
- it("text-delta appends to existing text part", () => {
30
- const parts = makeParts();
31
- applyChunkToParts(parts, { type: "text-start", id: "t1" });
32
- applyChunkToParts(parts, {
33
- type: "text-delta",
34
- id: "t1",
35
- delta: "Hello "
36
- });
37
- applyChunkToParts(parts, {
38
- type: "text-delta",
39
- id: "t1",
40
- delta: "world!"
41
- });
42
- expect(parts.length).toBe(1);
43
- expect((parts[0] as { text: string }).text).toBe("Hello world!");
44
- });
45
-
46
- it("text-delta creates new text part with streaming state if no text-start received", () => {
47
- const parts = makeParts();
48
- applyChunkToParts(parts, {
49
- type: "text-delta",
50
- id: "t1",
51
- delta: "fallback"
52
- });
53
- expect(parts.length).toBe(1);
54
- expect((parts[0] as { text: string }).text).toBe("fallback");
55
- expect((parts[0] as { state: string }).state).toBe("streaming");
56
- });
57
-
58
- it("text-end marks fallback text part as done", () => {
59
- const parts = makeParts();
60
- // No text-start — simulates stream resumption
61
- applyChunkToParts(parts, {
62
- type: "text-delta",
63
- id: "t1",
64
- delta: "resumed"
65
- });
66
- applyChunkToParts(parts, { type: "text-end", id: "t1" });
67
- expect((parts[0] as { state: string }).state).toBe("done");
68
- expect((parts[0] as { text: string }).text).toBe("resumed");
69
- });
70
-
71
- it("text-end marks text part as done", () => {
72
- const parts = makeParts();
73
- applyChunkToParts(parts, { type: "text-start", id: "t1" });
74
- applyChunkToParts(parts, {
75
- type: "text-delta",
76
- id: "t1",
77
- delta: "content"
78
- });
79
- applyChunkToParts(parts, { type: "text-end", id: "t1" });
80
- expect((parts[0] as { state: string }).state).toBe("done");
81
- expect((parts[0] as { text: string }).text).toBe("content");
82
- });
83
-
84
- it("handles a full text lifecycle", () => {
85
- const parts = makeParts();
86
- applyChunkToParts(parts, { type: "text-start", id: "t1" });
87
- applyChunkToParts(parts, {
88
- type: "text-delta",
89
- id: "t1",
90
- delta: "The "
91
- });
92
- applyChunkToParts(parts, {
93
- type: "text-delta",
94
- id: "t1",
95
- delta: "answer is 42."
96
- });
97
- applyChunkToParts(parts, { type: "text-end", id: "t1" });
98
-
99
- expect(parts.length).toBe(1);
100
- expect(parts[0]).toEqual({
101
- type: "text",
102
- text: "The answer is 42.",
103
- state: "done"
104
- });
105
- });
106
- });
107
-
108
- describe("reasoning chunks", () => {
109
- it("reasoning-start creates a streaming reasoning part", () => {
110
- const parts = makeParts();
111
- applyChunkToParts(parts, { type: "reasoning-start", id: "r1" });
112
- expect(parts.length).toBe(1);
113
- expect(parts[0]).toEqual({
114
- type: "reasoning",
115
- text: "",
116
- state: "streaming"
117
- });
118
- });
119
-
120
- it("reasoning-delta appends to reasoning part", () => {
121
- const parts = makeParts();
122
- applyChunkToParts(parts, { type: "reasoning-start", id: "r1" });
123
- applyChunkToParts(parts, {
124
- type: "reasoning-delta",
125
- id: "r1",
126
- delta: "thinking..."
127
- });
128
- expect((parts[0] as { text: string }).text).toBe("thinking...");
129
- });
130
-
131
- it("reasoning-end marks reasoning as done", () => {
132
- const parts = makeParts();
133
- applyChunkToParts(parts, { type: "reasoning-start", id: "r1" });
134
- applyChunkToParts(parts, {
135
- type: "reasoning-delta",
136
- id: "r1",
137
- delta: "done thinking"
138
- });
139
- applyChunkToParts(parts, { type: "reasoning-end", id: "r1" });
140
- expect((parts[0] as { state: string }).state).toBe("done");
141
- });
142
-
143
- it("reasoning-delta creates fallback part when no reasoning-start received", () => {
144
- const parts = makeParts();
145
- // No reasoning-start — simulates stream resumption where start was missed
146
- applyChunkToParts(parts, {
147
- type: "reasoning-delta",
148
- id: "r1",
149
- delta: "resumed thinking"
150
- });
151
- expect(parts.length).toBe(1);
152
- expect(parts[0]).toEqual({
153
- type: "reasoning",
154
- text: "resumed thinking",
155
- state: "streaming"
156
- });
157
-
158
- // Subsequent deltas append normally
159
- applyChunkToParts(parts, {
160
- type: "reasoning-delta",
161
- id: "r1",
162
- delta: " more"
163
- });
164
- expect((parts[0] as { text: string }).text).toBe("resumed thinking more");
165
- });
166
- });
167
-
168
- describe("file chunks", () => {
169
- it("creates a file part", () => {
170
- const parts = makeParts();
171
- applyChunkToParts(parts, {
172
- type: "file",
173
- mediaType: "image/png",
174
- url: "https://example.com/image.png"
175
- });
176
- expect(parts.length).toBe(1);
177
- expect(parts[0]).toEqual({
178
- type: "file",
179
- mediaType: "image/png",
180
- url: "https://example.com/image.png"
181
- });
182
- });
183
- });
184
-
185
- describe("source chunks", () => {
186
- it("creates a source-url part", () => {
187
- const parts = makeParts();
188
- applyChunkToParts(parts, {
189
- type: "source-url",
190
- sourceId: "s1",
191
- url: "https://example.com",
192
- title: "Example"
193
- });
194
- expect(parts[0]).toEqual({
195
- type: "source-url",
196
- sourceId: "s1",
197
- url: "https://example.com",
198
- title: "Example",
199
- providerMetadata: undefined
200
- });
201
- });
202
-
203
- it("creates a source-document part", () => {
204
- const parts = makeParts();
205
- applyChunkToParts(parts, {
206
- type: "source-document",
207
- sourceId: "d1",
208
- mediaType: "application/pdf",
209
- title: "Doc",
210
- filename: "doc.pdf"
211
- });
212
- expect(parts[0]).toEqual({
213
- type: "source-document",
214
- sourceId: "d1",
215
- mediaType: "application/pdf",
216
- title: "Doc",
217
- filename: "doc.pdf",
218
- providerMetadata: undefined
219
- });
220
- });
221
- });
222
-
223
- describe("tool chunks", () => {
224
- it("tool-input-available creates a tool part", () => {
225
- const parts = makeParts();
226
- applyChunkToParts(parts, {
227
- type: "tool-input-available",
228
- toolCallId: "call_1",
229
- toolName: "getWeather",
230
- input: { city: "London" }
231
- });
232
- expect(parts.length).toBe(1);
233
- const part = parts[0] as Record<string, unknown>;
234
- expect(part.type).toBe("tool-getWeather");
235
- expect(part.toolCallId).toBe("call_1");
236
- expect(part.state).toBe("input-available");
237
- expect(part.input).toEqual({ city: "London" });
238
- });
239
-
240
- it("tool-output-available updates existing tool part", () => {
241
- const parts = makeParts();
242
- applyChunkToParts(parts, {
243
- type: "tool-input-available",
244
- toolCallId: "call_1",
245
- toolName: "getWeather",
246
- input: { city: "London" }
247
- });
248
- applyChunkToParts(parts, {
249
- type: "tool-output-available",
250
- toolCallId: "call_1",
251
- output: "Sunny, 22°C"
252
- });
253
- expect(parts.length).toBe(1);
254
- const part = parts[0] as Record<string, unknown>;
255
- expect(part.state).toBe("output-available");
256
- expect(part.output).toBe("Sunny, 22°C");
257
- });
258
-
259
- it("tool-output-available is a no-op if tool part not found", () => {
260
- const parts = makeParts();
261
- // No tool-input-available first
262
- applyChunkToParts(parts, {
263
- type: "tool-output-available",
264
- toolCallId: "nonexistent",
265
- output: "result"
266
- });
267
- expect(parts.length).toBe(0); // Nothing added
268
- });
269
- });
270
-
271
- describe("step chunks", () => {
272
- it("start-step adds a step-start part", () => {
273
- const parts = makeParts();
274
- applyChunkToParts(parts, { type: "start-step" });
275
- expect(parts.length).toBe(1);
276
- expect(parts[0].type).toBe("step-start");
277
- });
278
-
279
- it("step-start (client alias) also adds a step-start part", () => {
280
- const parts = makeParts();
281
- applyChunkToParts(parts, { type: "step-start" });
282
- expect(parts.length).toBe(1);
283
- expect(parts[0].type).toBe("step-start");
284
- });
285
- });
286
-
287
- describe("data-* chunks", () => {
288
- it("appends a new data part", () => {
289
- const parts = makeParts();
290
- const handled = applyChunkToParts(parts, {
291
- type: "data-sources",
292
- data: { query: "hello", results: [] }
293
- });
294
- expect(handled).toBe(true);
295
- expect(parts.length).toBe(1);
296
- expect(parts[0]).toEqual({
297
- type: "data-sources",
298
- data: { query: "hello", results: [] }
299
- });
300
- });
301
-
302
- it("preserves the id field when present", () => {
303
- const parts = makeParts();
304
- applyChunkToParts(parts, {
305
- type: "data-sources",
306
- id: "src-1",
307
- data: { query: "hello", status: "searching" }
308
- });
309
- expect(parts[0]).toEqual({
310
- type: "data-sources",
311
- id: "src-1",
312
- data: { query: "hello", status: "searching" }
313
- });
314
- });
315
-
316
- it("reconciles by type+id (updates data in-place)", () => {
317
- const parts = makeParts();
318
- applyChunkToParts(parts, {
319
- type: "data-sources",
320
- id: "src-1",
321
- data: { status: "searching", results: [] }
322
- });
323
- applyChunkToParts(parts, {
324
- type: "data-sources",
325
- id: "src-1",
326
- data: { status: "found", results: ["doc1", "doc2"] }
327
- });
328
- // Should update in-place, not append
329
- expect(parts.length).toBe(1);
330
- expect((parts[0] as Record<string, unknown>).data).toEqual({
331
- status: "found",
332
- results: ["doc1", "doc2"]
333
- });
334
- });
335
-
336
- it("does not reconcile when ids differ", () => {
337
- const parts = makeParts();
338
- applyChunkToParts(parts, {
339
- type: "data-sources",
340
- id: "src-1",
341
- data: { query: "first" }
342
- });
343
- applyChunkToParts(parts, {
344
- type: "data-sources",
345
- id: "src-2",
346
- data: { query: "second" }
347
- });
348
- expect(parts.length).toBe(2);
349
- });
350
-
351
- it("does not reconcile when types differ", () => {
352
- const parts = makeParts();
353
- applyChunkToParts(parts, {
354
- type: "data-sources",
355
- id: "x",
356
- data: { from: "sources" }
357
- });
358
- applyChunkToParts(parts, {
359
- type: "data-usage",
360
- id: "x",
361
- data: { from: "usage" }
362
- });
363
- expect(parts.length).toBe(2);
364
- });
365
-
366
- it("does not reconcile parts without ids (appends each time)", () => {
367
- const parts = makeParts();
368
- applyChunkToParts(parts, {
369
- type: "data-status",
370
- data: { step: 1 }
371
- });
372
- applyChunkToParts(parts, {
373
- type: "data-status",
374
- data: { step: 2 }
375
- });
376
- expect(parts.length).toBe(2);
377
- });
378
-
379
- it("transient parts return true but are not added to parts", () => {
380
- const parts = makeParts();
381
- const handled = applyChunkToParts(parts, {
382
- type: "data-thinking",
383
- transient: true,
384
- data: { model: "gpt-4o" }
385
- });
386
- expect(handled).toBe(true);
387
- expect(parts.length).toBe(0);
388
- });
389
-
390
- it("transient parts with id are still skipped (not reconciled)", () => {
391
- const parts = makeParts();
392
- // First: non-transient part with id
393
- applyChunkToParts(parts, {
394
- type: "data-progress",
395
- id: "p-1",
396
- data: { step: 1 }
397
- });
398
- expect(parts.length).toBe(1);
399
-
400
- // Second: transient part with SAME type+id — should not reconcile
401
- const handled = applyChunkToParts(parts, {
402
- type: "data-progress",
403
- id: "p-1",
404
- transient: true,
405
- data: { step: 2 }
406
- });
407
- expect(handled).toBe(true);
408
- // Original part unchanged
409
- expect(parts.length).toBe(1);
410
- expect((parts[0] as Record<string, unknown>).data).toEqual({ step: 1 });
411
- });
412
-
413
- it("handles undefined data (part persisted without data field)", () => {
414
- const parts = makeParts();
415
- const handled = applyChunkToParts(parts, {
416
- type: "data-empty"
417
- } as StreamChunkData);
418
- expect(handled).toBe(true);
419
- expect(parts.length).toBe(1);
420
- expect(parts[0].type).toBe("data-empty");
421
- // data is undefined — JSON.stringify would drop it
422
- expect((parts[0] as Record<string, unknown>).data).toBeUndefined();
423
- });
424
-
425
- it("non-data-* prefixed types still return false", () => {
426
- const parts = makeParts();
427
- const handled = applyChunkToParts(parts, {
428
- type: "unknown-type"
429
- } as StreamChunkData);
430
- expect(handled).toBe(false);
431
- expect(parts.length).toBe(0);
432
- });
433
-
434
- it("coexists with other part types", () => {
435
- const parts = makeParts();
436
-
437
- // Text
438
- applyChunkToParts(parts, { type: "text-start", id: "t1" });
439
- applyChunkToParts(parts, {
440
- type: "text-delta",
441
- id: "t1",
442
- delta: "Hello"
443
- });
444
- applyChunkToParts(parts, { type: "text-end", id: "t1" });
445
-
446
- // Data part
447
- applyChunkToParts(parts, {
448
- type: "data-usage",
449
- data: { tokens: 42 }
450
- });
451
-
452
- expect(parts.length).toBe(2);
453
- expect(parts[0].type).toBe("text");
454
- expect(parts[1].type).toBe("data-usage");
455
- });
456
- });
457
-
458
- describe("unrecognized chunks", () => {
459
- it("returns false for unknown chunk types", () => {
460
- const parts = makeParts();
461
- const handled = applyChunkToParts(parts, {
462
- type: "unknown-type"
463
- } as StreamChunkData);
464
- expect(handled).toBe(false);
465
- expect(parts.length).toBe(0);
466
- });
467
- });
468
-
469
- describe("mixed content", () => {
470
- it("builds a complex message with text + reasoning + tool + file", () => {
471
- const parts = makeParts();
472
-
473
- // Reasoning first
474
- applyChunkToParts(parts, { type: "reasoning-start", id: "r1" });
475
- applyChunkToParts(parts, {
476
- type: "reasoning-delta",
477
- id: "r1",
478
- delta: "Let me think..."
479
- });
480
- applyChunkToParts(parts, { type: "reasoning-end", id: "r1" });
481
-
482
- // Step boundary
483
- applyChunkToParts(parts, { type: "start-step" });
484
-
485
- // Text
486
- applyChunkToParts(parts, { type: "text-start", id: "t1" });
487
- applyChunkToParts(parts, {
488
- type: "text-delta",
489
- id: "t1",
490
- delta: "Here's the weather"
491
- });
492
- applyChunkToParts(parts, { type: "text-end", id: "t1" });
493
-
494
- // Tool call
495
- applyChunkToParts(parts, {
496
- type: "tool-input-available",
497
- toolCallId: "call_1",
498
- toolName: "getWeather",
499
- input: { city: "London" }
500
- });
501
- applyChunkToParts(parts, {
502
- type: "tool-output-available",
503
- toolCallId: "call_1",
504
- output: { temp: 22, condition: "Sunny" }
505
- });
506
-
507
- // File
508
- applyChunkToParts(parts, {
509
- type: "file",
510
- mediaType: "image/png",
511
- url: "https://example.com/chart.png"
512
- });
513
-
514
- // reasoning + step-start + text + tool (updated in place) + file = 5
515
- expect(parts.length).toBe(5);
516
- expect(parts[0].type).toBe("reasoning");
517
- expect(parts[1].type).toBe("step-start");
518
- expect(parts[2].type).toBe("text");
519
- expect(parts[3].type).toBe("tool-getWeather");
520
- expect((parts[3] as Record<string, unknown>).state).toBe(
521
- "output-available"
522
- );
523
- expect(parts[4].type).toBe("file");
524
- });
525
- });
526
-
527
- describe("tool streaming lifecycle", () => {
528
- it("tool-input-start creates a tool part in input-streaming state", () => {
529
- const parts = makeParts();
530
- const handled = applyChunkToParts(parts, {
531
- type: "tool-input-start",
532
- toolCallId: "call_1",
533
- toolName: "getWeather"
534
- });
535
- expect(handled).toBe(true);
536
- expect(parts.length).toBe(1);
537
- const part = parts[0] as Record<string, unknown>;
538
- expect(part.type).toBe("tool-getWeather");
539
- expect(part.toolCallId).toBe("call_1");
540
- expect(part.state).toBe("input-streaming");
541
- expect(part.input).toBeUndefined();
542
- });
543
-
544
- it("tool-input-delta updates the tool part with partial input", () => {
545
- const parts = makeParts();
546
- applyChunkToParts(parts, {
547
- type: "tool-input-start",
548
- toolCallId: "call_1",
549
- toolName: "getWeather"
550
- });
551
- applyChunkToParts(parts, {
552
- type: "tool-input-delta",
553
- toolCallId: "call_1",
554
- input: { city: "Lon" }
555
- });
556
- expect(parts.length).toBe(1);
557
- expect((parts[0] as Record<string, unknown>).input).toEqual({
558
- city: "Lon"
559
- });
560
- expect((parts[0] as Record<string, unknown>).state).toBe(
561
- "input-streaming"
562
- );
563
- });
564
-
565
- it("tool-input-available finalizes an existing streaming tool part", () => {
566
- const parts = makeParts();
567
- applyChunkToParts(parts, {
568
- type: "tool-input-start",
569
- toolCallId: "call_1",
570
- toolName: "getWeather"
571
- });
572
- applyChunkToParts(parts, {
573
- type: "tool-input-available",
574
- toolCallId: "call_1",
575
- toolName: "getWeather",
576
- input: { city: "London" }
577
- });
578
- expect(parts.length).toBe(1);
579
- const part = parts[0] as Record<string, unknown>;
580
- expect(part.state).toBe("input-available");
581
- expect(part.input).toEqual({ city: "London" });
582
- });
583
-
584
- it("tool-input-available creates a new part if no tool-input-start", () => {
585
- const parts = makeParts();
586
- applyChunkToParts(parts, {
587
- type: "tool-input-available",
588
- toolCallId: "call_1",
589
- toolName: "getWeather",
590
- input: { city: "London" }
591
- });
592
- expect(parts.length).toBe(1);
593
- expect((parts[0] as Record<string, unknown>).state).toBe(
594
- "input-available"
595
- );
596
- });
597
-
598
- it("full tool streaming lifecycle: start -> delta -> available -> output", () => {
599
- const parts = makeParts();
600
-
601
- applyChunkToParts(parts, {
602
- type: "tool-input-start",
603
- toolCallId: "call_1",
604
- toolName: "getWeather"
605
- });
606
- expect((parts[0] as Record<string, unknown>).state).toBe(
607
- "input-streaming"
608
- );
609
-
610
- applyChunkToParts(parts, {
611
- type: "tool-input-delta",
612
- toolCallId: "call_1",
613
- input: { city: "Lon" }
614
- });
615
-
616
- applyChunkToParts(parts, {
617
- type: "tool-input-available",
618
- toolCallId: "call_1",
619
- toolName: "getWeather",
620
- input: { city: "London" }
621
- });
622
- expect((parts[0] as Record<string, unknown>).state).toBe(
623
- "input-available"
624
- );
625
-
626
- applyChunkToParts(parts, {
627
- type: "tool-output-available",
628
- toolCallId: "call_1",
629
- output: "Sunny, 22C"
630
- });
631
- expect((parts[0] as Record<string, unknown>).state).toBe(
632
- "output-available"
633
- );
634
- expect((parts[0] as Record<string, unknown>).output).toBe("Sunny, 22C");
635
- expect(parts.length).toBe(1);
636
- });
637
- });
638
-
639
- describe("tool error handling", () => {
640
- it("tool-input-error marks tool part as output-error", () => {
641
- const parts = makeParts();
642
- applyChunkToParts(parts, {
643
- type: "tool-input-start",
644
- toolCallId: "call_1",
645
- toolName: "getWeather"
646
- });
647
- applyChunkToParts(parts, {
648
- type: "tool-input-error",
649
- toolCallId: "call_1",
650
- toolName: "getWeather",
651
- input: '{"city": "Lond',
652
- errorText: "Unexpected end of JSON input"
653
- });
654
- const part = parts[0] as Record<string, unknown>;
655
- expect(part.state).toBe("output-error");
656
- expect(part.errorText).toBe("Unexpected end of JSON input");
657
- });
658
-
659
- it("tool-input-error creates a new part if no tool-input-start", () => {
660
- const parts = makeParts();
661
- applyChunkToParts(parts, {
662
- type: "tool-input-error",
663
- toolCallId: "call_1",
664
- toolName: "getWeather",
665
- errorText: "Schema validation failed"
666
- });
667
- expect(parts.length).toBe(1);
668
- expect((parts[0] as Record<string, unknown>).state).toBe("output-error");
669
- });
670
-
671
- it("tool-output-error marks tool part as output-error", () => {
672
- const parts = makeParts();
673
- applyChunkToParts(parts, {
674
- type: "tool-input-available",
675
- toolCallId: "call_1",
676
- toolName: "getWeather",
677
- input: { city: "London" }
678
- });
679
- applyChunkToParts(parts, {
680
- type: "tool-output-error",
681
- toolCallId: "call_1",
682
- errorText: "API rate limit exceeded"
683
- });
684
- const part = parts[0] as Record<string, unknown>;
685
- expect(part.state).toBe("output-error");
686
- expect(part.errorText).toBe("API rate limit exceeded");
687
- });
688
-
689
- it("tool-output-error is a no-op if tool part not found", () => {
690
- const parts = makeParts();
691
- applyChunkToParts(parts, {
692
- type: "tool-output-error",
693
- toolCallId: "nonexistent",
694
- errorText: "error"
695
- });
696
- expect(parts.length).toBe(0);
697
- });
698
- });
699
-
700
- describe("preliminary tool output", () => {
701
- it("tool-output-available with preliminary=true marks output as preliminary", () => {
702
- const parts = makeParts();
703
- applyChunkToParts(parts, {
704
- type: "tool-input-available",
705
- toolCallId: "call_1",
706
- toolName: "streamingTool",
707
- input: {}
708
- });
709
- applyChunkToParts(parts, {
710
- type: "tool-output-available",
711
- toolCallId: "call_1",
712
- output: { partial: true, data: "chunk1" },
713
- preliminary: true
714
- });
715
- const part = parts[0] as Record<string, unknown>;
716
- expect(part.state).toBe("output-available");
717
- expect(part.preliminary).toBe(true);
718
- expect(part.output).toEqual({ partial: true, data: "chunk1" });
719
- });
720
-
721
- it("subsequent tool-output-available with preliminary=false finalizes", () => {
722
- const parts = makeParts();
723
- applyChunkToParts(parts, {
724
- type: "tool-input-available",
725
- toolCallId: "call_1",
726
- toolName: "streamingTool",
727
- input: {}
728
- });
729
- applyChunkToParts(parts, {
730
- type: "tool-output-available",
731
- toolCallId: "call_1",
732
- output: "partial",
733
- preliminary: true
734
- });
735
- expect((parts[0] as Record<string, unknown>).preliminary).toBe(true);
736
-
737
- applyChunkToParts(parts, {
738
- type: "tool-output-available",
739
- toolCallId: "call_1",
740
- output: "complete result",
741
- preliminary: false
742
- });
743
- const part = parts[0] as Record<string, unknown>;
744
- expect(part.preliminary).toBe(false);
745
- expect(part.output).toBe("complete result");
746
- });
747
-
748
- it("tool-output-available without preliminary does not set the flag", () => {
749
- const parts = makeParts();
750
- applyChunkToParts(parts, {
751
- type: "tool-input-available",
752
- toolCallId: "call_1",
753
- toolName: "getWeather",
754
- input: { city: "London" }
755
- });
756
- applyChunkToParts(parts, {
757
- type: "tool-output-available",
758
- toolCallId: "call_1",
759
- output: "Sunny"
760
- });
761
- const part = parts[0] as Record<string, unknown>;
762
- expect(part.preliminary).toBeUndefined();
763
- });
764
- });
765
-
766
- describe("metadata and message-level chunks", () => {
767
- it("returns false for 'start' chunk (caller handles metadata)", () => {
768
- const parts = makeParts();
769
- const handled = applyChunkToParts(parts, {
770
- type: "start",
771
- messageId: "msg-1",
772
- messageMetadata: { model: "gpt-4o" }
773
- } as StreamChunkData);
774
- expect(handled).toBe(false);
775
- // Should not add any parts
776
- expect(parts.length).toBe(0);
777
- });
778
-
779
- it("returns false for 'finish' chunk (caller handles metadata)", () => {
780
- const parts = makeParts();
781
- const handled = applyChunkToParts(parts, {
782
- type: "finish",
783
- messageMetadata: { totalTokens: 100 }
784
- } as StreamChunkData);
785
- expect(handled).toBe(false);
786
- expect(parts.length).toBe(0);
787
- });
788
-
789
- it("returns false for 'message-metadata' chunk (caller handles metadata)", () => {
790
- const parts = makeParts();
791
- const handled = applyChunkToParts(parts, {
792
- type: "message-metadata",
793
- messageMetadata: { createdAt: 1234567890 }
794
- } as StreamChunkData);
795
- expect(handled).toBe(false);
796
- expect(parts.length).toBe(0);
797
- });
798
-
799
- it("returns false for 'finish-step' chunk", () => {
800
- const parts = makeParts();
801
- const handled = applyChunkToParts(parts, {
802
- type: "finish-step"
803
- } as StreamChunkData);
804
- expect(handled).toBe(false);
805
- expect(parts.length).toBe(0);
806
- });
807
- });
808
- });