@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.
- package/CHANGELOG.md +29 -0
- package/dist/anthropic/index.d.ts +10 -10
- package/dist/anthropic/index.js +58 -46
- package/dist/anthropic/index.js.map +1 -1
- package/dist/index.d.ts +25 -23
- package/dist/index.js +496 -436
- package/dist/index.js.map +1 -1
- package/docs/08-amazon-bedrock.mdx +80 -82
- package/package.json +5 -5
- package/src/amazon-bedrock-api-types.ts +224 -0
- 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} +217 -196
- 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} +34 -27
- 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} +43 -39
- 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} +5 -3
- 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} +22 -20
- package/src/anthropic/index.ts +19 -7
- package/src/{convert-bedrock-usage.ts → convert-amazon-bedrock-usage.ts} +4 -4
- package/src/{convert-to-bedrock-chat-messages.ts → convert-to-amazon-bedrock-chat-messages.ts} +146 -103
- package/src/index.ts +15 -8
- package/src/inject-fetch-headers.ts +1 -1
- 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/src/anthropic/bedrock-anthropic-fetch.ts +0 -94
- package/src/bedrock-api-types.ts +0 -219
- package/src/bedrock-image-settings.ts +0 -6
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
|
-
EmbeddingModelV4,
|
|
3
2
|
TooManyEmbeddingValuesForCallError,
|
|
3
|
+
type EmbeddingModelV4,
|
|
4
4
|
} from '@ai-sdk/provider';
|
|
5
5
|
import {
|
|
6
|
-
FetchFunction,
|
|
7
|
-
Resolvable,
|
|
8
6
|
combineHeaders,
|
|
9
7
|
createJsonErrorResponseHandler,
|
|
10
8
|
createJsonResponseHandler,
|
|
@@ -14,15 +12,17 @@ import {
|
|
|
14
12
|
serializeModelOptions,
|
|
15
13
|
WORKFLOW_SERIALIZE,
|
|
16
14
|
WORKFLOW_DESERIALIZE,
|
|
15
|
+
type FetchFunction,
|
|
16
|
+
type Resolvable,
|
|
17
17
|
} from '@ai-sdk/provider-utils';
|
|
18
18
|
import {
|
|
19
|
-
BedrockEmbeddingModelId,
|
|
20
19
|
amazonBedrockEmbeddingModelOptionsSchema,
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
type AmazonBedrockEmbeddingModelId,
|
|
21
|
+
} from './amazon-bedrock-embedding-model-options';
|
|
22
|
+
import { AmazonBedrockErrorSchema } from './amazon-bedrock-error';
|
|
23
23
|
import { z } from 'zod/v4';
|
|
24
24
|
|
|
25
|
-
type
|
|
25
|
+
type AmazonBedrockEmbeddingConfig = {
|
|
26
26
|
baseUrl: () => string;
|
|
27
27
|
headers?: Resolvable<Record<string, string | undefined>>;
|
|
28
28
|
fetch?: FetchFunction;
|
|
@@ -30,13 +30,13 @@ type BedrockEmbeddingConfig = {
|
|
|
30
30
|
|
|
31
31
|
type DoEmbedResponse = Awaited<ReturnType<EmbeddingModelV4['doEmbed']>>;
|
|
32
32
|
|
|
33
|
-
export class
|
|
33
|
+
export class AmazonBedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
34
34
|
readonly specificationVersion = 'v4';
|
|
35
35
|
readonly provider = 'amazon-bedrock';
|
|
36
36
|
readonly maxEmbeddingsPerCall = 1;
|
|
37
37
|
readonly supportsParallelCalls = true;
|
|
38
38
|
|
|
39
|
-
static [WORKFLOW_SERIALIZE](model:
|
|
39
|
+
static [WORKFLOW_SERIALIZE](model: AmazonBedrockEmbeddingModel) {
|
|
40
40
|
return serializeModelOptions({
|
|
41
41
|
modelId: model.modelId,
|
|
42
42
|
config: model.config,
|
|
@@ -45,14 +45,14 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
|
45
45
|
|
|
46
46
|
static [WORKFLOW_DESERIALIZE](options: {
|
|
47
47
|
modelId: string;
|
|
48
|
-
config:
|
|
48
|
+
config: AmazonBedrockEmbeddingConfig;
|
|
49
49
|
}) {
|
|
50
|
-
return new
|
|
50
|
+
return new AmazonBedrockEmbeddingModel(options.modelId, options.config);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
constructor(
|
|
54
|
-
readonly modelId:
|
|
55
|
-
private readonly config:
|
|
54
|
+
readonly modelId: AmazonBedrockEmbeddingModelId,
|
|
55
|
+
private readonly config: AmazonBedrockEmbeddingConfig,
|
|
56
56
|
) {}
|
|
57
57
|
|
|
58
58
|
private getUrl(modelId: string): string {
|
|
@@ -75,13 +75,20 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
// Parse provider options
|
|
79
|
-
|
|
78
|
+
// Parse provider options. Prefer `amazonBedrock`; fall back to legacy
|
|
79
|
+
// `bedrock` key for backward compatibility.
|
|
80
|
+
const amazonBedrockOptions =
|
|
81
|
+
(await parseProviderOptions({
|
|
82
|
+
provider: 'amazonBedrock',
|
|
83
|
+
providerOptions,
|
|
84
|
+
schema: amazonBedrockEmbeddingModelOptionsSchema,
|
|
85
|
+
})) ??
|
|
80
86
|
(await parseProviderOptions({
|
|
81
87
|
provider: 'bedrock',
|
|
82
88
|
providerOptions,
|
|
83
89
|
schema: amazonBedrockEmbeddingModelOptionsSchema,
|
|
84
|
-
})) ??
|
|
90
|
+
})) ??
|
|
91
|
+
{};
|
|
85
92
|
|
|
86
93
|
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html
|
|
87
94
|
//
|
|
@@ -97,10 +104,10 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
|
97
104
|
taskType: 'SINGLE_EMBEDDING',
|
|
98
105
|
singleEmbeddingParams: {
|
|
99
106
|
embeddingPurpose:
|
|
100
|
-
|
|
101
|
-
embeddingDimension:
|
|
107
|
+
amazonBedrockOptions.embeddingPurpose ?? 'GENERIC_INDEX',
|
|
108
|
+
embeddingDimension: amazonBedrockOptions.embeddingDimension ?? 1024,
|
|
102
109
|
text: {
|
|
103
|
-
truncationMode:
|
|
110
|
+
truncationMode: amazonBedrockOptions.truncate ?? 'END',
|
|
104
111
|
value: values[0],
|
|
105
112
|
},
|
|
106
113
|
},
|
|
@@ -109,15 +116,15 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
|
109
116
|
? {
|
|
110
117
|
// Cohere embedding models on Bedrock require `input_type`.
|
|
111
118
|
// Without it, the service attempts other schema branches and rejects the request.
|
|
112
|
-
input_type:
|
|
119
|
+
input_type: amazonBedrockOptions.inputType ?? 'search_query',
|
|
113
120
|
texts: [values[0]],
|
|
114
|
-
truncate:
|
|
115
|
-
output_dimension:
|
|
121
|
+
truncate: amazonBedrockOptions.truncate,
|
|
122
|
+
output_dimension: amazonBedrockOptions.outputDimension,
|
|
116
123
|
}
|
|
117
124
|
: {
|
|
118
125
|
inputText: values[0],
|
|
119
|
-
dimensions:
|
|
120
|
-
normalize:
|
|
126
|
+
dimensions: amazonBedrockOptions.dimensions,
|
|
127
|
+
normalize: amazonBedrockOptions.normalize,
|
|
121
128
|
};
|
|
122
129
|
|
|
123
130
|
const url = this.getUrl(this.modelId);
|
|
@@ -131,11 +138,11 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
|
131
138
|
),
|
|
132
139
|
body: args,
|
|
133
140
|
failedResponseHandler: createJsonErrorResponseHandler({
|
|
134
|
-
errorSchema:
|
|
141
|
+
errorSchema: AmazonBedrockErrorSchema,
|
|
135
142
|
errorToMessage: error => `${error.type}: ${error.message}`,
|
|
136
143
|
}),
|
|
137
144
|
successfulResponseHandler: createJsonResponseHandler(
|
|
138
|
-
|
|
145
|
+
AmazonBedrockEmbeddingResponseSchema,
|
|
139
146
|
),
|
|
140
147
|
fetch: this.config.fetch,
|
|
141
148
|
abortSignal,
|
|
@@ -180,7 +187,7 @@ export class BedrockEmbeddingModel implements EmbeddingModelV4 {
|
|
|
180
187
|
}
|
|
181
188
|
}
|
|
182
189
|
|
|
183
|
-
const
|
|
190
|
+
const AmazonBedrockEmbeddingResponseSchema = z.union([
|
|
184
191
|
// Titan-style response
|
|
185
192
|
z.object({
|
|
186
193
|
embedding: z.array(z.number()),
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { EmptyResponseBodyError } from '@ai-sdk/provider';
|
|
2
2
|
import {
|
|
3
|
-
ParseResult,
|
|
4
3
|
safeParseJSON,
|
|
5
4
|
extractResponseHeaders,
|
|
6
|
-
ResponseHandler,
|
|
7
5
|
safeValidateTypes,
|
|
6
|
+
type ParseResult,
|
|
7
|
+
type ResponseHandler,
|
|
8
8
|
} from '@ai-sdk/provider-utils';
|
|
9
|
-
import { ZodType } from 'zod/v4';
|
|
10
|
-
import {
|
|
9
|
+
import type { ZodType } from 'zod/v4';
|
|
10
|
+
import { createAmazonBedrockEventStreamDecoder } from './amazon-bedrock-event-stream-decoder';
|
|
11
11
|
|
|
12
|
-
export const
|
|
12
|
+
export const createAmazonBedrockEventStreamResponseHandler =
|
|
13
13
|
<T>(
|
|
14
14
|
chunkSchema: ZodType<T, any>,
|
|
15
15
|
): ResponseHandler<ReadableStream<ParseResult<T>>> =>
|
|
@@ -22,7 +22,7 @@ export const createBedrockEventStreamResponseHandler =
|
|
|
22
22
|
|
|
23
23
|
return {
|
|
24
24
|
responseHeaders,
|
|
25
|
-
value:
|
|
25
|
+
value: createAmazonBedrockEventStreamDecoder<ParseResult<T>>(
|
|
26
26
|
response.body,
|
|
27
27
|
async (event, controller) => {
|
|
28
28
|
if (event.messageType === 'event') {
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type {
|
|
2
2
|
ImageModelV4,
|
|
3
3
|
ImageModelV4File,
|
|
4
4
|
SharedV4Warning,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
6
|
import {
|
|
7
|
-
FetchFunction,
|
|
8
|
-
Resolvable,
|
|
9
7
|
combineHeaders,
|
|
10
8
|
convertUint8ArrayToBase64,
|
|
11
9
|
createJsonErrorResponseHandler,
|
|
@@ -15,15 +13,17 @@ import {
|
|
|
15
13
|
serializeModelOptions,
|
|
16
14
|
WORKFLOW_SERIALIZE,
|
|
17
15
|
WORKFLOW_DESERIALIZE,
|
|
16
|
+
type FetchFunction,
|
|
17
|
+
type Resolvable,
|
|
18
18
|
} from '@ai-sdk/provider-utils';
|
|
19
19
|
import {
|
|
20
|
-
BedrockImageModelId,
|
|
21
20
|
modelMaxImagesPerCall,
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
type AmazonBedrockImageModelId,
|
|
22
|
+
} from './amazon-bedrock-image-settings';
|
|
23
|
+
import { AmazonBedrockErrorSchema } from './amazon-bedrock-error';
|
|
24
24
|
import { z } from 'zod/v4';
|
|
25
25
|
|
|
26
|
-
type
|
|
26
|
+
type AmazonBedrockImageModelConfig = {
|
|
27
27
|
baseUrl: () => string;
|
|
28
28
|
headers?: Resolvable<Record<string, string | undefined>>;
|
|
29
29
|
fetch?: FetchFunction;
|
|
@@ -32,11 +32,11 @@ type BedrockImageModelConfig = {
|
|
|
32
32
|
};
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
export class
|
|
35
|
+
export class AmazonBedrockImageModel implements ImageModelV4 {
|
|
36
36
|
readonly specificationVersion = 'v4';
|
|
37
37
|
readonly provider = 'amazon-bedrock';
|
|
38
38
|
|
|
39
|
-
static [WORKFLOW_SERIALIZE](model:
|
|
39
|
+
static [WORKFLOW_SERIALIZE](model: AmazonBedrockImageModel) {
|
|
40
40
|
return serializeModelOptions({
|
|
41
41
|
modelId: model.modelId,
|
|
42
42
|
config: model.config,
|
|
@@ -45,9 +45,9 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
45
45
|
|
|
46
46
|
static [WORKFLOW_DESERIALIZE](options: {
|
|
47
47
|
modelId: string;
|
|
48
|
-
config:
|
|
48
|
+
config: AmazonBedrockImageModelConfig;
|
|
49
49
|
}) {
|
|
50
|
-
return new
|
|
50
|
+
return new AmazonBedrockImageModel(options.modelId, options.config);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
get maxImagesPerCall(): number {
|
|
@@ -60,8 +60,8 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
constructor(
|
|
63
|
-
readonly modelId:
|
|
64
|
-
private readonly config:
|
|
63
|
+
readonly modelId: AmazonBedrockImageModelId,
|
|
64
|
+
private readonly config: AmazonBedrockImageModelConfig,
|
|
65
65
|
) {}
|
|
66
66
|
|
|
67
67
|
async doGenerate({
|
|
@@ -83,17 +83,22 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
83
83
|
|
|
84
84
|
const hasFiles = files != null && files.length > 0;
|
|
85
85
|
|
|
86
|
+
// Prefer the new `amazonBedrock` providerOptions key; fall back to the
|
|
87
|
+
// legacy `bedrock` key for backward compatibility.
|
|
88
|
+
const amazonBedrockOptions = (providerOptions?.amazonBedrock ??
|
|
89
|
+
providerOptions?.bedrock) as Record<string, any> | undefined;
|
|
90
|
+
|
|
86
91
|
// Build image generation config (common to most modes)
|
|
87
92
|
const imageGenerationConfig = {
|
|
88
93
|
...(width ? { width } : {}),
|
|
89
94
|
...(height ? { height } : {}),
|
|
90
95
|
...(seed ? { seed } : {}),
|
|
91
96
|
...(n ? { numberOfImages: n } : {}),
|
|
92
|
-
...(
|
|
93
|
-
? { quality:
|
|
97
|
+
...(amazonBedrockOptions?.quality
|
|
98
|
+
? { quality: amazonBedrockOptions.quality }
|
|
94
99
|
: {}),
|
|
95
|
-
...(
|
|
96
|
-
? { cfgScale:
|
|
100
|
+
...(amazonBedrockOptions?.cfgScale
|
|
101
|
+
? { cfgScale: amazonBedrockOptions.cfgScale }
|
|
97
102
|
: {}),
|
|
98
103
|
};
|
|
99
104
|
|
|
@@ -102,11 +107,11 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
102
107
|
if (hasFiles) {
|
|
103
108
|
// Check if mask is actually provided (has valid data, not just an empty object)
|
|
104
109
|
const hasMask = mask?.type != null;
|
|
105
|
-
const hasMaskPrompt =
|
|
110
|
+
const hasMaskPrompt = amazonBedrockOptions?.maskPrompt != null;
|
|
106
111
|
|
|
107
112
|
// Determine task type from provider options, or infer from mask presence
|
|
108
113
|
const taskType =
|
|
109
|
-
|
|
114
|
+
amazonBedrockOptions?.taskType ??
|
|
110
115
|
(hasMask || hasMaskPrompt ? 'INPAINTING' : 'IMAGE_VARIATION');
|
|
111
116
|
|
|
112
117
|
const sourceImageBase64 = getBase64Data(files[0]);
|
|
@@ -116,8 +121,8 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
116
121
|
const inPaintingParams: Record<string, unknown> = {
|
|
117
122
|
image: sourceImageBase64,
|
|
118
123
|
...(prompt ? { text: prompt } : {}),
|
|
119
|
-
...(
|
|
120
|
-
? { negativeText:
|
|
124
|
+
...(amazonBedrockOptions?.negativeText
|
|
125
|
+
? { negativeText: amazonBedrockOptions.negativeText }
|
|
121
126
|
: {}),
|
|
122
127
|
};
|
|
123
128
|
|
|
@@ -125,7 +130,7 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
125
130
|
if (hasMask) {
|
|
126
131
|
inPaintingParams.maskImage = getBase64Data(mask);
|
|
127
132
|
} else if (hasMaskPrompt) {
|
|
128
|
-
inPaintingParams.maskPrompt =
|
|
133
|
+
inPaintingParams.maskPrompt = amazonBedrockOptions.maskPrompt;
|
|
129
134
|
}
|
|
130
135
|
|
|
131
136
|
args = {
|
|
@@ -140,11 +145,11 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
140
145
|
const outPaintingParams: Record<string, unknown> = {
|
|
141
146
|
image: sourceImageBase64,
|
|
142
147
|
...(prompt ? { text: prompt } : {}),
|
|
143
|
-
...(
|
|
144
|
-
? { negativeText:
|
|
148
|
+
...(amazonBedrockOptions?.negativeText
|
|
149
|
+
? { negativeText: amazonBedrockOptions.negativeText }
|
|
145
150
|
: {}),
|
|
146
|
-
...(
|
|
147
|
-
? { outPaintingMode:
|
|
151
|
+
...(amazonBedrockOptions?.outPaintingMode
|
|
152
|
+
? { outPaintingMode: amazonBedrockOptions.outPaintingMode }
|
|
148
153
|
: {}),
|
|
149
154
|
};
|
|
150
155
|
|
|
@@ -152,7 +157,7 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
152
157
|
if (hasMask) {
|
|
153
158
|
outPaintingParams.maskImage = getBase64Data(mask);
|
|
154
159
|
} else if (hasMaskPrompt) {
|
|
155
|
-
outPaintingParams.maskPrompt =
|
|
160
|
+
outPaintingParams.maskPrompt = amazonBedrockOptions.maskPrompt;
|
|
156
161
|
}
|
|
157
162
|
|
|
158
163
|
args = {
|
|
@@ -181,13 +186,12 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
181
186
|
const imageVariationParams: Record<string, unknown> = {
|
|
182
187
|
images,
|
|
183
188
|
...(prompt ? { text: prompt } : {}),
|
|
184
|
-
...(
|
|
185
|
-
? { negativeText:
|
|
189
|
+
...(amazonBedrockOptions?.negativeText
|
|
190
|
+
? { negativeText: amazonBedrockOptions.negativeText }
|
|
186
191
|
: {}),
|
|
187
|
-
...(
|
|
192
|
+
...(amazonBedrockOptions?.similarityStrength != null
|
|
188
193
|
? {
|
|
189
|
-
similarityStrength:
|
|
190
|
-
providerOptions.bedrock.similarityStrength,
|
|
194
|
+
similarityStrength: amazonBedrockOptions.similarityStrength,
|
|
191
195
|
}
|
|
192
196
|
: {}),
|
|
193
197
|
};
|
|
@@ -209,14 +213,14 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
209
213
|
taskType: 'TEXT_IMAGE',
|
|
210
214
|
textToImageParams: {
|
|
211
215
|
text: prompt,
|
|
212
|
-
...(
|
|
216
|
+
...(amazonBedrockOptions?.negativeText
|
|
213
217
|
? {
|
|
214
|
-
negativeText:
|
|
218
|
+
negativeText: amazonBedrockOptions.negativeText,
|
|
215
219
|
}
|
|
216
220
|
: {}),
|
|
217
|
-
...(
|
|
221
|
+
...(amazonBedrockOptions?.style
|
|
218
222
|
? {
|
|
219
|
-
style:
|
|
223
|
+
style: amazonBedrockOptions.style,
|
|
220
224
|
}
|
|
221
225
|
: {}),
|
|
222
226
|
},
|
|
@@ -244,11 +248,11 @@ export class BedrockImageModel implements ImageModelV4 {
|
|
|
244
248
|
),
|
|
245
249
|
body: args,
|
|
246
250
|
failedResponseHandler: createJsonErrorResponseHandler({
|
|
247
|
-
errorSchema:
|
|
251
|
+
errorSchema: AmazonBedrockErrorSchema,
|
|
248
252
|
errorToMessage: error => `${error.type}: ${error.message}`,
|
|
249
253
|
}),
|
|
250
254
|
successfulResponseHandler: createJsonResponseHandler(
|
|
251
|
-
|
|
255
|
+
amazonBedrockImageResponseSchema,
|
|
252
256
|
),
|
|
253
257
|
abortSignal,
|
|
254
258
|
fetch: this.config.fetch,
|
|
@@ -303,7 +307,7 @@ function getBase64Data(file: ImageModelV4File): string {
|
|
|
303
307
|
|
|
304
308
|
// minimal version of the schema, focussed on what is needed for the implementation
|
|
305
309
|
// this approach limits breakages when the API changes and increases efficiency
|
|
306
|
-
const
|
|
310
|
+
const amazonBedrockImageResponseSchema = z.object({
|
|
307
311
|
// Normal successful response
|
|
308
312
|
images: z.array(z.string()).optional(),
|
|
309
313
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type AmazonBedrockImageModelId =
|
|
2
|
+
| 'amazon.nova-canvas-v1:0'
|
|
3
|
+
| (string & {});
|
|
4
|
+
|
|
5
|
+
// https://docs.aws.amazon.com/nova/latest/userguide/image-gen-req-resp-structure.html
|
|
6
|
+
export const modelMaxImagesPerCall: Record<AmazonBedrockImageModelId, number> =
|
|
7
|
+
{
|
|
8
|
+
'amazon.nova-canvas-v1:0': 5,
|
|
9
|
+
};
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import {
|
|
2
|
-
JSONObject,
|
|
3
|
-
LanguageModelV4CallOptions,
|
|
4
|
-
SharedV4Warning,
|
|
5
2
|
UnsupportedFunctionalityError,
|
|
3
|
+
type JSONObject,
|
|
4
|
+
type LanguageModelV4CallOptions,
|
|
5
|
+
type SharedV4Warning,
|
|
6
6
|
} from '@ai-sdk/provider';
|
|
7
7
|
import { asSchema } from '@ai-sdk/provider-utils';
|
|
8
8
|
import {
|
|
9
9
|
anthropicTools,
|
|
10
10
|
prepareTools as prepareAnthropicTools,
|
|
11
11
|
} from '@ai-sdk/anthropic/internal';
|
|
12
|
-
import {
|
|
12
|
+
import type {
|
|
13
|
+
AmazonBedrockTool,
|
|
14
|
+
AmazonBedrockToolConfiguration,
|
|
15
|
+
} from './amazon-bedrock-api-types';
|
|
13
16
|
|
|
14
17
|
export async function prepareTools({
|
|
15
18
|
tools,
|
|
@@ -20,7 +23,7 @@ export async function prepareTools({
|
|
|
20
23
|
toolChoice?: LanguageModelV4CallOptions['toolChoice'];
|
|
21
24
|
modelId: string;
|
|
22
25
|
}): Promise<{
|
|
23
|
-
toolConfig:
|
|
26
|
+
toolConfig: AmazonBedrockToolConfiguration;
|
|
24
27
|
additionalTools: Record<string, unknown> | undefined;
|
|
25
28
|
betas: Set<string>;
|
|
26
29
|
toolWarnings: SharedV4Warning[];
|
|
@@ -68,7 +71,7 @@ export async function prepareTools({
|
|
|
68
71
|
const functionTools = supportedTools.filter(t => t.type === 'function');
|
|
69
72
|
|
|
70
73
|
let additionalTools: Record<string, unknown> | undefined = undefined;
|
|
71
|
-
const
|
|
74
|
+
const amazonBedrockTools: AmazonBedrockTool[] = [];
|
|
72
75
|
|
|
73
76
|
const usingAnthropicTools = isAnthropicModel && ProviderTools.length > 0;
|
|
74
77
|
|
|
@@ -105,7 +108,7 @@ export async function prepareTools({
|
|
|
105
108
|
|
|
106
109
|
if (toolFactory != null) {
|
|
107
110
|
const fullToolDefinition = (toolFactory as (args: any) => any)({});
|
|
108
|
-
|
|
111
|
+
amazonBedrockTools.push({
|
|
109
112
|
toolSpec: {
|
|
110
113
|
name: tool.name,
|
|
111
114
|
inputSchema: {
|
|
@@ -131,7 +134,7 @@ export async function prepareTools({
|
|
|
131
134
|
: functionTools;
|
|
132
135
|
|
|
133
136
|
for (const tool of filteredFunctionTools) {
|
|
134
|
-
|
|
137
|
+
amazonBedrockTools.push({
|
|
135
138
|
toolSpec: {
|
|
136
139
|
name: tool.name,
|
|
137
140
|
...(tool.description?.trim() !== ''
|
|
@@ -146,22 +149,23 @@ export async function prepareTools({
|
|
|
146
149
|
}
|
|
147
150
|
|
|
148
151
|
// Handle toolChoice for standard Bedrock tools, but NOT for Anthropic provider-defined tools
|
|
149
|
-
let
|
|
150
|
-
|
|
152
|
+
let amazonBedrockToolChoice: AmazonBedrockToolConfiguration['toolChoice'] =
|
|
153
|
+
undefined;
|
|
154
|
+
if (!usingAnthropicTools && amazonBedrockTools.length > 0 && toolChoice) {
|
|
151
155
|
const type = toolChoice.type;
|
|
152
156
|
switch (type) {
|
|
153
157
|
case 'auto':
|
|
154
|
-
|
|
158
|
+
amazonBedrockToolChoice = { auto: {} };
|
|
155
159
|
break;
|
|
156
160
|
case 'required':
|
|
157
|
-
|
|
161
|
+
amazonBedrockToolChoice = { any: {} };
|
|
158
162
|
break;
|
|
159
163
|
case 'none':
|
|
160
|
-
|
|
161
|
-
|
|
164
|
+
amazonBedrockTools.length = 0;
|
|
165
|
+
amazonBedrockToolChoice = undefined;
|
|
162
166
|
break;
|
|
163
167
|
case 'tool':
|
|
164
|
-
|
|
168
|
+
amazonBedrockToolChoice = { tool: { name: toolChoice.toolName } };
|
|
165
169
|
break;
|
|
166
170
|
default: {
|
|
167
171
|
const _exhaustiveCheck: never = type;
|
|
@@ -172,9 +176,9 @@ export async function prepareTools({
|
|
|
172
176
|
}
|
|
173
177
|
}
|
|
174
178
|
|
|
175
|
-
const toolConfig:
|
|
176
|
-
|
|
177
|
-
? { tools:
|
|
179
|
+
const toolConfig: AmazonBedrockToolConfiguration =
|
|
180
|
+
amazonBedrockTools.length > 0
|
|
181
|
+
? { tools: amazonBedrockTools, toolChoice: amazonBedrockToolChoice }
|
|
178
182
|
: {};
|
|
179
183
|
|
|
180
184
|
return {
|