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.mjs CHANGED
@@ -1587,6 +1587,94 @@ function toResponseMessages({
1587
1587
  }
1588
1588
  var experimental_generateText = generateText;
1589
1589
 
1590
+ // core/util/merge-streams.ts
1591
+ function mergeStreams(stream1, stream2) {
1592
+ const reader1 = stream1.getReader();
1593
+ const reader2 = stream2.getReader();
1594
+ let lastRead1 = void 0;
1595
+ let lastRead2 = void 0;
1596
+ let stream1Done = false;
1597
+ let stream2Done = false;
1598
+ async function readStream1(controller) {
1599
+ try {
1600
+ if (lastRead1 == null) {
1601
+ lastRead1 = reader1.read();
1602
+ }
1603
+ const result = await lastRead1;
1604
+ lastRead1 = void 0;
1605
+ if (!result.done) {
1606
+ controller.enqueue(result.value);
1607
+ } else {
1608
+ controller.close();
1609
+ }
1610
+ } catch (error) {
1611
+ controller.error(error);
1612
+ }
1613
+ }
1614
+ async function readStream2(controller) {
1615
+ try {
1616
+ if (lastRead2 == null) {
1617
+ lastRead2 = reader2.read();
1618
+ }
1619
+ const result = await lastRead2;
1620
+ lastRead2 = void 0;
1621
+ if (!result.done) {
1622
+ controller.enqueue(result.value);
1623
+ } else {
1624
+ controller.close();
1625
+ }
1626
+ } catch (error) {
1627
+ controller.error(error);
1628
+ }
1629
+ }
1630
+ return new ReadableStream({
1631
+ async pull(controller) {
1632
+ try {
1633
+ if (stream1Done) {
1634
+ await readStream2(controller);
1635
+ return;
1636
+ }
1637
+ if (stream2Done) {
1638
+ await readStream1(controller);
1639
+ return;
1640
+ }
1641
+ if (lastRead1 == null) {
1642
+ lastRead1 = reader1.read();
1643
+ }
1644
+ if (lastRead2 == null) {
1645
+ lastRead2 = reader2.read();
1646
+ }
1647
+ const { result, reader } = await Promise.race([
1648
+ lastRead1.then((result2) => ({ result: result2, reader: reader1 })),
1649
+ lastRead2.then((result2) => ({ result: result2, reader: reader2 }))
1650
+ ]);
1651
+ if (!result.done) {
1652
+ controller.enqueue(result.value);
1653
+ }
1654
+ if (reader === reader1) {
1655
+ lastRead1 = void 0;
1656
+ if (result.done) {
1657
+ await readStream2(controller);
1658
+ stream1Done = true;
1659
+ }
1660
+ } else {
1661
+ lastRead2 = void 0;
1662
+ if (result.done) {
1663
+ stream2Done = true;
1664
+ await readStream1(controller);
1665
+ }
1666
+ }
1667
+ } catch (error) {
1668
+ controller.error(error);
1669
+ }
1670
+ },
1671
+ cancel() {
1672
+ reader1.cancel();
1673
+ reader2.cancel();
1674
+ }
1675
+ });
1676
+ }
1677
+
1590
1678
  // core/generate-text/run-tools-transformation.ts
1591
1679
  import { NoSuchToolError as NoSuchToolError2 } from "@ai-sdk/provider";
1592
1680
  import { generateId } from "@ai-sdk/ui-utils";
@@ -2052,7 +2140,7 @@ var StreamTextResult = class {
2052
2140
  await callbacks.onFinal(aggregatedResponse);
2053
2141
  }
2054
2142
  });
