@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.
Files changed (37) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/anthropic/index.d.ts +10 -10
  3. package/dist/anthropic/index.js +65 -51
  4. package/dist/anthropic/index.js.map +1 -1
  5. package/dist/index.d.ts +21 -21
  6. package/dist/index.js +338 -298
  7. package/dist/index.js.map +1 -1
  8. package/docs/08-amazon-bedrock.mdx +66 -66
  9. package/package.json +3 -3
  10. package/src/{bedrock-api-types.ts → amazon-bedrock-api-types.ts} +60 -55
  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} +211 -190
  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} +31 -24
  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} +3 -3
  18. package/src/{bedrock-image-model.ts → amazon-bedrock-image-model.ts} +40 -36
  19. package/src/amazon-bedrock-image-settings.ts +9 -0
  20. package/src/{bedrock-prepare-tools.ts → amazon-bedrock-prepare-tools.ts} +18 -17
  21. package/src/{bedrock-provider.ts → amazon-bedrock-provider.ts} +40 -38
  22. package/src/amazon-bedrock-reasoning-metadata.ts +10 -0
  23. package/src/{bedrock-sigv4-fetch.ts → amazon-bedrock-sigv4-fetch.ts} +13 -7
  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} +18 -16
  27. package/src/anthropic/index.ts +19 -7
  28. package/src/{convert-bedrock-usage.ts → convert-amazon-bedrock-usage.ts} +3 -3
  29. package/src/{convert-to-bedrock-chat-messages.ts → convert-to-amazon-bedrock-chat-messages.ts} +92 -59
  30. package/src/index.ts +15 -8
  31. package/src/{map-bedrock-finish-reason.ts → map-amazon-bedrock-finish-reason.ts} +3 -3
  32. package/src/reranking/{bedrock-reranking-api.ts → amazon-bedrock-reranking-api.ts} +3 -3
  33. package/src/reranking/{bedrock-reranking-options.ts → amazon-bedrock-reranking-model-options.ts} +1 -1
  34. package/src/reranking/{bedrock-reranking-model.ts → amazon-bedrock-reranking-model.ts} +29 -21
  35. package/src/anthropic/bedrock-anthropic-fetch.ts +0 -94
  36. package/src/bedrock-image-settings.ts +0 -6
  37. package/src/bedrock-reasoning-metadata.ts +0 -10
@@ -1,8 +1,8 @@
1
1
  import type { LanguageModelV4FinishReason } from '@ai-sdk/provider';
