Skip to content

Commit

Permalink
consolidate the populateMemories functions in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Feb 15, 2024
1 parent 865e67c commit 491f3bc
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 220 deletions.
2 changes: 1 addition & 1 deletion src/agents/cj/actions/__tests__/introduce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from "../../../../test/cache";
dotenv.config();

const zeroUuid = "00000000-0000-0000-0000-000000000000";
const zeroUuid = "00000000-0000-0000-0000-000000000000" as UUID;

describe("Introduce Action", () => {
test("Introduce the user", async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/agents/cj/evaluators/__tests__/details.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "../../../../test/cache";
dotenv.config();

const zeroUuid = "00000000-0000-0000-0000-000000000000";
const zeroUuid = "00000000-0000-0000-0000-000000000000" as UUID;

describe("User Details", () => {
test("Get user details", async () => {
Expand Down
34 changes: 4 additions & 30 deletions src/lib/__tests__/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { type User } from "@supabase/supabase-js";
import { type UUID } from "crypto";
import dotenv from "dotenv";
import { getCachedEmbedding, writeCachedEmbedding } from "../../test/cache";
import { createRuntime } from "../../test/createRuntime";
import { getRelationship } from "../relationships";
import { type BgentRuntime } from "../runtime";
import { populateMemories } from "../../test/populateMemories";

dotenv.config();

const zeroUuid = "00000000-0000-0000-0000-000000000000";
const zeroUuid = "00000000-0000-0000-0000-000000000000" as UUID;

describe("Actions", () => {
let user: User;
Expand Down Expand Up @@ -46,32 +46,6 @@ describe("Actions", () => {
]);
}

async function populateMemories(
conversations: Array<
(user_id: string) => Array<{ user_id: string; content: string }>
>,
) {
for (const conversation of conversations) {
for (const c of conversation(user?.id as UUID)) {
const existingEmbedding = getCachedEmbedding(c.content);
const bakedMemory = await runtime.messageManager.addEmbeddingToMemory({
user_id: c.user_id as UUID,
user_ids: [user?.id as UUID, zeroUuid],
content: {
content: c.content,
},
room_id: room_id as UUID,
embedding: existingEmbedding,
});
await runtime.messageManager.createMemory(bakedMemory);
if (!existingEmbedding) {
writeCachedEmbedding(c.content, bakedMemory.embedding as number[]);
await new Promise((resolve) => setTimeout(resolve, 200));
}
}
}
}

test("Action handler test: continue", async () => {
// TODO: test action handler with a message that should continue the conversation
// evaluate that the response action is a continue
Expand All @@ -84,7 +58,7 @@ describe("Actions", () => {
// room_id: room_id as UUID
// }

await populateMemories([
await populateMemories(runtime, user, room_id, [
// continue conversation 1 (should continue)
]);

Expand Down Expand Up @@ -113,7 +87,7 @@ describe("Actions", () => {
// TODO: test action handler with a message that should wait for a response
// evaluate that the response action is a wait

await populateMemories([
await populateMemories(runtime, user, room_id, [
// continue conversation 1 (should wait)
]);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/__tests__/evaluation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { type UUID } from "crypto";
describe("Evaluation Process", () => {
let runtime: BgentRuntime;
let user: User;
const zeroUuid = "00000000-0000-0000-0000-000000000000";
const zeroUuid = "00000000-0000-0000-0000-000000000000" as UUID;

beforeAll(async () => {
const setup = await createRuntime();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/__tests__/relationships.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { BgentRuntime } from "../runtime";

dotenv.config();

const zeroUuid = "00000000-0000-0000-0000-000000000000";
const zeroUuid = "00000000-0000-0000-0000-000000000000" as UUID;

describe("Relationships Module", () => {
let runtime: BgentRuntime;
Expand Down
96 changes: 58 additions & 38 deletions src/lib/actions/__tests__/continue.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import { type User } from "@supabase/supabase-js";
import { type UUID } from "crypto";
import dotenv from "dotenv";
import { getCachedEmbedding, writeCachedEmbedding } from "../../../test/cache";
import { createRuntime } from "../../../test/createRuntime";
import { GetTellMeAboutYourselfConversation1 } from "../../../test/data";
import { Goodbye1 } from "../../../test/data";
import { populateMemories } from "../../../test/populateMemories";
import { getRelationship } from "../../relationships";
import { type BgentRuntime } from "../../runtime";
import { Content, type Message } from "../../types";
import action from "../continue";

dotenv.config();

const zeroUuid = "00000000-0000-0000-0000-000000000000";
const zeroUuid = "00000000-0000-0000-0000-000000000000" as UUID;

describe("User Profile", () => {
let user: User | null;
let user: User;
let runtime: BgentRuntime;
let room_id: UUID | null;
let room_id: UUID;

afterAll(async () => {
await cleanup();
});

beforeAll(async () => {
const setup = await createRuntime();
user = setup.user;
user = setup.session.user;
runtime = setup.runtime;

const data = await getRelationship({
supabase: runtime.supabase,
userA: user?.id as UUID,
userA: user.id,
userB: zeroUuid,
});

Expand All @@ -40,42 +40,16 @@ describe("User Profile", () => {

async function cleanup() {
await runtime.summarizationManager.removeAllMemoriesByUserIds([
user?.id as UUID,
user.id as UUID,
zeroUuid,
]);
await runtime.messageManager.removeAllMemoriesByUserIds([
user?.id as UUID,
user.id as UUID,
zeroUuid,
]);
}

async function populateMemories(
conversations: Array<
(user_id: string) => Array<{ user_id: string; content: string }>
>,
) {
for (const conversation of conversations) {
for (const c of conversation(user?.id as UUID)) {
const existingEmbedding = getCachedEmbedding(c.content);
const bakedMemory = await runtime.messageManager.addEmbeddingToMemory({
user_id: c.user_id as UUID,
user_ids: [user?.id as UUID, zeroUuid],
content: {
content: c.content,
},
room_id: room_id as UUID,
embedding: existingEmbedding,
});
await runtime.messageManager.createMemory(bakedMemory);
if (!existingEmbedding) {
writeCachedEmbedding(c.content, bakedMemory.embedding as number[]);
await new Promise((resolve) => setTimeout(resolve, 200));
}
}
}
}

test("Test continue action", async () => {
test("Test repetition check on continue", async () => {
const message: Message = {
senderId: zeroUuid as UUID,
agentId: zeroUuid,
Expand All @@ -90,10 +64,56 @@ describe("User Profile", () => {

const handler = action.handler!;

await populateMemories([GetTellMeAboutYourselfConversation1]);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const GetContinueExample1 = (_user_id: UUID) => [
{
user_id: zeroUuid,
content:
"Hmm, let think for a second, I was going to tell you about something...",
action: "CONTINUE",
},
{
user_id: zeroUuid,
content:
"I remember now, I was going to tell you about my favorite food, which is pizza.",
action: "CONTINUE",
},
{
user_id: zeroUuid,
content: "I love pizza, it's so delicious.",
action: "CONTINUE",
},
];

await populateMemories(runtime, user, room_id, [GetContinueExample1]);

const result = (await handler(runtime, message)) as Content;

console.log("result", result);

expect(result.action).not.toBe("CONTINUE");
}, 60000);

test("Test if not continue", async () => {
// this is basically the same test as the one in ignore.test.ts
const message: Message = {
senderId: user?.id as UUID,
agentId: zeroUuid,
userIds: [user?.id as UUID, zeroUuid],
content: "Bye",
room_id: room_id as UUID,
};

const handler = action.handler!;

await populateMemories(runtime, user, room_id, [Goodbye1]);

const result = (await handler(runtime, message)) as Content;

expect(result.content.length).toBeGreaterThan(1);
console.log("IGNORE result", result);

expect(result.action).toBe("IGNORE");
}, 60000);

// test conditions where we would expect a wait or an ignore
});
44 changes: 9 additions & 35 deletions src/lib/actions/__tests__/ignore.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type User } from "@supabase/supabase-js";
import { type UUID } from "crypto";
import dotenv from "dotenv";
import { getCachedEmbedding, writeCachedEmbedding } from "../../../test/cache";
import { createRuntime } from "../../../test/createRuntime";
import {
GetTellMeAboutYourselfConversationTroll1,
Expand All @@ -12,15 +11,16 @@ import { getRelationship } from "../../relationships";
import { type BgentRuntime } from "../../runtime";
import { Content, type Message } from "../../types";
import action from "../continue";
import { populateMemories } from "../../../test/populateMemories";

dotenv.config();

const zeroUuid = "00000000-0000-0000-0000-000000000000";
export const zeroUuid = "00000000-0000-0000-0000-000000000000" as UUID;

describe("User Profile", () => {
let user: User;
let runtime: BgentRuntime;
let room_id: UUID | null;
let room_id: UUID;

afterAll(async () => {
await cleanup();
Expand Down Expand Up @@ -53,32 +53,6 @@ describe("User Profile", () => {
]);
}

async function populateMemories(
conversations: Array<
(user_id: string) => Array<{ user_id: string; content: string }>
>,
) {
for (const conversation of conversations) {
for (const c of conversation(user?.id as UUID)) {
const existingEmbedding = getCachedEmbedding(c.content);
const bakedMemory = await runtime.messageManager.addEmbeddingToMemory({
user_id: c.user_id as UUID,
user_ids: [user?.id as UUID, zeroUuid],
content: {
content: c.content,
},
room_id: room_id as UUID,
embedding: existingEmbedding,
});
await runtime.messageManager.createMemory(bakedMemory);
if (!existingEmbedding) {
writeCachedEmbedding(c.content, bakedMemory.embedding as number[]);
await new Promise((resolve) => setTimeout(resolve, 200));
}
}
}
}

test("Test ignore action", async () => {
const message: Message = {
senderId: zeroUuid as UUID,
Expand All @@ -88,7 +62,7 @@ describe("User Profile", () => {
room_id: room_id as UUID,
};

await populateMemories([]);
await populateMemories(runtime, user, room_id, []);

const handler = action.handler!;

Expand All @@ -105,7 +79,7 @@ describe("User Profile", () => {
room_id: room_id as UUID,
};

await populateMemories([GetTellMeAboutYourselfConversationTroll1]);
await populateMemories(runtime, user, room_id, [GetTellMeAboutYourselfConversationTroll1]);

const response = await runtime.handleRequest(message);

Expand Down Expand Up @@ -134,7 +108,7 @@ describe("User Profile", () => {
room_id: room_id as UUID,
};

await populateMemories([GetTellMeAboutYourselfConversationTroll2]);
await populateMemories(runtime, user, room_id, [GetTellMeAboutYourselfConversationTroll2]);

const response = await runtime.handleRequest(message);

Expand All @@ -154,16 +128,16 @@ describe("User Profile", () => {
expect((lastMessage.content as Content).action).toBe("IGNORE");
}, 60000);

test("Action handler test 3: response should be ignore", async () => {
test("Expect ignore", async () => {
const message: Message = {
senderId: user.id as UUID,
agentId: zeroUuid,
userIds: [user?.id as UUID, zeroUuid],
content: "",
content: "Bye",
room_id: room_id as UUID,
};

await populateMemories([Goodbye1]);
await populateMemories(runtime, user, room_id, [Goodbye1]);

const response = await runtime.handleRequest(message);

Expand Down
Loading

0 comments on commit 491f3bc

Please sign in to comment.