Skip to main content

Message

Struct Message 

Source
pub struct Message {
    pub id: MessageId,
    pub role: MessageRole,
    pub parts: Vec<Part>,
    pub task_id: Option<TaskId>,
    pub context_id: Option<ContextId>,
    pub reference_task_ids: Option<Vec<TaskId>>,
    pub extensions: Option<Vec<String>>,
    pub metadata: Option<Value>,
}
Expand description

A message exchanged between a client and an agent.

Fields§

§id: MessageId

Unique message identifier.

§role: MessageRole

Role of the message originator.

§parts: Vec<Part>

Message content parts.

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.

§task_id: Option<TaskId>

Task this message belongs to, if any.

§context_id: Option<ContextId>

Conversation context this message belongs to, if any.

§reference_task_ids: Option<Vec<TaskId>>

IDs of tasks referenced by this message.

§extensions: Option<Vec<String>>

URIs of extensions used in this message.

§metadata: Option<Value>

Arbitrary metadata.

Implementations§

Source§

impl Message

Source

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);
Source

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);
Source

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);
Source

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);
Source

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"));
Source

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")));
Source

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")));
Source

pub fn with_reference_task_ids<I, T>(self, task_ids: I) -> Message
where I: IntoIterator<Item = T>, T: Into<TaskId>,

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));
Source

pub fn with_extensions<I, S>(self, extensions: I) -> Message
where I: IntoIterator<Item = S>, S: Into<String>,

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()][..]));
Source

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"));
Source

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.

Source

pub fn texts(&self) -> impl Iterator<Item = &str>

Returns the text of every text Part, in order, skipping non-text parts.

Use this when a message may carry several text parts and Message::text would silently drop all but the first.

Trait Implementations§

Source§

impl Clone for Message

Source§

fn clone(&self) -> Message

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Message

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Message

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Message, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Message

Source§

impl PartialEq for Message

Source§

fn eq(&self, other: &Message) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Message

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Message

Source§

impl TryFrom<Message> for Message

Source§

type Error = ConvertError

The type returned in the event of a conversion error.
Source§

fn try_from( value: Message, ) -> Result<Message, <Message as TryFrom<Message>>::Error>

Performs the conversion.
Source§

impl TryFrom<Message> for Message

Source§

type Error = ConvertError

The type returned in the event of a conversion error.
Source§

fn try_from( value: Message, ) -> Result<Message, <Message as TryFrom<Message>>::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more