openai 4.86.2 → 4.87.0

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 (198) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +50 -101
  3. package/core.js +2 -1
  4. package/core.js.map +1 -1
  5. package/core.mjs +2 -1
  6. package/core.mjs.map +1 -1
  7. package/helpers/zod.d.ts +14 -1
  8. package/helpers/zod.d.ts.map +1 -1
  9. package/helpers/zod.js +25 -1
  10. package/helpers/zod.js.map +1 -1
  11. package/helpers/zod.mjs +23 -1
  12. package/helpers/zod.mjs.map +1 -1
  13. package/index.d.mts +14 -3
  14. package/index.d.ts +14 -3
  15. package/index.d.ts.map +1 -1
  16. package/index.js +8 -0
  17. package/index.js.map +1 -1
  18. package/index.mjs +8 -0
  19. package/index.mjs.map +1 -1
  20. package/lib/ResponsesParser.d.ts +33 -0
  21. package/lib/ResponsesParser.d.ts.map +1 -0
  22. package/lib/ResponsesParser.js +171 -0
  23. package/lib/ResponsesParser.js.map +1 -0
  24. package/lib/ResponsesParser.mjs +160 -0
  25. package/lib/ResponsesParser.mjs.map +1 -0
  26. package/lib/parser.d.ts +7 -0
  27. package/lib/parser.d.ts.map +1 -1
  28. package/lib/parser.js +16 -1
  29. package/lib/parser.js.map +1 -1
  30. package/lib/parser.mjs +14 -0
  31. package/lib/parser.mjs.map +1 -1
  32. package/lib/responses/EventTypes.d.ts +9 -0
  33. package/lib/responses/EventTypes.d.ts.map +1 -0
  34. package/lib/responses/EventTypes.js +3 -0
  35. package/lib/responses/EventTypes.js.map +1 -0
  36. package/lib/responses/EventTypes.mjs +2 -0
  37. package/lib/responses/EventTypes.mjs.map +1 -0
  38. package/lib/responses/ResponseStream.d.ts +34 -0
  39. package/lib/responses/ResponseStream.d.ts.map +1 -0
  40. package/lib/responses/ResponseStream.js +238 -0
  41. package/lib/responses/ResponseStream.js.map +1 -0
  42. package/lib/responses/ResponseStream.mjs +234 -0
  43. package/lib/responses/ResponseStream.mjs.map +1 -0
  44. package/package.json +1 -1
  45. package/resources/beta/assistants.d.ts +41 -9
  46. package/resources/beta/assistants.d.ts.map +1 -1
  47. package/resources/beta/assistants.js.map +1 -1
  48. package/resources/beta/assistants.mjs.map +1 -1
  49. package/resources/beta/beta.d.ts +0 -4
  50. package/resources/beta/beta.d.ts.map +1 -1
  51. package/resources/beta/beta.js +0 -5
  52. package/resources/beta/beta.js.map +1 -1
  53. package/resources/beta/beta.mjs +0 -5
  54. package/resources/beta/beta.mjs.map +1 -1
  55. package/resources/beta/index.d.ts +0 -1
  56. package/resources/beta/index.d.ts.map +1 -1
  57. package/resources/beta/index.js +1 -4
  58. package/resources/beta/index.js.map +1 -1
  59. package/resources/beta/index.mjs +0 -1
  60. package/resources/beta/index.mjs.map +1 -1
  61. package/resources/beta/threads/runs/runs.d.ts +3 -4
  62. package/resources/beta/threads/runs/runs.d.ts.map +1 -1
  63. package/resources/beta/threads/runs/runs.js.map +1 -1
  64. package/resources/beta/threads/runs/runs.mjs.map +1 -1
  65. package/resources/beta/threads/threads.d.ts +73 -7
  66. package/resources/beta/threads/threads.d.ts.map +1 -1
  67. package/resources/beta/threads/threads.js.map +1 -1
  68. package/resources/beta/threads/threads.mjs.map +1 -1
  69. package/resources/chat/chat.d.ts +3 -2
  70. package/resources/chat/chat.d.ts.map +1 -1
  71. package/resources/chat/chat.js.map +1 -1
  72. package/resources/chat/chat.mjs.map +1 -1
  73. package/resources/chat/completions/completions.d.ts +200 -64
  74. package/resources/chat/completions/completions.d.ts.map +1 -1
  75. package/resources/chat/completions/completions.js +3 -3
  76. package/resources/chat/completions/completions.js.map +1 -1
  77. package/resources/chat/completions/completions.mjs +3 -3
  78. package/resources/chat/completions/completions.mjs.map +1 -1
  79. package/resources/chat/completions/index.d.ts +1 -1
  80. package/resources/chat/completions/index.d.ts.map +1 -1
  81. package/resources/chat/completions/index.js.map +1 -1
  82. package/resources/chat/completions/index.mjs.map +1 -1
  83. package/resources/chat/completions/messages.d.ts +1 -1
  84. package/resources/chat/index.d.ts +2 -2
  85. package/resources/chat/index.d.ts.map +1 -1
  86. package/resources/chat/index.js.map +1 -1
  87. package/resources/chat/index.mjs.map +1 -1
  88. package/resources/files.d.ts +9 -17
  89. package/resources/files.d.ts.map +1 -1
  90. package/resources/files.js.map +1 -1
  91. package/resources/files.mjs.map +1 -1
  92. package/resources/index.d.ts +2 -0
  93. package/resources/index.d.ts.map +1 -1
  94. package/resources/index.js +7 -1
  95. package/resources/index.js.map +1 -1
  96. package/resources/index.mjs +2 -0
  97. package/resources/index.mjs.map +1 -1
  98. package/resources/responses/index.d.ts +3 -0
  99. package/resources/responses/index.d.ts.map +1 -0
  100. package/resources/responses/index.js +10 -0
  101. package/resources/responses/index.js.map +1 -0
  102. package/resources/responses/index.mjs +4 -0
  103. package/resources/responses/index.mjs.map +1 -0
  104. package/resources/responses/input-items.d.ts +169 -0
  105. package/resources/responses/input-items.d.ts.map +1 -0
  106. package/resources/responses/input-items.js +24 -0
  107. package/resources/responses/input-items.js.map +1 -0
  108. package/resources/responses/input-items.mjs +19 -0
  109. package/resources/responses/input-items.mjs.map +1 -0
  110. package/resources/responses/responses.d.ts +2230 -0
  111. package/resources/responses/responses.d.ts.map +1 -0
  112. package/resources/responses/responses.js +77 -0
  113. package/resources/responses/responses.js.map +1 -0
  114. package/resources/responses/responses.mjs +50 -0
  115. package/resources/responses/responses.mjs.map +1 -0
  116. package/resources/shared.d.ts +99 -4
  117. package/resources/shared.d.ts.map +1 -1
  118. package/resources/uploads/uploads.d.ts +3 -4
  119. package/resources/uploads/uploads.d.ts.map +1 -1
  120. package/resources/uploads/uploads.js +3 -4
  121. package/resources/uploads/uploads.js.map +1 -1
  122. package/resources/uploads/uploads.mjs +3 -4
  123. package/resources/uploads/uploads.mjs.map +1 -1
  124. package/resources/{beta/vector-stores → vector-stores}/file-batches.d.ts +12 -4
  125. package/resources/vector-stores/file-batches.d.ts.map +1 -0
  126. package/resources/{beta/vector-stores → vector-stores}/file-batches.js +4 -4
  127. package/resources/vector-stores/file-batches.js.map +1 -0
  128. package/resources/{beta/vector-stores → vector-stores}/file-batches.mjs +4 -4
  129. package/resources/vector-stores/file-batches.mjs.map +1 -0
  130. package/resources/{beta/vector-stores → vector-stores}/files.d.ts +54 -5
  131. package/resources/vector-stores/files.d.ts.map +1 -0
  132. package/resources/{beta/vector-stores → vector-stores}/files.js +27 -4
  133. package/resources/vector-stores/files.js.map +1 -0
  134. package/resources/{beta/vector-stores → vector-stores}/files.mjs +25 -3
  135. package/resources/vector-stores/files.mjs.map +1 -0
  136. package/resources/vector-stores/index.d.ts +4 -0
  137. package/resources/vector-stores/index.d.ts.map +1 -0
  138. package/resources/{beta/vector-stores → vector-stores}/index.js +3 -1
  139. package/resources/vector-stores/index.js.map +1 -0
  140. package/resources/vector-stores/index.mjs +5 -0
  141. package/resources/vector-stores/index.mjs.map +1 -0
  142. package/resources/{beta/vector-stores → vector-stores}/vector-stores.d.ts +90 -7
  143. package/resources/vector-stores/vector-stores.d.ts.map +1 -0
  144. package/resources/{beta/vector-stores → vector-stores}/vector-stores.js +24 -4
  145. package/resources/vector-stores/vector-stores.js.map +1 -0
  146. package/resources/{beta/vector-stores → vector-stores}/vector-stores.mjs +23 -4
  147. package/resources/vector-stores/vector-stores.mjs.map +1 -0
  148. package/src/core.ts +2 -2
  149. package/src/helpers/zod.ts +46 -0
  150. package/src/index.ts +54 -4
  151. package/src/lib/ResponsesParser.ts +262 -0
  152. package/src/lib/parser.ts +28 -0
  153. package/src/lib/responses/EventTypes.ts +76 -0
  154. package/src/lib/responses/ResponseStream.ts +298 -0
  155. package/src/resources/beta/assistants.ts +46 -9
  156. package/src/resources/beta/beta.ts +0 -37
  157. package/src/resources/beta/index.ts +0 -16
  158. package/src/resources/beta/threads/runs/runs.ts +3 -4
  159. package/src/resources/beta/threads/threads.ts +83 -7
  160. package/src/resources/chat/chat.ts +3 -43
  161. package/src/resources/chat/completions/completions.ts +224 -66
  162. package/src/resources/chat/completions/index.ts +0 -1
  163. package/src/resources/chat/completions/messages.ts +1 -1
  164. package/src/resources/chat/index.ts +1 -2
  165. package/src/resources/files.ts +9 -17
  166. package/src/resources/index.ts +20 -0
  167. package/src/resources/responses/index.ts +9 -0
  168. package/src/resources/responses/input-items.ts +276 -0
  169. package/src/resources/responses/responses.ts +2761 -0
  170. package/src/resources/shared.ts +152 -4
  171. package/src/resources/uploads/uploads.ts +3 -4
  172. package/src/resources/{beta/vector-stores → vector-stores}/file-batches.ts +16 -7
  173. package/src/resources/{beta/vector-stores → vector-stores}/files.ts +85 -4
  174. package/src/resources/{beta/vector-stores → vector-stores}/index.ts +6 -0
  175. package/src/resources/{beta/vector-stores → vector-stores}/vector-stores.ts +125 -5
  176. package/src/streaming.ts +1 -1
  177. package/src/version.ts +1 -1
  178. package/streaming.js +1 -1
  179. package/streaming.js.map +1 -1
  180. package/streaming.mjs +1 -1
  181. package/streaming.mjs.map +1 -1
  182. package/version.d.ts +1 -1
  183. package/version.js +1 -1
  184. package/version.mjs +1 -1
  185. package/resources/beta/vector-stores/file-batches.d.ts.map +0 -1
  186. package/resources/beta/vector-stores/file-batches.js.map +0 -1
  187. package/resources/beta/vector-stores/file-batches.mjs.map +0 -1
  188. package/resources/beta/vector-stores/files.d.ts.map +0 -1
  189. package/resources/beta/vector-stores/files.js.map +0 -1
  190. package/resources/beta/vector-stores/files.mjs.map +0 -1
  191. package/resources/beta/vector-stores/index.d.ts +0 -4
  192. package/resources/beta/vector-stores/index.d.ts.map +0 -1
  193. package/resources/beta/vector-stores/index.js.map +0 -1
  194. package/resources/beta/vector-stores/index.mjs +0 -5
  195. package/resources/beta/vector-stores/index.mjs.map +0 -1
  196. package/resources/beta/vector-stores/vector-stores.d.ts.map +0 -1
  197. package/resources/beta/vector-stores/vector-stores.js.map +0 -1
  198. package/resources/beta/vector-stores/vector-stores.mjs.map +0 -1
