use std::sync::Arc;
use a2a_protocol_types::extensions::AgentExtension;
use a2a_protocol_types::{AgentCapabilities, AgentInterface, AgentSkill};
use axum::Router;
use axum::routing::{get, post};
use tokio::sync::RwLock;
use super::card::{CachedAgentCard, build_v1_agent_card, serve_agent_card, v1_agent_skills};
use super::convert::ADK_EXTENSION_URI;
use super::executor::V1Executor;
use super::jsonrpc_handler::jsonrpc_handler;
use super::processor::OutputMode;
use super::push::{NoOpPushNotificationSender, PushNotificationSender};
use super::request_handler::RequestHandler;
use super::rest_handler::rest_router;
use super::task_store::{InMemoryTaskStore, TaskStore};
use super::version::version_negotiation;
pub const AGENT_CARD_PATH: &str = "/.well-known/agent-card.json";
pub const DEFAULT_JSONRPC_PATH: &str = "/a2a/v1";
pub const DEFAULT_REST_PATH: &str = "/a2a/v1/rest";
pub struct A2aV1Server {
handler: Arc<RequestHandler>,
card: Arc<RwLock<CachedAgentCard>>,
jsonrpc_path: String,
rest_path: String,
}
impl A2aV1Server {
pub fn builder(
runner_config: Arc<adk_runner::RunnerConfig>,
base_url: impl Into<String>,
) -> A2aV1ServerBuilder {
A2aV1ServerBuilder {
runner_config,
base_url: base_url.into().trim_end_matches('/').to_string(),
jsonrpc_path: DEFAULT_JSONRPC_PATH.to_string(),
rest_path: DEFAULT_REST_PATH.to_string(),
task_store: None,
push_sender: None,
name: None,
description: None,
version: "1.0.0".to_string(),
streaming: true,
extra_skills: Vec::new(),
legacy_jsonrpc_url: None,
output_mode: OutputMode::default(),
extended_card: None,
hooks: super::hooks::ExecutorHooks::default(),
inactivity_timeout: None,
limits: None,
interceptors: Vec::new(),
}
}
pub fn handler(&self) -> &Arc<RequestHandler> {
&self.handler
}
pub fn card(&self) -> &Arc<RwLock<CachedAgentCard>> {
&self.card
}
pub fn card_router(&self) -> Router {
Router::new()
.route(
AGENT_CARD_PATH,
get(serve_agent_card).options(super::card::agent_card_preflight),
)
.with_state(self.card.clone())
}
pub fn rpc_router(&self) -> Router {
Router::new()
.route(
&self.jsonrpc_path,
post(jsonrpc_handler).fallback(super::jsonrpc_handler::jsonrpc_not_post),
)
.with_state(self.handler.clone())
.nest(&self.rest_path, rest_router(self.handler.clone()))
.route_layer(axum::middleware::from_fn(version_negotiation))
}
pub fn router(&self) -> Router {
self.card_router().merge(self.rpc_router())
}
}
pub struct A2aV1ServerBuilder {
runner_config: Arc<adk_runner::RunnerConfig>,
base_url: String,
jsonrpc_path: String,
rest_path: String,
task_store: Option<Arc<dyn TaskStore>>,
push_sender: Option<Arc<dyn PushNotificationSender>>,
name: Option<String>,
description: Option<String>,
version: String,
streaming: bool,
extra_skills: Vec<AgentSkill>,
legacy_jsonrpc_url: Option<String>,
output_mode: OutputMode,
extended_card: Option<super::request_handler::ExtendedCardProducer>,
hooks: super::hooks::ExecutorHooks,
inactivity_timeout: Option<std::time::Duration>,
limits: Option<super::run::ConcurrencyLimits>,
interceptors: Vec<Arc<dyn super::interceptor::CallInterceptor>>,
}
impl A2aV1ServerBuilder {
pub fn jsonrpc_path(mut self, path: impl Into<String>) -> Self {
self.jsonrpc_path = path.into();
self
}
pub fn rest_path(mut self, path: impl Into<String>) -> Self {
self.rest_path = path.into();
self
}
pub fn task_store(mut self, store: Arc<dyn TaskStore>) -> Self {
self.task_store = Some(store);
self
}
pub fn inactivity_timeout(mut self, timeout: std::time::Duration) -> Self {
self.inactivity_timeout = Some(timeout);
self
}
pub fn interceptor(
mut self,
interceptor: Arc<dyn super::interceptor::CallInterceptor>,
) -> Self {
self.interceptors.push(interceptor);
self
}
pub fn concurrency_limits(mut self, limits: super::run::ConcurrencyLimits) -> Self {
self.limits = Some(limits);
self
}
pub fn hooks(mut self, hooks: super::hooks::ExecutorHooks) -> Self {
self.hooks = hooks;
self
}
pub fn extended_agent_card(
mut self,
producer: super::request_handler::ExtendedCardProducer,
) -> Self {
self.extended_card = Some(producer);
self
}
pub fn output_mode(mut self, mode: OutputMode) -> Self {
self.output_mode = mode;
self
}
pub fn push_sender(mut self, sender: Arc<dyn PushNotificationSender>) -> Self {
self.push_sender = Some(sender);
self
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn version(mut self, version: impl Into<String>) -> Self {
self.version = version.into();
self
}
pub fn streaming(mut self, streaming: bool) -> Self {
self.streaming = streaming;
self
}
pub fn skills(mut self, skills: impl IntoIterator<Item = AgentSkill>) -> Self {
self.extra_skills.extend(skills);
self
}
pub fn legacy_jsonrpc_url(mut self, url: impl Into<String>) -> Self {
self.legacy_jsonrpc_url = Some(url.into());
self
}
pub fn build(self) -> A2aV1Server {
let agent = self.runner_config.agent.clone();
let jsonrpc_url = format!("{}{}", self.base_url, self.jsonrpc_path);
let rest_url = format!("{}{}", self.base_url, self.rest_path);
let mut skills = v1_agent_skills(agent.as_ref());
skills.extend(self.extra_skills);
let mut capabilities = AgentCapabilities::none()
.with_streaming(self.streaming)
.with_push_notifications(self.push_sender.is_some())
.with_extended_agent_card(self.extended_card.is_some());
capabilities.extensions = Some(vec![AgentExtension {
uri: ADK_EXTENSION_URI.to_string(),
description: Some(
"Content is spread across task artifacts and the status message, which carries \
long-running function calls."
.to_string(),
),
required: None,
params: None,
}]);
let mut card = build_v1_agent_card(
self.name.as_deref().unwrap_or_else(|| agent.name()),
self.description.as_deref().unwrap_or_else(|| agent.description()),
&jsonrpc_url,
&self.version,
skills,
capabilities,
);
card.supported_interfaces.push(AgentInterface {
url: rest_url,
protocol_binding: "HTTP+JSON".to_string(),
protocol_version: "1.0".to_string(),
tenant: None,
});
if let Some(legacy_url) = self.legacy_jsonrpc_url {
card.supported_interfaces.push(AgentInterface {
url: legacy_url,
protocol_binding: "JSONRPC".to_string(),
protocol_version: "0.3".to_string(),
tenant: None,
});
}
let card = Arc::new(RwLock::new(CachedAgentCard::new(card)));
let task_store: Arc<dyn TaskStore> =
self.task_store.unwrap_or_else(|| Arc::new(InMemoryTaskStore::new()));
let push_sender: Arc<dyn PushNotificationSender> =
self.push_sender.unwrap_or_else(|| Arc::new(NoOpPushNotificationSender));
let mut handler = RequestHandler::with_runner(
Arc::new(V1Executor::new(task_store.clone())),
task_store,
push_sender,
card.clone(),
self.runner_config,
)
.with_output_mode(self.output_mode)
.with_hooks(self.hooks);
if let Some(producer) = self.extended_card {
handler = handler.with_extended_card(producer);
}
if let Some(timeout) = self.inactivity_timeout {
handler = handler.with_inactivity_timeout(timeout);
}
if let Some(limits) = self.limits {
handler = handler.with_concurrency_limits(limits);
}
for interceptor in self.interceptors {
handler = handler.with_interceptor(interceptor);
}
let handler = Arc::new(handler);
A2aV1Server { handler, card, jsonrpc_path: self.jsonrpc_path, rest_path: self.rest_path }
}
}