runs.js 7.9 KB

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