Skip to main content

mur_common/
a2a.rs

1//! A2A v0.3 protocol envelope types.
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct Message {
6    pub role: String, // "user" | "agent" | "system"
7    pub parts: Vec<MessagePart>,
8}
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
11#[serde(tag = "kind", rename_all = "lowercase")]
12pub enum MessagePart {
13    Text {
14        text: String,
15    },
16    Data {
17        #[serde(rename = "mimeType")]
18        mime_type: String,
19        data: serde_json::Value,
20    },
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
24#[serde(rename_all = "lowercase")]
25pub enum TaskState {
26    Submitted,
27    Working,
28    Completed,
29    Failed,
30    Cancelled,
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct Task {
35    pub id: String,
36    pub state: TaskState,
37    pub messages: Vec<Message>,
38    #[serde(rename = "createdAt")]
39    pub created_at: String,
40    #[serde(rename = "completedAt", skip_serializing_if = "Option::is_none")]
41    pub completed_at: Option<String>,
42    #[serde(default, skip_serializing_if = "Option::is_none")]
43    pub error: Option<TaskError>,
44    #[serde(default, skip_serializing_if = "Option::is_none")]
45    pub usage: Option<serde_json::Value>,
46    /// Zero or more file artifacts produced by this task. Present when the
47    /// task completed with an `output_artifact_path` — the consumer reads
48    /// these files byte-by-byte instead of re-typing inline content through
49    /// another LLM (#715 Part B).
50    #[serde(default, skip_serializing_if = "Option::is_none")]
51    pub artifacts: Option<Vec<ArtifactInfo>>,
52}
53
54/// Metadata for an artifact produced by a task — a file the agent wrote to
55/// a known path so callers can read it byte-by-byte instead of re-typing the
56/// content through another LLM (issue #715 Part B).
57#[derive(Debug, Clone, Serialize, Deserialize)]
58pub struct ArtifactInfo {
59    /// Absolute path the artifact was written to.
60    pub path: String,
61    /// MIME type (e.g. "text/markdown", "application/json").
62    #[serde(rename = "mimeType")]
63    pub mime_type: String,
64    /// SHA-256 hex digest of the file content, for integrity checking by the
65    /// consumer before they use it. Always present when the runtime verified
66    /// the file post-completion.
67    #[serde(default, skip_serializing_if = "Option::is_none")]
68    pub sha256: Option<String>,
69    /// File size in bytes.
70    pub size_bytes: u64,
71}
72
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct TaskError {
75    pub code: String,
76    pub message: String,
77    pub recoverable: bool,
78    /// Boxed so `TaskError` stays small as the `Err` of hot `Result`s:
79    /// with serde_json's `preserve_order` (pulled in by lancedb ≥ 0.41)
80    /// an inline `Value` pushes it past clippy's `result_large_err` limit.
81    /// `Box` is transparent to serde, so the wire format is unchanged.
82    #[serde(default, skip_serializing_if = "Option::is_none")]
83    pub details: Option<Box<serde_json::Value>>,
84}
85
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct JsonRpcRequest {
88    pub jsonrpc: String, // always "2.0"
89    #[serde(default, skip_serializing_if = "Option::is_none")]
90    pub id: Option<serde_json::Value>, // None = notification
91    pub method: String,
92    #[serde(default, skip_serializing_if = "Option::is_none")]
93    pub params: Option<serde_json::Value>,
94}
95
96#[derive(Debug, Clone, Serialize, Deserialize)]
97pub struct JsonRpcResponse {
98    pub jsonrpc: String, // "2.0"
99    pub id: serde_json::Value,
100    #[serde(skip_serializing_if = "Option::is_none")]
101    pub result: Option<serde_json::Value>,
102    #[serde(skip_serializing_if = "Option::is_none")]
103    pub error: Option<JsonRpcError>,
104}
105
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct JsonRpcError {
108    pub code: i32,
109    pub message: String,
110    #[serde(default, skip_serializing_if = "Option::is_none")]
111    pub data: Option<serde_json::Value>,
112}
113
114#[cfg(test)]
115mod tests {
116    use super::TaskError;
117
118    /// Boxing `details` must not change the wire format: peers on older
119    /// builds send and expect a bare JSON value here.
120    #[test]
121    fn task_error_details_wire_format_unchanged() {
122        let wire = r#"{"code":"x","message":"m","recoverable":false,"details":{"k":[1,2]}}"#;
123        let err: TaskError = serde_json::from_str(wire).unwrap();
124        assert_eq!(
125            err.details.as_deref(),
126            Some(&serde_json::json!({"k": [1, 2]}))
127        );
128        assert_eq!(serde_json::to_string(&err).unwrap(), wire);
129
130        let bare = r#"{"code":"x","message":"m","recoverable":true}"#;
131        let err: TaskError = serde_json::from_str(bare).unwrap();
132        assert!(err.details.is_none());
133        assert_eq!(serde_json::to_string(&err).unwrap(), bare);
134    }
135}