@ai-sdk/anthropic 3.0.133 → 3.0.135
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 +16 -0
- package/dist/index.d.mts +121 -156
- package/dist/index.d.ts +121 -156
- package/dist/index.js +569 -537
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +526 -491
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +175 -139
- package/dist/internal/index.d.ts +175 -139
- package/dist/internal/index.js +562 -530
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +525 -490
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/anthropic-messages-api.ts +73 -57
- package/src/anthropic-messages-language-model.ts +31 -8
- package/src/anthropic-messages-options.ts +386 -367
- package/src/convert-to-anthropic-messages-prompt.ts +22 -0
- package/src/get-cache-control.ts +15 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/anthropic",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.135",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@ai-sdk/provider": "3.0.19",
|
|
40
|
-
"@ai-sdk/provider-utils": "4.0.
|
|
40
|
+
"@ai-sdk/provider-utils": "4.0.62"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/node": "20.17.24",
|
|
@@ -48,6 +48,7 @@ export interface AnthropicUserMessage {
|
|
|
48
48
|
content: Array<
|
|
49
49
|
| AnthropicTextContent
|
|
50
50
|
| AnthropicImageContent
|
|
51
|
+
| AnthropicVideoContent
|
|
51
52
|
| AnthropicDocumentContent
|
|
52
53
|
| AnthropicToolResultContent
|
|
53
54
|
>;
|
|
@@ -126,6 +127,13 @@ export interface AnthropicImageContent {
|
|
|
126
127
|
cache_control: AnthropicCacheControl | undefined;
|
|
127
128
|
}
|
|
128
129
|
|
|
130
|
+
/** Video blocks supplied by compatible providers through custom media conversion. */
|
|
131
|
+
export interface AnthropicVideoContent {
|
|
132
|
+
type: 'video';
|
|
133
|
+
source: AnthropicContentSource;
|
|
134
|
+
cache_control: AnthropicCacheControl | undefined;
|
|
135
|
+
}
|
|
136
|
+
|
|
129
137
|
export interface AnthropicDocumentContent {
|
|
130
138
|
type: 'document';
|
|
131
139
|
source: AnthropicContentSource;
|
|
@@ -641,55 +649,61 @@ export type AnthropicResponseContextManagement = {
|
|
|
641
649
|
applied_edits: AnthropicResponseContextManagementEdit[];
|
|
642
650
|
};
|
|
643
651
|
|
|
644
|
-
const anthropicStopDetailsSchema =
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
652
|
+
const anthropicStopDetailsSchema = () =>
|
|
653
|
+
z.object({
|
|
654
|
+
type: z.string(),
|
|
655
|
+
category: z.string().nullish(),
|
|
656
|
+
explanation: z.string().nullish(),
|
|
657
|
+
recommended_model: z.string().nullish(),
|
|
658
|
+
});
|
|
650
659
|
|
|
651
|
-
export type AnthropicStopDetails = z.infer<
|
|
660
|
+
export type AnthropicStopDetails = z.infer<
|
|
661
|
+
ReturnType<typeof anthropicStopDetailsSchema>
|
|
662
|
+
>;
|
|
652
663
|
|
|
653
|
-
const anthropicSafeguardResultSchema =
|
|
654
|
-
|
|
655
|
-
status: z.object({
|
|
664
|
+
const anthropicSafeguardResultSchema = () =>
|
|
665
|
+
z.object({
|
|
656
666
|
type: z.string(),
|
|
657
|
-
|
|
658
|
-
.
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
667
|
+
status: z.object({
|
|
668
|
+
type: z.string(),
|
|
669
|
+
tool_uses: z
|
|
670
|
+
.record(
|
|
671
|
+
z.string(),
|
|
672
|
+
z.object({
|
|
673
|
+
type: z.string(),
|
|
674
|
+
outcome: z.string().nullish(),
|
|
675
|
+
explanation: z.string().nullish(),
|
|
676
|
+
}),
|
|
677
|
+
)
|
|
678
|
+
.nullish(),
|
|
679
|
+
}),
|
|
680
|
+
});
|
|
669
681
|
|
|
670
682
|
export type AnthropicSafeguardResult = z.infer<
|
|
671
|
-
typeof anthropicSafeguardResultSchema
|
|
683
|
+
ReturnType<typeof anthropicSafeguardResultSchema>
|
|
672
684
|
>;
|
|
673
685
|
|
|
674
|
-
const anthropicToolCallCallerSchema =
|
|
675
|
-
z.
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
686
|
+
const anthropicToolCallCallerSchema = () =>
|
|
687
|
+
z.union([
|
|
688
|
+
z.object({
|
|
689
|
+
type: z.literal('code_execution_20250825'),
|
|
690
|
+
tool_id: z.string(),
|
|
691
|
+
}),
|
|
692
|
+
z.object({
|
|
693
|
+
type: z.literal('code_execution_20260120'),
|
|
694
|
+
tool_id: z.string(),
|
|
695
|
+
}),
|
|
696
|
+
z.object({
|
|
697
|
+
type: z.literal('direct'),
|
|
698
|
+
}),
|
|
699
|
+
]);
|
|
687
700
|
|
|
688
|
-
const anthropicInputTransformationSchema =
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
701
|
+
const anthropicInputTransformationSchema = () =>
|
|
702
|
+
z.object({
|
|
703
|
+
type: z.string(),
|
|
704
|
+
path: z.string(),
|
|
705
|
+
reason: z.string(),
|
|
706
|
+
});
|
|
693
707
|
|
|
694
708
|
// limited version of the schema, focussed on what is needed for the implementation
|
|
695
709
|
// this approach limits breakages when the API changes and increases efficiency
|
|
@@ -753,7 +767,7 @@ export const anthropicMessagesResponseSchema = lazySchema(() =>
|
|
|
753
767
|
name: z.string(),
|
|
754
768
|
input: z.unknown(),
|
|
755
769
|
// Programmatic tool calling: caller info when triggered from code execution
|
|
756
|
-
caller: anthropicToolCallCallerSchema.optional(),
|
|
770
|
+
caller: anthropicToolCallCallerSchema().optional(),
|
|
757
771
|
// Toolsets (e.g. computer toolset): name of the toolset this member call belongs to
|
|
758
772
|
toolset_name: z.string().nullish(),
|
|
759
773
|
}),
|
|
@@ -762,7 +776,7 @@ export const anthropicMessagesResponseSchema = lazySchema(() =>
|
|
|
762
776
|
id: z.string(),
|
|
763
777
|
name: z.string(),
|
|
764
778
|
input: z.record(z.string(), z.unknown()).nullish(),
|
|
765
|
-
caller: anthropicToolCallCallerSchema.optional(),
|
|
779
|
+
caller: anthropicToolCallCallerSchema().optional(),
|
|
766
780
|
}),
|
|
767
781
|
z.object({
|
|
768
782
|
type: z.literal('mcp_tool_use'),
|
|
@@ -785,7 +799,7 @@ export const anthropicMessagesResponseSchema = lazySchema(() =>
|
|
|
785
799
|
z.object({
|
|
786
800
|
type: z.literal('web_fetch_tool_result'),
|
|
787
801
|
tool_use_id: z.string(),
|
|
788
|
-
caller: anthropicToolCallCallerSchema.optional(),
|
|
802
|
+
caller: anthropicToolCallCallerSchema().optional(),
|
|
789
803
|
content: z.union([
|
|
790
804
|
z.object({
|
|
791
805
|
type: z.literal('web_fetch_result'),
|
|
@@ -818,7 +832,7 @@ export const anthropicMessagesResponseSchema = lazySchema(() =>
|
|
|
818
832
|
z.object({
|
|
819
833
|
type: z.literal('web_search_tool_result'),
|
|
820
834
|
tool_use_id: z.string(),
|
|
821
|
-
caller: anthropicToolCallCallerSchema.optional(),
|
|
835
|
+
caller: anthropicToolCallCallerSchema().optional(),
|
|
822
836
|
content: z.union([
|
|
823
837
|
z.array(
|
|
824
838
|
z.object({
|
|
@@ -981,11 +995,11 @@ export const anthropicMessagesResponseSchema = lazySchema(() =>
|
|
|
981
995
|
),
|
|
982
996
|
stop_reason: z.string().nullish(),
|
|
983
997
|
stop_sequence: z.string().nullish(),
|
|
984
|
-
stop_details: anthropicStopDetailsSchema.nullish(),
|
|
998
|
+
stop_details: anthropicStopDetailsSchema().nullish(),
|
|
985
999
|
input_transformations: z
|
|
986
|
-
.array(anthropicInputTransformationSchema)
|
|
1000
|
+
.array(anthropicInputTransformationSchema())
|
|
987
1001
|
.nullish(),
|
|
988
|
-
safeguard_results: z.array(anthropicSafeguardResultSchema).nullish(),
|
|
1002
|
+
safeguard_results: z.array(anthropicSafeguardResultSchema()).nullish(),
|
|
989
1003
|
usage: z.looseObject({
|
|
990
1004
|
input_tokens: z.number(),
|
|
991
1005
|
output_tokens: z.number(),
|
|
@@ -1079,7 +1093,7 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
|
|
|
1079
1093
|
id: z.string(),
|
|
1080
1094
|
name: z.string(),
|
|
1081
1095
|
input: z.unknown(),
|
|
1082
|
-
caller: anthropicToolCallCallerSchema.optional(),
|
|
1096
|
+
caller: anthropicToolCallCallerSchema().optional(),
|
|
1083
1097
|
toolset_name: z.string().nullish(),
|
|
1084
1098
|
}),
|
|
1085
1099
|
]),
|
|
@@ -1087,7 +1101,7 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
|
|
|
1087
1101
|
.nullish(),
|
|
1088
1102
|
stop_reason: z.string().nullish(),
|
|
1089
1103
|
input_transformations: z
|
|
1090
|
-
.array(anthropicInputTransformationSchema)
|
|
1104
|
+
.array(anthropicInputTransformationSchema())
|
|
1091
1105
|
.nullish(),
|
|
1092
1106
|
container: z
|
|
1093
1107
|
.object({
|
|
@@ -1116,7 +1130,7 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
|
|
|
1116
1130
|
// Programmatic tool calling: input may be present directly for deferred tool calls
|
|
1117
1131
|
input: z.record(z.string(), z.unknown()).optional(),
|
|
1118
1132
|
// Programmatic tool calling: caller info when triggered from code execution
|
|
1119
|
-
caller: anthropicToolCallCallerSchema.optional(),
|
|
1133
|
+
caller: anthropicToolCallCallerSchema().optional(),
|
|
1120
1134
|
// Toolsets (e.g. computer toolset): name of the toolset this member call belongs to
|
|
1121
1135
|
toolset_name: z.string().nullish(),
|
|
1122
1136
|
}),
|
|
@@ -1133,7 +1147,7 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
|
|
|
1133
1147
|
id: z.string(),
|
|
1134
1148
|
name: z.string(),
|
|
1135
1149
|
input: z.record(z.string(), z.unknown()).nullish(),
|
|
1136
|
-
caller: anthropicToolCallCallerSchema.optional(),
|
|
1150
|
+
caller: anthropicToolCallCallerSchema().optional(),
|
|
1137
1151
|
}),
|
|
1138
1152
|
z.object({
|
|
1139
1153
|
type: z.literal('mcp_tool_use'),
|
|
@@ -1156,7 +1170,7 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
|
|
|
1156
1170
|
z.object({
|
|
1157
1171
|
type: z.literal('web_fetch_tool_result'),
|
|
1158
1172
|
tool_use_id: z.string(),
|
|
1159
|
-
caller: anthropicToolCallCallerSchema.optional(),
|
|
1173
|
+
caller: anthropicToolCallCallerSchema().optional(),
|
|
1160
1174
|
content: z.union([
|
|
1161
1175
|
z.object({
|
|
1162
1176
|
type: z.literal('web_fetch_result'),
|
|
@@ -1189,7 +1203,7 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
|
|
|
1189
1203
|
z.object({
|
|
1190
1204
|
type: z.literal('web_search_tool_result'),
|
|
1191
1205
|
tool_use_id: z.string(),
|
|
1192
|
-
caller: anthropicToolCallCallerSchema.optional(),
|
|
1206
|
+
caller: anthropicToolCallCallerSchema().optional(),
|
|
1193
1207
|
content: z.union([
|
|
1194
1208
|
z.array(
|
|
1195
1209
|
z.object({
|
|
@@ -1419,8 +1433,10 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
|
|
|
1419
1433
|
delta: z.object({
|
|
1420
1434
|
stop_reason: z.string().nullish(),
|
|
1421
1435
|
stop_sequence: z.string().nullish(),
|
|
1422
|
-
stop_details: anthropicStopDetailsSchema.nullish(),
|
|
1423
|
-
safeguard_results: z
|
|
1436
|
+
stop_details: anthropicStopDetailsSchema().nullish(),
|
|
1437
|
+
safeguard_results: z
|
|
1438
|
+
.array(anthropicSafeguardResultSchema())
|
|
1439
|
+
.nullish(),
|
|
1424
1440
|
container: z
|
|
1425
1441
|
.object({
|
|
1426
1442
|
expires_at: z.string(),
|
|
@@ -1469,7 +1485,7 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
|
|
|
1469
1485
|
.nullish(),
|
|
1470
1486
|
}),
|
|
1471
1487
|
input_transformations: z
|
|
1472
|
-
.array(anthropicInputTransformationSchema)
|
|
1488
|
+
.array(anthropicInputTransformationSchema())
|
|
1473
1489
|
.nullish(),
|
|
1474
1490
|
context_management: z
|
|
1475
1491
|
.object({
|
|
@@ -56,7 +56,10 @@ import {
|
|
|
56
56
|
convertAnthropicMessagesUsage,
|
|
57
57
|
type AnthropicMessagesUsage,
|
|
58
58
|
} from './convert-anthropic-messages-usage';
|
|
59
|
-
import {
|
|
59
|
+
import {
|
|
60
|
+
convertToAnthropicMessagesPrompt,
|
|
61
|
+
type AnthropicMediaPartConverter,
|
|
62
|
+
} from './convert-to-anthropic-messages-prompt';
|
|
60
63
|
import { CacheControlValidator } from './get-cache-control';
|
|
61
64
|
import { mapAnthropicStopReason } from './map-anthropic-stop-reason';
|
|
62
65
|
import { sanitizeJsonSchema } from './sanitize-json-schema';
|
|
@@ -186,6 +189,14 @@ type AnthropicMessagesConfig = {
|
|
|
186
189
|
headers: Resolvable<Record<string, string | undefined>>;
|
|
187
190
|
fetch?: FetchFunction;
|
|
188
191
|
buildRequestUrl?: (baseURL: string, isStreaming: boolean) => string;
|
|
192
|
+
/** Custom media conversion for compatible providers. */
|
|
193
|
+
convertMediaPart?: AnthropicMediaPartConverter;
|
|
194
|
+
/** Maximum temperature for compatible providers. Defaults to 1. */
|
|
195
|
+
maxTemperature?: number;
|
|
196
|
+
/** Preserve temperature and topP with thinking for compatible providers. */
|
|
197
|
+
supportsTemperatureAndTopPWithThinking?: boolean;
|
|
198
|
+
/** Cache option namespace for compatible providers. Defaults to anthropic. */
|
|
199
|
+
cacheControlProviderOptionsName?: string;
|
|
189
200
|
transformRequestBody?: (
|
|
190
201
|
args: Record<string, any>,
|
|
191
202
|
betas: Set<string>,
|
|
@@ -278,18 +289,24 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
278
289
|
warnings.push({ type: 'unsupported', feature: 'seed' });
|
|
279
290
|
}
|
|
280
291
|
|
|
281
|
-
|
|
292
|
+
const maxTemperature = this.config.maxTemperature ?? 1;
|
|
293
|
+
const temperatureProvider =
|
|
294
|
+
this.config.maxTemperature == null
|
|
295
|
+
? 'anthropic'
|
|
296
|
+
: this.providerOptionsName;
|
|
297
|
+
|
|
298
|
+
if (temperature != null && temperature > maxTemperature) {
|
|
282
299
|
warnings.push({
|
|
283
300
|
type: 'unsupported',
|
|
284
301
|
feature: 'temperature',
|
|
285
|
-
details: `${temperature} exceeds
|
|
302
|
+
details: `${temperature} exceeds ${temperatureProvider} maximum of ${maxTemperature.toFixed(1)}. clamped to ${maxTemperature.toFixed(1)}`,
|
|
286
303
|
});
|
|
287
|
-
temperature =
|
|
304
|
+
temperature = maxTemperature;
|
|
288
305
|
} else if (temperature != null && temperature < 0) {
|
|
289
306
|
warnings.push({
|
|
290
307
|
type: 'unsupported',
|
|
291
308
|
feature: 'temperature',
|
|
292
|
-
details: `${temperature} is below
|
|
309
|
+
details: `${temperature} is below ${temperatureProvider} minimum of 0. clamped to 0`,
|
|
293
310
|
});
|
|
294
311
|
temperature = 0;
|
|
295
312
|
}
|
|
@@ -448,7 +465,9 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
448
465
|
const contextManagement = anthropicOptions?.contextManagement;
|
|
449
466
|
|
|
450
467
|
// Create a shared cache control validator to track breakpoints across tools and messages
|
|
451
|
-
const cacheControlValidator = new CacheControlValidator(
|
|
468
|
+
const cacheControlValidator = new CacheControlValidator(
|
|
469
|
+
this.config.cacheControlProviderOptionsName,
|
|
470
|
+
);
|
|
452
471
|
|
|
453
472
|
const toolNameMapping = createToolNameMapping({
|
|
454
473
|
tools,
|
|
@@ -487,6 +506,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
487
506
|
cacheControlValidator,
|
|
488
507
|
toolNameMapping,
|
|
489
508
|
toolsetNames,
|
|
509
|
+
convertMediaPart: this.config.convertMediaPart,
|
|
490
510
|
});
|
|
491
511
|
|
|
492
512
|
// Some models always run adaptive thinking and reject `disabled` thinking
|
|
@@ -799,7 +819,10 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
799
819
|
thinkingBudget = 1024;
|
|
800
820
|
}
|
|
801
821
|
|
|
802
|
-
if (
|
|
822
|
+
if (
|
|
823
|
+
baseArgs.temperature != null &&
|
|
824
|
+
!this.config.supportsTemperatureAndTopPWithThinking
|
|
825
|
+
) {
|
|
803
826
|
baseArgs.temperature = undefined;
|
|
804
827
|
warnings.push({
|
|
805
828
|
type: 'unsupported',
|
|
@@ -817,7 +840,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
817
840
|
});
|
|
818
841
|
}
|
|
819
842
|
|
|
820
|
-
if (topP != null) {
|
|
843
|
+
if (topP != null && !this.config.supportsTemperatureAndTopPWithThinking) {
|
|
821
844
|
baseArgs.top_p = undefined;
|
|
822
845
|
warnings.push({
|
|
823
846
|
type: 'unsupported',
|