UNPKG

ai-persona-hub

Version:

AI Profile CLI - Create custom AI profiles run against dynamic LLM providers

158 lines 6.16 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ChatHistoryManager = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); class ChatHistoryManager { profilesDir; constructor(profilesDir = './profiles') { this.profilesDir = path.resolve(profilesDir); } getHistoryDir(profileId) { return path.join(this.profilesDir, profileId, 'conversations'); } getHistoryFilePath(profileId) { return path.join(this.getHistoryDir(profileId), 'history.json'); } ensureHistoryDirectory(profileId) { const historyDir = this.getHistoryDir(profileId); if (!fs.existsSync(historyDir)) { fs.mkdirSync(historyDir, { recursive: true }); } } async loadChatHistory(profileId) { const historyFilePath = this.getHistoryFilePath(profileId); if (!fs.existsSync(historyFilePath)) { return { profileId, userMessages: [], lastUpdated: new Date().toISOString(), }; } try { const data = fs.readFileSync(historyFilePath, 'utf-8'); const history = JSON.parse(data); return history; } catch (error) { console.warn(`Warning: Could not load chat history for profile ${profileId}:`, error); return { profileId, userMessages: [], lastUpdated: new Date().toISOString(), }; } } async saveChatHistory(history) { this.ensureHistoryDirectory(history.profileId); const historyFilePath = this.getHistoryFilePath(history.profileId); try { const updatedHistory = { ...history, lastUpdated: new Date().toISOString(), }; fs.writeFileSync(historyFilePath, JSON.stringify(updatedHistory, null, 2)); } catch (error) { console.warn(`Warning: Could not save chat history for profile ${history.profileId}:`, error); } } async addUserMessage(profileId, message) { const history = await this.loadChatHistory(profileId); // Add the new message to the beginning of the array (most recent first) history.userMessages.unshift(message); // Keep only the last 100 messages to prevent unlimited growth if (history.userMessages.length > 100) { history.userMessages = history.userMessages.slice(0, 100); } await this.saveChatHistory(history); } async saveConversation(profileId, messages) { this.ensureHistoryDirectory(profileId); const conversationId = `conversation-${Date.now()}-${Math.random().toString(36).substring(2, 8)}`; const metadata = { id: conversationId, profileId, startedAt: new Date().toISOString(), lastMessageAt: new Date().toISOString(), messageCount: messages.length, }; const conversation = { metadata, messages, }; const conversationPath = path.join(this.getHistoryDir(profileId), `${conversationId}.json`); try { fs.writeFileSync(conversationPath, JSON.stringify(conversation, null, 2)); } catch (error) { console.warn(`Warning: Could not save conversation for profile ${profileId}:`, error); } } async getRecentConversations(profileId, limit = 10) { const historyDir = this.getHistoryDir(profileId); if (!fs.existsSync(historyDir)) { return []; } try { const files = fs .readdirSync(historyDir) .filter(file => file.startsWith('conversation-') && file.endsWith('.json')) .sort() .reverse() // Most recent first .slice(0, limit); const conversations = []; for (const file of files) { try { const filePath = path.join(historyDir, file); const data = fs.readFileSync(filePath, 'utf-8'); const conversation = JSON.parse(data); conversations.push(conversation); } catch (error) { console.warn(`Warning: Could not load conversation from ${file}:`, error); } } return conversations; } catch (error) { console.warn(`Warning: Could not list conversations for profile ${profileId}:`, error); return []; } } } exports.ChatHistoryManager = ChatHistoryManager; //# sourceMappingURL=chat-history-manager.js.map