1use crate::error::Result;
4use crate::llm::models::{LlmMessage, LlmToolCall, MessageRole};
5use base64::Engine;
6use serde_json::Value;
7use std::path::Path;
8use tracing::warn;
9
10#[derive(Debug, Clone)]
12pub struct OpenAIMessage {
13 pub role: String,
14 pub content: OpenAIContent,
15 pub tool_calls: Option<Vec<OpenAIToolCall>>,
16 pub tool_call_id: Option<String>,
17}
18
19#[derive(Debug, Clone)]
21pub enum OpenAIContent {
22 Text(String),
23 Parts(Vec<OpenAIContentPart>),
24}
25
26#[derive(Debug, Clone)]
28pub enum OpenAIContentPart {
29 Text { text: String },
30 ImageUrl { url: String },
31}
32
33#[derive(Debug, Clone)]
35pub struct OpenAIToolCall {
36 pub id: String,
37 pub r#type: String,
38 pub function: OpenAIToolCallFunction,
39}
40
41#[derive(Debug, Clone)]
43pub struct OpenAIToolCallFunction {
44 pub name: String,
45 pub arguments: String,
46}
47
48fn get_image_type(file_path: &str) -> &'static str {
50 let ext = Path::new(file_path)
51 .extension()
52 .and_then(|e| e.to_str())
53 .unwrap_or("")
54 .to_lowercase();
55
56 match ext.as_str() {
57 "jpg" | "jpeg" => "jpeg",
58 "png" => "png",
59 "gif" => "gif",
60 "webp" => "webp",
61 _ => "jpeg", }
63}
64
65fn encode_image_as_base64(file_path: &str) -> Result<String> {
67 if file_path.starts_with("https://")
68 || file_path.starts_with("http://")
69 || file_path.starts_with("data:")
70 {
71 return Ok(file_path.to_string());
72 }
73 let bytes = std::fs::read(file_path)?;
74 let base64_data = base64::engine::general_purpose::STANDARD.encode(&bytes);
75 let image_type = get_image_type(file_path);
76 Ok(format!("data:image/{};base64,{}", image_type, base64_data))
77}
78
79pub fn adapt_messages_to_openai(messages: &[LlmMessage]) -> Result<Vec<Value>> {
81 adapt_messages_to_openai_with_reasoning(messages, false)
82}
83
84pub fn adapt_messages_to_openai_with_reasoning(
86 messages: &[LlmMessage],
87 native_reasoning: bool,
88) -> Result<Vec<Value>> {
89 let mut result = Vec::new();
90
91 for msg in messages {
92 let openai_msg = match msg.role {
93 MessageRole::System => {
94 serde_json::json!({
95 "role": "system",
96 "content": msg.content.as_deref().unwrap_or("")
97 })
98 }
99 MessageRole::User => {
100 if let Some(ref image_paths) = msg.image_paths {
102 if !image_paths.is_empty() {
103 let mut content_parts = Vec::new();
104
105 if let Some(ref text) = msg.content {
107 if !text.is_empty() {
108 content_parts.push(serde_json::json!({
109 "type": "text",
110 "text": text
111 }));
112 }
113 }
114
115 for path in image_paths {
117 match encode_image_as_base64(path) {
118 Ok(data_url) => {
119 content_parts.push(serde_json::json!({
120 "type": "image_url",
121 "image_url": {
122 "url": data_url
123 }
124 }));
125 }
126 Err(e) => {
127 warn!(path = path, error = %e, "Failed to encode image");
128 }
129 }
130 }
131
132 serde_json::json!({
133 "role": "user",
134 "content": content_parts
135 })
136 } else {
137 serde_json::json!({
138 "role": "user",
139 "content": msg.content.as_deref().unwrap_or("")
140 })
141 }
142 } else {
143 serde_json::json!({
144 "role": "user",
145 "content": msg.content.as_deref().unwrap_or("")
146 })
147 }
148 }
149 MessageRole::Assistant => {
150 let mut assistant_msg = serde_json::json!({
151 "role": "assistant"
152 });
153
154 if let Some(ref content) = msg.content {
155 assistant_msg["content"] = serde_json::json!(content);
156 }
157
158 if native_reasoning {
159 if let Some(reasoning) = &msg.reasoning_content {
160 assistant_msg["reasoning_content"] = serde_json::json!(reasoning);
161 }
162 }
163 if let Some(ref tool_calls) = msg.tool_calls {
165 let formatted_calls: Vec<Value> = tool_calls
166 .iter()
167 .map(|tc| {
168 serde_json::json!({
169 "id": tc.id.as_deref().unwrap_or(""),
170 "type": "function",
171 "function": {
172 "name": tc.name,
173 "arguments": serde_json::to_string(&tc.arguments).unwrap_or_default()
174 }
175 })
176 })
177 .collect();
178 assistant_msg["tool_calls"] = serde_json::json!(formatted_calls);
179 }
180
181 assistant_msg
182 }
183 MessageRole::Tool => {
184 let tool_call_id = msg
186 .tool_calls
187 .as_ref()
188 .and_then(|tcs| tcs.first())
189 .and_then(|tc| tc.id.clone())
190 .unwrap_or_default();
191
192 serde_json::json!({
193 "role": "tool",
194 "content": msg.content.as_deref().unwrap_or(""),
195 "tool_call_id": tool_call_id
196 })
197 }
198 };
199
200 result.push(openai_msg);
201 }
202
203 Ok(result)
204}
205
206pub fn convert_tool_calls(tool_calls: &[Value]) -> Vec<LlmToolCall> {
208 tool_calls
209 .iter()
210 .filter_map(|tc| {
211 let id = tc["id"].as_str().map(String::from);
212 let name = tc["function"]["name"].as_str()?.to_string();
213 let args_str = tc["function"]["arguments"].as_str().unwrap_or("{}");
214
215 let arguments: std::collections::HashMap<String, Value> =
217 serde_json::from_str(args_str).unwrap_or_default();
218
219 Some(LlmToolCall {
220 id,
221 name,
222 arguments,
223 })
224 })
225 .collect()
226}
227
228#[cfg(test)]
229mod tests {
230 use super::*;
231 use std::collections::HashMap;
232 use std::io::Write;
233 use tempfile::NamedTempFile;
234
235 #[test]
236 fn test_get_image_type_jpg() {
237 assert_eq!(get_image_type("/path/to/image.jpg"), "jpeg");
238 assert_eq!(get_image_type("/path/to/image.jpeg"), "jpeg");
239 }
240
241 #[test]
242 fn test_get_image_type_png() {
243 assert_eq!(get_image_type("/path/to/image.png"), "png");
244 }
245
246 #[test]
247 fn test_get_image_type_gif() {
248 assert_eq!(get_image_type("/path/to/image.gif"), "gif");
249 }
250
251 #[test]
252 fn test_get_image_type_webp() {
253 assert_eq!(get_image_type("/path/to/image.webp"), "webp");
254 }
255
256 #[test]
257 fn test_get_image_type_unknown() {
258 assert_eq!(get_image_type("/path/to/image.unknown"), "jpeg");
259 }
260
261 #[test]
262 fn test_adapt_system_message() {
263 let messages = vec![LlmMessage::system("You are helpful")];
264
265 let result = adapt_messages_to_openai(&messages).unwrap();
266
267 assert_eq!(result.len(), 1);
268 assert_eq!(result[0]["role"], "system");
269 assert_eq!(result[0]["content"], "You are helpful");
270 }
271
272 #[test]
273 fn test_adapt_user_message() {
274 let messages = vec![LlmMessage::user("Hello")];
275
276 let result = adapt_messages_to_openai(&messages).unwrap();
277
278 assert_eq!(result.len(), 1);
279 assert_eq!(result[0]["role"], "user");
280 assert_eq!(result[0]["content"], "Hello");
281 }
282
283 #[test]
284 fn test_adapt_assistant_message() {
285 let messages = vec![LlmMessage::assistant("Hi there")];
286
287 let result = adapt_messages_to_openai(&messages).unwrap();
288
289 assert_eq!(result.len(), 1);
290 assert_eq!(result[0]["role"], "assistant");
291 assert_eq!(result[0]["content"], "Hi there");
292 }
293
294 #[test]
295 fn test_adapt_user_message_with_images() {
296 let mut temp_file = NamedTempFile::new().unwrap();
298 temp_file.write_all(b"fake image data").unwrap();
299 let path = temp_file.path().to_string_lossy().to_string();
300
301 let messages =
302 vec![LlmMessage::user("Describe this image").with_images(vec![path.clone()])];
303
304 let result = adapt_messages_to_openai(&messages).unwrap();
305
306 assert_eq!(result.len(), 1);
307 assert_eq!(result[0]["role"], "user");
308
309 let content = &result[0]["content"];
310 assert!(content.is_array());
311
312 let parts = content.as_array().unwrap();
313 assert_eq!(parts.len(), 2);
314 assert_eq!(parts[0]["type"], "text");
315 assert_eq!(parts[0]["text"], "Describe this image");
316 assert_eq!(parts[1]["type"], "image_url");
317 assert!(parts[1]["image_url"]["url"]
318 .as_str()
319 .unwrap()
320 .starts_with("data:image/jpeg;base64,"));
321 }
322
323 #[test]
324 fn test_adapt_user_message_preserves_remote_and_data_images() {
325 let images = vec![
326 "https://example.com/image.png".to_string(),
327 "http://example.com/image.jpg".to_string(),
328 "data:image/png;base64,aGVsbG8=".to_string(),
329 ];
330 let messages = vec![LlmMessage::user("Describe").with_images(images.clone())];
331 let result = adapt_messages_to_openai(&messages).unwrap();
332 let parts = result[0]["content"].as_array().unwrap();
333 assert_eq!(parts.len(), 4);
334 for (part, source) in parts[1..].iter().zip(images) {
335 assert_eq!(part["type"], "image_url");
336 assert_eq!(part["image_url"]["url"], source);
337 }
338 }
339
340 #[test]
341 fn test_adapt_assistant_with_tool_calls() {
342 let tool_call = LlmToolCall {
343 id: Some("call_123".to_string()),
344 name: "get_weather".to_string(),
345 arguments: {
346 let mut map = HashMap::new();
347 map.insert("location".to_string(), serde_json::json!("NYC"));
348 map
349 },
350 };
351
352 let messages = vec![LlmMessage {
353 role: MessageRole::Assistant,
354 content: None,
355 tool_calls: Some(vec![tool_call]),
356 image_paths: None,
357 reasoning_content: None,
358 }];
359
360 let result = adapt_messages_to_openai(&messages).unwrap();
361
362 assert_eq!(result.len(), 1);
363 assert_eq!(result[0]["role"], "assistant");
364
365 let tool_calls = &result[0]["tool_calls"];
366 assert!(tool_calls.is_array());
367
368 let calls = tool_calls.as_array().unwrap();
369 assert_eq!(calls.len(), 1);
370 assert_eq!(calls[0]["id"], "call_123");
371 assert_eq!(calls[0]["type"], "function");
372 assert_eq!(calls[0]["function"]["name"], "get_weather");
373 }
374
375 #[test]
376 fn test_adapt_tool_message() {
377 let messages = vec![LlmMessage {
378 role: MessageRole::Tool,
379 content: Some("Weather result: 72F".to_string()),
380 tool_calls: Some(vec![LlmToolCall {
381 id: Some("call_123".to_string()),
382 name: "get_weather".to_string(),
383 arguments: HashMap::new(),
384 }]),
385 image_paths: None,
386 reasoning_content: None,
387 }];
388
389 let result = adapt_messages_to_openai(&messages).unwrap();
390
391 assert_eq!(result.len(), 1);
392 assert_eq!(result[0]["role"], "tool");
393 assert_eq!(result[0]["content"], "Weather result: 72F");
394 assert_eq!(result[0]["tool_call_id"], "call_123");
395 }
396
397 #[test]
398 fn test_convert_tool_calls() {
399 let tool_calls = vec![serde_json::json!({
400 "id": "call_abc",
401 "type": "function",
402 "function": {
403 "name": "search",
404 "arguments": "{\"query\": \"test\"}"
405 }
406 })];
407
408 let result = convert_tool_calls(&tool_calls);
409
410 assert_eq!(result.len(), 1);
411 assert_eq!(result[0].id, Some("call_abc".to_string()));
412 assert_eq!(result[0].name, "search");
413 assert_eq!(result[0].arguments.get("query"), Some(&serde_json::json!("test")));
414 }
415
416 #[test]
417 fn test_convert_tool_calls_empty_args() {
418 let tool_calls = vec![serde_json::json!({
419 "id": "call_xyz",
420 "type": "function",
421 "function": {
422 "name": "no_args_tool",
423 "arguments": "{}"
424 }
425 })];
426
427 let result = convert_tool_calls(&tool_calls);
428
429 assert_eq!(result.len(), 1);
430 assert_eq!(result[0].name, "no_args_tool");
431 assert!(result[0].arguments.is_empty());
432 }
433}