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