File size: 855 Bytes
3661307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fs from 'node:fs';
import path from 'node:path';
import type { ChatMessage } from '../shared/chat.js';

type StoredRooms = Record<string, ChatMessage[]>;

const dataDir = path.resolve(process.cwd(), '.data');
const storePath = path.join(dataDir, 'chat-history.json');

export function loadChatHistory(): StoredRooms {
  try {
    if (!fs.existsSync(storePath)) {
      return {};
    }

    const rawData = fs.readFileSync(storePath, 'utf-8');
    return JSON.parse(rawData) as StoredRooms;
  } catch (error) {
    console.error('Failed to load chat history', error);
    return {};
  }
}

export function saveChatHistory(rooms: StoredRooms) {
  try {
    fs.mkdirSync(dataDir, { recursive: true });
    fs.writeFileSync(storePath, JSON.stringify(rooms, null, 2));
  } catch (error) {
    console.error('Failed to save chat history', error);
  }
}