diff --git a/packages/ui/src/stores/session-migration.ts b/packages/ui/src/stores/session-migration.ts index a488770..36664bc 100644 --- a/packages/ui/src/stores/session-migration.ts +++ b/packages/ui/src/stores/session-migration.ts @@ -79,6 +79,39 @@ export function getCachedSDKSessions(workspaceId: string): CachedSession[] { } } +/** + * Get ALL cached sessions from localStorage across all workspace IDs + * This is useful for migrating sessions when workspace IDs have changed + */ +export function getAllCachedSessions(): { workspaceId: string; sessions: CachedSession[] }[] { + const results: { workspaceId: string; sessions: CachedSession[] }[] = [] + + try { + for (let i = 0; i < localStorage.length; i++) { + const key = localStorage.key(i) + if (key && key.startsWith(SDK_SESSION_CACHE_PREFIX)) { + const workspaceId = key.substring(SDK_SESSION_CACHE_PREFIX.length) + const cached = localStorage.getItem(key) + if (cached) { + try { + const sessions = JSON.parse(cached) as CachedSession[] + if (sessions.length > 0) { + results.push({ workspaceId, sessions }) + log.info({ workspaceId, count: sessions.length }, "Found cached sessions") + } + } catch (e) { + // Invalid JSON, skip + } + } + } + } + } catch (error) { + log.error({ error }, "Failed to scan localStorage for cached sessions") + } + + return results +} + /** * Clear cached SDK sessions after successful migration */ @@ -199,8 +232,23 @@ export async function autoImportCachedSessions(workspaceId: string): Promise 0) { + log.info({ allCached: allCached.map(c => ({ id: c.workspaceId, count: c.sessions.length })) }, + "Found cached sessions from other workspace IDs, importing all") + + // Combine all cached sessions + for (const cache of allCached) { + cachedSessions = cachedSessions.concat(cache.sessions) + } + } + } if (cachedSessions.length === 0) { // Also check in-memory sessions as a fallback