Skip to content

Commit b70fcfe

Browse files
committed
Set up Memory ChromaDB #7317
1 parent 1a53afe commit b70fcfe

4 files changed

Lines changed: 103 additions & 1 deletion

File tree

.github/ISSUE/epic-ai-knowledge-evolution.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This initiative is a key pillar for advanced workflows, alongside mandatory unit
2828
## Sub-Tasks
2929

3030
### Phase 1: The Memory Core
31+
- **To Do:** ticket-refactor-ai-configuration.md
3132
- **To Do:** ticket-setup-memory-chromadb.md
3233
- **To Do:** ticket-create-memory-capture-api.md
3334
- **To Do:** ticket-implement-memory-backup-and-restore.md

.github/ISSUE/ticket-setup-memory-chromadb.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ GH ticket id: #7317
44

55
**Epic:** AI Knowledge Evolution
66
**Phase:** 1
7-
**Status:** To Do
7+
**Status:** Done
88

99
## Description
1010

buildScripts/ai/setupMemoryDB.mjs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @summary Initializes the ChromaDB database for the AI agent's persistent memory.
3+
*
4+
* This script is the first step in the "AI Knowledge Evolution" epic. Its sole purpose is to
5+
* set up the necessary infrastructure for the agent's memory. It connects to a dedicated
6+
* ChromaDB server instance (expected to be running via `npm run ai:server-memory`) and
7+
* creates a specific collection to store conversation history, decisions, and internal
8+
* thought processes.
9+
*
10+
* This is a one-time setup script. Running it multiple times is safe; it will not
11+
* re-create the collection if it already exists.
12+
*
13+
* @see {@link .github/ISSUE/ticket-setup-memory-chromadb.md}
14+
*/
15+
16+
import {ChromaClient} from 'chromadb';
17+
import path from 'path';
18+
19+
/**
20+
* Centralized configuration for the AI agent's memory database.
21+
* This defines the connection parameters and the name of the collection.
22+
* @type {{collectionName: string, host: string, port: number, path: string}}
23+
*/
24+
const memoryDBConfig = {
25+
collectionName: 'neo-agent-memory',
26+
host : 'localhost', // The hostname of the ChromaDB server
27+
port : 8001, // The port the ChromaDB server is listening on
28+
path : path.resolve(process.cwd(), './chroma-memory') // The local persistence path for the server
29+
};
30+
31+
/**
32+
* Initializes and returns a ChromaDB client configured to connect to the agent's
33+
* memory server.
34+
* @returns {ChromaClient} The configured ChromaDB client instance.
35+
*/
36+
function initializeClient() {
37+
const serverUrl = `http://${memoryDBConfig.host}:${memoryDBConfig.port}`;
38+
console.log(`Initializing ChromaDB HTTP client at: ${serverUrl}`);
39+
40+
// Instantiate the client using the host and port, as recommended by the API.
41+
// This avoids the deprecation warning associated with using the 'path' property for URLs.
42+
return new ChromaClient({
43+
host: memoryDBConfig.host,
44+
port: memoryDBConfig.port,
45+
ssl : false
46+
});
47+
}
48+
49+
/**
50+
* Ensures the dedicated collection for agent memories exists in the ChromaDB instance.
51+
* If the collection is not found, it will be created.
52+
* @param {ChromaClient} client The ChromaDB client instance.
53+
* @returns {Promise<void>}
54+
*/
55+
async function createMemoryCollection(client) {
56+
const {collectionName, host, port} = memoryDBConfig;
57+
console.log(`Checking for collection: "${collectionName}"...`);
58+
59+
try {
60+
const collections = await client.listCollections();
61+
const exists = collections.some(c => c.name === collectionName);
62+
63+
if (exists) {
64+
console.log(`Collection "${collectionName}" already exists.`);
65+
} else {
66+
console.log(`Creating collection: "${collectionName}"...`);
67+
await client.createCollection({name: collectionName});
68+
console.log(`Collection "${collectionName}" created successfully.`);
69+
}
70+
} catch (error) {
71+
// Provide a user-friendly error message if the connection fails.
72+
if (error.message.includes('Failed to connect') || error.message.includes('fetch failed')) {
73+
console.error(`
74+
Error: Could not connect to the ChromaDB server at http://${host}:${port}.`);
75+
console.error('Please ensure the memory server is running by executing: npm run ai:server-memory\n');
76+
process.exit(1);
77+
}
78+
throw error;
79+
}
80+
}
81+
82+
/**
83+
* Main execution function to orchestrate the database setup.
84+
* It initializes the client and creates the collection.
85+
* @returns {Promise<void>}
86+
*/
87+
async function setupMemoryDB() {
88+
try {
89+
const client = initializeClient();
90+
await createMemoryCollection(client);
91+
console.log('Agent memory database setup complete.');
92+
} catch (error) {
93+
console.error('Error setting up agent memory database:', error);
94+
process.exit(1);
95+
}
96+
}
97+
98+
// Execute the setup process.
99+
setupMemoryDB();

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"ai:embed-kb" : "node buildScripts/ai/embedKnowledgeBase.mjs",
1919
"ai:query" : "node buildScripts/ai/queryKnowledgeBase.mjs",
2020
"ai:server" : "chroma run --path ./chroma",
21+
"ai:server-memory" : "chroma run --path ./chroma-memory --port 8001",
22+
"ai:setup-memory-db" : "node buildScripts/ai/setupMemoryDB.mjs",
2123
"build-all" : "node ./buildScripts/buildAll.mjs -f -n",
2224
"build-all-questions" : "node ./buildScripts/buildAll.mjs -f",
2325
"build-dist-esm" : "node ./buildScripts/buildESModules.mjs",

0 commit comments

Comments
 (0)