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 sqlite and some sqljs tests #73

Merged
merged 2 commits into from
Mar 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
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 @@ -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");
}
Expand Down
15 changes: 1 addition & 14 deletions src/lib/actions/__tests__/elaborate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
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;
console.log("*** data", data);
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 @@ -501,6 +501,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 @@ -596,15 +597,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
44 changes: 24 additions & 20 deletions src/lib/adapters/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,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 @@ -461,43 +460,46 @@ export class SupabaseDatabaseAdapter extends DatabaseAdapter {
userA: UUID;
userB: UUID;
}): Promise<boolean> {
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,
Expand All @@ -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;
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: 1 addition & 1 deletion src/lib/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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.getRoomsForParticipants([
userA,
Expand Down
Loading