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
//! Push notification sender implementation

// This module is already conditionally compiled with #[cfg(feature = "server")] in mod.rs

use std::sync::Arc;

use async_trait::async_trait;
#[cfg(feature = "http-client")]
use reqwest::{
    Client,
    header::{AUTHORIZATION, CONTENT_TYPE, HeaderMap, HeaderValue},
};
use tokio::sync::Mutex;

use crate::domain::{
    A2AError, TaskArtifactUpdateEvent, TaskPushNotificationConfig, TaskStatusUpdateEvent,
};
use crate::port::AsyncPushNotifier;

/// Interface for a push notification sender
#[async_trait]
pub trait PushNotificationSender: Send + Sync {
    /// Send a status update notification
    async fn send_status_update(
        &self,
        config: &TaskPushNotificationConfig,
        event: &TaskStatusUpdateEvent,
    ) -> Result<(), A2AError>;

    /// Send an artifact update notification
    async fn send_artifact_update(
        &self,
        config: &TaskPushNotificationConfig,
        event: &TaskArtifactUpdateEvent,
    ) -> Result<(), A2AError>;
}

/// HTTP-based push notification sender
#[cfg(feature = "http-client")]
pub struct HttpPushNotificationSender {
    /// HTTP client for sending notifications
    client: Client,
    /// Timeout in seconds
    timeout: u64,
    /// Maximum number of retries
    max_retries: u32,
    /// Backoff factor in milliseconds
    backoff_ms: u64,
}

#[cfg(feature = "http-client")]
impl Default for HttpPushNotificationSender {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "http-client")]
impl HttpPushNotificationSender {
    /// Create a new push notification sender
    pub fn new() -> Self {
        Self {
            client: Client::new(),
            timeout: 30,      // Default timeout in seconds
            max_retries: 3,   // Default max retries
            backoff_ms: 1000, // Default backoff in milliseconds (1 second)
        }
    }

    /// Set the timeout for requests
    pub fn with_timeout(mut self, timeout: u64) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set the maximum number of retries
    pub fn with_max_retries(mut self, max_retries: u32) -> Self {
        self.max_retries = max_retries;
        self
    }

    /// Set the backoff factor in milliseconds
    pub fn with_backoff_ms(mut self, backoff_ms: u64) -> Self {
        self.backoff_ms = backoff_ms;
        self
    }

    /// Get the headers for a request
    fn get_headers(&self, config: &TaskPushNotificationConfig) -> HeaderMap {
        let mut headers = HeaderMap::new();
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));

        // Add token if provided
        if !config.token.is_empty() {
            headers.insert(
                AUTHORIZATION,
                HeaderValue::from_str(&format!("Bearer {}", config.token))
                    .unwrap_or_else(|_| HeaderValue::from_static("Invalid token")),
            );
        }

        // Add additional authentication headers if provided
        if let Some(auth) = config.authentication.as_option() {
            // Here we could add specific authentication headers based on the schemes
            // For now we just add the credentials if provided
            if !auth.credentials.is_empty() && !auth.scheme.is_empty() {
                let scheme = &auth.scheme;

                if scheme.to_lowercase() == "basic" {
                    headers.insert(
                        AUTHORIZATION,
                        HeaderValue::from_str(&format!("Basic {}", auth.credentials))
                            .unwrap_or_else(|_| HeaderValue::from_static("Invalid credentials")),
                    );
                } else if scheme.to_lowercase() == "bearer" {
                    headers.insert(
                        AUTHORIZATION,
                        HeaderValue::from_str(&format!("Bearer {}", auth.credentials))
                            .unwrap_or_else(|_| HeaderValue::from_static("Invalid credentials")),
                    );
                }
            }
        }

        headers
    }
}

