dify_client/
request.rs

1//! This module contains the request structures used in the Dify client SDK.
2//!
3//! The request structures define the data structures used to send various requests to the Dify API.
4//! These requests include sending chat messages, stopping stream tasks, getting suggested questions,
5//! providing message feedback, retrieving conversation history, managing conversations, uploading files,
6//! executing workflows, converting text to audio, converting audio to text, and generating completion messages.
7//!
8//! Each request structure is defined as a Rust struct and is annotated with `#[derive(Debug, Clone, Default, Serialize, Deserialize)]`
9//! to enable serialization and deserialization using the `serde` crate.
10//!
11//! The request structures include additional documentation comments to provide information about the purpose
12//! and usage of each field in the structure.
13//!
14//! Example:
15//!
16//! ```ignore
17//! // FILEPATH: ~/git/dify/dify-sdk-rs/dify-client/src/request.rs
18//! pub use bytes::Bytes;
19//! use serde::{Deserialize, Serialize};
20//! use std::collections::HashMap;
21//!
22//!
23//! /// 发送对话消息的请求
24//! /// 创建会话消息。
25//! #[derive(Debug, Clone, Default, Serialize, Deserialize)]
26//! pub struct ChatMessagesRequest {
27//!     /// 允许传入 App 定义的各变量值。
28//!     /// inputs 参数包含了多组键值对(Key/Value pairs),每组的键对应一个特定变量,每组的值则是该变量的具体值。
29//!     /// 默认 {}
30//!     pub inputs: HashMap<String, String>,
31//!     /// 用户输入/提问内容。
32//!     pub query: String,
33//!     /// 响应模式
34//!     /// * streaming 流式模式(推荐)。基于 SSE(Server-Sent Events)实现类似打字机输出方式的流式返回。
35//!     /// * blocking 阻塞模式,等待执行完毕后返回结果。(请求若流程较长可能会被中断)。
36//!     /// 由于 Cloudflare 限制,请求会在 100 秒超时无返回后中断。
37//!     pub response_mode: ResponseMode,
38//!     /// 用户标识,用于定义终端用户的身份,方便检索、统计。
39//!     /// 由开发者定义规则,需保证用户标识在应用内唯一。
40//!     pub user: String,
41//!     /// 会话 ID(选填),需要基于之前的聊天记录继续对话,必须传之前消息的 conversation_id。
42//!     pub conversation_id: String,
43//!     /// 上传的文件。
44//!     pub files: Vec<FileInput>,
45//!     /// 自动生成标题(选填),默认 true。
46//!     /// 若设置为 false,则可通过调用会话重命名接口并设置 auto_generate 为 true 实现异步生成标题。
47//!     pub auto_generate_name: bool,
48//! }
49//!
50//! // ... (other request structures)
51//!
52//! ```
53//!
54//! The request structures can be used to construct requests to the Dify API by populating the fields with the required data.
55//! These structures can then be serialized into JSON or other formats using the `serde` crate's serialization capabilities.
56//!
57//! For more information on each request structure and its fields, refer to the documentation comments provided for each structure.
58//!
59pub use bytes::Bytes;
60use serde::{Deserialize, Serialize};
61use std::collections::HashMap;
62
63/// 发送对话消息的请求
64/// 创建会话消息。
65#[derive(Debug, Clone, Default, Serialize, Deserialize)]
66pub struct ChatMessagesRequest {
67    /// 允许传入 App 定义的各变量值。  
68    /// inputs 参数包含了多组键值对(Key/Value pairs),每组的键对应一个特定变量,每组的值则是该变量的具体值。  
69    /// 默认 {}  
70    pub inputs: HashMap<String, String>,
71    /// 用户输入/提问内容。
72    pub query: String,
73    /// 响应模式  
74    /// * streaming 流式模式(推荐)。基于 SSE(Server-Sent Events)实现类似打字机输出方式的流式返回。
75    /// * blocking 阻塞模式,等待执行完毕后返回结果。(请求若流程较长可能会被中断)。  
76    /// 由于 Cloudflare 限制,请求会在 100 秒超时无返回后中断。
77    pub response_mode: ResponseMode,
78    /// 用户标识,用于定义终端用户的身份,方便检索、统计。  
79    /// 由开发者定义规则,需保证用户标识在应用内唯一。  
80    pub user: String,
81    /// 会话 ID(选填),需要基于之前的聊天记录继续对话,必须传之前消息的 conversation_id。
82    pub conversation_id: String,
83    /// 上传的文件。
84    pub files: Vec<FileInput>,
85    /// 自动生成标题(选填),默认 true。  
86    /// 若设置为 false,则可通过调用会话重命名接口并设置 auto_generate 为 true 实现异步生成标题。
87    pub auto_generate_name: bool,
88}
89
90/// 响应模式
91/// * streaming 流式模式(推荐)。基于 SSE(Server-Sent Events)实现类似打字机输出方式的流式返回。
92/// * blocking 阻塞模式,等待执行完毕后返回结果。(请求若流程较长可能会被中断)。  
93/// 由于 Cloudflare 限制,请求会在 100 秒超时无返回后中断。
94#[derive(Debug, Clone, Default, Serialize, Deserialize)]
95#[serde(rename_all = "snake_case")]
96pub enum ResponseMode {
97    /// 阻塞模式
98    #[default]
99    Blocking,
100    /// 流式模式
101    Streaming,
102}
103
104/// 文件类型
105#[derive(Debug, Clone, Default, Serialize, Deserialize)]
106#[serde(rename_all = "snake_case")]
107pub enum FileType {
108    #[default]
109    Image,
110}
111
112/// 上传的文件
113#[derive(Debug, Clone, Serialize, Deserialize)]
114#[serde(rename_all = "snake_case", tag = "transfer_method")]
115pub enum FileInput {
116    /// 图片地址方式传递
117    RemoteUrl {
118        /// 文件类型
119        #[serde(rename = "type")]
120        type_: FileType,
121        /// 图片地址
122        url: String,
123    },
124    /// 上传文件方式传递
125    LocalFile {
126        /// 文件类型
127        #[serde(rename = "type")]
128        type_: FileType,
129        /// 上传文件 ID
130        upload_file_id: String,
131    },
132}
133
134/// 停止响应请求
135#[derive(Debug, Clone, Default, Serialize, Deserialize)]
136pub struct StreamTaskStopRequest {
137    /// 任务 ID,可在流式返回 Chunk 中获取
138    pub task_id: String,
139    /// 用户标识,用于定义终端用户的身份,必须和发送消息接口传入 user 保持一致。
140    pub user: String,
141}
142
143/// 获取下一轮建议问题列表请求
144#[derive(Debug, Clone, Default, Serialize, Deserialize)]
145pub struct MessagesSuggestedRequest {
146    /// Message ID
147    pub message_id: String,
148}
149
150/// 消息反馈请求
151/// 消息终端用户反馈、点赞,方便应用开发者优化输出预期。
152#[derive(Debug, Clone, Default, Serialize, Deserialize)]
153pub struct MessagesFeedbacksRequest {
154    /// 消息 ID
155    pub message_id: String,
156    /// 点赞 Like, 点踩 Dislike, 撤销点赞 None
157    pub rating: Option<Feedback>,
158    /// 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
159    pub user: String,
160}
161
162/// 消息反馈
163#[derive(Debug, Clone, Default, Serialize, Deserialize)]
164#[serde(rename_all = "snake_case")]
165pub enum Feedback {
166    /// 点赞
167    #[default]
168    Like,
169    /// 点踩
170    Dislike,
171}
172
173/// 获取会话历史消息的请求
174/// 滚动加载形式返回历史聊天记录,第一页返回最新 limit 条,即:倒序返回。
175#[derive(Debug, Clone, Default, Deserialize, Serialize)]
176pub struct MessagesRequest {
177    /// 会话 ID
178    pub conversation_id: String,
179    /// 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
180    pub user: String,
181    /// 当前页第一条聊天记录的 ID,默认 None
182    pub first_id: Option<String>,
183    /// 一次请求返回多少条聊天记录,默认 20 条。
184    pub limit: Option<u32>,
185}
186
187/// 获取会话列表的请求
188#[derive(Debug, Clone, Default, Deserialize, Serialize)]
189pub struct ConversationsRequest {
190    /// 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
191    pub user: String,
192    /// 当前页最后面一条记录的 ID,默认 None
193    pub last_id: Option<String>,
194    /// 一次请求返回多少条记录
195    pub limit: Option<u32>,
196    /// 只返回置顶 true,只返回非置顶 false
197    pub pinned: bool,
198}
199
200/// 获取应用配置信息的请求
201#[derive(Debug, Clone, Default, Deserialize, Serialize)]
202pub struct ParametersRequest {
203    /// 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
204    pub user: String,
205}
206
207/// 获取应用Meta信息的请求
208#[derive(Debug, Clone, Default, Deserialize, Serialize)]
209pub struct MetaRequest {
210    /// 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
211    pub user: String,
212}
213
214/// 会话重命名请求
215#[derive(Debug, Clone, Default, Deserialize, Serialize)]
216pub struct ConversationsRenameRequest {
217    /// 会话 ID
218    pub conversation_id: String,
219    /// 名称,若 auto_generate 为 true 时,该参数可不传
220    pub name: Option<String>,
221    /// 自动生成标题,默认 false。
222    pub auto_generate: bool,
223    /// 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
224    pub user: String,
225}
226
227/// 删除会话请求
228#[derive(Debug, Clone, Default, Deserialize, Serialize)]
229pub struct ConversationsDeleteRequest {
230    /// 会话 ID
231    pub conversation_id: String,
232    /// 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
233    pub user: String,
234}
235
236/// 文字转语音请求
237#[derive(Debug, Clone, Default, Deserialize, Serialize)]
238pub struct TextToAudioRequest {
239    /// 语音生成内容。
240    pub text: String,
241    /// 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
242    pub user: String,
243    /// 是否启用流式输出true、false。
244    pub streaming: bool,
245}
246
247/// 语音转文字请求
248#[derive(Debug, Clone, Default)]
249pub struct AudioToTextRequest {
250    /// 语音文件。   
251    /// 支持格式:['mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'wav', 'webm'] 文件大小限制:15MB
252    pub file: Bytes,
253    /// 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
254    pub user: String,
255}
256
257/// 上传文件请求  
258#[derive(Debug, Clone, Default)]
259pub struct FilesUploadRequest {
260    /// 要上传的文件。
261    pub file: Bytes,
262    /// 用户标识,用于定义终端用户的身份,必须和发送消息接口传入 user 保持一致。
263    pub user: String,
264}
265
266/// 执行 workflow 请求
267#[derive(Debug, Clone, Default, Serialize, Deserialize)]
268pub struct WorkflowsRunRequest {
269    /// 允许传入 App 定义的各变量值。  
270    /// inputs 参数包含了多组键值对(Key/Value pairs),每组的键对应一个特定变量,每组的值则是该变量的具体值。  
271    /// 默认 {}  
272    pub inputs: HashMap<String, String>,
273    /// 响应模式  
274    /// * streaming 流式模式(推荐)。基于 SSE(Server-Sent Events)实现类似打字机输出方式的流式返回。
275    /// * blocking 阻塞模式,等待执行完毕后返回结果。(请求若流程较长可能会被中断)。  
276    /// 由于 Cloudflare 限制,请求会在 100 秒超时无返回后中断。
277    pub response_mode: ResponseMode,
278    /// 用户标识,用于定义终端用户的身份,方便检索、统计。  
279    /// 由开发者定义规则,需保证用户标识在应用内唯一。  
280    pub user: String,
281    /// 文件列表,适用于传入文件(图片)结合文本理解并回答问题,仅当模型支持 Vision 能力时可用。
282    pub files: Vec<FileInput>,
283}
284
285/// 文本生成请求
286#[derive(Debug, Clone, Default, Serialize, Deserialize)]
287pub struct CompletionMessagesRequest {
288    /// 允许传入 App 定义的各变量值。  
289    /// inputs 参数包含了多组键值对(Key/Value pairs),每组的键对应一个特定变量,每组的值则是该变量的具体值。  
290    /// 默认 {}  
291    pub inputs: HashMap<String, String>,
292    /// 响应模式  
293    /// * streaming 流式模式(推荐)。基于 SSE(Server-Sent Events)实现类似打字机输出方式的流式返回。
294    /// * blocking 阻塞模式,等待执行完毕后返回结果。(请求若流程较长可能会被中断)。  
295    /// 由于 Cloudflare 限制,请求会在 100 秒超时无返回后中断。
296    pub response_mode: ResponseMode,
297    /// 用户标识,用于定义终端用户的身份,方便检索、统计。  
298    /// 由开发者定义规则,需保证用户标识在应用内唯一。  
299    pub user: String,
300    /// 会话 ID(选填),需要基于之前的聊天记录继续对话,必须传之前消息的 conversation_id。
301    pub conversation_id: String,
302    /// 文件列表,适用于传入文件(图片)结合文本理解并回答问题,仅当模型支持 Vision 能力时可用。
303    pub files: Vec<FileInput>,
304}