openai 4.31.0 → 4.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/README.md +21 -4
  3. package/package.json +1 -2
  4. package/resources/beta/beta.d.ts +1 -0
  5. package/resources/beta/beta.d.ts.map +1 -1
  6. package/resources/beta/beta.js.map +1 -1
  7. package/resources/beta/beta.mjs.map +1 -1
  8. package/resources/beta/index.d.ts +1 -1
  9. package/resources/beta/index.d.ts.map +1 -1
  10. package/resources/beta/index.js.map +1 -1
  11. package/resources/beta/index.mjs.map +1 -1
  12. package/resources/beta/threads/index.d.ts +2 -2
  13. package/resources/beta/threads/index.d.ts.map +1 -1
  14. package/resources/beta/threads/index.js.map +1 -1
  15. package/resources/beta/threads/index.mjs.map +1 -1
  16. package/resources/beta/threads/messages/messages.d.ts +4 -0
  17. package/resources/beta/threads/messages/messages.d.ts.map +1 -1
  18. package/resources/beta/threads/messages/messages.js.map +1 -1
  19. package/resources/beta/threads/messages/messages.mjs.map +1 -1
  20. package/resources/beta/threads/runs/index.d.ts +1 -1
  21. package/resources/beta/threads/runs/index.d.ts.map +1 -1
  22. package/resources/beta/threads/runs/index.js.map +1 -1
  23. package/resources/beta/threads/runs/index.mjs.map +1 -1
  24. package/resources/beta/threads/runs/runs.d.ts +144 -1
  25. package/resources/beta/threads/runs/runs.d.ts.map +1 -1
  26. package/resources/beta/threads/runs/runs.js +74 -1
  27. package/resources/beta/threads/runs/runs.js.map +1 -1
  28. package/resources/beta/threads/runs/runs.mjs +74 -1
  29. package/resources/beta/threads/runs/runs.mjs.map +1 -1
  30. package/resources/beta/threads/threads.d.ts +104 -0
  31. package/resources/beta/threads/threads.d.ts.map +1 -1
  32. package/resources/beta/threads/threads.js +9 -0
  33. package/resources/beta/threads/threads.js.map +1 -1
  34. package/resources/beta/threads/threads.mjs +9 -0
  35. package/resources/beta/threads/threads.mjs.map +1 -1
  36. package/src/resources/beta/beta.ts +1 -0
  37. package/src/resources/beta/index.ts +1 -0
  38. package/src/resources/beta/threads/index.ts +4 -0
  39. package/src/resources/beta/threads/messages/messages.ts +5 -0
  40. package/src/resources/beta/threads/runs/index.ts +3 -0
  41. package/src/resources/beta/threads/runs/runs.ts +223 -1
  42. package/src/resources/beta/threads/threads.ts +124 -0
  43. package/src/version.ts +1 -1
  44. package/version.d.ts +1 -1
  45. package/version.js +1 -1
  46. package/version.mjs +1 -1
@@ -5,6 +5,7 @@ import { APIPromise } from "../../../../core";
5
5
  import { APIResource } from "../../../../resource";
6
6
  import { isRequestOptions } from "../../../../core";
7
7
  import { AssistantStream, RunCreateParamsBaseStream } from "../../../../lib/AssistantStream";
8
+ import { sleep } from "../../../../core";
8
9
  import { RunSubmitToolOutputsParamsStream } from "../../../../lib/AssistantStream";
9
10
  import * as RunsAPI from "./runs";
10
11
  import * as AssistantsAPI from "../../assistants/assistants";
@@ -102,8 +103,24 @@ export class Runs extends APIResource {
102
103
  });
103
104
  }
104
105
 
106
+ /**
107
+ * A helper to create a run an poll for a terminal state. More information on Run
108
+ * lifecycles can be found here:
109
+ * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
110
+ */
111
+ async createAndPoll(
112
+ threadId: string,
113
+ body: RunCreateParamsNonStreaming,
114
+ options?: Core.RequestOptions & { pollIntervalMs?: number },
115
+ ): Promise<Run> {
116
+ const run = await this.create(threadId, body, options);
117
+ return await this.poll(threadId, run.id, options);
118
+ }
119
+
105
120
  /**
106
121
  * Create a Run stream
122
+ *
123
+ * @deprecated use `stream` instead
107
124
  */
