@ai-sdk/anthropic-aws 0.0.0 → 1.0.0

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.
@@ -0,0 +1,262 @@
1
+ import {
2
+ NoSuchModelError,
3
+ type LanguageModelV3,
4
+ type ProviderV3,
5
+ } from '@ai-sdk/provider';
6
+ import {
7
+ loadOptionalSetting,
8
+ loadSetting,
9
+ withoutTrailingSlash,
10
+ type FetchFunction,
11
+ } from '@ai-sdk/provider-utils';
12
+ import {
13
+ AnthropicMessagesLanguageModel,
14
+ anthropicTools,
15
+ type AnthropicMessagesModelId,
16
+ } from '@ai-sdk/anthropic/internal';
17
+ import {
18
+ createApiKeyFetchFunction,
19
+ createSigV4FetchFunction,
20
+ type AnthropicAwsCredentials,
21
+ } from './anthropic-aws-fetch';
22
+
23
+ export interface AnthropicAwsProvider extends ProviderV3 {
24
+ /**
25
+ * Creates a model for text generation.
26
+ */
27
+ (modelId: AnthropicMessagesModelId): LanguageModelV3;
28
+
29
+ /**
30
+ * Creates a model for text generation.
31
+ */
32
+ languageModel(modelId: AnthropicMessagesModelId): LanguageModelV3;
33
+
34
+ /**
35
+ * @deprecated Use `embeddingModel` instead.
36
+ */
37
+ textEmbeddingModel(modelId: string): never;
38
+
39
+ tools: typeof anthropicTools;
40
+ }
41
+
42
+ export interface AnthropicAwsProviderSettings {
43
+ /**
44
+ * The AWS region to use for Claude Platform on AWS. Defaults to the value of the
45
+ * `AWS_REGION` environment variable. Required — no fallback default.
46
+ */
47
+ region?: string;
48
+
49
+ /**
50
+ * The Anthropic workspace ID for this AWS account. Sent on every request via the
51
+ * `anthropic-workspace-id` header. Defaults to the value of the
52
+ * `ANTHROPIC_AWS_WORKSPACE_ID` environment variable.
53
+ */
54
+ workspaceId?: string;
55
+
56
+ /**
57
+ * API key for authenticating requests via the `x-api-key` header.
58
+ * When provided, this will be used instead of AWS SigV4 authentication.
59
+ * Defaults to the value of the `ANTHROPIC_AWS_API_KEY` environment variable.
60
+ */
61
+ apiKey?: string;
62
+
63
+ /**
64
+ * The AWS access key ID to use for SigV4 authentication. Defaults to the value of the
65
+ * `AWS_ACCESS_KEY_ID` environment variable.
66
+ */
67
+ accessKeyId?: string;
68
+
69
+ /**
70
+ * The AWS secret access key to use for SigV4 authentication. Defaults to the value of the
71
+ * `AWS_SECRET_ACCESS_KEY` environment variable.
72
+ */
73
+ secretAccessKey?: string;
74
+
75
+ /**
76
+ * The AWS session token to use for SigV4 authentication. Defaults to the value of the
77
+ * `AWS_SESSION_TOKEN` environment variable.
78
+ */
79
+ sessionToken?: string;
80
+
81
+ /**
82
+ * Base URL for the Claude Platform on AWS API calls.
83
+ */
84
+ baseURL?: string;
85
+
86
+ /**
87
+ * Custom headers to include in the requests.
88
+ */
89
+ headers?: Record<string, string | undefined>;
90
+
91
+ /**
92
+ * Custom fetch implementation. You can use it as a middleware to intercept requests,
93
+ * or to provide a custom fetch implementation for e.g. testing.
94
+ */
95
+ fetch?: FetchFunction;
96
+
97
+ /**
98
+ * The AWS credential provider to use to get dynamic credentials similar to the
99
+ * AWS SDK. Setting a provider here will cause its credential values to be used
100
+ * instead of the `accessKeyId`, `secretAccessKey`, and `sessionToken` settings.
101
+ */
102
+ credentialProvider?: () => PromiseLike<
103
+ Omit<AnthropicAwsCredentials, 'region'>
104
+ >;
105
+
106
+ generateId?: () => string;
107
+ }
108
+
109
+ /**
110
+ * Create a Claude Platform on AWS provider instance.
111
+ * This provider uses the Anthropic Messages API hosted in AWS, authenticated
112
+ * with AWS SigV4 or an AWS-provisioned API key.
113
+ */
114
+ export function createAnthropicAws(
115
+ options: AnthropicAwsProviderSettings = {},
116
+ ): AnthropicAwsProvider {
117
+ const apiKey = loadOptionalSetting({
118
+ settingValue: options.apiKey,
119
+ environmentVariableName: 'ANTHROPIC_AWS_API_KEY',
120
+ });
121
+
122
+ const fetchFunction = apiKey
123
+ ? createApiKeyFetchFunction(apiKey, options.fetch)
124
+ : createSigV4FetchFunction(async () => {
125
+ const region = loadSetting({
126
+ settingValue: options.region,
127
+ settingName: 'region',
128
+ environmentVariableName: 'AWS_REGION',
129
+ description: 'AWS region',
130
+ });
131
+
132
+ if (options.credentialProvider) {
133
+ try {
134
+ return {
135
+ ...(await options.credentialProvider()),
136
+ region,
137
+ };
138
+ } catch (error) {
139
+ const errorMessage =
140
+ error instanceof Error ? error.message : String(error);
141
+ throw new Error(
142
+ `AWS credential provider failed: ${errorMessage}. ` +
143
+ 'Please ensure your credential provider returns valid AWS credentials ' +
144
+ 'with accessKeyId and secretAccessKey properties.',
145
+ );
146
+ }
147
+ }
148
+
149
+ try {
150
+ return {
151
+ region,
152
+ accessKeyId: loadSetting({
153
+ settingValue: options.accessKeyId,
154
+ settingName: 'accessKeyId',
155
+ environmentVariableName: 'AWS_ACCESS_KEY_ID',
156
+ description: 'AWS access key ID',
157
+ }),
158
+ secretAccessKey: loadSetting({
159
+ settingValue: options.secretAccessKey,
160
+ settingName: 'secretAccessKey',
161
+ environmentVariableName: 'AWS_SECRET_ACCESS_KEY',
162
+ description: 'AWS secret access key',
163
+ }),
164
+ sessionToken: loadOptionalSetting({
165
+ settingValue: options.sessionToken,
166
+ environmentVariableName: 'AWS_SESSION_TOKEN',
167
+ }),
168
+ };
169
+ } catch (error) {
170
+ const errorMessage =
171
+ error instanceof Error ? error.message : String(error);
172
+ if (
173
+ errorMessage.includes('AWS_ACCESS_KEY_ID') ||
174
+ errorMessage.includes('accessKeyId')
175
+ ) {
176
+ throw new Error(
177
+ 'AWS SigV4 authentication requires AWS credentials. Please provide either:\n' +
178
+ '1. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables\n' +
179
+ '2. Provide accessKeyId and secretAccessKey in options\n' +
180
+ '3. Use a credentialProvider function\n' +
181
+ '4. Use API key authentication with ANTHROPIC_AWS_API_KEY or apiKey option\n' +
182
+ `Original error: ${errorMessage}`,
183
+ );
184
+ }
185
+ if (
186
+ errorMessage.includes('AWS_SECRET_ACCESS_KEY') ||
187
+ errorMessage.includes('secretAccessKey')
188
+ ) {
189
+ throw new Error(
190
+ 'AWS SigV4 authentication requires both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. ' +
191
+ 'Please ensure both credentials are provided.\n' +
192
+ `Original error: ${errorMessage}`,
193
+ );
194
+ }
195
+ throw error;
196
+ }
197
+ }, options.fetch);
198
+
199
+ const getBaseURL = (): string =>
200
+ withoutTrailingSlash(options.baseURL) ??
201
+ `https://aws-external-anthropic.${loadSetting({
202
+ settingValue: options.region,
203
+ settingName: 'region',
204
+ environmentVariableName: 'AWS_REGION',
205
+ description: 'AWS region',
206
+ })}.api.aws/v1`;
207
+
208
+ const getHeaders = (): Record<string, string | undefined> => ({
209
+ 'anthropic-version': '2023-06-01',
210
+ 'anthropic-workspace-id': loadSetting({
211
+ settingValue: options.workspaceId,
212
+ settingName: 'workspaceId',
213
+ environmentVariableName: 'ANTHROPIC_AWS_WORKSPACE_ID',
214
+ description: 'Anthropic AWS workspace ID',
215
+ }),
216
+ ...options.headers,
217
+ });
218
+
219
+ const createChatModel = (modelId: AnthropicMessagesModelId) =>
220
+ new AnthropicMessagesLanguageModel(modelId, {
221
+ provider: 'anthropic-aws.messages',
222
+ baseURL: getBaseURL(),
223
+ headers: getHeaders,
224
+ fetch: fetchFunction,
225
+ generateId: options.generateId,
226
+ supportedUrls: () => ({
227
+ 'image/*': [/^https?:\/\/.*$/],
228
+ 'application/pdf': [/^https?:\/\/.*$/],
229
+ }),
230
+ });
231
+
232
+ const provider = function (modelId: AnthropicMessagesModelId) {
233
+ if (new.target) {
234
+ throw new Error(
235
+ 'The Anthropic AWS model function cannot be called with the new keyword.',
236
+ );
237
+ }
238
+ return createChatModel(modelId);
239
+ };
240
+
241
+ provider.specificationVersion = 'v3' as const;
242
+ provider.languageModel = createChatModel;
243
+ provider.chat = createChatModel;
244
+ provider.messages = createChatModel;
245
+
246
+ provider.embeddingModel = (modelId: string) => {
247
+ throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });
248
+ };
249
+ provider.textEmbeddingModel = provider.embeddingModel;
250
+ provider.imageModel = (modelId: string) => {
251
+ throw new NoSuchModelError({ modelId, modelType: 'imageModel' });
252
+ };
253
+
254
+ provider.tools = anthropicTools;
255
+
256
+ return provider;
257
+ }
258
+
259
+ /**
260
+ * Default Claude Platform on AWS provider instance.
261
+ */
262
+ export const anthropicAws = createAnthropicAws();
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ export { anthropicAws, createAnthropicAws } from './anthropic-aws-provider';
2
+ export type {
3
+ AnthropicAwsProvider,
4
+ AnthropicAwsProviderSettings,
5
+ } from './anthropic-aws-provider';
6
+ export type { AnthropicAwsCredentials } from './anthropic-aws-fetch';
7
+ export { VERSION } from './version';
package/src/version.ts ADDED
@@ -0,0 +1,6 @@
1
+ // Version string of this package injected at build time.
2
+ declare const __PACKAGE_VERSION__: string | undefined;
3
+ export const VERSION: string =
4
+ typeof __PACKAGE_VERSION__ !== 'undefined'
5
+ ? __PACKAGE_VERSION__
6
+ : '0.0.0-test';