#[non_exhaustive]pub struct Message {
pub id: MessageId,
pub context_id: Option<ContextId>,
pub task_id: Option<TaskId>,
pub role: MessageRole,
pub parts: Vec<Part>,
pub metadata: Option<Value>,
pub extensions: Option<Vec<String>>,
pub reference_task_ids: Option<Vec<TaskId>>,
}Expand description
Message is one unit of communication between client and server.
It can be
associated with a context and/or a task. For server messages, context_id must
be provided, and task_id only if a task was created. For client messages, both
fields are optional, with the caveat that if both are provided, they have to
match (the context_id has to be the one that is set on the task). If only
task_id is provided, the server will infer context_id from it.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.id: MessageIdThe unique identifier (e.g. UUID) of the message. This is created by the message creator.
context_id: Option<ContextId>Optional. The context id of the message. If set, the message will be associated with the given context.
task_id: Option<TaskId>Optional. The task id of the message. If set, the message will be associated with the given task.
role: MessageRoleIdentifies the sender of the message.
parts: Vec<Part>Parts is the container of the message content.
Spec requirement: Must contain at least one element. The A2A protocol does not define behavior for empty parts lists.
Parsing treats absence as empty (ProtoJSON printers omit empty
repeated fields; the gRPC binding already decodes it that way) — the
server rejects empty parts with a structured invalid-params error
instead of a JSON type error, uniformly across bindings.
metadata: Option<Value>Optional. Any metadata to provide along with the message.
extensions: Option<Vec<String>>The URIs of extensions that are present or contributed to this Message.
reference_task_ids: Option<Vec<TaskId>>A list of task IDs that this message references for additional context.
Implementations§
Source§impl Message
impl Message
Sourcepub fn new(
id: impl Into<MessageId>,
role: MessageRole,
parts: Vec<Part>,
) -> Message
pub fn new( id: impl Into<MessageId>, role: MessageRole, parts: Vec<Part>, ) -> Message
Creates a message with the given id, role and parts.
The five optional fields start as None; set the ones you need with
the with_* methods below. Before these existed the only way to make
a Message was a struct literal naming all eight fields, and this
workspace contained 105 of them — a message carrying one line of text
cost ten lines to write, five of which said None.
The id is a parameter rather than something this constructor invents,
for the same reason Artifact::new
takes one: this crate depends on serde and serde_json and nothing
else, so it has no random source to mint one from, and a
clock-derived id is not unique under concurrency. Servers in this
workspace use uuid::Uuid::new_v4(); a client that already has a
correlation id of its own should use that.
use a2a_protocol_types::message::{Message, MessageRole, Part};
let msg = Message::new("m1", MessageRole::User, vec![Part::text("hello")]);
assert_eq!(msg.text(), Some("hello"));
assert_eq!(msg.role, MessageRole::User);Sourcepub fn user(id: impl Into<MessageId>, parts: Vec<Part>) -> Message
pub fn user(id: impl Into<MessageId>, parts: Vec<Part>) -> Message
Creates a MessageRole::User message with the given parts.
use a2a_protocol_types::message::{Message, MessageRole, Part};
let msg = Message::user("m1", vec![Part::url("https://example.com/f.pdf")]);
assert_eq!(msg.role, MessageRole::User);Sourcepub fn agent(id: impl Into<MessageId>, parts: Vec<Part>) -> Message
pub fn agent(id: impl Into<MessageId>, parts: Vec<Part>) -> Message
Creates a MessageRole::Agent message with the given parts.
use a2a_protocol_types::message::{Message, MessageRole, Part};
let msg = Message::agent("m1", vec![Part::text("done")]);
assert_eq!(msg.role, MessageRole::Agent);Sourcepub fn user_text(id: impl Into<MessageId>, text: impl Into<String>) -> Message
pub fn user_text(id: impl Into<MessageId>, text: impl Into<String>) -> Message
Creates a MessageRole::User message carrying one text part.
The shape almost every caller wants: send this text to an agent.
use a2a_protocol_types::message::Message;
let msg = Message::user_text("m1", "what is the status of order 42?");
assert_eq!(msg.text(), Some("what is the status of order 42?"));
assert_eq!(msg.parts.len(), 1);Sourcepub fn agent_text(id: impl Into<MessageId>, text: impl Into<String>) -> Message
pub fn agent_text(id: impl Into<MessageId>, text: impl Into<String>) -> Message
Creates a MessageRole::Agent message carrying one text part.
use a2a_protocol_types::message::Message;
let msg = Message::agent_text("m2", "order 42 shipped on Tuesday");
assert_eq!(msg.text(), Some("order 42 shipped on Tuesday"));Sourcepub fn with_task_id(self, task_id: impl Into<TaskId>) -> Message
pub fn with_task_id(self, task_id: impl Into<TaskId>) -> Message
Attaches this message to an existing task.
Sending a follow-up without this is what makes it a new task on the same context rather than a continuation of the old one.
use a2a_protocol_types::message::Message;
use a2a_protocol_types::task::TaskId;
let msg = Message::user_text("m2", "and the one after that?")
.with_task_id("task-1");
assert_eq!(msg.task_id, Some(TaskId::new("task-1")));Sourcepub fn with_context_id(self, context_id: impl Into<ContextId>) -> Message
pub fn with_context_id(self, context_id: impl Into<ContextId>) -> Message
Places this message in an existing conversation context.
use a2a_protocol_types::message::Message;
use a2a_protocol_types::task::ContextId;
let msg = Message::user_text("m2", "go on").with_context_id("ctx-1");
assert_eq!(msg.context_id, Some(ContextId::new("ctx-1")));Sourcepub fn with_reference_task_ids<I, T>(self, task_ids: I) -> Message
pub fn with_reference_task_ids<I, T>(self, task_ids: I) -> Message
Names earlier tasks this message refers to.
use a2a_protocol_types::message::Message;
let msg = Message::user_text("m3", "compare those two")
.with_reference_task_ids(["task-1", "task-2"]);
assert_eq!(msg.reference_task_ids.map(|ids| ids.len()), Some(2));Sourcepub fn with_extensions<I, S>(self, extensions: I) -> Message
pub fn with_extensions<I, S>(self, extensions: I) -> Message
Declares the extension URIs this message activates.
use a2a_protocol_types::message::Message;
let msg = Message::user_text("m1", "hi")
.with_extensions(["https://example.com/ext/v1"]);
assert_eq!(msg.extensions.as_deref(), Some(&["https://example.com/ext/v1".to_owned()][..]));Sourcepub fn with_metadata(self, metadata: Value) -> Message
pub fn with_metadata(self, metadata: Value) -> Message
Attaches opaque metadata to this message.
use a2a_protocol_types::message::Message;
let msg = Message::user_text("m1", "hi")
.with_metadata(serde_json::json!({ "skill": "triage" }));
assert_eq!(msg.metadata.as_ref().and_then(|m| m["skill"].as_str()), Some("triage"));Sourcepub fn text(&self) -> Option<&str>
pub fn text(&self) -> Option<&str>
Returns the text of the first text Part, or None if this message
carries no text part.
Reading the caller’s text is the most common operation in agent code.
This is the one-liner form of the parts.iter().find_map(..) match over
PartContent::Text:
use a2a_protocol_types::message::{Message, Part};
let msg = Message::user(
"m1",
vec![Part::url("https://example.com/f.pdf"), Part::text("hello")],
);
assert_eq!(msg.text(), Some("hello"));Non-text parts are skipped rather than ending the search, so a message that leads with a file attachment still yields its text.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Message
impl<'de> Deserialize<'de> for Message
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Message, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Message, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
impl Eq for Message
Source§impl Serialize for Message
impl Serialize for Message
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl StructuralPartialEq for Message
Auto Trait Implementations§
impl Forget for Message
impl Freeze for Message
impl Move for Message
impl RefUnwindSafe for Message
impl Send for Message
impl Sync for Message
impl Unpin for Message
impl UnsafeUnpin for Message
impl UnwindSafe for Message
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].