ai 6.0.259 → 6.0.261
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/CHANGELOG.md +14 -0
- package/dist/index.js +118 -49
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +118 -49
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- package/dist/test/index.js +2 -2
- package/dist/test/index.js.map +1 -1
- package/dist/test/index.mjs +2 -2
- package/dist/test/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generate-object/stream-object.ts +61 -3
- package/src/generate-text/generate-text.ts +7 -1
- package/src/generate-text/is-tool-execution-allowed-finish-reason.ts +7 -0
- package/src/generate-text/run-tools-transformation.ts +48 -36
- package/src/test/mock-language-model-v2.ts +2 -2
- package/src/test/mock-language-model-v3.ts +2 -2
- package/src/ui/convert-to-model-messages.ts +1 -1
- package/src/util/create-stitchable-stream.ts +32 -15
package/dist/index.mjs
CHANGED
|
@@ -1193,7 +1193,7 @@ import {
|
|
|
1193
1193
|
} from "@ai-sdk/provider-utils";
|
|
1194
1194
|
|
|
1195
1195
|
// src/version.ts
|
|
1196
|
-
var VERSION = true ? "6.0.
|
|
1196
|
+
var VERSION = true ? "6.0.261" : "0.0.0-test";
|
|
1197
1197
|
|
|
1198
1198
|
// src/util/download/download.ts
|
|
1199
1199
|
var download = async ({
|
|
@@ -3118,6 +3118,11 @@ async function isApprovalNeeded({
|
|
|
3118
3118
|
});
|
|
3119
3119
|
}
|
|
3120
3120
|
|
|
3121
|
+
// src/generate-text/is-tool-execution-allowed-finish-reason.ts
|
|
3122
|
+
function isToolExecutionAllowedFinishReason(finishReason) {
|
|
3123
|
+
return finishReason === "stop" || finishReason === "tool-calls";
|
|
3124
|
+
}
|
|
3125
|
+
|
|
3121
3126
|
// src/generate-text/tool-approval-signature.ts
|
|
3122
3127
|
import {
|
|
3123
3128
|
convertBase64ToUint8Array as convertBase64ToUint8Array4,
|
|
@@ -4898,7 +4903,9 @@ async function generateText({
|
|
|
4898
4903
|
clientToolCalls = stepToolCalls.filter(
|
|
4899
4904
|
(toolCall) => !toolCall.providerExecuted
|
|
4900
4905
|
);
|
|
4901
|
-
if (stepToolSet != null
|
|
4906
|
+
if (stepToolSet != null && isToolExecutionAllowedFinishReason(
|
|
4907
|
+
currentModelResponse.finishReason.unified
|
|
4908
|
+
)) {
|
|
4902
4909
|
clientToolOutputs.push(
|
|
4903
4910
|
...await executeTools({
|
|
4904
4911
|
toolCalls: clientToolCalls.filter(
|
|
@@ -6565,32 +6572,33 @@ function createResolvablePromise() {
|
|
|
6565
6572
|
|
|
6566
6573
|
// src/util/create-stitchable-stream.ts
|
|
6567
6574
|
function createStitchableStream() {
|
|
6568
|
-
let
|
|
6575
|
+
let innerStreams = [];
|
|
6569
6576
|
let controller = null;
|
|
6570
6577
|
let isClosed = false;
|
|
6571
6578
|
let waitForNewStream = createResolvablePromise();
|
|
6572
6579
|
const terminate = () => {
|
|
6573
6580
|
isClosed = true;
|
|
6574
6581
|
waitForNewStream.resolve();
|
|
6575
|
-
|
|
6576
|
-
|
|
6582
|
+
innerStreams.forEach(({ reader }) => reader.cancel());
|
|
6583
|
+
innerStreams = [];
|
|
6577
6584
|
controller == null ? void 0 : controller.close();
|
|
6578
6585
|
};
|
|
6579
6586
|
const processPull = async () => {
|
|
6580
|
-
|
|
6587
|
+
var _a22, _b;
|
|
6588
|
+
if (isClosed && innerStreams.length === 0) {
|
|
6581
6589
|
controller == null ? void 0 : controller.close();
|
|
6582
6590
|
return;
|
|
6583
6591
|
}
|
|
6584
|
-
if (
|
|
6592
|
+
if (innerStreams.length === 0) {
|
|
6585
6593
|
waitForNewStream = createResolvablePromise();
|
|
6586
6594
|
await waitForNewStream.promise;
|
|
6587
6595
|
return processPull();
|
|
6588
6596
|
}
|
|
6589
6597
|
try {
|
|
6590
|
-
const { value, done } = await
|
|
6598
|
+
const { value, done } = await innerStreams[0].reader.read();
|
|
6591
6599
|
if (done) {
|
|
6592
|
-
|
|
6593
|
-
if (
|
|
6600
|
+
innerStreams.shift();
|
|
6601
|
+
if (innerStreams.length === 0 && isClosed) {
|
|
6594
6602
|
controller == null ? void 0 : controller.close();
|
|
6595
6603
|
} else {
|
|
6596
6604
|
await processPull();
|
|
@@ -6599,8 +6607,9 @@ function createStitchableStream() {
|
|
|
6599
6607
|
controller == null ? void 0 : controller.enqueue(value);
|
|
6600
6608
|
}
|
|
6601
6609
|
} catch (error) {
|
|
6610
|
+
(_b = (_a22 = innerStreams[0]).onError) == null ? void 0 : _b.call(_a22, error);
|
|
6602
6611
|
controller == null ? void 0 : controller.error(error);
|
|
6603
|
-
|
|
6612
|
+
innerStreams.shift();
|
|
6604
6613
|
terminate();
|
|
6605
6614
|
}
|
|
6606
6615
|
};
|
|
@@ -6611,18 +6620,21 @@ function createStitchableStream() {
|
|
|
6611
6620
|
},
|
|
6612
6621
|
pull: processPull,
|
|
6613
6622
|
async cancel() {
|
|
6614
|
-
for (const reader of
|
|
6623
|
+
for (const { reader } of innerStreams) {
|
|
6615
6624
|
await reader.cancel();
|
|
6616
6625
|
}
|
|
6617
|
-
|
|
6626
|
+
innerStreams = [];
|
|
6618
6627
|
isClosed = true;
|
|
6619
6628
|
}
|
|
6620
6629
|
}),
|
|
6621
|
-
addStream: (innerStream) => {
|
|
6630
|
+
addStream: (innerStream, callbacks) => {
|
|
6622
6631
|
if (isClosed) {
|
|
6623
6632
|
throw new Error("Cannot add inner stream: outer stream is closed");
|
|
6624
6633
|
}
|
|
6625
|
-
|
|
6634
|
+
innerStreams.push({
|
|
6635
|
+
reader: innerStream.getReader(),
|
|
6636
|
+
...callbacks
|
|
6637
|
+
});
|
|
6626
6638
|
waitForNewStream.resolve();
|
|
6627
6639
|
},
|
|
6628
6640
|
/**
|
|
@@ -6632,7 +6644,7 @@ function createStitchableStream() {
|
|
|
6632
6644
|
close: () => {
|
|
6633
6645
|
isClosed = true;
|
|
6634
6646
|
waitForNewStream.resolve();
|
|
6635
|
-
if (
|
|
6647
|
+
if (innerStreams.length === 0) {
|
|
6636
6648
|
controller == null ? void 0 : controller.close();
|
|
6637
6649
|
}
|
|
6638
6650
|
},
|
|
@@ -6696,6 +6708,7 @@ function runToolsTransformation({
|
|
|
6696
6708
|
}
|
|
6697
6709
|
}
|
|
6698
6710
|
const outstandingToolResults = /* @__PURE__ */ new Set();
|
|
6711
|
+
const toolCallsToExecute = [];
|
|
6699
6712
|
const toolCallsByToolCallId = /* @__PURE__ */ new Map();
|
|
6700
6713
|
let canClose = false;
|
|
6701
6714
|
let finishChunk = void 0;
|
|
@@ -6707,6 +6720,36 @@ function runToolsTransformation({
|
|
|
6707
6720
|
closeToolResultsStream();
|
|
6708
6721
|
}
|
|
6709
6722
|
}
|
|
6723
|
+
function executeToolCallAfterFinish(toolCall) {
|
|
6724
|
+
const toolExecutionId = generateId2();
|
|
6725
|
+
outstandingToolResults.add(toolExecutionId);
|
|
6726
|
+
executeToolCall({
|
|
6727
|
+
toolCall,
|
|
6728
|
+
tools,
|
|
6729
|
+
tracer,
|
|
6730
|
+
telemetry,
|
|
6731
|
+
messages,
|
|
6732
|
+
abortSignal,
|
|
6733
|
+
experimental_context,
|
|
6734
|
+
stepNumber,
|
|
6735
|
+
model,
|
|
6736
|
+
onToolCallStart,
|
|
6737
|
+
onToolCallFinish,
|
|
6738
|
+
onPreliminaryToolResult: (result) => {
|
|
6739
|
+
enqueueToolResult(result);
|
|
6740
|
+
}
|
|
6741
|
+
}).then((result) => {
|
|
6742
|
+
enqueueToolResult(result);
|
|
6743
|
+
}).catch((error) => {
|
|
6744
|
+
enqueueToolResult({
|
|
6745
|
+
type: "error",
|
|
6746
|
+
error
|
|
6747
|
+
});
|
|
6748
|
+
}).finally(() => {
|
|
6749
|
+
outstandingToolResults.delete(toolExecutionId);
|
|
6750
|
+
attemptClose();
|
|
6751
|
+
});
|
|
6752
|
+
}
|
|
6710
6753
|
const forwardStream = new TransformStream({
|
|
6711
6754
|
async transform(chunk, controller) {
|
|
6712
6755
|
const chunkType = chunk.type;
|
|
@@ -6747,6 +6790,13 @@ function runToolsTransformation({
|
|
|
6747
6790
|
usage: asLanguageModelUsage(chunk.usage),
|
|
6748
6791
|
providerMetadata: chunk.providerMetadata
|
|
6749
6792
|
};
|
|
6793
|
+
if (isToolExecutionAllowedFinishReason(chunk.finishReason.unified)) {
|
|
6794
|
+
for (const toolCall of toolCallsToExecute.splice(0)) {
|
|
6795
|
+
executeToolCallAfterFinish(toolCall);
|
|
6796
|
+
}
|
|
6797
|
+
} else {
|
|
6798
|
+
toolCallsToExecute.length = 0;
|
|
6799
|
+
}
|
|
6750
6800
|
break;
|
|
6751
6801
|
}
|
|
6752
6802
|
case "tool-approval-request": {
|
|
@@ -6778,7 +6828,7 @@ function runToolsTransformation({
|
|
|
6778
6828
|
messages
|
|
6779
6829
|
});
|
|
6780
6830
|
toolCallsByToolCallId.set(toolCall.toolCallId, toolCall);
|
|
6781
|
-
controller.enqueue(toolCall);
|
|
6831
|
+
controller.enqueue({ ...toolCall });
|
|
6782
6832
|
if (toolCall.invalid) {
|
|
6783
6833
|
if (!toolCall.providerExecuted) {
|
|
6784
6834
|
enqueueToolResult({
|
|
@@ -6830,34 +6880,7 @@ function runToolsTransformation({
|
|
|
6830
6880
|
break;
|
|
6831
6881
|
}
|
|
6832
6882
|
if (tool2.execute != null && toolCall.providerExecuted !== true) {
|
|
6833
|
-
|
|
6834
|
-
outstandingToolResults.add(toolExecutionId);
|
|
6835
|
-
executeToolCall({
|
|
6836
|
-
toolCall,
|
|
6837
|
-
tools,
|
|
6838
|
-
tracer,
|
|
6839
|
-
telemetry,
|
|
6840
|
-
messages,
|
|
6841
|
-
abortSignal,
|
|
6842
|
-
experimental_context,
|
|
6843
|
-
stepNumber,
|
|
6844
|
-
model,
|
|
6845
|
-
onToolCallStart,
|
|
6846
|
-
onToolCallFinish,
|
|
6847
|
-
onPreliminaryToolResult: (result) => {
|
|
6848
|
-
enqueueToolResult(result);
|
|
6849
|
-
}
|
|
6850
|
-
}).then((result) => {
|
|
6851
|
-
enqueueToolResult(result);
|
|
6852
|
-
}).catch((error) => {
|
|
6853
|
-
enqueueToolResult({
|
|
6854
|
-
type: "error",
|
|
6855
|
-
error
|
|
6856
|
-
});
|
|
6857
|
-
}).finally(() => {
|
|
6858
|
-
outstandingToolResults.delete(toolExecutionId);
|
|
6859
|
-
attemptClose();
|
|
6860
|
-
});
|
|
6883
|
+
toolCallsToExecute.push(toolCall);
|
|
6861
6884
|
}
|
|
6862
6885
|
} catch (error) {
|
|
6863
6886
|
enqueueToolResult({ type: "error", error });
|
|
@@ -9038,7 +9061,7 @@ async function convertToModelMessages(messages, options) {
|
|
|
9038
9061
|
messages = messages.map((message) => ({
|
|
9039
9062
|
...message,
|
|
9040
9063
|
parts: message.parts.filter(
|
|
9041
|
-
(part) => !isToolUIPart(part) || part.state === "approval-responded" || part.state === "output-available" || part.state === "output-error" || part.state === "output-denied"
|
|
9064
|
+
(part) => !isToolUIPart(part) || part.state === "approval-responded" || part.state === "output-available" && part.preliminary !== true || part.state === "output-error" || part.state === "output-denied"
|
|
9042
9065
|
)
|
|
9043
9066
|
}));
|
|
9044
9067
|
}
|
|
@@ -11240,6 +11263,12 @@ function simulateReadableStream({
|
|
|
11240
11263
|
|
|
11241
11264
|
// src/generate-object/stream-object.ts
|
|
11242
11265
|
var originalGenerateId4 = createIdGenerator4({ prefix: "aiobj", size: 24 });
|
|
11266
|
+
async function markPromiseAsHandled(promise) {
|
|
11267
|
+
try {
|
|
11268
|
+
await promise;
|
|
11269
|
+
} catch (e) {
|
|
11270
|
+
}
|
|
11271
|
+
}
|
|
11243
11272
|
function streamObject(options) {
|
|
11244
11273
|
const {
|
|
11245
11274
|
model,
|
|
@@ -11474,6 +11503,7 @@ var DefaultStreamObjectResult = class {
|
|
|
11474
11503
|
let providerMetadata;
|
|
11475
11504
|
let object2;
|
|
11476
11505
|
let error;
|
|
11506
|
+
let terminalError;
|
|
11477
11507
|
let accumulatedText = "";
|
|
11478
11508
|
let textDelta = "";
|
|
11479
11509
|
let fullResponse = {
|
|
@@ -11544,16 +11574,27 @@ var DefaultStreamObjectResult = class {
|
|
|
11544
11574
|
};
|
|
11545
11575
|
break;
|
|
11546
11576
|
}
|
|
11577
|
+
case "error": {
|
|
11578
|
+
if (terminalError === void 0) {
|
|
11579
|
+
const wrappedError = wrapGatewayError(chunk.error);
|
|
11580
|
+
terminalError = { error: wrappedError };
|
|
11581
|
+
error = wrappedError;
|
|
11582
|
+
finishReason = "error";
|
|
11583
|
+
self.rejectResultPromises(wrappedError);
|
|
11584
|
+
}
|
|
11585
|
+
controller.enqueue(chunk);
|
|
11586
|
+
break;
|
|
11587
|
+
}
|
|
11547
11588
|
case "finish": {
|
|
11548
11589
|
if (textDelta !== "") {
|
|
11549
11590
|
controller.enqueue({ type: "text-delta", textDelta });
|
|
11550
11591
|
}
|
|
11551
|
-
finishReason = chunk.finishReason.unified;
|
|
11592
|
+
finishReason = terminalError === void 0 ? chunk.finishReason.unified : "error";
|
|
11552
11593
|
usage = asLanguageModelUsage(chunk.usage);
|
|
11553
11594
|
providerMetadata = chunk.providerMetadata;
|
|
11554
11595
|
controller.enqueue({
|
|
11555
11596
|
...chunk,
|
|
11556
|
-
finishReason
|
|
11597
|
+
finishReason,
|
|
11557
11598
|
usage,
|
|
11558
11599
|
response: fullResponse
|
|
11559
11600
|
});
|
|
@@ -11562,6 +11603,9 @@ var DefaultStreamObjectResult = class {
|
|
|
11562
11603
|
provider: model.provider,
|
|
11563
11604
|
model: model.modelId
|
|
11564
11605
|
});
|
|
11606
|
+
if (terminalError !== void 0) {
|
|
11607
|
+
break;
|
|
11608
|
+
}
|
|
11565
11609
|
self._usage.resolve(usage);
|
|
11566
11610
|
self._providerMetadata.resolve(providerMetadata);
|
|
11567
11611
|
self._warnings.resolve(warnings);
|
|
@@ -11664,9 +11708,16 @@ var DefaultStreamObjectResult = class {
|
|
|
11664
11708
|
}
|
|
11665
11709
|
})
|
|
11666
11710
|
);
|
|
11667
|
-
stitchableStream.addStream(transformedStream
|
|
11711
|
+
stitchableStream.addStream(transformedStream, {
|
|
11712
|
+
onError(error2) {
|
|
11713
|
+
const wrappedError = wrapGatewayError(error2);
|
|
11714
|
+
self.rejectResultPromises(wrappedError);
|
|
11715
|
+
void onError({ error: wrappedError });
|
|
11716
|
+
}
|
|
11717
|
+
});
|
|
11668
11718
|
}
|
|
11669
11719
|
}).catch((error) => {
|
|
11720
|
+
self.rejectResultPromises(error);
|
|
11670
11721
|
stitchableStream.addStream(
|
|
11671
11722
|
new ReadableStream({
|
|
11672
11723
|
start(controller) {
|
|
@@ -11680,6 +11731,24 @@ var DefaultStreamObjectResult = class {
|
|
|
11680
11731
|
});
|
|
11681
11732
|
this.outputStrategy = outputStrategy;
|
|
11682
11733
|
}
|
|
11734
|
+
rejectResultPromises(error) {
|
|
11735
|
+
this.rejectResultPromise({ delayedPromise: this._object, error });
|
|
11736
|
+
this.rejectResultPromise({ delayedPromise: this._usage, error });
|
|
11737
|
+
this.rejectResultPromise({ delayedPromise: this._providerMetadata, error });
|
|
11738
|
+
this.rejectResultPromise({ delayedPromise: this._warnings, error });
|
|
11739
|
+
this.rejectResultPromise({ delayedPromise: this._request, error });
|
|
11740
|
+
this.rejectResultPromise({ delayedPromise: this._response, error });
|
|
11741
|
+
this.rejectResultPromise({ delayedPromise: this._finishReason, error });
|
|
11742
|
+
}
|
|
11743
|
+
rejectResultPromise({
|
|
11744
|
+
delayedPromise,
|
|
11745
|
+
error
|
|
11746
|
+
}) {
|
|
11747
|
+
if (delayedPromise.isPending()) {
|
|
11748
|
+
delayedPromise.reject(error);
|
|
11749
|
+
markPromiseAsHandled(delayedPromise.promise);
|
|
11750
|
+
}
|
|
11751
|
+
}
|
|
11683
11752
|
get object() {
|
|
11684
11753
|
return this._object.promise;
|
|
11685
11754
|
}
|