a2a-rs 0.5.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
//! In-process interop round-trip: the JSON-RPC **client** against the JSON-RPC
//! **server**, over a real socket.
//!
//! This proves byte-compatibility of [`JsonRpcClient`] with
//! [`JsonRpcAdapter`](a2a_rs::adapter::JsonRpcAdapter): the client's JSON-RPC
//! envelopes + ProtoJSON bodies are exactly what the server decodes, and the
//! client's SSE reassembly parses exactly what the server emits. A real
//! `TcpListener` (not `tower::oneshot`) is required because the streaming path
//! drives `reqwest` over a live connection.

#![cfg(all(feature = "jsonrpc-client", feature = "jsonrpc-server"))]

mod common;

use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use axum::{Json, Router, routing::get};
use common::TestBusinessHandler;
use futures::{Stream, StreamExt, stream};

use a2a_rs::adapter::{InMemoryTaskStorage, JsonRpcAdapter, SimpleAgentInfo, jsonrpc_router};
use a2a_rs::domain::{
    A2AError, AgentCard, AgentInterface, ContextId, Message, SendCompletion,
    TaskArtifactUpdateEvent, TaskId, TaskPushNotificationConfig, TaskState, TaskStatus,
    TaskStatusUpdateEvent,
};
use a2a_rs::port::streaming_handler::{SeqEvent, Subscriber};
use a2a_rs::port::{AsyncStreamingHandler, AsyncTaskLifecycle};
use a2a_rs::{JsonRpcClient, StreamItem, Transport, connect, default_registry};

// ---------------------------------------------------------------------------
// A streaming handler whose pull-streams are empty but valid, so `subscribe`
// emits the initial task snapshot then completes. (InMemoryTaskStorage returns
// `UnsupportedOperation` from `combined_update_stream`, which would fail
// `subscribe`.)
// ---------------------------------------------------------------------------

type StatusStream = Pin<Box<dyn Stream<Item = Result<TaskStatusUpdateEvent, A2AError>> + Send>>;
type ArtifactStream = Pin<Box<dyn Stream<Item = Result<TaskArtifactUpdateEvent, A2AError>> + Send>>;
type CombinedStream = Pin<Box<dyn Stream<Item = Result<SeqEvent, A2AError>> + Send>>;

#[derive(Clone)]
struct EmptyStreamHandler;

