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

     1  // Package googlecloud provides client configuration for Claude Platform on
     2  // Google Cloud — the first-party Anthropic API served through the Google Cloud
     3  // gateway.
     4  //
     5  // This is distinct from the older [github.com/anthropics/anthropic-sdk-go/vertex]
     6  // package, which targets the :rawPredict publisher-model API (publisher
     7  // model IDs, messages only). This client speaks the full first-party Anthropic
     8  // API: requests pass through the gateway unchanged — standard /v1/* paths,
     9  // standard model names, the complete API surface.
    10  //
    11  // The deprecated text Completions API is intentionally not exposed.
    12  package googlecloud
    13  
    14  import (
    15  	"context"
    16  
    17  	"golang.org/x/oauth2"
    18  
    19  	"github.com/anthropics/anthropic-sdk-go"
    20  	"github.com/anthropics/anthropic-sdk-go/option"
    21  )
    22  
    23  // ClientConfig holds the configuration for creating a Claude Platform on Google Cloud client.
    24  type ClientConfig struct {
    25  	// Project is the GCP consumer project ID. Resolved by precedence:
    26  	//  1. ClientConfig.Project
    27  	//  2. The ANTHROPIC_GOOGLE_CLOUD_PROJECT environment variable
    28  	//  3. The GOOGLE_CLOUD_PROJECT environment variable
    29  	//  4. The project reported by Application Default Credentials
    30  	//
    31  	// The ADC fallback (4) applies only when TokenSource is nil and SkipAuth is
    32  	// false; with an explicit TokenSource or SkipAuth, set Project (or BaseURL)
    33  	// explicitly.
    34  	Project string
    35  
    36  	// Location is the GCP location. Optional — defaults to "global", which is
    37  	// the region the gateway should normally be addressed through. Resolved by
    38  	// precedence:
    39  	//  1. ClientConfig.Location
    40  	//  2. The ANTHROPIC_GOOGLE_CLOUD_LOCATION environment variable
    41  	//  3. "global"
    42  	Location string
    43  
    44  	// WorkspaceID is the Anthropic workspace ID. Required: resolved by
    45  	// precedence
    46  	//  1. ClientConfig.WorkspaceID
    47  	//  2. The ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID environment variable
    48  	//
    49  	// [NewClient] returns an error when neither is set, unless SkipAuth is true
    50  	// and BaseURL is set explicitly.
    51  	WorkspaceID string
    52  
    53  	// BaseURL overrides the default gateway base URL. Resolved by precedence:
    54  	// ClientConfig.BaseURL > ANTHROPIC_GOOGLE_CLOUD_BASE_URL env > derived from
    55  	// project, location, and workspace ID.
    56  	BaseURL string
    57  
    58  	// TokenSource overrides Application Default Credentials for authentication.
    59  	// When nil, credentials are resolved via Google ADC with the cloud-platform scope.
    60  	TokenSource oauth2.TokenSource
    61  
    62  	// SkipAuth skips authentication, for when a gateway or proxy handles
    63  	// authentication upstream. No token is attached when SkipAuth is set.
    64  	// A workspace ID is still needed to derive the base URL —
    65  	// set BaseURL explicitly to construct without one. Mutually exclusive with
    66  	// TokenSource — setting both is a construction-time error.
    67  	SkipAuth bool
    68  }
    69  
    70  // Client provides access to the Anthropic API via the Google Cloud gateway. It
    71  // mirrors the surface of [anthropic.Client]; the gateway proxies the entire
    72  // first-party API. The deprecated Completions service is intentionally omitted.
    73  type Client struct {
    74  	Options  []option.RequestOption
    75  	Messages anthropic.MessageService
    76  	Models   anthropic.ModelService
    77  	Files    anthropic.FileService
    78  	Skills   anthropic.SkillService
    79  	Beta     anthropic.BetaService
    80  }
    81  
    82  // NewClient creates a new Claude Platform on Google Cloud client with the given
    83  // configuration.
    84  //
    85  // Authentication uses Google credentials by precedence:
    86  //  1. TokenSource arg
    87  //  2. Application Default Credentials (cloud-platform scope)
    88  //
    89  // The ctx is used for credential discovery during construction (e.g. probing
    90  // the metadata server) and is not retained afterward; it does not need to
    91  // outlive the client. Use a per-request context for individual API calls.
    92  func NewClient(ctx context.Context, cfg ClientConfig) (*Client, error) {
    93  	opts, err := createClientOptions(ctx, cfg)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	// We intentionally do not call anthropic.DefaultClientOptions() here.
    99  	// This client resolves its own base URL, auth, and workspace ID — the base
   100  	// SDK defaults (ANTHROPIC_API_KEY, ANTHROPIC_BASE_URL) do not apply.
   101  
   102  	return &Client{
   103  		Options:  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  }