| 61 | } |
| 62 | |
| 63 | export class BedrockApi implements BaseLlmApi { |
| 64 | constructor(protected config: BedrockConfig) { |
| 65 | if (config.env?.accessKeyId || config?.env?.secretAccessKey) { |
| 66 | if (!config.env?.accessKeyId) { |
| 67 | throw new Error( |
| 68 | "accessKeyId is required for Bedrock API. Only found secretAccessKey", |
| 69 | ); |
| 70 | } |
| 71 | if (!config.env?.secretAccessKey) { |
| 72 | throw new Error( |
| 73 | "secretAccessKey is required for Bedrock API. Only found accessKeyId", |
| 74 | ); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | async getCreds() { |
| 80 | if (this.config?.env?.accessKeyId && this.config?.env?.secretAccessKey) { |
| 81 | return { |
| 82 | accessKeyId: this.config.env.accessKeyId, |
| 83 | secretAccessKey: this.config.env.secretAccessKey, |
| 84 | }; |
| 85 | } |
| 86 | const profile = this.config.env?.profile ?? "bedrock"; |
| 87 | try { |
| 88 | return await fromNodeProviderChain({ |
| 89 | profile: profile, |
| 90 | ignoreCache: true, |
| 91 | })(); |
| 92 | } catch (e) { |
| 93 | console.warn( |
| 94 | `AWS profile with name ${profile} not found in ~/.aws/credentials, using default profile`, |
| 95 | ); |
| 96 | } |
| 97 | return await fromNodeProviderChain()(); |
| 98 | } |
| 99 | async getClient(): Promise<BedrockRuntimeClient> { |
| 100 | const region = this.config.env?.region; |
| 101 | |
| 102 | // If apiKey is provided, use bearer token authentication |
| 103 | if (this.config.apiKey) { |
| 104 | return new BedrockRuntimeClient({ |
| 105 | region, |
| 106 | token: fromStatic({ |
| 107 | token: { token: this.config.apiKey }, |
| 108 | }), |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | // Otherwise use IAM credentials (existing behavior) |
| 113 | const creds = await this.getCreds(); |
| 114 | return new BedrockRuntimeClient({ |
| 115 | region, |
| 116 | credentials: creds, |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | private _oaiPartToBedrockPart( |
nothing calls this directly
no outgoing calls
no test coverage detected