1use serde::{Deserialize, Serialize};
4use serde_json::Value;
5use std::collections::HashMap;
6
7use lc_shared::tools::ToolCall;
8
9use super::audio::AudioContent;
10use super::file::FileContent;
11use super::image::ImageContent;
12
13#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
15#[serde(rename_all = "lowercase")]
16pub enum MessageType {
17 System,
18 Human,
19 AI,
20 Tool { tool_call_id: String },
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct Message {
26 pub content: String,
27
28 #[serde(default)]
30 pub images: Vec<ImageContent>,
31
32 #[serde(default)]
34 pub audio: Vec<AudioContent>,
35
36 #[serde(default)]
38 pub files: Vec<FileContent>,
39
40 #[serde(rename = "type")]
41 pub message_type: MessageType,
42
43 #[serde(skip_serializing_if = "Option::is_none")]
44 pub name: Option<String>,
45
46 #[serde(default, skip_serializing_if = "HashMap::is_empty")]
47 pub additional_kwargs: HashMap<String, Value>,
48
49 #[serde(skip_serializing_if = "Option::is_none")]
50 pub id: Option<String>,
51
52 #[serde(skip_serializing_if = "Option::is_none")]
53 pub tool_calls: Option<Vec<ToolCall>>,
54}
55
56impl Message {
57 pub fn system(content: impl Into<String>) -> Self {
59 Self {
60 content: content.into(),
61 images: Vec::new(),
62 audio: Vec::new(),
63 files: Vec::new(),
64 message_type: MessageType::System,
65 name: None,
66 additional_kwargs: HashMap::new(),
67 id: None,
68 tool_calls: None,
69 }
70 }
71
72 pub fn human(content: impl Into<String>) -> Self {
74 Self {
75 content: content.into(),
76 images: Vec::new(),
77 audio: Vec::new(),
78 files: Vec::new(),
79 message_type: MessageType::Human,
80 name: None,
81 additional_kwargs: HashMap::new(),
82 id: None,
83 tool_calls: None,
84 }
85 }
86
87 pub fn human_with_image(content: impl Into<String>, image_url: impl Into<String>) -> Self {
89 Self {
90 content: content.into(),
91 images: vec![ImageContent::from_url(image_url)],
92 audio: Vec::new(),
93 files: Vec::new(),
94 message_type: MessageType::Human,
95 name: None,
96 additional_kwargs: HashMap::new(),
97 id: None,
98 tool_calls: None,
99 }
100 }
101
102 pub fn human_with_images(content: impl Into<String>, images: Vec<ImageContent>) -> Self {
104 Self {
105 content: content.into(),
106 images,
107 audio: Vec::new(),
108 files: Vec::new(),
109 message_type: MessageType::Human,
110 name: None,
111 additional_kwargs: HashMap::new(),
112 id: None,
113 tool_calls: None,
114 }
115 }
116
117 pub fn human_with_audio(content: impl Into<String>, audio: AudioContent) -> Self {
119 Self {
120 content: content.into(),
121 images: Vec::new(),
122 audio: vec![audio],
123 files: Vec::new(),
124 message_type: MessageType::Human,
125 name: None,
126 additional_kwargs: HashMap::new(),
127 id: None,
128 tool_calls: None,
129 }
130 }
131
132 pub fn human_with_file(content: impl Into<String>, file: FileContent) -> Self {
134 Self {
135 content: content.into(),
136 images: Vec::new(),
137 audio: Vec::new(),
138 files: vec![file],
139 message_type: MessageType::Human,
140 name: None,
141 additional_kwargs: HashMap::new(),
142 id: None,
143 tool_calls: None,
144 }
145 }
146
147 pub fn ai(content: impl Into<String>) -> Self {
149 Self {
150 content: content.into(),
151 images: Vec::new(),
152 audio: Vec::new(),
153 files: Vec::new(),
154 message_type: MessageType::AI,
155 name: None,
156 additional_kwargs: HashMap::new(),
157 id: None,
158 tool_calls: None,
159 }
160 }
161
162 pub fn ai_with_tool_calls(content: impl Into<String>, tool_calls: Vec<ToolCall>) -> Self {
164 Self {
165 content: content.into(),
166 images: Vec::new(),
167 audio: Vec::new(),
168 files: Vec::new(),
169 message_type: MessageType::AI,
170 name: None,
171 additional_kwargs: HashMap::new(),
172 id: None,
173 tool_calls: Some(tool_calls),
174 }
175 }
176
177 pub fn tool(tool_call_id: impl Into<String>, content: impl Into<String>) -> Self {
179 Self {
180 content: content.into(),
181 images: Vec::new(),
182 audio: Vec::new(),
183 files: Vec::new(),
184 message_type: MessageType::Tool {
185 tool_call_id: tool_call_id.into(),
186 },
187 name: None,
188 additional_kwargs: HashMap::new(),
189 id: None,
190 tool_calls: None,
191 }
192 }
193
194 pub fn with_name(mut self, name: impl Into<String>) -> Self {
196 self.name = Some(name.into());
197 self
198 }
199
200 pub fn with_id(mut self, id: impl Into<String>) -> Self {
202 self.id = Some(id.into());
203 self
204 }
205
206 pub fn with_additional_kwarg(mut self, key: impl Into<String>, value: Value) -> Self {
208 self.additional_kwargs.insert(key.into(), value);
209 self
210 }
211
212 pub fn with_image(mut self, image: ImageContent) -> Self {
214 self.images.push(image);
215 self
216 }
217
218 pub fn with_audio(mut self, audio: AudioContent) -> Self {
220 self.audio.push(audio);
221 self
222 }
223
224 pub fn with_file(mut self, file: FileContent) -> Self {
226 self.files.push(file);
227 self
228 }
229
230 pub fn has_images(&self) -> bool {
232 !self.images.is_empty()
233 }
234
235 pub fn has_audio(&self) -> bool {
237 !self.audio.is_empty()
238 }
239
240 pub fn has_files(&self) -> bool {
242 !self.files.is_empty()
243 }
244
245 pub fn is_multimodal(&self) -> bool {
247 self.has_images() || self.has_audio() || self.has_files()
248 }
249
250 pub fn type_str(&self) -> &str {
252 match &self.message_type {
253 MessageType::System => "system",
254 MessageType::Human => "human",
255 MessageType::AI => "ai",
256 MessageType::Tool { .. } => "tool",
257 }
258 }
259
260 pub fn has_tool_calls(&self) -> bool {
262 self.tool_calls.is_some() && !self.tool_calls.as_ref().unwrap().is_empty()
263 }
264
265 pub fn get_tool_calls(&self) -> Option<&[ToolCall]> {
267 self.tool_calls.as_deref()
268 }
269}
270
271#[cfg(test)]
272mod tests {
273 use super::*;
274
275 #[test]
276 fn test_human_with_image() {
277 let msg = Message::human_with_image("描述这张图", "https://example.com/img.jpg");
278 assert_eq!(msg.content, "描述这张图");
279 assert_eq!(msg.images.len(), 1);
280 assert_eq!(msg.images[0].url, "https://example.com/img.jpg");
281 assert!(msg.has_images());
282 }
283
284 #[test]
285 fn test_human_no_images_by_default() {
286 let msg = Message::human("纯文本");
287 assert!(msg.images.is_empty());
288 assert!(!msg.has_images());
289 }
290
291 #[test]
292 fn test_with_image_builder() {
293 let msg = Message::human("看图")
294 .with_image(ImageContent::from_url("https://example.com/a.png"))
295 .with_image(ImageContent::from_base64("abc"));
296 assert_eq!(msg.images.len(), 2);
297 }
298
299 #[test]
300 fn test_message_deserialize_without_images_field() {
301 let json = r#"{"content":"hi","type":"human"}"#;
303 let msg: Message = serde_json::from_str(json).unwrap();
304 assert_eq!(msg.content, "hi");
305 assert!(msg.images.is_empty());
306 }
307
308 #[test]
309 fn test_human_with_images_multiple() {
310 let msg = Message::human_with_images(
311 "多图",
312 vec![
313 ImageContent::from_url("https://example.com/1.jpg"),
314 ImageContent::from_url("https://example.com/2.jpg"),
315 ],
316 );
317 assert_eq!(msg.images.len(), 2);
318 }
319
320 #[test]
321 fn test_system_ai_no_images() {
322 assert!(Message::system("s").images.is_empty());
323 assert!(Message::ai("a").images.is_empty());
324 assert!(Message::tool("id", "c").images.is_empty());
325 }
326}