import { normalizeUrl } from "./utils.js"; export class WebFetchCache { private rawCache = new Map(); private extractedCache = new Map(); private ttlMs = 15 * 60 * 1000; private now() { return Date.now(); } private isStale(entry: { timestamp: number }) { return this.now() - entry.timestamp > this.ttlMs; } getRaw(url: string): string | undefined { const keys = [url, normalizeUrl(url)]; for (const key of keys) { const entry = this.rawCache.get(key); if (entry && !this.isStale(entry)) return entry.text; } return undefined; } setRaw(url: string, text: string) { const now = this.now(); this.rawCache.set(url, { text, timestamp: now }); this.rawCache.set(normalizeUrl(url), { text, timestamp: now }); } getExtracted(url: string, prompt: string): { text: string; ageMs: number } | undefined { const keys = [`${url}::${prompt}`, `${normalizeUrl(url)}::${prompt}`]; for (const key of keys) { const entry = this.extractedCache.get(key); if (entry && !this.isStale(entry)) { return { text: entry.text, ageMs: this.now() - entry.timestamp }; } } return undefined; } setExtracted(url: string, prompt: string, text: string) { this.extractedCache.set(`${normalizeUrl(url)}::${prompt}`, { text, timestamp: this.now() }); } // Exposed for testing setTtl(ms: number) { this.ttlMs = ms; } }