ai 7.0.89 → 7.0.91
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 +19 -0
- package/dist/index.d.ts +42 -4
- package/dist/index.js +401 -81
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +3 -1
- package/dist/internal/index.js +9 -7
- package/dist/internal/index.js.map +1 -1
- package/docs/02-foundations/02-providers-and-models.mdx +6 -0
- package/docs/03-agents/06-policy-tool-approvals.mdx +2 -1
- package/docs/03-agents/06-tool-approvals.mdx +7 -4
- package/docs/03-agents/07-workflow-agent.mdx +15 -0
- package/docs/03-ai-sdk-core/50-error-handling.mdx +83 -6
- package/docs/06-advanced/11-secure-url-fetching.mdx +7 -1
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +9 -2
- package/package.json +11 -11
- package/src/generate-text/execute-tools-from-stream.ts +18 -4
- package/src/generate-text/index.ts +2 -0
- package/src/generate-text/invoke-tool-callbacks-from-stream.ts +14 -2
- package/src/generate-text/stream-retry-attempt-boundary.ts +29 -0
- package/src/generate-text/stream-text.ts +635 -156
- package/src/util/prepare-retries.ts +9 -5
package/dist/index.js
CHANGED
|
@@ -1176,7 +1176,7 @@ import {
|
|
|
1176
1176
|
} from "@ai-sdk/provider-utils";
|
|
1177
1177
|
|
|
1178
1178
|
// src/version.ts
|
|
1179
|
-
var VERSION = true ? "7.0.
|
|
1179
|
+
var VERSION = true ? "7.0.91" : "0.0.0-test";
|
|
1180
1180
|
|
|
1181
1181
|
// src/util/download/download.ts
|
|
1182
1182
|
var download = async ({
|
|
@@ -2828,25 +2828,27 @@ var retryWithExponentialBackoffRespectingRetryHeaders = ({
|
|
|
2828
2828
|
// src/util/prepare-retries.ts
|
|
2829
2829
|
function prepareRetries({
|
|
2830
2830
|
maxRetries,
|
|
2831
|
-
abortSignal
|
|
2831
|
+
abortSignal,
|
|
2832
|
+
parameter = "maxRetries",
|
|
2833
|
+
defaultMaxRetries = 2
|
|
2832
2834
|
}) {
|
|
2833
2835
|
if (maxRetries != null) {
|
|
2834
2836
|
if (!Number.isInteger(maxRetries)) {
|
|
2835
2837
|
throw new InvalidArgumentError({
|
|
2836
|
-
parameter
|
|
2838
|
+
parameter,
|
|
2837
2839
|
value: maxRetries,
|
|
2838
|
-
message:
|
|
2840
|
+
message: `${parameter} must be an integer`
|
|
2839
2841
|
});
|
|
2840
2842
|
}
|
|
2841
2843
|
if (maxRetries < 0) {
|
|
2842
2844
|
throw new InvalidArgumentError({
|
|
2843
|
-
parameter
|
|
2845
|
+
parameter,
|
|
2844
2846
|
value: maxRetries,
|
|
2845
|
-
message:
|
|
2847
|
+
message: `${parameter} must be >= 0`
|
|
2846
2848
|
});
|
|
2847
2849
|
}
|
|
2848
2850
|
}
|
|
2849
|
-
const maxRetriesResult = maxRetries != null ? maxRetries :
|
|
2851
|
+
const maxRetriesResult = maxRetries != null ? maxRetries : defaultMaxRetries;
|
|
2850
2852
|
return {
|
|
2851
2853
|
maxRetries: maxRetriesResult,
|
|
2852
2854
|
retry: retryWithExponentialBackoffRespectingRetryHeaders({
|
|
@@ -8273,6 +8275,20 @@ function createStitchableStream() {
|
|
|
8273
8275
|
};
|
|
8274
8276
|
}
|
|
8275
8277
|
|
|
8278
|
+
// src/generate-text/stream-retry-attempt-boundary.ts
|
|
8279
|
+
var streamRetryAttemptBoundarySymbol = /* @__PURE__ */ Symbol("streamRetryAttemptBoundary");
|
|
8280
|
+
function createStreamRetryAttemptBoundaryPart({
|
|
8281
|
+
warnings
|
|
8282
|
+
}) {
|
|
8283
|
+
return {
|
|
8284
|
+
[streamRetryAttemptBoundarySymbol]: true,
|
|
8285
|
+
warnings
|
|
8286
|
+
};
|
|
8287
|
+
}
|
|
8288
|
+
function isStreamRetryAttemptBoundaryPart(part) {
|
|
8289
|
+
return typeof part === "object" && part != null && streamRetryAttemptBoundarySymbol in part;
|
|
8290
|
+
}
|
|
8291
|
+
|
|
8276
8292
|
// src/generate-text/execute-tools-from-stream.ts
|
|
8277
8293
|
function executeToolsFromStream({
|
|
8278
8294
|
stream,
|
|
@@ -8297,6 +8313,10 @@ function executeToolsFromStream({
|
|
|
8297
8313
|
new TransformStream({
|
|
8298
8314
|
async transform(chunk, controller) {
|
|
8299
8315
|
controller.enqueue(chunk);
|
|
8316
|
+
if (isStreamRetryAttemptBoundaryPart(chunk)) {
|
|
8317
|
+
toolCallsToExecute.length = 0;
|
|
8318
|
+
return;
|
|
8319
|
+
}
|
|
8300
8320
|
const chunkType = chunk.type;
|
|
8301
8321
|
switch (chunkType) {
|
|
8302
8322
|
case "tool-call": {
|
|
@@ -8444,6 +8464,9 @@ function invokeToolCallbacksFromStream({
|
|
|
8444
8464
|
new TransformStream({
|
|
8445
8465
|
async transform(chunk, controller) {
|
|
8446
8466
|
controller.enqueue(chunk);
|
|
8467
|
+
if (isStreamRetryAttemptBoundaryPart(chunk)) {
|
|
8468
|
+
return;
|
|
8469
|
+
}
|
|
8447
8470
|
switch (chunk.type) {
|
|
8448
8471
|
case "tool-input-start": {
|
|
8449
8472
|
ongoingToolCallToolNames[chunk.id] = chunk.toolName;
|
|
@@ -9109,6 +9132,7 @@ function streamText({
|
|
|
9109
9132
|
messages,
|
|
9110
9133
|
allowSystemInMessages,
|
|
9111
9134
|
maxRetries,
|
|
9135
|
+
streamRetries,
|
|
9112
9136
|
abortSignal,
|
|
9113
9137
|
timeout,
|
|
9114
9138
|
headers,
|
|
@@ -9131,9 +9155,7 @@ function streamText({
|
|
|
9131
9155
|
experimental_download: download2,
|
|
9132
9156
|
includeRawChunks,
|
|
9133
9157
|
onChunk,
|
|
9134
|
-
onError
|
|
9135
|
-
console.error(error);
|
|
9136
|
-
},
|
|
9158
|
+
onError: onErrorArg,
|
|
9137
9159
|
onFinish,
|
|
9138
9160
|
onEnd = onFinish,
|
|
9139
9161
|
onAbort,
|
|
@@ -9170,6 +9192,9 @@ function streamText({
|
|
|
9170
9192
|
const stepAbortController = stepTimeoutMs != null ? new AbortController() : void 0;
|
|
9171
9193
|
const firstChunkAbortController = firstChunkTimeoutMs != null ? new AbortController() : void 0;
|
|
9172
9194
|
const chunkAbortController = chunkTimeoutMs != null ? new AbortController() : void 0;
|
|
9195
|
+
const onError = onErrorArg != null ? onErrorArg : (({ error }) => {
|
|
9196
|
+
console.error(error);
|
|
9197
|
+
});
|
|
9173
9198
|
const resolvedOnStart = onStart != null ? onStart : experimental_onStart;
|
|
9174
9199
|
const resolvedOnStepStart = onStepStart != null ? onStepStart : experimental_onStepStart;
|
|
9175
9200
|
const resolvedOnLanguageModelCallStart = onLanguageModelCallStart != null ? onLanguageModelCallStart : experimental_onLanguageModelCallStart;
|
|
@@ -9183,6 +9208,7 @@ function streamText({
|
|
|
9183
9208
|
headers,
|
|
9184
9209
|
settings,
|
|
9185
9210
|
maxRetries,
|
|
9211
|
+
streamRetries,
|
|
9186
9212
|
abortSignal: mergeAbortSignals(
|
|
9187
9213
|
abortSignal,
|
|
9188
9214
|
totalTimeoutMs,
|
|
@@ -9221,6 +9247,7 @@ function streamText({
|
|
|
9221
9247
|
timeout,
|
|
9222
9248
|
onChunk,
|
|
9223
9249
|
onError,
|
|
9250
|
+
canRetryStreamViaOnError: streamRetries !== void 0 && onErrorArg != null,
|
|
9224
9251
|
onEnd,
|
|
9225
9252
|
onAbort,
|
|
9226
9253
|
onStepFinish: resolvedOnStepEnd,
|
|
@@ -9242,6 +9269,10 @@ function streamText({
|
|
|
9242
9269
|
}
|
|
9243
9270
|
});
|
|
9244
9271
|
}
|
|
9272
|
+
var streamRetryBoundarySymbol = /* @__PURE__ */ Symbol("streamRetryBoundary");
|
|
9273
|
+
function isStreamRetryBoundaryPart(part) {
|
|
9274
|
+
return streamRetryBoundarySymbol in part;
|
|
9275
|
+
}
|
|
9245
9276
|
async function markPromiseAsHandled(promise) {
|
|
9246
9277
|
try {
|
|
9247
9278
|
await promise;
|
|
@@ -9254,53 +9285,89 @@ function createOutputTransformStream(output) {
|
|
|
9254
9285
|
let textChunk = "";
|
|
9255
9286
|
let textProviderMetadata = void 0;
|
|
9256
9287
|
let lastPublishedValue = "";
|
|
9288
|
+
function resetAttemptState() {
|
|
9289
|
+
firstTextChunkId = void 0;
|
|
9290
|
+
text2 = "";
|
|
9291
|
+
textChunk = "";
|
|
9292
|
+
textProviderMetadata = void 0;
|
|
9293
|
+
lastPublishedValue = "";
|
|
9294
|
+
}
|
|
9295
|
+
function enqueueChunk({
|
|
9296
|
+
controller,
|
|
9297
|
+
chunk
|
|
9298
|
+
}) {
|
|
9299
|
+
controller.enqueue(chunk);
|
|
9300
|
+
}
|
|
9257
9301
|
function publishTextChunk({
|
|
9258
9302
|
controller,
|
|
9259
9303
|
partialOutput = void 0
|
|
9260
9304
|
}) {
|
|
9261
|
-
|
|
9262
|
-
|
|
9263
|
-
|
|
9264
|
-
|
|
9265
|
-
|
|
9266
|
-
|
|
9267
|
-
|
|
9268
|
-
|
|
9305
|
+
enqueueChunk({
|
|
9306
|
+
controller,
|
|
9307
|
+
chunk: {
|
|
9308
|
+
part: {
|
|
9309
|
+
type: "text-delta",
|
|
9310
|
+
id: firstTextChunkId,
|
|
9311
|
+
text: textChunk,
|
|
9312
|
+
providerMetadata: textProviderMetadata
|
|
9313
|
+
},
|
|
9314
|
+
partialOutput
|
|
9315
|
+
}
|
|
9269
9316
|
});
|
|
9270
9317
|
textChunk = "";
|
|
9271
9318
|
}
|
|
9272
9319
|
return new TransformStream({
|
|
9273
9320
|
async transform(chunk, controller) {
|
|
9274
9321
|
var _a25;
|
|
9322
|
+
if (isStreamRetryBoundaryPart(chunk)) {
|
|
9323
|
+
resetAttemptState();
|
|
9324
|
+
controller.enqueue(chunk);
|
|
9325
|
+
return;
|
|
9326
|
+
}
|
|
9275
9327
|
if (chunk.type === "finish-step" && textChunk.length > 0) {
|
|
9276
9328
|
publishTextChunk({ controller });
|
|
9277
9329
|
}
|
|
9278
9330
|
if (chunk.type !== "text-delta" && chunk.type !== "text-start" && chunk.type !== "text-end") {
|
|
9279
|
-
|
|
9331
|
+
enqueueChunk({
|
|
9332
|
+
controller,
|
|
9333
|
+
chunk: { part: chunk, partialOutput: void 0 }
|
|
9334
|
+
});
|
|
9280
9335
|
return;
|
|
9281
9336
|
}
|
|
9282
9337
|
if (firstTextChunkId == null) {
|
|
9283
9338
|
firstTextChunkId = chunk.id;
|
|
9284
9339
|
} else if (chunk.id !== firstTextChunkId) {
|
|
9285
|
-
|
|
9340
|
+
enqueueChunk({
|
|
9341
|
+
controller,
|
|
9342
|
+
chunk: { part: chunk, partialOutput: void 0 }
|
|
9343
|
+
});
|
|
9286
9344
|
return;
|
|
9287
9345
|
}
|
|
9288
9346
|
if (chunk.type === "text-start") {
|
|
9289
|
-
|
|
9347
|
+
enqueueChunk({
|
|
9348
|
+
controller,
|
|
9349
|
+
chunk: { part: chunk, partialOutput: void 0 }
|
|
9350
|
+
});
|
|
9290
9351
|
return;
|
|
9291
9352
|
}
|
|
9292
9353
|
if (chunk.type === "text-end") {
|
|
9293
9354
|
if (textChunk.length > 0) {
|
|
9294
9355
|
publishTextChunk({ controller });
|
|
9295
9356
|
}
|
|
9296
|
-
|
|
9357
|
+
enqueueChunk({
|
|
9358
|
+
controller,
|
|
9359
|
+
chunk: { part: chunk, partialOutput: void 0 }
|
|
9360
|
+
});
|
|
9297
9361
|
return;
|
|
9298
9362
|
}
|
|
9299
9363
|
text2 += chunk.text;
|
|
9300
9364
|
textChunk += chunk.text;
|
|
9301
9365
|
textProviderMetadata = (_a25 = chunk.providerMetadata) != null ? _a25 : textProviderMetadata;
|
|
9302
9366
|
if (chunk.text.length === 0 && chunk.providerMetadata != null) {
|
|
9303
|
-
|
|
9367
|
+
enqueueChunk({
|
|
9368
|
+
controller,
|
|
9369
|
+
chunk: { part: chunk, partialOutput: void 0 }
|
|
9370
|
+
});
|
|
9304
9371
|
return;
|
|
9305
9372
|
}
|
|
9306
9373
|
const result = await output.parsePartialOutput({ text: text2 });
|
|
@@ -9314,6 +9381,75 @@ function createOutputTransformStream(output) {
|
|
|
9314
9381
|
}
|
|
9315
9382
|
});
|
|
9316
9383
|
}
|
|
9384
|
+
function applyStreamTextTransforms({
|
|
9385
|
+
stream,
|
|
9386
|
+
transforms,
|
|
9387
|
+
tools,
|
|
9388
|
+
stopStream
|
|
9389
|
+
}) {
|
|
9390
|
+
const sourceReader = stream.getReader();
|
|
9391
|
+
let sourceDone = false;
|
|
9392
|
+
let pendingBoundary;
|
|
9393
|
+
let transformedSegmentReader;
|
|
9394
|
+
const createTransformedSegmentReader = () => {
|
|
9395
|
+
let segment = new ReadableStream(
|
|
9396
|
+
{
|
|
9397
|
+
async pull(controller) {
|
|
9398
|
+
const { done, value } = await sourceReader.read();
|
|
9399
|
+
if (done) {
|
|
9400
|
+
sourceDone = true;
|
|
9401
|
+
controller.close();
|
|
9402
|
+
return;
|
|
9403
|
+
}
|
|
9404
|
+
if (isStreamRetryBoundaryPart(value)) {
|
|
9405
|
+
pendingBoundary = value;
|
|
9406
|
+
controller.close();
|
|
9407
|
+
return;
|
|
9408
|
+
}
|
|
9409
|
+
controller.enqueue(value);
|
|
9410
|
+
},
|
|
9411
|
+
cancel(reason) {
|
|
9412
|
+
return sourceReader.cancel(reason);
|
|
9413
|
+
}
|
|
9414
|
+
},
|
|
9415
|
+
// Do not prefetch the next source chunk. `stopStream` is invoked from a
|
|
9416
|
+
// user transform and must close the gate before another chunk enters it.
|
|
9417
|
+
{ highWaterMark: 0 }
|
|
9418
|
+
);
|
|
9419
|
+
for (const transform of transforms) {
|
|
9420
|
+
segment = segment.pipeThrough(
|
|
9421
|
+
transform({
|
|
9422
|
+
tools,
|
|
9423
|
+
stopStream
|
|
9424
|
+
})
|
|
9425
|
+
);
|
|
9426
|
+
}
|
|
9427
|
+
return segment.getReader();
|
|
9428
|
+
};
|
|
9429
|
+
return new ReadableStream({
|
|
9430
|
+
async pull(controller) {
|
|
9431
|
+
transformedSegmentReader != null ? transformedSegmentReader : transformedSegmentReader = createTransformedSegmentReader();
|
|
9432
|
+
const { done, value } = await transformedSegmentReader.read();
|
|
9433
|
+
if (!done) {
|
|
9434
|
+
controller.enqueue(value);
|
|
9435
|
+
return;
|
|
9436
|
+
}
|
|
9437
|
+
transformedSegmentReader = void 0;
|
|
9438
|
+
if (pendingBoundary != null) {
|
|
9439
|
+
controller.enqueue(pendingBoundary);
|
|
9440
|
+
pendingBoundary = void 0;
|
|
9441
|
+
return;
|
|
9442
|
+
}
|
|
9443
|
+
if (sourceDone) {
|
|
9444
|
+
controller.close();
|
|
9445
|
+
}
|
|
9446
|
+
},
|
|
9447
|
+
async cancel(reason) {
|
|
9448
|
+
await (transformedSegmentReader == null ? void 0 : transformedSegmentReader.cancel(reason));
|
|
9449
|
+
await sourceReader.cancel(reason);
|
|
9450
|
+
}
|
|
9451
|
+
});
|
|
9452
|
+
}
|
|
9317
9453
|
var DefaultStreamTextResult = class {
|
|
9318
9454
|
constructor({
|
|
9319
9455
|
model,
|
|
@@ -9321,6 +9457,7 @@ var DefaultStreamTextResult = class {
|
|
|
9321
9457
|
headers,
|
|
9322
9458
|
settings,
|
|
9323
9459
|
maxRetries: maxRetriesArg,
|
|
9460
|
+
streamRetries: streamRetriesArg,
|
|
9324
9461
|
abortSignal,
|
|
9325
9462
|
stepTimeoutMs,
|
|
9326
9463
|
stepAbortController,
|
|
@@ -9354,6 +9491,7 @@ var DefaultStreamTextResult = class {
|
|
|
9354
9491
|
timeout,
|
|
9355
9492
|
onChunk,
|
|
9356
9493
|
onError,
|
|
9494
|
+
canRetryStreamViaOnError,
|
|
9357
9495
|
onEnd,
|
|
9358
9496
|
onAbort,
|
|
9359
9497
|
onStepFinish,
|
|
@@ -9419,24 +9557,44 @@ var DefaultStreamTextResult = class {
|
|
|
9419
9557
|
let activeTextContent = createIdMap();
|
|
9420
9558
|
let activeReasoningContent = createIdMap();
|
|
9421
9559
|
let recordedNoOutputError;
|
|
9560
|
+
const errorsHandledForStreamRetry = /* @__PURE__ */ new Set();
|
|
9422
9561
|
const eventProcessor = new TransformStream({
|
|
9423
9562
|
async transform(chunk, controller) {
|
|
9424
|
-
var _a25, _b25, _c, _d;
|
|
9425
|
-
|
|
9563
|
+
var _a25, _b25, _c, _d, _e;
|
|
9564
|
+
if (isStreamRetryBoundaryPart(chunk)) {
|
|
9565
|
+
const retryBoundary = chunk[streamRetryBoundarySymbol];
|
|
9566
|
+
recordedContent = [];
|
|
9567
|
+
activeReasoningContent = createIdMap();
|
|
9568
|
+
activeTextContent = createIdMap();
|
|
9569
|
+
recordedRequest = retryBoundary.request;
|
|
9570
|
+
recordedRequestMessages = (_a25 = retryBoundary.request.messages) != null ? _a25 : [];
|
|
9571
|
+
recordedWarnings = retryBoundary.warnings;
|
|
9572
|
+
return;
|
|
9573
|
+
}
|
|
9426
9574
|
const { part } = chunk;
|
|
9427
|
-
|
|
9428
|
-
|
|
9429
|
-
|
|
9430
|
-
|
|
9575
|
+
controller.enqueue(chunk);
|
|
9576
|
+
const callbacksHandledForStreamRetry = part.type === "error" && errorsHandledForStreamRetry.has(part.error);
|
|
9577
|
+
if (!callbacksHandledForStreamRetry) {
|
|
9578
|
+
await notify({
|
|
9579
|
+
event: { chunk: part },
|
|
9580
|
+
callbacks: onChunk
|
|
9581
|
+
});
|
|
9582
|
+
}
|
|
9431
9583
|
if (part.type === "error") {
|
|
9432
9584
|
const error = wrapGatewayError(part.error);
|
|
9433
9585
|
if (NoOutputGeneratedError.isInstance(error)) {
|
|
9434
9586
|
recordedNoOutputError = error;
|
|
9435
9587
|
}
|
|
9436
|
-
|
|
9437
|
-
|
|
9438
|
-
|
|
9439
|
-
|
|
9588
|
+
if (callbacksHandledForStreamRetry) {
|
|
9589
|
+
errorsHandledForStreamRetry.delete(part.error);
|
|
9590
|
+
} else {
|
|
9591
|
+
await notify({
|
|
9592
|
+
event: { error },
|
|
9593
|
+
callbacks: async (event) => {
|
|
9594
|
+
await onError(event);
|
|
9595
|
+
}
|
|
9596
|
+
});
|
|
9597
|
+
}
|
|
9440
9598
|
}
|
|
9441
9599
|
if (part.type === "custom" || part.type === "source" || part.type === "tool-call" || part.type === "tool-approval-request" || part.type === "tool-approval-response" || part.type === "tool-error") {
|
|
9442
9600
|
recordedContent.push(part);
|
|
@@ -9462,7 +9620,7 @@ var DefaultStreamTextResult = class {
|
|
|
9462
9620
|
return;
|
|
9463
9621
|
}
|
|
9464
9622
|
activeText.text += part.text;
|
|
9465
|
-
activeText.providerMetadata = (
|
|
9623
|
+
activeText.providerMetadata = (_b25 = part.providerMetadata) != null ? _b25 : activeText.providerMetadata;
|
|
9466
9624
|
}
|
|
9467
9625
|
if (part.type === "text-end") {
|
|
9468
9626
|
const activeText = activeTextContent[part.id];
|
|
@@ -9476,7 +9634,7 @@ var DefaultStreamTextResult = class {
|
|
|
9476
9634
|
});
|
|
9477
9635
|
return;
|
|
9478
9636
|
}
|
|
9479
|
-
activeText.providerMetadata = (
|
|
9637
|
+
activeText.providerMetadata = (_c = part.providerMetadata) != null ? _c : activeText.providerMetadata;
|
|
9480
9638
|
delete activeTextContent[part.id];
|
|
9481
9639
|
}
|
|
9482
9640
|
if (part.type === "reasoning-start") {
|
|
@@ -9500,7 +9658,7 @@ var DefaultStreamTextResult = class {
|
|
|
9500
9658
|
return;
|
|
9501
9659
|
}
|
|
9502
9660
|
activeReasoning.text += part.text;
|
|
9503
|
-
activeReasoning.providerMetadata = (
|
|
9661
|
+
activeReasoning.providerMetadata = (_d = part.providerMetadata) != null ? _d : activeReasoning.providerMetadata;
|
|
9504
9662
|
}
|
|
9505
9663
|
if (part.type === "reasoning-end") {
|
|
9506
9664
|
const activeReasoning = activeReasoningContent[part.id];
|
|
@@ -9514,7 +9672,7 @@ var DefaultStreamTextResult = class {
|
|
|
9514
9672
|
});
|
|
9515
9673
|
return;
|
|
9516
9674
|
}
|
|
9517
|
-
activeReasoning.providerMetadata = (
|
|
9675
|
+
activeReasoning.providerMetadata = (_e = part.providerMetadata) != null ? _e : activeReasoning.providerMetadata;
|
|
9518
9676
|
delete activeReasoningContent[part.id];
|
|
9519
9677
|
}
|
|
9520
9678
|
if (part.type === "file" || part.type === "reasoning-file") {
|
|
@@ -9736,22 +9894,26 @@ var DefaultStreamTextResult = class {
|
|
|
9736
9894
|
}
|
|
9737
9895
|
})
|
|
9738
9896
|
);
|
|
9739
|
-
|
|
9740
|
-
stream
|
|
9741
|
-
|
|
9742
|
-
|
|
9743
|
-
|
|
9744
|
-
|
|
9745
|
-
|
|
9746
|
-
|
|
9747
|
-
|
|
9748
|
-
);
|
|
9749
|
-
}
|
|
9897
|
+
stream = applyStreamTextTransforms({
|
|
9898
|
+
stream,
|
|
9899
|
+
transforms,
|
|
9900
|
+
tools,
|
|
9901
|
+
stopStream() {
|
|
9902
|
+
stitchableStream.terminate();
|
|
9903
|
+
isRunning = false;
|
|
9904
|
+
}
|
|
9905
|
+
});
|
|
9750
9906
|
this.baseStream = stream.pipeThrough(createOutputTransformStream(output != null ? output : text())).pipeThrough(eventProcessor);
|
|
9751
9907
|
const { maxRetries } = prepareRetries({
|
|
9752
9908
|
maxRetries: maxRetriesArg,
|
|
9753
9909
|
abortSignal
|
|
9754
9910
|
});
|
|
9911
|
+
const { maxRetries: streamRetries } = prepareRetries({
|
|
9912
|
+
maxRetries: streamRetriesArg,
|
|
9913
|
+
abortSignal,
|
|
9914
|
+
parameter: "streamRetries",
|
|
9915
|
+
defaultMaxRetries: 0
|
|
9916
|
+
});
|
|
9755
9917
|
const callSettings = prepareLanguageModelCallOptions(settings);
|
|
9756
9918
|
const self = this;
|
|
9757
9919
|
const callId = generateCallId();
|
|
@@ -10090,11 +10252,8 @@ var DefaultStreamTextResult = class {
|
|
|
10090
10252
|
});
|
|
10091
10253
|
const stepStartTimestampMs = now2();
|
|
10092
10254
|
const { retry } = prepareRetries({ maxRetries, abortSignal });
|
|
10093
|
-
|
|
10094
|
-
|
|
10095
|
-
request,
|
|
10096
|
-
response
|
|
10097
|
-
} = await runInStepTracingChannelContext(
|
|
10255
|
+
let hasNotifiedStepStart = false;
|
|
10256
|
+
const callLanguageModel = () => runInStepTracingChannelContext(
|
|
10098
10257
|
() => retry(
|
|
10099
10258
|
async () => {
|
|
10100
10259
|
var _a27, _b26;
|
|
@@ -10128,6 +10287,10 @@ var DefaultStreamTextResult = class {
|
|
|
10128
10287
|
),
|
|
10129
10288
|
onStart: async ({ promptMessages }) => {
|
|
10130
10289
|
var _a28, _b27;
|
|
10290
|
+
if (hasNotifiedStepStart) {
|
|
10291
|
+
return;
|
|
10292
|
+
}
|
|
10293
|
+
hasNotifiedStepStart = true;
|
|
10131
10294
|
await notify({
|
|
10132
10295
|
event: {
|
|
10133
10296
|
callId,
|
|
@@ -10160,6 +10323,135 @@ var DefaultStreamTextResult = class {
|
|
|
10160
10323
|
}
|
|
10161
10324
|
)
|
|
10162
10325
|
);
|
|
10326
|
+
const initialLanguageModelCall = await callLanguageModel();
|
|
10327
|
+
let request = initialLanguageModelCall.request;
|
|
10328
|
+
let response = initialLanguageModelCall.response;
|
|
10329
|
+
let languageModelStreamReader = initialLanguageModelCall.stream.getReader();
|
|
10330
|
+
let automaticStreamRetryCount = 0;
|
|
10331
|
+
let callbackStreamRetryCount = 0;
|
|
10332
|
+
let bufferedAttemptParts = [];
|
|
10333
|
+
const outputChunksHandledBeforeBuffering = /* @__PURE__ */ new WeakSet();
|
|
10334
|
+
const openTextParts = /* @__PURE__ */ new Set();
|
|
10335
|
+
const openReasoningParts = /* @__PURE__ */ new Set();
|
|
10336
|
+
let enqueueStreamRetryAttemptBoundary = false;
|
|
10337
|
+
const shouldBufferToolParts = streamRetries > 0 || canRetryStreamViaOnError;
|
|
10338
|
+
const languageModelStream = new ReadableStream({
|
|
10339
|
+
async pull(controller) {
|
|
10340
|
+
const enqueueAttemptPart = (part) => {
|
|
10341
|
+
switch (part.type) {
|
|
10342
|
+
case "text-start":
|
|
10343
|
+
openTextParts.add(part.id);
|
|
10344
|
+
break;
|
|
10345
|
+
case "text-end":
|
|
10346
|
+
openTextParts.delete(part.id);
|
|
10347
|
+
break;
|
|
10348
|
+
case "reasoning-start":
|
|
10349
|
+
openReasoningParts.add(part.id);
|
|
10350
|
+
break;
|
|
10351
|
+
case "reasoning-end":
|
|
10352
|
+
openReasoningParts.delete(part.id);
|
|
10353
|
+
break;
|
|
10354
|
+
}
|
|
10355
|
+
controller.enqueue(part);
|
|
10356
|
+
};
|
|
10357
|
+
const flushBufferedAttemptParts = () => {
|
|
10358
|
+
for (const part of bufferedAttemptParts) {
|
|
10359
|
+
enqueueAttemptPart(part);
|
|
10360
|
+
}
|
|
10361
|
+
bufferedAttemptParts = [];
|
|
10362
|
+
};
|
|
10363
|
+
const closeOpenAttemptParts = () => {
|
|
10364
|
+
for (const id of openTextParts) {
|
|
10365
|
+
controller.enqueue({ type: "text-end", id });
|
|
10366
|
+
}
|
|
10367
|
+
openTextParts.clear();
|
|
10368
|
+
for (const id of openReasoningParts) {
|
|
10369
|
+
controller.enqueue({ type: "reasoning-end", id });
|
|
10370
|
+
}
|
|
10371
|
+
openReasoningParts.clear();
|
|
10372
|
+
};
|
|
10373
|
+
while (true) {
|
|
10374
|
+
const { done, value } = await languageModelStreamReader.read();
|
|
10375
|
+
if (enqueueStreamRetryAttemptBoundary) {
|
|
10376
|
+
controller.enqueue(
|
|
10377
|
+
createStreamRetryAttemptBoundaryPart({
|
|
10378
|
+
warnings: !done && value.type === "model-call-start" ? value.warnings : []
|
|
10379
|
+
})
|
|
10380
|
+
);
|
|
10381
|
+
enqueueStreamRetryAttemptBoundary = false;
|
|
10382
|
+
}
|
|
10383
|
+
if (done) {
|
|
10384
|
+
flushBufferedAttemptParts();
|
|
10385
|
+
controller.close();
|
|
10386
|
+
return;
|
|
10387
|
+
}
|
|
10388
|
+
const isToolPart = value.type === "tool-input-start" || value.type === "tool-input-delta" || value.type === "tool-input-end" || value.type === "tool-call" || value.type === "tool-approval-request" || value.type === "tool-approval-response" || value.type === "tool-result" || value.type === "tool-error";
|
|
10389
|
+
if (value.type === "model-call-end") {
|
|
10390
|
+
flushBufferedAttemptParts();
|
|
10391
|
+
enqueueAttemptPart(value);
|
|
10392
|
+
return;
|
|
10393
|
+
}
|
|
10394
|
+
if (shouldBufferToolParts && value.type !== "error" && (isToolPart || bufferedAttemptParts.length > 0)) {
|
|
10395
|
+
if (isOutputChunk2(value)) {
|
|
10396
|
+
clearFirstChunkTimeout();
|
|
10397
|
+
resetChunkTimeout();
|
|
10398
|
+
outputChunksHandledBeforeBuffering.add(value);
|
|
10399
|
+
}
|
|
10400
|
+
bufferedAttemptParts.push(value);
|
|
10401
|
+
continue;
|
|
10402
|
+
}
|
|
10403
|
+
if (value.type !== "error") {
|
|
10404
|
+
enqueueAttemptPart(value);
|
|
10405
|
+
return;
|
|
10406
|
+
}
|
|
10407
|
+
await notify({
|
|
10408
|
+
event: { chunk: value },
|
|
10409
|
+
callbacks: onChunk
|
|
10410
|
+
});
|
|
10411
|
+
const error = wrapGatewayError(value.error);
|
|
10412
|
+
let onErrorResult;
|
|
10413
|
+
try {
|
|
10414
|
+
onErrorResult = await onError({ error });
|
|
10415
|
+
} catch (e) {
|
|
10416
|
+
}
|
|
10417
|
+
const callbackRequestedRetry = canRetryStreamViaOnError && typeof onErrorResult === "object" && onErrorResult != null && "retry" in onErrorResult && onErrorResult.retry === true;
|
|
10418
|
+
const automaticRetry = automaticStreamRetryCount < streamRetries;
|
|
10419
|
+
const callbackRetry = !automaticRetry && callbackRequestedRetry && callbackStreamRetryCount < 1;
|
|
10420
|
+
if (!automaticRetry && !callbackRetry) {
|
|
10421
|
+
flushBufferedAttemptParts();
|
|
10422
|
+
errorsHandledForStreamRetry.add(value.error);
|
|
10423
|
+
controller.enqueue(value);
|
|
10424
|
+
return;
|
|
10425
|
+
}
|
|
10426
|
+
if (automaticRetry) {
|
|
10427
|
+
automaticStreamRetryCount++;
|
|
10428
|
+
} else {
|
|
10429
|
+
callbackStreamRetryCount++;
|
|
10430
|
+
}
|
|
10431
|
+
await languageModelStreamReader.cancel(error);
|
|
10432
|
+
bufferedAttemptParts = [];
|
|
10433
|
+
closeOpenAttemptParts();
|
|
10434
|
+
let retryLanguageModelCall;
|
|
10435
|
+
try {
|
|
10436
|
+
retryLanguageModelCall = await callLanguageModel();
|
|
10437
|
+
} catch (retryError) {
|
|
10438
|
+
controller.enqueue({
|
|
10439
|
+
type: "error",
|
|
10440
|
+
error: retryError
|
|
10441
|
+
});
|
|
10442
|
+
controller.close();
|
|
10443
|
+
return;
|
|
10444
|
+
}
|
|
10445
|
+
request = retryLanguageModelCall.request;
|
|
10446
|
+
response = retryLanguageModelCall.response;
|
|
10447
|
+
languageModelStreamReader = retryLanguageModelCall.stream.getReader();
|
|
10448
|
+
enqueueStreamRetryAttemptBoundary = true;
|
|
10449
|
+
}
|
|
10450
|
+
},
|
|
10451
|
+
cancel(reason) {
|
|
10452
|
+
return languageModelStreamReader.cancel(reason);
|
|
10453
|
+
}
|
|
10454
|
+
});
|
|
10163
10455
|
startFirstChunkTimeout();
|
|
10164
10456
|
const streamAfterToolCallbackInvocation = invokeToolCallbacksFromStream({
|
|
10165
10457
|
stream: languageModelStream,
|
|
@@ -10197,12 +10489,12 @@ var DefaultStreamTextResult = class {
|
|
|
10197
10489
|
executeToolInTelemetryContext: telemetryDispatcher.executeTool,
|
|
10198
10490
|
runInTracingChannelSpan: runInTracingChannelSpanInStep
|
|
10199
10491
|
});
|
|
10200
|
-
const
|
|
10492
|
+
const getStepRequest = () => ({
|
|
10201
10493
|
...request,
|
|
10202
10494
|
body: include.requestBody ? request == null ? void 0 : request.body : void 0,
|
|
10203
10495
|
messages: include.requestMessages ? cloneModelMessages(stepMessages) : void 0
|
|
10204
|
-
};
|
|
10205
|
-
recordedRequestMessages = (_l =
|
|
10496
|
+
});
|
|
10497
|
+
recordedRequestMessages = (_l = getStepRequest().messages) != null ? _l : [];
|
|
10206
10498
|
const stepToolCalls = [];
|
|
10207
10499
|
const stepToolOutputs = [];
|
|
10208
10500
|
const stepToolApprovalResponses = [];
|
|
@@ -10214,7 +10506,7 @@ var DefaultStreamTextResult = class {
|
|
|
10214
10506
|
let stepUsage = createNullLanguageModelUsage();
|
|
10215
10507
|
let stepProviderMetadata;
|
|
10216
10508
|
let stepFirstChunk = true;
|
|
10217
|
-
|
|
10509
|
+
const createModelCallPerformance = () => ({
|
|
10218
10510
|
responseTimeMs: 0,
|
|
10219
10511
|
effectiveOutputTokensPerSecond: 0,
|
|
10220
10512
|
outputTokensPerSecond: void 0,
|
|
@@ -10222,39 +10514,67 @@ var DefaultStreamTextResult = class {
|
|
|
10222
10514
|
effectiveTotalTokensPerSecond: 0,
|
|
10223
10515
|
timeToFirstOutputMs: void 0,
|
|
10224
10516
|
timeBetweenOutputChunksMs: void 0
|
|
10225
|
-
};
|
|
10517
|
+
});
|
|
10518
|
+
let modelCallPerformance = createModelCallPerformance();
|
|
10226
10519
|
const toolExecutionMs = {};
|
|
10227
|
-
|
|
10520
|
+
const createStepResponse = () => ({
|
|
10228
10521
|
id: generateId4(),
|
|
10229
10522
|
timestamp: /* @__PURE__ */ new Date(),
|
|
10230
10523
|
modelId: model.modelId
|
|
10231
|
-
};
|
|
10524
|
+
});
|
|
10525
|
+
let stepResponse = createStepResponse();
|
|
10232
10526
|
const textPartIds = /* @__PURE__ */ new Map();
|
|
10233
10527
|
const reasoningPartIds = /* @__PURE__ */ new Map();
|
|
10528
|
+
const enqueueStepPart = (controller, part) => {
|
|
10529
|
+
controller.enqueue(part);
|
|
10530
|
+
};
|
|
10234
10531
|
self.addStream(
|
|
10235
10532
|
streamWithToolResults.pipeThrough(
|
|
10236
10533
|
new TransformStream({
|
|
10237
10534
|
async transform(chunk, controller) {
|
|
10238
10535
|
var _a27, _b26, _c2, _d2, _e2, _f2, _g2;
|
|
10536
|
+
if (isStreamRetryAttemptBoundaryPart(chunk)) {
|
|
10537
|
+
warnings = chunk.warnings;
|
|
10538
|
+
stepFinishReason = "other";
|
|
10539
|
+
stepRawFinishReason = void 0;
|
|
10540
|
+
hasReceivedTerminalChunk = false;
|
|
10541
|
+
hasReceivedOutputChunk = false;
|
|
10542
|
+
stepUsage = createNullLanguageModelUsage();
|
|
10543
|
+
stepProviderMetadata = void 0;
|
|
10544
|
+
modelCallPerformance = createModelCallPerformance();
|
|
10545
|
+
stepResponse = createStepResponse();
|
|
10546
|
+
textPartIds.clear();
|
|
10547
|
+
reasoningPartIds.clear();
|
|
10548
|
+
controller.enqueue({
|
|
10549
|
+
[streamRetryBoundarySymbol]: {
|
|
10550
|
+
request: getStepRequest(),
|
|
10551
|
+
warnings
|
|
10552
|
+
}
|
|
10553
|
+
});
|
|
10554
|
+
return;
|
|
10555
|
+
}
|
|
10239
10556
|
if (chunk.type === "model-call-start") {
|
|
10240
10557
|
warnings = chunk.warnings;
|
|
10241
10558
|
return;
|
|
10242
10559
|
}
|
|
10243
10560
|
if (stepFirstChunk) {
|
|
10244
10561
|
stepFirstChunk = false;
|
|
10245
|
-
controller
|
|
10562
|
+
enqueueStepPart(controller, {
|
|
10246
10563
|
type: "start-step",
|
|
10247
|
-
request:
|
|
10564
|
+
request: getStepRequest(),
|
|
10248
10565
|
warnings: warnings != null ? warnings : []
|
|
10249
10566
|
});
|
|
10250
10567
|
}
|
|
10251
10568
|
const chunkType = chunk.type;
|
|
10252
10569
|
if (isOutputChunk2(chunk)) {
|
|
10253
|
-
|
|
10570
|
+
const timeoutHandledBeforeBuffering = outputChunksHandledBeforeBuffering.has(chunk);
|
|
10571
|
+
if (!hasReceivedOutputChunk && !timeoutHandledBeforeBuffering) {
|
|
10254
10572
|
clearFirstChunkTimeout();
|
|
10255
10573
|
}
|
|
10256
10574
|
hasReceivedOutputChunk = true;
|
|
10257
|
-
|
|
10575
|
+
if (!timeoutHandledBeforeBuffering) {
|
|
10576
|
+
resetChunkTimeout();
|
|
10577
|
+
}
|
|
10258
10578
|
}
|
|
10259
10579
|
switch (chunkType) {
|
|
10260
10580
|
case "file":
|
|
@@ -10265,18 +10585,18 @@ var DefaultStreamTextResult = class {
|
|
|
10265
10585
|
case "tool-input-end":
|
|
10266
10586
|
case "tool-input-delta":
|
|
10267
10587
|
case "tool-approval-request": {
|
|
10268
|
-
controller
|
|
10588
|
+
enqueueStepPart(controller, chunk);
|
|
10269
10589
|
break;
|
|
10270
10590
|
}
|
|
10271
10591
|
case "text-start": {
|
|
10272
10592
|
const id = reserveTextPartId(chunk.id);
|
|
10273
10593
|
textPartIds.set(chunk.id, id);
|
|
10274
|
-
controller
|
|
10594
|
+
enqueueStepPart(controller, { ...chunk, id });
|
|
10275
10595
|
break;
|
|
10276
10596
|
}
|
|
10277
10597
|
case "text-delta": {
|
|
10278
10598
|
if (chunk.text.length > 0 || chunk.providerMetadata != null) {
|
|
10279
|
-
controller
|
|
10599
|
+
enqueueStepPart(controller, {
|
|
10280
10600
|
...chunk,
|
|
10281
10601
|
id: (_a27 = textPartIds.get(chunk.id)) != null ? _a27 : chunk.id
|
|
10282
10602
|
});
|
|
@@ -10284,7 +10604,7 @@ var DefaultStreamTextResult = class {
|
|
|
10284
10604
|
break;
|
|
10285
10605
|
}
|
|
10286
10606
|
case "text-end": {
|
|
10287
|
-
controller
|
|
10607
|
+
enqueueStepPart(controller, {
|
|
10288
10608
|
...chunk,
|
|
10289
10609
|
id: (_b26 = textPartIds.get(chunk.id)) != null ? _b26 : chunk.id
|
|
10290
10610
|
});
|
|
@@ -10294,18 +10614,18 @@ var DefaultStreamTextResult = class {
|
|
|
10294
10614
|
case "reasoning-start": {
|
|
10295
10615
|
const id = reserveReasoningPartId(chunk.id);
|
|
10296
10616
|
reasoningPartIds.set(chunk.id, id);
|
|
10297
|
-
controller
|
|
10617
|
+
enqueueStepPart(controller, { ...chunk, id });
|
|
10298
10618
|
break;
|
|
10299
10619
|
}
|
|
10300
10620
|
case "reasoning-delta": {
|
|
10301
|
-
controller
|
|
10621
|
+
enqueueStepPart(controller, {
|
|
10302
10622
|
...chunk,
|
|
10303
10623
|
id: (_c2 = reasoningPartIds.get(chunk.id)) != null ? _c2 : chunk.id
|
|
10304
10624
|
});
|
|
10305
10625
|
break;
|
|
10306
10626
|
}
|
|
10307
10627
|
case "reasoning-end": {
|
|
10308
|
-
controller
|
|
10628
|
+
enqueueStepPart(controller, {
|
|
10309
10629
|
...chunk,
|
|
10310
10630
|
id: (_d2 = reasoningPartIds.get(chunk.id)) != null ? _d2 : chunk.id
|
|
10311
10631
|
});
|
|
@@ -10313,24 +10633,24 @@ var DefaultStreamTextResult = class {
|
|
|
10313
10633
|
break;
|
|
10314
10634
|
}
|
|
10315
10635
|
case "tool-call": {
|
|
10316
|
-
controller
|
|
10636
|
+
enqueueStepPart(controller, chunk);
|
|
10317
10637
|
stepToolCalls.push(chunk);
|
|
10318
10638
|
break;
|
|
10319
10639
|
}
|
|
10320
10640
|
case "tool-approval-response": {
|
|
10321
|
-
controller
|
|
10641
|
+
enqueueStepPart(controller, chunk);
|
|
10322
10642
|
stepToolApprovalResponses.push(chunk);
|
|
10323
10643
|
break;
|
|
10324
10644
|
}
|
|
10325
10645
|
case "tool-result": {
|
|
10326
|
-
controller
|
|
10646
|
+
enqueueStepPart(controller, chunk);
|
|
10327
10647
|
if (!chunk.preliminary) {
|
|
10328
10648
|
stepToolOutputs.push(chunk);
|
|
10329
10649
|
}
|
|
10330
10650
|
break;
|
|
10331
10651
|
}
|
|
10332
10652
|
case "tool-error": {
|
|
10333
|
-
controller
|
|
10653
|
+
enqueueStepPart(controller, chunk);
|
|
10334
10654
|
stepToolOutputs.push(chunk);
|
|
10335
10655
|
break;
|
|
10336
10656
|
}
|
|
@@ -10357,13 +10677,13 @@ var DefaultStreamTextResult = class {
|
|
|
10357
10677
|
}
|
|
10358
10678
|
case "error": {
|
|
10359
10679
|
hasReceivedTerminalChunk = true;
|
|
10360
|
-
controller
|
|
10680
|
+
enqueueStepPart(controller, chunk);
|
|
10361
10681
|
stepFinishReason = "error";
|
|
10362
10682
|
break;
|
|
10363
10683
|
}
|
|
10364
10684
|
case "raw": {
|
|
10365
10685
|
if (include.rawChunks) {
|
|
10366
|
-
controller
|
|
10686
|
+
enqueueStepPart(controller, chunk);
|
|
10367
10687
|
}
|
|
10368
10688
|
break;
|
|
10369
10689
|
}
|
|
@@ -10376,7 +10696,7 @@ var DefaultStreamTextResult = class {
|
|
|
10376
10696
|
// invoke onEnd callback and resolve toolResults promise when the stream is about to close:
|
|
10377
10697
|
async flush(controller) {
|
|
10378
10698
|
if (!hasReceivedTerminalChunk && !hasReceivedOutputChunk) {
|
|
10379
|
-
controller
|
|
10699
|
+
enqueueStepPart(controller, {
|
|
10380
10700
|
type: "error",
|
|
10381
10701
|
error: new NoOutputGeneratedError({
|
|
10382
10702
|
message: "No output generated. The model stream ended without a finish chunk."
|
|
@@ -10403,7 +10723,7 @@ var DefaultStreamTextResult = class {
|
|
|
10403
10723
|
headers: response == null ? void 0 : response.headers
|
|
10404
10724
|
}
|
|
10405
10725
|
};
|
|
10406
|
-
controller
|
|
10726
|
+
enqueueStepPart(controller, finishStepPart);
|
|
10407
10727
|
const combinedUsage = addLanguageModelUsage(usage, stepUsage);
|
|
10408
10728
|
await stepFinish.promise;
|
|
10409
10729
|
const clientToolCalls = stepToolCalls.filter(
|
|
@@ -10452,14 +10772,14 @@ var DefaultStreamTextResult = class {
|
|
|
10452
10772
|
})
|
|
10453
10773
|
);
|
|
10454
10774
|
} catch (error) {
|
|
10455
|
-
controller
|
|
10775
|
+
enqueueStepPart(controller, {
|
|
10456
10776
|
type: "error",
|
|
10457
10777
|
error
|
|
10458
10778
|
});
|
|
10459
10779
|
self.closeStream();
|
|
10460
10780
|
}
|
|
10461
10781
|
} else {
|
|
10462
|
-
controller
|
|
10782
|
+
enqueueStepPart(controller, {
|
|
10463
10783
|
type: "finish",
|
|
10464
10784
|
finishReason: stepFinishReason,
|
|
10465
10785
|
rawFinishReason: stepRawFinishReason,
|