a2a-rs 0.4.0

Rust implementation of the Agent-to-Agent (A2A) Protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! The ConnectRPC transport adapter.
//!
//! `ConnectRpcAdapter` is the **outer** half of the service/transport split: a
//! thin transport adapter that implements the generated [`A2aService`] surface.
//! Its only job is to decode `buffa` wire views into domain values, delegate to
//! the inner [`TaskService`], and re-encode the domain results (and map
//! [`A2AError`] onto ConnectRPC error codes). All use-case orchestration lives
//! in [`TaskService`]; this layer holds no port traits directly.
//!
//! The public constructors (`new`, `with_handler`, `with_streaming_handler`)
//! each build the inner service and wrap it.

use async_trait::async_trait;
use buffa::Enumeration;
use std::pin::Pin;

use crate::{
    application::TaskService,
    domain::{
        A2AError, AgentCard, Task, TaskArtifactUpdateEvent, TaskId, TaskPushNotificationConfig,
        TaskStatusUpdateEvent,
        generated::{
            A2aService, CancelTaskRequestView, DeleteTaskPushNotificationConfigRequestView,
            GetExtendedAgentCardRequestView, GetTaskPushNotificationConfigRequestView,
            GetTaskRequestView, ListTaskPushNotificationConfigsRequestView,
            ListTaskPushNotificationConfigsResponse, ListTasksRequest, ListTasksRequestView,
            ListTasksResponse, SendMessageRequestView, SendMessageResponse, StreamResponse,
            SubscribeToTaskRequestView, TaskArtifactUpdateEvent as GenTaskArtifactUpdateEvent,
            TaskPushNotificationConfigView, TaskState,
            TaskStatusUpdateEvent as GenTaskStatusUpdateEvent, send_message_response,
            stream_response,
        },
    },
    port::{
        AsyncMessageHandler, AsyncNotificationManager, AsyncStreamingHandler, AsyncTaskLifecycle,
        AsyncTaskQuery, SeqEvent, UpdateEvent, streaming_handler::Subscriber,
    },
    services::server::AgentInfoProvider,
};

/// ConnectRPC transport adapter over a [`TaskService`].
///
/// Holds no ports directly — it owns the inner application service and forwards
/// decoded requests to it. Dispatch into the service goes through the service's
/// `Arc<dyn …>` fields, which is a cold path against the I/O each call performs.
#[derive(Clone)]
pub struct ConnectRpcAdapter {
    service: TaskService,
}

impl ConnectRpcAdapter {
    /// Create a new adapter from separate handlers, defaulting to a no-op
    /// streaming handler.
    ///
    /// `tasks` supplies both the lifecycle and query capabilities.
    pub fn new(
        message_handler: impl AsyncMessageHandler + 'static,
        tasks: impl AsyncTaskLifecycle + AsyncTaskQuery + 'static,
        notification_manager: impl AsyncNotificationManager + 'static,
        agent_info: impl AgentInfoProvider + 'static,
    ) -> Self {
        Self {
            service: TaskService::new(
                message_handler,
                tasks,
                notification_manager,
                agent_info,
                NoopStreamingHandler,
                crate::port::NoopPushNotifier,
            ),
        }
    }

    /// Create a new adapter from a single handler that implements every port,
    /// defaulting to a no-op streaming handler.
    pub fn with_handler(
        handler: impl AsyncMessageHandler
        + AsyncTaskLifecycle
        + AsyncTaskQuery
        + AsyncNotificationManager
        + 'static,
        agent_info: impl AgentInfoProvider + 'static,
    ) -> Self {
        Self {
            service: TaskService::with_handler(
                handler,
                agent_info,
                NoopStreamingHandler,
                crate::port::NoopPushNotifier,
            ),
        }
    }

    /// Builder-style method to inject custom streaming handler support.
    pub fn with_streaming_handler(
        self,
        streaming_handler: impl AsyncStreamingHandler + 'static,
    ) -> Self {
        Self {
            service: self.service.with_streaming_handler(streaming_handler),
        }
    }

    /// Builder-style method to inject a custom push notifier.
    pub fn with_push_notifier(
        self,
        push_notifier: impl crate::port::AsyncPushNotifier + 'static,
    ) -> Self {
        Self {
            service: self.service.with_push_notifier(push_notifier),
        }
    }
}

