Skip to main content

ds_api/raw/request/
message.rs

1use serde::{Deserialize, Serialize};
2
3// Unified message struct
4// This struct is used both for the `messages` array in requests and the `message` field in responses.
5// All fields are optional to cover different roles and scenarios.
6#[derive(Debug, Serialize, Deserialize, Default, Clone)]
7pub struct Message {
8    /// default role is User
9    pub role: Role,
10
11    /// The content may be null for assistant messages (only when `tool_calls` are present)
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub content: Option<String>,
14
15    /// Optional name to identify a user or function
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub name: Option<String>,
18
19    /// Required when role = "tool"; links to the previous tool call ID
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub tool_call_id: Option<String>,
22
23    /// Present when role = "assistant" and the model requested tool calls
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub tool_calls: Option<Vec<ToolCall>>,
26
27    /// Reasoning content produced by the model (may appear only in responses)
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub reasoning_content: Option<String>,
30
31    /// Beta: if true, forces the model to begin its reply with the prefix content provided in this assistant message
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub prefix: Option<bool>,
34}
35
36impl Message {
37    pub fn new(role: Role, message: &str) -> Self {
38        Self {
39            role,
40            content: Some(message.to_string()),
41            name: None,
42            tool_call_id: None,
43            tool_calls: None,
44            reasoning_content: None,
45            prefix: None,
46        }
47    }
48
49    pub fn user(message: &str) -> Self {
50        Self::new(Role::User, message)
51    }
52
53    pub fn assistant(message: &str) -> Self {
54        Self::new(Role::Assistant, message)
55    }
56}
57
58// Role enum (includes Tool variant)
59#[derive(Debug, Serialize, Deserialize, Clone, Default)]
60#[serde(rename_all = "lowercase")]
61pub enum Role {
62    System,
63    #[default]
64    User,
65    Assistant,
66    Tool,
67}
68
69// Tool call struct (reused in requests and responses)
70#[derive(Debug, Serialize, Deserialize, Clone)]
71pub struct ToolCall {
72    pub id: String,
73    pub r#type: ToolType,
74    pub function: FunctionCall,
75}
76
77#[derive(Debug, Serialize, Deserialize, Clone)]
78#[serde(rename_all = "lowercase")]
79pub enum ToolType {
80    Function,
81}
82
83#[derive(Debug, Serialize, Deserialize, Clone)]
84pub struct FunctionCall {
85    pub name: String,
86    pub arguments: String, // JSON string
87}