xonecas.io
53129
Pa

PAT / commit 0514ecc

feat: valkey for cache storage

xonecas2025-12-17 0514eccc09a673c86511c3344c7fb5a7c4402432 parent 3295fe4

15 files changed

AGENTS.md+10−6
@@ -130,12 +130,16 @@ Add tests when prompted only.
130130
131131 ### Cache Utility
132132
133-- **File-based Caching** (`src/lib/cache.ts`): Persistent caching utility with TTL support
134- - Functions: `getCache<T>(key: string)`, `setCache<T>(key: string, value: T, ttlMs?: number)`, `deleteCache(key: string)`, `clearCache()`, `hasCache(key: string)`
135- - Stores cache data in `data/cache.json` (created automatically)
136- - Automatically cleans up expired entries on load
133+- **Valkey-based Caching** (`src/lib/cache/`): Persistent caching utility with TTL support using Valkey (Redis-compatible)
134+ - Functions: `getCache<T>(key: string)`, `setCache<T>(key: string, value: T, ttlMs?: number)`
135+ - Cache API located in: `src/lib/cache/index.ts`
136+ - Valkey connection logic in: `src/lib/cache/valkey.ts`
137+ - Uses Valkey (Redis-compatible in-memory data store) for persistence
138+ - Automatically connects to Valkey on first cache operation
139+ - Uses Valkey's native TTL support (converts milliseconds to seconds)
140+ - Requires `VALKEY_URL` environment variable (format: `redis://:password@host:port`)
137141 - Used by tool implementations to cache API responses (6-hour TTL)
138- - Thread-safe file operations with error handling
142+ - Graceful error handling - cache operations continue even if persistence fails
139143
140144 ### Rate Limit Utility
141145
@@ -203,4 +207,4 @@ Add tests when prompted only.
203207
204208 # Known issues:
205209
206-The google based websearch can randomly fail with Request Entity not found. Generating a new custom search engine ID fixes the issue. Google has ignored all efforts to report this issue to them.
210+The google based websearch can randomly fail with Request Entity not found. Generating a new custom search engine ID fixes the issue. Google has ignored all efforts to report this issue to them.
app/page.tsx+6−3
@@ -16,7 +16,8 @@ export default function Home() {
1616 useThreads();
1717 const { currentThreadId, selectThread, deselectThread, messagesContainerRef } =
1818 useThreadSelection();
19- const { messages, isLoading, isLoadingMessages, sendMessage, clearMessages } = useMessages(currentThreadId);
19+ const { messages, isLoading, isLoadingMessages, sendMessage, clearMessages } =
20+ useMessages(currentThreadId);
2021 const messageInputRef = useRef<MessageInputRef>(null);
2122
2223 const handleThreadSelect = (threadId: number) => {
@@ -38,12 +39,14 @@ export default function Home() {
3839
3940 return (
4041 <div className="flex h-screen bg-neutral-100 text-neutral-800 dark:bg-neutral-950 dark:text-neutral-200">
41- <div className="flex min-w-0 flex-1 flex-col relative">
42+ <div className="relative flex min-w-0 flex-1 flex-col">
4243 <div
4344 ref={messagesContainerRef}
4445 className="flex-1 overflow-y-auto overflow-x-hidden bg-neutral-100 p-4 pb-80 text-neutral-900 dark:bg-neutral-900 dark:text-neutral-100"
4546 >
46- <div className={`mx-auto flex min-h-full max-w-5xl flex-col gap-8 ${currentThreadId === null ? "justify-center" : "justify-end"}`}>
47+ <div
48+ className={`mx-auto flex min-h-full max-w-5xl flex-col gap-8 ${currentThreadId === null ? "justify-center" : "justify-end"}`}
49+ >
4750 {currentThreadId === null ? (
4851 <NoThreadSelected threadCount={totalThreadCount} />
4952 ) : (
package-lock.json+85−1
@@ -16,6 +16,7 @@
1616 "drizzle-orm": "^0.45.1",
1717 "googleapis": "^169.0.0",
1818 "highlight.js": "^11.11.1",
19+ "iovalkey": "^0.3.3",
1920 "next": "^16.0.10",
2021 "ollama": "^0.6.3",
2122 "postcss": "^8.4.30",
@@ -29,7 +30,7 @@
2930 },
3031 "devDependencies": {
3132 "@types/better-sqlite3": "^7.6.13",
32- "@types/node": "^20.11.18",
33+ "@types/node": "^20.19.27",
3334 "@types/react": "^18.2.55",
3435 "@types/react-dom": "^18.2.19",
3536 "@vitest/ui": "^4.0.16",
@@ -1497,6 +1498,12 @@
14971498 "url": "https://opencollective.com/libvips"
14981499 }
14991500 },
1501+ "node_modules/@iovalkey/commands": {
1502+ "version": "0.1.0",
1503+ "resolved": "https://registry.npmjs.org/@iovalkey/commands/-/commands-0.1.0.tgz",
1504+ "integrity": "sha512-/B9W4qKSSITDii5nkBCHyPkIkAi+ealUtr1oqBJsLxjSRLka4pxun2VvMNSmcwgAMxgXtQfl0qRv7TE+udPJzg==",
1505+ "license": "MIT"
1506+ },
15001507 "node_modules/@isaacs/cliui": {
15011508 "version": "8.0.2",
15021509 "resolved": "https://registry.npmjs.org/@isaacs/cliui/-/cliui-8.0.2.tgz",
@@ -3624,6 +3631,15 @@
36243631 "integrity": "sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA==",
36253632 "license": "MIT"
36263633 },
3634+ "node_modules/cluster-key-slot": {
3635+ "version": "1.1.2",
3636+ "resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.2.tgz",
3637+ "integrity": "sha512-RMr0FhtfXemyinomL4hrWcYJxmX6deFdCxpJzhDttxgO1+bcCnkk+9drydLVDmAMG7NE6aN/fl4F7ucU/90gAA==",
3638+ "license": "Apache-2.0",
3639+ "engines": {
3640+ "node": ">=0.10.0"
3641+ }
3642+ },
36273643 "node_modules/color-convert": {
36283644 "version": "2.0.1",
36293645 "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
@@ -3867,6 +3883,15 @@
38673883 "url": "https://github.com/sponsors/ljharb"
38683884 }
38693885 },
3886+ "node_modules/denque": {
3887+ "version": "2.1.0",
3888+ "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz",
3889+ "integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==",
3890+ "license": "Apache-2.0",
3891+ "engines": {
3892+ "node": ">=0.10"
3893+ }
3894+ },
38703895 "node_modules/dequal": {
38713896 "version": "2.0.3",
38723897 "resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.3.tgz",
@@ -5798,6 +5823,26 @@
57985823 "node": ">= 0.4"
57995824 }
58005825 },
5826+ "node_modules/iovalkey": {
5827+ "version": "0.3.3",
5828+ "resolved": "https://registry.npmjs.org/iovalkey/-/iovalkey-0.3.3.tgz",
5829+ "integrity": "sha512-4rTJX6Q5wTYEvxboXi8DsEiUo+OvqJGtLYOSGm37KpdRXsG5XJjbVtYKGJpPSWP+QT7rWscA4vsrdmzbEbenpw==",
5830+ "license": "MIT",
5831+ "dependencies": {
5832+ "@iovalkey/commands": "^0.1.0",
5833+ "cluster-key-slot": "^1.1.0",
5834+ "debug": "^4.3.4",
5835+ "denque": "^2.1.0",
5836+ "lodash.defaults": "^4.2.0",
5837+ "lodash.isarguments": "^3.1.0",
5838+ "redis-errors": "^1.2.0",
5839+ "redis-parser": "^3.0.0",
5840+ "standard-as-callback": "^2.1.0"
5841+ },
5842+ "engines": {
5843+ "node": ">=18.12.0"
5844+ }
5845+ },
58015846 "node_modules/is-alphabetical": {
58025847 "version": "2.0.1",
58035848 "resolved": "https://registry.npmjs.org/is-alphabetical/-/is-alphabetical-2.0.1.tgz",
@@ -6515,6 +6560,18 @@
65156560 "url": "https://github.com/sponsors/sindresorhus"
65166561 }
65176562 },
6563+ "node_modules/lodash.defaults": {
6564+ "version": "4.2.0",
6565+ "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz",
6566+ "integrity": "sha512-qjxPLHd3r5DnsdGacqOMU6pb/avJzdh9tFX2ymgoZE27BmjXrNy/y4LoaiTeAb+O3gL8AfpJGtqfX/ae2leYYQ==",
6567+ "license": "MIT"
6568+ },
6569+ "node_modules/lodash.isarguments": {
6570+ "version": "3.1.0",
6571+ "resolved": "https://registry.npmjs.org/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz",
6572+ "integrity": "sha512-chi4NHZlZqZD18a0imDHnZPrDeBbTtVN7GXMwuGdRH9qotxAjYs3aVLKc7zNOG9eddR5Ksd8rvFEBc9SsggPpg==",
6573+ "license": "MIT"
6574+ },
65186575 "node_modules/lodash.merge": {
65196576 "version": "4.6.2",
65206577 "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz",
@@ -8592,6 +8649,27 @@
85928649 "node": ">=8.10.0"
85938650 }
85948651 },
8652+ "node_modules/redis-errors": {
8653+ "version": "1.2.0",
8654+ "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz",
8655+ "integrity": "sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==",
8656+ "license": "MIT",
8657+ "engines": {
8658+ "node": ">=4"
8659+ }
8660+ },
8661+ "node_modules/redis-parser": {
8662+ "version": "3.0.0",
8663+ "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz",
8664+ "integrity": "sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A==",
8665+ "license": "MIT",
8666+ "dependencies": {
8667+ "redis-errors": "^1.0.0"
8668+ },
8669+ "engines": {
8670+ "node": ">=4"
8671+ }
8672+ },
85958673 "node_modules/reflect.getprototypeof": {
85968674 "version": "1.0.10",
85978675 "resolved": "https://registry.npmjs.org/reflect.getprototypeof/-/reflect.getprototypeof-1.0.10.tgz",
@@ -9274,6 +9352,12 @@
92749352 "dev": true,
92759353 "license": "MIT"
92769354 },
9355+ "node_modules/standard-as-callback": {
9356+ "version": "2.1.0",
9357+ "resolved": "https://registry.npmjs.org/standard-as-callback/-/standard-as-callback-2.1.0.tgz",
9358+ "integrity": "sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A==",
9359+ "license": "MIT"
9360+ },
92779361 "node_modules/std-env": {
92789362 "version": "3.10.0",
92799363 "resolved": "https://registry.npmjs.org/std-env/-/std-env-3.10.0.tgz",
package.json+4−2
@@ -14,7 +14,8 @@
1414 "test:run": "vitest run",
1515 "db:generate": "drizzle-kit generate",
1616 "db:migrate": "drizzle-kit migrate",
17- "db:studio": "drizzle-kit studio"
17+ "db:studio": "drizzle-kit studio",
18+ "start:docker": "./scripts/start-docker.sh"
1819 },
1920 "dependencies": {
2021 "@googleapis/customsearch": "^7.0.1",
@@ -25,6 +26,7 @@
2526 "drizzle-orm": "^0.45.1",
2627 "googleapis": "^169.0.0",
2728 "highlight.js": "^11.11.1",
29+ "iovalkey": "^0.3.3",
2830 "next": "^16.0.10",
2931 "ollama": "^0.6.3",
3032 "postcss": "^8.4.30",
@@ -38,7 +40,7 @@
3840 },
3941 "devDependencies": {
4042 "@types/better-sqlite3": "^7.6.13",
41- "@types/node": "^20.11.18",
43+ "@types/node": "^20.19.27",
4244 "@types/react": "^18.2.55",
4345 "@types/react-dom": "^18.2.19",
4446 "@vitest/ui": "^4.0.16",
scripts/start-docker.sh+31−0
@@ -0,0 +1,31 @@
1+#!/usr/bin/env bash
2+set -euo pipefail
3+
4+# Load environment variables
5+if [ -f .env ]; then
6+ export $(grep -v '^#' .env | xargs)
7+fi
8+
9+# Check if VALKEY_PASSWORD is set
10+if [ -z "${VALKEY_PASSWORD:-}" ]; then
11+ echo "Error: VALKEY_PASSWORD is not set. Please set it in your .env file."
12+ exit 1
13+fi
14+
15+# Pull the image
16+docker pull valkey/valkey:7.2
17+
18+# Stop and remove existing container if it exists
19+if docker ps -a --format '{{.Names}}' | grep -q '^valkey$'; then
20+ docker stop valkey 2>/dev/null || true
21+ docker rm valkey 2>/dev/null || true
22+fi
23+
24+# Run the container
25+docker run -d \
26+ --name valkey \
27+ --restart unless-stopped \
28+ -p 6379:6379 \
29+ -e VALKEY_PASSWORD="${VALKEY_PASSWORD}" \
30+ valkey/valkey:7.2 \
31+ valkey-server --requirepass "${VALKEY_PASSWORD}"
src/components/Anchor.tsx+1−2
@@ -27,7 +27,7 @@ export default function Anchor({
2727
2828 return (
2929 <a
30- className={`flex items-center gap-1 min-w-0 px-2 py-1 rounded ${
30+ className={`flex min-w-0 items-center gap-1 rounded px-2 py-1 ${
3131 isSelected
3232 ? "bg-neutral-200 dark:bg-neutral-800"
3333 : "hover:bg-neutral-300 dark:hover:bg-neutral-700"
@@ -38,4 +38,3 @@ export default function Anchor({
3838 </a>
3939 );
4040 }
41-
src/components/MessageInput.tsx+3−3
@@ -106,10 +106,10 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
106106 };
107107
108108 return (
109- <div className="absolute bottom-0 left-0 right-0 z-10 flex justify-center p-1 pointer-events-none">
109+ <div className="pointer-events-none absolute bottom-0 left-0 right-0 z-10 flex justify-center p-1">
110110 <form
111111 onSubmit={handleSubmit}
112- className="relative w-full max-w-5xl bg-neutral-300 dark:bg-neutral-700 rounded-lg shadow-lg m-4 pointer-events-auto"
112+ className="pointer-events-auto relative m-4 w-full max-w-5xl rounded-lg bg-neutral-300 shadow-lg dark:bg-neutral-700"
113113 >
114114 <textarea
115115 ref={textareaRef}
@@ -119,7 +119,7 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
119119 placeholder={isAnyLoading ? "Loading..." : "Type a message..."}
120120 disabled={isAnyLoading}
121121 rows={3}
122- className="w-full resize-none overflow-y-auto p-4 pr-12 placeholder:italic placeholder:text-neutral-600 dark:placeholder:text-neutral-400 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 bg-transparent"
122+ className="w-full resize-none overflow-y-auto bg-transparent p-4 pr-12 placeholder:italic placeholder:text-neutral-600 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 dark:placeholder:text-neutral-400"
123123 />
124124 <div className="absolute bottom-2 right-2">
125125 <BasicButton color="neutral" type="submit" disabled={isAnyLoading}>
src/components/MessageList/AssistantMessage.tsx+0−1
@@ -63,4 +63,3 @@ export default function AssistantMessage({ message }: AssistantMessageProps) {
6363 </div>
6464 );
6565 }
66-
src/components/MessageList/UserMessage.tsx+1−2
@@ -37,7 +37,7 @@ export default function UserMessage({ message }: UserMessageProps) {
3737 }, [message.content]);
3838
3939 return (
40- <div className="ml-auto w-1/2 min-w-64 p-2 rounded-lg bg-neutral-200 dark:bg-neutral-800">
40+ <div className="ml-auto w-1/2 min-w-64 rounded-lg bg-neutral-200 p-2 dark:bg-neutral-800">
4141 <div className="relative">
4242 <div
4343 ref={contentRef}
@@ -56,4 +56,3 @@ export default function UserMessage({ message }: UserMessageProps) {
5656 </div>
5757 );
5858 }
59-
src/components/MessageList/index.tsx+0−1
@@ -51,4 +51,3 @@ export default function MessageList({ messages }: MessageListProps) {
5151 </>
5252 );
5353 }
54-
src/components/Sidebar.tsx+19−10
@@ -36,16 +36,21 @@ export default function Sidebar({
3636 <div className="flex flex-col gap-2">
3737 <label className="text-neutral-500">Threads</label>
3838 <div className="flex flex-col gap-2">
39- <Anchor href="#" onClick={(e) => {
40- e.preventDefault();
41- onCreateNewThread();
42- }}>
39+ <Anchor
40+ href="#"
41+ onClick={(e) => {
42+ e.preventDefault();
43+ onCreateNewThread();
44+ }}
45+ >
4346 <PlusIcon className="h-4 w-4" />
4447 New Thread
4548 </Anchor>
4649 <div className="flex flex-col">
4750 {threads.length === 0 ? (
48- <div className="px-3 py-2 text-neutral-600 dark:text-neutral-400">No threads yet</div>
51+ <div className="px-3 py-2 text-neutral-600 dark:text-neutral-400">
52+ No threads yet
53+ </div>
4954 ) : (
5055 threads.map((thread) => {
5156 const isSelected = currentThreadId === thread.id;
@@ -69,11 +74,15 @@ export default function Sidebar({
6974 </div>
7075 </div>
7176 {hasMoreThreads && (
72- <Anchor href="#" className="text-xs" onClick={(e) => {
73- e.preventDefault();
74- onLoadMore();
75- }}>
76- <ChevronDownIcon className="h-3 w-3 mt-0.5" />
77+ <Anchor
78+ href="#"
79+ className="text-xs"
80+ onClick={(e) => {
81+ e.preventDefault();
82+ onLoadMore();
83+ }}
84+ >
85+ <ChevronDownIcon className="mt-0.5 h-3 w-3" />
7786 Load More
7887 </Anchor>
7988 )}
src/components/icons/BowtieIcon.tsx+1−6
@@ -1,10 +1,6 @@
11 export default function BowtieIcon({ className }: { className?: string }) {
22 return (
3- <svg
4- xmlns="http://www.w3.org/2000/svg"
5- viewBox="0 0 64 64"
6- className={className}
7- >
3+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" className={className}>
84 <rect width="64" height="64" rx="8" ry="8" fill="white" />
95 <path d="M8 12L8 52L28 32L8 12Z" fill="black" />
106 <path d="M56 12L56 52L36 32L56 12Z" fill="black" />
@@ -12,4 +8,3 @@ export default function BowtieIcon({ className }: { className?: string }) {
128 </svg>
139 );
1410 }
15-
src/lib/cache.ts+0−170
@@ -1,170 +0,0 @@
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-}
src/lib/cache/index.ts+57−0
@@ -0,0 +1,57 @@
1+/* personal-assistant-thing/src/lib/cache/index.ts */
2+
3+import { debug } from "../debug";
4+import ValKey from "iovalkey";
5+
6+const valkey = new ValKey(process.env.VALKEY_URL);
7+
8+/**
9+ * Sets a value in the cache with an optional TTL (time-to-live).
10+ *
11+ * @param key - The cache key
12+ * @param value - The value to cache (must be JSON-serializable)
13+ * @param ttlMs - Optional time-to-live in milliseconds. If provided, the entry will expire after this duration.
14+ * @returns Promise that resolves when the value is cached
15+ */
16+export async function setCache<T>(key: string, value: T, ttlMs?: number): Promise<void> {
17+ try {
18+ const serialized = JSON.stringify(value);
19+
20+ if (ttlMs && ttlMs > 0) {
21+ // Convert milliseconds to seconds for Valkey
22+ const ttlSeconds = Math.ceil(ttlMs / 1000);
23+ await valkey.setex(key, ttlSeconds, serialized);
24+ debug(`[Cache] Set key: ${key} (TTL: ${ttlMs}ms)`);
25+ } else {
26+ await valkey.set(key, serialized);
27+ debug(`[Cache] Set key: ${key}`);
28+ }
29+ } catch (error: any) {
30+ debug(`[Cache] Error setting cache: ${error.message}`);
31+ // Don't throw - cache operations should continue even if persistence fails
32+ }
33+}
34+
35+/**
36+ * Gets a value from the cache.
37+ *
38+ * @param key - The cache key
39+ * @returns The cached value, or null if not found or expired
40+ */
41+export async function getCache<T>(key: string): Promise<T | null> {
42+ try {
43+ const serialized = await valkey.get(key);
44+
45+ if (!serialized) {
46+ debug(`[Cache] Cache miss for key: ${key}`);
47+ return null;
48+ }
49+
50+ debug(`[Cache] Cache hit for key: ${key}`);
51+ return JSON.parse(serialized) as T;
52+ } catch (error: any) {
53+ debug(`[Cache] Error getting cache: ${error.message}`);
54+ return null;
55+ }
56+}
57+
src/lib/ollama/index.ts+5−22
@@ -99,7 +99,7 @@ export async function fetchOllamaResponse(
9999 role: msg.role,
100100 content: msg.content,
101101 }));
102- const maxIterations = 4; // Prevent infinite loops
102+ const maxIterations = 6; // Prevent infinite loops
103103 let iterations = 0;
104104 const allToolCalls: any[] = []; // Collect all tool calls across iterations
105105
@@ -184,34 +184,17 @@ export async function fetchOllamaResponse(
184184 };
185185 }
186186
187- // If we hit max iterations, force a final response without tools
188- debug(`[Ollama] Max iterations reached (${maxIterations}), getting final response...`);
189-
190- // Add a system message to force a text response
191- const finalMessages: OllamaMessage[] = [
192- ...currentMessages,
193- {
194- role: "system",
195- content:
196- "You have reached the maximum number of tool call iterations. Please provide a final answer based on the information you have gathered so far. Do not make any more tool calls.",
197- },
198- ];
187+ // If we hit max iterations, send one more request without tools
188+ debug(`[Ollama] Max iterations reached (${maxIterations}), sending final request without tools...`);
199189
200190 const lastResult = await ollama.chat({
201191 model,
202- messages: finalMessages,
192+ messages: currentMessages,
203193 // Don't pass tools to force a text-only response
204194 });
205195 totalDuration += lastResult.total_duration;
206- let content = lastResult.message.content || "";
207-
208- // If content is still empty, provide a fallback message
209- if (!content || content.trim().length === 0) {
210- content =
211- "I've gathered information from multiple sources, but I'm unable to provide a complete answer at this time. The search results may not have contained the specific information you're looking for. Please try rephrasing your question or providing more specific details.";
212- debug(`[Ollama] Final response was empty, using fallback message`);
213- }
214196
197+ const content = lastResult.message.content || "";
215198 const generationTimeMs = Math.round(totalDuration / 1_000_000);
216199
217200 debug(