@fairyhunter13/ai-anthropic 3.0.58-fork.2 → 3.0.58-fork.21
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 +101 -4
- package/dist/index.d.mts +85 -6
- package/dist/index.d.ts +85 -6
- package/dist/index.js +696 -376
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +671 -347
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +124 -14
- package/dist/internal/index.d.ts +124 -14
- package/dist/internal/index.js +690 -368
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +670 -345
- package/dist/internal/index.mjs.map +1 -1
- package/docs/05-anthropic.mdx +53 -6
- package/package.json +7 -7
- package/src/anthropic-messages-api.ts +31 -7
- package/src/anthropic-messages-language-model.ts +272 -36
- package/src/anthropic-messages-options.ts +24 -0
- package/src/anthropic-prepare-tools.ts +50 -14
- package/src/anthropic-provider.ts +8 -8
- package/src/anthropic-tools.ts +18 -0
- package/src/convert-anthropic-messages-usage.ts +2 -2
- package/src/convert-to-anthropic-messages-prompt.ts +47 -27
- package/src/get-cache-control.ts +5 -5
- package/src/internal/index.ts +4 -1
- package/src/map-anthropic-stop-reason.ts +2 -2
- package/src/tool/web-fetch-20260309.ts +182 -0
- package/src/tool/web-search_20260209.ts +17 -0
|
@@ -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';
|
|
@@ -8,6 +8,7 @@ import { CacheControlValidator } from './get-cache-control';
|
|
|
8
8
|
import { textEditor_20250728ArgsSchema } from './tool/text-editor_20250728';
|
|
9
9
|
import { webSearch_20260209ArgsSchema } from './tool/web-search_20260209';
|
|
10
10
|
import { webSearch_20250305ArgsSchema } from './tool/web-search_20250305';
|
|
11
|
+
import { webFetch_20260309ArgsSchema } from './tool/web-fetch-20260309';
|
|
11
12
|
import { webFetch_20260209ArgsSchema } from './tool/web-fetch-20260209';
|
|
12
13
|
import { webFetch_20250910ArgsSchema } from './tool/web-fetch-20250910';
|
|
13
14
|
import { validateTypes } from '@ai-sdk/provider-utils';
|
|
@@ -26,26 +27,32 @@ export async function prepareTools({
|
|
|
26
27
|
disableParallelToolUse,
|
|
27
28
|
cacheControlValidator,
|
|
28
29
|
supportsStructuredOutput,
|
|
30
|
+
supportsStrictTools,
|
|
29
31
|
}: {
|
|
30
|
-
tools:
|
|
31
|
-
toolChoice:
|
|
32
|
+
tools: LanguageModelV4CallOptions['tools'];
|
|
33
|
+
toolChoice: LanguageModelV4CallOptions['toolChoice'] | undefined;
|
|
32
34
|
disableParallelToolUse?: boolean;
|
|
33
35
|
cacheControlValidator?: CacheControlValidator;
|
|
34
36
|
|
|
35
37
|
/**
|
|
36
|
-
* Whether the model supports structured output.
|
|
38
|
+
* Whether the model supports native structured output response format.
|
|
37
39
|
*/
|
|
38
40
|
supportsStructuredOutput: boolean;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Whether the model supports strict mode on tool definitions.
|
|
44
|
+
*/
|
|
45
|
+
supportsStrictTools: boolean;
|
|
39
46
|
}): Promise<{
|
|
40
47
|
tools: Array<AnthropicTool> | undefined;
|
|
41
48
|
toolChoice: AnthropicToolChoice | undefined;
|
|
42
|
-
toolWarnings:
|
|
49
|
+
toolWarnings: SharedV4Warning[];
|
|
43
50
|
betas: Set<string>;
|
|
44
51
|
}> {
|
|
45
52
|
// when the tools array is empty, change it to undefined to prevent errors:
|
|
46
53
|
tools = tools?.length ? tools : undefined;
|
|
47
54
|
|
|
48
|
-
const toolWarnings:
|
|
55
|
+
const toolWarnings: SharedV4Warning[] = [];
|
|
49
56
|
const betas = new Set<string>();
|
|
50
57
|
const validator = cacheControlValidator || new CacheControlValidator();
|
|
51
58
|
|
|
@@ -72,13 +79,21 @@ export async function prepareTools({
|
|
|
72
79
|
const deferLoading = anthropicOptions?.deferLoading;
|
|
73
80
|
const allowedCallers = anthropicOptions?.allowedCallers;
|
|
74
81
|
|
|
82
|
+
if (!supportsStrictTools && tool.strict != null) {
|
|
83
|
+
toolWarnings.push({
|
|
84
|
+
type: 'unsupported',
|
|
85
|
+
feature: 'strict',
|
|
86
|
+
details: `Tool '${tool.name}' has strict: ${tool.strict}, but strict mode is not supported by this provider. The strict property will be ignored.`,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
75
90
|
anthropicTools.push({
|
|
76
91
|
name: tool.name,
|
|
77
92
|
description: tool.description,
|
|
78
93
|
input_schema: tool.inputSchema,
|
|
79
94
|
cache_control: cacheControl,
|
|
80
95
|
...(eagerInputStreaming ? { eager_input_streaming: true } : {}),
|
|
81
|
-
...(
|
|
96
|
+
...(supportsStrictTools === true && tool.strict != null
|
|
82
97
|
? { strict: tool.strict }
|
|
83
98
|
: {}),
|
|
84
99
|
...(deferLoading != null ? { defer_loading: deferLoading } : {}),
|
|
@@ -120,7 +135,7 @@ export async function prepareTools({
|
|
|
120
135
|
break;
|
|
121
136
|
}
|
|
122
137
|
case 'anthropic.code_execution_20250825': {
|
|
123
|
-
|
|
138
|
+
// GA — no beta header needed
|
|
124
139
|
anthropicTools.push({
|
|
125
140
|
type: 'code_execution_20250825',
|
|
126
141
|
name: 'code_execution',
|
|
@@ -238,7 +253,7 @@ export async function prepareTools({
|
|
|
238
253
|
break;
|
|
239
254
|
}
|
|
240
255
|
case 'anthropic.web_fetch_20250910': {
|
|
241
|
-
|
|
256
|
+
// GA — no beta header needed
|
|
242
257
|
const args = await validateTypes({
|
|
243
258
|
value: tool.args,
|
|
244
259
|
schema: webFetch_20250910ArgsSchema,
|
|
@@ -256,7 +271,7 @@ export async function prepareTools({
|
|
|
256
271
|
break;
|
|
257
272
|
}
|
|
258
273
|
case 'anthropic.web_fetch_20260209': {
|
|
259
|
-
|
|
274
|
+
// GA — no beta header needed
|
|
260
275
|
const args = await validateTypes({
|
|
261
276
|
value: tool.args,
|
|
262
277
|
schema: webFetch_20260209ArgsSchema,
|
|
@@ -273,6 +288,28 @@ export async function prepareTools({
|
|
|
273
288
|
});
|
|
274
289
|
break;
|
|
275
290
|
}
|
|
291
|
+
case 'anthropic.web_fetch_20260309': {
|
|
292
|
+
// GA version — no beta header needed
|
|
293
|
+
const args = await validateTypes({
|
|
294
|
+
value: tool.args,
|
|
295
|
+
schema: webFetch_20260309ArgsSchema,
|
|
296
|
+
});
|
|
297
|
+
anthropicTools.push({
|
|
298
|
+
type: 'web_fetch_20260309',
|
|
299
|
+
name: 'web_fetch',
|
|
300
|
+
max_uses: args.maxUses,
|
|
301
|
+
allowed_domains: args.allowedDomains,
|
|
302
|
+
blocked_domains: args.blockedDomains,
|
|
303
|
+
citations: args.citations,
|
|
304
|
+
max_content_tokens: args.maxContentTokens,
|
|
305
|
+
allowed_callers: args.allowedCallers,
|
|
306
|
+
use_cache: args.useCache,
|
|
307
|
+
defer_loading: args.deferLoading,
|
|
308
|
+
strict: args.strict,
|
|
309
|
+
cache_control: undefined,
|
|
310
|
+
});
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
276
313
|
case 'anthropic.web_search_20250305': {
|
|
277
314
|
const args = await validateTypes({
|
|
278
315
|
value: tool.args,
|
|
@@ -290,7 +327,7 @@ export async function prepareTools({
|
|
|
290
327
|
break;
|
|
291
328
|
}
|
|
292
329
|
case 'anthropic.web_search_20260209': {
|
|
293
|
-
|
|
330
|
+
// GA — no beta header needed
|
|
294
331
|
const args = await validateTypes({
|
|
295
332
|
value: tool.args,
|
|
296
333
|
schema: webSearch_20260209ArgsSchema,
|
|
@@ -302,13 +339,13 @@ export async function prepareTools({
|
|
|
302
339
|
allowed_domains: args.allowedDomains,
|
|
303
340
|
blocked_domains: args.blockedDomains,
|
|
304
341
|
user_location: args.userLocation,
|
|
342
|
+
allowed_callers: args.allowedCallers,
|
|
305
343
|
cache_control: undefined,
|
|
306
344
|
});
|
|
307
345
|
break;
|
|
308
346
|
}
|
|
309
347
|
|
|
310
348
|
case 'anthropic.tool_search_regex_20251119': {
|
|
311
|
-
betas.add('advanced-tool-use-2025-11-20');
|
|
312
349
|
anthropicTools.push({
|
|
313
350
|
type: 'tool_search_tool_regex_20251119',
|
|
314
351
|
name: 'tool_search_tool_regex',
|
|
@@ -317,7 +354,6 @@ export async function prepareTools({
|
|
|
317
354
|
}
|
|
318
355
|
|
|
319
356
|
case 'anthropic.tool_search_bm25_20251119': {
|
|
320
|
-
betas.add('advanced-tool-use-2025-11-20');
|
|
321
357
|
anthropicTools.push({
|
|
322
358
|
type: 'tool_search_tool_bm25_20251119',
|
|
323
359
|
name: 'tool_search_tool_bm25',
|
|
@@ -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;
|
package/src/anthropic-tools.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { textEditor_20250429 } from './tool/text-editor_20250429';
|
|
|
13
13
|
import { textEditor_20250728 } from './tool/text-editor_20250728';
|
|
14
14
|
import { toolSearchBm25_20251119 } from './tool/tool-search-bm25_20251119';
|
|
15
15
|
import { toolSearchRegex_20251119 } from './tool/tool-search-regex_20251119';
|
|
16
|
+
import { webFetch_20260309 } from './tool/web-fetch-20260309';
|
|
16
17
|
import { webFetch_20260209 } from './tool/web-fetch-20260209';
|
|
17
18
|
import { webFetch_20250910 } from './tool/web-fetch-20250910';
|
|
18
19
|
import { webSearch_20260209 } from './tool/web-search_20260209';
|
|
@@ -186,6 +187,23 @@ export const anthropicTools = {
|
|
|
186
187
|
*/
|
|
187
188
|
webFetch_20260209,
|
|
188
189
|
|
|
190
|
+
/**
|
|
191
|
+
* Creates a web fetch tool that gives Claude direct access to real-time web content.
|
|
192
|
+
* GA version (no beta header required). Includes caller restrictions, cache control,
|
|
193
|
+
* deferred loading, and strict mode.
|
|
194
|
+
*
|
|
195
|
+
* @param maxUses - The max_uses parameter limits the number of web fetches performed
|
|
196
|
+
* @param allowedDomains - Only fetch from these domains
|
|
197
|
+
* @param blockedDomains - Never fetch from these domains
|
|
198
|
+
* @param citations - Citations configuration for fetched documents
|
|
199
|
+
* @param maxContentTokens - Max content tokens included in context
|
|
200
|
+
* @param allowedCallers - Restrict callers (e.g. ["direct"] to prevent code_execution usage)
|
|
201
|
+
* @param useCache - Whether to use cached content (set false for fresh content)
|
|
202
|
+
* @param deferLoading - Defer loading until tool_search discovers it
|
|
203
|
+
* @param strict - Enable strict schema validation
|
|
204
|
+
*/
|
|
205
|
+
webFetch_20260309,
|
|
206
|
+
|
|
189
207
|
/**
|
|
190
208
|
* Creates a web search tool that gives Claude direct access to real-time web content.
|
|
191
209
|
*
|
|
@@ -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,24 +55,24 @@ 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 isFileIdData(data:
|
|
67
|
+
function isFileIdData(data: LanguageModelV4DataContent): boolean {
|
|
68
68
|
return typeof data === 'string' && data.startsWith('anthropic-file://');
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
function getFileId(data:
|
|
71
|
+
function getFileId(data: LanguageModelV4DataContent): string {
|
|
72
72
|
return (data as string).replace('anthropic-file://', '');
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
function getUrlString(data:
|
|
75
|
+
function getUrlString(data: LanguageModelV4DataContent): string {
|
|
76
76
|
return data instanceof URL ? data.toString() : (data as string);
|
|
77
77
|
}
|
|
78
78
|
|
|
@@ -83,9 +83,9 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
83
83
|
cacheControlValidator,
|
|
84
84
|
toolNameMapping,
|
|
85
85
|
}: {
|
|
86
|
-
prompt:
|
|
86
|
+
prompt: LanguageModelV4Prompt;
|
|
87
87
|
sendReasoning: boolean;
|
|
88
|
-
warnings:
|
|
88
|
+
warnings: SharedV4Warning[];
|
|
89
89
|
cacheControlValidator?: CacheControlValidator;
|
|
90
90
|
toolNameMapping: ToolNameMapping;
|
|
91
91
|
}): Promise<{
|
|
@@ -100,7 +100,7 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
100
100
|
const messages: AnthropicMessagesPrompt['messages'] = [];
|
|
101
101
|
|
|
102
102
|
async function shouldEnableCitations(
|
|
103
|
-
providerMetadata:
|
|
103
|
+
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
104
104
|
): Promise<boolean> {
|
|
105
105
|
const anthropicOptions = await parseProviderOptions({
|
|
106
106
|
provider: 'anthropic',
|
|
@@ -112,7 +112,7 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
async function getDocumentMetadata(
|
|
115
|
-
providerMetadata:
|
|
115
|
+
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
116
116
|
): Promise<{ title?: string; context?: string }> {
|
|
117
117
|
const anthropicOptions = await parseProviderOptions({
|
|
118
118
|
provider: 'anthropic',
|
|
@@ -140,14 +140,17 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
140
140
|
});
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
system = block.messages.map(({ content, providerOptions }) =>
|
|
144
|
-
|
|
145
|
-
text: content,
|
|
146
|
-
cache_control: validator.getCacheControl(providerOptions, {
|
|
143
|
+
system = block.messages.map(({ content, providerOptions }) => {
|
|
144
|
+
const cc = validator.getCacheControl(providerOptions, {
|
|
147
145
|
type: 'system message',
|
|
148
146
|
canCache: true,
|
|
149
|
-
})
|
|
150
|
-
|
|
147
|
+
});
|
|
148
|
+
return {
|
|
149
|
+
type: 'text',
|
|
150
|
+
text: content,
|
|
151
|
+
cache_control: cc,
|
|
152
|
+
};
|
|
153
|
+
});
|
|
151
154
|
|
|
152
155
|
break;
|
|
153
156
|
}
|
|
@@ -909,7 +912,7 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
909
912
|
errorCode:
|
|
910
913
|
typeof extractedErrorCode === 'string'
|
|
911
914
|
? extractedErrorCode
|
|
912
|
-
: '
|
|
915
|
+
: 'unavailable',
|
|
913
916
|
};
|
|
914
917
|
}
|
|
915
918
|
|
|
@@ -918,7 +921,7 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
918
921
|
tool_use_id: part.toolCallId,
|
|
919
922
|
content: {
|
|
920
923
|
type: 'web_fetch_tool_result_error',
|
|
921
|
-
error_code: errorValue.errorCode ?? '
|
|
924
|
+
error_code: errorValue.errorCode ?? 'unavailable',
|
|
922
925
|
},
|
|
923
926
|
cache_control: cacheControl,
|
|
924
927
|
});
|
|
@@ -1012,6 +1015,23 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
1012
1015
|
) {
|
|
1013
1016
|
const output = part.output;
|
|
1014
1017
|
|
|
1018
|
+
if (
|
|
1019
|
+
output.type === 'error-text' ||
|
|
1020
|
+
output.type === 'error-json'
|
|
1021
|
+
) {
|
|
1022
|
+
anthropicContent.push({
|
|
1023
|
+
type: 'tool_search_tool_result',
|
|
1024
|
+
tool_use_id: part.toolCallId,
|
|
1025
|
+
content: {
|
|
1026
|
+
type: 'tool_search_tool_result_error',
|
|
1027
|
+
error_code: 'unavailable',
|
|
1028
|
+
},
|
|
1029
|
+
cache_control: cacheControl,
|
|
1030
|
+
});
|
|
1031
|
+
|
|
1032
|
+
break;
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1015
1035
|
if (output.type !== 'json') {
|
|
1016
1036
|
warnings.push({
|
|
1017
1037
|
type: 'other',
|
|
@@ -1076,19 +1096,19 @@ export async function convertToAnthropicMessagesPrompt({
|
|
|
1076
1096
|
|
|
1077
1097
|
type SystemBlock = {
|
|
1078
1098
|
type: 'system';
|
|
1079
|
-
messages: Array<
|
|
1099
|
+
messages: Array<LanguageModelV4Message & { role: 'system' }>;
|
|
1080
1100
|
};
|
|
1081
1101
|
type AssistantBlock = {
|
|
1082
1102
|
type: 'assistant';
|
|
1083
|
-
messages: Array<
|
|
1103
|
+
messages: Array<LanguageModelV4Message & { role: 'assistant' }>;
|
|
1084
1104
|
};
|
|
1085
1105
|
type UserBlock = {
|
|
1086
1106
|
type: 'user';
|
|
1087
|
-
messages: Array<
|
|
1107
|
+
messages: Array<LanguageModelV4Message & { role: 'user' | 'tool' }>;
|
|
1088
1108
|
};
|
|
1089
1109
|
|
|
1090
1110
|
function groupIntoBlocks(
|
|
1091
|
-
prompt:
|
|
1111
|
+
prompt: LanguageModelV4Prompt,
|
|
1092
1112
|
): Array<SystemBlock | AssistantBlock | UserBlock> {
|
|
1093
1113
|
const blocks: Array<SystemBlock | AssistantBlock | UserBlock> = [];
|
|
1094
1114
|
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
|
}
|
package/src/internal/index.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {
|
|
2
|
+
AnthropicMessagesLanguageModel,
|
|
3
|
+
getModelCapabilities,
|
|
4
|
+
} from '../anthropic-messages-language-model';
|
|
2
5
|
export { anthropicTools } from '../anthropic-tools';
|
|
3
6
|
export type { AnthropicMessagesModelId } from '../anthropic-messages-options';
|
|
4
7
|
export { prepareTools } from '../anthropic-prepare-tools';
|
|
@@ -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':
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createProviderToolFactoryWithOutputSchema,
|
|
3
|
+
lazySchema,
|
|
4
|
+
zodSchema,
|
|
5
|
+
} from '@ai-sdk/provider-utils';
|
|
6
|
+
import { z } from 'zod/v4';
|
|
7
|
+
|
|
8
|
+
export const webFetch_20260309ArgsSchema = lazySchema(() =>
|
|
9
|
+
zodSchema(
|
|
10
|
+
z.object({
|
|
11
|
+
maxUses: z.number().optional(),
|
|
12
|
+
allowedDomains: z.array(z.string()).optional(),
|
|
13
|
+
blockedDomains: z.array(z.string()).optional(),
|
|
14
|
+
citations: z.object({ enabled: z.boolean() }).optional(),
|
|
15
|
+
maxContentTokens: z.number().optional(),
|
|
16
|
+
allowedCallers: z
|
|
17
|
+
.array(
|
|
18
|
+
z.enum([
|
|
19
|
+
'direct',
|
|
20
|
+
'code_execution_20250825',
|
|
21
|
+
'code_execution_20260120',
|
|
22
|
+
]),
|
|
23
|
+
)
|
|
24
|
+
.optional(),
|
|
25
|
+
useCache: z.boolean().optional(),
|
|
26
|
+
deferLoading: z.boolean().optional(),
|
|
27
|
+
strict: z.boolean().optional(),
|
|
28
|
+
}),
|
|
29
|
+
),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
export const webFetch_20260309OutputSchema = lazySchema(() =>
|
|
33
|
+
zodSchema(
|
|
34
|
+
z.object({
|
|
35
|
+
type: z.literal('web_fetch_result'),
|
|
36
|
+
url: z.string(),
|
|
37
|
+
content: z.object({
|
|
38
|
+
type: z.literal('document'),
|
|
39
|
+
title: z.string().nullable(),
|
|
40
|
+
citations: z.object({ enabled: z.boolean() }).optional(),
|
|
41
|
+
source: z.union([
|
|
42
|
+
z.object({
|
|
43
|
+
type: z.literal('base64'),
|
|
44
|
+
mediaType: z.literal('application/pdf'),
|
|
45
|
+
data: z.string(),
|
|
46
|
+
}),
|
|
47
|
+
z.object({
|
|
48
|
+
type: z.literal('text'),
|
|
49
|
+
mediaType: z.literal('text/plain'),
|
|
50
|
+
data: z.string(),
|
|
51
|
+
}),
|
|
52
|
+
]),
|
|
53
|
+
}),
|
|
54
|
+
retrievedAt: z.string().nullable(),
|
|
55
|
+
}),
|
|
56
|
+
),
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const webFetch_20260309InputSchema = lazySchema(() =>
|
|
60
|
+
zodSchema(
|
|
61
|
+
z.object({
|
|
62
|
+
url: z.string(),
|
|
63
|
+
}),
|
|
64
|
+
),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const factory = createProviderToolFactoryWithOutputSchema<
|
|
68
|
+
{
|
|
69
|
+
/**
|
|
70
|
+
* The URL to fetch.
|
|
71
|
+
*/
|
|
72
|
+
url: string;
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
type: 'web_fetch_result';
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Fetched content URL
|
|
79
|
+
*/
|
|
80
|
+
url: string;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Fetched content.
|
|
84
|
+
*/
|
|
85
|
+
content: {
|
|
86
|
+
type: 'document';
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Title of the document
|
|
90
|
+
*/
|
|
91
|
+
title: string | null;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Citation configuration for the document
|
|
95
|
+
*/
|
|
96
|
+
citations?: { enabled: boolean };
|
|
97
|
+
|
|
98
|
+
source:
|
|
99
|
+
| {
|
|
100
|
+
type: 'base64';
|
|
101
|
+
mediaType: 'application/pdf';
|
|
102
|
+
data: string;
|
|
103
|
+
}
|
|
104
|
+
| {
|
|
105
|
+
type: 'text';
|
|
106
|
+
mediaType: 'text/plain';
|
|
107
|
+
data: string;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* ISO 8601 timestamp when the content was retrieved
|
|
113
|
+
*/
|
|
114
|
+
retrievedAt: string | null;
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
/**
|
|
118
|
+
* The maxUses parameter limits the number of web fetches performed
|
|
119
|
+
*/
|
|
120
|
+
maxUses?: number;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Only fetch from these domains
|
|
124
|
+
*/
|
|
125
|
+
allowedDomains?: string[];
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Never fetch from these domains
|
|
129
|
+
*/
|
|
130
|
+
blockedDomains?: string[];
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Unlike web search where citations are always enabled, citations are optional for
|
|
134
|
+
* web fetch. Set "citations": {"enabled": true} to enable Claude to cite specific passages
|
|
135
|
+
* from fetched documents.
|
|
136
|
+
*/
|
|
137
|
+
citations?: {
|
|
138
|
+
enabled: boolean;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The maxContentTokens parameter limits the amount of content that will be included in the context.
|
|
143
|
+
*/
|
|
144
|
+
maxContentTokens?: number;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Restrict which callers can invoke web_fetch.
|
|
148
|
+
* Set to ["direct"] to prevent code_execution from calling web_fetch programmatically.
|
|
149
|
+
*/
|
|
150
|
+
allowedCallers?: Array<
|
|
151
|
+
'direct' | 'code_execution_20250825' | 'code_execution_20260120'
|
|
152
|
+
>;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Whether to use cached content.
|
|
156
|
+
* Set to false to bypass the cache and fetch fresh content.
|
|
157
|
+
*/
|
|
158
|
+
useCache?: boolean;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* If true, tool will not be included in initial system prompt.
|
|
162
|
+
* Only loaded when returned via tool_reference from tool search.
|
|
163
|
+
*/
|
|
164
|
+
deferLoading?: boolean;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* When true, guarantees schema validation on tool names and inputs.
|
|
168
|
+
*/
|
|
169
|
+
strict?: boolean;
|
|
170
|
+
}
|
|
171
|
+
>({
|
|
172
|
+
id: 'anthropic.web_fetch_20260309',
|
|
173
|
+
inputSchema: webFetch_20260309InputSchema,
|
|
174
|
+
outputSchema: webFetch_20260309OutputSchema,
|
|
175
|
+
supportsDeferredResults: true,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
export const webFetch_20260309 = (
|
|
179
|
+
args: Parameters<typeof factory>[0] = {}, // default
|
|
180
|
+
) => {
|
|
181
|
+
return factory(args);
|
|
182
|
+
};
|