openai 4.8.0 → 4.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (319) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +13 -9
  3. package/_shims/MultipartBody.d.ts +9 -0
  4. package/_shims/MultipartBody.d.ts.map +1 -0
  5. package/_shims/MultipartBody.js +16 -0
  6. package/_shims/MultipartBody.js.map +1 -0
  7. package/_shims/MultipartBody.mjs +12 -0
  8. package/_shims/MultipartBody.mjs.map +1 -0
  9. package/_shims/README.md +46 -0
  10. package/{src/_shims/ReadableStream-node.ts → _shims/auto/runtime-bun.d.ts} +2 -3
  11. package/_shims/auto/runtime-bun.d.ts.map +1 -0
  12. package/_shims/auto/runtime-bun.js +21 -0
  13. package/_shims/auto/runtime-bun.js.map +1 -0
  14. package/_shims/auto/runtime-bun.mjs +2 -0
  15. package/_shims/auto/runtime-bun.mjs.map +1 -0
  16. package/_shims/auto/runtime-node.d.ts +5 -0
  17. package/_shims/auto/runtime-node.d.ts.map +1 -0
  18. package/_shims/auto/runtime-node.js +21 -0
  19. package/_shims/auto/runtime-node.js.map +1 -0
  20. package/_shims/auto/runtime-node.mjs +2 -0
  21. package/_shims/auto/runtime-node.mjs.map +1 -0
  22. package/_shims/auto/runtime.d.ts +5 -0
  23. package/_shims/auto/runtime.d.ts.map +1 -0
  24. package/_shims/auto/runtime.js +21 -0
  25. package/_shims/auto/runtime.js.map +1 -0
  26. package/_shims/auto/runtime.mjs +2 -0
  27. package/_shims/auto/runtime.mjs.map +1 -0
  28. package/_shims/auto/types-node.d.ts +5 -0
  29. package/_shims/auto/types-node.d.ts.map +1 -0
  30. package/_shims/auto/types-node.js +21 -0
  31. package/_shims/auto/types-node.js.map +1 -0
  32. package/_shims/auto/types-node.mjs +2 -0
  33. package/_shims/auto/types-node.mjs.map +1 -0
  34. package/_shims/auto/types.d.ts +99 -0
  35. package/_shims/{ReadableStream.js → auto/types.js} +0 -2
  36. package/{src/_shims/ReadableStream.js → _shims/auto/types.mjs} +0 -2
  37. package/_shims/bun-runtime.d.ts +6 -0
  38. package/_shims/bun-runtime.d.ts.map +1 -0
  39. package/_shims/bun-runtime.js +14 -0
  40. package/_shims/bun-runtime.js.map +1 -0
  41. package/_shims/bun-runtime.mjs +10 -0
  42. package/_shims/bun-runtime.mjs.map +1 -0
  43. package/_shims/index.d.ts +79 -0
  44. package/_shims/index.js +13 -0
  45. package/_shims/index.mjs +7 -0
  46. package/_shims/manual-types.d.ts +12 -0
  47. package/_shims/manual-types.js +3 -0
  48. package/_shims/manual-types.mjs +3 -0
  49. package/_shims/node-runtime.d.ts +3 -0
  50. package/_shims/node-runtime.d.ts.map +1 -0
  51. package/_shims/node-runtime.js +87 -0
  52. package/_shims/node-runtime.js.map +1 -0
  53. package/_shims/node-runtime.mjs +54 -0
  54. package/_shims/node-runtime.mjs.map +1 -0
  55. package/_shims/node-types.d.ts +42 -0
  56. package/_shims/node-types.js +3 -0
  57. package/_shims/node-types.mjs +3 -0
  58. package/_shims/registry.d.ts +35 -0
  59. package/_shims/registry.d.ts.map +1 -0
  60. package/_shims/registry.js +39 -0
  61. package/_shims/registry.js.map +1 -0
  62. package/_shims/registry.mjs +35 -0
  63. package/_shims/registry.mjs.map +1 -0
  64. package/_shims/web-runtime.d.ts +5 -0
  65. package/_shims/web-runtime.d.ts.map +1 -0
  66. package/_shims/web-runtime.js +70 -0
  67. package/_shims/web-runtime.js.map +1 -0
  68. package/_shims/web-runtime.mjs +63 -0
  69. package/_shims/web-runtime.mjs.map +1 -0
  70. package/_shims/web-types.d.ts +82 -0
  71. package/_shims/web-types.js +3 -0
  72. package/_shims/web-types.mjs +3 -0
  73. package/core.d.ts +170 -218
  74. package/core.d.ts.map +1 -1
  75. package/core.js +679 -737
  76. package/core.js.map +1 -1
  77. package/core.mjs +673 -688
  78. package/core.mjs.map +1 -1
  79. package/error.d.ts +32 -34
  80. package/error.js +95 -109
  81. package/error.mjs +92 -94
  82. package/index.d.mts +164 -175
  83. package/index.d.ts +164 -175
  84. package/index.d.ts.map +1 -1
  85. package/index.js +110 -161
  86. package/index.mjs +88 -109
  87. package/package.json +31 -58
  88. package/pagination.d.ts +31 -41
  89. package/pagination.js +52 -48
  90. package/pagination.mjs +50 -46
  91. package/resource.d.ts +10 -10
  92. package/resource.js +12 -12
  93. package/resource.mjs +10 -10
  94. package/resources/audio/audio.d.ts +12 -12
  95. package/resources/audio/audio.js +33 -50
  96. package/resources/audio/audio.mjs +11 -11
  97. package/resources/audio/index.d.ts +4 -4
  98. package/resources/audio/index.js +9 -24
  99. package/resources/audio/index.mjs +4 -4
  100. package/resources/audio/transcriptions.d.ts +44 -44
  101. package/resources/audio/transcriptions.js +13 -12
  102. package/resources/audio/transcriptions.mjs +9 -8
  103. package/resources/audio/translations.d.ts +38 -38
  104. package/resources/audio/translations.js +13 -12
  105. package/resources/audio/translations.mjs +9 -8
  106. package/resources/chat/chat.d.ts +17 -17
  107. package/resources/chat/chat.js +30 -47
  108. package/resources/chat/chat.mjs +8 -8
  109. package/resources/chat/completions.d.ts +330 -353
  110. package/resources/chat/completions.js +10 -13
  111. package/resources/chat/completions.mjs +7 -10
  112. package/resources/chat/index.d.ts +3 -17
  113. package/resources/chat/index.js +7 -17
  114. package/resources/chat/index.mjs +3 -3
  115. package/resources/completions.d.ts +216 -232
  116. package/resources/completions.js +10 -13
  117. package/resources/completions.mjs +7 -10
  118. package/resources/edits.d.ts +73 -73
  119. package/resources/edits.js +16 -15
  120. package/resources/edits.mjs +13 -12
  121. package/resources/embeddings.d.ts +73 -76
  122. package/resources/embeddings.js +12 -11
  123. package/resources/embeddings.mjs +9 -8
  124. package/resources/files.d.ts +91 -96
  125. package/resources/files.js +64 -65
  126. package/resources/files.mjs +57 -58
  127. package/resources/fine-tunes.d.ts +277 -290
  128. package/resources/fine-tunes.js +49 -50
  129. package/resources/fine-tunes.mjs +45 -46
  130. package/resources/fine-tuning/fine-tuning.d.ts +12 -12
  131. package/resources/fine-tuning/fine-tuning.js +32 -49
  132. package/resources/fine-tuning/fine-tuning.mjs +10 -10
  133. package/resources/fine-tuning/index.d.ts +3 -12
  134. package/resources/fine-tuning/index.js +9 -29
  135. package/resources/fine-tuning/index.mjs +3 -3
  136. package/resources/fine-tuning/jobs.d.ts +173 -179
  137. package/resources/fine-tuning/jobs.js +50 -47
  138. package/resources/fine-tuning/jobs.mjs +45 -42
  139. package/resources/images.d.ts +116 -119
  140. package/resources/images.js +25 -24
  141. package/resources/images.mjs +21 -20
  142. package/resources/index.d.ts +12 -37
  143. package/resources/index.js +29 -113
  144. package/resources/index.mjs +12 -12
  145. package/resources/models.d.ts +41 -40
  146. package/resources/models.js +30 -28
  147. package/resources/models.mjs +26 -24
  148. package/resources/moderations.d.ts +153 -156
  149. package/resources/moderations.js +12 -11
  150. package/resources/moderations.mjs +9 -8
  151. package/shims/node.d.ts +29 -0
  152. package/shims/node.d.ts.map +1 -0
  153. package/shims/node.js +31 -0
  154. package/shims/node.js.map +1 -0
  155. package/shims/node.mjs +5 -0
  156. package/shims/node.mjs.map +1 -0
  157. package/shims/web.d.ts +26 -0
  158. package/shims/web.d.ts.map +1 -0
  159. package/shims/web.js +31 -0
  160. package/shims/web.js.map +1 -0
  161. package/shims/web.mjs +5 -0
  162. package/shims/web.mjs.map +1 -0
  163. package/src/_shims/MultipartBody.ts +9 -0
  164. package/src/_shims/README.md +46 -0
  165. package/src/_shims/auto/runtime-bun.ts +4 -0
  166. package/src/_shims/auto/runtime-node.ts +4 -0
  167. package/src/_shims/auto/runtime.ts +4 -0
  168. package/src/_shims/auto/types-node.ts +4 -0
  169. package/src/_shims/auto/types.d.ts +99 -0
  170. package/src/_shims/auto/types.js +3 -0
  171. package/src/_shims/auto/types.mjs +3 -0
  172. package/src/_shims/bun-runtime.ts +14 -0
  173. package/src/_shims/index.d.ts +79 -0
  174. package/src/_shims/index.js +13 -0
  175. package/src/_shims/index.mjs +7 -0
  176. package/src/_shims/manual-types.d.ts +12 -0
  177. package/src/_shims/manual-types.js +3 -0
  178. package/src/_shims/manual-types.mjs +3 -0
  179. package/src/_shims/node-runtime.ts +79 -0
  180. package/src/_shims/node-types.d.ts +42 -0
  181. package/src/_shims/node-types.js +3 -0
  182. package/src/_shims/node-types.mjs +3 -0
  183. package/src/_shims/registry.ts +62 -0
  184. package/src/_shims/web-runtime.ts +91 -0
  185. package/src/_shims/web-types.d.ts +82 -0
  186. package/src/_shims/web-types.js +3 -0
  187. package/src/_shims/web-types.mjs +3 -0
  188. package/src/core.ts +34 -17
  189. package/src/error.ts +1 -1
  190. package/src/index.ts +6 -6
  191. package/src/pagination.ts +1 -1
  192. package/src/resource.ts +1 -1
  193. package/src/resources/audio/audio.ts +4 -4
  194. package/src/resources/audio/index.ts +3 -3
  195. package/src/resources/audio/transcriptions.ts +4 -4
  196. package/src/resources/audio/translations.ts +4 -4
  197. package/src/resources/chat/chat.ts +3 -3
  198. package/src/resources/chat/completions.ts +6 -6
  199. package/src/resources/chat/index.ts +2 -2
  200. package/src/resources/completions.ts +5 -5
  201. package/src/resources/edits.ts +4 -4
  202. package/src/resources/embeddings.ts +3 -3
  203. package/src/resources/files.ts +7 -7
  204. package/src/resources/fine-tunes.ts +7 -7
  205. package/src/resources/fine-tuning/fine-tuning.ts +3 -3
  206. package/src/resources/fine-tuning/index.ts +2 -2
  207. package/src/resources/fine-tuning/jobs.ts +6 -6
  208. package/src/resources/images.ts +4 -4
  209. package/src/resources/index.ts +11 -11
  210. package/src/resources/models.ts +4 -4
  211. package/src/resources/moderations.ts +3 -3
  212. package/src/shims/node.ts +50 -0
  213. package/src/shims/web.ts +50 -0
  214. package/src/streaming.ts +1 -1
  215. package/src/uploads.ts +12 -16
  216. package/src/version.ts +1 -1
  217. package/streaming.d.ts +9 -8
  218. package/streaming.d.ts.map +1 -1
  219. package/streaming.js +194 -185
  220. package/streaming.mjs +192 -183
  221. package/uploads.d.ts +23 -39
  222. package/uploads.d.ts.map +1 -1
  223. package/uploads.js +115 -157
  224. package/uploads.js.map +1 -1
  225. package/uploads.mjs +111 -133
  226. package/uploads.mjs.map +1 -1
  227. package/version.d.ts +2 -2
  228. package/version.js +4 -4
  229. package/version.mjs +2 -2
  230. package/_shims/ReadableStream-node.d.ts +0 -6
  231. package/_shims/ReadableStream-node.d.ts.map +0 -1
  232. package/_shims/ReadableStream-node.js +0 -14
  233. package/_shims/ReadableStream-node.js.map +0 -1
  234. package/_shims/ReadableStream-node.mjs +0 -3
  235. package/_shims/ReadableStream-node.mjs.map +0 -1
  236. package/_shims/ReadableStream.d.ts +0 -38
  237. package/_shims/ReadableStream.mjs +0 -7
  238. package/_shims/agent-node.d.ts +0 -7
  239. package/_shims/agent-node.d.ts.map +0 -1
  240. package/_shims/agent-node.js +0 -28
  241. package/_shims/agent-node.js.map +0 -1
  242. package/_shims/agent-node.mjs +0 -16
  243. package/_shims/agent-node.mjs.map +0 -1
  244. package/_shims/agent.d.ts +0 -9
  245. package/_shims/agent.d.ts.map +0 -1
  246. package/_shims/agent.js +0 -14
  247. package/_shims/agent.js.map +0 -1
  248. package/_shims/agent.mjs +0 -10
  249. package/_shims/agent.mjs.map +0 -1
  250. package/_shims/fetch-node.d.ts +0 -64
  251. package/_shims/fetch-node.js +0 -12
  252. package/_shims/fetch-node.mjs +0 -14
  253. package/_shims/fetch.d.ts +0 -60
  254. package/_shims/fetch.js +0 -13
  255. package/_shims/fetch.mjs +0 -15
  256. package/_shims/fileFromPath-node.d.ts +0 -17
  257. package/_shims/fileFromPath-node.d.ts.map +0 -1
  258. package/_shims/fileFromPath-node.js +0 -17
  259. package/_shims/fileFromPath-node.js.map +0 -1
  260. package/_shims/fileFromPath-node.mjs +0 -13
  261. package/_shims/fileFromPath-node.mjs.map +0 -1
  262. package/_shims/fileFromPath.d.ts +0 -22
  263. package/_shims/fileFromPath.d.ts.map +0 -1
  264. package/_shims/fileFromPath.js +0 -16
  265. package/_shims/fileFromPath.js.map +0 -1
  266. package/_shims/fileFromPath.mjs +0 -12
  267. package/_shims/fileFromPath.mjs.map +0 -1
  268. package/_shims/form-data-node.d.ts +0 -45
  269. package/_shims/form-data-node.js +0 -11
  270. package/_shims/form-data-node.mjs +0 -9
  271. package/_shims/form-data.d.ts +0 -43
  272. package/_shims/form-data.js +0 -17
  273. package/_shims/form-data.mjs +0 -20
  274. package/_shims/getMultipartRequestOptions-node.d.ts +0 -10
  275. package/_shims/getMultipartRequestOptions-node.d.ts.map +0 -1
  276. package/_shims/getMultipartRequestOptions-node.js +0 -22
  277. package/_shims/getMultipartRequestOptions-node.js.map +0 -1
  278. package/_shims/getMultipartRequestOptions-node.mjs +0 -18
  279. package/_shims/getMultipartRequestOptions-node.mjs.map +0 -1
  280. package/_shims/getMultipartRequestOptions.d.ts +0 -10
  281. package/_shims/getMultipartRequestOptions.d.ts.map +0 -1
  282. package/_shims/getMultipartRequestOptions.js +0 -12
  283. package/_shims/getMultipartRequestOptions.js.map +0 -1
  284. package/_shims/getMultipartRequestOptions.mjs +0 -8
  285. package/_shims/getMultipartRequestOptions.mjs.map +0 -1
  286. package/_shims/node-readable-node.d.ts +0 -8
  287. package/_shims/node-readable-node.d.ts.map +0 -1
  288. package/_shims/node-readable-node.js +0 -9
  289. package/_shims/node-readable-node.js.map +0 -1
  290. package/_shims/node-readable-node.mjs +0 -5
  291. package/_shims/node-readable-node.mjs.map +0 -1
  292. package/_shims/node-readable.d.ts +0 -23
  293. package/_shims/node-readable.d.ts.map +0 -1
  294. package/_shims/node-readable.js +0 -11
  295. package/_shims/node-readable.js.map +0 -1
  296. package/_shims/node-readable.mjs +0 -7
  297. package/_shims/node-readable.mjs.map +0 -1
  298. package/src/_shims/ReadableStream.d.ts +0 -38
  299. package/src/_shims/ReadableStream.mjs +0 -7
  300. package/src/_shims/agent-node.ts +0 -22
  301. package/src/_shims/agent.ts +0 -12
  302. package/src/_shims/fetch-node.d.ts +0 -64
  303. package/src/_shims/fetch-node.js +0 -12
  304. package/src/_shims/fetch-node.mjs +0 -14
  305. package/src/_shims/fetch.d.ts +0 -60
  306. package/src/_shims/fetch.js +0 -13
  307. package/src/_shims/fetch.mjs +0 -15
  308. package/src/_shims/fileFromPath-node.ts +0 -29
  309. package/src/_shims/fileFromPath.ts +0 -29
  310. package/src/_shims/form-data-node.d.ts +0 -45
  311. package/src/_shims/form-data-node.js +0 -11
  312. package/src/_shims/form-data-node.mjs +0 -9
  313. package/src/_shims/form-data.d.ts +0 -43
  314. package/src/_shims/form-data.js +0 -17
  315. package/src/_shims/form-data.mjs +0 -20
  316. package/src/_shims/getMultipartRequestOptions-node.ts +0 -25
  317. package/src/_shims/getMultipartRequestOptions.ts +0 -14
  318. package/src/_shims/node-readable-node.ts +0 -10
  319. package/src/_shims/node-readable.ts +0 -30
