@ai-sdk/amazon-bedrock 5.0.0-beta.44 → 5.0.0-beta.46
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 +29 -0
- package/dist/anthropic/index.d.ts +10 -10
- package/dist/anthropic/index.js +58 -46
- package/dist/anthropic/index.js.map +1 -1
- package/dist/index.d.ts +25 -23
- package/dist/index.js +496 -436
- package/dist/index.js.map +1 -1
- package/docs/08-amazon-bedrock.mdx +80 -82
- package/package.json +5 -5
- package/src/amazon-bedrock-api-types.ts +224 -0
- package/src/{bedrock-chat-options.ts → amazon-bedrock-chat-language-model-options.ts} +7 -7
- package/src/{bedrock-chat-language-model.ts → amazon-bedrock-chat-language-model.ts} +217 -196
- package/src/{bedrock-embedding-options.ts → amazon-bedrock-embedding-model-options.ts} +1 -1
- package/src/{bedrock-embedding-model.ts → amazon-bedrock-embedding-model.ts} +34 -27
- package/src/{bedrock-error.ts → amazon-bedrock-error.ts} +1 -1
- package/src/{bedrock-event-stream-decoder.ts → amazon-bedrock-event-stream-decoder.ts} +1 -1
- package/src/{bedrock-event-stream-response-handler.ts → amazon-bedrock-event-stream-response-handler.ts} +6 -6
- package/src/{bedrock-image-model.ts → amazon-bedrock-image-model.ts} +43 -39
- package/src/amazon-bedrock-image-settings.ts +9 -0
- package/src/{bedrock-prepare-tools.ts → amazon-bedrock-prepare-tools.ts} +22 -18
- package/src/{bedrock-provider.ts → amazon-bedrock-provider.ts} +53 -46
- package/src/amazon-bedrock-reasoning-metadata.ts +10 -0
- package/src/{bedrock-sigv4-fetch.ts → amazon-bedrock-sigv4-fetch.ts} +5 -3
- package/src/anthropic/amazon-bedrock-anthropic-fetch.ts +104 -0
- package/src/anthropic/{bedrock-anthropic-options.ts → amazon-bedrock-anthropic-options.ts} +1 -1
- package/src/anthropic/{bedrock-anthropic-provider.ts → amazon-bedrock-anthropic-provider.ts} +22 -20
- package/src/anthropic/index.ts +19 -7
- package/src/{convert-bedrock-usage.ts → convert-amazon-bedrock-usage.ts} +4 -4
- package/src/{convert-to-bedrock-chat-messages.ts → convert-to-amazon-bedrock-chat-messages.ts} +146 -103
- package/src/index.ts +15 -8
- package/src/inject-fetch-headers.ts +1 -1
- package/src/{map-bedrock-finish-reason.ts → map-amazon-bedrock-finish-reason.ts} +4 -4
- package/src/reranking/{bedrock-reranking-api.ts → amazon-bedrock-reranking-api.ts} +3 -3
- package/src/reranking/{bedrock-reranking-options.ts → amazon-bedrock-reranking-model-options.ts} +1 -1
- package/src/reranking/{bedrock-reranking-model.ts → amazon-bedrock-reranking-model.ts} +32 -25
- package/src/anthropic/bedrock-anthropic-fetch.ts +0 -94
- package/src/bedrock-api-types.ts +0 -219
- package/src/bedrock-image-settings.ts +0 -6
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { anthropicTools } from '@ai-sdk/anthropic/internal';
|
|
2
|
-
import {
|
|
2
|
+
import type {
|
|
3
3
|
EmbeddingModelV4,
|
|
4
4
|
ImageModelV4,
|
|
5
5
|
LanguageModelV4,
|
|
@@ -7,26 +7,26 @@ import {
|
|
|
7
7
|
RerankingModelV4,
|
|
8
8
|
} from '@ai-sdk/provider';
|
|
9
9
|
import {
|
|
10
|
-
FetchFunction,
|
|
11
10
|
generateId,
|
|
12
11
|
loadOptionalSetting,
|
|
13
12
|
loadSetting,
|
|
14
13
|
withoutTrailingSlash,
|
|
15
14
|
withUserAgentSuffix,
|
|
15
|
+
type FetchFunction,
|
|
16
16
|
} from '@ai-sdk/provider-utils';
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
17
|
+
import { AmazonBedrockChatLanguageModel } from './amazon-bedrock-chat-language-model';
|
|
18
|
+
import type { AmazonBedrockChatModelId } from './amazon-bedrock-chat-language-model-options';
|
|
19
|
+
import { AmazonBedrockEmbeddingModel } from './amazon-bedrock-embedding-model';
|
|
20
|
+
import type { AmazonBedrockEmbeddingModelId } from './amazon-bedrock-embedding-model-options';
|
|
21
|
+
import { AmazonBedrockImageModel } from './amazon-bedrock-image-model';
|
|
22
|
+
import type { AmazonBedrockImageModelId } from './amazon-bedrock-image-settings';
|
|
23
23
|
import {
|
|
24
|
-
BedrockCredentials,
|
|
25
24
|
createApiKeyFetchFunction,
|
|
26
25
|
createSigV4FetchFunction,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
import {
|
|
26
|
+
type AmazonBedrockCredentials,
|
|
27
|
+
} from './amazon-bedrock-sigv4-fetch';
|
|
28
|
+
import { AmazonBedrockRerankingModel } from './reranking/amazon-bedrock-reranking-model';
|
|
29
|
+
import type { AmazonBedrockRerankingModelId } from './reranking/amazon-bedrock-reranking-model-options';
|
|
30
30
|
import { VERSION } from './version';
|
|
31
31
|
|
|
32
32
|
export interface AmazonBedrockProviderSettings {
|
|
@@ -74,8 +74,10 @@ export interface AmazonBedrockProviderSettings {
|
|
|
74
74
|
secretAccessKey?: string;
|
|
75
75
|
|
|
76
76
|
/**
|
|
77
|
-
* The AWS session token to use for the Bedrock provider.
|
|
78
|
-
* `
|
|
77
|
+
* The AWS session token to use for the Bedrock provider. When `accessKeyId` and
|
|
78
|
+
* `secretAccessKey` are both passed explicitly as options, only this field is used
|
|
79
|
+
* If either access key field is omitted and resolved from the environment, the
|
|
80
|
+
* session token also falls back to `AWS_SESSION_TOKEN` when not set here.
|
|
79
81
|
*/
|
|
80
82
|
sessionToken?: string;
|
|
81
83
|
|
|
@@ -101,56 +103,58 @@ export interface AmazonBedrockProviderSettings {
|
|
|
101
103
|
* credential values to be used instead of the `accessKeyId`, `secretAccessKey`,
|
|
102
104
|
* and `sessionToken` settings.
|
|
103
105
|
*/
|
|
104
|
-
credentialProvider?: () => PromiseLike<
|
|
106
|
+
credentialProvider?: () => PromiseLike<
|
|
107
|
+
Omit<AmazonBedrockCredentials, 'region'>
|
|
108
|
+
>;
|
|
105
109
|
|
|
106
110
|
// for testing
|
|
107
111
|
generateId?: () => string;
|
|
108
112
|
}
|
|
109
113
|
|
|
110
114
|
export interface AmazonBedrockProvider extends ProviderV4 {
|
|
111
|
-
(modelId:
|
|
115
|
+
(modelId: AmazonBedrockChatModelId): LanguageModelV4;
|
|
112
116
|
|
|
113
|
-
languageModel(modelId:
|
|
117
|
+
languageModel(modelId: AmazonBedrockChatModelId): LanguageModelV4;
|
|
114
118
|
|
|
115
119
|
/**
|
|
116
120
|
* Creates a model for text embeddings.
|
|
117
121
|
*/
|
|
118
|
-
embedding(modelId:
|
|
122
|
+
embedding(modelId: AmazonBedrockEmbeddingModelId): EmbeddingModelV4;
|
|
119
123
|
|
|
120
124
|
/**
|
|
121
125
|
* Creates a model for text embeddings.
|
|
122
126
|
*/
|
|
123
|
-
embeddingModel(modelId:
|
|
127
|
+
embeddingModel(modelId: AmazonBedrockEmbeddingModelId): EmbeddingModelV4;
|
|
124
128
|
|
|
125
129
|
/**
|
|
126
130
|
* @deprecated Use `embedding` instead.
|
|
127
131
|
*/
|
|
128
|
-
textEmbedding(modelId:
|
|
132
|
+
textEmbedding(modelId: AmazonBedrockEmbeddingModelId): EmbeddingModelV4;
|
|
129
133
|
|
|
130
134
|
/**
|
|
131
135
|
* @deprecated Use `embeddingModel` instead.
|
|
132
136
|
*/
|
|
133
|
-
textEmbeddingModel(modelId:
|
|
137
|
+
textEmbeddingModel(modelId: AmazonBedrockEmbeddingModelId): EmbeddingModelV4;
|
|
134
138
|
|
|
135
139
|
/**
|
|
136
140
|
* Creates a model for image generation.
|
|
137
141
|
*/
|
|
138
|
-
image(modelId:
|
|
142
|
+
image(modelId: AmazonBedrockImageModelId): ImageModelV4;
|
|
139
143
|
|
|
140
144
|
/**
|
|
141
145
|
* Creates a model for image generation.
|
|
142
146
|
*/
|
|
143
|
-
imageModel(modelId:
|
|
147
|
+
imageModel(modelId: AmazonBedrockImageModelId): ImageModelV4;
|
|
144
148
|
|
|
145
149
|
/**
|
|
146
150
|
* Creates a model for reranking documents.
|
|
147
151
|
*/
|
|
148
|
-
reranking(modelId:
|
|
152
|
+
reranking(modelId: AmazonBedrockRerankingModelId): RerankingModelV4;
|
|
149
153
|
|
|
150
154
|
/**
|
|
151
155
|
* Creates a model for reranking documents.
|
|
152
156
|
*/
|
|
153
|
-
rerankingModel(modelId:
|
|
157
|
+
rerankingModel(modelId: AmazonBedrockRerankingModelId): RerankingModelV4;
|
|
154
158
|
|
|
155
159
|
/**
|
|
156
160
|
* Anthropic-specific tools that can be used with Anthropic models on Bedrock.
|
|
@@ -221,10 +225,13 @@ export function createAmazonBedrock(
|
|
|
221
225
|
environmentVariableName: 'AWS_SECRET_ACCESS_KEY',
|
|
222
226
|
description: 'AWS secret access key',
|
|
223
227
|
}),
|
|
224
|
-
sessionToken:
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
+
sessionToken:
|
|
229
|
+
options.accessKeyId != null && options.secretAccessKey != null
|
|
230
|
+
? options.sessionToken
|
|
231
|
+
: loadOptionalSetting({
|
|
232
|
+
settingValue: options.sessionToken,
|
|
233
|
+
environmentVariableName: 'AWS_SESSION_TOKEN',
|
|
234
|
+
}),
|
|
228
235
|
};
|
|
229
236
|
} catch (error) {
|
|
230
237
|
// Provide helpful error message for missing AWS credentials
|
|
@@ -263,7 +270,7 @@ export function createAmazonBedrock(
|
|
|
263
270
|
return withUserAgentSuffix(baseHeaders, `ai-sdk/amazon-bedrock/${VERSION}`);
|
|
264
271
|
};
|
|
265
272
|
|
|
266
|
-
const
|
|
273
|
+
const getAmazonBedrockRuntimeBaseUrl = (): string =>
|
|
267
274
|
withoutTrailingSlash(
|
|
268
275
|
options.baseURL ??
|
|
269
276
|
`https://bedrock-runtime.${loadSetting({
|
|
@@ -274,7 +281,7 @@ export function createAmazonBedrock(
|
|
|
274
281
|
})}.amazonaws.com`,
|
|
275
282
|
) ?? `https://bedrock-runtime.us-east-1.amazonaws.com`;
|
|
276
283
|
|
|
277
|
-
const
|
|
284
|
+
const getAmazonBedrockAgentRuntimeBaseUrl = (): string =>
|
|
278
285
|
withoutTrailingSlash(
|
|
279
286
|
options.baseURL ??
|
|
280
287
|
`https://bedrock-agent-runtime.${loadSetting({
|
|
@@ -285,15 +292,15 @@ export function createAmazonBedrock(
|
|
|
285
292
|
})}.amazonaws.com`,
|
|
286
293
|
) ?? `https://bedrock-agent-runtime.us-west-2.amazonaws.com`;
|
|
287
294
|
|
|
288
|
-
const createChatModel = (modelId:
|
|
289
|
-
new
|
|
290
|
-
baseUrl:
|
|
295
|
+
const createChatModel = (modelId: AmazonBedrockChatModelId) =>
|
|
296
|
+
new AmazonBedrockChatLanguageModel(modelId, {
|
|
297
|
+
baseUrl: getAmazonBedrockRuntimeBaseUrl,
|
|
291
298
|
headers: getHeaders,
|
|
292
299
|
fetch: fetchFunction,
|
|
293
300
|
generateId,
|
|
294
301
|
});
|
|
295
302
|
|
|
296
|
-
const provider = function (modelId:
|
|
303
|
+
const provider = function (modelId: AmazonBedrockChatModelId) {
|
|
297
304
|
if (new.target) {
|
|
298
305
|
throw new Error(
|
|
299
306
|
'The Amazon Bedrock model function cannot be called with the new keyword.',
|
|
@@ -303,23 +310,23 @@ export function createAmazonBedrock(
|
|
|
303
310
|
return createChatModel(modelId);
|
|
304
311
|
};
|
|
305
312
|
|
|
306
|
-
const createEmbeddingModel = (modelId:
|
|
307
|
-
new
|
|
308
|
-
baseUrl:
|
|
313
|
+
const createEmbeddingModel = (modelId: AmazonBedrockEmbeddingModelId) =>
|
|
314
|
+
new AmazonBedrockEmbeddingModel(modelId, {
|
|
315
|
+
baseUrl: getAmazonBedrockRuntimeBaseUrl,
|
|
309
316
|
headers: getHeaders,
|
|
310
317
|
fetch: fetchFunction,
|
|
311
318
|
});
|
|
312
319
|
|
|
313
|
-
const createImageModel = (modelId:
|
|
314
|
-
new
|
|
315
|
-
baseUrl:
|
|
320
|
+
const createImageModel = (modelId: AmazonBedrockImageModelId) =>
|
|
321
|
+
new AmazonBedrockImageModel(modelId, {
|
|
322
|
+
baseUrl: getAmazonBedrockRuntimeBaseUrl,
|
|
316
323
|
headers: getHeaders,
|
|
317
324
|
fetch: fetchFunction,
|
|
318
325
|
});
|
|
319
326
|
|
|
320
|
-
const createRerankingModel = (modelId:
|
|
321
|
-
new
|
|
322
|
-
baseUrl:
|
|
327
|
+
const createRerankingModel = (modelId: AmazonBedrockRerankingModelId) =>
|
|
328
|
+
new AmazonBedrockRerankingModel(modelId, {
|
|
329
|
+
baseUrl: getAmazonBedrockAgentRuntimeBaseUrl,
|
|
323
330
|
region: loadSetting({
|
|
324
331
|
settingValue: options.region,
|
|
325
332
|
settingName: 'region',
|
|
@@ -346,6 +353,6 @@ export function createAmazonBedrock(
|
|
|
346
353
|
}
|
|
347
354
|
|
|
348
355
|
/**
|
|
349
|
-
* Default Bedrock provider instance.
|
|
356
|
+
* Default Amazon Bedrock provider instance.
|
|
350
357
|
*/
|
|
351
|
-
export const
|
|
358
|
+
export const amazonBedrock = createAmazonBedrock();
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
|
|
3
|
+
export const amazonBedrockReasoningMetadataSchema = z.object({
|
|
4
|
+
signature: z.string().optional(),
|
|
5
|
+
redactedData: z.string().optional(),
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
export type AmazonBedrockReasoningMetadata = z.infer<
|
|
9
|
+
typeof amazonBedrockReasoningMetadataSchema
|
|
10
|
+
>;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
|
-
FetchFunction,
|
|
3
2
|
combineHeaders,
|
|
4
3
|
normalizeHeaders,
|
|
5
4
|
withUserAgentSuffix,
|
|
6
5
|
getRuntimeEnvironmentUserAgent,
|
|
6
|
+
type FetchFunction,
|
|
7
7
|
} from '@ai-sdk/provider-utils';
|
|
8
8
|
import { AwsV4Signer } from 'aws4fetch';
|
|
9
9
|
import { VERSION } from './version';
|
|
10
10
|
|
|
11
|
-
export interface
|
|
11
|
+
export interface AmazonBedrockCredentials {
|
|
12
12
|
region: string;
|
|
13
13
|
accessKeyId: string;
|
|
14
14
|
secretAccessKey: string;
|
|
@@ -23,7 +23,9 @@ export interface BedrockCredentials {
|
|
|
23
23
|
* @returns A FetchFunction that signs requests before passing them to the underlying fetch.
|
|
24
24
|
*/
|
|
25
25
|
export function createSigV4FetchFunction(
|
|
26
|
-
getCredentials: () =>
|
|
26
|
+
getCredentials: () =>
|
|
27
|
+
| AmazonBedrockCredentials
|
|
28
|
+
| PromiseLike<AmazonBedrockCredentials>,
|
|
27
29
|
fetch: FetchFunction = globalThis.fetch,
|
|
28
30
|
): FetchFunction {
|
|
29
31
|
return async (
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
convertBase64ToUint8Array,
|
|
3
|
+
safeParseJSON,
|
|
4
|
+
type FetchFunction,
|
|
5
|
+
} from '@ai-sdk/provider-utils';
|
|
6
|
+
import { z } from 'zod/v4';
|
|
7
|
+
import { createAmazonBedrockEventStreamDecoder } from '../amazon-bedrock-event-stream-decoder';
|
|
8
|
+
|
|
9
|
+
const amazonBedrockErrorSchema = z.looseObject({
|
|
10
|
+
message: z.string().optional(),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export function createAmazonBedrockAnthropicFetch(
|
|
14
|
+
baseFetch: FetchFunction,
|
|
15
|
+
): FetchFunction {
|
|
16
|
+
return async (url, options) => {
|
|
17
|
+
const response = await baseFetch(url, options);
|
|
18
|
+
|
|
19
|
+
// Transform Bedrock error responses into Anthropic error format
|
|
20
|
+
// so that anthropicFailedResponseHandler can extract the message.
|
|
21
|
+
if (!response.ok) {
|
|
22
|
+
const text = await response.text();
|
|
23
|
+
const parsed = await safeParseJSON({
|
|
24
|
+
text,
|
|
25
|
+
schema: amazonBedrockErrorSchema,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const message =
|
|
29
|
+
parsed.success && parsed.value.message ? parsed.value.message : text;
|
|
30
|
+
|
|
31
|
+
const anthropicError = JSON.stringify({
|
|
32
|
+
type: 'error',
|
|
33
|
+
error: { type: 'error', message },
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return new Response(anthropicError, {
|
|
37
|
+
status: response.status,
|
|
38
|
+
statusText: response.statusText,
|
|
39
|
+
headers: response.headers,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const contentType = response.headers.get('content-type');
|
|
44
|
+
if (
|
|
45
|
+
contentType?.includes('application/vnd.amazon.eventstream') &&
|
|
46
|
+
response.body != null
|
|
47
|
+
) {
|
|
48
|
+
const transformedBody = transformAmazonBedrockEventStreamToSSE(
|
|
49
|
+
response.body,
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
return new Response(transformedBody, {
|
|
53
|
+
status: response.status,
|
|
54
|
+
statusText: response.statusText,
|
|
55
|
+
headers: new Headers({
|
|
56
|
+
...Object.fromEntries(response.headers.entries()),
|
|
57
|
+
'content-type': 'text/event-stream',
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return response;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function transformAmazonBedrockEventStreamToSSE(
|
|
67
|
+
body: ReadableStream<Uint8Array>,
|
|
68
|
+
): ReadableStream<Uint8Array> {
|
|
69
|
+
const textEncoder = new TextEncoder();
|
|
70
|
+
|
|
71
|
+
return createAmazonBedrockEventStreamDecoder(
|
|
72
|
+
body,
|
|
73
|
+
async (event, controller) => {
|
|
74
|
+
if (event.messageType === 'event') {
|
|
75
|
+
if (event.eventType === 'chunk') {
|
|
76
|
+
const parsed = await safeParseJSON({ text: event.data });
|
|
77
|
+
if (!parsed.success) {
|
|
78
|
+
controller.enqueue(textEncoder.encode(`data: ${event.data}\n\n`));
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const bytes = (parsed.value as { bytes?: string }).bytes;
|
|
82
|
+
if (bytes) {
|
|
83
|
+
const anthropicEvent = new TextDecoder().decode(
|
|
84
|
+
convertBase64ToUint8Array(bytes),
|
|
85
|
+
);
|
|
86
|
+
controller.enqueue(
|
|
87
|
+
textEncoder.encode(`data: ${anthropicEvent}\n\n`),
|
|
88
|
+
);
|
|
89
|
+
} else {
|
|
90
|
+
controller.enqueue(textEncoder.encode(`data: ${event.data}\n\n`));
|
|
91
|
+
}
|
|
92
|
+
} else if (event.eventType === 'messageStop') {
|
|
93
|
+
controller.enqueue(textEncoder.encode('data: [DONE]\n\n'));
|
|
94
|
+
}
|
|
95
|
+
} else if (event.messageType === 'exception') {
|
|
96
|
+
controller.enqueue(
|
|
97
|
+
textEncoder.encode(
|
|
98
|
+
`data: ${JSON.stringify({ type: 'error', error: event.data })}\n\n`,
|
|
99
|
+
),
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
);
|
|
104
|
+
}
|
package/src/anthropic/{bedrock-anthropic-provider.ts → amazon-bedrock-anthropic-provider.ts}
RENAMED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
import {
|
|
2
|
-
LanguageModelV4,
|
|
3
2
|
NoSuchModelError,
|
|
4
|
-
|
|
3
|
+
type LanguageModelV4,
|
|
4
|
+
type ProviderV4,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
6
|
import {
|
|
7
|
-
FetchFunction,
|
|
8
7
|
loadOptionalSetting,
|
|
9
8
|
loadSetting,
|
|
10
|
-
Resolvable,
|
|
11
9
|
resolve,
|
|
12
10
|
withoutTrailingSlash,
|
|
13
11
|
withUserAgentSuffix,
|
|
12
|
+
type FetchFunction,
|
|
13
|
+
type Resolvable,
|
|
14
14
|
} from '@ai-sdk/provider-utils';
|
|
15
15
|
import {
|
|
16
16
|
anthropicTools,
|
|
17
17
|
AnthropicLanguageModel,
|
|
18
18
|
} from '@ai-sdk/anthropic/internal';
|
|
19
19
|
import {
|
|
20
|
-
BedrockCredentials,
|
|
21
20
|
createApiKeyFetchFunction,
|
|
22
21
|
createSigV4FetchFunction,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
import {
|
|
22
|
+
type AmazonBedrockCredentials,
|
|
23
|
+
} from '../amazon-bedrock-sigv4-fetch';
|
|
24
|
+
import { createAmazonBedrockAnthropicFetch } from './amazon-bedrock-anthropic-fetch';
|
|
25
|
+
import type { AmazonBedrockAnthropicModelId } from './amazon-bedrock-anthropic-options';
|
|
26
26
|
import { VERSION } from '../version';
|
|
27
27
|
|
|
28
28
|
// Bedrock requires newer tool versions than the default Anthropic SDK versions
|
|
@@ -53,16 +53,16 @@ const BEDROCK_TOOL_BETA_MAP: Record<string, string> = {
|
|
|
53
53
|
tool_search_tool_bm25_20251119: 'tool-search-tool-2025-10-19',
|
|
54
54
|
};
|
|
55
55
|
|
|
56
|
-
export interface
|
|
56
|
+
export interface AmazonBedrockAnthropicProvider extends ProviderV4 {
|
|
57
57
|
/**
|
|
58
58
|
* Creates a model for text generation.
|
|
59
59
|
*/
|
|
60
|
-
(modelId:
|
|
60
|
+
(modelId: AmazonBedrockAnthropicModelId): LanguageModelV4;
|
|
61
61
|
|
|
62
62
|
/**
|
|
63
63
|
* Creates a model for text generation.
|
|
64
64
|
*/
|
|
65
|
-
languageModel(modelId:
|
|
65
|
+
languageModel(modelId: AmazonBedrockAnthropicModelId): LanguageModelV4;
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
68
|
* Anthropic-specific computer use tool.
|
|
@@ -75,7 +75,7 @@ export interface BedrockAnthropicProvider extends ProviderV4 {
|
|
|
75
75
|
textEmbeddingModel(modelId: string): never;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
export interface
|
|
78
|
+
export interface AmazonBedrockAnthropicProviderSettings {
|
|
79
79
|
/**
|
|
80
80
|
* The AWS region to use for the Bedrock provider. Defaults to the value of the
|
|
81
81
|
* `AWS_REGION` environment variable.
|
|
@@ -129,7 +129,9 @@ export interface BedrockAnthropicProviderSettings {
|
|
|
129
129
|
* credential values to be used instead of the `accessKeyId`, `secretAccessKey`,
|
|
130
130
|
* and `sessionToken` settings.
|
|
131
131
|
*/
|
|
132
|
-
credentialProvider?: () => PromiseLike<
|
|
132
|
+
credentialProvider?: () => PromiseLike<
|
|
133
|
+
Omit<AmazonBedrockCredentials, 'region'>
|
|
134
|
+
>;
|
|
133
135
|
}
|
|
134
136
|
|
|
135
137
|
/**
|
|
@@ -137,9 +139,9 @@ export interface BedrockAnthropicProviderSettings {
|
|
|
137
139
|
* This provider uses the native Anthropic API through Bedrock's InvokeModel endpoint,
|
|
138
140
|
* bypassing the Converse API for better feature compatibility.
|
|
139
141
|
*/
|
|
140
|
-
export function
|
|
141
|
-
options:
|
|
142
|
-
):
|
|
142
|
+
export function createAmazonBedrockAnthropic(
|
|
143
|
+
options: AmazonBedrockAnthropicProviderSettings = {},
|
|
144
|
+
): AmazonBedrockAnthropicProvider {
|
|
143
145
|
// Check for API key authentication first
|
|
144
146
|
const rawApiKey = loadOptionalSetting({
|
|
145
147
|
settingValue: options.apiKey,
|
|
@@ -230,7 +232,7 @@ export function createBedrockAnthropic(
|
|
|
230
232
|
}, options.fetch);
|
|
231
233
|
|
|
232
234
|
// Wrap with Bedrock event stream to SSE transformer for streaming support
|
|
233
|
-
const fetchFunction =
|
|
235
|
+
const fetchFunction = createAmazonBedrockAnthropicFetch(baseFetchFunction);
|
|
234
236
|
|
|
235
237
|
const getBaseURL = (): string =>
|
|
236
238
|
withoutTrailingSlash(
|
|
@@ -248,7 +250,7 @@ export function createBedrockAnthropic(
|
|
|
248
250
|
return withUserAgentSuffix(baseHeaders, `ai-sdk/amazon-bedrock/${VERSION}`);
|
|
249
251
|
};
|
|
250
252
|
|
|
251
|
-
const createChatModel = (modelId:
|
|
253
|
+
const createChatModel = (modelId: AmazonBedrockAnthropicModelId) =>
|
|
252
254
|
new AnthropicLanguageModel(modelId, {
|
|
253
255
|
provider: 'bedrock.anthropic.messages',
|
|
254
256
|
baseURL: getBaseURL(),
|
|
@@ -333,7 +335,7 @@ export function createBedrockAnthropic(
|
|
|
333
335
|
supportsNativeStructuredOutput: true,
|
|
334
336
|
});
|
|
335
337
|
|
|
336
|
-
const provider = function (modelId:
|
|
338
|
+
const provider = function (modelId: AmazonBedrockAnthropicModelId) {
|
|
337
339
|
if (new.target) {
|
|
338
340
|
throw new Error(
|
|
339
341
|
'The Bedrock Anthropic model function cannot be called with the new keyword.',
|
|
@@ -364,4 +366,4 @@ export function createBedrockAnthropic(
|
|
|
364
366
|
/**
|
|
365
367
|
* Default Bedrock Anthropic provider instance.
|
|
366
368
|
*/
|
|
367
|
-
export const
|
|
369
|
+
export const amazonBedrockAnthropic = createAmazonBedrockAnthropic();
|
package/src/anthropic/index.ts
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
amazonBedrockAnthropic,
|
|
3
|
+
/** @deprecated Use `amazonBedrockAnthropic` instead. */
|
|
4
|
+
amazonBedrockAnthropic as bedrockAnthropic,
|
|
5
|
+
createAmazonBedrockAnthropic,
|
|
6
|
+
/** @deprecated Use `createAmazonBedrockAnthropic` instead. */
|
|
7
|
+
createAmazonBedrockAnthropic as createBedrockAnthropic,
|
|
8
|
+
} from './amazon-bedrock-anthropic-provider';
|
|
5
9
|
export type {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
AmazonBedrockAnthropicProvider,
|
|
11
|
+
/** @deprecated Use `AmazonBedrockAnthropicProvider` instead. */
|
|
12
|
+
AmazonBedrockAnthropicProvider as BedrockAnthropicProvider,
|
|
13
|
+
AmazonBedrockAnthropicProviderSettings,
|
|
14
|
+
/** @deprecated Use `AmazonBedrockAnthropicProviderSettings` instead. */
|
|
15
|
+
AmazonBedrockAnthropicProviderSettings as BedrockAnthropicProviderSettings,
|
|
16
|
+
} from './amazon-bedrock-anthropic-provider';
|
|
17
|
+
export type {
|
|
18
|
+
AmazonBedrockAnthropicModelId,
|
|
19
|
+
/** @deprecated Use `AmazonBedrockAnthropicModelId` instead. */
|
|
20
|
+
AmazonBedrockAnthropicModelId as BedrockAnthropicModelId,
|
|
21
|
+
} from './amazon-bedrock-anthropic-options';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { LanguageModelV4Usage } from '@ai-sdk/provider';
|
|
1
|
+
import type { LanguageModelV4Usage } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
|
-
export type
|
|
3
|
+
export type AmazonBedrockUsage = {
|
|
4
4
|
inputTokens: number;
|
|
5
5
|
outputTokens: number;
|
|
6
6
|
totalTokens?: number;
|
|
@@ -8,8 +8,8 @@ export type BedrockUsage = {
|
|
|
8
8
|
cacheWriteInputTokens?: number | null;
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
-
export function
|
|
12
|
-
usage:
|
|
11
|
+
export function convertAmazonBedrockUsage(
|
|
12
|
+
usage: AmazonBedrockUsage | undefined | null,
|
|
13
13
|
): LanguageModelV4Usage {
|
|
14
14
|
if (usage == null) {
|
|
15
15
|
return {
|