use std::sync::Arc;
use std::time::Duration;
use adk_core::Agent;
use adk_session::SessionService;
use super::error::AcpServerError;
#[derive(Clone, Debug, Default)]
pub enum TransportConfig {
#[default]
Stdio,
Http {
bind_address: String,
port: u16,
},
}
#[derive(Clone)]
pub struct AcpServerConfig {
pub agent: Arc<dyn Agent>,
pub session_service: Arc<dyn SessionService>,
pub agent_name: String,
pub agent_description: String,
pub streaming: bool,
pub tool_use: bool,
pub tool_names: Vec<String>,
pub max_sessions: usize,
pub permission_timeout: Duration,
pub shutdown_timeout: Duration,
pub transport: TransportConfig,
}
pub struct AcpServerConfigBuilder {
agent: Option<Arc<dyn Agent>>,
session_service: Option<Arc<dyn SessionService>>,
agent_name: String,
agent_description: String,
streaming: bool,
tool_use: bool,
tool_names: Vec<String>,
max_sessions: usize,
permission_timeout: Duration,
shutdown_timeout: Duration,
transport: TransportConfig,
}
impl Default for AcpServerConfigBuilder {
fn default() -> Self {
Self::new()
}
}
impl AcpServerConfigBuilder {
pub fn new() -> Self {
Self {
agent: None,
session_service: None,
agent_name: "adk-agent".to_string(),
agent_description: String::new(),
streaming: true,
tool_use: false,
tool_names: Vec::new(),
max_sessions: 16,
permission_timeout: Duration::from_secs(120),
shutdown_timeout: Duration::from_secs(30),
transport: TransportConfig::Stdio,
}
}
pub fn agent(mut self, agent: Arc<dyn Agent>) -> Self {
self.agent = Some(agent);
self
}
pub fn session_service(mut self, svc: Arc<dyn SessionService>) -> Self {
self.session_service = Some(svc);
self
}
pub fn agent_name(mut self, name: impl Into<String>) -> Self {
self.agent_name = name.into();
self
}
pub fn agent_description(mut self, desc: impl Into<String>) -> Self {
self.agent_description = desc.into();
self
}
pub fn streaming(mut self, enabled: bool) -> Self {
self.streaming = enabled;
self
}
pub fn tool_use(mut self, enabled: bool) -> Self {
self.tool_use = enabled;
self
}
pub fn tool_names(mut self, names: Vec<String>) -> Self {
self.tool_names = names;
self
}
pub fn max_sessions(mut self, max: usize) -> Self {
self.max_sessions = max;
self
}
pub fn permission_timeout(mut self, timeout: Duration) -> Self {
self.permission_timeout = timeout;
self
}
pub fn shutdown_timeout(mut self, timeout: Duration) -> Self {
self.shutdown_timeout = timeout;
self
}
pub fn transport(mut self, transport: TransportConfig) -> Self {
self.transport = transport;
self
}
pub fn build(self) -> Result<AcpServerConfig, AcpServerError> {
let agent =
self.agent.ok_or_else(|| AcpServerError::Internal("agent is required".to_string()))?;
let session_service = self
.session_service
.ok_or_else(|| AcpServerError::Internal("session_service is required".to_string()))?;
if self.max_sessions == 0 {
return Err(AcpServerError::Internal(
"max_sessions must be greater than 0".to_string(),
));
}
if self.permission_timeout.is_zero() {
return Err(AcpServerError::Internal(
"permission_timeout must be greater than 0".to_string(),
));
}
if self.shutdown_timeout.is_zero() {
return Err(AcpServerError::Internal(
"shutdown_timeout must be greater than 0".to_string(),
));
}
Ok(AcpServerConfig {
agent,
session_service,
agent_name: self.agent_name,
agent_description: self.agent_description,
streaming: self.streaming,
tool_use: self.tool_use,
tool_names: self.tool_names,
max_sessions: self.max_sessions,
permission_timeout: self.permission_timeout,
shutdown_timeout: self.shutdown_timeout,
transport: self.transport,
})
}
}