2055
- const streamDataTransformer = new TransformStream({
2143
+ const streamPartsTransformer = new TransformStream({
2056
2144
  transform: async (chunk, controller) => {
2057
2145
  const chunkType = chunk.type;
2058
2146
  switch (chunkType) {
@@ -2108,7 +2196,7 @@ var StreamTextResult = class {
2108
2196
  }
2109
2197
  }
2110
2198
  });
2111
- return this.fullStream.pipeThrough(callbackTransformer).pipeThrough(streamDataTransformer).pipeThrough(new TextEncoderStream());
2199
+ return this.fullStream.pipeThrough(callbackTransformer).pipeThrough(streamPartsTransformer).pipeThrough(new TextEncoderStream());
2112
2200
  }
2113
2201
  /**
2114
2202
  Writes stream data output to a Node.js response-like object.
@@ -2176,12 +2264,27 @@ var StreamTextResult = class {
2176
2264
  Converts the result to a streamed response object with a stream data part stream.
2177
2265
  It can be used with the `useChat` and `useCompletion` hooks.
2178
2266
 
2179
- @param init Optional headers.
2267
+ @param options An object with an init property (ResponseInit) and a data property.
2268
+ You can also pass in a ResponseInit directly (deprecated).
2180
2269
 
2181
2270
  @return A response object.
2182
2271
  */
2183
- toAIStreamResponse(init) {
2184
- return new StreamingTextResponse(this.toAIStream(), init);
2272
+ toAIStreamResponse(options) {
2273
+ var _a;
2274
+ const init = options == null ? void 0 : "init" in options ? options.init : {
2275
+ headers: "headers" in options ? options.headers : void 0,
2276
+ status: "status" in options ? options.status : void 0,
2277
+ statusText: "statusText" in options ? options.statusText : void 0
2278
+ };
2279
+ const data = options == null ? void 0 : "data" in options ? options.data : void 0;
2280
+ const stream = data ? mergeStreams(data.stream, this.toAIStream()) : this.toAIStream();
2281
+ return new Response(stream, {
2282
+ status: (_a = init == null ? void 0 : init.status) != null ? _a : 200,
2283
+ statusText: init == null ? void 0 : init.statusText,
2284
+ headers: prepareResponseHeaders(init, {
2285
+ contentType: "text/plain; charset=utf-8"
2286
+ })
2287
+ });
2185
2288
  }
2186
2289
  /**
2187
2290
  Creates a simple text stream response.
@@ -2625,7 +2728,7 @@ function readableFromAsyncIterable(iterable) {
2625
2728
 
2626
2729
  // streams/stream-data.ts
2627
2730
  import { formatStreamPart as formatStreamPart2 } from "@ai-sdk/ui-utils";
2628
- var StreamData = class {
2731
+ var StreamData2 = class {
2629
2732
  constructor() {
2630
2733
  this.encoder = new TextEncoder();
2631
2734
  this.controller = null;
@@ -2696,7 +2799,7 @@ function createStreamDataTransformer() {
2696
2799
  }
2697
2800
  });
2698
2801
  }
2699
- var experimental_StreamData = class extends StreamData {
2802
+ var experimental_StreamData = class extends StreamData2 {
2700
2803
  };
2701
2804
 
2702
2805
  // streams/anthropic-stream.ts
@@ -3466,94 +3569,6 @@ async function ReplicateStream(res, cb, options) {
3466
3569
  );
3467
3570
  }
3468
3571
 
3469
- // core/util/merge-streams.ts
3470
- function mergeStreams(stream1, stream2) {
3471
- const reader1 = stream1.getReader();
3472
- const reader2 = stream2.getReader();
3473
- let lastRead1 = void 0;
3474
- let lastRead2 = void 0;
3475
- let stream1Done = false;
3476
- let stream2Done = false;
3477
- async function readStream1(controller) {
3478
- try {
3479
- if (lastRead1 == null) {
3480
- lastRead1 = reader1.read();
3481
- }
3482
- const result = await lastRead1;
3483
- lastRead1 = void 0;
3484
- if (!result.done) {
3485
- controller.enqueue(result.value);
3486
- } else {
3487
- controller.close();
3488
- }
3489
- } catch (error) {
3490
- controller.error(error);
3491
- }
3492
- }
3493
- async function readStream2(controller) {
3494
- try {
3495
- if (lastRead2 == null) {
3496
- lastRead2 = reader2.read();
3497
- }
3498
- const result = await lastRead2;
3499
- lastRead2 = void 0;
3500
- if (!result.done) {
3501
- controller.enqueue(result.value);
3502
- } else {
3503
- controller.close();
3504
- }
3505
- } catch (error) {
3506
- controller.error(error);
3507
- }
3508
- }
3509
- return new ReadableStream({
3510
- async pull(controller) {
3511
- try {
3512
- if (stream1Done) {
3513
- readStream2(controller);
3514
- return;
3515
- }
3516
- if (stream2Done) {
3517
- readStream1(controller);
3518
- return;
3519
- }
3520
- if (lastRead1 == null) {
3521
- lastRead1 = reader1.read();
3522
- }
3523
- if (lastRead2 == null) {
3524
- lastRead2 = reader2.read();
3525
- }
3526
- const { result, reader } = await Promise.race([
3527
- lastRead1.then((result2) => ({ result: result2, reader: reader1 })),
3528
- lastRead2.then((result2) => ({ result: result2, reader: reader2 }))
3529
- ]);
3530
- if (!result.done) {
3531
- controller.enqueue(result.value);
3532
- }
3533
- if (reader === reader1) {
3534
- lastRead1 = void 0;
3535
- if (result.done) {
3536
- readStream2(controller);
3537
- stream1Done = true;
3538
- }
3539
- } else {
3540
- lastRead2 = void 0;
3541
- if (result.done) {
3542
- stream2Done = true;
3543
- readStream1(controller);
3544
- }
3545
- }
3546
- } catch (error) {
3547
- controller.error(error);
3548
- }
3549
- },
3550
- cancel() {
3551
- reader1.cancel();
3552
- reader2.cancel();
3553
- }
3554
- });
3555
- }
3556
-
3557
3572
  // streams/stream-to-response.ts
3558
3573
  function streamToResponse(res, response, init, data) {
3559
3574
  var _a;
@@ -3637,7 +3652,7 @@ export {
3637
3652
  OpenAIStream,
3638
3653
  ReplicateStream,
3639
3654
  RetryError2 as RetryError,
3640
- StreamData,
3655
+ StreamData2 as StreamData,
3641
3656
  StreamObjectResult,
3642
3657
  StreamTextResult,
3643
3658
  StreamingTextResponse,