-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode.go
More file actions
175 lines (156 loc) · 4.57 KB
/
opencode.go
File metadata and controls
175 lines (156 loc) · 4.57 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package profiles
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
)
// OpenCodeProfile implements Profile for the `opencode` CLI tool.
type OpenCodeProfile struct {
}
func (o *OpenCodeProfile) Name() string { return "OpenCode" }
func (o *OpenCodeProfile) BinaryName() string { return "opencode" }
func (o *OpenCodeProfile) CommonPaths() []string {
home, err := os.UserHomeDir()
if err != nil {
return nil
}
return []string{
filepath.Join(home, ".opencode", "bin", "opencode"),
filepath.Join(home, ".local", "bin", "opencode"),
}
}
func (o *OpenCodeProfile) InstallHint() string {
return "curl -fsSL https://opencode.ai/install | bash"
}
func (o *OpenCodeProfile) UninstallHint() string {
return "opencode uninstall --force\nrm -rf ~/.opencode/bin"
}
func (o *OpenCodeProfile) Uninstall() func() error {
return func() error {
if err := exec.Command("opencode", "uninstall", "--force").Run(); err != nil {
return err
}
home, err := os.UserHomeDir()
if err != nil {
return err
}
return os.RemoveAll(filepath.Join(home, ".opencode", "bin"))
}
}
func (o *OpenCodeProfile) SupportedBackends() []Backend {
return []Backend{
{Type: BackendAnthropic, DisplayName: "Anthropic API"},
{Type: BackendBedrock, DisplayName: "AWS Bedrock"},
{Type: BackendVertex, DisplayName: "Google Vertex"},
{Type: BackendOpenAI, DisplayName: "OpenAI Compatible"},
}
}
func (o *OpenCodeProfile) RequiredCompat(b Backend) []string {
switch b.Type {
case BackendAnthropic:
return []string{"anthropic_messages"}
case BackendBedrock:
return []string{"bedrock_converse"}
case BackendVertex:
return []string{"google_generate_content"}
case BackendOpenAI:
return []string{"openai_chat"}
default:
return nil
}
}
func (o *OpenCodeProfile) Env(apertureHost string, b Backend) (map[string]string, error) {
switch b.Type {
case BackendAnthropic:
return map[string]string{
"ANTHROPIC_BASE_URL": apertureHost + "/v1",
"ANTHROPIC_AUTH_TOKEN": "-",
}, nil
case BackendBedrock:
// Dummy AWS credentials so the SDK doesn't fail credential resolution;
// aperture handles actual auth at the /bedrock endpoint.
return map[string]string{
"AWS_ACCESS_KEY_ID": "not-needed",
"AWS_SECRET_ACCESS_KEY": "not-needed",
"AWS_REGION": "us-east-1",
}, nil
case BackendVertex:
return map[string]string{
"GOOGLE_CLOUD_PROJECT": "_aperture_auto_vertex_project_id_",
"GOOGLE_CLOUD_LOCATION": "_aperture_auto_vertex_region_",
}, nil
case BackendOpenAI:
return map[string]string{
"OPENAI_API_KEY": "not-needed",
"OPENAI_BASE_URL": apertureHost + "/v1",
}, nil
default:
return nil, fmt.Errorf("unsupported backend %q for OpenCode", b.Type)
}
}
type opencodeConfig struct {
Schema string `json:"$schema,omitempty"`
Provider map[string]opencodeProvider `json:"provider,omitempty"`
}
type opencodeProvider struct {
Options map[string]string `json:"options,omitempty"`
}
func (o *OpenCodeProfile) WriteConfig(apertureHost string, b Backend) (string, string, func(), error) {
var providerKey string
var options map[string]string
switch b.Type {
case BackendAnthropic:
providerKey = "anthropic"
options = map[string]string{
"apiKey": "{env:ANTHROPIC_AUTH_TOKEN}",
"baseURL": "{env:ANTHROPIC_BASE_URL}",
}
case BackendBedrock:
providerKey = "amazon-bedrock"
options = map[string]string{
"region": "us-east-1",
"endpoint": apertureHost + "/bedrock",
}
case BackendVertex:
providerKey = "google-vertex"
options = map[string]string{
"project": "_aperture_auto_vertex_project_id_",
"location": "_aperture_auto_vertex_region_",
"baseURL": apertureHost + "/v1",
}
case BackendOpenAI:
providerKey = "openai"
options = map[string]string{
"apiKey": "{env:OPENAI_API_KEY}",
"baseURL": "{env:OPENAI_BASE_URL}",
}
default:
return "", "", nil, fmt.Errorf("unsupported backend %q for OpenCode", b.Type)
}
provider := opencodeProvider{Options: options}
cfg := opencodeConfig{
Schema: "https://opencode.ai/config.json",
Provider: map[string]opencodeProvider{
providerKey: provider,
},
}
data, err := json.Marshal(cfg)
if err != nil {
return "", "", nil, err
}
home, err := os.UserHomeDir()
if err != nil {
return "", "", nil, err
}
configDir := filepath.Join(home, ".opencode")
if err := os.MkdirAll(configDir, 0o700); err != nil {
return "", "", nil, err
}
path := filepath.Join(configDir, "tmp_aperture_config.json")
if err := os.WriteFile(path, data, 0o600); err != nil {
return "", "", nil, err
}
return "OPENCODE_CONFIG", path, func() { os.Remove(path) }, nil
}