Skip to content

Commit

Permalink
fix getMemoriesByRoomIds which fixes our conversation context
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Sep 4, 2024
1 parent 639b107 commit 0b6780b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/adapters/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ export class SqliteDatabaseAdapter extends DatabaseAdapter {
roomIds: UUID[];
tableName: string;
}): Promise<Memory[]> {
if(!params.tableName) {
// default to messages
params.tableName = "messages";
}
const placeholders = params.roomIds.map(() => "?").join(", ");
const sql = `SELECT * FROM memories WHERE type = ? AND roomId IN (${placeholders})`;
const stmt = this.db.prepare(sql);
Expand Down
6 changes: 6 additions & 0 deletions src/core/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1052,16 +1052,22 @@ Text: ${attachment.text}
userB,
]);

console.log("*** getRecentInteractions: Rooms", rooms);

// Check the existing memories in the database
const existingMemories = await this.messageManager.getMemoriesByRoomIds({
roomIds: rooms,
});

console.log("*** getRecentInteractions: Existing Memories", existingMemories);

// Sort messages by timestamp in descending order
existingMemories.sort(
(a, b) => b.createdAt.getTime() - a.createdAt.getTime(),
);

console.log("*** getRecentInteractions: Sorted Memories", existingMemories);

// Take the most recent messages
const recentInteractionsData = existingMemories.slice(0, 20);
return recentInteractionsData;
Expand Down
4 changes: 2 additions & 2 deletions supabase/migrations/20240318103238_remote_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -607,10 +607,10 @@ ALTER TABLE ONLY "public"."memories"
ADD CONSTRAINT "memories_userId_fkey" FOREIGN KEY ("userId") REFERENCES "public"."accounts"("id");

ALTER TABLE ONLY "public"."relationships"
ADD CONSTRAINT "relationships_user_a_fkey" FOREIGN KEY ("userA") REFERENCES "public"."accounts"("id");
ADD CONSTRAINT "relationships_userA_fkey" FOREIGN KEY ("userA") REFERENCES "public"."accounts"("id");

ALTER TABLE ONLY "public"."relationships"
ADD CONSTRAINT "relationships_user_b_fkey" FOREIGN KEY ("userB") REFERENCES "public"."accounts"("id");
ADD CONSTRAINT "relationships_userB_fkey" FOREIGN KEY ("userB") REFERENCES "public"."accounts"("id");

ALTER TABLE ONLY "public"."relationships"
ADD CONSTRAINT "relationships_userId_fkey" FOREIGN KEY ("userId") REFERENCES "public"."accounts"("id");
Expand Down

0 comments on commit 0b6780b

Please sign in to comment.