@ai-sdk/anthropic 4.0.45 → 4.0.47
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 +18 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +498 -150
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +32 -7
- package/dist/internal/index.js +102 -80
- package/dist/internal/index.js.map +1 -1
- package/package.json +3 -3
- package/src/anthropic-api.ts +81 -71
- package/src/anthropic-files.ts +8 -3
- package/src/anthropic-language-model-options.ts +3 -0
- package/src/anthropic-language-model.ts +29 -9
- package/src/anthropic-messages-batch.ts +456 -67
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/anthropic",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.47",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@ai-sdk/provider": "4.0.
|
|
39
|
-
"@ai-sdk/provider-utils": "5.0.
|
|
38
|
+
"@ai-sdk/provider": "4.0.10",
|
|
39
|
+
"@ai-sdk/provider-utils": "5.0.35"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@ai-sdk/test-server": "2.0.1",
|
package/src/anthropic-api.ts
CHANGED
|
@@ -416,7 +416,13 @@ export interface AnthropicMcpToolResultContent {
|
|
|
416
416
|
type: 'mcp_tool_result';
|
|
417
417
|
tool_use_id: string;
|
|
418
418
|
is_error: boolean;
|
|
419
|
-
content:
|
|
419
|
+
content:
|
|
420
|
+
| string
|
|
421
|
+
| Array<{
|
|
422
|
+
type: 'text';
|
|
423
|
+
text: string;
|
|
424
|
+
citations?: Citation[] | null;
|
|
425
|
+
}>;
|
|
420
426
|
cache_control: AnthropicCacheControl | undefined;
|
|
421
427
|
}
|
|
422
428
|
|
|
@@ -655,6 +661,68 @@ const anthropicToolCallCallerSchema = z.union([
|
|
|
655
661
|
}),
|
|
656
662
|
]);
|
|
657
663
|
|
|
664
|
+
const anthropicCitationSchema = z.discriminatedUnion('type', [
|
|
665
|
+
z.object({
|
|
666
|
+
type: z.literal('web_search_result_location'),
|
|
667
|
+
cited_text: z.string(),
|
|
668
|
+
url: z.string(),
|
|
669
|
+
title: z.string().nullable(),
|
|
670
|
+
encrypted_index: z.string(),
|
|
671
|
+
}),
|
|
672
|
+
z.object({
|
|
673
|
+
type: z.literal('page_location'),
|
|
674
|
+
cited_text: z.string(),
|
|
675
|
+
document_index: z.number(),
|
|
676
|
+
document_title: z.string().nullable(),
|
|
677
|
+
start_page_number: z.number(),
|
|
678
|
+
end_page_number: z.number(),
|
|
679
|
+
file_id: z.string().nullish(),
|
|
680
|
+
}),
|
|
681
|
+
z.object({
|
|
682
|
+
type: z.literal('char_location'),
|
|
683
|
+
cited_text: z.string(),
|
|
684
|
+
document_index: z.number(),
|
|
685
|
+
document_title: z.string().nullable(),
|
|
686
|
+
start_char_index: z.number(),
|
|
687
|
+
end_char_index: z.number(),
|
|
688
|
+
file_id: z.string().nullish(),
|
|
689
|
+
}),
|
|
690
|
+
z.object({
|
|
691
|
+
type: z.literal('content_block_location'),
|
|
692
|
+
cited_text: z.string(),
|
|
693
|
+
document_index: z.number(),
|
|
694
|
+
document_title: z.string().nullable(),
|
|
695
|
+
start_block_index: z.number(),
|
|
696
|
+
end_block_index: z.number(),
|
|
697
|
+
file_id: z.string().nullable(),
|
|
698
|
+
}),
|
|
699
|
+
z.object({
|
|
700
|
+
type: z.literal('search_result_location'),
|
|
701
|
+
cited_text: z.string(),
|
|
702
|
+
search_result_index: z.number(),
|
|
703
|
+
source: z.string(),
|
|
704
|
+
title: z.string().nullable(),
|
|
705
|
+
start_block_index: z.number(),
|
|
706
|
+
end_block_index: z.number(),
|
|
707
|
+
}),
|
|
708
|
+
]);
|
|
709
|
+
|
|
710
|
+
const anthropicMcpToolResultContentSchema = z.union([
|
|
711
|
+
z.string(),
|
|
712
|
+
z.array(
|
|
713
|
+
z.union([
|
|
714
|
+
z.string(),
|
|
715
|
+
z.object({
|
|
716
|
+
type: z.literal('text'),
|
|
717
|
+
text: z.string(),
|
|
718
|
+
// MCP tool-result citations are opaque third-party pass-through data.
|
|
719
|
+
// Keep them permissive so new shapes do not reject whole responses.
|
|
720
|
+
citations: z.array(z.json()).nullable().optional(),
|
|
721
|
+
}),
|
|
722
|
+
]),
|
|
723
|
+
),
|
|
724
|
+
]);
|
|
725
|
+
|
|
658
726
|
// limited version of the schema, focussed on what is needed for the implementation
|
|
659
727
|
// this approach limits breakages when the API changes and increases efficiency
|
|
660
728
|
export const anthropicResponseSchema = lazySchema(() =>
|
|
@@ -668,35 +736,7 @@ export const anthropicResponseSchema = lazySchema(() =>
|
|
|
668
736
|
z.object({
|
|
669
737
|
type: z.literal('text'),
|
|
670
738
|
text: z.string(),
|
|
671
|
-
citations: z
|
|
672
|
-
.array(
|
|
673
|
-
z.discriminatedUnion('type', [
|
|
674
|
-
z.object({
|
|
675
|
-
type: z.literal('web_search_result_location'),
|
|
676
|
-
cited_text: z.string(),
|
|
677
|
-
url: z.string(),
|
|
678
|
-
title: z.string(),
|
|
679
|
-
encrypted_index: z.string(),
|
|
680
|
-
}),
|
|
681
|
-
z.object({
|
|
682
|
-
type: z.literal('page_location'),
|
|
683
|
-
cited_text: z.string(),
|
|
684
|
-
document_index: z.number(),
|
|
685
|
-
document_title: z.string().nullable(),
|
|
686
|
-
start_page_number: z.number(),
|
|
687
|
-
end_page_number: z.number(),
|
|
688
|
-
}),
|
|
689
|
-
z.object({
|
|
690
|
-
type: z.literal('char_location'),
|
|
691
|
-
cited_text: z.string(),
|
|
692
|
-
document_index: z.number(),
|
|
693
|
-
document_title: z.string().nullable(),
|
|
694
|
-
start_char_index: z.number(),
|
|
695
|
-
end_char_index: z.number(),
|
|
696
|
-
}),
|
|
697
|
-
]),
|
|
698
|
-
)
|
|
699
|
-
.optional(),
|
|
739
|
+
citations: z.array(anthropicCitationSchema).nullable().optional(),
|
|
700
740
|
}),
|
|
701
741
|
z.object({
|
|
702
742
|
type: z.literal('thinking'),
|
|
@@ -737,12 +777,7 @@ export const anthropicResponseSchema = lazySchema(() =>
|
|
|
737
777
|
type: z.literal('mcp_tool_result'),
|
|
738
778
|
tool_use_id: z.string(),
|
|
739
779
|
is_error: z.boolean(),
|
|
740
|
-
content:
|
|
741
|
-
z.union([
|
|
742
|
-
z.string(),
|
|
743
|
-
z.object({ type: z.literal('text'), text: z.string() }),
|
|
744
|
-
]),
|
|
745
|
-
),
|
|
780
|
+
content: anthropicMcpToolResultContentSchema,
|
|
746
781
|
}),
|
|
747
782
|
z.object({
|
|
748
783
|
type: z.literal('web_fetch_tool_result'),
|
|
@@ -752,7 +787,7 @@ export const anthropicResponseSchema = lazySchema(() =>
|
|
|
752
787
|
z.object({
|
|
753
788
|
type: z.literal('web_fetch_result'),
|
|
754
789
|
url: z.string(),
|
|
755
|
-
retrieved_at: z.string(),
|
|
790
|
+
retrieved_at: z.string().nullable(),
|
|
756
791
|
content: z.object({
|
|
757
792
|
type: z.literal('document'),
|
|
758
793
|
title: z.string().nullable(),
|
|
@@ -786,7 +821,7 @@ export const anthropicResponseSchema = lazySchema(() =>
|
|
|
786
821
|
z.object({
|
|
787
822
|
type: z.literal('web_search_result'),
|
|
788
823
|
url: z.string(),
|
|
789
|
-
title: z.string(),
|
|
824
|
+
title: z.string().nullable(),
|
|
790
825
|
encrypted_content: z.string(),
|
|
791
826
|
page_age: z.string().nullish(),
|
|
792
827
|
}),
|
|
@@ -914,6 +949,10 @@ export const anthropicResponseSchema = lazySchema(() =>
|
|
|
914
949
|
}),
|
|
915
950
|
]),
|
|
916
951
|
}),
|
|
952
|
+
z.object({
|
|
953
|
+
type: z.literal('container_upload'),
|
|
954
|
+
file_id: z.string(),
|
|
955
|
+
}),
|
|
917
956
|
// advisor results for advisor_20260301:
|
|
918
957
|
z.object({
|
|
919
958
|
type: z.literal('advisor_tool_result'),
|
|
@@ -1100,12 +1139,7 @@ export const anthropicChunkSchema = lazySchema(() =>
|
|
|
1100
1139
|
type: z.literal('mcp_tool_result'),
|
|
1101
1140
|
tool_use_id: z.string(),
|
|
1102
1141
|
is_error: z.boolean(),
|
|
1103
|
-
content:
|
|
1104
|
-
z.union([
|
|
1105
|
-
z.string(),
|
|
1106
|
-
z.object({ type: z.literal('text'), text: z.string() }),
|
|
1107
|
-
]),
|
|
1108
|
-
),
|
|
1142
|
+
content: anthropicMcpToolResultContentSchema,
|
|
1109
1143
|
}),
|
|
1110
1144
|
z.object({
|
|
1111
1145
|
type: z.literal('web_fetch_tool_result'),
|
|
@@ -1115,7 +1149,7 @@ export const anthropicChunkSchema = lazySchema(() =>
|
|
|
1115
1149
|
z.object({
|
|
1116
1150
|
type: z.literal('web_fetch_result'),
|
|
1117
1151
|
url: z.string(),
|
|
1118
|
-
retrieved_at: z.string(),
|
|
1152
|
+
retrieved_at: z.string().nullable(),
|
|
1119
1153
|
content: z.object({
|
|
1120
1154
|
type: z.literal('document'),
|
|
1121
1155
|
title: z.string().nullable(),
|
|
@@ -1149,7 +1183,7 @@ export const anthropicChunkSchema = lazySchema(() =>
|
|
|
1149
1183
|
z.object({
|
|
1150
1184
|
type: z.literal('web_search_result'),
|
|
1151
1185
|
url: z.string(),
|
|
1152
|
-
title: z.string(),
|
|
1186
|
+
title: z.string().nullable(),
|
|
1153
1187
|
encrypted_content: z.string(),
|
|
1154
1188
|
page_age: z.string().nullish(),
|
|
1155
1189
|
}),
|
|
@@ -1331,31 +1365,7 @@ export const anthropicChunkSchema = lazySchema(() =>
|
|
|
1331
1365
|
}),
|
|
1332
1366
|
z.object({
|
|
1333
1367
|
type: z.literal('citations_delta'),
|
|
1334
|
-
citation:
|
|
1335
|
-
z.object({
|
|
1336
|
-
type: z.literal('web_search_result_location'),
|
|
1337
|
-
cited_text: z.string(),
|
|
1338
|
-
url: z.string(),
|
|
1339
|
-
title: z.string(),
|
|
1340
|
-
encrypted_index: z.string(),
|
|
1341
|
-
}),
|
|
1342
|
-
z.object({
|
|
1343
|
-
type: z.literal('page_location'),
|
|
1344
|
-
cited_text: z.string(),
|
|
1345
|
-
document_index: z.number(),
|
|
1346
|
-
document_title: z.string().nullable(),
|
|
1347
|
-
start_page_number: z.number(),
|
|
1348
|
-
end_page_number: z.number(),
|
|
1349
|
-
}),
|
|
1350
|
-
z.object({
|
|
1351
|
-
type: z.literal('char_location'),
|
|
1352
|
-
cited_text: z.string(),
|
|
1353
|
-
document_index: z.number(),
|
|
1354
|
-
document_title: z.string().nullable(),
|
|
1355
|
-
start_char_index: z.number(),
|
|
1356
|
-
end_char_index: z.number(),
|
|
1357
|
-
}),
|
|
1358
|
-
]),
|
|
1368
|
+
citation: anthropicCitationSchema,
|
|
1359
1369
|
}),
|
|
1360
1370
|
]),
|
|
1361
1371
|
}),
|
package/src/anthropic-files.ts
CHANGED
|
@@ -49,6 +49,8 @@ export class AnthropicFiles implements FilesV4 {
|
|
|
49
49
|
data,
|
|
50
50
|
mediaType,
|
|
51
51
|
filename,
|
|
52
|
+
abortSignal,
|
|
53
|
+
headers,
|
|
52
54
|
}: FilesV4UploadFileCallOptions): Promise<FilesV4UploadFileResult> {
|
|
53
55
|
const fileBytes = convertInlineFileDataToUint8Array(data);
|
|
54
56
|
|
|
@@ -63,14 +65,17 @@ export class AnthropicFiles implements FilesV4 {
|
|
|
63
65
|
|
|
64
66
|
const { value: response } = await postFormDataToApi({
|
|
65
67
|
url: `${this.config.baseURL}/files`,
|
|
66
|
-
headers: combineHeaders(
|
|
67
|
-
|
|
68
|
-
|
|
68
|
+
headers: combineHeaders(
|
|
69
|
+
this.config.headers(),
|
|
70
|
+
{ 'anthropic-beta': 'files-api-2025-04-14' },
|
|
71
|
+
headers,
|
|
72
|
+
),
|
|
69
73
|
formData,
|
|
70
74
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
71
75
|
successfulResponseHandler: createJsonResponseHandler(
|
|
72
76
|
anthropicUploadFileResponseSchema,
|
|
73
77
|
),
|
|
78
|
+
abortSignal,
|
|
74
79
|
fetch: this.config.fetch,
|
|
75
80
|
});
|
|
76
81
|
|
|
@@ -268,6 +268,9 @@ export const anthropicLanguageModelOptions = z.object({
|
|
|
268
268
|
*/
|
|
269
269
|
speed: z.enum(['fast', 'standard']).optional(),
|
|
270
270
|
|
|
271
|
+
/** Selects Anthropic's service tier for the request. */
|
|
272
|
+
serviceTier: z.enum(['auto', 'standard_only']).optional(),
|
|
273
|
+
|
|
271
274
|
/**
|
|
272
275
|
* Controls where model inference runs for this request.
|
|
273
276
|
*
|
|
@@ -118,7 +118,7 @@ function getAnthropicStreamErrorMetadata(type: string): {
|
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
function createCitationSource(
|
|
121
|
+
export function createCitationSource(
|
|
122
122
|
citation: Citation,
|
|
123
123
|
citationDocuments: Array<{
|
|
124
124
|
title: string;
|
|
@@ -133,7 +133,7 @@ function createCitationSource(
|
|
|
133
133
|
sourceType: 'url' as const,
|
|
134
134
|
id: generateId(),
|
|
135
135
|
url: citation.url,
|
|
136
|
-
title: citation.title,
|
|
136
|
+
title: citation.title ?? undefined,
|
|
137
137
|
providerMetadata: {
|
|
138
138
|
anthropic: {
|
|
139
139
|
citedText: citation.cited_text,
|
|
@@ -226,7 +226,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
|
|
|
226
226
|
readonly modelId: AnthropicModelId;
|
|
227
227
|
|
|
228
228
|
protected readonly config: AnthropicLanguageModelConfig;
|
|
229
|
-
|
|
229
|
+
protected readonly generateId: () => string;
|
|
230
230
|
|
|
231
231
|
static [WORKFLOW_SERIALIZE](model: AnthropicLanguageModel) {
|
|
232
232
|
return serializeModelOptions({
|
|
@@ -600,6 +600,9 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
|
|
|
600
600
|
...(anthropicOptions?.speed && {
|
|
601
601
|
speed: anthropicOptions.speed,
|
|
602
602
|
}),
|
|
603
|
+
...(anthropicOptions?.serviceTier && {
|
|
604
|
+
service_tier: anthropicOptions.serviceTier,
|
|
605
|
+
}),
|
|
603
606
|
...(anthropicOptions?.inferenceGeo && {
|
|
604
607
|
inference_geo: anthropicOptions.inferenceGeo,
|
|
605
608
|
}),
|
|
@@ -1104,6 +1107,14 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
|
|
|
1104
1107
|
});
|
|
1105
1108
|
break;
|
|
1106
1109
|
}
|
|
1110
|
+
case 'container_upload': {
|
|
1111
|
+
content.push({
|
|
1112
|
+
type: 'custom',
|
|
1113
|
+
kind: 'anthropic.container_upload',
|
|
1114
|
+
providerMetadata: { anthropic: { fileId: part.file_id } },
|
|
1115
|
+
});
|
|
1116
|
+
break;
|
|
1117
|
+
}
|
|
1107
1118
|
case 'compaction': {
|
|
1108
1119
|
content.push({
|
|
1109
1120
|
type: 'text',
|
|
@@ -1301,7 +1312,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
|
|
|
1301
1312
|
toolName: toolNameMapping.toCustomToolName('web_search'),
|
|
1302
1313
|
result: part.content.map(result => ({
|
|
1303
1314
|
url: result.url,
|
|
1304
|
-
title: result.title,
|
|
1315
|
+
...(result.title != null ? { title: result.title } : {}),
|
|
1305
1316
|
pageAge: result.page_age ?? null,
|
|
1306
1317
|
encryptedContent: result.encrypted_content,
|
|
1307
1318
|
type: result.type,
|
|
@@ -1315,7 +1326,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
|
|
|
1315
1326
|
sourceType: 'url',
|
|
1316
1327
|
id: this.generateId(),
|
|
1317
1328
|
url: result.url,
|
|
1318
|
-
title: result.title,
|
|
1329
|
+
...(result.title != null ? { title: result.title } : {}),
|
|
1319
1330
|
providerMetadata: {
|
|
1320
1331
|
anthropic: {
|
|
1321
1332
|
pageAge: result.page_age ?? null,
|
|
@@ -2011,7 +2022,9 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
|
|
|
2011
2022
|
toolName: toolNameMapping.toCustomToolName('web_search'),
|
|
2012
2023
|
result: part.content.map(result => ({
|
|
2013
2024
|
url: result.url,
|
|
2014
|
-
|
|
2025
|
+
...(result.title != null
|
|
2026
|
+
? { title: result.title }
|
|
2027
|
+
: {}),
|
|
2015
2028
|
pageAge: result.page_age ?? null,
|
|
2016
2029
|
encryptedContent: result.encrypted_content,
|
|
2017
2030
|
type: result.type,
|
|
@@ -2025,7 +2038,9 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
|
|
|
2025
2038
|
sourceType: 'url',
|
|
2026
2039
|
id: generateId(),
|
|
2027
2040
|
url: result.url,
|
|
2028
|
-
|
|
2041
|
+
...(result.title != null
|
|
2042
|
+
? { title: result.title }
|
|
2043
|
+
: {}),
|
|
2029
2044
|
providerMetadata: {
|
|
2030
2045
|
anthropic: {
|
|
2031
2046
|
pageAge: result.page_age ?? null,
|
|
@@ -2921,7 +2936,7 @@ export function getModelCapabilities(modelId: string): {
|
|
|
2921
2936
|
}
|
|
2922
2937
|
}
|
|
2923
2938
|
|
|
2924
|
-
function hasWebTool20260209WithoutCodeExecution(
|
|
2939
|
+
export function hasWebTool20260209WithoutCodeExecution(
|
|
2925
2940
|
tools: AnthropicTool[] | undefined,
|
|
2926
2941
|
): boolean {
|
|
2927
2942
|
if (!tools) {
|
|
@@ -2938,7 +2953,12 @@ function hasWebTool20260209WithoutCodeExecution(
|
|
|
2938
2953
|
hasWebTool20260209 = true;
|
|
2939
2954
|
continue;
|
|
2940
2955
|
}
|
|
2941
|
-
if (
|
|
2956
|
+
if (
|
|
2957
|
+
'type' in tool &&
|
|
2958
|
+
(tool.type === 'code_execution_20250522' ||
|
|
2959
|
+
tool.type === 'code_execution_20250825' ||
|
|
2960
|
+
tool.type === 'code_execution_20260120')
|
|
2961
|
+
) {
|
|
2942
2962
|
hasCodeExecutionTool = true;
|
|
2943
2963
|
break;
|
|
2944
2964
|
}
|