Skip to main content

a2a_protocol_types/
wire_builders.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F. <[email protected]> (https://github.com/tomtom215)
3
4//! Constructors and builders for the protocol objects that are
5//! `#[non_exhaustive]` (audit G-10).
6//!
7//! A struct marked `#[non_exhaustive]` cannot be written as a struct literal
8//! outside its crate, so a field the A2A specification adds later is not a
9//! breaking change to every caller that built one. These are the other way in:
10//! `new` takes the fields the protocol requires, and a `with_*` method sets
11//! each optional one. Each leaves every field it is not given at the value a
12//! literal would have written (`None`, or empty), so converting a literal is
13//! mechanical and changes no wire byte.
14//!
15//! `Message`, `AgentCard`, `AgentSkill` and `AgentInterface` had theirs
16//! already, in their own modules; the rest are here.
17
18use crate::agent_card::{AgentCapabilities, AgentInterface, AgentProvider};
19use crate::artifact::{Artifact, ArtifactId};
20use crate::events::{TaskArtifactUpdateEvent, TaskStatusUpdateEvent};
21use crate::extensions::AgentExtension;
22use crate::message::Message;
23use crate::push::{AuthenticationInfo, TaskPushNotificationConfig};
24use crate::task::{ContextId, Task, TaskId, TaskStatus};
25
26impl TaskStatus {
27    /// Sets the message that goes with the state (for `failed` or
28    /// `input-required`, what the agent says about it).
29    #[must_use]
30    pub fn with_message(mut self, message: Message) -> Self {
31        self.message = Some(message);
32        self
33    }
34
35    /// Sets the timestamp to `timestamp` (ISO 8601, as the specification
36    /// writes it) where [`TaskStatus::with_timestamp`] would take the current
37    /// time.
38    #[must_use]
39    pub fn at(mut self, timestamp: impl Into<String>) -> Self {
40        self.timestamp = Some(timestamp.into());
41        self
42    }
43}
44
45impl Task {
46    /// Sets the message history.
47    #[must_use]
48    pub fn with_history(mut self, history: Vec<Message>) -> Self {
49        self.history = Some(history);
50        self
51    }
52
53    /// Sets the artifacts the task has produced.
54    #[must_use]
55    pub fn with_artifacts(mut self, artifacts: Vec<Artifact>) -> Self {
56        self.artifacts = Some(artifacts);
57        self
58    }
59
60    /// Sets the task's metadata.
61    #[must_use]
62    pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
63        self.metadata = Some(metadata);
64        self
65    }
66}
67
68impl Artifact {
69    /// Sets the artifact's human-readable name.
70    #[must_use]
71    pub fn with_name(mut self, name: impl Into<String>) -> Self {
72        self.name = Some(name.into());
73        self
74    }
75
76    /// Sets the artifact's description.
77    #[must_use]
78    pub fn with_description(mut self, description: impl Into<String>) -> Self {
79        self.description = Some(description.into());
80        self
81    }
82
83    /// Sets the artifact's metadata.
84    #[must_use]
85    pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
86        self.metadata = Some(metadata);
87        self
88    }
89
90    /// Sets the extension URIs that apply to the artifact.
91    #[must_use]
92    pub fn with_extensions<I, S>(mut self, extensions: I) -> Self
93    where
94        I: IntoIterator<Item = S>,
95        S: Into<String>,
96    {
97        self.extensions = Some(extensions.into_iter().map(Into::into).collect());
98        self
99    }
100
101    /// An artifact with the given id and parts, whatever the parts are: unlike
102    /// [`Artifact::new`] it does not assert that there is at least one, so a
103    /// test of [`Artifact::validate`] can build the artifact it expects to be
104    /// refused.
105    #[must_use]
106    pub fn unchecked(id: impl Into<ArtifactId>, parts: Vec<crate::message::Part>) -> Self {
107        let mut artifact = Self::new(id, vec![crate::message::Part::text(String::new())]);
108        artifact.parts = parts;
109        artifact
110    }
111}
112
113impl TaskStatusUpdateEvent {
114    /// An event reporting that task `task_id` of context `context_id` is now
115    /// in `status`, with no metadata.
116    #[must_use]
117    pub fn new(
118        task_id: impl Into<TaskId>,
119        context_id: impl Into<ContextId>,
120        status: TaskStatus,
121    ) -> Self {
122        Self {
123            task_id: task_id.into(),
124            context_id: context_id.into(),
125            status,
126            metadata: None,
127        }
128    }
129
130    /// Sets the event's metadata.
131    #[must_use]
132    pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
133        self.metadata = Some(metadata);
134        self
135    }
136}
137
138impl TaskArtifactUpdateEvent {
139    /// An event carrying `artifact` for task `task_id` of context
140    /// `context_id`, with `append`, `last_chunk` and metadata unset.
141    #[must_use]
142    pub fn new(
143        task_id: impl Into<TaskId>,
144        context_id: impl Into<ContextId>,
145        artifact: Artifact,
146    ) -> Self {
147        Self {
148            task_id: task_id.into(),
149            context_id: context_id.into(),
150            artifact,
151            append: None,
152            last_chunk: None,
153            metadata: None,
154        }
155    }
156
157    /// Says whether the parts are added to the artifact the client already
158    /// has (`true`) or replace it (`false`).
159    #[must_use]
160    pub const fn with_append(mut self, append: bool) -> Self {
161        self.append = Some(append);
162        self
163    }
164
165    /// Says whether this is the artifact's last chunk.
166    #[must_use]
167    pub const fn with_last_chunk(mut self, last_chunk: bool) -> Self {
168        self.last_chunk = Some(last_chunk);
169        self
170    }
171
172    /// Sets the event's metadata.
173    #[must_use]
174    pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
175        self.metadata = Some(metadata);
176        self
177    }
178}
179
180impl TaskPushNotificationConfig {
181    /// A config for `url` that names no task: what a request body is before
182    /// the task id is taken from the path or the method's parameters.
183    #[must_use]
184    pub fn for_url(url: impl Into<String>) -> Self {
185        Self {
186            tenant: None,
187            id: None,
188            task_id: None,
189            url: url.into(),
190            token: None,
191            authentication: None,
192        }
193    }
194
195    /// Sets the config's id (the server assigns one when it is absent).
196    #[must_use]
197    pub fn with_id(mut self, id: impl Into<String>) -> Self {
198        self.id = Some(id.into());
199        self
200    }
201
202    /// Sets the token the webhook receives to know a notification is for this
203    /// config (spec ยง13.2).
204    #[must_use]
205    pub fn with_token(mut self, token: impl Into<String>) -> Self {
206        self.token = Some(token.into());
207        self
208    }
209
210    /// Sets the credentials the webhook asks to be sent.
211    #[must_use]
212    pub fn with_authentication(mut self, authentication: AuthenticationInfo) -> Self {
213        self.authentication = Some(authentication);
214        self
215    }
216
217    /// Sets the tenant the config belongs to.
218    #[must_use]
219    pub fn with_tenant(mut self, tenant: impl Into<String>) -> Self {
220        self.tenant = Some(tenant.into());
221        self
222    }
223}
224
225impl AgentExtension {
226    /// Sets the description of what the extension does.
227    #[must_use]
228    pub fn with_description(mut self, description: impl Into<String>) -> Self {
229        self.description = Some(description.into());
230        self
231    }
232
233    /// Says whether a client must understand the extension to use the agent.
234    #[must_use]
235    pub const fn with_required(mut self, required: bool) -> Self {
236        self.required = Some(required);
237        self
238    }
239
240    /// Sets the extension's parameters.
241    #[must_use]
242    pub fn with_params(mut self, params: serde_json::Value) -> Self {
243        self.params = Some(params);
244        self
245    }
246}
247
248impl AgentProvider {
249    /// The organization that runs the agent, and its website.
250    #[must_use]
251    pub fn new(organization: impl Into<String>, url: impl Into<String>) -> Self {
252        Self {
253            url: url.into(),
254            organization: organization.into(),
255        }
256    }
257}
258
259impl AgentCapabilities {
260    /// Sets the extensions the agent supports.
261    #[must_use]
262    pub fn with_extensions(mut self, extensions: Vec<AgentExtension>) -> Self {
263        self.extensions = Some(extensions);
264        self
265    }
266}
267
268impl AgentInterface {
269    /// Sets the protocol version the interface speaks, where
270    /// [`AgentInterface::new`] writes the version this crate implements.
271    #[must_use]
272    pub fn with_protocol_version(mut self, version: impl Into<String>) -> Self {
273        self.protocol_version = version.into();
274        self
275    }
276}
277
278#[cfg(test)]
279#[path = "wire_builders_tests.rs"]
280mod tests;