Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
325 lines (314 loc) · 9.26 KB
/
Copy pathmain.go
File metadata and controls
325 lines (314 loc) · 9.26 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"context"
_ "embed"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"syscall"
"time"
)
//go:embed config.example.yaml
var example []byte
var version = "dev"
func main() {
if err := run(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, "codex-bridge:", err)
os.Exit(1)
}
}
func run(args []string) error {
dir, configDirErr := os.UserConfigDir()
fs := flag.NewFlagSet("codex-bridge", flag.ContinueOnError)
path := fs.String("config", filepath.Join(dir, "codex-bridge", "config.yaml"), "YAML config file")
force := fs.Bool("force", false, "with sync, restart the shared Codex server immediately (may interrupt active work)")
showVersion := fs.Bool("version", false, "Print the build version")
fs.Usage = func() {
fmt.Fprintln(fs.Output(), "Usage: codex-bridge [--config FILE] init|edit|sync|unsync|start|stop|restart|update|status|serve\n\ninit Create an example config\nedit Create config if missing, then open $VISUAL / $EDITOR or a detected editor\nsync Write Codex model settings and show how to reload them\nunsync Restore previous Codex provider settings\nstart Start proxy in background\nstop Stop proxy gracefully\nrestart Stop proxy gracefully, then start it with the current config\nupdate Install the latest release and restart the proxy\nstatus Show proxy health, model count, and Codex sync status\nserve Run in foreground (Ctrl-C stops)")
fs.PrintDefaults()
}
err := fs.Parse(args)
if err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return err
}
if *showVersion {
fmt.Println("codex-bridge", version)
return nil
}
if fs.NArg() < 1 {
fs.Usage()
return fmt.Errorf("expected one command")
}
command := fs.Arg(0)
if err = fs.Parse(fs.Args()[1:]); err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return err
}
if *showVersion {
fmt.Println("codex-bridge", version)
return nil
}
if fs.NArg() != 0 {
return fmt.Errorf("expected one command")
}
if *force && command != "sync" {
return fmt.Errorf("--force is only supported with sync")
}
if configDirErr != nil {
return configDirErr
}
*path, err = filepath.Abs(*path)
if err != nil {
return err
}
if command == "init" {
return initConfig(*path)
}
if command == "edit" {
if _, err := os.Lstat(*path); os.IsNotExist(err) {
if err := initConfig(*path); err != nil && !os.IsExist(err) {
return err
}
} else if err != nil {
return err
}
cmd, err := editorCommand(*path)
if err == nil {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
}
if err != nil {
return fmt.Errorf("editor failed: %w\nConfig file: %s\nEdit this file manually, or set VISUAL/EDITOR to an installed editor such as micro", err, *path)
}
return nil
}
if command == "unsync" {
return withLock(filepath.Join(codexDir(), ".codex-bridge.lock"), unsyncCodex)
}
c, err := loadConfig(*path)
if command == "status" {
return printStatus(*path, c, err)
}
if command == "stop" {
if running, e := os.ReadFile(*path + ".running"); e == nil && validListen(string(running)) {
c.Listen, err = string(running), nil
}
}
if err != nil {
return err
}
if command == "sync" {
return withLock(filepath.Join(codexDir(), ".codex-bridge.lock"), func() error { return syncCodex(*path, c, *force) })
}
if command == "update" {
for _, m := range c.Models {
if _, err := m.key(); err != nil {
return err
}
}
return updateBridge(*path)
}
token, err := localToken(*path)
if err != nil {
return err
}
switch command {
case "serve":
return serve(*path, c, token)
case "stop":
return stop(*path, c, token)
case "start", "restart":
return withLock(*path+".lock", func() error {
if command == "restart" {
for _, m := range c.Models {
if _, err := m.key(); err != nil {
return err
}
}
running := c
if address, err := os.ReadFile(*path + ".running"); err == nil && validListen(string(address)) {
running.Listen = string(address)
}
if control(running, token, *path, "GET", "/health") == nil {
if err := stop(*path, running, token); err != nil {
return err
}
}
}
return start(*path, c, token)
})
default:
fs.Usage()
return fmt.Errorf("unknown command %q", command)
}
}
func printStatus(path string, c Config, configErr error) error {
fmt.Println("Version:", version)
running := c
if address, err := os.ReadFile(path + ".running"); err == nil && validListen(string(address)) {
running.Listen = string(address)
}
token, err := os.ReadFile(path + ".token")
if err == nil {
err = control(running, string(token), path, "GET", "/health")
}
if err == nil {
fmt.Println("Running at http://" + running.Listen)
} else {
fmt.Println("Stopped or unreachable at http://" + running.Listen)
}
fmt.Println("Config:", path)
if configErr != nil {
fmt.Println("Models: unavailable")
fmt.Println("Codex sync: could not verify")
return configErr
}
fmt.Printf("Models: %d external\n", len(c.Models))
reason, verifyErr := codexSyncStatus(path, c)
if verifyErr != nil {
fmt.Println("Codex sync: could not verify")
fmt.Println("Reason:", verifyErr)
} else if reason != "" {
fmt.Println("Codex sync: unsynced")
fmt.Println("Reason:", reason)
} else {
fmt.Println("Codex sync: synced")
}
fmt.Println("Codex config:", filepath.Join(codexDir(), "config.toml"))
if err != nil {
return fmt.Errorf("proxy is not running or unreachable: %w", err)
}
return nil
}
func editorCommand(path string) (*exec.Cmd, error) {
if os.Getenv("VISUAL") != "" || os.Getenv("EDITOR") != "" {
return exec.Command("sh", "-c", `exec ${VISUAL:-$EDITOR} "$1"`, "sh", path), nil
}
for _, name := range []string{"micro", "nano", "mcedit", "joe", "nvim", "vim", "editor", "vi"} {
if executable, err := exec.LookPath(name); err == nil {
return exec.Command(executable, path), nil
}
}
return nil, fmt.Errorf("no terminal editor found on PATH")
}
func withLock(path string, fn func() error) error {
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return err
}
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return err
}
defer f.Close()
if err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
return err
}
defer syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
return fn()
}
func control(c Config, token, path, method, endpoint string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, method, "http://"+c.Listen+endpoint, nil)
if err != nil {
return err
}
req.Header.Set("X-Codex-Bridge-Token", token)
client := http.Client{Transport: &http.Transport{Proxy: nil}, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
defer client.CloseIdleConnections()
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
return fmt.Errorf("bridge HTTP %d", resp.StatusCode)
}
if endpoint == "/health" {
var body object
if err = json.NewDecoder(io.LimitReader(resp.Body, 4096)).Decode(&body); err != nil {
return err
}
if body["service"] != "codex-bridge" || body["config"] != path {
return fmt.Errorf("port belongs to another process or config")
}
}
return nil
}
func stop(path string, c Config, token string) error {
if err := control(c, token, path, "POST", "/stop"); err != nil {
return err
}
for i := 0; i < 120; i++ {
if control(c, token, path, "GET", "/health") != nil {
// serve removes this file only after in-flight requests finish.
if _, err := os.Stat(path + ".running"); os.IsNotExist(err) {
fmt.Println("Stopped")
return nil
}
}
time.Sleep(100 * time.Millisecond)
}
return fmt.Errorf("proxy has not stopped yet")
}
func start(path string, c Config, token string) error {
if running, err := os.ReadFile(path + ".running"); err == nil && validListen(string(running)) && string(running) != c.Listen {
old := Config{Listen: string(running)}
if control(old, token, path, "GET", "/health") == nil {
return fmt.Errorf("proxy is running at %s; run restart to change the listen address", old.Listen)
}
}
if control(c, token, path, "GET", "/health") == nil {
fmt.Println("Already running at http://" + c.Listen)
return nil
}
// Fail before forking when the configured keys are unavailable.
for _, m := range c.Models {
if _, err := m.key(); err != nil {
return err
}
}
executable, err := os.Executable()
if err != nil {
return err
}
f, err := os.OpenFile(path+".log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer f.Close()
cmd := exec.Command(executable, "--config", path, "serve")
cmd.Stdout = f
cmd.Stderr = f
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
if err = cmd.Start(); err != nil {
return err
}
done := make(chan error, 1)
go func() { done <- cmd.Wait() }()
for i := 0; i < 150; i++ {
if control(c, token, path, "GET", "/health") == nil {
fmt.Printf("Started at http://%s; log: %s.log\n", c.Listen, path)
return nil
}
select {
case err = <-done:
return fmt.Errorf("proxy exited (%v); see %s.log", err, path)
case <-time.After(100 * time.Millisecond):
}
}
return fmt.Errorf("proxy is still starting; inspect %s.log and run status", path)
}