Skip to content

Commit

Permalink
Fix sqlite and some sqljs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Mar 26, 2024
1 parent 9f393cc commit 63a15cc
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 60 deletions.
1 change: 1 addition & 0 deletions docs/docs/classes/BgentRuntime.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion scripts/concat.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/lib/__tests__/lore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
15 changes: 2 additions & 13 deletions src/lib/actions/__tests__/elaborate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
4 changes: 1 addition & 3 deletions src/lib/actions/__tests__/ignore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/lib/adapters/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ AND room_id = ?`;
}

async createRoom(room_id?: UUID): Promise<UUID> {
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));
Expand Down
25 changes: 16 additions & 9 deletions src/lib/adapters/sqljs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ export class SqlJsDatabaseAdapter extends DatabaseAdapter {
}

async createRoom(room_id?: UUID): Promise<UUID> {
room_id = room_id || (v4() as UUID);
try {
const sql = "INSERT INTO rooms (id) VALUES (?)";
const stmt = this.db.prepare(sql);
Expand Down Expand Up @@ -538,15 +539,21 @@ export class SqlJsDatabaseAdapter extends DatabaseAdapter {
userA: UUID;
userB: UUID;
}): Promise<Relationship | null> {
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<Relationship[]> {
Expand Down
49 changes: 23 additions & 26 deletions src/lib/adapters/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
Expand All @@ -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,
Expand Down Expand Up @@ -363,7 +360,6 @@ export class SupabaseDatabaseAdapter extends DatabaseAdapter {

async createRoom(room_id?: UUID): Promise<UUID> {
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,
});
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/lore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions src/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
}

Expand Down
6 changes: 1 addition & 5 deletions src/lib/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -518,8 +518,6 @@ export class BgentRuntime {
});
}

console.log("**** actorsData\n", actorsData);

const actors = formatActors({ actors: actorsData ?? [] });

const recentMessages = formatMessages({
Expand All @@ -531,8 +529,6 @@ export class BgentRuntime {
}),
});

console.log("**** recentMessages", recentMessages);

const recentFacts = formatFacts(recentFactsData);
const relevantFacts = formatFacts(relevantFactsData);

Expand Down
10 changes: 10 additions & 0 deletions src/test/getOrCreateRelationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 63a15cc

Please sign in to comment.