|
| 1 | +import Neo from '../../src/Neo.mjs'; |
| 2 | +import * as core from '../../src/core/_export.mjs'; |
| 3 | +import InstanceManager from '../../src/manager/Instance.mjs'; |
| 4 | +import ChromaManager from '../../ai/mcp/server/memory-core/managers/ChromaManager.mjs'; |
| 5 | +import SQLiteVectorManager from '../../ai/mcp/server/memory-core/managers/SQLiteVectorManager.mjs'; |
| 6 | +import TextEmbeddingService from '../../ai/mcp/server/memory-core/services/TextEmbeddingService.mjs'; |
| 7 | +import aiConfig from '../../ai/mcp/server/memory-core/config.mjs'; |
| 8 | + |
| 9 | +/** |
| 10 | + * @module buildScripts/ai/migrateChromaSummariesToSQLite |
| 11 | + */ |
| 12 | + |
| 13 | +/** |
| 14 | + * @summary Extracts legacy session topologies from the deprecated Chroma DB, re-embeds them utilizing the script's `qwen3` parameters, |
| 15 | + * and surgically imports them natively into the Neo SQLite matrix for Graph ingestion. |
| 16 | + * This script serves as the bridge between legacy storage and native 4096D architecture alignment. |
| 17 | + * Implements the Anchor & Echo enhancement strategy by rigorously documenting the transition phase of the REM processing context. |
| 18 | + * @returns {Promise<void>} |
| 19 | + */ |
| 20 | +async function migrateChromaSummariesToSQLite() { |
| 21 | + try { |
| 22 | + console.log("=== MIGRATE SUMMARIES: CHROMA -> NEO SQLITE ==="); |
| 23 | + aiConfig.neoEmbeddingProvider = 'openAiCompatible'; |
| 24 | + |
| 25 | + await ChromaManager.ready(); |
| 26 | + await SQLiteVectorManager.ready(); |
| 27 | + await TextEmbeddingService.ready(); |
| 28 | + |
| 29 | + const chromaColl = await ChromaManager.getSummaryCollection(); |
| 30 | + const sqliteColl = await SQLiteVectorManager.getSummaryCollection(); |
| 31 | + |
| 32 | + const count = await chromaColl.count(); |
| 33 | + console.log(`Found ${count} session summaries in ChromaDB.`); |
| 34 | + |
| 35 | + if (count === 0) { |
| 36 | + console.log("No summaries to rescue!"); |
| 37 | + process.exit(0); |
| 38 | + } |
| 39 | + |
| 40 | + const batchSize = 1000; |
| 41 | + let offset = 0; |
| 42 | + |
| 43 | + while (offset < count) { |
| 44 | + let batch = await chromaColl.get({ |
| 45 | + include: ["documents", "metadatas"], |
| 46 | + limit: batchSize, |
| 47 | + offset: offset |
| 48 | + }); |
| 49 | + |
| 50 | + if (!batch.ids || batch.ids.length === 0) break; |
| 51 | + |
| 52 | + const ids = batch.ids; |
| 53 | + const metadatas = batch.metadatas; |
| 54 | + const documents = batch.documents; |
| 55 | + const generatedEmbeddings = []; |
| 56 | + |
| 57 | + console.log(`Re-embedding ${documents.length} summaries via ${aiConfig.neoEmbeddingProvider}...`); |
| 58 | + |
| 59 | + for (let i = 0; i < documents.length; i++) { |
| 60 | + // Generates standardized 4096D vectors ensuring compliance with the native Edge Graph matrix properties |
| 61 | + const vec = await TextEmbeddingService.embedText(documents[i], 'openAiCompatible'); |
| 62 | + generatedEmbeddings.push(vec); |
| 63 | + if ((i + 1) % 50 === 0) { |
| 64 | + process.stdout.write(`... embedded ${i + 1}/${documents.length}\r`); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + console.log(`\nUpserting ${documents.length} summaries into SQLite...`); |
| 69 | + await sqliteColl.upsert({ |
| 70 | + ids, |
| 71 | + embeddings: generatedEmbeddings, |
| 72 | + metadatas, |
| 73 | + documents |
| 74 | + }); |
| 75 | + |
| 76 | + offset += batchSize; |
| 77 | + } |
| 78 | + |
| 79 | + console.log("✅ Successfully relocated summaries into SQLite vectors!"); |
| 80 | + process.exit(0); |
| 81 | + } catch (e) { |
| 82 | + console.error("❌ Migration Failed:", e); |
| 83 | + process.exit(1); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +migrateChromaSummariesToSQLite(); |
0 commit comments