files.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. "use strict";
  2. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.Files = void 0;
  5. const resource_1 = require("../../core/resource.js");
  6. const pagination_1 = require("../../core/pagination.js");
  7. const headers_1 = require("../../internal/headers.js");
  8. const utils_1 = require("../../internal/utils.js");
  9. const path_1 = require("../../internal/utils/path.js");
  10. class Files extends resource_1.APIResource {
  11. /**
  12. * Create a vector store file by attaching a
  13. * [File](https://platform.openai.com/docs/api-reference/files) to a
  14. * [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object).
  15. */
  16. create(vectorStoreID, body, options) {
  17. return this._client.post((0, path_1.path) `/vector_stores/${vectorStoreID}/files`, {
  18. body,
  19. ...options,
  20. headers: (0, headers_1.buildHeaders)([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
  21. });
  22. }
  23. /**
  24. * Retrieves a vector store file.
  25. */
  26. retrieve(fileID, params, options) {
  27. const { vector_store_id } = params;
  28. return this._client.get((0, path_1.path) `/vector_stores/${vector_store_id}/files/${fileID}`, {
  29. ...options,
  30. headers: (0, headers_1.buildHeaders)([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
  31. });
  32. }
  33. /**
  34. * Update attributes on a vector store file.
  35. */
  36. update(fileID, params, options) {
  37. const { vector_store_id, ...body } = params;
  38. return this._client.post((0, path_1.path) `/vector_stores/${vector_store_id}/files/${fileID}`, {
  39. body,
  40. ...options,
  41. headers: (0, headers_1.buildHeaders)([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
  42. });
  43. }
  44. /**
  45. * Returns a list of vector store files.
  46. */
  47. list(vectorStoreID, query = {}, options) {
  48. return this._client.getAPIList((0, path_1.path) `/vector_stores/${vectorStoreID}/files`, (pagination_1.CursorPage), {
  49. query,
  50. ...options,
  51. headers: (0, headers_1.buildHeaders)([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
  52. });
  53. }
  54. /**
  55. * Delete a vector store file. This will remove the file from the vector store but
  56. * the file itself will not be deleted. To delete the file, use the
  57. * [delete file](https://platform.openai.com/docs/api-reference/files/delete)
  58. * endpoint.
  59. */
  60. delete(fileID, params, options) {
  61. const { vector_store_id } = params;
  62. return this._client.delete((0, path_1.path) `/vector_stores/${vector_store_id}/files/${fileID}`, {
  63. ...options,
  64. headers: (0, headers_1.buildHeaders)([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]),
  65. });
  66. }
  67. /**
  68. * Attach a file to the given vector store and wait for it to be processed.
  69. */
  70. async createAndPoll(vectorStoreId, body, options) {
  71. const file = await this.create(vectorStoreId, body, options);
  72. return await this.poll(vectorStoreId, file.id, options);
  73. }
  74. /**
  75. * Wait for the vector store file to finish processing.
  76. *
  77. * Note: this will return even if the file failed to process, you need to check
  78. * file.last_error and file.status to handle these cases
  79. */
  80. async poll(vectorStoreID, fileID, options) {
  81. const headers = (0, headers_1.buildHeaders)([
  82. options?.headers,
  83. {
  84. 'X-Stainless-Poll-Helper': 'true',
  85. 'X-Stainless-Custom-Poll-Interval': options?.pollIntervalMs?.toString() ?? undefined,
  86. },
  87. ]);
  88. while (true) {
  89. const fileResponse = await this.retrieve(fileID, {
  90. vector_store_id: vectorStoreID,
  91. }, { ...options, headers }).withResponse();
  92. const file = fileResponse.data;
  93. switch (file.status) {
  94. case 'in_progress':
  95. let sleepInterval = 5000;
  96. if (options?.pollIntervalMs) {
  97. sleepInterval = options.pollIntervalMs;
  98. }
  99. else {
  100. const headerInterval = fileResponse.response.headers.get('openai-poll-after-ms');
  101. if (headerInterval) {
  102. const headerIntervalMs = parseInt(headerInterval);
  103. if (!isNaN(headerIntervalMs)) {
  104. sleepInterval = headerIntervalMs;
  105. }
  106. }
  107. }
  108. await (0, utils_1.sleep)(sleepInterval);
  109. break;
  110. case 'failed':
  111. case 'completed':
  112. return file;
  113. }
  114. }
  115. }
  116. /**
  117. * Upload a file to the `files` API and then attach it to the given vector store.
  118. *
  119. * Note the file will be asynchronously processed (you can use the alternative
  120. * polling helper method to wait for processing to complete).
  121. */
  122. async upload(vectorStoreId, file, options) {
  123. const fileInfo = await this._client.files.create({ file: file, purpose: 'assistants' }, options);
  124. return this.create(vectorStoreId, { file_id: fileInfo.id }, options);
  125. }
  126. /**
  127. * Add a file to a vector store and poll until processing is complete.
  128. */
  129. async uploadAndPoll(vectorStoreId, file, options) {
  130. const fileInfo = await this.upload(vectorStoreId, file, options);
  131. return await this.poll(vectorStoreId, fileInfo.id, options);
  132. }
  133. /**
  134. * Retrieve the parsed contents of a vector store file.
  135. */
  136. content(fileID, params, options) {
  137. const { vector_store_id } = params;
  138. return this._client.getAPIList((0, path_1.path) `/vector_stores/${vector_store_id}/files/${fileID}/content`, (pagination_1.Page), { ...options, headers: (0, headers_1.buildHeaders)([{ 'OpenAI-Beta': 'assistants=v2' }, options?.headers]) });
  139. }
  140. }
  141. exports.Files = Files;
  142. //# sourceMappingURL=files.js.map