#[cfg(feature = "http-client")]
#[async_trait]
impl PushNotificationSender for HttpPushNotificationSender {
    async fn send_status_update(
        &self,
        config: &TaskPushNotificationConfig,
        event: &TaskStatusUpdateEvent,
    ) -> Result<(), A2AError> {
        let mut last_error = None;

        #[cfg(feature = "tracing")]
        tracing::debug!(
            task_id = %event.task_id,
            url = %config.url,
            "Preparing to send HTTP push notification"
        );

        // Try with retries
        for attempt in 0..=self.max_retries {
            // If this is a retry, wait with exponential backoff
            if attempt > 0 {
                let backoff = self.backoff_ms * (1 << (attempt - 1));
                #[cfg(feature = "tracing")]
                tracing::debug!(
                    task_id = %event.task_id,
                    attempt = attempt,
                    backoff_ms = backoff,
                    "Retrying push notification after backoff"
                );
                tokio::time::sleep(tokio::time::Duration::from_millis(backoff)).await;
            }

            // Send the notification
            #[cfg(feature = "tracing")]
            tracing::debug!(
                task_id = %event.task_id,
                attempt = attempt,
                url = %config.url,
                "Sending HTTP POST request for push notification"
            );

            match self
                .client
                .post(&config.url)
                .headers(self.get_headers(config))
                .json(event)
                .timeout(std::time::Duration::from_secs(self.timeout))
                .send()
                .await
            {
                Ok(response) => {
                    let status = response.status();
                    #[cfg(feature = "tracing")]
                    tracing::debug!(
                        task_id = %event.task_id,
                        status = %status,
                        "Received response from push notification endpoint"
                    );

                    // Check if the request was successful
                    if status.is_success() {
                        #[cfg(feature = "tracing")]
                        tracing::info!(
                            task_id = %event.task_id,
                            status = %status,
                            "Push notification HTTP request succeeded"
                        );
                        return Ok(());
                    } else {
                        let body = response.text().await.unwrap_or_default();
                        #[cfg(feature = "tracing")]
                        tracing::warn!(
                            task_id = %event.task_id,
                            status = %status,
                            body = %body,
                            "Push notification HTTP request failed"
                        );
                        last_error = Some(A2AError::Internal(format!(
                            "Push notification failed with status {}: {}",
                            status, body
                        )));

                        // Don't retry on client errors (4xx)
                        if status.is_client_error() {
                            break;
                        }
                    }
                }
                Err(e) => {
                    #[cfg(feature = "tracing")]
                    tracing::warn!(
                        task_id = %event.task_id,
                        error = %e,
                        "Failed to send HTTP request for push notification"
                    );
                    // Store the error but continue retrying
                    last_error = Some(A2AError::Internal(format!(
                        "Failed to send push notification: {}",
                        e
                    )));
                }
            }
        }

        // Return the last error if we had one
        Err(last_error.unwrap_or_else(|| {
            A2AError::Internal("Unknown error sending push notification".to_string())
        }))
    }

    async fn send_artifact_update(
        &self,
        config: &TaskPushNotificationConfig,
        event: &TaskArtifactUpdateEvent,
    ) -> Result<(), A2AError> {
        let mut last_error = None;

        // Try with retries
        for attempt in 0..=self.max_retries {
            // If this is a retry, wait with exponential backoff
            if attempt > 0 {
                let backoff = self.backoff_ms * (1 << (attempt - 1));
                tokio::time::sleep(tokio::time::Duration::from_millis(backoff)).await;
            }

            // Send the notification
            match self
                .client
                .post(&config.url)
                .headers(self.get_headers(config))
                .json(event)
                .timeout(std::time::Duration::from_secs(self.timeout))
                .send()
                .await
            {
                Ok(response) => {
                    // Check if the request was successful
                    if response.status().is_success() {
                        return Ok(());
                    } else {
                        let status = response.status();
                        let body = response.text().await.unwrap_or_default();
                        last_error = Some(A2AError::Internal(format!(
                            "Push notification failed with status {}: {}",
                            status, body
                        )));

                        // Don't retry on client errors (4xx)
                        if status.is_client_error() {
                            break;
                        }
                    }
                }
                Err(e) => {
                    // Store the error but continue retrying
                    last_error = Some(A2AError::Internal(format!(
                        "Failed to send push notification: {}",
                        e
                    )));
                }
            }
        }

        // Return the last error if we had one
        Err(last_error.unwrap_or_else(|| {
            A2AError::Internal("Unknown error sending push notification".to_string())
        }))
    }
}

/// No-op push notification sender that does nothing
#[derive(Default)]
pub struct NoopPushNotificationSender;

#[async_trait]
impl PushNotificationSender for NoopPushNotificationSender {
    async fn send_status_update(
        &self,
        _config: &TaskPushNotificationConfig,
        _event: &TaskStatusUpdateEvent,
    ) -> Result<(), A2AError> {
        // Do nothing - no-op implementation
        Ok(())
    }

