runs.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. "use strict";
  2. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.Runs = void 0;
  5. const tslib_1 = require("../../../../internal/tslib.js");
  6. const resource_1 = require("../../../../core/resource.js");
  7. const StepsAPI = tslib_1.__importStar(require("./steps.js"));
  8. const steps_1 = require("./steps.js");
  9. const pagination_1 = require("../../../../core/pagination.js");
  10. const headers_1 = require("../../../../internal/headers.js");
  11. const AssistantStream_1 = require("../../../../lib/AssistantStream.js");
  12. const sleep_1 = require("../../../../internal/utils/sleep.js");
  13. const path_1 = require("../../../../internal/utils/path.js");
  14. /**
  15. * @deprecated The Assistants API is deprecated in favor of the Responses API
  16. */
  17. class Runs extends resource_1.APIResource {
  18. constructor() {
  19. super(...arguments);
  20. this.steps = new StepsAPI.Steps(this._client);
  21. }
  22. create(threadID, params, options) {
  23. const { include, ...body } = params;
  24. return this._client.post((0, path_1.path) `/threads/${threadID}/runs`, {
  25. query: { include },
  26. body,
  27. ...options,
  28. headers: (0, headers_1.buildHeaders)([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
  29. stream: params.stream ?? false,
  30. });
  31. }
  32. /**
  33. * Retrieves a run.
  34. *
  35. * @deprecated The Assistants API is deprecated in favor of the Responses API
  36. */
  37. retrieve(runID, params, options) {
  38. const { thread_id } = params;
  39. return this._client.get((0, path_1.path) `/threads/${thread_id}/runs/${runID}`, {
  40. ...options,
  41. headers: (0, headers_1.buildHeaders)([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
  42. });
  43. }
  44. /**
  45. * Modifies a run.
  46. *
  47. * @deprecated The Assistants API is deprecated in favor of the Responses API
  48. */
  49. update(runID, params, options) {
  50. const { thread_id, ...body } = params;
  51. return this._client.post((0, path_1.path) `/threads/${thread_id}/runs/${runID}`, {
  52. body,
  53. ...options,
  54. headers: (0, headers_1.buildHeaders)([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
  55. });
  56. }
  57. /**
  58. * Returns a list of runs belonging to a thread.
  59. *
  60. * @deprecated The Assistants API is deprecated in favor of the Responses API
  61. */
  62. list(threadID, query = {}, options) {
  63. return this._client.getAPIList((0, path_1.path) `/threads/${threadID}/runs`, (pagination_1.CursorPage), {
  64. query,
  65. ...options,
  66. headers: (0, headers_1.buildHeaders)([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
  67. });
  68. }
  69. /**
  70. * Cancels a run that is `in_progress`.
  71. *
  72. * @deprecated The Assistants API is deprecated in favor of the Responses API
  73. */
  74. cancel(runID, params, options) {
  75. const { thread_id } = params;
  76. return this._client.post((0, path_1.path) `/threads/${thread_id}/runs/${runID}/cancel`, {
  77. ...options,
  78. headers: (0, headers_1.buildHeaders)([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
  79. });
  80. }
  81. /**
  82. * A helper to create a run an poll for a terminal state. More information on Run
  83. * lifecycles can be found here:
  84. * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
  85. */
  86. async createAndPoll(threadId, body, options) {
  87. const run = await this.create(threadId, body, options);
  88. return await this.poll(run.id, { thread_id: threadId }, options);
  89. }
  90. /**
  91. * Create a Run stream
  92. *
  93. * @deprecated use `stream` instead
  94. */
  95. createAndStream(threadId, body, options) {
  96. return AssistantStream_1.AssistantStream.createAssistantStream(threadId, this._client.beta.threads.runs, body, options);
  97. }
  98. /**
  99. * A helper to poll a run status until it reaches a terminal state. More
  100. * information on Run lifecycles can be found here:
  101. * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
  102. */
  103. async poll(runId, params, options) {
  104. const headers = (0, headers_1.buildHeaders)([
  105. options?.headers,
  106. {
  107. 'X-Stainless-Poll-Helper': 'true',
  108. 'X-Stainless-Custom-Poll-Interval': options?.pollIntervalMs?.toString() ?? undefined,
  109. },
  110. ]);
  111. while (true) {
  112. const { data: run, response } = await this.retrieve(runId, params, {
  113. ...options,
  114. headers: { ...options?.headers, ...headers },
  115. }).withResponse();
  116. switch (run.status) {
  117. //If we are in any sort of intermediate state we poll
  118. case 'queued':
  119. case 'in_progress':
  120. case 'cancelling':
  121. let sleepInterval = 5000;
  122. if (options?.pollIntervalMs) {
  123. sleepInterval = options.pollIntervalMs;
  124. }
  125. else {
  126. const headerInterval = response.headers.get('openai-poll-after-ms');
  127. if (headerInterval) {
  128. const headerIntervalMs = parseInt(headerInterval);
  129. if (!isNaN(headerIntervalMs)) {
  130. sleepInterval = headerIntervalMs;
  131. }
  132. }
  133. }
  134. await (0, sleep_1.sleep)(sleepInterval);
  135. break;
  136. //We return the run in any terminal state.
  137. case 'requires_action':
  138. case 'incomplete':
  139. case 'cancelled':
  140. case 'completed':
  141. case 'failed':
  142. case 'expired':
  143. return run;
  144. }
  145. }
  146. }
  147. /**
  148. * Create a Run stream
  149. */
  150. stream(threadId, body, options) {
  151. return AssistantStream_1.AssistantStream.createAssistantStream(threadId, this._client.beta.threads.runs, body, options);
  152. }
  153. submitToolOutputs(runID, params, options) {
  154. const { thread_id, ...body } = params;
  155. return this._client.post((0, path_1.path) `/threads/${thread_id}/runs/${runID}/submit_tool_outputs`, {
  156. body,
  157. ...options,
  158. headers: (0, headers_1.buildHeaders)([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
  159. stream: params.stream ?? false,
  160. });
  161. }
  162. /**
  163. * A helper to submit a tool output to a run and poll for a terminal run state.
  164. * More information on Run lifecycles can be found here:
  165. * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
  166. */
  167. async submitToolOutputsAndPoll(runId, params, options) {
  168. const run = await this.submitToolOutputs(runId, params, options);
  169. return await this.poll(run.id, params, options);
  170. }
  171. /**
  172. * Submit the tool outputs from a previous run and stream the run to a terminal
  173. * state. More information on Run lifecycles can be found here:
  174. * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
  175. */
  176. submitToolOutputsStream(runId, params, options) {
  177. return AssistantStream_1.AssistantStream.createToolAssistantStream(runId, this._client.beta.threads.runs, params, options);
  178. }
  179. }
  180. exports.Runs = Runs;
  181. Runs.Steps = steps_1.Steps;
  182. //# sourceMappingURL=runs.js.map