ra2a-ext 0.10.1

Extension utilities for the A2A Rust SDK (ra2a)
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
//! Extension propagation interceptors for agent-to-agent chaining.
//!
//! Aligned with Go's `a2aext.NewServerPropagator` and `a2aext.NewClientPropagator`.
//!
//! When an agent (B) acts as both server and client in a chain (A → B → C),
//! the [`ServerPropagator`] extracts extension-related metadata and headers
//! from the incoming request (A → B), stores them in a [`PropagatorContext`],
//! and the [`ClientPropagator`] injects them into the outgoing request (B → C).
//!
//! ## Data flow
//!
//! ```text
//! A → [HTTP] → B (ServerPropagator.before extracts → PropagatorContext)
//!                   → handler wraps executor in PropagatorContext::scope()
//!                     → B calls C (ClientPropagator.before injects from task_local)
//! ```
//!
//! The user must wrap downstream client calls within [`PropagatorContext::scope()`]
//! so that the [`ClientPropagator`] can access the extracted data via `task_local`.

use std::cell::RefCell;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use ra2a::SVC_PARAM_EXTENSIONS;
use ra2a::error::A2AError;
use ra2a::types::AgentCard;

use crate::util::is_extension_supported;

tokio::task_local! {
    /// Mutable cell for propagator data. Must be initialized via
    /// [`init_propagation`] before [`ServerPropagator`] can store data.
    static PROPAGATOR_CTX: RefCell<Option<PropagatorContext>>;
}

/// Extension data extracted by [`ServerPropagator`] for downstream propagation.
///
/// Aligned with Go's internal `propagatorContext` struct.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct PropagatorContext {
    /// HTTP headers to propagate (key → values).
    pub request_headers: HashMap<String, Vec<String>>,
    /// Payload metadata to propagate (key → value).
    pub metadata: HashMap<String, serde_json::Value>,
}

impl PropagatorContext {
    /// Reads the current task-local propagator context, if set.
    #[must_use]
    pub fn current() -> Option<Self> {
        PROPAGATOR_CTX
            .try_with(|cell| cell.borrow().clone())
            .ok()
            .flatten()
    }

    /// Stores this context in the task-local cell.
    ///
    /// Requires that the current task is running within [`init_propagation`].
    /// Returns `true` if stored successfully.
    #[must_use]
    pub fn install(self) -> bool {
        PROPAGATOR_CTX
            .try_with(|cell| {
                *cell.borrow_mut() = Some(self);
            })
            .is_ok()
    }

    /// Executes a future with this context directly available via task-local.
    ///
    /// This is a convenience wrapper for simple cases where you already have
    /// the context and want to make it available to [`ClientPropagator`].
    pub async fn scope<F: Future>(self, f: F) -> F::Output {
        PROPAGATOR_CTX.scope(RefCell::new(Some(self)), f).await
    }
}

/// Wraps a future with an empty propagation scope.
///
/// Call this around your request handler so that [`ServerPropagator`] can store
/// extracted data and [`ClientPropagator`] can read it later.
///
/// # Example
///
/// ```rust,ignore
/// let result = ra2a_ext::init_propagation(async {
///     // ServerPropagator.before() stores data here
///     // handler runs
///     // ClientPropagator.before() reads data here
///     handle_request(req).await
/// }).await;
/// ```
pub async fn init_propagation<F: Future>(f: F) -> F::Output {
    PROPAGATOR_CTX.scope(RefCell::new(None), f).await
}

/// Predicate function for filtering metadata keys on the server side.
///
/// Receives the list of requested extension URIs and the metadata key.
/// Returns `true` if the key should be propagated.
pub(crate) type ServerMetadataPredicate = Arc<dyn Fn(&[String], &str) -> bool + Send + Sync>;

/// Predicate function for filtering request headers on the server side.
///
/// Receives the header key. Returns `true` if the header should be propagated.
pub(crate) type ServerHeaderPredicate = Arc<dyn Fn(&str) -> bool + Send + Sync>;

/// Configuration for [`ServerPropagator`].
///
/// Both predicates are optional — sensible defaults are used when `None`.
#[derive(Default)]
#[non_exhaustive]
pub struct ServerPropagatorConfig {
    /// Determines which payload metadata keys are propagated.
    ///
    /// Default: propagate keys whose name matches a client-requested extension URI.
    pub metadata_predicate: Option<ServerMetadataPredicate>,
    /// Determines which request headers are propagated.
    ///
    /// Default: propagate only the `x-a2a-extensions` header.
    pub header_predicate: Option<ServerHeaderPredicate>,
}

