@ai-sdk/anthropic 3.0.136 → 3.0.138
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.js +304 -50
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +305 -50
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +12 -1
- package/dist/internal/index.d.ts +12 -1
- package/dist/internal/index.js +307 -51
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +306 -50
- package/dist/internal/index.mjs.map +1 -1
- package/docs/05-anthropic.mdx +19 -2
- package/package.json +2 -2
- package/src/anthropic-messages-language-model.ts +136 -24
- package/src/convert-to-anthropic-messages-prompt.ts +21 -2
- package/src/has-circular-schema-reference.ts +118 -0
- package/src/internal/index.ts +1 -0
- package/src/sanitize-json-schema.ts +27 -24
- package/src/supports-native-structured-output-schema.ts +138 -0
package/dist/internal/index.mjs
CHANGED
|
@@ -2126,6 +2126,7 @@ import {
|
|
|
2126
2126
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
2127
2127
|
} from "@ai-sdk/provider";
|
|
2128
2128
|
import {
|
|
2129
|
+
stringifyWithBigIntAndCycles,
|
|
2129
2130
|
convertBase64ToUint8Array,
|
|
2130
2131
|
convertToBase64,
|
|
2131
2132
|
parseProviderOptions,
|
|
@@ -2840,7 +2841,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
2840
2841
|
case "json":
|
|
2841
2842
|
case "error-json":
|
|
2842
2843
|
default:
|
|
2843
|
-
contentValue =
|
|
2844
|
+
contentValue = stringifyWithBigIntAndCycles(output.value);
|
|
2844
2845
|
break;
|
|
2845
2846
|
}
|
|
2846
2847
|
const toolsetName = getAnthropicToolsetName({
|
|
@@ -3078,7 +3079,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
3078
3079
|
type: "tool_use",
|
|
3079
3080
|
id: part.toolCallId,
|
|
3080
3081
|
name: part.toolName,
|
|
3081
|
-
input: part.input,
|
|
3082
|
+
input: parseToolCallInput(part.input),
|
|
3082
3083
|
...caller && { caller },
|
|
3083
3084
|
cache_control: cacheControl
|
|
3084
3085
|
});
|
|
@@ -3563,6 +3564,18 @@ function getAnthropicCaller(providerOptions) {
|
|
|
3563
3564
|
}
|
|
3564
3565
|
return (caller == null ? void 0 : caller.type) === "direct" ? { type: "direct" } : void 0;
|
|
3565
3566
|
}
|
|
3567
|
+
function parseToolCallInput(input) {
|
|
3568
|
+
if (typeof input === "string") {
|
|
3569
|
+
try {
|
|
3570
|
+
const parsed = secureJsonParse(input);
|
|
3571
|
+
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
|
|
3572
|
+
return parsed;
|
|
3573
|
+
}
|
|
3574
|
+
} catch (e) {
|
|
3575
|
+
}
|
|
3576
|
+
}
|
|
3577
|
+
return input;
|
|
3578
|
+
}
|
|
3566
3579
|
|
|
3567
3580
|
// src/map-anthropic-stop-reason.ts
|
|
3568
3581
|
function mapAnthropicStopReason({
|
|
@@ -3628,9 +3641,8 @@ function sanitizeDefinition(definition) {
|
|
|
3628
3641
|
}
|
|
3629
3642
|
function sanitizeSchema(schema) {
|
|
3630
3643
|
const result = {};
|
|
3631
|
-
const schemaWithDefs = schema;
|
|
3632
3644
|
if (schema.$ref != null) {
|
|
3633
|
-
return { $ref: schema.$ref };
|
|
3645
|
+
return { $ref: schema.$ref, ...sanitizeSchemaDefinitions(schema) };
|
|
3634
3646
|
}
|
|
3635
3647
|
if (schema.$schema != null) {
|
|
3636
3648
|
result.$schema = schema.$schema;
|
|
@@ -3664,23 +3676,7 @@ function sanitizeSchema(schema) {
|
|
|
3664
3676
|
if (schema.allOf != null) {
|
|
3665
3677
|
result.allOf = schema.allOf.map(sanitizeDefinition);
|
|
3666
3678
|
}
|
|
3667
|
-
|
|
3668
|
-
result.definitions = Object.fromEntries(
|
|
3669
|
-
Object.entries(schema.definitions).map(([name, definition]) => [
|
|
3670
|
-
name,
|
|
3671
|
-
sanitizeDefinition(definition)
|
|
3672
|
-
])
|
|
3673
|
-
);
|
|
3674
|
-
}
|
|
3675
|
-
if (schemaWithDefs.$defs != null) {
|
|
3676
|
-
const resultWithDefs = result;
|
|
3677
|
-
resultWithDefs.$defs = Object.fromEntries(
|
|
3678
|
-
Object.entries(schemaWithDefs.$defs).map(([name, definition]) => [
|
|
3679
|
-
name,
|
|
3680
|
-
sanitizeDefinition(definition)
|
|
3681
|
-
])
|
|
3682
|
-
);
|
|
3683
|
-
}
|
|
3679
|
+
Object.assign(result, sanitizeSchemaDefinitions(schema));
|
|
3684
3680
|
if (schema.type === "object" || schema.properties != null) {
|
|
3685
3681
|
if (schema.properties != null) {
|
|
3686
3682
|
result.properties = Object.fromEntries(
|
|
@@ -3708,6 +3704,21 @@ ${constraintDescription}`;
|
|
|
3708
3704
|
}
|
|
3709
3705
|
return result;
|
|
3710
3706
|
}
|
|
3707
|
+
function sanitizeSchemaDefinitions(schema) {
|
|
3708
|
+
const { $defs } = schema;
|
|
3709
|
+
const sanitizeAll = (definitions) => Object.fromEntries(
|
|
3710
|
+
Object.entries(definitions).map(([name, definition]) => [
|
|
3711
|
+
name,
|
|
3712
|
+
sanitizeDefinition(definition)
|
|
3713
|
+
])
|
|
3714
|
+
);
|
|
3715
|
+
return {
|
|
3716
|
+
...schema.definitions != null && {
|
|
3717
|
+
definitions: sanitizeAll(schema.definitions)
|
|
3718
|
+
},
|
|
3719
|
+
...$defs != null && { $defs: sanitizeAll($defs) }
|
|
3720
|
+
};
|
|
3721
|
+
}
|
|
3711
3722
|
function getConstraintDescription(schema) {
|
|
3712
3723
|
const descriptions = DESCRIPTION_CONSTRAINT_KEYS.flatMap((key) => {
|
|
3713
3724
|
const value = schema[key];
|
|
@@ -3734,6 +3745,187 @@ function isPlainObject(value) {
|
|
|
3734
3745
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3735
3746
|
}
|
|
3736
3747
|
|
|
3748
|
+
// src/has-circular-schema-reference.ts
|
|
3749
|
+
var SCHEMA_MAP_KEYWORDS = /* @__PURE__ */ new Set([
|
|
3750
|
+
"dependentSchemas",
|
|
3751
|
+
"patternProperties",
|
|
3752
|
+
"properties"
|
|
3753
|
+
]);
|
|
3754
|
+
var DATA_KEYWORDS = /* @__PURE__ */ new Set(["const", "default", "enum", "examples"]);
|
|
3755
|
+
function hasCircularSchemaReference(schema) {
|
|
3756
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
3757
|
+
const visited = /* @__PURE__ */ new Set();
|
|
3758
|
+
const visit = (pointer) => {
|
|
3759
|
+
if (visiting.has(pointer)) {
|
|
3760
|
+
return true;
|
|
3761
|
+
}
|
|
3762
|
+
if (visited.has(pointer)) {
|
|
3763
|
+
return false;
|
|
3764
|
+
}
|
|
3765
|
+
const target = resolveSchemaPointer(schema, pointer);
|
|
3766
|
+
if (target === void 0) {
|
|
3767
|
+
return false;
|
|
3768
|
+
}
|
|
3769
|
+
visiting.add(pointer);
|
|
3770
|
+
const isCircular = collectLocalRefs(target).some(visit);
|
|
3771
|
+
visiting.delete(pointer);
|
|
3772
|
+
visited.add(pointer);
|
|
3773
|
+
return isCircular;
|
|
3774
|
+
};
|
|
3775
|
+
return visit("#");
|
|
3776
|
+
}
|
|
3777
|
+
function collectLocalRefs(node, refs = []) {
|
|
3778
|
+
if (Array.isArray(node)) {
|
|
3779
|
+
for (const item of node) {
|
|
3780
|
+
collectLocalRefs(item, refs);
|
|
3781
|
+
}
|
|
3782
|
+
return refs;
|
|
3783
|
+
}
|
|
3784
|
+
if (!isRecord(node)) {
|
|
3785
|
+
return refs;
|
|
3786
|
+
}
|
|
3787
|
+
for (const [key, value] of Object.entries(node)) {
|
|
3788
|
+
if (key === "$ref") {
|
|
3789
|
+
if (typeof value === "string" && value.startsWith("#")) {
|
|
3790
|
+
refs.push(value);
|
|
3791
|
+
}
|
|
3792
|
+
} else if (SCHEMA_MAP_KEYWORDS.has(key) && isRecord(value)) {
|
|
3793
|
+
for (const subschema of Object.values(value)) {
|
|
3794
|
+
collectLocalRefs(subschema, refs);
|
|
3795
|
+
}
|
|
3796
|
+
} else if (
|
|
3797
|
+
// definitions are only reached through a `$ref`:
|
|
3798
|
+
key !== "$defs" && key !== "definitions" && !DATA_KEYWORDS.has(key)
|
|
3799
|
+
) {
|
|
3800
|
+
collectLocalRefs(value, refs);
|
|
3801
|
+
}
|
|
3802
|
+
}
|
|
3803
|
+
return refs;
|
|
3804
|
+
}
|
|
3805
|
+
function resolveSchemaPointer(schema, pointer) {
|
|
3806
|
+
if (pointer === "#") {
|
|
3807
|
+
return schema;
|
|
3808
|
+
}
|
|
3809
|
+
if (!pointer.startsWith("#/")) {
|
|
3810
|
+
return void 0;
|
|
3811
|
+
}
|
|
3812
|
+
let decodedPointer;
|
|
3813
|
+
try {
|
|
3814
|
+
decodedPointer = decodeURIComponent(pointer.slice(2));
|
|
3815
|
+
} catch (e) {
|
|
3816
|
+
return void 0;
|
|
3817
|
+
}
|
|
3818
|
+
let current = schema;
|
|
3819
|
+
for (const rawSegment of decodedPointer.split("/")) {
|
|
3820
|
+
const segment = rawSegment.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
3821
|
+
if (Array.isArray(current)) {
|
|
3822
|
+
current = current[Number(segment)];
|
|
3823
|
+
} else if (isRecord(current)) {
|
|
3824
|
+
current = current[segment];
|
|
3825
|
+
} else {
|
|
3826
|
+
return void 0;
|
|
3827
|
+
}
|
|
3828
|
+
}
|
|
3829
|
+
return current;
|
|
3830
|
+
}
|
|
3831
|
+
function isRecord(value) {
|
|
3832
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3833
|
+
}
|
|
3834
|
+
|
|
3835
|
+
// src/supports-native-structured-output-schema.ts
|
|
3836
|
+
var MAX_OPTIONAL_PROPERTIES = 10;
|
|
3837
|
+
var SUBSCHEMA_KEYWORDS = [
|
|
3838
|
+
"additionalProperties",
|
|
3839
|
+
"contains",
|
|
3840
|
+
"else",
|
|
3841
|
+
"if",
|
|
3842
|
+
"items",
|
|
3843
|
+
"not",
|
|
3844
|
+
"then"
|
|
3845
|
+
];
|
|
3846
|
+
var SUBSCHEMA_LIST_KEYWORDS = ["allOf", "anyOf", "oneOf", "prefixItems"];
|
|
3847
|
+
var SUBSCHEMA_MAP_KEYWORDS = [
|
|
3848
|
+
"dependentSchemas",
|
|
3849
|
+
"patternProperties",
|
|
3850
|
+
"properties"
|
|
3851
|
+
];
|
|
3852
|
+
function supportsNativeStructuredOutputSchema(schema) {
|
|
3853
|
+
if (hasCircularSchemaReference(schema)) {
|
|
3854
|
+
return false;
|
|
3855
|
+
}
|
|
3856
|
+
const { optionalProperties, hasOpenObject } = getSchemaStats(
|
|
3857
|
+
schema,
|
|
3858
|
+
schema,
|
|
3859
|
+
/* @__PURE__ */ new Map()
|
|
3860
|
+
);
|
|
3861
|
+
return !hasOpenObject && optionalProperties <= MAX_OPTIONAL_PROPERTIES;
|
|
3862
|
+
}
|
|
3863
|
+
function getSchemaStats(node, root, refStats) {
|
|
3864
|
+
const stats = { optionalProperties: 0, hasOpenObject: false };
|
|
3865
|
+
if (!isRecord(node)) {
|
|
3866
|
+
return stats;
|
|
3867
|
+
}
|
|
3868
|
+
const add = (child) => {
|
|
3869
|
+
const childStats = getSchemaStats(child, root, refStats);
|
|
3870
|
+
stats.optionalProperties += childStats.optionalProperties;
|
|
3871
|
+
stats.hasOpenObject || (stats.hasOpenObject = childStats.hasOpenObject);
|
|
3872
|
+
};
|
|
3873
|
+
if (typeof node.$ref === "string" && node.$ref.startsWith("#")) {
|
|
3874
|
+
let resolved = refStats.get(node.$ref);
|
|
3875
|
+
if (resolved == null) {
|
|
3876
|
+
resolved = getSchemaStats(
|
|
3877
|
+
resolveSchemaPointer(root, node.$ref),
|
|
3878
|
+
root,
|
|
3879
|
+
refStats
|
|
3880
|
+
);
|
|
3881
|
+
refStats.set(node.$ref, resolved);
|
|
3882
|
+
}
|
|
3883
|
+
stats.optionalProperties += resolved.optionalProperties;
|
|
3884
|
+
stats.hasOpenObject || (stats.hasOpenObject = resolved.hasOpenObject);
|
|
3885
|
+
}
|
|
3886
|
+
if (isOpenObject(node)) {
|
|
3887
|
+
stats.hasOpenObject = true;
|
|
3888
|
+
}
|
|
3889
|
+
if (isRecord(node.properties)) {
|
|
3890
|
+
const required = new Set(Array.isArray(node.required) ? node.required : []);
|
|
3891
|
+
stats.optionalProperties += Object.keys(node.properties).filter(
|
|
3892
|
+
(name) => !required.has(name)
|
|
3893
|
+
).length;
|
|
3894
|
+
}
|
|
3895
|
+
for (const keyword of SUBSCHEMA_KEYWORDS) {
|
|
3896
|
+
const value = node[keyword];
|
|
3897
|
+
if (Array.isArray(value)) {
|
|
3898
|
+
value.forEach(add);
|
|
3899
|
+
} else {
|
|
3900
|
+
add(value);
|
|
3901
|
+
}
|
|
3902
|
+
}
|
|
3903
|
+
for (const keyword of SUBSCHEMA_LIST_KEYWORDS) {
|
|
3904
|
+
const value = node[keyword];
|
|
3905
|
+
if (Array.isArray(value)) {
|
|
3906
|
+
value.forEach(add);
|
|
3907
|
+
}
|
|
3908
|
+
}
|
|
3909
|
+
for (const keyword of SUBSCHEMA_MAP_KEYWORDS) {
|
|
3910
|
+
const value = node[keyword];
|
|
3911
|
+
if (isRecord(value)) {
|
|
3912
|
+
Object.values(value).forEach(add);
|
|
3913
|
+
}
|
|
3914
|
+
}
|
|
3915
|
+
return stats;
|
|
3916
|
+
}
|
|
3917
|
+
function isOpenObject(node) {
|
|
3918
|
+
const { additionalProperties, patternProperties, properties, type } = node;
|
|
3919
|
+
if (additionalProperties === true || isRecord(additionalProperties)) {
|
|
3920
|
+
return true;
|
|
3921
|
+
}
|
|
3922
|
+
if (isRecord(patternProperties) && Object.keys(patternProperties).length > 0) {
|
|
3923
|
+
return true;
|
|
3924
|
+
}
|
|
3925
|
+
const isObjectType = type === "object" || Array.isArray(type) && type.includes("object");
|
|
3926
|
+
return isObjectType && properties == null && additionalProperties !== false;
|
|
3927
|
+
}
|
|
3928
|
+
|
|
3737
3929
|
// src/anthropic-messages-language-model.ts
|
|
3738
3930
|
function createCitationSource(citation, citationDocuments, generateId2) {
|
|
3739
3931
|
var _a;
|
|
@@ -3807,6 +3999,31 @@ function toToolsetMemberInput({
|
|
|
3807
3999
|
...typeof input === "object" && input !== null && !Array.isArray(input) ? input : {}
|
|
3808
4000
|
};
|
|
3809
4001
|
}
|
|
4002
|
+
var TEXT_OR_THINKING_BLOCK_TYPES = /* @__PURE__ */ new Set([
|
|
4003
|
+
"redacted_thinking",
|
|
4004
|
+
"text",
|
|
4005
|
+
"thinking"
|
|
4006
|
+
]);
|
|
4007
|
+
function isContinuationStopReason(stopReason) {
|
|
4008
|
+
return stopReason === "pause_turn" || stopReason === "tool_use";
|
|
4009
|
+
}
|
|
4010
|
+
function getJsonResponseToolFallbackText(content, stopReason) {
|
|
4011
|
+
const text = /* @__PURE__ */ new Set();
|
|
4012
|
+
if (isContinuationStopReason(stopReason)) {
|
|
4013
|
+
return text;
|
|
4014
|
+
}
|
|
4015
|
+
for (const part of content) {
|
|
4016
|
+
if (part.type === "tool_use" && part.name === "json") {
|
|
4017
|
+
return /* @__PURE__ */ new Set();
|
|
4018
|
+
}
|
|
4019
|
+
if (part.type === "text") {
|
|
4020
|
+
text.add(part);
|
|
4021
|
+
} else if (!TEXT_OR_THINKING_BLOCK_TYPES.has(part.type)) {
|
|
4022
|
+
text.clear();
|
|
4023
|
+
}
|
|
4024
|
+
}
|
|
4025
|
+
return text;
|
|
4026
|
+
}
|
|
3810
4027
|
var AnthropicMessagesLanguageModel = class {
|
|
3811
4028
|
constructor(modelId, config) {
|
|
3812
4029
|
this.specificationVersion = "v3";
|
|
@@ -3951,10 +4168,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
3951
4168
|
}
|
|
3952
4169
|
const isAnthropicModel = isKnownModel || this.modelId.includes("claude-");
|
|
3953
4170
|
const supportsStructuredOutput = ((_b = this.config.supportsNativeStructuredOutput) != null ? _b : true) && modelSupportsStructuredOutput;
|
|
4171
|
+
const supportsResponseSchema = (responseFormat == null ? void 0 : responseFormat.type) !== "json" || responseFormat.schema == null || supportsNativeStructuredOutputSchema(responseFormat.schema);
|
|
3954
4172
|
const supportsStrictTools = ((_c = this.config.supportsStrictTools) != null ? _c : true) && modelSupportsStructuredOutput;
|
|
3955
4173
|
const structureOutputMode = (_d = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _d : "auto";
|
|
3956
|
-
let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
|
|
3957
|
-
if (!useStructuredOutput && rejectsForcedToolUse &&
|
|
4174
|
+
let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput && supportsResponseSchema;
|
|
4175
|
+
if (!useStructuredOutput && rejectsForcedToolUse && modelSupportsStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && supportsNativeStructuredOutputSchema(responseFormat.schema)) {
|
|
3958
4176
|
warnings.push({
|
|
3959
4177
|
type: "unsupported",
|
|
3960
4178
|
feature: "providerOptions.anthropic.structuredOutputMode",
|
|
@@ -4326,7 +4544,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4326
4544
|
betas: toolsBetas
|
|
4327
4545
|
} = await prepareTools(
|
|
4328
4546
|
jsonResponseTool != null ? {
|
|
4329
|
-
tools: [
|
|
4547
|
+
tools: [
|
|
4548
|
+
...(tools != null ? tools : []).filter(
|
|
4549
|
+
(tool) => (toolChoice == null ? void 0 : toolChoice.type) !== "none" && ((toolChoice == null ? void 0 : toolChoice.type) !== "tool" || tool.name === toolChoice.toolName)
|
|
4550
|
+
),
|
|
4551
|
+
jsonResponseTool
|
|
4552
|
+
],
|
|
4330
4553
|
toolChoice: { type: "required" },
|
|
4331
4554
|
disableParallelToolUse: true,
|
|
4332
4555
|
cacheControlValidator,
|
|
@@ -4460,10 +4683,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4460
4683
|
const mcpToolCalls = {};
|
|
4461
4684
|
const serverToolCalls = {};
|
|
4462
4685
|
let isJsonResponseFromTool = false;
|
|
4686
|
+
const jsonResponseToolFallbackText = usesJsonResponseTool ? getJsonResponseToolFallbackText(response.content, response.stop_reason) : void 0;
|
|
4463
4687
|
for (const part of response.content) {
|
|
4464
4688
|
switch (part.type) {
|
|
4465
4689
|
case "text": {
|
|
4466
|
-
if (
|
|
4690
|
+
if (jsonResponseToolFallbackText == null || jsonResponseToolFallbackText.has(part)) {
|
|
4467
4691
|
const webSearchCitations = (_a = part.citations) == null ? void 0 : _a.filter(
|
|
4468
4692
|
(citation) => citation.type === "web_search_result_location"
|
|
4469
4693
|
);
|
|
@@ -4999,6 +5223,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4999
5223
|
let safeguardResults;
|
|
5000
5224
|
let container = null;
|
|
5001
5225
|
let isJsonResponseFromTool = false;
|
|
5226
|
+
const heldBackText = [];
|
|
5227
|
+
const heldBackTextByIndex = /* @__PURE__ */ new Map();
|
|
5228
|
+
const createTextEnd = (id, citations) => ({
|
|
5229
|
+
type: "text-end",
|
|
5230
|
+
id,
|
|
5231
|
+
...citations.length > 0 && {
|
|
5232
|
+
providerMetadata: { anthropic: { citations } }
|
|
5233
|
+
}
|
|
5234
|
+
});
|
|
5002
5235
|
let isMessageOpen = false;
|
|
5003
5236
|
let activeMessageId;
|
|
5004
5237
|
let hasInvalidMessageSequence = false;
|
|
@@ -5010,7 +5243,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5010
5243
|
controller.enqueue({ type: "stream-start", warnings });
|
|
5011
5244
|
},
|
|
5012
5245
|
transform(chunk, controller) {
|
|
5013
|
-
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
5246
|
+
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
5014
5247
|
if (hasInvalidMessageSequence) {
|
|
5015
5248
|
return;
|
|
5016
5249
|
}
|
|
@@ -5033,9 +5266,19 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5033
5266
|
return;
|
|
5034
5267
|
}
|
|
5035
5268
|
blockType = contentBlockType;
|
|
5269
|
+
if (!TEXT_OR_THINKING_BLOCK_TYPES.has(contentBlockType)) {
|
|
5270
|
+
heldBackText.length = 0;
|
|
5271
|
+
}
|
|
5036
5272
|
switch (contentBlockType) {
|
|
5037
5273
|
case "text": {
|
|
5038
5274
|
if (usesJsonResponseTool) {
|
|
5275
|
+
const entry = {
|
|
5276
|
+
id: String(value.index),
|
|
5277
|
+
text: "",
|
|
5278
|
+
citations: []
|
|
5279
|
+
};
|
|
5280
|
+
heldBackText.push(entry);
|
|
5281
|
+
heldBackTextByIndex.set(value.index, entry);
|
|
5039
5282
|
return;
|
|
5040
5283
|
}
|
|
5041
5284
|
contentBlocks[value.index] = {
|
|
@@ -5490,17 +5733,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5490
5733
|
const contentBlock = contentBlocks[value.index];
|
|
5491
5734
|
switch (contentBlock.type) {
|
|
5492
5735
|
case "text": {
|
|
5493
|
-
controller.enqueue(
|
|
5494
|
-
|
|
5495
|
-
|
|
5496
|
-
|
|
5497
|
-
|
|
5498
|
-
|
|
5499
|
-
citations: contentBlock.citations
|
|
5500
|
-
}
|
|
5501
|
-
}
|
|
5502
|
-
}
|
|
5503
|
-
});
|
|
5736
|
+
controller.enqueue(
|
|
5737
|
+
createTextEnd(
|
|
5738
|
+
String(value.index),
|
|
5739
|
+
contentBlock.citations
|
|
5740
|
+
)
|
|
5741
|
+
);
|
|
5504
5742
|
break;
|
|
5505
5743
|
}
|
|
5506
5744
|
case "reasoning": {
|
|
@@ -5586,6 +5824,10 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5586
5824
|
switch (deltaType) {
|
|
5587
5825
|
case "text_delta": {
|
|
5588
5826
|
if (usesJsonResponseTool) {
|
|
5827
|
+
const entry = heldBackTextByIndex.get(value.index);
|
|
5828
|
+
if (entry != null) {
|
|
5829
|
+
entry.text += value.delta.text;
|
|
5830
|
+
}
|
|
5589
5831
|
return;
|
|
5590
5832
|
}
|
|
5591
5833
|
controller.enqueue({
|
|
@@ -5667,8 +5909,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5667
5909
|
case "citations_delta": {
|
|
5668
5910
|
const citation = value.delta.citation;
|
|
5669
5911
|
const contentBlock = contentBlocks[value.index];
|
|
5670
|
-
if (
|
|
5671
|
-
contentBlock.
|
|
5912
|
+
if (citation.type === "web_search_result_location") {
|
|
5913
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) === "text") {
|
|
5914
|
+
contentBlock.citations.push(citation);
|
|
5915
|
+
}
|
|
5916
|
+
(_e = heldBackTextByIndex.get(value.index)) == null ? void 0 : _e.citations.push(citation);
|
|
5672
5917
|
}
|
|
5673
5918
|
const source = createCitationSource(
|
|
5674
5919
|
citation,
|
|
@@ -5705,13 +5950,14 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5705
5950
|
}
|
|
5706
5951
|
isMessageOpen = true;
|
|
5707
5952
|
activeMessageId = value.message.id;
|
|
5953
|
+
heldBackTextByIndex.clear();
|
|
5708
5954
|
usage.input_tokens = value.message.usage.input_tokens;
|
|
5709
|
-
usage.cache_read_input_tokens = (
|
|
5710
|
-
usage.cache_creation_input_tokens = (
|
|
5955
|
+
usage.cache_read_input_tokens = (_f = value.message.usage.cache_read_input_tokens) != null ? _f : 0;
|
|
5956
|
+
usage.cache_creation_input_tokens = (_g = value.message.usage.cache_creation_input_tokens) != null ? _g : 0;
|
|
5711
5957
|
rawUsage = {
|
|
5712
5958
|
...value.message.usage
|
|
5713
5959
|
};
|
|
5714
|
-
cacheCreationInputTokens = (
|
|
5960
|
+
cacheCreationInputTokens = (_h = value.message.usage.cache_creation_input_tokens) != null ? _h : null;
|
|
5715
5961
|
if (value.message.input_transformations != null) {
|
|
5716
5962
|
inputTransformations = value.message.input_transformations;
|
|
5717
5963
|
}
|
|
@@ -5733,8 +5979,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5733
5979
|
}
|
|
5734
5980
|
controller.enqueue({
|
|
5735
5981
|
type: "response-metadata",
|
|
5736
|
-
id: (
|
|
5737
|
-
modelId: (
|
|
5982
|
+
id: (_i = value.message.id) != null ? _i : void 0,
|
|
5983
|
+
modelId: (_j = value.message.model) != null ? _j : void 0
|
|
5738
5984
|
});
|
|
5739
5985
|
if (value.message.content != null) {
|
|
5740
5986
|
for (let contentIndex = 0; contentIndex < value.message.content.length; contentIndex++) {
|
|
@@ -5746,7 +5992,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5746
5992
|
id: part.id,
|
|
5747
5993
|
toolName: part.name
|
|
5748
5994
|
});
|
|
5749
|
-
const inputStr = JSON.stringify((
|
|
5995
|
+
const inputStr = JSON.stringify((_k = part.input) != null ? _k : {});
|
|
5750
5996
|
controller.enqueue({
|
|
5751
5997
|
type: "tool-input-delta",
|
|
5752
5998
|
id: part.id,
|
|
@@ -5797,18 +6043,18 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5797
6043
|
finishReason: value.delta.stop_reason,
|
|
5798
6044
|
isJsonResponseFromTool
|
|
5799
6045
|
}),
|
|
5800
|
-
raw: (
|
|
6046
|
+
raw: (_l = value.delta.stop_reason) != null ? _l : void 0
|
|
5801
6047
|
};
|
|
5802
|
-
stopSequence = (
|
|
6048
|
+
stopSequence = (_m = value.delta.stop_sequence) != null ? _m : null;
|
|
5803
6049
|
stopDetails = mapAnthropicStopDetails(value.delta.stop_details);
|
|
5804
6050
|
container = value.delta.container != null ? {
|
|
5805
6051
|
expiresAt: value.delta.container.expires_at,
|
|
5806
6052
|
id: value.delta.container.id,
|
|
5807
|
-
skills: (
|
|
6053
|
+
skills: (_o = (_n = value.delta.container.skills) == null ? void 0 : _n.map((skill) => ({
|
|
5808
6054
|
type: skill.type,
|
|
5809
6055
|
skillId: skill.skill_id,
|
|
5810
6056
|
version: skill.version
|
|
5811
|
-
}))) != null ?
|
|
6057
|
+
}))) != null ? _o : null
|
|
5812
6058
|
} : null;
|
|
5813
6059
|
if (value.context_management) {
|
|
5814
6060
|
contextManagement = mapAnthropicResponseContextManagement(
|
|
@@ -5860,6 +6106,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5860
6106
|
if (usedCustomProviderKey && providerOptionsName !== "anthropic") {
|
|
5861
6107
|
providerMetadata[providerOptionsName] = anthropicMetadata;
|
|
5862
6108
|
}
|
|
6109
|
+
if (!isJsonResponseFromTool && !isContinuationStopReason(finishReason.raw)) {
|
|
6110
|
+
for (const { id, text, citations } of heldBackText) {
|
|
6111
|
+
if (text.length === 0) continue;
|
|
6112
|
+
controller.enqueue({ type: "text-start", id });
|
|
6113
|
+
controller.enqueue({ type: "text-delta", id, delta: text });
|
|
6114
|
+
controller.enqueue(createTextEnd(id, citations));
|
|
6115
|
+
}
|
|
6116
|
+
}
|
|
6117
|
+
heldBackText.length = 0;
|
|
5863
6118
|
controller.enqueue({
|
|
5864
6119
|
type: "finish",
|
|
5865
6120
|
finishReason,
|
|
@@ -6752,6 +7007,7 @@ export {
|
|
|
6752
7007
|
anthropicTools,
|
|
6753
7008
|
getModelCapabilities,
|
|
6754
7009
|
prepareTools,
|
|
6755
|
-
sanitizeJsonSchema
|
|
7010
|
+
sanitizeJsonSchema,
|
|
7011
|
+
supportsNativeStructuredOutputSchema
|
|
6756
7012
|
};
|
|
6757
7013
|
//# sourceMappingURL=index.mjs.map
|