108
125
  createAndStream(
109
126
  threadId: string,
@@ -113,6 +130,66 @@ export class Runs extends APIResource {
113
130
  return AssistantStream.createAssistantStream(threadId, this._client.beta.threads.runs, body, options);
114
131
  }
115
132
 
133
+ /**
134
+ * A helper to poll a run status until it reaches a terminal state. More
135
+ * information on Run lifecycles can be found here:
136
+ * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
137
+ */
138
+ async poll(
139
+ threadId: string,
140
+ runId: string,
141
+ options?: Core.RequestOptions & { pollIntervalMs?: number },
142
+ ): Promise<Run> {
143
+ const headers: { [key: string]: string } = { ...options?.headers, 'X-Stainless-Poll-Helper': 'true' };
144
+
145
+ if (options?.pollIntervalMs) {
146
+ headers['X-Stainless-Custom-Poll-Interval'] = options.pollIntervalMs.toString();
147
+ }
148
+
149
+ while (true) {
150
+ const { data: run, response } = await this.retrieve(threadId, runId, {
151
+ ...options,
152
+ headers: { ...options?.headers, ...headers },
153
+ }).withResponse();
154
+
155
+ switch (run.status) {
156
+ //If we are in any sort of intermediate state we poll
157
+ case 'queued':
158
+ case 'in_progress':
159
+ case 'cancelling':
160
+ let sleepInterval = 5000;
161
+
162
+ if (options?.pollIntervalMs) {
163
+ sleepInterval = options.pollIntervalMs;
164
+ } else {
165
+ const headerInterval = response.headers.get('openai-poll-after-ms');
166
+ if (headerInterval) {
167
+ const headerIntervalMs = parseInt(headerInterval);
168
+ if (!isNaN(headerIntervalMs)) {
169
+ sleepInterval = headerIntervalMs;
170
+ }
171
+ }
172
+ }
173
+ await sleep(sleepInterval);
174
+ break;
175
+ //We return the run in any terminal state.
176
+ case 'requires_action':
177
+ case 'cancelled':
178
+ case 'completed':
179
+ case 'failed':
180
+ case 'expired':
181
+ return run;
182
+ }
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Create a Run stream
188
+ */
189
+ stream(threadId: string, body: RunCreateParamsBaseStream, options?: Core.RequestOptions): AssistantStream {
190
+ return AssistantStream.createAssistantStream(threadId, this._client.beta.threads.runs, body, options);
191
+ }
192
+
116
193
  /**
117
194
  * When a run has the `status: "requires_action"` and `required_action.type` is
118
195
  * `submit_tool_outputs`, this endpoint can be used to submit the outputs from the
@@ -151,9 +228,25 @@ export class Runs extends APIResource {
151
228
  }) as APIPromise<Run> | APIPromise<Stream<AssistantsAPI.AssistantStreamEvent>>;
152
229
  }
153
230
 
231
+ /**
232
+ * A helper to submit a tool output to a run and poll for a terminal run state.
233
+ * More information on Run lifecycles can be found here:
234
+ * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
235
+ */
236
+ async submitToolOutputsAndPoll(
237
+ threadId: string,
238
+ runId: string,
239
+ body: RunSubmitToolOutputsParamsNonStreaming,
240
+ options?: Core.RequestOptions & { pollIntervalMs?: number },
241
+ ): Promise<Run> {
242
+ const run = await this.submitToolOutputs(threadId, runId, body, options);
243
+ return await this.poll(threadId, run.id, options);
244
+ }
245
+
154
246
  /**
155
247
  * Submit the tool outputs from a previous run and stream the run to a terminal
156
- * state.
248
+ * state. More information on Run lifecycles can be found here:
249
+ * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
157
250
  */
158
251
  submitToolOutputsStream(
159
252
  threadId: string,
@@ -529,6 +622,58 @@ export interface RunListParams extends CursorPageParams {
529
622
  order?: 'asc' | 'desc';
530
623
  }
531
624
 
625
+ export interface RunCreateAndPollParams {
626
+ /**
627
+ * The ID of the
628
+ * [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to
629
+ * execute this run.
630
+ */
631
+ assistant_id: string;
632
+
633
+ /**
634
+ * Appends additional instructions at the end of the instructions for the run. This
635
+ * is useful for modifying the behavior on a per-run basis without overriding other
636
+ * instructions.
637
+ */
638
+ additional_instructions?: string | null;
639
+
640
+ /**
641
+ * Overrides the
642
+ * [instructions](https://platform.openai.com/docs/api-reference/assistants/createAssistant)
643
+ * of the assistant. This is useful for modifying the behavior on a per-run basis.
644
+ */
645
+ instructions?: string | null;
646
+
647
+ /**
648
+ * Set of 16 key-value pairs that can be attached to an object. This can be useful
649
+ * for storing additional information about the object in a structured format. Keys
650
+ * can be a maximum of 64 characters long and values can be a maxium of 512
651
+ * characters long.
652
+ */
653
+ metadata?: unknown | null;
654
+
655
+ /**
656
+ * The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to
657
+ * be used to execute this run. If a value is provided here, it will override the
658
+ * model associated with the assistant. If not, the model associated with the
659
+ * assistant will be used.
660
+ */
661
+ model?: string | null;
662
+
663
+ /**
664
+ * What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
665
+ * make the output more random, while lower values like 0.2 will make it more
666
+ * focused and deterministic.
667
+ */
668
+ temperature?: number | null;
669
+
670
+ /**
671
+ * Override the tools the assistant can use for this run. This is useful for
672
+ * modifying the behavior on a per-run basis.
673
+ */
674
+ tools?: Array<AssistantsAPI.AssistantTool> | null;
675
+ }
676
+
532
677
  export interface RunCreateAndStreamParams {
533
678
  /**
534
679
  * The ID of the
@@ -581,6 +726,58 @@ export interface RunCreateAndStreamParams {
581
726
  tools?: Array<AssistantsAPI.AssistantTool> | null;
582
727
  }
583
728
 
729
+ export interface RunStreamParams {
730
+ /**
731
+ * The ID of the
732
+ * [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to
733
+ * execute this run.
734
+ */
735
+ assistant_id: string;
736
+
737
+ /**
738
+ * Appends additional instructions at the end of the instructions for the run. This
739
+ * is useful for modifying the behavior on a per-run basis without overriding other
740
+ * instructions.
741
+ */
742
+ additional_instructions?: string | null;
743
+
744
+ /**
745
+ * Overrides the
746
+ * [instructions](https://platform.openai.com/docs/api-reference/assistants/createAssistant)
747
+ * of the assistant. This is useful for modifying the behavior on a per-run basis.
748
+ */
749
+ instructions?: string | null;
750
+
751
+ /**
752
+ * Set of 16 key-value pairs that can be attached to an object. This can be useful
753
+ * for storing additional information about the object in a structured format. Keys
754
+ * can be a maximum of 64 characters long and values can be a maxium of 512
755
+ * characters long.
756
+ */
757
+ metadata?: unknown | null;
758
+
759
+ /**
760
+ * The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to
761
+ * be used to execute this run. If a value is provided here, it will override the
762
+ * model associated with the assistant. If not, the model associated with the
763
+ * assistant will be used.
764
+ */
765
+ model?: string | null;
766
+
767
+ /**
768
+ * What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
769
+ * make the output more random, while lower values like 0.2 will make it more
770
+ * focused and deterministic.
771
+ */
772
+ temperature?: number | null;
773
+
774
+ /**
775
+ * Override the tools the assistant can use for this run. This is useful for
776
+ * modifying the behavior on a per-run basis.
777
+ */
778
+ tools?: Array<AssistantsAPI.AssistantTool> | null;
779
+ }
780
+
584
781
  export type RunSubmitToolOutputsParams =
585
782
  | RunSubmitToolOutputsParamsNonStreaming
586
783
  | RunSubmitToolOutputsParamsStreaming;
@@ -635,6 +832,28 @@ export interface RunSubmitToolOutputsParamsStreaming extends RunSubmitToolOutput
635
832
  stream: true;
636
833
  }
637
834
 
835
+ export interface RunSubmitToolOutputsAndPollParams {
836
+ /**
837
+ * A list of tools for which the outputs are being submitted.
838
+ */
839
+ tool_outputs: Array<RunSubmitToolOutputsAndPollParams.ToolOutput>;
840
+ }
841
+
842
+ export namespace RunSubmitToolOutputsAndPollParams {
843
+ export interface ToolOutput {
844
+ /**
845
+ * The output of the tool call to be submitted to continue the run.
846
+ */
847
+ output?: string;
848
+
849
+ /**
850
+ * The ID of the tool call in the `required_action` object within the run object
851
+ * the output is being submitted for.
852
+ */
853
+ tool_call_id?: string;
854
+ }
855
+ }
856
+
638
857
  export interface RunSubmitToolOutputsStreamParams {
639
858
  /**
640
859
  * A list of tools for which the outputs are being submitted.
@@ -667,10 +886,13 @@ export namespace Runs {
667
886
  export import RunCreateParamsStreaming = RunsAPI.RunCreateParamsStreaming;
668
887
  export import RunUpdateParams = RunsAPI.RunUpdateParams;
669
888
  export import RunListParams = RunsAPI.RunListParams;
889
+ export import RunCreateAndPollParams = RunsAPI.RunCreateAndPollParams;
670
890
  export import RunCreateAndStreamParams = RunsAPI.RunCreateAndStreamParams;
891
+ export import RunStreamParams = RunsAPI.RunStreamParams;
671
892
  export import RunSubmitToolOutputsParams = RunsAPI.RunSubmitToolOutputsParams;
672
893
  export import RunSubmitToolOutputsParamsNonStreaming = RunsAPI.RunSubmitToolOutputsParamsNonStreaming;
673
894
  export import RunSubmitToolOutputsParamsStreaming = RunsAPI.RunSubmitToolOutputsParamsStreaming;
895
+ export import RunSubmitToolOutputsAndPollParams = RunsAPI.RunSubmitToolOutputsAndPollParams;
674
896
  export import RunSubmitToolOutputsStreamParams = RunsAPI.RunSubmitToolOutputsStreamParams;
675
897
  export import Steps = StepsAPI.Steps;
676
898
  export import CodeInterpreterLogs = StepsAPI.CodeInterpreterLogs;
@@ -92,6 +92,19 @@ export class Threads extends APIResource {
92
92
  }) as APIPromise<RunsAPI.Run> | APIPromise<Stream<AssistantsAPI.AssistantStreamEvent>>;
93
93
  }
94
94
 
95
+ /**
96
+ * A helper to create a thread, start a run and then poll for a terminal state.
97
+ * More information on Run lifecycles can be found here:
98
+ * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
99
+ */
100
+ async createAndRunPoll(
101
+ body: ThreadCreateAndRunParamsNonStreaming,
102
+ options?: Core.RequestOptions & { pollIntervalMs?: number },
103
+ ): Promise<Threads.Run> {
104
+ const run = await this.createAndRun(body, options);
105
+ return await this.runs.poll(run.thread_id, run.id, options);
106
+ }
107
+
95
108
  /**
96
109
  * Create a thread and stream the run back
97
110
  */
@@ -340,6 +353,113 @@ export interface ThreadCreateAndRunParamsStreaming extends ThreadCreateAndRunPar
340
353
  stream: true;
341
354
  }
342
355
 
356
+ export interface ThreadCreateAndRunPollParams {
357
+ /**
358
+ * The ID of the
359
+ * [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to
360
+ * execute this run.
361
+ */
362
+ assistant_id: string;
363
+
364
+ /**
365
+ * Override the default system message of the assistant. This is useful for
366
+ * modifying the behavior on a per-run basis.
367
+ */
368
+ instructions?: string | null;
369
+
370
+ /**
371
+ * Set of 16 key-value pairs that can be attached to an object. This can be useful
372
+ * for storing additional information about the object in a structured format. Keys
373
+ * can be a maximum of 64 characters long and values can be a maxium of 512
374
+ * characters long.
375
+ */
376
+ metadata?: unknown | null;
377
+
378
+ /**
379
+ * The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to
380
+ * be used to execute this run. If a value is provided here, it will override the
381
+ * model associated with the assistant. If not, the model associated with the
382
+ * assistant will be used.
383
+ */
384
+ model?: string | null;
385
+
386
+ /**
387
+ * What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
388
+ * make the output more random, while lower values like 0.2 will make it more
389
+ * focused and deterministic.
390
+ */
391
+ temperature?: number | null;
392
+
393
+ /**
394
+ * If no thread is provided, an empty thread will be created.
395
+ */
396
+ thread?: ThreadCreateAndRunPollParams.Thread;
397
+
398
+ /**
399
+ * Override the tools the assistant can use for this run. This is useful for
400
+ * modifying the behavior on a per-run basis.
401
+ */
402
+ tools?: Array<
403
+ AssistantsAPI.CodeInterpreterTool | AssistantsAPI.RetrievalTool | AssistantsAPI.FunctionTool
404
+ > | null;
405
+ }
406
+
407
+ export namespace ThreadCreateAndRunPollParams {
408
+ /**
409
+ * If no thread is provided, an empty thread will be created.
410
+ */
411
+ export interface Thread {
412
+ /**
413
+ * A list of [messages](https://platform.openai.com/docs/api-reference/messages) to
414
+ * start the thread with.
415
+ */
416
+ messages?: Array<Thread.Message>;
417
+
418
+ /**
419
+ * Set of 16 key-value pairs that can be attached to an object. This can be useful
420
+ * for storing additional information about the object in a structured format. Keys
421
+ * can be a maximum of 64 characters long and values can be a maxium of 512
422
+ * characters long.
423
+ */
424
+ metadata?: unknown | null;
425
+ }
426
+
427
+ export namespace Thread {
428
+ export interface Message {
429
+ /**
430
+ * The content of the message.
431
+ */
432
+ content: string;
433
+
434
+ /**
435
+ * The role of the entity that is creating the message. Allowed values include:
436
+ *
437
+ * - `user`: Indicates the message is sent by an actual user and should be used in
438
+ * most cases to represent user-generated messages.
439
+ * - `assistant`: Indicates the message is generated by the assistant. Use this
440
+ * value to insert messages from the assistant into the conversation.
441
+ */
442
+ role: 'user' | 'assistant';
443
+
444
+ /**
445
+ * A list of [File](https://platform.openai.com/docs/api-reference/files) IDs that
446
+ * the message should use. There can be a maximum of 10 files attached to a
447
+ * message. Useful for tools like `retrieval` and `code_interpreter` that can
448
+ * access and use files.
449
+ */
450
+ file_ids?: Array<string>;
451
+
452
+ /**
453
+ * Set of 16 key-value pairs that can be attached to an object. This can be useful
454
+ * for storing additional information about the object in a structured format. Keys
455
+ * can be a maximum of 64 characters long and values can be a maxium of 512
456
+ * characters long.
457
+ */
458
+ metadata?: unknown | null;
459
+ }
460
+ }
461
+ }
462
+
343
463
  export interface ThreadCreateAndRunStreamParams {
344
464
  /**
345
465
  * The ID of the
@@ -455,6 +575,7 @@ export namespace Threads {
455
575
  export import ThreadCreateAndRunParams = ThreadsAPI.ThreadCreateAndRunParams;
456
576
  export import ThreadCreateAndRunParamsNonStreaming = ThreadsAPI.ThreadCreateAndRunParamsNonStreaming;
457
577
  export import ThreadCreateAndRunParamsStreaming = ThreadsAPI.ThreadCreateAndRunParamsStreaming;
578
+ export import ThreadCreateAndRunPollParams = ThreadsAPI.ThreadCreateAndRunPollParams;
458
579
  export import ThreadCreateAndRunStreamParams = ThreadsAPI.ThreadCreateAndRunStreamParams;
459
580
  export import Runs = RunsAPI.Runs;
460
581
  export import RequiredActionFunctionToolCall = RunsAPI.RequiredActionFunctionToolCall;
@@ -466,10 +587,13 @@ export namespace Threads {
466
587
  export import RunCreateParamsStreaming = RunsAPI.RunCreateParamsStreaming;
467
588
  export import RunUpdateParams = RunsAPI.RunUpdateParams;
468
589
  export import RunListParams = RunsAPI.RunListParams;
590
+ export import RunCreateAndPollParams = RunsAPI.RunCreateAndPollParams;
469
591
  export import RunCreateAndStreamParams = RunsAPI.RunCreateAndStreamParams;
592
+ export import RunStreamParams = RunsAPI.RunStreamParams;
470
593
  export import RunSubmitToolOutputsParams = RunsAPI.RunSubmitToolOutputsParams;
471
594
  export import RunSubmitToolOutputsParamsNonStreaming = RunsAPI.RunSubmitToolOutputsParamsNonStreaming;
472
595
  export import RunSubmitToolOutputsParamsStreaming = RunsAPI.RunSubmitToolOutputsParamsStreaming;
596
+ export import RunSubmitToolOutputsAndPollParams = RunsAPI.RunSubmitToolOutputsAndPollParams;
473
597
  export import RunSubmitToolOutputsStreamParams = RunsAPI.RunSubmitToolOutputsStreamParams;
474
598
  export import Messages = MessagesAPI.Messages;
475
599
  export import Annotation = MessagesAPI.Annotation;
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '4.31.0'; // x-release-please-version
1
+ export const VERSION = '4.32.0'; // x-release-please-version
package/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "4.31.0";
1
+ export declare const VERSION = "4.32.0";
2
2
  //# sourceMappingURL=version.d.ts.map
package/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = '4.31.0'; // x-release-please-version
4
+ exports.VERSION = '4.32.0'; // x-release-please-version
5
5
  //# sourceMappingURL=version.js.map
package/version.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export const VERSION = '4.31.0'; // x-release-please-version
1
+ export const VERSION = '4.32.0'; // x-release-please-version
2
2
  //# sourceMappingURL=version.mjs.map