-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Use command line to run sub agent and sub recipe (in sequence or parallel) #3190
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8320bf9
parallel execution
lifeizhou-ap 8f9392b
fixed task schema
lifeizhou-ap e63485b
run subrecipe
lifeizhou-ap bab0b98
print output lively
lifeizhou-ap daed8a6
run single task
lifeizhou-ap 9d2b4b6
fmt
lifeizhou-ap d5a45a9
merge main
lifeizhou-ap 614c43d
merge conflicts
lifeizhou-ap a7cab6e
fixed format
lifeizhou-ap 9b5414e
use the same output pattern as previous sub recipe implementation
lifeizhou-ap 17e04e5
moved the stateless functions to session utils module
lifeizhou-ap 684ba42
Revert "moved the stateless functions to session utils module"
lifeizhou-ap 741007b
Merge branch 'main' into lifei/try-parallel
lifeizhou-ap 0dc0b44
renamed to sub-recipe-execute-tool
lifeizhou-ap dbb6dbb
cleaned tool description
lifeizhou-ap 197d3d1
added extra property execution mode
lifeizhou-ap 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 hidden or 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 hidden or 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 hidden or 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
103 changes: 103 additions & 0 deletions
103
crates/goose/src/agents/sub_recipe_execution_tool/executor.rs
This file contains hidden or 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,103 @@ | ||
| use std::sync::atomic::{AtomicBool, AtomicUsize}; | ||
| use std::sync::Arc; | ||
| use tokio::sync::mpsc; | ||
| use tokio::time::Instant; | ||
|
|
||
| use crate::agents::sub_recipe_execution_tool::lib::{ | ||
| Config, ExecutionResponse, ExecutionStats, Task, TaskResult, | ||
| }; | ||
| use crate::agents::sub_recipe_execution_tool::tasks::process_task; | ||
| use crate::agents::sub_recipe_execution_tool::workers::{run_scaler, spawn_worker, SharedState}; | ||
|
|
||
| pub async fn execute_single_task(task: &Task, config: Config) -> ExecutionResponse { | ||
| let start_time = Instant::now(); | ||
| let result = process_task(task, config.timeout_seconds).await; | ||
|
|
||
| let execution_time = start_time.elapsed().as_millis(); | ||
| let completed = if result.status == "success" { 1 } else { 0 }; | ||
| let failed = if result.status == "failed" { 1 } else { 0 }; | ||
|
|
||
| ExecutionResponse { | ||
| status: "completed".to_string(), | ||
| results: vec![result], | ||
| stats: ExecutionStats { | ||
| total_tasks: 1, | ||
| completed, | ||
| failed, | ||
| execution_time_ms: execution_time, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // Main parallel execution function | ||
| pub async fn parallel_execute(tasks: Vec<Task>, config: Config) -> ExecutionResponse { | ||
| let start_time = Instant::now(); | ||
| let task_count = tasks.len(); | ||
|
|
||
| // Create channels | ||
| let (task_tx, task_rx) = mpsc::channel::<Task>(task_count); | ||
| let (result_tx, mut result_rx) = mpsc::channel::<TaskResult>(task_count); | ||
|
|
||
| // Initialize shared state | ||
| let shared_state = Arc::new(SharedState { | ||
| task_receiver: Arc::new(tokio::sync::Mutex::new(task_rx)), | ||
| result_sender: result_tx, | ||
| active_workers: Arc::new(AtomicUsize::new(0)), | ||
| should_stop: Arc::new(AtomicBool::new(false)), | ||
| completed_tasks: Arc::new(AtomicUsize::new(0)), | ||
| }); | ||
|
|
||
| // Send all tasks to the queue | ||
| for task in tasks.clone() { | ||
| let _ = task_tx.send(task).await; | ||
| } | ||
| // Close sender so workers know when queue is empty | ||
| drop(task_tx); | ||
|
|
||
| // Start initial workers | ||
| let mut worker_handles = Vec::new(); | ||
| for i in 0..config.initial_workers { | ||
| let handle = spawn_worker(shared_state.clone(), i, config.timeout_seconds); | ||
| worker_handles.push(handle); | ||
| } | ||
|
|
||
| // Start the scaler | ||
| let scaler_state = shared_state.clone(); | ||
| let scaler_handle = tokio::spawn(async move { | ||
| run_scaler( | ||
| scaler_state, | ||
| task_count, | ||
| config.max_workers, | ||
| config.timeout_seconds, | ||
| ) | ||
| .await; | ||
| }); | ||
|
|
||
| // Collect results | ||
| let mut results = Vec::new(); | ||
| while let Some(result) = result_rx.recv().await { | ||
| results.push(result); | ||
| if results.len() >= task_count { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // Wait for scaler to finish | ||
| let _ = scaler_handle.await; | ||
|
|
||
| // Calculate stats | ||
| let execution_time = start_time.elapsed().as_millis(); | ||
| let completed = results.iter().filter(|r| r.status == "success").count(); | ||
| let failed = results.iter().filter(|r| r.status == "failed").count(); | ||
|
|
||
| ExecutionResponse { | ||
| status: "completed".to_string(), | ||
| results, | ||
| stats: ExecutionStats { | ||
| total_tasks: task_count, | ||
| completed, | ||
| failed, | ||
| execution_time_ms: execution_time, | ||
| }, | ||
| } | ||
| } |
This file contains hidden or 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,38 @@ | ||
| use crate::agents::sub_recipe_execution_tool::executor::execute_single_task; | ||
| pub use crate::agents::sub_recipe_execution_tool::executor::parallel_execute; | ||
| pub use crate::agents::sub_recipe_execution_tool::types::{ | ||
| Config, ExecutionResponse, ExecutionStats, Task, TaskResult, | ||
| }; | ||
|
|
||
| use serde_json::Value; | ||
|
|
||
| pub async fn execute_tasks(input: Value, execution_mode: &str) -> Result<Value, String> { | ||
| let tasks: Vec<Task> = | ||
| serde_json::from_value(input.get("tasks").ok_or("Missing tasks field")?.clone()) | ||
| .map_err(|e| format!("Failed to parse tasks: {}", e))?; | ||
|
|
||
| let config: Config = if let Some(config_value) = input.get("config") { | ||
| serde_json::from_value(config_value.clone()) | ||
| .map_err(|e| format!("Failed to parse config: {}", e))? | ||
| } else { | ||
| Config::default() | ||
| }; | ||
| let task_count = tasks.len(); | ||
| match execution_mode { | ||
| "sequential" => { | ||
| if task_count == 1 { | ||
| let response = execute_single_task(&tasks[0], config).await; | ||
| serde_json::to_value(response) | ||
| .map_err(|e| format!("Failed to serialize response: {}", e)) | ||
| } else { | ||
| Err("Sequential execution mode requires exactly one task".to_string()) | ||
| } | ||
| } | ||
| "parallel" => { | ||
| let response = parallel_execute(tasks, config).await; | ||
| serde_json::to_value(response) | ||
| .map_err(|e| format!("Failed to serialize response: {}", e)) | ||
| } | ||
| _ => Err("Invalid execution mode".to_string()), | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| mod executor; | ||
| pub mod lib; | ||
| pub mod sub_recipe_execute_task_tool; | ||
| mod tasks; | ||
| mod types; | ||
| mod workers; |
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.
shall we add the recipe description into the tool description?
Uh oh!
There was an error while loading. Please reload this page.
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.
Hi @wendytang This is a good idea!
However, I will hold on this change because at this point, the subrecipe does not have the content of the sub recipe file, so it does not know the subrecipe description. Parsing sub recipe file logic is currently in the goose-cli, we are going to move it into the goose package or parsing it before running sub-agent, and then we can get the description.
At the moment, without the sub recipe description in the tool description, it works fine. But definitely we will add this in when the parsing recipe file logic moves to goose package