threads.mjs 3.2 KB

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