62159
Sy
symb / files
symb / internal / tui / update_undo.go
3.6 KB
1package tui 2 3import ( 4 "context" 5 "errors" 6 7 tea "charm.land/bubbletea/v2" 8 "github.com/rs/zerolog/log" 9) 10 11// demoteOldUndo finds the existing entryUndo in convEntries and removes it. 12// The entrySeparator line above it is already present and stays. 13// Called before appending a new undo entry so only the latest turn shows the undo control. 14func (m *Model) demoteOldUndo() { 15 for i := len(m.convEntries) - 1; i >= 0; i-- { 16 if m.convEntries[i].kind == entryUndo { 17 m.convEntries = append(m.convEntries[:i], m.convEntries[i+1:]...) 18 return 19 } 20 } 21} 22 23// trimOldTurns drops the oldest display turns when we exceed maxDisplayTurns. 24// Messages live in the DB — this only trims display entries to bound rendering cost. 25func (m *Model) trimOldTurns() { 26 for len(m.turnBoundaries) > maxDisplayTurns { 27 cutConv := m.turnBoundaries[1].convIdx 28 29 m.convEntries = m.convEntries[cutConv:] 30 m.turnBoundaries = m.turnBoundaries[1:] 31 32 for i := range m.turnBoundaries { 33 m.turnBoundaries[i].convIdx -= cutConv 34 } 35 } 36 37 m.frameLines = nil 38 lines := m.wrappedConvLines() 39 convH := m.layout.conv.Dy() 40 maxScroll := len(lines) - convH 41 if maxScroll < 0 { 42 maxScroll = 0 43 } 44 if m.scrollOffset > maxScroll { 45 m.scrollOffset = maxScroll 46 } 47} 48 49// handleUndo reverts the most recent turn: restores files, truncates history 50// and convEntries, and cleans up the database. 51func (m *Model) handleUndo() (Model, tea.Cmd) { 52 if m.streaming || len(m.turnBoundaries) == 0 { 53 return *m, nil 54 } 55 if m.undoInFlight { 56 return *m, nil 57 } 58 59 tb := m.turnBoundaries[len(m.turnBoundaries)-1] 60 m.turnBoundaries = m.turnBoundaries[:len(m.turnBoundaries)-1] 61 62 // Restore token totals to the snapshot at turn start. 63 m.totalInputTokens = tb.inputTokens 64 m.totalOutputTokens = tb.outputTokens 65 m.turnInputTokens = 0 66 m.turnOutputTokens = 0 67 68 // 1. Truncate display entries. 69 m.convEntries = m.convEntries[:tb.convIdx] 70 71 // 6. Promote previous turn's demoted separator back to undo, if any. 72 if len(m.turnBoundaries) > 0 { 73 for i := len(m.convEntries) - 1; i >= 0; i-- { 74 if m.convEntries[i].kind == entrySeparator { 75 // Replace separator with separator+undo pair. 76 entries := m.makeUndoEntry(m.convEntries[i].full) 77 // Replace the separator entry in-place and insert undo after it. 78 m.convEntries[i] = entries[0] 79 m.convEntries = append(m.convEntries[:i+1], append([]convEntry{entries[1]}, m.convEntries[i+1:]...)...) 80 break 81 } 82 } 83 } 84 85 // 7. Reset streaming state. 86 m.streaming = false 87 m.streamEntryStart = -1 88 m.streamingReasoning = "" 89 m.streamingContent = "" 90 91 // 8. Scroll to bottom. 92 m.scrollOffset = 0 93 94 m.undoInFlight = true 95 cmd := m.undoSideEffectsCmd(tb.dbMsgID) 96 return *m, cmd 97} 98 99func (m *Model) undoSideEffectsCmd(dbMsgID int64) tea.Cmd { 100 tracker := m.deltaTracker 101 store := m.store 102 fileTracker := m.fileTracker 103 tsIndex := m.tsIndex 104 sessionID := m.sessionID 105 return func() tea.Msg { 106 var undoErr error 107 var restoredFiles []string 108 if tracker != nil && dbMsgID > 0 { 109 restoredFiles, undoErr = tracker.Undo(sessionID, dbMsgID) 110 tracker.DeleteTurn(sessionID, dbMsgID) 111 } 112 if store != nil && dbMsgID > 0 { 113 if err := store.DeleteMessagesFrom(sessionID, dbMsgID); err != nil { 114 log.Warn().Err(err).Msg("undo: failed to delete messages") 115 } 116 } 117 if fileTracker != nil { 118 fileTracker.Reset() 119 } 120 if tsIndex != nil { 121 for _, f := range restoredFiles { 122 tsIndex.UpdateFile(f) 123 } 124 } 125 return undoResultMsg{err: undoErr} 126 } 127} 128 129func (m Model) handleUndoResult(msg undoResultMsg) Model { 130 m.undoInFlight = false 131 if msg.err == nil || errors.Is(msg.err, context.Canceled) { 132 return m 133 } 134 m.appendText("", m.styles.Error.Render("undo file restore failed: "+msg.err.Error()), "") 135 return m 136}