Skip to main content

outfox_openai/spec/assistants/
step.rs

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