File size: 3,446 Bytes
d58d97b |
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
/*
ELYSIA CODE COMPANION v1.2.2 - Database Layer
IndexedDB via Dexie.js for chat history & cache
*/
import Utils from "./utils.js";
// Initialize Dexie Database
const db = new Dexie("ElysiaCodeCompanion");
db.version(1).stores({
chats: "++id, timestamp, model",
fileCache: "path, content, timestamp"
});
const DB = {
// Chat History
async saveChat(userMessage, assistantMessage, context = {}) {
try {
return await db.chats.add({
timestamp: Date.now(),
userMessage,
assistantMessage,
model: context.model || "unknown",
folderName: context.folderName || null,
fileCount: context.fileCount || 0
});
} catch (err) {
console.error("Failed to save chat:", err);
return null;
}
},
async getChats(limit = 50) {
try {
return await db.chats.orderBy("timestamp").reverse().limit(limit).toArray();
} catch (err) {
console.error("Failed to get chats:", err);
return [];
}
},
async clearChats() {
try {
await db.chats.clear();
Utils.toast.success("Chat history cleared");
return true;
} catch (err) {
console.error("Failed to clear chats:", err);
Utils.toast.error("Failed to clear chat history");
return false;
}
},
async getChat(id) {
try {
return await db.chats.get(id);
} catch (err) {
console.error("Failed to get chat:", err);
return null;
}
},
// File Cache - CURRENTLY UNUSED (FileSystem uses in-memory Map cache instead)
// Kept for potential future feature: persistent cache across browser sessions
// TODO: Consider using this for larger projects or implement cache strategy toggle
/*
async cacheFile(path, content) {
try {
await db.fileCache.put({
path,
content,
timestamp: Date.now()
});
return true;
} catch (err) {
console.error("Failed to cache file:", err);
return false;
}
},
async getCachedFile(path) {
try {
const cached = await db.fileCache.get(path);
// Cache expires after 1 hour
if (cached && Date.now() - cached.timestamp < 3600000) {
return cached.content;
}
return null;
} catch (err) {
console.error("Failed to get cached file:", err);
return null;
}
},
async clearFileCache() {
try {
await db.fileCache.clear();
return true;
} catch (err) {
console.error("Failed to clear file cache:", err);
return false;
}
},
*/
// Stats
async getStats() {
try {
const chatCount = await db.chats.count();
const cacheCount = await db.fileCache.count();
return { chatCount, cacheCount };
} catch (err) {
console.error("Failed to get stats:", err);
return { chatCount: 0, cacheCount: 0 };
}
}
};
export default DB;
|