feat: persistent conversation history across sessions and restarts
- ConversationStore: per-chat JSON files in data/, survives restarts - 6000 token budget per chat context (fits ~20-30 exchanges) - Auto-trims old messages, always includes most recent - Wired into message handler: loads history before AI call, saves after - /reset command to clear chat history per chat - Cross-session, cross-model, cross-chat isolation
This commit is contained in:
@@ -280,10 +280,166 @@ class MemoryStore {
|
||||
}
|
||||
}
|
||||
|
||||
// Singleton
|
||||
let _instance = null;
|
||||
export function getMemory() {
|
||||
if (!_instance) _instance = new MemoryStore();
|
||||
return _instance;
|
||||
// ───────────────────────────────────────────
|
||||
// CONVERSATION HISTORY — per-chat, cross-session, cross-model
|
||||
// ───────────────────────────────────────────
|
||||
|
||||
const HISTORY_DIR = path.join(process.cwd(), 'data');
|
||||
const MAX_HISTORY_PER_CHAT = 50; // last N exchanges per chat
|
||||
const MAX_CONTEXT_TOKENS = 6000; // ~8000 chars — keeps API cost sane
|
||||
const CHARS_PER_TOKEN = 1.3; // rough estimate for mixed content
|
||||
|
||||
class ConversationStore {
|
||||
constructor() {
|
||||
this.histories = new Map(); // chatKey → [{role, content, ts}]
|
||||
this.loaded = false;
|
||||
}
|
||||
|
||||
async init() {
|
||||
try {
|
||||
await fs.ensureDir(HISTORY_DIR);
|
||||
this.loaded = true;
|
||||
logger.info('✓ Conversation store initialized');
|
||||
} catch (e) {
|
||||
logger.error('Conversation store init failed:', e.message);
|
||||
this.loaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a unique key per chat/thread.
|
||||
* Groups DMs by user, groups by group chat ID.
|
||||
*/
|
||||
_key(chatId, threadId) {
|
||||
return `${chatId}:${threadId || 'main'}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file path for a chat's history.
|
||||
*/
|
||||
_filePath(chatKey) {
|
||||
const safe = chatKey.replace(/[^a-zA-Z0-9_\-]/g, '_');
|
||||
return path.join(HISTORY_DIR, `chat_${safe}.json`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load conversation history for a chat from disk.
|
||||
*/
|
||||
async load(chatKey) {
|
||||
if (this.histories.has(chatKey)) return this.histories.get(chatKey);
|
||||
try {
|
||||
const fp = this._filePath(chatKey);
|
||||
if (await fs.pathExists(fp)) {
|
||||
const data = await fs.readJson(fp);
|
||||
const history = Array.isArray(data) ? data : [];
|
||||
this.histories.set(chatKey, history);
|
||||
return history;
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn(`Failed to load history for ${chatKey}: ${e.message}`);
|
||||
}
|
||||
const empty = [];
|
||||
this.histories.set(chatKey, empty);
|
||||
return empty;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save conversation history to disk.
|
||||
*/
|
||||
async _save(chatKey) {
|
||||
const history = this.histories.get(chatKey);
|
||||
if (!history) return;
|
||||
try {
|
||||
const fp = this._filePath(chatKey);
|
||||
await fs.writeJson(fp, history, { spaces: 2 });
|
||||
} catch (e) {
|
||||
logger.error(`Failed to save history for ${chatKey}: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a message to conversation history.
|
||||
*/
|
||||
async add(chatKey, role, content) {
|
||||
if (!this.loaded) await this.init();
|
||||
const history = await this.load(chatKey);
|
||||
history.push({ role, content, ts: Date.now() });
|
||||
|
||||
// Trim to max entries (keep most recent)
|
||||
while (history.length > MAX_HISTORY_PER_CHAT) {
|
||||
history.shift();
|
||||
}
|
||||
|
||||
await this._save(chatKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get context messages for the API call.
|
||||
* Returns the most recent messages that fit within the token budget.
|
||||
* Excludes system messages (those are built separately).
|
||||
* Returns array of {role, content} for the API.
|
||||
*/
|
||||
async getContext(chatKey) {
|
||||
if (!this.loaded) await this.init();
|
||||
const history = await this.load(chatKey);
|
||||
if (history.length === 0) return [];
|
||||
|
||||
// Work backwards from most recent, fitting within budget
|
||||
const selected = [];
|
||||
let budget = MAX_CONTEXT_TOKENS;
|
||||
|
||||
for (let i = history.length - 1; i >= 0; i--) {
|
||||
const msg = history[i];
|
||||
if (msg.role === 'system') continue; // skip system messages
|
||||
const cost = Math.ceil(msg.content.length / CHARS_PER_TOKEN);
|
||||
if (cost > budget && selected.length > 0) break; // stop if budget exceeded (but always include last message)
|
||||
budget -= cost;
|
||||
selected.unshift({ role: msg.role, content: msg.content });
|
||||
}
|
||||
|
||||
return selected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear history for a specific chat.
|
||||
*/
|
||||
async clear(chatKey) {
|
||||
this.histories.delete(chatKey);
|
||||
try {
|
||||
const fp = this._filePath(chatKey);
|
||||
if (await fs.pathExists(fp)) await fs.remove(fp);
|
||||
} catch {}
|
||||
logger.info(`🗑 Cleared conversation history for ${chatKey}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear ALL conversation histories.
|
||||
*/
|
||||
async clearAll() {
|
||||
this.histories.clear();
|
||||
try {
|
||||
const files = await fs.readdir(HISTORY_DIR);
|
||||
for (const f of files) {
|
||||
if (f.startsWith('chat_') && f.endsWith('.json')) {
|
||||
await fs.remove(path.join(HISTORY_DIR, f));
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
logger.info('🗑 Cleared all conversation histories');
|
||||
}
|
||||
}
|
||||
export { MemoryStore };
|
||||
|
||||
// Singleton
|
||||
let _memoryInstance = null;
|
||||
export function getMemory() {
|
||||
if (!_memoryInstance) _memoryInstance = new MemoryStore();
|
||||
return _memoryInstance;
|
||||
}
|
||||
|
||||
let _conversationInstance = null;
|
||||
export function getConversation() {
|
||||
if (!_conversationInstance) _conversationInstance = new ConversationStore();
|
||||
return _conversationInstance;
|
||||
}
|
||||
|
||||
export { MemoryStore, ConversationStore };
|
||||
|
||||
Reference in New Issue
Block a user