Skip to content

Commit 53104a6

Browse files
claude[bot]olaservo
andcommitted
Add backward compatibility for memory.json -> memory.jsonl migration
When no custom MEMORY_FILE_PATH is set and memory.json exists but memory.jsonl doesn't exist, automatically migrate the old file to the new format with proper file extension. This ensures existing users don't lose their data when upgrading to the version with the corrected .jsonl extension. Co-authored-by: Ola Hungerford <olaservo@users.noreply.github.com>
1 parent 9d8c2df commit 53104a6

1 file changed

Lines changed: 37 additions & 6 deletions

File tree

src/memory/index.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,40 @@ import { fileURLToPath } from 'url';
1313
// Define memory file path using environment variable with fallback
1414
const defaultMemoryPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'memory.jsonl');
1515

16-
// If MEMORY_FILE_PATH is just a filename, put it in the same directory as the script
17-
const MEMORY_FILE_PATH = process.env.MEMORY_FILE_PATH
18-
? path.isAbsolute(process.env.MEMORY_FILE_PATH)
19-
? process.env.MEMORY_FILE_PATH
20-
: path.join(path.dirname(fileURLToPath(import.meta.url)), process.env.MEMORY_FILE_PATH)
21-
: defaultMemoryPath;
16+
// Handle backward compatibility: migrate memory.json to memory.jsonl if needed
17+
async function ensureMemoryFilePath(): Promise<string> {
18+
if (process.env.MEMORY_FILE_PATH) {
19+
// Custom path provided, use it as-is (with absolute path resolution)
20+
return path.isAbsolute(process.env.MEMORY_FILE_PATH)
21+
? process.env.MEMORY_FILE_PATH
22+
: path.join(path.dirname(fileURLToPath(import.meta.url)), process.env.MEMORY_FILE_PATH);
23+
}
24+
25+
// No custom path set, check for backward compatibility migration
26+
const oldMemoryPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'memory.json');
27+
const newMemoryPath = defaultMemoryPath;
28+
29+
try {
30+
// Check if old file exists and new file doesn't
31+
await fs.access(oldMemoryPath);
32+
try {
33+
await fs.access(newMemoryPath);
34+
// Both files exist, use new one (no migration needed)
35+
return newMemoryPath;
36+
} catch {
37+
// Old file exists, new file doesn't - migrate
38+
await fs.rename(oldMemoryPath, newMemoryPath);
39+
console.error('Migrated memory.json to memory.jsonl for JSONL format compatibility');
40+
return newMemoryPath;
41+
}
42+
} catch {
43+
// Old file doesn't exist, use new path
44+
return newMemoryPath;
45+
}
46+
}
47+
48+
// Initialize memory file path (will be set during startup)
49+
let MEMORY_FILE_PATH: string;
2250

2351
// We are storing our memory using entities, relations, and observations in a graph structure
2452
interface Entity {
@@ -410,6 +438,9 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
410438
});
411439

412440
async function main() {
441+
// Initialize memory file path with backward compatibility
442+
MEMORY_FILE_PATH = await ensureMemoryFilePath();
443+
413444
const transport = new StdioServerTransport();
414445
await server.connect(transport);
415446
console.error("Knowledge Graph MCP Server running on stdio");

0 commit comments

Comments
 (0)