Skip to main content

adk_server/a2a/
types.rs

1use serde::{Deserialize, Serialize};
2use serde_json::{Map, Value};
3
4#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
5#[serde(rename_all = "lowercase")]
6pub enum Role {
7    User,
8    Agent,
9}
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct FileContent {
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub name: Option<String>,
15    #[serde(skip_serializing_if = "Option::is_none", rename = "mimeType")]
16    pub mime_type: Option<String>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub bytes: Option<String>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub uri: Option<String>,
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
24#[serde(untagged)]
25pub enum Part {
26    Text {
27        text: String,
28        #[serde(skip_serializing_if = "Option::is_none")]
29        metadata: Option<Map<String, Value>>,
30    },
31    File {
32        file: FileContent,
33        #[serde(skip_serializing_if = "Option::is_none")]
34        metadata: Option<Map<String, Value>>,
35    },
36    Data {
37        data: Map<String, Value>,
38        #[serde(skip_serializing_if = "Option::is_none")]
39        metadata: Option<Map<String, Value>>,
40    },
41}
42
43impl Part {
44    pub fn text(text: String) -> Self {
45        Part::Text { text, metadata: None }
46    }
47
48    pub fn file(file: FileContent) -> Self {
49        Part::File { file, metadata: None }
50    }
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct Message {
55    pub role: Role,
56    pub parts: Vec<Part>,
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub metadata: Option<Map<String, Value>>,
59    #[serde(rename = "messageId")]
60    pub message_id: String,
61    #[serde(skip_serializing_if = "Option::is_none", rename = "taskId")]
62    pub task_id: Option<String>,
63    #[serde(skip_serializing_if = "Option::is_none", rename = "contextId")]
64    pub context_id: Option<String>,
65}
66
67impl Message {
68    pub fn builder() -> MessageBuilder {
69        MessageBuilder::default()
70    }
71}
72
73#[derive(Default)]
74pub struct MessageBuilder {
75    role: Option<Role>,
76    parts: Vec<Part>,
77    metadata: Option<Map<String, Value>>,
78    message_id: Option<String>,
79    task_id: Option<String>,
80    context_id: Option<String>,
81}
82
83impl MessageBuilder {
84    pub fn role(mut self, role: Role) -> Self {
85        self.role = Some(role);
86        self
87    }
88
89    pub fn parts(mut self, parts: Vec<Part>) -> Self {
90        self.parts = parts;
91        self
92    }
93
94    pub fn metadata(mut self, metadata: Option<Map<String, Value>>) -> Self {
95        self.metadata = metadata;
96        self
97    }
98
99    pub fn message_id(mut self, id: String) -> Self {
100        self.message_id = Some(id);
101        self
102    }
103
104    pub fn build(self) -> Message {
105        Message {
106            role: self.role.unwrap_or(Role::User),
107            parts: self.parts,
108            metadata: self.metadata,
109            message_id: self.message_id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string()),
110            task_id: self.task_id,
111            context_id: self.context_id,
112        }
113    }
114}
115
116#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct Artifact {
118    #[serde(rename = "artifactId")]
119    pub artifact_id: String,
120    #[serde(skip_serializing_if = "Option::is_none")]
121    pub name: Option<String>,
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub description: Option<String>,
124    pub parts: Vec<Part>,
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub metadata: Option<Map<String, Value>>,
127    #[serde(skip_serializing_if = "Option::is_none")]
128    pub extensions: Option<Vec<String>>,
129}
130
131#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
132#[serde(rename_all = "kebab-case")]
133pub enum TaskState {
134    Submitted,
135    Working,
136    /// The run stopped on a tool call held for confirmation or a long-running call.
137    InputRequired,
138    /// The run stopped on a credential request (an `adk_request_credential` call).
139    AuthRequired,
140    Completed,
141    Failed,
142    Canceled,
143}
144
145#[derive(Debug, Clone, Serialize, Deserialize)]
146pub struct TaskStatus {
147    pub state: TaskState,
148    #[serde(skip_serializing_if = "Option::is_none")]
149    pub message: Option<String>,
150}
151
152#[derive(Debug, Clone, Serialize, Deserialize)]
153pub struct TaskStatusUpdateEvent {
154    #[serde(rename = "taskId")]
155    pub task_id: String,
156    #[serde(skip_serializing_if = "Option::is_none", rename = "contextId")]
157    pub context_id: Option<String>,
158    pub status: TaskStatus,
159    #[serde(rename = "finalUpdate")]
160    pub final_update: bool,
161}
162
163#[derive(Debug, Clone, Serialize, Deserialize)]
164pub struct TaskArtifactUpdateEvent {
165    #[serde(rename = "taskId")]
166    pub task_id: String,
167    #[serde(skip_serializing_if = "Option::is_none", rename = "contextId")]
168    pub context_id: Option<String>,
169    pub artifact: Artifact,
170    pub append: bool,
171    #[serde(rename = "lastChunk")]
172    pub last_chunk: bool,
173}
174
175#[derive(Debug, Clone, Serialize, Deserialize)]
176#[serde(untagged)]
177pub enum UpdateEvent {
178    TaskStatusUpdate(TaskStatusUpdateEvent),
179    TaskArtifactUpdate(TaskArtifactUpdateEvent),
180}
181
182#[derive(Debug, Clone, Serialize, Deserialize)]
183pub struct AgentSkill {
184    pub id: String,
185    pub name: String,
186    pub description: String,
187    pub tags: Vec<String>,
188    #[serde(skip_serializing_if = "Option::is_none")]
189    pub examples: Option<Vec<String>>,
190}
191
192impl AgentSkill {
193    pub fn new(id: String, name: String, description: String, tags: Vec<String>) -> Self {
194        Self { id, name, description, tags, examples: None }
195    }
196}
197
198#[derive(Debug, Clone, Serialize, Deserialize, Default)]
199pub struct AgentCapabilities {
200    #[serde(default)]
201    pub streaming: bool,
202    #[serde(default, rename = "pushNotifications")]
203    pub push_notifications: bool,
204    #[serde(default, rename = "stateTransitionHistory")]
205    pub state_transition_history: bool,
206    #[serde(skip_serializing_if = "Option::is_none")]
207    pub extensions: Option<Vec<String>>,
208}
209
210#[derive(Debug, Clone, Serialize, Deserialize)]
211pub struct AgentCard {
212    pub name: String,
213    pub description: String,
214    pub url: String,
215    pub version: String,
216    #[serde(default, rename = "protocolVersion")]
217    pub protocol_version: String,
218    #[serde(default)]
219    pub capabilities: AgentCapabilities,
220    #[serde(default)]
221    pub skills: Vec<AgentSkill>,
222}
223
224impl AgentCard {
225    pub fn builder() -> AgentCardBuilder {
226        AgentCardBuilder::default()
227    }
228}
229
230#[derive(Default)]
231pub struct AgentCardBuilder {
232    name: Option<String>,
233    description: Option<String>,
234    url: Option<String>,
235    version: Option<String>,
236    capabilities: Option<AgentCapabilities>,
237    skills: Vec<AgentSkill>,
238}
239
240impl AgentCardBuilder {
241    pub fn name(mut self, name: String) -> Self {
242        self.name = Some(name);
243        self
244    }
245
246    pub fn description(mut self, description: String) -> Self {
247        self.description = Some(description);
248        self
249    }
250
251    pub fn url(mut self, url: String) -> Self {
252        self.url = Some(url);
253        self
254    }
255
256    pub fn version(mut self, version: String) -> Self {
257        self.version = Some(version);
258        self
259    }
260
261    pub fn capabilities(mut self, capabilities: AgentCapabilities) -> Self {
262        self.capabilities = Some(capabilities);
263        self
264    }
265
266    pub fn skills(mut self, skills: Vec<AgentSkill>) -> Self {
267        self.skills = skills;
268        self
269    }
270
271    /// Build the `AgentCard`, returning an error if required fields are missing.
272    ///
273    /// # Errors
274    ///
275    /// Returns a descriptive error string when `name`, `description`, or `url`
276    /// have not been set (or were set to empty strings).
277    pub fn build(self) -> std::result::Result<AgentCard, String> {
278        let name =
279            self.name.filter(|s| !s.is_empty()).ok_or("AgentCard requires a non-empty 'name'")?;
280        let description = self
281            .description
282            .filter(|s| !s.is_empty())
283            .ok_or("AgentCard requires a non-empty 'description'")?;
284        let url =
285            self.url.filter(|s| !s.is_empty()).ok_or("AgentCard requires a non-empty 'url'")?;
286
287        Ok(AgentCard {
288            name,
289            description,
290            url,
291            version: self.version.unwrap_or_else(|| "1.0.0".to_string()),
292            protocol_version: "0.3.0".to_string(),
293            capabilities: self.capabilities.unwrap_or(AgentCapabilities {
294                streaming: false,
295                push_notifications: false,
296                state_transition_history: false,
297                extensions: None,
298            }),
299            skills: self.skills,
300        })
301    }
302}