(sessionId, serverLastSeq)
| 160 | } |
| 161 | |
| 162 | async function syncSessionState(sessionId, serverLastSeq) { |
| 163 | const syncToken = ++S.sessionSyncToken; |
| 164 | const prevSessionId = S.sessionId; |
| 165 | const nextSessionId = sessionId || ''; |
| 166 | const sessionChanged = nextSessionId !== prevSessionId; |
| 167 | debugLog('sync_session_start', { |
| 168 | syncToken, |
| 169 | prevSessionId: prevSessionId || null, |
| 170 | nextSessionId: nextSessionId || null, |
| 171 | sessionChanged, |
| 172 | serverLastSeq, |
| 173 | waiting: S.waiting, |
| 174 | lastSeq: S.lastSeq, |
| 175 | wsState: wsReadyStateName(S.ws), |
| 176 | hidden: typeof document !== 'undefined' ? !!document.hidden : null, |
| 177 | online: typeof navigator !== 'undefined' && 'onLine' in navigator ? !!navigator.onLine : null, |
| 178 | }); |
| 179 | S.replaying = true; |
| 180 | |
| 181 | if (sessionChanged) { |
| 182 | S.sessionId = nextSessionId; |
| 183 | S.model = ''; |
| 184 | const shouldKeepOptimisticUi = !prevSessionId && hasOptimisticBubble(); |
| 185 | if (shouldKeepOptimisticUi) { |
| 186 | debugLog('sync_session_keep_optimistic', { |
| 187 | syncToken, |
| 188 | nextSessionId: nextSessionId || null, |
| 189 | }); |
| 190 | rebuildRuntimeStateFromDom(); |
| 191 | updateHeaderInfo(); |
| 192 | scheduleSessionCacheSave(); |
| 193 | } else { |
| 194 | debugLog('sync_session_clear_ui', { |
| 195 | syncToken, |
| 196 | nextSessionId: nextSessionId || null, |
| 197 | }); |
| 198 | clearConversationUi(); |
| 199 | } |
| 200 | if (nextSessionId && !shouldKeepOptimisticUi) { |
| 201 | const restored = await restoreSessionCache(nextSessionId); |
| 202 | if (!restored) updateHeaderInfo(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (syncToken !== S.sessionSyncToken) return; |
| 207 | |
| 208 | if (!S.ws || S.ws.readyState !== WebSocket.OPEN) return; |
| 209 | if (S.resumeRequestedFor === nextSessionId) return; |
| 210 | |
| 211 | S.resumeRequestedFor = nextSessionId; |
| 212 | debugLog('sync_session_resume_request', { |
| 213 | syncToken, |
| 214 | sessionId: nextSessionId || null, |
| 215 | lastSeq: nextSessionId ? S.lastSeq : 0, |
| 216 | serverLastSeq: Number.isInteger(serverLastSeq) ? serverLastSeq : null, |
| 217 | wsState: wsReadyStateName(S.ws), |
| 218 | hidden: typeof document !== 'undefined' ? !!document.hidden : null, |
| 219 | online: typeof navigator !== 'undefined' && 'onLine' in navigator ? !!navigator.onLine : null, |
no test coverage detected