-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
♻️ Refactor and add retries #857
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
b5dbe31
🐛 Remove isRunning from messageService
asim-shrestha 87f7ec0
🔥 AgentWork
asim-shrestha c375a22
🔥 Update AgentStore
asim-shrestha 392158e
🔥 Small refactor
asim-shrestha 3f8f7a6
🔥 Add with retries
asim-shrestha 2331a40
🔥 Update with retries
asim-shrestha 55f116e
🔥 Add network error message
asim-shrestha 8a40f34
🔥 Add on error and retries
asim-shrestha 4647590
Merge branch 'main' into pausing
asim-shrestha 819f46c
🔥 Fix issues
asim-shrestha 232eb9f
🔥 Update message service
asim-shrestha ea81885
🔥 Update with retries
asim-shrestha c2913c0
🔥 Update retries
asim-shrestha c5f8238
🔥 Fix waiting
asim-shrestha fe09791
🔥 Update prompts
asim-shrestha 89a563f
🔥 Update message
asim-shrestha 18567bd
🔥 Try finally
asim-shrestha 4b93b04
🔥 Remove comment
asim-shrestha 8d1a8b0
🔥 onError
asim-shrestha 6b0d830
🔥 Require onError
asim-shrestha 0d4d37b
🔥 Check for max loops and avoid retrying
asim-shrestha 751d25c
🔥 Update test
asim-shrestha 0450663
🔥 refactor
asim-shrestha a3a8481
🔥 Extract work to files
asim-shrestha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default interface AgentWork { | ||
run: () => Promise<void>; | ||
conclude: () => Promise<void>; | ||
next: () => AgentWork | undefined; | ||
onError: (e: unknown) => boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { Analysis } from "../analysis"; | ||
import type { Task } from "../../../types/task"; | ||
import type AutonomousAgent from "../autonomous-agent"; | ||
import type AgentWork from "./agent-work"; | ||
import ExecuteTaskWork from "./execute-task-work"; | ||
|
||
export default class AnalyzeTaskWork implements AgentWork { | ||
analysis: Analysis | undefined = undefined; | ||
|
||
constructor(private parent: AutonomousAgent, private task: Task) {} | ||
|
||
run = async () => { | ||
this.parent.messageService.startTaskMessage(this.task); | ||
this.task = this.parent.model.updateTaskStatus(this.task, "executing"); | ||
this.analysis = await this.parent.$api.analyzeTask(this.task.value); | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
conclude = async () => { | ||
if (this.analysis) { | ||
this.parent.messageService.sendAnalysisMessage(this.analysis); | ||
} else { | ||
this.parent.messageService.skipTaskMessage(this.task); | ||
} | ||
}; | ||
|
||
next = () => { | ||
if (!this.analysis) return undefined; | ||
return new ExecuteTaskWork(this.parent, this.task, this.analysis); | ||
}; | ||
|
||
onError = (e: unknown): boolean => { | ||
this.parent.messageService.sendErrorMessage(e); | ||
return false; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Task } from "../../../types/task"; | ||
import type AgentWork from "./agent-work"; | ||
import type AutonomousAgent from "../autonomous-agent"; | ||
|
||
export default class CreateTaskWork implements AgentWork { | ||
taskValues: string[] = []; | ||
|
||
constructor(private parent: AutonomousAgent, private task: Task, private result: string) {} | ||
|
||
run = async () => { | ||
this.taskValues = await this.parent.$api.getAdditionalTasks( | ||
{ | ||
current: this.task.value, | ||
remaining: this.parent.model.getRemainingTasks().map((task) => task.value), | ||
completed: this.parent.model.getCompletedTasks(), | ||
}, | ||
this.result | ||
); | ||
}; | ||
|
||
conclude = async () => { | ||
const TIMEOUT_LONG = 1000; | ||
await this.parent.createTasks(this.taskValues); | ||
await new Promise((r) => setTimeout(r, TIMEOUT_LONG)); | ||
}; | ||
|
||
next = () => undefined; | ||
|
||
// Ignore errors and simply avoid creating more tasks | ||
onError = (): boolean => true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { Task } from "../../../types/task"; | ||
import type { Analysis } from "../analysis"; | ||
import type { Message } from "../../../types/message"; | ||
import { v1 } from "uuid"; | ||
import { streamText } from "../../stream-utils"; | ||
import { toApiModelSettings } from "../../../utils/interfaces"; | ||
import type AgentWork from "./agent-work"; | ||
import type AutonomousAgent from "../autonomous-agent"; | ||
import CreateTaskWork from "./create-task-work"; | ||
|
||
export default class ExecuteTaskWork implements AgentWork { | ||
result = ""; | ||
|
||
constructor(private parent: AutonomousAgent, private task: Task, private analysis: Analysis) {} | ||
|
||
run = async () => { | ||
const executionMessage: Message = { | ||
...this.task, | ||
id: v1(), | ||
status: "completed", | ||
info: "Loading...", | ||
}; | ||
this.parent.messageService.sendMessage({ ...executionMessage, status: "completed" }); | ||
|
||
// TODO: this should be moved to the api layer | ||
await streamText( | ||
"/api/agent/execute", | ||
{ | ||
run_id: this.parent.$api.runId, | ||
goal: this.parent.model.getGoal(), | ||
task: this.task.value, | ||
analysis: this.analysis, | ||
model_settings: toApiModelSettings(this.parent.modelSettings), | ||
}, | ||
this.parent.$api.props.session?.accessToken || "", | ||
() => { | ||
executionMessage.info = ""; | ||
}, | ||
(text) => { | ||
executionMessage.info += text; | ||
this.parent.messageService.updateMessage(executionMessage); | ||
}, | ||
() => !this.parent.isRunning | ||
); | ||
this.result = executionMessage.info || ""; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
conclude = async () => { | ||
this.parent.model.updateTaskStatus(this.task, "completed"); | ||
this.parent.messageService.sendMessage({ ...this.task, status: "final" }); | ||
}; | ||
|
||
next = () => (this.result ? new CreateTaskWork(this.parent, this.task, "") : undefined); | ||
|
||
onError = (e: unknown): boolean => { | ||
this.parent.messageService.sendErrorMessage(e); | ||
return false; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type AutonomousAgent from "../autonomous-agent"; | ||
import type AgentWork from "./agent-work"; | ||
|
||
export default class StartGoalWork implements AgentWork { | ||
tasksValues: string[] = []; | ||
|
||
constructor(private parent: AutonomousAgent) {} | ||
|
||
run = async () => { | ||
this.parent.messageService.sendGoalMessage(this.parent.model.getGoal()); | ||
this.tasksValues = await this.parent.$api.getInitialTasks(); | ||
}; | ||
|
||
conclude = async () => { | ||
await this.parent.createTasks(this.tasksValues); | ||
}; | ||
|
||
onError = (e: unknown): boolean => { | ||
this.parent.messageService.sendErrorMessage(e); | ||
return false; | ||
}; | ||
|
||
next = () => undefined; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some jank with task coupling. It will display the task value after this 🙃