-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_config.go
More file actions
197 lines (180 loc) · 5.22 KB
/
Copy pathclient_config.go
File metadata and controls
197 lines (180 loc) · 5.22 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
package main
import (
"encoding/json"
"os"
"path/filepath"
)
// ClientConfig holds user defaults for new session creation.
// Stored at ~/.orbitor/config.json.
//
// Example:
//
// {
// "serverURL": "http://127.0.0.1:8080",
// "listenAddr": "127.0.0.1:8080",
// "defaultBackend": "claude",
// "defaultModel": "claude-sonnet-4-6",
// "skipPermissions": false,
// "planMode": false
// }
type ClientConfig struct {
ServerURL string `json:"serverURL"`
ListenAddr string `json:"listenAddr"`
DefaultBackend string `json:"defaultBackend"`
DefaultModel string `json:"defaultModel"`
SkipPermissions bool `json:"skipPermissions"`
PlanMode bool `json:"planMode"`
}
// defaultClientConfig returns built-in defaults used when no config file exists.
func defaultClientConfig() ClientConfig {
return ClientConfig{
ServerURL: "http://127.0.0.1:8080",
ListenAddr: "127.0.0.1:8080",
DefaultBackend: "claude",
}
}
// OrbitorDir returns the path to ~/.orbitor/ and ensures it exists.
func OrbitorDir() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
dir := filepath.Join(home, ".orbitor")
if err := os.MkdirAll(dir, 0o755); err != nil {
return "", err
}
return dir, nil
}
// ClientConfigPath returns the path to ~/.orbitor/config.json.
func ClientConfigPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".orbitor", "config.json"), nil
}
// LoadMCPServers reads MCP server definitions from the backend's native config
// file and returns them as a slice suitable for the ACP session/new mcpServers
// parameter. Returns an empty slice (never nil) if no servers are configured.
//
// Claude Code: ~/.claude.json → { "mcpServers": { "name": { ... } } }
// Copilot CLI: ~/.copilot/mcp-config.json → { "mcpServers": { "name": { ... } } }
//
// Both also support a project-local .mcp.json in the working directory.
func LoadMCPServers(backend, workingDir string) []any {
home, err := os.UserHomeDir()
if err != nil {
return []any{}
}
// Determine config file paths to read (global + project-local).
var paths []string
switch backend {
case "claude":
paths = append(paths, filepath.Join(home, ".claude.json"))
case "copilot":
paths = append(paths, filepath.Join(home, ".copilot", "mcp-config.json"))
}
if workingDir != "" {
paths = append(paths, filepath.Join(workingDir, ".mcp.json"))
}
// Merge servers from all config files (later files override earlier ones).
merged := map[string]json.RawMessage{}
for _, p := range paths {
data, err := os.ReadFile(p)
if err != nil {
continue
}
var cfg struct {
MCPServers map[string]json.RawMessage `json:"mcpServers"`
}
if json.Unmarshal(data, &cfg) != nil || cfg.MCPServers == nil {
continue
}
for name, server := range cfg.MCPServers {
merged[name] = server
}
}
if len(merged) == 0 {
return []any{}
}
// Convert the name→config map into the ACP array format.
// ACP expects a different schema than the native config files:
// - headers: array of [key, value] pairs (not an object)
// - env: array of [key, value] pairs (not an object)
// - type "local" → "stdio"
// - Extra fields (source, sourcePath, tools) are stripped.
var servers []any
for name, raw := range merged {
var obj map[string]any
if json.Unmarshal(raw, &obj) != nil {
continue
}
obj["name"] = name
// Normalize type: "local" is an alias for "stdio" in some configs.
if t, ok := obj["type"].(string); ok && t == "local" {
obj["type"] = "stdio"
}
// Convert headers object → array of [key, value] pairs.
if h, ok := obj["headers"].(map[string]any); ok {
pairs := make([]any, 0, len(h))
for k, v := range h {
pairs = append(pairs, []any{k, v})
}
obj["headers"] = pairs
} else if obj["headers"] == nil {
obj["headers"] = []any{}
}
// Convert env object → array of {name, value} objects.
if e, ok := obj["env"].(map[string]any); ok {
entries := make([]any, 0, len(e))
for k, v := range e {
entries = append(entries, map[string]any{"name": k, "value": v})
}
obj["env"] = entries
}
// Ensure required fields for stdio servers.
if t, _ := obj["type"].(string); t == "stdio" {
if obj["args"] == nil {
obj["args"] = []any{}
}
if obj["env"] == nil {
obj["env"] = []any{}
}
}
// Strip fields that are not part of the ACP schema.
delete(obj, "source")
delete(obj, "sourcePath")
delete(obj, "tools")
servers = append(servers, obj)
}
if servers == nil {
return []any{}
}
return servers
}
// LoadClientConfig reads ~/.orbitor/config.json and merges it with built-in
// defaults. Missing or unreadable config silently falls back to defaults.
func LoadClientConfig() ClientConfig {
cfg := defaultClientConfig()
path, err := ClientConfigPath()
if err != nil {
return cfg
}
f, err := os.Open(path)
if err != nil {
return cfg
}
defer f.Close()
// Decode into cfg so only explicitly-set fields overwrite defaults.
_ = json.NewDecoder(f).Decode(&cfg)
if cfg.ServerURL == "" {
cfg.ServerURL = "http://127.0.0.1:8080"
}
if cfg.ListenAddr == "" {
cfg.ListenAddr = "127.0.0.1:8080"
}
if cfg.DefaultBackend == "" {
cfg.DefaultBackend = "claude"
}
return cfg
}