threads.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.Threads = 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 MessagesAPI = __importStar(require("./messages.js"));
  32. const messages_1 = require("./messages.js");
  33. const RunsAPI = __importStar(require("./runs/runs.js"));
  34. const runs_1 = require("./runs/runs.js");
  35. /**
  36. * @deprecated The Assistants API is deprecated in favor of the Responses API
  37. */
  38. class Threads extends resource_1.APIResource {
  39. constructor() {
  40. super(...arguments);
  41. this.runs = new RunsAPI.Runs(this._client);
  42. this.messages = new MessagesAPI.Messages(this._client);
  43. }
  44. create(body = {}, options) {
  45. if ((0, core_1.isRequestOptions)(body)) {
  46. return this.create({}, body);
  47. }
  48. return this._client.post('/threads', {
  49. body,
  50. ...options,
  51. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  52. });
  53. }
  54. /**
  55. * Retrieves a thread.
  56. *
  57. * @deprecated The Assistants API is deprecated in favor of the Responses API
  58. */
  59. retrieve(threadId, options) {
  60. return this._client.get(`/threads/${threadId}`, {
  61. ...options,
  62. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  63. });
  64. }
  65. /**
  66. * Modifies a thread.
  67. *
  68. * @deprecated The Assistants API is deprecated in favor of the Responses API
  69. */
  70. update(threadId, body, options) {
  71. return this._client.post(`/threads/${threadId}`, {
  72. body,
  73. ...options,
  74. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  75. });
  76. }
  77. /**
  78. * Delete a thread.
  79. *
  80. * @deprecated The Assistants API is deprecated in favor of the Responses API
  81. */
  82. del(threadId, options) {
  83. return this._client.delete(`/threads/${threadId}`, {
  84. ...options,
  85. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  86. });
  87. }
  88. createAndRun(body, options) {
  89. return this._client.post('/threads/runs', {
  90. body,
  91. ...options,
  92. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  93. stream: body.stream ?? false,
  94. });
  95. }
  96. /**
  97. * A helper to create a thread, start a run and then poll for a terminal state.
  98. * More information on Run lifecycles can be found here:
  99. * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
  100. */
  101. async createAndRunPoll(body, options) {
  102. const run = await this.createAndRun(body, options);
  103. return await this.runs.poll(run.thread_id, run.id, options);
  104. }
  105. /**
  106. * Create a thread and stream the run back
  107. */
  108. createAndRunStream(body, options) {
  109. return AssistantStream_1.AssistantStream.createThreadAssistantStream(body, this._client.beta.threads, options);
  110. }
  111. }
  112. exports.Threads = Threads;
  113. Threads.Runs = runs_1.Runs;
  114. Threads.RunsPage = runs_1.RunsPage;
  115. Threads.Messages = messages_1.Messages;
  116. Threads.MessagesPage = messages_1.MessagesPage;
  117. //# sourceMappingURL=threads.js.map