ai 3.1.1 → 3.1.3

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
@@ -658,6 +658,7 @@ function fixJson(input) {
658
658
  break;
659
659
  }
660
660
  case "}": {
661
+ lastValidIndex = i;
661
662
  stack.pop();
662
663
  break;
663
664
  }
@@ -1392,9 +1393,40 @@ var StreamTextResult = class {
1392
1393
  warnings,
1393
1394
  rawResponse
1394
1395
  }) {
1395
- this.originalStream = stream;
1396
1396
  this.warnings = warnings;
1397
1397
  this.rawResponse = rawResponse;
1398
+ let resolveUsage;
1399
+ this.usage = new Promise((resolve) => {
1400
+ resolveUsage = resolve;
1401
+ });
1402
+ let resolveFinishReason;
1403
+ this.finishReason = new Promise((resolve) => {
1404
+ resolveFinishReason = resolve;
1405
+ });
1406
+ this.originalStream = stream.pipeThrough(
1407
+ new TransformStream({
1408
+ async transform(chunk, controller) {
1409
+ controller.enqueue(chunk);
1410
+ if (chunk.type === "finish") {
1411
+ resolveUsage(chunk.usage);
1412
+ resolveFinishReason(chunk.finishReason);
1413
+ }
1414
+ }
1415
+ })
1416
+ );
1417
+ }
1418
+ /**
1419
+ Split out a new stream from the original stream.
1420
+ The original stream is replaced to allow for further splitting,
1421
+ since we do not know how many times the stream will be split.
1422
+
1423
+ Note: this leads to buffering the stream content on the server.
1424
+ However, the LLM results are expected to be small enough to not cause issues.
1425
+ */
1426
+ teeStream() {
1427
+ const [stream1, stream2] = this.originalStream.tee();
1428
+ this.originalStream = stream2;
1429
+ return stream1;
1398
1430
  }
1399
1431
  /**
1400
1432
  A text stream that returns only the generated text deltas. You can use it
@@ -1402,7 +1434,7 @@ var StreamTextResult = class {
1402
1434
  stream will throw the error.
1403
1435
  */
1404
1436
  get textStream() {
1405
- return createAsyncIterableStream(this.originalStream, {
1437
+ return createAsyncIterableStream(this.teeStream(), {
1406
1438
  transform(chunk, controller) {
1407
1439
  if (chunk.type === "text-delta") {
1408
1440
  if (chunk.textDelta.length > 0) {
@@ -1421,7 +1453,7 @@ var StreamTextResult = class {
1421
1453
  stream will throw the error.
1422
1454
  */
1423
1455
  get fullStream() {
1424
- return createAsyncIterableStream(this.originalStream, {
1456
+ return createAsyncIterableStream(this.teeStream(), {
1425
1457
  transform(chunk, controller) {
1426
1458
  if (chunk.type === "text-delta") {
1427
1459
  if (chunk.textDelta.length > 0) {