@ai-sdk/anthropic 4.0.59 → 4.0.61

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.
@@ -81,12 +81,20 @@ export async function convertToAnthropicPrompt({
81
81
  warnings,
82
82
  cacheControlValidator,
83
83
  toolNameMapping,
84
+ toolsetNames = {},
84
85
  }: {
85
86
  prompt: LanguageModelV4Prompt;
86
87
  sendReasoning: boolean;
87
88
  warnings: SharedV4Warning[];
88
89
  cacheControlValidator?: CacheControlValidator;
89
90
  toolNameMapping: ToolNameMapping;
91
+
92
+ /**
93
+ * Maps custom tool names of toolset tools (e.g. the computer toolset) to
94
+ * the Anthropic `toolset_name`. Tool calls and results of these tools are
95
+ * serialized as toolset member calls.
96
+ */
97
+ toolsetNames?: Record<string, string>;
90
98
  }): Promise<{
91
99
  prompt: AnthropicPrompt;
92
100
  betas: Set<string>;
@@ -258,7 +266,7 @@ export async function convertToAnthropicPrompt({
258
266
  betas.add('mid-conversation-system-clear-at-2026-08-21');
259
267
  }
260
268
  if (message.effort != null) {
261
- betas.add('mid-conversation-effort-2026-08-01');
269
+ betas.add('mid-conversation-output-config-2026-07-01');
262
270
  }
263
271
  }
264
272
  }
