diff --git a/docs/docs/classes/BgentRuntime.md b/docs/docs/classes/BgentRuntime.md index f0b164b..2526127 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 856e9cc..40c5d12 100644 --- a/src/lib/__tests__/lore.test.ts +++ b/src/lib/__tests__/lore.test.ts @@ -45,6 +45,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 41258d3..f9dbb0c 100644 --- a/src/lib/actions/__tests__/elaborate.test.ts +++ b/src/lib/actions/__tests__/elaborate.test.ts @@ -64,20 +64,7 @@ describe("User Profile", () => { userB: zeroUuid, }); - if (!data) { - throw new Error("Relationship not found"); - } - - const rooms = await runtime.databaseAdapter.getRoomsForParticipants([ - 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 30996d1..4f5a3b2 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; console.log("*** data", data); diff --git a/src/lib/adapters/sqljs.ts b/src/lib/adapters/sqljs.ts index 03e8824..02b120d 100644 --- a/src/lib/adapters/sqljs.ts +++ b/src/lib/adapters/sqljs.ts @@ -501,6 +501,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); @@ -596,15 +597,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 2a7c83c..b7e7496 100644 --- a/src/lib/adapters/supabase.ts +++ b/src/lib/adapters/supabase.ts @@ -404,7 +404,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, }); @@ -461,43 +460,46 @@ export class SupabaseDatabaseAdapter extends DatabaseAdapter { userA: UUID; userB: UUID; }): Promise { - let allRoomData = await this.getRoomsForParticipants([ + const allRoomData = await this.getRoomsForParticipants([ params.userA, 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); } - } - - allRoomData = await this.getRoomsForParticipants([ - params.userA, - params.userB, - ]); - const room_id = allRoomData[0]; - - if (!room_id) { - throw new Error("Room not found"); + // @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 { 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, @@ -508,8 +510,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/runtime.ts b/src/lib/runtime.ts index 431ad8b..10c17a4 100644 --- a/src/lib/runtime.ts +++ b/src/lib/runtime.ts @@ -245,7 +245,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", diff --git a/src/test/getOrCreateRelationship.ts b/src/test/getOrCreateRelationship.ts index 0f53fa3..313d877 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.getRoomsForParticipants([ userA,