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

fix: db queries in sqljs database adapter not using agentId #606

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
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
30 changes: 16 additions & 14 deletions packages/adapter-sqljs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import { Database } from "./types.ts";

export class SqlJsDatabaseAdapter
extends DatabaseAdapter<Database>
implements IDatabaseCacheAdapter
{
implements IDatabaseCacheAdapter {
constructor(db: Database) {
super();
this.db = db;
Expand Down Expand Up @@ -71,19 +70,17 @@ export class SqlJsDatabaseAdapter
}

async getMemoriesByRoomIds(params: {
agentId: UUID;
roomIds: UUID[];
tableName: string;
agentId?: UUID;
}): Promise<Memory[]> {
const placeholders = params.roomIds.map(() => "?").join(", ");
let sql = `SELECT * FROM memories WHERE type = ? AND roomId IN (${placeholders})`;
let sql = `SELECT * FROM memories WHERE 'type' = ? AND agentId = ? AND roomId IN (${placeholders})`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice catch!

const stmt = this.db.prepare(sql);
const queryParams = [params.tableName, ...params.roomIds];
if (params.agentId) {
sql += " AND userId = ?";
queryParams.push(params.agentId);
}
const queryParams = [params.tableName, params.agentId, ...params.roomIds];
console.log({ queryParams })
stmt.bind(queryParams);
console.log({ queryParams })

const memories: Memory[] = [];
while (stmt.step()) {
Expand Down Expand Up @@ -224,6 +221,7 @@ export class SqlJsDatabaseAdapter
const similarMemories = await this.searchMemoriesByEmbedding(
memory.embedding,
{
agentId: memory.agentId,
tableName,
roomId: memory.roomId,
match_threshold: 0.95, // 5% similarity threshold
Expand Down Expand Up @@ -256,6 +254,7 @@ export class SqlJsDatabaseAdapter

async searchMemories(params: {
tableName: string;
agentId: UUID;
roomId: UUID;
embedding: number[];
match_threshold: number;
Expand All @@ -268,7 +267,7 @@ export class SqlJsDatabaseAdapter
// TODO: Uncomment when we compile sql.js with vss
// `, (1 - vss_distance_l2(embedding, ?)) AS similarity` +
` FROM memories
WHERE type = ?
WHERE type = ? AND agentId = ?
AND roomId = ?`;

if (params.unique) {
Expand All @@ -280,6 +279,7 @@ export class SqlJsDatabaseAdapter
stmt.bind([
// JSON.stringify(params.embedding),
params.tableName,
params.agentId,
params.roomId,
// params.match_count,
]);
Expand All @@ -300,10 +300,10 @@ export class SqlJsDatabaseAdapter
async searchMemoriesByEmbedding(
_embedding: number[],
params: {
agentId: UUID;
match_threshold?: number;
count?: number;
roomId?: UUID;
agentId?: UUID;
unique?: boolean;
tableName: string;
}
Expand All @@ -313,7 +313,7 @@ export class SqlJsDatabaseAdapter
// TODO: Uncomment when we compile sql.js with vss
// `, (1 - vss_distance_l2(embedding, ?)) AS similarity`+
` FROM memories
WHERE type = ?`;
WHERE type = ? AND agentId = ?`;

if (params.unique) {
sql += " AND `unique` = 1";
Expand All @@ -336,6 +336,7 @@ export class SqlJsDatabaseAdapter
const bindings = [
// JSON.stringify(embedding),
params.tableName,
params.agentId,
];
if (params.roomId) {
bindings.push(params.roomId);
Expand Down Expand Up @@ -752,13 +753,13 @@ export class SqlJsDatabaseAdapter

stmt.bind([params.key, params.agentId]);

let cached = undefined;
let cached: { value: string } | undefined = undefined;
if (stmt.step()) {
cached = stmt.getAsObject() as unknown as { value: string };
}
stmt.free();

return cached.value;
return cached?.value ?? undefined;
}

async setCache(params: {
Expand All @@ -785,6 +786,7 @@ export class SqlJsDatabaseAdapter
const stmt = this.db.prepare(sql);
stmt.run([params.key, params.agentId]);
stmt.free();
return true;
} catch (error) {
console.log("Error removing cache", error);
return false;
Expand Down
3 changes: 2 additions & 1 deletion packages/adapter-sqljs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
"rootDir": "src",
"strict": true
},
"include": ["src/**/*.ts"]
}