@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
|
@@ -1176,4 +1176,15 @@ declare function prepareTools({ tools, toolChoice, disableParallelToolUse, cache
|
|
|
1176
1176
|
*/
|
|
1177
1177
|
declare function sanitizeJsonSchema(schema: JSONSchema7): JSONSchema7;
|
|
1178
1178
|
|
|
1179
|
-
|
|
1179
|
+
/**
|
|
1180
|
+
* Whether Anthropic's native structured outputs (`output_config.format`) can
|
|
1181
|
+
* represent a schema without rejecting it or losing data.
|
|
1182
|
+
*
|
|
1183
|
+
* Rejected: self-referencing schemas, and schemas with too many optional
|
|
1184
|
+
* properties. Lossy: open objects (maps, free-form objects), which native
|
|
1185
|
+
* structured outputs close with `additionalProperties: false`, so the model
|
|
1186
|
+
* can only return `{}` for them.
|
|
1187
|
+
*/
|
|
1188
|
+
declare function supportsNativeStructuredOutputSchema(schema: JSONSchema7): boolean;
|
|
1189
|
+
|
|
1190
|
+
export { AnthropicMessagesLanguageModel, type AnthropicMessagesModelId, anthropicTools, getModelCapabilities, prepareTools, sanitizeJsonSchema, supportsNativeStructuredOutputSchema };
|
package/dist/internal/index.d.ts
CHANGED
|
@@ -1176,4 +1176,15 @@ declare function prepareTools({ tools, toolChoice, disableParallelToolUse, cache
|
|
|
1176
1176
|
*/
|
|
1177
1177
|
declare function sanitizeJsonSchema(schema: JSONSchema7): JSONSchema7;
|
|
1178
1178
|
|
|
1179
|
-
|
|
1179
|
+
/**
|
|
1180
|
+
* Whether Anthropic's native structured outputs (`output_config.format`) can
|
|
1181
|
+
* represent a schema without rejecting it or losing data.
|
|
1182
|
+
*
|
|
1183
|
+
* Rejected: self-referencing schemas, and schemas with too many optional
|
|
1184
|
+
* properties. Lossy: open objects (maps, free-form objects), which native
|
|
1185
|
+
* structured outputs close with `additionalProperties: false`, so the model
|
|
1186
|
+
* can only return `{}` for them.
|
|
1187
|
+
*/
|
|
1188
|
+
declare function supportsNativeStructuredOutputSchema(schema: JSONSchema7): boolean;
|
|
1189
|
+
|
|
1190
|
+
export { AnthropicMessagesLanguageModel, type AnthropicMessagesModelId, anthropicTools, getModelCapabilities, prepareTools, sanitizeJsonSchema, supportsNativeStructuredOutputSchema };
|
package/dist/internal/index.js
CHANGED
|
@@ -24,7 +24,8 @@ __export(index_exports, {
|
|
|
24
24
|
anthropicTools: () => anthropicTools,
|
|
25
25
|
getModelCapabilities: () => getModelCapabilities,
|
|
26
26
|
prepareTools: () => prepareTools,
|
|
27
|
-
sanitizeJsonSchema: () => sanitizeJsonSchema
|
|
27
|
+
sanitizeJsonSchema: () => sanitizeJsonSchema,
|
|
28
|
+
supportsNativeStructuredOutputSchema: () => supportsNativeStructuredOutputSchema
|
|
28
29
|
});
|
|
29
30
|
module.exports = __toCommonJS(index_exports);
|
|
30
31
|
|
|
@@ -2791,7 +2792,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
2791
2792
|
case "json":
|
|
2792
2793
|
case "error-json":
|
|
2793
2794
|
default:
|
|
2794
|
-
contentValue =
|
|
2795
|
+
contentValue = (0, import_provider_utils16.stringifyWithBigIntAndCycles)(output.value);
|
|
2795
2796
|
break;
|
|
2796
2797
|
}
|
|
2797
2798
|
const toolsetName = getAnthropicToolsetName({
|
|
@@ -3029,7 +3030,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
3029
3030
|
type: "tool_use",
|
|
3030
3031
|
id: part.toolCallId,
|
|
3031
3032
|
name: part.toolName,
|
|
3032
|
-
input: part.input,
|
|
3033
|
+
input: parseToolCallInput(part.input),
|
|
3033
3034
|
...caller && { caller },
|
|
3034
3035
|
cache_control: cacheControl
|
|
3035
3036
|
});
|
|
@@ -3514,6 +3515,18 @@ function getAnthropicCaller(providerOptions) {
|
|
|
3514
3515
|
}
|
|
3515
3516
|
return (caller == null ? void 0 : caller.type) === "direct" ? { type: "direct" } : void 0;
|
|
3516
3517
|
}
|
|
3518
|
+
function parseToolCallInput(input) {
|
|
3519
|
+
if (typeof input === "string") {
|
|
3520
|
+
try {
|
|
3521
|
+
const parsed = (0, import_provider_utils16.secureJsonParse)(input);
|
|
3522
|
+
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
|
|
3523
|
+
return parsed;
|
|
3524
|
+
}
|
|
3525
|
+
} catch (e) {
|
|
3526
|
+
}
|
|
3527
|
+
}
|
|
3528
|
+
return input;
|
|
3529
|
+
}
|
|
3517
3530
|
|
|
3518
3531
|
// src/map-anthropic-stop-reason.ts
|
|
3519
3532
|
function mapAnthropicStopReason({
|
|
@@ -3579,9 +3592,8 @@ function sanitizeDefinition(definition) {
|
|
|
3579
3592
|
}
|
|
3580
3593
|
function sanitizeSchema(schema) {
|
|
3581
3594
|
const result = {};
|
|
3582
|
-
const schemaWithDefs = schema;
|
|
3583
3595
|
if (schema.$ref != null) {
|
|
3584
|
-
return { $ref: schema.$ref };
|
|
3596
|
+
return { $ref: schema.$ref, ...sanitizeSchemaDefinitions(schema) };
|
|
3585
3597
|
}
|
|
3586
3598
|
if (schema.$schema != null) {
|
|
3587
3599
|
result.$schema = schema.$schema;
|
|
@@ -3615,23 +3627,7 @@ function sanitizeSchema(schema) {
|
|
|
3615
3627
|
if (schema.allOf != null) {
|
|
3616
3628
|
result.allOf = schema.allOf.map(sanitizeDefinition);
|
|
3617
3629
|
}
|
|
3618
|
-
|
|
3619
|
-
result.definitions = Object.fromEntries(
|
|
3620
|
-
Object.entries(schema.definitions).map(([name, definition]) => [
|
|
3621
|
-
name,
|
|
3622
|
-
sanitizeDefinition(definition)
|
|
3623
|
-
])
|
|
3624
|
-
);
|
|
3625
|
-
}
|
|
3626
|
-
if (schemaWithDefs.$defs != null) {
|
|
3627
|
-
const resultWithDefs = result;
|
|
3628
|
-
resultWithDefs.$defs = Object.fromEntries(
|
|
3629
|
-
Object.entries(schemaWithDefs.$defs).map(([name, definition]) => [
|
|
3630
|
-
name,
|
|
3631
|
-
sanitizeDefinition(definition)
|
|
3632
|
-
])
|
|
3633
|
-
);
|
|
3634
|
-
}
|
|
3630
|
+
Object.assign(result, sanitizeSchemaDefinitions(schema));
|
|
3635
3631
|
if (schema.type === "object" || schema.properties != null) {
|
|
3636
3632
|
if (schema.properties != null) {
|
|
3637
3633
|
result.properties = Object.fromEntries(
|
|
@@ -3659,6 +3655,21 @@ ${constraintDescription}`;
|
|
|
3659
3655
|
}
|
|
3660
3656
|
return result;
|
|
3661
3657
|
}
|
|
3658
|
+
function sanitizeSchemaDefinitions(schema) {
|
|
3659
|
+
const { $defs } = schema;
|
|
3660
|
+
const sanitizeAll = (definitions) => Object.fromEntries(
|
|
3661
|
+
Object.entries(definitions).map(([name, definition]) => [
|
|
3662
|
+
name,
|
|
3663
|
+
sanitizeDefinition(definition)
|
|
3664
|
+
])
|
|
3665
|
+
);
|
|
3666
|
+
return {
|
|
3667
|
+
...schema.definitions != null && {
|
|
3668
|
+
definitions: sanitizeAll(schema.definitions)
|
|
3669
|
+
},
|
|
3670
|
+
...$defs != null && { $defs: sanitizeAll($defs) }
|
|
3671
|
+
};
|
|
3672
|
+
}
|
|
3662
3673
|
function getConstraintDescription(schema) {
|
|
3663
3674
|
const descriptions = DESCRIPTION_CONSTRAINT_KEYS.flatMap((key) => {
|
|
3664
3675
|
const value = schema[key];
|
|
@@ -3685,6 +3696,187 @@ function isPlainObject(value) {
|
|
|
3685
3696
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3686
3697
|
}
|
|
3687
3698
|
|
|
3699
|
+
// src/has-circular-schema-reference.ts
|
|
3700
|
+
var SCHEMA_MAP_KEYWORDS = /* @__PURE__ */ new Set([
|
|
3701
|
+
"dependentSchemas",
|
|
3702
|
+
"patternProperties",
|
|
3703
|
+
"properties"
|
|
3704
|
+
]);
|
|
3705
|
+
var DATA_KEYWORDS = /* @__PURE__ */ new Set(["const", "default", "enum", "examples"]);
|
|
3706
|
+
function hasCircularSchemaReference(schema) {
|
|
3707
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
3708
|
+
const visited = /* @__PURE__ */ new Set();
|
|
3709
|
+
const visit = (pointer) => {
|
|
3710
|
+
if (visiting.has(pointer)) {
|
|
3711
|
+
return true;
|
|
3712
|
+
}
|
|
3713
|
+
if (visited.has(pointer)) {
|
|
3714
|
+
return false;
|
|
3715
|
+
}
|
|
3716
|
+
const target = resolveSchemaPointer(schema, pointer);
|
|
3717
|
+
if (target === void 0) {
|
|
3718
|
+
return false;
|
|
3719
|
+
}
|
|
3720
|
+
visiting.add(pointer);
|
|
3721
|
+
const isCircular = collectLocalRefs(target).some(visit);
|
|
3722
|
+
visiting.delete(pointer);
|
|
3723
|
+
visited.add(pointer);
|
|
3724
|
+
return isCircular;
|
|
3725
|
+
};
|
|
3726
|
+
return visit("#");
|
|
3727
|
+
}
|
|
3728
|
+
function collectLocalRefs(node, refs = []) {
|
|
3729
|
+
if (Array.isArray(node)) {
|
|
3730
|
+
for (const item of node) {
|
|
3731
|
+
collectLocalRefs(item, refs);
|
|
3732
|
+
}
|
|
3733
|
+
return refs;
|
|
3734
|
+
}
|
|
3735
|
+
if (!isRecord(node)) {
|
|
3736
|
+
return refs;
|
|
3737
|
+
}
|
|
3738
|
+
for (const [key, value] of Object.entries(node)) {
|
|
3739
|
+
if (key === "$ref") {
|
|
3740
|
+
if (typeof value === "string" && value.startsWith("#")) {
|
|
3741
|
+
refs.push(value);
|
|
3742
|
+
}
|
|
3743
|
+
} else if (SCHEMA_MAP_KEYWORDS.has(key) && isRecord(value)) {
|
|
3744
|
+
for (const subschema of Object.values(value)) {
|
|
3745
|
+
collectLocalRefs(subschema, refs);
|
|
3746
|
+
}
|
|
3747
|
+
} else if (
|
|
3748
|
+
// definitions are only reached through a `$ref`:
|
|
3749
|
+
key !== "$defs" && key !== "definitions" && !DATA_KEYWORDS.has(key)
|
|
3750
|
+
) {
|
|
3751
|
+
collectLocalRefs(value, refs);
|
|
3752
|
+
}
|
|
3753
|
+
}
|
|
3754
|
+
return refs;
|
|
3755
|
+
}
|
|
3756
|
+
function resolveSchemaPointer(schema, pointer) {
|
|
3757
|
+
if (pointer === "#") {
|
|
3758
|
+
return schema;
|
|
3759
|
+
}
|
|
3760
|
+
if (!pointer.startsWith("#/")) {
|
|
3761
|
+
return void 0;
|
|
3762
|
+
}
|
|
3763
|
+
let decodedPointer;
|
|
3764
|
+
try {
|
|
3765
|
+
decodedPointer = decodeURIComponent(pointer.slice(2));
|
|
3766
|
+
} catch (e) {
|
|
3767
|
+
return void 0;
|
|
3768
|
+
}
|
|
3769
|
+
let current = schema;
|
|
3770
|
+
for (const rawSegment of decodedPointer.split("/")) {
|
|
3771
|
+
const segment = rawSegment.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
3772
|
+
if (Array.isArray(current)) {
|
|
3773
|
+
current = current[Number(segment)];
|
|
3774
|
+
} else if (isRecord(current)) {
|
|
3775
|
+
current = current[segment];
|
|
3776
|
+
} else {
|
|
3777
|
+
return void 0;
|
|
3778
|
+
}
|
|
3779
|
+
}
|
|
3780
|
+
return current;
|
|
3781
|
+
}
|
|
3782
|
+
function isRecord(value) {
|
|
3783
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3784
|
+
}
|
|
3785
|
+
|
|
3786
|
+
// src/supports-native-structured-output-schema.ts
|
|
3787
|
+
var MAX_OPTIONAL_PROPERTIES = 10;
|
|
3788
|
+
var SUBSCHEMA_KEYWORDS = [
|
|
3789
|
+
"additionalProperties",
|
|
3790
|
+
"contains",
|
|
3791
|
+
"else",
|
|
3792
|
+
"if",
|
|
3793
|
+
"items",
|
|
3794
|
+
"not",
|
|
3795
|
+
"then"
|
|
3796
|
+
];
|
|
3797
|
+
var SUBSCHEMA_LIST_KEYWORDS = ["allOf", "anyOf", "oneOf", "prefixItems"];
|
|
3798
|
+
var SUBSCHEMA_MAP_KEYWORDS = [
|
|
3799
|
+
"dependentSchemas",
|
|
3800
|
+
"patternProperties",
|
|
3801
|
+
"properties"
|
|
3802
|
+
];
|
|
3803
|
+
function supportsNativeStructuredOutputSchema(schema) {
|
|
3804
|
+
if (hasCircularSchemaReference(schema)) {
|
|
3805
|
+
return false;
|
|
3806
|
+
}
|
|
3807
|
+
const { optionalProperties, hasOpenObject } = getSchemaStats(
|
|
3808
|
+
schema,
|
|
3809
|
+
schema,
|
|
3810
|
+
/* @__PURE__ */ new Map()
|
|
3811
|
+
);
|
|
3812
|
+
return !hasOpenObject && optionalProperties <= MAX_OPTIONAL_PROPERTIES;
|
|
3813
|
+
}
|
|
3814
|
+
function getSchemaStats(node, root, refStats) {
|
|
3815
|
+
const stats = { optionalProperties: 0, hasOpenObject: false };
|
|
3816
|
+
if (!isRecord(node)) {
|
|
3817
|
+
return stats;
|
|
3818
|
+
}
|
|
3819
|
+
const add = (child) => {
|
|
3820
|
+
const childStats = getSchemaStats(child, root, refStats);
|
|
3821
|
+
stats.optionalProperties += childStats.optionalProperties;
|
|
3822
|
+
stats.hasOpenObject || (stats.hasOpenObject = childStats.hasOpenObject);
|
|
3823
|
+
};
|
|
3824
|
+
if (typeof node.$ref === "string" && node.$ref.startsWith("#")) {
|
|
3825
|
+
let resolved = refStats.get(node.$ref);
|
|
3826
|
+
if (resolved == null) {
|
|
3827
|
+
resolved = getSchemaStats(
|
|
3828
|
+
resolveSchemaPointer(root, node.$ref),
|
|
3829
|
+
root,
|
|
3830
|
+
refStats
|
|
3831
|
+
);
|
|
3832
|
+
refStats.set(node.$ref, resolved);
|
|
3833
|
+
}
|
|
3834
|
+
stats.optionalProperties += resolved.optionalProperties;
|
|
3835
|
+
stats.hasOpenObject || (stats.hasOpenObject = resolved.hasOpenObject);
|
|
3836
|
+
}
|
|
3837
|
+
if (isOpenObject(node)) {
|
|
3838
|
+
stats.hasOpenObject = true;
|
|
3839
|
+
}
|
|
3840
|
+
if (isRecord(node.properties)) {
|
|
3841
|
+
const required = new Set(Array.isArray(node.required) ? node.required : []);
|
|
3842
|
+
stats.optionalProperties += Object.keys(node.properties).filter(
|
|
3843
|
+
(name) => !required.has(name)
|
|
3844
|
+
).length;
|
|
3845
|
+
}
|
|
3846
|
+
for (const keyword of SUBSCHEMA_KEYWORDS) {
|
|
3847
|
+
const value = node[keyword];
|
|
3848
|
+
if (Array.isArray(value)) {
|
|
3849
|
+
value.forEach(add);
|
|
3850
|
+
} else {
|
|
3851
|
+
add(value);
|
|
3852
|
+
}
|
|
3853
|
+
}
|
|
3854
|
+
for (const keyword of SUBSCHEMA_LIST_KEYWORDS) {
|
|
3855
|
+
const value = node[keyword];
|
|
3856
|
+
if (Array.isArray(value)) {
|
|
3857
|
+
value.forEach(add);
|
|
3858
|
+
}
|
|
3859
|
+
}
|
|
3860
|
+
for (const keyword of SUBSCHEMA_MAP_KEYWORDS) {
|
|
3861
|
+
const value = node[keyword];
|
|
3862
|
+
if (isRecord(value)) {
|
|
3863
|
+
Object.values(value).forEach(add);
|
|
3864
|
+
}
|
|
3865
|
+
}
|
|
3866
|
+
return stats;
|
|
3867
|
+
}
|
|
3868
|
+
function isOpenObject(node) {
|
|
3869
|
+
const { additionalProperties, patternProperties, properties, type } = node;
|
|
3870
|
+
if (additionalProperties === true || isRecord(additionalProperties)) {
|
|
3871
|
+
return true;
|
|
3872
|
+
}
|
|
3873
|
+
if (isRecord(patternProperties) && Object.keys(patternProperties).length > 0) {
|
|
3874
|
+
return true;
|
|
3875
|
+
}
|
|
3876
|
+
const isObjectType = type === "object" || Array.isArray(type) && type.includes("object");
|
|
3877
|
+
return isObjectType && properties == null && additionalProperties !== false;
|
|
3878
|
+
}
|
|
3879
|
+
|
|
3688
3880
|
// src/anthropic-messages-language-model.ts
|
|
3689
3881
|
function createCitationSource(citation, citationDocuments, generateId2) {
|
|
3690
3882
|
var _a;
|
|
@@ -3758,6 +3950,31 @@ function toToolsetMemberInput({
|
|
|
3758
3950
|
...typeof input === "object" && input !== null && !Array.isArray(input) ? input : {}
|
|
3759
3951
|
};
|
|
3760
3952
|
}
|
|
3953
|
+
var TEXT_OR_THINKING_BLOCK_TYPES = /* @__PURE__ */ new Set([
|
|
3954
|
+
"redacted_thinking",
|
|
3955
|
+
"text",
|
|
3956
|
+
"thinking"
|
|
3957
|
+
]);
|
|
3958
|
+
function isContinuationStopReason(stopReason) {
|
|
3959
|
+
return stopReason === "pause_turn" || stopReason === "tool_use";
|
|
3960
|
+
}
|
|
3961
|
+
function getJsonResponseToolFallbackText(content, stopReason) {
|
|
3962
|
+
const text = /* @__PURE__ */ new Set();
|
|
3963
|
+
if (isContinuationStopReason(stopReason)) {
|
|
3964
|
+
return text;
|
|
3965
|
+
}
|
|
3966
|
+
for (const part of content) {
|
|
3967
|
+
if (part.type === "tool_use" && part.name === "json") {
|
|
3968
|
+
return /* @__PURE__ */ new Set();
|
|
3969
|
+
}
|
|
3970
|
+
if (part.type === "text") {
|
|
3971
|
+
text.add(part);
|
|
3972
|
+
} else if (!TEXT_OR_THINKING_BLOCK_TYPES.has(part.type)) {
|
|
3973
|
+
text.clear();
|
|
3974
|
+
}
|
|
3975
|
+
}
|
|
3976
|
+
return text;
|
|
3977
|
+
}
|
|
3761
3978
|
var AnthropicMessagesLanguageModel = class {
|
|
3762
3979
|
constructor(modelId, config) {
|
|
3763
3980
|
this.specificationVersion = "v3";
|
|
@@ -3902,10 +4119,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
3902
4119
|
}
|
|
3903
4120
|
const isAnthropicModel = isKnownModel || this.modelId.includes("claude-");
|
|
3904
4121
|
const supportsStructuredOutput = ((_b = this.config.supportsNativeStructuredOutput) != null ? _b : true) && modelSupportsStructuredOutput;
|
|
4122
|
+
const supportsResponseSchema = (responseFormat == null ? void 0 : responseFormat.type) !== "json" || responseFormat.schema == null || supportsNativeStructuredOutputSchema(responseFormat.schema);
|
|
3905
4123
|
const supportsStrictTools = ((_c = this.config.supportsStrictTools) != null ? _c : true) && modelSupportsStructuredOutput;
|
|
3906
4124
|
const structureOutputMode = (_d = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _d : "auto";
|
|
3907
|
-
let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
|
|
3908
|
-
if (!useStructuredOutput && rejectsForcedToolUse &&
|
|
4125
|
+
let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput && supportsResponseSchema;
|
|
4126
|
+
if (!useStructuredOutput && rejectsForcedToolUse && modelSupportsStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && supportsNativeStructuredOutputSchema(responseFormat.schema)) {
|
|
3909
4127
|
warnings.push({
|
|
3910
4128
|
type: "unsupported",
|
|
3911
4129
|
feature: "providerOptions.anthropic.structuredOutputMode",
|
|
@@ -4277,7 +4495,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4277
4495
|
betas: toolsBetas
|
|
4278
4496
|
} = await prepareTools(
|
|
4279
4497
|
jsonResponseTool != null ? {
|
|
4280
|
-
tools: [
|
|
4498
|
+
tools: [
|
|
4499
|
+
...(tools != null ? tools : []).filter(
|
|
4500
|
+
(tool) => (toolChoice == null ? void 0 : toolChoice.type) !== "none" && ((toolChoice == null ? void 0 : toolChoice.type) !== "tool" || tool.name === toolChoice.toolName)
|
|
4501
|
+
),
|
|
4502
|
+
jsonResponseTool
|
|
4503
|
+
],
|
|
4281
4504
|
toolChoice: { type: "required" },
|
|
4282
4505
|
disableParallelToolUse: true,
|
|
4283
4506
|
cacheControlValidator,
|
|
@@ -4411,10 +4634,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4411
4634
|
const mcpToolCalls = {};
|
|
4412
4635
|
const serverToolCalls = {};
|
|
4413
4636
|
let isJsonResponseFromTool = false;
|
|
4637
|
+
const jsonResponseToolFallbackText = usesJsonResponseTool ? getJsonResponseToolFallbackText(response.content, response.stop_reason) : void 0;
|
|
4414
4638
|
for (const part of response.content) {
|
|
4415
4639
|
switch (part.type) {
|
|
4416
4640
|
case "text": {
|
|
4417
|
-
if (
|
|
4641
|
+
if (jsonResponseToolFallbackText == null || jsonResponseToolFallbackText.has(part)) {
|
|
4418
4642
|
const webSearchCitations = (_a = part.citations) == null ? void 0 : _a.filter(
|
|
4419
4643
|
(citation) => citation.type === "web_search_result_location"
|
|
4420
4644
|
);
|
|
@@ -4950,6 +5174,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4950
5174
|
let safeguardResults;
|
|
4951
5175
|
let container = null;
|
|
4952
5176
|
let isJsonResponseFromTool = false;
|
|
5177
|
+
const heldBackText = [];
|
|
5178
|
+
const heldBackTextByIndex = /* @__PURE__ */ new Map();
|
|
5179
|
+
const createTextEnd = (id, citations) => ({
|
|
5180
|
+
type: "text-end",
|
|
5181
|
+
id,
|
|
5182
|
+
...citations.length > 0 && {
|
|
5183
|
+
providerMetadata: { anthropic: { citations } }
|
|
5184
|
+
}
|
|
5185
|
+
});
|
|
4953
5186
|
let isMessageOpen = false;
|
|
4954
5187
|
let activeMessageId;
|
|
4955
5188
|
let hasInvalidMessageSequence = false;
|
|
@@ -4961,7 +5194,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4961
5194
|
controller.enqueue({ type: "stream-start", warnings });
|
|
4962
5195
|
},
|
|
4963
5196
|
transform(chunk, controller) {
|
|
4964
|
-
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
5197
|
+
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
4965
5198
|
if (hasInvalidMessageSequence) {
|
|
4966
5199
|
return;
|
|
4967
5200
|
}
|
|
@@ -4984,9 +5217,19 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4984
5217
|
return;
|
|
4985
5218
|
}
|
|
4986
5219
|
blockType = contentBlockType;
|
|
5220
|
+
if (!TEXT_OR_THINKING_BLOCK_TYPES.has(contentBlockType)) {
|
|
5221
|
+
heldBackText.length = 0;
|
|
5222
|
+
}
|
|
4987
5223
|
switch (contentBlockType) {
|
|
4988
5224
|
case "text": {
|
|
4989
5225
|
if (usesJsonResponseTool) {
|
|
5226
|
+
const entry = {
|
|
5227
|
+
id: String(value.index),
|
|
5228
|
+
text: "",
|
|
5229
|
+
citations: []
|
|
5230
|
+
};
|
|
5231
|
+
heldBackText.push(entry);
|
|
5232
|
+
heldBackTextByIndex.set(value.index, entry);
|
|
4990
5233
|
return;
|
|
4991
5234
|
}
|
|
4992
5235
|
contentBlocks[value.index] = {
|
|
@@ -5441,17 +5684,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5441
5684
|
const contentBlock = contentBlocks[value.index];
|
|
5442
5685
|
switch (contentBlock.type) {
|
|
5443
5686
|
case "text": {
|
|
5444
|
-
controller.enqueue(
|
|
5445
|
-
|
|
5446
|
-
|
|
5447
|
-
|
|
5448
|
-
|
|
5449
|
-
|
|
5450
|
-
citations: contentBlock.citations
|
|
5451
|
-
}
|
|
5452
|
-
}
|
|
5453
|
-
}
|
|
5454
|
-
});
|
|
5687
|
+
controller.enqueue(
|
|
5688
|
+
createTextEnd(
|
|
5689
|
+
String(value.index),
|
|
5690
|
+
contentBlock.citations
|
|
5691
|
+
)
|
|
5692
|
+
);
|
|
5455
5693
|
break;
|
|
5456
5694
|
}
|
|
5457
5695
|
case "reasoning": {
|
|
@@ -5537,6 +5775,10 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5537
5775
|
switch (deltaType) {
|
|
5538
5776
|
case "text_delta": {
|
|
5539
5777
|
if (usesJsonResponseTool) {
|
|
5778
|
+
const entry = heldBackTextByIndex.get(value.index);
|
|
5779
|
+
if (entry != null) {
|
|
5780
|
+
entry.text += value.delta.text;
|
|
5781
|
+
}
|
|
5540
5782
|
return;
|
|
5541
5783
|
}
|
|
5542
5784
|
controller.enqueue({
|
|
@@ -5618,8 +5860,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5618
5860
|
case "citations_delta": {
|
|
5619
5861
|
const citation = value.delta.citation;
|
|
5620
5862
|
const contentBlock = contentBlocks[value.index];
|
|
5621
|
-
if (
|
|
5622
|
-
contentBlock.
|
|
5863
|
+
if (citation.type === "web_search_result_location") {
|
|
5864
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) === "text") {
|
|
5865
|
+
contentBlock.citations.push(citation);
|
|
5866
|
+
}
|
|
5867
|
+
(_e = heldBackTextByIndex.get(value.index)) == null ? void 0 : _e.citations.push(citation);
|
|
5623
5868
|
}
|
|
5624
5869
|
const source = createCitationSource(
|
|
5625
5870
|
citation,
|
|
@@ -5656,13 +5901,14 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5656
5901
|
}
|
|
5657
5902
|
isMessageOpen = true;
|
|
5658
5903
|
activeMessageId = value.message.id;
|
|
5904
|
+
heldBackTextByIndex.clear();
|
|
5659
5905
|
usage.input_tokens = value.message.usage.input_tokens;
|
|
5660
|
-
usage.cache_read_input_tokens = (
|
|
5661
|
-
usage.cache_creation_input_tokens = (
|
|
5906
|
+
usage.cache_read_input_tokens = (_f = value.message.usage.cache_read_input_tokens) != null ? _f : 0;
|
|
5907
|
+
usage.cache_creation_input_tokens = (_g = value.message.usage.cache_creation_input_tokens) != null ? _g : 0;
|
|
5662
5908
|
rawUsage = {
|
|
5663
5909
|
...value.message.usage
|
|
5664
5910
|
};
|
|
5665
|
-
cacheCreationInputTokens = (
|
|
5911
|
+
cacheCreationInputTokens = (_h = value.message.usage.cache_creation_input_tokens) != null ? _h : null;
|
|
5666
5912
|
if (value.message.input_transformations != null) {
|
|
5667
5913
|
inputTransformations = value.message.input_transformations;
|
|
5668
5914
|
}
|
|
@@ -5684,8 +5930,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5684
5930
|
}
|
|
5685
5931
|
controller.enqueue({
|
|
5686
5932
|
type: "response-metadata",
|
|
5687
|
-
id: (
|
|
5688
|
-
modelId: (
|
|
5933
|
+
id: (_i = value.message.id) != null ? _i : void 0,
|
|
5934
|
+
modelId: (_j = value.message.model) != null ? _j : void 0
|
|
5689
5935
|
});
|
|
5690
5936
|
if (value.message.content != null) {
|
|
5691
5937
|
for (let contentIndex = 0; contentIndex < value.message.content.length; contentIndex++) {
|
|
@@ -5697,7 +5943,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5697
5943
|
id: part.id,
|
|
5698
5944
|
toolName: part.name
|
|
5699
5945
|
});
|
|
5700
|
-
const inputStr = JSON.stringify((
|
|
5946
|
+
const inputStr = JSON.stringify((_k = part.input) != null ? _k : {});
|
|
5701
5947
|
controller.enqueue({
|
|
5702
5948
|
type: "tool-input-delta",
|
|
5703
5949
|
id: part.id,
|
|
@@ -5748,18 +5994,18 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5748
5994
|
finishReason: value.delta.stop_reason,
|
|
5749
5995
|
isJsonResponseFromTool
|
|
5750
5996
|
}),
|
|
5751
|
-
raw: (
|
|
5997
|
+
raw: (_l = value.delta.stop_reason) != null ? _l : void 0
|
|
5752
5998
|
};
|
|
5753
|
-
stopSequence = (
|
|
5999
|
+
stopSequence = (_m = value.delta.stop_sequence) != null ? _m : null;
|
|
5754
6000
|
stopDetails = mapAnthropicStopDetails(value.delta.stop_details);
|
|
5755
6001
|
container = value.delta.container != null ? {
|
|
5756
6002
|
expiresAt: value.delta.container.expires_at,
|
|
5757
6003
|
id: value.delta.container.id,
|
|
5758
|
-
skills: (
|
|
6004
|
+
skills: (_o = (_n = value.delta.container.skills) == null ? void 0 : _n.map((skill) => ({
|
|
5759
6005
|
type: skill.type,
|
|
5760
6006
|
skillId: skill.skill_id,
|
|
5761
6007
|
version: skill.version
|
|
5762
|
-
}))) != null ?
|
|
6008
|
+
}))) != null ? _o : null
|
|
5763
6009
|
} : null;
|
|
5764
6010
|
if (value.context_management) {
|
|
5765
6011
|
contextManagement = mapAnthropicResponseContextManagement(
|
|
@@ -5811,6 +6057,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5811
6057
|
if (usedCustomProviderKey && providerOptionsName !== "anthropic") {
|
|
5812
6058
|
providerMetadata[providerOptionsName] = anthropicMetadata;
|
|
5813
6059
|
}
|
|
6060
|
+
if (!isJsonResponseFromTool && !isContinuationStopReason(finishReason.raw)) {
|
|
6061
|
+
for (const { id, text, citations } of heldBackText) {
|
|
6062
|
+
if (text.length === 0) continue;
|
|
6063
|
+
controller.enqueue({ type: "text-start", id });
|
|
6064
|
+
controller.enqueue({ type: "text-delta", id, delta: text });
|
|
6065
|
+
controller.enqueue(createTextEnd(id, citations));
|
|
6066
|
+
}
|
|
6067
|
+
}
|
|
6068
|
+
heldBackText.length = 0;
|
|
5814
6069
|
controller.enqueue({
|
|
5815
6070
|
type: "finish",
|
|
5816
6071
|
finishReason,
|
|
@@ -6664,6 +6919,7 @@ var anthropicTools = {
|
|
|
6664
6919
|
anthropicTools,
|
|
6665
6920
|
getModelCapabilities,
|
|
6666
6921
|
prepareTools,
|
|
6667
|
-
sanitizeJsonSchema
|
|
6922
|
+
sanitizeJsonSchema,
|
|
6923
|
+
supportsNativeStructuredOutputSchema
|
|
6668
6924
|
});
|
|
6669
6925
|
//# sourceMappingURL=index.js.map
|