    async fn send_artifact_update(
        &self,
        _config: &TaskPushNotificationConfig,
        _event: &TaskArtifactUpdateEvent,
    ) -> Result<(), A2AError> {
        // Do nothing - no-op implementation
        Ok(())
    }
}

/// In-memory push notification sender registry
pub struct PushNotificationRegistry {
    /// Sender for push notifications
    sender: Arc<dyn PushNotificationSender>,
    /// Registry of task IDs to push notification configs
    registry: Arc<Mutex<std::collections::HashMap<String, TaskPushNotificationConfig>>>,
}

impl PushNotificationRegistry {
    /// Create a new push notification registry
    pub fn new(sender: impl PushNotificationSender + 'static) -> Self {
        Self {
            sender: Arc::new(sender),
            registry: Arc::new(Mutex::new(std::collections::HashMap::new())),
        }
    }

    /// Register a push notification configuration for a task
    pub async fn register(
        &self,
        task_id: &str,
        config: TaskPushNotificationConfig,
    ) -> Result<(), A2AError> {
        let mut registry = self.registry.lock().await;
        registry.insert(task_id.to_string(), config);
        Ok(())
    }

    /// Unregister a push notification configuration for a task
    pub async fn unregister(&self, task_id: &str) -> Result<(), A2AError> {
        let mut registry = self.registry.lock().await;
        registry.remove(task_id);
        Ok(())
    }

    /// Get the push notification configuration for a task
    pub async fn get_config(
        &self,
        task_id: &str,
    ) -> Result<Option<TaskPushNotificationConfig>, A2AError> {
        let registry = self.registry.lock().await;
        Ok(registry.get(task_id).cloned())
    }

    /// Send a status update notification for a task
    pub async fn send_status_update(
        &self,
        task_id: &str,
        event: &TaskStatusUpdateEvent,
    ) -> Result<(), A2AError> {
        let config = {
            let registry = self.registry.lock().await;
            registry.get(task_id).cloned()
        };

        if let Some(config) = config {
            #[cfg(feature = "tracing")]
            tracing::info!(
                task_id = %task_id,
                url = %config.url,
                state = ?event.status.state,
                "📤 Sending push notification for status update"
            );

            match self.sender.send_status_update(&config, event).await {
                Ok(()) => {
                    #[cfg(feature = "tracing")]
                    tracing::info!(
                        task_id = %task_id,
                        "✅ Push notification sent successfully"
                    );
                    Ok(())
                }
                Err(e) => {
                    #[cfg(feature = "tracing")]
                    tracing::error!(
                        task_id = %task_id,
                        error = %e,
                        "❌ Failed to send push notification"
                    );
                    Err(e)
                }
            }
        } else {
            #[cfg(feature = "tracing")]
            tracing::debug!(
                task_id = %task_id,
                "⚠️  No push notification config registered for task"
            );
            // No push notification configured for this task
            Ok(())
        }
    }

    /// Send an artifact update notification for a task
    pub async fn send_artifact_update(
        &self,
        task_id: &str,
        event: &TaskArtifactUpdateEvent,
    ) -> Result<(), A2AError> {
        let registry = self.registry.lock().await;

        if let Some(config) = registry.get(task_id) {
            self.sender.send_artifact_update(config, event).await?;
            Ok(())
        } else {
            // No push notification configured for this task
            Ok(())
        }
    }
}

/// The registry is the in-house [`AsyncPushNotifier`] adapter: it looks up the
/// per-task config it holds and dispatches through its pluggable
/// [`PushNotificationSender`] backend. The two trait methods are exactly the
/// existing inherent `send_*` methods.
#[async_trait]
impl AsyncPushNotifier for PushNotificationRegistry {
    async fn notify_status(
        &self,
        task_id: &str,
        event: &TaskStatusUpdateEvent,
    ) -> Result<(), A2AError> {
        self.send_status_update(task_id, event).await
    }

    async fn notify_artifact(
        &self,
        task_id: &str,
        event: &TaskArtifactUpdateEvent,
    ) -> Result<(), A2AError> {
        self.send_artifact_update(task_id, event).await
    }
}