ai 4.2.11 → 4.3.1
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.mts +17 -3
- package/dist/index.d.ts +17 -3
- package/dist/index.js +62 -23
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +62 -23
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
@@ -1037,13 +1037,15 @@ var NoObjectGeneratedError = class extends AISDKError4 {
|
|
1037
1037
|
cause,
|
1038
1038
|
text: text2,
|
1039
1039
|
response,
|
1040
|
-
usage
|
1040
|
+
usage,
|
1041
|
+
finishReason
|
1041
1042
|
}) {
|
1042
1043
|
super({ name: name4, message, cause });
|
1043
1044
|
this[_a4] = true;
|
1044
1045
|
this.text = text2;
|
1045
1046
|
this.response = response;
|
1046
1047
|
this.usage = usage;
|
1048
|
+
this.finishReason = finishReason;
|
1047
1049
|
}
|
1048
1050
|
static isInstance(error) {
|
1049
1051
|
return AISDKError4.hasMarker(error, marker4);
|
@@ -2213,7 +2215,8 @@ var noSchemaOutputStrategy = {
|
|
2213
2215
|
message: "No object generated: response did not match schema.",
|
2214
2216
|
text: context.text,
|
2215
2217
|
response: context.response,
|
2216
|
-
usage: context.usage
|
2218
|
+
usage: context.usage,
|
2219
|
+
finishReason: context.finishReason
|
2217
2220
|
})
|
2218
2221
|
} : { success: true, value };
|
2219
2222
|
},
|
@@ -2710,7 +2713,8 @@ async function generateObject({
|
|
2710
2713
|
throw new NoObjectGeneratedError({
|
2711
2714
|
message: "No object generated: the model did not return a response.",
|
2712
2715
|
response: responseData,
|
2713
|
-
usage: calculateLanguageModelUsage(result2.usage)
|
2716
|
+
usage: calculateLanguageModelUsage(result2.usage),
|
2717
|
+
finishReason: result2.finishReason
|
2714
2718
|
});
|
2715
2719
|
}
|
2716
2720
|
span2.setAttributes(
|
@@ -2819,7 +2823,8 @@ async function generateObject({
|
|
2819
2823
|
throw new NoObjectGeneratedError({
|
2820
2824
|
message: "No object generated: the tool was not called.",
|
2821
2825
|
response: responseData,
|
2822
|
-
usage: calculateLanguageModelUsage(result2.usage)
|
2826
|
+
usage: calculateLanguageModelUsage(result2.usage),
|
2827
|
+
finishReason: result2.finishReason
|
2823
2828
|
});
|
2824
2829
|
}
|
2825
2830
|
span2.setAttributes(
|
@@ -2875,7 +2880,8 @@ async function generateObject({
|
|
2875
2880
|
cause: parseResult.error,
|
2876
2881
|
text: result2,
|
2877
2882
|
response,
|
2878
|
-
usage: calculateLanguageModelUsage(usage)
|
2883
|
+
usage: calculateLanguageModelUsage(usage),
|
2884
|
+
finishReason
|
2879
2885
|
});
|
2880
2886
|
}
|
2881
2887
|
const validationResult = outputStrategy.validateFinalResult(
|
@@ -2892,7 +2898,8 @@ async function generateObject({
|
|
2892
2898
|
cause: validationResult.error,
|
2893
2899
|
text: result2,
|
2894
2900
|
response,
|
2895
|
-
usage: calculateLanguageModelUsage(usage)
|
2901
|
+
usage: calculateLanguageModelUsage(usage),
|
2902
|
+
finishReason
|
2896
2903
|
});
|
2897
2904
|
}
|
2898
2905
|
return validationResult.value;
|
@@ -3500,7 +3507,8 @@ var DefaultStreamObjectResult = class {
|
|
3500
3507
|
cause: validationResult.error,
|
3501
3508
|
text: accumulatedText,
|
3502
3509
|
response,
|
3503
|
-
usage
|
3510
|
+
usage,
|
3511
|
+
finishReason
|
3504
3512
|
});
|
3505
3513
|
self.objectPromise.reject(error);
|
3506
3514
|
}
|
@@ -4328,7 +4336,11 @@ async function generateText({
|
|
4328
4336
|
}
|
4329
4337
|
return output.parseOutput(
|
4330
4338
|
{ text: text2 },
|
4331
|
-
{
|
4339
|
+
{
|
4340
|
+
response: currentModelResponse.response,
|
4341
|
+
usage,
|
4342
|
+
finishReason: currentModelResponse.finishReason
|
4343
|
+
}
|
4332
4344
|
);
|
4333
4345
|
},
|
4334
4346
|
toolCalls: currentToolCalls,
|
@@ -4588,7 +4600,8 @@ var object = ({
|
|
4588
4600
|
cause: parseResult.error,
|
4589
4601
|
text: text2,
|
4590
4602
|
response: context.response,
|
4591
|
-
usage: context.usage
|
4603
|
+
usage: context.usage,
|
4604
|
+
finishReason: context.finishReason
|
4592
4605
|
});
|
4593
4606
|
}
|
4594
4607
|
const validationResult = safeValidateTypes4({
|
@@ -4601,7 +4614,8 @@ var object = ({
|
|
4601
4614
|
cause: validationResult.error,
|
4602
4615
|
text: text2,
|
4603
4616
|
response: context.response,
|
4604
|
-
usage: context.usage
|
4617
|
+
usage: context.usage,
|
4618
|
+
finishReason: context.finishReason
|
4605
4619
|
});
|
4606
4620
|
}
|
4607
4621
|
return validationResult.value;
|
@@ -4610,23 +4624,49 @@ var object = ({
|
|
4610
4624
|
};
|
4611
4625
|
|
4612
4626
|
// core/generate-text/smooth-stream.ts
|
4613
|
-
import { InvalidArgumentError as InvalidArgumentError2 } from "@ai-sdk/provider";
|
4614
4627
|
import { delay as originalDelay } from "@ai-sdk/provider-utils";
|
4628
|
+
import { InvalidArgumentError as InvalidArgumentError2 } from "@ai-sdk/provider";
|
4615
4629
|
var CHUNKING_REGEXPS = {
|
4616
|
-
word: /\
|
4617
|
-
line:
|
4630
|
+
word: /\S+\s+/m,
|
4631
|
+
line: /\n+/m
|
4618
4632
|
};
|
4619
4633
|
function smoothStream({
|
4620
4634
|
delayInMs = 10,
|
4621
4635
|
chunking = "word",
|
4622
4636
|
_internal: { delay: delay2 = originalDelay } = {}
|
4623
4637
|
} = {}) {
|
4624
|
-
|
4625
|
-
if (
|
4626
|
-
|
4627
|
-
|
4628
|
-
|
4629
|
-
|
4638
|
+
let detectChunk;
|
4639
|
+
if (typeof chunking === "function") {
|
4640
|
+
detectChunk = (buffer) => {
|
4641
|
+
const match = chunking(buffer);
|
4642
|
+
if (match == null) {
|
4643
|
+
return null;
|
4644
|
+
}
|
4645
|
+
if (!match.length) {
|
4646
|
+
throw new Error(`Chunking function must return a non-empty string.`);
|
4647
|
+
}
|
4648
|
+
if (!buffer.startsWith(match)) {
|
4649
|
+
throw new Error(
|
4650
|
+
`Chunking function must return a match that is a prefix of the buffer. Received: "${match}" expected to start with "${buffer}"`
|
4651
|
+
);
|
4652
|
+
}
|
4653
|
+
return match;
|
4654
|
+
};
|
4655
|
+
} else {
|
4656
|
+
const chunkingRegex = typeof chunking === "string" ? CHUNKING_REGEXPS[chunking] : chunking;
|
4657
|
+
if (chunkingRegex == null) {
|
4658
|
+
throw new InvalidArgumentError2({
|
4659
|
+
argument: "chunking",
|
4660
|
+
message: `Chunking must be "word" or "line" or a RegExp. Received: ${chunking}`
|
4661
|
+
});
|
4662
|
+
}
|
4663
|
+
detectChunk = (buffer) => {
|
4664
|
+
const match = chunkingRegex.exec(buffer);
|
4665
|
+
if (!match) {
|
4666
|
+
return null;
|
4667
|
+
}
|
4668
|
+
return buffer.slice(0, match.index) + (match == null ? void 0 : match[0]);
|
4669
|
+
};
|
4630
4670
|
}
|
4631
4671
|
return () => {
|
4632
4672
|
let buffer = "";
|
@@ -4642,10 +4682,9 @@ function smoothStream({
|
|
4642
4682
|
}
|
4643
4683
|
buffer += chunk.textDelta;
|
4644
4684
|
let match;
|
4645
|
-
while ((match =
|
4646
|
-
|
4647
|
-
|
4648
|
-
buffer = buffer.slice(chunk2.length);
|
4685
|
+
while ((match = detectChunk(buffer)) != null) {
|
4686
|
+
controller.enqueue({ type: "text-delta", textDelta: match });
|
4687
|
+
buffer = buffer.slice(match.length);
|
4649
4688
|
await delay2(delayInMs);
|
4650
4689
|
}
|
4651
4690
|
}
|