/** * Agent Storage Module - File I/O Operations for Agent Data * * Features: * - Agent file persistence with three-file structure (config.json, system-prompt.md, memory.json) * - Kebab-case directory naming from agent names * - Date serialization handling for AgentMessage.createdAt * - Atomic file operations with error handling * - Enhanced loading with retry mechanism and partial recovery * - Batch loading optimization for performance * - Complete isolation from other internal modules * - Memory archiving for data preservation before clearing * * Core Functions: * - saveAgentToDisk: Save agent config, system prompt, and memory to separate files * - saveAgentConfigToDisk: Save agent config and system prompt without memory * - loadAgentFromDisk: Load complete agent data from files with Date reconstruction * - loadAgentFromDiskWithRetry: Enhanced loading with retry mechanism * - loadAllAgentsFromDisk: Scan and load all agents in world directory * - loadAllAgentsFromDiskBatch: Optimized batch loading with parallel processing * - deleteAgentFromDisk: Remove agent directory and all files * - agentExistsOnDisk: Check if agent directory exists * - getAgentDir: Get agent directory path * - ensureAgentDirectory: Create agent directory structure * - validateAgentIntegrity: Check agent data consistency * - repairAgentData: Attempt to repair corrupted agent files * - archiveAgentMemory: Archive current memory before clearing * * Implementation: * - Extracted from world-persistence.ts functions * - Uses only fs/promises, path, types.ts, and utils.js * - Handles memory as simple AgentMessage[] array * - Proper Date object serialization/deserialization * - Enhanced error recovery and data validation * - Memory archiving preserves conversation history with timestamps */ import { Agent, AgentMessage } from '../types.js'; /** * Agent loading options for enhanced control */ export interface AgentLoadOptions { includeMemory?: boolean; retryCount?: number; retryDelay?: number; allowPartialLoad?: boolean; validateIntegrity?: boolean; } /** * Agent integrity check result */ export interface AgentIntegrityResult { isValid: boolean; hasConfig: boolean; hasSystemPrompt: boolean; hasMemory: boolean; errors: string[]; warnings: string[]; } /** * Batch loading result with success and failure tracking */ export interface BatchLoadResult { successful: Agent[]; failed: Array<{ agentId: string; error: string; }>; totalCount: number; successCount: number; failureCount: number; } /** * Get agent directory path using kebab-case agent name */ export declare function getAgentDir(rootPath: string, worldId: string, agentId: string): string; /** * Ensure agent directory structure exists */ export declare function ensureAgentDirectory(rootPath: string, worldId: string, agentId: string): Promise; /** * Check if agent directory exists on disk */ export declare function agentExists(rootPath: string, worldId: string, agentId: string): Promise; /** * Validate agent data integrity */ export declare function validateAgentIntegrity(rootPath: string, worldId: string, agentId: string): Promise; /** * Attempt to repair corrupted agent data */ export declare function repairAgentData(rootPath: string, worldId: string, agentId: string): Promise; /** * Save agent to disk with three-file structure */ export declare function saveAgent(rootPath: string, worldId: string, agent: Agent): Promise; /** * Save agent configuration and system prompt to disk without memory * This is useful for saving agent metadata changes without including chat history */ export declare function saveAgentConfig(rootPath: string, worldId: string, agent: Agent): Promise; /** * Save only agent memory to disk (performance optimized) */ export declare function saveAgentMemory(rootPath: string, worldId: string, agentId: string, memory: AgentMessage[]): Promise; /** * Archive agent memory to timestamped file before clearing */ export declare function archiveAgentMemory(rootPath: string, worldId: string, agentId: string, memory: AgentMessage[]): Promise; /** * Enhanced agent loading with retry mechanism and partial recovery */ export declare function loadAgentWithRetry(rootPath: string, worldId: string, agentId: string, options?: AgentLoadOptions): Promise; /** * Load agent from disk with complete data reconstruction (original method) */ export declare function loadAgent(rootPath: string, worldId: string, agentId: string): Promise; /** * Optimized batch loading with parallel processing */ export declare function loadAgentsBatch(rootPath: string, worldId: string, options?: AgentLoadOptions): Promise; /** * Load all agents from world directory (original method) */ export declare function listAgents(rootPath: string, worldId: string): Promise; /** * Delete agent directory and all files */ export declare function deleteAgent(rootPath: string, worldId: string, agentId: string): Promise; /** * Delete all memory items with a specific chat ID from all agents in a world * Returns the number of memory items deleted */ export declare function deleteMemoryByChatId(rootPath: string, worldId: string, chatId: string): Promise;