/// Helper function to map A2AError to connectrpc::ConnectError
fn map_err(e: A2AError) -> ::connectrpc::ConnectError {
    match e {
        A2AError::TaskNotFound(msg) => {
            ::connectrpc::ConnectError::new(::connectrpc::ErrorCode::NotFound, msg)
        }
        A2AError::InvalidParams(msg) => {
            ::connectrpc::ConnectError::new(::connectrpc::ErrorCode::InvalidArgument, msg)
        }
        A2AError::ValidationError { field, message } => ::connectrpc::ConnectError::new(
            ::connectrpc::ErrorCode::InvalidArgument,
            format!("{}: {}", field, message),
        ),
        A2AError::UnsupportedOperation(msg) => {
            ::connectrpc::ConnectError::new(::connectrpc::ErrorCode::Unimplemented, msg)
        }
        A2AError::AuthenticatedExtendedCardNotConfigured => ::connectrpc::ConnectError::new(
            ::connectrpc::ErrorCode::FailedPrecondition,
            "Authenticated extended card not configured".to_string(),
        ),
        A2AError::MethodNotFound(msg) => {
            ::connectrpc::ConnectError::new(::connectrpc::ErrorCode::Unimplemented, msg)
        }
        _ => ::connectrpc::ConnectError::new(::connectrpc::ErrorCode::Internal, e.to_string()),
    }
}

/// Helper to map domain metadata to protobuf Struct
fn map_metadata(
    opt: Option<serde_json::Map<String, serde_json::Value>>,
) -> ::buffa::MessageField<::buffa_types::google::protobuf::Struct> {
    if let Some(map) = opt {
        let val = serde_json::Value::Object(map);
        if let Ok(struc) = serde_json::from_value::<::buffa_types::google::protobuf::Struct>(val) {
            return ::buffa::MessageField::some(struc);
        }
    }
    ::buffa::MessageField::none()
}

fn map_status_update(
    evt: crate::domain::events::TaskStatusUpdateEvent,
) -> GenTaskStatusUpdateEvent {
    GenTaskStatusUpdateEvent {
        task_id: evt.task_id,
        context_id: evt.context_id,
        status: ::buffa::MessageField::some(evt.status),
        metadata: map_metadata(evt.metadata),
        ..Default::default()
    }
}

fn map_artifact_update(
    evt: crate::domain::events::TaskArtifactUpdateEvent,
) -> GenTaskArtifactUpdateEvent {
    GenTaskArtifactUpdateEvent {
        task_id: evt.task_id,
        context_id: evt.context_id,
        artifact: ::buffa::MessageField::some(evt.artifact),
        append: evt.append.unwrap_or(false),
        last_chunk: evt.last_chunk.unwrap_or(false),
        metadata: map_metadata(evt.metadata),
        ..Default::default()
    }
}

/// Map a domain [`UpdateEvent`] onto its wire [`StreamResponse`].
///
/// Shared with the JSON-RPC adapter so both transports map streaming updates
/// through one path.
pub(super) fn map_update_event(evt: UpdateEvent) -> StreamResponse {
    match evt {
        UpdateEvent::StatusUpdate(event) => StreamResponse {
            payload: Some(stream_response::Payload::StatusUpdate(Box::new(
                map_status_update(event),
            ))),
            ..Default::default()
        },
        UpdateEvent::ArtifactUpdate(event) => StreamResponse {
            payload: Some(stream_response::Payload::ArtifactUpdate(Box::new(
                map_artifact_update(event),
            ))),
            ..Default::default()
        },
    }
}

impl A2aService for ConnectRpcAdapter {
    async fn send_message(
        &self,
        ctx: ::connectrpc::Context,
        request: ::buffa::view::OwnedView<SendMessageRequestView<'static>>,
    ) -> Result<(SendMessageResponse, ::connectrpc::Context), ::connectrpc::ConnectError> {
        let req = request.to_owned_message();
        let message = req.message.into_option().ok_or_else(|| {
            ::connectrpc::ConnectError::new(
                ::connectrpc::ErrorCode::InvalidArgument,
                "Missing message".to_string(),
            )
        })?;
        let config = req.configuration.into_option();

        let task_id = message.task_id.clone();
        let session_id = if message.context_id.is_empty() {
            None
        } else {
            Some(message.context_id.as_str())
        };

        let (push_config, history_limit) = decode_send_config(config);

        let task = self
            .service
            .send_message(&task_id, &message, session_id, push_config, history_limit)
            .await
            .map_err(map_err)?;

        let response = SendMessageResponse {
            payload: Some(send_message_response::Payload::Task(Box::new(task))),
            ..Default::default()
        };

        Ok((response, ctx))
    }

