Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 1,335 Bytes
ccfb504 fdc5e27 ccfb504 | 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 | /**
* Persist research sub-agent state (steps + stats) per session.
* Survives page refresh so the rolling display isn't lost mid-research.
*/
import type { PerSessionState } from '@/store/agentStore';
/** Max steps to keep in storage and display. Single source of truth. */
export const RESEARCH_MAX_STEPS = 4;
const STORAGE_KEY = 'hf-agent-research';
type ResearchState = {
steps: string[];
stats: PerSessionState['researchStats'];
};
type ResearchMap = Record<string, ResearchState>;
function readAll(): ResearchMap {
try {
const raw = localStorage.getItem(STORAGE_KEY);
return raw ? JSON.parse(raw) : {};
} catch {
return {};
}
}
function writeAll(map: ResearchMap): void {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(map));
} catch { /* quota exceeded — ignore */ }
}
export function saveResearch(
sessionId: string,
steps: string[],
stats: PerSessionState['researchStats'],
): void {
const map = readAll();
map[sessionId] = {
steps: steps.slice(-RESEARCH_MAX_STEPS),
stats,
};
writeAll(map);
}
export function loadResearch(sessionId: string): ResearchState | null {
const map = readAll();
return map[sessionId] ?? null;
}
export function clearResearch(sessionId: string): void {
const map = readAll();
delete map[sessionId];
writeAll(map);
}
|