#!/usr/bin/env node
import { promises as fs } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { logger } from './utils/logger.js';
import { KnowledgeGraphManager } from './core/KnowledgeGraphManager.js';
import { MCPServer } from './server/MCPServer.js';
import type {
Entity,
Relation,
KnowledgeGraph,
GraphStats,
ValidationReport,
ValidationError,
ValidationWarning,
SavedSearch,
TagAlias,
SearchResult,
BooleanQueryNode,
ImportResult,
CompressionResult,
} from './types/index.js';
// Define memory file path using environment variable with fallback
export const defaultMemoryPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'memory.jsonl');
// Handle backward compatibility: migrate memory.json to memory.jsonl if needed
export async function ensureMemoryFilePath(): Promise<string> {
if (process.env.MEMORY_FILE_PATH) {
// Custom path provided, use it as-is (with absolute path resolution)
return path.isAbsolute(process.env.MEMORY_FILE_PATH)
? process.env.MEMORY_FILE_PATH
: path.join(path.dirname(fileURLToPath(import.meta.url)), process.env.MEMORY_FILE_PATH);
}
// No custom path set, check for backward compatibility migration
const oldMemoryPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'memory.json');
const newMemoryPath = defaultMemoryPath;
try {
// Check if old file exists and new file doesn't
await fs.access(oldMemoryPath);
try {
await fs.access(newMemoryPath);
// Both files exist, use new one (no migration needed)
return newMemoryPath;
} catch {
// Old file exists, new file doesn't - migrate
logger.info('Found legacy memory.json file, migrating to memory.jsonl for JSONL format compatibility');
await fs.rename(oldMemoryPath, newMemoryPath);
logger.info('Successfully migrated memory.json to memory.jsonl');
return newMemoryPath;
}
} catch {
// Old file doesn't exist, use new path
return newMemoryPath;
}
}
// Re-export types for backward compatibility
export type {
Entity,
Relation,
KnowledgeGraph,
GraphStats,
ValidationReport,
ValidationError,
ValidationWarning,
SavedSearch,
TagAlias,
SearchResult,
BooleanQueryNode,
ImportResult,
CompressionResult,
};
// Re-export KnowledgeGraphManager for backward compatibility
export { KnowledgeGraphManager };
let knowledgeGraphManager: KnowledgeGraphManager;
async function main() {
// Initialize memory file path with backward compatibility
const memoryFilePath = await ensureMemoryFilePath();
// Initialize knowledge graph manager with the memory file path
knowledgeGraphManager = new KnowledgeGraphManager(memoryFilePath);
// Initialize and start MCP server
const server = new MCPServer(knowledgeGraphManager);
await server.start();
}
main().catch((error) => {
logger.error("Fatal error in main():", error);
process.exit(1);
});