| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "sync" |
| 10 | ) |
| 11 | |
| 12 | var errTerminalInputFull = errors.New("terminal input queue is full") |
| 13 | |
| 14 | type terminalPTY interface { |
| 15 | io.ReadWriteCloser |
| 16 | Resize(cols, rows uint16) error |
| 17 | Wait() error |
| 18 | Kill() error |
| 19 | } |
| 20 | |
| 21 | type terminalSession struct { |
| 22 | mu sync.Mutex |
| 23 | id, title string |
| 24 | sharedID string |
| 25 | connectionID string |
| 26 | conversationEnded bool |
| 27 | workspace string |
| 28 | kind, claudeAccountID string |
| 29 | loginGuestID string |
| 30 | loginLinkMu sync.Mutex |
| 31 | loginLinkCleanup sync.Once |
| 32 | claudeAccountSource string |
| 33 | cmd *exec.Cmd |
| 34 | file terminalPTY |
| 35 | history []byte |
| 36 | closed bool |
| 37 | clients map[chan []byte]bool |
| 38 | done chan struct{} |
| 39 | inputs chan string |
| 40 | stopping chan struct{} |
| 41 | closeOnce sync.Once |
| 42 | cliStartupDone chan struct{} |
| 43 | } |
| 44 | |
| 45 | func (t *terminalSession) ownsConversation() bool { |
| 46 | t.mu.Lock() |
| 47 | defer t.mu.Unlock() |
| 48 | return !t.closed && !t.conversationEnded |
| 49 | } |
| 50 | |
| 51 | func newTerminalSession(file terminalPTY) *terminalSession { |
| 52 | return &terminalSession{file: file, clients: map[chan []byte]bool{}, done: make(chan struct{}), inputs: make(chan string, 8), stopping: make(chan struct{})} |
| 53 | } |
| 54 | |
| 55 | func (t *terminalSession) summary() map[string]any { |
| 56 | t.mu.Lock() |
| 57 | defer t.mu.Unlock() |
| 58 | closed := t.closed |
| 59 | if t.done != nil { |
| 60 | select { |
| 61 | case <-t.done: |
| 62 | closed = true |
| 63 | default: |
| 64 | closed = false |
| 65 | } |
| 66 | } |
| 67 | return map[string]any{"id": t.id, "title": t.title, "closed": closed, "workspace": t.workspace, "kind": t.kind, |
| 68 | "claude_account_id": t.claudeAccountID, "claude_account_source": t.claudeAccountSource, "cli_session_id": t.sharedID, |
| 69 | "cli_starting": t.cliStartupPendingLocked()} |
| 70 | } |
| 71 | |
| 72 | func (t *terminalSession) cliStartupPendingLocked() bool { |
| 73 | if t.cliStartupDone == nil || t.closed { |
| 74 | return false |
| 75 | } |
| 76 | select { |
| 77 | case <-t.cliStartupDone: |
| 78 | return false |
| 79 | default: |
| 80 | return true |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func (t *terminalSession) finishCLIStartup() { |
| 85 | t.mu.Lock() |
| 86 | defer t.mu.Unlock() |
| 87 | if t.cliStartupPendingLocked() { |
| 88 | close(t.cliStartupDone) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func (t *terminalSession) stopPTY() { |
| 93 | t.closeOnce.Do(func() { |
| 94 | t.mu.Lock() |
| 95 | t.closed = true |
| 96 | t.mu.Unlock() |
| 97 | if t.stopping != nil { |
| 98 | close(t.stopping) |
| 99 | } |
| 100 | if t.file != nil { |
| 101 | |
| 102 | |
| 103 | _ = t.file.Kill() |
| 104 | _ = t.file.Close() |
| 105 | } |
| 106 | }) |
| 107 | } |
| 108 | |
| 109 | func (t *terminalSession) close() { |
| 110 | t.stopPTY() |
| 111 | if t.done != nil { |
| 112 | <-t.done |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | func (t *terminalSession) enqueueInput(ctx context.Context, input string) error { |
| 121 | if err := ctx.Err(); err != nil { |
| 122 | return err |
| 123 | } |
| 124 | t.mu.Lock() |
| 125 | defer t.mu.Unlock() |
| 126 | if t.closed { |
| 127 | return os.ErrClosed |
| 128 | } |
| 129 | select { |
| 130 | case t.inputs <- input: |
| 131 | return nil |
| 132 | default: |
| 133 | return errTerminalInputFull |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func (t *terminalSession) writeInput() { |
| 138 | for { |
| 139 | select { |
| 140 | case <-t.stopping: |
| 141 | return |
| 142 | case input := <-t.inputs: |
| 143 | if _, err := io.WriteString(t.file, input); err != nil { |
| 144 | t.stopPTY() |
| 145 | return |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | type terminalSubscription struct { |
| 152 | History []byte |
| 153 | Output <-chan []byte |
| 154 | CLIStartup <-chan struct{} |
| 155 | CLIStarting bool |
| 156 | session *terminalSession |
| 157 | channel chan []byte |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | func (t *terminalSession) subscribe() *terminalSubscription { |
| 163 | t.mu.Lock() |
| 164 | defer t.mu.Unlock() |
| 165 | channel := make(chan []byte, 64) |
| 166 | if t.closed { |
| 167 | close(channel) |
| 168 | } else { |
| 169 | t.clients[channel] = true |
| 170 | } |
| 171 | history := append([]byte(nil), t.history...) |
| 172 | if t.kind == "claude-login" { |
| 173 | history = []byte("请在 Cage 浏览器的临时登录页完成授权。若页面提供验证码,可返回此终端输入。\r\n") |
| 174 | } |
| 175 | return &terminalSubscription{History: history, Output: channel, CLIStartup: t.cliStartupDone, |
| 176 | CLIStarting: t.cliStartupPendingLocked(), session: t, channel: channel} |
| 177 | } |
| 178 | |
| 179 | |
| 180 | func (s *terminalSubscription) Close() { |
| 181 | s.session.mu.Lock() |
| 182 | defer s.session.mu.Unlock() |
| 183 | if s.session.clients[s.channel] { |
| 184 | delete(s.session.clients, s.channel) |
| 185 | close(s.channel) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func (t *terminalSession) run() { |
| 190 | writerDone := make(chan struct{}) |
| 191 | go func() { defer close(writerDone); t.writeInput() }() |
| 192 | t.read() |
| 193 | |
| 194 | |
| 195 | t.stopPTY() |
| 196 | _ = t.file.Wait() |
| 197 | <-writerDone |
| 198 | |
| 199 | |
| 200 | for len(t.inputs) > 0 { |
| 201 | <-t.inputs |
| 202 | } |
| 203 | t.mu.Lock() |
| 204 | for channel := range t.clients { |
| 205 | close(channel) |
| 206 | delete(t.clients, channel) |
| 207 | } |
| 208 | t.mu.Unlock() |
| 209 | close(t.done) |
| 210 | } |
| 211 | |
| 212 | func (t *terminalSession) read() { |
| 213 | buffer := make([]byte, 16<<10) |
| 214 | for { |
| 215 | n, err := t.file.Read(buffer) |
| 216 | if n > 0 { |
| 217 | chunk := append([]byte(nil), buffer[:n]...) |
| 218 | t.mu.Lock() |
| 219 | if t.kind != "claude-login" { |
| 220 | t.history = append(t.history, chunk...) |
| 221 | if len(t.history) > 512<<10 { |
| 222 | t.history = t.history[len(t.history)-(512<<10):] |
| 223 | } |
| 224 | } |
| 225 | for channel := range t.clients { |
| 226 | select { |
| 227 | case channel <- chunk: |
| 228 | default: |
| 229 | close(channel) |
| 230 | delete(t.clients, channel) |
| 231 | } |
| 232 | } |
| 233 | t.mu.Unlock() |
| 234 | } |
| 235 | if err != nil { |
| 236 | break |
| 237 | } |
| 238 | } |
| 239 | } |