Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(new-adapter): mongo-db adaptor #1427

Merged
merged 16 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ COSMOS_AVAILABLE_CHAINS= # mantrachaintestnet2,cosmos # Array of chains
CRONOSZKEVM_ADDRESS=
CRONOSZKEVM_PRIVATE_KEY=


# MongoDB
MONGODB_CONNECTION_STRING= #mongodb connection string
MONGODB_DATABASE= #name of the database in mongoDB atlas

# Fuel Ecosystem (FuelVM)
FUEL_WALLET_PRIVATE_KEY=

Expand Down Expand Up @@ -436,3 +441,4 @@ OPEN_WEATHER_API_KEY= # OpenWeather API key
# Allora
ALLORA_API_KEY= # Allora API key, format: UP-f8db7d6558ab432ca0d92716
ALLORA_CHAIN_SLUG= # must be one of mainnet, testnet. If not specified, it will use testnet by default

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.

12 changes: 12 additions & 0 deletions .idea/eliza.iml

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

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

6 changes: 6 additions & 0 deletions .idea/prettier.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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 @@ -376,7 +376,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, {
wtfsayo marked this conversation as resolved.
Show resolved Hide resolved
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
wtfsayo marked this conversation as resolved.
Show resolved Hide resolved
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;
wtfsayo marked this conversation as resolved.
Show resolved Hide resolved
} else if (process.env.POSTGRES_URL) {
elizaLogger.info("Initializing PostgreSQL connection...");
const db = new PostgresDatabaseAdapter({
connectionString: process.env.POSTGRES_URL,
Expand Down Expand Up @@ -405,7 +435,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
Loading