跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

browser_workspaces.go

175 行 · 5.4 KB历史下载
1package workbench
2
3import (
4 "bufio"
5 "bytes"
6 "context"
7 "encoding/json"
8 "errors"
9 "io"
10 "net/http"
11 "path/filepath"
12 "time"
13
14 "github.com/sylvinus/agent-vm/internal/paths"
15)
16
17// Browser URLs use guest paths. The workbench's project and terminal actions
18// use host paths, so an editor's workspace must identify its existing grant.
19// Never infer a host drive or a new grant from an arbitrary guest path.
20func (s *Server) browserStateForHost(body []byte) ([]byte, error) {
21 var state map[string]json.RawMessage
22 if err := json.Unmarshal(body, &state); err != nil {
23 return nil, errors.New("浏览器状态无效")
24 }
25 // Cache completed default saves on the host so Open/Reveal continue to work
26 // after the VM stops. A user-selected Save As location always wins.
27 var downloads []map[string]json.RawMessage
28 downloadsChanged := false
29 if json.Unmarshal(state["downloads"], &downloads) == nil {
30 root := s.environmentDir()
31 for _, record := range downloads {
32 encoded, _ := json.Marshal(record)
33 var item browserDownloadInfo
34 if json.Unmarshal(encoded, &item) != nil {
35 continue
36 }
37 if s.macosGuest != nil || s.windowsGuest != nil {
38 item.Target = "browser"
39 mapped := s.nativeDownloadForHost(item)
40 record["name"], _ = json.Marshal(mapped.Name)
41 record["path"], _ = json.Marshal(mapped.Path)
42 record["state"], _ = json.Marshal(mapped.State)
43 record["retryable"], _ = json.Marshal(mapped.Retryable)
44 delete(record, "error")
45 if mapped.Error != "" {
46 record["error"], _ = json.Marshal(mapped.Error)
47 }
48 downloadsChanged = true
49 continue
50 }
51 if item.State != "saved" || !browserFileID.MatchString(item.ID) || !browserDownloadName(item.Name) || item.Path != guestPath(filepath.Join(root, "Downloads", item.Name)) {
52 continue
53 }
54 hostPath := filepath.Join(root, "Downloads", item.Name)
55 if _, err := readSavedDownload(s.profile, item.ID); err != nil {
56 _ = rememberDownload(s.profile, SavedDownload{ID: item.ID, Name: item.Name, Path: hostPath}, true)
57 }
58 if paths.Windows() && hostPath != item.Path {
59 record["path"], _ = json.Marshal(hostPath)
60 downloadsChanged = true
61 }
62 }
63 if s.macosGuest != nil || s.windowsGuest != nil {
64 visible := make(map[string]bool, len(downloads))
65 for _, record := range downloads {
66 var id string
67 if json.Unmarshal(record["id"], &id) == nil {
68 visible[id] = true
69 }
70 }
71 s.discardMissingNativeDownloads("browser", visible)
72 }
73 }
74 if !paths.Windows() && s.windowsGuest == nil && s.macosGuest == nil {
75 return body, nil
76 }
77 var tabs []map[string]json.RawMessage
78 if err := json.Unmarshal(state["tabs"], &tabs); err != nil {
79 return nil, errors.New("浏览器标签状态无效")
80 }
81 authorized, err := s.projectPaths()
82 if err != nil {
83 return nil, errors.New("无法读取项目列表")
84 }
85 byGuest := make(map[string]string, len(authorized))
86 for _, hostPath := range authorized {
87 if s.macosGuest != nil {
88 guestPath := macOSProjectPath(s.macosGuest.User, hostPath)
89 byGuest[guestPath] = hostPath
90 byGuest[filepath.Base(guestPath)] = hostPath
91 } else {
92 byGuest[guestPath(hostPath)] = hostPath
93 }
94 }
95 if s.windowsGuest != nil {
96 allowed := map[string]bool{}
97 for _, hostPath := range authorized {
98 allowed[hostPath] = true
99 }
100 s.mu.Lock()
101 for guestPath, hostPath := range s.windowsProjectHistory {
102 if allowed[hostPath] {
103 byGuest[guestPath] = hostPath
104 }
105 }
106 s.mu.Unlock()
107 }
108 changed := downloadsChanged
109 for _, tab := range tabs {
110 var kind, workspace string
111 if json.Unmarshal(tab["kind"], &kind) != nil || kind != "editor" || json.Unmarshal(tab["workspace"], &workspace) != nil {
112 continue
113 }
114 if hostPath, ok := byGuest[workspace]; ok && hostPath != workspace {
115 tab["workspace"], _ = json.Marshal(hostPath)
116 changed = true
117 }
118 }
119 if !changed {
120 return body, nil
121 }
122 if downloadsChanged {
123 state["downloads"], _ = json.Marshal(downloads)
124 }
125 state["tabs"], err = json.Marshal(tabs)
126 if err != nil {
127 return nil, err
128 }
129 return json.Marshal(state)
130}
131
132// The guest emits one compact JSON snapshot per data line, plus heartbeat
133// comments. Resolve grants for each snapshot so an already-open stream also
134// recognizes projects added after it connected.
135func (s *Server) writeBrowserEvents(ctx context.Context, w http.ResponseWriter, body io.Reader) error {
136 scanner := bufio.NewScanner(body)
137 scanner.Buffer(make([]byte, 16<<10), (1<<20)+16)
138 controller := http.NewResponseController(w)
139 eventName := ""
140 for scanner.Scan() {
141 if err := ctx.Err(); err != nil {
142 return err
143 }
144 line := scanner.Bytes()
145 if bytes.HasPrefix(line, []byte("event: ")) {
146 eventName = string(line[len("event: "):])
147 }
148 // Named notifications retain their own payload and sequence. Only
149 // ordinary message events contain a browser state snapshot.
150 if bytes.HasPrefix(line, []byte("data: ")) && eventName == "" {
151 mapped, err := s.browserStateForHost(line[len("data: "):])
152 if err != nil {
153 return err
154 }
155 line = append([]byte("data: "), mapped...)
156 }
157 if len(line) > browserResponseLimit+len("data: ") {
158 return errors.New("浏览器事件超出大小限制")
159 }
160 _ = controller.SetWriteDeadline(time.Now().Add(10 * time.Second))
161 if err := ctx.Err(); err != nil {
162 return err
163 }
164 if _, err := w.Write(append(line, '\n')); err != nil {
165 return err
166 }
167 if len(line) == 0 {
168 eventName = ""
169 if err := controller.Flush(); err != nil {
170 return err
171 }
172 }
173 }
174 return scanner.Err()
175}