2
- import type { BedrockStopReason } from './bedrock-api-types';
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 & {});
@@ -9,16 +9,16 @@ import {
9
9
  type FetchFunction,
10
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
- bedrockRerankingResponseSchema,
15
- type BedrockRerankingInput,
16
- } from './bedrock-reranking-api';
14
+ amazonBedrockRerankingResponseSchema,
15
+ type AmazonBedrockRerankingInput,
16
+ } from './amazon-bedrock-reranking-api';
17
17
  import {
18
18
  amazonBedrockRerankingModelOptionsSchema,
19
- type BedrockRerankingModelId,
20
- } from './bedrock-reranking-options';
21
- type BedrockRerankingConfig = {
19
+ type AmazonBedrockRerankingModelId,
20
+ } from './amazon-bedrock-reranking-model-options';
21
+ type AmazonBedrockRerankingConfig = {
22
22
  baseUrl: () => string;
23
23
  region: string;
24
24
  headers: Resolvable<Record<string, string | undefined>>;
@@ -27,13 +27,13 @@ type BedrockRerankingConfig = {
27
27
 
28
28
  type DoRerankResponse = Awaited<ReturnType<RerankingModelV4['doRerank']>>;
29
29
 
30
- export class BedrockRerankingModel implements RerankingModelV4 {
30
+ export class AmazonBedrockRerankingModel implements RerankingModelV4 {
31
31
  readonly specificationVersion = 'v4';
32
32
  readonly provider = 'amazon-bedrock';
33
33
 
34
34
  constructor(
35
- readonly modelId: BedrockRerankingModelId,
36
- private readonly config: BedrockRerankingConfig,
35
+ readonly modelId: AmazonBedrockRerankingModelId,
36
+ private readonly config: AmazonBedrockRerankingConfig,
37
37
  ) {}
38
38
 
39
39
  async doRerank({
@@ -44,11 +44,19 @@ export class BedrockRerankingModel implements RerankingModelV4 {
44
44
  abortSignal,
45
45
  providerOptions,
46
46
  }: Parameters<RerankingModelV4['doRerank']>[0]): Promise<DoRerankResponse> {
47
- const bedrockOptions = await parseProviderOptions({
48
- provider: 'bedrock',
49
- providerOptions,
50
- schema: amazonBedrockRerankingModelOptionsSchema,
51
- });
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
+ }));
52
60
 
53
61
  const {
54
62
  value: response,
@@ -60,7 +68,7 @@ export class BedrockRerankingModel implements RerankingModelV4 {
60
68
  combineHeaders(await resolve(this.config.headers), headers),
61
69
  ),
62
70
  body: {
63
- nextToken: bedrockOptions?.nextToken,
71
+ nextToken: amazonBedrockOptions?.nextToken,
64
72
  queries: [
65
73
  {
66
74
  textQuery: { text: query },
@@ -68,11 +76,11 @@ export class BedrockRerankingModel implements RerankingModelV4 {
68
76
  },
69
77
  ],
70
78
  rerankingConfiguration: {
71
- bedrockRerankingConfiguration: {
79
+ amazonBedrockRerankingConfiguration: {
72
80
  modelConfiguration: {
73
81
  modelArn: `arn:aws:bedrock:${this.config.region}::foundation-model/${this.modelId}`,
74
82
  additionalModelRequestFields:
75
- bedrockOptions?.additionalModelRequestFields,
83
+ amazonBedrockOptions?.additionalModelRequestFields,
76
84
  },
77
85
  numberOfResults: topN,
78
86
  },
@@ -91,13 +99,13 @@ export class BedrockRerankingModel implements RerankingModelV4 {
91
99
  jsonDocument: value,
92
100
  },
93
101
  })),
94
- } satisfies BedrockRerankingInput,
102
+ } satisfies AmazonBedrockRerankingInput,
95
103
  failedResponseHandler: createJsonErrorResponseHandler({
96
- errorSchema: BedrockErrorSchema,
104
+ errorSchema: AmazonBedrockErrorSchema,
97
105
  errorToMessage: error => `${error.type}: ${error.message}`,
98
106
  }),
99
107
  successfulResponseHandler: createJsonResponseHandler(
100
- bedrockRerankingResponseSchema,
108
+ amazonBedrockRerankingResponseSchema,
101
109
  ),
102
110
  fetch: this.config.fetch,
103
111
  abortSignal,
@@ -1,94 +0,0 @@
1
- import {
2
- convertBase64ToUint8Array,
3
- safeParseJSON,
4
- type FetchFunction,
5
- } from '@ai-sdk/provider-utils';
6
- import { z } from 'zod/v4';
7
- import { createBedrockEventStreamDecoder } from '../bedrock-event-stream-decoder';
8
-
9
- const bedrockErrorSchema = z.looseObject({
10
- message: z.string().optional(),
11
- });
12
-
13
- export function createBedrockAnthropicFetch(
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({ text, schema: bedrockErrorSchema });
24
-
25
- const message =
26
- parsed.success && parsed.value.message ? parsed.value.message : text;
27
-
28
- const anthropicError = JSON.stringify({
29
- type: 'error',
30
- error: { type: 'error', message },
31
- });
32
-
33
- return new Response(anthropicError, {
34
- status: response.status,
35
- statusText: response.statusText,
36
- headers: response.headers,
37
- });
38
- }
39
-
40
- const contentType = response.headers.get('content-type');
41
- if (
42
- contentType?.includes('application/vnd.amazon.eventstream') &&
43
- response.body != null
44
- ) {
45
- const transformedBody = transformBedrockEventStreamToSSE(response.body);
46
-
47
- return new Response(transformedBody, {
48
- status: response.status,
49
- statusText: response.statusText,
50
- headers: new Headers({
51
- ...Object.fromEntries(response.headers.entries()),
52
- 'content-type': 'text/event-stream',
53
- }),
54
- });
55
- }
56
-
57
- return response;
58
- };
59
- }
60
-
61
- function transformBedrockEventStreamToSSE(
62
- body: ReadableStream<Uint8Array>,
63
- ): ReadableStream<Uint8Array> {
64
- const textEncoder = new TextEncoder();
65
-
66
- return createBedrockEventStreamDecoder(body, async (event, controller) => {
67
- if (event.messageType === 'event') {
68
- if (event.eventType === 'chunk') {
69
- const parsed = await safeParseJSON({ text: event.data });
70
- if (!parsed.success) {
71
- controller.enqueue(textEncoder.encode(`data: ${event.data}\n\n`));
72
- return;
73
- }
74
- const bytes = (parsed.value as { bytes?: string }).bytes;
75
- if (bytes) {
76
- const anthropicEvent = new TextDecoder().decode(
77
- convertBase64ToUint8Array(bytes),
78
- );
79
- controller.enqueue(textEncoder.encode(`data: ${anthropicEvent}\n\n`));
80
- } else {
81
- controller.enqueue(textEncoder.encode(`data: ${event.data}\n\n`));
82
- }
83
- } else if (event.eventType === 'messageStop') {
84
- controller.enqueue(textEncoder.encode('data: [DONE]\n\n'));
85
- }
86
- } else if (event.messageType === 'exception') {
87
- controller.enqueue(
88
- textEncoder.encode(
89
- `data: ${JSON.stringify({ type: 'error', error: event.data })}\n\n`,
90
- ),
91
- );
92
- }
93
- });
94
- }
@@ -1,6 +0,0 @@
1
- export type BedrockImageModelId = 'amazon.nova-canvas-v1:0' | (string & {});
2
-
3
- // https://docs.aws.amazon.com/nova/latest/userguide/image-gen-req-resp-structure.html
4
- export const modelMaxImagesPerCall: Record<BedrockImageModelId, number> = {
5
- 'amazon.nova-canvas-v1:0': 5,
6
- };
@@ -1,10 +0,0 @@
1
- import { z } from 'zod/v4';
2
-
3
- export const bedrockReasoningMetadataSchema = z.object({
4
- signature: z.string().optional(),
5
- redactedData: z.string().optional(),
6
- });
7
-
8
- export type BedrockReasoningMetadata = z.infer<
9
- typeof bedrockReasoningMetadataSchema
10
- >;