@ai-sdk/amazon-bedrock 5.0.0-beta.8 → 5.0.0-beta.85
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 +669 -4
- package/README.md +2 -0
- package/dist/anthropic/index.d.ts +10 -10
- package/dist/anthropic/index.js +151 -117
- package/dist/anthropic/index.js.map +1 -1
- package/dist/index.d.ts +36 -23
- package/dist/index.js +881 -604
- package/dist/index.js.map +1 -1
- package/dist/mantle/index.cjs +253 -0
- package/dist/mantle/index.cjs.map +1 -0
- package/dist/mantle/index.d.cts +99 -0
- package/dist/mantle/index.d.ts +99 -0
- package/dist/mantle/index.js +240 -0
- package/dist/mantle/index.js.map +1 -0
- package/docs/08-amazon-bedrock.mdx +310 -84
- package/mantle/index.d.ts +1 -0
- package/package.json +27 -20
- package/src/amazon-bedrock-api-types.ts +228 -0
- package/src/{bedrock-chat-options.ts → amazon-bedrock-chat-language-model-options.ts} +27 -8
- package/src/{bedrock-chat-language-model.ts → amazon-bedrock-chat-language-model.ts} +350 -180
- 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} +61 -29
- 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} +62 -38
- 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} +17 -9
- package/src/anthropic/amazon-bedrock-anthropic-fetch.ts +104 -0
- package/src/anthropic/{bedrock-anthropic-options.ts → amazon-bedrock-anthropic-options.ts} +7 -1
- package/src/anthropic/{bedrock-anthropic-provider.ts → amazon-bedrock-anthropic-provider.ts} +40 -24
- package/src/anthropic/index.ts +19 -7
- package/src/{convert-bedrock-usage.ts → convert-amazon-bedrock-usage.ts} +4 -4
- package/src/convert-to-amazon-bedrock-chat-messages.ts +556 -0
- package/src/index.ts +15 -8
- package/src/inject-fetch-headers.ts +1 -1
- package/src/mantle/bedrock-mantle-options.ts +15 -0
- package/src/mantle/bedrock-mantle-provider.ts +283 -0
- package/src/mantle/index.ts +6 -0
- 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/dist/anthropic/index.d.mts +0 -91
- package/dist/anthropic/index.mjs +0 -397
- package/dist/anthropic/index.mjs.map +0 -1
- package/dist/index.d.mts +0 -194
- package/dist/index.mjs +0 -2329
- package/dist/index.mjs.map +0 -1
- package/src/anthropic/bedrock-anthropic-fetch.ts +0 -68
- package/src/bedrock-api-types.ts +0 -216
- package/src/bedrock-image-settings.ts +0 -6
- package/src/convert-to-bedrock-chat-messages.ts +0 -468
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UnsupportedFunctionalityError,
|
|
3
|
+
type JSONObject,
|
|
4
|
+
type LanguageModelV4Message,
|
|
5
|
+
type LanguageModelV4Prompt,
|
|
6
|
+
type SharedV4ProviderMetadata,
|
|
7
|
+
} from '@ai-sdk/provider';
|
|
8
|
+
import {
|
|
9
|
+
convertToBase64,
|
|
10
|
+
getTopLevelMediaType,
|
|
11
|
+
isFullMediaType,
|
|
12
|
+
parseProviderOptions,
|
|
13
|
+
resolveFullMediaType,
|
|
14
|
+
stripFileExtension,
|
|
15
|
+
} from '@ai-sdk/provider-utils';
|
|
16
|
+
import {
|
|
17
|
+
BEDROCK_DOCUMENT_MIME_TYPES,
|
|
18
|
+
BEDROCK_IMAGE_MIME_TYPES,
|
|
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
|
+
import { normalizeToolCallId } from './normalize-tool-call-id';
|
|
32
|
+
|
|
33
|
+
function getCachePoint(
|
|
34
|
+
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
35
|
+
): AmazonBedrockCachePoint | undefined {
|
|
36
|
+
const cachePointConfig = (providerMetadata?.amazonBedrock?.cachePoint ??
|
|
37
|
+
providerMetadata?.bedrock?.cachePoint) as
|
|
38
|
+
| AmazonBedrockCachePoint['cachePoint']
|
|
39
|
+
| undefined;
|
|
40
|
+
|
|
41
|
+
if (!cachePointConfig) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return { cachePoint: cachePointConfig };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function pushCachePoint(
|
|
49
|
+
content:
|
|
50
|
+
| AmazonBedrockUserMessage['content']
|
|
51
|
+
| AmazonBedrockAssistantMessage['content'],
|
|
52
|
+
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
53
|
+
) {
|
|
54
|
+
const cachePoint = getCachePoint(providerMetadata);
|
|
55
|
+
if (cachePoint) {
|
|
56
|
+
content.push(cachePoint);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function shouldEnableCitations(
|
|
61
|
+
providerMetadata: SharedV4ProviderMetadata | undefined,
|
|
62
|
+
): Promise<boolean> {
|
|
63
|
+
const amazonBedrockOptions =
|
|
64
|
+
(await parseProviderOptions({
|
|
65
|
+
provider: 'amazonBedrock',
|
|
66
|
+
providerOptions: providerMetadata,
|
|
67
|
+
schema: amazonBedrockFilePartProviderOptions,
|
|
68
|
+
})) ??
|
|
69
|
+
(await parseProviderOptions({
|
|
70
|
+
provider: 'bedrock',
|
|
71
|
+
providerOptions: providerMetadata,
|
|
72
|
+
schema: amazonBedrockFilePartProviderOptions,
|
|
73
|
+
}));
|
|
74
|
+
|
|
75
|
+
return amazonBedrockOptions?.citations?.enabled ?? false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export async function convertToAmazonBedrockChatMessages(
|
|
79
|
+
prompt: LanguageModelV4Prompt,
|
|
80
|
+
isMistral: boolean = false,
|
|
81
|
+
): Promise<{
|
|
82
|
+
system: AmazonBedrockSystemMessages;
|
|
83
|
+
messages: AmazonBedrockMessages;
|
|
84
|
+
}> {
|
|
85
|
+
const blocks = groupIntoBlocks(prompt);
|
|
86
|
+
|
|
87
|
+
let system: AmazonBedrockSystemMessages = [];
|
|
88
|
+
const messages: AmazonBedrockMessages = [];
|
|
89
|
+
|
|
90
|
+
let documentCounter = 0;
|
|
91
|
+
const generateDocumentName = () => `document-${++documentCounter}`;
|
|
92
|
+
|
|
93
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
94
|
+
const block = blocks[i];
|
|
95
|
+
const isLastBlock = i === blocks.length - 1;
|
|
96
|
+
const type = block.type;
|
|
97
|
+
|
|
98
|
+
switch (type) {
|
|
99
|
+
case 'system': {
|
|
100
|
+
if (messages.length > 0) {
|
|
101
|
+
throw new UnsupportedFunctionalityError({
|
|
102
|
+
functionality:
|
|
103
|
+
'Multiple system messages that are separated by user/assistant messages',
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
for (const message of block.messages) {
|
|
108
|
+
system.push({ text: message.content });
|
|
109
|
+
const cachePoint = getCachePoint(message.providerOptions);
|
|
110
|
+
if (cachePoint) {
|
|
111
|
+
system.push(cachePoint);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
case 'user': {
|
|
118
|
+
// combines all user and tool messages in this block into a single message:
|
|
119
|
+
const amazonBedrockContent: AmazonBedrockUserMessage['content'] = [];
|
|
120
|
+
|
|
121
|
+
for (const message of block.messages) {
|
|
122
|
+
const { role, content, providerOptions } = message;
|
|
123
|
+
switch (role) {
|
|
124
|
+
case 'user': {
|
|
125
|
+
for (let j = 0; j < content.length; j++) {
|
|
126
|
+
const part = content[j];
|
|
127
|
+
|
|
128
|
+
switch (part.type) {
|
|
129
|
+
case 'text': {
|
|
130
|
+
amazonBedrockContent.push({
|
|
131
|
+
text: part.text,
|
|
132
|
+
});
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
case 'file': {
|
|
137
|
+
switch (part.data.type) {
|
|
138
|
+
case 'reference': {
|
|
139
|
+
throw new UnsupportedFunctionalityError({
|
|
140
|
+
functionality: 'file parts with provider references',
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
case 'url': {
|
|
144
|
+
throw new UnsupportedFunctionalityError({
|
|
145
|
+
functionality: 'File URL data',
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
case 'text': {
|
|
149
|
+
const textMediaType = isFullMediaType(part.mediaType)
|
|
150
|
+
? part.mediaType
|
|
151
|
+
: 'text/plain';
|
|
152
|
+
const enableCitations = await shouldEnableCitations(
|
|
153
|
+
part.providerOptions,
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
amazonBedrockContent.push({
|
|
157
|
+
document: {
|
|
158
|
+
format:
|
|
159
|
+
getAmazonBedrockDocumentFormat(textMediaType),
|
|
160
|
+
name: part.filename
|
|
161
|
+
? stripFileExtension(part.filename)
|
|
162
|
+
: generateDocumentName(),
|
|
163
|
+
source: {
|
|
164
|
+
bytes: convertToBase64(
|
|
165
|
+
new TextEncoder().encode(part.data.text),
|
|
166
|
+
),
|
|
167
|
+
},
|
|
168
|
+
...(enableCitations && {
|
|
169
|
+
citations: { enabled: true },
|
|
170
|
+
}),
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case 'data': {
|
|
176
|
+
const fullMediaType = resolveFullMediaType({ part });
|
|
177
|
+
|
|
178
|
+
if (getTopLevelMediaType(fullMediaType) === 'image') {
|
|
179
|
+
amazonBedrockContent.push({
|
|
180
|
+
image: {
|
|
181
|
+
format:
|
|
182
|
+
getAmazonBedrockImageFormat(fullMediaType),
|
|
183
|
+
source: {
|
|
184
|
+
bytes: convertToBase64(part.data.data),
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
} else {
|
|
189
|
+
const enableCitations = await shouldEnableCitations(
|
|
190
|
+
part.providerOptions,
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
amazonBedrockContent.push({
|
|
194
|
+
document: {
|
|
195
|
+
format:
|
|
196
|
+
getAmazonBedrockDocumentFormat(fullMediaType),
|
|
197
|
+
name: part.filename
|
|
198
|
+
? stripFileExtension(part.filename)
|
|
199
|
+
: generateDocumentName(),
|
|
200
|
+
source: {
|
|
201
|
+
bytes: convertToBase64(part.data.data),
|
|
202
|
+
},
|
|
203
|
+
...(enableCitations && {
|
|
204
|
+
citations: { enabled: true },
|
|
205
|
+
}),
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
pushCachePoint(amazonBedrockContent, part.providerOptions);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
case 'tool': {
|
|
223
|
+
for (const part of content) {
|
|
224
|
+
if (part.type === 'tool-approval-response') {
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
let toolResultContent;
|
|
228
|
+
|
|
229
|
+
const output = part.output;
|
|
230
|
+
switch (output.type) {
|
|
231
|
+
case 'content': {
|
|
232
|
+
toolResultContent = await Promise.all(
|
|
233
|
+
output.value.map(async contentPart => {
|
|
234
|
+
switch (contentPart.type) {
|
|
235
|
+
case 'text':
|
|
236
|
+
return { text: contentPart.text };
|
|
237
|
+
case 'file': {
|
|
238
|
+
if (contentPart.data.type !== 'data') {
|
|
239
|
+
throw new UnsupportedFunctionalityError({
|
|
240
|
+
functionality: `tool result file data of type "${contentPart.data.type}"`,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const fullMediaType = resolveFullMediaType({
|
|
245
|
+
part: contentPart,
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
if (
|
|
249
|
+
getTopLevelMediaType(fullMediaType) !== 'image'
|
|
250
|
+
) {
|
|
251
|
+
const enableCitations =
|
|
252
|
+
await shouldEnableCitations(
|
|
253
|
+
contentPart.providerOptions,
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
return {
|
|
257
|
+
document: {
|
|
258
|
+
format:
|
|
259
|
+
getAmazonBedrockDocumentFormat(
|
|
260
|
+
fullMediaType,
|
|
261
|
+
),
|
|
262
|
+
name: contentPart.filename
|
|
263
|
+
? stripFileExtension(contentPart.filename)
|
|
264
|
+
: generateDocumentName(),
|
|
265
|
+
source: {
|
|
266
|
+
bytes: convertToBase64(
|
|
267
|
+
contentPart.data.data,
|
|
268
|
+
),
|
|
269
|
+
},
|
|
270
|
+
...(enableCitations && {
|
|
271
|
+
citations: { enabled: true },
|
|
272
|
+
}),
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return {
|
|
278
|
+
image: {
|
|
279
|
+
format:
|
|
280
|
+
getAmazonBedrockImageFormat(fullMediaType),
|
|
281
|
+
source: {
|
|
282
|
+
bytes: convertToBase64(contentPart.data.data),
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
default: {
|
|
288
|
+
throw new UnsupportedFunctionalityError({
|
|
289
|
+
functionality: `unsupported tool content part type: ${contentPart.type}`,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}),
|
|
294
|
+
);
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
case 'text':
|
|
298
|
+
case 'error-text':
|
|
299
|
+
toolResultContent = [{ text: output.value }];
|
|
300
|
+
break;
|
|
301
|
+
case 'execution-denied':
|
|
302
|
+
toolResultContent = [
|
|
303
|
+
{ text: output.reason ?? 'Tool call execution denied.' },
|
|
304
|
+
];
|
|
305
|
+
break;
|
|
306
|
+
case 'json':
|
|
307
|
+
case 'error-json':
|
|
308
|
+
default:
|
|
309
|
+
toolResultContent = [
|
|
310
|
+
{ text: JSON.stringify(output.value) },
|
|
311
|
+
];
|
|
312
|
+
break;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
amazonBedrockContent.push({
|
|
316
|
+
toolResult: {
|
|
317
|
+
toolUseId: normalizeToolCallId(part.toolCallId, isMistral),
|
|
318
|
+
content: toolResultContent,
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
pushCachePoint(amazonBedrockContent, part.providerOptions);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
default: {
|
|
327
|
+
const _exhaustiveCheck: never = role;
|
|
328
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
pushCachePoint(amazonBedrockContent, providerOptions);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
messages.push({ role: 'user', content: amazonBedrockContent });
|
|
336
|
+
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
case 'assistant': {
|
|
341
|
+
// combines multiple assistant messages in this block into a single message:
|
|
342
|
+
const amazonBedrockContent: AmazonBedrockAssistantMessage['content'] =
|
|
343
|
+
[];
|
|
344
|
+
|
|
345
|
+
for (let j = 0; j < block.messages.length; j++) {
|
|
346
|
+
const message = block.messages[j];
|
|
347
|
+
const isLastMessage = j === block.messages.length - 1;
|
|
348
|
+
const { content } = message;
|
|
349
|
+
const hasReasoningBlocks = content.some(
|
|
350
|
+
part => part.type === 'reasoning',
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
for (let k = 0; k < content.length; k++) {
|
|
354
|
+
const part = content[k];
|
|
355
|
+
const isLastContentPart = k === content.length - 1;
|
|
356
|
+
|
|
357
|
+
switch (part.type) {
|
|
358
|
+
case 'text': {
|
|
359
|
+
// Skip empty text blocks unless reasoning blocks are present
|
|
360
|
+
if (!part.text.trim() && !hasReasoningBlocks) {
|
|
361
|
+
break;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
amazonBedrockContent.push({
|
|
365
|
+
text:
|
|
366
|
+
// trim the last text part if it's the last message in the block
|
|
367
|
+
// because Bedrock does not allow trailing whitespace
|
|
368
|
+
// in pre-filled assistant responses
|
|
369
|
+
trimIfLast(
|
|
370
|
+
isLastBlock,
|
|
371
|
+
isLastMessage,
|
|
372
|
+
isLastContentPart,
|
|
373
|
+
part.text,
|
|
374
|
+
),
|
|
375
|
+
});
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
case 'reasoning': {
|
|
380
|
+
const reasoningMetadata =
|
|
381
|
+
(await parseProviderOptions({
|
|
382
|
+
provider: 'amazonBedrock',
|
|
383
|
+
providerOptions: part.providerOptions,
|
|
384
|
+
schema: amazonBedrockReasoningMetadataSchema,
|
|
385
|
+
})) ??
|
|
386
|
+
(await parseProviderOptions({
|
|
387
|
+
provider: 'bedrock',
|
|
388
|
+
providerOptions: part.providerOptions,
|
|
389
|
+
schema: amazonBedrockReasoningMetadataSchema,
|
|
390
|
+
}));
|
|
391
|
+
|
|
392
|
+
if (reasoningMetadata?.signature != null) {
|
|
393
|
+
// do not trim reasoning text when a signature is present:
|
|
394
|
+
// the signature validates the exact original bytes
|
|
395
|
+
amazonBedrockContent.push({
|
|
396
|
+
reasoningContent: {
|
|
397
|
+
reasoningText: {
|
|
398
|
+
text: part.text,
|
|
399
|
+
signature: reasoningMetadata.signature,
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
});
|
|
403
|
+
} else if (reasoningMetadata?.redactedData != null) {
|
|
404
|
+
amazonBedrockContent.push({
|
|
405
|
+
reasoningContent: {
|
|
406
|
+
redactedReasoning: {
|
|
407
|
+
data: reasoningMetadata.redactedData,
|
|
408
|
+
},
|
|
409
|
+
},
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
// Unsigned reasoning is intentionally not replayed. Some
|
|
413
|
+
// Bedrock models (for example OpenAI gpt-oss) return reasoning
|
|
414
|
+
// without a signature; sending it back in multi-turn tool use
|
|
415
|
+
// can leak raw reasoning into the visible response.
|
|
416
|
+
break;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
case 'tool-call': {
|
|
420
|
+
amazonBedrockContent.push({
|
|
421
|
+
toolUse: {
|
|
422
|
+
toolUseId: normalizeToolCallId(part.toolCallId, isMistral),
|
|
423
|
+
name: part.toolName,
|
|
424
|
+
input: part.input as JSONObject,
|
|
425
|
+
},
|
|
426
|
+
});
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
pushCachePoint(amazonBedrockContent, part.providerOptions);
|
|
432
|
+
}
|
|
433
|
+
pushCachePoint(amazonBedrockContent, message.providerOptions);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
messages.push({ role: 'assistant', content: amazonBedrockContent });
|
|
437
|
+
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
default: {
|
|
442
|
+
const _exhaustiveCheck: never = type;
|
|
443
|
+
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
return { system, messages };
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function getAmazonBedrockImageFormat(
|
|
452
|
+
mimeType: string,
|
|
453
|
+
): AmazonBedrockImageFormat {
|
|
454
|
+
const format =
|
|
455
|
+
BEDROCK_IMAGE_MIME_TYPES[mimeType as AmazonBedrockImageMimeType];
|
|
456
|
+
if (!format) {
|
|
457
|
+
throw new UnsupportedFunctionalityError({
|
|
458
|
+
functionality: `image mime type: ${mimeType}`,
|
|
459
|
+
message: `Unsupported image mime type: ${mimeType}, expected one of: ${Object.keys(BEDROCK_IMAGE_MIME_TYPES).join(', ')}`,
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return format;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function getAmazonBedrockDocumentFormat(
|
|
467
|
+
mimeType: string,
|
|
468
|
+
): AmazonBedrockDocumentFormat {
|
|
469
|
+
const format =
|
|
470
|
+
BEDROCK_DOCUMENT_MIME_TYPES[mimeType as AmazonBedrockDocumentMimeType];
|
|
471
|
+
if (!format) {
|
|
472
|
+
throw new UnsupportedFunctionalityError({
|
|
473
|
+
functionality: `file mime type: ${mimeType}`,
|
|
474
|
+
message: `Unsupported file mime type: ${mimeType}, expected one of: ${Object.keys(BEDROCK_DOCUMENT_MIME_TYPES).join(', ')}`,
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
return format;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function trimIfLast(
|
|
481
|
+
isLastBlock: boolean,
|
|
482
|
+
isLastMessage: boolean,
|
|
483
|
+
isLastContentPart: boolean,
|
|
484
|
+
text: string,
|
|
485
|
+
) {
|
|
486
|
+
return isLastBlock && isLastMessage && isLastContentPart ? text.trim() : text;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
type SystemBlock = {
|
|
490
|
+
type: 'system';
|
|
491
|
+
messages: Array<LanguageModelV4Message & { role: 'system' }>;
|
|
492
|
+
};
|
|
493
|
+
type AssistantBlock = {
|
|
494
|
+
type: 'assistant';
|
|
495
|
+
messages: Array<LanguageModelV4Message & { role: 'assistant' }>;
|
|
496
|
+
};
|
|
497
|
+
type UserBlock = {
|
|
498
|
+
type: 'user';
|
|
499
|
+
messages: Array<LanguageModelV4Message & { role: 'user' | 'tool' }>;
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
function groupIntoBlocks(
|
|
503
|
+
prompt: LanguageModelV4Prompt,
|
|
504
|
+
): Array<SystemBlock | AssistantBlock | UserBlock> {
|
|
505
|
+
const blocks: Array<SystemBlock | AssistantBlock | UserBlock> = [];
|
|
506
|
+
let currentBlock: SystemBlock | AssistantBlock | UserBlock | undefined =
|
|
507
|
+
undefined;
|
|
508
|
+
|
|
509
|
+
for (const message of prompt) {
|
|
510
|
+
const { role } = message;
|
|
511
|
+
switch (role) {
|
|
512
|
+
case 'system': {
|
|
513
|
+
if (currentBlock?.type !== 'system') {
|
|
514
|
+
currentBlock = { type: 'system', messages: [] };
|
|
515
|
+
blocks.push(currentBlock);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
currentBlock.messages.push(message);
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
case 'assistant': {
|
|
522
|
+
if (currentBlock?.type !== 'assistant') {
|
|
523
|
+
currentBlock = { type: 'assistant', messages: [] };
|
|
524
|
+
blocks.push(currentBlock);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
currentBlock.messages.push(message);
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
case 'user': {
|
|
531
|
+
if (currentBlock?.type !== 'user') {
|
|
532
|
+
currentBlock = { type: 'user', messages: [] };
|
|
533
|
+
blocks.push(currentBlock);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
currentBlock.messages.push(message);
|
|
537
|
+
break;
|
|
538
|
+
}
|
|
539
|
+
case 'tool': {
|
|
540
|
+
if (currentBlock?.type !== 'user') {
|
|
541
|
+
currentBlock = { type: 'user', messages: [] };
|
|
542
|
+
blocks.push(currentBlock);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
currentBlock.messages.push(message);
|
|
546
|
+
break;
|
|
547
|
+
}
|
|
548
|
+
default: {
|
|
549
|
+
const _exhaustiveCheck: never = role;
|
|
550
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
return blocks;
|
|
556
|
+
}
|
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';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type BedrockMantleChatModelId =
|
|
2
|
+
| 'openai.gpt-oss-20b'
|
|
3
|
+
| 'openai.gpt-oss-120b'
|
|
4
|
+
| 'openai.gpt-oss-safeguard-20b'
|
|
5
|
+
| 'openai.gpt-oss-safeguard-120b'
|
|
6
|
+
| (string & {});
|
|
7
|
+
|
|
8
|
+
export type BedrockMantleResponsesModelId =
|
|
9
|
+
| 'openai.gpt-oss-20b'
|
|
10
|
+
| 'openai.gpt-oss-120b'
|
|
11
|
+
| (string & {});
|
|
12
|
+
|
|
13
|
+
export type BedrockMantleModelId =
|
|
14
|
+
| BedrockMantleChatModelId
|
|
15
|
+
| BedrockMantleResponsesModelId;
|