|
| 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(); |
0 commit comments