|
| 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. |
| 75 | +// |
| 76 | +// Auth is resolved by precedence: |
| 77 | +// 1. APIKey arg (x-api-key header) |
| 78 | +// 2. AWSAccessKey + AWSSecretAccessKey args (SigV4) |
| 79 | +// 3. AWSProfile arg (SigV4 via provider chain) |
| 80 | +// 4. AWS_BEARER_TOKEN_BEDROCK env var, then ANTHROPIC_AWS_API_KEY (x-api-key header) |
| 81 | +// 5. Default AWS credential chain (SigV4) |
| 82 | +func NewMantleClient(ctx context.Context, cfg MantleClientConfig, opts ...option.RequestOption) (*MantleClient, error) { |
| 83 | + baseOpts, err := awsauth.CreateClientOptions(ctx, mantleToInternalConfig(cfg), mantleResolveParams()) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + // We intentionally do not call anthropic.DefaultClientOptions() here. |
| 89 | + // The Mantle client resolves its own base URL, auth, and workspace ID — the |
| 90 | + // base SDK defaults (ANTHROPIC_API_KEY, ANTHROPIC_BASE_URL) do not apply. |
| 91 | + // |
| 92 | + // User-provided opts are appended last so they take highest precedence. |
| 93 | + opts = append(baseOpts, opts...) |
| 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 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func mantleToInternalConfig(cfg MantleClientConfig) awsauth.ClientConfig { |
| 116 | + return awsauth.ClientConfig{ |
| 117 | + APIKey: cfg.APIKey, |
| 118 | + AWSAccessKey: cfg.AWSAccessKey, |
| 119 | + AWSSecretAccessKey: cfg.AWSSecretAccessKey, |
| 120 | + AWSSessionToken: cfg.AWSSessionToken, |
| 121 | + AWSProfile: cfg.AWSProfile, |
| 122 | + AWSRegion: cfg.AWSRegion, |
| 123 | + BaseURL: cfg.BaseURL, |
| 124 | + SkipAuth: cfg.SkipAuth, |
| 125 | + } |
| 126 | +} |
0 commit comments