UNPKG

ragvault

Version:

Securely manage and query your private data using a local vector database. Your private RAG.

67 lines (66 loc) 2.54 kB
import fs from "fs/promises"; import path from "path"; import { HISTORY_DIR } from "../constant/index.js"; export async function getConversationHistory(username) { try { await fs.mkdir(HISTORY_DIR, { recursive: true }); const files = await fs.readdir(HISTORY_DIR); // Filter files for the specific user const userFiles = files.filter((file) => file.startsWith(`chat-${username}-`)); // Read first question from each file const historyFiles = await Promise.all(userFiles.map(async (filename) => { const content = await fs.readFile(path.join(HISTORY_DIR, filename), "utf-8"); try { const conversations = JSON.parse(content); return { firstQuestion: conversations[0]?.question || "Unknown question", filename, timestamp: filename .split("-") .slice(3) .join("-") .replace(".txt", ""), }; } catch (error) { return { firstQuestion: "Error reading conversation", filename, timestamp: filename .split("-") .slice(3) .join("-") .replace(".txt", ""), }; } })); return historyFiles; } catch (error) { console.error("Error getting conversation history:", error); return []; } } export async function readConversationFile(filename) { try { const filePath = path.join(HISTORY_DIR, filename); const content = await fs.readFile(filePath, "utf-8"); return JSON.parse(content); } catch (error) { console.error("Failed to read conversation file:", error); return []; } } export const saveConversationHistory = async (conversationHistory, username) => { try { const timestamp = new Date().toISOString().replace(/[:.]/g, "-"); await fs.mkdir(HISTORY_DIR, { recursive: true }); const filename = path.join(HISTORY_DIR, `chat-${username}-${timestamp}.txt`); await fs.writeFile(filename, JSON.stringify(conversationHistory, null, 2), "utf-8"); console.log(`\nConversation history saved to: ${filename}`); } catch (error) { console.error("\nFailed to save conversation history:", error); } };