|
| 1 | +import Base from '../../../../../../src/core/Base.mjs'; |
| 2 | +import aiConfig from '../config.mjs'; |
| 3 | +import ChromaManager from './ChromaManager.mjs'; |
| 4 | +import SQLiteVectorManager from './SQLiteVectorManager.mjs'; |
| 5 | + |
| 6 | +/** |
| 7 | + * @class Neo.ai.mcp.server.memory-core.managers.CollectionProxy |
| 8 | + * @extends Neo.core.Base |
| 9 | + */ |
| 10 | +class CollectionProxy extends Base { |
| 11 | + static config = { |
| 12 | + /** |
| 13 | + * @member {String} className='Neo.ai.mcp.server.memory-core.managers.CollectionProxy' |
| 14 | + * @protected |
| 15 | + */ |
| 16 | + className: 'Neo.ai.mcp.server.memory-core.managers.CollectionProxy', |
| 17 | + /** |
| 18 | + * @member {String} collectionType='memory' |
| 19 | + */ |
| 20 | + collectionType: 'memory' |
| 21 | + } |
| 22 | + |
| 23 | + async getManagers() { |
| 24 | + const engine = aiConfig.engine || 'both'; |
| 25 | + const managers = []; |
| 26 | + |
| 27 | + if (engine === 'chroma' || engine === 'both') { |
| 28 | + await ChromaManager.ready(); |
| 29 | + managers.push(ChromaManager); |
| 30 | + } |
| 31 | + |
| 32 | + if (engine === 'neo' || engine === 'both') { |
| 33 | + await SQLiteVectorManager.ready(); |
| 34 | + managers.push(SQLiteVectorManager); |
| 35 | + } |
| 36 | + |
| 37 | + return managers; |
| 38 | + } |
| 39 | + |
| 40 | + async getCollections() { |
| 41 | + const managers = await this.getManagers(); |
| 42 | + return Promise.all(managers.map(m => |
| 43 | + this.collectionType === 'memory' ? m.getMemoryCollection() : m.getSummaryCollection() |
| 44 | + )); |
| 45 | + } |
| 46 | + |
| 47 | + async add(args) { |
| 48 | + const collections = await this.getCollections(); |
| 49 | + await Promise.all(collections.map(c => c.add(args))); |
| 50 | + } |
| 51 | + |
| 52 | + async upsert(args) { |
| 53 | + const collections = await this.getCollections(); |
| 54 | + await Promise.all(collections.map(c => c.upsert(args))); |
| 55 | + } |
| 56 | + |
| 57 | + async get(args) { |
| 58 | + const collections = await this.getCollections(); |
| 59 | + return collections[0].get(args); |
| 60 | + } |
| 61 | + |
| 62 | + async query(args) { |
| 63 | + const collections = await this.getCollections(); |
| 64 | + return collections[0].query(args); |
| 65 | + } |
| 66 | + |
| 67 | + async count() { |
| 68 | + const collections = await this.getCollections(); |
| 69 | + return collections[0].count(); |
| 70 | + } |
| 71 | + |
| 72 | + async delete(args) { |
| 73 | + const collections = await this.getCollections(); |
| 74 | + await Promise.all(collections.map(c => c.delete(args))); |
| 75 | + } |
| 76 | + |
| 77 | + async drop() { |
| 78 | + const managers = await this.getManagers(); |
| 79 | + for (const manager of managers) { |
| 80 | + const coll = this.collectionType === 'memory' ? |
| 81 | + await manager.getMemoryCollection() : |
| 82 | + await manager.getSummaryCollection(); |
| 83 | + |
| 84 | + if (manager.client && manager.client.deleteCollection) { |
| 85 | + await manager.client.deleteCollection({ name: coll.name }); |
| 86 | + } else if (manager.deleteCollection) { |
| 87 | + await manager.deleteCollection(coll.name); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +export default Neo.setupClass(CollectionProxy); |
0 commit comments