mod event;
mod executor;
mod handler;
mod http;
mod middleware;
mod push;
mod rest;
mod task_store;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
pub use event::{Event, EventQueue, QueueManager};
pub use executor::{
AgentExecutor, ReferencedTasksLoader, RequestContext, RequestContextInterceptor,
};
pub use handler::{
DefaultRequestHandler, EventStream, InterceptedHandler, RequestHandler, handle_request,
};
pub use http::{
a2a_full_router, a2a_router, a2a_tenant_router, handle_agent_card, handle_jsonrpc, handle_sse,
};
pub use middleware::{
AuthenticatedUser, CallContext, CallInterceptor, PassthroughInterceptor, REQUEST_META, Request,
RequestMeta, Response, UnauthenticatedUser, User, request_meta,
};
pub use push::{
HttpPushSender, HttpPushSenderConfig, InMemoryPushNotificationConfigStore,
PushNotificationConfigStore, PushSender,
};
pub use rest::rest_router;
pub use task_store::{InMemoryTaskStore, TaskStore};
use crate::error::Result;
use crate::types::AgentCard;
pub trait AgentCardProducer: Send + Sync {
fn card(&self) -> Pin<Box<dyn Future<Output = Result<AgentCard>> + Send + '_>>;
}
impl AgentCardProducer for AgentCard {
fn card(&self) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + '_>> {
Box::pin(async { Ok(self.clone()) })
}
}
pub struct HandlerBuilder {
executor: Box<dyn AgentExecutor>,
agent_card: AgentCard,
task_store: Option<Arc<dyn TaskStore>>,
queue_manager: Option<Arc<QueueManager>>,
push_config_store: Option<Arc<dyn PushNotificationConfigStore>>,
push_sender: Option<Arc<dyn PushSender>>,
req_context_interceptors: Vec<Arc<dyn RequestContextInterceptor>>,
call_interceptors: Vec<Arc<dyn CallInterceptor>>,
extended_card_producer: Option<Arc<dyn AgentCardProducer>>,
}
impl std::fmt::Debug for HandlerBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HandlerBuilder")
.field("agent_card", &self.agent_card)
.finish_non_exhaustive()
}
}
impl HandlerBuilder {
pub fn new(executor: impl AgentExecutor + 'static, agent_card: AgentCard) -> Self {
Self {
executor: Box::new(executor),
agent_card,
task_store: None,
queue_manager: None,
push_config_store: None,
push_sender: None,
req_context_interceptors: Vec::new(),
call_interceptors: Vec::new(),
extended_card_producer: None,
}
}
#[must_use]
pub fn with_task_store(mut self, store: Arc<dyn TaskStore>) -> Self {
self.task_store = Some(store);
self
}
#[must_use]
pub fn with_queue_manager(mut self, manager: Arc<QueueManager>) -> Self {
self.queue_manager = Some(manager);
self
}
#[must_use]
pub fn with_push_notifications(
mut self,
store: Arc<dyn PushNotificationConfigStore>,
sender: Arc<dyn PushSender>,
) -> Self {
self.push_config_store = Some(store);
self.push_sender = Some(sender);
self
}
#[must_use]
pub fn with_request_context_interceptor(
mut self,
interceptor: Arc<dyn RequestContextInterceptor>,
) -> Self {
self.req_context_interceptors.push(interceptor);
self
}
#[must_use]
pub fn with_call_interceptor(mut self, interceptor: Arc<dyn CallInterceptor>) -> Self {
self.call_interceptors.push(interceptor);
self
}
#[must_use]
pub fn with_extended_agent_card(mut self, card: AgentCard) -> Self {
self.extended_card_producer = Some(Arc::new(card));
self
}
#[must_use]
pub fn with_extended_agent_card_producer(
mut self,
producer: Arc<dyn AgentCardProducer>,
) -> Self {
self.extended_card_producer = Some(producer);
self
}
#[must_use]
pub fn build(self) -> InterceptedHandler {
let mut handler = DefaultRequestHandler::new_from_boxed(self.executor, self.agent_card);
if let Some(manager) = self.queue_manager {
handler = handler.with_queue_manager(manager);
}
if let Some(store) = self.task_store {
handler = handler.with_task_store(store);
}
if let Some(store) = self.push_config_store {
handler = handler.with_push_config_store(store);
}
if let Some(sender) = self.push_sender {
handler = handler.with_push_sender(sender);
}
for interceptor in self.req_context_interceptors {
handler = handler.with_request_context_interceptor(interceptor);
}
if let Some(producer) = self.extended_card_producer {
handler = handler.with_extended_agent_card_producer(producer);
}
let mut ih = InterceptedHandler::new(Arc::new(handler));
for interceptor in self.call_interceptors {
ih = ih.with_interceptor(interceptor);
}
ih
}
}
#[derive(Clone)]
pub struct ServerState {
pub handler: Arc<dyn RequestHandler>,
pub card_producer: Arc<dyn AgentCardProducer>,
}
impl ServerState {
pub fn new(handler: Arc<dyn RequestHandler>, agent_card: AgentCard) -> Self {
Self {
handler,
card_producer: Arc::new(agent_card),
}
}
pub fn with_card_producer(
handler: Arc<dyn RequestHandler>,
card_producer: Arc<dyn AgentCardProducer>,
) -> Self {
Self {
handler,
card_producer,
}
}
pub fn from_executor(executor: impl AgentExecutor + 'static, agent_card: AgentCard) -> Self {
let card_producer: Arc<dyn AgentCardProducer> = Arc::new(agent_card.clone());
let handler = Arc::new(DefaultRequestHandler::new(executor, agent_card));
Self {
handler,
card_producer,
}
}
}
impl std::fmt::Debug for ServerState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ServerState").finish_non_exhaustive()
}
}