@ai-sdk/anthropic 4.0.97 → 4.0.99

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.
@@ -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: toolChanges.length,
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
- if (message.toolChangeCount > 0) {
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) {
@@ -33,7 +33,12 @@ export class CacheControlValidator {
33
33
  private breakpointCount = 0;
34
34
  private warnings: SharedV4Warning[] = [];
35
35
 
36
- constructor(private readonly providerOptionsName = 'anthropic') {}
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,
@@ -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';
@@ -33,30 +33,39 @@ export const computerToolset_20260801Members = [
33
33
  export type ComputerToolset_20260801Member =
34
34
  (typeof computerToolset_20260801Members)[number];
35
35
 
36
- const computerToolset_20260801InputSchema = lazySchema(() =>
37
- zodSchema(
38
- z.object({
39
- action: z.enum(computerToolset_20260801Members),
40
- coordinate: z.tuple([z.number().int(), z.number().int()]).optional(),
41
- duration: z.number().optional(),
42
- region: z
43
- .tuple([
44
- z.number().int(),
45
- z.number().int(),
46
- z.number().int(),
47
- z.number().int(),
48
- ])
49
- .optional(),
50
- repeat: z.number().int().optional(),
51
- scroll_amount: z.number().optional(),
52
- scroll_direction: z.enum(['up', 'down', 'left', 'right']).optional(),
53
- start_coordinate: z
54
- .tuple([z.number().int(), z.number().int()])
55
- .optional(),
56
- text: z.string().optional(),
57
- }),
58
- ),
59
- );
36
+ const computerToolset_20260801InputSchema = (
37
+ configs?: Partial<
38
+ Record<ComputerToolset_20260801Member, { enabled?: boolean }>
39
+ >,
40
+ ) =>
41
+ lazySchema(() =>
42
+ zodSchema(
43
+ z.object({
44
+ action: z.enum(
45
+ computerToolset_20260801Members.filter(
46
+ member => configs?.[member]?.enabled !== false,
47
+ ),
48
+ ),
49
+ coordinate: z.tuple([z.number().int(), z.number().int()]).optional(),
50
+ duration: z.number().optional(),
51
+ region: z
52
+ .tuple([
53
+ z.number().int(),
54
+ z.number().int(),
55
+ z.number().int(),
56
+ z.number().int(),
57
+ ])
58
+ .optional(),
59
+ repeat: z.number().int().optional(),
60
+ scroll_amount: z.number().optional(),
61
+ scroll_direction: z.enum(['up', 'down', 'left', 'right']).optional(),
62
+ start_coordinate: z
63
+ .tuple([z.number().int(), z.number().int()])
64
+ .optional(),
65
+ text: z.string().optional(),
66
+ }),
67
+ ),
68
+ );
60
69
 
61
70
  export const computerToolset_20260801ArgsSchema = lazySchema(() =>
62
71
  zodSchema(
@@ -74,7 +83,7 @@ export const computerToolset_20260801ArgsSchema = lazySchema(() =>
74
83
  ),
75
84
  );
76
85
 
77
- export const computerToolset_20260801 = createProviderDefinedToolFactory<
86
+ const factory = createProviderDefinedToolFactory<
78
87
  {
79
88
  /**
80
89
  * The member tool that Claude invoked. Each member is returned by the API
@@ -161,5 +170,10 @@ export const computerToolset_20260801 = createProviderDefinedToolFactory<
161
170
  }
162
171
  >({
163
172
  id: 'anthropic.computer_toolset_20260801',
164
- inputSchema: computerToolset_20260801InputSchema,
173
+ inputSchema: computerToolset_20260801InputSchema(),
174
+ });
175
+
176
+ export const computerToolset_20260801: typeof factory = args => ({
177
+ ...factory(args),
178
+ inputSchema: computerToolset_20260801InputSchema(args.configs),
165
179
  });