@ai-sdk/anthropic 3.0.137 → 3.0.139
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.mts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +209 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +213 -28
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +19 -2
- package/dist/internal/index.d.ts +19 -2
- package/dist/internal/index.js +212 -27
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +212 -26
- package/dist/internal/index.mjs.map +1 -1
- package/docs/05-anthropic.mdx +74 -3
- package/package.json +2 -2
- package/src/anthropic-messages-api.ts +19 -6
- package/src/anthropic-messages-language-model.ts +142 -6
- package/src/anthropic-messages-options.ts +31 -5
- package/src/anthropic-prepare-tools.ts +21 -9
- package/src/convert-to-anthropic-messages-prompt.ts +147 -3
- package/src/internal/index.ts +1 -0
|
@@ -3,11 +3,14 @@ import {
|
|
|
3
3
|
type SharedV3Warning,
|
|
4
4
|
type LanguageModelV3DataContent,
|
|
5
5
|
type LanguageModelV3FilePart,
|
|
6
|
+
type LanguageModelV3FunctionTool,
|
|
6
7
|
type LanguageModelV3Message,
|
|
7
8
|
type LanguageModelV3Prompt,
|
|
9
|
+
type LanguageModelV3ProviderTool,
|
|
8
10
|
type SharedV3ProviderMetadata,
|
|
9
11
|
} from '@ai-sdk/provider';
|
|
10
12
|
import {
|
|
13
|
+
stringifyWithBigIntAndCycles,
|
|
11
14
|
convertBase64ToUint8Array,
|
|
12
15
|
convertToBase64,
|
|
13
16
|
parseProviderOptions,
|
|
@@ -24,6 +27,7 @@ import {
|
|
|
24
27
|
type AnthropicMessagesPrompt,
|
|
25
28
|
type AnthropicSystemMessage,
|
|
26
29
|
type AnthropicTextContent,
|
|
30
|
+
type AnthropicTool,
|
|
27
31
|
type AnthropicToolChangeContent,
|
|
28
32
|
type AnthropicToolCallCaller,
|
|
29
33
|
type AnthropicToolResultContent,
|
|
@@ -120,6 +124,66 @@ export type AnthropicMediaPartConverter = (
|
|
|
120
124
|
| undefined
|
|
121
125
|
>;
|
|
122
126
|
|
|
127
|
+
// The computer use tools cannot be defined in a message (the API rejects
|
|
128
|
+
// them), they must be declared in `tools` and added by reference.
|
|
129
|
+
function isComputerTool(
|
|
130
|
+
tool: LanguageModelV3FunctionTool | LanguageModelV3ProviderTool,
|
|
131
|
+
): boolean {
|
|
132
|
+
return tool.type === 'provider' && tool.id.startsWith('anthropic.computer_');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Collects the tools that mid-conversation tool changes add by value, so they
|
|
137
|
+
* can take part in tool name mapping like the tools of the `tools` option.
|
|
138
|
+
* Tools that cannot be added by value are left out because they are ignored.
|
|
139
|
+
*/
|
|
140
|
+
export async function getInlineToolsFromPrompt(
|
|
141
|
+
prompt: LanguageModelV3Prompt,
|
|
142
|
+
): Promise<Array<LanguageModelV3FunctionTool | LanguageModelV3ProviderTool>> {
|
|
143
|
+
const inlineTools: Array<
|
|
144
|
+
LanguageModelV3FunctionTool | LanguageModelV3ProviderTool
|
|
145
|
+
> = [];
|
|
146
|
+
|
|
147
|
+
for (const message of prompt) {
|
|
148
|
+
if (message.role !== 'system' || message.providerOptions == null) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const options = await parseProviderOptions({
|
|
153
|
+
provider: 'anthropic',
|
|
154
|
+
providerOptions: message.providerOptions,
|
|
155
|
+
schema: anthropicSystemMessageProviderOptions,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
for (const toolChange of options?.toolChanges ?? []) {
|
|
159
|
+
if ('tool' in toolChange) {
|
|
160
|
+
const tool = toolChange.tool as
|
|
161
|
+
| LanguageModelV3FunctionTool
|
|
162
|
+
| LanguageModelV3ProviderTool;
|
|
163
|
+
|
|
164
|
+
if (!isComputerTool(tool)) {
|
|
165
|
+
inlineTools.push(tool);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return inlineTools;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Converts a tool that is defined by value in a mid-conversation tool change
|
|
176
|
+
* into the Anthropic tool definition, the same way entries of the top-level
|
|
177
|
+
* `tools` option are converted.
|
|
178
|
+
*/
|
|
179
|
+
export type AnthropicInlineToolConverter = (
|
|
180
|
+
tool: LanguageModelV3FunctionTool | LanguageModelV3ProviderTool,
|
|
181
|
+
) => Promise<{
|
|
182
|
+
definition: AnthropicTool | undefined;
|
|
183
|
+
warnings: SharedV3Warning[];
|
|
184
|
+
betas: Set<string>;
|
|
185
|
+
}>;
|
|
186
|
+
|
|
123
187
|
export async function convertToAnthropicMessagesPrompt({
|
|
124
188
|
prompt,
|
|
125
189
|
sendReasoning,
|
|
@@ -128,6 +192,7 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
128
192
|
toolNameMapping,
|
|
129
193
|
toolsetNames = {},
|
|
130
194
|
convertMediaPart,
|
|
195
|
+
convertInlineTool,
|
|
131
196
|
}: {
|
|
132
197
|
prompt: LanguageModelV3Prompt;
|
|
133
198
|
sendReasoning: boolean;
|
|
@@ -136,6 +201,12 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
136
201
|
toolNameMapping: ToolNameMapping;
|
|
137
202
|
convertMediaPart?: AnthropicMediaPartConverter;
|
|
138
203
|
|
|
204
|
+
/**
|
|
205
|
+
* Converts tools that mid-conversation tool changes add by value. Required
|
|
206
|
+
* when the prompt contains such tool changes.
|
|
207
|
+
*/
|
|
208
|
+
convertInlineTool?: AnthropicInlineToolConverter;
|
|
209
|
+
|
|
139
210
|
/**
|
|
140
211
|
* Maps custom tool names of toolset tools (e.g. the computer toolset) to
|
|
141
212
|
* the Anthropic `toolset_name`. Tool calls and results of these tools are
|
|
@@ -190,6 +261,7 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
190
261
|
case 'system': {
|
|
191
262
|
const systemMessages: AnthropicSystemMessage[] = [];
|
|
192
263
|
let toolChangeCount = 0;
|
|
264
|
+
let hasInlineToolDefinition = false;
|
|
193
265
|
let hasMidConversationOptions = false;
|
|
194
266
|
|
|
195
267
|
for (const { content: text, providerOptions } of block.messages) {
|
|
@@ -220,6 +292,52 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
220
292
|
}
|
|
221
293
|
|
|
222
294
|
for (const toolChange of toolChanges) {
|
|
295
|
+
if ('tool' in toolChange) {
|
|
296
|
+
if (convertInlineTool == null) {
|
|
297
|
+
throw new UnsupportedFunctionalityError({
|
|
298
|
+
functionality:
|
|
299
|
+
'tool additions by value in mid-conversation tool changes',
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const tool = toolChange.tool as
|
|
304
|
+
| LanguageModelV3FunctionTool
|
|
305
|
+
| LanguageModelV3ProviderTool;
|
|
306
|
+
|
|
307
|
+
if (isComputerTool(tool)) {
|
|
308
|
+
warnings.push({
|
|
309
|
+
type: 'other',
|
|
310
|
+
message:
|
|
311
|
+
`The tool '${tool.name}' cannot be added by value in a tool change. ` +
|
|
312
|
+
'Declare it in the tools option and add it by reference with toolName. ' +
|
|
313
|
+
'The tool change has been ignored.',
|
|
314
|
+
});
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const {
|
|
319
|
+
definition,
|
|
320
|
+
warnings: toolWarnings,
|
|
321
|
+
betas: toolBetas,
|
|
322
|
+
} = await convertInlineTool(tool);
|
|
323
|
+
warnings.push(...toolWarnings);
|
|
324
|
+
for (const beta of toolBetas) {
|
|
325
|
+
betas.add(beta);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (definition == null) {
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
content.push({
|
|
333
|
+
type: 'tool_addition',
|
|
334
|
+
tool: { type: 'tool_definition', definition },
|
|
335
|
+
} satisfies AnthropicToolChangeContent);
|
|
336
|
+
toolChangeCount++;
|
|
337
|
+
hasInlineToolDefinition = true;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
|
|
223
341
|
toolChangeCount++;
|
|
224
342
|
content.push({
|
|
225
343
|
type: toolChange.type,
|
|
@@ -230,6 +348,11 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
230
348
|
} satisfies AnthropicToolChangeContent);
|
|
231
349
|
}
|
|
232
350
|
|
|
351
|
+
// Every change was ignored and nothing else is left to send.
|
|
352
|
+
if (content.length === 0 && clearAt == null && effort == null) {
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
|
|
233
356
|
hasMidConversationOptions ||= clearAt != null || effort != null;
|
|
234
357
|
|
|
235
358
|
systemMessages.push({
|
|
@@ -292,7 +415,10 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
292
415
|
} else {
|
|
293
416
|
messages.push(...systemMessages);
|
|
294
417
|
betas.add('mid-conversation-system-2026-04-07');
|
|
295
|
-
|
|
418
|
+
// The inline tools beta also covers changes by reference.
|
|
419
|
+
if (hasInlineToolDefinition) {
|
|
420
|
+
betas.add('inline-tools-2026-09-15');
|
|
421
|
+
} else if (toolChangeCount > 0) {
|
|
296
422
|
betas.add('mid-conversation-tool-changes-2026-07-01');
|
|
297
423
|
}
|
|
298
424
|
if (systemMessages.some(message => message.clear_at != null)) {
|
|
@@ -585,7 +711,7 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
585
711
|
case 'json':
|
|
586
712
|
case 'error-json':
|
|
587
713
|
default:
|
|
588
|
-
contentValue =
|
|
714
|
+
contentValue = stringifyWithBigIntAndCycles(output.value);
|
|
589
715
|
break;
|
|
590
716
|
}
|
|
591
717
|
|
|
@@ -899,7 +1025,7 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
899
1025
|
type: 'tool_use',
|
|
900
1026
|
id: part.toolCallId,
|
|
901
1027
|
name: part.toolName,
|
|
902
|
-
input: part.input,
|
|
1028
|
+
input: parseToolCallInput(part.input),
|
|
903
1029
|
...(caller && { caller }),
|
|
904
1030
|
cache_control: cacheControl,
|
|
905
1031
|
});
|
|
@@ -1564,3 +1690,21 @@ function getAnthropicCaller(
|
|
|
1564
1690
|
|
|
1565
1691
|
return caller?.type === 'direct' ? { type: 'direct' } : undefined;
|
|
1566
1692
|
}
|
|
1693
|
+
|
|
1694
|
+
function parseToolCallInput(input: unknown): unknown {
|
|
1695
|
+
if (typeof input === 'string') {
|
|
1696
|
+
try {
|
|
1697
|
+
const parsed = secureJsonParse(input);
|
|
1698
|
+
if (
|
|
1699
|
+
typeof parsed === 'object' &&
|
|
1700
|
+
parsed !== null &&
|
|
1701
|
+
!Array.isArray(parsed)
|
|
1702
|
+
) {
|
|
1703
|
+
return parsed;
|
|
1704
|
+
}
|
|
1705
|
+
} catch {
|
|
1706
|
+
// Preserve malformed input for replay.
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
return input;
|
|
1710
|
+
}
|
package/src/internal/index.ts
CHANGED
|
@@ -6,3 +6,4 @@ export { anthropicTools } from '../anthropic-tools';
|
|
|
6
6
|
export type { AnthropicMessagesModelId } from '../anthropic-messages-options';
|
|
7
7
|
export { prepareTools } from '../anthropic-prepare-tools';
|
|
8
8
|
export { sanitizeJsonSchema } from '../sanitize-json-schema';
|
|
9
|
+
export { supportsNativeStructuredOutputSchema } from '../supports-native-structured-output-schema';
|