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>
impl<C> Message<C>
Sourcepub fn system(content: Vec<ContentPart>) -> Result<Self, KitError>
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);
Sourcepub fn developer(content: Vec<ContentPart>) -> Result<Self, KitError>
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);
Sourcepub fn user(content: Vec<ContentPart>) -> Result<Self, KitError>
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);
Sourcepub fn assistant(
content: Vec<ContentPart>,
function_calls: Vec<FunctionCall>,
) -> Result<Self, KitError>
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);
Sourcepub fn tool(results: Vec<FunctionResult>) -> Result<Self, KitError>
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"));
Sourcepub fn assistant_text(text: impl Into<String>) -> Result<Self, KitError>
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);
Sourcepub fn with_continuation(self, continuation: C) -> Result<Self, KitError>
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());
Sourcepub const fn role(&self) -> Role
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);
Sourcepub fn content(&self) -> &[ContentPart]
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"));
Sourcepub fn function_calls(&self) -> &[FunctionCall]
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");
Sourcepub fn function_results(&self) -> &[FunctionResult]
pub fn function_results(&self) -> &[FunctionResult]
Returns caller-supplied function results.
See crate::generation::Message::tool for a complete example.
Sourcepub const fn continuation(&self) -> Option<&C>
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.