use std::sync::Arc;
use adk_core::{AdkError, Result, Tool, ToolContext};
use async_trait::async_trait;
use serde_json::{Value, json};
use tracing::debug;
use crate::connection::{AcpAgentConfig, prompt_agent};
pub struct AcpAgentTool {
name: String,
description: String,
config: AcpAgentConfig,
}
impl AcpAgentTool {
pub fn new(command: impl Into<String>) -> Self {
let command = command.into();
let name = command.split_whitespace().next().unwrap_or("acp-agent").to_string();
Self {
name: name.clone(),
description: format!("Delegate tasks to the {name} ACP agent"),
config: AcpAgentConfig::new(&command),
}
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = name.into();
self
}
pub fn description(mut self, desc: impl Into<String>) -> Self {
self.description = desc.into();
self
}
pub fn working_dir(mut self, path: impl Into<std::path::PathBuf>) -> Self {
self.config.working_dir = path.into();
self
}
pub fn auto_approve(mut self, approve: bool) -> Self {
self.config.auto_approve = approve;
self
}
}
impl std::fmt::Debug for AcpAgentTool {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AcpAgentTool")
.field("name", &self.name)
.field("command", &self.config.command)
.finish()
}
}
#[async_trait]
impl Tool for AcpAgentTool {
fn name(&self) -> &str {
&self.name
}
fn description(&self) -> &str {
&self.description
}
fn parameters_schema(&self) -> Option<Value> {
Some(json!({
"type": "object",
"properties": {
"prompt": {
"type": "string",
"description": "The task or question to send to the ACP agent"
}
},
"required": ["prompt"]
}))
}
async fn execute(&self, _ctx: Arc<dyn ToolContext>, args: Value) -> Result<Value> {
let prompt = args
.get("prompt")
.and_then(|v| v.as_str())
.ok_or_else(|| AdkError::tool("AcpAgentTool requires a 'prompt' string field"))?;
debug!(tool = %self.name, prompt_len = prompt.len(), "invoking ACP agent");
let response = prompt_agent(&self.config, prompt)
.await
.map_err(|e| AdkError::tool(format!("ACP agent error: {e}")))?;
Ok(json!({ "response": response }))
}
}