-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathremote_tool_opencode.go
More file actions
99 lines (86 loc) · 2.24 KB
/
remote_tool_opencode.go
File metadata and controls
99 lines (86 loc) · 2.24 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
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)
type OpencodeAdapter struct {
app *App
}
func NewOpencodeAdapter(app *App) *OpencodeAdapter {
return &OpencodeAdapter{app: app}
}
func (a *OpencodeAdapter) ProviderName() string {
return "opencode"
}
func (a *OpencodeAdapter) BuildCommand(spec LaunchSpec) (CommandSpec, error) {
if spec.SessionID == "" {
return CommandSpec{}, fmt.Errorf("opencode session id is required")
}
cfg, err := a.app.LoadConfig()
if err != nil {
return CommandSpec{}, err
}
if err := a.app.syncToOpencodeSettings(cfg, spec.ProjectPath, spec.SessionID); err != nil {
return CommandSpec{}, err
}
tm := NewToolManager(a.app)
status := tm.GetToolStatus("opencode")
if !status.Installed || status.Path == "" {
return CommandSpec{}, fmt.Errorf("opencode is not installed")
}
env := a.buildCommandEnv(spec.Env, spec.ModelID)
return CommandSpec{
Command: a.resolveExecutable(status.Path),
Cwd: spec.ProjectPath,
Env: env,
Cols: 120,
Rows: 32,
}, nil
}
func (a *OpencodeAdapter) resolveExecutable(path string) string {
cleaned := filepath.Clean(path)
if runtime.GOOS != "windows" {
return cleaned
}
ext := strings.ToLower(filepath.Ext(cleaned))
if ext == ".cmd" || ext == ".bat" || ext == ".ps1" {
exePath := filepath.Join(filepath.Dir(cleaned), "opencode.exe")
if _, err := os.Stat(exePath); err == nil {
return exePath
}
}
return cleaned
}
func (a *OpencodeAdapter) buildCommandEnv(base map[string]string, modelID string) map[string]string {
env := map[string]string{}
for k, v := range base {
env[k] = v
}
home, _ := os.UserHomeDir()
localToolPath := filepath.Join(home, ".cceasy", "tools")
npmPath := filepath.Join(os.Getenv("AppData"), "npm")
nodePath := `C:\Program Files\nodejs`
gitCmdPath := `C:\Program Files\Git\cmd`
gitBinPath := `C:\Program Files\Git\bin`
gitUsrBinPath := `C:\Program Files\Git\usr\bin`
basePath := env["PATH"]
if strings.TrimSpace(basePath) == "" {
basePath = os.Getenv("PATH")
}
env["PATH"] = strings.Join([]string{
localToolPath,
npmPath,
nodePath,
gitCmdPath,
gitBinPath,
gitUsrBinPath,
basePath,
}, ";")
if modelID != "" && env["OPENCODE_MODEL"] == "" {
env["OPENCODE_MODEL"] = modelID
}
return env
}