| .. | .. |
|---|
| 98 | 98 | |
|---|
| 99 | 99 | String? get currentSessionId => _currentSessionId; |
|---|
| 100 | 100 | |
|---|
| 101 | | - /// Switch to a new session and load its messages. |
|---|
| 102 | | - Future<void> switchSession(String sessionId) async { |
|---|
| 103 | | - final trace = StackTrace.current.toString().split('\n').take(4).join(' | '); |
|---|
| 104 | | - TraceService.instance.addTrace( |
|---|
| 105 | | - 'switchSession', |
|---|
| 106 | | - 'from=${_currentSessionId?.substring(0, 8) ?? "null"}(${state.length}) → ${sessionId.substring(0, 8)} | $trace', |
|---|
| 107 | | - ); |
|---|
| 108 | | - // Write current session to disk |
|---|
| 109 | | - if (_currentSessionId != null && state.isNotEmpty) { |
|---|
| 110 | | - await MessageStore.writeDirect(_currentSessionId!, state); |
|---|
| 111 | | - } |
|---|
| 112 | | - |
|---|
| 113 | | - // Skip reload if staying on the same session — messages are already in memory |
|---|
| 101 | + /// Switch to a session. SYNCHRONOUS — no async gap, no race with incoming |
|---|
| 102 | + /// messages. MessageStoreV2.loadSession reads from the in-memory index. |
|---|
| 103 | + void switchSession(String sessionId) { |
|---|
| 114 | 104 | if (_currentSessionId == sessionId) { |
|---|
| 115 | | - TraceService.instance.addTrace('switchSession SKIP', 'already on ${sessionId.substring(0, 8)}'); |
|---|
| 105 | + TraceService.instance.addTrace( |
|---|
| 106 | + 'switchSession SKIP', 'already on ${sessionId.substring(0, 8)}'); |
|---|
| 116 | 107 | return; |
|---|
| 117 | 108 | } |
|---|
| 118 | | - |
|---|
| 109 | + TraceService.instance.addTrace( |
|---|
| 110 | + 'switchSession', |
|---|
| 111 | + 'from=${_currentSessionId?.substring(0, 8) ?? "null"}(${state.length}) → ${sessionId.substring(0, 8)}', |
|---|
| 112 | + ); |
|---|
| 119 | 113 | _currentSessionId = sessionId; |
|---|
| 120 | | - final messages = await MessageStore.loadAll(sessionId); |
|---|
| 121 | | - // Merge: if addMessage ran during loadAll and added messages for THIS session, |
|---|
| 122 | | - // they'll be in state but not in the loaded messages. Keep the longer list. |
|---|
| 123 | | - if (state.length > messages.length && _currentSessionId == sessionId) { |
|---|
| 124 | | - TraceService.instance.addTrace('switchSession MERGE', 'kept ${state.length} (loaded ${messages.length})'); |
|---|
| 125 | | - } else { |
|---|
| 126 | | - state = messages; |
|---|
| 127 | | - } |
|---|
| 114 | + state = MessageStoreV2.loadSession(sessionId); |
|---|
| 128 | 115 | } |
|---|
| 129 | 116 | |
|---|
| 130 | | - /// Add a message to the current session. |
|---|
| 117 | + /// Add a message to the current session (display + append-only persist). |
|---|
| 131 | 118 | void addMessage(Message message) { |
|---|
| 132 | 119 | state = [...state, message]; |
|---|
| 133 | 120 | if (_currentSessionId != null) { |
|---|
| 134 | | - MessageStore.save(_currentSessionId!, state); |
|---|
| 121 | + MessageStoreV2.append(_currentSessionId!, message); |
|---|
| 135 | 122 | } |
|---|
| 136 | 123 | } |
|---|
| 137 | 124 | |
|---|
| 138 | | - /// Update a message by ID. |
|---|
| 125 | + /// Update a message by ID (in-memory only — patch is not persisted to log). |
|---|
| 139 | 126 | void updateMessage(String id, Message Function(Message) updater) { |
|---|
| 140 | 127 | state = state.map((m) => m.id == id ? updater(m) : m).toList(); |
|---|
| 141 | | - if (_currentSessionId != null) { |
|---|
| 142 | | - MessageStore.save(_currentSessionId!, state); |
|---|
| 143 | | - } |
|---|
| 144 | 128 | } |
|---|
| 145 | 129 | |
|---|
| 146 | | - /// Remove a message by ID. |
|---|
| 130 | + /// Remove a message by ID (in-memory only). |
|---|
| 147 | 131 | void removeMessage(String id) { |
|---|
| 148 | 132 | state = state.where((m) => m.id != id).toList(); |
|---|
| 149 | | - if (_currentSessionId != null) { |
|---|
| 150 | | - MessageStore.save(_currentSessionId!, state); |
|---|
| 151 | | - } |
|---|
| 152 | 133 | } |
|---|
| 153 | 134 | |
|---|
| 154 | | - /// Remove all messages matching a predicate. |
|---|
| 135 | + /// Remove all messages matching a predicate (in-memory only). |
|---|
| 155 | 136 | void removeWhere(bool Function(Message) test) { |
|---|
| 156 | 137 | state = state.where((m) => !test(m)).toList(); |
|---|
| 157 | | - if (_currentSessionId != null) { |
|---|
| 158 | | - MessageStore.save(_currentSessionId!, state); |
|---|
| 159 | | - } |
|---|
| 160 | 138 | } |
|---|
| 161 | 139 | |
|---|
| 162 | | - /// Clear all messages for the current session. |
|---|
| 140 | + /// Clear all messages for the current session (in-memory only). |
|---|
| 163 | 141 | void clearMessages() { |
|---|
| 164 | 142 | state = []; |
|---|
| 165 | | - if (_currentSessionId != null) { |
|---|
| 166 | | - MessageStore.save(_currentSessionId!, state); |
|---|
| 167 | | - } |
|---|
| 168 | 143 | } |
|---|
| 169 | 144 | |
|---|
| 170 | 145 | void updateContent(String messageId, String content) { |
|---|
| .. | .. |
|---|
| 185 | 160 | else |
|---|
| 186 | 161 | m, |
|---|
| 187 | 162 | ]; |
|---|
| 188 | | - if (_currentSessionId != null) { |
|---|
| 189 | | - MessageStore.save(_currentSessionId!, state); |
|---|
| 190 | | - } |
|---|
| 191 | | - } |
|---|
| 192 | | - |
|---|
| 193 | | - /// Load more (older) messages for pagination. |
|---|
| 194 | | - Future<void> loadMore() async { |
|---|
| 195 | | - if (_currentSessionId == null) return; |
|---|
| 196 | | - final older = await MessageStore.load( |
|---|
| 197 | | - _currentSessionId!, |
|---|
| 198 | | - offset: state.length, |
|---|
| 199 | | - limit: 50, |
|---|
| 200 | | - ); |
|---|
| 201 | | - if (older.isNotEmpty) { |
|---|
| 202 | | - state = [...older, ...state]; |
|---|
| 203 | | - } |
|---|
| 204 | 163 | } |
|---|
| 205 | 164 | } |
|---|
| 206 | 165 | |
|---|