impl std::fmt::Debug for ServerPropagatorConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ServerPropagatorConfig")
            .field("metadata_predicate", &self.metadata_predicate.is_some())
            .field("header_predicate", &self.header_predicate.is_some())
            .finish()
    }
}

/// Server-side [`CallInterceptor`](ra2a::server::CallInterceptor) that extracts
/// extension-related metadata and headers from incoming requests.
///
/// The extracted data is stored in a [`PropagatorContext`] via `task_local`.
/// The handler must be wrapped in [`init_propagation`] for this to work.
/// [`ClientPropagator`] reads the stored context when making downstream calls.
///
/// Aligned with Go's `a2aext.NewServerPropagator`.
pub struct ServerPropagator {
    /// Metadata filter predicate.
    metadata_predicate: ServerMetadataPredicate,
    /// Header filter predicate.
    header_predicate: ServerHeaderPredicate,
}

impl ServerPropagator {
    /// Creates a new server propagator with default configuration.
    ///
    /// Default behavior:
    /// - Propagates metadata keys matching client-requested extension URIs
    /// - Propagates the `x-a2a-extensions` header
    #[must_use]
    pub fn new() -> Self {
        Self::with_config(ServerPropagatorConfig::default())
    }

    /// Creates a new server propagator with custom configuration.
    #[must_use]
    pub fn with_config(config: ServerPropagatorConfig) -> Self {
        let metadata_predicate = config.metadata_predicate.unwrap_or_else(|| {
            Arc::new(|requested_uris: &[String], key: &str| requested_uris.iter().any(|u| u == key))
        });

        let header_predicate = config.header_predicate.unwrap_or_else(|| {
            Arc::new(|key: &str| key.eq_ignore_ascii_case(SVC_PARAM_EXTENSIONS))
        });

        Self {
            metadata_predicate,
            header_predicate,
        }
    }
}

impl Default for ServerPropagator {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for ServerPropagator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ServerPropagator").finish_non_exhaustive()
    }
}

impl ServerPropagator {
    /// Extracts extension metadata from the incoming request into the call context.
    fn propagate_server(&self, ctx: &mut ra2a::server::CallContext, req: &ra2a::server::Request) {
        let mut prop_ctx = PropagatorContext::default();

        let requested = ctx.requested_extension_uris();

        extract_metadata(
            req,
            &requested,
            &self.metadata_predicate,
            &mut prop_ctx.metadata,
        );

        let request_meta = ctx.request_meta();
        for (header_name, header_values) in request_meta.iter() {
            if (self.header_predicate)(header_name) {
                prop_ctx
                    .request_headers
                    .insert(header_name.to_owned(), header_values.to_vec());
            }
        }

        if let Some(ext_values) = prop_ctx.request_headers.get(SVC_PARAM_EXTENSIONS) {
            for uri in ext_values {
                ctx.activate_extension(uri);
            }
        }

        // Best-effort install; fails silently if not inside init_propagation.
        let _installed = prop_ctx.install();
    }
}

impl ra2a::server::CallInterceptor for ServerPropagator {
    fn before<'a>(
        &'a self,
        ctx: &'a mut ra2a::server::CallContext,
        req: &'a mut ra2a::server::Request,
    ) -> Pin<Box<dyn Future<Output = Result<(), A2AError>> + Send + 'a>> {
        self.propagate_server(ctx, req);
        Box::pin(std::future::ready(Ok(())))
    }

    fn after<'a>(
        &'a self,
        _ctx: &'a ra2a::server::CallContext,
        _resp: &'a mut ra2a::server::Response,
    ) -> Pin<Box<dyn Future<Output = Result<(), A2AError>> + Send + 'a>> {
        Box::pin(async { Ok(()) })
    }
}

/// Extracts matching metadata from known request payload types.
fn extract_metadata(
    req: &ra2a::server::Request,
    requested: &[String],
    predicate: &ServerMetadataPredicate,
    out: &mut HashMap<String, serde_json::Value>,
) {
    if let Some(params) = req.downcast_ref::<ra2a::SendMessageRequest>()
        && let Some(ref meta) = params.metadata
    {
        collect_matching_metadata(meta, requested, predicate, out);
    }
}

/// Collects metadata entries that pass the predicate.
fn collect_matching_metadata(
    metadata: &ra2a::Metadata,
    requested: &[String],
    predicate: &ServerMetadataPredicate,
    out: &mut HashMap<String, serde_json::Value>,
) {
    for (k, v) in metadata {
        if predicate(requested, k) {
            out.insert(k.clone(), v.clone());
        }
    }
}

