1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Response message type for chat responses.
use serde::{Deserialize, Serialize};
#[cfg(feature = "tools")]
use crate::tools::ToolCall;
/// Message in a chat response.
///
/// Contains the assistant's response, including the text content,
/// optional thinking/reasoning output, and any tool calls requested.
///
/// # Examples
///
/// ```no_run
/// use ollama_oxide::ResponseMessage;
///
/// // Deserialize from API response
/// let json = r#"{
/// "role": "assistant",
/// "content": "Hello! How can I help you today?"
/// }"#;
///
/// let msg: ResponseMessage = serde_json::from_str(json).unwrap();
/// assert_eq!(msg.content.as_deref(), Some("Hello! How can I help you today?"));
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct ResponseMessage {
/// Role of the message (always "assistant" for responses).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub role: Option<String>,
/// Assistant's text response.
///
/// May be empty or None when tool_calls are present.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
/// Thinking/reasoning output (if think was enabled in the request).
///
/// Contains the model's reasoning process when thinking mode is enabled.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub thinking: Option<String>,
/// Tool calls requested by the assistant.
///
/// When present, the assistant is requesting your application to
/// execute one or more functions. Execute them and send the results
/// back in a follow-up request.
///
/// Requires the `tools` feature.
#[cfg(feature = "tools")]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCall>>,
/// Optional images in the response.
///
/// Some models may return generated images.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub images: Option<Vec<String>>,
}
impl ResponseMessage {
/// Create a new response message with content.
///
/// # Arguments
///
/// * `content` - The assistant's response text
///
/// # Examples
///
/// ```no_run
/// use ollama_oxide::ResponseMessage;
///
/// let msg = ResponseMessage::new("Hello!");
/// assert_eq!(msg.content(), Some("Hello!"));
/// ```
pub fn new(content: impl Into<String>) -> Self {
Self {
role: Some("assistant".to_string()),
content: Some(content.into()),
thinking: None,
#[cfg(feature = "tools")]
tool_calls: None,
images: None,
}
}
/// Create an empty response message.
///
/// Useful for responses that only contain tool calls.
///
/// # Examples
///
/// ```no_run
/// use ollama_oxide::ResponseMessage;
///
/// let msg = ResponseMessage::empty();
/// assert!(msg.content().is_none());
/// ```
pub fn empty() -> Self {
Self {
role: Some("assistant".to_string()),
content: None,
thinking: None,
#[cfg(feature = "tools")]
tool_calls: None,
images: None,
}
}
/// Get the text content of the message.
///
/// # Returns
///
/// The content as a string slice, or `None` if no content.
///
/// # Examples
///
/// ```no_run
/// use ollama_oxide::ResponseMessage;
///
/// let msg = ResponseMessage::new("Hi there!");
/// assert_eq!(msg.content(), Some("Hi there!"));
/// ```
pub fn content(&self) -> Option<&str> {
self.content.as_deref()
}
/// Get the thinking output.
///
/// # Returns
///
/// The thinking content as a string slice, or `None` if not available.
///
/// # Examples
///
/// ```no_run
/// use ollama_oxide::ResponseMessage;
///
/// let msg = ResponseMessage::default();
/// assert!(msg.thinking().is_none());
/// ```
pub fn thinking(&self) -> Option<&str> {
self.thinking.as_deref()
}
/// Get the tool calls if any.
///
/// Requires the `tools` feature.
///
/// # Returns
///
/// A slice of tool calls, or `None` if no tool calls.
///
/// # Examples
///
/// ```no_run
/// use ollama_oxide::ResponseMessage;
///
/// let msg = ResponseMessage::new("Hello");
/// assert!(msg.tool_calls().is_none());
/// ```
#[cfg(feature = "tools")]
pub fn tool_calls(&self) -> Option<&[ToolCall]> {
self.tool_calls.as_deref()
}
/// Check if the message contains tool calls.
///
/// Requires the `tools` feature.
///
/// # Examples
///
/// ```no_run
/// use ollama_oxide::ResponseMessage;
///
/// let msg = ResponseMessage::new("No tools here");
/// assert!(!msg.has_tool_calls());
/// ```
#[cfg(feature = "tools")]
pub fn has_tool_calls(&self) -> bool {
self.tool_calls
.as_ref()
.map(|tc| !tc.is_empty())
.unwrap_or(false)
}
/// Check if the message has content.
///
/// Returns true if content is present and non-empty.
///
/// # Examples
///
/// ```no_run
/// use ollama_oxide::ResponseMessage;
///
/// let msg = ResponseMessage::new("Hello");
/// assert!(msg.has_content());
///
/// let empty = ResponseMessage::new("");
/// assert!(!empty.has_content());
/// ```
pub fn has_content(&self) -> bool {
self.content
.as_ref()
.map(|c| !c.is_empty())
.unwrap_or(false)
}
/// Check if thinking output is available.
///
/// # Examples
///
/// ```no_run
/// use ollama_oxide::ResponseMessage;
///
/// let msg = ResponseMessage::default();
/// assert!(!msg.has_thinking());
/// ```
pub fn has_thinking(&self) -> bool {
self.thinking
.as_ref()
.map(|t| !t.is_empty())
.unwrap_or(false)
}
/// Check if the message has images.
///
/// # Examples
///
/// ```no_run
/// use ollama_oxide::ResponseMessage;
///
/// let msg = ResponseMessage::new("No images");
/// assert!(!msg.has_images());
/// ```
pub fn has_images(&self) -> bool {
self.images.as_ref().map(|i| !i.is_empty()).unwrap_or(false)
}
/// Get the images if any.
///
/// # Returns
///
/// A slice of base64-encoded images, or `None` if no images.
pub fn images(&self) -> Option<&[String]> {
self.images.as_deref()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "tools")]
use crate::ToolCallFunction;
#[allow(unused_imports)]
use serde_json::json;
#[test]
fn test_response_message_new() {
let msg = ResponseMessage::new("Hello");
assert_eq!(msg.role, Some("assistant".to_string()));
assert_eq!(msg.content, Some("Hello".to_string()));
assert!(msg.thinking.is_none());
#[cfg(feature = "tools")]
assert!(msg.tool_calls.is_none());
assert!(msg.images.is_none());
}
#[test]
fn test_response_message_empty() {
let msg = ResponseMessage::empty();
assert_eq!(msg.role, Some("assistant".to_string()));
assert!(msg.content.is_none());
}
#[test]
fn test_response_message_default() {
let msg = ResponseMessage::default();
assert!(msg.role.is_none());
assert!(msg.content.is_none());
assert!(msg.thinking.is_none());
#[cfg(feature = "tools")]
assert!(msg.tool_calls.is_none());
assert!(msg.images.is_none());
}
#[test]
fn test_response_message_content() {
let msg = ResponseMessage::new("Test content");
assert_eq!(msg.content(), Some("Test content"));
let empty = ResponseMessage::empty();
assert!(empty.content().is_none());
}
#[test]
fn test_response_message_thinking() {
let mut msg = ResponseMessage::new("Response");
msg.thinking = Some("Thinking process...".to_string());
assert_eq!(msg.thinking(), Some("Thinking process..."));
}
#[cfg(feature = "tools")]
#[test]
fn test_response_message_tool_calls() {
let call = ToolCall::new(ToolCallFunction::new("test"));
let mut msg = ResponseMessage::empty();
msg.tool_calls = Some(vec![call]);
assert!(msg.tool_calls().is_some());
assert_eq!(msg.tool_calls().unwrap().len(), 1);
}
#[cfg(feature = "tools")]
#[test]
fn test_response_message_has_tool_calls() {
let msg = ResponseMessage::new("No tools");
assert!(!msg.has_tool_calls());
let mut msg_with_tools = ResponseMessage::empty();
msg_with_tools.tool_calls = Some(vec![ToolCall::new(ToolCallFunction::new("f"))]);
assert!(msg_with_tools.has_tool_calls());
let mut empty_tools = ResponseMessage::empty();
empty_tools.tool_calls = Some(vec![]);
assert!(!empty_tools.has_tool_calls());
}
#[test]
fn test_response_message_has_content() {
let msg = ResponseMessage::new("Hello");
assert!(msg.has_content());
let empty_string = ResponseMessage::new("");
assert!(!empty_string.has_content());
let no_content = ResponseMessage::empty();
assert!(!no_content.has_content());
}
#[test]
fn test_response_message_has_thinking() {
let msg = ResponseMessage::new("Test");
assert!(!msg.has_thinking());
let mut with_thinking = ResponseMessage::new("Test");
with_thinking.thinking = Some("I'm thinking...".to_string());
assert!(with_thinking.has_thinking());
let mut empty_thinking = ResponseMessage::new("Test");
empty_thinking.thinking = Some("".to_string());
assert!(!empty_thinking.has_thinking());
}
#[test]
fn test_response_message_has_images() {
let msg = ResponseMessage::new("Test");
assert!(!msg.has_images());
let mut with_images = ResponseMessage::new("Test");
with_images.images = Some(vec!["base64data".to_string()]);
assert!(with_images.has_images());
}
#[test]
fn test_response_message_images() {
let mut msg = ResponseMessage::new("Test");
msg.images = Some(vec!["img1".to_string(), "img2".to_string()]);
let images = msg.images().unwrap();
assert_eq!(images.len(), 2);
assert_eq!(images[0], "img1");
}
#[test]
fn test_response_message_serialize() {
let msg = ResponseMessage::new("Hello!");
let json = serde_json::to_value(&msg).unwrap();
assert_eq!(json["role"], "assistant");
assert_eq!(json["content"], "Hello!");
assert!(json.get("thinking").is_none()); // Skipped when None
#[cfg(feature = "tools")]
assert!(json.get("tool_calls").is_none());
assert!(json.get("images").is_none());
}
#[cfg(feature = "tools")]
#[test]
fn test_response_message_serialize_with_tool_calls() {
let mut msg = ResponseMessage::empty();
msg.tool_calls = Some(vec![ToolCall::new(ToolCallFunction::with_arguments(
"test",
json!({"x": 1}),
))]);
let json = serde_json::to_value(&msg).unwrap();
assert!(json.get("tool_calls").is_some());
assert_eq!(json["tool_calls"][0]["function"]["name"], "test");
}
#[test]
fn test_response_message_deserialize() {
let json = r#"{
"role": "assistant",
"content": "Hello!"
}"#;
let msg: ResponseMessage = serde_json::from_str(json).unwrap();
assert_eq!(msg.role, Some("assistant".to_string()));
assert_eq!(msg.content, Some("Hello!".to_string()));
}
#[test]
fn test_response_message_deserialize_with_thinking() {
let json = r#"{
"role": "assistant",
"content": "The answer is 42.",
"thinking": "Let me calculate this step by step..."
}"#;
let msg: ResponseMessage = serde_json::from_str(json).unwrap();
assert_eq!(msg.content(), Some("The answer is 42."));
assert_eq!(
msg.thinking(),
Some("Let me calculate this step by step...")
);
}
#[cfg(feature = "tools")]
#[test]
fn test_response_message_deserialize_with_tool_calls() {
let json = r#"{
"role": "assistant",
"content": "",
"tool_calls": [
{
"function": {
"name": "get_weather",
"arguments": {"location": "Paris"}
}
}
]
}"#;
let msg: ResponseMessage = serde_json::from_str(json).unwrap();
assert!(msg.has_tool_calls());
let calls = msg.tool_calls().unwrap();
assert_eq!(calls[0].function_name(), Some("get_weather"));
}
#[test]
fn test_response_message_deserialize_empty() {
let json = "{}";
let msg: ResponseMessage = serde_json::from_str(json).unwrap();
assert!(msg.role.is_none());
assert!(msg.content.is_none());
}
#[test]
fn test_response_message_clone() {
let mut msg = ResponseMessage::new("Test");
msg.thinking = Some("Thinking".to_string());
#[cfg(feature = "tools")]
{
msg.tool_calls = Some(vec![ToolCall::new(ToolCallFunction::new("f"))]);
}
let cloned = msg.clone();
assert_eq!(msg, cloned);
}
#[test]
fn test_response_message_equality() {
let msg1 = ResponseMessage::new("Hello");
let msg2 = ResponseMessage::new("Hello");
let msg3 = ResponseMessage::new("World");
assert_eq!(msg1, msg2);
assert_ne!(msg1, msg3);
}
}