@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
240 lines (236 loc) • 8.39 kB
JavaScript
// src/mantle/bedrock-mantle-provider.ts
import {
OpenAIChatLanguageModel,
OpenAIResponsesLanguageModel
} from "@ai-sdk/openai/internal";
import {
NoSuchModelError
} from "@ai-sdk/provider";
import {
loadOptionalSetting,
loadSetting,
withoutTrailingSlash,
withUserAgentSuffix as withUserAgentSuffix2
} from "@ai-sdk/provider-utils";
// src/amazon-bedrock-sigv4-fetch.ts
import {
combineHeaders,
normalizeHeaders,
withUserAgentSuffix,
getRuntimeEnvironmentUserAgent
} from "@ai-sdk/provider-utils";
import { AwsV4Signer } from "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 = combineHeaders(
normalizeHeaders(request == null ? void 0 : request.headers),
normalizeHeaders(init == null ? void 0 : init.headers)
);
const headersWithUserAgent = withUserAgentSuffix(
originalHeaders,
`ai-sdk/amazon-bedrock/${VERSION}`,
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 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 = normalizeHeaders(signingResult.headers);
const combinedHeaders = 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 = normalizeHeaders(init == null ? void 0 : init.headers);
const headersWithUserAgent = withUserAgentSuffix(
originalHeaders,
`ai-sdk/amazon-bedrock/${VERSION}`,
getRuntimeEnvironmentUserAgent()
);
const finalHeaders = combineHeaders(headersWithUserAgent, {
Authorization: `Bearer ${apiKey}`
});
return effectiveFetch(input, {
...init,
headers: finalHeaders
});
};
}
// src/mantle/bedrock-mantle-provider.ts
function createBedrockMantle(options = {}) {
const rawApiKey = 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 = 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: loadSetting({
settingValue: options.accessKeyId,
settingName: "accessKeyId",
environmentVariableName: "AWS_ACCESS_KEY_ID",
description: "AWS access key ID"
}),
secretAccessKey: loadSetting({
settingValue: options.secretAccessKey,
settingName: "secretAccessKey",
environmentVariableName: "AWS_SECRET_ACCESS_KEY",
description: "AWS secret access key"
}),
sessionToken: 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 = withoutTrailingSlash(
(_a = options.baseURL) != null ? _a : `https://bedrock-mantle.${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 withUserAgentSuffix2(
(_a = options.headers) != null ? _a : {},
`ai-sdk/amazon-bedrock/${VERSION}`
);
};
const url = ({ path }) => `${getBaseURL()}${path}`;
const createChatModel = (modelId) => new OpenAIChatLanguageModel(modelId, {
provider: "bedrock-mantle.chat",
url,
headers: getHeaders,
fetch: fetchFunction
});
const createResponsesModel = (modelId) => new 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 NoSuchModelError({ modelId, modelType: "embeddingModel" });
};
provider.textEmbeddingModel = provider.embeddingModel;
provider.imageModel = (modelId) => {
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
};
return provider;
}
var bedrockMantle = createBedrockMantle();
export {
bedrockMantle,
createBedrockMantle
};
//# sourceMappingURL=index.js.map