@ai-sdk/anthropic 2.0.0-alpha.8 → 2.0.0-alpha.9
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 +9 -0
- package/dist/index.d.mts +89 -0
- package/dist/index.d.ts +89 -0
- package/dist/index.js +225 -22
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +228 -22
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +2 -0
- package/dist/internal/index.d.ts +2 -0
- package/dist/internal/index.js +212 -13
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +214 -13
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/internal/index.js
CHANGED
|
@@ -47,6 +47,13 @@ var anthropicFailedResponseHandler = (0, import_provider_utils.createJsonErrorRe
|
|
|
47
47
|
|
|
48
48
|
// src/anthropic-messages-options.ts
|
|
49
49
|
var import_zod2 = require("zod");
|
|
50
|
+
var webSearchLocationSchema = import_zod2.z.object({
|
|
51
|
+
type: import_zod2.z.literal("approximate"),
|
|
52
|
+
city: import_zod2.z.string().optional(),
|
|
53
|
+
region: import_zod2.z.string().optional(),
|
|
54
|
+
country: import_zod2.z.string(),
|
|
55
|
+
timezone: import_zod2.z.string().optional()
|
|
56
|
+
});
|
|
50
57
|
var anthropicProviderOptions = import_zod2.z.object({
|
|
51
58
|
/**
|
|
52
59
|
Include reasoning content in requests sent to the model. Defaults to `true`.
|
|
@@ -57,11 +64,39 @@ var anthropicProviderOptions = import_zod2.z.object({
|
|
|
57
64
|
thinking: import_zod2.z.object({
|
|
58
65
|
type: import_zod2.z.union([import_zod2.z.literal("enabled"), import_zod2.z.literal("disabled")]),
|
|
59
66
|
budgetTokens: import_zod2.z.number().optional()
|
|
67
|
+
}).optional(),
|
|
68
|
+
/**
|
|
69
|
+
* Web search tool configuration for Claude models that support it.
|
|
70
|
+
* When provided, automatically adds the web search tool to the request.
|
|
71
|
+
*/
|
|
72
|
+
webSearch: import_zod2.z.object({
|
|
73
|
+
/**
|
|
74
|
+
* Limit the number of searches per request (optional)
|
|
75
|
+
* Defaults to 5 if not specified
|
|
76
|
+
*/
|
|
77
|
+
maxUses: import_zod2.z.number().min(1).max(20).optional(),
|
|
78
|
+
/**
|
|
79
|
+
* Only include results from these domains (optional)
|
|
80
|
+
* Cannot be used with blockedDomains
|
|
81
|
+
*/
|
|
82
|
+
allowedDomains: import_zod2.z.array(import_zod2.z.string()).optional(),
|
|
83
|
+
/**
|
|
84
|
+
* Never include results from these domains (optional)
|
|
85
|
+
* Cannot be used with allowedDomains
|
|
86
|
+
*/
|
|
87
|
+
blockedDomains: import_zod2.z.array(import_zod2.z.string()).optional(),
|
|
88
|
+
/**
|
|
89
|
+
* Localize search results based on user location (optional)
|
|
90
|
+
*/
|
|
91
|
+
userLocation: webSearchLocationSchema.optional()
|
|
60
92
|
}).optional()
|
|
61
93
|
});
|
|
62
94
|
|
|
63
95
|
// src/anthropic-prepare-tools.ts
|
|
64
96
|
var import_provider = require("@ai-sdk/provider");
|
|
97
|
+
function isWebSearchTool(tool) {
|
|
98
|
+
return typeof tool === "object" && tool !== null && "type" in tool && tool.type === "web_search_20250305";
|
|
99
|
+
}
|
|
65
100
|
function prepareTools({
|
|
66
101
|
tools,
|
|
67
102
|
toolChoice
|
|
@@ -74,6 +109,10 @@ function prepareTools({
|
|
|
74
109
|
}
|
|
75
110
|
const anthropicTools2 = [];
|
|
76
111
|
for (const tool of tools) {
|
|
112
|
+
if (isWebSearchTool(tool)) {
|
|
113
|
+
anthropicTools2.push(tool);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
77
116
|
switch (tool.type) {
|
|
78
117
|
case "function":
|
|
79
118
|
anthropicTools2.push({
|
|
@@ -485,8 +524,10 @@ function mapAnthropicStopReason({
|
|
|
485
524
|
var AnthropicMessagesLanguageModel = class {
|
|
486
525
|
constructor(modelId, config) {
|
|
487
526
|
this.specificationVersion = "v2";
|
|
527
|
+
var _a;
|
|
488
528
|
this.modelId = modelId;
|
|
489
529
|
this.config = config;
|
|
530
|
+
this.generateId = (_a = config.generateId) != null ? _a : import_provider_utils3.generateId;
|
|
490
531
|
}
|
|
491
532
|
supportsUrl(url) {
|
|
492
533
|
return url.protocol === "https:";
|
|
@@ -616,6 +657,27 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
616
657
|
}
|
|
617
658
|
baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
|
|
618
659
|
}
|
|
660
|
+
let modifiedTools = tools;
|
|
661
|
+
let modifiedToolChoice = toolChoice;
|
|
662
|
+
if (anthropicOptions == null ? void 0 : anthropicOptions.webSearch) {
|
|
663
|
+
const webSearchTool = {
|
|
664
|
+
type: "web_search_20250305",
|
|
665
|
+
name: "web_search",
|
|
666
|
+
max_uses: anthropicOptions.webSearch.maxUses,
|
|
667
|
+
allowed_domains: anthropicOptions.webSearch.allowedDomains,
|
|
668
|
+
blocked_domains: anthropicOptions.webSearch.blockedDomains,
|
|
669
|
+
...anthropicOptions.webSearch.userLocation && {
|
|
670
|
+
user_location: {
|
|
671
|
+
type: anthropicOptions.webSearch.userLocation.type,
|
|
672
|
+
country: anthropicOptions.webSearch.userLocation.country,
|
|
673
|
+
city: anthropicOptions.webSearch.userLocation.city,
|
|
674
|
+
region: anthropicOptions.webSearch.userLocation.region,
|
|
675
|
+
timezone: anthropicOptions.webSearch.userLocation.timezone
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
modifiedTools = tools ? [...tools, webSearchTool] : [webSearchTool];
|
|
680
|
+
}
|
|
619
681
|
const {
|
|
620
682
|
tools: anthropicTools2,
|
|
621
683
|
toolChoice: anthropicToolChoice,
|
|
@@ -625,7 +687,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
625
687
|
jsonResponseTool != null ? {
|
|
626
688
|
tools: [jsonResponseTool],
|
|
627
689
|
toolChoice: { type: "tool", toolName: jsonResponseTool.name }
|
|
628
|
-
} : { tools, toolChoice }
|
|
690
|
+
} : { tools: modifiedTools, toolChoice: modifiedToolChoice }
|
|
629
691
|
);
|
|
630
692
|
return {
|
|
631
693
|
args: {
|
|
@@ -657,7 +719,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
657
719
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
658
720
|
}
|
|
659
721
|
async doGenerate(options) {
|
|
660
|
-
var _a, _b, _c, _d;
|
|
722
|
+
var _a, _b, _c, _d, _e;
|
|
661
723
|
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
662
724
|
const {
|
|
663
725
|
responseHeaders,
|
|
@@ -723,6 +785,38 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
723
785
|
);
|
|
724
786
|
break;
|
|
725
787
|
}
|
|
788
|
+
case "server_tool_use": {
|
|
789
|
+
continue;
|
|
790
|
+
}
|
|
791
|
+
case "web_search_tool_result": {
|
|
792
|
+
if (Array.isArray(part.content)) {
|
|
793
|
+
for (const result of part.content) {
|
|
794
|
+
if (result.type === "web_search_result") {
|
|
795
|
+
content.push({
|
|
796
|
+
type: "source",
|
|
797
|
+
sourceType: "url",
|
|
798
|
+
id: this.generateId(),
|
|
799
|
+
url: result.url,
|
|
800
|
+
title: result.title,
|
|
801
|
+
providerMetadata: {
|
|
802
|
+
anthropic: {
|
|
803
|
+
encryptedContent: result.encrypted_content,
|
|
804
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
} else if (part.content.type === "web_search_tool_result_error") {
|
|
811
|
+
throw new import_provider3.APICallError({
|
|
812
|
+
message: `Web search failed: ${part.content.error_code}`,
|
|
813
|
+
url: "web_search_api",
|
|
814
|
+
requestBodyValues: { tool_use_id: part.tool_use_id },
|
|
815
|
+
data: { error_code: part.content.error_code }
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
break;
|
|
819
|
+
}
|
|
726
820
|
}
|
|
727
821
|
}
|
|
728
822
|
return {
|
|
@@ -735,19 +829,19 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
735
829
|
inputTokens: response.usage.input_tokens,
|
|
736
830
|
outputTokens: response.usage.output_tokens,
|
|
737
831
|
totalTokens: response.usage.input_tokens + response.usage.output_tokens,
|
|
738
|
-
cachedInputTokens: (
|
|
832
|
+
cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
|
|
739
833
|
},
|
|
740
834
|
request: { body: args },
|
|
741
835
|
response: {
|
|
742
|
-
id: (
|
|
743
|
-
modelId: (
|
|
836
|
+
id: (_c = response.id) != null ? _c : void 0,
|
|
837
|
+
modelId: (_d = response.model) != null ? _d : void 0,
|
|
744
838
|
headers: responseHeaders,
|
|
745
839
|
body: rawResponse
|
|
746
840
|
},
|
|
747
841
|
warnings,
|
|
748
842
|
providerMetadata: {
|
|
749
843
|
anthropic: {
|
|
750
|
-
cacheCreationInputTokens: (
|
|
844
|
+
cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null
|
|
751
845
|
}
|
|
752
846
|
}
|
|
753
847
|
};
|
|
@@ -775,6 +869,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
775
869
|
const toolCallContentBlocks = {};
|
|
776
870
|
let providerMetadata = void 0;
|
|
777
871
|
let blockType = void 0;
|
|
872
|
+
const config = this.config;
|
|
873
|
+
const generateId2 = this.generateId;
|
|
778
874
|
return {
|
|
779
875
|
stream: response.pipeThrough(
|
|
780
876
|
new TransformStream({
|
|
@@ -782,7 +878,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
782
878
|
controller.enqueue({ type: "stream-start", warnings });
|
|
783
879
|
},
|
|
784
880
|
transform(chunk, controller) {
|
|
785
|
-
var _a, _b, _c, _d, _e, _f;
|
|
881
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
786
882
|
if (!chunk.success) {
|
|
787
883
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
788
884
|
return;
|
|
@@ -821,6 +917,40 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
821
917
|
};
|
|
822
918
|
return;
|
|
823
919
|
}
|
|
920
|
+
case "server_tool_use": {
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
case "web_search_tool_result": {
|
|
924
|
+
if (Array.isArray(value.content_block.content)) {
|
|
925
|
+
for (const result of value.content_block.content) {
|
|
926
|
+
if (result.type === "web_search_result") {
|
|
927
|
+
controller.enqueue({
|
|
928
|
+
type: "source",
|
|
929
|
+
sourceType: "url",
|
|
930
|
+
id: generateId2(),
|
|
931
|
+
url: result.url,
|
|
932
|
+
title: result.title,
|
|
933
|
+
providerMetadata: {
|
|
934
|
+
anthropic: {
|
|
935
|
+
encryptedContent: result.encrypted_content,
|
|
936
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
} else if (value.content_block.content.type === "web_search_tool_result_error") {
|
|
943
|
+
controller.enqueue({
|
|
944
|
+
type: "error",
|
|
945
|
+
error: {
|
|
946
|
+
type: "web-search-error",
|
|
947
|
+
message: `Web search failed: ${value.content_block.content.error_code}`,
|
|
948
|
+
code: value.content_block.content.error_code
|
|
949
|
+
}
|
|
950
|
+
});
|
|
951
|
+
}
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
824
954
|
default: {
|
|
825
955
|
const _exhaustiveCheck = contentBlockType;
|
|
826
956
|
throw new Error(
|
|
@@ -883,6 +1013,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
883
1013
|
}
|
|
884
1014
|
case "input_json_delta": {
|
|
885
1015
|
const contentBlock = toolCallContentBlocks[value.index];
|
|
1016
|
+
if (!contentBlock) {
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
886
1019
|
controller.enqueue(
|
|
887
1020
|
jsonResponseTool != null ? {
|
|
888
1021
|
type: "text",
|
|
@@ -898,6 +1031,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
898
1031
|
contentBlock.jsonText += value.delta.partial_json;
|
|
899
1032
|
return;
|
|
900
1033
|
}
|
|
1034
|
+
case "citations_delta": {
|
|
1035
|
+
return;
|
|
1036
|
+
}
|
|
901
1037
|
default: {
|
|
902
1038
|
const _exhaustiveCheck = deltaType;
|
|
903
1039
|
throw new Error(
|
|
@@ -908,22 +1044,22 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
908
1044
|
}
|
|
909
1045
|
case "message_start": {
|
|
910
1046
|
usage.inputTokens = value.message.usage.input_tokens;
|
|
911
|
-
usage.cachedInputTokens = (
|
|
1047
|
+
usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
|
|
912
1048
|
providerMetadata = {
|
|
913
1049
|
anthropic: {
|
|
914
|
-
cacheCreationInputTokens: (
|
|
1050
|
+
cacheCreationInputTokens: (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null
|
|
915
1051
|
}
|
|
916
1052
|
};
|
|
917
1053
|
controller.enqueue({
|
|
918
1054
|
type: "response-metadata",
|
|
919
|
-
id: (
|
|
920
|
-
modelId: (
|
|
1055
|
+
id: (_d = value.message.id) != null ? _d : void 0,
|
|
1056
|
+
modelId: (_e = value.message.model) != null ? _e : void 0
|
|
921
1057
|
});
|
|
922
1058
|
return;
|
|
923
1059
|
}
|
|
924
1060
|
case "message_delta": {
|
|
925
1061
|
usage.outputTokens = value.usage.output_tokens;
|
|
926
|
-
usage.totalTokens = ((
|
|
1062
|
+
usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
|
|
927
1063
|
finishReason = mapAnthropicStopReason({
|
|
928
1064
|
finishReason: value.delta.stop_reason,
|
|
929
1065
|
isJsonResponseFromTool: jsonResponseTool != null
|
|
@@ -980,6 +1116,31 @@ var anthropicMessagesResponseSchema = import_zod3.z.object({
|
|
|
980
1116
|
id: import_zod3.z.string(),
|
|
981
1117
|
name: import_zod3.z.string(),
|
|
982
1118
|
input: import_zod3.z.unknown()
|
|
1119
|
+
}),
|
|
1120
|
+
import_zod3.z.object({
|
|
1121
|
+
type: import_zod3.z.literal("server_tool_use"),
|
|
1122
|
+
id: import_zod3.z.string(),
|
|
1123
|
+
name: import_zod3.z.string(),
|
|
1124
|
+
input: import_zod3.z.record(import_zod3.z.unknown()).nullish()
|
|
1125
|
+
}),
|
|
1126
|
+
import_zod3.z.object({
|
|
1127
|
+
type: import_zod3.z.literal("web_search_tool_result"),
|
|
1128
|
+
tool_use_id: import_zod3.z.string(),
|
|
1129
|
+
content: import_zod3.z.union([
|
|
1130
|
+
import_zod3.z.array(
|
|
1131
|
+
import_zod3.z.object({
|
|
1132
|
+
type: import_zod3.z.literal("web_search_result"),
|
|
1133
|
+
url: import_zod3.z.string(),
|
|
1134
|
+
title: import_zod3.z.string(),
|
|
1135
|
+
encrypted_content: import_zod3.z.string(),
|
|
1136
|
+
page_age: import_zod3.z.string().nullish()
|
|
1137
|
+
})
|
|
1138
|
+
),
|
|
1139
|
+
import_zod3.z.object({
|
|
1140
|
+
type: import_zod3.z.literal("web_search_tool_result_error"),
|
|
1141
|
+
error_code: import_zod3.z.string()
|
|
1142
|
+
})
|
|
1143
|
+
])
|
|
983
1144
|
})
|
|
984
1145
|
])
|
|
985
1146
|
),
|
|
@@ -988,7 +1149,10 @@ var anthropicMessagesResponseSchema = import_zod3.z.object({
|
|
|
988
1149
|
input_tokens: import_zod3.z.number(),
|
|
989
1150
|
output_tokens: import_zod3.z.number(),
|
|
990
1151
|
cache_creation_input_tokens: import_zod3.z.number().nullish(),
|
|
991
|
-
cache_read_input_tokens: import_zod3.z.number().nullish()
|
|
1152
|
+
cache_read_input_tokens: import_zod3.z.number().nullish(),
|
|
1153
|
+
server_tool_use: import_zod3.z.object({
|
|
1154
|
+
web_search_requests: import_zod3.z.number()
|
|
1155
|
+
}).nullish()
|
|
992
1156
|
})
|
|
993
1157
|
});
|
|
994
1158
|
var anthropicMessagesChunkSchema = import_zod3.z.discriminatedUnion("type", [
|
|
@@ -1025,6 +1189,31 @@ var anthropicMessagesChunkSchema = import_zod3.z.discriminatedUnion("type", [
|
|
|
1025
1189
|
import_zod3.z.object({
|
|
1026
1190
|
type: import_zod3.z.literal("redacted_thinking"),
|
|
1027
1191
|
data: import_zod3.z.string()
|
|
1192
|
+
}),
|
|
1193
|
+
import_zod3.z.object({
|
|
1194
|
+
type: import_zod3.z.literal("server_tool_use"),
|
|
1195
|
+
id: import_zod3.z.string(),
|
|
1196
|
+
name: import_zod3.z.string(),
|
|
1197
|
+
input: import_zod3.z.record(import_zod3.z.unknown()).nullish()
|
|
1198
|
+
}),
|
|
1199
|
+
import_zod3.z.object({
|
|
1200
|
+
type: import_zod3.z.literal("web_search_tool_result"),
|
|
1201
|
+
tool_use_id: import_zod3.z.string(),
|
|
1202
|
+
content: import_zod3.z.union([
|
|
1203
|
+
import_zod3.z.array(
|
|
1204
|
+
import_zod3.z.object({
|
|
1205
|
+
type: import_zod3.z.literal("web_search_result"),
|
|
1206
|
+
url: import_zod3.z.string(),
|
|
1207
|
+
title: import_zod3.z.string(),
|
|
1208
|
+
encrypted_content: import_zod3.z.string(),
|
|
1209
|
+
page_age: import_zod3.z.string().nullish()
|
|
1210
|
+
})
|
|
1211
|
+
),
|
|
1212
|
+
import_zod3.z.object({
|
|
1213
|
+
type: import_zod3.z.literal("web_search_tool_result_error"),
|
|
1214
|
+
error_code: import_zod3.z.string()
|
|
1215
|
+
})
|
|
1216
|
+
])
|
|
1028
1217
|
})
|
|
1029
1218
|
])
|
|
1030
1219
|
}),
|
|
@@ -1047,6 +1236,16 @@ var anthropicMessagesChunkSchema = import_zod3.z.discriminatedUnion("type", [
|
|
|
1047
1236
|
import_zod3.z.object({
|
|
1048
1237
|
type: import_zod3.z.literal("signature_delta"),
|
|
1049
1238
|
signature: import_zod3.z.string()
|
|
1239
|
+
}),
|
|
1240
|
+
import_zod3.z.object({
|
|
1241
|
+
type: import_zod3.z.literal("citations_delta"),
|
|
1242
|
+
citation: import_zod3.z.object({
|
|
1243
|
+
type: import_zod3.z.literal("web_search_result_location"),
|
|
1244
|
+
cited_text: import_zod3.z.string(),
|
|
1245
|
+
url: import_zod3.z.string(),
|
|
1246
|
+
title: import_zod3.z.string(),
|
|
1247
|
+
encrypted_index: import_zod3.z.string()
|
|
1248
|
+
})
|
|
1050
1249
|
})
|
|
1051
1250
|
])
|
|
1052
1251
|
}),
|