serdes-ai-core 0.2.2

Core types, messages, and error handling for serdes-ai
Documentation
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
//! Response message types from model interactions.
//!
//! This module defines the message types that are returned FROM the model,
//! including text content, tool calls, and thinking/reasoning content.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use super::parts::{BuiltinToolCallPart, FilePart, TextPart, ThinkingPart, ToolCallPart};
use crate::usage::RequestUsage;

/// A complete model response containing multiple parts.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ModelResponse {
    /// The response parts.
    pub parts: Vec<ModelResponsePart>,
    /// Name of the model that generated this response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model_name: Option<String>,
    /// When this response was received.
    pub timestamp: DateTime<Utc>,
    /// Why the model stopped generating.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub finish_reason: Option<FinishReason>,
    /// Token usage for this request.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<RequestUsage>,
    /// Vendor-specific response ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vendor_id: Option<String>,
    /// Vendor-specific details.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vendor_details: Option<serde_json::Value>,
    /// Kind identifier.
    #[serde(default = "default_response_kind")]
    pub kind: String,
}

fn default_response_kind() -> String {
    "response".to_string()
}

impl ModelResponse {
    /// Create a new empty response.
    #[must_use]
    pub fn new() -> Self {
        Self {
            parts: Vec::new(),
            model_name: None,
            timestamp: Utc::now(),
            finish_reason: None,
            usage: None,
            vendor_id: None,
            vendor_details: None,
            kind: "response".to_string(),
        }
    }

    /// Create a response with the given parts.
    #[must_use]
    pub fn with_parts(parts: Vec<ModelResponsePart>) -> Self {
        Self {
            parts,
            ..Self::new()
        }
    }

    /// Create a simple text response.
    #[must_use]
    pub fn text(content: impl Into<String>) -> Self {
        Self::with_parts(vec![ModelResponsePart::Text(TextPart::new(content))])
    }

    /// Add a part.
    pub fn add_part(&mut self, part: ModelResponsePart) {
        self.parts.push(part);
    }

    /// Set the model name.
    #[must_use]
    pub fn with_model_name(mut self, name: impl Into<String>) -> Self {
        self.model_name = Some(name.into());
        self
    }

    /// Set the finish reason.
    #[must_use]
    pub fn with_finish_reason(mut self, reason: FinishReason) -> Self {
        self.finish_reason = Some(reason);
        self
    }

    /// Set the usage.
    #[must_use]
    pub fn with_usage(mut self, usage: RequestUsage) -> Self {
        self.usage = Some(usage);
        self
    }

    /// Set the vendor ID.
    #[must_use]
    pub fn with_vendor_id(mut self, id: impl Into<String>) -> Self {
        self.vendor_id = Some(id.into());
        self
    }

    /// Set vendor details.
    #[must_use]
    pub fn with_vendor_details(mut self, details: serde_json::Value) -> Self {
        self.vendor_details = Some(details);
        self
    }

    /// Get all text parts.
    pub fn text_parts(&self) -> impl Iterator<Item = &TextPart> {
        self.parts.iter().filter_map(|p| match p {
            ModelResponsePart::Text(t) => Some(t),
            _ => None,
        })
    }

    /// Get all tool call parts.
    pub fn tool_call_parts(&self) -> impl Iterator<Item = &ToolCallPart> {
        self.parts.iter().filter_map(|p| match p {
            ModelResponsePart::ToolCall(t) => Some(t),
            _ => None,
        })
    }

    /// Get all thinking parts.
    pub fn thinking_parts(&self) -> impl Iterator<Item = &ThinkingPart> {
        self.parts.iter().filter_map(|p| match p {
            ModelResponsePart::Thinking(t) => Some(t),
            _ => None,
        })
    }

    /// Get all file parts.
    pub fn file_parts(&self) -> impl Iterator<Item = &FilePart> {
        self.parts.iter().filter_map(|p| match p {
            ModelResponsePart::File(f) => Some(f),
            _ => None,
        })
    }

    /// Get all text parts as a vector.
    #[deprecated(note = "Use text_parts() iterator instead")]
    pub fn text_parts_vec(&self) -> Vec<&TextPart> {
        self.text_parts().collect()
    }

    /// Get all tool call parts as a vector.
    #[deprecated(note = "Use tool_call_parts() iterator instead")]
    pub fn tool_call_parts_vec(&self) -> Vec<&ToolCallPart> {
        self.tool_call_parts().collect()
    }

