@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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @ai-sdk/anthropic
|
|
2
2
|
|
|
3
|
+
## 3.0.138
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- d900e58: Enable native structured output and strict tools for supported Claude 5 models on Bedrock. In automatic mode, Bedrock and Anthropic now fall back for schemas that native structured output cannot represent: recursive references, open objects, or more than ten optional properties. Anthropic uses the JSON response tool; Bedrock Converse uses the JSON tool or JSON instructions for models that reject forced tool use. The JSON-tool fallback excludes disabled and unselected application tools. Explicit `outputFormat` selection remains unchanged.
|
|
8
|
+
- d900e58: fix(anthropic): parse valid JSON string tool input and safely serialize BigInt and cycles in tool results
|
|
9
|
+
- Updated dependencies [d900e58]
|
|
10
|
+
- Updated dependencies [d900e58]
|
|
11
|
+
- Updated dependencies [d900e58]
|
|
12
|
+
- @ai-sdk/provider-utils@4.0.64
|
|
13
|
+
|
|
14
|
+
## 3.0.137
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- 7f91bae: fix(anthropic): use native structured outputs for Claude models that reject forced tool use (Sonnet 5.5, Opus 5.5, Fable 5.1) even when the provider disables them by default, as Amazon Bedrock and Google Vertex do. Self-referencing schemas keep the JSON response tool. Also keep `definitions` and `$defs` next to a top-level `$ref` when sanitizing structured output schemas, so the reference still resolves.
|
|
19
|
+
- 210ca0a: fix(anthropic): when the JSON response tool is in use but the model answers in text without calling it (for example with manual thinking, where the forced tool choice is downgraded to `auto`), return the final answer text instead of dropping it. Text before a tool call, result, or message continuation is still dropped.
|
|
20
|
+
- 7f91bae: fix(anthropic): keep the JSON response tool for schemas that native structured outputs cannot represent when upgrading models that reject forced tool use. Schemas with open objects (maps, free-form objects) would otherwise come back as `{}`, and schemas with more than 10 optional properties can be rejected as too complex.
|
|
21
|
+
|
|
3
22
|
## 3.0.136
|
|
4
23
|
|
|
5
24
|
### Patch Changes
|
package/dist/index.js
CHANGED
|
@@ -32,7 +32,7 @@ var import_provider4 = require("@ai-sdk/provider");
|
|
|
32
32
|
var import_provider_utils28 = require("@ai-sdk/provider-utils");
|
|
33
33
|
|
|
34
34
|
// src/version.ts
|
|
35
|
-
var VERSION = true ? "3.0.
|
|
35
|
+
var VERSION = true ? "3.0.138" : "0.0.0-test";
|
|
36
36
|
|
|
37
37
|
// src/anthropic-messages-language-model.ts
|
|
38
38
|
var import_provider3 = require("@ai-sdk/provider");
|
|
@@ -2797,7 +2797,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
2797
2797
|
case "json":
|
|
2798
2798
|
case "error-json":
|
|
2799
2799
|
default:
|
|
2800
|
-
contentValue =
|
|
2800
|
+
contentValue = (0, import_provider_utils16.stringifyWithBigIntAndCycles)(output.value);
|
|
2801
2801
|
break;
|
|
2802
2802
|
}
|
|
2803
2803
|
const toolsetName = getAnthropicToolsetName({
|
|
@@ -3035,7 +3035,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
3035
3035
|
type: "tool_use",
|
|
3036
3036
|
id: part.toolCallId,
|
|
3037
3037
|
name: part.toolName,
|
|
3038
|
-
input: part.input,
|
|
3038
|
+
input: parseToolCallInput(part.input),
|
|
3039
3039
|
...caller && { caller },
|
|
3040
3040
|
cache_control: cacheControl
|
|
3041
3041
|
});
|
|
@@ -3520,6 +3520,18 @@ function getAnthropicCaller(providerOptions) {
|
|
|
3520
3520
|
}
|
|
3521
3521
|
return (caller == null ? void 0 : caller.type) === "direct" ? { type: "direct" } : void 0;
|
|
3522
3522
|
}
|
|
3523
|
+
function parseToolCallInput(input) {
|
|
3524
|
+
if (typeof input === "string") {
|
|
3525
|
+
try {
|
|
3526
|
+
const parsed = (0, import_provider_utils16.secureJsonParse)(input);
|
|
3527
|
+
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
|
|
3528
|
+
return parsed;
|
|
3529
|
+
}
|
|
3530
|
+
} catch (e) {
|
|
3531
|
+
}
|
|
3532
|
+
}
|
|
3533
|
+
return input;
|
|
3534
|
+
}
|
|
3523
3535
|
|
|
3524
3536
|
// src/map-anthropic-stop-reason.ts
|
|
3525
3537
|
function mapAnthropicStopReason({
|
|
@@ -3585,9 +3597,8 @@ function sanitizeDefinition(definition) {
|
|
|
3585
3597
|
}
|
|
3586
3598
|
function sanitizeSchema(schema) {
|
|
3587
3599
|
const result = {};
|
|
3588
|
-
const schemaWithDefs = schema;
|
|
3589
3600
|
if (schema.$ref != null) {
|
|
3590
|
-
return { $ref: schema.$ref };
|
|
3601
|
+
return { $ref: schema.$ref, ...sanitizeSchemaDefinitions(schema) };
|
|
3591
3602
|
}
|
|
3592
3603
|
if (schema.$schema != null) {
|
|
3593
3604
|
result.$schema = schema.$schema;
|
|
@@ -3621,23 +3632,7 @@ function sanitizeSchema(schema) {
|
|
|
3621
3632
|
if (schema.allOf != null) {
|
|
3622
3633
|
result.allOf = schema.allOf.map(sanitizeDefinition);
|
|
3623
3634
|
}
|
|
3624
|
-
|
|
3625
|
-
result.definitions = Object.fromEntries(
|
|
3626
|
-
Object.entries(schema.definitions).map(([name, definition]) => [
|
|
3627
|
-
name,
|
|
3628
|
-
sanitizeDefinition(definition)
|
|
3629
|
-
])
|
|
3630
|
-
);
|
|
3631
|
-
}
|
|
3632
|
-
if (schemaWithDefs.$defs != null) {
|
|
3633
|
-
const resultWithDefs = result;
|
|
3634
|
-
resultWithDefs.$defs = Object.fromEntries(
|
|
3635
|
-
Object.entries(schemaWithDefs.$defs).map(([name, definition]) => [
|
|
3636
|
-
name,
|
|
3637
|
-
sanitizeDefinition(definition)
|
|
3638
|
-
])
|
|
3639
|
-
);
|
|
3640
|
-
}
|
|
3635
|
+
Object.assign(result, sanitizeSchemaDefinitions(schema));
|
|
3641
3636
|
if (schema.type === "object" || schema.properties != null) {
|
|
3642
3637
|
if (schema.properties != null) {
|
|
3643
3638
|
result.properties = Object.fromEntries(
|
|
@@ -3665,6 +3660,21 @@ ${constraintDescription}`;
|
|
|
3665
3660
|
}
|
|
3666
3661
|
return result;
|
|
3667
3662
|
}
|
|
3663
|
+
function sanitizeSchemaDefinitions(schema) {
|
|
3664
|
+
const { $defs } = schema;
|
|
3665
|
+
const sanitizeAll = (definitions) => Object.fromEntries(
|
|
3666
|
+
Object.entries(definitions).map(([name, definition]) => [
|
|
3667
|
+
name,
|
|
3668
|
+
sanitizeDefinition(definition)
|
|
3669
|
+
])
|
|
3670
|
+
);
|
|
3671
|
+
return {
|
|
3672
|
+
...schema.definitions != null && {
|
|
3673
|
+
definitions: sanitizeAll(schema.definitions)
|
|
3674
|
+
},
|
|
3675
|
+
...$defs != null && { $defs: sanitizeAll($defs) }
|
|
3676
|
+
};
|
|
3677
|
+
}
|
|
3668
3678
|
function getConstraintDescription(schema) {
|
|
3669
3679
|
const descriptions = DESCRIPTION_CONSTRAINT_KEYS.flatMap((key) => {
|
|
3670
3680
|
const value = schema[key];
|
|
@@ -3691,6 +3701,187 @@ function isPlainObject(value) {
|
|
|
3691
3701
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3692
3702
|
}
|
|
3693
3703
|
|
|
3704
|
+
// src/has-circular-schema-reference.ts
|
|
3705
|
+
var SCHEMA_MAP_KEYWORDS = /* @__PURE__ */ new Set([
|
|
3706
|
+
"dependentSchemas",
|
|
3707
|
+
"patternProperties",
|
|
3708
|
+
"properties"
|
|
3709
|
+
]);
|
|
3710
|
+
var DATA_KEYWORDS = /* @__PURE__ */ new Set(["const", "default", "enum", "examples"]);
|
|
3711
|
+
function hasCircularSchemaReference(schema) {
|
|
3712
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
3713
|
+
const visited = /* @__PURE__ */ new Set();
|
|
3714
|
+
const visit = (pointer) => {
|
|
3715
|
+
if (visiting.has(pointer)) {
|
|
3716
|
+
return true;
|
|
3717
|
+
}
|
|
3718
|
+
if (visited.has(pointer)) {
|
|
3719
|
+
return false;
|
|
3720
|
+
}
|
|
3721
|
+
const target = resolveSchemaPointer(schema, pointer);
|
|
3722
|
+
if (target === void 0) {
|
|
3723
|
+
return false;
|
|
3724
|
+
}
|
|
3725
|
+
visiting.add(pointer);
|
|
3726
|
+
const isCircular = collectLocalRefs(target).some(visit);
|
|
3727
|
+
visiting.delete(pointer);
|
|
3728
|
+
visited.add(pointer);
|
|
3729
|
+
return isCircular;
|
|
3730
|
+
};
|
|
3731
|
+
return visit("#");
|
|
3732
|
+
}
|
|
3733
|
+
function collectLocalRefs(node, refs = []) {
|
|
3734
|
+
if (Array.isArray(node)) {
|
|
3735
|
+
for (const item of node) {
|
|
3736
|
+
collectLocalRefs(item, refs);
|
|
3737
|
+
}
|
|
3738
|
+
return refs;
|
|
3739
|
+
}
|
|
3740
|
+
if (!isRecord(node)) {
|
|
3741
|
+
return refs;
|
|
3742
|
+
}
|
|
3743
|
+
for (const [key, value] of Object.entries(node)) {
|
|
3744
|
+
if (key === "$ref") {
|
|
3745
|
+
if (typeof value === "string" && value.startsWith("#")) {
|
|
3746
|
+
refs.push(value);
|
|
3747
|
+
}
|
|
3748
|
+
} else if (SCHEMA_MAP_KEYWORDS.has(key) && isRecord(value)) {
|
|
3749
|
+
for (const subschema of Object.values(value)) {
|
|
3750
|
+
collectLocalRefs(subschema, refs);
|
|
3751
|
+
}
|
|
3752
|
+
} else if (
|
|
3753
|
+
// definitions are only reached through a `$ref`:
|
|
3754
|
+
key !== "$defs" && key !== "definitions" && !DATA_KEYWORDS.has(key)
|
|
3755
|
+
) {
|
|
3756
|
+
collectLocalRefs(value, refs);
|
|
3757
|
+
}
|
|
3758
|
+
}
|
|
3759
|
+
return refs;
|
|
3760
|
+
}
|
|
3761
|
+
function resolveSchemaPointer(schema, pointer) {
|
|
3762
|
+
if (pointer === "#") {
|
|
3763
|
+
return schema;
|
|
3764
|
+
}
|
|
3765
|
+
if (!pointer.startsWith("#/")) {
|
|
3766
|
+
return void 0;
|
|
3767
|
+
}
|
|
3768
|
+
let decodedPointer;
|
|
3769
|
+
try {
|
|
3770
|
+
decodedPointer = decodeURIComponent(pointer.slice(2));
|
|
3771
|
+
} catch (e) {
|
|
3772
|
+
return void 0;
|
|
3773
|
+
}
|
|
3774
|
+
let current = schema;
|
|
3775
|
+
for (const rawSegment of decodedPointer.split("/")) {
|
|
3776
|
+
const segment = rawSegment.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
3777
|
+
if (Array.isArray(current)) {
|
|
3778
|
+
current = current[Number(segment)];
|
|
3779
|
+
} else if (isRecord(current)) {
|
|
3780
|
+
current = current[segment];
|
|
3781
|
+
} else {
|
|
3782
|
+
return void 0;
|
|
3783
|
+
}
|
|
3784
|
+
}
|
|
3785
|
+
return current;
|
|
3786
|
+
}
|
|
3787
|
+
function isRecord(value) {
|
|
3788
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3789
|
+
}
|
|
3790
|
+
|
|
3791
|
+
// src/supports-native-structured-output-schema.ts
|
|
3792
|
+
var MAX_OPTIONAL_PROPERTIES = 10;
|
|
3793
|
+
var SUBSCHEMA_KEYWORDS = [
|
|
3794
|
+
"additionalProperties",
|
|
3795
|
+
"contains",
|
|
3796
|
+
"else",
|
|
3797
|
+
"if",
|
|
3798
|
+
"items",
|
|
3799
|
+
"not",
|
|
3800
|
+
"then"
|
|
3801
|
+
];
|
|
3802
|
+
var SUBSCHEMA_LIST_KEYWORDS = ["allOf", "anyOf", "oneOf", "prefixItems"];
|
|
3803
|
+
var SUBSCHEMA_MAP_KEYWORDS = [
|
|
3804
|
+
"dependentSchemas",
|
|
3805
|
+
"patternProperties",
|
|
3806
|
+
"properties"
|
|
3807
|
+
];
|
|
3808
|
+
function supportsNativeStructuredOutputSchema(schema) {
|
|
3809
|
+
if (hasCircularSchemaReference(schema)) {
|
|
3810
|
+
return false;
|
|
3811
|
+
}
|
|
3812
|
+
const { optionalProperties, hasOpenObject } = getSchemaStats(
|
|
3813
|
+
schema,
|
|
3814
|
+
schema,
|
|
3815
|
+
/* @__PURE__ */ new Map()
|
|
3816
|
+
);
|
|
3817
|
+
return !hasOpenObject && optionalProperties <= MAX_OPTIONAL_PROPERTIES;
|
|
3818
|
+
}
|
|
3819
|
+
function getSchemaStats(node, root, refStats) {
|
|
3820
|
+
const stats = { optionalProperties: 0, hasOpenObject: false };
|
|
3821
|
+
if (!isRecord(node)) {
|
|
3822
|
+
return stats;
|
|
3823
|
+
}
|
|
3824
|
+
const add = (child) => {
|
|
3825
|
+
const childStats = getSchemaStats(child, root, refStats);
|
|
3826
|
+
stats.optionalProperties += childStats.optionalProperties;
|
|
3827
|
+
stats.hasOpenObject || (stats.hasOpenObject = childStats.hasOpenObject);
|
|
3828
|
+
};
|
|
3829
|
+
if (typeof node.$ref === "string" && node.$ref.startsWith("#")) {
|
|
3830
|
+
let resolved = refStats.get(node.$ref);
|
|
3831
|
+
if (resolved == null) {
|
|
3832
|
+
resolved = getSchemaStats(
|
|
3833
|
+
resolveSchemaPointer(root, node.$ref),
|
|
3834
|
+
root,
|
|
3835
|
+
refStats
|
|
3836
|
+
);
|
|
3837
|
+
refStats.set(node.$ref, resolved);
|
|
3838
|
+
}
|
|
3839
|
+
stats.optionalProperties += resolved.optionalProperties;
|
|
3840
|
+
stats.hasOpenObject || (stats.hasOpenObject = resolved.hasOpenObject);
|
|
3841
|
+
}
|
|
3842
|
+
if (isOpenObject(node)) {
|
|
3843
|
+
stats.hasOpenObject = true;
|
|
3844
|
+
}
|
|
3845
|
+
if (isRecord(node.properties)) {
|
|
3846
|
+
const required = new Set(Array.isArray(node.required) ? node.required : []);
|
|
3847
|
+
stats.optionalProperties += Object.keys(node.properties).filter(
|
|
3848
|
+
(name) => !required.has(name)
|
|
3849
|
+
).length;
|
|
3850
|
+
}
|
|
3851
|
+
for (const keyword of SUBSCHEMA_KEYWORDS) {
|
|
3852
|
+
const value = node[keyword];
|
|
3853
|
+
if (Array.isArray(value)) {
|
|
3854
|
+
value.forEach(add);
|
|
3855
|
+
} else {
|
|
3856
|
+
add(value);
|
|
3857
|
+
}
|
|
3858
|
+
}
|
|
3859
|
+
for (const keyword of SUBSCHEMA_LIST_KEYWORDS) {
|
|
3860
|
+
const value = node[keyword];
|
|
3861
|
+
if (Array.isArray(value)) {
|
|
3862
|
+
value.forEach(add);
|
|
3863
|
+
}
|
|
3864
|
+
}
|
|
3865
|
+
for (const keyword of SUBSCHEMA_MAP_KEYWORDS) {
|
|
3866
|
+
const value = node[keyword];
|
|
3867
|
+
if (isRecord(value)) {
|
|
3868
|
+
Object.values(value).forEach(add);
|
|
3869
|
+
}
|
|
3870
|
+
}
|
|
3871
|
+
return stats;
|
|
3872
|
+
}
|
|
3873
|
+
function isOpenObject(node) {
|
|
3874
|
+
const { additionalProperties, patternProperties, properties, type } = node;
|
|
3875
|
+
if (additionalProperties === true || isRecord(additionalProperties)) {
|
|
3876
|
+
return true;
|
|
3877
|
+
}
|
|
3878
|
+
if (isRecord(patternProperties) && Object.keys(patternProperties).length > 0) {
|
|
3879
|
+
return true;
|
|
3880
|
+
}
|
|
3881
|
+
const isObjectType = type === "object" || Array.isArray(type) && type.includes("object");
|
|
3882
|
+
return isObjectType && properties == null && additionalProperties !== false;
|
|
3883
|
+
}
|
|
3884
|
+
|
|
3694
3885
|
// src/anthropic-messages-language-model.ts
|
|
3695
3886
|
function createCitationSource(citation, citationDocuments, generateId3) {
|
|
3696
3887
|
var _a;
|
|
@@ -3764,6 +3955,31 @@ function toToolsetMemberInput({
|
|
|
3764
3955
|
...typeof input === "object" && input !== null && !Array.isArray(input) ? input : {}
|
|
3765
3956
|
};
|
|
3766
3957
|
}
|
|
3958
|
+
var TEXT_OR_THINKING_BLOCK_TYPES = /* @__PURE__ */ new Set([
|
|
3959
|
+
"redacted_thinking",
|
|
3960
|
+
"text",
|
|
3961
|
+
"thinking"
|
|
3962
|
+
]);
|
|
3963
|
+
function isContinuationStopReason(stopReason) {
|
|
3964
|
+
return stopReason === "pause_turn" || stopReason === "tool_use";
|
|
3965
|
+
}
|
|
3966
|
+
function getJsonResponseToolFallbackText(content, stopReason) {
|
|
3967
|
+
const text = /* @__PURE__ */ new Set();
|
|
3968
|
+
if (isContinuationStopReason(stopReason)) {
|
|
3969
|
+
return text;
|
|
3970
|
+
}
|
|
3971
|
+
for (const part of content) {
|
|
3972
|
+
if (part.type === "tool_use" && part.name === "json") {
|
|
3973
|
+
return /* @__PURE__ */ new Set();
|
|
3974
|
+
}
|
|
3975
|
+
if (part.type === "text") {
|
|
3976
|
+
text.add(part);
|
|
3977
|
+
} else if (!TEXT_OR_THINKING_BLOCK_TYPES.has(part.type)) {
|
|
3978
|
+
text.clear();
|
|
3979
|
+
}
|
|
3980
|
+
}
|
|
3981
|
+
return text;
|
|
3982
|
+
}
|
|
3767
3983
|
var AnthropicMessagesLanguageModel = class {
|
|
3768
3984
|
constructor(modelId, config) {
|
|
3769
3985
|
this.specificationVersion = "v3";
|
|
@@ -3908,10 +4124,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
3908
4124
|
}
|
|
3909
4125
|
const isAnthropicModel = isKnownModel || this.modelId.includes("claude-");
|
|
3910
4126
|
const supportsStructuredOutput = ((_b = this.config.supportsNativeStructuredOutput) != null ? _b : true) && modelSupportsStructuredOutput;
|
|
4127
|
+
const supportsResponseSchema = (responseFormat == null ? void 0 : responseFormat.type) !== "json" || responseFormat.schema == null || supportsNativeStructuredOutputSchema(responseFormat.schema);
|
|
3911
4128
|
const supportsStrictTools = ((_c = this.config.supportsStrictTools) != null ? _c : true) && modelSupportsStructuredOutput;
|
|
3912
4129
|
const structureOutputMode = (_d = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _d : "auto";
|
|
3913
|
-
let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
|
|
3914
|
-
if (!useStructuredOutput && rejectsForcedToolUse &&
|
|
4130
|
+
let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput && supportsResponseSchema;
|
|
4131
|
+
if (!useStructuredOutput && rejectsForcedToolUse && modelSupportsStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && supportsNativeStructuredOutputSchema(responseFormat.schema)) {
|
|
3915
4132
|
warnings.push({
|
|
3916
4133
|
type: "unsupported",
|
|
3917
4134
|
feature: "providerOptions.anthropic.structuredOutputMode",
|
|
@@ -4283,7 +4500,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4283
4500
|
betas: toolsBetas
|
|
4284
4501
|
} = await prepareTools(
|
|
4285
4502
|
jsonResponseTool != null ? {
|
|
4286
|
-
tools: [
|
|
4503
|
+
tools: [
|
|
4504
|
+
...(tools != null ? tools : []).filter(
|
|
4505
|
+
(tool) => (toolChoice == null ? void 0 : toolChoice.type) !== "none" && ((toolChoice == null ? void 0 : toolChoice.type) !== "tool" || tool.name === toolChoice.toolName)
|
|
4506
|
+
),
|
|
4507
|
+
jsonResponseTool
|
|
4508
|
+
],
|
|
4287
4509
|
toolChoice: { type: "required" },
|
|
4288
4510
|
disableParallelToolUse: true,
|
|
4289
4511
|
cacheControlValidator,
|
|
@@ -4417,10 +4639,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4417
4639
|
const mcpToolCalls = {};
|
|
4418
4640
|
const serverToolCalls = {};
|
|
4419
4641
|
let isJsonResponseFromTool = false;
|
|
4642
|
+
const jsonResponseToolFallbackText = usesJsonResponseTool ? getJsonResponseToolFallbackText(response.content, response.stop_reason) : void 0;
|
|
4420
4643
|
for (const part of response.content) {
|
|
4421
4644
|
switch (part.type) {
|
|
4422
4645
|
case "text": {
|
|
4423
|
-
if (
|
|
4646
|
+
if (jsonResponseToolFallbackText == null || jsonResponseToolFallbackText.has(part)) {
|
|
4424
4647
|
const webSearchCitations = (_a = part.citations) == null ? void 0 : _a.filter(
|
|
4425
4648
|
(citation) => citation.type === "web_search_result_location"
|
|
4426
4649
|
);
|
|
@@ -4956,6 +5179,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4956
5179
|
let safeguardResults;
|
|
4957
5180
|
let container = null;
|
|
4958
5181
|
let isJsonResponseFromTool = false;
|
|
5182
|
+
const heldBackText = [];
|
|
5183
|
+
const heldBackTextByIndex = /* @__PURE__ */ new Map();
|
|
5184
|
+
const createTextEnd = (id, citations) => ({
|
|
5185
|
+
type: "text-end",
|
|
5186
|
+
id,
|
|
5187
|
+
...citations.length > 0 && {
|
|
5188
|
+
providerMetadata: { anthropic: { citations } }
|
|
5189
|
+
}
|
|
5190
|
+
});
|
|
4959
5191
|
let isMessageOpen = false;
|
|
4960
5192
|
let activeMessageId;
|
|
4961
5193
|
let hasInvalidMessageSequence = false;
|
|
@@ -4967,7 +5199,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4967
5199
|
controller.enqueue({ type: "stream-start", warnings });
|
|
4968
5200
|
},
|
|
4969
5201
|
transform(chunk, controller) {
|
|
4970
|
-
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
5202
|
+
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
4971
5203
|
if (hasInvalidMessageSequence) {
|
|
4972
5204
|
return;
|
|
4973
5205
|
}
|
|
@@ -4990,9 +5222,19 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4990
5222
|
return;
|
|
4991
5223
|
}
|
|
4992
5224
|
blockType = contentBlockType;
|
|
5225
|
+
if (!TEXT_OR_THINKING_BLOCK_TYPES.has(contentBlockType)) {
|
|
5226
|
+
heldBackText.length = 0;
|
|
5227
|
+
}
|
|
4993
5228
|
switch (contentBlockType) {
|
|
4994
5229
|
case "text": {
|
|
4995
5230
|
if (usesJsonResponseTool) {
|
|
5231
|
+
const entry = {
|
|
5232
|
+
id: String(value.index),
|
|
5233
|
+
text: "",
|
|
5234
|
+
citations: []
|
|
5235
|
+
};
|
|
5236
|
+
heldBackText.push(entry);
|
|
5237
|
+
heldBackTextByIndex.set(value.index, entry);
|
|
4996
5238
|
return;
|
|
4997
5239
|
}
|
|
4998
5240
|
contentBlocks[value.index] = {
|
|
@@ -5447,17 +5689,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5447
5689
|
const contentBlock = contentBlocks[value.index];
|
|
5448
5690
|
switch (contentBlock.type) {
|
|
5449
5691
|
case "text": {
|
|
5450
|
-
controller.enqueue(
|
|
5451
|
-
|
|
5452
|
-
|
|
5453
|
-
|
|
5454
|
-
|
|
5455
|
-
|
|
5456
|
-
citations: contentBlock.citations
|
|
5457
|
-
}
|
|
5458
|
-
}
|
|
5459
|
-
}
|
|
5460
|
-
});
|
|
5692
|
+
controller.enqueue(
|
|
5693
|
+
createTextEnd(
|
|
5694
|
+
String(value.index),
|
|
5695
|
+
contentBlock.citations
|
|
5696
|
+
)
|
|
5697
|
+
);
|
|
5461
5698
|
break;
|
|
5462
5699
|
}
|
|
5463
5700
|
case "reasoning": {
|
|
@@ -5543,6 +5780,10 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5543
5780
|
switch (deltaType) {
|
|
5544
5781
|
case "text_delta": {
|
|
5545
5782
|
if (usesJsonResponseTool) {
|
|
5783
|
+
const entry = heldBackTextByIndex.get(value.index);
|
|
5784
|
+
if (entry != null) {
|
|
5785
|
+
entry.text += value.delta.text;
|
|
5786
|
+
}
|
|
5546
5787
|
return;
|
|
5547
5788
|
}
|
|
5548
5789
|
controller.enqueue({
|
|
@@ -5624,8 +5865,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5624
5865
|
case "citations_delta": {
|
|
5625
5866
|
const citation = value.delta.citation;
|
|
5626
5867
|
const contentBlock = contentBlocks[value.index];
|
|
5627
|
-
if (
|
|
5628
|
-
contentBlock.
|
|
5868
|
+
if (citation.type === "web_search_result_location") {
|
|
5869
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) === "text") {
|
|
5870
|
+
contentBlock.citations.push(citation);
|
|
5871
|
+
}
|
|
5872
|
+
(_e = heldBackTextByIndex.get(value.index)) == null ? void 0 : _e.citations.push(citation);
|
|
5629
5873
|
}
|
|
5630
5874
|
const source = createCitationSource(
|
|
5631
5875
|
citation,
|
|
@@ -5662,13 +5906,14 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5662
5906
|
}
|
|
5663
5907
|
isMessageOpen = true;
|
|
5664
5908
|
activeMessageId = value.message.id;
|
|
5909
|
+
heldBackTextByIndex.clear();
|
|
5665
5910
|
usage.input_tokens = value.message.usage.input_tokens;
|
|
5666
|
-
usage.cache_read_input_tokens = (
|
|
5667
|
-
usage.cache_creation_input_tokens = (
|
|
5911
|
+
usage.cache_read_input_tokens = (_f = value.message.usage.cache_read_input_tokens) != null ? _f : 0;
|
|
5912
|
+
usage.cache_creation_input_tokens = (_g = value.message.usage.cache_creation_input_tokens) != null ? _g : 0;
|
|
5668
5913
|
rawUsage = {
|
|
5669
5914
|
...value.message.usage
|
|
5670
5915
|
};
|
|
5671
|
-
cacheCreationInputTokens = (
|
|
5916
|
+
cacheCreationInputTokens = (_h = value.message.usage.cache_creation_input_tokens) != null ? _h : null;
|
|
5672
5917
|
if (value.message.input_transformations != null) {
|
|
5673
5918
|
inputTransformations = value.message.input_transformations;
|
|
5674
5919
|
}
|
|
@@ -5690,8 +5935,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5690
5935
|
}
|
|
5691
5936
|
controller.enqueue({
|
|
5692
5937
|
type: "response-metadata",
|
|
5693
|
-
id: (
|
|
5694
|
-
modelId: (
|
|
5938
|
+
id: (_i = value.message.id) != null ? _i : void 0,
|
|
5939
|
+
modelId: (_j = value.message.model) != null ? _j : void 0
|
|
5695
5940
|
});
|
|
5696
5941
|
if (value.message.content != null) {
|
|
5697
5942
|
for (let contentIndex = 0; contentIndex < value.message.content.length; contentIndex++) {
|
|
@@ -5703,7 +5948,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5703
5948
|
id: part.id,
|
|
5704
5949
|
toolName: part.name
|
|
5705
5950
|
});
|
|
5706
|
-
const inputStr = JSON.stringify((
|
|
5951
|
+
const inputStr = JSON.stringify((_k = part.input) != null ? _k : {});
|
|
5707
5952
|
controller.enqueue({
|
|
5708
5953
|
type: "tool-input-delta",
|
|
5709
5954
|
id: part.id,
|
|
@@ -5754,18 +5999,18 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5754
5999
|
finishReason: value.delta.stop_reason,
|
|
5755
6000
|
isJsonResponseFromTool
|
|
5756
6001
|
}),
|
|
5757
|
-
raw: (
|
|
6002
|
+
raw: (_l = value.delta.stop_reason) != null ? _l : void 0
|
|
5758
6003
|
};
|
|
5759
|
-
stopSequence = (
|
|
6004
|
+
stopSequence = (_m = value.delta.stop_sequence) != null ? _m : null;
|
|
5760
6005
|
stopDetails = mapAnthropicStopDetails(value.delta.stop_details);
|
|
5761
6006
|
container = value.delta.container != null ? {
|
|
5762
6007
|
expiresAt: value.delta.container.expires_at,
|
|
5763
6008
|
id: value.delta.container.id,
|
|
5764
|
-
skills: (
|
|
6009
|
+
skills: (_o = (_n = value.delta.container.skills) == null ? void 0 : _n.map((skill) => ({
|
|
5765
6010
|
type: skill.type,
|
|
5766
6011
|
skillId: skill.skill_id,
|
|
5767
6012
|
version: skill.version
|
|
5768
|
-
}))) != null ?
|
|
6013
|
+
}))) != null ? _o : null
|
|
5769
6014
|
} : null;
|
|
5770
6015
|
if (value.context_management) {
|
|
5771
6016
|
contextManagement = mapAnthropicResponseContextManagement(
|
|
@@ -5817,6 +6062,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5817
6062
|
if (usedCustomProviderKey && providerOptionsName !== "anthropic") {
|
|
5818
6063
|
providerMetadata[providerOptionsName] = anthropicMetadata;
|
|
5819
6064
|
}
|
|
6065
|
+
if (!isJsonResponseFromTool && !isContinuationStopReason(finishReason.raw)) {
|
|
6066
|
+
for (const { id, text, citations } of heldBackText) {
|
|
6067
|
+
if (text.length === 0) continue;
|
|
6068
|
+
controller.enqueue({ type: "text-start", id });
|
|
6069
|
+
controller.enqueue({ type: "text-delta", id, delta: text });
|
|
6070
|
+
controller.enqueue(createTextEnd(id, citations));
|
|
6071
|
+
}
|
|
6072
|
+
}
|
|
6073
|
+
heldBackText.length = 0;
|
|
5820
6074
|
controller.enqueue({
|
|
5821
6075
|
type: "finish",
|
|
5822
6076
|
finishReason,
|