use super::{failure, Arguments, Tool, ToolContext, ToolError};
use crate::types::ToolResult;
use serde_json::{json, Map, Value};
pub const NAME: &str = "delegate";
pub struct DelegateTool;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DelegateRequest {
pub role: String,
pub task: String,
pub agent: Option<String>,
pub skill: Option<String>,
pub isolate: bool,
pub files: Vec<String>,
pub summary: Option<String>,
}
pub fn request(args: &Map<String, Value>) -> Result<DelegateRequest, ToolError> {
let mut arguments = Arguments::new(args);
let role = arguments.string("role", Some("assistant"));
let task = arguments.string("task", None);
let agent = arguments.optional_string("agent");
let skill = arguments.optional_string("skill");
let isolate = arguments.boolean("isolate", false);
let files = arguments.string_list("files");
let summary = arguments.optional_string("summary");
arguments.finish()?;
let filled = |value: Option<String>| value.filter(|text| !text.trim().is_empty());
Ok(DelegateRequest {
role,
task,
agent: filled(agent).map(|name| name.trim().to_owned()),
skill: filled(skill).map(|name| name.trim().to_owned()),
isolate,
files,
summary: filled(summary),
})
}
pub fn python_parameters() -> Value {
json!({
"properties": {
"role": {
"default": "assistant",
"description": "The sub-agent's role/persona.",
"title": "Role",
"type": "string",
},
"task": {
"description": "The self-contained task for the sub-agent.",
"title": "Task",
"type": "string",
},
},
"required": ["task"],
"type": "object",
})
}
impl Tool for DelegateTool {
fn name(&self) -> &str {
NAME
}
fn description(&self) -> &str {
"Delegate a focused subtask to a fresh sub-agent with a given role (e.g. 'researcher', \
'coder', 'reviewer'). The sub-agent runs to completion and returns its result. Use for \
well-scoped subtasks you want handled independently."
}
fn side_effecting(&self) -> bool {
false
}
fn parallel_safe(&self) -> bool {
false
}
fn parameters(&self) -> Value {
let mut schema = python_parameters();
let nullable_string = |title: &str, description: &str| {
json!({
"anyOf": [{"type": "string"}, {"type": "null"}],
"default": null,
"description": description,
"title": title,
})
};
let properties = schema["properties"]
.as_object_mut()
.expect("the schema has properties");
properties.insert(
"agent".into(),
nullable_string(
"Agent",
"The name of a specialist sub-agent to run, from the list in the system \
prompt. It brings its own instructions and tools, and `role` is ignored.",
),
);
properties.insert(
"skill".into(),
nullable_string(
"Skill",
"A skill to preload into the specialist sub-agent for this run (needs `agent`).",
),
);
properties.insert(
"isolate".into(),
json!({
"default": false,
"description": "Run the specialist sub-agent (needs `agent`) in a git worktree of \
HEAD, and get its diff back; nothing is applied. It does not see uncommitted \
changes, MCP tools are left out unless the specialty names them, and under the \
Docker sandbox git does not work inside it.",
"title": "Isolate",
"type": "boolean",
}),
);
properties.insert(
"files".into(),
json!({
"default": [],
"description": "Workspace paths whose contents are attached to the task.",
"items": {"type": "string"},
"title": "Files",
"type": "array",
}),
);
properties.insert(
"summary".into(),
nullable_string(
"Summary",
"Context for the sub-agent that the task does not say: what was decided, \
what was ruled out.",
),
);
schema
}
fn execute(
&self,
args: &Map<String, Value>,
_ctx: &ToolContext,
) -> Result<ToolResult, ToolError> {
request(args)?;
Ok(failure(self.name(), "Delegation is disabled.".into()))
}
}