a2a-lf 0.3.0

A2A v1 protocol types and core definitions
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
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0
use std::collections::HashMap;

use chrono::Utc;
use serde_json::Value;

use crate::errordetails::{self, TypedDetail};

/// A2A-specific error codes (JSON-RPC).
pub mod error_code {
    // A2A application errors
    pub const TASK_NOT_FOUND: i32 = -32001;
    pub const TASK_NOT_CANCELABLE: i32 = -32002;
    pub const PUSH_NOTIFICATION_NOT_SUPPORTED: i32 = -32003;
    pub const UNSUPPORTED_OPERATION: i32 = -32004;
    pub const CONTENT_TYPE_NOT_SUPPORTED: i32 = -32005;
    pub const INVALID_AGENT_RESPONSE: i32 = -32006;
    pub const EXTENDED_CARD_NOT_CONFIGURED: i32 = -32007;
    pub const EXTENSION_SUPPORT_REQUIRED: i32 = -32008;
    pub const VERSION_NOT_SUPPORTED: i32 = -32009;

    // Standard JSON-RPC errors
    pub const PARSE_ERROR: i32 = -32700;
    pub const INVALID_REQUEST: i32 = -32600;
    pub const METHOD_NOT_FOUND: i32 = -32601;
    pub const INVALID_PARAMS: i32 = -32602;
    pub const INTERNAL_ERROR: i32 = -32603;
}

/// Returns the reason string for an error code.
pub fn error_reason(code: i32) -> &'static str {
    match code {
        error_code::TASK_NOT_FOUND => "TASK_NOT_FOUND",
        error_code::TASK_NOT_CANCELABLE => "TASK_NOT_CANCELABLE",
        error_code::PUSH_NOTIFICATION_NOT_SUPPORTED => "PUSH_NOTIFICATION_NOT_SUPPORTED",
        error_code::UNSUPPORTED_OPERATION => "UNSUPPORTED_OPERATION",
        error_code::CONTENT_TYPE_NOT_SUPPORTED => "CONTENT_TYPE_NOT_SUPPORTED",
        error_code::INVALID_AGENT_RESPONSE => "INVALID_AGENT_RESPONSE",
        error_code::EXTENDED_CARD_NOT_CONFIGURED => "EXTENDED_AGENT_CARD_NOT_CONFIGURED",
        error_code::EXTENSION_SUPPORT_REQUIRED => "EXTENSION_SUPPORT_REQUIRED",
        error_code::VERSION_NOT_SUPPORTED => "VERSION_NOT_SUPPORTED",
        error_code::PARSE_ERROR => "PARSE_ERROR",
        error_code::INVALID_REQUEST => "INVALID_REQUEST",
        error_code::METHOD_NOT_FOUND => "METHOD_NOT_FOUND",
        error_code::INVALID_PARAMS => "INVALID_PARAMS",
        _ => "INTERNAL_ERROR",
    }
}

/// Returns the error code for a reason string, or `None` if unrecognized.
pub fn reason_to_error_code(reason: &str) -> Option<i32> {
    match reason {
        "TASK_NOT_FOUND" => Some(error_code::TASK_NOT_FOUND),
        "TASK_NOT_CANCELABLE" => Some(error_code::TASK_NOT_CANCELABLE),
        "PUSH_NOTIFICATION_NOT_SUPPORTED" => Some(error_code::PUSH_NOTIFICATION_NOT_SUPPORTED),
        "UNSUPPORTED_OPERATION" => Some(error_code::UNSUPPORTED_OPERATION),
        "UNSUPPORTED_CONTENT_TYPE" | "CONTENT_TYPE_NOT_SUPPORTED" => {
            Some(error_code::CONTENT_TYPE_NOT_SUPPORTED)
        }
        "INVALID_AGENT_RESPONSE" => Some(error_code::INVALID_AGENT_RESPONSE),
        "EXTENDED_AGENT_CARD_NOT_CONFIGURED" | "EXTENDED_CARD_NOT_CONFIGURED" => {
            Some(error_code::EXTENDED_CARD_NOT_CONFIGURED)
        }
        "EXTENSION_SUPPORT_REQUIRED" => Some(error_code::EXTENSION_SUPPORT_REQUIRED),
        "VERSION_NOT_SUPPORTED" => Some(error_code::VERSION_NOT_SUPPORTED),
        "PARSE_ERROR" => Some(error_code::PARSE_ERROR),
        "INVALID_REQUEST" => Some(error_code::INVALID_REQUEST),
        "METHOD_NOT_FOUND" => Some(error_code::METHOD_NOT_FOUND),
        "INVALID_PARAMS" => Some(error_code::INVALID_PARAMS),
        "INTERNAL_ERROR" => Some(error_code::INTERNAL_ERROR),
        _ => None,
    }
}