    #[allow(clippy::result_large_err)]
    async fn send_streaming_message(
        &self,
        ctx: ::connectrpc::Context,
        request: ::buffa::view::OwnedView<SendMessageRequestView<'static>>,
    ) -> Result<
        (
            ::std::pin::Pin<
                Box<
                    dyn ::futures::Stream<Item = Result<StreamResponse, ::connectrpc::ConnectError>>
                        + Send,
                >,
            >,
            ::connectrpc::Context,
        ),
        ::connectrpc::ConnectError,
    > {
        let req = request.to_owned_message();
        let message = req.message.into_option().ok_or_else(|| {
            ::connectrpc::ConnectError::new(
                ::connectrpc::ErrorCode::InvalidArgument,
                "Missing message".to_string(),
            )
        })?;
        let config = req.configuration.into_option();

        let task_id = message.task_id.clone();
        let session_id = if message.context_id.is_empty() {
            None
        } else {
            Some(message.context_id.as_str())
        };

        let (push_config, history_limit) = decode_send_config(config);

        let (task, update_stream) = self
            .service
            .send_streaming_message(&task_id, &message, session_id, push_config, history_limit)
            .await
            .map_err(map_err)?;

        use futures::StreamExt;

        let initial_response = StreamResponse {
            payload: Some(stream_response::Payload::Task(Box::new(task))),
            ..Default::default()
        };

        let mapped_stream =
            update_stream.map(|item| item.map(|seq| map_update_event(seq.event)).map_err(map_err));

        let chained_stream =
            futures::stream::once(async { Ok(initial_response) }).chain(mapped_stream);

        Ok((Box::pin(chained_stream), ctx))
    }

    async fn get_task(
        &self,
        ctx: ::connectrpc::Context,
        request: ::buffa::view::OwnedView<GetTaskRequestView<'static>>,
    ) -> Result<(Task, ::connectrpc::Context), ::connectrpc::ConnectError> {
        let req = request.to_owned_message();
        let history_length = req.history_length.map(|l| l as u32);
        let id: TaskId = req.id.parse().map_err(map_err)?;
        let task = self
            .service
            .get(&id, history_length)
            .await
            .map_err(map_err)?;
        Ok((task, ctx))
    }

    async fn list_tasks(
        &self,
        ctx: ::connectrpc::Context,
        request: ::buffa::view::OwnedView<ListTasksRequestView<'static>>,
    ) -> Result<(ListTasksResponse, ::connectrpc::Context), ::connectrpc::ConnectError> {
        let req = request.to_owned_message();
        let params = list_request_to_params(req);

        let result = self.service.list(&params).await.map_err(map_err)?;

        let response = ListTasksResponse {
            tasks: result.tasks,
            next_page_token: result.next_page_token,
            page_size: result.page_size,
            total_size: result.total_size,
            ..Default::default()
        };

        Ok((response, ctx))
    }

    async fn cancel_task(
        &self,
        ctx: ::connectrpc::Context,
        request: ::buffa::view::OwnedView<CancelTaskRequestView<'static>>,
    ) -> Result<(Task, ::connectrpc::Context), ::connectrpc::ConnectError> {
        let req = request.to_owned_message();
        let id: TaskId = req.id.parse().map_err(map_err)?;
        let task = self.service.cancel(&id).await.map_err(map_err)?;
        Ok((task, ctx))
    }

