ai 3.2.26 → 3.2.28
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 +23 -9
- package/dist/index.d.ts +23 -9
- package/dist/index.js +117 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +117 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.mjs
CHANGED
@@ -748,6 +748,44 @@ function createAsyncIterableStream(source, transformer) {
|
|
748
748
|
return transformedStream;
|
749
749
|
}
|
750
750
|
|
751
|
+
// core/util/delayed-promise.ts
|
752
|
+
var DelayedPromise = class {
|
753
|
+
constructor() {
|
754
|
+
this.status = { type: "pending" };
|
755
|
+
this._resolve = void 0;
|
756
|
+
this._reject = void 0;
|
757
|
+
}
|
758
|
+
get value() {
|
759
|
+
if (this.promise) {
|
760
|
+
return this.promise;
|
761
|
+
}
|
762
|
+
this.promise = new Promise((resolve, reject) => {
|
763
|
+
if (this.status.type === "resolved") {
|
764
|
+
resolve(this.status.value);
|
765
|
+
} else if (this.status.type === "rejected") {
|
766
|
+
reject(this.status.error);
|
767
|
+
}
|
768
|
+
this._resolve = resolve;
|
769
|
+
this._reject = reject;
|
770
|
+
});
|
771
|
+
return this.promise;
|
772
|
+
}
|
773
|
+
resolve(value) {
|
774
|
+
var _a;
|
775
|
+
this.status = { type: "resolved", value };
|
776
|
+
if (this.promise) {
|
777
|
+
(_a = this._resolve) == null ? void 0 : _a.call(this, value);
|
778
|
+
}
|
779
|
+
}
|
780
|
+
reject(error) {
|
781
|
+
var _a;
|
782
|
+
this.status = { type: "rejected", error };
|
783
|
+
if (this.promise) {
|
784
|
+
(_a = this._reject) == null ? void 0 : _a.call(this, error);
|
785
|
+
}
|
786
|
+
}
|
787
|
+
};
|
788
|
+
|
751
789
|
// core/generate-object/stream-object.ts
|
752
790
|
async function streamObject({
|
753
791
|
model,
|
@@ -892,12 +930,7 @@ var StreamObjectResult = class {
|
|
892
930
|
}) {
|
893
931
|
this.warnings = warnings;
|
894
932
|
this.rawResponse = rawResponse;
|
895
|
-
|
896
|
-
let rejectObject;
|
897
|
-
this.object = new Promise((resolve, reject) => {
|
898
|
-
resolveObject = resolve;
|
899
|
-
rejectObject = reject;
|
900
|
-
});
|
933
|
+
this.objectPromise = new DelayedPromise();
|
901
934
|
let resolveUsage;
|
902
935
|
this.usage = new Promise((resolve) => {
|
903
936
|
resolveUsage = resolve;
|
@@ -908,6 +941,7 @@ var StreamObjectResult = class {
|
|
908
941
|
let accumulatedText = "";
|
909
942
|
let delta = "";
|
910
943
|
let latestObject = void 0;
|
944
|
+
const self = this;
|
911
945
|
this.originalStream = stream.pipeThrough(
|
912
946
|
new TransformStream({
|
913
947
|
async transform(chunk, controller) {
|
@@ -948,10 +982,10 @@ var StreamObjectResult = class {
|
|
948
982
|
});
|
949
983
|
if (validationResult.success) {
|
950
984
|
object = validationResult.value;
|
951
|
-
|
985
|
+
self.objectPromise.resolve(object);
|
952
986
|
} else {
|
953
987
|
error = validationResult.error;
|
954
|
-
|
988
|
+
self.objectPromise.reject(error);
|
955
989
|
}
|
956
990
|
break;
|
957
991
|
}
|
@@ -983,6 +1017,12 @@ var StreamObjectResult = class {
|
|
983
1017
|
);
|
984
1018
|
}
|
985
1019
|
/**
|
1020
|
+
The generated object (typed according to the schema). Resolved when the response is finished.
|
1021
|
+
*/
|
1022
|
+
get object() {
|
1023
|
+
return this.objectPromise.value;
|
1024
|
+
}
|
1025
|
+
/**
|
986
1026
|
Stream of partial objects. It gets more complete as the stream progresses.
|
987
1027
|
|
988
1028
|
Note that the partial object is not validated.
|
@@ -1367,6 +1407,11 @@ async function generateText({
|
|
1367
1407
|
let roundtripCount = 0;
|
1368
1408
|
const responseMessages = [];
|
1369
1409
|
const roundtrips = [];
|
1410
|
+
const usage = {
|
1411
|
+
completionTokens: 0,
|
1412
|
+
promptTokens: 0,
|
1413
|
+
totalTokens: 0
|
1414
|
+
};
|
1370
1415
|
do {
|
1371
1416
|
const currentInputFormat = roundtripCount === 0 ? validatedPrompt.type : "messages";
|
1372
1417
|
currentModelResponse = await retry(
|
@@ -1406,12 +1451,18 @@ async function generateText({
|
|
1406
1451
|
tools,
|
1407
1452
|
tracer
|
1408
1453
|
});
|
1454
|
+
const currentUsage = calculateCompletionTokenUsage(
|
1455
|
+
currentModelResponse.usage
|
1456
|
+
);
|
1457
|
+
usage.completionTokens += currentUsage.completionTokens;
|
1458
|
+
usage.promptTokens += currentUsage.promptTokens;
|
1459
|
+
usage.totalTokens += currentUsage.totalTokens;
|
1409
1460
|
roundtrips.push({
|
1410
1461
|
text: (_b = currentModelResponse.text) != null ? _b : "",
|
1411
1462
|
toolCalls: currentToolCalls,
|
1412
1463
|
toolResults: currentToolResults,
|
1413
1464
|
finishReason: currentModelResponse.finishReason,
|
1414
|
-
usage:
|
1465
|
+
usage: currentUsage,
|
1415
1466
|
warnings: currentModelResponse.warnings,
|
1416
1467
|
logprobs: currentModelResponse.logprobs
|
1417
1468
|
});
|
@@ -1445,7 +1496,7 @@ async function generateText({
|
|
1445
1496
|
toolCalls: currentToolCalls,
|
1446
1497
|
toolResults: currentToolResults,
|
1447
1498
|
finishReason: currentModelResponse.finishReason,
|
1448
|
-
usage
|
1499
|
+
usage,
|
1449
1500
|
warnings: currentModelResponse.warnings,
|
1450
1501
|
rawResponse: currentModelResponse.rawResponse,
|
1451
1502
|
logprobs: currentModelResponse.logprobs,
|
@@ -1542,6 +1593,7 @@ import { generateId } from "@ai-sdk/ui-utils";
|
|
1542
1593
|
function runToolsTransformation({
|
1543
1594
|
tools,
|
1544
1595
|
generatorStream,
|
1596
|
+
toolCallStreaming,
|
1545
1597
|
tracer
|
1546
1598
|
}) {
|
1547
1599
|
let canClose = false;
|
@@ -1552,6 +1604,7 @@ function runToolsTransformation({
|
|
1552
1604
|
toolResultsStreamController = controller;
|
1553
1605
|
}
|
1554
1606
|
});
|
1607
|
+
const activeToolCalls = {};
|
1555
1608
|
const forwardStream = new TransformStream({
|
1556
1609
|
transform(chunk, controller) {
|
1557
1610
|
const chunkType = chunk.type;
|
@@ -1561,6 +1614,25 @@ function runToolsTransformation({
|
|
1561
1614
|
controller.enqueue(chunk);
|
1562
1615
|
break;
|
1563
1616
|
}
|
1617
|
+
case "tool-call-delta": {
|
1618
|
+
if (toolCallStreaming) {
|
1619
|
+
if (!activeToolCalls[chunk.toolCallId]) {
|
1620
|
+
controller.enqueue({
|
1621
|
+
type: "tool-call-streaming-start",
|
1622
|
+
toolCallId: chunk.toolCallId,
|
1623
|
+
toolName: chunk.toolName
|
1624
|
+
});
|
1625
|
+
activeToolCalls[chunk.toolCallId] = true;
|
1626
|
+
}
|
1627
|
+
controller.enqueue({
|
1628
|
+
type: "tool-call-delta",
|
1629
|
+
toolCallId: chunk.toolCallId,
|
1630
|
+
toolName: chunk.toolName,
|
1631
|
+
argsTextDelta: chunk.argsTextDelta
|
1632
|
+
});
|
1633
|
+
}
|
1634
|
+
break;
|
1635
|
+
}
|
1564
1636
|
case "tool-call": {
|
1565
1637
|
const toolName = chunk.toolName;
|
1566
1638
|
if (tools == null) {
|
@@ -1646,9 +1718,6 @@ function runToolsTransformation({
|
|
1646
1718
|
});
|
1647
1719
|
break;
|
1648
1720
|
}
|
1649
|
-
case "tool-call-delta": {
|
1650
|
-
break;
|
1651
|
-
}
|
1652
1721
|
default: {
|
1653
1722
|
const _exhaustiveCheck = chunkType;
|
1654
1723
|
throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`);
|
@@ -1701,6 +1770,7 @@ async function streamText({
|
|
1701
1770
|
abortSignal,
|
1702
1771
|
headers,
|
1703
1772
|
experimental_telemetry: telemetry,
|
1773
|
+
experimental_toolCallStreaming: toolCallStreaming = false,
|
1704
1774
|
onFinish,
|
1705
1775
|
...settings
|
1706
1776
|
}) {
|
@@ -1761,6 +1831,7 @@ async function streamText({
|
|
1761
1831
|
stream: runToolsTransformation({
|
1762
1832
|
tools,
|
1763
1833
|
generatorStream: stream,
|
1834
|
+
toolCallStreaming,
|
1764
1835
|
tracer
|
1765
1836
|
}),
|
1766
1837
|
warnings,
|
@@ -1838,6 +1909,8 @@ var StreamTextResult = class {
|
|
1838
1909
|
resolveText(text);
|
1839
1910
|
resolveToolCalls(toolCalls);
|
1840
1911
|
break;
|
1912
|
+
case "tool-call-streaming-start":
|
1913
|
+
case "tool-call-delta":
|
1841
1914
|
case "error":
|
1842
1915
|
break;
|
1843
1916
|
default: {
|
@@ -1981,10 +2054,27 @@ var StreamTextResult = class {
|
|
1981
2054
|
});
|
1982
2055
|
const streamDataTransformer = new TransformStream({
|
1983
2056
|
transform: async (chunk, controller) => {
|
1984
|
-
|
2057
|
+
const chunkType = chunk.type;
|
2058
|
+
switch (chunkType) {
|
1985
2059
|
case "text-delta":
|
1986
2060
|
controller.enqueue(formatStreamPart("text", chunk.textDelta));
|
1987
2061
|
break;
|
2062
|
+
case "tool-call-streaming-start":
|
2063
|
+
controller.enqueue(
|
2064
|
+
formatStreamPart("tool_call_streaming_start", {
|
2065
|
+
toolCallId: chunk.toolCallId,
|
2066
|
+
toolName: chunk.toolName
|
2067
|
+
})
|
2068
|
+
);
|
2069
|
+
break;
|
2070
|
+
case "tool-call-delta":
|
2071
|
+
controller.enqueue(
|
2072
|
+
formatStreamPart("tool_call_delta", {
|
2073
|
+
toolCallId: chunk.toolCallId,
|
2074
|
+
argsTextDelta: chunk.argsTextDelta
|
2075
|
+
})
|
2076
|
+
);
|
2077
|
+
break;
|
1988
2078
|
case "tool-call":
|
1989
2079
|
controller.enqueue(
|
1990
2080
|
formatStreamPart("tool_call", {
|
@@ -2009,6 +2099,12 @@ var StreamTextResult = class {
|
|
2009
2099
|
formatStreamPart("error", JSON.stringify(chunk.error))
|
2010
2100
|
);
|
2011
2101
|
break;
|
2102
|
+
case "finish":
|
2103
|
+
break;
|
2104
|
+
default: {
|
2105
|
+
const exhaustiveCheck = chunkType;
|
2106
|
+
throw new Error(`Unknown chunk type: ${exhaustiveCheck}`);
|
2107
|
+
}
|
2012
2108
|
}
|
2013
2109
|
}
|
2014
2110
|
});
|
@@ -2171,6 +2267,13 @@ function convertToCoreMessages(messages) {
|
|
2171
2267
|
experimental_attachments
|
2172
2268
|
} of messages) {
|
2173
2269
|
switch (role) {
|
2270
|
+
case "system": {
|
2271
|
+
coreMessages.push({
|
2272
|
+
role: "system",
|
2273
|
+
content
|
2274
|
+
});
|
2275
|
+
break;
|
2276
|
+
}
|
2174
2277
|
case "user": {
|
2175
2278
|
coreMessages.push({
|
2176
2279
|
role: "user",
|