Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 1,857 Bytes
962191f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | /**
* localStorage cache of raw backend (litellm Message) dicts keyed by
* session ID. Used to restore a session into a fresh backend after the
* Space restarts — the browser-side UIMessages are what the user sees,
* but the LLM needs the backend format to continue the conversation.
*/
import { logger } from '@/utils/logger';
const STORAGE_KEY = 'hf-agent-backend-messages';
const MAX_SESSIONS = 50;
type MessagesMap = Record<string, unknown[]>;
function readAll(): MessagesMap {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return {};
const parsed = JSON.parse(raw);
if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {
return parsed as MessagesMap;
}
return {};
} catch {
return {};
}
}
function writeAll(map: MessagesMap): void {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(map));
} catch (e) {
// Quota exceeded is the most common reason — the cache is best-effort.
logger.warn('Failed to persist backend messages:', e);
}
}
export function loadBackendMessages(sessionId: string): unknown[] {
const map = readAll();
return map[sessionId] ?? [];
}
export function saveBackendMessages(sessionId: string, messages: unknown[]): void {
const map = readAll();
map[sessionId] = messages;
const keys = Object.keys(map);
if (keys.length > MAX_SESSIONS) {
const toRemove = keys.slice(0, keys.length - MAX_SESSIONS);
for (const k of toRemove) delete map[k];
}
writeAll(map);
}
export function moveBackendMessages(fromId: string, toId: string): void {
const map = readAll();
if (!map[fromId]) return;
map[toId] = map[fromId];
delete map[fromId];
writeAll(map);
}
export function deleteBackendMessages(sessionId: string): void {
const map = readAll();
delete map[sessionId];
writeAll(map);
}
|