    #[allow(clippy::result_large_err)]
    async fn subscribe_to_task(
        &self,
        ctx: ::connectrpc::Context,
        request: ::buffa::view::OwnedView<SubscribeToTaskRequestView<'static>>,
    ) -> Result<
        (
            ::std::pin::Pin<
                Box<
                    dyn ::futures::Stream<Item = Result<StreamResponse, ::connectrpc::ConnectError>>
                        + Send,
                >,
            >,
            ::connectrpc::Context,
        ),
        ::connectrpc::ConnectError,
    > {
        let req = request.to_owned_message();

        let (initial_task, update_stream) = self
            .service
            .subscribe(&req.id, None)
            .await
            .map_err(map_err)?;

        use futures::StreamExt;

        let mapped_stream =
            update_stream.map(|item| item.map(|seq| map_update_event(seq.event)).map_err(map_err));

        if let Some(task) = initial_task {
            let initial_response = StreamResponse {
                payload: Some(stream_response::Payload::Task(Box::new(task))),
                ..Default::default()
            };
            let chained_stream =
                futures::stream::once(async { Ok(initial_response) }).chain(mapped_stream);
            Ok((Box::pin(chained_stream), ctx))
        } else {
            Ok((Box::pin(mapped_stream), ctx))
        }
    }

    async fn create_task_push_notification_config(
        &self,
        ctx: ::connectrpc::Context,
        request: ::buffa::view::OwnedView<TaskPushNotificationConfigView<'static>>,
    ) -> Result<(TaskPushNotificationConfig, ::connectrpc::Context), ::connectrpc::ConnectError>
    {
        let config = request.to_owned_message();
        let created_config = self
            .service
            .set_push_config(&config)
            .await
            .map_err(map_err)?;
        Ok((created_config, ctx))
    }

    async fn get_task_push_notification_config(
        &self,
        ctx: ::connectrpc::Context,
        request: ::buffa::view::OwnedView<GetTaskPushNotificationConfigRequestView<'static>>,
    ) -> Result<(TaskPushNotificationConfig, ::connectrpc::Context), ::connectrpc::ConnectError>
    {
        let req = request.to_owned_message();
        let params = crate::domain::GetTaskPushNotificationConfigParams {
            id: req.task_id,
            push_notification_config_id: Some(req.id),
            metadata: None,
        };
        let config = self
            .service
            .get_push_config(&params)
            .await
            .map_err(map_err)?;
        Ok((config, ctx))
    }

    async fn list_task_push_notification_configs(
        &self,
        ctx: ::connectrpc::Context,
        request: ::buffa::view::OwnedView<ListTaskPushNotificationConfigsRequestView<'static>>,
    ) -> Result<
        (
            ListTaskPushNotificationConfigsResponse,
            ::connectrpc::Context,
        ),
        ::connectrpc::ConnectError,
    > {
        let req = request.to_owned_message();
        let params = crate::domain::ListTaskPushNotificationConfigsParams {
            id: req.task_id,
            metadata: None,
        };
        let configs = self
            .service
            .list_push_configs(&params)
            .await
            .map_err(map_err)?;
        let response = ListTaskPushNotificationConfigsResponse {
            configs,
            ..Default::default()
        };
        Ok((response, ctx))
    }

    async fn get_extended_agent_card(
        &self,
        ctx: ::connectrpc::Context,
        request: ::buffa::view::OwnedView<GetExtendedAgentCardRequestView<'static>>,
    ) -> Result<(AgentCard, ::connectrpc::Context), ::connectrpc::ConnectError> {
        let _req = request.to_owned_message();
        let card = self.service.extended_agent_card().await.map_err(map_err)?;
        Ok((card, ctx))
    }

    async fn delete_task_push_notification_config(
        &self,
        ctx: ::connectrpc::Context,
        request: ::buffa::view::OwnedView<DeleteTaskPushNotificationConfigRequestView<'static>>,
    ) -> Result<
        (
            ::buffa_types::google::protobuf::Empty,
            ::connectrpc::Context,
        ),
        ::connectrpc::ConnectError,
    > {
        let req = request.to_owned_message();
        let params = crate::domain::DeleteTaskPushNotificationConfigParams {
            id: req.task_id,
            push_notification_config_id: req.id,
            metadata: None,
        };
        self.service
            .delete_push_config(&params)
            .await
            .map_err(map_err)?;
        Ok((::buffa_types::google::protobuf::Empty::default(), ctx))
    }
}

