export const MAX_RECENT_SUBSPACES = 10; const RECENT_STORAGE_KEY_PREFIX = "tsagent.kbSubspaces.v2"; const ACTIVE_STORAGE_KEY_PREFIX = "tsagent.kbActiveSubspace.v2"; type SubspaceStorage = Pick; function normalizeSubspace(value: unknown): string | null { if (typeof value !== "string") return null; const normalized = value.trim(); return normalized || null; } export function getRecentSubspaceStorageKey( dataScopeKey: string, knowledgeBaseName: string, ): string { return `${RECENT_STORAGE_KEY_PREFIX}:${JSON.stringify([ dataScopeKey, knowledgeBaseName, ])}`; } export function getActiveSubspaceStorageKey( dataScopeKey: string, knowledgeBaseName: string, ): string { return `${ACTIVE_STORAGE_KEY_PREFIX}:${JSON.stringify([ dataScopeKey, knowledgeBaseName, ])}`; } export function readRecentSubspaces( storage: Pick, dataScopeKey: string, knowledgeBaseName: string, ): string[] { try { const raw = storage.getItem( getRecentSubspaceStorageKey(dataScopeKey, knowledgeBaseName), ); if (!raw) return []; const parsed = JSON.parse(raw) as unknown; if (!Array.isArray(parsed)) return []; const seen = new Set(); return parsed.reduce((recent, item) => { const subspace = normalizeSubspace(item); if ( !subspace || seen.has(subspace) || recent.length >= MAX_RECENT_SUBSPACES ) { return recent; } seen.add(subspace); recent.push(subspace); return recent; }, []); } catch { return []; } } export function rememberRecentSubspace( storage: Pick, dataScopeKey: string, knowledgeBaseName: string, value: unknown, ): string[] { const subspace = normalizeSubspace(value); const recent = readRecentSubspaces(storage, dataScopeKey, knowledgeBaseName); if (!subspace) return recent; const next = [subspace, ...recent.filter((item) => item !== subspace)].slice( 0, MAX_RECENT_SUBSPACES, ); try { storage.setItem( getRecentSubspaceStorageKey(dataScopeKey, knowledgeBaseName), JSON.stringify(next), ); } catch { // Browser storage availability must not block navigation. } return next; } export function clearRecentSubspaces( storage: Pick, dataScopeKey: string, knowledgeBaseName: string, ): void { try { storage.removeItem( getRecentSubspaceStorageKey(dataScopeKey, knowledgeBaseName), ); } catch { // Browser storage availability must not block navigation. } } export function readActiveSubspace( storage: Pick, dataScopeKey: string, knowledgeBaseName: string, ): string | null { try { return normalizeSubspace( storage.getItem( getActiveSubspaceStorageKey(dataScopeKey, knowledgeBaseName), ), ); } catch { return null; } } export function writeActiveSubspace( storage: Pick, dataScopeKey: string, knowledgeBaseName: string, value: unknown, ): void { const subspace = normalizeSubspace(value); if (!subspace) return; try { storage.setItem( getActiveSubspaceStorageKey(dataScopeKey, knowledgeBaseName), subspace, ); } catch { // Browser storage availability must not block navigation. } } export function clearActiveSubspace( storage: Pick, dataScopeKey: string, knowledgeBaseName: string, ): void { try { storage.removeItem( getActiveSubspaceStorageKey(dataScopeKey, knowledgeBaseName), ); } catch { // Browser storage availability must not block navigation. } }