@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/index.mjs
CHANGED
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
} from "@ai-sdk/provider-utils";
|
|
13
13
|
|
|
14
14
|
// src/version.ts
|
|
15
|
-
var VERSION = true ? "3.0.
|
|
15
|
+
var VERSION = true ? "3.0.138" : "0.0.0-test";
|
|
16
16
|
|
|
17
17
|
// src/anthropic-messages-language-model.ts
|
|
18
18
|
import {
|
|
@@ -2142,6 +2142,7 @@ import {
|
|
|
2142
2142
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
2143
2143
|
} from "@ai-sdk/provider";
|
|
2144
2144
|
import {
|
|
2145
|
+
stringifyWithBigIntAndCycles,
|
|
2145
2146
|
convertBase64ToUint8Array,
|
|
2146
2147
|
convertToBase64,
|
|
2147
2148
|
parseProviderOptions,
|
|
@@ -2856,7 +2857,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
2856
2857
|
case "json":
|
|
2857
2858
|
case "error-json":
|
|
2858
2859
|
default:
|
|
2859
|
-
contentValue =
|
|
2860
|
+
contentValue = stringifyWithBigIntAndCycles(output.value);
|
|
2860
2861
|
break;
|
|
2861
2862
|
}
|
|
2862
2863
|
const toolsetName = getAnthropicToolsetName({
|
|
@@ -3094,7 +3095,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
3094
3095
|
type: "tool_use",
|
|
3095
3096
|
id: part.toolCallId,
|
|
3096
3097
|
name: part.toolName,
|
|
3097
|
-
input: part.input,
|
|
3098
|
+
input: parseToolCallInput(part.input),
|
|
3098
3099
|
...caller && { caller },
|
|
3099
3100
|
cache_control: cacheControl
|
|
3100
3101
|
});
|
|
@@ -3579,6 +3580,18 @@ function getAnthropicCaller(providerOptions) {
|
|
|
3579
3580
|
}
|
|
3580
3581
|
return (caller == null ? void 0 : caller.type) === "direct" ? { type: "direct" } : void 0;
|
|
3581
3582
|
}
|
|
3583
|
+
function parseToolCallInput(input) {
|
|
3584
|
+
if (typeof input === "string") {
|
|
3585
|
+
try {
|
|
3586
|
+
const parsed = secureJsonParse(input);
|
|
3587
|
+
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
|
|
3588
|
+
return parsed;
|
|
3589
|
+
}
|
|
3590
|
+
} catch (e) {
|
|
3591
|
+
}
|
|
3592
|
+
}
|
|
3593
|
+
return input;
|
|
3594
|
+
}
|
|
3582
3595
|
|
|
3583
3596
|
// src/map-anthropic-stop-reason.ts
|
|
3584
3597
|
function mapAnthropicStopReason({
|
|
@@ -3644,9 +3657,8 @@ function sanitizeDefinition(definition) {
|
|
|
3644
3657
|
}
|
|
3645
3658
|
function sanitizeSchema(schema) {
|
|
3646
3659
|
const result = {};
|
|
3647
|
-
const schemaWithDefs = schema;
|
|
3648
3660
|
if (schema.$ref != null) {
|
|
3649
|
-
return { $ref: schema.$ref };
|
|
3661
|
+
return { $ref: schema.$ref, ...sanitizeSchemaDefinitions(schema) };
|
|
3650
3662
|
}
|
|
3651
3663
|
if (schema.$schema != null) {
|
|
3652
3664
|
result.$schema = schema.$schema;
|
|
@@ -3680,23 +3692,7 @@ function sanitizeSchema(schema) {
|
|
|
3680
3692
|
if (schema.allOf != null) {
|
|
3681
3693
|
result.allOf = schema.allOf.map(sanitizeDefinition);
|
|
3682
3694
|
}
|
|
3683
|
-
|
|
3684
|
-
result.definitions = Object.fromEntries(
|
|
3685
|
-
Object.entries(schema.definitions).map(([name, definition]) => [
|
|
3686
|
-
name,
|
|
3687
|
-
sanitizeDefinition(definition)
|
|
3688
|
-
])
|
|
3689
|
-
);
|
|
3690
|
-
}
|
|
3691
|
-
if (schemaWithDefs.$defs != null) {
|
|
3692
|
-
const resultWithDefs = result;
|
|
3693
|
-
resultWithDefs.$defs = Object.fromEntries(
|
|
3694
|
-
Object.entries(schemaWithDefs.$defs).map(([name, definition]) => [
|
|
3695
|
-
name,
|
|
3696
|
-
sanitizeDefinition(definition)
|
|
3697
|
-
])
|
|
3698
|
-
);
|
|
3699
|
-
}
|
|
3695
|
+
Object.assign(result, sanitizeSchemaDefinitions(schema));
|
|
3700
3696
|
if (schema.type === "object" || schema.properties != null) {
|
|
3701
3697
|
if (schema.properties != null) {
|
|
3702
3698
|
result.properties = Object.fromEntries(
|
|
@@ -3724,6 +3720,21 @@ ${constraintDescription}`;
|
|
|
3724
3720
|
}
|
|
3725
3721
|
return result;
|
|
3726
3722
|
}
|
|
3723
|
+
function sanitizeSchemaDefinitions(schema) {
|
|
3724
|
+
const { $defs } = schema;
|
|
3725
|
+
const sanitizeAll = (definitions) => Object.fromEntries(
|
|
3726
|
+
Object.entries(definitions).map(([name, definition]) => [
|
|
3727
|
+
name,
|
|
3728
|
+
sanitizeDefinition(definition)
|
|
3729
|
+
])
|
|
3730
|
+
);
|
|
3731
|
+
return {
|
|
3732
|
+
...schema.definitions != null && {
|
|
3733
|
+
definitions: sanitizeAll(schema.definitions)
|
|
3734
|
+
},
|
|
3735
|
+
...$defs != null && { $defs: sanitizeAll($defs) }
|
|
3736
|
+
};
|
|
3737
|
+
}
|
|
3727
3738
|
function getConstraintDescription(schema) {
|
|
3728
3739
|
const descriptions = DESCRIPTION_CONSTRAINT_KEYS.flatMap((key) => {
|
|
3729
3740
|
const value = schema[key];
|
|
@@ -3750,6 +3761,187 @@ function isPlainObject(value) {
|
|
|
3750
3761
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3751
3762
|
}
|
|
3752
3763
|
|
|
3764
|
+
// src/has-circular-schema-reference.ts
|
|
3765
|
+
var SCHEMA_MAP_KEYWORDS = /* @__PURE__ */ new Set([
|
|
3766
|
+
"dependentSchemas",
|
|
3767
|
+
"patternProperties",
|
|
3768
|
+
"properties"
|
|
3769
|
+
]);
|
|
3770
|
+
var DATA_KEYWORDS = /* @__PURE__ */ new Set(["const", "default", "enum", "examples"]);
|
|
3771
|
+
function hasCircularSchemaReference(schema) {
|
|
3772
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
3773
|
+
const visited = /* @__PURE__ */ new Set();
|
|
3774
|
+
const visit = (pointer) => {
|
|
3775
|
+
if (visiting.has(pointer)) {
|
|
3776
|
+
return true;
|
|
3777
|
+
}
|
|
3778
|
+
if (visited.has(pointer)) {
|
|
3779
|
+
return false;
|
|
3780
|
+
}
|
|
3781
|
+
const target = resolveSchemaPointer(schema, pointer);
|
|
3782
|
+
if (target === void 0) {
|
|
3783
|
+
return false;
|
|
3784
|
+
}
|
|
3785
|
+
visiting.add(pointer);
|
|
3786
|
+
const isCircular = collectLocalRefs(target).some(visit);
|
|
3787
|
+
visiting.delete(pointer);
|
|
3788
|
+
visited.add(pointer);
|
|
3789
|
+
return isCircular;
|
|
3790
|
+
};
|
|
3791
|
+
return visit("#");
|
|
3792
|
+
}
|
|
3793
|
+
function collectLocalRefs(node, refs = []) {
|
|
3794
|
+
if (Array.isArray(node)) {
|
|
3795
|
+
for (const item of node) {
|
|
3796
|
+
collectLocalRefs(item, refs);
|
|
3797
|
+
}
|
|
3798
|
+
return refs;
|
|
3799
|
+
}
|
|
3800
|
+
if (!isRecord(node)) {
|
|
3801
|
+
return refs;
|
|
3802
|
+
}
|
|
3803
|
+
for (const [key, value] of Object.entries(node)) {
|
|
3804
|
+
if (key === "$ref") {
|
|
3805
|
+
if (typeof value === "string" && value.startsWith("#")) {
|
|
3806
|
+
refs.push(value);
|
|
3807
|
+
}
|
|
3808
|
+
} else if (SCHEMA_MAP_KEYWORDS.has(key) && isRecord(value)) {
|
|
3809
|
+
for (const subschema of Object.values(value)) {
|
|
3810
|
+
collectLocalRefs(subschema, refs);
|
|
3811
|
+
}
|
|
3812
|
+
} else if (
|
|
3813
|
+
// definitions are only reached through a `$ref`:
|
|
3814
|
+
key !== "$defs" && key !== "definitions" && !DATA_KEYWORDS.has(key)
|
|
3815
|
+
) {
|
|
3816
|
+
collectLocalRefs(value, refs);
|
|
3817
|
+
}
|
|
3818
|
+
}
|
|
3819
|
+
return refs;
|
|
3820
|
+
}
|
|
3821
|
+
function resolveSchemaPointer(schema, pointer) {
|
|
3822
|
+
if (pointer === "#") {
|
|
3823
|
+
return schema;
|
|
3824
|
+
}
|
|
3825
|
+
if (!pointer.startsWith("#/")) {
|
|
3826
|
+
return void 0;
|
|
3827
|
+
}
|
|
3828
|
+
let decodedPointer;
|
|
3829
|
+
try {
|
|
3830
|
+
decodedPointer = decodeURIComponent(pointer.slice(2));
|
|
3831
|
+
} catch (e) {
|
|
3832
|
+
return void 0;
|
|
3833
|
+
}
|
|
3834
|
+
let current = schema;
|
|
3835
|
+
for (const rawSegment of decodedPointer.split("/")) {
|
|
3836
|
+
const segment = rawSegment.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
3837
|
+
if (Array.isArray(current)) {
|
|
3838
|
+
current = current[Number(segment)];
|
|
3839
|
+
} else if (isRecord(current)) {
|
|
3840
|
+
current = current[segment];
|
|
3841
|
+
} else {
|
|
3842
|
+
return void 0;
|
|
3843
|
+
}
|
|
3844
|
+
}
|
|
3845
|
+
return current;
|
|
3846
|
+
}
|
|
3847
|
+
function isRecord(value) {
|
|
3848
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3849
|
+
}
|
|
3850
|
+
|
|
3851
|
+
// src/supports-native-structured-output-schema.ts
|
|
3852
|
+
var MAX_OPTIONAL_PROPERTIES = 10;
|
|
3853
|
+
var SUBSCHEMA_KEYWORDS = [
|
|
3854
|
+
"additionalProperties",
|
|
3855
|
+
"contains",
|
|
3856
|
+
"else",
|
|
3857
|
+
"if",
|
|
3858
|
+
"items",
|
|
3859
|
+
"not",
|
|
3860
|
+
"then"
|
|
3861
|
+
];
|
|
3862
|
+
var SUBSCHEMA_LIST_KEYWORDS = ["allOf", "anyOf", "oneOf", "prefixItems"];
|
|
3863
|
+
var SUBSCHEMA_MAP_KEYWORDS = [
|
|
3864
|
+
"dependentSchemas",
|
|
3865
|
+
"patternProperties",
|
|
3866
|
+
"properties"
|
|
3867
|
+
];
|
|
3868
|
+
function supportsNativeStructuredOutputSchema(schema) {
|
|
3869
|
+
if (hasCircularSchemaReference(schema)) {
|
|
3870
|
+
return false;
|
|
3871
|
+
}
|
|
3872
|
+
const { optionalProperties, hasOpenObject } = getSchemaStats(
|
|
3873
|
+
schema,
|
|
3874
|
+
schema,
|
|
3875
|
+
/* @__PURE__ */ new Map()
|
|
3876
|
+
);
|
|
3877
|
+
return !hasOpenObject && optionalProperties <= MAX_OPTIONAL_PROPERTIES;
|
|
3878
|
+
}
|
|
3879
|
+
function getSchemaStats(node, root, refStats) {
|
|
3880
|
+
const stats = { optionalProperties: 0, hasOpenObject: false };
|
|
3881
|
+
if (!isRecord(node)) {
|
|
3882
|
+
return stats;
|
|
3883
|
+
}
|
|
3884
|
+
const add = (child) => {
|
|
3885
|
+
const childStats = getSchemaStats(child, root, refStats);
|
|
3886
|
+
stats.optionalProperties += childStats.optionalProperties;
|
|
3887
|
+
stats.hasOpenObject || (stats.hasOpenObject = childStats.hasOpenObject);
|
|
3888
|
+
};
|
|
3889
|
+
if (typeof node.$ref === "string" && node.$ref.startsWith("#")) {
|
|
3890
|
+
let resolved = refStats.get(node.$ref);
|
|
3891
|
+
if (resolved == null) {
|
|
3892
|
+
resolved = getSchemaStats(
|
|
3893
|
+
resolveSchemaPointer(root, node.$ref),
|
|
3894
|
+
root,
|
|
3895
|
+
refStats
|
|
3896
|
+
);
|
|
3897
|
+
refStats.set(node.$ref, resolved);
|
|
3898
|
+
}
|
|
3899
|
+
stats.optionalProperties += resolved.optionalProperties;
|
|
3900
|
+
stats.hasOpenObject || (stats.hasOpenObject = resolved.hasOpenObject);
|
|
3901
|
+
}
|
|
3902
|
+
if (isOpenObject(node)) {
|
|
3903
|
+
stats.hasOpenObject = true;
|
|
3904
|
+
}
|
|
3905
|
+
if (isRecord(node.properties)) {
|
|
3906
|
+
const required = new Set(Array.isArray(node.required) ? node.required : []);
|
|
3907
|
+
stats.optionalProperties += Object.keys(node.properties).filter(
|
|
3908
|
+
(name) => !required.has(name)
|
|
3909
|
+
).length;
|
|
3910
|
+
}
|
|
3911
|
+
for (const keyword of SUBSCHEMA_KEYWORDS) {
|
|
3912
|
+
const value = node[keyword];
|
|
3913
|
+
if (Array.isArray(value)) {
|
|
3914
|
+
value.forEach(add);
|
|
3915
|
+
} else {
|
|
3916
|
+
add(value);
|
|
3917
|
+
}
|
|
3918
|
+
}
|
|
3919
|
+
for (const keyword of SUBSCHEMA_LIST_KEYWORDS) {
|
|
3920
|
+
const value = node[keyword];
|
|
3921
|
+
if (Array.isArray(value)) {
|
|
3922
|
+
value.forEach(add);
|
|
3923
|
+
}
|
|
3924
|
+
}
|
|
3925
|
+
for (const keyword of SUBSCHEMA_MAP_KEYWORDS) {
|
|
3926
|
+
const value = node[keyword];
|
|
3927
|
+
if (isRecord(value)) {
|
|
3928
|
+
Object.values(value).forEach(add);
|
|
3929
|
+
}
|
|
3930
|
+
}
|
|
3931
|
+
return stats;
|
|
3932
|
+
}
|
|
3933
|
+
function isOpenObject(node) {
|
|
3934
|
+
const { additionalProperties, patternProperties, properties, type } = node;
|
|
3935
|
+
if (additionalProperties === true || isRecord(additionalProperties)) {
|
|
3936
|
+
return true;
|
|
3937
|
+
}
|
|
3938
|
+
if (isRecord(patternProperties) && Object.keys(patternProperties).length > 0) {
|
|
3939
|
+
return true;
|
|
3940
|
+
}
|
|
3941
|
+
const isObjectType = type === "object" || Array.isArray(type) && type.includes("object");
|
|
3942
|
+
return isObjectType && properties == null && additionalProperties !== false;
|
|
3943
|
+
}
|
|
3944
|
+
|
|
3753
3945
|
// src/anthropic-messages-language-model.ts
|
|
3754
3946
|
function createCitationSource(citation, citationDocuments, generateId3) {
|
|
3755
3947
|
var _a;
|
|
@@ -3823,6 +4015,31 @@ function toToolsetMemberInput({
|
|
|
3823
4015
|
...typeof input === "object" && input !== null && !Array.isArray(input) ? input : {}
|
|
3824
4016
|
};
|
|
3825
4017
|
}
|
|
4018
|
+
var TEXT_OR_THINKING_BLOCK_TYPES = /* @__PURE__ */ new Set([
|
|
4019
|
+
"redacted_thinking",
|
|
4020
|
+
"text",
|
|
4021
|
+
"thinking"
|
|
4022
|
+
]);
|
|
4023
|
+
function isContinuationStopReason(stopReason) {
|
|
4024
|
+
return stopReason === "pause_turn" || stopReason === "tool_use";
|
|
4025
|
+
}
|
|
4026
|
+
function getJsonResponseToolFallbackText(content, stopReason) {
|
|
4027
|
+
const text = /* @__PURE__ */ new Set();
|
|
4028
|
+
if (isContinuationStopReason(stopReason)) {
|
|
4029
|
+
return text;
|
|
4030
|
+
}
|
|
4031
|
+
for (const part of content) {
|
|
4032
|
+
if (part.type === "tool_use" && part.name === "json") {
|
|
4033
|
+
return /* @__PURE__ */ new Set();
|
|
4034
|
+
}
|
|
4035
|
+
if (part.type === "text") {
|
|
4036
|
+
text.add(part);
|
|
4037
|
+
} else if (!TEXT_OR_THINKING_BLOCK_TYPES.has(part.type)) {
|
|
4038
|
+
text.clear();
|
|
4039
|
+
}
|
|
4040
|
+
}
|
|
4041
|
+
return text;
|
|
4042
|
+
}
|
|
3826
4043
|
var AnthropicMessagesLanguageModel = class {
|
|
3827
4044
|
constructor(modelId, config) {
|
|
3828
4045
|
this.specificationVersion = "v3";
|
|
@@ -3967,10 +4184,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
3967
4184
|
}
|
|
3968
4185
|
const isAnthropicModel = isKnownModel || this.modelId.includes("claude-");
|
|
3969
4186
|
const supportsStructuredOutput = ((_b = this.config.supportsNativeStructuredOutput) != null ? _b : true) && modelSupportsStructuredOutput;
|
|
4187
|
+
const supportsResponseSchema = (responseFormat == null ? void 0 : responseFormat.type) !== "json" || responseFormat.schema == null || supportsNativeStructuredOutputSchema(responseFormat.schema);
|
|
3970
4188
|
const supportsStrictTools = ((_c = this.config.supportsStrictTools) != null ? _c : true) && modelSupportsStructuredOutput;
|
|
3971
4189
|
const structureOutputMode = (_d = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _d : "auto";
|
|
3972
|
-
let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
|
|
3973
|
-
if (!useStructuredOutput && rejectsForcedToolUse &&
|
|
4190
|
+
let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput && supportsResponseSchema;
|
|
4191
|
+
if (!useStructuredOutput && rejectsForcedToolUse && modelSupportsStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && supportsNativeStructuredOutputSchema(responseFormat.schema)) {
|
|
3974
4192
|
warnings.push({
|
|
3975
4193
|
type: "unsupported",
|
|
3976
4194
|
feature: "providerOptions.anthropic.structuredOutputMode",
|
|
@@ -4342,7 +4560,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4342
4560
|
betas: toolsBetas
|
|
4343
4561
|
} = await prepareTools(
|
|
4344
4562
|
jsonResponseTool != null ? {
|
|
4345
|
-
tools: [
|
|
4563
|
+
tools: [
|
|
4564
|
+
...(tools != null ? tools : []).filter(
|
|
4565
|
+
(tool) => (toolChoice == null ? void 0 : toolChoice.type) !== "none" && ((toolChoice == null ? void 0 : toolChoice.type) !== "tool" || tool.name === toolChoice.toolName)
|
|
4566
|
+
),
|
|
4567
|
+
jsonResponseTool
|
|
4568
|
+
],
|
|
4346
4569
|
toolChoice: { type: "required" },
|
|
4347
4570
|
disableParallelToolUse: true,
|
|
4348
4571
|
cacheControlValidator,
|
|
@@ -4476,10 +4699,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
4476
4699
|
const mcpToolCalls = {};
|
|
4477
4700
|
const serverToolCalls = {};
|
|
4478
4701
|
let isJsonResponseFromTool = false;
|
|
4702
|
+
const jsonResponseToolFallbackText = usesJsonResponseTool ? getJsonResponseToolFallbackText(response.content, response.stop_reason) : void 0;
|
|
4479
4703
|
for (const part of response.content) {
|
|
4480
4704
|
switch (part.type) {
|
|
4481
4705
|
case "text": {
|
|
4482
|
-
if (
|
|
4706
|
+
if (jsonResponseToolFallbackText == null || jsonResponseToolFallbackText.has(part)) {
|
|
4483
4707
|
const webSearchCitations = (_a = part.citations) == null ? void 0 : _a.filter(
|
|
4484
4708
|
(citation) => citation.type === "web_search_result_location"
|
|
4485
4709
|
);
|
|
@@ -5015,6 +5239,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5015
5239
|
let safeguardResults;
|
|
5016
5240
|
let container = null;
|
|
5017
5241
|
let isJsonResponseFromTool = false;
|
|
5242
|
+
const heldBackText = [];
|
|
5243
|
+
const heldBackTextByIndex = /* @__PURE__ */ new Map();
|
|
5244
|
+
const createTextEnd = (id, citations) => ({
|
|
5245
|
+
type: "text-end",
|
|
5246
|
+
id,
|
|
5247
|
+
...citations.length > 0 && {
|
|
5248
|
+
providerMetadata: { anthropic: { citations } }
|
|
5249
|
+
}
|
|
5250
|
+
});
|
|
5018
5251
|
let isMessageOpen = false;
|
|
5019
5252
|
let activeMessageId;
|
|
5020
5253
|
let hasInvalidMessageSequence = false;
|
|
@@ -5026,7 +5259,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5026
5259
|
controller.enqueue({ type: "stream-start", warnings });
|
|
5027
5260
|
},
|
|
5028
5261
|
transform(chunk, controller) {
|
|
5029
|
-
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
5262
|
+
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
5030
5263
|
if (hasInvalidMessageSequence) {
|
|
5031
5264
|
return;
|
|
5032
5265
|
}
|
|
@@ -5049,9 +5282,19 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5049
5282
|
return;
|
|
5050
5283
|
}
|
|
5051
5284
|
blockType = contentBlockType;
|
|
5285
|
+
if (!TEXT_OR_THINKING_BLOCK_TYPES.has(contentBlockType)) {
|
|
5286
|
+
heldBackText.length = 0;
|
|
5287
|
+
}
|
|
5052
5288
|
switch (contentBlockType) {
|
|
5053
5289
|
case "text": {
|
|
5054
5290
|
if (usesJsonResponseTool) {
|
|
5291
|
+
const entry = {
|
|
5292
|
+
id: String(value.index),
|
|
5293
|
+
text: "",
|
|
5294
|
+
citations: []
|
|
5295
|
+
};
|
|
5296
|
+
heldBackText.push(entry);
|
|
5297
|
+
heldBackTextByIndex.set(value.index, entry);
|
|
5055
5298
|
return;
|
|
5056
5299
|
}
|
|
5057
5300
|
contentBlocks[value.index] = {
|
|
@@ -5506,17 +5749,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5506
5749
|
const contentBlock = contentBlocks[value.index];
|
|
5507
5750
|
switch (contentBlock.type) {
|
|
5508
5751
|
case "text": {
|
|
5509
|
-
controller.enqueue(
|
|
5510
|
-
|
|
5511
|
-
|
|
5512
|
-
|
|
5513
|
-
|
|
5514
|
-
|
|
5515
|
-
citations: contentBlock.citations
|
|
5516
|
-
}
|
|
5517
|
-
}
|
|
5518
|
-
}
|
|
5519
|
-
});
|
|
5752
|
+
controller.enqueue(
|
|
5753
|
+
createTextEnd(
|
|
5754
|
+
String(value.index),
|
|
5755
|
+
contentBlock.citations
|
|
5756
|
+
)
|
|
5757
|
+
);
|
|
5520
5758
|
break;
|
|
5521
5759
|
}
|
|
5522
5760
|
case "reasoning": {
|
|
@@ -5602,6 +5840,10 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5602
5840
|
switch (deltaType) {
|
|
5603
5841
|
case "text_delta": {
|
|
5604
5842
|
if (usesJsonResponseTool) {
|
|
5843
|
+
const entry = heldBackTextByIndex.get(value.index);
|
|
5844
|
+
if (entry != null) {
|
|
5845
|
+
entry.text += value.delta.text;
|
|
5846
|
+
}
|
|
5605
5847
|
return;
|
|
5606
5848
|
}
|
|
5607
5849
|
controller.enqueue({
|
|
@@ -5683,8 +5925,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5683
5925
|
case "citations_delta": {
|
|
5684
5926
|
const citation = value.delta.citation;
|
|
5685
5927
|
const contentBlock = contentBlocks[value.index];
|
|
5686
|
-
if (
|
|
5687
|
-
contentBlock.
|
|
5928
|
+
if (citation.type === "web_search_result_location") {
|
|
5929
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) === "text") {
|
|
5930
|
+
contentBlock.citations.push(citation);
|
|
5931
|
+
}
|
|
5932
|
+
(_e = heldBackTextByIndex.get(value.index)) == null ? void 0 : _e.citations.push(citation);
|
|
5688
5933
|
}
|
|
5689
5934
|
const source = createCitationSource(
|
|
5690
5935
|
citation,
|
|
@@ -5721,13 +5966,14 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5721
5966
|
}
|
|
5722
5967
|
isMessageOpen = true;
|
|
5723
5968
|
activeMessageId = value.message.id;
|
|
5969
|
+
heldBackTextByIndex.clear();
|
|
5724
5970
|
usage.input_tokens = value.message.usage.input_tokens;
|
|
5725
|
-
usage.cache_read_input_tokens = (
|
|
5726
|
-
usage.cache_creation_input_tokens = (
|
|
5971
|
+
usage.cache_read_input_tokens = (_f = value.message.usage.cache_read_input_tokens) != null ? _f : 0;
|
|
5972
|
+
usage.cache_creation_input_tokens = (_g = value.message.usage.cache_creation_input_tokens) != null ? _g : 0;
|
|
5727
5973
|
rawUsage = {
|
|
5728
5974
|
...value.message.usage
|
|
5729
5975
|
};
|
|
5730
|
-
cacheCreationInputTokens = (
|
|
5976
|
+
cacheCreationInputTokens = (_h = value.message.usage.cache_creation_input_tokens) != null ? _h : null;
|
|
5731
5977
|
if (value.message.input_transformations != null) {
|
|
5732
5978
|
inputTransformations = value.message.input_transformations;
|
|
5733
5979
|
}
|
|
@@ -5749,8 +5995,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5749
5995
|
}
|
|
5750
5996
|
controller.enqueue({
|
|
5751
5997
|
type: "response-metadata",
|
|
5752
|
-
id: (
|
|
5753
|
-
modelId: (
|
|
5998
|
+
id: (_i = value.message.id) != null ? _i : void 0,
|
|
5999
|
+
modelId: (_j = value.message.model) != null ? _j : void 0
|
|
5754
6000
|
});
|
|
5755
6001
|
if (value.message.content != null) {
|
|
5756
6002
|
for (let contentIndex = 0; contentIndex < value.message.content.length; contentIndex++) {
|
|
@@ -5762,7 +6008,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5762
6008
|
id: part.id,
|
|
5763
6009
|
toolName: part.name
|
|
5764
6010
|
});
|
|
5765
|
-
const inputStr = JSON.stringify((
|
|
6011
|
+
const inputStr = JSON.stringify((_k = part.input) != null ? _k : {});
|
|
5766
6012
|
controller.enqueue({
|
|
5767
6013
|
type: "tool-input-delta",
|
|
5768
6014
|
id: part.id,
|
|
@@ -5813,18 +6059,18 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5813
6059
|
finishReason: value.delta.stop_reason,
|
|
5814
6060
|
isJsonResponseFromTool
|
|
5815
6061
|
}),
|
|
5816
|
-
raw: (
|
|
6062
|
+
raw: (_l = value.delta.stop_reason) != null ? _l : void 0
|
|
5817
6063
|
};
|
|
5818
|
-
stopSequence = (
|
|
6064
|
+
stopSequence = (_m = value.delta.stop_sequence) != null ? _m : null;
|
|
5819
6065
|
stopDetails = mapAnthropicStopDetails(value.delta.stop_details);
|
|
5820
6066
|
container = value.delta.container != null ? {
|
|
5821
6067
|
expiresAt: value.delta.container.expires_at,
|
|
5822
6068
|
id: value.delta.container.id,
|
|
5823
|
-
skills: (
|
|
6069
|
+
skills: (_o = (_n = value.delta.container.skills) == null ? void 0 : _n.map((skill) => ({
|
|
5824
6070
|
type: skill.type,
|
|
5825
6071
|
skillId: skill.skill_id,
|
|
5826
6072
|
version: skill.version
|
|
5827
|
-
}))) != null ?
|
|
6073
|
+
}))) != null ? _o : null
|
|
5828
6074
|
} : null;
|
|
5829
6075
|
if (value.context_management) {
|
|
5830
6076
|
contextManagement = mapAnthropicResponseContextManagement(
|
|
@@ -5876,6 +6122,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
5876
6122
|
if (usedCustomProviderKey && providerOptionsName !== "anthropic") {
|
|
5877
6123
|
providerMetadata[providerOptionsName] = anthropicMetadata;
|
|
5878
6124
|
}
|
|
6125
|
+
if (!isJsonResponseFromTool && !isContinuationStopReason(finishReason.raw)) {
|
|
6126
|
+
for (const { id, text, citations } of heldBackText) {
|
|
6127
|
+
if (text.length === 0) continue;
|
|
6128
|
+
controller.enqueue({ type: "text-start", id });
|
|
6129
|
+
controller.enqueue({ type: "text-delta", id, delta: text });
|
|
6130
|
+
controller.enqueue(createTextEnd(id, citations));
|
|
6131
|
+
}
|
|
6132
|
+
}
|
|
6133
|
+
heldBackText.length = 0;
|
|
5879
6134
|
controller.enqueue({
|
|
5880
6135
|
type: "finish",
|
|
5881
6136
|
finishReason,
|