files.js 5.9 KB

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