async_openai/types/
step.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5use super::{FileSearchRankingOptions, ImageFile, LastError, RunStatus};
6
7#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
8#[serde(rename_all = "snake_case")]
9pub enum RunStepType {
10    MessageCreation,
11    ToolCalls,
12}
13
14/// Represents a step in execution of a run.
15#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
16pub struct RunStepObject {
17    /// The identifier, which can be referenced in API endpoints.
18    pub id: String,
19    /// The object type, which is always `thread.run.step`.
20    pub object: String,
21    /// The Unix timestamp (in seconds) for when the run step was created.
22    pub created_at: i32,
23
24    /// The ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) associated with the run step.
25    pub assistant_id: Option<String>,
26
27    /// The ID of the [thread](https://platform.openai.com/docs/api-reference/threads) that was run.
28    pub thread_id: String,
29
30    ///  The ID of the [run](https://platform.openai.com/docs/api-reference/runs) that this run step is a part of.
31    pub run_id: String,
32
33    /// The type of run step, which can be either `message_creation` or `tool_calls`.
34    pub r#type: RunStepType,
35
36    /// The status of the run step, which can be either `in_progress`, `cancelled`, `failed`, `completed`, or `expired`.
37    pub status: RunStatus,
38
39    /// The details of the run step.
40    pub step_details: StepDetails,
41
42    /// The last error associated with this run. Will be `null` if there are no errors.
43    pub last_error: Option<LastError>,
44
45    ///The Unix timestamp (in seconds) for when the run step expired. A step is considered expired if the parent run is expired.
46    pub expires_at: Option<i32>,
47
48    /// The Unix timestamp (in seconds) for when the run step was cancelled.
49    pub cancelled_at: Option<i32>,
50
51    /// The Unix timestamp (in seconds) for when the run step failed.
52    pub failed_at: Option<i32>,
53
54    /// The Unix timestamp (in seconds) for when the run step completed.
55    pub completed_at: Option<i32>,
56
57    pub metadata: Option<HashMap<String, serde_json::Value>>,
58
59    /// Usage statistics related to the run step. This value will be `null` while the run step's status is `in_progress`.
60    pub usage: Option<RunStepCompletionUsage>,
61}
62
63#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
64pub struct RunStepCompletionUsage {
65    /// Number of completion tokens used over the course of the run step.
66    pub completion_tokens: u32,
67    /// Number of prompt tokens used over the course of the run step.
68    pub prompt_tokens: u32,
69    /// Total number of tokens used (prompt + completion).
70    pub total_tokens: u32,
71}
72
73#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
74#[serde(tag = "type")]
75#[serde(rename_all = "snake_case")]
76pub enum StepDetails {
77    MessageCreation(RunStepDetailsMessageCreationObject),
78    ToolCalls(RunStepDetailsToolCallsObject),
79}
80
81/// Details of the message creation by the run step.
82#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
83pub struct RunStepDetailsMessageCreationObject {
84    pub message_creation: MessageCreation,
85}
86
87#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
88pub struct MessageCreation {
89    /// The ID of the message that was created by this run step.
90    pub message_id: String,
91}
92
93/// Details of the tool call.
94#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
95pub struct RunStepDetailsToolCallsObject {
96    /// An array of tool calls the run step was involved in. These can be associated with one of three types of tools: `code_interpreter`, `file_search`, or `function`.
97    pub tool_calls: Vec<RunStepDetailsToolCalls>,
98}
99
100#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
101#[serde(tag = "type")]
102#[serde(rename_all = "snake_case")]
103pub enum RunStepDetailsToolCalls {
104    /// Details of the Code Interpreter tool call the run step was involved in.
105    CodeInterpreter(RunStepDetailsToolCallsCodeObject),
106    FileSearch(RunStepDetailsToolCallsFileSearchObject),
107    Function(RunStepDetailsToolCallsFunctionObject),
108}
109
110/// Code interpreter tool call
111#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
112pub struct RunStepDetailsToolCallsCodeObject {
113    /// The ID of the tool call.
114    pub id: String,
115
116    /// The Code Interpreter tool call definition.
117    pub code_interpreter: CodeInterpreter,
118}
119
120#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
121pub struct CodeInterpreter {
122    /// The input to the Code Interpreter tool call.
123    pub input: String,
124    /// The outputs from the Code Interpreter tool call. Code Interpreter can output one or more items, including text (`logs`) or images (`image`). Each of these are represented by a different object type.
125    pub outputs: Vec<CodeInterpreterOutput>,
126}
127
128#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
129#[serde(tag = "type")]
130#[serde(rename_all = "lowercase")]
131pub enum CodeInterpreterOutput {
132    /// Code interpreter log output
133    Logs(RunStepDetailsToolCallsCodeOutputLogsObject),
134    /// Code interpreter image output
135    Image(RunStepDetailsToolCallsCodeOutputImageObject),
136}
137
138/// Text output from the Code Interpreter tool call as part of a run step.
139#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
140pub struct RunStepDetailsToolCallsCodeOutputLogsObject {
141    /// The text output from the Code Interpreter tool call.
142    pub logs: String,
143}
144
145#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
146pub struct RunStepDetailsToolCallsCodeOutputImageObject {
147    /// The [file](https://platform.openai.com/docs/api-reference/files) ID of the image.
148    pub image: ImageFile,
149}
150
151/// File search tool call
152#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
153pub struct RunStepDetailsToolCallsFileSearchObject {
154    /// The ID of the tool call object.
155    pub id: String,
156    /// For now, this is always going to be an empty object.
157    pub file_search: RunStepDetailsToolCallsFileSearchObjectFileSearch,
158}
159
160#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
161pub struct RunStepDetailsToolCallsFileSearchObjectFileSearch {
162    pub ranking_options: Option<FileSearchRankingOptions>,
163    /// The results of the file search.
164    pub results: Option<Vec<RunStepDetailsToolCallsFileSearchResultObject>>,
165}
166
167/// A result instance of the file search.
168#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
169pub struct RunStepDetailsToolCallsFileSearchResultObject {
170    /// The ID of the file that result was found in.
171    pub file_id: String,
172    /// The name of the file that result was found in.
173    pub file_name: String,
174    /// The score of the result. All values must be a floating point number between 0 and 1.
175    pub score: f32,
176    /// The content of the result that was found. The content is only included if requested via the include query parameter.
177    pub content: Option<Vec<RunStepDetailsToolCallsFileSearchResultObjectContent>>,
178}
179
180#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
181pub struct RunStepDetailsToolCallsFileSearchResultObjectContent {
182    // note: type is text hence omitted from struct
183    /// The text content of the file.
184    pub text: Option<String>,
185}
186
187#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
188pub struct RunStepDetailsToolCallsFunctionObject {
189    /// The ID of the tool call object.
190    pub id: String,
191    /// he definition of the function that was called.
192    pub function: RunStepFunctionObject,
193}
194
195#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
196pub struct RunStepFunctionObject {
197    /// The name of the function.
198    pub name: String,
199    /// The arguments passed to the function.
200    pub arguments: String,
201    /// The output of the function. This will be `null` if the outputs have not been [submitted](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs) yet.
202    pub output: Option<String>,
203}
204
205#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
206pub struct RunStepFunctionObjectDelta {
207    /// The name of the function.
208    pub name: Option<String>,
209    /// The arguments passed to the function.
210    pub arguments: Option<String>,
211    /// The output of the function. This will be `null` if the outputs have not been [submitted](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs) yet.
212    pub output: Option<String>,
213}
214
215#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
216pub struct ListRunStepsResponse {
217    pub object: String,
218    pub data: Vec<RunStepObject>,
219    pub first_id: Option<String>,
220    pub last_id: Option<String>,
221    pub has_more: bool,
222}
223
224/// Represents a run step delta i.e. any changed fields on a run step during streaming.
225#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
226pub struct RunStepDeltaObject {
227    /// The identifier of the run step, which can be referenced in API endpoints.
228    pub id: String,
229    /// The object type, which is always `thread.run.step.delta`.
230    pub object: String,
231    /// The delta containing the fields that have changed on the run step.
232    pub delta: RunStepDelta,
233}
234
235#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
236pub struct RunStepDelta {
237    pub step_details: DeltaStepDetails,
238}
239
240#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
241#[serde(tag = "type")]
242#[serde(rename_all = "snake_case")]
243pub enum DeltaStepDetails {
244    MessageCreation(RunStepDeltaStepDetailsMessageCreationObject),
245    ToolCalls(RunStepDeltaStepDetailsToolCallsObject),
246}
247
248/// Details of the message creation by the run step.
249#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
250pub struct RunStepDeltaStepDetailsMessageCreationObject {
251    pub message_creation: Option<MessageCreation>,
252}
253
254/// Details of the tool call.
255#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
256pub struct RunStepDeltaStepDetailsToolCallsObject {
257    /// An array of tool calls the run step was involved in. These can be associated with one of three types of tools: `code_interpreter`, `file_search`, or `function`.
258    pub tool_calls: Option<Vec<RunStepDeltaStepDetailsToolCalls>>,
259}
260
261#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
262#[serde(tag = "type")]
263#[serde(rename_all = "snake_case")]
264pub enum RunStepDeltaStepDetailsToolCalls {
265    CodeInterpreter(RunStepDeltaStepDetailsToolCallsCodeObject),
266    FileSearch(RunStepDeltaStepDetailsToolCallsFileSearchObject),
267    Function(RunStepDeltaStepDetailsToolCallsFunctionObject),
268}
269
270/// Details of the Code Interpreter tool call the run step was involved in.
271#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
272pub struct RunStepDeltaStepDetailsToolCallsCodeObject {
273    /// The index of the tool call in the tool calls array.
274    pub index: u32,
275    /// The ID of the tool call.
276    pub id: Option<String>,
277    /// The Code Interpreter tool call definition.
278    pub code_interpreter: Option<DeltaCodeInterpreter>,
279}
280
281#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
282pub struct DeltaCodeInterpreter {
283    /// The input to the Code Interpreter tool call.
284    pub input: Option<String>,
285    /// The outputs from the Code Interpreter tool call. Code Interpreter can output one or more items, including text (`logs`) or images (`image`). Each of these are represented by a different object type.
286    pub outputs: Option<Vec<DeltaCodeInterpreterOutput>>,
287}
288
289#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
290#[serde(tag = "type")]
291#[serde(rename_all = "lowercase")]
292pub enum DeltaCodeInterpreterOutput {
293    Logs(RunStepDeltaStepDetailsToolCallsCodeOutputLogsObject),
294    Image(RunStepDeltaStepDetailsToolCallsCodeOutputImageObject),
295}
296
297/// Text output from the Code Interpreter tool call as part of a run step.
298#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
299pub struct RunStepDeltaStepDetailsToolCallsCodeOutputLogsObject {
300    /// The index of the output in the outputs array.
301    pub index: u32,
302    /// The text output from the Code Interpreter tool call.
303    pub logs: Option<String>,
304}
305
306/// Code interpreter image output
307#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
308pub struct RunStepDeltaStepDetailsToolCallsCodeOutputImageObject {
309    /// The index of the output in the outputs array.
310    pub index: u32,
311
312    pub image: Option<ImageFile>,
313}
314
315#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
316pub struct RunStepDeltaStepDetailsToolCallsFileSearchObject {
317    /// The index of the tool call in the tool calls array.
318    pub index: u32,
319    /// The ID of the tool call object.
320    pub id: Option<String>,
321    /// For now, this is always going to be an empty object.
322    pub file_search: Option<serde_json::Value>,
323}
324
325/// Function tool call
326#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
327pub struct RunStepDeltaStepDetailsToolCallsFunctionObject {
328    /// The index of the tool call in the tool calls array.
329    pub index: u32,
330    /// The ID of the tool call object.
331    pub id: Option<String>,
332    /// The definition of the function that was called.
333    pub function: Option<RunStepFunctionObjectDelta>,
334}