import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs' import { join } from 'path' import { homedir } from 'os' import { randomUUID } from 'crypto' /** * Agent Storage Utilities * Provides file-based state isolation for different agents * Based on Kode's Agent ID architecture */ /** * Get the kode config directory */ function getConfigDirectory(): string { return process.env.KODE_CONFIG_DIR ?? process.env.ANYKODE_CONFIG_DIR ?? join(homedir(), '.kode') } /** * Get the current session ID */ function getSessionId(): string { // This should be set when the session starts return process.env.ANYKODE_SESSION_ID ?? 'default-session' } /** * Generate agent-specific file path * Pattern: ${sessionId}-agent-${agentId}.json * Stored in ~/.kode/ directory */ export function getAgentFilePath(agentId: string): string { const sessionId = getSessionId() const filename = `${sessionId}-agent-${agentId}.json` const configDir = getConfigDirectory() // Ensure kode config directory exists if (!existsSync(configDir)) { mkdirSync(configDir, { recursive: true }) } return join(configDir, filename) } /** * Read agent-specific data from storage */ export function readAgentData(agentId: string): T | null { const filePath = getAgentFilePath(agentId) if (!existsSync(filePath)) { return null } try { const content = readFileSync(filePath, 'utf-8') return JSON.parse(content) as T } catch (error) { console.error(`Failed to read agent data for ${agentId}:`, error) return null } } /** * Write agent-specific data to storage */ export function writeAgentData(agentId: string, data: T): void { const filePath = getAgentFilePath(agentId) try { writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf-8') } catch (error) { console.error(`Failed to write agent data for ${agentId}:`, error) throw error } } /** * Get default agent ID if none is provided */ export function getDefaultAgentId(): string { return 'default' } /** * Resolve agent ID from context */ export function resolveAgentId(agentId?: string): string { return agentId || getDefaultAgentId() } /** * Generate a new unique Agent ID */ export function generateAgentId(): string { return randomUUID() }