2026-07-11 13:16:10 +07:00
|
|
|
use tokio::sync::mpsc;
|
2026-07-11 18:23:01 +07:00
|
|
|
use crate::dto::chat::message::ChatMessage;
|
2026-07-11 20:21:59 +07:00
|
|
|
use crate::tool::{all_tools, tool_is_risky};
|
2026-07-11 13:16:10 +07:00
|
|
|
use super::context::SubagentContext;
|
|
|
|
|
use super::event::SubagentEvent;
|
|
|
|
|
|
|
|
|
|
pub const MAX_AGENT_STEPS: usize = 25;
|
|
|
|
|
|
2026-07-11 18:23:01 +07:00
|
|
|
fn tool_call_from_response(response: &str) -> Vec<String> {
|
|
|
|
|
let mut calls = Vec::new();
|
|
|
|
|
for line in response.lines() {
|
|
|
|
|
let trimmed = line.trim();
|
|
|
|
|
if let Some(tool_call) = trimmed.strip_prefix("Tool: ") {
|
|
|
|
|
calls.push(tool_call.to_string());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
calls
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn run_subagent(ctx: SubagentContext, tx: mpsc::Sender<SubagentEvent>) -> anyhow::Result<String> {
|
|
|
|
|
let mut output = String::new();
|
2026-07-11 18:23:01 +07:00
|
|
|
let mut messages: Vec<ChatMessage> = Vec::new();
|
|
|
|
|
messages.push(ChatMessage::system(ctx.system_prompt.clone()));
|
|
|
|
|
|
2026-07-11 20:21:59 +07:00
|
|
|
let tool_ctx = crate::tool::ToolCtx::builder()
|
|
|
|
|
.session_dir(ctx.session_dir.clone())
|
|
|
|
|
.origin(crate::app::state::types::Origin::SubAgent)
|
|
|
|
|
.build();
|
|
|
|
|
|
2026-07-11 18:23:01 +07:00
|
|
|
let max_steps = ctx.max_steps.min(MAX_AGENT_STEPS);
|
|
|
|
|
for step in 0..max_steps {
|
2026-07-11 22:10:17 +07:00
|
|
|
let api_key = std::env::var("API_KEY").unwrap_or_default();
|
|
|
|
|
let model = std::env::var("MODEL").unwrap_or_default();
|
2026-07-11 18:23:01 +07:00
|
|
|
|
2026-07-11 22:10:17 +07:00
|
|
|
let client = crate::service::provider::LlmClient::new(api_key, model);
|
2026-07-11 18:23:01 +07:00
|
|
|
let response = match client.chat(&messages) {
|
|
|
|
|
Ok(r) => r,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
let _ = tx.blocking_send(SubagentEvent::StepFailed {
|
2026-07-11 23:45:13 +07:00
|
|
|
_step: step,
|
|
|
|
|
_error: e.to_string(),
|
2026-07-11 18:23:01 +07:00
|
|
|
});
|
|
|
|
|
anyhow::bail!("subagent call failed at step {}: {}", step, e);
|
|
|
|
|
}
|
2026-07-11 13:16:10 +07:00
|
|
|
};
|
2026-07-11 18:23:01 +07:00
|
|
|
|
|
|
|
|
let _ = tx.blocking_send(SubagentEvent::ToolCall {
|
2026-07-11 23:45:13 +07:00
|
|
|
_tool: "api".to_string(),
|
|
|
|
|
_args: serde_json::json!({"response": response}),
|
2026-07-11 18:23:01 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let tool_calls = tool_call_from_response(&response);
|
|
|
|
|
if tool_calls.is_empty() {
|
|
|
|
|
output.push_str(&response);
|
|
|
|
|
output.push('\n');
|
|
|
|
|
let _ = tx.blocking_send(SubagentEvent::StepCompleted {
|
2026-07-11 23:45:13 +07:00
|
|
|
_step: step,
|
|
|
|
|
_output: response.clone(),
|
2026-07-11 18:23:01 +07:00
|
|
|
});
|
|
|
|
|
if !response.contains("Tool:") {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2026-07-11 20:21:59 +07:00
|
|
|
let tools = all_tools();
|
2026-07-11 18:23:01 +07:00
|
|
|
for tool_name in &tool_calls {
|
2026-07-11 20:21:59 +07:00
|
|
|
let explicitly_allowed = ctx.allowed_tools.contains(tool_name);
|
|
|
|
|
let generally_allowed = ctx.allowed_tools.is_empty() || explicitly_allowed;
|
|
|
|
|
|
|
|
|
|
if !generally_allowed {
|
2026-07-11 18:23:01 +07:00
|
|
|
let msg = format!("tool '{}' not allowed for this subagent", tool_name);
|
2026-07-11 20:21:59 +07:00
|
|
|
messages.push(ChatMessage::tool_result(tool_name.clone(), msg.clone()));
|
|
|
|
|
let _ = tx.blocking_send(SubagentEvent::ToolResult {
|
2026-07-11 23:45:13 +07:00
|
|
|
_tool: tool_name.clone(),
|
|
|
|
|
_output: msg,
|
2026-07-11 20:21:59 +07:00
|
|
|
});
|
2026-07-11 18:23:01 +07:00
|
|
|
continue;
|
|
|
|
|
}
|
2026-07-11 20:21:59 +07:00
|
|
|
|
|
|
|
|
if tool_is_risky(tool_name) && !explicitly_allowed {
|
|
|
|
|
let msg = format!("risky tool '{}' requires explicit permission; not allowed for this subagent", tool_name);
|
|
|
|
|
messages.push(ChatMessage::tool_result(tool_name.clone(), msg.clone()));
|
|
|
|
|
let _ = tx.blocking_send(SubagentEvent::ToolResult {
|
2026-07-11 23:45:13 +07:00
|
|
|
_tool: tool_name.clone(),
|
|
|
|
|
_output: msg,
|
2026-07-11 20:21:59 +07:00
|
|
|
});
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let result = match tools.iter().find(|t| t.name() == *tool_name) {
|
|
|
|
|
Some(tool) => tool.run(&tool_ctx, &serde_json::json!({})),
|
|
|
|
|
None => Err(anyhow::anyhow!("tool '{}' not found", tool_name)),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match result {
|
|
|
|
|
Ok(output_text) => {
|
|
|
|
|
let _ = tx.blocking_send(SubagentEvent::ToolResult {
|
2026-07-11 23:45:13 +07:00
|
|
|
_tool: tool_name.clone(),
|
|
|
|
|
_output: output_text,
|
2026-07-11 20:21:59 +07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
let msg = format!("tool '{}' failed: {}", tool_name, e);
|
|
|
|
|
let _ = tx.blocking_send(SubagentEvent::ToolResult {
|
2026-07-11 23:45:13 +07:00
|
|
|
_tool: tool_name.clone(),
|
|
|
|
|
_output: msg,
|
2026-07-11 20:21:59 +07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-11 18:23:01 +07:00
|
|
|
}
|
|
|
|
|
let _ = tx.blocking_send(SubagentEvent::StepCompleted {
|
2026-07-11 23:45:13 +07:00
|
|
|
_step: step,
|
|
|
|
|
_output: response.clone(),
|
2026-07-11 18:23:01 +07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let assistant_msg = ChatMessage::assistant(Some(response.clone()));
|
|
|
|
|
messages.push(assistant_msg);
|
|
|
|
|
let user_msg = ChatMessage::user("Continue with the next step based on the tool results above.".to_string());
|
|
|
|
|
messages.push(user_msg);
|
2026-07-11 13:16:10 +07:00
|
|
|
}
|
2026-07-11 18:23:01 +07:00
|
|
|
|
2026-07-11 23:45:13 +07:00
|
|
|
let _ = tx.blocking_send(SubagentEvent::Completed { _output: output.clone() });
|
2026-07-11 13:16:10 +07:00
|
|
|
Ok(output)
|
|
|
|
|
}
|