@@ -0,0 +1,238 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _ResponseStream_instances, _ResponseStream_params, _ResponseStream_currentResponseSnapshot, _ResponseStream_finalResponse, _ResponseStream_beginRequest, _ResponseStream_addEvent, _ResponseStream_endRequest, _ResponseStream_accumulateResponse;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.ResponseStream = void 0;
16
+ const error_1 = require("../../error.js");
17
+ const EventStream_1 = require("../EventStream.js");
18
+ const ResponsesParser_1 = require("../ResponsesParser.js");
19
+ class ResponseStream extends EventStream_1.EventStream {
20
+ constructor(params) {
21
+ super();
22
+ _ResponseStream_instances.add(this);
23
+ _ResponseStream_params.set(this, void 0);
24
+ _ResponseStream_currentResponseSnapshot.set(this, void 0);
25
+ _ResponseStream_finalResponse.set(this, void 0);
26
+ __classPrivateFieldSet(this, _ResponseStream_params, params, "f");
27
+ }
28
+ static createResponse(client, params, options) {
29
+ const runner = new ResponseStream(params);
30
+ runner._run(() => runner._createResponse(client, params, {
31
+ ...options,
32
+ headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' },
33
+ }));
34
+ return runner;
35
+ }
36
+ async _createResponse(client, params, options) {
37
+ const signal = options?.signal;
38
+ if (signal) {
39
+ if (signal.aborted)
40
+ this.controller.abort();
41
+ signal.addEventListener('abort', () => this.controller.abort());
42
+ }
43
+ __classPrivateFieldGet(this, _ResponseStream_instances, "m", _ResponseStream_beginRequest).call(this);
44
+ const stream = await client.responses.create({ ...params, stream: true }, { ...options, signal: this.controller.signal });
45
+ this._connected();
46
+ for await (const event of stream) {
47
+ __classPrivateFieldGet(this, _ResponseStream_instances, "m", _ResponseStream_addEvent).call(this, event);
48
+ }
49
+ if (stream.controller.signal?.aborted) {
50
+ throw new error_1.APIUserAbortError();
51
+ }
52
+ return __classPrivateFieldGet(this, _ResponseStream_instances, "m", _ResponseStream_endRequest).call(this);
53
+ }
54
+ [(_ResponseStream_params = new WeakMap(), _ResponseStream_currentResponseSnapshot = new WeakMap(), _ResponseStream_finalResponse = new WeakMap(), _ResponseStream_instances = new WeakSet(), _ResponseStream_beginRequest = function _ResponseStream_beginRequest() {
55
+ if (this.ended)
56
+ return;
57
+ __classPrivateFieldSet(this, _ResponseStream_currentResponseSnapshot, undefined, "f");
58
+ }, _ResponseStream_addEvent = function _ResponseStream_addEvent(event) {
59
+ if (this.ended)
60
+ return;
61
+ const response = __classPrivateFieldGet(this, _ResponseStream_instances, "m", _ResponseStream_accumulateResponse).call(this, event);
62
+ this._emit('event', event);
63
+ switch (event.type) {
64
+ case 'response.output_text.delta': {
65
+ const output = response.output[event.output_index];
66
+ if (!output) {
67
+ throw new error_1.OpenAIError(`missing output at index ${event.output_index}`);
68
+ }
69
+ if (output.type === 'message') {
70
+ const content = output.content[event.content_index];
71
+ if (!content) {
72
+ throw new error_1.OpenAIError(`missing content at index ${event.content_index}`);
73
+ }
74
+ if (content.type !== 'output_text') {
75
+ throw new error_1.OpenAIError(`expected content to be 'output_text', got ${content.type}`);
76
+ }
77
+ this._emit('response.output_text.delta', {
78
+ ...event,
79
+ snapshot: content.text,
80
+ });
81
+ }
82
+ break;
83
+ }
84
+ case 'response.function_call_arguments.delta': {
85
+ const output = response.output[event.output_index];
86
+ if (!output) {
87
+ throw new error_1.OpenAIError(`missing output at index ${event.output_index}`);
88
+ }
89
+ if (output.type === 'function_call') {
90
+ this._emit('response.function_call_arguments.delta', {
91
+ ...event,
92
+ snapshot: output.arguments,
93
+ });
94
+ }
95
+ break;
96
+ }
97
+ default:
98
+ // @ts-ignore
99
+ this._emit(event.type, event);
100
+ break;
101
+ }
102
+ }, _ResponseStream_endRequest = function _ResponseStream_endRequest() {
103
+ if (this.ended) {
104
+ throw new error_1.OpenAIError(`stream has ended, this shouldn't happen`);
105
+ }
106
+ const snapshot = __classPrivateFieldGet(this, _ResponseStream_currentResponseSnapshot, "f");
107
+ if (!snapshot) {
108
+ throw new error_1.OpenAIError(`request ended without sending any events`);
109
+ }
110
+ __classPrivateFieldSet(this, _ResponseStream_currentResponseSnapshot, undefined, "f");
111
+ const parsedResponse = finalizeResponse(snapshot, __classPrivateFieldGet(this, _ResponseStream_params, "f"));
112
+ __classPrivateFieldSet(this, _ResponseStream_finalResponse, parsedResponse, "f");
113
+ return parsedResponse;
114
+ }, _ResponseStream_accumulateResponse = function _ResponseStream_accumulateResponse(event) {
115
+ let snapshot = __classPrivateFieldGet(this, _ResponseStream_currentResponseSnapshot, "f");
116
+ if (!snapshot) {
117
+ if (event.type !== 'response.created') {
118
+ throw new error_1.OpenAIError(`When snapshot hasn't been set yet, expected 'response.created' event, got ${event.type}`);
119
+ }
120
+ snapshot = __classPrivateFieldSet(this, _ResponseStream_currentResponseSnapshot, event.response, "f");
121
+ return snapshot;
122
+ }
123
+ switch (event.type) {
124
+ case 'response.output_item.added': {
125
+ snapshot.output.push(event.item);
126
+ break;
127
+ }
128
+ case 'response.content_part.added': {
129
+ const output = snapshot.output[event.output_index];
130
+ if (!output) {
131
+ throw new error_1.OpenAIError(`missing output at index ${event.output_index}`);
132
+ }
133
+ if (output.type === 'message') {
134
+ output.content.push(event.part);
135
+ }
136
+ break;
137
+ }
138
+ case 'response.output_text.delta': {
139
+ const output = snapshot.output[event.output_index];
140
+ if (!output) {
141
+ throw new error_1.OpenAIError(`missing output at index ${event.output_index}`);
142
+ }
143
+ if (output.type === 'message') {
144
+ const content = output.content[event.content_index];
145
+ if (!content) {
146
+ throw new error_1.OpenAIError(`missing content at index ${event.content_index}`);
147
+ }
148
+ if (content.type !== 'output_text') {
149
+ throw new error_1.OpenAIError(`expected content to be 'output_text', got ${content.type}`);
150
+ }
151
+ content.text += event.delta;
152
+ }
153
+ break;
154
+ }
155
+ case 'response.function_call_arguments.delta': {
156
+ const output = snapshot.output[event.output_index];
157
+ if (!output) {
158
+ throw new error_1.OpenAIError(`missing output at index ${event.output_index}`);
159
+ }
160
+ if (output.type === 'function_call') {
161
+ output.arguments += event.delta;
162
+ }
163
+ break;
164
+ }
165
+ case 'response.completed': {
166
+ __classPrivateFieldSet(this, _ResponseStream_currentResponseSnapshot, event.response, "f");
167
+ break;
168
+ }
169
+ }
170
+ return snapshot;
171
+ }, Symbol.asyncIterator)]() {
172
+ const pushQueue = [];
173
+ const readQueue = [];
174
+ let done = false;
175
+ this.on('event', (event) => {
176
+ const reader = readQueue.shift();
177
+ if (reader) {
178
+ reader.resolve(event);
179
+ }
180
+ else {
181
+ pushQueue.push(event);
182
+ }
183
+ });
184
+ this.on('end', () => {
185
+ done = true;
186
+ for (const reader of readQueue) {
187
+ reader.resolve(undefined);
188
+ }
189
+ readQueue.length = 0;
190
+ });
191
+ this.on('abort', (err) => {
192
+ done = true;
193
+ for (const reader of readQueue) {
194
+ reader.reject(err);
195
+ }
196
+ readQueue.length = 0;
197
+ });
198
+ this.on('error', (err) => {
199
+ done = true;
200
+ for (const reader of readQueue) {
201
+ reader.reject(err);
202
+ }
203
+ readQueue.length = 0;
204
+ });
205
+ return {
206
+ next: async () => {
207
+ if (!pushQueue.length) {
208
+ if (done) {
209
+ return { value: undefined, done: true };
210
+ }
211
+ return new Promise((resolve, reject) => readQueue.push({ resolve, reject })).then((event) => (event ? { value: event, done: false } : { value: undefined, done: true }));
212
+ }
213
+ const event = pushQueue.shift();
214
+ return { value: event, done: false };
215
+ },
216
+ return: async () => {
217
+ this.abort();
218
+ return { value: undefined, done: true };
219
+ },
220
+ };
221
+ }
222
+ /**
223
+ * @returns a promise that resolves with the final Response, or rejects
224
+ * if an error occurred or the stream ended prematurely without producing a REsponse.
225
+ */
226
+ async finalResponse() {
227
+ await this.done();
228
+ const response = __classPrivateFieldGet(this, _ResponseStream_finalResponse, "f");
229
+ if (!response)
230
+ throw new error_1.OpenAIError('stream ended without producing a ChatCompletion');
231
+ return response;
232
+ }
233
+ }
234
+ exports.ResponseStream = ResponseStream;
235
+ function finalizeResponse(snapshot, params) {
236
+ return (0, ResponsesParser_1.maybeParseResponse)(snapshot, params);
237
+ }
238
+ //# sourceMappingURL=ResponseStream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ResponseStream.js","sourceRoot":"","sources":["../../src/lib/responses/ResponseStream.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAQA,0CAA6D;AAE7D,mDAA8D;AAE9D,2DAAwD;AAsBxD,MAAa,cACX,SAAQ,yBAA2B;IAOnC,YAAY,MAAsC;QAChD,KAAK,EAAE,CAAC;;QALV,yCAAwC;QACxC,0DAA+C;QAC/C,gDAAoD;QAIlD,uBAAA,IAAI,0BAAW,MAAM,MAAA,CAAC;IACxB,CAAC;IAED,MAAM,CAAC,cAAc,CACnB,MAAc,EACd,MAA4B,EAC5B,OAA6B;QAE7B,MAAM,MAAM,GAAG,IAAI,cAAc,CAAU,MAAuC,CAAC,CAAC;QACpF,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CACf,MAAM,CAAC,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE;YACrC,GAAG,OAAO;YACV,OAAO,EAAE,EAAE,GAAG,OAAO,EAAE,OAAO,EAAE,2BAA2B,EAAE,QAAQ,EAAE;SACxE,CAAC,CACH,CAAC;QACF,OAAO,MAAM,CAAC;IAChB,CAAC;IAsES,KAAK,CAAC,eAAe,CAC7B,MAAc,EACd,MAA+B,E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CACvB,QAAkB,EAClB,MAAsC;IAEtC,OAAO,IAAA,oCAAkB,EAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;AAC9C,CAAC"}
@@ -0,0 +1,234 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _ResponseStream_instances, _ResponseStream_params, _ResponseStream_currentResponseSnapshot, _ResponseStream_finalResponse, _ResponseStream_beginRequest, _ResponseStream_addEvent, _ResponseStream_endRequest, _ResponseStream_accumulateResponse;
13
+ import { APIUserAbortError, OpenAIError } from "../../error.mjs";
14
+ import { EventStream } from "../EventStream.mjs";
15
+ import { maybeParseResponse } from "../ResponsesParser.mjs";
16
+ export class ResponseStream extends EventStream {
17
+ constructor(params) {
18
+ super();
19
+ _ResponseStream_instances.add(this);
20
+ _ResponseStream_params.set(this, void 0);
21
+ _ResponseStream_currentResponseSnapshot.set(this, void 0);
22
+ _ResponseStream_finalResponse.set(this, void 0);
23
+ __classPrivateFieldSet(this, _ResponseStream_params, params, "f");
24
+ }
25
+ static createResponse(client, params, options) {
26
+ const runner = new ResponseStream(params);
27
+ runner._run(() => runner._createResponse(client, params, {
28
+ ...options,
29
+ headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' },
30
+ }));
31
+ return runner;
32
+ }
33
+ async _createResponse(client, params, options) {
34
+ const signal = options?.signal;
35
+ if (signal) {
36
+ if (signal.aborted)
37
+ this.controller.abort();
38
+ signal.addEventListener('abort', () => this.controller.abort());
39
+ }
40
+ __classPrivateFieldGet(this, _ResponseStream_instances, "m", _ResponseStream_beginRequest).call(this);
41
+ const stream = await client.responses.create({ ...params, stream: true }, { ...options, signal: this.controller.signal });
42
+ this._connected();
43
+ for await (const event of stream) {
44
+ __classPrivateFieldGet(this, _ResponseStream_instances, "m", _ResponseStream_addEvent).call(this, event);
45
+ }
46
+ if (stream.controller.signal?.aborted) {
47
+ throw new APIUserAbortError();
48
+ }
49
+ return __classPrivateFieldGet(this, _ResponseStream_instances, "m", _ResponseStream_endRequest).call(this);
50
+ }
51
+ [(_ResponseStream_params = new WeakMap(), _ResponseStream_currentResponseSnapshot = new WeakMap(), _ResponseStream_finalResponse = new WeakMap(), _ResponseStream_instances = new WeakSet(), _ResponseStream_beginRequest = function _ResponseStream_beginRequest() {
52
+ if (this.ended)
53
+ return;
54
+ __classPrivateFieldSet(this, _ResponseStream_currentResponseSnapshot, undefined, "f");
55
+ }, _ResponseStream_addEvent = function _ResponseStream_addEvent(event) {
56
+ if (this.ended)
57
+ return;
58
+ const response = __classPrivateFieldGet(this, _ResponseStream_instances, "m", _ResponseStream_accumulateResponse).call(this, event);
59
+ this._emit('event', event);
60
+ switch (event.type) {
61
+ case 'response.output_text.delta': {
62
+ const output = response.output[event.output_index];
63
+ if (!output) {
64
+ throw new OpenAIError(`missing output at index ${event.output_index}`);
65
+ }
66
+ if (output.type === 'message') {
67
+ const content = output.content[event.content_index];
68
+ if (!content) {
69
+ throw new OpenAIError(`missing content at index ${event.content_index}`);
70
+ }
71
+ if (content.type !== 'output_text') {
72
+ throw new OpenAIError(`expected content to be 'output_text', got ${content.type}`);
73
+ }
74
+ this._emit('response.output_text.delta', {
75
+ ...event,
76
+ snapshot: content.text,
77
+ });
78
+ }
79
+ break;
80
+ }
81
+ case 'response.function_call_arguments.delta': {
82
+ const output = response.output[event.output_index];
83
+ if (!output) {
84
+ throw new OpenAIError(`missing output at index ${event.output_index}`);
85
+ }
86
+ if (output.type === 'function_call') {
87
+ this._emit('response.function_call_arguments.delta', {
88
+ ...event,
89
+ snapshot: output.arguments,
90
+ });
91
+ }
92
+ break;
93
+ }
94
+ default:
95
+ // @ts-ignore
96
+ this._emit(event.type, event);
97
+ break;
98
+ }
99
+ }, _ResponseStream_endRequest = function _ResponseStream_endRequest() {
100
+ if (this.ended) {
101
+ throw new OpenAIError(`stream has ended, this shouldn't happen`);
102
+ }
103
+ const snapshot = __classPrivateFieldGet(this, _ResponseStream_currentResponseSnapshot, "f");
104
+ if (!snapshot) {
105
+ throw new OpenAIError(`request ended without sending any events`);
106
+ }
107
+ __classPrivateFieldSet(this, _ResponseStream_currentResponseSnapshot, undefined, "f");
108
+ const parsedResponse = finalizeResponse(snapshot, __classPrivateFieldGet(this, _ResponseStream_params, "f"));
109
+ __classPrivateFieldSet(this, _ResponseStream_finalResponse, parsedResponse, "f");
110
+ return parsedResponse;
111
+ }, _ResponseStream_accumulateResponse = function _ResponseStream_accumulateResponse(event) {
112
+ let snapshot = __classPrivateFieldGet(this, _ResponseStream_currentResponseSnapshot, "f");
113
+ if (!snapshot) {
114
+ if (event.type !== 'response.created') {
115
+ throw new OpenAIError(`When snapshot hasn't been set yet, expected 'response.created' event, got ${event.type}`);
116
+ }
117
+ snapshot = __classPrivateFieldSet(this, _ResponseStream_currentResponseSnapshot, event.response, "f");
118
+ return snapshot;
119
+ }
120
+ switch (event.type) {
121
+ case 'response.output_item.added': {
122
+ snapshot.output.push(event.item);
123
+ break;
124
+ }
125
+ case 'response.content_part.added': {
126
+ const output = snapshot.output[event.output_index];
127
+ if (!output) {
128
+ throw new OpenAIError(`missing output at index ${event.output_index}`);
129
+ }
130
+ if (output.type === 'message') {
131
+ output.content.push(event.part);
132
+ }
133
+ break;
134
+ }
135
+ case 'response.output_text.delta': {
136
+ const output = snapshot.output[event.output_index];
137
+ if (!output) {
138
+ throw new OpenAIError(`missing output at index ${event.output_index}`);
139
+ }
140
+ if (output.type === 'message') {
141
+ const content = output.content[event.content_index];
142
+ if (!content) {
143
+ throw new OpenAIError(`missing content at index ${event.content_index}`);
144
+ }
145
+ if (content.type !== 'output_text') {
146
+ throw new OpenAIError(`expected content to be 'output_text', got ${content.type}`);
147
+ }
148
+ content.text += event.delta;
149
+ }
150
+ break;
151
+ }
152
+ case 'response.function_call_arguments.delta': {
153
+ const output = snapshot.output[event.output_index];
154
+ if (!output) {
155
+ throw new OpenAIError(`missing output at index ${event.output_index}`);
156
+ }
157
+ if (output.type === 'function_call') {
158
+ output.arguments += event.delta;
159
+ }
160
+ break;
161
+ }
162
+ case 'response.completed': {
163
+ __classPrivateFieldSet(this, _ResponseStream_currentResponseSnapshot, event.response, "f");
164
+ break;
165
+ }
166
+ }
167
+ return snapshot;
168
+ }, Symbol.asyncIterator)]() {
169
+ const pushQueue = [];
170
+ const readQueue = [];
171
+ let done = false;
172
+ this.on('event', (event) => {
173
+ const reader = readQueue.shift();
174
+ if (reader) {
175
+ reader.resolve(event);
176
+ }
177
+ else {
178
+ pushQueue.push(event);
179
+ }
180
+ });
181
+ this.on('end', () => {
182
+ done = true;
183
+ for (const reader of readQueue) {
184
+ reader.resolve(undefined);
185
+ }
186
+ readQueue.length = 0;
187
+ });
188
+ this.on('abort', (err) => {
189
+ done = true;
190
+ for (const reader of readQueue) {
191
+ reader.reject(err);
192
+ }
193
+ readQueue.length = 0;
194
+ });
195
+ this.on('error', (err) => {
196
+ done = true;
197
+ for (const reader of readQueue) {
198
+ reader.reject(err);
199
+ }
200
+ readQueue.length = 0;
201
+ });
202
+ return {
203
+ next: async () => {
204
+ if (!pushQueue.length) {
205
+ if (done) {
206
+ return { value: undefined, done: true };
207
+ }
208
+ return new Promise((resolve, reject) => readQueue.push({ resolve, reject })).then((event) => (event ? { value: event, done: false } : { value: undefined, done: true }));
209
+ }
210
+ const event = pushQueue.shift();
211
+ return { value: event, done: false };
212
+ },
213
+ return: async () => {
214
+ this.abort();
215
+ return { value: undefined, done: true };
216
+ },
217
+ };
218
+ }
219
+ /**
220
+ * @returns a promise that resolves with the final Response, or rejects
221
+ * if an error occurred or the stream ended prematurely without producing a REsponse.
222
+ */
223
+ async finalResponse() {
224
+ await this.done();
225
+ const response = __classPrivateFieldGet(this, _ResponseStream_finalResponse, "f");
226
+ if (!response)
227
+ throw new OpenAIError('stream ended without producing a ChatCompletion');
228
+ return response;
229
+ }
230
+ }
231
+ function finalizeResponse(snapshot, params) {
232
+ return maybeParseResponse(snapshot, params);
233
+ }
234
+ //# sourceMappingURL=ResponseStream.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ResponseStream.mjs","sourceRoot":"","sources":["../../src/lib/responses/ResponseStream.ts"],"names":[],"mappings":";;;;;;;;;;;;OAQO,EAAE,iBAAiB,EAAE,WAAW,EAAE;OAElC,EAAmB,WAAW,EAAE;OAEhC,EAAE,kBAAkB,EAAE;AAsB7B,MAAM,OAAO,cACX,SAAQ,WAA2B;IAOnC,YAAY,MAAsC;QAChD,KAAK,EAAE,CAAC;;QALV,yCAAwC;QACxC,0DAA+C;QAC/C,gDAAoD;QAIlD,uBAAA,IAAI,0BAAW,MAAM,MAAA,CAAC;IACxB,CAAC;IAED,MAAM,CAAC,cAAc,CACnB,MAAc,EACd,MAA4B,EAC5B,OAA6B;QAE7B,MAAM,MAAM,GAAG,IAAI,cAAc,CAAU,MAAuC,CAAC,CAAC;QACpF,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CACf,MAAM,CAAC,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE;YACrC,GAAG,OAAO;YACV,OAAO,EAAE,EAAE,GAAG,OAAO,EAAE,OAAO,EAAE,2BAA2B,EAAE,QAAQ,EAAE;SACxE,CAAC,CACH,CAAC;QACF,OAAO,MAAM,CAAC;IAChB,CAA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F;AAED,SAAS,gBAAgB,CACvB,QAAkB,EAClB,MAAsC;IAEtC,OAAO,kBAAkB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;AAC9C,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openai",
3
- "version": "4.86.2",
3
+ "version": "4.87.0",
4
4
  "description": "The official TypeScript library for the OpenAI API",
