|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import Utils from "./utils.js";
|
|
|
|
|
|
|
|
|
const db = new Dexie("ElysiaCodeCompanion");
|
|
|
|
|
|
db.version(1).stores({
|
|
|
chats: "++id, timestamp, model",
|
|
|
fileCache: "path, content, timestamp"
|
|
|
});
|
|
|
|
|
|
const DB = {
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|