@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/index.mjs
CHANGED
|
@@ -3,18 +3,21 @@ import {
|
|
|
3
3
|
NoSuchModelError
|
|
4
4
|
} from "@ai-sdk/provider";
|
|
5
5
|
import {
|
|
6
|
+
generateId as generateId2,
|
|
6
7
|
loadApiKey,
|
|
7
8
|
withoutTrailingSlash
|
|
8
9
|
} from "@ai-sdk/provider-utils";
|
|
9
10
|
|
|
10
11
|
// src/anthropic-messages-language-model.ts
|
|
11
12
|
import {
|
|
13
|
+
APICallError,
|
|
12
14
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
13
15
|
} from "@ai-sdk/provider";
|
|
14
16
|
import {
|
|
15
17
|
combineHeaders,
|
|
16
18
|
createEventSourceResponseHandler,
|
|
17
19
|
createJsonResponseHandler,
|
|
20
|
+
generateId,
|
|
18
21
|
parseProviderOptions as parseProviderOptions2,
|
|
19
22
|
postJsonToApi,
|
|
20
23
|
resolve
|
|
@@ -38,6 +41,13 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
38
41
|
|
|
39
42
|
// src/anthropic-messages-options.ts
|
|
40
43
|
import { z as z2 } from "zod";
|
|
44
|
+
var webSearchLocationSchema = z2.object({
|
|
45
|
+
type: z2.literal("approximate"),
|
|
46
|
+
city: z2.string().optional(),
|
|
47
|
+
region: z2.string().optional(),
|
|
48
|
+
country: z2.string(),
|
|
49
|
+
timezone: z2.string().optional()
|
|
50
|
+
});
|
|
41
51
|
var anthropicProviderOptions = z2.object({
|
|
42
52
|
/**
|
|
43
53
|
Include reasoning content in requests sent to the model. Defaults to `true`.
|
|
@@ -48,6 +58,31 @@ var anthropicProviderOptions = z2.object({
|
|
|
48
58
|
thinking: z2.object({
|
|
49
59
|
type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
|
|
50
60
|
budgetTokens: z2.number().optional()
|
|
61
|
+
}).optional(),
|
|
62
|
+
/**
|
|
63
|
+
* Web search tool configuration for Claude models that support it.
|
|
64
|
+
* When provided, automatically adds the web search tool to the request.
|
|
65
|
+
*/
|
|
66
|
+
webSearch: z2.object({
|
|
67
|
+
/**
|
|
68
|
+
* Limit the number of searches per request (optional)
|
|
69
|
+
* Defaults to 5 if not specified
|
|
70
|
+
*/
|
|
71
|
+
maxUses: z2.number().min(1).max(20).optional(),
|
|
72
|
+
/**
|
|
73
|
+
* Only include results from these domains (optional)
|
|
74
|
+
* Cannot be used with blockedDomains
|
|
75
|
+
*/
|
|
76
|
+
allowedDomains: z2.array(z2.string()).optional(),
|
|
77
|
+
/**
|
|
78
|
+
* Never include results from these domains (optional)
|
|
79
|
+
* Cannot be used with allowedDomains
|
|
80
|
+
*/
|
|
81
|
+
blockedDomains: z2.array(z2.string()).optional(),
|
|
82
|
+
/**
|
|
83
|
+
* Localize search results based on user location (optional)
|
|
84
|
+
*/
|
|
85
|
+
userLocation: webSearchLocationSchema.optional()
|
|
51
86
|
}).optional()
|
|
52
87
|
});
|
|
53
88
|
|
|
@@ -55,6 +90,9 @@ var anthropicProviderOptions = z2.object({
|
|
|
55
90
|
import {
|
|
56
91
|
UnsupportedFunctionalityError
|
|
57
92
|
} from "@ai-sdk/provider";
|
|
93
|
+
function isWebSearchTool(tool) {
|
|
94
|
+
return typeof tool === "object" && tool !== null && "type" in tool && tool.type === "web_search_20250305";
|
|
95
|
+
}
|
|
58
96
|
function prepareTools({
|
|
59
97
|
tools,
|
|
60
98
|
toolChoice
|
|
@@ -67,6 +105,10 @@ function prepareTools({
|
|
|
67
105
|
}
|
|
68
106
|
const anthropicTools2 = [];
|
|
69
107
|
for (const tool of tools) {
|
|
108
|
+
if (isWebSearchTool(tool)) {
|
|
109
|
+
anthropicTools2.push(tool);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
70
112
|
switch (tool.type) {
|
|
71
113
|
case "function":
|
|
72
114
|
anthropicTools2.push({
|
|
@@ -480,8 +522,10 @@ function mapAnthropicStopReason({
|
|
|
480
522
|
var AnthropicMessagesLanguageModel = class {
|
|
481
523
|
constructor(modelId, config) {
|
|
482
524
|
this.specificationVersion = "v2";
|
|
525
|
+
var _a;
|
|
483
526
|
this.modelId = modelId;
|
|
484
527
|
this.config = config;
|
|
528
|
+
this.generateId = (_a = config.generateId) != null ? _a : generateId;
|
|
485
529
|
}
|
|
486
530
|
supportsUrl(url) {
|
|
487
531
|
return url.protocol === "https:";
|
|
@@ -611,6 +655,27 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
611
655
|
}
|
|
612
656
|
baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
|
|
613
657
|
}
|
|
658
|
+
let modifiedTools = tools;
|
|
659
|
+
let modifiedToolChoice = toolChoice;
|
|
660
|
+
if (anthropicOptions == null ? void 0 : anthropicOptions.webSearch) {
|
|
661
|
+
const webSearchTool = {
|
|
662
|
+
type: "web_search_20250305",
|
|
663
|
+
name: "web_search",
|
|
664
|
+
max_uses: anthropicOptions.webSearch.maxUses,
|
|
665
|
+
allowed_domains: anthropicOptions.webSearch.allowedDomains,
|
|
666
|
+
blocked_domains: anthropicOptions.webSearch.blockedDomains,
|
|
667
|
+
...anthropicOptions.webSearch.userLocation && {
|
|
668
|
+
user_location: {
|
|
669
|
+
type: anthropicOptions.webSearch.userLocation.type,
|
|
670
|
+
country: anthropicOptions.webSearch.userLocation.country,
|
|
671
|
+
city: anthropicOptions.webSearch.userLocation.city,
|
|
672
|
+
region: anthropicOptions.webSearch.userLocation.region,
|
|
673
|
+
timezone: anthropicOptions.webSearch.userLocation.timezone
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
modifiedTools = tools ? [...tools, webSearchTool] : [webSearchTool];
|
|
678
|
+
}
|
|
614
679
|
const {
|
|
615
680
|
tools: anthropicTools2,
|
|
616
681
|
toolChoice: anthropicToolChoice,
|
|
@@ -620,7 +685,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
620
685
|
jsonResponseTool != null ? {
|
|
621
686
|
tools: [jsonResponseTool],
|
|
622
687
|
toolChoice: { type: "tool", toolName: jsonResponseTool.name }
|
|
623
|
-
} : { tools, toolChoice }
|
|
688
|
+
} : { tools: modifiedTools, toolChoice: modifiedToolChoice }
|
|
624
689
|
);
|
|
625
690
|
return {
|
|
626
691
|
args: {
|
|
@@ -652,7 +717,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
652
717
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
653
718
|
}
|
|
654
719
|
async doGenerate(options) {
|
|
655
|
-
var _a, _b, _c, _d;
|
|
720
|
+
var _a, _b, _c, _d, _e;
|
|
656
721
|
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
657
722
|
const {
|
|
658
723
|
responseHeaders,
|
|
@@ -718,6 +783,38 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
718
783
|
);
|
|
719
784
|
break;
|
|
720
785
|
}
|
|
786
|
+
case "server_tool_use": {
|
|
787
|
+
continue;
|
|
788
|
+
}
|
|
789
|
+
case "web_search_tool_result": {
|
|
790
|
+
if (Array.isArray(part.content)) {
|
|
791
|
+
for (const result of part.content) {
|
|
792
|
+
if (result.type === "web_search_result") {
|
|
793
|
+
content.push({
|
|
794
|
+
type: "source",
|
|
795
|
+
sourceType: "url",
|
|
796
|
+
id: this.generateId(),
|
|
797
|
+
url: result.url,
|
|
798
|
+
title: result.title,
|
|
799
|
+
providerMetadata: {
|
|
800
|
+
anthropic: {
|
|
801
|
+
encryptedContent: result.encrypted_content,
|
|
802
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
} else if (part.content.type === "web_search_tool_result_error") {
|
|
809
|
+
throw new APICallError({
|
|
810
|
+
message: `Web search failed: ${part.content.error_code}`,
|
|
811
|
+
url: "web_search_api",
|
|
812
|
+
requestBodyValues: { tool_use_id: part.tool_use_id },
|
|
813
|
+
data: { error_code: part.content.error_code }
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
break;
|
|
817
|
+
}
|
|
721
818
|
}
|
|
722
819
|
}
|
|
723
820
|
return {
|
|
@@ -730,19 +827,19 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
730
827
|
inputTokens: response.usage.input_tokens,
|
|
731
828
|
outputTokens: response.usage.output_tokens,
|
|
732
829
|
totalTokens: response.usage.input_tokens + response.usage.output_tokens,
|
|
733
|
-
cachedInputTokens: (
|
|
830
|
+
cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
|
|
734
831
|
},
|
|
735
832
|
request: { body: args },
|
|
736
833
|
response: {
|
|
737
|
-
id: (
|
|
738
|
-
modelId: (
|
|
834
|
+
id: (_c = response.id) != null ? _c : void 0,
|
|
835
|
+
modelId: (_d = response.model) != null ? _d : void 0,
|
|
739
836
|
headers: responseHeaders,
|
|
740
837
|
body: rawResponse
|
|
741
838
|
},
|
|
742
839
|
warnings,
|
|
743
840
|
providerMetadata: {
|
|
744
841
|
anthropic: {
|
|
745
|
-
cacheCreationInputTokens: (
|
|
842
|
+
cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null
|
|
746
843
|
}
|
|
747
844
|
}
|
|
748
845
|
};
|
|
@@ -770,6 +867,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
770
867
|
const toolCallContentBlocks = {};
|
|
771
868
|
let providerMetadata = void 0;
|
|
772
869
|
let blockType = void 0;
|
|
870
|
+
const config = this.config;
|
|
871
|
+
const generateId3 = this.generateId;
|
|
773
872
|
return {
|
|
774
873
|
stream: response.pipeThrough(
|
|
775
874
|
new TransformStream({
|
|
@@ -777,7 +876,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
777
876
|
controller.enqueue({ type: "stream-start", warnings });
|
|
778
877
|
},
|
|
779
878
|
transform(chunk, controller) {
|
|
780
|
-
var _a, _b, _c, _d, _e, _f;
|
|
879
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
781
880
|
if (!chunk.success) {
|
|
782
881
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
783
882
|
return;
|
|
@@ -816,6 +915,40 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
816
915
|
};
|
|
817
916
|
return;
|
|
818
917
|
}
|
|
918
|
+
case "server_tool_use": {
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
case "web_search_tool_result": {
|
|
922
|
+
if (Array.isArray(value.content_block.content)) {
|
|
923
|
+
for (const result of value.content_block.content) {
|
|
924
|
+
if (result.type === "web_search_result") {
|
|
925
|
+
controller.enqueue({
|
|
926
|
+
type: "source",
|
|
927
|
+
sourceType: "url",
|
|
928
|
+
id: generateId3(),
|
|
929
|
+
url: result.url,
|
|
930
|
+
title: result.title,
|
|
931
|
+
providerMetadata: {
|
|
932
|
+
anthropic: {
|
|
933
|
+
encryptedContent: result.encrypted_content,
|
|
934
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
} else if (value.content_block.content.type === "web_search_tool_result_error") {
|
|
941
|
+
controller.enqueue({
|
|
942
|
+
type: "error",
|
|
943
|
+
error: {
|
|
944
|
+
type: "web-search-error",
|
|
945
|
+
message: `Web search failed: ${value.content_block.content.error_code}`,
|
|
946
|
+
code: value.content_block.content.error_code
|
|
947
|
+
}
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
return;
|
|
951
|
+
}
|
|
819
952
|
default: {
|
|
820
953
|
const _exhaustiveCheck = contentBlockType;
|
|
821
954
|
throw new Error(
|
|
@@ -878,6 +1011,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
878
1011
|
}
|
|
879
1012
|
case "input_json_delta": {
|
|
880
1013
|
const contentBlock = toolCallContentBlocks[value.index];
|
|
1014
|
+
if (!contentBlock) {
|
|
1015
|
+
return;
|
|
1016
|
+
}
|
|
881
1017
|
controller.enqueue(
|
|
882
1018
|
jsonResponseTool != null ? {
|
|
883
1019
|
type: "text",
|
|
@@ -893,6 +1029,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
893
1029
|
contentBlock.jsonText += value.delta.partial_json;
|
|
894
1030
|
return;
|
|
895
1031
|
}
|
|
1032
|
+
case "citations_delta": {
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
896
1035
|
default: {
|
|
897
1036
|
const _exhaustiveCheck = deltaType;
|
|
898
1037
|
throw new Error(
|
|
@@ -903,22 +1042,22 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
903
1042
|
}
|
|
904
1043
|
case "message_start": {
|
|
905
1044
|
usage.inputTokens = value.message.usage.input_tokens;
|
|
906
|
-
usage.cachedInputTokens = (
|
|
1045
|
+
usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
|
|
907
1046
|
providerMetadata = {
|
|
908
1047
|
anthropic: {
|
|
909
|
-
cacheCreationInputTokens: (
|
|
1048
|
+
cacheCreationInputTokens: (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null
|
|
910
1049
|
}
|
|
911
1050
|
};
|
|
912
1051
|
controller.enqueue({
|
|
913
1052
|
type: "response-metadata",
|
|
914
|
-
id: (
|
|
915
|
-
modelId: (
|
|
1053
|
+
id: (_d = value.message.id) != null ? _d : void 0,
|
|
1054
|
+
modelId: (_e = value.message.model) != null ? _e : void 0
|
|
916
1055
|
});
|
|
917
1056
|
return;
|
|
918
1057
|
}
|
|
919
1058
|
case "message_delta": {
|
|
920
1059
|
usage.outputTokens = value.usage.output_tokens;
|
|
921
|
-
usage.totalTokens = ((
|
|
1060
|
+
usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
|
|
922
1061
|
finishReason = mapAnthropicStopReason({
|
|
923
1062
|
finishReason: value.delta.stop_reason,
|
|
924
1063
|
isJsonResponseFromTool: jsonResponseTool != null
|
|
@@ -975,6 +1114,31 @@ var anthropicMessagesResponseSchema = z3.object({
|
|
|
975
1114
|
id: z3.string(),
|
|
976
1115
|
name: z3.string(),
|
|
977
1116
|
input: z3.unknown()
|
|
1117
|
+
}),
|
|
1118
|
+
z3.object({
|
|
1119
|
+
type: z3.literal("server_tool_use"),
|
|
1120
|
+
id: z3.string(),
|
|
1121
|
+
name: z3.string(),
|
|
1122
|
+
input: z3.record(z3.unknown()).nullish()
|
|
1123
|
+
}),
|
|
1124
|
+
z3.object({
|
|
1125
|
+
type: z3.literal("web_search_tool_result"),
|
|
1126
|
+
tool_use_id: z3.string(),
|
|
1127
|
+
content: z3.union([
|
|
1128
|
+
z3.array(
|
|
1129
|
+
z3.object({
|
|
1130
|
+
type: z3.literal("web_search_result"),
|
|
1131
|
+
url: z3.string(),
|
|
1132
|
+
title: z3.string(),
|
|
1133
|
+
encrypted_content: z3.string(),
|
|
1134
|
+
page_age: z3.string().nullish()
|
|
1135
|
+
})
|
|
1136
|
+
),
|
|
1137
|
+
z3.object({
|
|
1138
|
+
type: z3.literal("web_search_tool_result_error"),
|
|
1139
|
+
error_code: z3.string()
|
|
1140
|
+
})
|
|
1141
|
+
])
|
|
978
1142
|
})
|
|
979
1143
|
])
|
|
980
1144
|
),
|
|
@@ -983,7 +1147,10 @@ var anthropicMessagesResponseSchema = z3.object({
|
|
|
983
1147
|
input_tokens: z3.number(),
|
|
984
1148
|
output_tokens: z3.number(),
|
|
985
1149
|
cache_creation_input_tokens: z3.number().nullish(),
|
|
986
|
-
cache_read_input_tokens: z3.number().nullish()
|
|
1150
|
+
cache_read_input_tokens: z3.number().nullish(),
|
|
1151
|
+
server_tool_use: z3.object({
|
|
1152
|
+
web_search_requests: z3.number()
|
|
1153
|
+
}).nullish()
|
|
987
1154
|
})
|
|
988
1155
|
});
|
|
989
1156
|
var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
@@ -1020,6 +1187,31 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
|
1020
1187
|
z3.object({
|
|
1021
1188
|
type: z3.literal("redacted_thinking"),
|
|
1022
1189
|
data: z3.string()
|
|
1190
|
+
}),
|
|
1191
|
+
z3.object({
|
|
1192
|
+
type: z3.literal("server_tool_use"),
|
|
1193
|
+
id: z3.string(),
|
|
1194
|
+
name: z3.string(),
|
|
1195
|
+
input: z3.record(z3.unknown()).nullish()
|
|
1196
|
+
}),
|
|
1197
|
+
z3.object({
|
|
1198
|
+
type: z3.literal("web_search_tool_result"),
|
|
1199
|
+
tool_use_id: z3.string(),
|
|
1200
|
+
content: z3.union([
|
|
1201
|
+
z3.array(
|
|
1202
|
+
z3.object({
|
|
1203
|
+
type: z3.literal("web_search_result"),
|
|
1204
|
+
url: z3.string(),
|
|
1205
|
+
title: z3.string(),
|
|
1206
|
+
encrypted_content: z3.string(),
|
|
1207
|
+
page_age: z3.string().nullish()
|
|
1208
|
+
})
|
|
1209
|
+
),
|
|
1210
|
+
z3.object({
|
|
1211
|
+
type: z3.literal("web_search_tool_result_error"),
|
|
1212
|
+
error_code: z3.string()
|
|
1213
|
+
})
|
|
1214
|
+
])
|
|
1023
1215
|
})
|
|
1024
1216
|
])
|
|
1025
1217
|
}),
|
|
@@ -1042,6 +1234,16 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
|
1042
1234
|
z3.object({
|
|
1043
1235
|
type: z3.literal("signature_delta"),
|
|
1044
1236
|
signature: z3.string()
|
|
1237
|
+
}),
|
|
1238
|
+
z3.object({
|
|
1239
|
+
type: z3.literal("citations_delta"),
|
|
1240
|
+
citation: z3.object({
|
|
1241
|
+
type: z3.literal("web_search_result_location"),
|
|
1242
|
+
cited_text: z3.string(),
|
|
1243
|
+
url: z3.string(),
|
|
1244
|
+
title: z3.string(),
|
|
1245
|
+
encrypted_index: z3.string()
|
|
1246
|
+
})
|
|
1045
1247
|
})
|
|
1046
1248
|
])
|
|
1047
1249
|
}),
|
|
@@ -1233,15 +1435,19 @@ function createAnthropic(options = {}) {
|
|
|
1233
1435
|
}),
|
|
1234
1436
|
...options.headers
|
|
1235
1437
|
});
|
|
1236
|
-
const createChatModel = (modelId) =>
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1438
|
+
const createChatModel = (modelId) => {
|
|
1439
|
+
var _a2;
|
|
1440
|
+
return new AnthropicMessagesLanguageModel(modelId, {
|
|
1441
|
+
provider: "anthropic.messages",
|
|
1442
|
+
baseURL,
|
|
1443
|
+
headers: getHeaders,
|
|
1444
|
+
fetch: options.fetch,
|
|
1445
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : generateId2,
|
|
1446
|
+
supportedUrls: () => ({
|
|
1447
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
1448
|
+
})
|
|
1449
|
+
});
|
|
1450
|
+
};
|
|
1245
1451
|
const provider = function(modelId) {
|
|
1246
1452
|
if (new.target) {
|
|
1247
1453
|
throw new Error(
|