SuperCoder/internal/llm/provider/bedrock.go

100 lines
2.5 KiB
Go
Raw Normal View History

2025-04-09 17:45:41 +02:00
package provider
import (
"context"
"errors"
"fmt"
"os"
"strings"
2025-04-24 18:25:52 +02:00
"github.com/opencode-ai/opencode/internal/llm/tools"
"github.com/opencode-ai/opencode/internal/message"
2025-04-09 17:45:41 +02:00
)
type bedrockOptions struct {
// Bedrock specific options can be added here
2025-04-09 17:45:41 +02:00
}
type BedrockOption func(*bedrockOptions)
2025-04-09 17:45:41 +02:00
type bedrockClient struct {
providerOptions providerClientOptions
options bedrockOptions
childProvider ProviderClient
2025-04-09 17:45:41 +02:00
}
type BedrockClient ProviderClient
func newBedrockClient(opts providerClientOptions) BedrockClient {
bedrockOpts := bedrockOptions{}
// Apply bedrock specific options if they are added in the future
2025-04-09 17:45:41 +02:00
// Get AWS region from environment
2025-04-09 17:45:41 +02:00
region := os.Getenv("AWS_REGION")
if region == "" {
region = os.Getenv("AWS_DEFAULT_REGION")
}
if region == "" {
2025-04-09 20:05:22 +02:00
region = "us-east-1" // default region
2025-04-09 17:45:41 +02:00
}
if len(region) < 2 {
return &bedrockClient{
providerOptions: opts,
options: bedrockOpts,
childProvider: nil, // Will cause an error when used
}
2025-04-09 17:45:41 +02:00
}
// Prefix the model name with region
2025-04-09 17:45:41 +02:00
regionPrefix := region[:2]
modelName := opts.model.APIModel
opts.model.APIModel = fmt.Sprintf("%s.%s", regionPrefix, modelName)
2025-04-09 17:45:41 +02:00
// Determine which provider to use based on the model
if strings.Contains(string(opts.model.APIModel), "anthropic") {
// Create Anthropic client with Bedrock configuration
anthropicOpts := opts
anthropicOpts.anthropicOptions = append(anthropicOpts.anthropicOptions,
WithAnthropicBedrock(true),
2025-04-09 17:45:41 +02:00
WithAnthropicDisableCache(),
)
return &bedrockClient{
providerOptions: opts,
options: bedrockOpts,
childProvider: newAnthropicClient(anthropicOpts),
2025-04-09 17:45:41 +02:00
}
}
// Return client with nil childProvider if model is not supported
// This will cause an error when used
return &bedrockClient{
providerOptions: opts,
options: bedrockOpts,
childProvider: nil,
2025-04-09 17:45:41 +02:00
}
}
func (b *bedrockClient) send(ctx context.Context, messages []message.Message, tools []tools.BaseTool) (*ProviderResponse, error) {
if b.childProvider == nil {
return nil, errors.New("unsupported model for bedrock provider")
2025-04-09 17:45:41 +02:00
}
return b.childProvider.send(ctx, messages, tools)
2025-04-09 17:45:41 +02:00
}
func (b *bedrockClient) stream(ctx context.Context, messages []message.Message, tools []tools.BaseTool) <-chan ProviderEvent {
eventChan := make(chan ProviderEvent)
if b.childProvider == nil {
go func() {
eventChan <- ProviderEvent{
Type: EventError,
Error: errors.New("unsupported model for bedrock provider"),
}
close(eventChan)
}()
return eventChan
2025-04-09 17:45:41 +02:00
}
return b.childProvider.stream(ctx, messages, tools)
}