返回 DeepSeek-Reasonix
resume_picker.go
根目录 / internal / cli / resume_picker.go
1 package cli
2
3 import (
4 "errors"
5 "fmt"
6
7 tea "charm.land/bubbletea/v2"
8 "github.com/charmbracelet/x/ansi"
9
10 "reasonix/internal/agent"
11 "reasonix/internal/i18n"
12 "reasonix/internal/session"
13 )
14
15 // resumePicker is an in-chat overlay for "/resume" that lets the user pick a
16 // saved session by navigating with ↑/↓ and confirming with Enter. It mirrors
17 // the rewindPicker pattern: keys route through handleResumePickerKey and it
18 // renders via renderResumePicker while m.resumePick is set.
19 type resumePicker struct {
20 entries []resumeEntry
21 sel int // selected index
22 active int // index of the currently-active session (-1 when none)
23 quick *quickPicker
24 }
25
26 // openResumePicker populates the picker from the session directory and opens it.
27 // A no-op (with a notice) when there are no saved sessions.
28 func (m *chatTUI) openResumePicker() {
29 reclaimCLIRecoveryBranches(m.ctrl.SessionDir())
30 entries := resumeEntries(m.ctrl.SessionDir())
31 if len(entries) == 0 {
32 m.notice(i18n.M.NoSessionToResume)
33 return
34 }
35 active := m.ctrl.SessionPath()
36 activeIdx := -1
37 reclaimedIdx := -1
38 for i, entry := range entries {
39 if entry.session.Path == active || resumeEntryIsActive(m.ctrl, entry) {
40 activeIdx = i
41 }
42 if resumeTargetMatches(m.reclaimedTarget, entry) {
43 reclaimedIdx = i
44 }
45 }
46 // Default selection: the first session after the active one, else 0.
47 sel := 0
48 if activeIdx >= 0 && activeIdx+1 < len(entries) {
49 sel = activeIdx + 1
50 }
51 if m.sessionReclaimed && reclaimedIdx >= 0 && len(entries) > 1 {
52 sel = reclaimedIdx + 1
53 if sel >= len(entries) {
54 sel = 0
55 }
56 }
57 items := make([]quickPickerItem, 0, len(entries))
58 for i, entry := range entries {
59 status := ""
60 switch i {
61 case activeIdx:
62 status = "active"
63 case reclaimedIdx:
64 status = "taken back"
65 }
66 label := sessionPickerLabel(entry.session)
67 description := entry.session.ModTime.Local().Format("2006-01-02 15:04")
68 if entry.project != "" {
69 label = fmt.Sprintf("[%s] %s", entry.project, label)
70 description = entry.project + " · " + description
71 }
72 items = append(items, quickPickerItem{
73 ID: entry.session.Path, Label: label,
74 Description: description, Status: status,
75 })
76 }
77 m.resumePick = &resumePicker{
78 entries: entries, sel: sel, active: activeIdx,
79 quick: &quickPicker{kind: quickPickerResume, title: i18n.M.ResumePickTitle, items: items, selected: sel},
80 }
81 }
82
83 func resumeTargetMatches(target cliResumeTarget, entry resumeEntry) bool {
84 if target.canonical() || entry.target.canonical() {
85 return target.canonical() && entry.target.canonical() && target.ref == entry.target.ref
86 }
87 return target.path != "" && agent.CanonicalSessionPath(target.path) == agent.CanonicalSessionPath(entry.session.Path)
88 }
89
90 func (m chatTUI) handleResumePickerKey(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
91 r := m.resumePick
92 if r == nil {
93 return m, nil
94 }
95 if r.quick != nil {
96 result := r.quick.handleKey(msg)
97 r.sel = r.quick.selected
98 if result.cancelled {
99 m.resumePick = nil
100 return m, nil
101 }
102 if result.choice != nil {
103 for i, entry := range r.entries {
104 if entry.session.Path == result.choice.ID {
105 r.sel = i
106 break
107 }
108 }
109 return m.applyResumePick()
110 }
111 return m, nil
112 }
113 switch msg.String() {
114 case "up", "k":
115 if r.sel > 0 {
116 r.sel--
117 }
118 case "down", "j":
119 if r.sel < len(r.entries)-1 {
120 r.sel++
121 }
122 case "enter":
123 return m.applyResumePick()
124 case "esc":
125 m.resumePick = nil
126 }
127 return m, nil
128 }
129
130 func (m chatTUI) applyResumePick() (tea.Model, tea.Cmd) {
131 r := m.resumePick
132 if r == nil || r.sel < 0 || r.sel >= len(r.entries) {
133 return m, nil
134 }
135 target := r.entries[r.sel]
136 m.resumePick = nil
137 if resumeEntryIsActive(m.ctrl, target) {
138 m.notice(i18n.M.ResumeAlreadyActive)
139 return m, nil
140 }
141 if m.ctrl.Running() {
142 m.notice(i18n.M.ResumeBusy)
143 return m, nil
144 }
145 detached := m.sessionReclaimed || m.takeover != nil && m.takeover.Returned()
146 if !detached {
147 // Snapshot before moving the lease: the outgoing session must be written
148 // while this process still owns it.
149 if err := m.ctrl.Snapshot(); err != nil {
150 m.notice("resume: snapshot current session: " + err.Error())
151 return m, nil
152 }
153 m.followSessionLease()
154 }
155 if target.target.canonical() {
156 if err := m.commitCanonicalSessionSwitch(target.target.ref); err != nil {
157 if !detached {
158 m.restoreSessionLease()
159 }
160 if errors.Is(err, session.ErrWriterOwned) {
161 m.pendingTakeoverPath = cliCanonicalRoute(target.target.ref.SessionID)
162 m.notice("resume: " + sessionWriterHeldNotice())
163 m.notice("run /takeover to take this session over")
164 return m, nil
165 }
166 m.notice("resume: " + err.Error())
167 return m, nil
168 }
169 } else if err := m.commitSessionSwitch(target.session.Path); err != nil {
170 m.notice("resume: " + sessionLeaseHeldNotice(err))
171 if cliSessionTakeoverCandidate(err) {
172 m.pendingTakeoverPath = target.session.Path
173 m.notice("run /takeover to take this session over")
174 }
175 return m, nil
176 }
177 m.resumeAfterReclaim()
178 m.replayActiveBranch(i18n.M.ResumedTitle)
179 return m, nil
180 }
181
182 func (m chatTUI) renderResumePicker() string {
183 return m.renderResumePickerPanel().text
184 }
185
186 func (m chatTUI) renderResumePickerPanel() panelContent {
187 r := m.resumePick
188 if r == nil {
189 return panelContent{}
190 }
191 if r.quick != nil {
192 return r.quick.renderPanel(m.width)
193 }
194 w := max(m.width, 10)
195 var b panelBuilder
196 b.WriteString(accent(i18n.M.ResumePickTitle) + "\n")
197 for i, entry := range r.entries {
198 label := sessionPickerLabel(entry.session)
199 if entry.project != "" {
200 label = fmt.Sprintf("[%s] %s", entry.project, label)
201 }
202 if i == r.active {
203 label = dim(label) + " " + dim("(active)")
204 }
205 b.MarkFocus(i == r.sel, i)
206 b.WriteString(rowLine(i == r.sel, i+1, "", label, false) + "\n")
207 }
208 b.WriteString(dim(i18n.M.ResumePickHint))
209 return b.render(choicePanelStyle.Width(w), m.width)
210 }
211
212 // sessionPickerLabel is the "N turns · display title" line, truncated to fit.
213 // Explicit session renames win, then topic titles, then the raw preview.
214 func sessionPickerLabel(s agent.SessionInfo) string {
215 preview := s.CustomTitle
216 if preview == "" {
217 preview = s.TopicTitle
218 }
219 if preview == "" {
220 preview = s.Preview
221 }
222 if preview == "" {
223 preview = "(no user message yet)"
224 }
225 return recoverySessionBadge(s) + fmt.Sprintf("%d turns · %s", s.Turns, ansi.Truncate(preview, 60, "…"))
226 }
227
227 lines GO