support secretAccessKey and accessKeyId for bedrock

This commit is contained in:
Dallin Romney 2025-07-07 18:47:37 -07:00
parent 81860c6028
commit 2b88e54e37
9 changed files with 190 additions and 43 deletions

View file

@ -126,6 +126,12 @@ async function modelConfigToBaseLLM({
if ("profile" in env && typeof env.profile === "string") {
options.profile = env.profile;
}
if ("accessKeyId" in env && typeof env.accessKeyId === "string") {
options.accessKeyId = env.accessKeyId;
}
if ("secretAccessKey" in env && typeof env.secretAccessKey === "string") {
options.secretAccessKey = env.secretAccessKey;
}
if ("modelArn" in env && typeof env.modelArn === "string") {
options.modelArn = env.modelArn;
}

2
core/index.d.ts vendored
View file

@ -611,6 +611,8 @@ export interface LLMOptions {
// AWS options
profile?: string;
modelArn?: string;
accessKeyId?: string;
secretAccessKey?: string;
// AWS and VertexAI Options
region?: string;

View file

@ -172,6 +172,8 @@ export abstract class BaseLLM implements ILLM {
accountId?: string;
aiGatewaySlug?: string;
profile?: string | undefined;
accessKeyId?: string;
secretAccessKey?: string;
// For IBM watsonx
deploymentId?: string;
@ -270,6 +272,8 @@ export abstract class BaseLLM implements ILLM {
this.region = options.region;
this.projectId = options.projectId;
this.profile = options.profile;
this.accessKeyId = options.accessKeyId;
this.secretAccessKey = options.secretAccessKey;
this.openaiAdapter = this.createOpenAiAdapter();

View file

@ -73,11 +73,6 @@ class Bedrock extends BaseLLM {
if (!options.apiBase) {
this.apiBase = `https://bedrock-runtime.${options.region}.amazonaws.com`;
}
if (options.profile) {
this.profile = options.profile;
} else {
this.profile = "bedrock";
}
this.requestOptions = {
region: options.region,
@ -612,30 +607,22 @@ class Bedrock extends BaseLLM {
}
private async _getCredentials() {
if (this.apiKey) {
const [accessKeyId, secretAccessKey] = this.apiKey.split(":");
if (!accessKeyId || !secretAccessKey) {
throw new Error(
"Invalid Bedrock API key format. Expected format: accessKeyId:secretAccessKey",
);
}
if (this.accessKeyId && this.secretAccessKey) {
return {
accessKeyId,
secretAccessKey,
accessKeyId: this.accessKeyId,
secretAccessKey: this.secretAccessKey,
};
}
if (this.profile) {
try {
return await fromNodeProviderChain({
profile: this.profile,
ignoreCache: true,
})();
} catch (e) {
console.warn(
`AWS profile with name ${this.profile} not found in ~/.aws/credentials, using default profile`,
);
}
const profile = this.profile ?? "bedrock";
try {
return await fromNodeProviderChain({
profile: profile,
ignoreCache: true,
})();
} catch (e) {
console.warn(
`AWS profile with name ${profile} not found in ~/.aws/credentials, using default profile`,
);
}
return await fromNodeProviderChain()();
}

View file

@ -359,8 +359,18 @@
"description": "The AWS security profile to use",
"type": "string"
},
"accessKeyId": {
"title": "Access Key ID",
"description": "AWS access key id (can be used together with secretAccessKey in place of profile)",
"type": "string"
},
"secretAccessKey": {
"title": "Secret Access Key",
"description": "AWS secret access key (can be used together with secretAccessKey in place of profile)",
"type": "string"
},
"modelArn": {
"title": "Profile",
"title": "Model ARN",
"description": "The AWS arn for the imported model",
"type": "string"
},
@ -2903,6 +2913,16 @@
"title": "Profile",
"description": "The AWS security profile to use",
"type": "string"
},
"accessKeyId": {
"title": "Access Key ID",
"description": "AWS access key id (can be used together with secretAccessKey in place of profile)",
"type": "string"
},
"secretAccessKey": {
"title": "Secret Access Key",
"description": "AWS secret access key (can be used together with secretAccessKey in place of profile)",
"type": "string"
}
},
"required": ["provider"],

View file

@ -10,6 +10,7 @@
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/client-bedrock-runtime": "^3.842.0",
"@aws-sdk/credential-providers": "^3.840.0",
"@continuedev/config-types": "^1.0.5",
"@continuedev/config-yaml": "^1.0.51",
"@continuedev/fetch": "^1.0.15",
@ -255,6 +256,56 @@
"uuid": "dist/bin/uuid"
}
},
"node_modules/@aws-sdk/client-cognito-identity": {
"version": "3.840.0",
"resolved": "https://registry.npmjs.org/@aws-sdk/client-cognito-identity/-/client-cognito-identity-3.840.0.tgz",
"integrity": "sha512-0sn/X63Xqqh5D1FYmdSHiS9SkDzTitoGO++/8IFik4xf/jpn4ZQkIoDPvpxFZcLvebMuUa6jAQs4ap4RusKGkg==",
"license": "Apache-2.0",
"dependencies": {
"@aws-crypto/sha256-browser": "5.2.0",
"@aws-crypto/sha256-js": "5.2.0",
"@aws-sdk/core": "3.840.0",
"@aws-sdk/credential-provider-node": "3.840.0",
"@aws-sdk/middleware-host-header": "3.840.0",
"@aws-sdk/middleware-logger": "3.840.0",
"@aws-sdk/middleware-recursion-detection": "3.840.0",
"@aws-sdk/middleware-user-agent": "3.840.0",
"@aws-sdk/region-config-resolver": "3.840.0",
"@aws-sdk/types": "3.840.0",
"@aws-sdk/util-endpoints": "3.840.0",
"@aws-sdk/util-user-agent-browser": "3.840.0",
"@aws-sdk/util-user-agent-node": "3.840.0",
"@smithy/config-resolver": "^4.1.4",
"@smithy/core": "^3.6.0",
"@smithy/fetch-http-handler": "^5.0.4",
"@smithy/hash-node": "^4.0.4",
"@smithy/invalid-dependency": "^4.0.4",
"@smithy/middleware-content-length": "^4.0.4",
"@smithy/middleware-endpoint": "^4.1.13",
"@smithy/middleware-retry": "^4.1.14",
"@smithy/middleware-serde": "^4.0.8",
"@smithy/middleware-stack": "^4.0.4",
"@smithy/node-config-provider": "^4.1.3",
"@smithy/node-http-handler": "^4.0.6",
"@smithy/protocol-http": "^5.1.2",
"@smithy/smithy-client": "^4.4.5",
"@smithy/types": "^4.3.1",
"@smithy/url-parser": "^4.0.4",
"@smithy/util-base64": "^4.0.0",
"@smithy/util-body-length-browser": "^4.0.0",
"@smithy/util-body-length-node": "^4.0.0",
"@smithy/util-defaults-mode-browser": "^4.0.21",
"@smithy/util-defaults-mode-node": "^4.0.21",
"@smithy/util-endpoints": "^3.0.6",
"@smithy/util-middleware": "^4.0.4",
"@smithy/util-retry": "^4.0.6",
"@smithy/util-utf8": "^4.0.0",
"tslib": "^2.6.2"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@aws-sdk/client-sso": {
"version": "3.840.0",
"resolved": "https://registry.npmjs.org/@aws-sdk/client-sso/-/client-sso-3.840.0.tgz",
@ -330,6 +381,22 @@
"node": ">=18.0.0"
}
},
"node_modules/@aws-sdk/credential-provider-cognito-identity": {
"version": "3.840.0",
"resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-cognito-identity/-/credential-provider-cognito-identity-3.840.0.tgz",
"integrity": "sha512-p1RaMVd6+6ruYjKsWRCZT/jWhrYfDKbXY+/ScIYTvcaOOf9ArMtVnhFk3egewrC7kPXFGRYhg2GPmxRotNYMng==",
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/client-cognito-identity": "3.840.0",
"@aws-sdk/types": "3.840.0",
"@smithy/property-provider": "^4.0.4",
"@smithy/types": "^4.3.1",
"tslib": "^2.6.2"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@aws-sdk/credential-provider-env": {
"version": "3.840.0",
"resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-env/-/credential-provider-env-3.840.0.tgz",
@ -467,6 +534,36 @@
"node": ">=18.0.0"
}
},
"node_modules/@aws-sdk/credential-providers": {
"version": "3.840.0",
"resolved": "https://registry.npmjs.org/@aws-sdk/credential-providers/-/credential-providers-3.840.0.tgz",
"integrity": "sha512-+CxYdGd+uM4NZ9VUvFTU1c/H61qhDB4q362k8xKU+bz24g//LDQ5Mpwksv8OUD1en44v4fUwgZ4SthPZMs+eFQ==",
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/client-cognito-identity": "3.840.0",
"@aws-sdk/core": "3.840.0",
"@aws-sdk/credential-provider-cognito-identity": "3.840.0",
"@aws-sdk/credential-provider-env": "3.840.0",
"@aws-sdk/credential-provider-http": "3.840.0",
"@aws-sdk/credential-provider-ini": "3.840.0",
"@aws-sdk/credential-provider-node": "3.840.0",
"@aws-sdk/credential-provider-process": "3.840.0",
"@aws-sdk/credential-provider-sso": "3.840.0",
"@aws-sdk/credential-provider-web-identity": "3.840.0",
"@aws-sdk/nested-clients": "3.840.0",
"@aws-sdk/types": "3.840.0",
"@smithy/config-resolver": "^4.1.4",
"@smithy/core": "^3.6.0",
"@smithy/credential-provider-imds": "^4.0.6",
"@smithy/node-config-provider": "^4.1.3",
"@smithy/property-provider": "^4.0.4",
"@smithy/types": "^4.3.1",
"tslib": "^2.6.2"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@aws-sdk/eventstream-handler-node": {
"version": "3.840.0",
"resolved": "https://registry.npmjs.org/@aws-sdk/eventstream-handler-node/-/eventstream-handler-node-3.840.0.tgz",

View file

@ -13,6 +13,7 @@
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/client-bedrock-runtime": "^3.842.0",
"@aws-sdk/credential-providers": "^3.840.0",
"@continuedev/config-types": "^1.0.5",
"@continuedev/config-yaml": "^1.0.51",
"@continuedev/fetch": "^1.0.15",

View file

@ -27,6 +27,7 @@ import {
Model,
} from "openai/resources/index";
import { fromNodeProviderChain } from "@aws-sdk/credential-providers";
import { BedrockConfig } from "../types.js";
import { chatChunk, chatChunkFromDelta, embedding, rerank } from "../util.js";
import { safeParseArgs } from "../util/parseArgs.js";
@ -56,32 +57,56 @@ interface ToolUseState {
}
export class BedrockApi implements BaseLlmApi {
private client: BedrockRuntimeClient;
private _currentToolResponse: Partial<ToolUseState> | null = null;
constructor(protected config: BedrockConfig) {
if (!config.env?.region) {
throw new Error("region is required for Bedrock API");
}
if (!config.apiKey) {
if (config.env?.accessKeyId || config?.env?.secretAccessKey) {
if (!config.env?.accessKeyId) {
throw new Error(
"accessKeyId is required for Bedrock API. Only found secretAccessKey",
);
}
if (!config.env?.secretAccessKey) {
throw new Error(
"secretAccessKey is required for Bedrock API. Only found accessKeyId",
);
}
} else if (!config.env?.profile) {
throw new Error(
"apiKey in the format accessKeyId:secretAccessKey is required for Bedrock API",
"AWS credentials profile not found. Please provide accessKeyId and secretAccessKey or profile",
);
}
}
const [accessKeyId, secretAccessKey] = config.apiKey.split(":");
if (!accessKeyId || !secretAccessKey) {
throw new Error(
"Bedrock apiKey must be in the format accessKeyId:secretAccessKey",
async getCreds() {
if (this.config?.env?.accessKeyId && this.config?.env?.secretAccessKey) {
return {
accessKeyId: this.config.env.accessKeyId,
secretAccessKey: this.config.env.secretAccessKey,
};
}
const profile = this.config.env.profile ?? "bedrock";
try {
return await fromNodeProviderChain({
profile: profile,
ignoreCache: true,
})();
} catch (e) {
console.warn(
`AWS profile with name ${profile} not found in ~/.aws/credentials, using default profile`,
);
}
this.client = new BedrockRuntimeClient({
region: config.env.region,
credentials: {
accessKeyId,
secretAccessKey,
},
return await fromNodeProviderChain()();
}
async getClient(): Promise<BedrockRuntimeClient> {
const creds = await this.getCreds();
return new BedrockRuntimeClient({
region: this.config.env.region,
credentials: creds,
});
}
@ -431,7 +456,8 @@ export class BedrockApi implements BaseLlmApi {
...requestBody,
});
const response = await this.client.send(command, { abortSignal: signal });
const client = await this.getClient();
const response = await client.send(command, { abortSignal: signal });
if (!response?.stream) {
throw new Error("No stream received from Bedrock API");
@ -551,7 +577,8 @@ export class BedrockApi implements BaseLlmApi {
contentType: "application/json",
};
const command = new InvokeModelCommand(payload);
const response = await this.client.send(command);
const client = await this.getClient();
const response = await client.send(command);
if (!response.body) {
throw new Error("No response body");
}

View file

@ -79,6 +79,9 @@ export const BedrockConfigSchema = OpenAIConfigSchema.extend({
// }).optional(),
env: z.object({
region: z.string().optional(),
accessKeyId: z.string().optional(),
secretAccessKey: z.string().optional(),
profile: z.string().optional(),
}),
});
export type BedrockConfig = z.infer<typeof BedrockConfigSchema>;