5
5
  "author": "OpenAI <support@openai.com>",
6
6
  "types": "./index.d.ts",
@@ -1,10 +1,8 @@
1
1
  import { APIResource } from "../../resource.js";
2
2
  import * as Core from "../../core.js";
3
3
  import * as Shared from "../shared.js";
4
- import * as ChatAPI from "../chat/chat.js";
5
4
  import * as MessagesAPI from "./threads/messages.js";
6
5
  import * as ThreadsAPI from "./threads/threads.js";
7
- import * as VectorStoresAPI from "./vector-stores/vector-stores.js";
8
6
  import * as RunsAPI from "./threads/runs/runs.js";
9
7
  import * as StepsAPI from "./threads/runs/steps.js";
10
8
  import { CursorPage, type CursorPageParams } from "../../pagination.js";
@@ -878,7 +876,7 @@ export interface AssistantCreateParams {
878
876
  * [Model overview](https://platform.openai.com/docs/models) for descriptions of
879
877
  * them.
880
878
  */
881
- model: (string & {}) | ChatAPI.ChatModel;
879
+ model: (string & {}) | Shared.ChatModel;
882
880
  /**
883
881
  * The description of the assistant. The maximum length is 512 characters.
884
882
  */
@@ -902,14 +900,14 @@ export interface AssistantCreateParams {
902
900
  */
903
901
  name?: string | null;
904
902
  /**
905
- * **o1 and o3-mini models only**
903
+ * **o-series models only**
906
904
  *
907
905
  * Constrains effort on reasoning for
908
906
  * [reasoning models](https://platform.openai.com/docs/guides/reasoning). Currently
909
907
  * supported values are `low`, `medium`, and `high`. Reducing reasoning effort can
910
908
  * result in faster responses and fewer tokens used on reasoning in a response.
911
909
  */
912
- reasoning_effort?: 'low' | 'medium' | 'high' | null;
910
+ reasoning_effort?: Shared.ReasoningEffort | null;
913
911
  /**
914
912
  * Specifies the format that the model must output. Compatible with
915
913
  * [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
@@ -1001,9 +999,9 @@ export declare namespace AssistantCreateParams {
1001
999
  interface VectorStore {
1002
1000
  /**
1003
1001
  * The chunking strategy used to chunk the file(s). If not set, will use the `auto`
1004
- * strategy. Only applicable if `file_ids` is non-empty.
1002
+ * strategy.
1005
1003
  */
1006
- chunking_strategy?: VectorStoresAPI.FileChunkingStrategyParam;
1004
+ chunking_strategy?: VectorStore.Auto | VectorStore.Static;
1007
1005
  /**
1008
1006
  * A list of [file](https://platform.openai.com/docs/api-reference/files) IDs to
1009
1007
  * add to the vector store. There can be a maximum of 10000 files in a vector
@@ -1020,6 +1018,40 @@ export declare namespace AssistantCreateParams {
1020
1018
  */
1021
1019
  metadata?: Shared.Metadata | null;
1022
1020
  }
1021
+ namespace VectorStore {
1022
+ /**
1023
+ * The default strategy. This strategy currently uses a `max_chunk_size_tokens` of
1024
+ * `800` and `chunk_overlap_tokens` of `400`.
1025
+ */
1026
+ interface Auto {
1027
+ /**
1028
+ * Always `auto`.
1029
+ */
1030
+ type: 'auto';
1031
+ }
1032
+ interface Static {
1033
+ static: Static.Static;
1034
+ /**
1035
+ * Always `static`.
1036
+ */
1037
+ type: 'static';
1038
+ }
1039
+ namespace Static {
1040
+ interface Static {
1041
+ /**
1042
+ * The number of tokens that overlap between chunks. The default value is `400`.
1043
+ *
1044
+ * Note that the overlap must not exceed half of `max_chunk_size_tokens`.
1045
+ */
1046
+ chunk_overlap_tokens: number;
1047
+ /**
1048
+ * The maximum number of tokens in each chunk. The default value is `800`. The
1049
+ * minimum value is `100` and the maximum value is `4096`.
1050
+ */
1051
+ max_chunk_size_tokens: number;
1052
+ }
1053
+ }
1054
+ }
1023
1055
  }
1024
1056
  }
1025
1057
  }
@@ -1055,14 +1087,14 @@ export interface AssistantUpdateParams {
1055
1087
  */
1056
1088
  name?: string | null;
1057
1089
  /**
1058
- * **o1 and o3-mini models only**
1090
+ * **o-series models only**
1059
1091
  *
1060
1092
  * Constrains effort on reasoning for
1061
1093
  * [reasoning models](https://platform.openai.com/docs/guides/reasoning). Currently
1062
1094
  * supported values are `low`, `medium`, and `high`. Reducing reasoning effort can
1063
1095
  * result in faster responses and fewer tokens used on reasoning in a response.
1064
1096
  */
1065
- reasoning_effort?: 'low' | 'medium' | 'high' | null;
1097
+ reasoning_effort?: Shared.ReasoningEffort | null;
1066
1098
  /**
1067
1099
  * Specifies the format that the model must output. Compatible with
1068
1100
  * [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),