-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbridge.go
More file actions
613 lines (581 loc) · 19.1 KB
/
bridge.go
File metadata and controls
613 lines (581 loc) · 19.1 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/websocket"
"net/http"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"time"
)
func (r *launcherRuntime) handleBridgeRequest(path string, payload json.RawMessage) map[string]any {
started := time.Now()
var payloadMap map[string]any
_ = json.Unmarshal(payload, &payloadMap)
appendDiagnosticLog("bridge.request", map[string]any{"path": path, "payload_keys": mapKeys(payloadMap)})
var result map[string]any
switch path {
case "/backend/status", "/backend/repair":
result = map[string]any{"status": "ok", "message": "后端已连接", "version": version}
case "/settings/get":
result = bridgeSettingsValue(loadSettings())
case "/settings/set":
result = r.setBridgeSettings(payloadMap)
case "/diagnostics/log":
r.logRendererDiagnostic(payload)
result = map[string]any{"status": "ok", "message": "日志已记录"}
case "/user-scripts/list":
result = userScriptInventoryValue()
case "/user-scripts/set-enabled":
config := loadUserScriptConfig()
config.Enabled = boolFromAny(payloadMap["enabled"])
if err := saveUserScriptConfig(config); err != nil {
result = map[string]any{"status": "failed", "message": err.Error()}
} else {
result = userScriptInventoryValue()
}
case "/user-scripts/set-script-enabled":
key := strings.TrimSpace(stringFromAny(payloadMap["key"]))
if key == "" {
result = map[string]any{"status": "failed", "message": "脚本 key 不能为空"}
break
}
config := loadUserScriptConfig()
config.Scripts[key] = boolFromAny(payloadMap["enabled"])
if err := saveUserScriptConfig(config); err != nil {
result = map[string]any{"status": "failed", "message": err.Error()}
} else {
result = userScriptInventoryValue()
}
case "/user-scripts/reload":
bundle := enabledUserScriptBundle()
if strings.TrimSpace(bundle) != "" {
if _, err := r.evaluateOnCodex(bundle, false); err != nil {
result = map[string]any{"status": "failed", "message": err.Error()}
break
}
}
result = userScriptInventoryValue()
case "/devtools/open":
result = r.openDevTools()
case "/manager/open":
if err := openManagerApp(); err != nil {
result = map[string]any{"status": "failed", "message": "打开管理工具失败:" + err.Error()}
} else {
result = map[string]any{"status": "ok", "message": "管理工具已打开"}
}
case "/codex-model-catalog", "/codex-config-model":
result = codexModelCatalogValue()
case "/zed-remote/status":
result = zedRemoteStatusValue()
case "/zed-remote/resolve-host", "/zed-remote/fallback-request", "/zed-remote/open":
result = map[string]any{"status": "failed", "message": "Go 管理器暂未实现 Zed Remote 桥接"}
case "/delete", "/undo", "/archived-thread", "/move-thread-workspace", "/move-thread-projectless", "/export-markdown", "/thread-sort-key", "/thread-sort-keys":
result = handleSessionDataRoute(path, payloadMap)
default:
result = map[string]any{"status": "failed", "message": "Unknown bridge path", "path": path}
appendDiagnosticLog("bridge.unknown_path", map[string]any{"path": path})
}
appendDiagnosticLog("bridge.response", map[string]any{
"path": path,
"elapsed_ms": time.Since(started).Milliseconds(),
"status": stringFromAny(result["status"]),
})
return result
}
func mapKeys(value map[string]any) []string {
keys := make([]string, 0, len(value))
for key := range value {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func bridgeSettingsValue(settings backendSettings) map[string]any {
return map[string]any{
"providerSyncEnabled": settings.ProviderSync,
"enhancementsEnabled": settings.Enhancements,
"launchMode": settings.LaunchMode,
"language": settings.Language,
}
}
func (r *launcherRuntime) setBridgeSettings(payload map[string]any) map[string]any {
settings := loadSettings()
if _, ok := payload["providerSyncEnabled"]; ok {
settings.ProviderSync = boolFromAny(payload["providerSyncEnabled"])
}
if _, ok := payload["enhancementsEnabled"]; ok {
settings.Enhancements = boolFromAny(payload["enhancementsEnabled"])
}
if value := strings.TrimSpace(stringFromAny(payload["launchMode"])); value == "patch" || value == "relay" {
settings.LaunchMode = value
}
if _, ok := payload["language"]; ok {
settings.Language = normalizeLanguage(stringFromAny(payload["language"]))
}
if err := saveSettings(settings); err != nil {
return map[string]any{"status": "failed", "message": err.Error()}
}
r.settings = settings
result := bridgeSettingsValue(settings)
result["status"] = "ok"
return result
}
func enabledUserScriptBundle() string {
config := loadUserScriptConfig()
if !config.Enabled {
return ""
}
var parts []string
inventory := scanUserScripts()
for _, item := range inventory.Scripts {
if !item.Enabled {
continue
}
var dir string
switch item.Source {
case "builtin":
dir = inventory.BuiltinDir
case "user":
dir = inventory.UserDir
default:
continue
}
data, err := os.ReadFile(filepath.Join(dir, item.Name))
if err != nil {
continue
}
parts = append(parts, fmt.Sprintf("\n;(() => {\n%s\n})();\n", string(data)))
}
return strings.Join(parts, "\n")
}
func unsupportedBridgeDataRoute(path string, payload map[string]any) map[string]any {
sessionID := stringFromAny(payload["session_id"])
if path == "/thread-sort-key" {
return map[string]any{"status": "ok", "session_id": sessionID}
}
if path == "/thread-sort-keys" {
return map[string]any{"status": "ok", "sessions": []any{}}
}
return map[string]any{"status": "failed", "session_id": sessionID, "message": "Go 管理器暂未实现该页面数据操作:" + path}
}
func zedRemoteStatusValue() map[string]any {
return map[string]any{
"status": "ok",
"platformSupported": runtime.GOOS == "darwin",
"zedAppFound": fileExists("/Applications/Zed.app"),
"zedCliFound": executableInPath("zed") != "",
}
}
func executableInPath(name string) string {
for _, dir := range filepath.SplitList(os.Getenv("PATH") + string(os.PathListSeparator) + defaultGUIPath) {
if candidate := filepath.Join(dir, name); fileExists(candidate) {
return candidate
}
}
return ""
}
func codexModelCatalogValue() map[string]any {
configPath := filepath.Join(codexHomeDir(), "config.toml")
data, _ := os.ReadFile(configPath)
contents := string(data)
modelProvider := rootKeyString(contents, "model_provider")
model := rootKeyString(contents, "model")
defaultModel := rootKeyString(contents, "default_model")
providerValues := tableValues(contents, "model_providers."+modelProvider)
providerName := unquoteToml(providerValues["name"])
if providerName == "" {
providerName = modelProvider
}
models := uniqueStrings([]string{model, defaultModel, defaultRelayTestModel})
if len(models) == 0 {
models = []string{defaultRelayTestModel}
}
if defaultModel == "" {
defaultModel = firstNonEmpty(model, models[0])
}
return map[string]any{
"status": "ok",
"model": model,
"default_model": defaultModel,
"model_provider": modelProvider,
"provider_name": providerName,
"models": models,
"sources": []any{},
"responses_api": map[string]any{"status": "unknown", "endpoint": "", "message": ""},
}
}
func uniqueStrings(values []string) []string {
seen := map[string]bool{}
var out []string
for _, value := range values {
value = strings.TrimSpace(value)
if value == "" || seen[value] {
continue
}
seen[value] = true
out = append(out, value)
}
return out
}
func firstNonEmpty(values ...string) string {
for _, value := range values {
if strings.TrimSpace(value) != "" {
return strings.TrimSpace(value)
}
}
return ""
}
func (r *launcherRuntime) retryInjection(helperPort uint16) error {
var lastErr error
for attempt := 1; attempt <= 24; attempt++ {
if err := r.inject(helperPort); err != nil {
lastErr = err
appendDiagnosticLog("inject.retry", map[string]any{"attempt": attempt, "debug_port": r.debugPort, "helper_port": helperPort, "error": err.Error()})
time.Sleep(500 * time.Millisecond)
continue
}
appendDiagnosticLog("inject.ok", map[string]any{"debug_port": r.debugPort, "helper_port": helperPort, "attempt": attempt})
return nil
}
if lastErr == nil {
lastErr = errors.New("Codex injection failed")
}
return lastErr
}
func (r *launcherRuntime) inject(helperPort uint16) error {
targetCtx, targetCancel := context.WithTimeout(context.Background(), cdpConnectTimeout)
defer targetCancel()
targets, err := listCDPTargets(targetCtx, r.debugPort)
if err != nil {
return err
}
target, err := pickCDPPageTarget(targets)
if err != nil {
return err
}
if target.WebSocketDebuggerURL == "" {
return errors.New("selected CDP target has no websocket URL")
}
installCtx, installCancel := context.WithTimeout(context.Background(), 12*time.Second)
defer installCancel()
return r.installBridge(installCtx, target.WebSocketDebuggerURL, helperPort)
}
func (r *launcherRuntime) bridgeWatchdog(helperPort uint16) {
ticker := time.NewTicker(launcherCheckInterval)
defer ticker.Stop()
for range ticker.C {
ok, err := r.bridgeHealthy()
if err != nil {
appendDiagnosticLog("bridge.health_error", map[string]any{"error": err.Error()})
}
if ok {
continue
}
appendDiagnosticLog("bridge.reinject_start", map[string]any{"debug_port": r.debugPort, "helper_port": helperPort})
if err := r.retryInjection(helperPort); err != nil {
appendDiagnosticLog("bridge.reinject_failed", map[string]any{"error": err.Error()})
}
}
}
func (r *launcherRuntime) bridgeHealthy() (bool, error) {
result, err := r.evaluateOnCodex(bridgeHealthCheckScript(), true)
if err != nil {
return false, err
}
return cdpResultBool(result), nil
}
func (r *launcherRuntime) openDevTools() map[string]any {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
targets, err := listCDPTargets(ctx, r.debugPort)
if err != nil {
return map[string]any{"status": "failed", "message": err.Error()}
}
target, err := pickCDPPageTarget(targets)
if err != nil {
return map[string]any{"status": "failed", "message": err.Error()}
}
if strings.TrimSpace(target.ID) == "" {
return map[string]any{"status": "failed", "message": "CDP target id 为空"}
}
url := fmt.Sprintf("http://127.0.0.1:%d/devtools/inspector.html?ws=127.0.0.1:%d/devtools/page/%s", r.debugPort, r.debugPort, target.ID)
if err := openURL(url); err != nil {
return map[string]any{"status": "failed", "message": "打开 DevTools 失败:" + err.Error(), "url": url}
}
return map[string]any{"status": "ok", "message": "DevTools 已打开", "url": url, "target_id": target.ID}
}
func (r *launcherRuntime) evaluateOnCodex(script string, awaitPromise bool) (json.RawMessage, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
targets, err := listCDPTargets(ctx, r.debugPort)
if err != nil {
return nil, err
}
target, err := pickCDPPageTarget(targets)
if err != nil {
return nil, err
}
conn, _, err := websocket.DefaultDialer.DialContext(ctx, target.WebSocketDebuggerURL, nil)
if err != nil {
return nil, err
}
defer conn.Close()
session := newCDPSession(conn, nil)
return session.send(ctx, "Runtime.evaluate", runtimeEvaluateParams(script, awaitPromise))
}
func listCDPTargets(ctx context.Context, debugPort uint16) ([]cdpTarget, error) {
request, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d/json", debugPort), nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("CDP target list HTTP %d", resp.StatusCode)
}
var targets []cdpTarget
if err := json.NewDecoder(resp.Body).Decode(&targets); err != nil {
return nil, err
}
return targets, nil
}
func pickCDPPageTarget(targets []cdpTarget) (cdpTarget, error) {
var fallback *cdpTarget
for i := range targets {
target := targets[i]
if target.WebSocketDebuggerURL == "" {
continue
}
if isCodexCDPPageTarget(target) {
return target, nil
}
if fallback == nil {
fallback = &targets[i]
}
}
for i := range targets {
target := targets[i]
if target.WebSocketDebuggerURL != "" && target.Type == "page" {
return target, nil
}
}
if fallback != nil {
return *fallback, nil
}
return cdpTarget{}, errors.New("未找到可注入的 Codex CDP 页面 target")
}
func isCodexCDPPageTarget(target cdpTarget) bool {
if target.Type != "page" || target.WebSocketDebuggerURL == "" {
return false
}
return strings.HasPrefix(target.URL, "app://-/") || strings.EqualFold(target.Title, "Codex")
}
func (r *launcherRuntime) installBridge(ctx context.Context, websocketURL string, helperPort uint16) error {
conn, _, err := websocket.DefaultDialer.DialContext(ctx, websocketURL, nil)
if err != nil {
return err
}
handler := func(path string, payload json.RawMessage) map[string]any {
return r.handleBridgeRequest(path, payload)
}
session := newCDPSession(conn, handler)
if _, err := session.send(ctx, "Runtime.enable", map[string]any{}); err != nil {
_ = conn.Close()
return err
}
_, _ = session.send(ctx, "Runtime.removeBinding", map[string]any{"name": bridgeBindingName})
if _, err := session.send(ctx, "Runtime.addBinding", map[string]any{"name": bridgeBindingName}); err != nil {
_ = conn.Close()
return err
}
bridge := bridgeScript(bridgeBindingName)
if _, err := session.send(ctx, "Page.addScriptToEvaluateOnNewDocument", map[string]any{"source": bridge}); err != nil {
_ = conn.Close()
return err
}
if _, err := session.send(ctx, "Runtime.evaluate", runtimeEvaluateParams(bridge, false)); err != nil {
_ = conn.Close()
return err
}
scripts := []string{injectionScript(helperPort)}
if bundle := enabledUserScriptBundle(); strings.TrimSpace(bundle) != "" {
scripts = append(scripts, bundle)
}
for _, script := range scripts {
if _, err := session.send(ctx, "Page.addScriptToEvaluateOnNewDocument", map[string]any{"source": script}); err != nil {
_ = conn.Close()
return err
}
if _, err := session.send(ctx, "Runtime.evaluate", runtimeEvaluateParams(script, false)); err != nil {
_ = conn.Close()
return err
}
}
return nil
}
func runtimeEvaluateParams(script string, awaitPromise bool) map[string]any {
return map[string]any{"expression": script, "awaitPromise": awaitPromise, "allowUnsafeEvalBlockedByCSP": true}
}
func injectionScript(helperPort uint16) string {
helperURL := fmt.Sprintf("http://127.0.0.1:%d", helperPort)
helperJSON, _ := json.Marshal(helperURL)
versionJSON, _ := json.Marshal(version)
buildJSON, _ := json.Marshal("go-20260524-1")
return fmt.Sprintf("window.__CODEX_SESSION_DELETE_HELPER__ = %s;\nwindow.__CODEX_PLUS_VERSION__ = %s;\nwindow.__CODEX_PLUS_BUILD__ = %s;\n%s", helperJSON, versionJSON, buildJSON, rendererInjectScript)
}
func bridgeScript(bindingName string) string {
return fmt.Sprintf(`
(() => {
window.__codexSessionDeleteCallbacks = new Map();
window.__codexSessionDeleteSeq = 0;
window.__codexSessionDeleteResolve = (id, result) => {
const callback = window.__codexSessionDeleteCallbacks.get(id);
if (!callback) return;
window.__codexSessionDeleteCallbacks.delete(id);
callback.resolve(result);
};
window.__codexSessionDeleteReject = (id, message) => {
const callback = window.__codexSessionDeleteCallbacks.get(id);
if (!callback) return;
window.__codexSessionDeleteCallbacks.delete(id);
callback.resolve({ status: "failed", message });
};
window.__codexSessionDeleteBridge = (path, payload) => new Promise((resolve) => {
const id = String(++window.__codexSessionDeleteSeq);
window.__codexSessionDeleteCallbacks.set(id, { resolve });
window.%s(JSON.stringify({ id, path, payload }));
});
})();
`, bindingName)
}
func bridgeHealthCheckScript() string {
return `
(() => {
const bridge = window.__codexSessionDeleteBridge;
if (typeof bridge !== "function") return false;
try {
return Promise.race([
Promise.resolve(bridge("/backend/status", {})).then((result) => !!result && result.status === "ok"),
new Promise((resolve) => setTimeout(() => resolve(false), 2000)),
]);
} catch (error) {
return false;
}
})()
`
}
func cdpResultBool(result json.RawMessage) bool {
var envelope struct {
Result struct {
Type string `json:"type"`
Value bool `json:"value"`
} `json:"result"`
}
return json.Unmarshal(result, &envelope) == nil && envelope.Result.Value
}
func newCDPSession(conn *websocket.Conn, handler func(string, json.RawMessage) map[string]any) *cdpSession {
session := &cdpSession{
conn: conn,
handler: handler,
nextID: 1,
pending: map[int64]chan cdpResponse{},
}
go session.readLoop()
return session
}
func (s *cdpSession) send(ctx context.Context, method string, params any) (json.RawMessage, error) {
s.mu.Lock()
id := s.nextID
s.nextID++
responseCh := make(chan cdpResponse, 1)
s.pending[id] = responseCh
s.mu.Unlock()
payload := map[string]any{"id": id, "method": method, "params": params}
s.writeMu.Lock()
if err := s.conn.WriteJSON(payload); err != nil {
s.writeMu.Unlock()
s.mu.Lock()
delete(s.pending, id)
s.mu.Unlock()
return nil, err
}
s.writeMu.Unlock()
timer := time.NewTimer(cdpCommandTimeout)
defer timer.Stop()
select {
case response := <-responseCh:
if response.Error != nil {
return nil, fmt.Errorf("CDP command %s failed: %s", method, response.Error.Message)
}
return response.Result, nil
case <-ctx.Done():
s.mu.Lock()
delete(s.pending, id)
s.mu.Unlock()
return nil, ctx.Err()
case <-timer.C:
s.mu.Lock()
delete(s.pending, id)
s.mu.Unlock()
return nil, fmt.Errorf("timed out waiting for CDP command %s", method)
}
}
func (s *cdpSession) readLoop() {
defer s.conn.Close()
for {
var response cdpResponse
if err := s.conn.ReadJSON(&response); err != nil {
return
}
if response.ID != 0 {
s.mu.Lock()
if ch, ok := s.pending[response.ID]; ok {
delete(s.pending, response.ID)
s.mu.Unlock()
ch <- response
} else {
s.mu.Unlock()
}
continue
}
if response.Method == "Runtime.bindingCalled" && s.handler != nil {
go s.handleBinding(response.Params)
continue
}
}
}
func (s *cdpSession) handleBinding(params json.RawMessage) {
var raw struct {
Payload string `json:"payload"`
}
if err := json.Unmarshal(params, &raw); err != nil {
appendDiagnosticLog("bridge.payload_parse_failed", map[string]any{"error": err.Error()})
return
}
var payload bridgePayload
if err := json.Unmarshal([]byte(raw.Payload), &payload); err != nil {
appendDiagnosticLog("bridge.payload_parse_failed", map[string]any{"error": err.Error()})
return
}
result := s.handler(payload.Path, payload.Payload)
expr := bridgeResolveExpression(payload.ID, result)
ctx, cancel := context.WithTimeout(context.Background(), cdpCommandTimeout)
defer cancel()
if _, err := s.send(ctx, "Runtime.evaluate", runtimeEvaluateParams(expr, false)); err != nil {
appendDiagnosticLog("bridge.resolve_failed", map[string]any{"request_id": payload.ID, "error": err.Error()})
}
}
func bridgeResolveExpression(requestID string, result map[string]any) string {
id, _ := json.Marshal(requestID)
value, _ := json.Marshal(result)
return fmt.Sprintf("window.__codexSessionDeleteResolve(%s, %s)", id, value)
}