跳到主要内容

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

浏览文件

macos_audio.go

87 行 · 3.1 KB历史下载
1package workbench
2
3import (
4 "bufio"
5 "bytes"
6 "context"
7 "encoding/json"
8 "io"
9 "net/http"
10 "strings"
11 "time"
12)
13
14func (s *Server) macOSAudioStatus(w http.ResponseWriter, r *http.Request) {
15 ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
16 defer cancel()
17 cmd := s.sshCommand(ctx, "-T", s.sshTarget(), "--", macOSWindowSocketCommand)
18 cmd.Stdin = strings.NewReader("{\"action\":\"audio-status\"}\n")
19 cmd.Stderr = io.Discard
20 var data bytes.Buffer
21 cmd.Stdout = &boundedWindowsStatusWriter{buffer: &data, remaining: 4096}
22 var status struct {
23 Supported bool `json:"supported"`
24 Available bool `json:"available"`
25 Reason string `json:"reason"`
26 Error string `json:"error"`
27 }
28 if cmd.Run() != nil || json.Unmarshal(data.Bytes(), &status) != nil {
29 http.Error(w, "虚拟 Mac 声音组件尚未连接,请先完成客机组件更新", http.StatusBadGateway)
30 return
31 }
32 if status.Reason == "" {
33 status.Reason = status.Error
34 }
35 if !status.Supported && status.Reason != "requires_gui_login" && status.Reason != "screen_recording_permission" {
36 http.Error(w, "虚拟 Mac 声音组件尚未连接,请先完成客机组件更新", http.StatusBadGateway)
37 return
38 }
39 if !status.Available {
40 message := "请先登录并解锁虚拟 Mac 的普通账户"
41 if status.Reason == "screen_recording_permission" {
42 message = "请在虚拟 Mac 的系统设置中允许 Cage Guest Window 录制屏幕和系统声音"
43 }
44 http.Error(w, message, http.StatusConflict)
45 return
46 }
47 jsonReply(w, audioFormat)
48}
49
50func (s *Server) macOSAudioCapture(w http.ResponseWriter, r *http.Request, ctx context.Context, cancel context.CancelFunc) {
51 cmd := s.sshCommand(ctx, "-T", s.sshTarget(), "--", macOSWindowSocketCommand)
52 cmd.Stderr = io.Discard
53 input, err := cmd.StdinPipe()
54 if err != nil {
55 http.Error(w, "声音连接未能建立", http.StatusBadGateway)
56 return
57 }
58 pipe, err := cmd.StdoutPipe()
59 if err != nil {
60 input.Close()
61 http.Error(w, "声音连接未能建立", http.StatusBadGateway)
62 return
63 }
64 if err = cmd.Start(); err != nil {
65 input.Close()
66 pipe.Close()
67 http.Error(w, "声音连接未能建立", http.StatusBadGateway)
68 return
69 }
70 defer func() { cancel(); input.Close(); pipe.Close(); _ = cmd.Wait() }()
71 // Keep the request side open until cancellation so the guest can distinguish
72 // a live, output-only subscriber from a disconnected SSH client.
73 if _, err = io.WriteString(input, "{\"action\":\"audio-capture\"}\n"); err != nil {
74 http.Error(w, "声音连接未能建立", http.StatusBadGateway)
75 return
76 }
77 deadlineDone := make(chan struct{})
78 deadline := time.AfterFunc(15*time.Second, func() { defer close(deadlineDone); cancel(); pipe.Close() })
79 reader := bufio.NewReaderSize(pipe, 4096)
80 err = readAudioReady(reader)
81 stopped := cancelWindowsAdmissionDeadline(deadline, deadlineDone)
82 if err != nil || !stopped {
83 http.Error(w, "虚拟 Mac 声音未能启动,请确认客机已解锁并允许 Cage Guest Window 录制屏幕和系统声音", http.StatusBadGateway)
84 return
85 }
86 s.streamAudio(w, r.WithContext(ctx), bufferedAudio{Reader: reader, Closer: pipe})
87}