From 63a15cc0b8c785cfe7f38dadc3cb7da8effc3c60 Mon Sep 17 00:00:00 2001 From: moon Date: Mon, 25 Mar 2024 18:41:14 -0700 Subject: [PATCH] Fix sqlite and some sqljs tests --- docs/docs/classes/BgentRuntime.md | 1 + scripts/concat.mjs | 2 +- src/lib/__tests__/lore.test.ts | 3 ++ src/lib/actions/__tests__/elaborate.test.ts | 15 +------ src/lib/actions/__tests__/ignore.test.ts | 4 +- src/lib/adapters/sqlite.ts | 1 + src/lib/adapters/sqljs.ts | 25 +++++++---- src/lib/adapters/supabase.ts | 49 ++++++++++----------- src/lib/lore.ts | 2 +- src/lib/messages.ts | 2 - src/lib/runtime.ts | 6 +-- src/test/getOrCreateRelationship.ts | 10 +++++ 12 files changed, 60 insertions(+), 60 deletions(-) diff --git a/docs/docs/classes/BgentRuntime.md b/docs/docs/classes/BgentRuntime.md index e1073f1..3b5d5a2 100644 --- a/docs/docs/classes/BgentRuntime.md +++ b/docs/docs/classes/BgentRuntime.md @@ -228,6 +228,7 @@ Send a message to the OpenAI API for completion. | `opts.model` | `undefined` \| `string` | `undefined` | The model to use for completion. | | `opts.presence_penalty` | `undefined` \| `number` | `0.0` | The presence penalty to apply to the completion. | | `opts.stop` | `undefined` \| `never`[] | `[]` | A list of strings to stop the completion at. | +| `opts.temperature` | `undefined` \| `number` | `0.7` | The temperature to apply to the completion. | #### Returns diff --git a/scripts/concat.mjs b/scripts/concat.mjs index 5edfd17..cc4e937 100644 --- a/scripts/concat.mjs +++ b/scripts/concat.mjs @@ -5,7 +5,7 @@ import { fileURLToPath } from 'url' const instructions = 'The above code was taken from my codebase at https://github.com/jointhealliance/bgent.' // Patterns to ignore -const ignorePatterns = ["evaluator", "action", "utils", "template", "util", "test", "types", "constants", "agents", "context", "provider", "logger"] +const ignorePatterns = ["evaluator", "sqlite", "action", "utils", "template", "util", "test", "types", "constants", "agents", "context", "provider", "logger"] // __dirname is not defined in ES module scope, so we need to create it const __filename = fileURLToPath(import.meta.url) diff --git a/src/lib/__tests__/lore.test.ts b/src/lib/__tests__/lore.test.ts index a273825..be02dcc 100644 --- a/src/lib/__tests__/lore.test.ts +++ b/src/lib/__tests__/lore.test.ts @@ -26,6 +26,9 @@ describe("Lore", () => { userB: zeroUuid, }); + // create a room at zeroUuid + await runtime.databaseAdapter.createRoom(zeroUuid); + if (!data) { throw new Error("Relationship not found"); } diff --git a/src/lib/actions/__tests__/elaborate.test.ts b/src/lib/actions/__tests__/elaborate.test.ts index 11e2727..b69458a 100644 --- a/src/lib/actions/__tests__/elaborate.test.ts +++ b/src/lib/actions/__tests__/elaborate.test.ts @@ -64,20 +64,9 @@ describe("User Profile", () => { userB: zeroUuid, }); - if (!data) { - throw new Error("Relationship not found"); - } + console.log("data", data); - const rooms = await runtime.databaseAdapter.getRoomsByParticipants([ - user.id as UUID, - zeroUuid, - ]); - - if (!rooms || rooms.length === 0) { - throw new Error("Room not found"); - } - - room_id = rooms[0]; + room_id = data.room_id; await cleanup(); }); diff --git a/src/lib/actions/__tests__/ignore.test.ts b/src/lib/actions/__tests__/ignore.test.ts index 3d7ee26..7288491 100644 --- a/src/lib/actions/__tests__/ignore.test.ts +++ b/src/lib/actions/__tests__/ignore.test.ts @@ -153,9 +153,7 @@ describe("Ignore action tests", () => { userB: zeroUuid, }); - if (!data) { - throw new Error("Relationship not found"); - } + console.log("data is", data); room_id = data?.room_id; diff --git a/src/lib/adapters/sqlite.ts b/src/lib/adapters/sqlite.ts index 1caa2ed..5b7de77 100644 --- a/src/lib/adapters/sqlite.ts +++ b/src/lib/adapters/sqlite.ts @@ -394,6 +394,7 @@ AND room_id = ?`; } async createRoom(room_id?: UUID): Promise { + room_id = room_id || (v4() as UUID); try { const sql = "INSERT INTO rooms (id) VALUES (?)"; this.db.prepare(sql).run(room_id ?? (v4() as UUID)); diff --git a/src/lib/adapters/sqljs.ts b/src/lib/adapters/sqljs.ts index 02065b2..ecac757 100644 --- a/src/lib/adapters/sqljs.ts +++ b/src/lib/adapters/sqljs.ts @@ -455,6 +455,7 @@ export class SqlJsDatabaseAdapter extends DatabaseAdapter { } async createRoom(room_id?: UUID): Promise { + room_id = room_id || (v4() as UUID); try { const sql = "INSERT INTO rooms (id) VALUES (?)"; const stmt = this.db.prepare(sql); @@ -538,15 +539,21 @@ export class SqlJsDatabaseAdapter extends DatabaseAdapter { userA: UUID; userB: UUID; }): Promise { - const sql = - "SELECT * FROM relationships WHERE (user_a = ? AND user_b = ?) OR (user_a = ? AND user_b = ?)"; - const stmt = this.db.prepare(sql); - stmt.bind([params.userA, params.userB, params.userB, params.userA]); - const relationship = stmt.getAsObject() as unknown as - | Relationship - | undefined; - stmt.free(); - return relationship || null; + let relationship: Relationship | null = null; + try { + const sql = + "SELECT * FROM relationships WHERE (user_a = ? AND user_b = ?) OR (user_a = ? AND user_b = ?)"; + const stmt = this.db.prepare(sql); + stmt.bind([params.userA, params.userB, params.userB, params.userA]); + + if (stmt.step()) { + relationship = stmt.getAsObject() as unknown as Relationship; + } + stmt.free(); + } catch (error) { + console.log("Error fetching relationship", error); + } + return relationship; } async getRelationships(params: { user_id: UUID }): Promise { diff --git a/src/lib/adapters/supabase.ts b/src/lib/adapters/supabase.ts index 7edecee..c4ceca0 100644 --- a/src/lib/adapters/supabase.ts +++ b/src/lib/adapters/supabase.ts @@ -51,8 +51,6 @@ export class SupabaseDatabaseAdapter extends DatabaseAdapter { ) .eq("id", params.room_id); - console.log("response", response); - if (response.error) { console.error("Error!" + response.error); return []; @@ -62,7 +60,6 @@ export class SupabaseDatabaseAdapter extends DatabaseAdapter { return data .map((room) => room.participants.map((participant) => { - console.log("***** participant", participant); const user = participant.account as unknown as Actor; return { name: user?.name, @@ -363,7 +360,6 @@ export class SupabaseDatabaseAdapter extends DatabaseAdapter { async createRoom(room_id?: UUID): Promise { room_id = room_id ?? (uuid() as UUID); - console.log("creating room with id", room_id); const { data, error } = await this.supabase.rpc("create_room", { room_id, }); @@ -421,43 +417,42 @@ export class SupabaseDatabaseAdapter extends DatabaseAdapter { params.userB, ]); + let room_id: UUID; + if (!allRoomData || allRoomData.length === 0) { - const { error: roomsError } = await this.supabase + // If no existing room is found, create a new room + const { data: newRoomData, error: roomsError } = await this.supabase .from("rooms") - .insert({}); + .insert({}) + .single(); if (roomsError) { - throw new Error("Room error: " + roomsError.message); + throw new Error("Room creation error: " + roomsError.message); } - } - // get the room_id from the room creation - const { data, error: roomError } = await this.supabase - .from("rooms") - .select("id") - .eq("name", `Room for ${params.userA} and ${params.userB}`) - .single(); - - if (roomError) { - throw new Error("Room error: " + roomError.message); + // @ts-expect-error - newRoomData is not null + room_id = newRoomData?.id as UUID; + } else { + // If an existing room is found, use the first room's ID + room_id = allRoomData[0]; } - const room_id = data.id as UUID; - if (!room_id) { - throw new Error("Room not found"); - } + // Add participants to the room const { error: participantsError } = await this.supabase .from("participants") .insert([ { user_id: params.userA, room_id }, { user_id: params.userB, room_id }, ]); + if (participantsError) { - throw new Error(participantsError.message); + throw new Error( + "Participants creation error: " + participantsError.message, + ); } - // then create a relationship between the two users with the room_id as the relationship's room_id - const { error } = await this.supabase + // Create or update the relationship between the two users + const { error: relationshipError } = await this.supabase .from("relationships") .upsert({ user_a: params.userA, @@ -468,8 +463,10 @@ export class SupabaseDatabaseAdapter extends DatabaseAdapter { .eq("user_a", params.userA) .eq("user_b", params.userB); - if (error) { - throw new Error("Relationship error: " + error.message); + if (relationshipError) { + throw new Error( + "Relationship creation error: " + relationshipError.message, + ); } return true; diff --git a/src/lib/lore.ts b/src/lib/lore.ts index 783ddaf..e3ddea6 100644 --- a/src/lib/lore.ts +++ b/src/lib/lore.ts @@ -41,7 +41,7 @@ export async function addLore({ user_id, content: { content: content.content, source }, room_id, - embedding: embedding, + embedding, }); } catch (e) { console.error("Error adding lore", e); diff --git a/src/lib/messages.ts b/src/lib/messages.ts index f809959..0f7f3c9 100644 --- a/src/lib/messages.ts +++ b/src/lib/messages.ts @@ -11,9 +11,7 @@ export async function getActorDetails({ runtime: BgentRuntime; room_id: UUID; }) { - console.log("getActorDetails"); const actors = await runtime.databaseAdapter.getActorDetails({ room_id }); - console.log("actors", actors); return actors as Actor[]; } diff --git a/src/lib/runtime.ts b/src/lib/runtime.ts index c999be0..f980b74 100644 --- a/src/lib/runtime.ts +++ b/src/lib/runtime.ts @@ -246,7 +246,7 @@ export class BgentRuntime { model = this.model, frequency_penalty = 0.0, presence_penalty = 0.0, - temperature = 0.7 + temperature = 0.7, }) { const requestOptions = { method: "POST", @@ -518,8 +518,6 @@ export class BgentRuntime { }); } - console.log("**** actorsData\n", actorsData); - const actors = formatActors({ actors: actorsData ?? [] }); const recentMessages = formatMessages({ @@ -531,8 +529,6 @@ export class BgentRuntime { }), }); - console.log("**** recentMessages", recentMessages); - const recentFacts = formatFacts(recentFactsData); const relevantFacts = formatFacts(relevantFactsData); diff --git a/src/test/getOrCreateRelationship.ts b/src/test/getOrCreateRelationship.ts index da614bd..68a5bbd 100644 --- a/src/test/getOrCreateRelationship.ts +++ b/src/test/getOrCreateRelationship.ts @@ -17,6 +17,16 @@ export async function getOrCreateRelationship({ } catch (error) { console.log("Error fetching relationship", error); } + + if (!relationship) { + await runtime.databaseAdapter.createRelationship({ + userA, + userB, + }); + + relationship = await getRelationship({ runtime, userA, userB }); + } + // Check if a room already exists for the participants const rooms = await runtime.databaseAdapter.getRoomsByParticipants([ userA,