Improve session migration: scan ALL localStorage caches to handle workspace ID changes
Some checks failed
Release Binaries / release (push) Has been cancelled
Some checks failed
Release Binaries / release (push) Has been cancelled
This commit is contained in:
@@ -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
|
* Clear cached SDK sessions after successful migration
|
||||||
*/
|
*/
|
||||||
@@ -199,8 +232,23 @@ export async function autoImportCachedSessions(workspaceId: string): Promise<Mig
|
|||||||
return { success: true, imported: 0, skipped: 0 }
|
return { success: true, imported: 0, skipped: 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get cached sessions from localStorage
|
// First, try to get cached sessions for this specific workspace ID
|
||||||
const cachedSessions = getCachedSDKSessions(workspaceId)
|
let cachedSessions = getCachedSDKSessions(workspaceId)
|
||||||
|
|
||||||
|
// If no sessions found for this workspace, check ALL cached sessions
|
||||||
|
// This handles the case where workspace IDs changed (e.g., after the deterministic ID fix)
|
||||||
|
if (cachedSessions.length === 0) {
|
||||||
|
const allCached = getAllCachedSessions()
|
||||||
|
if (allCached.length > 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) {
|
if (cachedSessions.length === 0) {
|
||||||
// Also check in-memory sessions as a fallback
|
// Also check in-memory sessions as a fallback
|
||||||
|
|||||||
Reference in New Issue
Block a user