/// An A2A protocol error.
#[derive(Debug, Clone, thiserror::Error)]
#[error("{message}")]
pub struct A2AError {
    pub code: i32,
    pub message: String,
    pub details: Option<Vec<TypedDetail>>,
}

impl A2AError {
    pub fn new(code: i32, message: impl Into<String>) -> Self {
        A2AError {
            code,
            message: message.into(),
            details: None,
        }
    }

    pub fn with_details(mut self, details: Vec<TypedDetail>) -> Self {
        self.details = Some(details);
        self
    }

    // Convenience constructors for common errors

    pub fn task_not_found(task_id: &str) -> Self {
        A2AError::new(
            error_code::TASK_NOT_FOUND,
            format!("task not found: {task_id}"),
        )
    }

    pub fn task_not_cancelable(task_id: &str) -> Self {
        A2AError::new(
            error_code::TASK_NOT_CANCELABLE,
            format!("task cannot be canceled: {task_id}"),
        )
    }

    pub fn push_notification_not_supported() -> Self {
        A2AError::new(
            error_code::PUSH_NOTIFICATION_NOT_SUPPORTED,
            "push notification not supported",
        )
    }

    pub fn unsupported_operation(msg: impl Into<String>) -> Self {
        A2AError::new(error_code::UNSUPPORTED_OPERATION, msg)
    }

    pub fn content_type_not_supported() -> Self {
        A2AError::new(
            error_code::CONTENT_TYPE_NOT_SUPPORTED,
            "incompatible content types",
        )
    }

    pub fn invalid_agent_response() -> Self {
        A2AError::new(error_code::INVALID_AGENT_RESPONSE, "invalid agent response")
    }

    pub fn version_not_supported(version: &str) -> Self {
        A2AError::new(
            error_code::VERSION_NOT_SUPPORTED,
            format!("version not supported: {version}"),
        )
    }

    pub fn internal(msg: impl Into<String>) -> Self {
        A2AError::new(error_code::INTERNAL_ERROR, msg)
    }

    pub fn invalid_params(msg: impl Into<String>) -> Self {
        A2AError::new(error_code::INVALID_PARAMS, msg)
    }

    pub fn parse_error(msg: impl Into<String>) -> Self {
        A2AError::new(error_code::PARSE_ERROR, msg)
    }

    pub fn invalid_request(msg: impl Into<String>) -> Self {
        A2AError::new(error_code::INVALID_REQUEST, msg)
    }

    pub fn method_not_found(method: &str) -> Self {
        A2AError::new(
            error_code::METHOD_NOT_FOUND,
            format!("method not found: {method}"),
        )
    }

    /// Map A2A error code to HTTP status code for REST binding.
    pub fn http_status_code(&self) -> u16 {
        match self.code {
            error_code::TASK_NOT_FOUND => 404,
            error_code::TASK_NOT_CANCELABLE => 400,
            error_code::PUSH_NOTIFICATION_NOT_SUPPORTED => 400,
            error_code::UNSUPPORTED_OPERATION => 400,
            error_code::CONTENT_TYPE_NOT_SUPPORTED => 400,
            error_code::VERSION_NOT_SUPPORTED => 400,
            error_code::PARSE_ERROR => 400,
            error_code::INVALID_REQUEST => 400,
            error_code::METHOD_NOT_FOUND => 501,
            error_code::INVALID_PARAMS => 400,
            error_code::INTERNAL_ERROR => 500,
            _ => 500,
        }
    }

    /// Convert to a JSON-RPC error object.
    ///
    /// The `data` field is always populated with an array of typed detail
    /// objects. An `ErrorInfo` detail (with reason, domain, and timestamp)
    /// is always appended as the last element, matching the Go SDK behavior.
    pub fn to_jsonrpc_error(&self) -> crate::JsonRpcError {
        let reason = error_reason(self.code);
        let metadata = HashMap::from([("timestamp".to_string(), Utc::now().to_rfc3339())]);

        let mut data: Vec<Value> = self
            .details
            .as_ref()
            .map(|d| {
                d.iter()
                    .filter(|detail| detail.type_url != errordetails::ERROR_INFO_TYPE)
                    .map(|detail| serde_json::to_value(detail).unwrap_or_default())
                    .collect()
            })
            .unwrap_or_default();

        let mut error_info =
            TypedDetail::error_info(reason, errordetails::PROTOCOL_DOMAIN, Some(metadata));

        if let Some(details) = &self.details {
            for existing in details
                .iter()
                .filter(|d| d.type_url == errordetails::ERROR_INFO_TYPE)
            {
                if let Some(Value::Object(meta)) = existing.value.get("metadata") {
                    if let Some(Value::Object(info_meta)) = error_info.value.get_mut("metadata") {
                        for (k, v) in meta {
                            info_meta.entry(k.clone()).or_insert_with(|| v.clone());
                        }
                    }
                }
            }
        }

        data.push(serde_json::to_value(&error_info).unwrap_or_default());

        crate::JsonRpcError {
            code: self.code,
            message: self.message.clone(),
            data: Some(Value::Array(data)),
        }
    }
}

