runs.mjs 6.6 KB

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