xonecas.io
53129
Pa

PAT / commit 4deb00a

feat: cache 3rd party data for 6 hours

xonecas2025-12-17 4deb00a706045c4546b4cb7df81b696cd54e4cfc parent 6edc2b6

4 files changed

src/lib/cache.ts+171−0
@@ -0,0 +1,171 @@
1+/* personal-assistant-thing/src/lib/cache.ts */
2+
3+import { promises as fs } from "fs";
4+import { join } from "path";
5+import { debug } from "./debug";
6+
7+/**
8+ * Cache entry with optional expiration timestamp.
9+ */
10+interface CacheEntry<T> {
11+ value: T;
12+ expiresAt?: number; // Unix timestamp in milliseconds
13+}
14+
15+/**
16+ * Cache data structure stored on disk.
17+ */
18+interface CacheData {
19+ [key: string]: CacheEntry<any>;
20+}
21+
22+const CACHE_FILE = join(process.cwd(), "data", "cache.json");
23+
24+/**
25+ * Loads cache data from disk.
26+ * Returns empty object if file doesn't exist or can't be read.
27+ *
28+ * @returns Cache data object
29+ */
30+async function loadCache(): Promise<CacheData> {
31+ try {
32+ const data = await fs.readFile(CACHE_FILE, "utf-8");
33+ const cache = JSON.parse(data) as CacheData;
34+
35+ // Clean up expired entries
36+ const now = Date.now();
37+ const cleanedCache: CacheData = {};
38+ for (const [key, entry] of Object.entries(cache)) {
39+ if (!entry.expiresAt || entry.expiresAt > now) {
40+ cleanedCache[key] = entry;
41+ }
42+ }
43+
44+ // If we removed expired entries, save the cleaned cache
45+ if (Object.keys(cleanedCache).length !== Object.keys(cache).length) {
46+ await saveCache(cleanedCache);
47+ }
48+
49+ return cleanedCache;
50+ } catch (error: any) {
51+ // File doesn't exist or can't be read - this is fine
52+ if (error.code === "ENOENT") {
53+ debug(`[Cache] No existing cache file found, starting fresh`);
54+ return {};
55+ }
56+ debug(`[Cache] Error loading cache: ${error.message}`);
57+ return {};
58+ }
59+}
60+
61+/**
62+ * Saves cache data to disk.
63+ * Creates the data directory if it doesn't exist.
64+ *
65+ * @param cache - The cache data to save
66+ */
67+async function saveCache(cache: CacheData): Promise<void> {
68+ try {
69+ const dataDir = join(process.cwd(), "data");
70+ // Ensure data directory exists
71+ try {
72+ await fs.access(dataDir);
73+ } catch {
74+ await fs.mkdir(dataDir, { recursive: true });
75+ }
76+
77+ await fs.writeFile(CACHE_FILE, JSON.stringify(cache, null, 2), "utf-8");
78+ debug(`[Cache] Cache saved to ${CACHE_FILE}`);
79+ } catch (error: any) {
80+ debug(`[Cache] Error saving cache: ${error.message}`);
81+ // Don't throw - cache operations should continue even if persistence fails
82+ }
83+}
84+
85+/**
86+ * Sets a value in the cache with an optional TTL (time-to-live).
87+ *
88+ * @param key - The cache key
89+ * @param value - The value to cache (must be JSON-serializable)
90+ * @param ttlMs - Optional time-to-live in milliseconds. If provided, the entry will expire after this duration.
91+ * @returns Promise that resolves when the value is cached
92+ */
93+export async function setCache<T>(key: string, value: T, ttlMs?: number): Promise<void> {
94+ const cache = await loadCache();
95+ const entry: CacheEntry<T> = {
96+ value,
97+ };
98+
99+ if (ttlMs && ttlMs > 0) {
100+ entry.expiresAt = Date.now() + ttlMs;
101+ }
102+
103+ cache[key] = entry;
104+ await saveCache(cache);
105+ debug(`[Cache] Set key: ${key}${ttlMs ? ` (TTL: ${ttlMs}ms)` : ""}`);
106+}
107+
108+/**
109+ * Gets a value from the cache.
110+ *
111+ * @param key - The cache key
112+ * @returns The cached value, or null if not found or expired
113+ */
114+export async function getCache<T>(key: string): Promise<T | null> {
115+ const cache = await loadCache();
116+ const entry = cache[key];
117+
118+ if (!entry) {
119+ debug(`[Cache] Cache miss for key: ${key}`);
120+ return null;
121+ }
122+
123+ // Check if expired
124+ if (entry.expiresAt && entry.expiresAt <= Date.now()) {
125+ // Remove expired entry
126+ delete cache[key];
127+ await saveCache(cache);
128+ debug(`[Cache] Cache expired for key: ${key}`);
129+ return null;
130+ }
131+
132+ debug(`[Cache] Cache hit for key: ${key}`);
133+ return entry.value as T;
134+}
135+
136+/**
137+ * Removes a key from the cache.
138+ *
139+ * @param key - The cache key to remove
140+ * @returns Promise that resolves when the key is removed
141+ */
142+export async function deleteCache(key: string): Promise<void> {
143+ const cache = await loadCache();
144+ if (key in cache) {
145+ delete cache[key];
146+ await saveCache(cache);
147+ debug(`[Cache] Deleted key: ${key}`);
148+ }
149+}
150+
151+/**
152+ * Clears all entries from the cache.
153+ *
154+ * @returns Promise that resolves when the cache is cleared
155+ */
156+export async function clearCache(): Promise<void> {
157+ await saveCache({});
158+ debug(`[Cache] Cache cleared`);
159+}
160+
161+/**
162+ * Checks if a key exists in the cache and is not expired.
163+ *
164+ * @param key - The cache key to check
165+ * @returns true if the key exists and is not expired, false otherwise
166+ */
167+export async function hasCache(key: string): Promise<boolean> {
168+ const value = await getCache(key);
169+ return value !== null;
170+}
171+
src/lib/ollama/duckduckgo.ts+21−2
@@ -1,14 +1,26 @@
11 /* personal-assistant-thing/src/lib/ollama/duckduckgo.ts */
22
33 import { debug } from "../debug";
4+import { getCache, setCache } from "../cache";
5+
6+const CACHE_TTL_MS = 6 * 60 * 60 * 1000; // 6 hours
47
58 /**
69 * Queries DuckDuckGo's Instant Answer API for information about a given query.
10+ * Results are cached for 6 hours.
711 *
812 * @param query - The search query to send to DuckDuckGo.
913 * @returns A formatted string containing the instant answer information.
1014 */
1115 export async function queryDuckDuckGo(query: string): Promise<string> {
16+ // Check cache first
17+ const cacheKey = `duckduckgo:${query.toLowerCase().trim()}`;
18+ const cached = await getCache<string>(cacheKey);
19+ if (cached !== null) {
20+ debug(`[DuckDuckGo] Cache hit for: "${query}"`);
21+ return cached;
22+ }
23+
1224 debug(`[DuckDuckGo] Querying: "${query}"`);
1325
1426 try {
@@ -70,12 +82,19 @@ export async function queryDuckDuckGo(query: string): Promise<string> {
7082 // DuckDuckGo Instant Answer API has limitations - it doesn't support all query types
7183 // (e.g., weather forecasts, real-time data). This is expected behavior.
7284 debug(`[DuckDuckGo] No instant answer data available for this query type`);
73- return `No instant answer available for query: "${query}". Note: DuckDuckGo Instant Answer API has limited coverage and may not support weather forecasts, real-time data, or certain query types.`;
85+ const result = `No instant answer available for query: "${query}". Note: DuckDuckGo Instant Answer API has limited coverage and may not support weather forecasts, real-time data, or certain query types.`;
86+ // Cache even "no answer" responses to avoid repeated API calls
87+ await setCache(cacheKey, result, CACHE_TTL_MS);
88+ return result;
7489 }
7590
76- return parts.join("\n\n");
91+ const result = parts.join("\n\n");
92+ // Cache successful results
93+ await setCache(cacheKey, result, CACHE_TTL_MS);
94+ return result;
7795 } catch (error) {
7896 console.error("DuckDuckGo query error:", error);
97+ // Don't cache errors
7998 return `Error querying DuckDuckGo: ${error instanceof Error ? error.message : "Unknown error"}`;
8099 }
81100 }
src/lib/ollama/weather.ts+17−1
@@ -1,6 +1,9 @@
11 /* personal-assistant-thing/src/lib/ollama/weather.ts */
22
33 import { debug } from "../debug";
4+import { getCache, setCache } from "../cache";
5+
6+const CACHE_TTL_MS = 6 * 60 * 60 * 1000; // 6 hours
47
58 /**
69 * Converts WMO weather code to human-readable description.
@@ -43,11 +46,20 @@ function getWeatherDescription(code: number): string {
4346
4447 /**
4548 * Queries Open-Meteo's weather API for current weather and forecast information.
49+ * Results are cached for 6 hours.
4650 *
4751 * @param location - The location name (e.g., "New York", "London", "Tokyo").
4852 * @returns A formatted string containing weather information.
4953 */
5054 export async function queryWeather(location: string): Promise<string> {
55+ // Check cache first
56+ const cacheKey = `weather:${location.toLowerCase().trim()}`;
57+ const cached = await getCache<string>(cacheKey);
58+ if (cached !== null) {
59+ debug(`[Weather] Cache hit for: "${location}"`);
60+ return cached;
61+ }
62+
5163 debug(`[Weather] Querying weather for: "${location}"`);
5264
5365 try {
@@ -143,9 +155,13 @@ export async function queryWeather(location: string): Promise<string> {
143155 }
144156 }
145157
146- return parts.join("\n");
158+ const result = parts.join("\n");
159+ // Cache successful results
160+ await setCache(cacheKey, result, CACHE_TTL_MS);
161+ return result;
147162 } catch (error) {
148163 console.error("Weather query error:", error);
164+ // Don't cache errors
149165 return `Error querying weather: ${error instanceof Error ? error.message : "Unknown error"}`;
150166 }
151167 }
src/lib/ollama/websearch.ts+21−2
@@ -2,6 +2,9 @@
22
33 import { google } from "googleapis";
44 import { debug, loadRateLimitState, saveRateLimitState } from "../debug";
5+import { getCache, setCache } from "../cache";
6+
7+const CACHE_TTL_MS = 6 * 60 * 60 * 1000; // 6 hours
58
69 /**
710 * Rate limiter for web search requests.
@@ -107,11 +110,20 @@ function incrementWebSearchRateLimit(): void {
107110 /**
108111 * Performs a web search using Google Custom Search API.
109112 * Enforces a rate limit of 100 requests per 24 hours.
113+ * Results are cached for 6 hours.
110114 *
111115 * @param query - The search query to execute.
112116 * @returns A formatted string containing search results.
113117 */
114118 export async function queryWebSearch(query: string): Promise<string> {
119+ // Check cache first
120+ const cacheKey = `websearch:${query.toLowerCase().trim()}`;
121+ const cached = await getCache<string>(cacheKey);
122+ if (cached !== null) {
123+ debug(`[WebSearch] Cache hit for: "${query}"`);
124+ return cached;
125+ }
126+
115127 debug(`[WebSearch] Querying: "${query}"`);
116128
117129 const apiKey = process.env.GOOGLE_CUSTOM_SEARCH_API_KEY;
@@ -146,7 +158,10 @@ export async function queryWebSearch(query: string): Promise<string> {
146158
147159 if (!res.data.items || res.data.items.length === 0) {
148160 debug(`[WebSearch] No results found for query: "${query}"`);
149- return `No search results found for query: "${query}"`;
161+ const result = `No search results found for query: "${query}"`;
162+ // Cache even "no results" responses to avoid repeated API calls
163+ await setCache(cacheKey, result, CACHE_TTL_MS);
164+ return result;
150165 }
151166
152167 debug(`[WebSearch] Found ${res.data.items.length} results`);
@@ -165,9 +180,13 @@ export async function queryWebSearch(query: string): Promise<string> {
165180 parts.push(""); // Empty line between results
166181 });
167182
168- return parts.join("\n").trim();
183+ const result = parts.join("\n").trim();
184+ // Cache successful results
185+ await setCache(cacheKey, result, CACHE_TTL_MS);
186+ return result;
169187 } catch (error) {
170188 console.error("Web search error:", error);
189+ // Don't cache errors
171190 return `Error performing web search: ${error instanceof Error ? error.message : "Unknown error"}`;
172191 }
173192 }