| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "unicode/utf8" |
| 10 | ) |
| 11 | |
| 12 | type sessionMetadataInput struct { |
| 13 | Title *string `json:"title"` |
| 14 | Pinned *bool `json:"pinned"` |
| 15 | } |
| 16 | |
| 17 | func (s *Server) updateSessionMetadata(id string, input sessionMetadataInput, requestContext context.Context) (mobileSession, error) { |
| 18 | if !s.op.TryLock() { |
| 19 | return mobileSession{}, errTerminalBusy |
| 20 | } |
| 21 | defer s.op.Unlock() |
| 22 | if input.Title == nil && input.Pinned == nil { |
| 23 | return mobileSession{}, errors.New("请选择要修改的会话信息") |
| 24 | } |
| 25 | if input.Title != nil { |
| 26 | title := strings.TrimSpace(*input.Title) |
| 27 | if title == "" || !utf8.ValidString(title) || utf8.RuneCountInString(title) > 100 { |
| 28 | return mobileSession{}, errors.New("会话名称需要 1 至 100 个字符") |
| 29 | } |
| 30 | input.Title = &title |
| 31 | } |
| 32 | s.conversationMu.Lock() |
| 33 | defer s.conversationMu.Unlock() |
| 34 | if requestContext != nil { |
| 35 | s.account.mu.Lock() |
| 36 | defer s.account.mu.Unlock() |
| 37 | if !s.mobileAuthorized(requestContext) { |
| 38 | return mobileSession{}, errMobileLocked |
| 39 | } |
| 40 | } |
| 41 | m := &s.mobileCLI |
| 42 | m.mu.Lock() |
| 43 | defer m.mu.Unlock() |
| 44 | record, err := m.record(s.profile, id) |
| 45 | if err != nil { |
| 46 | return mobileSession{}, err |
| 47 | } |
| 48 | if record.Deleted { |
| 49 | return mobileSession{}, os.ErrNotExist |
| 50 | } |
| 51 | previous := *record |
| 52 | if input.Title != nil { |
| 53 | record.CustomTitle = *input.Title |
| 54 | } |
| 55 | if input.Pinned != nil { |
| 56 | record.Pinned = *input.Pinned |
| 57 | } |
| 58 | record.MetadataSet = true |
| 59 | record.changed() |
| 60 | if err := writePrivate(mobileRecordPath(s.profile, id), record); err != nil { |
| 61 | *record = previous |
| 62 | return mobileSession{}, err |
| 63 | } |
| 64 | if m.catalog != nil { |
| 65 | m.catalog[id] = record.summary() |
| 66 | } |
| 67 | return record.snapshot(), nil |
| 68 | } |
| 69 | |
| 70 | |
| 71 | func (s *Server) setSessionArchived(id string, archived bool, requestContext context.Context) error { |
| 72 | if requestContext != nil && requestContext.Err() != nil { |
| 73 | return requestContext.Err() |
| 74 | } |
| 75 | |
| 76 | |
| 77 | s.op.Lock() |
| 78 | defer s.op.Unlock() |
| 79 | if requestContext != nil && requestContext.Err() != nil { |
| 80 | return requestContext.Err() |
| 81 | } |
| 82 | s.conversationMu.Lock() |
| 83 | defer s.conversationMu.Unlock() |
| 84 | m := &s.mobileCLI |
| 85 | if requestContext != nil { |
| 86 | s.account.mu.Lock() |
| 87 | if !s.mobileAuthorized(requestContext) { |
| 88 | s.account.mu.Unlock() |
| 89 | return errMobileLocked |
| 90 | } |
| 91 | } |
| 92 | m.mu.Lock() |
| 93 | record, err := m.record(s.profile, id) |
| 94 | if err != nil || record.Deleted { |
| 95 | m.mu.Unlock() |
| 96 | if requestContext != nil { |
| 97 | s.account.mu.Unlock() |
| 98 | } |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | return os.ErrNotExist |
| 103 | } |
| 104 | previous := *record |
| 105 | if record.Deleting && !archived { |
| 106 | m.mu.Unlock() |
| 107 | if requestContext != nil { |
| 108 | s.account.mu.Unlock() |
| 109 | } |
| 110 | return errors.New("永久删除尚未完成,请先重试删除") |
| 111 | } |
| 112 | record.Archived = archived |
| 113 | record.changed() |
| 114 | if err := writePrivate(mobileRecordPath(s.profile, id), record); err != nil { |
| 115 | *record = previous |
| 116 | m.mu.Unlock() |
| 117 | if requestContext != nil { |
| 118 | s.account.mu.Unlock() |
| 119 | } |
| 120 | return err |
| 121 | } |
| 122 | if m.catalog != nil { |
| 123 | m.catalog[id] = record.summary() |
| 124 | } |
| 125 | var done chan struct{} |
| 126 | if run := m.active[id]; archived && run != nil && run.cancel != nil { |
| 127 | run.cancel() |
| 128 | done = run.done |
| 129 | } |
| 130 | terminalID := record.TerminalID |
| 131 | m.mu.Unlock() |
| 132 | if requestContext != nil { |
| 133 | s.account.mu.Unlock() |
| 134 | } |
| 135 | if terminal := s.terminals.get(terminalID); archived && terminal != nil { |
| 136 | if terminal.summary()["cli_session_id"] == id { |
| 137 | s.terminals.remove(terminalID) |
| 138 | } |
| 139 | } |
| 140 | if done != nil { |
| 141 | <-done |
| 142 | } |
| 143 | return nil |
| 144 | } |
| 145 | |
| 146 | func (s *Server) mobileSessionMetadata(w http.ResponseWriter, r *http.Request) { |
| 147 | view, _, ok := s.authorizedMobileSessionRecord(w, r) |
| 148 | if !ok { |
| 149 | return |
| 150 | } |
| 151 | var input sessionMetadataInput |
| 152 | if !readJSON(w, r, &input) { |
| 153 | return |
| 154 | } |
| 155 | updated, err := s.updateSessionMetadata(view.ID, input, r.Context()) |
| 156 | if err != nil { |
| 157 | requestError(w, err) |
| 158 | return |
| 159 | } |
| 160 | jsonReply(w, updated) |
| 161 | } |
| 162 | |
| 163 | func (s *Server) mobileArchiveSession(w http.ResponseWriter, r *http.Request) { |
| 164 | s.changeSessionArchive(w, r, true) |
| 165 | } |
| 166 | |
| 167 | func (s *Server) mobileRestoreSession(w http.ResponseWriter, r *http.Request) { |
| 168 | s.changeSessionArchive(w, r, false) |
| 169 | } |
| 170 | |
| 171 | func (s *Server) changeSessionArchive(w http.ResponseWriter, r *http.Request, archived bool) { |
| 172 | view, _, ok := s.authorizedMobileSessionRecord(w, r) |
| 173 | if !ok { |
| 174 | return |
| 175 | } |
| 176 | var input struct{} |
| 177 | if !readJSON(w, r, &input) { |
| 178 | return |
| 179 | } |
| 180 | if err := s.setSessionArchived(view.ID, archived, r.Context()); err != nil { |
| 181 | requestError(w, err) |
| 182 | return |
| 183 | } |
| 184 | jsonReply(w, map[string]bool{"ok": true}) |
| 185 | } |
| 186 | |
| 187 | func (s *Server) mobilePurgeSession(w http.ResponseWriter, r *http.Request) { |
| 188 | |
| 189 | var input struct { |
| 190 | Confirm bool `json:"confirm"` |
| 191 | } |
| 192 | if !readJSON(w, r, &input) { |
| 193 | return |
| 194 | } |
| 195 | if !input.Confirm { |
| 196 | http.Error(w, "需要确认永久删除该会话及 Claude Code 原始历史", 400) |
| 197 | return |
| 198 | } |
| 199 | if err := s.purgeSession(r.Context(), r.PathValue("id")); err != nil { |
| 200 | requestError(w, err) |
| 201 | return |
| 202 | } |
| 203 | jsonReply(w, map[string]bool{"ok": true}) |
| 204 | } |
| 205 | |
| 206 | func (s *Server) purgeSession(ctx context.Context, id string) error { |
| 207 | if !s.op.TryLock() { |
| 208 | return errTerminalBusy |
| 209 | } |
| 210 | defer s.op.Unlock() |
| 211 | s.conversationMu.Lock() |
| 212 | defer s.conversationMu.Unlock() |
| 213 | s.account.mu.Lock() |
| 214 | authorized := s.mobileAuthorized(ctx) |
| 215 | s.account.mu.Unlock() |
| 216 | if !authorized { |
| 217 | return errMobileLocked |
| 218 | } |
| 219 | m := &s.mobileCLI |
| 220 | m.mu.Lock() |
| 221 | record, err := m.record(s.profile, id) |
| 222 | m.mu.Unlock() |
| 223 | if err != nil { |
| 224 | return err |
| 225 | } |
| 226 | if _, err := s.projectWorkspace(record.Workspace); err != nil { |
| 227 | return err |
| 228 | } |
| 229 | if record.Purged { |
| 230 | return nil |
| 231 | } |
| 232 | if s.windowsGuest != nil && !record.Terminal && (record.Resume || len(record.Turns) > 0) { |
| 233 | return errWindowsLegacyMobile |
| 234 | } |
| 235 | if !record.Archived { |
| 236 | return errors.New("请先归档会话,再从已归档会话中永久删除") |
| 237 | } |
| 238 | |
| 239 | |
| 240 | record.Deleting = true |
| 241 | record.changed() |
| 242 | m.mu.Lock() |
| 243 | if err := writePrivate(mobileRecordPath(s.profile, id), record); err != nil { |
| 244 | m.mu.Unlock() |
| 245 | return err |
| 246 | } |
| 247 | if m.catalog != nil { |
| 248 | m.catalog[id] = record.summary() |
| 249 | } |
| 250 | m.mu.Unlock() |
| 251 | var result struct { |
| 252 | OK bool `json:"ok"` |
| 253 | Error string `json:"error"` |
| 254 | } |
| 255 | executions := append([]claudeExecution{}, record.Executions...) |
| 256 | executions = append(executions, claudeExecution{record.CLIID, record.Account, record.GuestConfig}) |
| 257 | for _, execution := range executions { |
| 258 | input := map[string]any{ |
| 259 | "cli_id": execution.CLIID, "config": execution.Config, |
| 260 | "account": execution.Account.GuestID, "workspace": guestPath(record.Workspace), |
| 261 | "expect_history": execution.CLIID != record.CLIID || record.Resume, |
| 262 | } |
| 263 | if err := s.windowsConversationInput(record, input); err != nil { |
| 264 | return err |
| 265 | } |
| 266 | if err := s.runCLIChannel(ctx, "purge", input, &result); err != nil { |
| 267 | return err |
| 268 | } |
| 269 | if !result.OK { |
| 270 | return errors.New(result.Error) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | |
| 275 | tombstone := &mobileRecord{mobileSession: mobileSession{mobileSummary: mobileSummary{ |
| 276 | ID: record.ID, Workspace: record.Workspace, Deleted: true, Purged: true, |
| 277 | Revision: record.Revision, Generation: record.Generation, |
| 278 | }, Turns: []mobileTurn{}}, CLIID: record.CLIID} |
| 279 | for _, execution := range record.Executions { |
| 280 | tombstone.Executions = append(tombstone.Executions, claudeExecution{CLIID: execution.CLIID}) |
| 281 | } |
| 282 | tombstone.changed() |
| 283 | m.mu.Lock() |
| 284 | defer m.mu.Unlock() |
| 285 | if err := writePrivate(mobileRecordPath(s.profile, id), tombstone); err != nil { |
| 286 | return err |
| 287 | } |
| 288 | delete(m.active, id) |
| 289 | if m.catalog != nil { |
| 290 | m.catalog[id] = tombstone.summary() |
| 291 | } |
| 292 | return nil |
| 293 | } |
| 294 | |
| 295 | func (s *Server) terminalSessionMetadata(w http.ResponseWriter, r *http.Request) { |
| 296 | terminal := s.terminals.get(r.PathValue("id")) |
| 297 | if terminal == nil { |
| 298 | http.NotFound(w, r) |
| 299 | return |
| 300 | } |
| 301 | id, _ := terminal.summary()["cli_session_id"].(string) |
| 302 | if id == "" { |
| 303 | http.Error(w, "此终端没有共享 Claude Code 会话", 409) |
| 304 | return |
| 305 | } |
| 306 | var input sessionMetadataInput |
| 307 | if !readJSON(w, r, &input) { |
| 308 | return |
| 309 | } |
| 310 | view, err := s.updateSessionMetadata(id, input, nil) |
| 311 | if err != nil { |
| 312 | requestError(w, err) |
| 313 | return |
| 314 | } |
| 315 | jsonReply(w, view) |
| 316 | } |
| 317 | |
| 318 | func (s *Server) terminalSummaries() []map[string]any { |
| 319 | list := s.terminals.snapshot() |
| 320 | m := &s.mobileCLI |
| 321 | m.mu.Lock() |
| 322 | defer m.mu.Unlock() |
| 323 | for _, item := range list { |
| 324 | id, _ := item["cli_session_id"].(string) |
| 325 | if id == "" { |
| 326 | continue |
| 327 | } |
| 328 | record, err := m.record(s.profile, id) |
| 329 | if err != nil { |
| 330 | continue |
| 331 | } |
| 332 | item["custom_title"], item["pinned"], item["metadata_set"] = record.CustomTitle, record.Pinned, record.MetadataSet |
| 333 | item["title"] = record.snapshot().Title |
| 334 | if s.macOSLegacyWorkspace(record) != "" { |
| 335 | item["workspace_notice"] = macOSLegacyWorkspaceNotice |
| 336 | } |
| 337 | } |
| 338 | return list |
| 339 | } |