@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.mjs
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
// src/anthropic-messages-language-model.ts
|
|
2
2
|
import {
|
|
3
|
+
APICallError,
|
|
3
4
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
4
5
|
} from "@ai-sdk/provider";
|
|
5
6
|
import {
|
|
6
7
|
combineHeaders,
|
|
7
8
|
createEventSourceResponseHandler,
|
|
8
9
|
createJsonResponseHandler,
|
|
10
|
+
generateId,
|
|
9
11
|
parseProviderOptions as parseProviderOptions2,
|
|
10
12
|
postJsonToApi,
|
|
11
13
|
resolve
|
|
@@ -29,6 +31,13 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
29
31
|
|
|
30
32
|
// src/anthropic-messages-options.ts
|
|
31
33
|
import { z as z2 } from "zod";
|
|
34
|
+
var webSearchLocationSchema = z2.object({
|
|
35
|
+
type: z2.literal("approximate"),
|
|
36
|
+
city: z2.string().optional(),
|
|
37
|
+
region: z2.string().optional(),
|
|
38
|
+
country: z2.string(),
|
|
39
|
+
timezone: z2.string().optional()
|
|
40
|
+
});
|
|
32
41
|
var anthropicProviderOptions = z2.object({
|
|
33
42
|
/**
|
|
34
43
|
Include reasoning content in requests sent to the model. Defaults to `true`.
|
|
@@ -39,6 +48,31 @@ var anthropicProviderOptions = z2.object({
|
|
|
39
48
|
thinking: z2.object({
|
|
40
49
|
type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
|
|
41
50
|
budgetTokens: z2.number().optional()
|
|
51
|
+
}).optional(),
|
|
52
|
+
/**
|
|
53
|
+
* Web search tool configuration for Claude models that support it.
|
|
54
|
+
* When provided, automatically adds the web search tool to the request.
|
|
55
|
+
*/
|
|
56
|
+
webSearch: z2.object({
|
|
57
|
+
/**
|
|
58
|
+
* Limit the number of searches per request (optional)
|
|
59
|
+
* Defaults to 5 if not specified
|
|
60
|
+
*/
|
|
61
|
+
maxUses: z2.number().min(1).max(20).optional(),
|
|
62
|
+
/**
|
|
63
|
+
* Only include results from these domains (optional)
|
|
64
|
+
* Cannot be used with blockedDomains
|
|
65
|
+
*/
|
|
66
|
+
allowedDomains: z2.array(z2.string()).optional(),
|
|
67
|
+
/**
|
|
68
|
+
* Never include results from these domains (optional)
|
|
69
|
+
* Cannot be used with allowedDomains
|
|
70
|
+
*/
|
|
71
|
+
blockedDomains: z2.array(z2.string()).optional(),
|
|
72
|
+
/**
|
|
73
|
+
* Localize search results based on user location (optional)
|
|
74
|
+
*/
|
|
75
|
+
userLocation: webSearchLocationSchema.optional()
|
|
42
76
|
}).optional()
|
|
43
77
|
});
|
|
44
78
|
|
|
@@ -46,6 +80,9 @@ var anthropicProviderOptions = z2.object({
|
|
|
46
80
|
import {
|
|
47
81
|
UnsupportedFunctionalityError
|
|
48
82
|
} from "@ai-sdk/provider";
|
|
83
|
+
function isWebSearchTool(tool) {
|
|
84
|
+
return typeof tool === "object" && tool !== null && "type" in tool && tool.type === "web_search_20250305";
|
|
85
|
+
}
|
|
49
86
|
function prepareTools({
|
|
50
87
|
tools,
|
|
51
88
|
toolChoice
|
|
@@ -58,6 +95,10 @@ function prepareTools({
|
|
|
58
95
|
}
|
|
59
96
|
const anthropicTools2 = [];
|
|
60
97
|
for (const tool of tools) {
|
|
98
|
+
if (isWebSearchTool(tool)) {
|
|
99
|
+
anthropicTools2.push(tool);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
61
102
|
switch (tool.type) {
|
|
62
103
|
case "function":
|
|
63
104
|
anthropicTools2.push({
|
|
@@ -471,8 +512,10 @@ function mapAnthropicStopReason({
|
|
|
471
512
|
var AnthropicMessagesLanguageModel = class {
|
|
472
513
|
constructor(modelId, config) {
|
|
473
514
|
this.specificationVersion = "v2";
|
|
515
|
+
var _a;
|
|
474
516
|
this.modelId = modelId;
|
|
475
517
|
this.config = config;
|
|
518
|
+
this.generateId = (_a = config.generateId) != null ? _a : generateId;
|
|
476
519
|
}
|
|
477
520
|
supportsUrl(url) {
|
|
478
521
|
return url.protocol === "https:";
|
|
@@ -602,6 +645,27 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
602
645
|
}
|
|
603
646
|
baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
|
|
604
647
|
}
|
|
648
|
+
let modifiedTools = tools;
|
|
649
|
+
let modifiedToolChoice = toolChoice;
|
|
650
|
+
if (anthropicOptions == null ? void 0 : anthropicOptions.webSearch) {
|
|
651
|
+
const webSearchTool = {
|
|
652
|
+
type: "web_search_20250305",
|
|
653
|
+
name: "web_search",
|
|
654
|
+
max_uses: anthropicOptions.webSearch.maxUses,
|
|
655
|
+
allowed_domains: anthropicOptions.webSearch.allowedDomains,
|
|
656
|
+
blocked_domains: anthropicOptions.webSearch.blockedDomains,
|
|
657
|
+
...anthropicOptions.webSearch.userLocation && {
|
|
658
|
+
user_location: {
|
|
659
|
+
type: anthropicOptions.webSearch.userLocation.type,
|
|
660
|
+
country: anthropicOptions.webSearch.userLocation.country,
|
|
661
|
+
city: anthropicOptions.webSearch.userLocation.city,
|
|
662
|
+
region: anthropicOptions.webSearch.userLocation.region,
|
|
663
|
+
timezone: anthropicOptions.webSearch.userLocation.timezone
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
};
|
|
667
|
+
modifiedTools = tools ? [...tools, webSearchTool] : [webSearchTool];
|
|
668
|
+
}
|
|
605
669
|
const {
|
|
606
670
|
tools: anthropicTools2,
|
|
607
671
|
toolChoice: anthropicToolChoice,
|
|
@@ -611,7 +675,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
611
675
|
jsonResponseTool != null ? {
|
|
612
676
|
tools: [jsonResponseTool],
|
|
613
677
|
toolChoice: { type: "tool", toolName: jsonResponseTool.name }
|
|
614
|
-
} : { tools, toolChoice }
|
|
678
|
+
} : { tools: modifiedTools, toolChoice: modifiedToolChoice }
|
|
615
679
|
);
|
|
616
680
|
return {
|
|
617
681
|
args: {
|
|
@@ -643,7 +707,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
643
707
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
644
708
|
}
|
|
645
709
|
async doGenerate(options) {
|
|
646
|
-
var _a, _b, _c, _d;
|
|
710
|
+
var _a, _b, _c, _d, _e;
|
|
647
711
|
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
648
712
|
const {
|
|
649
713
|
responseHeaders,
|
|
@@ -709,6 +773,38 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
709
773
|
);
|
|
710
774
|
break;
|
|
711
775
|
}
|
|
776
|
+
case "server_tool_use": {
|
|
777
|
+
continue;
|
|
778
|
+
}
|
|
779
|
+
case "web_search_tool_result": {
|
|
780
|
+
if (Array.isArray(part.content)) {
|
|
781
|
+
for (const result of part.content) {
|
|
782
|
+
if (result.type === "web_search_result") {
|
|
783
|
+
content.push({
|
|
784
|
+
type: "source",
|
|
785
|
+
sourceType: "url",
|
|
786
|
+
id: this.generateId(),
|
|
787
|
+
url: result.url,
|
|
788
|
+
title: result.title,
|
|
789
|
+
providerMetadata: {
|
|
790
|
+
anthropic: {
|
|
791
|
+
encryptedContent: result.encrypted_content,
|
|
792
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
} else if (part.content.type === "web_search_tool_result_error") {
|
|
799
|
+
throw new APICallError({
|
|
800
|
+
message: `Web search failed: ${part.content.error_code}`,
|
|
801
|
+
url: "web_search_api",
|
|
802
|
+
requestBodyValues: { tool_use_id: part.tool_use_id },
|
|
803
|
+
data: { error_code: part.content.error_code }
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
break;
|
|
807
|
+
}
|
|
712
808
|
}
|
|
713
809
|
}
|
|
714
810
|
return {
|
|
@@ -721,19 +817,19 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
721
817
|
inputTokens: response.usage.input_tokens,
|
|
722
818
|
outputTokens: response.usage.output_tokens,
|
|
723
819
|
totalTokens: response.usage.input_tokens + response.usage.output_tokens,
|
|
724
|
-
cachedInputTokens: (
|
|
820
|
+
cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
|
|
725
821
|
},
|
|
726
822
|
request: { body: args },
|
|
727
823
|
response: {
|
|
728
|
-
id: (
|
|
729
|
-
modelId: (
|
|
824
|
+
id: (_c = response.id) != null ? _c : void 0,
|
|
825
|
+
modelId: (_d = response.model) != null ? _d : void 0,
|
|
730
826
|
headers: responseHeaders,
|
|
731
827
|
body: rawResponse
|
|
732
828
|
},
|
|
733
829
|
warnings,
|
|
734
830
|
providerMetadata: {
|
|
735
831
|
anthropic: {
|
|
736
|
-
cacheCreationInputTokens: (
|
|
832
|
+
cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null
|
|
737
833
|
}
|
|
738
834
|
}
|
|
739
835
|
};
|
|
@@ -761,6 +857,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
761
857
|
const toolCallContentBlocks = {};
|
|
762
858
|
let providerMetadata = void 0;
|
|
763
859
|
let blockType = void 0;
|
|
860
|
+
const config = this.config;
|
|
861
|
+
const generateId2 = this.generateId;
|
|
764
862
|
return {
|
|
765
863
|
stream: response.pipeThrough(
|
|
766
864
|
new TransformStream({
|
|
@@ -768,7 +866,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
768
866
|
controller.enqueue({ type: "stream-start", warnings });
|
|
769
867
|
},
|
|
770
868
|
transform(chunk, controller) {
|
|
771
|
-
var _a, _b, _c, _d, _e, _f;
|
|
869
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
772
870
|
if (!chunk.success) {
|
|
773
871
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
774
872
|
return;
|
|
@@ -807,6 +905,40 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
807
905
|
};
|
|
808
906
|
return;
|
|
809
907
|
}
|
|
908
|
+
case "server_tool_use": {
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
case "web_search_tool_result": {
|
|
912
|
+
if (Array.isArray(value.content_block.content)) {
|
|
913
|
+
for (const result of value.content_block.content) {
|
|
914
|
+
if (result.type === "web_search_result") {
|
|
915
|
+
controller.enqueue({
|
|
916
|
+
type: "source",
|
|
917
|
+
sourceType: "url",
|
|
918
|
+
id: generateId2(),
|
|
919
|
+
url: result.url,
|
|
920
|
+
title: result.title,
|
|
921
|
+
providerMetadata: {
|
|
922
|
+
anthropic: {
|
|
923
|
+
encryptedContent: result.encrypted_content,
|
|
924
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
} else if (value.content_block.content.type === "web_search_tool_result_error") {
|
|
931
|
+
controller.enqueue({
|
|
932
|
+
type: "error",
|
|
933
|
+
error: {
|
|
934
|
+
type: "web-search-error",
|
|
935
|
+
message: `Web search failed: ${value.content_block.content.error_code}`,
|
|
936
|
+
code: value.content_block.content.error_code
|
|
937
|
+
}
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
return;
|
|
941
|
+
}
|
|
810
942
|
default: {
|
|
811
943
|
const _exhaustiveCheck = contentBlockType;
|
|
812
944
|
throw new Error(
|
|
@@ -869,6 +1001,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
869
1001
|
}
|
|
870
1002
|
case "input_json_delta": {
|
|
871
1003
|
const contentBlock = toolCallContentBlocks[value.index];
|
|
1004
|
+
if (!contentBlock) {
|
|
1005
|
+
return;
|
|
1006
|
+
}
|
|
872
1007
|
controller.enqueue(
|
|
873
1008
|
jsonResponseTool != null ? {
|
|
874
1009
|
type: "text",
|
|
@@ -884,6 +1019,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
884
1019
|
contentBlock.jsonText += value.delta.partial_json;
|
|
885
1020
|
return;
|
|
886
1021
|
}
|
|
1022
|
+
case "citations_delta": {
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
887
1025
|
default: {
|
|
888
1026
|
const _exhaustiveCheck = deltaType;
|
|
889
1027
|
throw new Error(
|
|
@@ -894,22 +1032,22 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
894
1032
|
}
|
|
895
1033
|
case "message_start": {
|
|
896
1034
|
usage.inputTokens = value.message.usage.input_tokens;
|
|
897
|
-
usage.cachedInputTokens = (
|
|
1035
|
+
usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
|
|
898
1036
|
providerMetadata = {
|
|
899
1037
|
anthropic: {
|
|
900
|
-
cacheCreationInputTokens: (
|
|
1038
|
+
cacheCreationInputTokens: (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null
|
|
901
1039
|
}
|
|
902
1040
|
};
|
|
903
1041
|
controller.enqueue({
|
|
904
1042
|
type: "response-metadata",
|
|
905
|
-
id: (
|
|
906
|
-
modelId: (
|
|
1043
|
+
id: (_d = value.message.id) != null ? _d : void 0,
|
|
1044
|
+
modelId: (_e = value.message.model) != null ? _e : void 0
|
|
907
1045
|
});
|
|
908
1046
|
return;
|
|
909
1047
|
}
|
|
910
1048
|
case "message_delta": {
|
|
911
1049
|
usage.outputTokens = value.usage.output_tokens;
|
|
912
|
-
usage.totalTokens = ((
|
|
1050
|
+
usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
|
|
913
1051
|
finishReason = mapAnthropicStopReason({
|
|
914
1052
|
finishReason: value.delta.stop_reason,
|
|
915
1053
|
isJsonResponseFromTool: jsonResponseTool != null
|
|
@@ -966,6 +1104,31 @@ var anthropicMessagesResponseSchema = z3.object({
|
|
|
966
1104
|
id: z3.string(),
|
|
967
1105
|
name: z3.string(),
|
|
968
1106
|
input: z3.unknown()
|
|
1107
|
+
}),
|
|
1108
|
+
z3.object({
|
|
1109
|
+
type: z3.literal("server_tool_use"),
|
|
1110
|
+
id: z3.string(),
|
|
1111
|
+
name: z3.string(),
|
|
1112
|
+
input: z3.record(z3.unknown()).nullish()
|
|
1113
|
+
}),
|
|
1114
|
+
z3.object({
|
|
1115
|
+
type: z3.literal("web_search_tool_result"),
|
|
1116
|
+
tool_use_id: z3.string(),
|
|
1117
|
+
content: z3.union([
|
|
1118
|
+
z3.array(
|
|
1119
|
+
z3.object({
|
|
1120
|
+
type: z3.literal("web_search_result"),
|
|
1121
|
+
url: z3.string(),
|
|
1122
|
+
title: z3.string(),
|
|
1123
|
+
encrypted_content: z3.string(),
|
|
1124
|
+
page_age: z3.string().nullish()
|
|
1125
|
+
})
|
|
1126
|
+
),
|
|
1127
|
+
z3.object({
|
|
1128
|
+
type: z3.literal("web_search_tool_result_error"),
|
|
1129
|
+
error_code: z3.string()
|
|
1130
|
+
})
|
|
1131
|
+
])
|
|
969
1132
|
})
|
|
970
1133
|
])
|
|
971
1134
|
),
|
|
@@ -974,7 +1137,10 @@ var anthropicMessagesResponseSchema = z3.object({
|
|
|
974
1137
|
input_tokens: z3.number(),
|
|
975
1138
|
output_tokens: z3.number(),
|
|
976
1139
|
cache_creation_input_tokens: z3.number().nullish(),
|
|
977
|
-
cache_read_input_tokens: z3.number().nullish()
|
|
1140
|
+
cache_read_input_tokens: z3.number().nullish(),
|
|
1141
|
+
server_tool_use: z3.object({
|
|
1142
|
+
web_search_requests: z3.number()
|
|
1143
|
+
}).nullish()
|
|
978
1144
|
})
|
|
979
1145
|
});
|
|
980
1146
|
var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
@@ -1011,6 +1177,31 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
|
1011
1177
|
z3.object({
|
|
1012
1178
|
type: z3.literal("redacted_thinking"),
|
|
1013
1179
|
data: z3.string()
|
|
1180
|
+
}),
|
|
1181
|
+
z3.object({
|
|
1182
|
+
type: z3.literal("server_tool_use"),
|
|
1183
|
+
id: z3.string(),
|
|
1184
|
+
name: z3.string(),
|
|
1185
|
+
input: z3.record(z3.unknown()).nullish()
|
|
1186
|
+
}),
|
|
1187
|
+
z3.object({
|
|
1188
|
+
type: z3.literal("web_search_tool_result"),
|
|
1189
|
+
tool_use_id: z3.string(),
|
|
1190
|
+
content: z3.union([
|
|
1191
|
+
z3.array(
|
|
1192
|
+
z3.object({
|
|
1193
|
+
type: z3.literal("web_search_result"),
|
|
1194
|
+
url: z3.string(),
|
|
1195
|
+
title: z3.string(),
|
|
1196
|
+
encrypted_content: z3.string(),
|
|
1197
|
+
page_age: z3.string().nullish()
|
|
1198
|
+
})
|
|
1199
|
+
),
|
|
1200
|
+
z3.object({
|
|
1201
|
+
type: z3.literal("web_search_tool_result_error"),
|
|
1202
|
+
error_code: z3.string()
|
|
1203
|
+
})
|
|
1204
|
+
])
|
|
1014
1205
|
})
|
|
1015
1206
|
])
|
|
1016
1207
|
}),
|
|
@@ -1033,6 +1224,16 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
|
1033
1224
|
z3.object({
|
|
1034
1225
|
type: z3.literal("signature_delta"),
|
|
1035
1226
|
signature: z3.string()
|
|
1227
|
+
}),
|
|
1228
|
+
z3.object({
|
|
1229
|
+
type: z3.literal("citations_delta"),
|
|
1230
|
+
citation: z3.object({
|
|
1231
|
+
type: z3.literal("web_search_result_location"),
|
|
1232
|
+
cited_text: z3.string(),
|
|
1233
|
+
url: z3.string(),
|
|
1234
|
+
title: z3.string(),
|
|
1235
|
+
encrypted_index: z3.string()
|
|
1236
|
+
})
|
|
1036
1237
|
})
|
|
1037
1238
|
])
|
|
1038
1239
|
}),
|