@@ -636,9 +644,16 @@ export async function convertToAnthropicPrompt({
636
644
  break;
637
645
  }
638
646
 
647
+ const toolsetName = getAnthropicToolsetName({
648
+ toolName: part.toolName,
649
+ providerOptions: part.providerOptions,
650
+ toolsetNames,
651
+ });
652
+
639
653
  anthropicContent.push({
640
654
  type: 'tool_result',
641
655
  tool_use_id: part.toolCallId,
656
+ ...(toolsetName != null && { toolset_name: toolsetName }),
642
657
  content: contentValue,
643
658
  is_error:
644
659
  output.type === 'error-text' || output.type === 'error-json'
@@ -700,6 +715,10 @@ export async function convertToAnthropicPrompt({
700
715
  | undefined;
701
716
 
702
717
  if (textMetadata?.type === 'compaction') {
718
+ if (part.text === '') {
719
+ break;
720
+ }
721
+
703
722
  anthropicContent.push({
704
723
  type: 'compaction',
705
724
  content: part.text,
@@ -902,6 +921,38 @@ export async function convertToAnthropicPrompt({
902
921
  break;
903
922
  }
904
923
 
924
+ const toolsetName = getAnthropicToolsetName({
925
+ toolName: part.toolName,
926
+ providerOptions: part.providerOptions,
927
+ toolsetNames,
928
+ });
929
+
930
+ if (toolsetName != null) {
931
+ // toolset member call: the `action` is the member tool name
932
+ const { action, ...memberInput } = toAnthropicToolInput(
933
+ part.input,
934
+ );
935
+
936
+ if (typeof action !== 'string') {
937
+ warnings.push({
938
+ type: 'other',
939
+ message: `toolset tool call for tool ${part.toolName} is missing the action`,
940
+ });
941
+ break;
942
+ }
943
+
944
+ anthropicContent.push({
945
+ type: 'tool_use',
946
+ id: part.toolCallId,
947
+ name: action,
948
+ toolset_name: toolsetName,
949
+ input: memberInput,
950
+ ...(caller && { caller }),
951
+ cache_control: cacheControl,
952
+ });
953
+ break;
954
+ }
955
+
905
956
  anthropicContent.push({
906
957
  type: 'tool_use',
907
958
  id: part.toolCallId,
@@ -1339,10 +1390,12 @@ export async function convertToAnthropicPrompt({
1339
1390
  }
1340
1391
  }
1341
1392
 
1342
- messages.push({
1343
- role: 'assistant',
1344
- content: moveToolUseBlocksToEnd(anthropicContent),
1345
- });
1393
+ if (anthropicContent.length > 0) {
1394
+ messages.push({
1395
+ role: 'assistant',
1396
+ content: moveToolUseBlocksToEnd(anthropicContent),
1397
+ });
1398
+ }
1346
1399
 
1347
1400
  break;
1348
1401
  }
@@ -1457,6 +1510,32 @@ function moveToolUseBlocksToEnd(
1457
1510
  return result;
1458
1511
  }
1459
1512
 
1513
+ /**
1514
+ * Resolves the Anthropic `toolset_name` for a tool call or tool result. The
1515
+ * toolset is identified either through the tools passed to the request or
1516
+ * through the `toolsetName` provider metadata of a previous response.
1517
+ */
1518
+ function getAnthropicToolsetName({
1519
+ toolName,
1520
+ providerOptions,
1521
+ toolsetNames,
1522
+ }: {
1523
+ toolName: string;
1524
+ providerOptions: SharedV4ProviderMetadata | undefined;
1525
+ toolsetNames: Record<string, string>;
1526
+ }): string | undefined {
1527
+ const fromTools = toolsetNames[toolName];
1528
+ if (fromTools != null) {
1529
+ return fromTools;
1530
+ }
1531
+
1532
+ const fromMetadata = (
1533
+ providerOptions?.anthropic as { toolsetName?: unknown } | undefined
1534
+ )?.toolsetName;
1535
+
1536
+ return typeof fromMetadata === 'string' ? fromMetadata : undefined;
1537
+ }
1538
+
1460
1539
  function getAnthropicCaller(
1461
1540
  providerOptions: SharedV4ProviderMetadata | undefined,
1462
1541
  ): AnthropicToolCallCaller | undefined {
@@ -0,0 +1,165 @@
1
+ import {
2
+ createProviderDefinedToolFactory,
3
+ lazySchema,
4
+ zodSchema,
5
+ } from '@ai-sdk/provider-utils';
6
+ import { z } from 'zod/v4';
7
+
8
+ /**
9
+ * Member tools of the computer toolset. Each member is returned by the API as
10
+ * its own `tool_use` block; the AI SDK exposes it as the `action` of a single
11
+ * computer tool call.
12
+ */
13
+ export const computerToolset_20260801Members = [
14
+ 'screenshot',
15
+ 'zoom',
16
+ 'left_click',
17
+ 'right_click',
18
+ 'middle_click',
19
+ 'double_click',
20
+ 'triple_click',
21
+ 'left_click_drag',
22
+ 'mouse_move',
23
+ 'left_mouse_down',
24
+ 'left_mouse_up',
25
+ 'cursor_position',
26
+ 'scroll',
27
+ 'type',
28
+ 'key',
29
+ 'hold_key',
30
+ 'wait',
31
+ ] as const;
32
+
33
+ export type ComputerToolset_20260801Member =
34
+ (typeof computerToolset_20260801Members)[number];
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
+ );
60
+
61
+ export const computerToolset_20260801ArgsSchema = lazySchema(() =>
62
+ zodSchema(
63
+ z.object({
64
+ configs: z
65
+ .partialRecord(
66
+ z.enum(computerToolset_20260801Members),
67
+ z.object({
68
+ enabled: z.boolean().optional(),
69
+ deferLoading: z.boolean().optional(),
70
+ }),
71
+ )
72
+ .optional(),
73
+ }),
74
+ ),
75
+ );
76
+
77
+ export const computerToolset_20260801 = createProviderDefinedToolFactory<
78
+ {
79
+ /**
80
+ * The member tool that Claude invoked. Each member is returned by the API
81
+ * as a separate `tool_use` block; the AI SDK maps it to the `action`.
82
+ *
83
+ * - `screenshot`: Capture the full display as an image.
84
+ * - `zoom`: Capture a `region` of the screen at full resolution.
85
+ * - `left_click`, `right_click`, `middle_click`, `double_click`, `triple_click`: Click at `coordinate`. `text` may hold modifier keys to hold while clicking.
86
+ * - `left_click_drag`: Drag from `start_coordinate` to `coordinate`.
87
+ * - `mouse_move`: Move the cursor to `coordinate` without clicking.
88
+ * - `left_mouse_down`, `left_mouse_up`: Press or release the left mouse button at the current position.
89
+ * - `cursor_position`: Report the current cursor position as text.
90
+ * - `scroll`: Scroll `scroll_amount` clicks in `scroll_direction` at `coordinate`.
91
+ * - `type`: Type the literal `text`.
92
+ * - `key`: Press a key or key combination (`text`), optionally `repeat` times.
93
+ * - `hold_key`: Hold a key or key combination (`text`) for `duration` seconds.
94
+ * - `wait`: Pause for `duration` seconds.
95
+ */
96
+ action: ComputerToolset_20260801Member;
97
+
98
+ /**
99
+ * (x, y): Pixel coordinates in the space of the screenshots you return.
100
+ */
101
+ coordinate?: [number, number];
102
+
103
+ /**
104
+ * Duration in seconds. Used by `hold_key` and `wait`.
105
+ */
106
+ duration?: number;
107
+
108
+ /**
109
+ * [x1, y1, x2, y2]: The region to zoom into. Used by `zoom`.
110
+ */
111
+ region?: [number, number, number, number];
112
+
113
+ /**
114
+ * How many times to press the key. Used by `key`.
115
+ */
116
+ repeat?: number;
117
+
118
+ /**
119
+ * The number of scroll wheel clicks. Used by `scroll`.
120
+ */
121
+ scroll_amount?: number;
122
+
123
+ /**
124
+ * The direction to scroll. Used by `scroll`.
125
+ */
126
+ scroll_direction?: 'up' | 'down' | 'left' | 'right';
127
+
128
+ /**
129
+ * (x, y): Where a drag starts. Used by `left_click_drag`.
130
+ */
131
+ start_coordinate?: [number, number];
132
+
133
+ /**
134
+ * Text to type, a key combination to press or hold, or modifier keys to
135
+ * hold during click and scroll actions.
136
+ */
137
+ text?: string;
138
+ },
139
+ {
140
+ /**
141
+ * Per-member configuration. Members you omit keep their defaults
142
+ * (all members enabled, including `zoom`).
143
+ */
144
+ configs?: Partial<
145
+ Record<
146
+ ComputerToolset_20260801Member,
147
+ {
148
+ /**
149
+ * Whether Claude can use this member tool. Default: true.
150
+ */
151
+ enabled?: boolean;
152
+
153
+ /**
154
+ * Whether this member tool is only loaded once discovered through
155
+ * tool search. Default: false.
156
+ */
157
+ deferLoading?: boolean;
158
+ }
159
+ >
160
+ >;
161
+ }
162
+ >({
163
+ id: 'anthropic.computer_toolset_20260801',
164
+ inputSchema: computerToolset_20260801InputSchema,
165
+ });