@ai-sdk/anthropic 4.0.0-beta.2 → 4.0.0-beta.4
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 +12 -0
- package/dist/index.d.mts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/index.js +28 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +25 -14
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +22 -13
- package/dist/internal/index.d.ts +22 -13
- package/dist/internal/index.js +26 -15
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +23 -12
- package/dist/internal/index.mjs.map +1 -1
- package/docs/05-anthropic.mdx +45 -4
- package/package.json +3 -3
- package/src/anthropic-messages-language-model.ts +46 -34
- package/src/anthropic-prepare-tools.ts +22 -8
- package/src/anthropic-provider.ts +8 -8
- package/src/convert-anthropic-messages-usage.ts +2 -2
- package/src/convert-to-anthropic-messages-prompt.ts +17 -17
- package/src/get-cache-control.ts +5 -5
- package/src/map-anthropic-stop-reason.ts +2 -2
package/docs/05-anthropic.mdx
CHANGED
|
@@ -220,17 +220,58 @@ The `speed` option accepts `'fast'` or `'standard'` (default behavior).
|
|
|
220
220
|
|
|
221
221
|
### Reasoning
|
|
222
222
|
|
|
223
|
-
Anthropic
|
|
223
|
+
Anthropic models support extended thinking, where Claude shows its reasoning process before providing a final answer.
|
|
224
224
|
|
|
225
|
-
|
|
226
|
-
|
|
225
|
+
#### Adaptive Thinking
|
|
226
|
+
|
|
227
|
+
For newer models (`claude-sonnet-4-6`, `claude-opus-4-6`, and later), use adaptive thinking.
|
|
228
|
+
Claude automatically determines how much reasoning to use based on the complexity of the prompt.
|
|
227
229
|
|
|
228
230
|
```ts highlight="4,8-10"
|
|
229
231
|
import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
|
|
230
232
|
import { generateText } from 'ai';
|
|
231
233
|
|
|
232
234
|
const { text, reasoningText, reasoning } = await generateText({
|
|
233
|
-
model: anthropic('claude-opus-4-
|
|
235
|
+
model: anthropic('claude-opus-4-6'),
|
|
236
|
+
prompt: 'How many people will live in the world in 2040?',
|
|
237
|
+
providerOptions: {
|
|
238
|
+
anthropic: {
|
|
239
|
+
thinking: { type: 'adaptive' },
|
|
240
|
+
} satisfies AnthropicLanguageModelOptions,
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
console.log(reasoningText); // reasoning text
|
|
245
|
+
console.log(reasoning); // reasoning details including redacted reasoning
|
|
246
|
+
console.log(text); // text response
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
You can combine adaptive thinking with the `effort` option to control how much reasoning Claude uses:
|
|
250
|
+
|
|
251
|
+
```ts highlight="6-8"
|
|
252
|
+
const { text } = await generateText({
|
|
253
|
+
model: anthropic('claude-opus-4-6'),
|
|
254
|
+
prompt: 'Invent a new holiday and describe its traditions.',
|
|
255
|
+
providerOptions: {
|
|
256
|
+
anthropic: {
|
|
257
|
+
thinking: { type: 'adaptive' },
|
|
258
|
+
effort: 'max', // 'low' | 'medium' | 'high' | 'max'
|
|
259
|
+
} satisfies AnthropicLanguageModelOptions,
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
#### Budget-Based Thinking
|
|
265
|
+
|
|
266
|
+
For earlier models (`claude-opus-4-20250514`, `claude-sonnet-4-20250514`, `claude-sonnet-4-5-20250929`),
|
|
267
|
+
use `type: 'enabled'` with an explicit token budget:
|
|
268
|
+
|
|
269
|
+
```ts highlight="4,8-10"
|
|
270
|
+
import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
|
|
271
|
+
import { generateText } from 'ai';
|
|
272
|
+
|
|
273
|
+
const { text, reasoningText, reasoning } = await generateText({
|
|
274
|
+
model: anthropic('claude-sonnet-4-5-20250929'),
|
|
234
275
|
prompt: 'How many people will live in the world in 2040?',
|
|
235
276
|
providerOptions: {
|
|
236
277
|
anthropic: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/anthropic",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.4",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"tsup": "^8",
|
|
45
45
|
"typescript": "5.8.3",
|
|
46
46
|
"zod": "3.25.76",
|
|
47
|
-
"@ai-
|
|
48
|
-
"@
|
|
47
|
+
"@vercel/ai-tsconfig": "0.0.0",
|
|
48
|
+
"@ai-sdk/test-server": "2.0.0-beta.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"zod": "^3.25.76 || ^4.1.8"
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
2
|
APICallError,
|
|
3
3
|
JSONObject,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
4
|
+
LanguageModelV4,
|
|
5
|
+
LanguageModelV4CallOptions,
|
|
6
|
+
LanguageModelV4Content,
|
|
7
|
+
LanguageModelV4FinishReason,
|
|
8
|
+
LanguageModelV4FunctionTool,
|
|
9
|
+
LanguageModelV4GenerateResult,
|
|
10
|
+
LanguageModelV4Prompt,
|
|
11
|
+
LanguageModelV4Source,
|
|
12
|
+
LanguageModelV4StreamPart,
|
|
13
|
+
LanguageModelV4StreamResult,
|
|
14
|
+
LanguageModelV4ToolCall,
|
|
15
|
+
SharedV4ProviderMetadata,
|
|
16
|
+
SharedV4Warning,
|
|
17
17
|
} from '@ai-sdk/provider';
|
|
18
18
|
import {
|
|
19
19
|
combineHeaders,
|
|
@@ -61,7 +61,7 @@ function createCitationSource(
|
|
|
61
61
|
mediaType: string;
|
|
62
62
|
}>,
|
|
63
63
|
generateId: () => string,
|
|
64
|
-
):
|
|
64
|
+
): LanguageModelV4Source | undefined {
|
|
65
65
|
if (citation.type === 'web_search_result_location') {
|
|
66
66
|
return {
|
|
67
67
|
type: 'source' as const,
|
|
@@ -74,7 +74,7 @@ function createCitationSource(
|
|
|
74
74
|
citedText: citation.cited_text,
|
|
75
75
|
encryptedIndex: citation.encrypted_index,
|
|
76
76
|
},
|
|
77
|
-
} satisfies
|
|
77
|
+
} satisfies SharedV4ProviderMetadata,
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
80
|
|
|
@@ -108,7 +108,7 @@ function createCitationSource(
|
|
|
108
108
|
startCharIndex: citation.start_char_index,
|
|
109
109
|
endCharIndex: citation.end_char_index,
|
|
110
110
|
},
|
|
111
|
-
} satisfies
|
|
111
|
+
} satisfies SharedV4ProviderMetadata,
|
|
112
112
|
};
|
|
113
113
|
}
|
|
114
114
|
|
|
@@ -122,17 +122,23 @@ type AnthropicMessagesConfig = {
|
|
|
122
122
|
args: Record<string, any>,
|
|
123
123
|
betas: Set<string>,
|
|
124
124
|
) => Record<string, any>;
|
|
125
|
-
supportedUrls?: () =>
|
|
125
|
+
supportedUrls?: () => LanguageModelV4['supportedUrls'];
|
|
126
126
|
generateId?: () => string;
|
|
127
127
|
|
|
128
128
|
/**
|
|
129
129
|
* When false, the model will use JSON tool fallback for structured outputs.
|
|
130
130
|
*/
|
|
131
131
|
supportsNativeStructuredOutput?: boolean;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* When false, `strict` on tool definitions will be ignored and a warning emitted.
|
|
135
|
+
* Defaults to true.
|
|
136
|
+
*/
|
|
137
|
+
supportsStrictTools?: boolean;
|
|
132
138
|
};
|
|
133
139
|
|
|
134
|
-
export class AnthropicMessagesLanguageModel implements
|
|
135
|
-
readonly specificationVersion = '
|
|
140
|
+
export class AnthropicMessagesLanguageModel implements LanguageModelV4 {
|
|
141
|
+
readonly specificationVersion = 'v4';
|
|
136
142
|
|
|
137
143
|
readonly modelId: AnthropicMessagesModelId;
|
|
138
144
|
|
|
@@ -186,11 +192,11 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
186
192
|
toolChoice,
|
|
187
193
|
providerOptions,
|
|
188
194
|
stream,
|
|
189
|
-
}:
|
|
195
|
+
}: LanguageModelV4CallOptions & {
|
|
190
196
|
stream: boolean;
|
|
191
197
|
userSuppliedBetas: Set<string>;
|
|
192
198
|
}) {
|
|
193
|
-
const warnings:
|
|
199
|
+
const warnings: SharedV4Warning[] = [];
|
|
194
200
|
|
|
195
201
|
if (frequencyPenalty != null) {
|
|
196
202
|
warnings.push({ type: 'unsupported', feature: 'frequencyPenalty' });
|
|
@@ -270,13 +276,17 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
270
276
|
(this.config.supportsNativeStructuredOutput ?? true) &&
|
|
271
277
|
modelSupportsStructuredOutput;
|
|
272
278
|
|
|
279
|
+
const supportsStrictTools =
|
|
280
|
+
(this.config.supportsStrictTools ?? true) &&
|
|
281
|
+
modelSupportsStructuredOutput;
|
|
282
|
+
|
|
273
283
|
const structureOutputMode =
|
|
274
284
|
anthropicOptions?.structuredOutputMode ?? 'auto';
|
|
275
285
|
const useStructuredOutput =
|
|
276
286
|
structureOutputMode === 'outputFormat' ||
|
|
277
287
|
(structureOutputMode === 'auto' && supportsStructuredOutput);
|
|
278
288
|
|
|
279
|
-
const jsonResponseTool:
|
|
289
|
+
const jsonResponseTool: LanguageModelV4FunctionTool | undefined =
|
|
280
290
|
responseFormat?.type === 'json' &&
|
|
281
291
|
responseFormat.schema != null &&
|
|
282
292
|
!useStructuredOutput
|
|
@@ -614,6 +624,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
614
624
|
disableParallelToolUse: true,
|
|
615
625
|
cacheControlValidator,
|
|
616
626
|
supportsStructuredOutput: false,
|
|
627
|
+
supportsStrictTools,
|
|
617
628
|
}
|
|
618
629
|
: {
|
|
619
630
|
tools: tools ?? [],
|
|
@@ -621,6 +632,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
621
632
|
disableParallelToolUse: anthropicOptions?.disableParallelToolUse,
|
|
622
633
|
cacheControlValidator,
|
|
623
634
|
supportsStructuredOutput,
|
|
635
|
+
supportsStrictTools,
|
|
624
636
|
},
|
|
625
637
|
);
|
|
626
638
|
|
|
@@ -694,7 +706,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
694
706
|
return this.config.transformRequestBody?.(args, betas) ?? args;
|
|
695
707
|
}
|
|
696
708
|
|
|
697
|
-
private extractCitationDocuments(prompt:
|
|
709
|
+
private extractCitationDocuments(prompt: LanguageModelV4Prompt): Array<{
|
|
698
710
|
title: string;
|
|
699
711
|
filename?: string;
|
|
700
712
|
mediaType: string;
|
|
@@ -738,8 +750,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
738
750
|
}
|
|
739
751
|
|
|
740
752
|
async doGenerate(
|
|
741
|
-
options:
|
|
742
|
-
): Promise<
|
|
753
|
+
options: LanguageModelV4CallOptions,
|
|
754
|
+
): Promise<LanguageModelV4GenerateResult> {
|
|
743
755
|
const {
|
|
744
756
|
args,
|
|
745
757
|
warnings,
|
|
@@ -779,8 +791,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
779
791
|
fetch: this.config.fetch,
|
|
780
792
|
});
|
|
781
793
|
|
|
782
|
-
const content: Array<
|
|
783
|
-
const mcpToolCalls: Record<string,
|
|
794
|
+
const content: Array<LanguageModelV4Content> = [];
|
|
795
|
+
const mcpToolCalls: Record<string, LanguageModelV4ToolCall> = {};
|
|
784
796
|
const serverToolCalls: Record<string, string> = {}; // tool_use_id -> provider tool name
|
|
785
797
|
let isJsonResponseFromTool = false;
|
|
786
798
|
|
|
@@ -1205,7 +1217,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1205
1217
|
) ?? null,
|
|
1206
1218
|
} satisfies AnthropicMessageMetadata;
|
|
1207
1219
|
|
|
1208
|
-
const providerMetadata:
|
|
1220
|
+
const providerMetadata: SharedV4ProviderMetadata = {
|
|
1209
1221
|
anthropic: anthropicMetadata,
|
|
1210
1222
|
};
|
|
1211
1223
|
|
|
@@ -1219,8 +1231,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1219
1231
|
}
|
|
1220
1232
|
|
|
1221
1233
|
async doStream(
|
|
1222
|
-
options:
|
|
1223
|
-
): Promise<
|
|
1234
|
+
options: LanguageModelV4CallOptions,
|
|
1235
|
+
): Promise<LanguageModelV4StreamResult> {
|
|
1224
1236
|
const {
|
|
1225
1237
|
args: body,
|
|
1226
1238
|
warnings,
|
|
@@ -1257,7 +1269,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1257
1269
|
fetch: this.config.fetch,
|
|
1258
1270
|
});
|
|
1259
1271
|
|
|
1260
|
-
let finishReason:
|
|
1272
|
+
let finishReason: LanguageModelV4FinishReason = {
|
|
1261
1273
|
unified: 'other',
|
|
1262
1274
|
raw: undefined,
|
|
1263
1275
|
};
|
|
@@ -1289,7 +1301,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1289
1301
|
}
|
|
1290
1302
|
| { type: 'text' | 'reasoning' }
|
|
1291
1303
|
> = {};
|
|
1292
|
-
const mcpToolCalls: Record<string,
|
|
1304
|
+
const mcpToolCalls: Record<string, LanguageModelV4ToolCall> = {};
|
|
1293
1305
|
const serverToolCalls: Record<string, string> = {}; // tool_use_id -> provider tool name
|
|
1294
1306
|
|
|
1295
1307
|
let contextManagement:
|
|
@@ -1323,7 +1335,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1323
1335
|
const transformedStream = response.pipeThrough(
|
|
1324
1336
|
new TransformStream<
|
|
1325
1337
|
ParseResult<InferSchema<typeof anthropicMessagesChunkSchema>>,
|
|
1326
|
-
|
|
1338
|
+
LanguageModelV4StreamPart
|
|
1327
1339
|
>({
|
|
1328
1340
|
start(controller) {
|
|
1329
1341
|
controller.enqueue({ type: 'stream-start', warnings });
|
|
@@ -2176,7 +2188,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
2176
2188
|
contextManagement,
|
|
2177
2189
|
} satisfies AnthropicMessageMetadata;
|
|
2178
2190
|
|
|
2179
|
-
const providerMetadata:
|
|
2191
|
+
const providerMetadata: SharedV4ProviderMetadata = {
|
|
2180
2192
|
anthropic: anthropicMetadata,
|
|
2181
2193
|
};
|
|
2182
2194
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
LanguageModelV4CallOptions,
|
|
3
|
+
SharedV4Warning,
|
|
4
4
|
UnsupportedFunctionalityError,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
6
|
import { AnthropicTool, AnthropicToolChoice } from './anthropic-messages-api';
|
|
@@ -26,26 +26,32 @@ export async function prepareTools({
|
|
|
26
26
|
disableParallelToolUse,
|
|
27
27
|
cacheControlValidator,
|
|
28
28
|
supportsStructuredOutput,
|
|
29
|
+
supportsStrictTools,
|
|
29
30
|
}: {
|
|
30
|
-
tools:
|
|
31
|
-
toolChoice:
|
|
31
|
+
tools: LanguageModelV4CallOptions['tools'];
|
|
32
|
+
toolChoice: LanguageModelV4CallOptions['toolChoice'] | undefined;
|
|
32
33
|
disableParallelToolUse?: boolean;
|
|
33
34
|
cacheControlValidator?: CacheControlValidator;
|
|
34
35
|
|
|
35
36
|
/**
|
|
36
|
-
* Whether the model supports structured output.
|
|
37
|
+
* Whether the model supports native structured output response format.
|
|
37
38
|
*/
|
|
38
39
|
supportsStructuredOutput: boolean;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Whether the model supports strict mode on tool definitions.
|
|
43
|
+
*/
|
|
44
|
+
supportsStrictTools: boolean;
|
|
39
45
|
}): Promise<{
|
|
40
46
|
tools: Array<AnthropicTool> | undefined;
|
|
41
47
|
toolChoice: AnthropicToolChoice | undefined;
|
|
42
|
-
toolWarnings:
|
|
48
|
+
toolWarnings: SharedV4Warning[];
|
|
43
49
|
betas: Set<string>;
|
|
44
50
|
}> {
|
|
45
51
|
// when the tools array is empty, change it to undefined to prevent errors:
|
|
46
52
|
tools = tools?.length ? tools : undefined;
|
|
47
53
|
|
|
48
|
-
const toolWarnings:
|
|
54
|
+
const toolWarnings: SharedV4Warning[] = [];
|
|
49
55
|
const betas = new Set<string>();
|
|
50
56
|
const validator = cacheControlValidator || new CacheControlValidator();
|
|
51
57
|
|
|
@@ -72,13 +78,21 @@ export async function prepareTools({
|
|
|
72
78
|
const deferLoading = anthropicOptions?.deferLoading;
|
|
73
79
|
const allowedCallers = anthropicOptions?.allowedCallers;
|
|
74
80
|
|
|
81
|
+
if (!supportsStrictTools && tool.strict != null) {
|
|
82
|
+
toolWarnings.push({
|
|
83
|
+
type: 'unsupported',
|
|
84
|
+
feature: 'strict',
|
|
85
|
+
details: `Tool '${tool.name}' has strict: ${tool.strict}, but strict mode is not supported by this provider. The strict property will be ignored.`,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
75
89
|
anthropicTools.push({
|
|
76
90
|
name: tool.name,
|
|
77
91
|
description: tool.description,
|
|
78
92
|
input_schema: tool.inputSchema,
|
|
79
93
|
cache_control: cacheControl,
|
|
80
94
|
...(eagerInputStreaming ? { eager_input_streaming: true } : {}),
|
|
81
|
-
...(
|
|
95
|
+
...(supportsStrictTools === true && tool.strict != null
|
|
82
96
|
? { strict: tool.strict }
|
|
83
97
|
: {}),
|
|
84
98
|
...(deferLoading != null ? { defer_loading: deferLoading } : {}),
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
InvalidArgumentError,
|
|
3
|
-
|
|
3
|
+
LanguageModelV4,
|
|
4
4
|
NoSuchModelError,
|
|
5
|
-
|
|
5
|
+
ProviderV4,
|
|
6
6
|
} from '@ai-sdk/provider';
|
|
7
7
|
import {
|
|
8
8
|
FetchFunction,
|
|
@@ -17,20 +17,20 @@ import { AnthropicMessagesLanguageModel } from './anthropic-messages-language-mo
|
|
|
17
17
|
import { AnthropicMessagesModelId } from './anthropic-messages-options';
|
|
18
18
|
import { anthropicTools } from './anthropic-tools';
|
|
19
19
|
|
|
20
|
-
export interface AnthropicProvider extends
|
|
20
|
+
export interface AnthropicProvider extends ProviderV4 {
|
|
21
21
|
/**
|
|
22
22
|
* Creates a model for text generation.
|
|
23
23
|
*/
|
|
24
|
-
(modelId: AnthropicMessagesModelId):
|
|
24
|
+
(modelId: AnthropicMessagesModelId): LanguageModelV4;
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* Creates a model for text generation.
|
|
28
28
|
*/
|
|
29
|
-
languageModel(modelId: AnthropicMessagesModelId):
|
|
29
|
+
languageModel(modelId: AnthropicMessagesModelId): LanguageModelV4;
|
|
30
30
|
|
|
31
|
-
chat(modelId: AnthropicMessagesModelId):
|
|
31
|
+
chat(modelId: AnthropicMessagesModelId): LanguageModelV4;
|
|
32
32
|
|
|
33
|
-
messages(modelId: AnthropicMessagesModelId):
|
|
33
|
+
messages(modelId: AnthropicMessagesModelId): LanguageModelV4;
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* @deprecated Use `embeddingModel` instead.
|
|
@@ -153,7 +153,7 @@ export function createAnthropic(
|
|
|
153
153
|
return createChatModel(modelId);
|
|
154
154
|
};
|
|
155
155
|
|
|
156
|
-
provider.specificationVersion = '
|
|
156
|
+
provider.specificationVersion = 'v4' as const;
|
|
157
157
|
provider.languageModel = createChatModel;
|
|
158
158
|
provider.chat = createChatModel;
|
|
159
159
|
provider.messages = createChatModel;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JSONObject,
|
|
1
|
+
import { JSONObject, LanguageModelV4Usage } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Represents a single iteration in the usage breakdown.
|
|
@@ -31,7 +31,7 @@ export function convertAnthropicMessagesUsage({
|
|
|
31
31
|
}: {
|
|
32
32
|
usage: AnthropicMessagesUsage;
|
|
33
33
|
rawUsage?: JSONObject;
|
|
34
|
-
}):
|
|
34
|
+
}): LanguageModelV4Usage {
|
|
35
35
|
const cacheCreationTokens = usage.cache_creation_input_tokens ?? 0;
|
|
36
36
|
const cacheReadTokens = usage.cache_read_input_tokens ?? 0;
|
|
37
37
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
SharedV4Warning,
|
|
3
|
+
LanguageModelV4DataContent,
|
|
4
|
+
LanguageModelV4Message,
|
|
5
|
+
LanguageModelV4Prompt,
|
|
6
|
+
SharedV4ProviderMetadata,
|
|
7
7
|
UnsupportedFunctionalityError,
|
|
8
8
|
} from '@ai-sdk/provider';
|
|
9
9
|
import {
|
|
@@ -31,7 +31,7 @@ import { toolSearchRegex_20251119OutputSchema as toolSearchOutputSchema } from '
|
|
|
31
31
|
import { webFetch_20250910OutputSchema } from './tool/web-fetch-20250910';
|
|
32
32
|
import { webSearch_20250305OutputSchema } from './tool/web-search_20250305';
|
|
33
33
|
|
|
34
|
-
function convertToString(data:
|
|
34
|
+
function convertToString(data: LanguageModelV4DataContent): string {
|
|
35
35
|
if (typeof data === 'string') {
|
|
36
36
|
return new TextDecoder().decode(convertBase64ToUint8Array(data));
|
|
37
37
|
}
|
|
@@ -55,16 +55,16 @@ function convertToString(data: LanguageModelV3DataContent): string {
|
|
|
55
55
|
* Checks if data is a URL (either a URL object or a URL string).
|
|
56
56
|
*/
|
|
57
57
|
function isUrlData(
|
|
58
|
-
data:
|
|
58
|
+
data: LanguageModelV4DataContent,
|
|
59
59
|
): data is URL | (string & { __brand: 'url-string' }) {
|
|
60
60
|
return data instanceof URL || isUrlString(data);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
function isUrlString(data:
|
|
63
|
+
function isUrlString(data: LanguageModelV4DataContent): boolean {
|
|
64
64
|
return typeof data === 'string' && /^https?:\/\//i.test(data);
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
function getUrlString(data:
|
|
67
|
+
function getUrlString(data: LanguageModelV4DataContent): string {
|
|
68
68
|
return data instanceof URL ? data.toString() : (data as string);
|
|
69
69
|
}
|
|
70
70
|
|
|
@@ -75,9 +75,9 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
75
75
|
cacheControlValidator,
|
|
76
76
|
toolNameMapping,
|
|
77
77
|
}: {
|
|
78
|
-
prompt:
|
|
78
|
+
prompt: LanguageModelV4Prompt;
|
|
79
79
|
sendReasoning: boolean;
|
|
80
|
-
warnings:
|
|
80
|
+
warnings: SharedV4Warning[];
|
|
81
81
|
cacheControlValidator?: CacheControlValidator;
|
|
82
82
|
toolNameMapping: ToolNameMapping;
|
|
83
83
|
}): Promise<{
|
|
@@ -92,7 +92,7 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
92
92
|
const messages: AnthropicMessagesPrompt['messages'] = [];
|
|
93
93
|
|
|
94
94
|
async function shouldEnableCitations(
|
|
95
|
-
providerMetadata:
|
|
95
|
+
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
96
96
|
): Promise<boolean> {
|
|
97
97
|
const anthropicOptions = await parseProviderOptions({
|
|
98
98
|
provider: 'anthropic',
|
|
@@ -104,7 +104,7 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
async function getDocumentMetadata(
|
|
107
|
-
providerMetadata:
|
|
107
|
+
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
108
108
|
): Promise<{ title?: string; context?: string }> {
|
|
109
109
|
const anthropicOptions = await parseProviderOptions({
|
|
110
110
|
provider: 'anthropic',
|
|
@@ -1051,19 +1051,19 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
1051
1051
|
|
|
1052
1052
|
type SystemBlock = {
|
|
1053
1053
|
type: 'system';
|
|
1054
|
-
messages: Array<
|
|
1054
|
+
messages: Array<LanguageModelV4Message & { role: 'system' }>;
|
|
1055
1055
|
};
|
|
1056
1056
|
type AssistantBlock = {
|
|
1057
1057
|
type: 'assistant';
|
|
1058
|
-
messages: Array<
|
|
1058
|
+
messages: Array<LanguageModelV4Message & { role: 'assistant' }>;
|
|
1059
1059
|
};
|
|
1060
1060
|
type UserBlock = {
|
|
1061
1061
|
type: 'user';
|
|
1062
|
-
messages: Array<
|
|
1062
|
+
messages: Array<LanguageModelV4Message & { role: 'user' | 'tool' }>;
|
|
1063
1063
|
};
|
|
1064
1064
|
|
|
1065
1065
|
function groupIntoBlocks(
|
|
1066
|
-
prompt:
|
|
1066
|
+
prompt: LanguageModelV4Prompt,
|
|
1067
1067
|
): Array<SystemBlock | AssistantBlock | UserBlock> {
|
|
1068
1068
|
const blocks: Array<SystemBlock | AssistantBlock | UserBlock> = [];
|
|
1069
1069
|
let currentBlock: SystemBlock | AssistantBlock | UserBlock | undefined =
|
package/src/get-cache-control.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SharedV4Warning, SharedV4ProviderMetadata } from '@ai-sdk/provider';
|
|
2
2
|
import { AnthropicCacheControl } from './anthropic-messages-api';
|
|
3
3
|
|
|
4
4
|
// Anthropic allows a maximum of 4 cache breakpoints per request
|
|
@@ -7,7 +7,7 @@ const MAX_CACHE_BREAKPOINTS = 4;
|
|
|
7
7
|
// Helper function to extract cache_control from provider metadata
|
|
8
8
|
// Allows both cacheControl and cache_control for flexibility
|
|
9
9
|
function getCacheControl(
|
|
10
|
-
providerMetadata:
|
|
10
|
+
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
11
11
|
): AnthropicCacheControl | undefined {
|
|
12
12
|
const anthropic = providerMetadata?.anthropic;
|
|
13
13
|
|
|
@@ -21,10 +21,10 @@ function getCacheControl(
|
|
|
21
21
|
|
|
22
22
|
export class CacheControlValidator {
|
|
23
23
|
private breakpointCount = 0;
|
|
24
|
-
private warnings:
|
|
24
|
+
private warnings: SharedV4Warning[] = [];
|
|
25
25
|
|
|
26
26
|
getCacheControl(
|
|
27
|
-
providerMetadata:
|
|
27
|
+
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
28
28
|
context: { type: string; canCache: boolean },
|
|
29
29
|
): AnthropicCacheControl | undefined {
|
|
30
30
|
const cacheControlValue = getCacheControl(providerMetadata);
|
|
@@ -57,7 +57,7 @@ export class CacheControlValidator {
|
|
|
57
57
|
return cacheControlValue;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
getWarnings():
|
|
60
|
+
getWarnings(): SharedV4Warning[] {
|
|
61
61
|
return this.warnings;
|
|
62
62
|
}
|
|
63
63
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LanguageModelV4FinishReason } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @see https://docs.anthropic.com/en/api/messages#response-stop-reason
|
|
@@ -9,7 +9,7 @@ export function mapAnthropicStopReason({
|
|
|
9
9
|
}: {
|
|
10
10
|
finishReason: string | null | undefined;
|
|
11
11
|
isJsonResponseFromTool?: boolean;
|
|
12
|
-
}):
|
|
12
|
+
}): LanguageModelV4FinishReason['unified'] {
|
|
13
13
|
switch (finishReason) {
|
|
14
14
|
case 'pause_turn':
|
|
15
15
|
case 'end_turn':
|