diff --git a/providers/anthropic/anthropic.go b/providers/anthropic/anthropic.go index 882a22c9c..49fba5002 100644 --- a/providers/anthropic/anthropic.go +++ b/providers/anthropic/anthropic.go @@ -16,13 +16,9 @@ import ( "charm.land/fantasy" "charm.land/fantasy/object" "charm.land/fantasy/providers/internal/httpheaders" - "github.com/aws/aws-sdk-go-v2/config" "github.com/charmbracelet/anthropic-sdk-go" - "github.com/charmbracelet/anthropic-sdk-go/bedrock" "github.com/charmbracelet/anthropic-sdk-go/option" "github.com/charmbracelet/anthropic-sdk-go/packages/param" - "github.com/charmbracelet/anthropic-sdk-go/vertex" - "golang.org/x/oauth2/google" ) // betaRequestOptions converts beta flag strings into request @@ -201,42 +197,17 @@ func (a *provider) LanguageModel(ctx context.Context, modelID string) (fantasy.L clientOptions = append(clientOptions, option.WithHTTPClient(a.options.client)) } if a.options.vertexProject != "" && a.options.vertexLocation != "" { - var credentials *google.Credentials - if a.options.skipAuth { - credentials = &google.Credentials{TokenSource: &googleDummyTokenSource{}} - } else { - var err error - credentials, err = google.FindDefaultCredentials(ctx, VertexAuthScope) - if err != nil { - return nil, err - } + var err error + clientOptions, err = a.configureVertexOptions(ctx, clientOptions) + if err != nil { + return nil, err } - - clientOptions = append( - clientOptions, - vertex.WithCredentials( - ctx, - a.options.vertexLocation, - a.options.vertexProject, - credentials, - ), - ) } if a.options.useBedrock { - modelID = bedrockPrefixModelWithRegion(modelID) - - if a.options.skipAuth || a.options.apiKey != "" { - clientOptions = append( - clientOptions, - bedrock.WithConfig(bedrockBasicAuthConfig(a.options.apiKey)), - ) - } else { - if cfg, err := config.LoadDefaultConfig(ctx); err == nil { - clientOptions = append( - clientOptions, - bedrock.WithConfig(cfg), - ) - } + var err error + modelID, clientOptions, err = a.configureBedrockOptions(ctx, modelID, clientOptions) + if err != nil { + return nil, err } if a.options.baseURL != "" { clientOptions = append(clientOptions, option.WithBaseURL(a.options.baseURL)) diff --git a/providers/anthropic/bedrock.go b/providers/anthropic/bedrock.go index 8d9b94959..aa29d34c4 100644 --- a/providers/anthropic/bedrock.go +++ b/providers/anthropic/bedrock.go @@ -1,3 +1,5 @@ +//go:build !noaws + package anthropic import ( diff --git a/providers/anthropic/bedrock_lm.go b/providers/anthropic/bedrock_lm.go new file mode 100644 index 000000000..b138529dc --- /dev/null +++ b/providers/anthropic/bedrock_lm.go @@ -0,0 +1,29 @@ +//go:build !noaws + +package anthropic + +import ( + "context" + + "github.com/aws/aws-sdk-go-v2/config" + "github.com/charmbracelet/anthropic-sdk-go/bedrock" + "github.com/charmbracelet/anthropic-sdk-go/option" +) + +// configureBedrockOptions applies AWS Bedrock-specific options when useBedrock is set. +// Returns the potentially modified modelID (with region prefix) and updated client options. +func (a *provider) configureBedrockOptions(ctx context.Context, modelID string, clientOptions []option.RequestOption) (string, []option.RequestOption, error) { + modelID = bedrockPrefixModelWithRegion(modelID) + + if a.options.skipAuth || a.options.apiKey != "" { + clientOptions = append(clientOptions, bedrock.WithConfig(bedrockBasicAuthConfig(a.options.apiKey))) + } else { + cfg, err := config.LoadDefaultConfig(ctx) + if err != nil { + return "", nil, err + } + clientOptions = append(clientOptions, bedrock.WithConfig(cfg)) + } + + return modelID, clientOptions, nil +} diff --git a/providers/anthropic/bedrock_lm_stub.go b/providers/anthropic/bedrock_lm_stub.go new file mode 100644 index 000000000..9cb950431 --- /dev/null +++ b/providers/anthropic/bedrock_lm_stub.go @@ -0,0 +1,14 @@ +//go:build noaws + +package anthropic + +import ( + "context" + "fmt" + + "github.com/charmbracelet/anthropic-sdk-go/option" +) + +func (a *provider) configureBedrockOptions(ctx context.Context, modelID string, clientOptions []option.RequestOption) (string, []option.RequestOption, error) { + return "", nil, fmt.Errorf("AWS Bedrock support not compiled in; remove -tags noaws") +} diff --git a/providers/anthropic/google.go b/providers/anthropic/google.go index a4e4ece32..22a494842 100644 --- a/providers/anthropic/google.go +++ b/providers/anthropic/google.go @@ -1,3 +1,5 @@ +//go:build !nogoogle + package anthropic import ( diff --git a/providers/anthropic/vertex_lm.go b/providers/anthropic/vertex_lm.go new file mode 100644 index 000000000..cd1f3ab2f --- /dev/null +++ b/providers/anthropic/vertex_lm.go @@ -0,0 +1,35 @@ +//go:build !nogoogle + +package anthropic + +import ( + "context" + + "github.com/charmbracelet/anthropic-sdk-go/option" + "github.com/charmbracelet/anthropic-sdk-go/vertex" + "golang.org/x/oauth2/google" +) + +// configureVertexOptions applies Google Vertex AI-specific options when both +// vertexProject and vertexLocation are set. +func (a *provider) configureVertexOptions(ctx context.Context, clientOptions []option.RequestOption) ([]option.RequestOption, error) { + var credentials *google.Credentials + if a.options.skipAuth { + credentials = &google.Credentials{TokenSource: &googleDummyTokenSource{}} + } else { + var err error + credentials, err = google.FindDefaultCredentials(ctx, VertexAuthScope) + if err != nil { + return nil, err + } + } + + clientOptions = append(clientOptions, vertex.WithCredentials( + ctx, + a.options.vertexLocation, + a.options.vertexProject, + credentials, + )) + + return clientOptions, nil +} diff --git a/providers/anthropic/vertex_lm_stub.go b/providers/anthropic/vertex_lm_stub.go new file mode 100644 index 000000000..e90551c3a --- /dev/null +++ b/providers/anthropic/vertex_lm_stub.go @@ -0,0 +1,14 @@ +//go:build nogoogle + +package anthropic + +import ( + "context" + "fmt" + + "github.com/charmbracelet/anthropic-sdk-go/option" +) + +func (a *provider) configureVertexOptions(ctx context.Context, clientOptions []option.RequestOption) ([]option.RequestOption, error) { + return nil, fmt.Errorf("Google Vertex AI support not compiled in; remove -tags nogoogle") +} diff --git a/providers/azure/azure.go b/providers/azure/azure.go index bec505cdb..f6d57fe56 100644 --- a/providers/azure/azure.go +++ b/providers/azure/azure.go @@ -1,3 +1,5 @@ +//go:build !noazure + // Package azure provides an implementation of the fantasy AI SDK for Azure's language models. package azure diff --git a/providers/azure/azure_test.go b/providers/azure/azure_test.go index 6113699d6..52c2589fd 100644 --- a/providers/azure/azure_test.go +++ b/providers/azure/azure_test.go @@ -1,3 +1,5 @@ +//go:build !noazure + package azure import ( diff --git a/providers/azure/useragent_test.go b/providers/azure/useragent_test.go index fa4a9e201..153460e61 100644 --- a/providers/azure/useragent_test.go +++ b/providers/azure/useragent_test.go @@ -1,3 +1,5 @@ +//go:build !noazure + package azure import ( diff --git a/providers/bedrock/bedrock.go b/providers/bedrock/bedrock.go index a430314e2..00b516ca0 100644 --- a/providers/bedrock/bedrock.go +++ b/providers/bedrock/bedrock.go @@ -1,3 +1,5 @@ +//go:build !noaws + // Package bedrock provides an implementation of the fantasy AI SDK for AWS Bedrock's language models. package bedrock diff --git a/providers/bedrock/useragent_test.go b/providers/bedrock/useragent_test.go index 03ca7a6b5..87d7914a9 100644 --- a/providers/bedrock/useragent_test.go +++ b/providers/bedrock/useragent_test.go @@ -1,3 +1,5 @@ +//go:build !noaws + package bedrock import ( diff --git a/providers/google/auth.go b/providers/google/auth.go index c993c1be1..7d56b06d9 100644 --- a/providers/google/auth.go +++ b/providers/google/auth.go @@ -1,3 +1,5 @@ +//go:build !nogoogle + // Package google provides an implementation of the fantasy AI SDK for Google's language models. package google diff --git a/providers/google/call_useragent.go b/providers/google/call_useragent.go index a3521dd75..47d2e2b9e 100644 --- a/providers/google/call_useragent.go +++ b/providers/google/call_useragent.go @@ -1,3 +1,5 @@ +//go:build !nogoogle + package google import ( diff --git a/providers/google/error.go b/providers/google/error.go index cd706384e..709a66b95 100644 --- a/providers/google/error.go +++ b/providers/google/error.go @@ -1,3 +1,5 @@ +//go:build !nogoogle + package google import ( diff --git a/providers/google/error_test.go b/providers/google/error_test.go index 693f35ee2..31caa482f 100644 --- a/providers/google/error_test.go +++ b/providers/google/error_test.go @@ -1,3 +1,5 @@ +//go:build !nogoogle + package google import ( diff --git a/providers/google/google.go b/providers/google/google.go index 700341f7c..4e0966da6 100644 --- a/providers/google/google.go +++ b/providers/google/google.go @@ -1,3 +1,5 @@ +//go:build !nogoogle + package google import ( diff --git a/providers/google/provider_options.go b/providers/google/provider_options.go index f05004405..84a9dd75d 100644 --- a/providers/google/provider_options.go +++ b/providers/google/provider_options.go @@ -1,3 +1,5 @@ +//go:build !nogoogle + // Package google provides an implementation of the fantasy AI SDK for Google's language models. package google diff --git a/providers/google/slice.go b/providers/google/slice.go index 215355efe..481cf6277 100644 --- a/providers/google/slice.go +++ b/providers/google/slice.go @@ -1,3 +1,5 @@ +//go:build !nogoogle + package google func depointerSlice[T any](s []*T) []T { diff --git a/providers/google/useragent_test.go b/providers/google/useragent_test.go index 5977977f8..c296f4d8d 100644 --- a/providers/google/useragent_test.go +++ b/providers/google/useragent_test.go @@ -1,3 +1,5 @@ +//go:build !nogoogle + package google import ( diff --git a/providers/kronk/kronk.go b/providers/kronk/kronk.go index 8f7c8815a..54d61b590 100644 --- a/providers/kronk/kronk.go +++ b/providers/kronk/kronk.go @@ -1,3 +1,5 @@ +//go:build !nokronk + // Package kronk provides an implementation of the fantasy AI SDK for local // models using the Kronk SDK. package kronk diff --git a/providers/kronk/language_model.go b/providers/kronk/language_model.go index dba0adce8..5705dd504 100644 --- a/providers/kronk/language_model.go +++ b/providers/kronk/language_model.go @@ -1,3 +1,5 @@ +//go:build !nokronk + package kronk import ( diff --git a/providers/kronk/language_model_hooks.go b/providers/kronk/language_model_hooks.go index a824f791d..8f6917dee 100644 --- a/providers/kronk/language_model_hooks.go +++ b/providers/kronk/language_model_hooks.go @@ -1,3 +1,5 @@ +//go:build !nokronk + package kronk import ( diff --git a/providers/kronk/options.go b/providers/kronk/options.go index 747920465..9d1269001 100644 --- a/providers/kronk/options.go +++ b/providers/kronk/options.go @@ -1,3 +1,5 @@ +//go:build !nokronk + package kronk import ( diff --git a/providers/kronk/provider_options.go b/providers/kronk/provider_options.go index 217242ea4..32c5ce594 100644 --- a/providers/kronk/provider_options.go +++ b/providers/kronk/provider_options.go @@ -1,3 +1,5 @@ +//go:build !nokronk + package kronk import ( diff --git a/providers/openrouter/language_model_hooks.go b/providers/openrouter/language_model_hooks.go index d38fb8b17..eaa74c858 100644 --- a/providers/openrouter/language_model_hooks.go +++ b/providers/openrouter/language_model_hooks.go @@ -1,3 +1,5 @@ +//go:build !noopenrouter + package openrouter import ( diff --git a/providers/openrouter/openrouter.go b/providers/openrouter/openrouter.go index 623186bc6..da6d70e82 100644 --- a/providers/openrouter/openrouter.go +++ b/providers/openrouter/openrouter.go @@ -1,3 +1,5 @@ +//go:build !noopenrouter + // Package openrouter provides an implementation of the fantasy AI SDK for OpenRouter's language models. package openrouter diff --git a/providers/openrouter/provider_options.go b/providers/openrouter/provider_options.go index 5ef7f9dd4..50b8e8e85 100644 --- a/providers/openrouter/provider_options.go +++ b/providers/openrouter/provider_options.go @@ -1,3 +1,5 @@ +//go:build !noopenrouter + // Package openrouter provides an implementation of the fantasy AI SDK for OpenRouter's language models. package openrouter diff --git a/providers/openrouter/useragent_test.go b/providers/openrouter/useragent_test.go index 5c38db8ea..670aaa79c 100644 --- a/providers/openrouter/useragent_test.go +++ b/providers/openrouter/useragent_test.go @@ -1,3 +1,5 @@ +//go:build !noopenrouter + package openrouter import ( diff --git a/providers/vercel/language_model_hooks.go b/providers/vercel/language_model_hooks.go index e3b829210..bb30cccdc 100644 --- a/providers/vercel/language_model_hooks.go +++ b/providers/vercel/language_model_hooks.go @@ -1,3 +1,5 @@ +//go:build !novercel + package vercel import ( diff --git a/providers/vercel/provider_options.go b/providers/vercel/provider_options.go index 163921b97..a4d0aa9e3 100644 --- a/providers/vercel/provider_options.go +++ b/providers/vercel/provider_options.go @@ -1,3 +1,5 @@ +//go:build !novercel + // Package vercel provides an implementation of the fantasy AI SDK for Vercel AI Gateway. package vercel diff --git a/providers/vercel/vercel.go b/providers/vercel/vercel.go index 65e8d31c3..af6fc9d68 100644 --- a/providers/vercel/vercel.go +++ b/providers/vercel/vercel.go @@ -1,3 +1,5 @@ +//go:build !novercel + // Package vercel provides an implementation of the fantasy AI SDK for Vercel AI Gateway. package vercel diff --git a/providertests/azure_responses_test.go b/providertests/azure_responses_test.go index b7c89084b..97bd6fdf7 100644 --- a/providertests/azure_responses_test.go +++ b/providertests/azure_responses_test.go @@ -1,3 +1,5 @@ +//go:build !noazure + package providertests import ( diff --git a/providertests/azure_test.go b/providertests/azure_test.go index 198b8dc1c..65ae53c15 100644 --- a/providertests/azure_test.go +++ b/providertests/azure_test.go @@ -1,3 +1,5 @@ +//go:build !noazure + package providertests import ( diff --git a/providertests/bedrock_test.go b/providertests/bedrock_test.go index 382201ea0..6b45153be 100644 --- a/providertests/bedrock_test.go +++ b/providertests/bedrock_test.go @@ -1,3 +1,5 @@ +//go:build !noaws + package providertests import ( diff --git a/providertests/google_test.go b/providertests/google_test.go index cbd9fec8b..3b71d04f4 100644 --- a/providertests/google_test.go +++ b/providertests/google_test.go @@ -1,3 +1,5 @@ +//go:build !nogoogle + package providertests import ( diff --git a/providertests/image_upload_test.go b/providertests/image_upload_test.go index 5ec3ea4f3..a193f0ff6 100644 --- a/providertests/image_upload_test.go +++ b/providertests/image_upload_test.go @@ -1,3 +1,5 @@ +//go:build !nogoogle + package providertests import ( diff --git a/providertests/openrouter_test.go b/providertests/openrouter_test.go index b4b36627e..999fb39f8 100644 --- a/providertests/openrouter_test.go +++ b/providertests/openrouter_test.go @@ -1,3 +1,5 @@ +//go:build !noopenrouter + package providertests import ( diff --git a/providertests/provider_registry_test.go b/providertests/provider_registry_test.go index c0f3f848e..c2ef7b34a 100644 --- a/providertests/provider_registry_test.go +++ b/providertests/provider_registry_test.go @@ -1,3 +1,5 @@ +//go:build !nogoogle && !noopenrouter + package providertests import ( diff --git a/providertests/vercel_test.go b/providertests/vercel_test.go index 0361ec0ba..bdb1a7c76 100644 --- a/providertests/vercel_test.go +++ b/providertests/vercel_test.go @@ -1,3 +1,5 @@ +//go:build !novercel + package providertests import (