package/streaming.mjs CHANGED
@@ -1,95 +1,105 @@
1
1
  export class Stream {
2
- constructor(response, controller) {
3
- this.response = response;
4
- this.controller = controller;
5
- this.decoder = new SSEDecoder();
6
- }
7
- async *iterMessages() {
8
- if (!this.response.body) {
9
- this.controller.abort();
10
- throw new Error(`Attempted to iterate over a response with no body`);
11
- }
12
- const lineDecoder = new LineDecoder();
13
- const iter = readableStreamAsyncIterable(this.response.body);
14
- for await (const chunk of iter) {
15
- for (const line of lineDecoder.decode(chunk)) {
16
- const sse = this.decoder.decode(line);
17
- if (sse) yield sse;
18
- }
19
- }
20
- for (const line of lineDecoder.flush()) {
21
- const sse = this.decoder.decode(line);
22
- if (sse) yield sse;
2
+ constructor(response, controller) {
3
+ this.response = response;
4
+ this.controller = controller;
5
+ this.decoder = new SSEDecoder();
6
+ }
7
+ async *iterMessages() {
8
+ if (!this.response.body) {
9
+ this.controller.abort();
10
+ throw new Error(`Attempted to iterate over a response with no body`);
11
+ }
12
+ const lineDecoder = new LineDecoder();
13
+ const iter = readableStreamAsyncIterable(this.response.body);
14
+ for await (const chunk of iter) {
15
+ for (const line of lineDecoder.decode(chunk)) {
16
+ const sse = this.decoder.decode(line);
17
+ if (sse)
18
+ yield sse;
19
+ }
20
+ }
21
+ for (const line of lineDecoder.flush()) {
22
+ const sse = this.decoder.decode(line);
23
+ if (sse)
24
+ yield sse;
25
+ }
23
26
  }
24
- }
25
- async *[Symbol.asyncIterator]() {
26
- let done = false;
27
- try {
28
- for await (const sse of this.iterMessages()) {
29
- if (done) continue;
30
- if (sse.data.startsWith('[DONE]')) {
31
- done = true;
32
- continue;
33
- }
34
- if (sse.event === null) {
35
- try {
36
- yield JSON.parse(sse.data);
37
- } catch (e) {
38
- console.error(`Could not parse message into JSON:`, sse.data);
39
- console.error(`From chunk:`, sse.raw);
27
+ async *[Symbol.asyncIterator]() {
28
+ let done = false;
29
+ try {
30
+ for await (const sse of this.iterMessages()) {
31
+ if (done)
32
+ continue;
33
+ if (sse.data.startsWith('[DONE]')) {
34
+ done = true;
35
+ continue;
36
+ }
37
+ if (sse.event === null) {
38
+ try {
39
+ yield JSON.parse(sse.data);
40
+ }
41
+ catch (e) {
42
+ console.error(`Could not parse message into JSON:`, sse.data);
43
+ console.error(`From chunk:`, sse.raw);
44
+ throw e;
45
+ }
46
+ }
47
+ }
48
+ done = true;
49
+ }
50
+ catch (e) {
51
+ // If the user calls `stream.controller.abort()`, we should exit without throwing.
52
+ if (e instanceof Error && e.name === 'AbortError')
53
+ return;
40
54
  throw e;
41
- }
42
- }
43
- }
44
- done = true;
45
- } catch (e) {
46
- // If the user calls `stream.controller.abort()`, we should exit without throwing.
47
- if (e instanceof Error && e.name === 'AbortError') return;
48
- throw e;
49
- } finally {
50
- // If the user `break`s, abort the ongoing request.
51
- if (!done) this.controller.abort();
55
+ }
56
+ finally {
57
+ // If the user `break`s, abort the ongoing request.
58
+ if (!done)
59
+ this.controller.abort();
60
+ }
52
61
  }
53
- }
54
62
  }
55
63
  class SSEDecoder {
56
- constructor() {
57
- this.event = null;
58
- this.data = [];
59
- this.chunks = [];
60
- }
61
- decode(line) {
62
- if (line.endsWith('\r')) {
63
- line = line.substring(0, line.length - 1);
64
- }
65
- if (!line) {
66
- // empty line and we didn't previously encounter any messages
67
- if (!this.event && !this.data.length) return null;
68
- const sse = {
69
- event: this.event,
70
- data: this.data.join('\n'),
71
- raw: this.chunks,
72
- };
73
- this.event = null;
74
- this.data = [];
75
- this.chunks = [];
76
- return sse;
77
- }
78
- this.chunks.push(line);
79
- if (line.startsWith(':')) {
80
- return null;
81
- }
82
- let [fieldname, _, value] = partition(line, ':');
83
- if (value.startsWith(' ')) {
84
- value = value.substring(1);
85
- }
86
- if (fieldname === 'event') {
87
- this.event = value;
88
- } else if (fieldname === 'data') {
89
- this.data.push(value);
64
+ constructor() {
65
+ this.event = null;
66
+ this.data = [];
67
+ this.chunks = [];
68
+ }
69
+ decode(line) {
70
+ if (line.endsWith('\r')) {
71
+ line = line.substring(0, line.length - 1);
72
+ }
73
+ if (!line) {
74
+ // empty line and we didn't previously encounter any messages
75
+ if (!this.event && !this.data.length)
76
+ return null;
77
+ const sse = {
78
+ event: this.event,
79
+ data: this.data.join('\n'),
80
+ raw: this.chunks,
81
+ };
82
+ this.event = null;
83
+ this.data = [];
84
+ this.chunks = [];
85
+ return sse;
86
+ }
87
+ this.chunks.push(line);
88
+ if (line.startsWith(':')) {
89
+ return null;
90
+ }
91
+ let [fieldname, _, value] = partition(line, ':');
92
+ if (value.startsWith(' ')) {
93
+ value = value.substring(1);
94
+ }
95
+ if (fieldname === 'event') {
96
+ this.event = value;
97
+ }
98
+ else if (fieldname === 'data') {
99
+ this.data.push(value);
100
+ }
101
+ return null;
90
102
  }
91
- return null;
92
- }
93
103
  }
94
104
  /**
95
105
  * A re-implementation of httpx's `LineDecoder` in Python that handles incrementally
@@ -98,87 +108,83 @@ class SSEDecoder {
98
108
  * https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258
99
109
  */
100
110
  class LineDecoder {
101
- constructor() {
102
- this.buffer = [];
103
- this.trailingCR = false;
104
- }
105
- decode(chunk) {
106
- let text = this.decodeText(chunk);
107
- if (this.trailingCR) {
108
- text = '\r' + text;
109
- this.trailingCR = false;
110
- }
111
- if (text.endsWith('\r')) {
112
- this.trailingCR = true;
113
- text = text.slice(0, -1);
114
- }
115
- if (!text) {
116
- return [];
117
- }
118
- const trailingNewline = LineDecoder.NEWLINE_CHARS.has(text[text.length - 1] || '');
119
- let lines = text.split(LineDecoder.NEWLINE_REGEXP);
120
- if (lines.length === 1 && !trailingNewline) {
121
- this.buffer.push(lines[0]);
122
- return [];
123
- }
124
- if (this.buffer.length > 0) {
125
- lines = [this.buffer.join('') + lines[0], ...lines.slice(1)];
126
- this.buffer = [];
127
- }
128
- if (!trailingNewline) {
129
- this.buffer = [lines.pop() || ''];
130
- }
131
- return lines;
132
- }
133
- decodeText(bytes) {
134
- var _a;
135
- if (bytes == null) return '';
136
- if (typeof bytes === 'string') return bytes;
137
- // Node:
138
- if (typeof Buffer !== 'undefined') {
139
- if (bytes instanceof Buffer) {
140
- return bytes.toString();
141
- }
142
- if (bytes instanceof Uint8Array) {
143
- return Buffer.from(bytes).toString();
144
- }
145
- throw new Error(
146
- `Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.`,
147
- );
148
- }
149
- // Browser
150
- if (typeof TextDecoder !== 'undefined') {
151
- if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
152
- (_a = this.textDecoder) !== null && _a !== void 0 ? _a : (this.textDecoder = new TextDecoder('utf8'));
153
- return this.textDecoder.decode(bytes);
154
- }
155
- throw new Error(
156
- `Unexpected: received non-Uint8Array/ArrayBuffer (${bytes.constructor.name}) in a web platform. Please report this error.`,
157
- );
111
+ constructor() {
112
+ this.buffer = [];
113
+ this.trailingCR = false;
114
+ }
115
+ decode(chunk) {
116
+ let text = this.decodeText(chunk);
117
+ if (this.trailingCR) {
118
+ text = '\r' + text;
119
+ this.trailingCR = false;
120
+ }
121
+ if (text.endsWith('\r')) {
122
+ this.trailingCR = true;
123
+ text = text.slice(0, -1);
124
+ }
125
+ if (!text) {
126
+ return [];
127
+ }
128
+ const trailingNewline = LineDecoder.NEWLINE_CHARS.has(text[text.length - 1] || '');
129
+ let lines = text.split(LineDecoder.NEWLINE_REGEXP);
130
+ if (lines.length === 1 && !trailingNewline) {
131
+ this.buffer.push(lines[0]);
132
+ return [];
133
+ }
134
+ if (this.buffer.length > 0) {
135
+ lines = [this.buffer.join('') + lines[0], ...lines.slice(1)];
136
+ this.buffer = [];
137
+ }
138
+ if (!trailingNewline) {
139
+ this.buffer = [lines.pop() || ''];
140
+ }
141
+ return lines;
142
+ }
143
+ decodeText(bytes) {
144
+ var _a;
145
+ if (bytes == null)
146
+ return '';
147
+ if (typeof bytes === 'string')
148
+ return bytes;
149
+ // Node:
150
+ if (typeof Buffer !== 'undefined') {
151
+ if (bytes instanceof Buffer) {
152
+ return bytes.toString();
153
+ }
154
+ if (bytes instanceof Uint8Array) {
155
+ return Buffer.from(bytes).toString();
156
+ }
157
+ throw new Error(`Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.`);
158
+ }
159
+ // Browser
160
+ if (typeof TextDecoder !== 'undefined') {
161
+ if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
162
+ (_a = this.textDecoder) !== null && _a !== void 0 ? _a : (this.textDecoder = new TextDecoder('utf8'));
163
+ return this.textDecoder.decode(bytes);
164
+ }
165
+ throw new Error(`Unexpected: received non-Uint8Array/ArrayBuffer (${bytes.constructor.name}) in a web platform. Please report this error.`);
166
+ }
167
+ throw new Error(`Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.`);
158
168
  }
159
- throw new Error(
160
- `Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.`,
161
- );
162
- }
163
- flush() {
164
- if (!this.buffer.length && !this.trailingCR) {
165
- return [];
169
+ flush() {
170
+ if (!this.buffer.length && !this.trailingCR) {
171
+ return [];
172
+ }
173
+ const lines = [this.buffer.join('')];
174
+ this.buffer = [];
175
+ this.trailingCR = false;
176
+ return lines;
166
177
  }
167
- const lines = [this.buffer.join('')];
168
- this.buffer = [];
169
- this.trailingCR = false;
170
- return lines;
171
- }
172
178
  }
173
179
  // prettier-ignore
174
180
  LineDecoder.NEWLINE_CHARS = new Set(['\n', '\r', '\x0b', '\x0c', '\x1c', '\x1d', '\x1e', '\x85', '\u2028', '\u2029']);
175
181
  LineDecoder.NEWLINE_REGEXP = /\r\n|[\n\r\x0b\x0c\x1c\x1d\x1e\x85\u2028\u2029]/g;
176
182
  function partition(str, delimiter) {
177
- const index = str.indexOf(delimiter);
178
- if (index !== -1) {
179
- return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];
180
- }
181
- return [str, '', ''];
183
+ const index = str.indexOf(delimiter);
184
+ if (index !== -1) {
185
+ return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];
186
+ }
187
+ return [str, '', ''];
182
188
  }
183
189
  /**
184
190
  * Most browsers don't yet have async iterable support for ReadableStream,
@@ -187,28 +193,31 @@ function partition(str, delimiter) {
187
193
  * This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
188
194
  */
189
195
  function readableStreamAsyncIterable(stream) {
190
- if (stream[Symbol.asyncIterator]) return stream;
191
- const reader = stream.getReader();
192
- return {
193
- async next() {
194
- try {
195
- const result = await reader.read();
196
- if (result === null || result === void 0 ? void 0 : result.done) reader.releaseLock(); // release lock when stream becomes closed
197
- return result;
198
- } catch (e) {
199
- reader.releaseLock(); // release lock when stream becomes errored
200
- throw e;
201
- }
202
- },
203
- async return() {
204
- const cancelPromise = reader.cancel();
205
- reader.releaseLock();
206
- await cancelPromise;
207
- return { done: true, value: undefined };
208
- },
209
- [Symbol.asyncIterator]() {
210
- return this;
211
- },
212
- };
196
+ if (stream[Symbol.asyncIterator])
197
+ return stream;
198
+ const reader = stream.getReader();
199
+ return {
200
+ async next() {
201
+ try {
202
+ const result = await reader.read();
203
+ if (result === null || result === void 0 ? void 0 : result.done)
204
+ reader.releaseLock(); // release lock when stream becomes closed
205
+ return result;
206
+ }
207
+ catch (e) {
208
+ reader.releaseLock(); // release lock when stream becomes errored
209
+ throw e;
210
+ }
211
+ },
212
+ async return() {
213
+ const cancelPromise = reader.cancel();
214
+ reader.releaseLock();
215
+ await cancelPromise;
216
+ return { done: true, value: undefined };
217
+ },
218
+ [Symbol.asyncIterator]() {
219
+ return this;
220
+ },
221
+ };
213
222
  }
214
- //# sourceMappingURL=streaming.mjs.map
223
+ //# sourceMappingURL=streaming.mjs.map
package/uploads.d.ts CHANGED
@@ -1,10 +1,7 @@
1
- import { type RequestOptions } from './core.js';
2
- import { type Readable } from 'openai/_shims/node-readable';
3
- import { type BodyInit } from 'openai/_shims/fetch';
4
- import { FormData, type Blob, type FilePropertyBag } from 'openai/_shims/form-data';
5
- import { fileFromPath } from 'openai/_shims/fileFromPath';
6
- import { type FsReadStream } from 'openai/_shims/node-readable';
7
- export { fileFromPath };
1
+ import { type RequestOptions } from "./core.js";
2
+ import { FormData, type Blob, type FilePropertyBag, type FsReadStream } from "./_shims/index.js";
3
+ import { MultipartBody } from "./_shims/MultipartBody.js";
4
+ export { fileFromPath } from "./_shims/index.js";
8
5
  type BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | Uint8Array | DataView;
9
6
  export type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | Uint8Array | DataView;
10
7
  /**
@@ -21,30 +18,30 @@ export type Uploadable = FileLike | ResponseLike | FsReadStream;
21
18
  * Intended to match web.Blob, node.Blob, node-fetch.Blob, etc.
22
19
  */
23
20
  export interface BlobLike {
24
- /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
25
- readonly size: number;
26
- /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
27
- readonly type: string;
28
- /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
29
- text(): Promise<string>;
30
- /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
31
- slice(start?: number, end?: number): BlobLike;
21
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
22
+ readonly size: number;
23
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
24
+ readonly type: string;
25
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
26
+ text(): Promise<string>;
27
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
28
+ slice(start?: number, end?: number): BlobLike;
32
29
  }
33
30
  /**
34
31
  * Intended to match web.File, node.File, node-fetch.File, etc.
35
32
  */
36
33
  export interface FileLike extends BlobLike {
37
- /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
38
- readonly lastModified: number;
39
- /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
40
- readonly name: string;
34
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
35
+ readonly lastModified: number;
36
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
37
+ readonly name: string;
41
38
  }
42
39
  /**
43
40
  * Intended to match web.Response, node.Response, node-fetch.Response, etc.
44
41
  */
45
42
  export interface ResponseLike {
46
- url: string;
47
- blob(): Promise<BlobLike>;
43
+ url: string;
44
+ blob(): Promise<BlobLike>;
48
45
  }
49
46
  export declare const isResponseLike: (value: any) => value is ResponseLike;
50
47
  export declare const isFileLike: (value: any) => value is FileLike;
@@ -53,7 +50,7 @@ export declare const isFileLike: (value: any) => value is FileLike;
53
50
  * adds the arrayBuffer() method type because it is available and used at runtime
54
51
  */
55
52
  export declare const isBlobLike: (value: any) => value is BlobLike & {
56
- arrayBuffer(): Promise<ArrayBuffer>;
53
+ arrayBuffer(): Promise<ArrayBuffer>;
57
54
  };
58
55
  export declare const isUploadable: (value: any) => value is Uploadable;
59
56
  export type ToFileInput = Uploadable | Exclude<BlobLikePart, string> | AsyncIterable<BlobLikePart>;
@@ -66,26 +63,13 @@ export type ToFileInput = Uploadable | Exclude<BlobLikePart, string> | AsyncIter
66
63
  * @param {number=} options.lastModified the last modified timestamp
67
64
  * @returns a {@link File} with the given properties
68
65
  */
69
- export declare function toFile(
70
- value: ToFileInput | PromiseLike<ToFileInput>,
71
- name?: string | null | undefined,
72
- options?: FilePropertyBag | undefined,
73
- ): Promise<FileLike>;
74
- export declare class MultipartBody {
75
- body: Readable | BodyInit;
76
- constructor(body: Readable | BodyInit);
77
- get [Symbol.toStringTag](): string;
78
- }
66
+ export declare function toFile(value: ToFileInput | PromiseLike<ToFileInput>, name?: string | null | undefined, options?: FilePropertyBag | undefined): Promise<FileLike>;
79
67
  export declare const isMultipartBody: (body: any) => body is MultipartBody;
80
68
  /**
81
69
  * Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
82
70
  * Otherwise returns the request as is.
83
71
  */
84
- export declare const maybeMultipartFormRequestOptions: <T extends {} = Record<string, unknown>>(
85
- opts: RequestOptions<T>,
86
- ) => Promise<RequestOptions<MultipartBody | T>>;
87
- export declare const multipartFormRequestOptions: <T extends {} = Record<string, unknown>>(
88
- opts: RequestOptions<T>,
89
- ) => Promise<RequestOptions<MultipartBody | T>>;
72
+ export declare const maybeMultipartFormRequestOptions: <T extends {} = Record<string, unknown>>(opts: RequestOptions<T>) => Promise<RequestOptions<MultipartBody | T>>;
73
+ export declare const multipartFormRequestOptions: <T extends {} = Record<string, unknown>>(opts: RequestOptions<T>) => Promise<RequestOptions<MultipartBody | T>>;
90
74
  export declare const createForm: <T = Record<string, unknown>>(body: T | undefined) => Promise<FormData>;
91
- //# sourceMappingURL=uploads.d.ts.map
75
+ //# sourceMappingURL=uploads.d.ts.map
package/uploads.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"uploads.d.ts","sourceRoot":"","sources":["src/uploads.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,QAAQ,CAAC;AAC7C,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,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