-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.js
More file actions
74 lines (64 loc) · 2.96 KB
/
Copy pathcache.js
File metadata and controls
74 lines (64 loc) · 2.96 KB
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
69
70
71
72
73
74
// Analysis cache + history — persists each completed scan in chrome.storage.local
// keyed by repo, so revisiting a repo loads instantly (no AI call) and Options can
// list everything analyzed. Lens results (session-only) and the raw README are
// not cached.
const PREFIX = 'rlcache:';
const LENS_KEYS = ['deepDive', 'systems', 'ideate', 'prioritize', 'sktpg', 'versus', 'synergies'];
export function cacheKey(platform, repoId) {
return `${PREFIX}${platform}:${repoId}`;
}
/** Store a completed analysis (trimmed of README + transient lens results). */
export async function cacheAnalysis(platform, repoId, data) {
const trimmed = { ...data };
delete trimmed.readme;
for (const k of LENS_KEYS) delete trimmed[k];
delete trimmed.loading;
delete trimmed.status;
trimmed.platform = platform;
trimmed.repoId = repoId;
trimmed.cachedAt = Date.now();
await chrome.storage.local.set({ [cacheKey(platform, repoId)]: trimmed });
}
export async function getCached(platform, repoId) {
const k = cacheKey(platform, repoId);
return (await chrome.storage.local.get(k))[k] || null;
}
/** All cached analyses, newest first. */
export async function listCached() {
const all = await chrome.storage.local.get(null);
return Object.entries(all)
.filter(([k]) => k.startsWith(PREFIX))
.map(([, v]) => v)
.sort((a, b) => (b.cachedAt || 0) - (a.cachedAt || 0));
}
export async function removeCached(platform, repoId) {
await chrome.storage.local.remove(cacheKey(platform, repoId));
}
/** Open a cached/saved analysis in a fresh output tab — instant, no AI call.
* Shared by Options' History list and the Library's cards. */
export async function openCachedAnalysis(analysis) {
const key = 'repolens_' + crypto.randomUUID();
await chrome.storage.session.set({ [key]: { ...analysis, cached: true, loading: false } });
chrome.tabs.create({ url: chrome.runtime.getURL(`output-tab.html?key=${key}`) });
}
const KNOWN_PLATFORMS = new Set(['github', 'gitlab', 'npm', 'pypi']);
/** Restore cached analyses from a backup. 'replace' clears the cache first;
* 'merge' (default) upserts by repo. Only entries for known platforms are
* accepted, so a crafted backup can't inject odd cache keys. Returns how many
* entries were written. */
export async function importCache(entries = [], { mode = 'merge' } = {}) {
if (mode === 'replace') await clearCache();
const valid = (entries || []).filter((c) => c && c.repoId && KNOWN_PLATFORMS.has(c.platform));
const obj = {};
for (const c of valid) obj[cacheKey(c.platform, c.repoId)] = c;
if (Object.keys(obj).length) await chrome.storage.local.set(obj);
return valid.length;
}
/** Remove every cached analysis (the rlcache:* keys), leaving settings intact.
* Returns the number of entries cleared. */
export async function clearCache() {
const all = await chrome.storage.local.get(null);
const keys = Object.keys(all).filter((k) => k.startsWith(PREFIX));
if (keys.length) await chrome.storage.local.remove(keys);
return keys.length;
}