跳到主要内容

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

浏览文件

native_desktop_downloads.go

141 行 · 4.4 KB历史下载
1package workbench
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "io"
8 "strings"
9)
10
11func nativeDesktopDownloadInput(method, path string, input any) (map[string]any, error) {
12 if method != "POST" || (path != "/file-open" && path != "/file-read" && path != "/file-close") {
13 return nil, errors.New("客机下载操作无效")
14 }
15 data, err := json.Marshal(input)
16 if err != nil {
17 return nil, err
18 }
19 var request map[string]any
20 if err = json.Unmarshal(data, &request); err != nil {
21 return nil, err
22 }
23 request["action"] = "files-download-" + strings.TrimPrefix(path, "/file-")
24 return request, nil
25}
26
27func (s *Server) nativeDesktopDownloadRequest(ctx context.Context, method, path string, input any) ([]byte, error) {
28 request, err := nativeDesktopDownloadInput(method, path, input)
29 if err != nil {
30 return nil, err
31 }
32 if s.windowsGuest != nil {
33 var reply map[string]any
34 if err = s.windowsDesktopRequest(ctx, request["action"].(string), request, &reply); err != nil {
35 return nil, windowsDesktopFileFailure(err)
36 }
37 return json.Marshal(reply)
38 }
39 data, err := json.Marshal(request)
40 if err != nil {
41 return nil, err
42 }
43 var mac macOSWindowRequest
44 if err = json.Unmarshal(data, &mac); err != nil {
45 return nil, err
46 }
47 reply, err := s.macOSDesktopFileCall(ctx, mac)
48 if err != nil {
49 return nil, err
50 }
51 return json.Marshal(reply)
52}
53
54type nativeDesktopDownload struct {
55 io.ReadCloser
56 connection *nativeWindowConnection
57}
58
59func (d *nativeDesktopDownload) Close() error {
60 err := d.ReadCloser.Close()
61 d.connection.Close()
62 return err
63}
64
65// One download owns one ordered window channel. Chunk reads reuse its process
66// and SSH or Hyper-V transport, while the producer retains the file identity.
67func (s *Server) openNativeDesktopDownload(ctx context.Context, id string) (io.ReadCloser, int64, string, error) {
68 var connection *nativeWindowConnection
69 var err error
70 if s.windowsGuest != nil {
71 connection, err = s.windowsWindowConnection(ctx)
72 } else {
73 transport, cancel := context.WithCancel(ctx)
74 cmd := s.sshCommand(transport, "-T", s.sshTarget(), "--", macOSWindowSocketCommand)
75 connection, err = startWindowConnection(cmd, cancel)
76 }
77 if err != nil {
78 return nil, 0, "", err
79 }
80 request := func(call context.Context, method, path string, input any) ([]byte, error) {
81 if path == "/file-close" && ctx.Err() != nil {
82 return s.nativeDesktopDownloadRequest(call, method, path, input)
83 }
84 if err := call.Err(); err != nil {
85 return nil, err
86 }
87 payload, err := nativeDesktopDownloadInput(method, path, input)
88 if err != nil {
89 return nil, err
90 }
91 payload["keepAlive"] = true
92 stop := context.AfterFunc(call, connection.Close)
93 defer stop()
94 var reply json.RawMessage
95 if err = connection.request(payload, &reply); err != nil {
96 var failure windowsWindowFailure
97 if s.macosGuest != nil && errors.As(err, &failure) {
98 return nil, macOSFileFailure(failure.code)
99 }
100 return nil, windowsDesktopFileFailure(err)
101 }
102 return reply, nil
103 }
104 reader, size, name, err := openNativeFileDownload(ctx, id, request)
105 if err != nil {
106 connection.Close()
107 return nil, 0, "", err
108 }
109 return &nativeDesktopDownload{ReadCloser: reader, connection: connection}, size, name, nil
110}
111
112func desktopDownloadFailure(reason string) error {
113 switch reason {
114 case "desktop_download_incomplete":
115 return errors.New("Claude Desktop 尚未完整保存文件,请回到应用重新下载")
116 case "desktop_download_changed":
117 return errors.New("Claude Desktop 下载文件在传输中发生变化,请重新下载")
118 case "desktop_download_unavailable", "desktop_download_transfer_invalid":
119 return errors.New("Claude Desktop 下载文件已不可用,请确认客机已解锁后重新下载")
120 case "desktop_download_limit":
121 return errors.New("待保存的 Claude Desktop 文件过多,请先完成现有下载后重试")
122 case "file_open_target_changed":
123 return errors.New("文件或 Claude 窗口已变化,请回到原窗口重新打开文件")
124 case "file_open_request_invalid", "file_open_request_unavailable":
125 return errors.New("文件打开请求已失效,请确认客机已解锁后重新操作")
126 case "file_open_request_limit":
127 return errors.New("正在处理的文件打开请求过多,请稍后重试")
128 default:
129 return nil
130 }
131}
132
133func windowsDesktopFileFailure(err error) error {
134 var failure windowsWindowFailure
135 if errors.As(err, &failure) {
136 if display := desktopDownloadFailure(failure.code); display != nil {
137 return display
138 }
139 }
140 return err
141}