github.com/anthropics/anthropic-sdk-go@v1.70.1/bedrock/bedrockmantle.go (about) 1 package bedrock 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/anthropics/anthropic-sdk-go" 8 "github.com/anthropics/anthropic-sdk-go/internal/awsauth" 9 "github.com/anthropics/anthropic-sdk-go/option" 10 ) 11 12 const mantleServiceName = "bedrock-mantle" 13 14 // MantleClientConfig holds the configuration for creating an Anthropic Bedrock Mantle client. 15 type MantleClientConfig struct { 16 // APIKey is the Anthropic API key for x-api-key authentication. 17 // Takes precedence over AWS credentials. When no AWS auth args are set, falls back 18 // to the AWS_BEARER_TOKEN_BEDROCK environment variable (then ANTHROPIC_AWS_API_KEY) 19 // before trying SigV4. 20 APIKey string 21 22 // AWSAccessKey is the AWS access key ID for SigV4 authentication. 23 // Must be paired with AWSSecretAccessKey. When unset, credentials are resolved 24 // via the default AWS credential chain (env vars, shared credentials file, IAM roles, etc.). 25 AWSAccessKey string 26 27 // AWSSecretAccessKey is the AWS secret access key for SigV4 authentication. 28 // When unset, credentials are resolved via the default AWS credential chain 29 // (env vars, shared credentials file, IAM roles, etc.). 30 AWSSecretAccessKey string 31 32 // AWSSessionToken is the optional AWS session token for temporary credentials. 33 // When unset, resolved via the default AWS credential chain if applicable. 34 AWSSessionToken string 35 36 // AWSProfile is the AWS named profile for credential resolution via the provider chain. 37 AWSProfile string 38 39 // AWSRegion is the AWS region for the base URL and SigV4 signing. 40 // Resolved by precedence: MantleClientConfig.AWSRegion > AWS_REGION env var. 41 AWSRegion string 42 43 // BaseURL overrides the default base URL. 44 // Resolved by precedence: MantleClientConfig.BaseURL > ANTHROPIC_BEDROCK_MANTLE_BASE_URL env > 45 // https://bedrock-mantle.{region}.api.aws/anthropic 46 BaseURL string 47 48 // SkipAuth skips Mantle-specific authentication (API key and SigV4). 49 // This is useful when a gateway or proxy handles authentication on your behalf. 50 // Note: when using [NewMantleClient], the base SDK may still send an X-Api-Key header 51 // if the ANTHROPIC_API_KEY environment variable is set. 52 SkipAuth bool 53 } 54 55 // MantleClient provides access to the Anthropic Bedrock Mantle API. 56 // Only the Messages API (/v1/messages) and its subpaths are supported. 57 type MantleClient struct { 58 Options []option.RequestOption 59 Messages anthropic.MessageService 60 Beta MantleBetaService 61 } 62 63 // MantleBetaService exposes only the beta resources supported by Bedrock Mantle. 64 type MantleBetaService struct { 65 Options []option.RequestOption 66 Messages anthropic.BetaMessageService 67 } 68 69 // NewMantleClient creates a new Bedrock Mantle client with the given configuration. 70 // Only the Messages API (/v1/messages) and its subpaths are supported on Bedrock Mantle. 71 // 72 // Any additional [option.RequestOption] values are applied after the client's 73 // internal options (base URL, auth, etc.), so they can be used to set custom 74 // headers, timeouts, middleware, and other request-level settings. When SigV4 75 // authentication is in use, the signing middleware runs after any middleware 76 // registered through these options, so the signature covers their request 77 // mutations. Note that per-request middleware passed at a method call site 78 // still runs after signing and must not mutate the request. 79 // 80 // Auth is resolved by precedence: 81 // 1. APIKey arg (x-api-key header) 82 // 2. AWSAccessKey + AWSSecretAccessKey args (SigV4) 83 // 3. AWSProfile arg (SigV4 via provider chain) 84 // 4. AWS_BEARER_TOKEN_BEDROCK env var, then ANTHROPIC_AWS_API_KEY (x-api-key header) 85 // 5. Default AWS credential chain (SigV4) 86 func NewMantleClient(ctx context.Context, cfg MantleClientConfig, opts ...option.RequestOption) (*MantleClient, error) { 87 // We intentionally do not call anthropic.DefaultClientOptions() here. 88 // The Mantle client resolves its own base URL, auth, and workspace ID — the 89 // base SDK defaults (ANTHROPIC_API_KEY, ANTHROPIC_BASE_URL) do not apply. 90 opts, err := awsauth.CreateClientOptions(ctx, mantleToInternalConfig(cfg), mantleResolveParams(), opts...) 91 if err != nil { 92 return nil, err 93 } 94 95 return &MantleClient{ 96 Options: opts, 97 Messages: anthropic.NewMessageService(opts...), 98 Beta: MantleBetaService{ 99 Options: opts, 100 Messages: anthropic.NewBetaMessageService(opts...), 101 }, 102 }, nil 103 } 104 105 func mantleResolveParams() awsauth.ResolveParams { 106 return awsauth.ResolveParams{ 107 EnvAPIKey: "AWS_BEARER_TOKEN_BEDROCK", 108 EnvAPIKeyFallback: "ANTHROPIC_AWS_API_KEY", 109 EnvBaseURL: "ANTHROPIC_BEDROCK_MANTLE_BASE_URL", 110 DeriveBaseURL: func(region string) string { return fmt.Sprintf("https://bedrock-mantle.%s.api.aws/anthropic", region) }, 111 ServiceName: mantleServiceName, 112 UseBearerAuth: true, 113 } 114 } 115 116 func mantleToInternalConfig(cfg MantleClientConfig) awsauth.ClientConfig { 117 return awsauth.ClientConfig{ 118 APIKey: cfg.APIKey, 119 AWSAccessKey: cfg.AWSAccessKey, 120 AWSSecretAccessKey: cfg.AWSSecretAccessKey, 121 AWSSessionToken: cfg.AWSSessionToken, 122 AWSProfile: cfg.AWSProfile, 123 AWSRegion: cfg.AWSRegion, 124 BaseURL: cfg.BaseURL, 125 SkipAuth: cfg.SkipAuth, 126 } 127 }