-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathzed_remote.go
More file actions
1027 lines (965 loc) · 30.2 KB
/
Copy pathzed_remote.go
File metadata and controls
1027 lines (965 loc) · 30.2 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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"database/sql"
"encoding/json"
"fmt"
"hash/fnv"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"time"
)
type zedRemoteError struct {
message string
}
func (e zedRemoteError) Error() string {
return e.message
}
type sshTarget struct {
User string
Host string
Port *uint16
}
type zedRemoteProject struct {
ID string `json:"id"`
Label string `json:"label"`
HostID string `json:"hostId"`
SSH sshTarget `json:"ssh"`
Path string `json:"path"`
URL string `json:"url"`
Source string `json:"source"`
LastOpenedAtMS *int64 `json:"lastOpenedAtMs,omitempty"`
IsCurrent bool `json:"isCurrent"`
}
type zedRemoteProjectRegistry struct {
Projects []zedRemoteProject `json:"projects"`
}
func zedRemoteStatusValue() map[string]any {
appPath := findZedAppPath()
cliPath := executableInPath("zed")
platformSupported := runtime.GOOS == "darwin" || runtime.GOOS == "windows" || runtime.GOOS == "linux"
status := "ok"
if !platformSupported {
status = "failed"
}
return map[string]any{
"status": status,
"platformSupported": platformSupported,
"zedAppFound": appPath != "",
"zedCliFound": cliPath != "",
"zedAppPath": appPath,
"zedCliPath": cliPath,
}
}
func findZedAppPath() string {
candidates := []string{
"/Applications/Zed.app",
"/Applications/Zed Preview.app",
"/Applications/Zed Nightly.app",
}
if home, err := os.UserHomeDir(); err == nil && home != "" {
candidates = append(candidates,
filepath.Join(home, "Applications", "Zed.app"),
filepath.Join(home, "Applications", "Zed Preview.app"),
filepath.Join(home, "Applications", "Zed Nightly.app"),
)
}
for _, candidate := range candidates {
if fileExists(candidate) {
return candidate
}
}
return ""
}
func splitSSHAuthority(value string) (string, string, *uint16, error) {
authority := strings.TrimSpace(value)
if authority == "" {
return "", "", nil, nil
}
user := ""
if index := strings.LastIndex(authority, "@"); index >= 0 {
user = strings.TrimSpace(authority[:index])
authority = authority[index+1:]
}
if strings.HasPrefix(authority, "[") {
if closeIndex := strings.Index(authority, "]"); closeIndex >= 0 {
host := strings.TrimSpace(authority[:closeIndex+1])
suffix := authority[closeIndex+1:]
var port *uint16
if strings.HasPrefix(suffix, ":") {
parsed, err := parseSSHPort(strings.TrimPrefix(suffix, ":"))
if err != nil {
return "", "", nil, err
}
port = parsed
}
return user, host, port, nil
}
return user, strings.TrimSpace(authority), nil, nil
}
if strings.Count(authority, ":") == 1 {
host, rawPort, _ := strings.Cut(authority, ":")
if rawPort != "" && allASCII(rawPort, func(ch byte) bool { return ch >= '0' && ch <= '9' }) {
port, err := parseSSHPort(rawPort)
if err != nil {
return "", "", nil, err
}
return user, strings.TrimSpace(host), port, nil
}
}
return user, strings.TrimSpace(authority), nil, nil
}
func parseSSHPort(value string) (*uint16, error) {
value = strings.TrimSpace(value)
if value == "" {
return nil, nil
}
parsed, err := strconv.ParseUint(value, 10, 16)
if err != nil || parsed == 0 {
return nil, zedRemoteError{"Invalid SSH port"}
}
port := uint16(parsed)
return &port, nil
}
func parseSSHPortAny(value any) (*uint16, error) {
switch typed := value.(type) {
case nil:
return nil, nil
case float64:
if typed <= 0 || typed > 65535 || typed != float64(uint16(typed)) {
return nil, zedRemoteError{"Invalid SSH port"}
}
port := uint16(typed)
return &port, nil
case int:
if typed <= 0 || typed > 65535 {
return nil, zedRemoteError{"Invalid SSH port"}
}
port := uint16(typed)
return &port, nil
case string:
return parseSSHPort(typed)
default:
return nil, zedRemoteError{"Invalid SSH port"}
}
}
func validateSSHHost(host string) (string, error) {
host = strings.TrimSpace(host)
if host == "" {
return "", zedRemoteError{"Cannot determine remote SSH host for this file"}
}
for _, ch := range host {
if ch <= 0x20 || strings.ContainsRune("/?#@", ch) {
return "", zedRemoteError{"Invalid SSH host"}
}
}
if strings.HasPrefix(host, "[") || strings.HasSuffix(host, "]") {
if !(strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]")) {
return "", zedRemoteError{"Invalid SSH host"}
}
if net.ParseIP(strings.TrimSuffix(strings.TrimPrefix(host, "["), "]")) == nil {
return "", zedRemoteError{"Invalid SSH host"}
}
return host, nil
}
if strings.ContainsAny(host, "[]") {
return "", zedRemoteError{"Invalid SSH host"}
}
return host, nil
}
func sshTargetFromPayload(payload map[string]any) (sshTarget, error) {
ssh, _ := payload["ssh"].(map[string]any)
rawHost := firstString(ssh["host"], ssh["hostname"], ssh["hostName"])
authorityUser, authorityHost, authorityPort, err := splitSSHAuthority(rawHost)
if err != nil {
return sshTarget{}, err
}
user := firstString(ssh["user"], ssh["username"], authorityUser)
host, err := validateSSHHost(authorityHost)
if err != nil {
return sshTarget{}, err
}
port := authorityPort
if rawPort, ok := ssh["port"]; ok {
if text, ok := rawPort.(string); !ok || strings.TrimSpace(text) != "" {
port, err = parseSSHPortAny(rawPort)
if err != nil {
return sshTarget{}, err
}
}
}
return sshTarget{User: user, Host: host, Port: port}, nil
}
func encodeRemotePath(path string) (string, error) {
if path == "" {
return "", zedRemoteError{"Remote path is required"}
}
if !strings.HasPrefix(path, "/") {
return "", zedRemoteError{"Remote path must be absolute"}
}
segments := strings.Split(path, "/")
for index, segment := range segments {
segments[index] = percentEncodeSegment(segment)
}
return strings.Join(segments, "/"), nil
}
func buildZedRemoteURL(target sshTarget, path string) (string, error) {
host, err := validateSSHHost(target.Host)
if err != nil {
return "", err
}
encodedPath, err := encodeRemotePath(path)
if err != nil {
return "", err
}
userPrefix := ""
if strings.TrimSpace(target.User) != "" {
userPrefix = percentEncodeSegment(strings.TrimSpace(target.User)) + "@"
}
portSuffix := ""
if target.Port != nil {
portSuffix = ":" + strconv.Itoa(int(*target.Port))
}
return "ssh://" + userPrefix + host + portSuffix + encodedPath, nil
}
func percentEncodeSegment(segment string) string {
var out strings.Builder
for _, b := range []byte(segment) {
ch := rune(b)
if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '-' || ch == '.' || ch == '_' || ch == '~' {
out.WriteByte(b)
} else {
out.WriteString(fmt.Sprintf("%%%02X", b))
}
}
return out.String()
}
func zedCLIArgsForStrategy(strategy, rawURL string) []string {
args := []string{}
switch normalizeZedOpenStrategy(strategy) {
case "addToFocusedWorkspace":
args = append(args, "-a")
case "reuseWindow":
args = append(args, "-r")
case "newWindow":
args = append(args, "-n")
case "default":
}
return append(args, rawURL)
}
func launchZedURL(rawURL string) error {
return launchZedURLWithStrategy(rawURL, "default")
}
func launchZedURLWithStrategy(rawURL, strategy string) error {
if cliPath := executableInPath("zed"); cliPath != "" {
cmd := exec.Command(cliPath, zedCLIArgsForStrategy(strategy, rawURL)...)
hideSubprocessWindow(cmd)
return cmd.Start()
}
if runtime.GOOS == "darwin" {
if appPath := findZedAppPath(); appPath != "" {
cmd := exec.Command("open", "-a", appPath, rawURL)
hideSubprocessWindow(cmd)
return cmd.Start()
}
}
return zedRemoteError{"Zed CLI is not installed or not available on PATH"}
}
func targetFromManagedRemoteConnection(connection map[string]any) (sshTarget, error) {
sshHost := firstString(connection["sshHost"], connection["hostname"])
sshAlias := firstString(connection["sshAlias"], connection["alias"])
authorityUser, authorityHost, authorityPort, err := splitSSHAuthority(sshHost)
if err != nil {
return sshTarget{}, err
}
host := firstString(authorityHost, sshAlias)
user := firstString(connection["sshUser"], connection["user"], authorityUser)
port := authorityPort
if rawPort, ok := connection["sshPort"]; ok {
if text, ok := rawPort.(string); !ok || strings.TrimSpace(text) != "" {
port, err = parseSSHPortAny(rawPort)
if err != nil {
return sshTarget{}, err
}
}
}
host, err = validateSSHHost(host)
if err != nil {
return sshTarget{}, err
}
return sshTarget{User: user, Host: host, Port: port}, nil
}
func resolveSSHTargetFromGlobalState(state map[string]any, hostID string) (sshTarget, error) {
hostID = strings.TrimSpace(hostID)
if hostID == "" {
return sshTarget{}, zedRemoteError{"Remote host id is required"}
}
connections, _ := state["codex-managed-remote-connections"].([]any)
for _, item := range connections {
connection, _ := item.(map[string]any)
if stringFromAny(connection["hostId"]) != hostID {
continue
}
return targetFromManagedRemoteConnection(connection)
}
return sshTarget{}, zedRemoteError{"Cannot resolve remote SSH host for this file"}
}
func resolveSSHTargetForHostID(hostID string) (sshTarget, error) {
data, err := os.ReadFile(codexGlobalStatePath(codexHomeDir()))
if err != nil {
return sshTarget{}, fmt.Errorf("Cannot read Codex remote connection state: %w", err)
}
var state map[string]any
if err := json.Unmarshal(data, &state); err != nil {
return sshTarget{}, fmt.Errorf("Cannot parse Codex remote connection state: %w", err)
}
return resolveSSHTargetFromGlobalState(state, hostID)
}
func resolveSSHTargetResponse(payload map[string]any) map[string]any {
target, err := resolveSSHTargetForHostID(stringFromAny(payload["hostId"]))
if err != nil {
return map[string]any{"status": "failed", "message": err.Error()}
}
return map[string]any{
"status": "ok",
"ssh": map[string]any{
"user": target.User,
"host": target.Host,
"port": target.Port,
},
}
}
func zedOpenRemote(payload map[string]any) map[string]any {
target, err := sshTargetFromPayload(payload)
if err == nil {
var url string
url, err = buildZedRemoteURL(target, stringFromAny(payload["path"]))
if err == nil {
strategy := normalizeZedOpenStrategy(firstString(payload["strategy"], loadSettings().ZedRemoteOpenStrategy))
err = launchZedURLWithStrategy(url, strategy)
if err == nil {
settings := loadSettings()
if settings.ZedRemoteProjectRegistryEnabled && payload["remember"] != false {
if _, rememberErr := rememberZedRemoteProject(payload, &target, url); rememberErr != nil {
appendDiagnosticLog("zed_remote.remember_failed", map[string]any{"error": rememberErr.Error()})
}
}
return map[string]any{"status": "ok", "url": url, "strategy": strategy}
}
}
}
return map[string]any{"status": "failed", "message": err.Error()}
}
func zedRemoteProjectsResponse(payload map[string]any) map[string]any {
settings := loadSettings()
if !settings.ZedRemoteProjectRegistryEnabled {
return map[string]any{"status": "ok", "projects": []zedRemoteProject{}, "message": "Zed 项目记录已关闭"}
}
projects, err := listZedRemoteProjects(payload, zedRegistryPath(), filepath.Join(codexHomeDir(), "state_5.sqlite"))
if err != nil {
return map[string]any{"status": "failed", "message": err.Error()}
}
return map[string]any{"status": "ok", "projects": projects}
}
func zedRememberRemoteProjectResponse(payload map[string]any) map[string]any {
project, err := rememberZedRemoteProject(payload, nil, "")
if err != nil {
return map[string]any{"status": "failed", "message": err.Error()}
}
return map[string]any{"status": "ok", "project": project}
}
func zedForgetRemoteProjectResponse(payload map[string]any) map[string]any {
removed, err := forgetZedRemoteProject(payload)
if err != nil {
return map[string]any{"status": "failed", "message": err.Error()}
}
return map[string]any{"status": "ok", "removed": removed}
}
func (s *server) zedRemoteProjects(args map[string]any) commandResult {
result := zedRemoteProjectsResponse(mapArgOrSelf(args))
status := stringFromAny(result["status"])
if status == "ok" {
result["message"] = "Zed 远程项目已读取。"
return commandResult(result)
}
if result["message"] == nil {
result["message"] = "读取 Zed 远程项目失败。"
}
return commandResult(result)
}
func (s *server) zedRemoteOpen(args map[string]any) commandResult {
result := zedOpenRemote(mapArgOrSelf(args))
if stringFromAny(result["status"]) == "ok" {
result["message"] = "已请求 Zed 打开远程项目。"
return commandResult(result)
}
if result["message"] == nil {
result["message"] = "打开 Zed 远程项目失败。"
}
return commandResult(result)
}
func (s *server) zedRemoteForgetProject(args map[string]any) commandResult {
result := zedForgetRemoteProjectResponse(mapArgOrSelf(args))
if stringFromAny(result["status"]) == "ok" {
projects := zedRemoteProjectsResponse(map[string]any{})
projects["removed"] = result["removed"]
projects["message"] = "Zed 最近项目记录已移除。"
return commandResult(projects)
}
if result["message"] == nil {
result["message"] = "移除 Zed 最近项目失败。"
}
return commandResult(result)
}
func mapArgOrSelf(args map[string]any) map[string]any {
if request := mapArg(args, "request"); len(request) > 0 {
return request
}
return args
}
func zedFallbackRequestResponse(payload map[string]any) map[string]any {
data, err := os.ReadFile(codexGlobalStatePath(codexHomeDir()))
if err != nil {
return map[string]any{"status": "failed", "message": "Cannot read Codex remote connection state: " + err.Error()}
}
var state map[string]any
if err := json.Unmarshal(data, &state); err != nil {
return map[string]any{"status": "failed", "message": "Cannot parse Codex remote connection state: " + err.Error()}
}
request, err := fallbackOpenRequestFromGlobalStateWithContext(
state,
stringFromAny(payload["hostId"]),
stringFromAny(payload["threadId"]),
stringFromAny(payload["workspaceRoot"]),
stringFromAny(payload["remoteProjectId"]),
)
if err != nil {
return map[string]any{"status": "failed", "message": err.Error()}
}
request["status"] = "ok"
return request
}
func fallbackOpenRequestFromGlobalStateWithContext(state map[string]any, hostID, threadID, workspaceRoot, remoteProjectID string) (map[string]any, error) {
hint := threadWorkspaceHint(state, threadID)
selectedHostID := firstString(hostID, hostIDFromHint(hint), state["selected-remote-host-id"])
hintedPath := firstString(workspaceRoot, workspacePathFromHint(hint), workspaceRootFromSQLite(threadID, ""))
if strings.HasPrefix(hintedPath, "/") {
resolvedHostID := hostIDForRemotePath(state, selectedHostID, hintedPath)
return openRequestForRemotePath(state, resolvedHostID, hintedPath)
}
requestedProjectID := strings.TrimSpace(remoteProjectID)
if requestedProjectID != "" {
if strings.HasPrefix(requestedProjectID, "/") {
return openRequestForRemotePath(state, selectedHostID, requestedProjectID)
}
for _, project := range orderedRemoteProjectsFromGlobalState(state) {
if stringFromAny(project["id"]) != requestedProjectID {
continue
}
projectHostID := stringFromAny(project["hostId"])
if selectedHostID != "" && projectHostID != selectedHostID {
continue
}
return openRequestForRemotePath(state, projectHostID, stringFromAny(project["remotePath"]))
}
}
for _, project := range orderedRemoteProjectsFromGlobalState(state) {
projectHostID := stringFromAny(project["hostId"])
remotePath := stringFromAny(project["remotePath"])
if (selectedHostID == "" || projectHostID == selectedHostID) && strings.HasPrefix(remotePath, "/") {
return openRequestForRemotePath(state, firstString(selectedHostID, projectHostID), remotePath)
}
}
return nil, zedRemoteError{"Cannot determine remote workspace or file for Zed"}
}
func workspaceRootFromSQLite(threadID, statePath string) string {
threadID = normalizeThreadID(threadID)
if threadID == "" {
return ""
}
if statePath == "" {
statePath = filepath.Join(codexHomeDir(), "state_5.sqlite")
}
if !fileExists(statePath) {
return ""
}
db, err := sql.Open("sqlite", statePath)
if err != nil {
return ""
}
defer db.Close()
var cwd string
if err := db.QueryRow("SELECT cwd FROM threads WHERE id = ?1 LIMIT 1", threadID).Scan(&cwd); err != nil {
return ""
}
return strings.TrimSpace(cwd)
}
func orderedRemoteProjectsFromGlobalState(state map[string]any) []map[string]any {
rawProjects, _ := state["remote-projects"].([]any)
var projects []map[string]any
for _, item := range rawProjects {
project, _ := item.(map[string]any)
if project != nil {
projects = append(projects, project)
}
}
rawOrder, _ := state["project-order"].([]any)
var ordered []map[string]any
used := map[string]bool{}
for _, item := range rawOrder {
id := stringFromAny(item)
for _, project := range projects {
if stringFromAny(project["id"]) == id {
ordered = append(ordered, project)
used[id] = true
break
}
}
}
for _, project := range projects {
id := stringFromAny(project["id"])
if !used[id] {
ordered = append(ordered, project)
}
}
return ordered
}
func workspacePathFromHint(hint any) string {
switch typed := hint.(type) {
case string:
return strings.TrimSpace(typed)
case map[string]any:
return firstString(typed["remotePath"], typed["remoteWorkspaceRoot"], typed["workspaceRoot"], typed["path"], typed["cwd"])
default:
return ""
}
}
func normalizeThreadID(threadID string) string {
return strings.TrimPrefix(strings.TrimSpace(threadID), "local:")
}
func hostIDFromHint(hint any) string {
object, _ := hint.(map[string]any)
if object == nil {
return ""
}
return firstString(object["hostId"], object["remoteHostId"])
}
func threadWorkspaceHint(state map[string]any, threadID string) any {
if strings.TrimSpace(threadID) == "" {
return nil
}
hints, _ := state["thread-workspace-root-hints"].(map[string]any)
if hints == nil {
return nil
}
bare := normalizeThreadID(threadID)
for _, key := range []string{threadID, bare, "local:" + bare} {
if value, ok := hints[key]; ok {
return value
}
}
return nil
}
func hostIDForRemotePath(state map[string]any, preferredHostID, remotePath string) string {
if strings.TrimSpace(preferredHostID) != "" {
return strings.TrimSpace(preferredHostID)
}
for _, project := range orderedRemoteProjectsFromGlobalState(state) {
projectPath := strings.TrimRight(stringFromAny(project["remotePath"]), "/")
if projectPath != "" && (remotePath == projectPath || strings.HasPrefix(remotePath, projectPath+"/")) {
return stringFromAny(project["hostId"])
}
}
return ""
}
func openRequestForRemotePath(state map[string]any, hostID, remotePath string) (map[string]any, error) {
if !strings.HasPrefix(remotePath, "/") {
return nil, zedRemoteError{"Cannot determine remote workspace or file for Zed"}
}
if strings.TrimSpace(hostID) == "" {
return nil, zedRemoteError{"Remote host id is required"}
}
target, err := resolveSSHTargetFromGlobalState(state, hostID)
if err != nil {
return nil, err
}
return map[string]any{
"hostId": hostID,
"ssh": map[string]any{
"user": target.User,
"host": target.Host,
"port": target.Port,
},
"path": remotePath,
}, nil
}
func zedRegistryPath() string {
return filepath.Join(stateDir(), "zed_remote_projects.json")
}
func listZedRemoteProjects(payload map[string]any, registryPath, sqlitePath string) ([]zedRemoteProject, error) {
var projects []zedRemoteProject
state, stateErr := readCodexGlobalStateMap()
if stateErr == nil {
collectCurrentZedProject(state, payload, &projects)
collectCodexRemoteProjects(state, &projects)
collectThreadWorkspaceHintProjects(state, &projects)
collectSQLiteThreadCWDProjects(state, sqlitePath, &projects)
} else if !os.IsNotExist(stateErr) {
return nil, stateErr
}
recent, err := readZedRegistryProjects(registryPath)
if err != nil {
return nil, err
}
for _, project := range recent {
project.Source = "recent"
project.IsCurrent = false
pushZedProject(&projects, project)
}
sort.SliceStable(projects, func(i, j int) bool {
if projects[i].IsCurrent != projects[j].IsCurrent {
return projects[i].IsCurrent
}
pi, pj := zedProjectSourcePriority(projects[i].Source), zedProjectSourcePriority(projects[j].Source)
if pi != pj {
return pi < pj
}
li, lj := int64(0), int64(0)
if projects[i].LastOpenedAtMS != nil {
li = *projects[i].LastOpenedAtMS
}
if projects[j].LastOpenedAtMS != nil {
lj = *projects[j].LastOpenedAtMS
}
if li != lj {
return li > lj
}
return projects[i].Label < projects[j].Label
})
return projects, nil
}
func readCodexGlobalStateMap() (map[string]any, error) {
data, err := os.ReadFile(codexGlobalStatePath(codexHomeDir()))
if err != nil {
return nil, err
}
var state map[string]any
if err := json.Unmarshal(data, &state); err != nil {
return nil, fmt.Errorf("Cannot parse Codex remote connection state: %w", err)
}
return state, nil
}
func collectCurrentZedProject(state map[string]any, payload map[string]any, projects *[]zedRemoteProject) {
hostID := stringFromAny(payload["hostId"])
threadID := firstString(payload["threadId"], payload["sessionId"], payload["session_id"])
workspaceRoot := firstString(payload["remoteWorkspaceRoot"], payload["workspaceRoot"], payload["cwd"], payload["path"])
remoteProjectID := firstString(payload["remoteProjectId"], payload["projectId"])
if hostID == "" && threadID == "" && workspaceRoot == "" && remoteProjectID == "" && stringFromAny(state["selected-remote-host-id"]) == "" {
return
}
request, err := fallbackOpenRequestFromGlobalStateWithContext(state, hostID, threadID, workspaceRoot, remoteProjectID)
if err != nil {
return
}
project, err := zedProjectFromPayload(request, "currentThread", true, nil)
if err == nil {
pushZedProject(projects, project)
}
}
func collectCodexRemoteProjects(state map[string]any, projects *[]zedRemoteProject) {
for _, item := range orderedRemoteProjectsFromGlobalState(state) {
hostID := stringFromAny(item["hostId"])
remotePath := stringFromAny(item["remotePath"])
if hostID == "" || !strings.HasPrefix(remotePath, "/") {
continue
}
target, err := resolveSSHTargetFromGlobalState(state, hostID)
if err != nil {
continue
}
url, err := buildZedRemoteURL(target, remotePath)
if err != nil {
continue
}
project := zedProjectFromParts(hostID, target, remotePath, url, firstString(item["label"], item["name"]), "codexRemoteProject", nil, false)
pushZedProject(projects, project)
}
}
func collectThreadWorkspaceHintProjects(state map[string]any, projects *[]zedRemoteProject) {
hints, _ := state["thread-workspace-root-hints"].(map[string]any)
for _, hint := range hints {
remotePath := workspacePathFromHint(hint)
if !strings.HasPrefix(remotePath, "/") {
continue
}
hostID := hostIDForRemotePath(state, hostIDFromHint(hint), remotePath)
if hostID == "" {
continue
}
target, err := resolveSSHTargetFromGlobalState(state, hostID)
if err != nil {
continue
}
url, err := buildZedRemoteURL(target, remotePath)
if err != nil {
continue
}
project := zedProjectFromParts(hostID, target, remotePath, url, "", "threadWorkspaceHint", nil, false)
pushZedProject(projects, project)
}
}
func collectSQLiteThreadCWDProjects(state map[string]any, sqlitePath string, projects *[]zedRemoteProject) {
for _, cwd := range sqliteThreadCWDs(sqlitePath) {
if !strings.HasPrefix(cwd, "/") {
continue
}
hostID := hostIDForRemotePath(state, "", cwd)
if hostID == "" {
continue
}
target, err := resolveSSHTargetFromGlobalState(state, hostID)
if err != nil {
continue
}
url, err := buildZedRemoteURL(target, cwd)
if err != nil {
continue
}
project := zedProjectFromParts(hostID, target, cwd, url, "", "sqliteThreadCwd", nil, false)
pushZedProject(projects, project)
}
}
func sqliteThreadCWDs(path string) []string {
if path == "" || !fileExists(path) {
return nil
}
db, err := sql.Open("sqlite", path)
if err != nil {
return nil
}
defer db.Close()
rows, err := db.Query("SELECT DISTINCT cwd FROM threads WHERE cwd IS NOT NULL AND cwd != '' LIMIT 80")
if err != nil {
return nil
}
defer rows.Close()
var out []string
for rows.Next() {
var cwd string
if rows.Scan(&cwd) == nil && strings.TrimSpace(cwd) != "" {
out = append(out, strings.TrimSpace(cwd))
}
}
return out
}
func zedProjectFromPayload(payload map[string]any, source string, current bool, lastOpenedAtMS *int64) (zedRemoteProject, error) {
target, err := sshTargetFromPayload(payload)
if err != nil {
return zedRemoteProject{}, err
}
path := stringFromAny(payload["path"])
url := stringFromAny(payload["url"])
if url == "" {
url, err = buildZedRemoteURL(target, path)
if err != nil {
return zedRemoteProject{}, err
}
}
return zedProjectFromParts(stringFromAny(payload["hostId"]), target, path, url, stringFromAny(payload["label"]), source, lastOpenedAtMS, current), nil
}
func zedProjectFromParts(hostID string, target sshTarget, path, url, label, source string, lastOpenedAtMS *int64, current bool) zedRemoteProject {
path = strings.TrimSpace(path)
if strings.TrimSpace(label) == "" {
label = zedLabelFromPath(path)
}
return zedRemoteProject{
ID: zedProjectID(target, path),
Label: strings.TrimSpace(label),
HostID: strings.TrimSpace(hostID),
SSH: target,
Path: path,
URL: url,
Source: source,
LastOpenedAtMS: lastOpenedAtMS,
IsCurrent: current,
}
}
func rememberZedRemoteProject(payload map[string]any, resolvedTarget *sshTarget, resolvedURL string) (zedRemoteProject, error) {
target := sshTarget{}
var err error
if resolvedTarget != nil {
target = *resolvedTarget
} else {
target, err = sshTargetFromPayload(payload)
if err != nil {
return zedRemoteProject{}, err
}
}
path := stringFromAny(payload["path"])
url := resolvedURL
if url == "" {
url, err = buildZedRemoteURL(target, path)
if err != nil {
return zedRemoteProject{}, err
}
}
now := time.Now().UnixMilli()
project := zedProjectFromParts(stringFromAny(payload["hostId"]), target, path, url, stringFromAny(payload["label"]), "recent", &now, false)
registryPath := zedRegistryPath()
projects, err := readZedRegistryProjects(registryPath)
if err != nil {
return zedRemoteProject{}, err
}
pushZedProject(&projects, project)
sort.SliceStable(projects, func(i, j int) bool {
li, lj := int64(0), int64(0)
if projects[i].LastOpenedAtMS != nil {
li = *projects[i].LastOpenedAtMS
}
if projects[j].LastOpenedAtMS != nil {
lj = *projects[j].LastOpenedAtMS
}
return li > lj
})
if len(projects) > 100 {
projects = projects[:100]
}
if err := writeZedRegistryProjects(registryPath, projects); err != nil {
return zedRemoteProject{}, err
}
return project, nil
}
func forgetZedRemoteProject(payload map[string]any) (int, error) {
id := stringFromAny(payload["id"])
if id == "" {
target, err := sshTargetFromPayload(payload)
if err != nil {
return 0, err
}
id = zedProjectID(target, stringFromAny(payload["path"]))
}
path := zedRegistryPath()
projects, err := readZedRegistryProjects(path)
if err != nil {
return 0, err
}
before := len(projects)
filtered := projects[:0]
for _, project := range projects {
if project.ID != id {
filtered = append(filtered, project)
}
}
if err := writeZedRegistryProjects(path, filtered); err != nil {
return 0, err
}
return before - len(filtered), nil
}
func readZedRegistryProjects(path string) ([]zedRemoteProject, error) {
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("Cannot read ChatGPT Codex Tools Zed remote project registry: %w", err)
}
if strings.TrimSpace(string(data)) == "" {
return nil, nil
}
var registry zedRemoteProjectRegistry
if err := json.Unmarshal(data, ®istry); err == nil {
return registry.Projects, nil
}
var projects []zedRemoteProject
if err := json.Unmarshal(data, &projects); err != nil {
return nil, fmt.Errorf("Cannot parse ChatGPT Codex Tools Zed remote project registry: %w", err)
}
return projects, nil
}
func writeZedRegistryProjects(path string, projects []zedRemoteProject) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return fmt.Errorf("Cannot write ChatGPT Codex Tools Zed remote project registry: %w", err)
}
data, err := json.MarshalIndent(zedRemoteProjectRegistry{Projects: projects}, "", " ")
if err != nil {
return fmt.Errorf("Cannot write ChatGPT Codex Tools Zed remote project registry: %w", err)
}
if err := atomicWrite(path, data); err != nil {
return fmt.Errorf("Cannot write ChatGPT Codex Tools Zed remote project registry: %w", err)
}
return nil
}
func pushZedProject(projects *[]zedRemoteProject, project zedRemoteProject) {
if project.ID == "" || project.Path == "" || project.URL == "" {
return
}
for index := range *projects {
existing := &(*projects)[index]
if existing.ID != project.ID {
continue
}
if zedProjectSourcePriority(project.Source) < zedProjectSourcePriority(existing.Source) {
existing.Source = project.Source
existing.Label = project.Label
existing.HostID = project.HostID
}
if existing.LastOpenedAtMS == nil || (project.LastOpenedAtMS != nil && *project.LastOpenedAtMS > *existing.LastOpenedAtMS) {
existing.LastOpenedAtMS = project.LastOpenedAtMS
}
existing.IsCurrent = existing.IsCurrent || project.IsCurrent
return
}
*projects = append(*projects, project)
}
func zedProjectSourcePriority(source string) int {
switch source {
case "currentThread":
return 0
case "codexRemoteProject":
return 1
case "threadWorkspaceHint":
return 2
case "sqliteThreadCwd":
return 3
case "recent":
return 4
default:
return 9
}
}
func zedProjectID(target sshTarget, path string) string {
port := ""
if target.Port != nil {
port = strconv.Itoa(int(*target.Port))
}
hash := fnv.New64a()