@ai-sdk/amazon-bedrock 3.0.0-beta.6 → 3.0.0-beta.7
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 +6 -0
- package/README.md +75 -0
- package/dist/index.d.mts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +71 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +71 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1361,10 +1361,28 @@ function prepareBodyString(body) {
|
|
|
1361
1361
|
return JSON.stringify(body);
|
|
1362
1362
|
}
|
|
1363
1363
|
}
|
|
1364
|
+
function createApiKeyFetchFunction(apiKey, fetch = globalThis.fetch) {
|
|
1365
|
+
return async (input, init) => {
|
|
1366
|
+
const originalHeaders = extractHeaders(init == null ? void 0 : init.headers);
|
|
1367
|
+
return fetch(input, {
|
|
1368
|
+
...init,
|
|
1369
|
+
headers: removeUndefinedEntries(
|
|
1370
|
+
combineHeaders4(originalHeaders, {
|
|
1371
|
+
Authorization: `Bearer ${apiKey}`
|
|
1372
|
+
})
|
|
1373
|
+
)
|
|
1374
|
+
});
|
|
1375
|
+
};
|
|
1376
|
+
}
|
|
1364
1377
|
|
|
1365
1378
|
// src/bedrock-provider.ts
|
|
1366
1379
|
function createAmazonBedrock(options = {}) {
|
|
1367
|
-
const
|
|
1380
|
+
const rawApiKey = loadOptionalSetting({
|
|
1381
|
+
settingValue: options.apiKey,
|
|
1382
|
+
environmentVariableName: "AWS_BEARER_TOKEN_BEDROCK"
|
|
1383
|
+
});
|
|
1384
|
+
const apiKey = rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : void 0;
|
|
1385
|
+
const fetchFunction = apiKey ? createApiKeyFetchFunction(apiKey, options.fetch) : createSigV4FetchFunction(async () => {
|
|
1368
1386
|
const region = loadSetting({
|
|
1369
1387
|
settingValue: options.region,
|
|
1370
1388
|
settingName: "region",
|
|
@@ -1372,30 +1390,58 @@ function createAmazonBedrock(options = {}) {
|
|
|
1372
1390
|
description: "AWS region"
|
|
1373
1391
|
});
|
|
1374
1392
|
if (options.credentialProvider) {
|
|
1393
|
+
try {
|
|
1394
|
+
return {
|
|
1395
|
+
...await options.credentialProvider(),
|
|
1396
|
+
region
|
|
1397
|
+
};
|
|
1398
|
+
} catch (error) {
|
|
1399
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1400
|
+
throw new Error(
|
|
1401
|
+
`AWS credential provider failed: ${errorMessage}. Please ensure your credential provider returns valid AWS credentials with accessKeyId and secretAccessKey properties.`
|
|
1402
|
+
);
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
try {
|
|
1375
1406
|
return {
|
|
1376
|
-
|
|
1377
|
-
|
|
1407
|
+
region,
|
|
1408
|
+
accessKeyId: loadSetting({
|
|
1409
|
+
settingValue: options.accessKeyId,
|
|
1410
|
+
settingName: "accessKeyId",
|
|
1411
|
+
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
1412
|
+
description: "AWS access key ID"
|
|
1413
|
+
}),
|
|
1414
|
+
secretAccessKey: loadSetting({
|
|
1415
|
+
settingValue: options.secretAccessKey,
|
|
1416
|
+
settingName: "secretAccessKey",
|
|
1417
|
+
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
1418
|
+
description: "AWS secret access key"
|
|
1419
|
+
}),
|
|
1420
|
+
sessionToken: loadOptionalSetting({
|
|
1421
|
+
settingValue: options.sessionToken,
|
|
1422
|
+
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
1423
|
+
})
|
|
1378
1424
|
};
|
|
1425
|
+
} catch (error) {
|
|
1426
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1427
|
+
if (errorMessage.includes("AWS_ACCESS_KEY_ID") || errorMessage.includes("accessKeyId")) {
|
|
1428
|
+
throw new Error(
|
|
1429
|
+
`AWS SigV4 authentication requires AWS credentials. Please provide either:
|
|
1430
|
+
1. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables
|
|
1431
|
+
2. Provide accessKeyId and secretAccessKey in options
|
|
1432
|
+
3. Use a credentialProvider function
|
|
1433
|
+
4. Use API key authentication with AWS_BEARER_TOKEN_BEDROCK or apiKey option
|
|
1434
|
+
Original error: ${errorMessage}`
|
|
1435
|
+
);
|
|
1436
|
+
}
|
|
1437
|
+
if (errorMessage.includes("AWS_SECRET_ACCESS_KEY") || errorMessage.includes("secretAccessKey")) {
|
|
1438
|
+
throw new Error(
|
|
1439
|
+
`AWS SigV4 authentication requires both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Please ensure both credentials are provided.
|
|
1440
|
+
Original error: ${errorMessage}`
|
|
1441
|
+
);
|
|
1442
|
+
}
|
|
1443
|
+
throw error;
|
|
1379
1444
|
}
|
|
1380
|
-
return {
|
|
1381
|
-
region,
|
|
1382
|
-
accessKeyId: loadSetting({
|
|
1383
|
-
settingValue: options.accessKeyId,
|
|
1384
|
-
settingName: "accessKeyId",
|
|
1385
|
-
environmentVariableName: "AWS_ACCESS_KEY_ID",
|
|
1386
|
-
description: "AWS access key ID"
|
|
1387
|
-
}),
|
|
1388
|
-
secretAccessKey: loadSetting({
|
|
1389
|
-
settingValue: options.secretAccessKey,
|
|
1390
|
-
settingName: "secretAccessKey",
|
|
1391
|
-
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
|
|
1392
|
-
description: "AWS secret access key"
|
|
1393
|
-
}),
|
|
1394
|
-
sessionToken: loadOptionalSetting({
|
|
1395
|
-
settingValue: options.sessionToken,
|
|
1396
|
-
environmentVariableName: "AWS_SESSION_TOKEN"
|
|
1397
|
-
})
|
|
1398
|
-
};
|
|
1399
1445
|
}, options.fetch);
|
|
1400
1446
|
const getBaseUrl = () => {
|
|
1401
1447
|
var _a, _b;
|
|
@@ -1413,7 +1459,7 @@ function createAmazonBedrock(options = {}) {
|
|
|
1413
1459
|
return new BedrockChatLanguageModel(modelId, {
|
|
1414
1460
|
baseUrl: getBaseUrl,
|
|
1415
1461
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1416
|
-
fetch:
|
|
1462
|
+
fetch: fetchFunction,
|
|
1417
1463
|
generateId
|
|
1418
1464
|
});
|
|
1419
1465
|
};
|
|
@@ -1430,7 +1476,7 @@ function createAmazonBedrock(options = {}) {
|
|
|
1430
1476
|
return new BedrockEmbeddingModel(modelId, {
|
|
1431
1477
|
baseUrl: getBaseUrl,
|
|
1432
1478
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1433
|
-
fetch:
|
|
1479
|
+
fetch: fetchFunction
|
|
1434
1480
|
});
|
|
1435
1481
|
};
|
|
1436
1482
|
const createImageModel = (modelId) => {
|
|
@@ -1438,7 +1484,7 @@ function createAmazonBedrock(options = {}) {
|
|
|
1438
1484
|
return new BedrockImageModel(modelId, {
|
|
1439
1485
|
baseUrl: getBaseUrl,
|
|
1440
1486
|
headers: (_a = options.headers) != null ? _a : {},
|
|
1441
|
-
fetch:
|
|
1487
|
+
fetch: fetchFunction
|
|
1442
1488
|
});
|
|
1443
1489
|
};
|
|
1444
1490
|
provider.languageModel = createChatModel;
|