Skip to main content

brainwires_a2a/server/
handler.rs

1//! The `A2aHandler` trait — implement once, serve all 3 bindings.
2
3use std::pin::Pin;
4
5use async_trait::async_trait;
6use futures::Stream;
7
8use crate::agent_card::AgentCard;
9use crate::error::A2aError;
10use crate::params::*;
11use crate::push_notification::TaskPushNotificationConfig;
12use crate::streaming::{SendMessageResponse, StreamResponse};
13use crate::task::Task;
14
15/// Core handler trait for A2A agents.
16///
17/// Implement this trait to define agent behavior. The server infrastructure
18/// will route requests from JSON-RPC, REST, and gRPC to these methods.
19#[async_trait]
20pub trait A2aHandler: Send + Sync + 'static {
21    /// Return the agent card for discovery.
22    fn agent_card(&self) -> &AgentCard;
23
24    /// Handle a `message/send` request.
25    async fn on_send_message(
26        &self,
27        req: SendMessageRequest,
28    ) -> Result<SendMessageResponse, A2aError>;
29
30    /// Handle a `message/stream` request (server-streaming).
31    async fn on_send_streaming_message(
32        &self,
33        req: SendMessageRequest,
34    ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamResponse, A2aError>> + Send>>, A2aError>;
35
36    /// Handle a `tasks/get` request.
37    async fn on_get_task(&self, req: GetTaskRequest) -> Result<Task, A2aError>;
38
39    /// Handle a `tasks/list` request.
40    async fn on_list_tasks(&self, req: ListTasksRequest) -> Result<ListTasksResponse, A2aError>;
41
42    /// Handle a `tasks/cancel` request.
43    async fn on_cancel_task(&self, req: CancelTaskRequest) -> Result<Task, A2aError>;
44
45    /// Handle a `tasks/resubscribe` request (server-streaming).
46    async fn on_subscribe_to_task(
47        &self,
48        req: SubscribeToTaskRequest,
49    ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamResponse, A2aError>> + Send>>, A2aError>;
50
51    /// Create a push notification config. Default returns unsupported.
52    async fn on_create_push_config(
53        &self,
54        _config: TaskPushNotificationConfig,
55    ) -> Result<TaskPushNotificationConfig, A2aError> {
56        Err(A2aError::push_not_supported())
57    }
58
59    /// Get a push notification config. Default returns unsupported.
60    async fn on_get_push_config(
61        &self,
62        _req: GetTaskPushNotificationConfigRequest,
63    ) -> Result<TaskPushNotificationConfig, A2aError> {
64        Err(A2aError::push_not_supported())
65    }
66
67    /// List push notification configs. Default returns unsupported.
68    async fn on_list_push_configs(
69        &self,
70        _req: ListTaskPushNotificationConfigsRequest,
71    ) -> Result<ListTaskPushNotificationConfigsResponse, A2aError> {
72        Err(A2aError::push_not_supported())
73    }
74
75    /// Delete a push notification config. Default returns unsupported.
76    async fn on_delete_push_config(
77        &self,
78        _req: DeleteTaskPushNotificationConfigRequest,
79    ) -> Result<(), A2aError> {
80        Err(A2aError::push_not_supported())
81    }
82
83    /// Get the authenticated extended agent card. Default returns not configured.
84    async fn on_get_extended_agent_card(
85        &self,
86        _req: GetExtendedAgentCardRequest,
87    ) -> Result<AgentCard, A2aError> {
88        Err(A2aError::extended_card_not_configured())
89    }
90}