Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 8 additions & 37 deletions providers/anthropic/anthropic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions providers/anthropic/bedrock.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !noaws

package anthropic

import (
Expand Down
29 changes: 29 additions & 0 deletions providers/anthropic/bedrock_lm.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions providers/anthropic/bedrock_lm_stub.go
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 2 additions & 0 deletions providers/anthropic/google.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nogoogle

package anthropic

import (
Expand Down
35 changes: 35 additions & 0 deletions providers/anthropic/vertex_lm.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions providers/anthropic/vertex_lm_stub.go
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 2 additions & 0 deletions providers/azure/azure.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !noazure

// Package azure provides an implementation of the fantasy AI SDK for Azure's language models.
package azure

Expand Down
2 changes: 2 additions & 0 deletions providers/azure/azure_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !noazure

package azure

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/azure/useragent_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !noazure

package azure

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/bedrock/bedrock.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 2 additions & 0 deletions providers/bedrock/useragent_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !noaws

package bedrock

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/google/auth.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nogoogle

// Package google provides an implementation of the fantasy AI SDK for Google's language models.
package google

Expand Down
2 changes: 2 additions & 0 deletions providers/google/call_useragent.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nogoogle

package google

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/google/error.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nogoogle

package google

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/google/error_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nogoogle

package google

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/google/google.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nogoogle

package google

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/google/provider_options.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nogoogle

// Package google provides an implementation of the fantasy AI SDK for Google's language models.
package google

Expand Down
2 changes: 2 additions & 0 deletions providers/google/slice.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nogoogle

package google

func depointerSlice[T any](s []*T) []T {
Expand Down
2 changes: 2 additions & 0 deletions providers/google/useragent_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nogoogle

package google

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/kronk/kronk.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions providers/kronk/language_model.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nokronk

package kronk

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/kronk/language_model_hooks.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nokronk

package kronk

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/kronk/options.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nokronk

package kronk

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/kronk/provider_options.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nokronk

package kronk

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/openrouter/language_model_hooks.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !noopenrouter

package openrouter

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/openrouter/openrouter.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !noopenrouter

// Package openrouter provides an implementation of the fantasy AI SDK for OpenRouter's language models.
package openrouter

Expand Down
2 changes: 2 additions & 0 deletions providers/openrouter/provider_options.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !noopenrouter

// Package openrouter provides an implementation of the fantasy AI SDK for OpenRouter's language models.
package openrouter

Expand Down
2 changes: 2 additions & 0 deletions providers/openrouter/useragent_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !noopenrouter

package openrouter

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/vercel/language_model_hooks.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !novercel

package vercel

import (
Expand Down
2 changes: 2 additions & 0 deletions providers/vercel/provider_options.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !novercel

// Package vercel provides an implementation of the fantasy AI SDK for Vercel AI Gateway.
package vercel

Expand Down
2 changes: 2 additions & 0 deletions providers/vercel/vercel.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !novercel

// Package vercel provides an implementation of the fantasy AI SDK for Vercel AI Gateway.
package vercel

Expand Down
2 changes: 2 additions & 0 deletions providertests/azure_responses_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !noazure

package providertests

import (
Expand Down
2 changes: 2 additions & 0 deletions providertests/azure_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !noazure

package providertests

import (
Expand Down
2 changes: 2 additions & 0 deletions providertests/bedrock_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !noaws

package providertests

import (
Expand Down
2 changes: 2 additions & 0 deletions providertests/google_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nogoogle

package providertests

import (
Expand Down
2 changes: 2 additions & 0 deletions providertests/image_upload_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nogoogle

package providertests

import (
Expand Down
2 changes: 2 additions & 0 deletions providertests/openrouter_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !noopenrouter

package providertests

import (
Expand Down
2 changes: 2 additions & 0 deletions providertests/provider_registry_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !nogoogle && !noopenrouter

package providertests

import (
Expand Down
2 changes: 2 additions & 0 deletions providertests/vercel_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !novercel

package providertests

import (
Expand Down
Loading