/// Predicate function for filtering metadata keys on the client side.
///
/// Receives the target server's agent card (if available), the list of
/// requested extension URIs, and the metadata key.
pub(crate) type ClientMetadataPredicate =
    Arc<dyn Fn(Option<&AgentCard>, &[String], &str) -> bool + Send + Sync>;

/// Predicate function for filtering request headers on the client side.
///
/// Receives the target server's agent card (if available), the header key
/// and value. Returns `true` if the header should be forwarded.
pub(crate) type ClientHeaderPredicate =
    Arc<dyn Fn(Option<&AgentCard>, &str, &str) -> bool + Send + Sync>;

/// Configuration for [`ClientPropagator`].
#[derive(Default)]
#[non_exhaustive]
pub struct ClientPropagatorConfig {
    /// Determines which payload metadata keys are propagated.
    ///
    /// Default: propagate keys that are requested extensions and supported by
    /// the downstream server.
    pub metadata_predicate: Option<ClientMetadataPredicate>,
    /// Determines which request headers are propagated.
    ///
    /// Default: propagate `x-a2a-extensions` header values for extensions
    /// supported by the downstream server.
    pub header_predicate: Option<ClientHeaderPredicate>,
}

impl std::fmt::Debug for ClientPropagatorConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ClientPropagatorConfig")
            .field("metadata_predicate", &self.metadata_predicate.is_some())
            .field("header_predicate", &self.header_predicate.is_some())
            .finish()
    }
}

/// Client-side [`CallInterceptor`](ra2a::client::CallInterceptor) that injects
/// propagated extension data into outgoing requests.
///
/// Reads [`PropagatorContext`] from the task-local (set by [`ServerPropagator`])
/// and injects matching metadata and headers into the outgoing request.
///
/// Aligned with Go's `a2aext.NewClientPropagator`.
pub struct ClientPropagator {
    /// Metadata filter predicate.
    metadata_predicate: ClientMetadataPredicate,
    /// Header filter predicate.
    header_predicate: ClientHeaderPredicate,
}

impl ClientPropagator {
    /// Creates a new client propagator with default configuration.
    #[must_use]
    pub fn new() -> Self {
        Self::with_config(ClientPropagatorConfig::default())
    }

    /// Creates a new client propagator with custom configuration.
    #[must_use]
    pub fn with_config(config: ClientPropagatorConfig) -> Self {
        let metadata_predicate = config
            .metadata_predicate
            .unwrap_or_else(|| Arc::new(default_client_metadata_predicate));

        let header_predicate = config
            .header_predicate
            .unwrap_or_else(|| Arc::new(default_client_header_predicate));

        Self {
            metadata_predicate,
            header_predicate,
        }
    }
}

impl Default for ClientPropagator {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for ClientPropagator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ClientPropagator").finish_non_exhaustive()
    }
}

impl ClientPropagator {
    /// Injects extension metadata from the current propagator context into the outgoing request.
    fn propagate_client(&self, req: &mut ra2a::client::Request) {
        let Some(prop_ctx) = PropagatorContext::current() else {
            return;
        };

        let requested: Vec<String> = prop_ctx
            .request_headers
            .get(SVC_PARAM_EXTENSIONS)
            .cloned()
            .unwrap_or_default();

        if !prop_ctx.metadata.is_empty() {
            inject_metadata(
                &mut *req.payload,
                &prop_ctx.metadata,
                req.card.as_ref(),
                &requested,
                &self.metadata_predicate,
            );
        }

        for (name, val) in prop_ctx
            .request_headers
            .iter()
            .flat_map(|(k, vs)| vs.iter().map(move |v| (k, v)))
        {
            if (self.header_predicate)(req.card.as_ref(), name, val) {
                req.service_params.append(name, val);
            }
        }
    }
}

impl ra2a::client::CallInterceptor for ClientPropagator {
    fn before<'a>(
        &'a self,
        req: &'a mut ra2a::client::Request,
    ) -> Pin<Box<dyn Future<Output = ra2a::error::Result<()>> + Send + 'a>> {
        self.propagate_client(req);
        Box::pin(std::future::ready(Ok(())))
    }
}

/// Default metadata predicate for [`ClientPropagator`]: propagates keys that
/// are in the requested list and supported by the downstream agent card.
fn default_client_metadata_predicate(
    card: Option<&AgentCard>,
    requested: &[String],
    key: &str,
) -> bool {
    requested.iter().any(|u| u == key) && is_extension_supported(card, key)
}

/// Default header predicate for [`ClientPropagator`]: propagates
/// `x-a2a-extensions` header values for extensions supported by the downstream agent.
fn default_client_header_predicate(card: Option<&AgentCard>, key: &str, val: &str) -> bool {
    key.eq_ignore_ascii_case(SVC_PARAM_EXTENSIONS) && is_extension_supported(card, val)
}

