forked from openai/openai-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruns.ts
468 lines (411 loc) · 12.6 KB
/
runs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// File generated from our OpenAPI spec by Stainless.
import * as Core from 'openai/core';
import { APIResource } from 'openai/resource';
import { isRequestOptions } from 'openai/core';
import * as RunsAPI from 'openai/resources/beta/threads/runs/runs';
import * as Shared from 'openai/resources/shared';
import * as StepsAPI from 'openai/resources/beta/threads/runs/steps';
import { CursorPage, type CursorPageParams } from 'openai/pagination';
export class Runs extends APIResource {
steps: StepsAPI.Steps = new StepsAPI.Steps(this.client);
/**
* Create a run.
*/
create(threadId: string, body: RunCreateParams, options?: Core.RequestOptions): Core.APIPromise<Run> {
return this.post(`/threads/${threadId}/runs`, {
body,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
}
/**
* Retrieves a run.
*/
retrieve(threadId: string, runId: string, options?: Core.RequestOptions): Core.APIPromise<Run> {
return this.get(`/threads/${threadId}/runs/${runId}`, {
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
}
/**
* Modifies a run.
*/
update(
threadId: string,
runId: string,
body: RunUpdateParams,
options?: Core.RequestOptions,
): Core.APIPromise<Run> {
return this.post(`/threads/${threadId}/runs/${runId}`, {
body,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
}
/**
* Returns a list of runs belonging to a thread.
*/
list(
threadId: string,
query?: RunListParams,
options?: Core.RequestOptions,
): Core.PagePromise<RunsPage, Run>;
list(threadId: string, options?: Core.RequestOptions): Core.PagePromise<RunsPage, Run>;
list(
threadId: string,
query: RunListParams | Core.RequestOptions = {},
options?: Core.RequestOptions,
): Core.PagePromise<RunsPage, Run> {
if (isRequestOptions(query)) {
return this.list(threadId, {}, query);
}
return this.getAPIList(`/threads/${threadId}/runs`, RunsPage, {
query,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
}
/**
* Cancels a run that is `in_progress`.
*/
cancel(threadId: string, runId: string, options?: Core.RequestOptions): Core.APIPromise<Run> {
return this.post(`/threads/${threadId}/runs/${runId}/cancel`, {
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
}
/**
* When a run has the `status: "requires_action"` and `required_action.type` is
* `submit_tool_outputs`, this endpoint can be used to submit the outputs from the
* tool calls once they're all completed. All outputs must be submitted in a single
* request.
*/
submitToolOutputs(
threadId: string,
runId: string,
body: RunSubmitToolOutputsParams,
options?: Core.RequestOptions,
): Core.APIPromise<Run> {
return this.post(`/threads/${threadId}/runs/${runId}/submit_tool_outputs`, {
body,
...options,
headers: { 'OpenAI-Beta': 'assistants=v1', ...options?.headers },
});
}
}
export class RunsPage extends CursorPage<Run> {}
/**
* Tool call objects
*/
export interface RequiredActionFunctionToolCall {
/**
* The ID of the tool call. This ID must be referenced when you submit the tool
* outputs in using the
* [Submit tool outputs to run](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs)
* endpoint.
*/
id: string;
/**
* The function definition.
*/
function: RequiredActionFunctionToolCall.Function;
/**
* The type of tool call the output is required for. For now, this is always
* `function`.
*/
type: 'function';
}
export namespace RequiredActionFunctionToolCall {
/**
* The function definition.
*/
export interface Function {
/**
* The arguments that the model expects you to pass to the function.
*/
arguments: string;
/**
* The name of the function.
*/
name: string;
}
}
/**
* Represents an execution run on a
* [thread](https://platform.openai.com/docs/api-reference/threads).
*/
export interface Run {
/**
* The identifier, which can be referenced in API endpoints.
*/
id: string;
/**
* The ID of the
* [assistant](https://platform.openai.com/docs/api-reference/assistants) used for
* execution of this run.
*/
assistant_id: string;
/**
* The Unix timestamp (in seconds) for when the run was cancelled.
*/
cancelled_at: number | null;
/**
* The Unix timestamp (in seconds) for when the run was completed.
*/
completed_at: number | null;
/**
* The Unix timestamp (in seconds) for when the run was created.
*/
created_at: number;
/**
* The Unix timestamp (in seconds) for when the run will expire.
*/
expires_at: number;
/**
* The Unix timestamp (in seconds) for when the run failed.
*/
failed_at: number | null;
/**
* The list of [File](https://platform.openai.com/docs/api-reference/files) IDs the
* [assistant](https://platform.openai.com/docs/api-reference/assistants) used for
* this run.
*/
file_ids: Array<string>;
/**
* The instructions that the
* [assistant](https://platform.openai.com/docs/api-reference/assistants) used for
* this run.
*/
instructions: string;
/**
* The last error associated with this run. Will be `null` if there are no errors.
*/
last_error: Run.LastError | null;
/**
* Set of 16 key-value pairs that can be attached to an object. This can be useful
* for storing additional information about the object in a structured format. Keys
* can be a maximum of 64 characters long and values can be a maxium of 512
* characters long.
*/
metadata: unknown | null;
/**
* The model that the
* [assistant](https://platform.openai.com/docs/api-reference/assistants) used for
* this run.
*/
model: string;
/**
* The object type, which is always `thread.run`.
*/
object: 'thread.run';
/**
* Details on the action required to continue the run. Will be `null` if no action
* is required.
*/
required_action: Run.RequiredAction | null;
/**
* The Unix timestamp (in seconds) for when the run was started.
*/
started_at: number | null;
/**
* The status of the run, which can be either `queued`, `in_progress`,
* `requires_action`, `cancelling`, `cancelled`, `failed`, `completed`, or
* `expired`.
*/
status:
| 'queued'
| 'in_progress'
| 'requires_action'
| 'cancelling'
| 'cancelled'
| 'failed'
| 'completed'
| 'expired';
/**
* The ID of the [thread](https://platform.openai.com/docs/api-reference/threads)
* that was executed on as a part of this run.
*/
thread_id: string;
/**
* The list of tools that the
* [assistant](https://platform.openai.com/docs/api-reference/assistants) used for
* this run.
*/
tools: Array<Run.AssistantToolsCode | Run.AssistantToolsRetrieval | Run.AssistantToolsFunction>;
}
export namespace Run {
/**
* The last error associated with this run. Will be `null` if there are no errors.
*/
export interface LastError {
/**
* One of `server_error` or `rate_limit_exceeded`.
*/
code: 'server_error' | 'rate_limit_exceeded';
/**
* A human-readable description of the error.
*/
message: string;
}
/**
* Details on the action required to continue the run. Will be `null` if no action
* is required.
*/
export interface RequiredAction {
/**
* Details on the tool outputs needed for this run to continue.
*/
submit_tool_outputs: RequiredAction.SubmitToolOutputs;
/**
* For now, this is always `submit_tool_outputs`.
*/
type: 'submit_tool_outputs';
}
export namespace RequiredAction {
/**
* Details on the tool outputs needed for this run to continue.
*/
export interface SubmitToolOutputs {
/**
* A list of the relevant tool calls.
*/
tool_calls: Array<RunsAPI.RequiredActionFunctionToolCall>;
}
}
export interface AssistantToolsCode {
/**
* The type of tool being defined: `code_interpreter`
*/
type: 'code_interpreter';
}
export interface AssistantToolsRetrieval {
/**
* The type of tool being defined: `retrieval`
*/
type: 'retrieval';
}
export interface AssistantToolsFunction {
function: Shared.FunctionDefinition;
/**
* The type of tool being defined: `function`
*/
type: 'function';
}
}
export interface RunCreateParams {
/**
* The ID of the
* [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to
* execute this run.
*/
assistant_id: string;
/**
* Override the default system message of the assistant. This is useful for
* modifying the behavior on a per-run basis.
*/
instructions?: string | null;
/**
* Set of 16 key-value pairs that can be attached to an object. This can be useful
* for storing additional information about the object in a structured format. Keys
* can be a maximum of 64 characters long and values can be a maxium of 512
* characters long.
*/
metadata?: unknown | null;
/**
* The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to
* be used to execute this run. If a value is provided here, it will override the
* model associated with the assistant. If not, the model associated with the
* assistant will be used.
*/
model?: string | null;
/**
* Override the tools the assistant can use for this run. This is useful for
* modifying the behavior on a per-run basis.
*/
tools?: Array<
| RunCreateParams.AssistantToolsCode
| RunCreateParams.AssistantToolsRetrieval
| RunCreateParams.AssistantToolsFunction
> | null;
}
export namespace RunCreateParams {
export interface AssistantToolsCode {
/**
* The type of tool being defined: `code_interpreter`
*/
type: 'code_interpreter';
}
export interface AssistantToolsRetrieval {
/**
* The type of tool being defined: `retrieval`
*/
type: 'retrieval';
}
export interface AssistantToolsFunction {
function: Shared.FunctionDefinition;
/**
* The type of tool being defined: `function`
*/
type: 'function';
}
}
export interface RunUpdateParams {
/**
* Set of 16 key-value pairs that can be attached to an object. This can be useful
* for storing additional information about the object in a structured format. Keys
* can be a maximum of 64 characters long and values can be a maxium of 512
* characters long.
*/
metadata?: unknown | null;
}
export interface RunListParams extends CursorPageParams {
/**
* A cursor for use in pagination. `before` is an object ID that defines your place
* in the list. For instance, if you make a list request and receive 100 objects,
* ending with obj_foo, your subsequent call can include before=obj_foo in order to
* fetch the previous page of the list.
*/
before?: string;
/**
* Sort order by the `created_at` timestamp of the objects. `asc` for ascending
* order and `desc` for descending order.
*/
order?: 'asc' | 'desc';
}
export interface RunSubmitToolOutputsParams {
/**
* A list of tools for which the outputs are being submitted.
*/
tool_outputs: Array<RunSubmitToolOutputsParams.ToolOutput>;
}
export namespace RunSubmitToolOutputsParams {
export interface ToolOutput {
/**
* The output of the tool call to be submitted to continue the run.
*/
output?: string;
/**
* The ID of the tool call in the `required_action` object within the run object
* the output is being submitted for.
*/
tool_call_id?: string;
}
}
export namespace Runs {
export import RequiredActionFunctionToolCall = RunsAPI.RequiredActionFunctionToolCall;
export import Run = RunsAPI.Run;
export import RunsPage = RunsAPI.RunsPage;
export import RunCreateParams = RunsAPI.RunCreateParams;
export import RunUpdateParams = RunsAPI.RunUpdateParams;
export import RunListParams = RunsAPI.RunListParams;
export import RunSubmitToolOutputsParams = RunsAPI.RunSubmitToolOutputsParams;
export import Steps = StepsAPI.Steps;
export import CodeToolCall = StepsAPI.CodeToolCall;
export import FunctionToolCall = StepsAPI.FunctionToolCall;
export import MessageCreationStepDetails = StepsAPI.MessageCreationStepDetails;
export import RetrievalToolCall = StepsAPI.RetrievalToolCall;
export import RunStep = StepsAPI.RunStep;
export import ToolCallsStepDetails = StepsAPI.ToolCallsStepDetails;
export import RunStepsPage = StepsAPI.RunStepsPage;
export import StepListParams = StepsAPI.StepListParams;
}