UNPKG

create-ai-chat-context-experimental

Version:

Phase 2: TypeScript rewrite - AI Chat Context & Memory System with conversation extraction and AICF format support (powered by aicf-core v2.1.0).

429 lines 16.8 kB
"use strict"; /** * This file is part of create-ai-chat-context-experimental. * Licensed under the GNU Affero General Public License v3.0 or later (AGPL-3.0-or-later). * See LICENSE file for details. */ 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.MemoryDropoffAgent = void 0; /** * Memory Dropoff Agent - Phase 7 (Updated for Phase 6.5 Session Files) * Phase 8: Enhanced with aicf-core integration - October 2025 * * Manages SESSION file lifecycle by age: * - 0-2 days (sessions/): FULL session data (template format) * - 2-7 days (medium/): SUMMARY format (key conversations only) * - 7-14 days (old/): KEY POINTS only (decisions, outcomes) * - 14+ days (archive/): SINGLE LINE per conversation * * Pipeline: * .aicf/sessions/{date}-session.aicf * ↓ (2 days) * .aicf/medium/{date}-session.aicf (compressed) * ↓ (7 days) * .aicf/old/{date}-session.aicf (more compressed) * ↓ (14 days) * .aicf/archive/{date}-session.aicf (single line per conversation) * * Note: Works with session files from SessionConsolidationAgent (Phase 6.5) * Now uses aicf-core for enterprise-grade file operations */ const fs_1 = require("fs"); const path_1 = require("path"); const result_js_1 = require("../types/result.js"); const aicf_core_1 = require("aicf-core"); class MemoryDropoffAgent { aicfDir; sessionsDir; mediumDir; oldDir; archiveDir; aicfWriter; constructor(cwd = process.cwd()) { this.aicfDir = (0, path_1.join)(cwd, '.aicf'); this.sessionsDir = (0, path_1.join)(this.aicfDir, 'sessions'); this.mediumDir = (0, path_1.join)(this.aicfDir, 'medium'); this.oldDir = (0, path_1.join)(this.aicfDir, 'old'); this.archiveDir = (0, path_1.join)(this.aicfDir, 'archive'); this.aicfWriter = new aicf_core_1.AICFWriter(this.aicfDir); } /** * Run dropoff process: analyze ages, move files, compress content */ async dropoff() { try { // Ensure all directories exist this.ensureDirectories(); // Analyze all session files const sessions = this.analyzeAllSessions(); // Move and compress files based on age let movedToMedium = 0; let movedToOld = 0; let movedToArchive = 0; let compressed = 0; for (const session of sessions) { if (session.targetFolder && session.targetFolder !== session.currentFolder) { const moveResult = await this.moveAndCompress(session); if (moveResult.ok) { compressed++; if (session.targetFolder === 'medium') movedToMedium++; if (session.targetFolder === 'old') movedToOld++; if (session.targetFolder === 'archive') movedToArchive++; } } } // Count files in each folder const stats = { sessionFiles: this.countFiles(this.sessionsDir), mediumFiles: this.countFiles(this.mediumDir), oldFiles: this.countFiles(this.oldDir), archiveFiles: this.countFiles(this.archiveDir), movedToMedium, movedToOld, movedToArchive, compressed, timestamp: new Date().toISOString(), }; return (0, result_js_1.Ok)(stats); } catch (error) { return (0, result_js_1.Err)(error instanceof Error ? error : new Error(`Dropoff failed: ${String(error)}`)); } } /** * Analyze all session files across all folders */ analyzeAllSessions() { const sessions = []; const now = new Date(); // Analyze sessions/ const sessionFiles = this.getAicfFiles(this.sessionsDir); for (const fileName of sessionFiles) { const age = this.analyzeFile(fileName, 'sessions', now); if (age) sessions.push(age); } // Analyze medium/ const mediumFiles = this.getAicfFiles(this.mediumDir); for (const fileName of mediumFiles) { const age = this.analyzeFile(fileName, 'medium', now); if (age) sessions.push(age); } // Analyze old/ const oldFiles = this.getAicfFiles(this.oldDir); for (const fileName of oldFiles) { const age = this.analyzeFile(fileName, 'old', now); if (age) sessions.push(age); } // Analyze archive/ const archiveFiles = this.getAicfFiles(this.archiveDir); for (const fileName of archiveFiles) { const age = this.analyzeFile(fileName, 'archive', now); if (age) sessions.push(age); } return sessions; } /** * Analyze a single session file and determine its age and target folder */ analyzeFile(fileName, currentFolder, now) { // Parse date from filename: {date}-session.aicf // Example: 2025-10-25-session.aicf const match = fileName.match(/^(\d{4}-\d{2}-\d{2})-session\.aicf$/); if (!match) return null; const dateStr = match[1]; if (!dateStr) return null; const date = new Date(dateStr); const ageInDays = Math.floor((now.getTime() - date.getTime()) / (1000 * 60 * 60 * 24)); // Determine target folder based on age (SHORTER WINDOWS for Phase 6.5) // 0-2 days: sessions/ (FULL) // 2-7 days: medium/ (SUMMARY) // 7-14 days: old/ (KEY_POINTS) // 14+ days: archive/ (SINGLE_LINE) let targetFolder = null; if (ageInDays >= 14) { targetFolder = 'archive'; } else if (ageInDays >= 7) { targetFolder = 'old'; } else if (ageInDays >= 2) { targetFolder = 'medium'; } else { targetFolder = 'sessions'; } const folderPath = this.getFolderPath(currentFolder); return { filePath: (0, path_1.join)(folderPath, fileName), fileName, sessionDate: dateStr, date, ageInDays, currentFolder, targetFolder, }; } /** * Move session file and compress content based on target folder * NOW USES aicf-core FOR ENTERPRISE-GRADE WRITES: * - Thread-safe file locking (prevents corruption) * - Atomic writes (all-or-nothing) * - Input validation (schema-based) * - PII redaction (if enabled) * - Error recovery (corrupted file detection) */ async moveAndCompress(session) { try { if (!session.targetFolder) return (0, result_js_1.Ok)(undefined); // Read original content (still using readFileSync for now) // TODO: Consider using aicf-core's reader for consistency const content = (0, fs_1.readFileSync)(session.filePath, 'utf-8'); // Compress based on target folder let compressed; if (session.targetFolder === 'medium') { compressed = this.compressToSummary(content, session); } else if (session.targetFolder === 'old') { compressed = this.compressToKeyPoints(content, session); } else if (session.targetFolder === 'archive') { compressed = this.compressToSingleLine(content, session); } else { compressed = content; // No compression for sessions } // Build relative file path for aicf-core const targetFolderName = session.targetFolder; const fileName = `${targetFolderName}/${session.fileName}`; // Use aicf-core's appendLine for enterprise-grade writes // This gives us: thread-safe locking, validation, PII redaction, error recovery const writeResult = await this.aicfWriter.appendLine(fileName, compressed); if (!writeResult.ok) { return (0, result_js_1.Err)(new Error(`Failed to write compressed file: ${writeResult.error.message}`)); } // Delete original file (only if write was successful) const fs = await Promise.resolve().then(() => __importStar(require('fs/promises'))); await fs.unlink(session.filePath); return (0, result_js_1.Ok)(undefined); } catch (error) { return (0, result_js_1.Err)(error instanceof Error ? error : new Error(`Failed to move and compress: ${String(error)}`)); } } /** * Compress to SUMMARY format (2-7 days) * Keep: Only conversations with explicit decisions or actions * Remove: Conversations with "No explicit decisions" and "No explicit actions" */ compressToSummary(content, session) { const lines = content.split('\n'); const compressed = []; // Keep header compressed.push('@CONVERSATIONS'); compressed.push('@SCHEMA'); compressed.push('C#|TIMESTAMP|TITLE|SUMMARY|AI_MODEL|DECISIONS|ACTIONS|STATUS'); compressed.push(''); compressed.push('@DATA'); // Filter conversations: keep only those with meaningful decisions or actions let inData = false; for (const line of lines) { if (line === '@DATA') { inData = true; continue; } else if (line.startsWith('@NOTES')) { inData = false; } if (inData && line.trim() && !line.startsWith('@')) { // Parse conversation line const parts = line.split('|'); if (parts.length >= 8) { const decisions = parts[5] || ''; const actions = parts[6] || ''; // Keep only if has meaningful decisions OR actions if ((decisions && decisions !== 'No explicit decisions') || (actions && actions !== 'No explicit actions')) { compressed.push(line); } } } } // Add notes compressed.push(''); compressed.push('@NOTES'); compressed.push(`- Session: ${session.sessionDate}`); compressed.push(`- Compression: SUMMARY (filtered for meaningful conversations)`); compressed.push(`- Age: ${session.ageInDays} days`); return compressed.join('\n'); } /** * Compress to KEY POINTS format (7-14 days) * Keep: Only DECISIONS and ACTIONS columns, remove TITLE and SUMMARY */ compressToKeyPoints(content, session) { const lines = content.split('\n'); const compressed = []; // Keep header with reduced schema compressed.push('@CONVERSATIONS'); compressed.push('@SCHEMA'); compressed.push('C#|TIMESTAMP|AI_MODEL|DECISIONS|ACTIONS|STATUS'); compressed.push(''); compressed.push('@DATA'); // Extract only key fields let inData = false; for (const line of lines) { if (line === '@DATA') { inData = true; continue; } else if (line.startsWith('@NOTES')) { inData = false; } if (inData && line.trim() && !line.startsWith('@')) { // Parse conversation line const parts = line.split('|'); if (parts.length >= 8) { const cNum = parts[0]; const timestamp = parts[1]; const aiModel = parts[4]; const decisions = parts[5]; const actions = parts[6]; const status = parts[7]; // Keep only if has meaningful decisions OR actions if ((decisions && decisions !== 'No explicit decisions') || (actions && actions !== 'No explicit actions')) { compressed.push(`${cNum}|${timestamp}|${aiModel}|${decisions}|${actions}|${status}`); } } } } // Add notes compressed.push(''); compressed.push('@NOTES'); compressed.push(`- Session: ${session.sessionDate}`); compressed.push(`- Compression: KEY_POINTS (decisions and actions only)`); compressed.push(`- Age: ${session.ageInDays} days`); return compressed.join('\n'); } /** * Compress to SINGLE LINE format (14+ days) * Keep: Only one line per conversation with timestamp and summary */ compressToSingleLine(content, session) { const lines = content.split('\n'); const compressed = []; compressed.push(`@SESSION|${session.sessionDate}|Age: ${session.ageInDays} days`); compressed.push(''); // Extract only conversations with meaningful content let inData = false; for (const line of lines) { if (line === '@DATA') { inData = true; continue; } else if (line.startsWith('@NOTES')) { inData = false; } if (inData && line.trim() && !line.startsWith('@')) { // Parse conversation line const parts = line.split('|'); if (parts.length >= 8) { const timestamp = parts[1]; const title = parts[2]; const decisions = parts[5]; const actions = parts[6]; // Keep only if has meaningful decisions OR actions if ((decisions && decisions !== 'No explicit decisions') || (actions && actions !== 'No explicit actions')) { compressed.push(`${timestamp}|${title}`); } } } } return compressed.join('\n'); } /** * Get folder path by name */ getFolderPath(folder) { switch (folder) { case 'sessions': return this.sessionsDir; case 'medium': return this.mediumDir; case 'old': return this.oldDir; case 'archive': return this.archiveDir; } } /** * Get all .aicf files in a directory */ getAicfFiles(dir) { if (!(0, fs_1.existsSync)(dir)) return []; return (0, fs_1.readdirSync)(dir).filter((f) => f.endsWith('.aicf')); } /** * Count files in a directory */ countFiles(dir) { return this.getAicfFiles(dir).length; } /** * Ensure all directories exist */ ensureDirectories() { [this.sessionsDir, this.mediumDir, this.oldDir, this.archiveDir].forEach((dir) => { if (!(0, fs_1.existsSync)(dir)) { (0, fs_1.mkdirSync)(dir, { recursive: true }); } }); } } exports.MemoryDropoffAgent = MemoryDropoffAgent; //# sourceMappingURL=MemoryDropoffAgent.js.map