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

chore: add conditionals for supabase to agent directory #2032

Merged
merged 14 commits into from
Jan 11, 2025
1 change: 1 addition & 0 deletions agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"exec": "node --enable-source-maps --loader ts-node/esm src/index.ts"
},
"dependencies": {
"@elizaos/adapter-supabase": "workspace:*",
"@elizaos/adapter-postgres": "workspace:*",
"@elizaos/adapter-redis": "workspace:*",
"@elizaos/adapter-sqlite": "workspace:*",
Expand Down
39 changes: 32 additions & 7 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PGLiteDatabaseAdapter } from "@elizaos/adapter-pglite";
import { PostgresDatabaseAdapter } from "@elizaos/adapter-postgres";
import { RedisClient } from "@elizaos/adapter-redis";
import { SqliteDatabaseAdapter } from "@elizaos/adapter-sqlite";
import { SupabaseDatabaseAdapter } from "@elizaos/adapter-supabase";
import { AutoClientInterface } from "@elizaos/client-auto";
import { DiscordClientInterface } from "@elizaos/client-discord";
import { FarcasterAgentClient } from "@elizaos/client-farcaster";
Expand Down Expand Up @@ -456,7 +457,24 @@ export function getTokenForProvider(
}

function initializeDatabase(dataDir: string) {
if (process.env.POSTGRES_URL) {
if (process.env.SUPABASE_URL && process.env.SUPABASE_ANON_KEY) {
elizaLogger.info("Initializing Supabase connection...");
const db = new SupabaseDatabaseAdapter(
process.env.SUPABASE_URL,
process.env.SUPABASE_ANON_KEY
);

// Test the connection
db.init()
.then(() => {
elizaLogger.success("Successfully connected to Supabase database");
})
.catch((error) => {
elizaLogger.error("Failed to connect to Supabase:", error);
});

return db;
} else if (process.env.POSTGRES_URL) {
elizaLogger.info("Initializing PostgreSQL connection...");
const db = new PostgresDatabaseAdapter({
connectionString: process.env.POSTGRES_URL,
Expand All @@ -466,9 +484,7 @@ function initializeDatabase(dataDir: string) {
// Test the connection
db.init()
.then(() => {
elizaLogger.success(
"Successfully connected to PostgreSQL database"
);
elizaLogger.success("Successfully connected to PostgreSQL database");
})
.catch((error) => {
elizaLogger.error("Failed to connect to PostgreSQL:", error);
Expand All @@ -483,10 +499,19 @@ function initializeDatabase(dataDir: string) {
});
return db;
} else {
const filePath =
process.env.SQLITE_FILE ?? path.resolve(dataDir, "db.sqlite");
// ":memory:";
const filePath = process.env.SQLITE_FILE ?? path.resolve(dataDir, "db.sqlite");
elizaLogger.info(`Initializing SQLite database at ${filePath}...`);
const db = new SqliteDatabaseAdapter(new Database(filePath));

// Test the connection
db.init()
.then(() => {
elizaLogger.success("Successfully connected to SQLite database");
})
.catch((error) => {
elizaLogger.error("Failed to connect to SQLite:", error);
});

return db;
}
}
Expand Down
Loading