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
§
content: Vec<ContentBlock>User
Fields
§
content: Vec<ContentBlock>Assistant
Fields
§
content: Vec<ContentBlock>ToolResult
Implementations§
Source§impl Message
impl Message
Sourcepub fn system_text(s: &str) -> Self
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");Sourcepub fn user_text(s: &str) -> Self
pub fn user_text(s: &str) -> Self
便捷构造:纯文本 User 消息。
use lellm_core::Message;
let msg = Message::user_text("hello");Sourcepub fn assistant_text(s: &str) -> Self
pub fn assistant_text(s: &str) -> Self
便捷构造:纯文本 Assistant 消息。
Sourcepub fn user_text_image(text: &str, media_type: String, data: String) -> Self
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(),
);Sourcepub fn user_image(media_type: String, data: String) -> Self
pub fn user_image(media_type: String, data: String) -> Self
便捷构造:仅图片的 User 消息。
Sourcepub fn system(content: Vec<ContentBlock>) -> Self
pub fn system(content: Vec<ContentBlock>) -> Self
便捷构造:System 消息(自定义 ContentBlock)。
Sourcepub fn user(content: Vec<ContentBlock>) -> Self
pub fn user(content: Vec<ContentBlock>) -> Self
便捷构造:User 消息(自定义 ContentBlock)。
Sourcepub fn assistant(content: Vec<ContentBlock>) -> Self
pub fn assistant(content: Vec<ContentBlock>) -> Self
便捷构造:Assistant 消息(自定义 ContentBlock)。
Sourcepub fn tool_result_ok(call_id: impl Into<String>, content: String) -> Self
pub fn tool_result_ok(call_id: impl Into<String>, content: String) -> Self
便捷构造:ToolResult 消息(成功)。
Sourcepub fn tool_error(call_id: impl Into<String>, error: String) -> Self
pub fn tool_error(call_id: impl Into<String>, error: String) -> Self
便捷构造:ToolResult 消息(失败)。
Sourcepub fn content(&self) -> &Vec<ContentBlock>
pub fn content(&self) -> &Vec<ContentBlock>
返回内容块的引用(用于 provider 适配器序列化)
Sourcepub fn tool_call_id(&self) -> String
pub fn tool_call_id(&self) -> String
返回 ToolResult 的 tool_call_id(仅 ToolResult 变体有效,其他返回 None)
Sourcepub fn is_tool_error(&self) -> bool
pub fn is_tool_error(&self) -> bool
返回 ToolResult 的 is_error 标记(仅 ToolResult 变体有效)
Sourcepub fn tool_result(call: &ToolCall, result: &ToolResult) -> Self
pub fn tool_result(call: &ToolCall, result: &ToolResult) -> Self
从工具调用结果构建 Message::ToolResult
成功 → 序列化 serde_json::Value 为文本,is_error: false
失败 → "tool error: {e}" 文本 content,is_error: true
Sourcepub fn validate(&self) -> Result<(), ParseError>
pub fn validate(&self) -> Result<(), ParseError>
语义校验 — 检查 Message 变体与 ContentBlock 的合法性。
v0.1 核心规则:
ToolResult禁止包含ToolCall或ThinkingToolResult.tool_call_id非空Assistant中的ToolCall.id非空User禁止包含Thinking
Sourcepub fn extract_tool_calls(&self) -> Vec<ToolCall>
pub fn extract_tool_calls(&self) -> Vec<ToolCall>
提取所有 ToolCall(仅 Assistant 消息包含)
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Message
impl<'de> Deserialize<'de> for Message
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze 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§
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
Mutably borrows from an owned value. Read more