ai_lib/types/
common.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value as JsonValue;
3
4/// Message content — moved to an enum to support multimodal and structured content
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub enum Content {
7    #[serde(rename = "text")]
8    Text(String),
9    /// Generic JSON content for structured payloads (e.g. function call args)
10    #[serde(rename = "json")]
11    Json(JsonValue),
12    /// Reference to an image (url) or metadata; adapters may upload or inline as needed
13    #[serde(rename = "image")]
14    Image {
15        url: Option<String>,
16        mime: Option<String>,
17        name: Option<String>,
18    },
19    /// Reference to audio content
20    #[serde(rename = "audio")]
21    Audio {
22        url: Option<String>,
23        mime: Option<String>,
24    },
25}
26
27impl Content {
28    /// Return a best-effort textual representation for legacy code paths
29    pub fn as_text(&self) -> String {
30        match self {
31            Content::Text(s) => s.clone(),
32            Content::Json(v) => v.to_string(),
33            Content::Image { url, .. } => url.clone().unwrap_or_default(),
34            Content::Audio { url, .. } => url.clone().unwrap_or_default(),
35        }
36    }
37
38    /// Convenience constructor for text content
39    pub fn new_text<S: Into<String>>(s: S) -> Self {
40        Content::Text(s.into())
41    }
42
43    /// Convenience constructor for JSON content
44    pub fn new_json(v: JsonValue) -> Self {
45        Content::Json(v)
46    }
47
48    /// Convenience constructor for image content
49    pub fn new_image(url: Option<String>, mime: Option<String>, name: Option<String>) -> Self {
50        Content::Image { url, mime, name }
51    }
52
53    /// Convenience constructor for audio content
54    pub fn new_audio(url: Option<String>, mime: Option<String>) -> Self {
55        Content::Audio { url, mime }
56    }
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct Message {
61    pub role: Role,
62    pub content: Content,
63    /// Optional function call payload when assistant invokes a tool
64    pub function_call: Option<crate::types::function_call::FunctionCall>,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub enum Role {
69    #[serde(rename = "system")]
70    System,
71    #[serde(rename = "user")]
72    User,
73    #[serde(rename = "assistant")]
74    Assistant,
75}
76
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct Choice {
79    pub index: u32,
80    pub message: Message,
81    pub finish_reason: Option<String>,
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize)]
85pub struct Usage {
86    pub prompt_tokens: u32,
87    pub completion_tokens: u32,
88    pub total_tokens: u32,
89}