Skip to main content

Message

Enum Message 

Source
pub enum Message {
    System {
        content: Vec<ContentBlock>,
    },
    User {
        content: Vec<ContentBlock>,
    },
    Assistant {
        content: Vec<ContentBlock>,
    },
    ToolResult {
        tool_call_id: String,
        is_error: bool,
        content: Vec<ContentBlock>,
    },
}
Expand description

对话中的单条消息。

Variants§

§

System

Fields

§

User

Fields

§

Assistant

Fields

§

ToolResult

Fields

§tool_call_id: String
§is_error: bool

工具执行是否失败(供 Provider API 映射,如 Anthropic is_error: true

Implementations§

Source§

impl Message

Source

pub fn system_text(s: &str) -> Self

便捷构造:纯文本 System 消息。

use lellm_core::Message;

// 之前:
// Message::System { content: lellm_core::text_block("you are helpful".to_string()) }

// 现在:
let msg = Message::system_text("you are helpful");
Source

pub fn user_text(s: &str) -> Self

便捷构造:纯文本 User 消息。

use lellm_core::Message;

let msg = Message::user_text("hello");
Source

pub fn assistant_text(s: &str) -> Self

便捷构造:纯文本 Assistant 消息。

Source

pub fn user_text_image(text: &str, media_type: String, data: String) -> Self

便捷构造:带图片的 User 消息(文本 + 图片)。

use lellm_core::Message;

let msg = Message::user_text_image(
    "what's in this image?",
    "image/png".to_string(),
    "base64_encoded_data".to_string(),
);
Source

pub fn user_image(media_type: String, data: String) -> Self

便捷构造:仅图片的 User 消息。

Source

pub fn system(content: Vec<ContentBlock>) -> Self

便捷构造:System 消息(自定义 ContentBlock)。

Source

pub fn user(content: Vec<ContentBlock>) -> Self

便捷构造:User 消息(自定义 ContentBlock)。

Source

pub fn assistant(content: Vec<ContentBlock>) -> Self

便捷构造:Assistant 消息(自定义 ContentBlock)。

Source

pub fn tool_result_ok(call_id: impl Into<String>, content: String) -> Self

便捷构造:ToolResult 消息(成功)。

Source

pub fn tool_error(call_id: impl Into<String>, error: String) -> Self

便捷构造:ToolResult 消息(失败)。

Source

pub fn content(&self) -> &Vec<ContentBlock>

返回内容块的引用(用于 provider 适配器序列化)

Source

pub fn tool_call_id(&self) -> String

返回 ToolResult 的 tool_call_id(仅 ToolResult 变体有效,其他返回 None)

Source

pub fn is_tool_error(&self) -> bool

返回 ToolResult 的 is_error 标记(仅 ToolResult 变体有效)

Source

pub fn tool_result(call: &ToolCall, result: &ToolResult) -> Self

从工具调用结果构建 Message::ToolResult

成功 → 序列化 serde_json::Value 为文本,is_error: false 失败 → "tool error: {e}" 文本 content,is_error: true

Source

pub fn validate(&self) -> Result<(), ParseError>

语义校验 — 检查 Message 变体与 ContentBlock 的合法性。

v0.1 核心规则:

  1. ToolResult 禁止包含 ToolCallThinking
  2. ToolResult.tool_call_id 非空
  3. Assistant 中的 ToolCall.id 非空
  4. User 禁止包含 Thinking
Source

pub fn extract_tool_calls(&self) -> Vec<ToolCall>

提取所有 ToolCall(仅 Assistant 消息包含)

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

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

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

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 Serialize for Message

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

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<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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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 = Infallible

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

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

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.