返回 DeepSeek-Reasonix
workspace.go
根目录 / desktop / workspace.go
1 package main
2
3 import (
4 "encoding/json"
5 "os"
6 "path/filepath"
7 "strings"
8
9 "reasonix/internal/config"
10 "reasonix/internal/fileutil"
11 )
12
13 // The desktop is a GUI app: launched from Finder or `open`, it starts with the
14 // working directory set to "/" (read-only), so anything cwd-relative — config,
15 // .env writes, memory/skill discovery — fails or lands nowhere useful. We keep a
16 // real working folder instead: remember the last one the user picked and chdir
17 // into it at startup, falling back to the home directory when there's none and
18 // cwd isn't writable.
19
20 // workspaceStatePath is where the last working folder is remembered (under the
21 // user config dir, shared with the rest of Reasonix's state).
22 func workspaceStatePath() string {
23 dir := config.MemoryUserDir() // …/reasonix
24 if dir == "" {
25 return ""
26 }
27 return filepath.Join(dir, "desktop-workspace")
28 }
29
30 func workspaceListPath() string {
31 dir := config.MemoryUserDir()
32 if dir == "" {
33 return ""
34 }
35 return filepath.Join(dir, "desktop-workspaces.json")
36 }
37
38 // saveWorkspace records dir as the last working folder.
39 func saveWorkspace(dir string) {
40 p := workspaceStatePath()
41 if p == "" || dir == "" {
42 return
43 }
44 if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
45 return
46 }
47 _ = fileutil.AtomicWriteFile(p, []byte(dir), 0o644)
48 }
49
50 // clearWorkspace removes the active workspace pointer file so that no stale
51 // reference remains after the current workspace is deleted.
52 func clearWorkspace() {
53 p := workspaceStatePath()
54 if p == "" {
55 return
56 }
57 _ = os.Remove(p)
58 }
59
60 // loadWorkspace returns the remembered working folder, or "" if none.
61 func loadWorkspace() string {
62 p := workspaceStatePath()
63 if p == "" {
64 return ""
65 }
66 b, err := readFileUTF8(p)
67 if err != nil {
68 return ""
69 }
70 return strings.TrimSpace(string(b))
71 }
72
73 func loadWorkspaces() []string {
74 p := workspaceListPath()
75 if p == "" {
76 return nil
77 }
78 var paths []string
79 b, err := readFileUTF8(p)
80 if err != nil || json.Unmarshal(b, &paths) != nil {
81 return nil
82 }
83 out := make([]string, 0, len(paths))
84 seen := map[string]bool{}
85 for _, path := range paths {
86 path = strings.TrimSpace(path)
87 if path == "" || seen[path] {
88 continue
89 }
90 seen[path] = true
91 out = append(out, path)
92 }
93 return out
94 }
95
96 func rememberWorkspace(dir string) {
97 dir = strings.TrimSpace(dir)
98 if dir == "" {
99 return
100 }
101 if abs, err := filepath.Abs(dir); err == nil {
102 dir = abs
103 }
104 paths := []string{dir}
105 for _, path := range loadWorkspaces() {
106 if path != dir {
107 paths = append(paths, path)
108 }
109 if len(paths) >= 12 {
110 break
111 }
112 }
113 p := workspaceListPath()
114 if p == "" {
115 return
116 }
117 if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
118 return
119 }
120 if b, err := json.MarshalIndent(paths, "", " "); err == nil {
121 _ = fileutil.AtomicWriteFile(p, b, 0o644)
122 }
123 }
124
125 func forgetWorkspace(dir string) {
126 dir = strings.TrimSpace(dir)
127 if dir == "" {
128 return
129 }
130 if abs, err := filepath.Abs(dir); err == nil {
131 dir = abs
132 }
133 paths := make([]string, 0, len(loadWorkspaces()))
134 for _, path := range loadWorkspaces() {
135 if abs, err := filepath.Abs(path); err == nil {
136 path = abs
137 }
138 if path != dir {
139 paths = append(paths, path)
140 }
141 }
142 p := workspaceListPath()
143 if p == "" {
144 return
145 }
146 if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
147 return
148 }
149 if b, err := json.MarshalIndent(paths, "", " "); err == nil {
150 _ = fileutil.AtomicWriteFile(p, b, 0o644)
151 }
152 }
153
154 // ensureWorkspace establishes a writable working directory at startup: the
155 // remembered folder if it's still a directory, else the home directory when the
156 // current cwd isn't writable (the Finder/`open` "/" case). A writable cwd with no
157 // remembered folder (e.g. `wails dev` in the repo) is left untouched.
158 func ensureWorkspace() {
159 if ws := loadWorkspace(); ws != "" {
160 if info, err := os.Stat(ws); err == nil && info.IsDir() && os.Chdir(ws) == nil {
161 return
162 }
163 }
164 if cwdWritable() {
165 return
166 }
167 if home, err := os.UserHomeDir(); err == nil {
168 _ = os.Chdir(home)
169 }
170 }
171
172 // cwdWritable reports whether the current directory accepts a file write — the
173 // reliable test for the read-only "/" a GUI launch lands in.
174 func cwdWritable() bool {
175 cwd, err := os.Getwd()
176 if err != nil {
177 return false
178 }
179 f, err := os.CreateTemp(cwd, ".reasonix-wtest-*")
180 if err != nil {
181 return false
182 }
183 name := f.Name()
184 _ = f.Close()
185 _ = os.Remove(name)
186 return true
187 }
188
188 lines GO