@ai-sdk/amazon-bedrock 3.0.0-canary.9 → 3.0.1
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 +509 -0
- package/README.md +77 -2
- package/dist/index.d.mts +45 -40
- package/dist/index.d.ts +45 -40
- package/dist/index.js +750 -404
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +663 -312
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -6
package/dist/index.js
CHANGED
|
@@ -26,11 +26,12 @@ __export(src_exports, {
|
|
|
26
26
|
module.exports = __toCommonJS(src_exports);
|
|
27
27
|
|
|
28
28
|
// src/bedrock-provider.ts
|
|
29
|
-
var
|
|
29
|
+
var import_provider_utils8 = require("@ai-sdk/provider-utils");
|
|
30
|
+
var import_internal2 = require("@ai-sdk/anthropic/internal");
|
|
30
31
|
|
|
31
32
|
// src/bedrock-chat-language-model.ts
|
|
32
|
-
var
|
|
33
|
-
var
|
|
33
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
34
|
+
var import_v43 = require("zod/v4");
|
|
34
35
|
|
|
35
36
|
// src/bedrock-api-types.ts
|
|
36
37
|
var BEDROCK_CACHE_POINT = {
|
|
@@ -48,27 +49,44 @@ var BEDROCK_STOP_REASONS = [
|
|
|
48
49
|
"tool-calls",
|
|
49
50
|
"tool_use"
|
|
50
51
|
];
|
|
52
|
+
var BEDROCK_IMAGE_MIME_TYPES = {
|
|
53
|
+
"image/jpeg": "jpeg",
|
|
54
|
+
"image/png": "png",
|
|
55
|
+
"image/gif": "gif",
|
|
56
|
+
"image/webp": "webp"
|
|
57
|
+
};
|
|
58
|
+
var BEDROCK_DOCUMENT_MIME_TYPES = {
|
|
59
|
+
"application/pdf": "pdf",
|
|
60
|
+
"text/csv": "csv",
|
|
61
|
+
"application/msword": "doc",
|
|
62
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
63
|
+
"application/vnd.ms-excel": "xls",
|
|
64
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
|
65
|
+
"text/html": "html",
|
|
66
|
+
"text/plain": "txt",
|
|
67
|
+
"text/markdown": "md"
|
|
68
|
+
};
|
|
51
69
|
|
|
52
70
|
// src/bedrock-chat-options.ts
|
|
53
|
-
var
|
|
54
|
-
var bedrockProviderOptions =
|
|
71
|
+
var import_v4 = require("zod/v4");
|
|
72
|
+
var bedrockProviderOptions = import_v4.z.object({
|
|
55
73
|
/**
|
|
56
74
|
* Additional inference parameters that the model supports,
|
|
57
75
|
* beyond the base set of inference parameters that Converse
|
|
58
76
|
* supports in the inferenceConfig field
|
|
59
77
|
*/
|
|
60
|
-
additionalModelRequestFields:
|
|
61
|
-
reasoningConfig:
|
|
62
|
-
type:
|
|
63
|
-
budgetTokens:
|
|
64
|
-
}).
|
|
78
|
+
additionalModelRequestFields: import_v4.z.record(import_v4.z.string(), import_v4.z.any()).optional(),
|
|
79
|
+
reasoningConfig: import_v4.z.object({
|
|
80
|
+
type: import_v4.z.union([import_v4.z.literal("enabled"), import_v4.z.literal("disabled")]).optional(),
|
|
81
|
+
budgetTokens: import_v4.z.number().optional()
|
|
82
|
+
}).optional()
|
|
65
83
|
});
|
|
66
84
|
|
|
67
85
|
// src/bedrock-error.ts
|
|
68
|
-
var
|
|
69
|
-
var BedrockErrorSchema =
|
|
70
|
-
message:
|
|
71
|
-
type:
|
|
86
|
+
var import_v42 = require("zod/v4");
|
|
87
|
+
var BedrockErrorSchema = import_v42.z.object({
|
|
88
|
+
message: import_v42.z.string(),
|
|
89
|
+
type: import_v42.z.string().nullish()
|
|
72
90
|
});
|
|
73
91
|
|
|
74
92
|
// src/bedrock-event-stream-response-handler.ts
|
|
@@ -88,7 +106,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
|
|
|
88
106
|
responseHeaders,
|
|
89
107
|
value: response.body.pipeThrough(
|
|
90
108
|
new TransformStream({
|
|
91
|
-
transform(chunk, controller) {
|
|
109
|
+
async transform(chunk, controller) {
|
|
92
110
|
var _a, _b;
|
|
93
111
|
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
94
112
|
newBuffer.set(buffer);
|
|
@@ -109,7 +127,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
|
|
|
109
127
|
buffer = buffer.slice(totalLength);
|
|
110
128
|
if (((_a = decoded.headers[":message-type"]) == null ? void 0 : _a.value) === "event") {
|
|
111
129
|
const data = textDecoder.decode(decoded.body);
|
|
112
|
-
const parsedDataResult = (0, import_provider_utils.safeParseJSON)({ text: data });
|
|
130
|
+
const parsedDataResult = await (0, import_provider_utils.safeParseJSON)({ text: data });
|
|
113
131
|
if (!parsedDataResult.success) {
|
|
114
132
|
controller.enqueue(parsedDataResult);
|
|
115
133
|
break;
|
|
@@ -118,7 +136,7 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
|
|
|
118
136
|
let wrappedData = {
|
|
119
137
|
[(_b = decoded.headers[":event-type"]) == null ? void 0 : _b.value]: parsedDataResult.value
|
|
120
138
|
};
|
|
121
|
-
const validatedWrappedData = (0, import_provider_utils.safeValidateTypes)({
|
|
139
|
+
const validatedWrappedData = await (0, import_provider_utils.safeValidateTypes)({
|
|
122
140
|
value: wrappedData,
|
|
123
141
|
schema: chunkSchema
|
|
124
142
|
});
|
|
@@ -144,87 +162,155 @@ var createBedrockEventStreamResponseHandler = (chunkSchema) => async ({ response
|
|
|
144
162
|
|
|
145
163
|
// src/bedrock-prepare-tools.ts
|
|
146
164
|
var import_provider2 = require("@ai-sdk/provider");
|
|
165
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
166
|
+
var import_internal = require("@ai-sdk/anthropic/internal");
|
|
147
167
|
function prepareTools({
|
|
148
168
|
tools,
|
|
149
|
-
toolChoice
|
|
169
|
+
toolChoice,
|
|
170
|
+
modelId
|
|
150
171
|
}) {
|
|
151
|
-
|
|
152
|
-
|
|
172
|
+
const toolWarnings = [];
|
|
173
|
+
const betas = /* @__PURE__ */ new Set();
|
|
174
|
+
if (tools == null || tools.length === 0) {
|
|
153
175
|
return {
|
|
154
|
-
toolConfig: {
|
|
155
|
-
|
|
176
|
+
toolConfig: {},
|
|
177
|
+
additionalTools: void 0,
|
|
178
|
+
betas,
|
|
179
|
+
toolWarnings
|
|
156
180
|
};
|
|
157
181
|
}
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
bedrockTools.push({
|
|
165
|
-
toolSpec: {
|
|
166
|
-
name: tool.name,
|
|
167
|
-
description: tool.description,
|
|
168
|
-
inputSchema: {
|
|
169
|
-
json: tool.parameters
|
|
170
|
-
}
|
|
171
|
-
}
|
|
182
|
+
const supportedTools = tools.filter((tool) => {
|
|
183
|
+
if (tool.type === "provider-defined" && tool.id === "anthropic.web_search_20250305") {
|
|
184
|
+
toolWarnings.push({
|
|
185
|
+
type: "unsupported-tool",
|
|
186
|
+
tool,
|
|
187
|
+
details: "The web_search_20250305 tool is not supported on Amazon Bedrock."
|
|
172
188
|
});
|
|
189
|
+
return false;
|
|
173
190
|
}
|
|
174
|
-
|
|
175
|
-
|
|
191
|
+
return true;
|
|
192
|
+
});
|
|
193
|
+
if (supportedTools.length === 0) {
|
|
176
194
|
return {
|
|
177
|
-
toolConfig: {
|
|
195
|
+
toolConfig: {},
|
|
196
|
+
additionalTools: void 0,
|
|
197
|
+
betas,
|
|
178
198
|
toolWarnings
|
|
179
199
|
};
|
|
180
200
|
}
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
201
|
+
const isAnthropicModel = modelId.includes("anthropic.");
|
|
202
|
+
const providerDefinedTools = supportedTools.filter(
|
|
203
|
+
(t) => t.type === "provider-defined"
|
|
204
|
+
);
|
|
205
|
+
const functionTools = supportedTools.filter((t) => t.type === "function");
|
|
206
|
+
let additionalTools = void 0;
|
|
207
|
+
const bedrockTools = [];
|
|
208
|
+
const usingAnthropicTools = isAnthropicModel && providerDefinedTools.length > 0;
|
|
209
|
+
if (usingAnthropicTools) {
|
|
210
|
+
if (functionTools.length > 0) {
|
|
211
|
+
toolWarnings.push({
|
|
212
|
+
type: "unsupported-setting",
|
|
213
|
+
setting: "tools",
|
|
214
|
+
details: "Mixed Anthropic provider-defined tools and standard function tools are not supported in a single call to Bedrock. Only Anthropic tools will be used."
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
const {
|
|
218
|
+
toolChoice: preparedAnthropicToolChoice,
|
|
219
|
+
toolWarnings: anthropicToolWarnings,
|
|
220
|
+
betas: anthropicBetas
|
|
221
|
+
} = (0, import_internal.prepareTools)({
|
|
222
|
+
tools: providerDefinedTools,
|
|
223
|
+
toolChoice
|
|
224
|
+
});
|
|
225
|
+
toolWarnings.push(...anthropicToolWarnings);
|
|
226
|
+
anthropicBetas.forEach((beta) => betas.add(beta));
|
|
227
|
+
if (preparedAnthropicToolChoice) {
|
|
228
|
+
additionalTools = {
|
|
229
|
+
tool_choice: preparedAnthropicToolChoice
|
|
205
230
|
};
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
231
|
+
}
|
|
232
|
+
for (const tool of providerDefinedTools) {
|
|
233
|
+
const toolFactory = Object.values(import_internal.anthropicTools).find((factory) => {
|
|
234
|
+
const instance = factory({});
|
|
235
|
+
return instance.id === tool.id;
|
|
210
236
|
});
|
|
237
|
+
if (toolFactory != null) {
|
|
238
|
+
const fullToolDefinition = toolFactory({});
|
|
239
|
+
bedrockTools.push({
|
|
240
|
+
toolSpec: {
|
|
241
|
+
name: tool.name,
|
|
242
|
+
inputSchema: {
|
|
243
|
+
json: (0, import_provider_utils2.asSchema)(fullToolDefinition.inputSchema).jsonSchema
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
} else {
|
|
248
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
} else {
|
|
252
|
+
for (const tool of providerDefinedTools) {
|
|
253
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
211
254
|
}
|
|
212
255
|
}
|
|
256
|
+
for (const tool of functionTools) {
|
|
257
|
+
bedrockTools.push({
|
|
258
|
+
toolSpec: {
|
|
259
|
+
name: tool.name,
|
|
260
|
+
description: tool.description,
|
|
261
|
+
inputSchema: {
|
|
262
|
+
json: tool.inputSchema
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
let bedrockToolChoice = void 0;
|
|
268
|
+
if (!usingAnthropicTools && bedrockTools.length > 0 && toolChoice) {
|
|
269
|
+
const type = toolChoice.type;
|
|
270
|
+
switch (type) {
|
|
271
|
+
case "auto":
|
|
272
|
+
bedrockToolChoice = { auto: {} };
|
|
273
|
+
break;
|
|
274
|
+
case "required":
|
|
275
|
+
bedrockToolChoice = { any: {} };
|
|
276
|
+
break;
|
|
277
|
+
case "none":
|
|
278
|
+
bedrockTools.length = 0;
|
|
279
|
+
bedrockToolChoice = void 0;
|
|
280
|
+
break;
|
|
281
|
+
case "tool":
|
|
282
|
+
bedrockToolChoice = { tool: { name: toolChoice.toolName } };
|
|
283
|
+
break;
|
|
284
|
+
default: {
|
|
285
|
+
const _exhaustiveCheck = type;
|
|
286
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
287
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
const toolConfig = bedrockTools.length > 0 ? { tools: bedrockTools, toolChoice: bedrockToolChoice } : {};
|
|
293
|
+
return {
|
|
294
|
+
toolConfig,
|
|
295
|
+
additionalTools,
|
|
296
|
+
betas,
|
|
297
|
+
toolWarnings
|
|
298
|
+
};
|
|
213
299
|
}
|
|
214
300
|
|
|
215
301
|
// src/convert-to-bedrock-chat-messages.ts
|
|
216
302
|
var import_provider3 = require("@ai-sdk/provider");
|
|
217
|
-
var
|
|
218
|
-
var generateFileId = (0, import_provider_utils2.createIdGenerator)({ prefix: "file", size: 16 });
|
|
303
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
219
304
|
function getCachePoint(providerMetadata) {
|
|
220
305
|
var _a;
|
|
221
306
|
return (_a = providerMetadata == null ? void 0 : providerMetadata.bedrock) == null ? void 0 : _a.cachePoint;
|
|
222
307
|
}
|
|
223
|
-
function convertToBedrockChatMessages(prompt) {
|
|
224
|
-
var _a, _b, _c, _d;
|
|
308
|
+
async function convertToBedrockChatMessages(prompt) {
|
|
225
309
|
const blocks = groupIntoBlocks(prompt);
|
|
226
310
|
let system = [];
|
|
227
311
|
const messages = [];
|
|
312
|
+
let documentCounter = 0;
|
|
313
|
+
const generateDocumentName = () => `document-${++documentCounter}`;
|
|
228
314
|
for (let i = 0; i < blocks.length; i++) {
|
|
229
315
|
const block = blocks[i];
|
|
230
316
|
const isLastBlock = i === blocks.length - 1;
|
|
@@ -266,21 +352,24 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
266
352
|
});
|
|
267
353
|
}
|
|
268
354
|
if (part.mediaType.startsWith("image/")) {
|
|
269
|
-
const bedrockImageFormat = part.mediaType === "image/*" ? void 0 : (_b = (_a = part.mediaType) == null ? void 0 : _a.split("/")) == null ? void 0 : _b[1];
|
|
270
355
|
bedrockContent.push({
|
|
271
356
|
image: {
|
|
272
|
-
format:
|
|
273
|
-
source: { bytes: (0,
|
|
357
|
+
format: getBedrockImageFormat(part.mediaType),
|
|
358
|
+
source: { bytes: (0, import_provider_utils3.convertToBase64)(part.data) }
|
|
274
359
|
}
|
|
275
360
|
});
|
|
276
361
|
} else {
|
|
362
|
+
if (!part.mediaType) {
|
|
363
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
364
|
+
functionality: "file without mime type",
|
|
365
|
+
message: "File mime type is required in user message part content"
|
|
366
|
+
});
|
|
367
|
+
}
|
|
277
368
|
bedrockContent.push({
|
|
278
369
|
document: {
|
|
279
|
-
format: (
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
name: generateFileId(),
|
|
283
|
-
source: { bytes: (0, import_provider_utils2.convertToBase64)(part.data) }
|
|
370
|
+
format: getBedrockDocumentFormat(part.mediaType),
|
|
371
|
+
name: generateDocumentName(),
|
|
372
|
+
source: { bytes: (0, import_provider_utils3.convertToBase64)(part.data) }
|
|
284
373
|
}
|
|
285
374
|
});
|
|
286
375
|
}
|
|
@@ -291,36 +380,46 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
291
380
|
break;
|
|
292
381
|
}
|
|
293
382
|
case "tool": {
|
|
294
|
-
for (
|
|
295
|
-
|
|
296
|
-
const
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
const format = part2.mediaType.split("/")[1];
|
|
309
|
-
if (!isBedrockImageFormat(format)) {
|
|
310
|
-
throw new Error(
|
|
311
|
-
`Unsupported image format: ${format}`
|
|
312
|
-
);
|
|
313
|
-
}
|
|
314
|
-
return {
|
|
315
|
-
image: {
|
|
316
|
-
format,
|
|
317
|
-
source: {
|
|
318
|
-
bytes: part2.data
|
|
383
|
+
for (const part of content) {
|
|
384
|
+
let toolResultContent;
|
|
385
|
+
const output = part.output;
|
|
386
|
+
switch (output.type) {
|
|
387
|
+
case "content": {
|
|
388
|
+
toolResultContent = output.value.map((contentPart) => {
|
|
389
|
+
switch (contentPart.type) {
|
|
390
|
+
case "text":
|
|
391
|
+
return { text: contentPart.text };
|
|
392
|
+
case "media":
|
|
393
|
+
if (!contentPart.mediaType.startsWith("image/")) {
|
|
394
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
395
|
+
functionality: `media type: ${contentPart.mediaType}`
|
|
396
|
+
});
|
|
319
397
|
}
|
|
320
|
-
|
|
321
|
-
|
|
398
|
+
const format = getBedrockImageFormat(
|
|
399
|
+
contentPart.mediaType
|
|
400
|
+
);
|
|
401
|
+
return {
|
|
402
|
+
image: {
|
|
403
|
+
format,
|
|
404
|
+
source: { bytes: contentPart.data }
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
break;
|
|
322
410
|
}
|
|
323
|
-
|
|
411
|
+
case "text":
|
|
412
|
+
case "error-text":
|
|
413
|
+
toolResultContent = [{ text: output.value }];
|
|
414
|
+
break;
|
|
415
|
+
case "json":
|
|
416
|
+
case "error-json":
|
|
417
|
+
default:
|
|
418
|
+
toolResultContent = [
|
|
419
|
+
{ text: JSON.stringify(output.value) }
|
|
420
|
+
];
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
324
423
|
bedrockContent.push({
|
|
325
424
|
toolResult: {
|
|
326
425
|
toolUseId: part.toolCallId,
|
|
@@ -369,32 +468,39 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
369
468
|
break;
|
|
370
469
|
}
|
|
371
470
|
case "reasoning": {
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
// because Bedrock does not allow trailing whitespace
|
|
377
|
-
// in pre-filled assistant responses
|
|
378
|
-
text: trimIfLast(
|
|
379
|
-
isLastBlock,
|
|
380
|
-
isLastMessage,
|
|
381
|
-
isLastContentPart,
|
|
382
|
-
part.text
|
|
383
|
-
),
|
|
384
|
-
signature: part.signature
|
|
385
|
-
}
|
|
386
|
-
}
|
|
471
|
+
const reasoningMetadata = await (0, import_provider_utils3.parseProviderOptions)({
|
|
472
|
+
provider: "bedrock",
|
|
473
|
+
providerOptions: part.providerOptions,
|
|
474
|
+
schema: bedrockReasoningMetadataSchema
|
|
387
475
|
});
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
476
|
+
if (reasoningMetadata != null) {
|
|
477
|
+
if (reasoningMetadata.signature != null) {
|
|
478
|
+
bedrockContent.push({
|
|
479
|
+
reasoningContent: {
|
|
480
|
+
reasoningText: {
|
|
481
|
+
// trim the last text part if it's the last message in the block
|
|
482
|
+
// because Bedrock does not allow trailing whitespace
|
|
483
|
+
// in pre-filled assistant responses
|
|
484
|
+
text: trimIfLast(
|
|
485
|
+
isLastBlock,
|
|
486
|
+
isLastMessage,
|
|
487
|
+
isLastContentPart,
|
|
488
|
+
part.text
|
|
489
|
+
),
|
|
490
|
+
signature: reasoningMetadata.signature
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
} else if (reasoningMetadata.redactedData != null) {
|
|
495
|
+
bedrockContent.push({
|
|
496
|
+
reasoningContent: {
|
|
497
|
+
redactedReasoning: {
|
|
498
|
+
data: reasoningMetadata.redactedData
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
});
|
|
396
502
|
}
|
|
397
|
-
}
|
|
503
|
+
}
|
|
398
504
|
break;
|
|
399
505
|
}
|
|
400
506
|
case "tool-call": {
|
|
@@ -402,7 +508,7 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
402
508
|
toolUse: {
|
|
403
509
|
toolUseId: part.toolCallId,
|
|
404
510
|
name: part.toolName,
|
|
405
|
-
input: part.
|
|
511
|
+
input: part.input
|
|
406
512
|
}
|
|
407
513
|
});
|
|
408
514
|
break;
|
|
@@ -424,8 +530,31 @@ function convertToBedrockChatMessages(prompt) {
|
|
|
424
530
|
}
|
|
425
531
|
return { system, messages };
|
|
426
532
|
}
|
|
427
|
-
function
|
|
428
|
-
|
|
533
|
+
function getBedrockImageFormat(mimeType) {
|
|
534
|
+
if (!mimeType) {
|
|
535
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
536
|
+
functionality: "image without mime type",
|
|
537
|
+
message: "Image mime type is required in user message part content"
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
const format = BEDROCK_IMAGE_MIME_TYPES[mimeType];
|
|
541
|
+
if (!format) {
|
|
542
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
543
|
+
functionality: `image mime type: ${mimeType}`,
|
|
544
|
+
message: `Unsupported image mime type: ${mimeType}, expected one of: ${Object.keys(BEDROCK_IMAGE_MIME_TYPES).join(", ")}`
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
return format;
|
|
548
|
+
}
|
|
549
|
+
function getBedrockDocumentFormat(mimeType) {
|
|
550
|
+
const format = BEDROCK_DOCUMENT_MIME_TYPES[mimeType];
|
|
551
|
+
if (!format) {
|
|
552
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
553
|
+
functionality: `file mime type: ${mimeType}`,
|
|
554
|
+
message: `Unsupported file mime type: ${mimeType}, expected one of: ${Object.keys(BEDROCK_DOCUMENT_MIME_TYPES).join(", ")}`
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
return format;
|
|
429
558
|
}
|
|
430
559
|
function trimIfLast(isLastBlock, isLastMessage, isLastContentPart, text) {
|
|
431
560
|
return isLastBlock && isLastMessage && isLastContentPart ? text.trim() : text;
|
|
@@ -502,8 +631,11 @@ var BedrockChatLanguageModel = class {
|
|
|
502
631
|
this.config = config;
|
|
503
632
|
this.specificationVersion = "v2";
|
|
504
633
|
this.provider = "amazon-bedrock";
|
|
634
|
+
this.supportedUrls = {
|
|
635
|
+
// no supported urls for bedrock
|
|
636
|
+
};
|
|
505
637
|
}
|
|
506
|
-
getArgs({
|
|
638
|
+
async getArgs({
|
|
507
639
|
prompt,
|
|
508
640
|
maxOutputTokens,
|
|
509
641
|
temperature,
|
|
@@ -518,8 +650,8 @@ var BedrockChatLanguageModel = class {
|
|
|
518
650
|
toolChoice,
|
|
519
651
|
providerOptions
|
|
520
652
|
}) {
|
|
521
|
-
var _a, _b, _c, _d, _e;
|
|
522
|
-
const bedrockOptions = (_a = (0,
|
|
653
|
+
var _a, _b, _c, _d, _e, _f;
|
|
654
|
+
const bedrockOptions = (_a = await (0, import_provider_utils4.parseProviderOptions)({
|
|
523
655
|
provider: "bedrock",
|
|
524
656
|
providerOptions,
|
|
525
657
|
schema: bedrockProviderOptions
|
|
@@ -549,14 +681,39 @@ var BedrockChatLanguageModel = class {
|
|
|
549
681
|
setting: "topK"
|
|
550
682
|
});
|
|
551
683
|
}
|
|
552
|
-
if (responseFormat != null && responseFormat.type !== "text") {
|
|
684
|
+
if (responseFormat != null && responseFormat.type !== "text" && responseFormat.type !== "json") {
|
|
553
685
|
warnings.push({
|
|
554
686
|
type: "unsupported-setting",
|
|
555
687
|
setting: "responseFormat",
|
|
556
|
-
details: "
|
|
688
|
+
details: "Only text and json response formats are supported."
|
|
557
689
|
});
|
|
558
690
|
}
|
|
559
|
-
|
|
691
|
+
if (tools != null && (responseFormat == null ? void 0 : responseFormat.type) === "json") {
|
|
692
|
+
if (tools.length > 0) {
|
|
693
|
+
warnings.push({
|
|
694
|
+
type: "other",
|
|
695
|
+
message: "JSON response format does not support tools. The provided tools are ignored."
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
|
|
700
|
+
type: "function",
|
|
701
|
+
name: "json",
|
|
702
|
+
description: "Respond with a JSON object.",
|
|
703
|
+
inputSchema: responseFormat.schema
|
|
704
|
+
} : void 0;
|
|
705
|
+
const { toolConfig, additionalTools, toolWarnings, betas } = prepareTools({
|
|
706
|
+
tools: jsonResponseTool ? [jsonResponseTool, ...tools != null ? tools : []] : tools,
|
|
707
|
+
toolChoice: jsonResponseTool != null ? { type: "tool", toolName: jsonResponseTool.name } : toolChoice,
|
|
708
|
+
modelId: this.modelId
|
|
709
|
+
});
|
|
710
|
+
warnings.push(...toolWarnings);
|
|
711
|
+
if (additionalTools) {
|
|
712
|
+
bedrockOptions.additionalModelRequestFields = {
|
|
713
|
+
...bedrockOptions.additionalModelRequestFields,
|
|
714
|
+
...additionalTools
|
|
715
|
+
};
|
|
716
|
+
}
|
|
560
717
|
const isThinking = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled";
|
|
561
718
|
const thinkingBudget = (_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.budgetTokens;
|
|
562
719
|
const inferenceConfig = {
|
|
@@ -573,7 +730,7 @@ var BedrockChatLanguageModel = class {
|
|
|
573
730
|
}
|
|
574
731
|
bedrockOptions.additionalModelRequestFields = {
|
|
575
732
|
...bedrockOptions.additionalModelRequestFields,
|
|
576
|
-
|
|
733
|
+
thinking: {
|
|
577
734
|
type: (_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.type,
|
|
578
735
|
budget_tokens: thinkingBudget
|
|
579
736
|
}
|
|
@@ -595,7 +752,34 @@ var BedrockChatLanguageModel = class {
|
|
|
595
752
|
details: "topP is not supported when thinking is enabled"
|
|
596
753
|
});
|
|
597
754
|
}
|
|
598
|
-
const
|
|
755
|
+
const hasAnyTools = ((_f = (_e = toolConfig.tools) == null ? void 0 : _e.length) != null ? _f : 0) > 0 || additionalTools;
|
|
756
|
+
let filteredPrompt = prompt;
|
|
757
|
+
if (!hasAnyTools) {
|
|
758
|
+
const hasToolContent = prompt.some(
|
|
759
|
+
(message) => "content" in message && Array.isArray(message.content) && message.content.some(
|
|
760
|
+
(part) => part.type === "tool-call" || part.type === "tool-result"
|
|
761
|
+
)
|
|
762
|
+
);
|
|
763
|
+
if (hasToolContent) {
|
|
764
|
+
filteredPrompt = prompt.map(
|
|
765
|
+
(message) => message.role === "system" ? message : {
|
|
766
|
+
...message,
|
|
767
|
+
content: message.content.filter(
|
|
768
|
+
(part) => part.type !== "tool-call" && part.type !== "tool-result"
|
|
769
|
+
)
|
|
770
|
+
}
|
|
771
|
+
).filter(
|
|
772
|
+
(message) => message.role === "system" || message.content.length > 0
|
|
773
|
+
);
|
|
774
|
+
warnings.push({
|
|
775
|
+
type: "unsupported-setting",
|
|
776
|
+
setting: "toolContent",
|
|
777
|
+
details: "Tool calls and results removed from conversation because Bedrock does not support tool content without active tools."
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
const { system, messages } = await convertToBedrockChatMessages(filteredPrompt);
|
|
782
|
+
const { reasoningConfig: _, ...filteredBedrockOptions } = (providerOptions == null ? void 0 : providerOptions.bedrock) || {};
|
|
599
783
|
return {
|
|
600
784
|
command: {
|
|
601
785
|
system,
|
|
@@ -604,36 +788,45 @@ var BedrockChatLanguageModel = class {
|
|
|
604
788
|
...Object.keys(inferenceConfig).length > 0 && {
|
|
605
789
|
inferenceConfig
|
|
606
790
|
},
|
|
607
|
-
...
|
|
608
|
-
...
|
|
791
|
+
...filteredBedrockOptions,
|
|
792
|
+
...toolConfig.tools !== void 0 && toolConfig.tools.length > 0 ? { toolConfig } : {}
|
|
609
793
|
},
|
|
610
|
-
warnings
|
|
794
|
+
warnings,
|
|
795
|
+
usesJsonResponseTool: jsonResponseTool != null,
|
|
796
|
+
betas
|
|
611
797
|
};
|
|
612
798
|
}
|
|
613
|
-
async
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
799
|
+
async getHeaders({
|
|
800
|
+
betas,
|
|
801
|
+
headers
|
|
802
|
+
}) {
|
|
803
|
+
return (0, import_provider_utils4.combineHeaders)(
|
|
804
|
+
await (0, import_provider_utils4.resolve)(this.config.headers),
|
|
805
|
+
betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
|
|
806
|
+
headers
|
|
807
|
+
);
|
|
617
808
|
}
|
|
618
809
|
async doGenerate(options) {
|
|
619
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
620
|
-
const {
|
|
810
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
811
|
+
const {
|
|
812
|
+
command: args,
|
|
813
|
+
warnings,
|
|
814
|
+
usesJsonResponseTool,
|
|
815
|
+
betas
|
|
816
|
+
} = await this.getArgs(options);
|
|
621
817
|
const url = `${this.getUrl(this.modelId)}/converse`;
|
|
622
|
-
const { value: response, responseHeaders } = await (0,
|
|
818
|
+
const { value: response, responseHeaders } = await (0, import_provider_utils4.postJsonToApi)({
|
|
623
819
|
url,
|
|
624
|
-
headers: (
|
|
625
|
-
await (0, import_provider_utils3.resolve)(this.config.headers),
|
|
626
|
-
options.headers
|
|
627
|
-
),
|
|
820
|
+
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
628
821
|
body: args,
|
|
629
|
-
failedResponseHandler: (0,
|
|
822
|
+
failedResponseHandler: (0, import_provider_utils4.createJsonErrorResponseHandler)({
|
|
630
823
|
errorSchema: BedrockErrorSchema,
|
|
631
824
|
errorToMessage: (error) => {
|
|
632
825
|
var _a2;
|
|
633
826
|
return `${(_a2 = error.message) != null ? _a2 : "Unknown error"}`;
|
|
634
827
|
}
|
|
635
828
|
}),
|
|
636
|
-
successfulResponseHandler: (0,
|
|
829
|
+
successfulResponseHandler: (0, import_provider_utils4.createJsonResponseHandler)(
|
|
637
830
|
BedrockResponseSchema
|
|
638
831
|
),
|
|
639
832
|
abortSignal: options.abortSignal,
|
|
@@ -642,49 +835,60 @@ var BedrockChatLanguageModel = class {
|
|
|
642
835
|
const content = [];
|
|
643
836
|
for (const part of response.output.message.content) {
|
|
644
837
|
if (part.text) {
|
|
645
|
-
|
|
838
|
+
if (!usesJsonResponseTool) {
|
|
839
|
+
content.push({ type: "text", text: part.text });
|
|
840
|
+
}
|
|
646
841
|
}
|
|
647
842
|
if (part.reasoningContent) {
|
|
648
843
|
if ("reasoningText" in part.reasoningContent) {
|
|
649
|
-
|
|
844
|
+
const reasoning = {
|
|
650
845
|
type: "reasoning",
|
|
651
|
-
reasoningType: "text",
|
|
652
846
|
text: part.reasoningContent.reasoningText.text
|
|
653
|
-
}
|
|
847
|
+
};
|
|
654
848
|
if (part.reasoningContent.reasoningText.signature) {
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
}
|
|
849
|
+
reasoning.providerMetadata = {
|
|
850
|
+
bedrock: {
|
|
851
|
+
signature: part.reasoningContent.reasoningText.signature
|
|
852
|
+
}
|
|
853
|
+
};
|
|
660
854
|
}
|
|
855
|
+
content.push(reasoning);
|
|
661
856
|
} else if ("redactedReasoning" in part.reasoningContent) {
|
|
662
857
|
content.push({
|
|
663
858
|
type: "reasoning",
|
|
664
|
-
|
|
665
|
-
|
|
859
|
+
text: "",
|
|
860
|
+
providerMetadata: {
|
|
861
|
+
bedrock: {
|
|
862
|
+
redactedData: (_a = part.reasoningContent.redactedReasoning.data) != null ? _a : ""
|
|
863
|
+
}
|
|
864
|
+
}
|
|
666
865
|
});
|
|
667
866
|
}
|
|
668
867
|
}
|
|
669
868
|
if (part.toolUse) {
|
|
670
|
-
content.push(
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
869
|
+
content.push(
|
|
870
|
+
// when a json response tool is used, the tool call becomes the text:
|
|
871
|
+
usesJsonResponseTool ? {
|
|
872
|
+
type: "text",
|
|
873
|
+
text: JSON.stringify(part.toolUse.input)
|
|
874
|
+
} : {
|
|
875
|
+
type: "tool-call",
|
|
876
|
+
toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
|
|
877
|
+
toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
|
|
878
|
+
input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
|
|
879
|
+
}
|
|
880
|
+
);
|
|
677
881
|
}
|
|
678
882
|
}
|
|
679
|
-
const providerMetadata = response.trace || response.usage ? {
|
|
883
|
+
const providerMetadata = response.trace || response.usage || usesJsonResponseTool ? {
|
|
680
884
|
bedrock: {
|
|
681
885
|
...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
|
|
682
|
-
...response.usage && {
|
|
886
|
+
...((_h = response.usage) == null ? void 0 : _h.cacheWriteInputTokens) != null && {
|
|
683
887
|
usage: {
|
|
684
|
-
|
|
685
|
-
cacheWriteInputTokens: (_k = (_j = response.usage) == null ? void 0 : _j.cacheWriteInputTokens) != null ? _k : Number.NaN
|
|
888
|
+
cacheWriteInputTokens: response.usage.cacheWriteInputTokens
|
|
686
889
|
}
|
|
687
|
-
}
|
|
890
|
+
},
|
|
891
|
+
...usesJsonResponseTool && { isJsonResponseFromTool: true }
|
|
688
892
|
}
|
|
689
893
|
} : void 0;
|
|
690
894
|
return {
|
|
@@ -693,8 +897,10 @@ var BedrockChatLanguageModel = class {
|
|
|
693
897
|
response.stopReason
|
|
694
898
|
),
|
|
695
899
|
usage: {
|
|
696
|
-
inputTokens: (
|
|
697
|
-
outputTokens: (
|
|
900
|
+
inputTokens: (_i = response.usage) == null ? void 0 : _i.inputTokens,
|
|
901
|
+
outputTokens: (_j = response.usage) == null ? void 0 : _j.outputTokens,
|
|
902
|
+
totalTokens: ((_k = response.usage) == null ? void 0 : _k.inputTokens) + ((_l = response.usage) == null ? void 0 : _l.outputTokens),
|
|
903
|
+
cachedInputTokens: (_n = (_m = response.usage) == null ? void 0 : _m.cacheReadInputTokens) != null ? _n : void 0
|
|
698
904
|
},
|
|
699
905
|
response: {
|
|
700
906
|
// TODO add id, timestamp, etc
|
|
@@ -705,16 +911,18 @@ var BedrockChatLanguageModel = class {
|
|
|
705
911
|
};
|
|
706
912
|
}
|
|
707
913
|
async doStream(options) {
|
|
708
|
-
const {
|
|
914
|
+
const {
|
|
915
|
+
command: args,
|
|
916
|
+
warnings,
|
|
917
|
+
usesJsonResponseTool,
|
|
918
|
+
betas
|
|
919
|
+
} = await this.getArgs(options);
|
|
709
920
|
const url = `${this.getUrl(this.modelId)}/converse-stream`;
|
|
710
|
-
const { value: response, responseHeaders } = await (0,
|
|
921
|
+
const { value: response, responseHeaders } = await (0, import_provider_utils4.postJsonToApi)({
|
|
711
922
|
url,
|
|
712
|
-
headers: (
|
|
713
|
-
await (0, import_provider_utils3.resolve)(this.config.headers),
|
|
714
|
-
options.headers
|
|
715
|
-
),
|
|
923
|
+
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
716
924
|
body: args,
|
|
717
|
-
failedResponseHandler: (0,
|
|
925
|
+
failedResponseHandler: (0, import_provider_utils4.createJsonErrorResponseHandler)({
|
|
718
926
|
errorSchema: BedrockErrorSchema,
|
|
719
927
|
errorToMessage: (error) => `${error.type}: ${error.message}`
|
|
720
928
|
}),
|
|
@@ -725,10 +933,11 @@ var BedrockChatLanguageModel = class {
|
|
|
725
933
|
let finishReason = "unknown";
|
|
726
934
|
const usage = {
|
|
727
935
|
inputTokens: void 0,
|
|
728
|
-
outputTokens: void 0
|
|
936
|
+
outputTokens: void 0,
|
|
937
|
+
totalTokens: void 0
|
|
729
938
|
};
|
|
730
939
|
let providerMetadata = void 0;
|
|
731
|
-
const
|
|
940
|
+
const contentBlocks = {};
|
|
732
941
|
return {
|
|
733
942
|
stream: response.pipeThrough(
|
|
734
943
|
new TransformStream({
|
|
@@ -736,11 +945,14 @@ var BedrockChatLanguageModel = class {
|
|
|
736
945
|
controller.enqueue({ type: "stream-start", warnings });
|
|
737
946
|
},
|
|
738
947
|
transform(chunk, controller) {
|
|
739
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
948
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
|
|
740
949
|
function enqueueError(bedrockError) {
|
|
741
950
|
finishReason = "error";
|
|
742
951
|
controller.enqueue({ type: "error", error: bedrockError });
|
|
743
952
|
}
|
|
953
|
+
if (options.includeRawChunks) {
|
|
954
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
955
|
+
}
|
|
744
956
|
if (!chunk.success) {
|
|
745
957
|
enqueueError(chunk.error);
|
|
746
958
|
return;
|
|
@@ -770,87 +982,174 @@ var BedrockChatLanguageModel = class {
|
|
|
770
982
|
if (value.metadata) {
|
|
771
983
|
usage.inputTokens = (_b = (_a = value.metadata.usage) == null ? void 0 : _a.inputTokens) != null ? _b : usage.inputTokens;
|
|
772
984
|
usage.outputTokens = (_d = (_c = value.metadata.usage) == null ? void 0 : _c.outputTokens) != null ? _d : usage.outputTokens;
|
|
773
|
-
|
|
985
|
+
usage.totalTokens = ((_e = usage.inputTokens) != null ? _e : 0) + ((_f = usage.outputTokens) != null ? _f : 0);
|
|
986
|
+
usage.cachedInputTokens = (_h = (_g = value.metadata.usage) == null ? void 0 : _g.cacheReadInputTokens) != null ? _h : usage.cachedInputTokens;
|
|
987
|
+
const cacheUsage = ((_i = value.metadata.usage) == null ? void 0 : _i.cacheWriteInputTokens) != null ? {
|
|
774
988
|
usage: {
|
|
775
|
-
|
|
776
|
-
cacheWriteInputTokens: (_j = (_i = value.metadata.usage) == null ? void 0 : _i.cacheWriteInputTokens) != null ? _j : Number.NaN
|
|
989
|
+
cacheWriteInputTokens: value.metadata.usage.cacheWriteInputTokens
|
|
777
990
|
}
|
|
778
991
|
} : void 0;
|
|
779
992
|
const trace = value.metadata.trace ? {
|
|
780
993
|
trace: value.metadata.trace
|
|
781
994
|
} : void 0;
|
|
782
|
-
if (cacheUsage || trace) {
|
|
995
|
+
if (cacheUsage || trace || usesJsonResponseTool) {
|
|
783
996
|
providerMetadata = {
|
|
784
997
|
bedrock: {
|
|
785
998
|
...cacheUsage,
|
|
786
|
-
...trace
|
|
999
|
+
...trace,
|
|
1000
|
+
...usesJsonResponseTool && {
|
|
1001
|
+
isJsonResponseFromTool: true
|
|
1002
|
+
}
|
|
787
1003
|
}
|
|
788
1004
|
};
|
|
789
1005
|
}
|
|
790
1006
|
}
|
|
791
|
-
if (((
|
|
1007
|
+
if (((_j = value.contentBlockStart) == null ? void 0 : _j.contentBlockIndex) != null && !((_l = (_k = value.contentBlockStart) == null ? void 0 : _k.start) == null ? void 0 : _l.toolUse)) {
|
|
1008
|
+
const blockIndex = value.contentBlockStart.contentBlockIndex;
|
|
1009
|
+
contentBlocks[blockIndex] = { type: "text" };
|
|
792
1010
|
controller.enqueue({
|
|
793
|
-
type: "text",
|
|
794
|
-
|
|
1011
|
+
type: "text-start",
|
|
1012
|
+
id: String(blockIndex)
|
|
795
1013
|
});
|
|
796
1014
|
}
|
|
797
|
-
if (((
|
|
1015
|
+
if (((_m = value.contentBlockDelta) == null ? void 0 : _m.delta) && "text" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.text) {
|
|
1016
|
+
const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
|
|
1017
|
+
if (contentBlocks[blockIndex] == null) {
|
|
1018
|
+
contentBlocks[blockIndex] = { type: "text" };
|
|
1019
|
+
if (!usesJsonResponseTool) {
|
|
1020
|
+
controller.enqueue({
|
|
1021
|
+
type: "text-start",
|
|
1022
|
+
id: String(blockIndex)
|
|
1023
|
+
});
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
if (!usesJsonResponseTool) {
|
|
1027
|
+
controller.enqueue({
|
|
1028
|
+
type: "text-delta",
|
|
1029
|
+
id: String(blockIndex),
|
|
1030
|
+
delta: value.contentBlockDelta.delta.text
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
|
|
1035
|
+
const blockIndex = value.contentBlockStop.contentBlockIndex;
|
|
1036
|
+
const contentBlock = contentBlocks[blockIndex];
|
|
1037
|
+
if (contentBlock != null) {
|
|
1038
|
+
if (contentBlock.type === "reasoning") {
|
|
1039
|
+
controller.enqueue({
|
|
1040
|
+
type: "reasoning-end",
|
|
1041
|
+
id: String(blockIndex)
|
|
1042
|
+
});
|
|
1043
|
+
} else if (contentBlock.type === "text") {
|
|
1044
|
+
if (!usesJsonResponseTool) {
|
|
1045
|
+
controller.enqueue({
|
|
1046
|
+
type: "text-end",
|
|
1047
|
+
id: String(blockIndex)
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
} else if (contentBlock.type === "tool-call") {
|
|
1051
|
+
if (usesJsonResponseTool) {
|
|
1052
|
+
controller.enqueue({
|
|
1053
|
+
type: "text-start",
|
|
1054
|
+
id: String(blockIndex)
|
|
1055
|
+
});
|
|
1056
|
+
controller.enqueue({
|
|
1057
|
+
type: "text-delta",
|
|
1058
|
+
id: String(blockIndex),
|
|
1059
|
+
delta: contentBlock.jsonText
|
|
1060
|
+
});
|
|
1061
|
+
controller.enqueue({
|
|
1062
|
+
type: "text-end",
|
|
1063
|
+
id: String(blockIndex)
|
|
1064
|
+
});
|
|
1065
|
+
} else {
|
|
1066
|
+
controller.enqueue({
|
|
1067
|
+
type: "tool-input-end",
|
|
1068
|
+
id: contentBlock.toolCallId
|
|
1069
|
+
});
|
|
1070
|
+
controller.enqueue({
|
|
1071
|
+
type: "tool-call",
|
|
1072
|
+
toolCallId: contentBlock.toolCallId,
|
|
1073
|
+
toolName: contentBlock.toolName,
|
|
1074
|
+
input: contentBlock.jsonText
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
delete contentBlocks[blockIndex];
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
if (((_o = value.contentBlockDelta) == null ? void 0 : _o.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
|
|
1082
|
+
const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
|
|
798
1083
|
const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
|
|
799
1084
|
if ("text" in reasoningContent && reasoningContent.text) {
|
|
1085
|
+
if (contentBlocks[blockIndex] == null) {
|
|
1086
|
+
contentBlocks[blockIndex] = { type: "reasoning" };
|
|
1087
|
+
controller.enqueue({
|
|
1088
|
+
type: "reasoning-start",
|
|
1089
|
+
id: String(blockIndex)
|
|
1090
|
+
});
|
|
1091
|
+
}
|
|
800
1092
|
controller.enqueue({
|
|
801
|
-
type: "reasoning",
|
|
802
|
-
|
|
803
|
-
|
|
1093
|
+
type: "reasoning-delta",
|
|
1094
|
+
id: String(blockIndex),
|
|
1095
|
+
delta: reasoningContent.text
|
|
804
1096
|
});
|
|
805
1097
|
} else if ("signature" in reasoningContent && reasoningContent.signature) {
|
|
806
1098
|
controller.enqueue({
|
|
807
|
-
type: "reasoning",
|
|
808
|
-
|
|
809
|
-
|
|
1099
|
+
type: "reasoning-delta",
|
|
1100
|
+
id: String(blockIndex),
|
|
1101
|
+
delta: "",
|
|
1102
|
+
providerMetadata: {
|
|
1103
|
+
bedrock: {
|
|
1104
|
+
signature: reasoningContent.signature
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
810
1107
|
});
|
|
811
1108
|
} else if ("data" in reasoningContent && reasoningContent.data) {
|
|
812
1109
|
controller.enqueue({
|
|
813
|
-
type: "reasoning",
|
|
814
|
-
|
|
815
|
-
|
|
1110
|
+
type: "reasoning-delta",
|
|
1111
|
+
id: String(blockIndex),
|
|
1112
|
+
delta: "",
|
|
1113
|
+
providerMetadata: {
|
|
1114
|
+
bedrock: {
|
|
1115
|
+
redactedData: reasoningContent.data
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
816
1118
|
});
|
|
817
1119
|
}
|
|
818
1120
|
}
|
|
819
1121
|
const contentBlockStart = value.contentBlockStart;
|
|
820
|
-
if (((
|
|
1122
|
+
if (((_p = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _p.toolUse) != null) {
|
|
821
1123
|
const toolUse = contentBlockStart.start.toolUse;
|
|
822
|
-
|
|
1124
|
+
const blockIndex = contentBlockStart.contentBlockIndex;
|
|
1125
|
+
contentBlocks[blockIndex] = {
|
|
1126
|
+
type: "tool-call",
|
|
823
1127
|
toolCallId: toolUse.toolUseId,
|
|
824
1128
|
toolName: toolUse.name,
|
|
825
1129
|
jsonText: ""
|
|
826
1130
|
};
|
|
1131
|
+
if (!usesJsonResponseTool) {
|
|
1132
|
+
controller.enqueue({
|
|
1133
|
+
type: "tool-input-start",
|
|
1134
|
+
id: toolUse.toolUseId,
|
|
1135
|
+
toolName: toolUse.name
|
|
1136
|
+
});
|
|
1137
|
+
}
|
|
827
1138
|
}
|
|
828
1139
|
const contentBlockDelta = value.contentBlockDelta;
|
|
829
1140
|
if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
|
|
830
|
-
const
|
|
831
|
-
const
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
if (contentBlockStop != null) {
|
|
843
|
-
const index = contentBlockStop.contentBlockIndex;
|
|
844
|
-
const contentBlock = toolCallContentBlocks[index];
|
|
845
|
-
if (contentBlock != null) {
|
|
846
|
-
controller.enqueue({
|
|
847
|
-
type: "tool-call",
|
|
848
|
-
toolCallType: "function",
|
|
849
|
-
toolCallId: contentBlock.toolCallId,
|
|
850
|
-
toolName: contentBlock.toolName,
|
|
851
|
-
args: contentBlock.jsonText
|
|
852
|
-
});
|
|
853
|
-
delete toolCallContentBlocks[index];
|
|
1141
|
+
const blockIndex = contentBlockDelta.contentBlockIndex;
|
|
1142
|
+
const contentBlock = contentBlocks[blockIndex];
|
|
1143
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
|
|
1144
|
+
const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
|
|
1145
|
+
if (!usesJsonResponseTool) {
|
|
1146
|
+
controller.enqueue({
|
|
1147
|
+
type: "tool-input-delta",
|
|
1148
|
+
id: contentBlock.toolCallId,
|
|
1149
|
+
delta
|
|
1150
|
+
});
|
|
1151
|
+
}
|
|
1152
|
+
contentBlock.jsonText += delta;
|
|
854
1153
|
}
|
|
855
1154
|
}
|
|
856
1155
|
},
|
|
@@ -873,129 +1172,134 @@ var BedrockChatLanguageModel = class {
|
|
|
873
1172
|
return `${this.config.baseUrl()}/model/${encodedModelId}`;
|
|
874
1173
|
}
|
|
875
1174
|
};
|
|
876
|
-
var BedrockStopReasonSchema =
|
|
877
|
-
|
|
878
|
-
|
|
1175
|
+
var BedrockStopReasonSchema = import_v43.z.union([
|
|
1176
|
+
import_v43.z.enum(BEDROCK_STOP_REASONS),
|
|
1177
|
+
import_v43.z.string()
|
|
879
1178
|
]);
|
|
880
|
-
var BedrockToolUseSchema =
|
|
881
|
-
toolUseId:
|
|
882
|
-
name:
|
|
883
|
-
input:
|
|
1179
|
+
var BedrockToolUseSchema = import_v43.z.object({
|
|
1180
|
+
toolUseId: import_v43.z.string(),
|
|
1181
|
+
name: import_v43.z.string(),
|
|
1182
|
+
input: import_v43.z.unknown()
|
|
884
1183
|
});
|
|
885
|
-
var BedrockReasoningTextSchema =
|
|
886
|
-
signature:
|
|
887
|
-
text:
|
|
1184
|
+
var BedrockReasoningTextSchema = import_v43.z.object({
|
|
1185
|
+
signature: import_v43.z.string().nullish(),
|
|
1186
|
+
text: import_v43.z.string()
|
|
888
1187
|
});
|
|
889
|
-
var BedrockRedactedReasoningSchema =
|
|
890
|
-
data:
|
|
1188
|
+
var BedrockRedactedReasoningSchema = import_v43.z.object({
|
|
1189
|
+
data: import_v43.z.string()
|
|
891
1190
|
});
|
|
892
|
-
var BedrockResponseSchema =
|
|
893
|
-
metrics:
|
|
894
|
-
latencyMs:
|
|
1191
|
+
var BedrockResponseSchema = import_v43.z.object({
|
|
1192
|
+
metrics: import_v43.z.object({
|
|
1193
|
+
latencyMs: import_v43.z.number()
|
|
895
1194
|
}).nullish(),
|
|
896
|
-
output:
|
|
897
|
-
message:
|
|
898
|
-
content:
|
|
899
|
-
|
|
900
|
-
text:
|
|
1195
|
+
output: import_v43.z.object({
|
|
1196
|
+
message: import_v43.z.object({
|
|
1197
|
+
content: import_v43.z.array(
|
|
1198
|
+
import_v43.z.object({
|
|
1199
|
+
text: import_v43.z.string().nullish(),
|
|
901
1200
|
toolUse: BedrockToolUseSchema.nullish(),
|
|
902
|
-
reasoningContent:
|
|
903
|
-
|
|
1201
|
+
reasoningContent: import_v43.z.union([
|
|
1202
|
+
import_v43.z.object({
|
|
904
1203
|
reasoningText: BedrockReasoningTextSchema
|
|
905
1204
|
}),
|
|
906
|
-
|
|
1205
|
+
import_v43.z.object({
|
|
907
1206
|
redactedReasoning: BedrockRedactedReasoningSchema
|
|
908
1207
|
})
|
|
909
1208
|
]).nullish()
|
|
910
1209
|
})
|
|
911
1210
|
),
|
|
912
|
-
role:
|
|
1211
|
+
role: import_v43.z.string()
|
|
913
1212
|
})
|
|
914
1213
|
}),
|
|
915
1214
|
stopReason: BedrockStopReasonSchema,
|
|
916
|
-
trace:
|
|
917
|
-
usage:
|
|
918
|
-
inputTokens:
|
|
919
|
-
outputTokens:
|
|
920
|
-
totalTokens:
|
|
921
|
-
cacheReadInputTokens:
|
|
922
|
-
cacheWriteInputTokens:
|
|
1215
|
+
trace: import_v43.z.unknown().nullish(),
|
|
1216
|
+
usage: import_v43.z.object({
|
|
1217
|
+
inputTokens: import_v43.z.number(),
|
|
1218
|
+
outputTokens: import_v43.z.number(),
|
|
1219
|
+
totalTokens: import_v43.z.number(),
|
|
1220
|
+
cacheReadInputTokens: import_v43.z.number().nullish(),
|
|
1221
|
+
cacheWriteInputTokens: import_v43.z.number().nullish()
|
|
923
1222
|
})
|
|
924
1223
|
});
|
|
925
|
-
var BedrockStreamSchema =
|
|
926
|
-
contentBlockDelta:
|
|
927
|
-
contentBlockIndex:
|
|
928
|
-
delta:
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
reasoningContent:
|
|
1224
|
+
var BedrockStreamSchema = import_v43.z.object({
|
|
1225
|
+
contentBlockDelta: import_v43.z.object({
|
|
1226
|
+
contentBlockIndex: import_v43.z.number(),
|
|
1227
|
+
delta: import_v43.z.union([
|
|
1228
|
+
import_v43.z.object({ text: import_v43.z.string() }),
|
|
1229
|
+
import_v43.z.object({ toolUse: import_v43.z.object({ input: import_v43.z.string() }) }),
|
|
1230
|
+
import_v43.z.object({
|
|
1231
|
+
reasoningContent: import_v43.z.object({ text: import_v43.z.string() })
|
|
933
1232
|
}),
|
|
934
|
-
|
|
935
|
-
reasoningContent:
|
|
936
|
-
signature:
|
|
1233
|
+
import_v43.z.object({
|
|
1234
|
+
reasoningContent: import_v43.z.object({
|
|
1235
|
+
signature: import_v43.z.string()
|
|
937
1236
|
})
|
|
938
1237
|
}),
|
|
939
|
-
|
|
940
|
-
reasoningContent:
|
|
1238
|
+
import_v43.z.object({
|
|
1239
|
+
reasoningContent: import_v43.z.object({ data: import_v43.z.string() })
|
|
941
1240
|
})
|
|
942
1241
|
]).nullish()
|
|
943
1242
|
}).nullish(),
|
|
944
|
-
contentBlockStart:
|
|
945
|
-
contentBlockIndex:
|
|
946
|
-
start:
|
|
1243
|
+
contentBlockStart: import_v43.z.object({
|
|
1244
|
+
contentBlockIndex: import_v43.z.number(),
|
|
1245
|
+
start: import_v43.z.object({
|
|
947
1246
|
toolUse: BedrockToolUseSchema.nullish()
|
|
948
1247
|
}).nullish()
|
|
949
1248
|
}).nullish(),
|
|
950
|
-
contentBlockStop:
|
|
951
|
-
contentBlockIndex:
|
|
1249
|
+
contentBlockStop: import_v43.z.object({
|
|
1250
|
+
contentBlockIndex: import_v43.z.number()
|
|
952
1251
|
}).nullish(),
|
|
953
|
-
internalServerException:
|
|
954
|
-
messageStop:
|
|
955
|
-
additionalModelResponseFields:
|
|
1252
|
+
internalServerException: import_v43.z.record(import_v43.z.string(), import_v43.z.unknown()).nullish(),
|
|
1253
|
+
messageStop: import_v43.z.object({
|
|
1254
|
+
additionalModelResponseFields: import_v43.z.record(import_v43.z.string(), import_v43.z.unknown()).nullish(),
|
|
956
1255
|
stopReason: BedrockStopReasonSchema
|
|
957
1256
|
}).nullish(),
|
|
958
|
-
metadata:
|
|
959
|
-
trace:
|
|
960
|
-
usage:
|
|
961
|
-
cacheReadInputTokens:
|
|
962
|
-
cacheWriteInputTokens:
|
|
963
|
-
inputTokens:
|
|
964
|
-
outputTokens:
|
|
1257
|
+
metadata: import_v43.z.object({
|
|
1258
|
+
trace: import_v43.z.unknown().nullish(),
|
|
1259
|
+
usage: import_v43.z.object({
|
|
1260
|
+
cacheReadInputTokens: import_v43.z.number().nullish(),
|
|
1261
|
+
cacheWriteInputTokens: import_v43.z.number().nullish(),
|
|
1262
|
+
inputTokens: import_v43.z.number(),
|
|
1263
|
+
outputTokens: import_v43.z.number()
|
|
965
1264
|
}).nullish()
|
|
966
1265
|
}).nullish(),
|
|
967
|
-
modelStreamErrorException:
|
|
968
|
-
throttlingException:
|
|
969
|
-
validationException:
|
|
1266
|
+
modelStreamErrorException: import_v43.z.record(import_v43.z.string(), import_v43.z.unknown()).nullish(),
|
|
1267
|
+
throttlingException: import_v43.z.record(import_v43.z.string(), import_v43.z.unknown()).nullish(),
|
|
1268
|
+
validationException: import_v43.z.record(import_v43.z.string(), import_v43.z.unknown()).nullish()
|
|
1269
|
+
});
|
|
1270
|
+
var bedrockReasoningMetadataSchema = import_v43.z.object({
|
|
1271
|
+
signature: import_v43.z.string().optional(),
|
|
1272
|
+
redactedData: import_v43.z.string().optional()
|
|
970
1273
|
});
|
|
971
1274
|
|
|
972
1275
|
// src/bedrock-embedding-model.ts
|
|
973
|
-
var
|
|
1276
|
+
var import_provider4 = require("@ai-sdk/provider");
|
|
1277
|
+
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
|
974
1278
|
|
|
975
1279
|
// src/bedrock-embedding-options.ts
|
|
976
|
-
var
|
|
977
|
-
var bedrockEmbeddingProviderOptions =
|
|
1280
|
+
var import_v44 = require("zod/v4");
|
|
1281
|
+
var bedrockEmbeddingProviderOptions = import_v44.z.object({
|
|
978
1282
|
/**
|
|
979
1283
|
The number of dimensions the resulting output embeddings should have (defaults to 1024).
|
|
980
1284
|
Only supported in amazon.titan-embed-text-v2:0.
|
|
981
1285
|
*/
|
|
982
|
-
dimensions:
|
|
1286
|
+
dimensions: import_v44.z.union([import_v44.z.literal(1024), import_v44.z.literal(512), import_v44.z.literal(256)]).optional(),
|
|
983
1287
|
/**
|
|
984
1288
|
Flag indicating whether or not to normalize the output embeddings. Defaults to true
|
|
985
1289
|
Only supported in amazon.titan-embed-text-v2:0.
|
|
986
1290
|
*/
|
|
987
|
-
normalize:
|
|
1291
|
+
normalize: import_v44.z.boolean().optional()
|
|
988
1292
|
});
|
|
989
1293
|
|
|
990
1294
|
// src/bedrock-embedding-model.ts
|
|
991
|
-
var
|
|
1295
|
+
var import_v45 = require("zod/v4");
|
|
992
1296
|
var BedrockEmbeddingModel = class {
|
|
993
1297
|
constructor(modelId, config) {
|
|
994
1298
|
this.modelId = modelId;
|
|
995
1299
|
this.config = config;
|
|
996
1300
|
this.specificationVersion = "v2";
|
|
997
1301
|
this.provider = "amazon-bedrock";
|
|
998
|
-
this.maxEmbeddingsPerCall =
|
|
1302
|
+
this.maxEmbeddingsPerCall = 1;
|
|
999
1303
|
this.supportsParallelCalls = true;
|
|
1000
1304
|
}
|
|
1001
1305
|
getUrl(modelId) {
|
|
@@ -1009,57 +1313,54 @@ var BedrockEmbeddingModel = class {
|
|
|
1009
1313
|
providerOptions
|
|
1010
1314
|
}) {
|
|
1011
1315
|
var _a;
|
|
1012
|
-
|
|
1316
|
+
if (values.length > this.maxEmbeddingsPerCall) {
|
|
1317
|
+
throw new import_provider4.TooManyEmbeddingValuesForCallError({
|
|
1318
|
+
provider: this.provider,
|
|
1319
|
+
modelId: this.modelId,
|
|
1320
|
+
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
1321
|
+
values
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
const bedrockOptions = (_a = await (0, import_provider_utils5.parseProviderOptions)({
|
|
1013
1325
|
provider: "bedrock",
|
|
1014
1326
|
providerOptions,
|
|
1015
1327
|
schema: bedrockEmbeddingProviderOptions
|
|
1016
1328
|
})) != null ? _a : {};
|
|
1017
|
-
const
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
inputTextTokenCount: response.inputTextTokenCount
|
|
1043
|
-
};
|
|
1329
|
+
const args = {
|
|
1330
|
+
inputText: values[0],
|
|
1331
|
+
dimensions: bedrockOptions.dimensions,
|
|
1332
|
+
normalize: bedrockOptions.normalize
|
|
1333
|
+
};
|
|
1334
|
+
const url = this.getUrl(this.modelId);
|
|
1335
|
+
const { value: response } = await (0, import_provider_utils5.postJsonToApi)({
|
|
1336
|
+
url,
|
|
1337
|
+
headers: await (0, import_provider_utils5.resolve)(
|
|
1338
|
+
(0, import_provider_utils5.combineHeaders)(await (0, import_provider_utils5.resolve)(this.config.headers), headers)
|
|
1339
|
+
),
|
|
1340
|
+
body: args,
|
|
1341
|
+
failedResponseHandler: (0, import_provider_utils5.createJsonErrorResponseHandler)({
|
|
1342
|
+
errorSchema: BedrockErrorSchema,
|
|
1343
|
+
errorToMessage: (error) => `${error.type}: ${error.message}`
|
|
1344
|
+
}),
|
|
1345
|
+
successfulResponseHandler: (0, import_provider_utils5.createJsonResponseHandler)(
|
|
1346
|
+
BedrockEmbeddingResponseSchema
|
|
1347
|
+
),
|
|
1348
|
+
fetch: this.config.fetch,
|
|
1349
|
+
abortSignal
|
|
1350
|
+
});
|
|
1351
|
+
return {
|
|
1352
|
+
embeddings: [response.embedding],
|
|
1353
|
+
usage: { tokens: response.inputTextTokenCount }
|
|
1044
1354
|
};
|
|
1045
|
-
const responses = await Promise.all(values.map(embedSingleText));
|
|
1046
|
-
return responses.reduce(
|
|
1047
|
-
(accumulated, response) => {
|
|
1048
|
-
accumulated.embeddings.push(response.embedding);
|
|
1049
|
-
accumulated.usage.tokens += response.inputTextTokenCount;
|
|
1050
|
-
return accumulated;
|
|
1051
|
-
},
|
|
1052
|
-
{ embeddings: [], usage: { tokens: 0 } }
|
|
1053
|
-
);
|
|
1054
1355
|
}
|
|
1055
1356
|
};
|
|
1056
|
-
var BedrockEmbeddingResponseSchema =
|
|
1057
|
-
embedding:
|
|
1058
|
-
inputTextTokenCount:
|
|
1357
|
+
var BedrockEmbeddingResponseSchema = import_v45.z.object({
|
|
1358
|
+
embedding: import_v45.z.array(import_v45.z.number()),
|
|
1359
|
+
inputTextTokenCount: import_v45.z.number()
|
|
1059
1360
|
});
|
|
1060
1361
|
|
|
1061
1362
|
// src/bedrock-image-model.ts
|
|
1062
|
-
var
|
|
1363
|
+
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
|
1063
1364
|
|
|
1064
1365
|
// src/bedrock-image-settings.ts
|
|
1065
1366
|
var modelMaxImagesPerCall = {
|
|
@@ -1067,18 +1368,17 @@ var modelMaxImagesPerCall = {
|
|
|
1067
1368
|
};
|
|
1068
1369
|
|
|
1069
1370
|
// src/bedrock-image-model.ts
|
|
1070
|
-
var
|
|
1371
|
+
var import_v46 = require("zod/v4");
|
|
1071
1372
|
var BedrockImageModel = class {
|
|
1072
|
-
constructor(modelId,
|
|
1373
|
+
constructor(modelId, config) {
|
|
1073
1374
|
this.modelId = modelId;
|
|
1074
|
-
this.settings = settings;
|
|
1075
1375
|
this.config = config;
|
|
1076
|
-
this.specificationVersion = "
|
|
1376
|
+
this.specificationVersion = "v2";
|
|
1077
1377
|
this.provider = "amazon-bedrock";
|
|
1078
1378
|
}
|
|
1079
1379
|
get maxImagesPerCall() {
|
|
1080
|
-
var _a
|
|
1081
|
-
return (
|
|
1380
|
+
var _a;
|
|
1381
|
+
return (_a = modelMaxImagesPerCall[this.modelId]) != null ? _a : 1;
|
|
1082
1382
|
}
|
|
1083
1383
|
getUrl(modelId) {
|
|
1084
1384
|
const encodedModelId = encodeURIComponent(modelId);
|
|
@@ -1094,7 +1394,7 @@ var BedrockImageModel = class {
|
|
|
1094
1394
|
headers,
|
|
1095
1395
|
abortSignal
|
|
1096
1396
|
}) {
|
|
1097
|
-
var _a, _b, _c, _d, _e, _f;
|
|
1397
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
1098
1398
|
const warnings = [];
|
|
1099
1399
|
const [width, height] = size ? size.split("x").map(Number) : [];
|
|
1100
1400
|
const args = {
|
|
@@ -1103,6 +1403,9 @@ var BedrockImageModel = class {
|
|
|
1103
1403
|
text: prompt,
|
|
1104
1404
|
...((_a = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _a.negativeText) ? {
|
|
1105
1405
|
negativeText: providerOptions.bedrock.negativeText
|
|
1406
|
+
} : {},
|
|
1407
|
+
...((_b = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _b.style) ? {
|
|
1408
|
+
style: providerOptions.bedrock.style
|
|
1106
1409
|
} : {}
|
|
1107
1410
|
},
|
|
1108
1411
|
imageGenerationConfig: {
|
|
@@ -1110,8 +1413,8 @@ var BedrockImageModel = class {
|
|
|
1110
1413
|
...height ? { height } : {},
|
|
1111
1414
|
...seed ? { seed } : {},
|
|
1112
1415
|
...n ? { numberOfImages: n } : {},
|
|
1113
|
-
...((
|
|
1114
|
-
...((
|
|
1416
|
+
...((_c = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _c.quality) ? { quality: providerOptions.bedrock.quality } : {},
|
|
1417
|
+
...((_d = providerOptions == null ? void 0 : providerOptions.bedrock) == null ? void 0 : _d.cfgScale) ? { cfgScale: providerOptions.bedrock.cfgScale } : {}
|
|
1115
1418
|
}
|
|
1116
1419
|
};
|
|
1117
1420
|
if (aspectRatio != void 0) {
|
|
@@ -1121,18 +1424,18 @@ var BedrockImageModel = class {
|
|
|
1121
1424
|
details: "This model does not support aspect ratio. Use `size` instead."
|
|
1122
1425
|
});
|
|
1123
1426
|
}
|
|
1124
|
-
const currentDate = (
|
|
1125
|
-
const { value: response, responseHeaders } = await (0,
|
|
1427
|
+
const currentDate = (_g = (_f = (_e = this.config._internal) == null ? void 0 : _e.currentDate) == null ? void 0 : _f.call(_e)) != null ? _g : /* @__PURE__ */ new Date();
|
|
1428
|
+
const { value: response, responseHeaders } = await (0, import_provider_utils6.postJsonToApi)({
|
|
1126
1429
|
url: this.getUrl(this.modelId),
|
|
1127
|
-
headers: await (0,
|
|
1128
|
-
(0,
|
|
1430
|
+
headers: await (0, import_provider_utils6.resolve)(
|
|
1431
|
+
(0, import_provider_utils6.combineHeaders)(await (0, import_provider_utils6.resolve)(this.config.headers), headers)
|
|
1129
1432
|
),
|
|
1130
1433
|
body: args,
|
|
1131
|
-
failedResponseHandler: (0,
|
|
1434
|
+
failedResponseHandler: (0, import_provider_utils6.createJsonErrorResponseHandler)({
|
|
1132
1435
|
errorSchema: BedrockErrorSchema,
|
|
1133
1436
|
errorToMessage: (error) => `${error.type}: ${error.message}`
|
|
1134
1437
|
}),
|
|
1135
|
-
successfulResponseHandler: (0,
|
|
1438
|
+
successfulResponseHandler: (0, import_provider_utils6.createJsonResponseHandler)(
|
|
1136
1439
|
bedrockImageResponseSchema
|
|
1137
1440
|
),
|
|
1138
1441
|
abortSignal,
|
|
@@ -1149,8 +1452,8 @@ var BedrockImageModel = class {
|
|
|
1149
1452
|
};
|
|
1150
1453
|
}
|
|
1151
1454
|
};
|
|
1152
|
-
var bedrockImageResponseSchema =
|
|
1153
|
-
images:
|
|
1455
|
+
var bedrockImageResponseSchema = import_v46.z.object({
|
|
1456
|
+
images: import_v46.z.array(import_v46.z.string())
|
|
1154
1457
|
});
|
|
1155
1458
|
|
|
1156
1459
|
// src/headers-utils.ts
|
|
@@ -1172,15 +1475,11 @@ function extractHeaders(headers) {
|
|
|
1172
1475
|
return originalHeaders;
|
|
1173
1476
|
}
|
|
1174
1477
|
function convertHeadersToRecord(headers) {
|
|
1175
|
-
|
|
1176
|
-
headers.forEach((value, key) => {
|
|
1177
|
-
record[key.toLowerCase()] = value;
|
|
1178
|
-
});
|
|
1179
|
-
return record;
|
|
1478
|
+
return Object.fromEntries([...headers]);
|
|
1180
1479
|
}
|
|
1181
1480
|
|
|
1182
1481
|
// src/bedrock-sigv4-fetch.ts
|
|
1183
|
-
var
|
|
1482
|
+
var import_provider_utils7 = require("@ai-sdk/provider-utils");
|
|
1184
1483
|
var import_aws4fetch = require("aws4fetch");
|
|
1185
1484
|
function createSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) {
|
|
1186
1485
|
return async (input, init) => {
|
|
@@ -1195,7 +1494,7 @@ function createSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) {
|
|
|
1195
1494
|
const signer = new import_aws4fetch.AwsV4Signer({
|
|
1196
1495
|
url,
|
|
1197
1496
|
method: "POST",
|
|
1198
|
-
headers: Object.entries((0,
|
|
1497
|
+
headers: Object.entries((0, import_provider_utils7.removeUndefinedEntries)(originalHeaders)),
|
|
1199
1498
|
body,
|
|
1200
1499
|
region: credentials.region,
|
|
1201
1500
|
accessKeyId: credentials.accessKeyId,
|
|
@@ -1208,8 +1507,8 @@ function createSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) {
|
|
|
1208
1507
|
return fetch(input, {
|
|
1209
1508
|
...init,
|
|
1210
1509
|
body,
|
|
1211
|
-
headers: (0,
|
|
1212
|
-
(0,
|
|
1510
|
+
headers: (0, import_provider_utils7.removeUndefinedEntries)(
|
|
1511
|
+
(0, import_provider_utils7.combineHeaders)(originalHeaders, signedHeaders)
|
|
1213
1512
|
)
|
|
1214
1513
|
});
|
|
1215
1514
|
};
|
|
@@ -1225,46 +1524,92 @@ function prepareBodyString(body) {
|
|
|
1225
1524
|
return JSON.stringify(body);
|
|
1226
1525
|
}
|
|
1227
1526
|
}
|
|
1527
|
+
function createApiKeyFetchFunction(apiKey, fetch = globalThis.fetch) {
|
|
1528
|
+
return async (input, init) => {
|
|
1529
|
+
const originalHeaders = extractHeaders(init == null ? void 0 : init.headers);
|
|
1530
|
+
return fetch(input, {
|
|
1531
|
+
...init,
|
|
1532
|
+
headers: (0, import_provider_utils7.removeUndefinedEntries)(
|
|
1533
|
+
(0, import_provider_utils7.combineHeaders)(originalHeaders, {
|
|
1534
|
+
Authorization: `Bearer ${apiKey}`
|
|
1535
|
+
})
|
|
1536
|
+
)
|
|
1537
|
+
});
|
|
1538
|
+
};
|
|
1539
|
+
}
|
|
1228
1540
|
|
|
1229
1541
|
// src/bedrock-provider.ts
|
|
1230
1542
|
function createAmazonBedrock(options = {}) {
|
|
1231
|
-
const
|
|
1232
|
-
|
|
1543
|
+
const rawApiKey = (0, import_provider_utils8.loadOptionalSetting)({
|
|
1544
|
+
settingValue: options.apiKey,
|
|
1545
|
+
environmentVariableName: "AWS_BEARER_TOKEN_BEDROCK"
|
|
1546
|
+
});
|
|
1547
|
+
const apiKey = rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : void 0;
|
|
1548
|
+
const fetchFunction = apiKey ? createApiKeyFetchFunction(apiKey, options.fetch) : createSigV4FetchFunction(async () => {
|
|
1549
|
+
const region = (0, import_provider_utils8.loadSetting)({
|
|
1233
1550
|
settingValue: options.region,
|
|
1234
1551
|
settingName: "region",
|
|
1235
1552
|
environmentVariableName: "AWS_REGION",
|
|
1236
1553
|
description: "AWS region"
|
|
1237
1554
|
});
|
|
1238
1555
|
if (options.credentialProvider) {
|
|
1556
|
+
try {
|
|
1557
|
+
return {
|
|
1558
|
+
...await options.credentialProvider(),
|
|
1559
|
+
region
|
|
1560
|
+
};
|
|
1561
|
+
} catch (error) {
|
|
1562
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1563
|
+
throw new Error(
|
|
1564
|
+
`AWS credential provider failed: ${errorMessage}. Please ensure your credential provider returns valid AWS credentials with accessKeyId and secretAccessKey properties.`
|
|
1565
|
+
);
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
try {
|
|
1239
1569
|
return {
|
|
1240
|
-
|
|
1241
|
-
|
|
1570
|
+
region,
|
|
1571
|
+
accessKeyId: (0, import_provider_utils8.loadSetting)({
|
|
1572
|
+
settingValue: options.accessKeyId,
|
|
1573
|
+
settingName: "accessKeyId",
|
|
1574
|
+
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
1575
|
+
description: "AWS access key ID"
|
|
1576
|
+
}),
|
|
1577
|
+
secretAccessKey: (0, import_provider_utils8.loadSetting)({
|
|
1578
|
+
settingValue: options.secretAccessKey,
|
|
1579
|
+
settingName: "secretAccessKey",
|
|
1580
|
+
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
1581
|
+
description: "AWS secret access key"
|
|
1582
|
+
}),
|
|
1583
|
+
sessionToken: (0, import_provider_utils8.loadOptionalSetting)({
|
|
1584
|
+
settingValue: options.sessionToken,
|
|
1585
|
+
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
1586
|
+
})
|
|
1242
1587
|
};
|
|
1588
|
+
} catch (error) {
|
|
1589
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1590
|
+
if (errorMessage.includes("AWS_ACCESS_KEY_ID") || errorMessage.includes("accessKeyId")) {
|
|
1591
|
+
throw new Error(
|
|
1592
|
+
`AWS SigV4 authentication requires AWS credentials. Please provide either:
|
|
1593
|
+
1. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables
|
|
1594
|
+
2. Provide accessKeyId and secretAccessKey in options
|
|
1595
|
+
3. Use a credentialProvider function
|
|
1596
|
+
4. Use API key authentication with AWS_BEARER_TOKEN_BEDROCK or apiKey option
|
|
1597
|
+
Original error: ${errorMessage}`
|
|
1598
|
+
);
|
|
1599
|
+
}
|
|
1600
|
+
if (errorMessage.includes("AWS_SECRET_ACCESS_KEY") || errorMessage.includes("secretAccessKey")) {
|
|
1601
|
+
throw new Error(
|
|
1602
|
+
`AWS SigV4 authentication requires both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Please ensure both credentials are provided.
|
|
1603
|
+
Original error: ${errorMessage}`
|
|
1604
|
+
);
|
|
1605
|
+
}
|
|
1606
|
+
throw error;
|
|
1243
1607
|
}
|
|
1244
|
-
return {
|
|
1245
|
-
region,
|
|
1246
|
-
accessKeyId: (0, import_provider_utils7.loadSetting)({
|
|
1247
|
-
settingValue: options.accessKeyId,
|
|
1248
|
-
settingName: "accessKeyId",
|
|
1249
|
-
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
1250
|
-
description: "AWS access key ID"
|
|
1251
|
-
}),
|
|
1252
|
-
secretAccessKey: (0, import_provider_utils7.loadSetting)({
|
|
1253
|
-
settingValue: options.secretAccessKey,
|
|
1254
|
-
settingName: "secretAccessKey",
|
|
1255
|
-
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
1256
|
-
description: "AWS secret access key"
|
|
1257
|
-
}),
|
|
1258
|
-
sessionToken: (0, import_provider_utils7.loadOptionalSetting)({
|
|
1259
|
-
settingValue: options.sessionToken,
|
|
1260
|
-
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
1261
|
-
})
|
|
1262
|
-
};
|
|
1263
1608
|
}, options.fetch);
|
|
1264
1609
|
const getBaseUrl = () => {
|
|
1265
1610
|
var _a, _b;
|
|
1266
|
-
return (_b = (0,
|
|
1267
|
-
(_a = options.baseURL) != null ? _a : `https://bedrock-runtime.${(0,
|
|
1611
|
+
return (_b = (0, import_provider_utils8.withoutTrailingSlash)(
|
|
1612
|
+
(_a = options.baseURL) != null ? _a : `https://bedrock-runtime.${(0, import_provider_utils8.loadSetting)({
|
|
1268
1613
|
settingValue: options.region,
|
|
1269
1614
|
settingName: "region",
|
|
1270
1615
|
environmentVariableName: "AWS_REGION",
|
|
@@ -1277,8 +1622,8 @@ function createAmazonBedrock(options = {}) {
|
|
|
1277
1622
|
return new BedrockChatLanguageModel(modelId, {
|
|
1278
1623
|
baseUrl: getBaseUrl,
|
|
1279
1624
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1280
|
-
fetch:
|
|
1281
|
-
generateId:
|
|
1625
|
+
fetch: fetchFunction,
|
|
1626
|
+
generateId: import_provider_utils8.generateId
|
|
1282
1627
|
});
|
|
1283
1628
|
};
|
|
1284
1629
|
const provider = function(modelId) {
|
|
@@ -1294,15 +1639,15 @@ function createAmazonBedrock(options = {}) {
|
|
|
1294
1639
|
return new BedrockEmbeddingModel(modelId, {
|
|
1295
1640
|
baseUrl: getBaseUrl,
|
|
1296
1641
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1297
|
-
fetch:
|
|
1642
|
+
fetch: fetchFunction
|
|
1298
1643
|
});
|
|
1299
1644
|
};
|
|
1300
|
-
const createImageModel = (modelId
|
|
1645
|
+
const createImageModel = (modelId) => {
|
|
1301
1646
|
var _a;
|
|
1302
|
-
return new BedrockImageModel(modelId,
|
|
1647
|
+
return new BedrockImageModel(modelId, {
|
|
1303
1648
|
baseUrl: getBaseUrl,
|
|
1304
1649
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1305
|
-
fetch:
|
|
1650
|
+
fetch: fetchFunction
|
|
1306
1651
|
});
|
|
1307
1652
|
};
|
|
1308
1653
|
provider.languageModel = createChatModel;
|
|
@@ -1311,6 +1656,7 @@ function createAmazonBedrock(options = {}) {
|
|
|
1311
1656
|
provider.textEmbeddingModel = createEmbeddingModel;
|
|
1312
1657
|
provider.image = createImageModel;
|
|
1313
1658
|
provider.imageModel = createImageModel;
|
|
1659
|
+
provider.tools = import_internal2.anthropicTools;
|
|
1314
1660
|
return provider;
|
|
1315
1661
|
}
|
|
1316
1662
|
var bedrock = createAmazonBedrock();
|