跳到主要内容

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

浏览文件

desktop.go

119 行 · 4.0 KB历史下载
1// Package desktop reaches the user out of any terminal: notifications and
2// yes/no dialogs, where the desktop has a way (osascript on macOS; notify-send,
3// zenity or kdialog on Linux). Text always goes as arguments, never as script.
4package desktop
5
6import (
7 "context"
8 "errors"
9 "os"
10 "os/exec"
11 "runtime"
12 "strings"
13 "time"
14)
15
16// Notify shows a notification, without waiting; nothing where there is no
17// way.
18func Notify(title, msg string) {
19 var cmd *exec.Cmd
20 switch runtime.GOOS {
21 case "darwin":
22 cmd = exec.Command("osascript", "-e", "on run argv", "-e", "display notification (item 2 of argv) with title (item 1 of argv)", "-e", "end run", title, msg)
23 case "linux":
24 if _, err := exec.LookPath("notify-send"); err != nil {
25 return
26 }
27 cmd = exec.Command("notify-send", "--", title, msg)
28 default:
29 return
30 }
31 if cmd.Start() == nil {
32 go func() { _ = cmd.Wait() }()
33 }
34}
35
36// Ask shows a dialog with Deny (the default) and Allow, until ctx ends (a
37// refusal). ok is false when no dialog could be shown (no tool, no display or
38// graphical session, the tool failed to start): allow is then false and nobody
39// answered. ok is true when the user answered, or ctx ended with the dialog up.
40func Ask(ctx context.Context, title, msg string) (allow, ok bool) {
41 switch runtime.GOOS {
42 case "darwin":
43 res := run(ctx, "osascript", "-e", "on run argv",
44 "-e", `display dialog (item 2 of argv) with title (item 1 of argv) buttons {"Deny", "Allow"} default button "Deny" with icon caution`,
45 "-e", "end run", title, msg)
46 switch {
47 case res.err == nil:
48 return strings.Contains(res.stdout, "button returned:Allow"), true
49 case res.timedOut:
50 return false, true
51 }
52 // Deny by closing the dialog is "User canceled. (-128)", an exit status 1. Without a
53 // graphical session osascript exits 1 as well, with another error.
54 return false, strings.Contains(res.stderr, "(-128)")
55 case "linux":
56 if os.Getenv("DISPLAY") == "" && os.Getenv("WAYLAND_DISPLAY") == "" {
57 return false, false
58 }
59 if _, err := exec.LookPath("zenity"); err == nil {
60 res := run(ctx, "zenity", "--question", "--title", title, "--text", msg,
61 "--ok-label=Allow", "--cancel-label=Deny", "--default-cancel", "--no-markup")
62 if allow, ok := linuxAnswer(res, 1, "cannot open display", "failed to open display", "unable to init server"); ok {
63 return allow, true
64 }
65 }
66 if _, err := exec.LookPath("kdialog"); err == nil {
67 res := run(ctx, "kdialog", "--title", title, "--warningyesno", msg,
68 "--yes-label", "Allow", "--no-label", "Deny")
69 if allow, ok := linuxAnswer(res, 2, "could not connect to display", "cannot connect to x server", "no qt platform plugin"); ok {
70 return allow, true
71 }
72 }
73 }
74 return false, false
75}
76
77// linuxAnswer reads the result of zenity or kdialog: exit 0 is Allow, and an exit status up to
78// maxDeny is Deny, unless stderr says the tool had no display to show the dialog on.
79func linuxAnswer(res result, maxDeny int, noDisplay ...string) (allow, ok bool) {
80 if res.err == nil {
81 return true, true
82 }
83 if res.timedOut {
84 return false, true
85 }
86 if res.code < 1 || res.code > maxDeny {
87 return false, false
88 }
89 stderr := strings.ToLower(res.stderr)
90 for _, s := range noDisplay {
91 if strings.Contains(stderr, s) {
92 return false, false
93 }
94 }
95 return false, true
96}
97
98type result struct {
99 stdout, stderr string
100 err error
101 code int // the exit status, -1 when the tool did not exit (not found, killed)
102 timedOut bool // ctx ended while the tool ran
103}
104
105func run(ctx context.Context, name string, args ...string) result {
106 cmd := exec.CommandContext(ctx, name, args...)
107 var stdout, stderr strings.Builder
108 cmd.Stdout, cmd.Stderr = &stdout, &stderr
109 cmd.WaitDelay = time.Second
110 err := cmd.Run()
111 res := result{stdout: stdout.String(), stderr: stderr.String(), err: err, code: -1}
112 var ee *exec.ExitError
113 if errors.As(err, &ee) {
114 res.code = ee.ExitCode()
115 // Killed when ctx ended, rather than exited.
116 res.timedOut = res.code == -1 && ctx.Err() != nil
117 }
118 return res
119}