返回 DeepSeek-Reasonix
remoteSessionCache.ts
根目录 / desktop / frontend / src / lib / remoteSessionCache.ts
1 import type { RemoteSessionView } from "./types";
2
3 const REMOTE_SESSIONS_CACHE_PREFIX = "projectTree:remoteSessions:";
4 const REMOTE_SESSION_CACHE_VERSION = 1;
5 export const REMOTE_SESSION_CACHE_TTL_MS = 5 * 60 * 1000;
6
7 interface RemoteSessionCacheEnvelope {
8 version: typeof REMOTE_SESSION_CACHE_VERSION;
9 savedAt: number;
10 rows: RemoteSessionView[];
11 }
12
13 function cacheKey(groupKey: string): string {
14 return REMOTE_SESSIONS_CACHE_PREFIX + groupKey;
15 }
16
17 function parseRows(value: unknown): RemoteSessionView[] | undefined {
18 if (!Array.isArray(value)) return undefined;
19 const rows: RemoteSessionView[] = [];
20 for (const row of value) {
21 if (!row || typeof row !== "object") return undefined;
22 const candidate = row as Partial<RemoteSessionView>;
23 if (typeof candidate.name !== "string") return undefined;
24 const valid = (candidate.title === undefined || typeof candidate.title === "string")
25 && (candidate.turns === undefined || typeof candidate.turns === "number" && Number.isFinite(candidate.turns))
26 && (candidate.path === undefined || typeof candidate.path === "string")
27 && (candidate.current === undefined || typeof candidate.current === "boolean")
28 && (candidate.running === undefined || typeof candidate.running === "boolean")
29 && (candidate.resultSequence === undefined || Number.isSafeInteger(candidate.resultSequence) && candidate.resultSequence >= 0)
30 && (candidate.metadataReady === undefined || typeof candidate.metadataReady === "boolean")
31 && (candidate.lastActivityAt === undefined
32 || typeof candidate.lastActivityAt === "number" && Number.isFinite(candidate.lastActivityAt))
33 && (candidate.pinned === undefined || typeof candidate.pinned === "boolean");
34 if (!valid) return undefined;
35 rows.push({
36 ...candidate,
37 name: candidate.name,
38 title: candidate.title ?? "",
39 turns: candidate.turns ?? 0,
40 });
41 }
42 return rows;
43 }
44
45 export function removeRemoteSessionCache(groupKey: string): void {
46 try {
47 if (typeof localStorage !== "undefined") localStorage.removeItem(cacheKey(groupKey));
48 } catch {
49 // localStorage is an optional optimistic-paint cache.
50 }
51 }
52
53 export function saveRemoteSessionCache(groupKey: string, rows: RemoteSessionView[], now = Date.now()): void {
54 try {
55 if (typeof localStorage === "undefined") return;
56 const durableRows = rows.filter((row) => row.name.trim() !== "");
57 const envelope: RemoteSessionCacheEnvelope = {
58 version: REMOTE_SESSION_CACHE_VERSION,
59 savedAt: now,
60 rows: durableRows,
61 };
62 localStorage.setItem(cacheKey(groupKey), JSON.stringify(envelope));
63 } catch {
64 // localStorage can be unavailable or full; live state remains authoritative.
65 }
66 }
67
68 export function loadRemoteSessionCache(groupKey: string, now = Date.now()): RemoteSessionView[] {
69 try {
70 if (typeof localStorage === "undefined") return [];
71 const raw = localStorage.getItem(cacheKey(groupKey));
72 if (!raw) return [];
73 const parsed = JSON.parse(raw) as Partial<RemoteSessionCacheEnvelope> | null;
74 const rows = parseRows(parsed?.rows);
75 const fresh = parsed?.version === REMOTE_SESSION_CACHE_VERSION
76 && typeof parsed.savedAt === "number"
77 && parsed.savedAt <= now
78 && now - parsed.savedAt <= REMOTE_SESSION_CACHE_TTL_MS
79 && rows !== undefined;
80 if (!fresh) {
81 removeRemoteSessionCache(groupKey);
82 return [];
83 }
84 return rows;
85 } catch {
86 removeRemoteSessionCache(groupKey);
87 return [];
88 }
89 }
90
90 lines TYPESCRIPT