ai 3.2.27 → 3.2.29
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 +29 -11
- package/dist/index.d.ts +29 -11
- package/dist/index.js +215 -108
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +215 -108
- 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.
|
@@ -1547,12 +1587,101 @@ function toResponseMessages({
|
|
1547
1587
|
}
|
1548
1588
|
var experimental_generateText = generateText;
|
1549
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
|
+
|
1550
1678
|
// core/generate-text/run-tools-transformation.ts
|
1551
1679
|
import { NoSuchToolError as NoSuchToolError2 } from "@ai-sdk/provider";
|
1552
1680
|
import { generateId } from "@ai-sdk/ui-utils";
|
1553
1681
|
function runToolsTransformation({
|
1554
1682
|
tools,
|
1555
1683
|
generatorStream,
|
1684
|
+
toolCallStreaming,
|
1556
1685
|
tracer
|
1557
1686
|
}) {
|
1558
1687
|
let canClose = false;
|
@@ -1563,6 +1692,7 @@ function runToolsTransformation({
|
|
1563
1692
|
toolResultsStreamController = controller;
|
1564
1693
|
}
|
1565
1694
|
});
|
1695
|
+
const activeToolCalls = {};
|
1566
1696
|
const forwardStream = new TransformStream({
|
1567
1697
|
transform(chunk, controller) {
|
1568
1698
|
const chunkType = chunk.type;
|
@@ -1572,6 +1702,25 @@ function runToolsTransformation({
|
|
1572
1702
|
controller.enqueue(chunk);
|
1573
1703
|
break;
|
1574
1704
|
}
|
1705
|
+
case "tool-call-delta": {
|
1706
|
+
if (toolCallStreaming) {
|
1707
|
+
if (!activeToolCalls[chunk.toolCallId]) {
|
1708
|
+
controller.enqueue({
|
1709
|
+
type: "tool-call-streaming-start",
|
1710
|
+
toolCallId: chunk.toolCallId,
|
1711
|
+
toolName: chunk.toolName
|
1712
|
+
});
|
1713
|
+
activeToolCalls[chunk.toolCallId] = true;
|
1714
|
+
}
|
1715
|
+
controller.enqueue({
|
1716
|
+
type: "tool-call-delta",
|
1717
|
+
toolCallId: chunk.toolCallId,
|
1718
|
+
toolName: chunk.toolName,
|
1719
|
+
argsTextDelta: chunk.argsTextDelta
|
1720
|
+
});
|
1721
|
+
}
|
1722
|
+
break;
|
1723
|
+
}
|
1575
1724
|
case "tool-call": {
|
1576
1725
|
const toolName = chunk.toolName;
|
1577
1726
|
if (tools == null) {
|
@@ -1657,9 +1806,6 @@ function runToolsTransformation({
|
|
1657
1806
|
});
|
1658
1807
|
break;
|
1659
1808
|
}
|
1660
|
-
case "tool-call-delta": {
|
1661
|
-
break;
|
1662
|
-
}
|
1663
1809
|
default: {
|
1664
1810
|
const _exhaustiveCheck = chunkType;
|
1665
1811
|
throw new Error(`Unhandled chunk type: ${_exhaustiveCheck}`);
|
@@ -1712,6 +1858,7 @@ async function streamText({
|
|
1712
1858
|
abortSignal,
|
1713
1859
|
headers,
|
1714
1860
|
experimental_telemetry: telemetry,
|
1861
|
+
experimental_toolCallStreaming: toolCallStreaming = false,
|
1715
1862
|
onFinish,
|
1716
1863
|
...settings
|
1717
1864
|
}) {
|
@@ -1772,6 +1919,7 @@ async function streamText({
|
|
1772
1919
|
stream: runToolsTransformation({
|
1773
1920
|
tools,
|
1774
1921
|
generatorStream: stream,
|
1922
|
+
toolCallStreaming,
|
1775
1923
|
tracer
|
1776
1924
|
}),
|
1777
1925
|
warnings,
|
@@ -1849,6 +1997,8 @@ var StreamTextResult = class {
|
|
1849
1997
|
resolveText(text);
|
1850
1998
|
resolveToolCalls(toolCalls);
|
1851
1999
|
break;
|
2000
|
+
case "tool-call-streaming-start":
|
2001
|
+
case "tool-call-delta":
|
1852
2002
|
case "error":
|
1853
2003
|
break;
|
1854
2004
|
default: {
|
@@ -1990,12 +2140,29 @@ var StreamTextResult = class {
|
|
1990
2140
|
await callbacks.onFinal(aggregatedResponse);
|
1991
2141
|
}
|
1992
2142
|
});
|
1993
|
-
const
|
2143
|
+
const streamPartsTransformer = new TransformStream({
|
1994
2144
|
transform: async (chunk, controller) => {
|
1995
|
-
|
2145
|
+
const chunkType = chunk.type;
|
2146
|
+
switch (chunkType) {
|
1996
2147
|
case "text-delta":
|
1997
2148
|
controller.enqueue(formatStreamPart("text", chunk.textDelta));
|
1998
2149
|
break;
|
2150
|
+
case "tool-call-streaming-start":
|
2151
|
+
controller.enqueue(
|
2152
|
+
formatStreamPart("tool_call_streaming_start", {
|
2153
|
+
toolCallId: chunk.toolCallId,
|
2154
|
+
toolName: chunk.toolName
|
2155
|
+
})
|
2156
|
+
);
|
2157
|
+
break;
|
2158
|
+
case "tool-call-delta":
|
2159
|
+
controller.enqueue(
|
2160
|
+
formatStreamPart("tool_call_delta", {
|
2161
|
+
toolCallId: chunk.toolCallId,
|
2162
|
+
argsTextDelta: chunk.argsTextDelta
|
2163
|
+
})
|
2164
|
+
);
|
2165
|
+
break;
|
1999
2166
|
case "tool-call":
|
2000
2167
|
controller.enqueue(
|
2001
2168
|
formatStreamPart("tool_call", {
|
@@ -2020,10 +2187,16 @@ var StreamTextResult = class {
|
|
2020
2187
|
formatStreamPart("error", JSON.stringify(chunk.error))
|
2021
2188
|
);
|
2022
2189
|
break;
|
2190
|
+
case "finish":
|
2191
|
+
break;
|
2192
|
+
default: {
|
2193
|
+
const exhaustiveCheck = chunkType;
|
2194
|
+
throw new Error(`Unknown chunk type: ${exhaustiveCheck}`);
|
2195
|
+
}
|
2023
2196
|
}
|
2024
2197
|
}
|
2025
2198
|
});
|
2026
|
-
return this.fullStream.pipeThrough(callbackTransformer).pipeThrough(
|
2199
|
+
return this.fullStream.pipeThrough(callbackTransformer).pipeThrough(streamPartsTransformer).pipeThrough(new TextEncoderStream());
|
2027
2200
|
}
|
2028
2201
|
/**
|
2029
2202
|
Writes stream data output to a Node.js response-like object.
|
@@ -2091,12 +2264,27 @@ var StreamTextResult = class {
|
|
2091
2264
|
Converts the result to a streamed response object with a stream data part stream.
|
2092
2265
|
It can be used with the `useChat` and `useCompletion` hooks.
|
2093
2266
|
|
2094
|
-
@param init
|
2267
|
+
@param options An object with an init property (ResponseInit) and a data property.
|
2268
|
+
You can also pass in a ResponseInit directly (deprecated).
|
2095
2269
|
|
2096
2270
|
@return A response object.
|
2097
2271
|
*/
|
2098
|
-
toAIStreamResponse(
|
2099
|
-
|
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
|
+
});
|
2100
2288
|
}
|
2101
2289
|
/**
|
2102
2290
|
Creates a simple text stream response.
|
@@ -2182,6 +2370,13 @@ function convertToCoreMessages(messages) {
|
|
2182
2370
|
experimental_attachments
|
2183
2371
|
} of messages) {
|
2184
2372
|
switch (role) {
|
2373
|
+
case "system": {
|
2374
|
+
coreMessages.push({
|
2375
|
+
role: "system",
|
2376
|
+
content
|
2377
|
+
});
|
2378
|
+
break;
|
2379
|
+
}
|
2185
2380
|
case "user": {
|
2186
2381
|
coreMessages.push({
|
2187
2382
|
role: "user",
|
@@ -2533,7 +2728,7 @@ function readableFromAsyncIterable(iterable) {
|
|
2533
2728
|
|
2534
2729
|
// streams/stream-data.ts
|
2535
2730
|
import { formatStreamPart as formatStreamPart2 } from "@ai-sdk/ui-utils";
|
2536
|
-
var
|
2731
|
+
var StreamData2 = class {
|
2537
2732
|
constructor() {
|
2538
2733
|
this.encoder = new TextEncoder();
|
2539
2734
|
this.controller = null;
|
@@ -2604,7 +2799,7 @@ function createStreamDataTransformer() {
|
|
2604
2799
|
}
|
2605
2800
|
});
|
2606
2801
|
}
|
2607
|
-
var experimental_StreamData = class extends
|
2802
|
+
var experimental_StreamData = class extends StreamData2 {
|
2608
2803
|
};
|
2609
2804
|
|
2610
2805
|
// streams/anthropic-stream.ts
|
@@ -3374,94 +3569,6 @@ async function ReplicateStream(res, cb, options) {
|
|
3374
3569
|
);
|
3375
3570
|
}
|
3376
3571
|
|
3377
|
-
// core/util/merge-streams.ts
|
3378
|
-
function mergeStreams(stream1, stream2) {
|
3379
|
-
const reader1 = stream1.getReader();
|
3380
|
-
const reader2 = stream2.getReader();
|
3381
|
-
let lastRead1 = void 0;
|
3382
|
-
let lastRead2 = void 0;
|
3383
|
-
let stream1Done = false;
|
3384
|
-
let stream2Done = false;
|
3385
|
-
async function readStream1(controller) {
|
3386
|
-
try {
|
3387
|
-
if (lastRead1 == null) {
|
3388
|
-
lastRead1 = reader1.read();
|
3389
|
-
}
|
3390
|
-
const result = await lastRead1;
|
3391
|
-
lastRead1 = void 0;
|
3392
|
-
if (!result.done) {
|
3393
|
-
controller.enqueue(result.value);
|
3394
|
-
} else {
|
3395
|
-
controller.close();
|
3396
|
-
}
|
3397
|
-
} catch (error) {
|
3398
|
-
controller.error(error);
|
3399
|
-
}
|
3400
|
-
}
|
3401
|
-
async function readStream2(controller) {
|
3402
|
-
try {
|
3403
|
-
if (lastRead2 == null) {
|
3404
|
-
lastRead2 = reader2.read();
|
3405
|
-
}
|
3406
|
-
const result = await lastRead2;
|
3407
|
-
lastRead2 = void 0;
|
3408
|
-
if (!result.done) {
|
3409
|
-
controller.enqueue(result.value);
|
3410
|
-
} else {
|
3411
|
-
controller.close();
|
3412
|
-
}
|
3413
|
-
} catch (error) {
|
3414
|
-
controller.error(error);
|
3415
|
-
}
|
3416
|
-
}
|
3417
|
-
return new ReadableStream({
|
3418
|
-
async pull(controller) {
|
3419
|
-
try {
|
3420
|
-
if (stream1Done) {
|
3421
|
-
readStream2(controller);
|
3422
|
-
return;
|
3423
|
-
}
|
3424
|
-
if (stream2Done) {
|
3425
|
-
readStream1(controller);
|
3426
|
-
return;
|
3427
|
-
}
|
3428
|
-
if (lastRead1 == null) {
|
3429
|
-
lastRead1 = reader1.read();
|
3430
|
-
}
|
3431
|
-
if (lastRead2 == null) {
|
3432
|
-
lastRead2 = reader2.read();
|
3433
|
-
}
|
3434
|
-
const { result, reader } = await Promise.race([
|
3435
|
-
lastRead1.then((result2) => ({ result: result2, reader: reader1 })),
|
3436
|
-
lastRead2.then((result2) => ({ result: result2, reader: reader2 }))
|
3437
|
-
]);
|
3438
|
-
if (!result.done) {
|
3439
|
-
controller.enqueue(result.value);
|
3440
|
-
}
|
3441
|
-
if (reader === reader1) {
|
3442
|
-
lastRead1 = void 0;
|
3443
|
-
if (result.done) {
|
3444
|
-
readStream2(controller);
|
3445
|
-
stream1Done = true;
|
3446
|
-
}
|
3447
|
-
} else {
|
3448
|
-
lastRead2 = void 0;
|
3449
|
-
if (result.done) {
|
3450
|
-
stream2Done = true;
|
3451
|
-
readStream1(controller);
|
3452
|
-
}
|
3453
|
-
}
|
3454
|
-
} catch (error) {
|
3455
|
-
controller.error(error);
|
3456
|
-
}
|
3457
|
-
},
|
3458
|
-
cancel() {
|
3459
|
-
reader1.cancel();
|
3460
|
-
reader2.cancel();
|
3461
|
-
}
|
3462
|
-
});
|
3463
|
-
}
|
3464
|
-
|
3465
3572
|
// streams/stream-to-response.ts
|
3466
3573
|
function streamToResponse(res, response, init, data) {
|
3467
3574
|
var _a;
|
@@ -3545,7 +3652,7 @@ export {
|
|
3545
3652
|
OpenAIStream,
|
3546
3653
|
ReplicateStream,
|
3547
3654
|
RetryError2 as RetryError,
|
3548
|
-
StreamData,
|
3655
|
+
StreamData2 as StreamData,
|
3549
3656
|
StreamObjectResult,
|
3550
3657
|
StreamTextResult,
|
3551
3658
|
StreamingTextResponse,
|