Skip to content

Commit f5fe606

Browse files
committed
mongo-db adaptor
Added an adaptor which connects to mongo db atlas. Allowing you to store agent data in the cloud. If you have the appropriate tier you can also take advantage of their vector search functionaility.
1 parent 4c658d7 commit f5fe606

File tree

9 files changed

+1036
-2
lines changed

9 files changed

+1036
-2
lines changed

.env.example

+4
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,7 @@ STORY_PRIVATE_KEY= # Story private key
335335
STORY_API_BASE_URL= # Story API base URL
336336
STORY_API_KEY= # Story API key
337337
PINATA_JWT= # Pinata JWT for uploading files to IPFS
338+
339+
# MongoDB
340+
MONGODB_CONNECTION_STRING= #mongodb connection string
341+
MONGODB_DATABASE= #name of the database in mongoDB atlas

.idea/.gitignore

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agent/src/index.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,37 @@ export function getTokenForProvider(
328328
}
329329

330330
function initializeDatabase(dataDir: string) {
331-
if (process.env.POSTGRES_URL) {
331+
if (process.env.MONGODB_CONNECTION_STRING) {
332+
elizaLogger.log("Initializing database on MongoDB Atlas");
333+
const client = new MongoClient(process.env.MONGODB_CONNECTION_STRING, {
334+
maxPoolSize: 100,
335+
minPoolSize: 5,
336+
maxIdleTimeMS: 60000,
337+
connectTimeoutMS: 10000,
338+
serverSelectionTimeoutMS: 5000,
339+
socketTimeoutMS: 45000,
340+
compressors: ['zlib'],
341+
retryWrites: true,
342+
retryReads: true
343+
});
344+
345+
const dbName = process.env.MONGODB_DATABASE_NAME || 'CumulusAiAgent'; // Default database name
346+
const db = new MongoDBDatabaseAdapter(client, dbName);
347+
348+
// Test the connection
349+
db.init()
350+
.then(() => {
351+
elizaLogger.success(
352+
"Successfully connected to MongoDB Atlas"
353+
);
354+
})
355+
.catch((error) => {
356+
elizaLogger.error("Failed to connect to MongoDB Atlas:", error);
357+
throw error; // Re-throw to handle it in the calling code
358+
});
359+
360+
return db;
361+
} else if (process.env.POSTGRES_URL) {
332362
elizaLogger.info("Initializing PostgreSQL connection...");
333363
const db = new PostgresDatabaseAdapter({
334364
connectionString: process.env.POSTGRES_URL,
@@ -350,7 +380,6 @@ function initializeDatabase(dataDir: string) {
350380
} else {
351381
const filePath =
352382
process.env.SQLITE_FILE ?? path.resolve(dataDir, "db.sqlite");
353-
// ":memory:";
354383
const db = new SqliteDatabaseAdapter(new Database(filePath));
355384
return db;
356385
}

packages/adapter-mongodb/.npmignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*
2+
3+
!dist/**
4+
!package.json
5+
!readme.md
6+
!tsup.config.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import eslintGlobalConfig from "../../eslint.config.mjs";
2+
3+
export default [...eslintGlobalConfig];

packages/adapter-mongodb/package.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "@ai16z/adapter-mongodb",
3+
"version": "0.1.0",
4+
"main": "dist/index.js",
5+
"type": "module",
6+
"types": "dist/index.d.ts",
7+
"dependencies": {
8+
"@ai16z/eliza": "workspace:*",
9+
"mongodb": "^6.3.0",
10+
"uuid": "^9.0.1"
11+
},
12+
"devDependencies": {
13+
"@types/node": "^20.10.0",
14+
"@types/uuid": "^10.0.0",
15+
"tsup": "8.3.5",
16+
"typescript": "^5.0.0"
17+
},
18+
"scripts": {
19+
"build": "tsup --format esm --dts",
20+
"dev": "tsup --format esm --dts --watch",
21+
"lint": "eslint --fix --cache ."
22+
},
23+
"engines": {
24+
"node": ">=16.0.0"
25+
}
26+
}

0 commit comments

Comments
 (0)