@@ -8,35 +8,50 @@ import { fromIni } from "@aws-sdk/credential-providers"
88// https://docs.anthropic.com/en/api/claude-on-amazon-bedrock
99export class AwsBedrockHandler implements ApiHandler {
1010 private options : ApiHandlerOptions
11- private client : AnthropicBedrock
11+ private client : AnthropicBedrock | any
12+ private initializationPromise : Promise < void >
1213
1314 constructor ( options : ApiHandlerOptions ) {
1415 this . options = options
16+ this . initializationPromise = this . initializeClient ( )
17+ }
1518
16- const clientConfig : any = {
19+ private async initializeClient ( ) {
20+ let clientConfig : any = {
1721 awsRegion : this . options . awsRegion || "us-east-1" ,
1822 }
19-
20- if ( this . options . awsUseProfile ) {
21- // Use profile-based credentials if enabled
22- if ( this . options . awsProfile ) {
23- clientConfig . credentials = fromIni ( {
24- profile : this . options . awsProfile ,
25- } )
26- } else {
27- // Use default profile if no specific profile is set
28- clientConfig . credentials = fromIni ( )
29- }
30- } else if ( this . options . awsAccessKey && this . options . awsSecretKey ) {
31- // Use direct credentials if provided
32- clientConfig . awsAccessKey = this . options . awsAccessKey
33- clientConfig . awsSecretKey = this . options . awsSecretKey
34- if ( this . options . awsSessionToken ) {
35- clientConfig . awsSessionToken = this . options . awsSessionToken
23+ try {
24+ if ( this . options . awsUseProfile ) {
25+ // Use profile-based credentials if enabled
26+ // Use named profile, defaulting to 'default' if not specified
27+ var credentials : any
28+ if ( this . options . awsProfile ) {
29+ credentials = await fromIni ( {
30+ profile : this . options . awsProfile ,
31+ ignoreCache : true ,
32+ } ) ( )
33+ } else {
34+ credentials = await fromIni ( {
35+ ignoreCache : true ,
36+ } ) ( )
37+ }
38+ clientConfig . awsAccessKey = credentials . accessKeyId
39+ clientConfig . awsSecretKey = credentials . secretAccessKey
40+ clientConfig . awsSessionToken = credentials . sessionToken
41+ } else if ( this . options . awsAccessKey && this . options . awsSecretKey ) {
42+ // Use direct credentials if provided
43+ clientConfig . awsAccessKey = this . options . awsAccessKey
44+ clientConfig . awsSecretKey = this . options . awsSecretKey
45+ if ( this . options . awsSessionToken ) {
46+ clientConfig . awsSessionToken = this . options . awsSessionToken
47+ }
3648 }
49+ } catch ( error ) {
50+ console . error ( "Failed to initialize Bedrock client:" , error )
51+ throw error
52+ } finally {
53+ this . client = new AnthropicBedrock ( clientConfig )
3754 }
38-
39- this . client = new AnthropicBedrock ( clientConfig )
4055 }
4156
4257 async * createMessage ( systemPrompt : string , messages : Anthropic . Messages . MessageParam [ ] ) : ApiStream {
0 commit comments