@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.
Files changed (38) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/dist/anthropic/index.d.ts +10 -10
  3. package/dist/anthropic/index.js +58 -46
  4. package/dist/anthropic/index.js.map +1 -1
  5. package/dist/index.d.ts +25 -23
  6. package/dist/index.js +496 -436
  7. package/dist/index.js.map +1 -1
  8. package/docs/08-amazon-bedrock.mdx +80 -82
  9. package/package.json +5 -5
  10. package/src/amazon-bedrock-api-types.ts +224 -0
  11. package/src/{bedrock-chat-options.ts → amazon-bedrock-chat-language-model-options.ts} +7 -7
  12. package/src/{bedrock-chat-language-model.ts → amazon-bedrock-chat-language-model.ts} +217 -196
  13. package/src/{bedrock-embedding-options.ts → amazon-bedrock-embedding-model-options.ts} +1 -1
  14. package/src/{bedrock-embedding-model.ts → amazon-bedrock-embedding-model.ts} +34 -27
  15. package/src/{bedrock-error.ts → amazon-bedrock-error.ts} +1 -1
  16. package/src/{bedrock-event-stream-decoder.ts → amazon-bedrock-event-stream-decoder.ts} +1 -1
  17. package/src/{bedrock-event-stream-response-handler.ts → amazon-bedrock-event-stream-response-handler.ts} +6 -6
  18. package/src/{bedrock-image-model.ts → amazon-bedrock-image-model.ts} +43 -39
  19. package/src/amazon-bedrock-image-settings.ts +9 -0
  20. package/src/{bedrock-prepare-tools.ts → amazon-bedrock-prepare-tools.ts} +22 -18
  21. package/src/{bedrock-provider.ts → amazon-bedrock-provider.ts} +53 -46
  22. package/src/amazon-bedrock-reasoning-metadata.ts +10 -0
  23. package/src/{bedrock-sigv4-fetch.ts → amazon-bedrock-sigv4-fetch.ts} +5 -3
  24. package/src/anthropic/amazon-bedrock-anthropic-fetch.ts +104 -0
  25. package/src/anthropic/{bedrock-anthropic-options.ts → amazon-bedrock-anthropic-options.ts} +1 -1
  26. package/src/anthropic/{bedrock-anthropic-provider.ts → amazon-bedrock-anthropic-provider.ts} +22 -20
  27. package/src/anthropic/index.ts +19 -7
  28. package/src/{convert-bedrock-usage.ts → convert-amazon-bedrock-usage.ts} +4 -4
  29. package/src/{convert-to-bedrock-chat-messages.ts → convert-to-amazon-bedrock-chat-messages.ts} +146 -103
  30. package/src/index.ts +15 -8
  31. package/src/inject-fetch-headers.ts +1 -1
  32. package/src/{map-bedrock-finish-reason.ts → map-amazon-bedrock-finish-reason.ts} +4 -4
  33. package/src/reranking/{bedrock-reranking-api.ts → amazon-bedrock-reranking-api.ts} +3 -3
  34. package/src/reranking/{bedrock-reranking-options.ts → amazon-bedrock-reranking-model-options.ts} +1 -1
  35. package/src/reranking/{bedrock-reranking-model.ts → amazon-bedrock-reranking-model.ts} +32 -25
  36. package/src/anthropic/bedrock-anthropic-fetch.ts +0 -94
  37. package/src/bedrock-api-types.ts +0 -219
  38. package/src/bedrock-image-settings.ts +0 -6
@@ -1,38 +1,41 @@
1
1
  import {
2
- JSONObject,
3
- LanguageModelV4Message,
4
- LanguageModelV4Prompt,
5
- SharedV4ProviderMetadata,
6
2
  UnsupportedFunctionalityError,
3
+ type JSONObject,
4
+ type LanguageModelV4Message,
5
+ type LanguageModelV4Prompt,
6
+ type SharedV4ProviderMetadata,
7
7
  } from '@ai-sdk/provider';
8
8
  import {
9
9
  convertToBase64,
10
- isProviderReference,
10
+ getTopLevelMediaType,
11
+ isFullMediaType,
11
12
  parseProviderOptions,
13
+ resolveFullMediaType,
12
14
  stripFileExtension,
13
15
  } from '@ai-sdk/provider-utils';
