Skip to content

Commit

Permalink
mongo-db adaptor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jobyid committed Dec 24, 2024
1 parent 4c658d7 commit f5fe606
Show file tree
Hide file tree
Showing 9 changed files with 1,036 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,7 @@ STORY_PRIVATE_KEY= # Story private key
STORY_API_BASE_URL= # Story API base URL
STORY_API_KEY= # Story API key
PINATA_JWT= # Pinata JWT for uploading files to IPFS

# MongoDB
MONGODB_CONNECTION_STRING= #mongodb connection string
MONGODB_DATABASE= #name of the database in mongoDB atlas
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 31 additions & 2 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,37 @@ export function getTokenForProvider(
}

function initializeDatabase(dataDir: string) {
if (process.env.POSTGRES_URL) {
if (process.env.MONGODB_CONNECTION_STRING) {
elizaLogger.log("Initializing database on MongoDB Atlas");
const client = new MongoClient(process.env.MONGODB_CONNECTION_STRING, {
maxPoolSize: 100,
minPoolSize: 5,
maxIdleTimeMS: 60000,
connectTimeoutMS: 10000,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
compressors: ['zlib'],
retryWrites: true,
retryReads: true
});

const dbName = process.env.MONGODB_DATABASE_NAME || 'CumulusAiAgent'; // Default database name
const db = new MongoDBDatabaseAdapter(client, dbName);

// Test the connection
db.init()
.then(() => {
elizaLogger.success(
"Successfully connected to MongoDB Atlas"
);
})
.catch((error) => {
elizaLogger.error("Failed to connect to MongoDB Atlas:", error);
throw error; // Re-throw to handle it in the calling code
});

return db;
} else if (process.env.POSTGRES_URL) {
elizaLogger.info("Initializing PostgreSQL connection...");
const db = new PostgresDatabaseAdapter({
connectionString: process.env.POSTGRES_URL,
Expand All @@ -350,7 +380,6 @@ function initializeDatabase(dataDir: string) {
} else {
const filePath =
process.env.SQLITE_FILE ?? path.resolve(dataDir, "db.sqlite");
// ":memory:";
const db = new SqliteDatabaseAdapter(new Database(filePath));
return db;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/adapter-mongodb/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*

!dist/**
!package.json
!readme.md
!tsup.config.ts
3 changes: 3 additions & 0 deletions packages/adapter-mongodb/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import eslintGlobalConfig from "../../eslint.config.mjs";

export default [...eslintGlobalConfig];
26 changes: 26 additions & 0 deletions packages/adapter-mongodb/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@ai16z/adapter-mongodb",
"version": "0.1.0",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"dependencies": {
"@ai16z/eliza": "workspace:*",
"mongodb": "^6.3.0",
"uuid": "^9.0.1"
},
"devDependencies": {
"@types/node": "^20.10.0",
"@types/uuid": "^10.0.0",
"tsup": "8.3.5",
"typescript": "^5.0.0"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"lint": "eslint --fix --cache ."
},
"engines": {
"node": ">=16.0.0"
}
}
Loading

0 comments on commit f5fe606

Please sign in to comment.