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

refactor: better db connection handling #635

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Changes from all 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: 5 additions & 1 deletion agent/src/index.ts
Original file line number Diff line number Diff line change
@@ -303,6 +303,7 @@ function intializeDbCache(character: Character, db: IDatabaseCacheAdapter) {
}

async function startAgent(character: Character, directClient) {
let db: IDatabaseAdapter & IDatabaseCacheAdapter;
try {
character.id ??= stringToUuid(character.name);
character.username ??= character.name;
@@ -314,7 +315,7 @@ async function startAgent(character: Character, directClient) {
fs.mkdirSync(dataDir, { recursive: true });
}

const db = initializeDatabase(dataDir);
db = initializeDatabase(dataDir);

await db.init();

@@ -334,6 +335,9 @@ async function startAgent(character: Character, directClient) {
error
);
console.error(error);
if (db) {
await db.close();
}
throw error;
}
}
4 changes: 4 additions & 0 deletions packages/adapter-postgres/src/index.ts
Original file line number Diff line number Diff line change
@@ -113,6 +113,10 @@ export class PostgresDatabaseAdapter
await this.query(schema);
}

async close() {
await this.pool.end();
}

async testConnection(): Promise<boolean> {
let client;
try {
4 changes: 4 additions & 0 deletions packages/adapter-sqlite/src/index.ts
Original file line number Diff line number Diff line change
@@ -80,6 +80,10 @@ export class SqliteDatabaseAdapter
this.db.exec(sqliteTables);
}

async close() {
this.db.close();
}

async getAccountById(userId: UUID): Promise<Account | null> {
const sql = "SELECT * FROM accounts WHERE id = ?";
const account = this.db.prepare(sql).get(userId) as Account;
17 changes: 13 additions & 4 deletions packages/adapter-sqljs/src/index.ts
Original file line number Diff line number Diff line change
@@ -18,7 +18,8 @@ import { Database } from "./types.ts";

export class SqlJsDatabaseAdapter
extends DatabaseAdapter<Database>
implements IDatabaseCacheAdapter {
implements IDatabaseCacheAdapter
{
constructor(db: Database) {
super();
this.db = db;
@@ -28,6 +29,10 @@ export class SqlJsDatabaseAdapter
this.db.exec(sqliteTables);
}

async close() {
this.db.close();
}

async getRoom(roomId: UUID): Promise<UUID | null> {
const sql = "SELECT id FROM rooms WHERE id = ?";
const stmt = this.db.prepare(sql);
@@ -77,10 +82,14 @@ export class SqlJsDatabaseAdapter
const placeholders = params.roomIds.map(() => "?").join(", ");
const sql = `SELECT * FROM memories WHERE 'type' = ? AND agentId = ? AND roomId IN (${placeholders})`;
const stmt = this.db.prepare(sql);
const queryParams = [params.tableName, params.agentId, ...params.roomIds];
console.log({ queryParams })
const queryParams = [
params.tableName,
params.agentId,
...params.roomIds,
];
console.log({ queryParams });
stmt.bind(queryParams);
console.log({ queryParams })
console.log({ queryParams });

const memories: Memory[] = [];
while (stmt.step()) {
8 changes: 8 additions & 0 deletions packages/adapter-supabase/src/index.ts
Original file line number Diff line number Diff line change
@@ -100,6 +100,14 @@ export class SupabaseDatabaseAdapter extends DatabaseAdapter {
this.supabase = createClient(supabaseUrl, supabaseKey);
}

async init() {
// noop
}

async close() {
// noop
}

async getMemoriesByRoomIds(params: {
roomIds: UUID[];
agentId?: UUID;
13 changes: 13 additions & 0 deletions packages/core/src/database.ts
Original file line number Diff line number Diff line change
@@ -19,6 +19,19 @@ export abstract class DatabaseAdapter<DB = any> implements IDatabaseAdapter {
* The database instance.
*/
db: DB;

/**
* Optional initialization method for the database adapter.
* @returns A Promise that resolves when initialization is complete.
*/
abstract init(): Promise<void>;

/**
* Optional close method for the database adapter.
* @returns A Promise that resolves when closing is complete.
*/
abstract close(): Promise<void>;

/**
* Retrieves an account by its ID.
* @param userId The UUID of the user account to retrieve.
9 changes: 6 additions & 3 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -297,9 +297,9 @@ export interface State {
formattedConversation?: string;

/** Optional formatted knowledge */
knowledge?: string,
knowledge?: string;
/** Optional knowledge data */
knowledgeData?: KnowledgeItem[],
knowledgeData?: KnowledgeItem[];

/** Additional dynamic properties */
[key: string]: unknown;
@@ -714,7 +714,10 @@ export interface IDatabaseAdapter {
db: any;

/** Optional initialization */
init?(): Promise<void>;
init(): Promise<void>;

/** Close database connection */
close(): Promise<void>;

/** Get account by ID */
getAccountById(userId: UUID): Promise<Account | null>;