ai 7.0.169 → 7.0.170
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 +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +159 -38
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts.map +1 -1
- package/dist/internal/index.js +1 -1
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +9 -0
- package/docs/03-ai-sdk-core/32-decisions.mdx +14 -10
- package/docs/07-reference/02-ai-sdk-ui/32-prune-messages.mdx +9 -0
- package/package.json +5 -5
- package/src/generate-object/output-strategy.ts +8 -0
- package/src/generate-object/stream-object.ts +22 -0
- package/src/generate-text/output.ts +22 -28
- package/src/generate-text/stream-text.ts +23 -0
- package/src/ui/chat.ts +6 -0
- package/src/util/array-output-stream.ts +161 -0
package/dist/index.js
CHANGED
|
@@ -1242,7 +1242,7 @@ function cloneValue(value, seen) {
|
|
|
1242
1242
|
}
|
|
1243
1243
|
//#endregion
|
|
1244
1244
|
//#region src/version.ts
|
|
1245
|
-
const VERSION = "7.0.
|
|
1245
|
+
const VERSION = "7.0.170";
|
|
1246
1246
|
//#endregion
|
|
1247
1247
|
//#region src/util/download/download.ts
|
|
1248
1248
|
/**
|
|
@@ -3195,6 +3195,96 @@ function isToolExecutionAllowedFinishReason(finishReason) {
|
|
|
3195
3195
|
return finishReason === "stop" || finishReason === "tool-calls";
|
|
3196
3196
|
}
|
|
3197
3197
|
//#endregion
|
|
3198
|
+
//#region src/util/array-output-stream.ts
|
|
3199
|
+
const arrayStreamParserFactories = /* @__PURE__ */ new WeakMap();
|
|
3200
|
+
function createArrayStreamParser(output) {
|
|
3201
|
+
return arrayStreamParserFactories.get(output)?.();
|
|
3202
|
+
}
|
|
3203
|
+
/**
|
|
3204
|
+
* Scan only new text and parse/validate each delimited element once. Keep the
|
|
3205
|
+
* last element pending until another starts or the enclosing object closes,
|
|
3206
|
+
* matching the partial JSON parser's treatment of an incomplete response.
|
|
3207
|
+
* Nonstandard envelopes and malformed JSON use the existing partial parser.
|
|
3208
|
+
*/
|
|
3209
|
+
function createArrayOutputStreamParser(schema, skipInvalidElements) {
|
|
3210
|
+
let phase = 0;
|
|
3211
|
+
let prefix = "";
|
|
3212
|
+
let elementText = "";
|
|
3213
|
+
let depth = 0;
|
|
3214
|
+
let inString = false;
|
|
3215
|
+
let escaped = false;
|
|
3216
|
+
let afterComma = false;
|
|
3217
|
+
let pending = [];
|
|
3218
|
+
const elements = [];
|
|
3219
|
+
let publishedCount;
|
|
3220
|
+
async function finishElement() {
|
|
3221
|
+
const parsed = await safeParseJSON({ text: elementText });
|
|
3222
|
+
if (!parsed.success) return false;
|
|
3223
|
+
const validated = await safeValidateTypes({
|
|
3224
|
+
value: parsed.value,
|
|
3225
|
+
schema
|
|
3226
|
+
});
|
|
3227
|
+
if (validated.success) pending = [validated.value];
|
|
3228
|
+
else if (!skipInvalidElements) return false;
|
|
3229
|
+
elementText = "";
|
|
3230
|
+
return true;
|
|
3231
|
+
}
|
|
3232
|
+
function publishPending() {
|
|
3233
|
+
elements.push(...pending);
|
|
3234
|
+
pending = [];
|
|
3235
|
+
}
|
|
3236
|
+
return async (textDelta) => {
|
|
3237
|
+
for (const char of textDelta) {
|
|
3238
|
+
if (phase === 0) {
|
|
3239
|
+
prefix += char;
|
|
3240
|
+
if (char === "[") {
|
|
3241
|
+
if (!/^\s*\{\s*"elements"\s*:\s*\[$/.test(prefix)) return "fallback";
|
|
3242
|
+
phase = 1;
|
|
3243
|
+
} else if (prefix.length > 256) return "fallback";
|
|
3244
|
+
continue;
|
|
3245
|
+
}
|
|
3246
|
+
if (phase === 2 || phase === 3) {
|
|
3247
|
+
if (/\s/.test(char)) continue;
|
|
3248
|
+
if (phase === 2 && char === "}") {
|
|
3249
|
+
publishPending();
|
|
3250
|
+
phase = 3;
|
|
3251
|
+
continue;
|
|
3252
|
+
}
|
|
3253
|
+
return "fallback";
|
|
3254
|
+
}
|
|
3255
|
+
if (!inString && depth === 0 && (char === "," || char === "]")) {
|
|
3256
|
+
if (elementText.length > 0) {
|
|
3257
|
+
if (!await finishElement()) return "fallback";
|
|
3258
|
+
} else if (char === "," || afterComma) return "fallback";
|
|
3259
|
+
afterComma = char === ",";
|
|
3260
|
+
if (char === "]") phase = 2;
|
|
3261
|
+
continue;
|
|
3262
|
+
}
|
|
3263
|
+
if (elementText.length === 0) {
|
|
3264
|
+
if (/\s/.test(char)) continue;
|
|
3265
|
+
publishPending();
|
|
3266
|
+
afterComma = false;
|
|
3267
|
+
}
|
|
3268
|
+
elementText += char;
|
|
3269
|
+
if (inString) {
|
|
3270
|
+
if (escaped) escaped = false;
|
|
3271
|
+
else if (char === "\\") escaped = true;
|
|
3272
|
+
else if (char === "\"") inString = false;
|
|
3273
|
+
} else if (char === "\"") inString = true;
|
|
3274
|
+
else if (char === "{" || char === "[") depth++;
|
|
3275
|
+
else if (char === "}" || char === "]") depth--;
|
|
3276
|
+
}
|
|
3277
|
+
if (phase === 0 || publishedCount === elements.length) return;
|
|
3278
|
+
const previousCount = publishedCount ?? 0;
|
|
3279
|
+
const delta = (publishedCount == null ? "[" : "") + (previousCount > 0 && elements.length > previousCount ? "," : "") + elements.slice(previousCount).map((value) => JSON.stringify(value)).join(",") + (phase === 3 ? "]" : "");
|
|
3280
|
+
publishedCount = elements.length;
|
|
3281
|
+
return {
|
|
3282
|
+
partial: elements.slice(),
|
|
3283
|
+
textDelta: delta
|
|
3284
|
+
};
|
|
3285
|
+
};
|
|
3286
|
+
}
|
|
3287
|
+
//#endregion
|
|
3198
3288
|
//#region src/util/fix-json.ts
|
|
3199
3289
|
function fixJson(input) {
|
|
3200
3290
|
const stack = ["ROOT"];
|
|
@@ -3506,24 +3596,20 @@ function object$2({ schema: inputSchema, name, description }) {
|
|
|
3506
3596
|
async parseCompleteOutput({ text }, context) {
|
|
3507
3597
|
const parseResult = await safeParseJSON({ text });
|
|
3508
3598
|
if (!parseResult.success) throw new NoObjectGeneratedError({
|
|
3599
|
+
...context,
|
|
3509
3600
|
message: "No object generated: could not parse the response.",
|
|
3510
3601
|
cause: parseResult.error,
|
|
3511
|
-
text
|
|
3512
|
-
response: context.response,
|
|
3513
|
-
usage: context.usage,
|
|
3514
|
-
finishReason: context.finishReason
|
|
3602
|
+
text
|
|
3515
3603
|
});
|
|
3516
3604
|
const validationResult = await safeValidateTypes({
|
|
3517
3605
|
value: parseResult.value,
|
|
3518
3606
|
schema
|
|
3519
3607
|
});
|
|
3520
3608
|
if (!validationResult.success) throw new NoObjectGeneratedError({
|
|
3609
|
+
...context,
|
|
3521
3610
|
message: "No object generated: response did not match schema.",
|
|
3522
3611
|
cause: validationResult.error,
|
|
3523
|
-
text
|
|
3524
|
-
response: context.response,
|
|
3525
|
-
usage: context.usage,
|
|
3526
|
-
finishReason: context.finishReason
|
|
3612
|
+
text
|
|
3527
3613
|
});
|
|
3528
3614
|
return validationResult.value;
|
|
3529
3615
|
},
|
|
@@ -3554,7 +3640,7 @@ function array$2({ element: inputElementSchema, minItems, maxItems, name, descri
|
|
|
3554
3640
|
message: "minItems must be less than or equal to maxItems"
|
|
3555
3641
|
});
|
|
3556
3642
|
const elementSchema = asSchema$1(inputElementSchema);
|
|
3557
|
-
|
|
3643
|
+
const output = {
|
|
3558
3644
|
name: "array",
|
|
3559
3645
|
responseFormat: resolve(elementSchema.jsonSchema).then((jsonSchema) => {
|
|
3560
3646
|
const { $schema: _$schema, definitions, $defs, ...itemSchema } = jsonSchema;
|
|
@@ -3581,24 +3667,20 @@ function array$2({ element: inputElementSchema, minItems, maxItems, name, descri
|
|
|
3581
3667
|
async parseCompleteOutput({ text }, context) {
|
|
3582
3668
|
const parseResult = await safeParseJSON({ text });
|
|
3583
3669
|
if (!parseResult.success) throw new NoObjectGeneratedError({
|
|
3670
|
+
...context,
|
|
3584
3671
|
message: "No object generated: could not parse the response.",
|
|
3585
3672
|
cause: parseResult.error,
|
|
3586
|
-
text
|
|
3587
|
-
response: context.response,
|
|
3588
|
-
usage: context.usage,
|
|
3589
|
-
finishReason: context.finishReason
|
|
3673
|
+
text
|
|
3590
3674
|
});
|
|
3591
3675
|
const outerValue = parseResult.value;
|
|
3592
3676
|
if (outerValue == null || typeof outerValue !== "object" || !("elements" in outerValue) || !Array.isArray(outerValue.elements)) throw new NoObjectGeneratedError({
|
|
3677
|
+
...context,
|
|
3593
3678
|
message: "No object generated: response did not match schema.",
|
|
3594
3679
|
cause: new TypeValidationError$1({
|
|
3595
3680
|
value: outerValue,
|
|
3596
3681
|
cause: "response must be an object with an elements array"
|
|
3597
3682
|
}),
|
|
3598
|
-
text
|
|
3599
|
-
response: context.response,
|
|
3600
|
-
usage: context.usage,
|
|
3601
|
-
finishReason: context.finishReason
|
|
3683
|
+
text
|
|
3602
3684
|
});
|
|
3603
3685
|
const lengthValidationError = getArrayLengthValidationError({
|
|
3604
3686
|
value: outerValue.elements,
|
|
@@ -3606,12 +3688,10 @@ function array$2({ element: inputElementSchema, minItems, maxItems, name, descri
|
|
|
3606
3688
|
maxItems
|
|
3607
3689
|
});
|
|
3608
3690
|
if (lengthValidationError != null) throw new NoObjectGeneratedError({
|
|
3691
|
+
...context,
|
|
3609
3692
|
message: "No object generated: response did not match schema.",
|
|
3610
3693
|
cause: lengthValidationError,
|
|
3611
|
-
text
|
|
3612
|
-
response: context.response,
|
|
3613
|
-
usage: context.usage,
|
|
3614
|
-
finishReason: context.finishReason
|
|
3694
|
+
text
|
|
3615
3695
|
});
|
|
3616
3696
|
const validatedElements = [];
|
|
3617
3697
|
for (const element of outerValue.elements) {
|
|
@@ -3620,12 +3700,10 @@ function array$2({ element: inputElementSchema, minItems, maxItems, name, descri
|
|
|
3620
3700
|
schema: elementSchema
|
|
3621
3701
|
});
|
|
3622
3702
|
if (!validationResult.success) throw new NoObjectGeneratedError({
|
|
3703
|
+
...context,
|
|
3623
3704
|
message: "No object generated: response did not match schema.",
|
|
3624
3705
|
cause: validationResult.error,
|
|
3625
|
-
text
|
|
3626
|
-
response: context.response,
|
|
3627
|
-
usage: context.usage,
|
|
3628
|
-
finishReason: context.finishReason
|
|
3706
|
+
text
|
|
3629
3707
|
});
|
|
3630
3708
|
validatedElements.push(validationResult.value);
|
|
3631
3709
|
}
|
|
@@ -3669,6 +3747,9 @@ function array$2({ element: inputElementSchema, minItems, maxItems, name, descri
|
|
|
3669
3747
|
} });
|
|
3670
3748
|
}
|
|
3671
3749
|
};
|
|
3750
|
+
const parsePartialOutput = output.parsePartialOutput;
|
|
3751
|
+
arrayStreamParserFactories.set(output, () => output.parsePartialOutput === parsePartialOutput ? createArrayOutputStreamParser(elementSchema, true) : void 0);
|
|
3752
|
+
return output;
|
|
3672
3753
|
}
|
|
3673
3754
|
function validateArrayBound({ name, value }) {
|
|
3674
3755
|
if (value == null) return;
|
|
@@ -3724,24 +3805,20 @@ const choice = ({ options: choiceOptions, name, description }) => {
|
|
|
3724
3805
|
async parseCompleteOutput({ text }, context) {
|
|
3725
3806
|
const parseResult = await safeParseJSON({ text });
|
|
3726
3807
|
if (!parseResult.success) throw new NoObjectGeneratedError({
|
|
3808
|
+
...context,
|
|
3727
3809
|
message: "No object generated: could not parse the response.",
|
|
3728
3810
|
cause: parseResult.error,
|
|
3729
|
-
text
|
|
3730
|
-
response: context.response,
|
|
3731
|
-
usage: context.usage,
|
|
3732
|
-
finishReason: context.finishReason
|
|
3811
|
+
text
|
|
3733
3812
|
});
|
|
3734
3813
|
const outerValue = parseResult.value;
|
|
3735
3814
|
if (outerValue == null || typeof outerValue !== "object" || !("result" in outerValue) || typeof outerValue.result !== "string" || !choiceOptions.includes(outerValue.result)) throw new NoObjectGeneratedError({
|
|
3815
|
+
...context,
|
|
3736
3816
|
message: "No object generated: response did not match schema.",
|
|
3737
3817
|
cause: new TypeValidationError$1({
|
|
3738
3818
|
value: outerValue,
|
|
3739
3819
|
cause: "response must be an object that contains a choice value."
|
|
3740
3820
|
}),
|
|
3741
|
-
text
|
|
3742
|
-
response: context.response,
|
|
3743
|
-
usage: context.usage,
|
|
3744
|
-
finishReason: context.finishReason
|
|
3821
|
+
text
|
|
3745
3822
|
});
|
|
3746
3823
|
return outerValue.result;
|
|
3747
3824
|
},
|
|
@@ -3783,12 +3860,10 @@ const json = ({ name, description } = {}) => {
|
|
|
3783
3860
|
async parseCompleteOutput({ text }, context) {
|
|
3784
3861
|
const parseResult = await safeParseJSON({ text });
|
|
3785
3862
|
if (!parseResult.success) throw new NoObjectGeneratedError({
|
|
3863
|
+
...context,
|
|
3786
3864
|
message: "No object generated: could not parse the response.",
|
|
3787
3865
|
cause: parseResult.error,
|
|
3788
|
-
text
|
|
3789
|
-
response: context.response,
|
|
3790
|
-
usage: context.usage,
|
|
3791
|
-
finishReason: context.finishReason
|
|
3866
|
+
text
|
|
3792
3867
|
});
|
|
3793
3868
|
return parseResult.value;
|
|
3794
3869
|
},
|
|
@@ -9304,9 +9379,13 @@ function createOutputTransformStream(output, collectOutput = true) {
|
|
|
9304
9379
|
let text = "";
|
|
9305
9380
|
let textChunk = "";
|
|
9306
9381
|
let textProviderMetadata = void 0;
|
|
9382
|
+
let arrayStreamParser = createArrayStreamParser(output);
|
|
9383
|
+
let lastArrayPartial;
|
|
9307
9384
|
let lastPublishedValue = void 0;
|
|
9308
9385
|
let hasPublishedValue = false;
|
|
9309
9386
|
function resetOutputState() {
|
|
9387
|
+
arrayStreamParser = createArrayStreamParser(output);
|
|
9388
|
+
lastArrayPartial = void 0;
|
|
9310
9389
|
firstTextChunkId = void 0;
|
|
9311
9390
|
text = "";
|
|
9312
9391
|
textChunk = "";
|
|
@@ -9402,6 +9481,24 @@ function createOutputTransformStream(output, collectOutput = true) {
|
|
|
9402
9481
|
});
|
|
9403
9482
|
return;
|
|
9404
9483
|
}
|
|
9484
|
+
if (arrayStreamParser != null) {
|
|
9485
|
+
const result = await arrayStreamParser(chunk.text);
|
|
9486
|
+
if (result !== "fallback") {
|
|
9487
|
+
if (result != null) {
|
|
9488
|
+
lastArrayPartial = result.partial;
|
|
9489
|
+
publishTextChunk({
|
|
9490
|
+
controller,
|
|
9491
|
+
partialOutput: lastArrayPartial
|
|
9492
|
+
});
|
|
9493
|
+
}
|
|
9494
|
+
return;
|
|
9495
|
+
}
|
|
9496
|
+
arrayStreamParser = void 0;
|
|
9497
|
+
if (lastArrayPartial != null) {
|
|
9498
|
+
lastPublishedValue = JSON.stringify(lastArrayPartial);
|
|
9499
|
+
hasPublishedValue = true;
|
|
9500
|
+
}
|
|
9501
|
+
}
|
|
9405
9502
|
const result = await output.parsePartialOutput({ text });
|
|
9406
9503
|
if (result !== void 0) {
|
|
9407
9504
|
const currentValue = typeof result.partial === "string" ? result.partial : JSON.stringify(result.partial);
|
|
@@ -13803,6 +13900,7 @@ const objectOutputStrategy = (schema) => ({
|
|
|
13803
13900
|
const arrayOutputStrategy = (schema) => {
|
|
13804
13901
|
return {
|
|
13805
13902
|
type: "array",
|
|
13903
|
+
createArrayStreamParser: () => createArrayOutputStreamParser(schema, false),
|
|
13806
13904
|
jsonSchema: async () => {
|
|
13807
13905
|
const { $schema: _$schema, definitions, $defs, ...itemSchema } = await schema.jsonSchema;
|
|
13808
13906
|
return {
|
|
@@ -14758,6 +14856,7 @@ var DefaultStreamObjectResult = class {
|
|
|
14758
14856
|
let error;
|
|
14759
14857
|
let terminalError;
|
|
14760
14858
|
let msToFirstChunk = void 0;
|
|
14859
|
+
let arrayStreamParser = outputStrategy.createArrayStreamParser?.();
|
|
14761
14860
|
let accumulatedText = "";
|
|
14762
14861
|
let textDelta = "";
|
|
14763
14862
|
let fullResponse = {
|
|
@@ -14782,6 +14881,26 @@ var DefaultStreamObjectResult = class {
|
|
|
14782
14881
|
if (typeof chunk === "string") {
|
|
14783
14882
|
accumulatedText += chunk;
|
|
14784
14883
|
textDelta += chunk;
|
|
14884
|
+
if (arrayStreamParser != null) {
|
|
14885
|
+
const result = await arrayStreamParser(chunk);
|
|
14886
|
+
if (result !== "fallback") {
|
|
14887
|
+
if (result != null) {
|
|
14888
|
+
latestObject = result.partial;
|
|
14889
|
+
controller.enqueue({
|
|
14890
|
+
type: "object",
|
|
14891
|
+
object: latestObject
|
|
14892
|
+
});
|
|
14893
|
+
controller.enqueue({
|
|
14894
|
+
type: "text-delta",
|
|
14895
|
+
textDelta: result.textDelta
|
|
14896
|
+
});
|
|
14897
|
+
textDelta = "";
|
|
14898
|
+
isFirstDelta = false;
|
|
14899
|
+
}
|
|
14900
|
+
return;
|
|
14901
|
+
}
|
|
14902
|
+
arrayStreamParser = void 0;
|
|
14903
|
+
}
|
|
14785
14904
|
const { value: currentObjectJson, state: parseState } = await parsePartialJson(accumulatedText);
|
|
14786
14905
|
if (currentObjectJson !== void 0 && !isDeepEqualData(latestObjectJson, currentObjectJson)) {
|
|
14787
14906
|
const validationResult = await outputStrategy.validatePartialResult({
|
|
@@ -21147,7 +21266,9 @@ var AbstractChat = class {
|
|
|
21147
21266
|
const message = this.experimental_queuedMessages.find((message) => message.id === id);
|
|
21148
21267
|
if (message == null) return;
|
|
21149
21268
|
this.setQueuedMessages([message, ...this.experimental_queuedMessages.filter((queued) => queued.id !== id)]);
|
|
21269
|
+
const stopGeneration = this.stopGeneration + 1;
|
|
21150
21270
|
await this.stop();
|
|
21271
|
+
if (stopGeneration !== this.stopGeneration) return;
|
|
21151
21272
|
await this.resumeQueue({
|
|
21152
21273
|
skipWaitForUser: true,
|
|
21153
21274
|
options: this.queuedMessageOptions.get(id)
|