@ai-sdk/amazon-bedrock 5.0.0-beta.45 → 5.0.0-beta.47
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 +21 -0
- package/dist/anthropic/index.d.ts +10 -10
- package/dist/anthropic/index.js +65 -51
- package/dist/anthropic/index.js.map +1 -1
- package/dist/index.d.ts +21 -21
- package/dist/index.js +338 -298
- package/dist/index.js.map +1 -1
- package/docs/08-amazon-bedrock.mdx +66 -66
- package/package.json +3 -3
- package/src/{bedrock-api-types.ts → amazon-bedrock-api-types.ts} +60 -55
- 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} +211 -190
- 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} +31 -24
- 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} +3 -3
- package/src/{bedrock-image-model.ts → amazon-bedrock-image-model.ts} +40 -36
- package/src/amazon-bedrock-image-settings.ts +9 -0
- package/src/{bedrock-prepare-tools.ts → amazon-bedrock-prepare-tools.ts} +18 -17
- package/src/{bedrock-provider.ts → amazon-bedrock-provider.ts} +40 -38
- package/src/amazon-bedrock-reasoning-metadata.ts +10 -0
- package/src/{bedrock-sigv4-fetch.ts → amazon-bedrock-sigv4-fetch.ts} +13 -7
- 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} +18 -16
- package/src/anthropic/index.ts +19 -7
- package/src/{convert-bedrock-usage.ts → convert-amazon-bedrock-usage.ts} +3 -3
- package/src/{convert-to-bedrock-chat-messages.ts → convert-to-amazon-bedrock-chat-messages.ts} +92 -59
- package/src/index.ts +15 -8
- package/src/{map-bedrock-finish-reason.ts → map-amazon-bedrock-finish-reason.ts} +3 -3
- 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} +29 -21
- package/src/anthropic/bedrock-anthropic-fetch.ts +0 -94
- package/src/bedrock-image-settings.ts +0 -6
- package/src/bedrock-reasoning-metadata.ts +0 -10
|
@@ -8,7 +8,7 @@ import {
|
|
|
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,13 +23,17 @@ 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: () =>
|
|
27
|
-
|
|
26
|
+
getCredentials: () =>
|
|
27
|
+
| AmazonBedrockCredentials
|
|
28
|
+
| PromiseLike<AmazonBedrockCredentials>,
|
|
29
|
+
fetch?: FetchFunction,
|
|
28
30
|
): FetchFunction {
|
|
29
31
|
return async (
|
|
30
32
|
input: RequestInfo | URL,
|
|
31
33
|
init?: RequestInit,
|
|
32
34
|
): Promise<Response> => {
|
|
35
|
+
// avoid caching globalThis.fetch in case it is patched by other libraries
|
|
36
|
+
const effectiveFetch = fetch ?? globalThis.fetch;
|
|
33
37
|
const request = input instanceof Request ? input : undefined;
|
|
34
38
|
const originalHeaders = combineHeaders(
|
|
35
39
|
normalizeHeaders(request?.headers),
|
|
@@ -51,7 +55,7 @@ export function createSigV4FetchFunction(
|
|
|
51
55
|
const effectiveMethod = init?.method ?? request?.method;
|
|
52
56
|
|
|
53
57
|
if (effectiveMethod?.toUpperCase() !== 'POST' || !effectiveBody) {
|
|
54
|
-
return
|
|
58
|
+
return effectiveFetch(input, {
|
|
55
59
|
...init,
|
|
56
60
|
headers: headersWithUserAgent as HeadersInit,
|
|
57
61
|
});
|
|
@@ -84,7 +88,7 @@ export function createSigV4FetchFunction(
|
|
|
84
88
|
// Use the combined headers directly as HeadersInit
|
|
85
89
|
const combinedHeaders = combineHeaders(headersWithUserAgent, signedHeaders);
|
|
86
90
|
|
|
87
|
-
return
|
|
91
|
+
return effectiveFetch(input, {
|
|
88
92
|
...init,
|
|
89
93
|
body,
|
|
90
94
|
headers: combinedHeaders as HeadersInit,
|
|
@@ -113,12 +117,14 @@ function prepareBodyString(body: BodyInit | undefined): string {
|
|
|
113
117
|
*/
|
|
114
118
|
export function createApiKeyFetchFunction(
|
|
115
119
|
apiKey: string,
|
|
116
|
-
fetch
|
|
120
|
+
fetch?: FetchFunction,
|
|
117
121
|
): FetchFunction {
|
|
118
122
|
return async (
|
|
119
123
|
input: RequestInfo | URL,
|
|
120
124
|
init?: RequestInit,
|
|
121
125
|
): Promise<Response> => {
|
|
126
|
+
// avoid caching globalThis.fetch in case it is patched by other libraries
|
|
127
|
+
const effectiveFetch = fetch ?? globalThis.fetch;
|
|
122
128
|
const originalHeaders = normalizeHeaders(init?.headers);
|
|
123
129
|
const headersWithUserAgent = withUserAgentSuffix(
|
|
124
130
|
originalHeaders,
|
|
@@ -130,7 +136,7 @@ export function createApiKeyFetchFunction(
|
|
|
130
136
|
Authorization: `Bearer ${apiKey}`,
|
|
131
137
|
});
|
|
132
138
|
|
|
133
|
-
return
|
|
139
|
+
return effectiveFetch(input, {
|
|
134
140
|
...init,
|
|
135
141
|
headers: finalHeaders as HeadersInit,
|
|
136
142
|
});
|
|
@@ -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
|
@@ -19,10 +19,10 @@ import {
|
|
|
19
19
|
import {
|
|
20
20
|
createApiKeyFetchFunction,
|
|
21
21
|
createSigV4FetchFunction,
|
|
22
|
-
type
|
|
23
|
-
} from '../bedrock-sigv4-fetch';
|
|
24
|
-
import {
|
|
25
|
-
import type {
|
|
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
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 {
|
package/src/{convert-to-bedrock-chat-messages.ts → convert-to-amazon-bedrock-chat-messages.ts}
RENAMED
|
@@ -16,25 +16,26 @@ import {
|
|
|
16
16
|
import {
|
|
17
17
|
BEDROCK_DOCUMENT_MIME_TYPES,
|
|
18
18
|
BEDROCK_IMAGE_MIME_TYPES,
|
|
19
|
-
type
|
|
20
|
-
type
|
|
21
|
-
type
|
|
22
|
-
type
|
|
23
|
-
type
|
|
24
|
-
type
|
|
25
|
-
type
|
|
26
|
-
type
|
|
27
|
-
type
|
|
28
|
-
} from './bedrock-api-types';
|
|
29
|
-
import {
|
|
30
|
-
import {
|
|
19
|
+
type AmazonBedrockAssistantMessage,
|
|
20
|
+
type AmazonBedrockCachePoint,
|
|
21
|
+
type AmazonBedrockDocumentFormat,
|
|
22
|
+
type AmazonBedrockDocumentMimeType,
|
|
23
|
+
type AmazonBedrockImageFormat,
|
|
24
|
+
type AmazonBedrockImageMimeType,
|
|
25
|
+
type AmazonBedrockMessages,
|
|
26
|
+
type AmazonBedrockSystemMessages,
|
|
27
|
+
type AmazonBedrockUserMessage,
|
|
28
|
+
} from './amazon-bedrock-api-types';
|
|
29
|
+
import { amazonBedrockFilePartProviderOptions } from './amazon-bedrock-chat-language-model-options';
|
|
30
|
+
import { amazonBedrockReasoningMetadataSchema } from './amazon-bedrock-reasoning-metadata';
|
|
31
31
|
import { normalizeToolCallId } from './normalize-tool-call-id';
|
|
32
32
|
|
|
33
33
|
function getCachePoint(
|
|
34
34
|
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
35
|
-
):
|
|
36
|
-
const cachePointConfig = providerMetadata?.
|
|
37
|
-
|
|
35
|
+
): AmazonBedrockCachePoint | undefined {
|
|
36
|
+
const cachePointConfig = (providerMetadata?.amazonBedrock?.cachePoint ??
|
|
37
|
+
providerMetadata?.bedrock?.cachePoint) as
|
|
38
|
+
| AmazonBedrockCachePoint['cachePoint']
|
|
38
39
|
| undefined;
|
|
39
40
|
|
|
40
41
|
if (!cachePointConfig) {
|
|
@@ -47,26 +48,32 @@ function getCachePoint(
|
|
|
47
48
|
async function shouldEnableCitations(
|
|
48
49
|
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
49
50
|
): Promise<boolean> {
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
const amazonBedrockOptions =
|
|
52
|
+
(await parseProviderOptions({
|
|
53
|
+
provider: 'amazonBedrock',
|
|
54
|
+
providerOptions: providerMetadata,
|
|
55
|
+
schema: amazonBedrockFilePartProviderOptions,
|
|
56
|
+
})) ??
|
|
57
|
+
(await parseProviderOptions({
|
|
58
|
+
provider: 'bedrock',
|
|
59
|
+
providerOptions: providerMetadata,
|
|
60
|
+
schema: amazonBedrockFilePartProviderOptions,
|
|
61
|
+
}));
|
|
62
|
+
|
|
63
|
+
return amazonBedrockOptions?.citations?.enabled ?? false;
|
|
57
64
|
}
|
|
58
65
|
|
|
59
|
-
export async function
|
|
66
|
+
export async function convertToAmazonBedrockChatMessages(
|
|
60
67
|
prompt: LanguageModelV4Prompt,
|
|
61
68
|
isMistral: boolean = false,
|
|
62
69
|
): Promise<{
|
|
63
|
-
system:
|
|
64
|
-
messages:
|
|
70
|
+
system: AmazonBedrockSystemMessages;
|
|
71
|
+
messages: AmazonBedrockMessages;
|
|
65
72
|
}> {
|
|
66
73
|
const blocks = groupIntoBlocks(prompt);
|
|
67
74
|
|
|
68
|
-
let system:
|
|
69
|
-
const messages:
|
|
75
|
+
let system: AmazonBedrockSystemMessages = [];
|
|
76
|
+
const messages: AmazonBedrockMessages = [];
|
|
70
77
|
|
|
71
78
|
let documentCounter = 0;
|
|
72
79
|
const generateDocumentName = () => `document-${++documentCounter}`;
|
|
@@ -97,7 +104,7 @@ export async function convertToBedrockChatMessages(
|
|
|
97
104
|
|
|
98
105
|
case 'user': {
|
|
99
106
|
// combines all user and tool messages in this block into a single message:
|
|
100
|
-
const
|
|
107
|
+
const amazonBedrockContent: AmazonBedrockUserMessage['content'] = [];
|
|
101
108
|
|
|
102
109
|
for (const message of block.messages) {
|
|
103
110
|
const { role, content, providerOptions } = message;
|
|
@@ -108,7 +115,7 @@ export async function convertToBedrockChatMessages(
|
|
|
108
115
|
|
|
109
116
|
switch (part.type) {
|
|
110
117
|
case 'text': {
|
|
111
|
-
|
|
118
|
+
amazonBedrockContent.push({
|
|
112
119
|
text: part.text,
|
|
113
120
|
});
|
|
114
121
|
break;
|
|
@@ -134,9 +141,10 @@ export async function convertToBedrockChatMessages(
|
|
|
134
141
|
part.providerOptions,
|
|
135
142
|
);
|
|
136
143
|
|
|
137
|
-
|
|
144
|
+
amazonBedrockContent.push({
|
|
138
145
|
document: {
|
|
139
|
-
format:
|
|
146
|
+
format:
|
|
147
|
+
getAmazonBedrockDocumentFormat(textMediaType),
|
|
140
148
|
name: part.filename
|
|
141
149
|
? stripFileExtension(part.filename)
|
|
142
150
|
: generateDocumentName(),
|
|
@@ -156,9 +164,10 @@ export async function convertToBedrockChatMessages(
|
|
|
156
164
|
const fullMediaType = resolveFullMediaType({ part });
|
|
157
165
|
|
|
158
166
|
if (getTopLevelMediaType(fullMediaType) === 'image') {
|
|
159
|
-
|
|
167
|
+
amazonBedrockContent.push({
|
|
160
168
|
image: {
|
|
161
|
-
format:
|
|
169
|
+
format:
|
|
170
|
+
getAmazonBedrockImageFormat(fullMediaType),
|
|
162
171
|
source: {
|
|
163
172
|
bytes: convertToBase64(part.data.data),
|
|
164
173
|
},
|
|
@@ -169,9 +178,10 @@ export async function convertToBedrockChatMessages(
|
|
|
169
178
|
part.providerOptions,
|
|
170
179
|
);
|
|
171
180
|
|
|
172
|
-
|
|
181
|
+
amazonBedrockContent.push({
|
|
173
182
|
document: {
|
|
174
|
-
format:
|
|
183
|
+
format:
|
|
184
|
+
getAmazonBedrockDocumentFormat(fullMediaType),
|
|
175
185
|
name: part.filename
|
|
176
186
|
? stripFileExtension(part.filename)
|
|
177
187
|
: generateDocumentName(),
|
|
@@ -216,7 +226,7 @@ export async function convertToBedrockChatMessages(
|
|
|
216
226
|
});
|
|
217
227
|
}
|
|
218
228
|
|
|
219
|
-
const format =
|
|
229
|
+
const format = getAmazonBedrockImageFormat(
|
|
220
230
|
contentPart.mediaType,
|
|
221
231
|
);
|
|
222
232
|
|
|
@@ -253,7 +263,7 @@ export async function convertToBedrockChatMessages(
|
|
|
253
263
|
break;
|
|
254
264
|
}
|
|
255
265
|
|
|
256
|
-
|
|
266
|
+
amazonBedrockContent.push({
|
|
257
267
|
toolResult: {
|
|
258
268
|
toolUseId: normalizeToolCallId(part.toolCallId, isMistral),
|
|
259
269
|
content: toolResultContent,
|
|
@@ -271,18 +281,19 @@ export async function convertToBedrockChatMessages(
|
|
|
271
281
|
|
|
272
282
|
const cachePoint = getCachePoint(providerOptions);
|
|
273
283
|
if (cachePoint) {
|
|
274
|
-
|
|
284
|
+
amazonBedrockContent.push(cachePoint);
|
|
275
285
|
}
|
|
276
286
|
}
|
|
277
287
|
|
|
278
|
-
messages.push({ role: 'user', content:
|
|
288
|
+
messages.push({ role: 'user', content: amazonBedrockContent });
|
|
279
289
|
|
|
280
290
|
break;
|
|
281
291
|
}
|
|
282
292
|
|
|
283
293
|
case 'assistant': {
|
|
284
294
|
// combines multiple assistant messages in this block into a single message:
|
|
285
|
-
const
|
|
295
|
+
const amazonBedrockContent: AmazonBedrockAssistantMessage['content'] =
|
|
296
|
+
[];
|
|
286
297
|
|
|
287
298
|
for (let j = 0; j < block.messages.length; j++) {
|
|
288
299
|
const message = block.messages[j];
|
|
@@ -303,7 +314,7 @@ export async function convertToBedrockChatMessages(
|
|
|
303
314
|
break;
|
|
304
315
|
}
|
|
305
316
|
|
|
306
|
-
|
|
317
|
+
amazonBedrockContent.push({
|
|
307
318
|
text:
|
|
308
319
|
// trim the last text part if it's the last message in the block
|
|
309
320
|
// because Bedrock does not allow trailing whitespace
|
|
@@ -319,16 +330,22 @@ export async function convertToBedrockChatMessages(
|
|
|
319
330
|
}
|
|
320
331
|
|
|
321
332
|
case 'reasoning': {
|
|
322
|
-
const reasoningMetadata =
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
333
|
+
const reasoningMetadata =
|
|
334
|
+
(await parseProviderOptions({
|
|
335
|
+
provider: 'amazonBedrock',
|
|
336
|
+
providerOptions: part.providerOptions,
|
|
337
|
+
schema: amazonBedrockReasoningMetadataSchema,
|
|
338
|
+
})) ??
|
|
339
|
+
(await parseProviderOptions({
|
|
340
|
+
provider: 'bedrock',
|
|
341
|
+
providerOptions: part.providerOptions,
|
|
342
|
+
schema: amazonBedrockReasoningMetadataSchema,
|
|
343
|
+
}));
|
|
327
344
|
|
|
328
345
|
if (reasoningMetadata?.signature != null) {
|
|
329
346
|
// do not trim reasoning text when a signature is present:
|
|
330
347
|
// the signature validates the exact original bytes
|
|
331
|
-
|
|
348
|
+
amazonBedrockContent.push({
|
|
332
349
|
reasoningContent: {
|
|
333
350
|
reasoningText: {
|
|
334
351
|
text: part.text,
|
|
@@ -337,18 +354,29 @@ export async function convertToBedrockChatMessages(
|
|
|
337
354
|
},
|
|
338
355
|
});
|
|
339
356
|
} else if (reasoningMetadata?.redactedData != null) {
|
|
340
|
-
|
|
357
|
+
amazonBedrockContent.push({
|
|
341
358
|
reasoningContent: {
|
|
342
359
|
redactedReasoning: {
|
|
343
360
|
data: reasoningMetadata.redactedData,
|
|
344
361
|
},
|
|
345
362
|
},
|
|
346
363
|
});
|
|
347
|
-
} else
|
|
348
|
-
|
|
349
|
-
|
|
364
|
+
} else if (
|
|
365
|
+
part.providerOptions == null ||
|
|
366
|
+
Object.keys(part.providerOptions).every(
|
|
367
|
+
k => k === 'bedrock' || k === 'amazonBedrock',
|
|
368
|
+
)
|
|
369
|
+
) {
|
|
370
|
+
// No foreign-provider metadata — preserve text. This covers
|
|
371
|
+
// the prefill case where the caller hand-crafts a reasoning
|
|
372
|
+
// block without a signature. Forwarding reasoning that was
|
|
373
|
+
// signed by a different provider (e.g. anthropic) would
|
|
374
|
+
// cause Bedrock to reject with
|
|
375
|
+
// `thinking.signature: Field required`, so we drop those.
|
|
376
|
+
// trim the last text part if it's the last message in the
|
|
377
|
+
// block because Bedrock does not allow trailing whitespace
|
|
350
378
|
// in pre-filled assistant responses
|
|
351
|
-
|
|
379
|
+
amazonBedrockContent.push({
|
|
352
380
|
reasoningContent: {
|
|
353
381
|
reasoningText: {
|
|
354
382
|
text: trimIfLast(
|
|
@@ -366,7 +394,7 @@ export async function convertToBedrockChatMessages(
|
|
|
366
394
|
}
|
|
367
395
|
|
|
368
396
|
case 'tool-call': {
|
|
369
|
-
|
|
397
|
+
amazonBedrockContent.push({
|
|
370
398
|
toolUse: {
|
|
371
399
|
toolUseId: normalizeToolCallId(part.toolCallId, isMistral),
|
|
372
400
|
name: part.toolName,
|
|
@@ -379,11 +407,11 @@ export async function convertToBedrockChatMessages(
|
|
|
379
407
|
}
|
|
380
408
|
const cachePoint = getCachePoint(message.providerOptions);
|
|
381
409
|
if (cachePoint) {
|
|
382
|
-
|
|
410
|
+
amazonBedrockContent.push(cachePoint);
|
|
383
411
|
}
|
|
384
412
|
}
|
|
385
413
|
|
|
386
|
-
messages.push({ role: 'assistant', content:
|
|
414
|
+
messages.push({ role: 'assistant', content: amazonBedrockContent });
|
|
387
415
|
|
|
388
416
|
break;
|
|
389
417
|
}
|
|
@@ -398,8 +426,11 @@ export async function convertToBedrockChatMessages(
|
|
|
398
426
|
return { system, messages };
|
|
399
427
|
}
|
|
400
428
|
|
|
401
|
-
function
|
|
402
|
-
|
|
429
|
+
function getAmazonBedrockImageFormat(
|
|
430
|
+
mimeType: string,
|
|
431
|
+
): AmazonBedrockImageFormat {
|
|
432
|
+
const format =
|
|
433
|
+
BEDROCK_IMAGE_MIME_TYPES[mimeType as AmazonBedrockImageMimeType];
|
|
403
434
|
if (!format) {
|
|
404
435
|
throw new UnsupportedFunctionalityError({
|
|
405
436
|
functionality: `image mime type: ${mimeType}`,
|
|
@@ -410,9 +441,11 @@ function getBedrockImageFormat(mimeType: string): BedrockImageFormat {
|
|
|
410
441
|
return format;
|
|
411
442
|
}
|
|
412
443
|
|
|
413
|
-
function
|
|
444
|
+
function getAmazonBedrockDocumentFormat(
|
|
445
|
+
mimeType: string,
|
|
446
|
+
): AmazonBedrockDocumentFormat {
|
|
414
447
|
const format =
|
|
415
|
-
BEDROCK_DOCUMENT_MIME_TYPES[mimeType as
|
|
448
|
+
BEDROCK_DOCUMENT_MIME_TYPES[mimeType as AmazonBedrockDocumentMimeType];
|
|
416
449
|
if (!format) {
|
|
417
450
|
throw new UnsupportedFunctionalityError({
|
|
418
451
|
functionality: `file mime type: ${mimeType}`,
|
package/src/index.ts
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
export type { AnthropicProviderOptions } from '@ai-sdk/anthropic';
|
|
2
2
|
|
|
3
|
-
export type { AmazonBedrockEmbeddingModelOptions } from './bedrock-embedding-options';
|
|
3
|
+
export type { AmazonBedrockEmbeddingModelOptions } from './amazon-bedrock-embedding-model-options';
|
|
4
4
|
export type {
|
|
5
|
-
|
|
6
|
-
/** @deprecated Use `
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
AmazonBedrockLanguageModelChatOptions,
|
|
6
|
+
/** @deprecated Use `AmazonBedrockLanguageModelChatOptions` instead. */
|
|
7
|
+
AmazonBedrockLanguageModelChatOptions as AmazonBedrockLanguageModelOptions,
|
|
8
|
+
/** @deprecated Use `AmazonBedrockLanguageModelChatOptions` instead. */
|
|
9
|
+
AmazonBedrockLanguageModelChatOptions as BedrockProviderOptions,
|
|
10
|
+
} from './amazon-bedrock-chat-language-model-options';
|
|
11
|
+
export {
|
|
12
|
+
amazonBedrock,
|
|
13
|
+
/** @deprecated Use `amazonBedrock` instead. */
|
|
14
|
+
amazonBedrock as bedrock,
|
|
15
|
+
createAmazonBedrock,
|
|
16
|
+
} from './amazon-bedrock-provider';
|
|
10
17
|
export type {
|
|
11
18
|
AmazonBedrockProvider,
|
|
12
19
|
AmazonBedrockProviderSettings,
|
|
13
|
-
} from './bedrock-provider';
|
|
20
|
+
} from './amazon-bedrock-provider';
|
|
14
21
|
export type {
|
|
15
22
|
AmazonBedrockRerankingModelOptions,
|
|
16
23
|
/** @deprecated Use `AmazonBedrockRerankingModelOptions` instead. */
|
|
17
24
|
AmazonBedrockRerankingModelOptions as BedrockRerankingOptions,
|
|
18
|
-
} from './reranking/bedrock-reranking-options';
|
|
25
|
+
} from './reranking/amazon-bedrock-reranking-model-options';
|
|
19
26
|
export { VERSION } from './version';
|