    /// Get all thinking parts as a vector.
    #[deprecated(note = "Use thinking_parts() iterator instead")]
    pub fn thinking_parts_vec(&self) -> Vec<&ThinkingPart> {
        self.thinking_parts().collect()
    }

    /// Get all file parts as a vector.
    #[deprecated(note = "Use file_parts() iterator instead")]
    pub fn file_parts_vec(&self) -> Vec<&FilePart> {
        self.file_parts().collect()
    }

    /// Check if this response contains file parts.
    #[must_use]
    pub fn has_files(&self) -> bool {
        self.parts
            .iter()
            .any(|p| matches!(p, ModelResponsePart::File(_)))
    }

    /// Get all builtin tool call parts.
    pub fn builtin_tool_call_parts(&self) -> impl Iterator<Item = &BuiltinToolCallPart> {
        self.parts.iter().filter_map(|p| match p {
            ModelResponsePart::BuiltinToolCall(b) => Some(b),
            _ => None,
        })
    }

    /// Get all builtin tool call parts as a vector.
    #[deprecated(note = "Use builtin_tool_call_parts() iterator instead")]
    pub fn builtin_tool_call_parts_vec(&self) -> Vec<&BuiltinToolCallPart> {
        self.builtin_tool_call_parts().collect()
    }

    /// Check if this response contains builtin tool calls.
    #[must_use]
    pub fn has_builtin_tool_calls(&self) -> bool {
        self.parts
            .iter()
            .any(|p| matches!(p, ModelResponsePart::BuiltinToolCall(_)))
    }

    /// Get combined text content.
    #[must_use]
    pub fn text_content(&self) -> String {
        self.text_parts()
            .map(|p| p.content.as_str())
            .collect::<Vec<_>>()
            .join("")
    }

    /// Check if this response contains tool calls.
    #[must_use]
    pub fn has_tool_calls(&self) -> bool {
        self.parts
            .iter()
            .any(|p| matches!(p, ModelResponsePart::ToolCall(_)))
    }

    /// Check if the response is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.parts.is_empty()
    }

    /// Get the number of parts.
    #[must_use]
    pub fn len(&self) -> usize {
        self.parts.len()
    }
}

impl Default for ModelResponse {
    fn default() -> Self {
        Self::new()
    }
}

impl FromIterator<ModelResponsePart> for ModelResponse {
    fn from_iter<T: IntoIterator<Item = ModelResponsePart>>(iter: T) -> Self {
        Self::with_parts(iter.into_iter().collect())
    }
}

/// Individual parts of a model response.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "part_kind", rename_all = "kebab-case")]
pub enum ModelResponsePart {
    /// Text content.
    Text(TextPart),
    /// Tool call.
    ToolCall(ToolCallPart),
    /// Thinking/reasoning content.
    Thinking(ThinkingPart),
    /// File content (e.g., generated images).
    File(FilePart),
    /// Builtin tool call (web search, code execution, etc.).
    BuiltinToolCall(BuiltinToolCallPart),
}

impl ModelResponsePart {
    /// Create a text part.
    #[must_use]
    pub fn text(content: impl Into<String>) -> Self {
        Self::Text(TextPart::new(content))
    }

    /// Create a tool call part.
    #[must_use]
    pub fn tool_call(
        tool_name: impl Into<String>,
        args: impl Into<super::parts::ToolCallArgs>,
    ) -> Self {
        Self::ToolCall(ToolCallPart::new(tool_name, args))
    }

    /// Create a thinking part.
    #[must_use]
    pub fn thinking(content: impl Into<String>) -> Self {
        Self::Thinking(ThinkingPart::new(content))
    }

    /// Create a file part from raw bytes and media type.
    #[must_use]
    pub fn file(data: Vec<u8>, media_type: impl Into<String>) -> Self {
        Self::File(FilePart::from_bytes(data, media_type))
    }

    /// Create a builtin tool call part.
    #[must_use]
    pub fn builtin_tool_call(
        tool_name: impl Into<String>,
        args: impl Into<super::parts::ToolCallArgs>,
    ) -> Self {
        Self::BuiltinToolCall(BuiltinToolCallPart::new(tool_name, args))
    }

