Skip to content

Commit 2709e54

Browse files
committed
PoC: Refactor ChromaManager to a Neo.mjs Class #7537
1 parent 6028d86 commit 2709e54

3 files changed

Lines changed: 147 additions & 70 deletions

File tree

ai/mcp/server/memory-core/mcp-stdio.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2-
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
1+
import {Server} from '@modelcontextprotocol/sdk/server/index.js';
2+
import {StdioServerTransport} from '@modelcontextprotocol/sdk/server/stdio.js';
3+
import Neo from '../../../../src/Neo.mjs';
4+
import * as core from '../../../../src/core/_export.mjs';
5+
import InstanceManager from '../../../../src/manager/Instance.mjs';
6+
37
import {
48
CallToolRequestSchema,
59
ListToolsRequestSchema,
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import {ChromaClient} from 'chromadb';
2+
import aiConfig from '../../config.mjs';
3+
import Base from '../../../../../src/core/Base.mjs';
4+
5+
/**
6+
* Simple manager around the Chroma client that lazily caches frequently used collections.
7+
* @class AI.mcp.server.memory.ChromaManager
8+
* @extends Neo.core.Base
9+
* @singleton
10+
*/
11+
class ChromaManager extends Base {
12+
static config = {
13+
/**
14+
* @member {String} className='AI.mcp.server.memory.ChromaManager'
15+
* @protected
16+
*/
17+
className: 'AI.mcp.server.memory.ChromaManager',
18+
/**
19+
* @member {ChromaClient|null} client_=null
20+
* @protected
21+
* @reactive
22+
*/
23+
client_: null,
24+
/**
25+
* @member {Boolean} connected_=false
26+
* @reactive
27+
*/
28+
connected_: false,
29+
/**
30+
* @member {import('chromadb').Collection|null} memoryCollection_=null
31+
* @protected
32+
* @reactive
33+
*/
34+
memoryCollection_: null,
35+
/**
36+
* @member {Boolean} singleton=true
37+
* @protected
38+
*/
39+
singleton: true,
40+
/**
41+
* @member {import('chromadb').Collection|null} summaryCollection_=null
42+
* @protected
43+
* @reactive
44+
*/
45+
summaryCollection_: null
46+
}
47+
48+
/**
49+
* Triggered after the connected config got changed
50+
* @param {Boolean} value
51+
* @param {Boolean} oldValue
52+
* @protected
53+
*/
54+
afterSetConnected(value, oldValue) {
55+
// console.error(`[ChromaManager] ${value ? 'Connected' : 'Disconnected'}`); // Use logger instead
56+
// this.fire?.('connectionChange', { connected: value }); // Not part of PoC yet
57+
}
58+
59+
/**
60+
*
61+
*/
62+
onConstructed() {
63+
super.onConstructed();
64+
65+
// The client is created here, but the connection is established in initAsync
66+
const {host, port} = aiConfig.memory;
67+
this.client = new ChromaClient({host, port, ssl: false});
68+
}
69+
70+
/**
71+
* @returns {Promise<void>}
72+
*/
73+
async initAsync() {
74+
await super.initAsync();
75+
await this.connect();
76+
}
77+
78+
/**
79+
* Establishes connection to ChromaDB.
80+
*/
81+
async connect() {
82+
try {
83+
await this.client.heartbeat();
84+
this.connected = true;
85+
} catch (e) {
86+
this.connected = false;
87+
throw e;
88+
}
89+
}
90+
91+
/**
92+
* Ensures the process can reach the Chroma server and both collections are available.
93+
* @returns {Promise<{heartbeat: number, memoryCollection: string, summaryCollection: string}>}
94+
*/
95+
async checkConnectivity() {
96+
const heartbeat = await this.client.heartbeat();
97+
98+
const memory = await this.getMemoryCollection();
99+
const summaries = await this.getSummaryCollection();
100+
101+
return {
102+
heartbeat,
103+
memoryCollection : memory.name,
104+
summaryCollection: summaries.name
105+
};
106+
}
107+
108+
/**
109+
* @returns {Promise<import('chromadb').Collection>}
110+
*/
111+
async getMemoryCollection() {
112+
if (!this.memoryCollection) {
113+
const {collectionName} = aiConfig.memory;
114+
115+
this.memoryCollection = await this.client.getCollection({
116+
name : collectionName,
117+
embeddingFunction: aiConfig.dummyEmbeddingFunction
118+
});
119+
}
120+
121+
return this.memoryCollection;
122+
}
123+
124+
/**
125+
* @returns {Promise<import('chromadb').Collection>}
126+
*/
127+
async getSummaryCollection() {
128+
if (!this.summaryCollection) {
129+
const {collectionName} = aiConfig.sessions;
130+
131+
this.summaryCollection = await this.client.getOrCreateCollection({
132+
name : collectionName,
133+
embeddingFunction: aiConfig.dummyEmbeddingFunction
134+
});
135+
}
136+
137+
return this.summaryCollection;
138+
}
139+
}
140+
141+
export default Neo.setupClass(ChromaManager);

ai/mcp/server/memory-core/services/chromaManager.mjs

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)