-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathopencode.go
More file actions
264 lines (224 loc) · 6.85 KB
/
Copy pathopencode.go
File metadata and controls
264 lines (224 loc) · 6.85 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package commands
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/docker/model-runner/cmd/cli/pkg/standalone"
"github.com/docker/model-runner/cmd/cli/pkg/types"
"github.com/spf13/cobra"
)
// determineHostPort determines the port to use for host access to model-runner.
//
// The function uses the following logic:
// - For Desktop engine kind: returns the default port (modelRunner global is used)
// - For Cloud/Moby engine kinds: uses the port from the runner parameter if available
// - Falls back to default port if no port is found
//
// Note: The runner parameter is only used for Cloud/Moby engine kinds.
// For Desktop engine kinds, the global modelRunner context is used instead.
func determineHostPort(runner *standaloneRunner) uint16 {
kind := modelRunner.EngineKind()
// For Desktop, use the default port
if kind == types.ModelRunnerEngineKindDesktop {
return standalone.DefaultControllerPortMoby
}
// For Cloud/Moby, use the actual port from the runner
if runner != nil && runner.hostPort != 0 {
return runner.hostPort
}
if runner != nil && runner.gatewayPort != 0 {
return runner.gatewayPort
}
// Fallback to default
return standalone.DefaultControllerPortMoby
}
// setupOpenCode writes the opencode.json and model.json configuration files.
func setupOpenCode(cmd *cobra.Command, runner *standaloneRunner, model string) error {
// Check if model exists, pull if not
if err := ensureModelExists(cmd, model); err != nil {
return err
}
// Write opencode.json config
if err := writeOpenCodeConfig(runner, model); err != nil {
return fmt.Errorf("failed to write opencode config: %w", err)
}
// Write model.json state
if err := writeOpenCodeState(model); err != nil {
return fmt.Errorf("failed to write opencode state: %w", err)
}
return nil
}
// ensureModelExists checks if the model exists locally, and pulls it if not.
func ensureModelExists(cmd *cobra.Command, model string) error {
models, err := desktopClient.List()
if err != nil {
return fmt.Errorf("failed to list models: %w", err)
}
// Check if model exists
modelExists := false
for _, m := range models {
for _, tag := range m.Tags {
if tag == model || strings.TrimPrefix(tag, "ai/") == model {
modelExists = true
break
}
}
if modelExists {
break
}
}
if !modelExists {
cmd.Printf("Model %s not found locally. Pulling...\n", model)
if err := pullModel(cmd, desktopClient, model); err != nil {
return fmt.Errorf("failed to pull model: %w", err)
}
}
return nil
}
// writeOpenCodeConfig writes the ~/.config/opencode/opencode.json file.
func writeOpenCodeConfig(runner *standaloneRunner, model string) error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
configPath := filepath.Join(home, ".config", "opencode", "opencode.json")
if err := os.MkdirAll(filepath.Dir(configPath), 0o755); err != nil {
return err
}
config := make(map[string]any)
if data, err := os.ReadFile(configPath); err == nil {
_ = json.Unmarshal(data, &config) // Ignore parse errors; treat missing/corrupt files as empty
}
config["$schema"] = "https://opencode.ai/config.json"
provider, ok := config["provider"].(map[string]any)
if !ok {
provider = make(map[string]any)
}
// Determine the correct port for host access
port := determineHostPort(runner)
opencodeBaseURL := fmt.Sprintf("http://127.0.0.1:%d/v1", port)
dmr, ok := provider["dmr"].(map[string]any)
if !ok {
dmr = map[string]any{
"npm": "@ai-sdk/openai-compatible",
"name": "Docker Model Runner (local)",
"options": map[string]any{
"baseURL": opencodeBaseURL,
},
}
} else {
// Update baseURL in existing config
if options, ok := dmr["options"].(map[string]any); ok {
options["baseURL"] = opencodeBaseURL
} else {
dmr["options"] = map[string]any{
"baseURL": opencodeBaseURL,
}
}
}
models, ok := dmr["models"].(map[string]any)
if !ok {
models = make(map[string]any)
}
// Add model entry
entry := map[string]any{
"name": model,
"_launch": true,
}
models[model] = entry
dmr["models"] = models
provider["dmr"] = dmr
config["provider"] = provider
configData, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
return os.WriteFile(configPath, configData, 0o644)
}
// writeOpenCodeState writes the ~/.local/state/opencode/model.json file.
func writeOpenCodeState(model string) error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
statePath := filepath.Join(home, ".local", "state", "opencode", "model.json")
if err := os.MkdirAll(filepath.Dir(statePath), 0o755); err != nil {
return err
}
state := map[string]any{
"recent": []any{},
"favorite": []any{},
"variant": map[string]any{},
}
if data, err := os.ReadFile(statePath); err == nil {
_ = json.Unmarshal(data, &state) // Ignore parse errors; use defaults
}
recent, _ := state["recent"].([]any)
// Remove existing entry for this model if present
newRecent := []any{}
for _, entry := range recent {
e, ok := entry.(map[string]any)
if !ok || e["providerID"] != "dmr" {
newRecent = append(newRecent, entry)
continue
}
modelID, ok := e["modelID"].(string)
if !ok || modelID != model {
newRecent = append(newRecent, entry)
}
}
// Prepend the new model
newRecent = append([]any{
map[string]any{
"providerID": "dmr",
"modelID": model,
},
}, newRecent...)
// Keep only the most recent 10 models
const maxRecentModels = 10
if len(newRecent) > maxRecentModels {
newRecent = newRecent[:maxRecentModels]
}
state["recent"] = newRecent
stateData, err := json.MarshalIndent(state, "", " ")
if err != nil {
return err
}
return os.WriteFile(statePath, stateData, 0o644)
}
// launchOpenCode launches opencode with automatic configuration.
// It handles model verification, config file creation, and state management.
func launchOpenCode(cmd *cobra.Command, baseURL string, model string, runner *standaloneRunner, appArgs []string, dryRun bool) error {
if !dryRun {
if _, err := exec.LookPath("opencode"); err != nil {
cmd.PrintErrf("%q executable not found in PATH.\n", "opencode")
cmd.PrintErrf("Configure your app to use:\n")
env := openaiEnv(openaiPathSuffix)(baseURL)
for _, e := range env {
cmd.PrintErrf(" %s\n", e)
}
return fmt.Errorf("opencode not found; please install it and re-run")
}
}
// Setup opencode configuration (skip in dry-run mode)
if model != "" && !dryRun {
if err := setupOpenCode(cmd, runner, model); err != nil {
return fmt.Errorf("failed to setup opencode: %w", err)
}
}
env := openaiEnv(openaiPathSuffix)(baseURL)
if dryRun {
cmd.Printf("Would run: opencode %s\n", strings.Join(appArgs, " "))
for _, e := range env {
cmd.Printf(" %s\n", e)
}
if model != "" {
cmd.Printf("Would configure opencode with model: %s\n", model)
}
return nil
}
return runExternal(cmd, withEnv(env...), "opencode", appArgs...)
}