跳到主要内容

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

浏览文件

mobile_cli_adopt.go

107 行 · 3.3 KB历史下载
1package workbench
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "io"
8 "strings"
9 "time"
10
11 "al.essio.dev/pkg/shellescape"
12)
13
14// Older phones created headless sessions. Reuse their CLI UUID only after the
15// original provider binding has been checked, without sending a model request.
16func (s *Server) promoteMobileConversation(ctx context.Context, id string) error {
17 if !s.op.TryLock() {
18 return errTerminalBusy
19 }
20 defer s.op.Unlock()
21 m := &s.mobileCLI
22 m.mu.Lock()
23 record, err := m.record(s.profile, id)
24 active := m.active[id] != nil
25 m.mu.Unlock()
26 if err != nil {
27 return err
28 }
29 if record.Terminal {
30 return nil
31 }
32 if active || record.turnActive() {
33 return errors.New("会话仍在运行,请等待本轮完成后打开")
34 }
35 selection, launcher, _, err := s.prepareMobileLaunch(record)
36 if err != nil {
37 return err
38 }
39 if record.SingleTurn {
40 return errors.New("此会话的原账号与上游无法验证,请核对原配置后新建会话")
41 }
42 config := ""
43 if record.Resume {
44 prefix, err := mobileLaunchArgs(launcher, selection)
45 if err != nil {
46 return err
47 }
48 args := mobileIdentityCommand(prefix, []string{"--cage-verify", "--setting-sources", "user", "--resume", record.CLIID})
49 // Match the login environment and project of the original mobile turn.
50 body := "exec 1>&3 3>&-; cd -- " + shellescape.Quote(guestPath(record.Workspace)) + " && exec " + shellescape.QuoteCommand(args)
51 command := "exec /bin/bash -c " + shellescape.Quote("exec 3>&1 1>&2; exec /bin/bash -lc "+shellescape.Quote(body))
52 check, cancel := context.WithTimeout(ctx, 20*time.Second)
53 defer cancel()
54 cmd := s.sshCommand(check, "-T", s.sshTarget(), "--", command)
55 cmd.Stderr = io.Discard
56 cmd.WaitDelay = 2 * time.Second
57 out, err := cmd.Output()
58 var result struct {
59 Config string `json:"config"`
60 }
61 if err != nil || len(out) > 4096 || json.Unmarshal(out, &result) != nil || !strings.HasPrefix(result.Config, "/") {
62 return errors.New("无法验证本会话原先使用的账号与上游,请检查原配置后重试")
63 }
64 config = result.Config
65 }
66 s.account.mu.Lock()
67 defer s.account.mu.Unlock()
68 if !s.mobileAuthorized(ctx) {
69 return errMobileLocked
70 }
71 m.mu.Lock()
72 defer m.mu.Unlock()
73 // Metadata operations can run while the guest verifies the identity.
74 record, err = m.record(s.profile, id)
75 if err != nil {
76 return err
77 }
78 if record.Archived || record.Deleted {
79 return errors.New("此会话已归档或删除,请先恢复会话")
80 }
81 record.LegacyTurns = append([]mobileTurn(nil), record.Turns...)
82 record.Terminal, record.Account, record.Launcher = true, selection, launcher
83 record.GuestConfig, record.GuestWorkspace = config, guestPath(record.Workspace)
84 record.changed()
85 return writePrivate(mobileRecordPath(s.profile, id), record)
86}
87
88// Preserve preflight failures that never reached the CLI transcript. Completed
89// turns come from the original JSONL, with their mobile request IDs retained.
90func adoptedMobileTurns(legacy, current []mobileTurn) []mobileTurn {
91 if len(legacy) == 0 {
92 return current
93 }
94 result := make([]mobileTurn, 0, len(legacy)+len(current))
95 position := 0
96 for _, old := range legacy {
97 if position < len(current) && old.Prompt == current[position].Prompt {
98 turn := current[position]
99 turn.ID = old.ID
100 result = append(result, turn)
101 position++
102 } else {
103 result = append(result, old)
104 }
105 }
106 return append(result, current[position:]...)
107}