@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
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { LanguageModelV4FinishReason } from '@ai-sdk/provider';
|
|
2
|
-
import type {
|
|
2
|
+
import type { AmazonBedrockStopReason } from './amazon-bedrock-api-types';
|
|
3
3
|
|
|
4
|
-
export function
|
|
5
|
-
finishReason:
|
|
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
|
|
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
|
-
|
|
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
|
|
32
|
+
export const amazonBedrockRerankingResponseSchema = lazySchema(() =>
|
|
33
33
|
zodSchema(
|
|
34
34
|
z.object({
|
|
35
35
|
results: z.array(
|
package/src/reranking/{bedrock-reranking-options.ts → amazon-bedrock-reranking-model-options.ts}
RENAMED
|
@@ -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
|
|
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 {
|
|
12
|
+
import { AmazonBedrockErrorSchema } from '../amazon-bedrock-error';
|
|
13
13
|
import {
|
|
14
|
-
|
|
15
|
-
type
|
|
16
|
-
} from './bedrock-reranking-api';
|
|
14
|
+
amazonBedrockRerankingResponseSchema,
|
|
15
|
+
type AmazonBedrockRerankingInput,
|
|
16
|
+
} from './amazon-bedrock-reranking-api';
|
|
17
17
|
import {
|
|
18
18
|
amazonBedrockRerankingModelOptionsSchema,
|
|
19
|
-
type
|
|
20
|
-
} from './bedrock-reranking-options';
|
|
21
|
-
type
|
|
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
|
|
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:
|
|
36
|
-
private readonly config:
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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:
|
|
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
|
-
|
|
79
|
+
amazonBedrockRerankingConfiguration: {
|
|
72
80
|
modelConfiguration: {
|
|
73
81
|
modelArn: `arn:aws:bedrock:${this.config.region}::foundation-model/${this.modelId}`,
|
|
74
82
|
additionalModelRequestFields:
|
|
75
|
-
|
|
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
|
|
102
|
+
} satisfies AmazonBedrockRerankingInput,
|
|
95
103
|
failedResponseHandler: createJsonErrorResponseHandler({
|
|
96
|
-
errorSchema:
|
|
104
|
+
errorSchema: AmazonBedrockErrorSchema,
|
|
97
105
|
errorToMessage: error => `${error.type}: ${error.message}`,
|
|
98
106
|
}),
|
|
99
107
|
successfulResponseHandler: createJsonResponseHandler(
|
|
100
|
-
|
|
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
|
-
>;
|