@ai-sdk/anthropic 4.0.97 → 4.0.98
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.ts +20 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +189 -43
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +276 -9
- package/dist/internal/index.d.ts.map +1 -1
- package/dist/internal/index.js +184 -43
- package/dist/internal/index.js.map +1 -1
- package/docs/05-anthropic.mdx +205 -1
- package/package.json +1 -1
- package/src/anthropic-api.ts +19 -6
- package/src/anthropic-batch.ts +6 -0
- package/src/anthropic-error.ts +7 -0
- package/src/anthropic-language-model-options.ts +46 -5
- package/src/anthropic-language-model.ts +106 -12
- package/src/anthropic-model-catalog.ts +4 -0
- package/src/anthropic-prepare-tools.ts +21 -9
- package/src/anthropic-provider.ts +1 -0
- package/src/convert-to-anthropic-prompt.ts +136 -2
- package/src/get-cache-control.ts +6 -1
- package/src/internal/index.ts +6 -0
|
@@ -27,6 +27,7 @@ export interface AnthropicToolOptions {
|
|
|
27
27
|
export async function prepareTools({
|
|
28
28
|
tools,
|
|
29
29
|
toolChoice,
|
|
30
|
+
hasInlineTools = false,
|
|
30
31
|
disableParallelToolUse,
|
|
31
32
|
cacheControlValidator,
|
|
32
33
|
supportsStructuredOutput,
|
|
@@ -36,6 +37,13 @@ export async function prepareTools({
|
|
|
36
37
|
}: {
|
|
37
38
|
tools: LanguageModelV4CallOptions['tools'];
|
|
38
39
|
toolChoice: LanguageModelV4CallOptions['toolChoice'] | undefined;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Whether tools are added by value in the prompt (mid-conversation tool
|
|
43
|
+
* changes). The model has tools even when `tools` is empty, so the tool
|
|
44
|
+
* choice and parallel tool use settings still need to be sent.
|
|
45
|
+
*/
|
|
46
|
+
hasInlineTools?: boolean;
|
|
39
47
|
disableParallelToolUse?: boolean;
|
|
40
48
|
cacheControlValidator?: CacheControlValidator;
|
|
41
49
|
|
|
@@ -74,13 +82,13 @@ export async function prepareTools({
|
|
|
74
82
|
const betas = new Set<string>();
|
|
75
83
|
const validator = cacheControlValidator || new CacheControlValidator();
|
|
76
84
|
|
|
77
|
-
if (tools == null) {
|
|
85
|
+
if (tools == null && !hasInlineTools) {
|
|
78
86
|
return { tools: undefined, toolChoice: undefined, toolWarnings, betas };
|
|
79
87
|
}
|
|
80
88
|
|
|
81
89
|
const anthropicTools: AnthropicTool[] = [];
|
|
82
90
|
|
|
83
|
-
for (const tool of tools) {
|
|
91
|
+
for (const tool of tools ?? []) {
|
|
84
92
|
switch (tool.type) {
|
|
85
93
|
case 'function': {
|
|
86
94
|
const cacheControl = validator.getCacheControl(tool.providerOptions, {
|
|
@@ -464,9 +472,13 @@ export async function prepareTools({
|
|
|
464
472
|
}
|
|
465
473
|
}
|
|
466
474
|
|
|
475
|
+
// when the user passed no tools (they exist only in tool changes inside the
|
|
476
|
+
// messages), omit `tools` instead of sending an empty array
|
|
477
|
+
const declaredTools = tools == null ? undefined : anthropicTools;
|
|
478
|
+
|
|
467
479
|
if (toolChoice == null) {
|
|
468
480
|
return {
|
|
469
|
-
tools:
|
|
481
|
+
tools: declaredTools,
|
|
470
482
|
toolChoice: disableParallelToolUse
|
|
471
483
|
? { type: 'auto', disable_parallel_tool_use: disableParallelToolUse }
|
|
472
484
|
: undefined,
|
|
@@ -480,7 +492,7 @@ export async function prepareTools({
|
|
|
480
492
|
switch (type) {
|
|
481
493
|
case 'auto':
|
|
482
494
|
return {
|
|
483
|
-
tools:
|
|
495
|
+
tools: declaredTools,
|
|
484
496
|
toolChoice: {
|
|
485
497
|
type: 'auto',
|
|
486
498
|
disable_parallel_tool_use: disableParallelToolUse,
|
|
@@ -498,7 +510,7 @@ export async function prepareTools({
|
|
|
498
510
|
`Using 'auto' instead. Instruct the model to use a tool in the prompt and verify that a tool call was made.`,
|
|
499
511
|
});
|
|
500
512
|
return {
|
|
501
|
-
tools:
|
|
513
|
+
tools: declaredTools,
|
|
502
514
|
toolChoice: {
|
|
503
515
|
type: 'auto',
|
|
504
516
|
disable_parallel_tool_use: disableParallelToolUse,
|
|
@@ -508,7 +520,7 @@ export async function prepareTools({
|
|
|
508
520
|
};
|
|
509
521
|
}
|
|
510
522
|
return {
|
|
511
|
-
tools:
|
|
523
|
+
tools: declaredTools,
|
|
512
524
|
toolChoice: {
|
|
513
525
|
type: 'any',
|
|
514
526
|
disable_parallel_tool_use: disableParallelToolUse,
|
|
@@ -518,7 +530,7 @@ export async function prepareTools({
|
|
|
518
530
|
};
|
|
519
531
|
case 'none':
|
|
520
532
|
return {
|
|
521
|
-
tools:
|
|
533
|
+
tools: declaredTools,
|
|
522
534
|
toolChoice: { type: 'none' },
|
|
523
535
|
toolWarnings,
|
|
524
536
|
betas,
|
|
@@ -534,7 +546,7 @@ export async function prepareTools({
|
|
|
534
546
|
`Instruct the model to use the tool in the prompt and verify that a tool call was made.`,
|
|
535
547
|
});
|
|
536
548
|
return {
|
|
537
|
-
tools:
|
|
549
|
+
tools: declaredTools?.filter(
|
|
538
550
|
tool => 'name' in tool && tool.name === toolChoice.toolName,
|
|
539
551
|
),
|
|
540
552
|
toolChoice: {
|
|
@@ -546,7 +558,7 @@ export async function prepareTools({
|
|
|
546
558
|
};
|
|
547
559
|
}
|
|
548
560
|
return {
|
|
549
|
-
tools:
|
|
561
|
+
tools: declaredTools,
|
|
550
562
|
toolChoice: {
|
|
551
563
|
type: 'tool',
|
|
552
564
|
name: toolChoice.toolName,
|
|
@@ -3,6 +3,8 @@ import {
|
|
|
3
3
|
type JSONObject,
|
|
4
4
|
type JSONValue,
|
|
5
5
|
type LanguageModelV4FilePart,
|
|
6
|
+
type LanguageModelV4FunctionTool,
|
|
7
|
+
type LanguageModelV4ProviderTool,
|
|
6
8
|
type SharedV4Warning,
|
|
7
9
|
type LanguageModelV4Message,
|
|
8
10
|
type LanguageModelV4Prompt,
|
|
@@ -30,6 +32,7 @@ import {
|
|
|
30
32
|
type AnthropicPrompt,
|
|
31
33
|
type AnthropicSystemMessage,
|
|
32
34
|
type AnthropicTextContent,
|
|
35
|
+
type AnthropicTool,
|
|
33
36
|
type AnthropicToolChangeContent,
|
|
34
37
|
type AnthropicToolCallCaller,
|
|
35
38
|
type AnthropicToolResultContent,
|
|
@@ -89,6 +92,66 @@ export type AnthropicMediaPartConverter = (
|
|
|
89
92
|
| undefined
|
|
90
93
|
>;
|
|
91
94
|
|
|
95
|
+
// The computer use tools cannot be defined in a message (the API rejects
|
|
96
|
+
// them), they must be declared in `tools` and added by reference.
|
|
97
|
+
function isComputerTool(
|
|
98
|
+
tool: LanguageModelV4FunctionTool | LanguageModelV4ProviderTool,
|
|
99
|
+
): boolean {
|
|
100
|
+
return tool.type === 'provider' && tool.id.startsWith('anthropic.computer_');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Collects the tools that mid-conversation tool changes add by value, so they
|
|
105
|
+
* can take part in tool name mapping like the tools of the `tools` option.
|
|
106
|
+
* Tools that cannot be added by value are left out because they are ignored.
|
|
107
|
+
*/
|
|
108
|
+
export async function getInlineToolsFromPrompt(
|
|
109
|
+
prompt: LanguageModelV4Prompt,
|
|
110
|
+
): Promise<Array<LanguageModelV4FunctionTool | LanguageModelV4ProviderTool>> {
|
|
111
|
+
const inlineTools: Array<
|
|
112
|
+
LanguageModelV4FunctionTool | LanguageModelV4ProviderTool
|
|
113
|
+
> = [];
|
|
114
|
+
|
|
115
|
+
for (const message of prompt) {
|
|
116
|
+
if (message.role !== 'system' || message.providerOptions == null) {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const options = await parseProviderOptions({
|
|
121
|
+
provider: 'anthropic',
|
|
122
|
+
providerOptions: message.providerOptions,
|
|
123
|
+
schema: anthropicSystemMessageProviderOptions,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
for (const toolChange of options?.toolChanges ?? []) {
|
|
127
|
+
if ('tool' in toolChange) {
|
|
128
|
+
const tool = toolChange.tool as
|
|
129
|
+
| LanguageModelV4FunctionTool
|
|
130
|
+
| LanguageModelV4ProviderTool;
|
|
131
|
+
|
|
132
|
+
if (!isComputerTool(tool)) {
|
|
133
|
+
inlineTools.push(tool);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return inlineTools;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Converts a tool that is defined by value in a mid-conversation tool change
|
|
144
|
+
* into the Anthropic tool definition, the same way entries of the top-level
|
|
145
|
+
* `tools` option are converted.
|
|
146
|
+
*/
|
|
147
|
+
export type AnthropicInlineToolConverter = (
|
|
148
|
+
tool: LanguageModelV4FunctionTool | LanguageModelV4ProviderTool,
|
|
149
|
+
) => Promise<{
|
|
150
|
+
definition: AnthropicTool | undefined;
|
|
151
|
+
warnings: SharedV4Warning[];
|
|
152
|
+
betas: Set<string>;
|
|
153
|
+
}>;
|
|
154
|
+
|
|
92
155
|
export async function convertToAnthropicPrompt({
|
|
93
156
|
prompt,
|
|
94
157
|
sendReasoning,
|
|
@@ -97,6 +160,7 @@ export async function convertToAnthropicPrompt({
|
|
|
97
160
|
toolNameMapping,
|
|
98
161
|
toolsetNames = {},
|
|
99
162
|
convertMediaPart,
|
|
163
|
+
convertInlineTool,
|
|
100
164
|
}: {
|
|
101
165
|
prompt: LanguageModelV4Prompt;
|
|
102
166
|
sendReasoning: boolean;
|
|
@@ -111,6 +175,12 @@ export async function convertToAnthropicPrompt({
|
|
|
111
175
|
* serialized as toolset member calls.
|
|
112
176
|
*/
|
|
113
177
|
toolsetNames?: Record<string, string>;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Converts tools that are added by value in mid-conversation tool changes.
|
|
181
|
+
* Required when the prompt contains such additions.
|
|
182
|
+
*/
|
|
183
|
+
convertInlineTool?: AnthropicInlineToolConverter;
|
|
114
184
|
}): Promise<{
|
|
115
185
|
prompt: AnthropicPrompt;
|
|
116
186
|
betas: Set<string>;
|
|
@@ -174,6 +244,7 @@ export async function convertToAnthropicPrompt({
|
|
|
174
244
|
clearAt?: 'next_user_message';
|
|
175
245
|
effort?: 'low' | 'medium' | 'high' | 'xhigh' | 'max';
|
|
176
246
|
toolChangeCount: number;
|
|
247
|
+
hasInlineToolDefinition: boolean;
|
|
177
248
|
}> = [];
|
|
178
249
|
|
|
179
250
|
for (const { content: text, providerOptions } of block.messages) {
|
|
@@ -203,7 +274,56 @@ export async function convertToAnthropicPrompt({
|
|
|
203
274
|
});
|
|
204
275
|
}
|
|
205
276
|
|
|
277
|
+
let toolChangeCount = 0;
|
|
278
|
+
let hasInlineToolDefinition = false;
|
|
279
|
+
|
|
206
280
|
for (const toolChange of toolChanges) {
|
|
281
|
+
if ('tool' in toolChange) {
|
|
282
|
+
if (convertInlineTool == null) {
|
|
283
|
+
throw new UnsupportedFunctionalityError({
|
|
284
|
+
functionality:
|
|
285
|
+
'tool additions by value in mid-conversation tool changes',
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const tool = toolChange.tool as
|
|
290
|
+
| LanguageModelV4FunctionTool
|
|
291
|
+
| LanguageModelV4ProviderTool;
|
|
292
|
+
|
|
293
|
+
if (isComputerTool(tool)) {
|
|
294
|
+
warnings.push({
|
|
295
|
+
type: 'other',
|
|
296
|
+
message:
|
|
297
|
+
`The tool '${tool.name}' cannot be added by value in a tool change. ` +
|
|
298
|
+
'Declare it in the tools option and add it by reference with toolName. ' +
|
|
299
|
+
'The tool change has been ignored.',
|
|
300
|
+
});
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const {
|
|
305
|
+
definition,
|
|
306
|
+
warnings: toolWarnings,
|
|
307
|
+
betas: toolBetas,
|
|
308
|
+
} = await convertInlineTool(tool);
|
|
309
|
+
warnings.push(...toolWarnings);
|
|
310
|
+
for (const beta of toolBetas) {
|
|
311
|
+
betas.add(beta);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (definition == null) {
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
content.push({
|
|
319
|
+
type: 'tool_addition',
|
|
320
|
+
tool: { type: 'tool_definition', definition },
|
|
321
|
+
} satisfies AnthropicToolChangeContent);
|
|
322
|
+
toolChangeCount++;
|
|
323
|
+
hasInlineToolDefinition = true;
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
|
|
207
327
|
content.push({
|
|
208
328
|
type: toolChange.type,
|
|
209
329
|
tool: {
|
|
@@ -211,13 +331,24 @@ export async function convertToAnthropicPrompt({
|
|
|
211
331
|
name: toolNameMapping.toProviderToolName(toolChange.toolName),
|
|
212
332
|
},
|
|
213
333
|
} satisfies AnthropicToolChangeContent);
|
|
334
|
+
toolChangeCount++;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Every change was ignored and nothing else is left to send.
|
|
338
|
+
if (
|
|
339
|
+
content.length === 0 &&
|
|
340
|
+
systemMessageOptions?.clearAt == null &&
|
|
341
|
+
systemMessageOptions?.effort == null
|
|
342
|
+
) {
|
|
343
|
+
continue;
|
|
214
344
|
}
|
|
215
345
|
|
|
216
346
|
convertedMessages.push({
|
|
217
347
|
content,
|
|
218
348
|
clearAt: systemMessageOptions?.clearAt,
|
|
219
349
|
effort: systemMessageOptions?.effort,
|
|
220
|
-
toolChangeCount
|
|
350
|
+
toolChangeCount,
|
|
351
|
+
hasInlineToolDefinition,
|
|
221
352
|
});
|
|
222
353
|
}
|
|
223
354
|
|
|
@@ -285,7 +416,10 @@ export async function convertToAnthropicPrompt({
|
|
|
285
416
|
}),
|
|
286
417
|
});
|
|
287
418
|
|
|
288
|
-
|
|
419
|
+
// The inline tools beta also covers changes by reference.
|
|
420
|
+
if (message.hasInlineToolDefinition) {
|
|
421
|
+
betas.add('inline-tools-2026-09-15');
|
|
422
|
+
} else if (message.toolChangeCount > 0) {
|
|
289
423
|
betas.add('mid-conversation-tool-changes-2026-07-01');
|
|
290
424
|
}
|
|
291
425
|
if (message.clearAt != null) {
|
package/src/get-cache-control.ts
CHANGED
|
@@ -33,7 +33,12 @@ export class CacheControlValidator {
|
|
|
33
33
|
private breakpointCount = 0;
|
|
34
34
|
private warnings: SharedV4Warning[] = [];
|
|
35
35
|
|
|
36
|
-
constructor(
|
|
36
|
+
constructor(
|
|
37
|
+
private readonly providerOptionsName = 'anthropic',
|
|
38
|
+
initialBreakpointCount = 0,
|
|
39
|
+
) {
|
|
40
|
+
this.breakpointCount = initialBreakpointCount;
|
|
41
|
+
}
|
|
37
42
|
|
|
38
43
|
getCacheControl(
|
|
39
44
|
providerMetadata: SharedV4ProviderMetadata | undefined,
|
package/src/internal/index.ts
CHANGED
|
@@ -15,3 +15,9 @@ export { prepareTools } from '../anthropic-prepare-tools';
|
|
|
15
15
|
export { sanitizeJsonSchema } from '../sanitize-json-schema';
|
|
16
16
|
export { supportsNativeStructuredOutputSchema } from '../supports-native-structured-output-schema';
|
|
17
17
|
export { AnthropicSkills } from '../skills/anthropic-skills';
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
anthropicModelCatalog,
|
|
21
|
+
anthropicProviderOptions,
|
|
22
|
+
getAnthropicLanguageModelEntry,
|
|
23
|
+
} from '../anthropic-model-catalog';
|