This repository was archived by the owner on Sep 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathbedrock.go
More file actions
100 lines (84 loc) · 2.55 KB
/
bedrock.go
File metadata and controls
100 lines (84 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package provider
import (
"context"
"errors"
"fmt"
"os"
"strings"
"github.com/opencode-ai/opencode/internal/llm/tools"
"github.com/opencode-ai/opencode/internal/message"
)
type bedrockOptions struct {
// Bedrock specific options can be added here
}
type BedrockOption func(*bedrockOptions)
type bedrockClient struct {
providerOptions providerClientOptions
options bedrockOptions
childProvider ProviderClient
}
type BedrockClient ProviderClient
func newBedrockClient(opts providerClientOptions) BedrockClient {
bedrockOpts := bedrockOptions{}
// Apply bedrock specific options if they are added in the future
// Get AWS region from environment
region := os.Getenv("AWS_REGION")
if region == "" {
region = os.Getenv("AWS_DEFAULT_REGION")
}
if region == "" {
region = "us-east-1" // default region
}
if len(region) < 2 {
return &bedrockClient{
providerOptions: opts,
options: bedrockOpts,
childProvider: nil, // Will cause an error when used
}
}
// Prefix the model name with region
regionPrefix := region[:2]
modelName := opts.model.APIModel
opts.model.APIModel = fmt.Sprintf("%s.%s", regionPrefix, modelName)
// 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),
WithAnthropicDisableCache(),
)
return &bedrockClient{
providerOptions: opts,
options: bedrockOpts,
childProvider: newAnthropicClient(anthropicOpts),
}
}
// 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,
}
}
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")
}
return b.childProvider.send(ctx, messages, tools)
}
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
}
return b.childProvider.stream(ctx, messages, tools)
}