github.com/anthropics/anthropic-sdk-go@v1.70.1/aws/aws.go (about)

     1  // Package aws provides client configuration for the Anthropic AWS gateway.
     2  //
     3  // This package name may shadow the AWS SDK's "aws" package. If you use both in the
     4  // same file, alias one of them:
     5  //
     6  //	import (
     7  //	    anthropicaws "github.com/anthropics/anthropic-sdk-go/aws"
     8  //	    awssdk "github.com/aws/aws-sdk-go-v2/aws"
     9  //	)
    10  package aws
    11  
    12  import (
    13  	"context"
    14  	"fmt"
    15  
    16  	"github.com/anthropics/anthropic-sdk-go"
    17  	"github.com/anthropics/anthropic-sdk-go/internal/awsauth"
    18  	"github.com/anthropics/anthropic-sdk-go/option"
    19  )
    20  
    21  const defaultServiceName = "aws-external-anthropic"
    22  
    23  // ClientConfig holds the configuration for creating an Anthropic AWS gateway client.
    24  type ClientConfig struct {
    25  	// APIKey is the Anthropic API key for x-api-key authentication.
    26  	// Takes precedence over AWS credentials. When no AWS auth args are set, falls back
    27  	// to the ANTHROPIC_AWS_API_KEY environment variable before trying SigV4.
    28  	APIKey string
    29  
    30  	// AWSAccessKey is the AWS access key ID for SigV4 authentication.
    31  	// Must be paired with AWSSecretAccessKey. When unset, credentials are resolved
    32  	// via the default AWS credential chain (env vars, shared credentials file, IAM roles, etc.).
    33  	AWSAccessKey string
    34  
    35  	// AWSSecretAccessKey is the AWS secret access key for SigV4 authentication.
    36  	// When unset, credentials are resolved via the default AWS credential chain
    37  	// (env vars, shared credentials file, IAM roles, etc.).
    38  	AWSSecretAccessKey string
    39  
    40  	// AWSSessionToken is the optional AWS session token for temporary credentials.
    41  	// When unset, resolved via the default AWS credential chain if applicable.
    42  	AWSSessionToken string
    43  
    44  	// AWSProfile is the AWS named profile for credential resolution via the provider chain.
    45  	AWSProfile string
    46  
    47  	// AWSRegion is the AWS region for the gateway URL and SigV4 signing.
    48  	// Resolved by precedence: ClientConfig.AWSRegion > AWS_REGION env var.
    49  	AWSRegion string
    50  
    51  	// WorkspaceID is sent as the anthropic-workspace-id header on every request.
    52  	// Resolved by precedence: ClientConfig.WorkspaceID > ANTHROPIC_AWS_WORKSPACE_ID env var. Required.
    53  	WorkspaceID string
    54  
    55  	// BaseURL overrides the default gateway base URL.
    56  	// Resolved by precedence: ClientConfig.BaseURL > ANTHROPIC_AWS_BASE_URL env > https://aws-external-anthropic.{region}.api.aws
    57  	BaseURL string
    58  
    59  	// SkipAuth skips all authentication (API key and SigV4) and the workspace ID requirement.
    60  	// This is useful when a gateway or proxy handles authentication on your behalf.
    61  	SkipAuth bool
    62  }
    63  
    64  // Client provides access to the Anthropic API via the AWS gateway.
    65  type Client struct {
    66  	Options     []option.RequestOption
    67  	Completions anthropic.CompletionService
    68  	Messages    anthropic.MessageService
    69  	Models      anthropic.ModelService
    70  	Files       anthropic.FileService
    71  	Skills      anthropic.SkillService
    72  	Beta        anthropic.BetaService
    73  }
    74  
    75  // NewClient creates a new AWS gateway client with the given configuration.
    76  //
    77  // Any additional [option.RequestOption] values are applied after the client's
    78  // internal options (base URL, auth, etc.), so they can be used to set custom
    79  // headers, timeouts, middleware, and other request-level settings. When SigV4
    80  // authentication is in use, the signing middleware runs after any middleware
    81  // registered through these options, so the signature covers their request
    82  // mutations. Per-request middleware passed at a method call site instead runs
    83  // after signing and must not mutate the request.
    84  //
    85  // Auth is resolved by precedence:
    86  //  1. APIKey arg (x-api-key header)
    87  //  2. AWSAccessKey + AWSSecretAccessKey args (SigV4)
    88  //  3. AWSProfile arg (SigV4 via provider chain)
    89  //  4. ANTHROPIC_AWS_API_KEY env var (x-api-key header)
    90  //  5. Default AWS credential chain (SigV4)
    91  func NewClient(ctx context.Context, cfg ClientConfig, opts ...option.RequestOption) (*Client, error) {
    92  	opts, err := awsauth.CreateClientOptions(ctx, toInternalConfig(cfg), awsResolveParams(), opts...)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	// We intentionally do not call anthropic.DefaultClientOptions() here.
    98  	// The AWS client resolves its own base URL, auth, and workspace ID — the
    99  	// base SDK defaults (ANTHROPIC_API_KEY, ANTHROPIC_BASE_URL) do not apply.
   100  
   101  	return &Client{
   102  		Options:     opts,
   103  		Completions: anthropic.NewCompletionService(opts...),
   104  		Messages:    anthropic.NewMessageService(opts...),
   105  		Models:      anthropic.NewModelService(opts...),
   106  		Files:       anthropic.NewFileService(opts...),
   107  		Skills:      anthropic.NewSkillService(opts...),
   108  		Beta:        anthropic.NewBetaService(opts...),
   109  	}, nil
   110  }
   111  
   112  func awsResolveParams() awsauth.ResolveParams {
   113  	return awsauth.ResolveParams{
   114  		EnvAPIKey:      "ANTHROPIC_AWS_API_KEY",
   115  		EnvWorkspaceID: "ANTHROPIC_AWS_WORKSPACE_ID",
   116  		EnvBaseURL:     "ANTHROPIC_AWS_BASE_URL",
   117  		DeriveBaseURL:  func(region string) string { return fmt.Sprintf("https://aws-external-anthropic.%s.api.aws", region) },
   118  		ServiceName:    defaultServiceName,
   119  	}
   120  }
   121  
   122  func toInternalConfig(cfg ClientConfig) awsauth.ClientConfig {
   123  	return awsauth.ClientConfig{
   124  		APIKey:             cfg.APIKey,
   125  		AWSAccessKey:       cfg.AWSAccessKey,
   126  		AWSSecretAccessKey: cfg.AWSSecretAccessKey,
   127  		AWSSessionToken:    cfg.AWSSessionToken,
   128  		AWSProfile:         cfg.AWSProfile,
   129  		AWSRegion:          cfg.AWSRegion,
   130  		WorkspaceID:        cfg.WorkspaceID,
   131  		BaseURL:            cfg.BaseURL,
   132  		SkipAuth:           cfg.SkipAuth,
   133  	}
   134  }