14
16
  import {
15
17
  BEDROCK_DOCUMENT_MIME_TYPES,
16
18
  BEDROCK_IMAGE_MIME_TYPES,
17
- BedrockAssistantMessage,
18
- BedrockCachePoint,
19
- BedrockDocumentFormat,
20
- BedrockDocumentMimeType,
21
- BedrockImageFormat,
22
- BedrockImageMimeType,
23
- BedrockMessages,
24
- BedrockSystemMessages,
25
- BedrockUserMessage,
26
- } from './bedrock-api-types';
27
- import { bedrockReasoningMetadataSchema } from './bedrock-chat-language-model';
28
- import { bedrockFilePartProviderOptions } from './bedrock-chat-options';
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';
29
31
  import { normalizeToolCallId } from './normalize-tool-call-id';
30
32
 
31
33
  function getCachePoint(
32
34
  providerMetadata: SharedV4ProviderMetadata | undefined,
33
- ): BedrockCachePoint | undefined {
34
- const cachePointConfig = providerMetadata?.bedrock?.cachePoint as
35
- | BedrockCachePoint['cachePoint']
35
+ ): AmazonBedrockCachePoint | undefined {
36
+ const cachePointConfig = (providerMetadata?.amazonBedrock?.cachePoint ??
37
+ providerMetadata?.bedrock?.cachePoint) as
38
+ | AmazonBedrockCachePoint['cachePoint']
36
39
  | undefined;
37
40
 
38
41
  if (!cachePointConfig) {
@@ -45,26 +48,32 @@ function getCachePoint(
45
48
  async function shouldEnableCitations(
46
49
  providerMetadata: SharedV4ProviderMetadata | undefined,
47
50
  ): Promise<boolean> {
48
- const bedrockOptions = await parseProviderOptions({
49
- provider: 'bedrock',
50
- providerOptions: providerMetadata,
51
- schema: bedrockFilePartProviderOptions,
52
- });
53
-
54
- return bedrockOptions?.citations?.enabled ?? false;
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;
55
64
  }
56
65
 
57
- export async function convertToBedrockChatMessages(
66
+ export async function convertToAmazonBedrockChatMessages(
58
67
  prompt: LanguageModelV4Prompt,
59
68
  isMistral: boolean = false,
60
69
  ): Promise<{
61
- system: BedrockSystemMessages;
62
- messages: BedrockMessages;
70
+ system: AmazonBedrockSystemMessages;
71
+ messages: AmazonBedrockMessages;
63
72
  }> {
64
73
  const blocks = groupIntoBlocks(prompt);
65
74
 
66
- let system: BedrockSystemMessages = [];
67
- const messages: BedrockMessages = [];
75
+ let system: AmazonBedrockSystemMessages = [];
76
+ const messages: AmazonBedrockMessages = [];
68
77
 
69
78
  let documentCounter = 0;
70
79
  const generateDocumentName = () => `document-${++documentCounter}`;
@@ -95,7 +104,7 @@ export async function convertToBedrockChatMessages(
95
104
 
96
105
  case 'user': {
97
106
  // combines all user and tool messages in this block into a single message:
98
- const bedrockContent: BedrockUserMessage['content'] = [];
107
+ const amazonBedrockContent: AmazonBedrockUserMessage['content'] = [];
99
108
 
100
109
  for (const message of block.messages) {
101
110
  const { role, content, providerOptions } = message;
@@ -106,58 +115,87 @@ export async function convertToBedrockChatMessages(
106
115
 
107
116
  switch (part.type) {
108
117
  case 'text': {
109
- bedrockContent.push({
118
+ amazonBedrockContent.push({
110
119
  text: part.text,
111
120
  });
112
121
  break;
113
122
  }
114
123
 
115
124
  case 'file': {
116
- if (isProviderReference(part.data)) {
117
- throw new UnsupportedFunctionalityError({
118
- functionality: 'file parts with provider references',
119
- });
120
- }
121
-
122
- if (part.data instanceof URL) {
123
- // The AI SDK automatically downloads files for user file parts with URLs
124
- throw new UnsupportedFunctionalityError({
125
- functionality: 'File URL data',
126
- });
127
- }
128
-
129
- if (part.mediaType.startsWith('image/')) {
130
- bedrockContent.push({
131
- image: {
132
- format: getBedrockImageFormat(part.mediaType),
133
- source: { bytes: convertToBase64(part.data) },
134
- },
135
- });
136
- } else {
137
- if (!part.mediaType) {
125
+ switch (part.data.type) {
126
+ case 'reference': {
138
127
  throw new UnsupportedFunctionalityError({
139
- functionality: 'file without mime type',
140
- message:
141
- 'File mime type is required in user message part content',
128
+ functionality: 'file parts with provider references',
142
129
  });
143
130
  }
131
+ case 'url': {
132
+ throw new UnsupportedFunctionalityError({
133
+ functionality: 'File URL data',
134
+ });
135
+ }
136
+ case 'text': {
137
+ const textMediaType = isFullMediaType(part.mediaType)
138
+ ? part.mediaType
139
+ : 'text/plain';
140
+ const enableCitations = await shouldEnableCitations(
141
+ part.providerOptions,
142
+ );
143
+
144
+ amazonBedrockContent.push({
145
+ document: {
146
+ format:
147
+ getAmazonBedrockDocumentFormat(textMediaType),
148
+ name: part.filename
149
+ ? stripFileExtension(part.filename)
150
+ : generateDocumentName(),
151
+ source: {
152
+ bytes: convertToBase64(
153
+ new TextEncoder().encode(part.data.text),
154
+ ),
155
+ },
156
+ ...(enableCitations && {
157
+ citations: { enabled: true },
158
+ }),
159
+ },
160
+ });
161
+ break;
162
+ }
163
+ case 'data': {
164
+ const fullMediaType = resolveFullMediaType({ part });
165
+
166
+ if (getTopLevelMediaType(fullMediaType) === 'image') {
167
+ amazonBedrockContent.push({
168
+ image: {
169
+ format:
170
+ getAmazonBedrockImageFormat(fullMediaType),
171
+ source: {
172
+ bytes: convertToBase64(part.data.data),
173
+ },
174
+ },
175
+ });
176
+ } else {
177
+ const enableCitations = await shouldEnableCitations(
178
+ part.providerOptions,
179
+ );
144
180
 
145
- const enableCitations = await shouldEnableCitations(
146
- part.providerOptions,
147
- );
148
-
149
- bedrockContent.push({
150
- document: {
151
- format: getBedrockDocumentFormat(part.mediaType),
152
- name: part.filename
153
- ? stripFileExtension(part.filename)
154
- : generateDocumentName(),
155
- source: { bytes: convertToBase64(part.data) },
156
- ...(enableCitations && {
157
- citations: { enabled: true },
158
- }),
159
- },
160
- });
181
+ amazonBedrockContent.push({
182
+ document: {
183
+ format:
184
+ getAmazonBedrockDocumentFormat(fullMediaType),
185
+ name: part.filename
186
+ ? stripFileExtension(part.filename)
187
+ : generateDocumentName(),
188
+ source: {
189
+ bytes: convertToBase64(part.data.data),
190
+ },
191
+ ...(enableCitations && {
192
+ citations: { enabled: true },
193
+ }),
194
+ },
195
+ });
196
+ }
197
+ break;
198
+ }
161
199
  }
162
200
 
163
201
  break;
@@ -188,7 +226,7 @@ export async function convertToBedrockChatMessages(
188
226
  });
189
227
  }
190
228
 
191
- const format = getBedrockImageFormat(
229
+ const format = getAmazonBedrockImageFormat(
192
230
  contentPart.mediaType,
193
231
  );
194
232
 
@@ -225,7 +263,7 @@ export async function convertToBedrockChatMessages(
225
263
  break;
226
264
  }
227
265
 
228
- bedrockContent.push({
266
+ amazonBedrockContent.push({
229
267
  toolResult: {
230
268
  toolUseId: normalizeToolCallId(part.toolCallId, isMistral),
231
269
  content: toolResultContent,
@@ -243,18 +281,19 @@ export async function convertToBedrockChatMessages(
243
281
 
244
282
  const cachePoint = getCachePoint(providerOptions);
245
283
  if (cachePoint) {
246
- bedrockContent.push(cachePoint);
284
+ amazonBedrockContent.push(cachePoint);
247
285
  }
248
286
  }
249
287
 
250
- messages.push({ role: 'user', content: bedrockContent });
288
+ messages.push({ role: 'user', content: amazonBedrockContent });
251
289
 
252
290
  break;
253
291
  }
254
292
 
255
293
  case 'assistant': {
256
294
  // combines multiple assistant messages in this block into a single message:
257
- const bedrockContent: BedrockAssistantMessage['content'] = [];
295
+ const amazonBedrockContent: AmazonBedrockAssistantMessage['content'] =
296
+ [];
258
297
 
259
298
  for (let j = 0; j < block.messages.length; j++) {
260
299
  const message = block.messages[j];
@@ -275,7 +314,7 @@ export async function convertToBedrockChatMessages(
275
314
  break;
276
315
  }
277
316
 
278
- bedrockContent.push({
317
+ amazonBedrockContent.push({
279
318
  text:
280
319
  // trim the last text part if it's the last message in the block
281
320
  // because Bedrock does not allow trailing whitespace
@@ -291,16 +330,22 @@ export async function convertToBedrockChatMessages(
291
330
  }
292
331
 
293
332
  case 'reasoning': {
294
- const reasoningMetadata = await parseProviderOptions({
295
- provider: 'bedrock',
296
- providerOptions: part.providerOptions,
297
- schema: bedrockReasoningMetadataSchema,
298
- });
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
+ }));
299
344
 
300
345
  if (reasoningMetadata?.signature != null) {
301
346
  // do not trim reasoning text when a signature is present:
302
347
  // the signature validates the exact original bytes
303
- bedrockContent.push({
348
+ amazonBedrockContent.push({
304
349
  reasoningContent: {
305
350
  reasoningText: {
306
351
  text: part.text,
@@ -309,7 +354,7 @@ export async function convertToBedrockChatMessages(
309
354
  },
310
355
  });
311
356
  } else if (reasoningMetadata?.redactedData != null) {
312
- bedrockContent.push({
357
+ amazonBedrockContent.push({
313
358
  reasoningContent: {
314
359
  redactedReasoning: {
315
360
  data: reasoningMetadata.redactedData,
@@ -320,7 +365,7 @@ export async function convertToBedrockChatMessages(
320
365
  // trim the last text part if it's the last message in the block
321
366
  // because Bedrock does not allow trailing whitespace
322
367
  // in pre-filled assistant responses
323
- bedrockContent.push({
368
+ amazonBedrockContent.push({
324
369
  reasoningContent: {
325
370
  reasoningText: {
326
371
  text: trimIfLast(
@@ -338,7 +383,7 @@ export async function convertToBedrockChatMessages(
338
383
  }
339
384
 
340
385
  case 'tool-call': {
341
- bedrockContent.push({
386
+ amazonBedrockContent.push({
342
387
  toolUse: {
343
388
  toolUseId: normalizeToolCallId(part.toolCallId, isMistral),
344
389
  name: part.toolName,
@@ -351,11 +396,11 @@ export async function convertToBedrockChatMessages(
351
396
  }
352
397
  const cachePoint = getCachePoint(message.providerOptions);
353
398
  if (cachePoint) {
354
- bedrockContent.push(cachePoint);
399
+ amazonBedrockContent.push(cachePoint);
355
400
  }
356
401
  }
357
402
 
358
- messages.push({ role: 'assistant', content: bedrockContent });
403
+ messages.push({ role: 'assistant', content: amazonBedrockContent });
359
404
 
360
405
  break;
361
406
  }
@@ -370,15 +415,11 @@ export async function convertToBedrockChatMessages(
370
415
  return { system, messages };
371
416
  }
372
417
 
373
- function getBedrockImageFormat(mimeType?: string): BedrockImageFormat {
374
- if (!mimeType) {
375
- throw new UnsupportedFunctionalityError({
376
- functionality: 'image without mime type',
377
- message: 'Image mime type is required in user message part content',
378
- });
379
- }
380
-
381
- const format = BEDROCK_IMAGE_MIME_TYPES[mimeType as BedrockImageMimeType];
418
+ function getAmazonBedrockImageFormat(
419
+ mimeType: string,
420
+ ): AmazonBedrockImageFormat {
421
+ const format =
422
+ BEDROCK_IMAGE_MIME_TYPES[mimeType as AmazonBedrockImageMimeType];
382
423
  if (!format) {
383
424
  throw new UnsupportedFunctionalityError({
384
425
  functionality: `image mime type: ${mimeType}`,
@@ -389,9 +430,11 @@ function getBedrockImageFormat(mimeType?: string): BedrockImageFormat {
389
430
  return format;
390
431
  }
391
432
 
392
- function getBedrockDocumentFormat(mimeType: string): BedrockDocumentFormat {
433
+ function getAmazonBedrockDocumentFormat(
434
+ mimeType: string,
435
+ ): AmazonBedrockDocumentFormat {
393
436
  const format =
394
- BEDROCK_DOCUMENT_MIME_TYPES[mimeType as BedrockDocumentMimeType];
437
+ BEDROCK_DOCUMENT_MIME_TYPES[mimeType as AmazonBedrockDocumentMimeType];
395
438
  if (!format) {
396
439
  throw new UnsupportedFunctionalityError({
397
440
  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
- AmazonBedrockLanguageModelOptions,
6
- /** @deprecated Use `AmazonBedrockLanguageModelOptions` instead. */
7
- AmazonBedrockLanguageModelOptions as BedrockProviderOptions,
8
- } from './bedrock-chat-options';
9
- export { bedrock, createAmazonBedrock } from './bedrock-provider';
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';
@@ -1,8 +1,8 @@
1
1
  import {
2
- type FetchFunction,
3
2
  getRuntimeEnvironmentUserAgent,
4
3
  normalizeHeaders,
5
4
  withUserAgentSuffix,
5
+ type FetchFunction,
6
6
  } from '@ai-sdk/provider-utils';
7
7
  import { VERSION } from './version';
8
8
 
@@ -1,8 +1,8 @@
1
- import { LanguageModelV4FinishReason } from '@ai-sdk/provider';
2
- import { BedrockStopReason } from './bedrock-api-types';
1
+ import type { LanguageModelV4FinishReason } from '@ai-sdk/provider';
2
+ import type { AmazonBedrockStopReason } from './amazon-bedrock-api-types';
3
3
 
4
- export function mapBedrockFinishReason(
5
- finishReason: BedrockStopReason,
4
+ export function mapAmazonBedrockFinishReason(
5
+ finishReason: AmazonBedrockStopReason,
6
6
  isJsonResponseFromTool?: boolean,
7
7
  ): LanguageModelV4FinishReason['unified'] {
8
8
  switch (finishReason) {
@@ -2,12 +2,12 @@ import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
2
2
  import { z } from 'zod/v4';
3
3
 
4
4
  // https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Rerank.html
5
- export type BedrockRerankingInput = {
5
+ export type AmazonBedrockRerankingInput = {
6
6
  nextToken?: string;
7
7
  queries: [{ type: 'TEXT'; textQuery: { text: string } }];
8
8
  rerankingConfiguration: {
9
9
  type: 'BEDROCK_RERANKING_MODEL';
10
- bedrockRerankingConfiguration: {
10
+ amazonBedrockRerankingConfiguration: {
11
11
  modelConfiguration: {
12
12
  modelArn: string;
13
13
  additionalModelRequestFields?: Record<string, unknown>;
@@ -29,7 +29,7 @@ export type BedrockRerankingInput = {
29
29
  }[];
30
30
  };
31
31
 
32
- export const bedrockRerankingResponseSchema = lazySchema(() =>
32
+ export const amazonBedrockRerankingResponseSchema = lazySchema(() =>
33
33
  zodSchema(
34
34
  z.object({
35
35
  results: z.array(
@@ -2,7 +2,7 @@ import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
2
2
  import { z } from 'zod/v4';
3
3
 
4
4
  // https://docs.aws.amazon.com/bedrock/latest/userguide/rerank-supported.html
5
- export type BedrockRerankingModelId =
5
+ export type AmazonBedrockRerankingModelId =
6
6
  | 'amazon.rerank-v1:0'
7
7
  | 'cohere.rerank-v3-5:0'
8
8
  | (string & {});
@@ -1,25 +1,24 @@
1
- import { RerankingModelV4 } from '@ai-sdk/provider';
1
+ import type { RerankingModelV4 } from '@ai-sdk/provider';
2
2
  import {
3
- FetchFunction,
4
- Resolvable,
5
3
  combineHeaders,
6
4
  createJsonErrorResponseHandler,
7
5
  createJsonResponseHandler,
8
6
  parseProviderOptions,
9
7
  postJsonToApi,
10
8
  resolve,
9
+ type FetchFunction,
10
+ type Resolvable,
11
11
  } from '@ai-sdk/provider-utils';
12
- import { BedrockErrorSchema } from '../bedrock-error';
12
+ import { AmazonBedrockErrorSchema } from '../amazon-bedrock-error';
13
13
  import {
14
- BedrockRerankingInput,
15
- bedrockRerankingResponseSchema,
16
- } from './bedrock-reranking-api';
14
+ amazonBedrockRerankingResponseSchema,
15
+ type AmazonBedrockRerankingInput,
16
+ } from './amazon-bedrock-reranking-api';
17
17
  import {
18
- BedrockRerankingModelId,
19
18
  amazonBedrockRerankingModelOptionsSchema,
20
- } from './bedrock-reranking-options';
21
-
22
- type BedrockRerankingConfig = {
19
+ type AmazonBedrockRerankingModelId,
20
+ } from './amazon-bedrock-reranking-model-options';
21
+ type AmazonBedrockRerankingConfig = {
23
22
  baseUrl: () => string;
24
23
  region: string;
25
24
  headers: Resolvable<Record<string, string | undefined>>;
@@ -28,13 +27,13 @@ type BedrockRerankingConfig = {
28
27
 
29
28
  type DoRerankResponse = Awaited<ReturnType<RerankingModelV4['doRerank']>>;
30
29
 
31
- export class BedrockRerankingModel implements RerankingModelV4 {
30
+ export class AmazonBedrockRerankingModel implements RerankingModelV4 {
32
31
  readonly specificationVersion = 'v4';
33
32
  readonly provider = 'amazon-bedrock';
34
33
 
35
34
  constructor(
36
- readonly modelId: BedrockRerankingModelId,
37
- private readonly config: BedrockRerankingConfig,
35
+ readonly modelId: AmazonBedrockRerankingModelId,
36
+ private readonly config: AmazonBedrockRerankingConfig,
38
37
  ) {}
39
38
 
40
39
  async doRerank({
@@ -45,11 +44,19 @@ export class BedrockRerankingModel implements RerankingModelV4 {
45
44
  abortSignal,
46
45
  providerOptions,
47
46
  }: Parameters<RerankingModelV4['doRerank']>[0]): Promise<DoRerankResponse> {
48
- const bedrockOptions = await parseProviderOptions({
49
- provider: 'bedrock',
50
- providerOptions,
51
- schema: amazonBedrockRerankingModelOptionsSchema,
52
- });
47
+ // Prefer `amazonBedrock`; fall back to legacy `bedrock` for backward
48
+ // compatibility.
49
+ const amazonBedrockOptions =
50
+ (await parseProviderOptions({
51
+ provider: 'amazonBedrock',
52
+ providerOptions,
53
+ schema: amazonBedrockRerankingModelOptionsSchema,
54
+ })) ??
55
+ (await parseProviderOptions({
56
+ provider: 'bedrock',
57
+ providerOptions,
58
+ schema: amazonBedrockRerankingModelOptionsSchema,
59
+ }));
53
60
 
54
61
  const {
55
62
  value: response,
@@ -61,7 +68,7 @@ export class BedrockRerankingModel implements RerankingModelV4 {
61
68
  combineHeaders(await resolve(this.config.headers), headers),
62
69
  ),
63
70
  body: {
64
- nextToken: bedrockOptions?.nextToken,
71
+ nextToken: amazonBedrockOptions?.nextToken,
65
72
  queries: [
66
73
  {
67
74
  textQuery: { text: query },
@@ -69,11 +76,11 @@ export class BedrockRerankingModel implements RerankingModelV4 {
69
76
  },
70
77
  ],
71
78
  rerankingConfiguration: {
72
- bedrockRerankingConfiguration: {
79
+ amazonBedrockRerankingConfiguration: {
73
80
  modelConfiguration: {
74
81
  modelArn: `arn:aws:bedrock:${this.config.region}::foundation-model/${this.modelId}`,
75
82
  additionalModelRequestFields:
76
- bedrockOptions?.additionalModelRequestFields,
83
+ amazonBedrockOptions?.additionalModelRequestFields,
77
84
  },
78
85
  numberOfResults: topN,
79
86
  },
@@ -92,13 +99,13 @@ export class BedrockRerankingModel implements RerankingModelV4 {
92
99
  jsonDocument: value,
93
100
  },
94
101
  })),
95
- } satisfies BedrockRerankingInput,
102
+ } satisfies AmazonBedrockRerankingInput,
96
103
  failedResponseHandler: createJsonErrorResponseHandler({
97
- errorSchema: BedrockErrorSchema,
104
+ errorSchema: AmazonBedrockErrorSchema,
98
105
  errorToMessage: error => `${error.type}: ${error.message}`,
99
106
  }),
100
107
  successfulResponseHandler: createJsonResponseHandler(
101
- bedrockRerankingResponseSchema,
108
+ amazonBedrockRerankingResponseSchema,
102
109
  ),
103
110
  fetch: this.config.fetch,
104
111
  abortSignal,