|
| 1 | +import {ChromaClient} from 'chromadb'; |
| 2 | +import aiConfig from '../../../../../buildScripts/ai/aiConfig.mjs'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Simple manager around the Chroma client that lazily caches frequently used collections. |
| 6 | + */ |
| 7 | +class ChromaManager { |
| 8 | + constructor() { |
| 9 | + const {host, port} = aiConfig.memory; |
| 10 | + |
| 11 | + this.client = new ChromaClient({host, port, ssl: false}); |
| 12 | + this.memoryCollection = null; |
| 13 | + this.summaryCollection = null; |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Ensures the process can reach the Chroma server and both collections are available. |
| 18 | + * @returns {Promise<{heartbeat: number, memoryCollection: string, summaryCollection: string}>} |
| 19 | + */ |
| 20 | + async checkConnectivity() { |
| 21 | + const heartbeat = await this.client.heartbeat(); |
| 22 | + |
| 23 | + const memory = await this.getMemoryCollection(); |
| 24 | + const summaries = await this.getSummaryCollection(); |
| 25 | + |
| 26 | + return { |
| 27 | + heartbeat, |
| 28 | + memoryCollection : memory.name, |
| 29 | + summaryCollection : summaries.name |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @returns {Promise<import('chromadb').Collection>} |
| 35 | + */ |
| 36 | + async getMemoryCollection() { |
| 37 | + if (!this.memoryCollection) { |
| 38 | + const {collectionName} = aiConfig.memory; |
| 39 | + |
| 40 | + this.memoryCollection = await this.client.getCollection({ |
| 41 | + name : collectionName, |
| 42 | + embeddingFunction: aiConfig.dummyEmbeddingFunction |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + return this.memoryCollection; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @returns {Promise<import('chromadb').Collection>} |
| 51 | + */ |
| 52 | + async getSummaryCollection() { |
| 53 | + if (!this.summaryCollection) { |
| 54 | + const {collectionName} = aiConfig.sessions; |
| 55 | + |
| 56 | + this.summaryCollection = await this.client.getOrCreateCollection({ |
| 57 | + name : collectionName, |
| 58 | + embeddingFunction: aiConfig.dummyEmbeddingFunction |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + return this.summaryCollection; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +const chromaManager = new ChromaManager(); |
| 67 | + |
| 68 | +export default chromaManager; |
0 commit comments