Skip to content

Commit fed685f

Browse files
jobyidwtfsayo
andauthored
feat(new-adapter): mongo-db adaptor (#1427)
* 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. * mongo fix * mongoDB Fixes for Merge * Fixing Mongo for new version * k * update `.env.example` and agent/src/index * test mgdb package * Update pnpm-lock.yaml * Update index.ts * update mongodb implementation and tests * remove unnecessary check ins --------- Co-authored-by: Sayo <hi@sayo.wtf>
1 parent 14a407c commit fed685f

18 files changed

+2340
-255
lines changed

.env.example

+16-16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ VITE_SERVER_PORT=${SERVER_PORT}
1515
SUPABASE_URL=
1616
SUPABASE_ANON_KEY=
1717

18+
# MongoDB
19+
MONGODB_CONNECTION_STRING= #mongodb connection string
20+
MONGODB_DATABASE= #name of the database in mongoDB atlas #default: 'elizaAgent'
21+
1822
# Comma separated list of remote character urls (optional)
1923
REMOTE_CHARACTER_URLS=
2024

@@ -134,14 +138,23 @@ ETERNALAI_AGENT_ID= # Ex: 1711
134138
ETERNALAI_API_KEY=
135139
ETERNALAI_LOG=false #Default: false
136140

137-
# Hyperbolic Configuration
141+
# Hyperbolic Provider Configuration
138142
HYPERBOLIC_API_KEY= # Hyperbolic API Key
139143
HYPERBOLIC_MODEL=
144+
140145
IMAGE_HYPERBOLIC_MODEL= # Default: FLUX.1-dev
141146
SMALL_HYPERBOLIC_MODEL= # Default: meta-llama/Llama-3.2-3B-Instruct
142147
MEDIUM_HYPERBOLIC_MODEL= # Default: meta-llama/Meta-Llama-3.1-70B-Instruct
143148
LARGE_HYPERBOLIC_MODEL= # Default: meta-llama/Meta-Llama-3.1-405-Instruct
144149

150+
151+
# Hyperbolic Plugin Configuration
152+
HYPERBOLIC_ENV=production
153+
HYPERBOLIC_API_KEY=
154+
HYPERBOLIC_GRANULAR_LOG=true
155+
HYPERBOLIC_SPASH=true
156+
HYPERBOLIC_LOG_LEVEL=debug
157+
145158
# Infera Configuration
146159
INFERA_API_KEY= # visit api.infera.org/docs to obtain an API key under /signup_user
147160
INFERA_MODEL= # Default: llama3.2:latest
@@ -453,12 +466,6 @@ VLOG= # true/false; if you want to use TEE Verifiable Log, set this to "true"
453466
# Galadriel Configuration
454467
GALADRIEL_API_KEY=gal-* # Get from https://dashboard.galadriel.com/
455468

456-
# Venice Configuration
457-
VENICE_API_KEY= # generate from venice settings
458-
SMALL_VENICE_MODEL= # Default: llama-3.3-70b
459-
MEDIUM_VENICE_MODEL= # Default: llama-3.3-70b
460-
LARGE_VENICE_MODEL= # Default: llama-3.1-405b
461-
IMAGE_VENICE_MODEL= # Default: fluently-xl
462469

463470
# Akash Chat API Configuration docs: https://chatapi.akash.network/documentation
464471
AKASH_CHAT_API_KEY= # Get from https://chatapi.akash.network/
@@ -555,6 +562,7 @@ COSMOS_AVAILABLE_CHAINS= # mantrachaintestnet2,cosmos # Array of chains
555562
CRONOSZKEVM_ADDRESS=
556563
CRONOSZKEVM_PRIVATE_KEY=
557564

565+
558566
# Fuel Ecosystem (FuelVM)
559567
FUEL_WALLET_PRIVATE_KEY=
560568

@@ -613,6 +621,7 @@ ECHOCHAMBERS_QUIET_PERIOD=900 # 15 minutes - waits for 15 minutes of silence
613621
ALLORA_API_KEY= # Allora API key, format: UP-f8db7d6558ab432ca0d92716
614622
ALLORA_CHAIN_SLUG= # must be one of mainnet, testnet. If not specified, it will use testnet by default
615623

624+
616625
# B2 Network
617626
B2_PRIVATE_KEY= # Private key of the B2 Network account to use for the agent
618627

@@ -834,15 +843,6 @@ SUNO_API_KEY=
834843
# Udio AI Music Generation
835844
UDIO_AUTH_TOKEN=
836845

837-
# ####################################
838-
# #### Hyperbolic Configuration ####
839-
# ####################################
840-
HYPERBOLIC_ENV=production
841-
HYPERBOLIC_API_KEY=
842-
HYPERBOLIC_GRANULAR_LOG=true
843-
HYPERBOLIC_SPASH=true
844-
HYPERBOLIC_LOG_LEVEL=debug
845-
846846
# Football Plugin Configuration
847847
FOOTBALL_API_KEY= # API key from Football-Data.org (https://www.football-data.org/)
848848

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ packages/plugin-buttplug/intiface-engine
1212
node-compile-cache
1313

1414
.idea
15+
.vscode
16+
.zed
1517
.DS_Store
1618

1719
dist/

.turbo/config.json

-3
This file was deleted.

.vscode/launch.json

-16
This file was deleted.

.vscode/settings.json

-58
This file was deleted.

.vscode/tasks.json

-14
This file was deleted.

agent/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@elizaos/adapter-sqlite": "workspace:*",
2525
"@elizaos/adapter-pglite": "workspace:*",
2626
"@elizaos/adapter-qdrant": "workspace:*",
27+
"@elizaos/adapter-mongodb": "workspace:*",
2728
"@elizaos/client-auto": "workspace:*",
2829
"@elizaos/client-direct": "workspace:*",
2930
"@elizaos/client-discord": "workspace:*",

agent/src/index.ts

+40-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { LensAgentClient } from "@elizaos/client-lens"
1111
import { SlackClientInterface } from "@elizaos/client-slack"
1212
import { TelegramClientInterface } from "@elizaos/client-telegram"
1313
import { TwitterClientInterface } from "@elizaos/client-twitter"
14+
import { MongoDBDatabaseAdapter } from "@elizaos/adapter-mongodb"
15+
1416
import { FarcasterClientInterface } from "@elizaos/client-farcaster"
1517
import { OmniflixPlugin } from "@elizaos/plugin-omniflix"
1618
import { JeeterClientInterface } from "@elizaos/client-simsai"
@@ -137,6 +139,7 @@ import { zerionPlugin } from "@elizaos/plugin-zerion"
137139
import { minaPlugin } from "@elizaos/plugin-mina"
138140
import { ankrPlugin } from "@elizaos/plugin-ankr";
139141
import { formPlugin } from "@elizaos/plugin-form";
142+
import { MongoClient } from "mongodb";
140143

141144
const __filename = fileURLToPath(import.meta.url) // get the resolved path to the file
142145
const __dirname = path.dirname(__filename) // get the name of the directory
@@ -510,9 +513,42 @@ export function getTokenForProvider(provider: ModelProviderName, character: Char
510513
}
511514

512515
function initializeDatabase(dataDir: string) {
513-
if (process.env.SUPABASE_URL && process.env.SUPABASE_ANON_KEY) {
514-
elizaLogger.info("Initializing Supabase connection...")
515-
const db = new SupabaseDatabaseAdapter(process.env.SUPABASE_URL, process.env.SUPABASE_ANON_KEY)
516+
if (process.env.MONGODB_CONNECTION_STRING) {
517+
elizaLogger.log("Initializing database on MongoDB Atlas");
518+
const client = new MongoClient(process.env.MONGODB_CONNECTION_STRING, {
519+
maxPoolSize: 100,
520+
minPoolSize: 5,
521+
maxIdleTimeMS: 60000,
522+
connectTimeoutMS: 10000,
523+
serverSelectionTimeoutMS: 5000,
524+
socketTimeoutMS: 45000,
525+
compressors: ['zlib'],
526+
retryWrites: true,
527+
retryReads: true
528+
});
529+
530+
const dbName = process.env.MONGODB_DATABASE || 'elizaAgent';
531+
const db = new MongoDBDatabaseAdapter(client, dbName);
532+
533+
// Test the connection
534+
db.init()
535+
.then(() => {
536+
elizaLogger.success(
537+
"Successfully connected to MongoDB Atlas"
538+
);
539+
})
540+
.catch((error) => {
541+
elizaLogger.error("Failed to connect to MongoDB Atlas:", error);
542+
throw error; // Re-throw to handle it in the calling code
543+
});
544+
545+
return db;
546+
} else if (process.env.SUPABASE_URL && process.env.SUPABASE_ANON_KEY) {
547+
elizaLogger.info("Initializing Supabase connection...");
548+
const db = new SupabaseDatabaseAdapter(
549+
process.env.SUPABASE_URL,
550+
process.env.SUPABASE_ANON_KEY,
551+
);
516552

517553
// Test the connection
518554
db.init()
@@ -579,6 +615,7 @@ export async function initializeClients(character: Character, runtime: IAgentRun
579615
const clientTypes: string[] = character.clients?.map((str) => str.toLowerCase()) || []
580616
elizaLogger.log("initializeClients", clientTypes, "for", character.name)
581617

618+
582619
// Start Auto Client if "auto" detected as a configured client
583620
if (clientTypes.includes(Clients.AUTO)) {
584621
const autoClient = await AutoClientInterface.start(runtime)

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

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "@elizaos/adapter-mongodb",
3+
"version": "0.0.1",
4+
"description": "MongoDB adapter for ElizaOS",
5+
"main": "dist/index.js",
6+
"type": "module",
7+
"types": "dist/index.d.ts",
8+
"dependencies": {
9+
"@elizaos/core": "workspace:*",
10+
"mongodb": "^6.3.0",
11+
"uuid": "^9.0.1"
12+
},
13+
"devDependencies": {
14+
"@types/jest": "^29.5.11",
15+
"@types/node": "^20.11.5",
16+
"@types/uuid": "^9.0.7",
17+
"jest": "^29.7.0",
18+
"ts-jest": "^29.1.1",
19+
"tsup": "^8.0.1",
20+
"typescript": "^5.3.3"
21+
},
22+
"scripts": {
23+
"build": "tsup",
24+
"dev": "tsup --format esm --dts --watch",
25+
"lint": "eslint --fix --cache .",
26+
"test": "cd src/__tests__ && ./run_tests.sh",
27+
"test:watch": "jest --watch"
28+
},
29+
"engines": {
30+
"node": ">=16.0.0"
31+
},
32+
"jest": {
33+
"preset": "ts-jest",
34+
"testEnvironment": "node",
35+
"testMatch": ["<rootDir>/src/__tests__/**/*.test.ts"]
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json
2+
version: '3.8'
3+
services:
4+
mongodb-test:
5+
image: mongo:latest
6+
environment:
7+
MONGO_INITDB_ROOT_USERNAME: mongodb
8+
MONGO_INITDB_ROOT_PASSWORD: mongodb
9+
MONGO_INITDB_DATABASE: eliza_test
10+
ports:
11+
- "27018:27017"
12+
healthcheck:
13+
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
14+
interval: 5s
15+
timeout: 5s
16+
retries: 5

0 commit comments

Comments
 (0)