Skip to main content

ds_api/raw/request/
message.rs

1use serde::{Deserialize, Serialize};
2
3// 统一的消息结构体
4// 该结构体同时用于请求中的 messages 数组和响应中的 message 字段。
5// 所有字段均为可选,以覆盖不同角色和场景的需求。
6#[derive(Debug, Serialize, Deserialize, Default, Clone)]
7pub struct Message {
8    /// default role is User
9    pub role: Role,
10
11    /// content 在 assistant 消息可能为 null(仅 tool_calls 时)
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub content: Option<String>,
14
15    /// 用于标识用户/函数名称(可选)
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub name: Option<String>,
18
19    /// 当 role = "tool" 时必须提供,关联之前的工具调用 ID
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub tool_call_id: Option<String>,
22
23    /// 当 role = "assistant" 且模型请求调用工具时包含此字段
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub tool_calls: Option<Vec<ToolCall>>,
26
27    /// 模型推理过程的内容(仅在响应中可能包含)
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub reasoning_content: Option<String>,
30
31    /// Beta 功能:设置此参数为 true,来强制模型在其回答中以此 assistant 消息中提供的前缀内容开始
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
50// 角色枚举(包含 Tool 变体)
51#[derive(Debug, Serialize, Deserialize, Clone, Default)]
52#[serde(rename_all = "lowercase")]
53pub enum Role {
54    System,
55    #[default]
56    User,
57    Assistant,
58    Tool,
59}
60
61// 工具调用结构体(请求和响应中复用)
62#[derive(Debug, Serialize, Deserialize, Clone)]
63pub struct ToolCall {
64    pub id: String,
65    pub r#type: ToolType,
66    pub function: FunctionCall,
67}
68
69#[derive(Debug, Serialize, Deserialize, Clone)]
70#[serde(rename_all = "lowercase")]
71pub enum ToolType {
72    Function,
73}
74
75#[derive(Debug, Serialize, Deserialize, Clone)]
76pub struct FunctionCall {
77    pub name: String,
78    pub arguments: String, // JSON 字符串
79}