#[async_trait]
impl AsyncStreamingHandler for EmptyStreamHandler {
    async fn add_status_subscriber(
        &self,
        _task_id: &str,
        _subscriber: Box<dyn Subscriber<TaskStatusUpdateEvent> + Send + Sync>,
    ) -> Result<String, A2AError> {
        Ok("status-sub".to_string())
    }
    async fn add_artifact_subscriber(
        &self,
        _task_id: &str,
        _subscriber: Box<dyn Subscriber<TaskArtifactUpdateEvent> + Send + Sync>,
    ) -> Result<String, A2AError> {
        Ok("artifact-sub".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<StatusStream, A2AError> {
        Ok(Box::pin(stream::empty()))
    }
    async fn artifact_update_stream(&self, _task_id: &str) -> Result<ArtifactStream, A2AError> {
        Ok(Box::pin(stream::empty()))
    }
    async fn combined_update_stream(
        &self,
        _task_id: &str,
        _from_event_id: Option<u64>,
    ) -> Result<CombinedStream, A2AError> {
        Ok(Box::pin(stream::empty()))
    }
}

// ---------------------------------------------------------------------------
// Server harness
// ---------------------------------------------------------------------------

/// Spawn the JSON-RPC server (with an agent-card route) on an ephemeral port and
/// return its base URL.
async fn spawn_server() -> String {
    spawn_server_with_handler().await.0
}

/// As [`spawn_server`], also handing back the handler — the seam a test needs to
/// put a task into a state the wire cannot reach on its own.
async fn spawn_server_with_handler() -> (String, TestBusinessHandler) {
    let handler = TestBusinessHandler::with_storage(InMemoryTaskStorage::new());
    let agent_info = SimpleAgentInfo::new("interop".to_string(), "http://localhost".to_string());
    let adapter = Arc::new(
        JsonRpcAdapter::with_handler(handler.clone(), agent_info)
            .with_streaming_handler(EmptyStreamHandler),
    );

    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let base = format!("http://{}", listener.local_addr().unwrap());

    let card = AgentCard {
        supported_interfaces: vec![AgentInterface {
            url: base.clone(),
            protocol_binding: "JSONRPC".to_string(),
            protocol_version: "1.0".to_string(),
            ..Default::default()
        }],
        ..Default::default()
    };

    let app: Router = jsonrpc_router(adapter).route(
        "/.well-known/agent-card.json",
        get(move || {
            let card = card.clone();
            async move { Json(card) }
        }),
    );

    tokio::spawn(async move {
        axum::serve(listener, app).await.unwrap();
    });

    (base, handler)
}

fn message() -> Message {
    Message::user_text("hello".to_string(), "m1".to_string())
}

/// Spawn a server whose streaming backend is a real (shared) in-memory handler,
/// returning the base URL and a handle to broadcast through the same backend.
async fn spawn_server_streaming() -> (String, TestBusinessHandler) {
    let handler = TestBusinessHandler::with_storage(InMemoryTaskStorage::new());
    let agent_info = SimpleAgentInfo::new("interop".to_string(), "http://localhost".to_string());
    let adapter = Arc::new(
        JsonRpcAdapter::with_handler(handler.clone(), agent_info)
            .with_streaming_handler(handler.clone()),
    );

    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let base = format!("http://{}", listener.local_addr().unwrap());
    let app: Router = jsonrpc_router(adapter);
    tokio::spawn(async move {
        axum::serve(listener, app).await.unwrap();
    });
    (base, handler)
}

fn status_update(task_id: &str, state: TaskState) -> TaskStatusUpdateEvent {
    TaskStatusUpdateEvent {
        task_id: task_id.to_string(),
        context_id: "ctx".to_string(),
        kind: "status-update".to_string(),
        status: TaskStatus::new(state, None),
        metadata: None,
    }
}

/// End-to-end Last-Event-ID resumption: the server emits SSE `id:` fields, the
/// client parses them, and reconnecting with `Last-Event-ID` replays only the
/// buffered events after that id (preceded by the initial task snapshot).
#[tokio::test]
async fn subscribe_resumes_from_last_event_id() {
    let (base, handler) = spawn_server_streaming().await;
    let client = JsonRpcClient::new(base);

    // Create the task so subscribe emits an initial snapshot.
    client
        .send_task_message(
            "task-resume",
            &message(),
            None,
            None,
            SendCompletion::WhenSettled,
        )
        .await
        .unwrap();

    // Two updates broadcast before any subscriber — buffered as event ids 1 and 2.
    handler
        .broadcast_status_update(
            "task-resume",
            status_update("task-resume", TaskState::Working),
        )
        .await
        .unwrap();
    handler
        .broadcast_status_update(
            "task-resume",
            status_update("task-resume", TaskState::Completed),
        )
        .await
        .unwrap();

    // First subscription replays everything (id > 0); discover the id the server
    // assigned to the Completed event (the message handler may emit its own
    // events too, so we don't assume absolute ids).
    let mut all = client
        .subscribe_to_task("task-resume", None, Some("0"))
        .await
        .unwrap();
    let mut completed_id = None;
    for _ in 0..16 {
        match tokio::time::timeout(Duration::from_secs(2), all.next()).await {
            Ok(Some(Ok(ev))) => {
                if let StreamItem::StatusUpdate(e) = &ev.item
                    && e.status.state == ::buffa::EnumValue::from(TaskState::Completed)
                {
                    completed_id = ev.event_id;
                    break;
                }
            }
            _ => break,
        }
    }
    let completed_id = completed_id.expect("should observe the Completed event with an id");
    drop(all);

    // Resume from just before Completed: only that event replays, after the snapshot.
    let mut stream = client
        .subscribe_to_task("task-resume", None, Some(&(completed_id - 1).to_string()))
        .await
        .unwrap();

    let mut got = Vec::new();
    while got.len() < 2 {
        let ev = tokio::time::timeout(Duration::from_secs(5), stream.next())
            .await
            .expect("event within 5s")
            .expect("stream not empty")
            .expect("ok event");
        got.push(ev);
    }

    // First: initial task snapshot (no id). Second: the replayed Completed event.
    assert!(
        matches!(got[0].item, StreamItem::Task(_)),
        "first must be the snapshot"
    );
    assert_eq!(got[0].event_id, None);
    assert_eq!(
        got[1].event_id,
        Some(completed_id),
        "only the Completed event should replay after Last-Event-ID = completed-1"
    );
    match &got[1].item {
        StreamItem::StatusUpdate(e) => {
            assert_eq!(
                e.status.state,
                ::buffa::EnumValue::from(TaskState::Completed)
            )
        }
        other => panic!("expected StatusUpdate, got {other:?}"),
    }
}

/// Attach a live subscription to a freshly created (non-terminal) task and
/// consume the initial snapshot, leaving the stream positioned on updates.
async fn subscribe_to_live_task(
    client: &JsonRpcClient,
    handler: &TestBusinessHandler,
    task_id: &str,
) -> Pin<Box<dyn Stream<Item = Result<a2a_rs::StreamEvent, A2AError>> + Send>> {
    let id: TaskId = task_id.parse().unwrap();
    let ctx: ContextId = "ctx".parse().unwrap();
    handler.create(&id, &ctx).await.unwrap();

    let mut stream = client.subscribe_to_task(task_id, None, None).await.unwrap();
    let first = tokio::time::timeout(Duration::from_secs(5), stream.next())
        .await
        .expect("snapshot within 5s")
        .expect("stream not empty")
        .expect("ok event");
    assert!(
        matches!(first.item, StreamItem::Task(_)),
        "first event must be the initial snapshot"
    );
    stream
}

/// Read the next event, then assert the stream **ends** rather than parking
/// open. Before this, a subscriber stayed connected after the task finished and
/// every run had to be capped with an external `timeout`.
async fn assert_closes_after_next(
    stream: &mut Pin<Box<dyn Stream<Item = Result<a2a_rs::StreamEvent, A2AError>> + Send>>,
    expected: TaskState,
) {
    let ev = tokio::time::timeout(Duration::from_secs(5), stream.next())
        .await
        .expect("settling event within 5s")
        .expect("stream not empty")
        .expect("ok event");
    match &ev.item {
        StreamItem::StatusUpdate(e) => {
            assert_eq!(e.status.state, ::buffa::EnumValue::from(expected))
        }
        other => panic!("expected StatusUpdate, got {other:?}"),
    }

    // The settling event is delivered *and* the stream then closes on its own.
    let end = tokio::time::timeout(Duration::from_secs(5), stream.next())
        .await
        .expect("stream must close within 5s rather than hang open");
    assert!(end.is_none(), "stream should be closed, got {end:?}");
}

/// A subscription ends once the task reaches a terminal state.
#[tokio::test]
async fn subscribe_stream_closes_on_terminal_state() {
    let (base, handler) = spawn_server_streaming().await;
    let client = JsonRpcClient::new(base);

    let mut stream = subscribe_to_live_task(&client, &handler, "task-close-terminal").await;
    handler
        .broadcast_status_update(
            "task-close-terminal",
            status_update("task-close-terminal", TaskState::Completed),
        )
        .await
        .unwrap();

    assert_closes_after_next(&mut stream, TaskState::Completed).await;
}

/// And on an *interrupted* state, which is the worse hang of the two: the agent
/// is waiting on the caller, so a stream left open has both sides waiting on
/// each other.
#[tokio::test]
async fn subscribe_stream_closes_on_interrupted_state() {
    let (base, handler) = spawn_server_streaming().await;
    let client = JsonRpcClient::new(base);

    let mut stream = subscribe_to_live_task(&client, &handler, "task-close-input").await;
    handler
        .broadcast_status_update(
            "task-close-input",
            status_update("task-close-input", TaskState::InputRequired),
        )
        .await
        .unwrap();

    assert_closes_after_next(&mut stream, TaskState::InputRequired).await;
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[tokio::test]
async fn unary_roundtrip_send_get_list_cancel() {
    let (base, handler) = spawn_server_with_handler().await;
    let client = JsonRpcClient::new(base);

    // send → returns a task
    let task = client
        .send_task_message(
            "task-1",
            &message(),
            None,
            None,
            SendCompletion::WhenSettled,
        )
        .await
        .unwrap();
    let id = task.id.clone();
    assert!(!id.is_empty());
    assert_eq!(
        task.status.state,
        TaskState::Completed,
        "the echo agent finishes what it is sent"
    );

    // get → same task
    let got = client.get_task(&id, None).await.unwrap();
    assert_eq!(got.id, id);

    // list → contains it
    let listed = client.list_tasks(&Default::default()).await.unwrap();
    assert!(
        listed.tasks.iter().any(|t| t.id == id),
        "listed tasks should contain {id}"
    );

    // cancel → a task still in flight. The one above is already `Completed`, and
    // a completed task is correctly *not* cancelable, so cancelling it would
    // exercise the error path rather than `cancel_task`'s success decoding.
    let pending: TaskId = "task-pending".parse().unwrap();
    handler
        .create(&pending, &"ctx".parse::<ContextId>().unwrap())
        .await
        .unwrap();
    handler
        .update_status(&pending, TaskState::Working, None)
        .await
        .unwrap();
    let canceled = client.cancel_task(pending.as_str()).await.unwrap();
    assert_eq!(canceled.id, "task-pending");
    assert_eq!(canceled.status.state, TaskState::Canceled);
}

/// The other half of that pair: a finished task refuses cancellation, and the
/// refusal survives the trip back with its typed details intact.
#[tokio::test]
async fn cancelling_a_completed_task_is_refused_over_the_wire() {
    let base = spawn_server().await;
    let client = JsonRpcClient::new(base);

    client
        .send_task_message(
            "task-done",
            &message(),
            None,
            None,
            SendCompletion::WhenSettled,
        )
        .await
        .unwrap();

    let err = client
        .cancel_task("task-done")
        .await
        .expect_err("a completed task cannot be canceled");
    let rendered = format!("{err:?}");
    assert!(
        rendered.contains("TASK_NOT_CANCELABLE"),
        "the reason has to survive the trip: {rendered}"
    );
}

#[tokio::test]
async fn push_config_lifecycle() {
    let base = spawn_server().await;
    let client = JsonRpcClient::new(base);

    let task = client
        .send_task_message(
            "task-pc",
            &message(),
            None,
            None,
            SendCompletion::WhenSettled,
        )
        .await
        .unwrap();
    let id = task.id.clone();

    let config = TaskPushNotificationConfig {
        task_id: id.clone(),
        id: "cfg-1".to_string(),
        url: "https://example.com/webhook".to_string(),
        token: "tok".to_string(),
        ..Default::default()
    };

    client.set_task_push_notification(&config).await.unwrap();

    let configs = client.list_push_notification_configs(&id).await.unwrap();
    assert!(
        !configs.is_empty(),
        "config list should be non-empty after create"
    );

    let got = client
        .get_push_notification_config(&id, "cfg-1")
        .await
        .unwrap();
    assert_eq!(got.url, "https://example.com/webhook");

    client
        .delete_push_notification_config(&id, "cfg-1")
        .await
        .unwrap();
}

#[tokio::test]
async fn subscribe_yields_initial_task_over_sse() {
    let base = spawn_server().await;
    let client = JsonRpcClient::new(base);

    let task = client
        .send_task_message(
            "task-sub",
            &message(),
            None,
            None,
            SendCompletion::WhenSettled,
        )
        .await
        .unwrap();
    let id = task.id.clone();

    let mut stream = client.subscribe_to_task(&id, None, None).await.unwrap();

    // First SSE event must be the initial task snapshot — proving the client's
    // SSE reassembly + JSON-RPC frame + StreamResponse union decode all work.
    let first = tokio::time::timeout(Duration::from_secs(5), stream.next())
        .await
        .expect("subscribe stream should yield within 5s")
        .expect("subscribe stream should not be empty")
        .expect("first event should be Ok");

    match first.item {
        StreamItem::Task(t) => assert_eq!(t.id, id),
        other => panic!("expected initial Task snapshot, got {other:?}"),
    }
}

#[tokio::test]
async fn connect_negotiates_jsonrpc_from_card() {
    let base = spawn_server().await;

    // connect() fetches the card and negotiates; the card only offers JSONRPC.
    let transport = connect(&base, &default_registry()).await.unwrap();
    assert_eq!(transport.protocol(), "JSONRPC");

    let task = transport
        .send_task_message(
            "task-neg",
            &message(),
            None,
            None,
            SendCompletion::WhenSettled,
        )
        .await
        .unwrap();
    let got = transport.get_task(&task.id, None).await.unwrap();
    assert_eq!(got.id, task.id);
}

#[tokio::test]
async fn get_task_not_found_maps_to_typed_error() {
    let base = spawn_server().await;
    let client = JsonRpcClient::new(base);

    let err = client.get_task("does-not-exist", None).await.unwrap_err();
    assert!(
        matches!(err, A2AError::TaskNotFound(_)),
        "expected TaskNotFound, got {err:?}"
    );
}