@ai-sdk/anthropic 4.0.0-beta.2 → 4.0.0-beta.3
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 +6 -0
- package/dist/index.d.mts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3 -3
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +11 -11
- package/dist/internal/index.d.ts +11 -11
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +1 -1
- package/dist/internal/index.mjs.map +1 -1
- package/docs/05-anthropic.mdx +45 -4
- package/package.json +1 -1
- package/src/anthropic-messages-language-model.ts +34 -34
- package/src/anthropic-prepare-tools.ts +6 -6
- 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,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,7 +122,7 @@ 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
|
/**
|
|
@@ -131,8 +131,8 @@ type AnthropicMessagesConfig = {
|
|
|
131
131
|
supportsNativeStructuredOutput?: boolean;
|
|
132
132
|
};
|
|
133
133
|
|
|
134
|
-
export class AnthropicMessagesLanguageModel implements
|
|
135
|
-
readonly specificationVersion = '
|
|
134
|
+
export class AnthropicMessagesLanguageModel implements LanguageModelV4 {
|
|
135
|
+
readonly specificationVersion = 'v4';
|
|
136
136
|
|
|
137
137
|
readonly modelId: AnthropicMessagesModelId;
|
|
138
138
|
|
|
@@ -186,11 +186,11 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
186
186
|
toolChoice,
|
|
187
187
|
providerOptions,
|
|
188
188
|
stream,
|
|
189
|
-
}:
|
|
189
|
+
}: LanguageModelV4CallOptions & {
|
|
190
190
|
stream: boolean;
|
|
191
191
|
userSuppliedBetas: Set<string>;
|
|
192
192
|
}) {
|
|
193
|
-
const warnings:
|
|
193
|
+
const warnings: SharedV4Warning[] = [];
|
|
194
194
|
|
|
195
195
|
if (frequencyPenalty != null) {
|
|
196
196
|
warnings.push({ type: 'unsupported', feature: 'frequencyPenalty' });
|
|
@@ -276,7 +276,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
276
276
|
structureOutputMode === 'outputFormat' ||
|
|
277
277
|
(structureOutputMode === 'auto' && supportsStructuredOutput);
|
|
278
278
|
|
|
279
|
-
const jsonResponseTool:
|
|
279
|
+
const jsonResponseTool: LanguageModelV4FunctionTool | undefined =
|
|
280
280
|
responseFormat?.type === 'json' &&
|
|
281
281
|
responseFormat.schema != null &&
|
|
282
282
|
!useStructuredOutput
|
|
@@ -694,7 +694,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
694
694
|
return this.config.transformRequestBody?.(args, betas) ?? args;
|
|
695
695
|
}
|
|
696
696
|
|
|
697
|
-
private extractCitationDocuments(prompt:
|
|
697
|
+
private extractCitationDocuments(prompt: LanguageModelV4Prompt): Array<{
|
|
698
698
|
title: string;
|
|
699
699
|
filename?: string;
|
|
700
700
|
mediaType: string;
|
|
@@ -738,8 +738,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
738
738
|
}
|
|
739
739
|
|
|
740
740
|
async doGenerate(
|
|
741
|
-
options:
|
|
742
|
-
): Promise<
|
|
741
|
+
options: LanguageModelV4CallOptions,
|
|
742
|
+
): Promise<LanguageModelV4GenerateResult> {
|
|
743
743
|
const {
|
|
744
744
|
args,
|
|
745
745
|
warnings,
|
|
@@ -779,8 +779,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
779
779
|
fetch: this.config.fetch,
|
|
780
780
|
});
|
|
781
781
|
|
|
782
|
-
const content: Array<
|
|
783
|
-
const mcpToolCalls: Record<string,
|
|
782
|
+
const content: Array<LanguageModelV4Content> = [];
|
|
783
|
+
const mcpToolCalls: Record<string, LanguageModelV4ToolCall> = {};
|
|
784
784
|
const serverToolCalls: Record<string, string> = {}; // tool_use_id -> provider tool name
|
|
785
785
|
let isJsonResponseFromTool = false;
|
|
786
786
|
|
|
@@ -1205,7 +1205,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1205
1205
|
) ?? null,
|
|
1206
1206
|
} satisfies AnthropicMessageMetadata;
|
|
1207
1207
|
|
|
1208
|
-
const providerMetadata:
|
|
1208
|
+
const providerMetadata: SharedV4ProviderMetadata = {
|
|
1209
1209
|
anthropic: anthropicMetadata,
|
|
1210
1210
|
};
|
|
1211
1211
|
|
|
@@ -1219,8 +1219,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1219
1219
|
}
|
|
1220
1220
|
|
|
1221
1221
|
async doStream(
|
|
1222
|
-
options:
|
|
1223
|
-
): Promise<
|
|
1222
|
+
options: LanguageModelV4CallOptions,
|
|
1223
|
+
): Promise<LanguageModelV4StreamResult> {
|
|
1224
1224
|
const {
|
|
1225
1225
|
args: body,
|
|
1226
1226
|
warnings,
|
|
@@ -1257,7 +1257,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1257
1257
|
fetch: this.config.fetch,
|
|
1258
1258
|
});
|
|
1259
1259
|
|
|
1260
|
-
let finishReason:
|
|
1260
|
+
let finishReason: LanguageModelV4FinishReason = {
|
|
1261
1261
|
unified: 'other',
|
|
1262
1262
|
raw: undefined,
|
|
1263
1263
|
};
|
|
@@ -1289,7 +1289,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1289
1289
|
}
|
|
1290
1290
|
| { type: 'text' | 'reasoning' }
|
|
1291
1291
|
> = {};
|
|
1292
|
-
const mcpToolCalls: Record<string,
|
|
1292
|
+
const mcpToolCalls: Record<string, LanguageModelV4ToolCall> = {};
|
|
1293
1293
|
const serverToolCalls: Record<string, string> = {}; // tool_use_id -> provider tool name
|
|
1294
1294
|
|
|
1295
1295
|
let contextManagement:
|
|
@@ -1323,7 +1323,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
1323
1323
|
const transformedStream = response.pipeThrough(
|
|
1324
1324
|
new TransformStream<
|
|
1325
1325
|
ParseResult<InferSchema<typeof anthropicMessagesChunkSchema>>,
|
|
1326
|
-
|
|
1326
|
+
LanguageModelV4StreamPart
|
|
1327
1327
|
>({
|
|
1328
1328
|
start(controller) {
|
|
1329
1329
|
controller.enqueue({ type: 'stream-start', warnings });
|
|
@@ -2176,7 +2176,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
|
|
|
2176
2176
|
contextManagement,
|
|
2177
2177
|
} satisfies AnthropicMessageMetadata;
|
|
2178
2178
|
|
|
2179
|
-
const providerMetadata:
|
|
2179
|
+
const providerMetadata: SharedV4ProviderMetadata = {
|
|
2180
2180
|
anthropic: anthropicMetadata,
|
|
2181
2181
|
};
|
|
2182
2182
|
|
|
@@ -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';
|
|
@@ -27,8 +27,8 @@ export async function prepareTools({
|
|
|
27
27
|
cacheControlValidator,
|
|
28
28
|
supportsStructuredOutput,
|
|
29
29
|
}: {
|
|
30
|
-
tools:
|
|
31
|
-
toolChoice:
|
|
30
|
+
tools: LanguageModelV4CallOptions['tools'];
|
|
31
|
+
toolChoice: LanguageModelV4CallOptions['toolChoice'] | undefined;
|
|
32
32
|
disableParallelToolUse?: boolean;
|
|
33
33
|
cacheControlValidator?: CacheControlValidator;
|
|
34
34
|
|
|
@@ -39,13 +39,13 @@ export async function prepareTools({
|
|
|
39
39
|
}): Promise<{
|
|
40
40
|
tools: Array<AnthropicTool> | undefined;
|
|
41
41
|
toolChoice: AnthropicToolChoice | undefined;
|
|
42
|
-
toolWarnings:
|
|
42
|
+
toolWarnings: SharedV4Warning[];
|
|
43
43
|
betas: Set<string>;
|
|
44
44
|
}> {
|
|
45
45
|
// when the tools array is empty, change it to undefined to prevent errors:
|
|
46
46
|
tools = tools?.length ? tools : undefined;
|
|
47
47
|
|
|
48
|
-
const toolWarnings:
|
|
48
|
+
const toolWarnings: SharedV4Warning[] = [];
|
|
49
49
|
const betas = new Set<string>();
|
|
50
50
|
const validator = cacheControlValidator || new CacheControlValidator();
|
|
51
51
|
|
|
@@ -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':
|