Skip to main content

Message

Struct Message 

Source
pub struct Message<C = GenerationContinuation> { /* private fields */ }
Expand description

Portable message with private function and continuation state.

A caller Model can return its own continuation type. Preserve that exact assistant value before appending the matching function-result message:

use providers_kit::generation::{
    ContentPart, FunctionCall, FunctionResult, GenerateRequest, Message,
};
let user = Message::<u32>::user_text("Look up record a")?;
// Representative output constructed by a caller Model with Continuation = u32.
let call = FunctionCall::new(Some("call-1".into()), "lookup", serde_json::json!({"key":"a"}))?;
let assistant = Message::assistant(Vec::new(), vec![call])?.with_continuation(7)?;
let result = FunctionResult::new(Some("call-1".into()), "lookup",
    vec![ContentPart::Json { value: serde_json::json!({"value":2}) }], false)?;
let next = GenerateRequest::new(vec![user, assistant, Message::tool(vec![result])?])?;
assert_eq!(next.messages()[1].continuation(), Some(&7));
assert_eq!(next.messages()[2].function_results()[0].call_id(), Some("call-1"));
assert!(serde_json::to_value(&next).is_err());

Role, function calls/results, and continuation are validated together. Supplied continuations stay opaque; caller Models use their own exact associated type. Assistant messages returned by a Model must be replayed unchanged.

See crate::generation::GenerationContinuation for a complete example.

Implementations§

Source§

impl<C> Message<C>

Source

pub fn system(content: Vec<ContentPart>) -> Result<Self, KitError>

Creates a text-only system instruction.

§Errors

Returns an input error for empty or invalid content, or content incompatible with the role. Instruction messages permit only text; user messages reject thinking and raw JSON.

§Examples
use providers_kit::generation::{Message, ContentPart, Role};
let message = Message::<()>::system(vec![ContentPart::Text { text: "Hello".into() }])?;
assert_eq!(message.role(), Role::System);
Source

pub fn developer(content: Vec<ContentPart>) -> Result<Self, KitError>

Creates a text-only developer instruction.

§Errors

Returns an input error for empty or invalid content, or content incompatible with the role. Instruction messages permit only text; user messages reject thinking and raw JSON.

§Examples
use providers_kit::generation::{Message, ContentPart, Role};
let message = Message::<()>::developer(vec![ContentPart::Text { text: "Hello".into() }])?;
assert_eq!(message.role(), Role::Developer);
Source

pub fn user(content: Vec<ContentPart>) -> Result<Self, KitError>

Creates a user message.

§Errors

Returns an input error for empty or invalid content, or content incompatible with the role. Instruction messages permit only text; user messages reject thinking and raw JSON.

§Examples
use providers_kit::generation::{Message, ContentPart, Role};
let message = Message::<()>::user(vec![ContentPart::Text { text: "Hello".into() }])?;
assert_eq!(message.role(), Role::User);
Source

pub fn assistant( content: Vec<ContentPart>, function_calls: Vec<FunctionCall>, ) -> Result<Self, KitError>

Creates an assistant message with visible content and/or function calls.

§Errors

Returns an input error if both content and calls are empty, content is malformed, or the role cannot represent the supplied values.

§Examples
use providers_kit::generation::FunctionCall;
let call = FunctionCall::new(Some("call-1".into()), "lookup", serde_json::json!({"key":"a"}))?;
use providers_kit::generation::Message;
let message = Message::<()>::assistant(Vec::new(), vec![call])?;
assert_eq!(message.function_calls().len(), 1);
Source

pub fn tool(results: Vec<FunctionResult>) -> Result<Self, KitError>

Creates a tool message containing validated function results.

§Errors

Returns an input error when no function results are supplied or the message shape is invalid.

