use agent manager for subagent (#4828)

This commit is contained in:
Yingjie He
2025-10-06 21:17:38 -07:00
committed by GitHub
parent bb1d24de80
commit 0600cb4389
12 changed files with 221 additions and 452 deletions
@@ -12,21 +12,13 @@ use tokio::sync::mpsc::Sender;
use tokio_util::sync::CancellationToken;
pub async fn execute_tasks(
input: Value,
task_ids: Vec<String>,
execution_mode: ExecutionMode,
notifier: Sender<ServerNotification>,
task_config: TaskConfig,
tasks_manager: &TasksManager,
cancellation_token: Option<CancellationToken>,
) -> Result<Value, String> {
let task_ids: Vec<String> = serde_json::from_value(
input
.get("task_ids")
.ok_or("Missing task_ids field")?
.clone(),
)
.map_err(|e| format!("Failed to parse task_ids: {}", e))?;
let tasks = tasks_manager.get_tasks(&task_ids).await?;
let task_count = tasks.len();
@@ -1,14 +1,12 @@
use std::borrow::Cow;
use rmcp::model::{Content, ErrorCode, ErrorData, ServerNotification, Tool, ToolAnnotations};
use serde_json::Value;
use crate::agents::subagent_task_config::TaskConfig;
use crate::agents::{
subagent_execution_tool::lib::execute_tasks,
subagent_execution_tool::task_types::ExecutionMode,
subagent_execution_tool::tasks_manager::TasksManager, tool_execution::ToolCallResult,
};
use rmcp::model::{Content, ErrorCode, ErrorData, ServerNotification, Tool, ToolAnnotations};
use rmcp::object;
use tokio::sync::mpsc;
use tokio_stream;
@@ -62,7 +60,8 @@ pub fn create_subagent_execute_task_tool() -> Tool {
}
pub async fn run_tasks(
execute_data: Value,
task_ids: Vec<String>,
execution_mode: ExecutionMode,
task_config: TaskConfig,
tasks_manager: &TasksManager,
cancellation_token: Option<CancellationToken>,
@@ -71,14 +70,8 @@ pub async fn run_tasks(
let tasks_manager_clone = tasks_manager.clone();
let result_future = async move {
let execute_data_clone = execute_data.clone();
let execution_mode = execute_data_clone
.get("execution_mode")
.and_then(|v| serde_json::from_value::<ExecutionMode>(v.clone()).ok())
.unwrap_or_default();
match execute_tasks(
execute_data,
task_ids,
execution_mode,
notification_tx,
task_config,
@@ -74,7 +74,7 @@ async fn handle_inline_recipe_task(
mut task_config: TaskConfig,
cancellation_token: CancellationToken,
) -> Result<Value, String> {
use crate::agents::subagent_handler::run_complete_subagent_task_with_options;
use crate::agents::subagent_handler::run_complete_subagent_task;
use crate::recipe::Recipe;
let recipe_value = task
@@ -91,14 +91,23 @@ async fn handle_inline_recipe_task(
.and_then(|v| v.as_bool())
.unwrap_or(false);
task_config.extensions = recipe.extensions.clone();
if let Some(exts) = recipe.extensions {
if !exts.is_empty() {
task_config.extensions = exts.clone();
}
}
let instruction = recipe
.instructions
.or(recipe.prompt)
.ok_or_else(|| "No instructions or prompt in recipe".to_string())?;
let result = tokio::select! {
result = run_complete_subagent_task_with_options(instruction, task_config, return_last_only) => result,
result = run_complete_subagent_task(
instruction,
task_config,
return_last_only,
) => result,
_ = cancellation_token.cancelled() => {
return Err("Task cancelled".to_string());
}