    /// Get the part kind.
    #[must_use]
    pub fn part_kind(&self) -> &'static str {
        match self {
            Self::Text(_) => TextPart::PART_KIND,
            Self::ToolCall(_) => ToolCallPart::PART_KIND,
            Self::Thinking(_) => ThinkingPart::PART_KIND,
            Self::File(_) => FilePart::PART_KIND,
            Self::BuiltinToolCall(_) => BuiltinToolCallPart::PART_KIND,
        }
    }

    /// Check if this is a text part.
    #[must_use]
    pub fn is_text(&self) -> bool {
        matches!(self, Self::Text(_))
    }

    /// Check if this is a tool call part.
    #[must_use]
    pub fn is_tool_call(&self) -> bool {
        matches!(self, Self::ToolCall(_))
    }

    /// Check if this is a thinking part.
    #[must_use]
    pub fn is_thinking(&self) -> bool {
        matches!(self, Self::Thinking(_))
    }

    /// Check if this is a file part.
    #[must_use]
    pub fn is_file(&self) -> bool {
        matches!(self, Self::File(_))
    }

    /// Check if this is a builtin tool call part.
    #[must_use]
    pub fn is_builtin_tool_call(&self) -> bool {
        matches!(self, Self::BuiltinToolCall(_))
    }
}

impl From<TextPart> for ModelResponsePart {
    fn from(p: TextPart) -> Self {
        Self::Text(p)
    }
}

impl From<ToolCallPart> for ModelResponsePart {
    fn from(p: ToolCallPart) -> Self {
        Self::ToolCall(p)
    }
}

impl From<ThinkingPart> for ModelResponsePart {
    fn from(p: ThinkingPart) -> Self {
        Self::Thinking(p)
    }
}

impl From<FilePart> for ModelResponsePart {
    fn from(p: FilePart) -> Self {
        Self::File(p)
    }
}

impl From<BuiltinToolCallPart> for ModelResponsePart {
    fn from(p: BuiltinToolCallPart) -> Self {
        Self::BuiltinToolCall(p)
    }
}

/// Reason why the model stopped generating.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FinishReason {
    /// Natural end of response.
    Stop,
    /// Maximum tokens reached.
    Length,
    /// Content was filtered.
    ContentFilter,
    /// Model wants to call tools.
    ToolCall,
    /// An error occurred.
    Error,
    /// End of turn.
    EndTurn,
    /// Stop sequence encountered.
    StopSequence,
}

impl FinishReason {
    /// Check if this indicates the response is complete.
    #[must_use]
    pub fn is_complete(&self) -> bool {
        matches!(self, Self::Stop | Self::EndTurn | Self::StopSequence)
    }

    /// Check if this indicates truncation.
    #[must_use]
    pub fn is_truncated(&self) -> bool {
        matches!(self, Self::Length)
    }

    /// Check if this indicates tool use.
    #[must_use]
    pub fn is_tool_call(&self) -> bool {
        matches!(self, Self::ToolCall)
    }
}

impl std::fmt::Display for FinishReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Stop => write!(f, "stop"),
            Self::Length => write!(f, "length"),
            Self::ContentFilter => write!(f, "content_filter"),
            Self::ToolCall => write!(f, "tool_call"),
            Self::Error => write!(f, "error"),
            Self::EndTurn => write!(f, "end_turn"),
            Self::StopSequence => write!(f, "stop_sequence"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_model_response_new() {
        let response = ModelResponse::new();
        assert!(response.is_empty());
        assert!(!response.has_tool_calls());
    }

    #[test]
    fn test_model_response_text() {
        let response = ModelResponse::text("Hello, world!");
        assert_eq!(response.len(), 1);
        assert_eq!(response.text_content(), "Hello, world!");
    }

    #[test]
    fn test_model_response_with_tool_calls() {
        let response = ModelResponse::with_parts(vec![
            ModelResponsePart::text("Let me check the weather."),
            ModelResponsePart::tool_call("get_weather", serde_json::json!({"city": "NYC"})),
        ]);
        assert!(response.has_tool_calls());
        assert_eq!(response.tool_call_parts().count(), 1);
    }

    #[test]
    fn test_finish_reason() {
        assert!(FinishReason::Stop.is_complete());
        assert!(FinishReason::Length.is_truncated());
        assert!(FinishReason::ToolCall.is_tool_call());
    }

    #[test]
    fn test_serde_roundtrip() {
        let response = ModelResponse::with_parts(vec![
            ModelResponsePart::text("Hello"),
            ModelResponsePart::thinking("Thinking..."),
        ])
        .with_model_name("gpt-4")
        .with_finish_reason(FinishReason::Stop);

        let json = serde_json::to_string(&response).unwrap();
        let parsed: ModelResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(response.len(), parsed.len());
        assert_eq!(response.model_name, parsed.model_name);
    }
}