/// Injects matching metadata into known outgoing payload types.
fn inject_metadata(
    payload: &mut dyn std::any::Any,
    metadata: &HashMap<String, serde_json::Value>,
    card: Option<&AgentCard>,
    requested: &[String],
    predicate: &ClientMetadataPredicate,
) {
    if let Some(params) = payload.downcast_mut::<ra2a::SendMessageRequest>() {
        let meta = params.metadata.get_or_insert_with(Default::default);
        inject_matching_metadata(meta, metadata, card, requested, predicate);
    }
}

/// Inserts metadata entries that pass the predicate into the target map.
fn inject_matching_metadata(
    target: &mut ra2a::Metadata,
    source: &HashMap<String, serde_json::Value>,
    card: Option<&AgentCard>,
    requested: &[String],
    predicate: &ClientMetadataPredicate,
) {
    for (k, v) in source {
        if predicate(card, requested, k) {
            target.insert(k.clone(), v.clone());
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, reason = "tests use unwrap for brevity")]
mod tests {
    use ra2a::client::{CallInterceptor as _, ServiceParams};
    use ra2a::types::{
        AgentCapabilities, AgentCard, AgentExtension, AgentInterface, TransportProtocol,
    };

    use super::*;

    fn make_card(uris: &[&str]) -> AgentCard {
        let mut card = AgentCard::new(
            "test",
            "test agent",
            vec![AgentInterface::new(
                "https://example.com",
                TransportProtocol::new("JSONRPC"),
            )],
        );
        card.capabilities = AgentCapabilities {
            extensions: uris
                .iter()
                .map(|u| AgentExtension {
                    uri: (*u).into(),
                    description: None,
                    required: false,
                    params: None,
                })
                .collect(),
            ..AgentCapabilities::default()
        };
        card
    }

    #[tokio::test]
    async fn test_client_propagator_injects_headers() {
        let propagator = ClientPropagator::new();
        let card = make_card(&["urn:a2a:ext:duration"]);

        let mut prop_ctx = PropagatorContext::default();
        prop_ctx.request_headers.insert(
            SVC_PARAM_EXTENSIONS.to_owned(),
            vec!["urn:a2a:ext:duration".into()],
        );

        let mut req = ra2a::client::Request {
            method: "message/send".into(),
            service_params: ServiceParams::default(),
            card: Some(card),
            payload: Box::new(()),
        };

        prop_ctx
            .scope(async {
                propagator.before(&mut req).await.unwrap();
            })
            .await;

        let vals = req.service_params.get_all(SVC_PARAM_EXTENSIONS);
        assert_eq!(vals, &["urn:a2a:ext:duration"]);
    }

    #[tokio::test]
    async fn test_client_propagator_filters_unsupported() {
        let propagator = ClientPropagator::new();
        let card = make_card(&["urn:a2a:ext:other"]);

        let mut prop_ctx = PropagatorContext::default();
        prop_ctx.request_headers.insert(
            SVC_PARAM_EXTENSIONS.to_owned(),
            vec!["urn:a2a:ext:duration".into()],
        );

        let mut req = ra2a::client::Request {
            method: "message/send".into(),
            service_params: ServiceParams::default(),
            card: Some(card),
            payload: Box::new(()),
        };

        prop_ctx
            .scope(async {
                propagator.before(&mut req).await.unwrap();
            })
            .await;

        let vals = req.service_params.get_all(SVC_PARAM_EXTENSIONS);
        assert!(vals.is_empty());
    }

    #[tokio::test]
    async fn test_client_propagator_no_context_is_noop() {
        let propagator = ClientPropagator::new();

        let mut req = ra2a::client::Request {
            method: "message/send".into(),
            service_params: ServiceParams::default(),
            card: None,
            payload: Box::new(()),
        };

        propagator.before(&mut req).await.unwrap();
        assert!(req.service_params.is_empty());
    }

    #[tokio::test]
    async fn test_propagator_context_install_and_read() {
        let ctx = PropagatorContext {
            request_headers: {
                let mut m = HashMap::new();
                m.insert("x-test".into(), vec!["val1".into()]);
                m
            },
            metadata: HashMap::new(),
        };

        init_propagation(async {
            assert!(PropagatorContext::current().is_none());
            assert!(ctx.install());
            let read = PropagatorContext::current().unwrap();
            assert_eq!(
                read.request_headers.get("x-test").unwrap(),
                &["val1".to_owned()]
            );
        })
        .await;
    }
}