Skip to content

Commit

Permalink
Merge pull request #45 from JoinTheAlliance/fix/goal-update
Browse files Browse the repository at this point in the history
Fix/goal update
  • Loading branch information
lalalune authored Mar 5, 2024
2 parents d669435 + fdcaa64 commit a953f35
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bgent",
"version": "0.0.27",
"version": "0.0.28",
"private": false,
"description": "bgent. because agent was taken.",
"type": "module",
Expand Down
11 changes: 8 additions & 3 deletions src/lib/evaluators/__tests__/goal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { populateMemories } from "../../../test/populateMemories";
import { createGoal, getGoals } from "../../goals";
import { getRelationship } from "../../relationships";
import { type BgentRuntime } from "../../runtime";
import { Goal, GoalStatus, Objective, type Message } from "../../types";
import { Goal, GoalStatus, Objective, type Message, State } from "../../types";
import evaluator from "../goal";
import { defaultActions } from "../../actions";
import { zeroUuid } from "../../constants";
Expand Down Expand Up @@ -100,14 +100,17 @@ describe("Goals Evaluator", () => {
};

// Process the message with the goal evaluator
await evaluator.handler(runtime, message);
await evaluator.handler(runtime, message, {} as unknown as State, {
onlyInProgress: false,
});

// Fetch the updated goal to verify the objectives and status were updated
const updatedGoals = await getGoals({
runtime,
userIds: [user.id as UUID, zeroUuid],
onlyInProgress: false,
});

const updatedTestGoal = updatedGoals.find(
(goal: Goal) => goal.name === "Test Goal",
);
Expand Down Expand Up @@ -150,7 +153,9 @@ describe("Goals Evaluator", () => {
room_id,
};

await evaluator.handler(runtime, message);
await evaluator.handler(runtime, message, {} as State, {
onlyInProgress: false,
});

const goals = await getGoals({
runtime,
Expand Down
34 changes: 28 additions & 6 deletions src/lib/evaluators/goal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { composeContext } from "../context";
import { getGoals } from "../goals";
import { type BgentRuntime } from "../runtime";
import { type Goal, type Message, type State } from "../types";
import { Objective, type Goal, type Message, type State } from "../types";
import { parseJsonArrayFromText } from "../utils";

const template = `TASK: Update Goal
Expand Down Expand Up @@ -44,15 +44,17 @@ Response format should be:
async function handler(
runtime: BgentRuntime,
message: Message,
state: State | undefined,
options: { [key: string]: unknown } = { onlyInProgress: true },
): Promise<Goal[]> {
// get goals
let goalsData = await getGoals({
runtime,
userIds: message.userIds,
onlyInProgress: false,
onlyInProgress: options.onlyInProgress as boolean,
});

const state = (await runtime.composeState(message)) as State;
state = (await runtime.composeState(message)) as State;
const context = composeContext({
state,
template,
Expand All @@ -79,22 +81,42 @@ async function handler(
.map((goal: Goal) => {
const update = updates?.find((u) => u.id === goal.id);
if (update) {
const objectives = goal.objectives;

// for each objective in update.objectives, find the objective with the same description in 'objectives' and set the 'completed' value to the update.objectives value
for (const objective of objectives) {
const updatedObjective = update.objectives.find(
(o: Objective) => o.description === objective.description,
);
if (updatedObjective) {
objective.completed = updatedObjective.completed;
}
}

return {
...goal,
...update,
objectives: { ...goal.objectives, ...update.objectives },
objectives: [...goal.objectives, ...update.objectives],
}; // Merging the update into the existing goal
} else {
console.warn("**** ID NOT FOUND");
}
return null; // No update for this goal
})
.filter(Boolean);

// Update goals in the database
for (const goal of updatedGoals) {
await runtime.supabase
const id = goal.id;
// delete id from goal
if (goal.id) delete goal.id;
const { error } = await runtime.supabase
.from("goals")
.update({ ...goal })
.match({ id: goal.id });
.match({ id });
if (error) {
console.log("ERROR: " + JSON.stringify(error));
}
}

return updatedGoals; // Return updated goals for further processing or logging
Expand Down
15 changes: 8 additions & 7 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export type Handler = (
runtime: BgentRuntime,
message: Message,
state?: State,
options?: { [key: string]: unknown }, // additional options can be used for things like tests or state-passing on a chain
) => Promise<unknown>;

/**
Expand Down Expand Up @@ -190,11 +191,11 @@ export interface Provider {
* Represents a relationship between two users, including their IDs, the status of the relationship, and the room ID in which the relationship is established.
*/
export interface Relationship {
id: UUID
user_a: UUID
user_b: UUID
user_id: UUID
room_id: UUID
status: string
created_at?: string
id: UUID;
user_a: UUID;
user_b: UUID;
user_id: UUID;
room_id: UUID;
status: string;
created_at?: string;
}

0 comments on commit a953f35

Please sign in to comment.