Skip to content

A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.

TypeScript

fix: keep goal-planning ideas attached to their original goal (#70)

wangtaod7627f70a923
diff --git a/apps/server/src/engine/service.ts b/apps/server/src/engine/service.ts
index b3ed502..4354887 100644
--- a/apps/server/src/engine/service.ts
+++ b/apps/server/src/engine/service.ts
@@ -656,11 +656,16 @@ export class AgentService {
if (idea?.status !== "accepted") return idea;
}
if (idea.taskId && (await this.db.get(owner, "tasks", idea.taskId))) return idea;
- const goal = await this.createGoal(
- owner,
- { title: idea.title, description: idea.reason },
- hash(`idea-goal:${id}`),
- );
+ const goalId =
+ typeof idea.input.goalId === "string"
+ ? idea.input.goalId
+ : (
+ await this.createGoal(
+ owner,
+ { title: idea.title, description: idea.reason },
+ hash(`idea-goal:${id}`),
+ )
+ ).id;
const task = await this.createTask(
owner,
{
@@ -668,7 +673,7 @@ export class AgentService {
prompt: idea.prompt,
kind: idea.kind,
input: idea.input,
- goalId: goal.id,
+ goalId,
},
`idea:${id}`,
);
diff --git a/tests/workflows.test.ts b/tests/workflows.test.ts
index 99d523f..cafc544 100644
--- a/tests/workflows.test.ts
+++ b/tests/workflows.test.ts
@@ -5,7 +5,13 @@ import { join } from "node:path";
import { after, before, test } from "node:test";
import { createApp } from "../apps/server/src/app.ts";
import { createStore, type Store } from "../apps/server/src/db.ts";
-import type { AgentNotification, AgentTask, Idea, Monitor } from "../packages/domain/src/agent.ts";
+import type {
+ AgentNotification,
+ AgentTask,
+ Goal,
+ Idea,
+ Monitor,
+} from "../packages/domain/src/agent.ts";
import type { ActionProposal, Artifact } from "../packages/domain/src/index.ts";
import { createSamplePdf } from "../packages/integrations/src/pdf.ts";
@@ -219,6 +225,60 @@ test("ideas ignore sent replies while retaining unfinished incoming requests", a
assert.ok(!ideas.some((idea) => idea.input.messageId === sent.id));
});
+test("accepting a goal idea keeps the task attached to the original goal", async () => {
+ const ideaOwner = "goal-plan-ideas";
+ const goal = await server.agent.createGoal(ideaOwner, {
+ title: "Plan a walking routine",
+ description: "Walk three times each week",
+ category: "Health",
+ });
+ const ideas = await server.agent.refreshIdeas(ideaOwner);
+ const idea = ideas.find((candidate) => candidate.input.goalId === goal.id);
+ assert.ok(idea);
+
+ const accepted = await server.agent.decideIdea(ideaOwner, idea.id, "accept");
+ assert.ok(accepted?.taskId);
+ const task = await server.agent.getTask(ideaOwner, accepted.taskId);
+ assert.equal(task.goalId, goal.id);
+ assert.deepEqual(await db.list<Goal>(ideaOwner, "goals"), [goal]);
+
+ const retried = await server.agent.decideIdea(ideaOwner, idea.id, "accept");
+ assert.equal(retried?.taskId, task.id);
+ assert.equal((await db.list<AgentTask>(ideaOwner, "tasks")).length, 1);
+ assert.deepEqual(await db.list<Goal>(ideaOwner, "goals"), [goal]);
+
+ await server.agent.updateGoal(ideaOwner, goal.id, { status: "paused" });
+ assert.equal((await server.agent.getTask(ideaOwner, task.id)).status, "paused");
+});
+
+test("accepting an idea without a goal still creates one goal and task", async () => {
+ const ideaOwner = "standalone-ideas";
+ const idea: Idea = {
+ id: "standalone-plan",
+ title: "Plan a weekend walk",
+ reason: "Make time to get outdoors",
+ prompt: "Plan a weekend walk",
+ kind: "plan",
+ input: {},
+ evidence: [],
+ status: "new",
+ createdAt: new Date().toISOString(),
+ };
+ await db.put(ideaOwner, "ideas", idea);
+ const accepted = await server.agent.decideIdea(ideaOwner, idea.id, "accept");
+ assert.ok(accepted?.taskId);
+ await server.agent.decideIdea(ideaOwner, idea.id, "accept");
+
+ const goals = await db.list<Goal>(ideaOwner, "goals");
+ assert.equal(goals.length, 1);
+ assert.equal(goals[0].title, idea.title);
+ assert.equal(goals[0].description, idea.reason);
+ const tasks = await db.list<AgentTask>(ideaOwner, "tasks");
+ assert.equal(tasks.length, 1);
+ assert.equal(tasks[0].id, accepted.taskId);
+ assert.equal(tasks[0].goalId, goals[0].id);
+});
+
test("cancelling a task denies its pending action", async () => {
const { task, action } = await documentTask();
await server.agent.control(owner, task.id, "cancel");