@ai-sdk/amazon-bedrock 5.0.0-beta.7 → 5.0.0-beta.85
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 +677 -4
- package/README.md +2 -0
- package/dist/anthropic/index.d.ts +10 -10
- package/dist/anthropic/index.js +151 -117
- package/dist/anthropic/index.js.map +1 -1
- package/dist/index.d.ts +36 -23
- package/dist/index.js +881 -604
- package/dist/index.js.map +1 -1
- package/dist/mantle/index.cjs +253 -0
- package/dist/mantle/index.cjs.map +1 -0
- package/dist/mantle/index.d.cts +99 -0
- package/dist/mantle/index.d.ts +99 -0
- package/dist/mantle/index.js +240 -0
- package/dist/mantle/index.js.map +1 -0
- package/docs/08-amazon-bedrock.mdx +310 -84
- package/mantle/index.d.ts +1 -0
- package/package.json +27 -20
- package/src/amazon-bedrock-api-types.ts +228 -0
- package/src/{bedrock-chat-options.ts → amazon-bedrock-chat-language-model-options.ts} +27 -8
- package/src/{bedrock-chat-language-model.ts → amazon-bedrock-chat-language-model.ts} +350 -180
- 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} +61 -29
- 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} +62 -38
- 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} +17 -9
- package/src/anthropic/amazon-bedrock-anthropic-fetch.ts +104 -0
- package/src/anthropic/{bedrock-anthropic-options.ts → amazon-bedrock-anthropic-options.ts} +7 -1
- package/src/anthropic/{bedrock-anthropic-provider.ts → amazon-bedrock-anthropic-provider.ts} +40 -24
- package/src/anthropic/index.ts +19 -7
- package/src/{convert-bedrock-usage.ts → convert-amazon-bedrock-usage.ts} +4 -4
- package/src/convert-to-amazon-bedrock-chat-messages.ts +556 -0
- package/src/index.ts +15 -8
- package/src/inject-fetch-headers.ts +1 -1
- package/src/mantle/bedrock-mantle-options.ts +15 -0
- package/src/mantle/bedrock-mantle-provider.ts +283 -0
- package/src/mantle/index.ts +6 -0
- 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/dist/anthropic/index.d.mts +0 -91
- package/dist/anthropic/index.mjs +0 -397
- package/dist/anthropic/index.mjs.map +0 -1
- package/dist/index.d.mts +0 -194
- package/dist/index.mjs +0 -2329
- package/dist/index.mjs.map +0 -1
- package/src/anthropic/bedrock-anthropic-fetch.ts +0 -68
- package/src/bedrock-api-types.ts +0 -216
- package/src/bedrock-image-settings.ts +0 -6
- package/src/convert-to-bedrock-chat-messages.ts +0 -468
|
@@ -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;
|
|
@@ -20,16 +20,22 @@ export interface BedrockCredentials {
|
|
|
20
20
|
*
|
|
21
21
|
* @param getCredentials - Function that returns the AWS credentials to use when signing.
|
|
22
22
|
* @param fetch - Optional original fetch implementation to wrap. Defaults to global fetch.
|
|
23
|
+
* @param service - The AWS service name to use for SigV4 signing scope. Defaults to 'bedrock'.
|
|
23
24
|
* @returns A FetchFunction that signs requests before passing them to the underlying fetch.
|
|
24
25
|
*/
|
|
25
26
|
export function createSigV4FetchFunction(
|
|
26
|
-
getCredentials: () =>
|
|
27
|
-
|
|
27
|
+
getCredentials: () =>
|
|
28
|
+
| AmazonBedrockCredentials
|
|
29
|
+
| PromiseLike<AmazonBedrockCredentials>,
|
|
30
|
+
fetch?: FetchFunction,
|
|
31
|
+
service: string = 'bedrock',
|
|
28
32
|
): FetchFunction {
|
|
29
33
|
return async (
|
|
30
34
|
input: RequestInfo | URL,
|
|
31
35
|
init?: RequestInit,
|
|
32
36
|
): Promise<Response> => {
|
|
37
|
+
// avoid caching globalThis.fetch in case it is patched by other libraries
|
|
38
|
+
const effectiveFetch = fetch ?? globalThis.fetch;
|
|
33
39
|
const request = input instanceof Request ? input : undefined;
|
|
34
40
|
const originalHeaders = combineHeaders(
|
|
35
41
|
normalizeHeaders(request?.headers),
|
|
@@ -51,7 +57,7 @@ export function createSigV4FetchFunction(
|
|
|
51
57
|
const effectiveMethod = init?.method ?? request?.method;
|
|
52
58
|
|
|
53
59
|
if (effectiveMethod?.toUpperCase() !== 'POST' || !effectiveBody) {
|
|
54
|
-
return
|
|
60
|
+
return effectiveFetch(input, {
|
|
55
61
|
...init,
|
|
56
62
|
headers: headersWithUserAgent as HeadersInit,
|
|
57
63
|
});
|
|
@@ -75,7 +81,7 @@ export function createSigV4FetchFunction(
|
|
|
75
81
|
accessKeyId: credentials.accessKeyId,
|
|
76
82
|
secretAccessKey: credentials.secretAccessKey,
|
|
77
83
|
sessionToken: credentials.sessionToken,
|
|
78
|
-
service
|
|
84
|
+
service,
|
|
79
85
|
});
|
|
80
86
|
|
|
81
87
|
const signingResult = await signer.sign();
|
|
@@ -84,7 +90,7 @@ export function createSigV4FetchFunction(
|
|
|
84
90
|
// Use the combined headers directly as HeadersInit
|
|
85
91
|
const combinedHeaders = combineHeaders(headersWithUserAgent, signedHeaders);
|
|
86
92
|
|
|
87
|
-
return
|
|
93
|
+
return effectiveFetch(input, {
|
|
88
94
|
...init,
|
|
89
95
|
body,
|
|
90
96
|
headers: combinedHeaders as HeadersInit,
|
|
@@ -113,12 +119,14 @@ function prepareBodyString(body: BodyInit | undefined): string {
|
|
|
113
119
|
*/
|
|
114
120
|
export function createApiKeyFetchFunction(
|
|
115
121
|
apiKey: string,
|
|
116
|
-
fetch
|
|
122
|
+
fetch?: FetchFunction,
|
|
117
123
|
): FetchFunction {
|
|
118
124
|
return async (
|
|
119
125
|
input: RequestInfo | URL,
|
|
120
126
|
init?: RequestInit,
|
|
121
127
|
): Promise<Response> => {
|
|
128
|
+
// avoid caching globalThis.fetch in case it is patched by other libraries
|
|
129
|
+
const effectiveFetch = fetch ?? globalThis.fetch;
|
|
122
130
|
const originalHeaders = normalizeHeaders(init?.headers);
|
|
123
131
|
const headersWithUserAgent = withUserAgentSuffix(
|
|
124
132
|
originalHeaders,
|
|
@@ -130,7 +138,7 @@ export function createApiKeyFetchFunction(
|
|
|
130
138
|
Authorization: `Bearer ${apiKey}`,
|
|
131
139
|
});
|
|
132
140
|
|
|
133
|
-
return
|
|
141
|
+
return effectiveFetch(input, {
|
|
134
142
|
...init,
|
|
135
143
|
headers: finalHeaders as HeadersInit,
|
|
136
144
|
});
|
|
@@ -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
|
+
}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type AmazonBedrockAnthropicModelId =
|
|
2
|
+
| 'anthropic.claude-fable-5'
|
|
3
|
+
| 'anthropic.claude-opus-4-8'
|
|
4
|
+
| 'anthropic.claude-opus-4-7'
|
|
2
5
|
| 'anthropic.claude-opus-4-6-v1'
|
|
3
6
|
| 'anthropic.claude-sonnet-4-6-v1'
|
|
4
7
|
| 'anthropic.claude-opus-4-5-20251101-v1:0'
|
|
@@ -14,6 +17,9 @@ export type BedrockAnthropicModelId =
|
|
|
14
17
|
| 'anthropic.claude-3-opus-20240229-v1:0'
|
|
15
18
|
| 'anthropic.claude-3-sonnet-20240229-v1:0'
|
|
16
19
|
| 'anthropic.claude-3-haiku-20240307-v1:0'
|
|
20
|
+
| 'us.anthropic.claude-fable-5'
|
|
21
|
+
| 'us.anthropic.claude-opus-4-8'
|
|
22
|
+
| 'us.anthropic.claude-opus-4-7'
|
|
17
23
|
| 'us.anthropic.claude-opus-4-6-v1'
|
|
18
24
|
| 'us.anthropic.claude-sonnet-4-6-v1'
|
|
19
25
|
| 'us.anthropic.claude-opus-4-5-20251101-v1:0'
|
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
|
|
@@ -47,18 +47,22 @@ const BEDROCK_TOOL_BETA_MAP: Record<string, string> = {
|
|
|
47
47
|
text_editor_20250728: 'computer-use-2025-01-24',
|
|
48
48
|
computer_20250124: 'computer-use-2025-01-24',
|
|
49
49
|
computer_20241022: 'computer-use-2024-10-22',
|
|
50
|
+
tool_search_tool_regex_20251119: 'tool-search-tool-2025-10-19',
|
|
51
|
+
// BM25 is not currently supported on Bedrock, but including the beta flag
|
|
52
|
+
// so that Bedrock returns a more useful error message if it's used.
|
|
53
|
+
tool_search_tool_bm25_20251119: 'tool-search-tool-2025-10-19',
|
|
50
54
|
};
|
|
51
55
|
|
|
52
|
-
export interface
|
|
56
|
+
export interface AmazonBedrockAnthropicProvider extends ProviderV4 {
|
|
53
57
|
/**
|
|
54
58
|
* Creates a model for text generation.
|
|
55
59
|
*/
|
|
56
|
-
(modelId:
|
|
60
|
+
(modelId: AmazonBedrockAnthropicModelId): LanguageModelV4;
|
|
57
61
|
|
|
58
62
|
/**
|
|
59
63
|
* Creates a model for text generation.
|
|
60
64
|
*/
|
|
61
|
-
languageModel(modelId:
|
|
65
|
+
languageModel(modelId: AmazonBedrockAnthropicModelId): LanguageModelV4;
|
|
62
66
|
|
|
63
67
|
/**
|
|
64
68
|
* Anthropic-specific computer use tool.
|
|
@@ -71,7 +75,7 @@ export interface BedrockAnthropicProvider extends ProviderV4 {
|
|
|
71
75
|
textEmbeddingModel(modelId: string): never;
|
|
72
76
|
}
|
|
73
77
|
|
|
74
|
-
export interface
|
|
78
|
+
export interface AmazonBedrockAnthropicProviderSettings {
|
|
75
79
|
/**
|
|
76
80
|
* The AWS region to use for the Bedrock provider. Defaults to the value of the
|
|
77
81
|
* `AWS_REGION` environment variable.
|
|
@@ -125,7 +129,9 @@ export interface BedrockAnthropicProviderSettings {
|
|
|
125
129
|
* credential values to be used instead of the `accessKeyId`, `secretAccessKey`,
|
|
126
130
|
* and `sessionToken` settings.
|
|
127
131
|
*/
|
|
128
|
-
credentialProvider?: () => PromiseLike<
|
|
132
|
+
credentialProvider?: () => PromiseLike<
|
|
133
|
+
Omit<AmazonBedrockCredentials, 'region'>
|
|
134
|
+
>;
|
|
129
135
|
}
|
|
130
136
|
|
|
131
137
|
/**
|
|
@@ -133,9 +139,9 @@ export interface BedrockAnthropicProviderSettings {
|
|
|
133
139
|
* This provider uses the native Anthropic API through Bedrock's InvokeModel endpoint,
|
|
134
140
|
* bypassing the Converse API for better feature compatibility.
|
|
135
141
|
*/
|
|
136
|
-
export function
|
|
137
|
-
options:
|
|
138
|
-
):
|
|
142
|
+
export function createAmazonBedrockAnthropic(
|
|
143
|
+
options: AmazonBedrockAnthropicProviderSettings = {},
|
|
144
|
+
): AmazonBedrockAnthropicProvider {
|
|
139
145
|
// Check for API key authentication first
|
|
140
146
|
const rawApiKey = loadOptionalSetting({
|
|
141
147
|
settingValue: options.apiKey,
|
|
@@ -226,7 +232,7 @@ export function createBedrockAnthropic(
|
|
|
226
232
|
}, options.fetch);
|
|
227
233
|
|
|
228
234
|
// Wrap with Bedrock event stream to SSE transformer for streaming support
|
|
229
|
-
const fetchFunction =
|
|
235
|
+
const fetchFunction = createAmazonBedrockAnthropicFetch(baseFetchFunction);
|
|
230
236
|
|
|
231
237
|
const getBaseURL = (): string =>
|
|
232
238
|
withoutTrailingSlash(
|
|
@@ -244,8 +250,8 @@ export function createBedrockAnthropic(
|
|
|
244
250
|
return withUserAgentSuffix(baseHeaders, `ai-sdk/amazon-bedrock/${VERSION}`);
|
|
245
251
|
};
|
|
246
252
|
|
|
247
|
-
const createChatModel = (modelId:
|
|
248
|
-
new
|
|
253
|
+
const createChatModel = (modelId: AmazonBedrockAnthropicModelId) =>
|
|
254
|
+
new AnthropicLanguageModel(modelId, {
|
|
249
255
|
provider: 'bedrock.anthropic.messages',
|
|
250
256
|
baseURL: getBaseURL(),
|
|
251
257
|
headers: getHeaders,
|
|
@@ -257,7 +263,13 @@ export function createBedrockAnthropic(
|
|
|
257
263
|
}`,
|
|
258
264
|
|
|
259
265
|
transformRequestBody: (args, betas) => {
|
|
260
|
-
const {
|
|
266
|
+
const {
|
|
267
|
+
model: _model,
|
|
268
|
+
stream: _stream,
|
|
269
|
+
tool_choice,
|
|
270
|
+
tools,
|
|
271
|
+
...rest
|
|
272
|
+
} = args;
|
|
261
273
|
|
|
262
274
|
const transformedToolChoice =
|
|
263
275
|
tool_choice != null
|
|
@@ -320,10 +332,14 @@ export function createBedrockAnthropic(
|
|
|
320
332
|
// Bedrock Anthropic doesn't support URL sources, force download and base64 conversion
|
|
321
333
|
supportedUrls: () => ({}),
|
|
322
334
|
// native structured output via output_config.format is supported on Bedrock
|
|
323
|
-
|
|
335
|
+
// Bedrock rejects `output_config.format` for `claude-opus-4-7`, `claude-opus-4-8`, and `claude-fable-5`
|
|
336
|
+
supportsNativeStructuredOutput:
|
|
337
|
+
!modelId.includes('claude-opus-4-7') &&
|
|
338
|
+
!modelId.includes('claude-opus-4-8') &&
|
|
339
|
+
!modelId.includes('claude-fable-5'),
|
|
324
340
|
});
|
|
325
341
|
|
|
326
|
-
const provider = function (modelId:
|
|
342
|
+
const provider = function (modelId: AmazonBedrockAnthropicModelId) {
|
|
327
343
|
if (new.target) {
|
|
328
344
|
throw new Error(
|
|
329
345
|
'The Bedrock Anthropic model function cannot be called with the new keyword.',
|
|
@@ -354,4 +370,4 @@ export function createBedrockAnthropic(
|
|
|
354
370
|
/**
|
|
355
371
|
* Default Bedrock Anthropic provider instance.
|
|
356
372
|
*/
|
|
357
|
-
export const
|
|
373
|
+
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 {
|