@fairyhunter13/ai-anthropic 3.0.58-fork.1
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 +2521 -0
- package/LICENSE +13 -0
- package/README.md +43 -0
- package/dist/index.d.mts +1099 -0
- package/dist/index.d.ts +1099 -0
- package/dist/index.js +5222 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +5298 -0
- package/dist/index.mjs.map +1 -0
- package/dist/internal/index.d.mts +960 -0
- package/dist/internal/index.d.ts +960 -0
- package/dist/internal/index.js +5122 -0
- package/dist/internal/index.js.map +1 -0
- package/dist/internal/index.mjs +5190 -0
- package/dist/internal/index.mjs.map +1 -0
- package/docs/05-anthropic.mdx +1321 -0
- package/internal.d.ts +1 -0
- package/package.json +83 -0
- package/src/anthropic-error.ts +26 -0
- package/src/anthropic-message-metadata.ts +143 -0
- package/src/anthropic-messages-api.ts +1348 -0
- package/src/anthropic-messages-language-model.ts +2407 -0
- package/src/anthropic-messages-options.ts +267 -0
- package/src/anthropic-prepare-tools.ts +404 -0
- package/src/anthropic-provider.ts +177 -0
- package/src/anthropic-tools.ts +238 -0
- package/src/convert-anthropic-messages-usage.ts +73 -0
- package/src/convert-to-anthropic-messages-prompt.ts +1144 -0
- package/src/forward-anthropic-container-id-from-last-step.ts +38 -0
- package/src/get-cache-control.ts +63 -0
- package/src/index.ts +17 -0
- package/src/internal/index.ts +4 -0
- package/src/map-anthropic-stop-reason.ts +30 -0
- package/src/tool/bash_20241022.ts +33 -0
- package/src/tool/bash_20250124.ts +33 -0
- package/src/tool/code-execution_20250522.ts +61 -0
- package/src/tool/code-execution_20250825.ts +281 -0
- package/src/tool/code-execution_20260120.ts +315 -0
- package/src/tool/computer_20241022.ts +87 -0
- package/src/tool/computer_20250124.ts +130 -0
- package/src/tool/computer_20251124.ts +151 -0
- package/src/tool/memory_20250818.ts +62 -0
- package/src/tool/text-editor_20241022.ts +69 -0
- package/src/tool/text-editor_20250124.ts +69 -0
- package/src/tool/text-editor_20250429.ts +70 -0
- package/src/tool/text-editor_20250728.ts +86 -0
- package/src/tool/tool-search-bm25_20251119.ts +99 -0
- package/src/tool/tool-search-regex_20251119.ts +111 -0
- package/src/tool/web-fetch-20250910.ts +145 -0
- package/src/tool/web-fetch-20260209.ts +145 -0
- package/src/tool/web-search_20250305.ts +136 -0
- package/src/tool/web-search_20260209.ts +136 -0
- package/src/version.ts +6 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import {
|
|
2
|
+
InvalidArgumentError,
|
|
3
|
+
LanguageModelV3,
|
|
4
|
+
NoSuchModelError,
|
|
5
|
+
ProviderV3,
|
|
6
|
+
} from '@ai-sdk/provider';
|
|
7
|
+
import {
|
|
8
|
+
FetchFunction,
|
|
9
|
+
generateId,
|
|
10
|
+
loadApiKey,
|
|
11
|
+
loadOptionalSetting,
|
|
12
|
+
withoutTrailingSlash,
|
|
13
|
+
withUserAgentSuffix,
|
|
14
|
+
} from '@ai-sdk/provider-utils';
|
|
15
|
+
import { VERSION } from './version';
|
|
16
|
+
import { AnthropicMessagesLanguageModel } from './anthropic-messages-language-model';
|
|
17
|
+
import { AnthropicMessagesModelId } from './anthropic-messages-options';
|
|
18
|
+
import { anthropicTools } from './anthropic-tools';
|
|
19
|
+
|
|
20
|
+
export interface AnthropicProvider extends ProviderV3 {
|
|
21
|
+
/**
|
|
22
|
+
* Creates a model for text generation.
|
|
23
|
+
*/
|
|
24
|
+
(modelId: AnthropicMessagesModelId): LanguageModelV3;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Creates a model for text generation.
|
|
28
|
+
*/
|
|
29
|
+
languageModel(modelId: AnthropicMessagesModelId): LanguageModelV3;
|
|
30
|
+
|
|
31
|
+
chat(modelId: AnthropicMessagesModelId): LanguageModelV3;
|
|
32
|
+
|
|
33
|
+
messages(modelId: AnthropicMessagesModelId): LanguageModelV3;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @deprecated Use `embeddingModel` instead.
|
|
37
|
+
*/
|
|
38
|
+
textEmbeddingModel(modelId: string): never;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Anthropic-specific computer use tool.
|
|
42
|
+
*/
|
|
43
|
+
tools: typeof anthropicTools;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface AnthropicProviderSettings {
|
|
47
|
+
/**
|
|
48
|
+
* Use a different URL prefix for API calls, e.g. to use proxy servers.
|
|
49
|
+
* The default prefix is `https://api.anthropic.com/v1`.
|
|
50
|
+
*/
|
|
51
|
+
baseURL?: string;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* API key that is being send using the `x-api-key` header.
|
|
55
|
+
* It defaults to the `ANTHROPIC_API_KEY` environment variable.
|
|
56
|
+
* Only one of `apiKey` or `authToken` is required.
|
|
57
|
+
*/
|
|
58
|
+
apiKey?: string;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Auth token that is being sent using the `Authorization: Bearer` header.
|
|
62
|
+
* It defaults to the `ANTHROPIC_AUTH_TOKEN` environment variable.
|
|
63
|
+
* Only one of `apiKey` or `authToken` is required.
|
|
64
|
+
*/
|
|
65
|
+
authToken?: string;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Custom headers to include in the requests.
|
|
69
|
+
*/
|
|
70
|
+
headers?: Record<string, string>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
74
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
75
|
+
*/
|
|
76
|
+
fetch?: FetchFunction;
|
|
77
|
+
|
|
78
|
+
generateId?: () => string;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Custom provider name
|
|
82
|
+
* Defaults to 'anthropic.messages'.
|
|
83
|
+
*/
|
|
84
|
+
name?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Create an Anthropic provider instance.
|
|
89
|
+
*/
|
|
90
|
+
export function createAnthropic(
|
|
91
|
+
options: AnthropicProviderSettings = {},
|
|
92
|
+
): AnthropicProvider {
|
|
93
|
+
const baseURL =
|
|
94
|
+
withoutTrailingSlash(
|
|
95
|
+
loadOptionalSetting({
|
|
96
|
+
settingValue: options.baseURL,
|
|
97
|
+
environmentVariableName: 'ANTHROPIC_BASE_URL',
|
|
98
|
+
}),
|
|
99
|
+
) ?? 'https://api.anthropic.com/v1';
|
|
100
|
+
|
|
101
|
+
const providerName = options.name ?? 'anthropic.messages';
|
|
102
|
+
|
|
103
|
+
// Only error if both are explicitly provided in options
|
|
104
|
+
if (options.apiKey && options.authToken) {
|
|
105
|
+
throw new InvalidArgumentError({
|
|
106
|
+
argument: 'apiKey/authToken',
|
|
107
|
+
message:
|
|
108
|
+
'Both apiKey and authToken were provided. Please use only one authentication method.',
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const getHeaders = () => {
|
|
113
|
+
const authHeaders: Record<string, string> = options.authToken
|
|
114
|
+
? { Authorization: `Bearer ${options.authToken}` }
|
|
115
|
+
: {
|
|
116
|
+
'x-api-key': loadApiKey({
|
|
117
|
+
apiKey: options.apiKey,
|
|
118
|
+
environmentVariableName: 'ANTHROPIC_API_KEY',
|
|
119
|
+
description: 'Anthropic',
|
|
120
|
+
}),
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
return withUserAgentSuffix(
|
|
124
|
+
{
|
|
125
|
+
'anthropic-version': '2023-06-01',
|
|
126
|
+
...authHeaders,
|
|
127
|
+
...options.headers,
|
|
128
|
+
},
|
|
129
|
+
`ai-sdk/anthropic/${VERSION}`,
|
|
130
|
+
);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const createChatModel = (modelId: AnthropicMessagesModelId) =>
|
|
134
|
+
new AnthropicMessagesLanguageModel(modelId, {
|
|
135
|
+
provider: providerName,
|
|
136
|
+
baseURL,
|
|
137
|
+
headers: getHeaders,
|
|
138
|
+
fetch: options.fetch,
|
|
139
|
+
generateId: options.generateId ?? generateId,
|
|
140
|
+
supportedUrls: () => ({
|
|
141
|
+
'image/*': [/^https?:\/\/.*$/],
|
|
142
|
+
'application/pdf': [/^https?:\/\/.*$/],
|
|
143
|
+
}),
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const provider = function (modelId: AnthropicMessagesModelId) {
|
|
147
|
+
if (new.target) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
'The Anthropic model function cannot be called with the new keyword.',
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return createChatModel(modelId);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
provider.specificationVersion = 'v3' as const;
|
|
157
|
+
provider.languageModel = createChatModel;
|
|
158
|
+
provider.chat = createChatModel;
|
|
159
|
+
provider.messages = createChatModel;
|
|
160
|
+
|
|
161
|
+
provider.embeddingModel = (modelId: string) => {
|
|
162
|
+
throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });
|
|
163
|
+
};
|
|
164
|
+
provider.textEmbeddingModel = provider.embeddingModel;
|
|
165
|
+
provider.imageModel = (modelId: string) => {
|
|
166
|
+
throw new NoSuchModelError({ modelId, modelType: 'imageModel' });
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
provider.tools = anthropicTools;
|
|
170
|
+
|
|
171
|
+
return provider;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Default Anthropic provider instance.
|
|
176
|
+
*/
|
|
177
|
+
export const anthropic = createAnthropic();
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { bash_20241022 } from './tool/bash_20241022';
|
|
2
|
+
import { bash_20250124 } from './tool/bash_20250124';
|
|
3
|
+
import { codeExecution_20250522 } from './tool/code-execution_20250522';
|
|
4
|
+
import { codeExecution_20250825 } from './tool/code-execution_20250825';
|
|
5
|
+
import { codeExecution_20260120 } from './tool/code-execution_20260120';
|
|
6
|
+
import { computer_20241022 } from './tool/computer_20241022';
|
|
7
|
+
import { computer_20250124 } from './tool/computer_20250124';
|
|
8
|
+
import { computer_20251124 } from './tool/computer_20251124';
|
|
9
|
+
import { memory_20250818 } from './tool/memory_20250818';
|
|
10
|
+
import { textEditor_20241022 } from './tool/text-editor_20241022';
|
|
11
|
+
import { textEditor_20250124 } from './tool/text-editor_20250124';
|
|
12
|
+
import { textEditor_20250429 } from './tool/text-editor_20250429';
|
|
13
|
+
import { textEditor_20250728 } from './tool/text-editor_20250728';
|
|
14
|
+
import { toolSearchBm25_20251119 } from './tool/tool-search-bm25_20251119';
|
|
15
|
+
import { toolSearchRegex_20251119 } from './tool/tool-search-regex_20251119';
|
|
16
|
+
import { webFetch_20260209 } from './tool/web-fetch-20260209';
|
|
17
|
+
import { webFetch_20250910 } from './tool/web-fetch-20250910';
|
|
18
|
+
import { webSearch_20260209 } from './tool/web-search_20260209';
|
|
19
|
+
import { webSearch_20250305 } from './tool/web-search_20250305';
|
|
20
|
+
|
|
21
|
+
export const anthropicTools = {
|
|
22
|
+
/**
|
|
23
|
+
* The bash tool enables Claude to execute shell commands in a persistent bash session,
|
|
24
|
+
* allowing system operations, script execution, and command-line automation.
|
|
25
|
+
*
|
|
26
|
+
* Image results are supported.
|
|
27
|
+
*/
|
|
28
|
+
bash_20241022,
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The bash tool enables Claude to execute shell commands in a persistent bash session,
|
|
32
|
+
* allowing system operations, script execution, and command-line automation.
|
|
33
|
+
*
|
|
34
|
+
* Image results are supported.
|
|
35
|
+
*/
|
|
36
|
+
bash_20250124,
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Claude can analyze data, create visualizations, perform complex calculations,
|
|
40
|
+
* run system commands, create and edit files, and process uploaded files directly within
|
|
41
|
+
* the API conversation.
|
|
42
|
+
*
|
|
43
|
+
* The code execution tool allows Claude to run Bash commands and manipulate files,
|
|
44
|
+
* including writing code, in a secure, sandboxed environment.
|
|
45
|
+
*/
|
|
46
|
+
codeExecution_20250522,
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Claude can analyze data, create visualizations, perform complex calculations,
|
|
50
|
+
* run system commands, create and edit files, and process uploaded files directly within
|
|
51
|
+
* the API conversation.
|
|
52
|
+
*
|
|
53
|
+
* The code execution tool allows Claude to run both Python and Bash commands and manipulate files,
|
|
54
|
+
* including writing code, in a secure, sandboxed environment.
|
|
55
|
+
*
|
|
56
|
+
* This is the latest version with enhanced Bash support and file operations.
|
|
57
|
+
*/
|
|
58
|
+
codeExecution_20250825,
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Claude can analyze data, create visualizations, perform complex calculations,
|
|
62
|
+
* run system commands, create and edit files, and process uploaded files directly within
|
|
63
|
+
* the API conversation.
|
|
64
|
+
*
|
|
65
|
+
* The code execution tool allows Claude to run both Python and Bash commands and manipulate files,
|
|
66
|
+
* including writing code, in a secure, sandboxed environment.
|
|
67
|
+
*
|
|
68
|
+
* This is the recommended version. Does not require a beta header.
|
|
69
|
+
*
|
|
70
|
+
* Supported models: Claude Opus 4.6, Sonnet 4.6, Sonnet 4.5, Opus 4.5
|
|
71
|
+
*/
|
|
72
|
+
codeExecution_20260120,
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Claude can interact with computer environments through the computer use tool, which
|
|
76
|
+
* provides screenshot capabilities and mouse/keyboard control for autonomous desktop interaction.
|
|
77
|
+
*
|
|
78
|
+
* Image results are supported.
|
|
79
|
+
*
|
|
80
|
+
* @param displayWidthPx - The width of the display being controlled by the model in pixels.
|
|
81
|
+
* @param displayHeightPx - The height of the display being controlled by the model in pixels.
|
|
82
|
+
* @param displayNumber - The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
|
|
83
|
+
*/
|
|
84
|
+
computer_20241022,
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Claude can interact with computer environments through the computer use tool, which
|
|
88
|
+
* provides screenshot capabilities and mouse/keyboard control for autonomous desktop interaction.
|
|
89
|
+
*
|
|
90
|
+
* Image results are supported.
|
|
91
|
+
*
|
|
92
|
+
* @param displayWidthPx - The width of the display being controlled by the model in pixels.
|
|
93
|
+
* @param displayHeightPx - The height of the display being controlled by the model in pixels.
|
|
94
|
+
* @param displayNumber - The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
|
|
95
|
+
*/
|
|
96
|
+
computer_20250124,
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Claude can interact with computer environments through the computer use tool, which
|
|
100
|
+
* provides screenshot capabilities and mouse/keyboard control for autonomous desktop interaction.
|
|
101
|
+
*
|
|
102
|
+
* This version adds the zoom action for detailed screen region inspection.
|
|
103
|
+
*
|
|
104
|
+
* Image results are supported.
|
|
105
|
+
*
|
|
106
|
+
* Supported models: Claude Opus 4.5
|
|
107
|
+
*
|
|
108
|
+
* @param displayWidthPx - The width of the display being controlled by the model in pixels.
|
|
109
|
+
* @param displayHeightPx - The height of the display being controlled by the model in pixels.
|
|
110
|
+
* @param displayNumber - The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
|
|
111
|
+
* @param enableZoom - Enable zoom action. Set to true to allow Claude to zoom into specific screen regions. Default: false.
|
|
112
|
+
*/
|
|
113
|
+
computer_20251124,
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The memory tool enables Claude to store and retrieve information across conversations through a memory file directory.
|
|
117
|
+
* Claude can create, read, update, and delete files that persist between sessions,
|
|
118
|
+
* allowing it to build knowledge over time without keeping everything in the context window.
|
|
119
|
+
* The memory tool operates client-side—you control where and how the data is stored through your own infrastructure.
|
|
120
|
+
*
|
|
121
|
+
* Supported models: Claude Sonnet 4.5, Claude Sonnet 4, Claude Opus 4.1, Claude Opus 4.
|
|
122
|
+
*/
|
|
123
|
+
memory_20250818,
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Claude can use an Anthropic-defined text editor tool to view and modify text files,
|
|
127
|
+
* helping you debug, fix, and improve your code or other text documents. This allows Claude
|
|
128
|
+
* to directly interact with your files, providing hands-on assistance rather than just suggesting changes.
|
|
129
|
+
*
|
|
130
|
+
* Supported models: Claude Sonnet 3.5
|
|
131
|
+
*/
|
|
132
|
+
textEditor_20241022,
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Claude can use an Anthropic-defined text editor tool to view and modify text files,
|
|
136
|
+
* helping you debug, fix, and improve your code or other text documents. This allows Claude
|
|
137
|
+
* to directly interact with your files, providing hands-on assistance rather than just suggesting changes.
|
|
138
|
+
*
|
|
139
|
+
* Supported models: Claude Sonnet 3.7
|
|
140
|
+
*/
|
|
141
|
+
textEditor_20250124,
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Claude can use an Anthropic-defined text editor tool to view and modify text files,
|
|
145
|
+
* helping you debug, fix, and improve your code or other text documents. This allows Claude
|
|
146
|
+
* to directly interact with your files, providing hands-on assistance rather than just suggesting changes.
|
|
147
|
+
*
|
|
148
|
+
* Note: This version does not support the "undo_edit" command.
|
|
149
|
+
*
|
|
150
|
+
* @deprecated Use textEditor_20250728 instead
|
|
151
|
+
*/
|
|
152
|
+
textEditor_20250429,
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Claude can use an Anthropic-defined text editor tool to view and modify text files,
|
|
156
|
+
* helping you debug, fix, and improve your code or other text documents. This allows Claude
|
|
157
|
+
* to directly interact with your files, providing hands-on assistance rather than just suggesting changes.
|
|
158
|
+
*
|
|
159
|
+
* Note: This version does not support the "undo_edit" command and adds optional max_characters parameter.
|
|
160
|
+
*
|
|
161
|
+
* Supported models: Claude Sonnet 4, Opus 4, and Opus 4.1
|
|
162
|
+
*
|
|
163
|
+
* @param maxCharacters - Optional maximum number of characters to view in the file
|
|
164
|
+
*/
|
|
165
|
+
textEditor_20250728,
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Creates a web fetch tool that gives Claude direct access to real-time web content.
|
|
169
|
+
*
|
|
170
|
+
* @param maxUses - The max_uses parameter limits the number of web fetches performed
|
|
171
|
+
* @param allowedDomains - Only fetch from these domains
|
|
172
|
+
* @param blockedDomains - Never fetch from these domains
|
|
173
|
+
* @param citations - Unlike web search where citations are always enabled, citations are optional for web fetch. Set "citations": {"enabled": true} to enable Claude to cite specific passages from fetched documents.
|
|
174
|
+
* @param maxContentTokens - The max_content_tokens parameter limits the amount of content that will be included in the context.
|
|
175
|
+
*/
|
|
176
|
+
webFetch_20250910,
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Creates a web fetch tool that gives Claude direct access to real-time web content.
|
|
180
|
+
*
|
|
181
|
+
* @param maxUses - The max_uses parameter limits the number of web fetches performed
|
|
182
|
+
* @param allowedDomains - Only fetch from these domains
|
|
183
|
+
* @param blockedDomains - Never fetch from these domains
|
|
184
|
+
* @param citations - Unlike web search where citations are always enabled, citations are optional for web fetch. Set "citations": {"enabled": true} to enable Claude to cite specific passages from fetched documents.
|
|
185
|
+
* @param maxContentTokens - The max_content_tokens parameter limits the amount of content that will be included in the context.
|
|
186
|
+
*/
|
|
187
|
+
webFetch_20260209,
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Creates a web search tool that gives Claude direct access to real-time web content.
|
|
191
|
+
*
|
|
192
|
+
* @param maxUses - Maximum number of web searches Claude can perform during the conversation.
|
|
193
|
+
* @param allowedDomains - Optional list of domains that Claude is allowed to search.
|
|
194
|
+
* @param blockedDomains - Optional list of domains that Claude should avoid when searching.
|
|
195
|
+
* @param userLocation - Optional user location information to provide geographically relevant search results.
|
|
196
|
+
*/
|
|
197
|
+
webSearch_20250305,
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Creates a web search tool that gives Claude direct access to real-time web content.
|
|
201
|
+
*
|
|
202
|
+
* @param maxUses - Maximum number of web searches Claude can perform during the conversation.
|
|
203
|
+
* @param allowedDomains - Optional list of domains that Claude is allowed to search.
|
|
204
|
+
* @param blockedDomains - Optional list of domains that Claude should avoid when searching.
|
|
205
|
+
* @param userLocation - Optional user location information to provide geographically relevant search results.
|
|
206
|
+
*/
|
|
207
|
+
webSearch_20260209,
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Creates a tool search tool that uses regex patterns to find tools.
|
|
211
|
+
*
|
|
212
|
+
* The tool search tool enables Claude to work with hundreds or thousands of tools
|
|
213
|
+
* by dynamically discovering and loading them on-demand. Instead of loading all
|
|
214
|
+
* tool definitions into the context window upfront, Claude searches your tool
|
|
215
|
+
* catalog and loads only the tools it needs.
|
|
216
|
+
*
|
|
217
|
+
* Use `providerOptions: { anthropic: { deferLoading: true } }` on other tools
|
|
218
|
+
* to mark them for deferred loading.
|
|
219
|
+
*
|
|
220
|
+
* Supported models: Claude Opus 4.5, Claude Sonnet 4.5
|
|
221
|
+
*/
|
|
222
|
+
toolSearchRegex_20251119,
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Creates a tool search tool that uses BM25 (natural language) to find tools.
|
|
226
|
+
*
|
|
227
|
+
* The tool search tool enables Claude to work with hundreds or thousands of tools
|
|
228
|
+
* by dynamically discovering and loading them on-demand. Instead of loading all
|
|
229
|
+
* tool definitions into the context window upfront, Claude searches your tool
|
|
230
|
+
* catalog and loads only the tools it needs.
|
|
231
|
+
*
|
|
232
|
+
* Use `providerOptions: { anthropic: { deferLoading: true } }` on other tools
|
|
233
|
+
* to mark them for deferred loading.
|
|
234
|
+
*
|
|
235
|
+
* Supported models: Claude Opus 4.5, Claude Sonnet 4.5
|
|
236
|
+
*/
|
|
237
|
+
toolSearchBm25_20251119,
|
|
238
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { JSONObject, LanguageModelV3Usage } from '@ai-sdk/provider';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents a single iteration in the usage breakdown.
|
|
5
|
+
* When compaction occurs, the API returns an iterations array showing
|
|
6
|
+
* usage for each sampling iteration (compaction + message).
|
|
7
|
+
*/
|
|
8
|
+
export type AnthropicUsageIteration = {
|
|
9
|
+
type: 'compaction' | 'message';
|
|
10
|
+
input_tokens: number;
|
|
11
|
+
output_tokens: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type AnthropicMessagesUsage = {
|
|
15
|
+
input_tokens: number;
|
|
16
|
+
output_tokens: number;
|
|
17
|
+
cache_creation_input_tokens?: number | null;
|
|
18
|
+
cache_read_input_tokens?: number | null;
|
|
19
|
+
/**
|
|
20
|
+
* When compaction is triggered, this array contains usage for each
|
|
21
|
+
* sampling iteration. The top-level input_tokens and output_tokens
|
|
22
|
+
* do NOT include compaction iteration usage - to get total tokens
|
|
23
|
+
* consumed and billed, sum across all entries in this array.
|
|
24
|
+
*/
|
|
25
|
+
iterations?: AnthropicUsageIteration[] | null;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export function convertAnthropicMessagesUsage({
|
|
29
|
+
usage,
|
|
30
|
+
rawUsage,
|
|
31
|
+
}: {
|
|
32
|
+
usage: AnthropicMessagesUsage;
|
|
33
|
+
rawUsage?: JSONObject;
|
|
34
|
+
}): LanguageModelV3Usage {
|
|
35
|
+
const cacheCreationTokens = usage.cache_creation_input_tokens ?? 0;
|
|
36
|
+
const cacheReadTokens = usage.cache_read_input_tokens ?? 0;
|
|
37
|
+
|
|
38
|
+
// When iterations is present (compaction occurred), sum across all iterations
|
|
39
|
+
// to get the true total tokens consumed/billed. The top-level input_tokens
|
|
40
|
+
// and output_tokens exclude compaction iteration usage.
|
|
41
|
+
let inputTokens: number;
|
|
42
|
+
let outputTokens: number;
|
|
43
|
+
|
|
44
|
+
if (usage.iterations && usage.iterations.length > 0) {
|
|
45
|
+
const totals = usage.iterations.reduce(
|
|
46
|
+
(acc, iter) => ({
|
|
47
|
+
input: acc.input + iter.input_tokens,
|
|
48
|
+
output: acc.output + iter.output_tokens,
|
|
49
|
+
}),
|
|
50
|
+
{ input: 0, output: 0 },
|
|
51
|
+
);
|
|
52
|
+
inputTokens = totals.input;
|
|
53
|
+
outputTokens = totals.output;
|
|
54
|
+
} else {
|
|
55
|
+
inputTokens = usage.input_tokens;
|
|
56
|
+
outputTokens = usage.output_tokens;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
inputTokens: {
|
|
61
|
+
total: inputTokens + cacheCreationTokens + cacheReadTokens,
|
|
62
|
+
noCache: inputTokens,
|
|
63
|
+
cacheRead: cacheReadTokens,
|
|
64
|
+
cacheWrite: cacheCreationTokens,
|
|
65
|
+
},
|
|
66
|
+
outputTokens: {
|
|
67
|
+
total: outputTokens,
|
|
68
|
+
text: undefined,
|
|
69
|
+
reasoning: undefined,
|
|
70
|
+
},
|
|
71
|
+
raw: rawUsage ?? usage,
|
|
72
|
+
};
|
|
73
|
+
}
|