跳到主要内容

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

浏览文件

cli_connect.go

114 行 · 3.4 KB历史下载
1package workbench
2
3import (
4 "context"
5 "regexp"
6 "strings"
7 "time"
8 "unicode"
9)
10
11var cliStartupEscape = regexp.MustCompile(`\x1b\[[0-?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)`)
12var cliOwnedChannel = regexp.MustCompile(`Channels:server:cage(?:[❯›>]?1\.|Iamusingthisforlocaldevelopment)`)
13
14func cliStartupText(data []byte) string {
15 return strings.Map(func(r rune) rune {
16 if unicode.IsSpace(r) {
17 return -1
18 }
19 return r
20 }, cliStartupEscape.ReplaceAllString(string(data), ""))
21}
22
23// Cage already owns this locally generated channel and the user has granted
24// access to this project. Only their two startup dialogs can be completed here.
25// Tool approvals, external MCP servers and login dialogs remain untouched.
26func cliStartupDialog(text, workspace string) string {
27 if strings.Contains(text, "WARNING:Loadingdevelopmentchannels") &&
28 cliOwnedChannel.MatchString(text) &&
29 strings.Contains(text, "Iamusingthisforlocaldevelopment") {
30 return "channel"
31 }
32 if workspace != "" && strings.Contains(strings.ToLower(text), "quicksafetycheck:isthisaprojectyoucreatedoroneyoutrust?") &&
33 strings.Contains(text, cliStartupText([]byte(workspace))) &&
34 strings.Contains(text, "Yes,Itrustthisfolder") {
35 return "project"
36 }
37 return ""
38}
39
40func (s *Server) prepareCLIConnection(terminal *terminalSession, id string) {
41 terminal.mu.Lock()
42 terminal.cliStartupDone = make(chan struct{})
43 terminal.mu.Unlock()
44 s.activities.Add(1)
45 go func() {
46 defer s.activities.Done()
47 defer terminal.finishCLIStartup()
48 ctx, cancel := context.WithTimeout(s.lifetimeContext(), 45*time.Second)
49 defer cancel()
50 s.mobileCLI.mu.Lock()
51 record, err := s.mobileCLI.record(s.profile, id)
52 s.mobileCLI.mu.Unlock()
53 if err != nil {
54 return
55 }
56 ticker := time.NewTicker(200 * time.Millisecond)
57 defer ticker.Stop()
58 seen := map[string]time.Time{}
59 completed := map[string]bool{}
60 nextCheck := time.Now()
61 for {
62 select {
63 case <-ctx.Done():
64 return
65 case <-terminal.done:
66 return
67 case <-ticker.C:
68 }
69 if time.Now().After(nextCheck) {
70 var current terminalConversation
71 if err := s.runCLIChannel(ctx, "view", map[string]string{"cli_id": record.CLIID, "config": record.GuestConfig, "connection": record.Connection}, &current); err != nil {
72 nextCheck = time.Now().Add(time.Second)
73 continue
74 }
75 if current.ChannelReady || current.CanSend {
76 var response struct {
77 OK bool `json:"ok"`
78 }
79 if err := s.runCLIChannel(ctx, "rpc", map[string]string{"cli_id": record.CLIID, "connection": record.Connection, "action": "resume"}, &response); err != nil || !response.OK {
80 continue
81 }
82 return
83 }
84 if current.Active || current.ConnectionNotice != "" {
85 return
86 }
87 nextCheck = time.Now().Add(time.Second)
88 }
89 terminal.mu.Lock()
90 text := cliStartupText(terminal.history)
91 terminal.mu.Unlock()
92 dialog := cliStartupDialog(text, record.GuestWorkspace)
93 if dialog == "" || completed[dialog] {
94 continue
95 }
96 if seen[dialog].IsZero() {
97 seen[dialog] = time.Now()
98 continue
99 }
100 // The CLI ignores keys during a dialog's opening guard interval.
101 if time.Since(seen[dialog]) < time.Second {
102 continue
103 }
104 input := "\r"
105 if dialog == "project" && strings.Contains(text, "❯No,exit") {
106 input = "\x1b[B\r"
107 }
108 if err := terminal.enqueueInput(ctx, input); err != nil {
109 return
110 }
111 completed[dialog] = true
112 }
113 }()
114}