@ai-sdk/amazon-bedrock
Version:
The **[Amazon Bedrock provider](https://ai-sdk.dev/providers/ai-sdk-providers/amazon-bedrock)** for the [AI SDK](https://ai-sdk.dev/docs) contains language model support for the Amazon Bedrock [converse API](https://docs.aws.amazon.com/bedrock/latest/APIR
253 lines (247 loc) • 9.88 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/mantle/index.ts
var index_exports = {};
__export(index_exports, {
bedrockMantle: () => bedrockMantle,
createBedrockMantle: () => createBedrockMantle
});
module.exports = __toCommonJS(index_exports);
// src/mantle/bedrock-mantle-provider.ts
var import_internal = require("@ai-sdk/openai/internal");
var import_provider = require("@ai-sdk/provider");
var import_provider_utils2 = require("@ai-sdk/provider-utils");
// src/amazon-bedrock-sigv4-fetch.ts
var import_provider_utils = require("@ai-sdk/provider-utils");
var import_aws4fetch = require("aws4fetch");
// src/version.ts
var VERSION = true ? "5.0.10" : "0.0.0-test";
// src/amazon-bedrock-sigv4-fetch.ts
function createSigV4FetchFunction(getCredentials, fetch, service = "bedrock") {
return async (input, init) => {
var _a, _b;
const effectiveFetch = fetch != null ? fetch : globalThis.fetch;
const request = input instanceof Request ? input : void 0;
const originalHeaders = (0, import_provider_utils.combineHeaders)(
(0, import_provider_utils.normalizeHeaders)(request == null ? void 0 : request.headers),
(0, import_provider_utils.normalizeHeaders)(init == null ? void 0 : init.headers)
);
const headersWithUserAgent = (0, import_provider_utils.withUserAgentSuffix)(
originalHeaders,
`ai-sdk/amazon-bedrock/${VERSION}`,
(0, import_provider_utils.getRuntimeEnvironmentUserAgent)()
);
let effectiveBody = (_a = init == null ? void 0 : init.body) != null ? _a : void 0;
if (effectiveBody === void 0 && request && request.body !== null) {
try {
effectiveBody = await request.clone().text();
} catch (e) {
}
}
const effectiveMethod = (_b = init == null ? void 0 : init.method) != null ? _b : request == null ? void 0 : request.method;
if ((effectiveMethod == null ? void 0 : effectiveMethod.toUpperCase()) !== "POST" || !effectiveBody) {
return effectiveFetch(input, {
...init,
headers: headersWithUserAgent
});
}
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
const body = prepareBodyString(effectiveBody);
const credentials = await getCredentials();
const signer = new import_aws4fetch.AwsV4Signer({
url,
method: "POST",
headers: Object.entries(headersWithUserAgent),
body,
region: credentials.region,
accessKeyId: credentials.accessKeyId,
secretAccessKey: credentials.secretAccessKey,
sessionToken: credentials.sessionToken,
service
});
const signingResult = await signer.sign();
const signedHeaders = (0, import_provider_utils.normalizeHeaders)(signingResult.headers);
const combinedHeaders = (0, import_provider_utils.combineHeaders)(headersWithUserAgent, signedHeaders);
return effectiveFetch(input, {
...init,
body,
headers: combinedHeaders
});
};
}
function prepareBodyString(body) {
if (typeof body === "string") {
return body;
} else if (body instanceof Uint8Array) {
return new TextDecoder().decode(body);
} else if (body instanceof ArrayBuffer) {
return new TextDecoder().decode(new Uint8Array(body));
} else {
return JSON.stringify(body);
}
}
function createApiKeyFetchFunction(apiKey, fetch) {
return async (input, init) => {
const effectiveFetch = fetch != null ? fetch : globalThis.fetch;
const originalHeaders = (0, import_provider_utils.normalizeHeaders)(init == null ? void 0 : init.headers);
const headersWithUserAgent = (0, import_provider_utils.withUserAgentSuffix)(
originalHeaders,
`ai-sdk/amazon-bedrock/${VERSION}`,
(0, import_provider_utils.getRuntimeEnvironmentUserAgent)()
);
const finalHeaders = (0, import_provider_utils.combineHeaders)(headersWithUserAgent, {
Authorization: `Bearer ${apiKey}`
});
return effectiveFetch(input, {
...init,
headers: finalHeaders
});
};
}
// src/mantle/bedrock-mantle-provider.ts
function createBedrockMantle(options = {}) {
const rawApiKey = (0, import_provider_utils2.loadOptionalSetting)({
settingValue: options.apiKey,
environmentVariableName: "AWS_BEARER_TOKEN_BEDROCK"
});
const apiKey = rawApiKey && rawApiKey.trim().length > 0 ? rawApiKey.trim() : void 0;
const fetchFunction = apiKey ? createApiKeyFetchFunction(apiKey, options.fetch) : createSigV4FetchFunction(
async () => {
const region = (0, import_provider_utils2.loadSetting)({
settingValue: options.region,
settingName: "region",
environmentVariableName: "AWS_REGION",
description: "AWS region"
});
if (options.credentialProvider) {
try {
return {
...await options.credentialProvider(),
region
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(
`AWS credential provider failed: ${errorMessage}. Please ensure your credential provider returns valid AWS credentials with accessKeyId and secretAccessKey properties.`
);
}
}
try {
return {
region,
accessKeyId: (0, import_provider_utils2.loadSetting)({
settingValue: options.accessKeyId,
settingName: "accessKeyId",
environmentVariableName: "AWS_ACCESS_KEY_ID",
description: "AWS access key ID"
}),
secretAccessKey: (0, import_provider_utils2.loadSetting)({
settingValue: options.secretAccessKey,
settingName: "secretAccessKey",
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
description: "AWS secret access key"
}),
sessionToken: (0, import_provider_utils2.loadOptionalSetting)({
settingValue: options.sessionToken,
environmentVariableName: "AWS_SESSION_TOKEN"
})
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
if (errorMessage.includes("AWS_ACCESS_KEY_ID") || errorMessage.includes("accessKeyId")) {
throw new Error(
`AWS SigV4 authentication requires AWS credentials. Please provide either:
1. Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables
2. Provide accessKeyId and secretAccessKey in options
3. Use a credentialProvider function
4. Use API key authentication with AWS_BEARER_TOKEN_BEDROCK or apiKey option
Original error: ${errorMessage}`
);
}
if (errorMessage.includes("AWS_SECRET_ACCESS_KEY") || errorMessage.includes("secretAccessKey")) {
throw new Error(
`AWS SigV4 authentication requires both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Please ensure both credentials are provided.
Original error: ${errorMessage}`
);
}
throw error;
}
},
options.fetch,
"bedrock-mantle"
);
const getBaseURL = () => {
var _a, _b;
return (_b = (0, import_provider_utils2.withoutTrailingSlash)(
(_a = options.baseURL) != null ? _a : `https://bedrock-mantle.${(0, import_provider_utils2.loadSetting)({
settingValue: options.region,
settingName: "region",
environmentVariableName: "AWS_REGION",
description: "AWS region"
})}.api.aws/v1`
)) != null ? _b : "https://bedrock-mantle.us-east-1.api.aws/v1";
};
const getHeaders = () => {
var _a;
return (0, import_provider_utils2.withUserAgentSuffix)(
(_a = options.headers) != null ? _a : {},
`ai-sdk/amazon-bedrock/${VERSION}`
);
};
const url = ({ path }) => `${getBaseURL()}${path}`;
const createChatModel = (modelId) => new import_internal.OpenAIChatLanguageModel(modelId, {
provider: "bedrock-mantle.chat",
url,
headers: getHeaders,
fetch: fetchFunction
});
const createResponsesModel = (modelId) => new import_internal.OpenAIResponsesLanguageModel(modelId, {
provider: "bedrock-mantle.responses",
url,
headers: getHeaders,
fetch: fetchFunction
});
const provider = function(modelId) {
if (new.target) {
throw new Error(
"The Bedrock Mantle model function cannot be called with the new keyword."
);
}
return createChatModel(modelId);
};
provider.specificationVersion = "v4";
provider.languageModel = createChatModel;
provider.chat = createChatModel;
provider.responses = createResponsesModel;
provider.embeddingModel = (modelId) => {
throw new import_provider.NoSuchModelError({ modelId, modelType: "embeddingModel" });
};
provider.textEmbeddingModel = provider.embeddingModel;
provider.imageModel = (modelId) => {
throw new import_provider.NoSuchModelError({ modelId, modelType: "imageModel" });
};
return provider;
}
var bedrockMantle = createBedrockMantle();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
bedrockMantle,
createBedrockMantle
});
//# sourceMappingURL=index.cjs.map