§Examples
use providers_kit::generation::{FunctionResult, ContentPart};
let result = FunctionResult::new(
    Some("call-1".into()),
    "lookup",
    vec![ContentPart::Json {
        value: serde_json::json!({"value":2}),
    }],
    false,
)?;
use providers_kit::generation::Message;
let message = Message::<()>::tool(vec![result])?;
assert_eq!(message.function_results()[0].call_id(), Some("call-1"));
Source

pub fn user_text(text: impl Into<String>) -> Result<Self, KitError>

Creates one user text message.

§Errors

Returns an input error when the text is empty or whitespace-only.

§Examples
use providers_kit::generation::{Message, Role};
let message = Message::<()>::user_text("Hello")?;
assert_eq!(message.role(), Role::User);
assert_eq!(message.content().len(), 1);
Source

pub fn assistant_text(text: impl Into<String>) -> Result<Self, KitError>

Creates one assistant text message.

§Errors

Returns an input error when the text is empty or whitespace-only.

§Examples
use providers_kit::generation::{Message, Role};
let message = Message::<()>::assistant_text("Hello")?;
assert_eq!(message.role(), Role::Assistant);
assert_eq!(message.content().len(), 1);
Source

pub fn with_continuation(self, continuation: C) -> Result<Self, KitError>

Attaches continuation state to an assistant message.

Continuation is valid only on assistant messages and is never silently serialized. Use this constructor for a caller Model’s continuation; preserve supplied returned messages unchanged rather than reconstructing their private replay state.

§Errors

Returns an input error when the message role is not Assistant.

§Examples
use providers_kit::generation::Message;
let message = Message::assistant_text("continue")?.with_continuation(7_u32)?;
assert_eq!(message.continuation(), Some(&7));
assert!(serde_json::to_value(&message).is_err());
assert!(Message::user_text("hello")?.with_continuation(7_u32).is_err());
Source

pub const fn role(&self) -> Role

Returns the speaker role.

§Examples
use providers_kit::generation::{Message, ContentPart, Role};
let message = Message::<()>::user_text("Hello")?;
assert_eq!(message.role(), Role::User);
Source

pub fn content(&self) -> &[ContentPart]

Returns ordered visible content.

§Examples
use providers_kit::generation::{Message, ContentPart, Role};
let message = Message::<()>::user_text("Hello")?;
assert!(matches!(&message.content()[0], ContentPart::Text { text } if text == "Hello"));
Source

pub fn function_calls(&self) -> &[FunctionCall]

Returns model-requested function calls.

§Examples
use providers_kit::generation::FunctionCall;
let call = FunctionCall::new(Some("call-1".into()), "lookup", serde_json::json!({"key":"a"}))?;
let message = providers_kit::generation::Message::<()>::assistant(Vec::new(), vec![call])?;
assert_eq!(message.function_calls()[0].name(), "lookup");
Source

pub fn function_results(&self) -> &[FunctionResult]

Returns caller-supplied function results.

See crate::generation::Message::tool for a complete example.

Source

pub const fn continuation(&self) -> Option<&C>

Returns opaque continuation state when this is a returned assistant message.

See crate::generation::Message::with_continuation for a complete example.

Trait Implementations§

Source§

impl<C: Clone> Clone for Message<C>

Source§

fn clone(&self) -> Self

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<C> Debug for Message<C>

Source§

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

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

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

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

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

impl<C: PartialEq> PartialEq for Message<C>

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl<C> Serialize for Message<C>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

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

impl<C: PartialEq> StructuralPartialEq for Message<C>

Auto Trait Implementations§

§

impl<C> Freeze for Message<C>
where Option<C>: Freeze,

§

impl<C> RefUnwindSafe for Message<C>

§

impl<C> Send for Message<C>
where Option<C>: Send,

§

impl<C> Sync for Message<C>
where Option<C>: Sync,

§

impl<C> Unpin for Message<C>
where Option<C>: Unpin,

§

impl<C> UnsafeUnpin for Message<C>
where Option<C>: UnsafeUnpin,

§

impl<C> UnwindSafe for Message<C>
where Option<C>: UnwindSafe,

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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