threads.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 * as MessagesAPI from "./messages.mjs";
  6. import { Messages, MessagesPage, } from "./messages.mjs";
  7. import * as RunsAPI from "./runs/runs.mjs";
  8. import { Runs, RunsPage, } from "./runs/runs.mjs";
  9. /**
  10. * @deprecated The Assistants API is deprecated in favor of the Responses API
  11. */
  12. export class Threads extends APIResource {
  13. constructor() {
  14. super(...arguments);
  15. this.runs = new RunsAPI.Runs(this._client);
  16. this.messages = new MessagesAPI.Messages(this._client);
  17. }
  18. create(body = {}, options) {
  19. if (isRequestOptions(body)) {
  20. return this.create({}, body);
  21. }
  22. return this._client.post('/threads', {
  23. body,
  24. ...options,
  25. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  26. });
  27. }
  28. /**
  29. * Retrieves a thread.
  30. *
  31. * @deprecated The Assistants API is deprecated in favor of the Responses API
  32. */
  33. retrieve(threadId, options) {
  34. return this._client.get(`/threads/${threadId}`, {
  35. ...options,
  36. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  37. });
  38. }
  39. /**
  40. * Modifies a thread.
  41. *
  42. * @deprecated The Assistants API is deprecated in favor of the Responses API
  43. */
  44. update(threadId, body, options) {
  45. return this._client.post(`/threads/${threadId}`, {
  46. body,
  47. ...options,
  48. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  49. });
  50. }
  51. /**
  52. * Delete a thread.
  53. *
  54. * @deprecated The Assistants API is deprecated in favor of the Responses API
  55. */
  56. del(threadId, options) {
  57. return this._client.delete(`/threads/${threadId}`, {
  58. ...options,
  59. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  60. });
  61. }
  62. createAndRun(body, options) {
  63. return this._client.post('/threads/runs', {
  64. body,
  65. ...options,
  66. headers: { 'OpenAI-Beta': 'assistants=v2', ...options?.headers },
  67. stream: body.stream ?? false,
  68. });
  69. }
  70. /**
  71. * A helper to create a thread, start a run and then poll for a terminal state.
  72. * More information on Run lifecycles can be found here:
  73. * https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
  74. */
  75. async createAndRunPoll(body, options) {
  76. const run = await this.createAndRun(body, options);
  77. return await this.runs.poll(run.thread_id, run.id, options);
  78. }
  79. /**
  80. * Create a thread and stream the run back
  81. */
  82. createAndRunStream(body, options) {
  83. return AssistantStream.createThreadAssistantStream(body, this._client.beta.threads, options);
  84. }
  85. }
  86. Threads.Runs = Runs;
  87. Threads.RunsPage = RunsPage;
  88. Threads.Messages = Messages;
  89. Threads.MessagesPage = MessagesPage;
  90. //# sourceMappingURL=threads.mjs.map