impl From<A2AError> for crate::JsonRpcError {
    fn from(e: A2AError) -> Self {
        e.to_jsonrpc_error()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_error_constructors() {
        let e = A2AError::task_not_found("t1");
        assert_eq!(e.code, error_code::TASK_NOT_FOUND);
        assert!(e.message.contains("t1"));

        let e = A2AError::task_not_cancelable("t2");
        assert_eq!(e.code, error_code::TASK_NOT_CANCELABLE);

        let e = A2AError::push_notification_not_supported();
        assert_eq!(e.code, error_code::PUSH_NOTIFICATION_NOT_SUPPORTED);

        let e = A2AError::unsupported_operation("nope");
        assert_eq!(e.code, error_code::UNSUPPORTED_OPERATION);

        let e = A2AError::content_type_not_supported();
        assert_eq!(e.code, error_code::CONTENT_TYPE_NOT_SUPPORTED);

        let e = A2AError::invalid_agent_response();
        assert_eq!(e.code, error_code::INVALID_AGENT_RESPONSE);

        let e = A2AError::version_not_supported("2.0");
        assert_eq!(e.code, error_code::VERSION_NOT_SUPPORTED);

        let e = A2AError::internal("boom");
        assert_eq!(e.code, error_code::INTERNAL_ERROR);

        let e = A2AError::invalid_params("bad param");
        assert_eq!(e.code, error_code::INVALID_PARAMS);

        let e = A2AError::parse_error("bad json");
        assert_eq!(e.code, error_code::PARSE_ERROR);

        let e = A2AError::invalid_request("bad req");
        assert_eq!(e.code, error_code::INVALID_REQUEST);

        let e = A2AError::method_not_found("foo");
        assert_eq!(e.code, error_code::METHOD_NOT_FOUND);
    }

    #[test]
    fn test_http_status_codes() {
        assert_eq!(A2AError::task_not_found("x").http_status_code(), 404);
        assert_eq!(A2AError::task_not_cancelable("x").http_status_code(), 400);
        assert_eq!(A2AError::internal("x").http_status_code(), 500);
        assert_eq!(A2AError::invalid_params("x").http_status_code(), 400);
        assert_eq!(
            A2AError::content_type_not_supported().http_status_code(),
            400
        );
        assert_eq!(A2AError::new(9999, "unknown").http_status_code(), 500);
    }

    #[test]
    fn test_http_status_codes_for_remaining_a2a_mappings() {
        assert_eq!(
            A2AError::push_notification_not_supported().http_status_code(),
            400
        );
        assert_eq!(
            A2AError::unsupported_operation("nope").http_status_code(),
            400
        );
        assert_eq!(
            A2AError::version_not_supported("9.9").http_status_code(),
            400
        );
        assert_eq!(A2AError::parse_error("bad").http_status_code(), 400);
        assert_eq!(A2AError::invalid_request("bad").http_status_code(), 400);
        assert_eq!(
            A2AError::method_not_found("missing").http_status_code(),
            501
        );
    }

    #[test]
    fn test_to_jsonrpc_error() {
        let e = A2AError::task_not_found("t1");
        let rpc = e.to_jsonrpc_error();
        assert_eq!(rpc.code, error_code::TASK_NOT_FOUND);
        assert!(rpc.message.contains("t1"));
        let data = rpc.data.expect("data should always be present");
        let arr = data.as_array().expect("data should be an array");
        assert_eq!(arr.len(), 1);
        assert_eq!(arr[0]["@type"], errordetails::ERROR_INFO_TYPE);
        assert_eq!(arr[0]["reason"], "TASK_NOT_FOUND");
        assert_eq!(arr[0]["domain"], errordetails::PROTOCOL_DOMAIN);
        assert!(arr[0]["metadata"]["timestamp"].is_string());
    }

    #[test]
    fn test_with_details() {
        use std::collections::HashMap;
        let struct_detail = TypedDetail::from_struct(HashMap::from([(
            "key".to_string(),
            Value::String("val".to_string()),
        )]));
        let e = A2AError::internal("err").with_details(vec![struct_detail]);

        let rpc = e.to_jsonrpc_error();
        let data = rpc.data.expect("data should always be present");
        let arr = data.as_array().unwrap();
        assert_eq!(arr.len(), 2);
        assert_eq!(arr[0]["key"], "val");
        assert_eq!(arr[1]["@type"], errordetails::ERROR_INFO_TYPE);
        assert_eq!(arr[1]["reason"], "INTERNAL_ERROR");
    }

    #[test]
    fn test_to_jsonrpc_error_merges_existing_error_info_metadata() {
        let existing_info = TypedDetail::error_info(
            "TASK_NOT_FOUND",
            errordetails::PROTOCOL_DOMAIN,
            Some(HashMap::from([("taskId".to_string(), "t1".to_string())])),
        );
        let e = A2AError::task_not_found("t1").with_details(vec![existing_info]);
        let rpc = e.to_jsonrpc_error();
        let data = rpc.data.unwrap();
        let arr = data.as_array().unwrap();
        // Existing ErrorInfo is filtered; merged into auto-generated one
        assert_eq!(arr.len(), 1);
        assert_eq!(arr[0]["@type"], errordetails::ERROR_INFO_TYPE);
        assert_eq!(arr[0]["metadata"]["taskId"], "t1");
        assert!(arr[0]["metadata"]["timestamp"].is_string());
    }

    #[test]
    fn test_reason_to_error_code() {
        assert_eq!(
            reason_to_error_code("TASK_NOT_FOUND"),
            Some(error_code::TASK_NOT_FOUND)
        );
        assert_eq!(
            reason_to_error_code("TASK_NOT_CANCELABLE"),
            Some(error_code::TASK_NOT_CANCELABLE)
        );
        assert_eq!(
            reason_to_error_code("PUSH_NOTIFICATION_NOT_SUPPORTED"),
            Some(error_code::PUSH_NOTIFICATION_NOT_SUPPORTED)
        );
        assert_eq!(
            reason_to_error_code("UNSUPPORTED_OPERATION"),
            Some(error_code::UNSUPPORTED_OPERATION)
        );
        assert_eq!(
            reason_to_error_code("CONTENT_TYPE_NOT_SUPPORTED"),
            Some(error_code::CONTENT_TYPE_NOT_SUPPORTED)
        );
        assert_eq!(
            reason_to_error_code("UNSUPPORTED_CONTENT_TYPE"),
            Some(error_code::CONTENT_TYPE_NOT_SUPPORTED)
        );
        assert_eq!(
            reason_to_error_code("INVALID_AGENT_RESPONSE"),
            Some(error_code::INVALID_AGENT_RESPONSE)
        );
        assert_eq!(
            reason_to_error_code("EXTENDED_AGENT_CARD_NOT_CONFIGURED"),
            Some(error_code::EXTENDED_CARD_NOT_CONFIGURED)
        );
        assert_eq!(
            reason_to_error_code("EXTENDED_CARD_NOT_CONFIGURED"),
            Some(error_code::EXTENDED_CARD_NOT_CONFIGURED)
        );
        assert_eq!(
            reason_to_error_code("EXTENSION_SUPPORT_REQUIRED"),
            Some(error_code::EXTENSION_SUPPORT_REQUIRED)
        );
        assert_eq!(
            reason_to_error_code("VERSION_NOT_SUPPORTED"),
            Some(error_code::VERSION_NOT_SUPPORTED)
        );
        assert_eq!(
            reason_to_error_code("PARSE_ERROR"),
            Some(error_code::PARSE_ERROR)
        );
        assert_eq!(
            reason_to_error_code("INVALID_REQUEST"),
            Some(error_code::INVALID_REQUEST)
        );
        assert_eq!(
            reason_to_error_code("METHOD_NOT_FOUND"),
            Some(error_code::METHOD_NOT_FOUND)
        );
        assert_eq!(
            reason_to_error_code("INVALID_PARAMS"),
            Some(error_code::INVALID_PARAMS)
        );
        assert_eq!(
            reason_to_error_code("INTERNAL_ERROR"),
            Some(error_code::INTERNAL_ERROR)
        );
        assert_eq!(reason_to_error_code("UNKNOWN_REASON"), None);
    }

    #[test]
    fn test_error_display() {
        let e = A2AError::internal("test message");
        assert_eq!(format!("{e}"), "test message");
    }

    #[test]
    fn test_jsonrpc_error_from() {
        let e = A2AError::internal("test");
        let rpc: crate::JsonRpcError = e.into();
        assert_eq!(rpc.code, error_code::INTERNAL_ERROR);
    }
}