Skip to content

Commit

Permalink
feat: add taskId to sort result in chat window #78
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydzhou committed Apr 28, 2023
1 parent 8d3e498 commit 8100e9e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
36 changes: 23 additions & 13 deletions src/components/AutonomousAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AutonomousAgent {
name: string;
goal: string;
tasks: string[] = [];
taskIds: {[key: string]: string} = {};
completedTasks: string[] = [];
modelSettings: ModelSettings;
isRunning = true;
Expand Down Expand Up @@ -61,9 +62,10 @@ class AutonomousAgent {
// Initialize by getting tasks
try {
this.tasks = await this.getInitialTasks();
this.genTaskId();
for (const task of this.tasks) {
await new Promise((r) => setTimeout(r, TIMOUT_SHORT));
this.sendTaskMessage(task);
this.sendTaskMessage(task, this.taskIds[task]);
}
} catch (e) {
console.log(e);
Expand All @@ -75,6 +77,10 @@ class AutonomousAgent {
await this.loop();
}

genTaskId() {
this.tasks.forEach(task => this.taskIds[task] = this.taskIds[task] || v4())
}

async loop() {
console.log(`Loop ${this.numLoops}`);
console.log(this.tasks);
Expand Down Expand Up @@ -104,14 +110,15 @@ class AutonomousAgent {
// Get and remove first task
this.completedTasks.push(this.tasks[0] || "");
const currentTask = this.tasks.shift();
this.sendThinkingMessage();
const currentTaskId = this.taskIds[currentTask]
this.sendThinkingMessage(currentTaskId);

const result = await this.executeTask(currentTask as string);
this.sendExecutionMessage(currentTask as string, result);
this.sendExecutionMessage(currentTask as string, result, currentTaskId);

// Wait before adding tasks
await new Promise((r) => setTimeout(r, TIMEOUT_LONG));
this.sendThinkingMessage();
this.sendThinkingMessage(currentTaskId);

// Add new tasks
try {
Expand All @@ -120,18 +127,19 @@ class AutonomousAgent {
result
);
this.tasks = newTasks.concat(this.tasks);
this.genTaskId();
for (const task of newTasks) {
await new Promise((r) => setTimeout(r, TIMOUT_SHORT));
this.sendTaskMessage(task);
this.sendTaskMessage(task, this.taskIds[task], currentTaskId);
}

if (newTasks.length == 0) {
this.sendActionMessage("task-marked-as-complete");
this.sendActionMessage("task-marked-as-complete", currentTaskId);
}
} catch (e) {
console.log(e);
this.sendErrorMessage(`errors.adding-additional-task`);
this.sendActionMessage("task-marked-as-complete");
this.sendActionMessage("task-marked-as-complete", currentTaskId);
}

await this.loop();
Expand Down Expand Up @@ -271,31 +279,33 @@ class AutonomousAgent {
});
}

sendThinkingMessage() {
this.sendMessage({ type: "thinking", value: "" });
sendThinkingMessage(taskId?: string) {
this.sendMessage({ type: "thinking", value: "", taskId });
}

sendTaskMessage(task: string) {
this.sendMessage({ type: "task", value: task });
sendTaskMessage(task: string, taskId: string, parentTaskId?: string) {
this.sendMessage({ type: "task", value: task, taskId, parentTaskId });
}

sendErrorMessage(error: string) {
this.sendMessage({ type: "system", value: error });
}

sendExecutionMessage(task: string, execution: string) {
sendExecutionMessage(task: string, execution: string, taskId: string) {
this.sendMessage({
type: "action",
info: `Executing "${task}"`,
value: execution,
taskId,
});
}

sendActionMessage(message: string) {
sendActionMessage(message: string, taskId: string) {
this.sendMessage({
type: "action",
info: message,
value: "",
taskId,
});
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ const Home: NextPage = () => {
}, [agent]);

const handleAddMessage = (message: Message) => {
setMessages((prev) => [...prev, message]);
setMessages((prev) => {
const index = prev.findLastIndex(i => i.taskId && i.taskId === message.taskId)
prev.splice(index > -1 ? index + 1 : prev.length, 0, message)
return [...prev]
});
};

const tasks = messages.filter((message) => message.type === "task");
Expand Down
2 changes: 2 additions & 0 deletions src/types/agentTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { z } from "zod";

export const messageParser = z.object({
taskId: z.string().optional(),
parentTaskId: z.string().optional(),
type: z.enum(["goal", "thinking", "task", "action", "system"]),
info: z.string().optional(),
value: z.string(),
Expand Down

0 comments on commit 8100e9e

Please sign in to comment.