ai 3.2.28 → 3.2.30

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/index.d.mts CHANGED
@@ -1383,11 +1383,15 @@ declare class StreamTextResult<TOOLS extends Record<string, CoreTool>> {
1383
1383
  Converts the result to a streamed response object with a stream data part stream.
1384
1384
  It can be used with the `useChat` and `useCompletion` hooks.
1385
1385
 
1386
- @param init Optional headers.
1386
+ @param options An object with an init property (ResponseInit) and a data property.
1387
+ You can also pass in a ResponseInit directly (deprecated).
1387
1388
 
1388
1389
  @return A response object.
1389
1390
  */
1390
- toAIStreamResponse(init?: ResponseInit): Response;
1391
+ toAIStreamResponse(options?: ResponseInit | {
1392
+ init?: ResponseInit;
1393
+ data?: StreamData;
1394
+ }): Response;
1391
1395
  /**
1392
1396
  Creates a simple text stream response.
1393
1397
  Each text delta is encoded as UTF-8 and sent as a separate chunk.
package/dist/index.d.ts CHANGED
@@ -1383,11 +1383,15 @@ declare class StreamTextResult<TOOLS extends Record<string, CoreTool>> {
1383
1383
  Converts the result to a streamed response object with a stream data part stream.
1384
1384
  It can be used with the `useChat` and `useCompletion` hooks.
1385
1385
 
1386
- @param init Optional headers.
1386
+ @param options An object with an init property (ResponseInit) and a data property.
1387
+ You can also pass in a ResponseInit directly (deprecated).
1387
1388
 
1388
1389
  @return A response object.
1389
1390
  */
1390
- toAIStreamResponse(init?: ResponseInit): Response;
1391
+ toAIStreamResponse(options?: ResponseInit | {
1392
+ init?: ResponseInit;
1393
+ data?: StreamData;
1394
+ }): Response;
1391
1395
  /**
1392
1396
  Creates a simple text stream response.
1393
1397
  Each text delta is encoded as UTF-8 and sent as a separate chunk.
package/dist/index.js CHANGED
@@ -67,7 +67,7 @@ __export(streams_exports, {
67
67
  OpenAIStream: () => OpenAIStream,
68
68
  ReplicateStream: () => ReplicateStream,
69
69
  RetryError: () => import_provider8.RetryError,
70
- StreamData: () => StreamData,
70
+ StreamData: () => StreamData2,
71
71
  StreamObjectResult: () => StreamObjectResult,
72
72
  StreamTextResult: () => StreamTextResult,
73
73
  StreamingTextResponse: () => StreamingTextResponse,
@@ -1677,6 +1677,94 @@ function toResponseMessages({
1677
1677
  }
1678
1678
  var experimental_generateText = generateText;
1679
1679
 
1680
+ // core/util/merge-streams.ts
1681
+ function mergeStreams(stream1, stream2) {
1682
+ const reader1 = stream1.getReader();
1683
+ const reader2 = stream2.getReader();
1684
+ let lastRead1 = void 0;
1685
+ let lastRead2 = void 0;
1686
+ let stream1Done = false;
1687
+ let stream2Done = false;
1688
+ async function readStream1(controller) {
1689
+ try {
1690
+ if (lastRead1 == null) {
1691
+ lastRead1 = reader1.read();
1692
+ }
1693
+ const result = await lastRead1;
1694
+ lastRead1 = void 0;
1695
+ if (!result.done) {
1696
+ controller.enqueue(result.value);
1697
+ } else {
1698
+ controller.close();
1699
+ }
1700
+ } catch (error) {
1701
+ controller.error(error);
1702
+ }
1703
+ }
1704
+ async function readStream2(controller) {
1705
+ try {
1706
+ if (lastRead2 == null) {
1707
+ lastRead2 = reader2.read();
1708
+ }
1709
+ const result = await lastRead2;
1710
+ lastRead2 = void 0;
1711
+ if (!result.done) {
1712
+ controller.enqueue(result.value);
1713
+ } else {
1714
+ controller.close();
1715
+ }
1716
+ } catch (error) {
1717
+ controller.error(error);
1718
+ }
1719
+ }
1720
+ return new ReadableStream({
1721
+ async pull(controller) {
1722
+ try {
1723
+ if (stream1Done) {
1724
+ await readStream2(controller);
1725
+ return;
1726
+ }
1727
+ if (stream2Done) {
1728
+ await readStream1(controller);
1729
+ return;
1730
+ }
1731
+ if (lastRead1 == null) {
1732
+ lastRead1 = reader1.read();
1733
+ }
1734
+ if (lastRead2 == null) {
1735
+ lastRead2 = reader2.read();
1736
+ }
1737
+ const { result, reader } = await Promise.race([
1738
+ lastRead1.then((result2) => ({ result: result2, reader: reader1 })),
1739
+ lastRead2.then((result2) => ({ result: result2, reader: reader2 }))
1740
+ ]);
1741
+ if (!result.done) {
1742
+ controller.enqueue(result.value);
1743
+ }
1744
+ if (reader === reader1) {
1745
+ lastRead1 = void 0;
1746
+ if (result.done) {
1747
+ await readStream2(controller);
1748
+ stream1Done = true;
1749
+ }
1750
+ } else {
1751
+ lastRead2 = void 0;
1752
+ if (result.done) {
1753
+ stream2Done = true;
1754
+ await readStream1(controller);
1755
+ }
1756
+ }
1757
+ } catch (error) {
1758
+ controller.error(error);
1759
+ }
1760
+ },
1761
+ cancel() {
1762
+ reader1.cancel();
1763
+ reader2.cancel();
1764
+ }
1765
+ });
1766
+ }
1767
+
1680
1768
  // core/generate-text/run-tools-transformation.ts
1681
1769
  var import_provider7 = require("@ai-sdk/provider");
1682
1770
  var import_ui_utils2 = require("@ai-sdk/ui-utils");
@@ -2142,7 +2230,7 @@ var StreamTextResult = class {
2142
2230
  await callbacks.onFinal(aggregatedResponse);
2143
2231
  }
2144
2232
  });
2145
- const streamDataTransformer = new TransformStream({
2233
+ const streamPartsTransformer = new TransformStream({
2146
2234
  transform: async (chunk, controller) => {
2147
2235
  const chunkType = chunk.type;
2148
2236
  switch (chunkType) {
@@ -2198,7 +2286,7 @@ var StreamTextResult = class {
2198
2286
  }
2199
2287
  }
2200
2288
  });
2201
- return this.fullStream.pipeThrough(callbackTransformer).pipeThrough(streamDataTransformer).pipeThrough(new TextEncoderStream());
2289
+ return this.fullStream.pipeThrough(callbackTransformer).pipeThrough(streamPartsTransformer).pipeThrough(new TextEncoderStream());
2202
2290
  }
2203
2291
  /**
2204
2292
  Writes stream data output to a Node.js response-like object.
@@ -2266,12 +2354,27 @@ var StreamTextResult = class {
2266
2354
  Converts the result to a streamed response object with a stream data part stream.
2267
2355
  It can be used with the `useChat` and `useCompletion` hooks.
2268
2356
 
2269
- @param init Optional headers.
2357
+ @param options An object with an init property (ResponseInit) and a data property.
2358
+ You can also pass in a ResponseInit directly (deprecated).
2270
2359
 
2271
2360
  @return A response object.
2272
2361
  */
2273
- toAIStreamResponse(init) {
2274
- return new StreamingTextResponse(this.toAIStream(), init);
2362
+ toAIStreamResponse(options) {
2363
+ var _a;
2364
+ const init = options == null ? void 0 : "init" in options ? options.init : {
2365
+ headers: "headers" in options ? options.headers : void 0,
2366
+ status: "status" in options ? options.status : void 0,
2367
+ statusText: "statusText" in options ? options.statusText : void 0
2368
+ };
2369
+ const data = options == null ? void 0 : "data" in options ? options.data : void 0;
2370
+ const stream = data ? mergeStreams(data.stream, this.toAIStream()) : this.toAIStream();
2371
+ return new Response(stream, {
2372
+ status: (_a = init == null ? void 0 : init.status) != null ? _a : 200,
2373
+ statusText: init == null ? void 0 : init.statusText,
2374
+ headers: prepareResponseHeaders(init, {
2375
+ contentType: "text/plain; charset=utf-8"
2376
+ })
2377
+ });
2275
2378
  }
2276
2379
  /**
2277
2380
  Creates a simple text stream response.
@@ -2696,7 +2799,7 @@ function readableFromAsyncIterable(iterable) {
2696
2799
 
2697
2800
  // streams/stream-data.ts
2698
2801
  var import_ui_utils3 = require("@ai-sdk/ui-utils");
2699
- var StreamData = class {
2802
+ var StreamData2 = class {
2700
2803
  constructor() {
2701
2804
  this.encoder = new TextEncoder();
2702
2805
  this.controller = null;
@@ -2767,7 +2870,7 @@ function createStreamDataTransformer() {
2767
2870
  }
2768
2871
  });
2769
2872
  }
2770
- var experimental_StreamData = class extends StreamData {
2873
+ var experimental_StreamData = class extends StreamData2 {
2771
2874
  };
2772
2875
 
2773
2876
  // streams/anthropic-stream.ts
@@ -3532,94 +3635,6 @@ async function ReplicateStream(res, cb, options) {
3532
3635
  );
3533
3636
  }
3534
3637
 
3535
- // core/util/merge-streams.ts
3536
- function mergeStreams(stream1, stream2) {
3537
- const reader1 = stream1.getReader();
3538
- const reader2 = stream2.getReader();
3539
- let lastRead1 = void 0;
3540
- let lastRead2 = void 0;
3541
- let stream1Done = false;
3542
- let stream2Done = false;
3543
- async function readStream1(controller) {
3544
- try {
3545
- if (lastRead1 == null) {
3546
- lastRead1 = reader1.read();
3547
- }
3548
- const result = await lastRead1;
3549
- lastRead1 = void 0;
3550
- if (!result.done) {
3551
- controller.enqueue(result.value);
3552
- } else {
3553
- controller.close();
3554
- }
3555
- } catch (error) {
3556
- controller.error(error);
3557
- }
3558
- }
3559
- async function readStream2(controller) {
3560
- try {
3561
- if (lastRead2 == null) {
3562
- lastRead2 = reader2.read();
3563
- }
3564
- const result = await lastRead2;
3565
- lastRead2 = void 0;
3566
- if (!result.done) {
3567
- controller.enqueue(result.value);
3568
- } else {
3569
- controller.close();
3570
- }
3571
- } catch (error) {
3572
- controller.error(error);
3573
- }
3574
- }
3575
- return new ReadableStream({
3576
- async pull(controller) {
3577
- try {
3578
- if (stream1Done) {
3579
- readStream2(controller);
3580
- return;
3581
- }
3582
- if (stream2Done) {
3583
- readStream1(controller);
3584
- return;
3585
- }
3586
- if (lastRead1 == null) {
3587
- lastRead1 = reader1.read();
3588
- }
3589
- if (lastRead2 == null) {
3590
- lastRead2 = reader2.read();
3591
- }
3592
- const { result, reader } = await Promise.race([
3593
- lastRead1.then((result2) => ({ result: result2, reader: reader1 })),
3594
- lastRead2.then((result2) => ({ result: result2, reader: reader2 }))
3595
- ]);
3596
- if (!result.done) {
3597
- controller.enqueue(result.value);
3598
- }
3599
- if (reader === reader1) {
3600
- lastRead1 = void 0;
3601
- if (result.done) {
3602
- readStream2(controller);
3603
- stream1Done = true;
3604
- }
3605
- } else {
3606
- lastRead2 = void 0;
3607
- if (result.done) {
3608
- stream2Done = true;
3609
- readStream1(controller);
3610
- }
3611
- }
3612
- } catch (error) {
3613
- controller.error(error);
3614
- }
3615
- },
3616
- cancel() {
3617
- reader1.cancel();
3618
- reader2.cancel();
3619
- }
3620
- });
3621
- }
3622
-
3623
3638
  // streams/stream-to-response.ts
3624
3639
  function streamToResponse(res, response, init, data) {
3625
3640
  var _a;