/// Map a generated `ListTasksRequest` (proto wire message) onto the domain
/// [`ListTasksParams`]. Shared with the JSON-RPC adapter.
pub(super) fn list_request_to_params(req: ListTasksRequest) -> crate::domain::ListTasksParams {
    crate::domain::ListTasksParams {
        context_id: if req.context_id.is_empty() {
            None
        } else {
            Some(req.context_id)
        },
        status: match req.status.to_i32() {
            0 => None,
            val => Some(TaskState::from_i32(val).unwrap_or(TaskState::TASK_STATE_UNSPECIFIED)),
        },
        page_size: req.page_size,
        page_token: if req.page_token.is_empty() {
            None
        } else {
            Some(req.page_token)
        },
        history_length: req.history_length,
        include_artifacts: req.include_artifacts,
        status_timestamp_after: req.status_timestamp_after.as_option().map(|t| {
            let dt = chrono::DateTime::<chrono::Utc>::from_timestamp(t.seconds, t.nanos as u32)
                .unwrap_or_default();
            dt.to_rfc3339()
        }),
        metadata: None,
    }
}

/// Decode the optional `SendMessageConfiguration` view into the domain push
/// config + history limit the service expects.
///
/// Shared with the JSON-RPC adapter (both decode the same generated config
/// message), so the two transports agree on the wire shape.
pub(super) fn decode_send_config(
    config: Option<crate::domain::generated::SendMessageConfiguration>,
) -> (Option<TaskPushNotificationConfig>, Option<u32>) {
    let Some(c) = config else {
        return (None, None);
    };
    let push_config = c.task_push_notification_config.into_option();
    let history_limit = c.history_length.map(|limit| limit as u32);
    (push_config, history_limit)
}

/// A no-op [`AsyncStreamingHandler`] used as the adapter's default streaming port
/// when the caller has no real streaming backend to inject.
#[derive(Clone, Debug, Default)]
pub struct NoopStreamingHandler;

#[async_trait]
impl AsyncStreamingHandler for NoopStreamingHandler {
    async fn add_status_subscriber(
        &self,
        _task_id: &str,
        _subscriber: Box<dyn Subscriber<TaskStatusUpdateEvent> + Send + Sync>,
    ) -> Result<String, A2AError> {
        Err(A2AError::UnsupportedOperation(
            "Streaming not supported by this processor".to_string(),
        ))
    }

    async fn add_artifact_subscriber(
        &self,
        _task_id: &str,
        _subscriber: Box<dyn Subscriber<TaskArtifactUpdateEvent> + Send + Sync>,
    ) -> Result<String, A2AError> {
        Err(A2AError::UnsupportedOperation(
            "Streaming not supported by this processor".to_string(),
        ))
    }

    async fn remove_subscription(&self, _subscription_id: &str) -> Result<(), A2AError> {
        Ok(())
    }

    async fn remove_task_subscribers(&self, _task_id: &str) -> Result<(), A2AError> {
        Ok(())
    }

    async fn get_subscriber_count(&self, _task_id: &str) -> Result<usize, A2AError> {
        Ok(0)
    }

    async fn broadcast_status_update(
        &self,
        _task_id: &str,
        _update: TaskStatusUpdateEvent,
    ) -> Result<(), A2AError> {
        Ok(())
    }

    async fn broadcast_artifact_update(
        &self,
        _task_id: &str,
        _update: TaskArtifactUpdateEvent,
    ) -> Result<(), A2AError> {
        Ok(())
    }

    async fn status_update_stream(
        &self,
        _task_id: &str,
    ) -> Result<
        Pin<Box<dyn ::futures::Stream<Item = Result<TaskStatusUpdateEvent, A2AError>> + Send>>,
        A2AError,
    > {
        Err(A2AError::UnsupportedOperation(
            "Streaming not supported by this processor".to_string(),
        ))
    }

    async fn artifact_update_stream(
        &self,
        _task_id: &str,
    ) -> Result<
        Pin<Box<dyn ::futures::Stream<Item = Result<TaskArtifactUpdateEvent, A2AError>> + Send>>,
        A2AError,
    > {
        Err(A2AError::UnsupportedOperation(
            "Streaming not supported by this processor".to_string(),
        ))
    }

    async fn combined_update_stream(
        &self,
        _task_id: &str,
        _from_event_id: Option<u64>,
    ) -> Result<Pin<Box<dyn ::futures::Stream<Item = Result<SeqEvent, A2AError>> + Send>>, A2AError>
    {
        Err(A2AError::UnsupportedOperation(
            "Streaming not supported by this processor".to_string(),
        ))
    }
}