fix: clean up subagent (#3565)
This commit is contained in:
@@ -2,43 +2,63 @@ use crate::agents::subagent::SubAgent;
|
||||
use crate::agents::subagent_task_config::TaskConfig;
|
||||
use anyhow::Result;
|
||||
use mcp_core::ToolError;
|
||||
use rmcp::model::Content;
|
||||
use serde_json::Value;
|
||||
|
||||
/// Standalone function to run a complete subagent task
|
||||
pub async fn run_complete_subagent_task(
|
||||
task_arguments: Value,
|
||||
text_instruction: String,
|
||||
task_config: TaskConfig,
|
||||
) -> Result<Vec<Content>, ToolError> {
|
||||
// Parse arguments - using "task" as the main message parameter
|
||||
let text_instruction = task_arguments
|
||||
.get("text_instruction")
|
||||
.and_then(|v| v.as_str())
|
||||
.ok_or_else(|| ToolError::ExecutionError("Missing text_instruction parameter".to_string()))?
|
||||
.to_string();
|
||||
|
||||
) -> Result<String, anyhow::Error> {
|
||||
// Create the subagent with the parent agent's provider
|
||||
let (subagent, handle) = SubAgent::new(task_config.clone())
|
||||
let subagent = SubAgent::new(task_config.clone())
|
||||
.await
|
||||
.map_err(|e| ToolError::ExecutionError(format!("Failed to create subagent: {}", e)))?;
|
||||
|
||||
// Execute the subagent task
|
||||
let result = match subagent.reply_subagent(text_instruction, task_config).await {
|
||||
Ok(response) => {
|
||||
let response_text = response.as_concat_text();
|
||||
Ok(vec![Content::text(response_text)])
|
||||
}
|
||||
Err(e) => Err(ToolError::ExecutionError(format!(
|
||||
"Subagent execution failed: {}",
|
||||
e
|
||||
))),
|
||||
};
|
||||
let messages = subagent
|
||||
.reply_subagent(text_instruction, task_config)
|
||||
.await?;
|
||||
|
||||
// Clean up the subagent handle
|
||||
if let Err(e) = handle.await {
|
||||
tracing::debug!("Subagent handle cleanup error: {}", e);
|
||||
}
|
||||
// Extract all text content from all messages
|
||||
let all_text_content: Vec<String> = messages
|
||||
.iter()
|
||||
.flat_map(|message| {
|
||||
message.content.iter().filter_map(|content| {
|
||||
match content {
|
||||
crate::message::MessageContent::Text(text_content) => {
|
||||
Some(text_content.text.clone())
|
||||
}
|
||||
crate::message::MessageContent::ToolResponse(tool_response) => {
|
||||
// Extract text from tool response
|
||||
if let Ok(contents) = &tool_response.tool_result {
|
||||
let texts: Vec<String> = contents
|
||||
.iter()
|
||||
.filter_map(|content| {
|
||||
if let rmcp::model::RawContent::Text(raw_text_content) =
|
||||
&content.raw
|
||||
{
|
||||
Some(raw_text_content.text.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
if !texts.is_empty() {
|
||||
Some(format!("Tool result: {}", texts.join("\n")))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
let response_text = all_text_content.join("\n");
|
||||
|
||||
// Return the result
|
||||
result
|
||||
Ok(response_text)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user