ai 3.1.14 → 3.1.15
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 +60 -4
- package/dist/index.d.ts +60 -4
- package/dist/index.js +65 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +65 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
@@ -1449,6 +1449,7 @@ async function streamText({
|
|
1449
1449
|
messages,
|
1450
1450
|
maxRetries,
|
1451
1451
|
abortSignal,
|
1452
|
+
onFinish,
|
1452
1453
|
...settings
|
1453
1454
|
}) {
|
1454
1455
|
const retry = retryWithExponentialBackoff({ maxRetries });
|
@@ -1476,17 +1477,20 @@ async function streamText({
|
|
1476
1477
|
generatorStream: stream
|
1477
1478
|
}),
|
1478
1479
|
warnings,
|
1479
|
-
rawResponse
|
1480
|
+
rawResponse,
|
1481
|
+
onFinish
|
1480
1482
|
});
|
1481
1483
|
}
|
1482
1484
|
var StreamTextResult = class {
|
1483
1485
|
constructor({
|
1484
1486
|
stream,
|
1485
1487
|
warnings,
|
1486
|
-
rawResponse
|
1488
|
+
rawResponse,
|
1489
|
+
onFinish
|
1487
1490
|
}) {
|
1488
1491
|
this.warnings = warnings;
|
1489
1492
|
this.rawResponse = rawResponse;
|
1493
|
+
this.onFinish = onFinish;
|
1490
1494
|
let resolveUsage;
|
1491
1495
|
this.usage = new Promise((resolve) => {
|
1492
1496
|
resolveUsage = resolve;
|
@@ -1495,13 +1499,70 @@ var StreamTextResult = class {
|
|
1495
1499
|
this.finishReason = new Promise((resolve) => {
|
1496
1500
|
resolveFinishReason = resolve;
|
1497
1501
|
});
|
1502
|
+
let resolveText;
|
1503
|
+
this.text = new Promise((resolve) => {
|
1504
|
+
resolveText = resolve;
|
1505
|
+
});
|
1506
|
+
let resolveToolCalls;
|
1507
|
+
this.toolCalls = new Promise((resolve) => {
|
1508
|
+
resolveToolCalls = resolve;
|
1509
|
+
});
|
1510
|
+
let resolveToolResults;
|
1511
|
+
this.toolResults = new Promise((resolve) => {
|
1512
|
+
resolveToolResults = resolve;
|
1513
|
+
});
|
1514
|
+
let finishReason;
|
1515
|
+
let usage;
|
1516
|
+
let text = "";
|
1517
|
+
const toolCalls = [];
|
1518
|
+
const toolResults = [];
|
1519
|
+
const self = this;
|
1498
1520
|
this.originalStream = stream.pipeThrough(
|
1499
1521
|
new TransformStream({
|
1500
1522
|
async transform(chunk, controller) {
|
1501
1523
|
controller.enqueue(chunk);
|
1524
|
+
if (chunk.type === "text-delta") {
|
1525
|
+
text += chunk.textDelta;
|
1526
|
+
}
|
1527
|
+
if (chunk.type === "tool-call") {
|
1528
|
+
toolCalls.push(chunk);
|
1529
|
+
}
|
1530
|
+
if (chunk.type === "tool-result") {
|
1531
|
+
toolResults.push(chunk);
|
1532
|
+
}
|
1502
1533
|
if (chunk.type === "finish") {
|
1503
|
-
|
1504
|
-
|
1534
|
+
usage = chunk.usage;
|
1535
|
+
finishReason = chunk.finishReason;
|
1536
|
+
resolveUsage(usage);
|
1537
|
+
resolveFinishReason(finishReason);
|
1538
|
+
resolveText(text);
|
1539
|
+
resolveToolCalls(toolCalls);
|
1540
|
+
}
|
1541
|
+
},
|
1542
|
+
// invoke onFinish callback and resolve toolResults promise when the stream is about to close:
|
1543
|
+
async flush(controller) {
|
1544
|
+
var _a;
|
1545
|
+
try {
|
1546
|
+
resolveToolResults(toolResults);
|
1547
|
+
await ((_a = self.onFinish) == null ? void 0 : _a.call(self, {
|
1548
|
+
finishReason: finishReason != null ? finishReason : "unknown",
|
1549
|
+
usage: usage != null ? usage : {
|
1550
|
+
promptTokens: NaN,
|
1551
|
+
completionTokens: NaN,
|
1552
|
+
totalTokens: NaN
|
1553
|
+
},
|
1554
|
+
text,
|
1555
|
+
toolCalls,
|
1556
|
+
// The tool results are inferred as a never[] type, because they are
|
1557
|
+
// optional and the execute method with an inferred result type is
|
1558
|
+
// optional as well. Therefore we need to cast the toolResults to any.
|
1559
|
+
// The type exposed to the users will be correctly inferred.
|
1560
|
+
toolResults,
|
1561
|
+
rawResponse,
|
1562
|
+
warnings
|
1563
|
+
}));
|
1564
|
+
} catch (error) {
|
1565
|
+
controller.error(error);
|
1505
1566
|
}
|
1506
1567
|
}
|
1507
1568
|
})
|