openai 7.5.0 → 7.6.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 (597) hide show
  1. package/CHANGELOG.md +105 -0
  2. package/README.md +46 -2
  3. package/auth/index.d.mts +1 -1
  4. package/auth/index.d.mts.map +1 -1
  5. package/auth/index.d.ts +1 -1
  6. package/auth/index.d.ts.map +1 -1
  7. package/auth/index.js.map +1 -1
  8. package/auth/index.mjs.map +1 -1
  9. package/auth/subject-token-providers.d.mts.map +1 -1
  10. package/auth/subject-token-providers.d.ts.map +1 -1
  11. package/auth/subject-token-providers.js +176 -4
  12. package/auth/subject-token-providers.js.map +1 -1
  13. package/auth/subject-token-providers.mjs +176 -4
  14. package/auth/subject-token-providers.mjs.map +1 -1
  15. package/auth/types.d.mts +17 -0
  16. package/auth/types.d.mts.map +1 -1
  17. package/auth/types.d.ts +17 -0
  18. package/auth/types.d.ts.map +1 -1
  19. package/auth/workload-identity-auth.d.mts.map +1 -1
  20. package/auth/workload-identity-auth.d.ts.map +1 -1
  21. package/auth/workload-identity-auth.js +80 -7
  22. package/auth/workload-identity-auth.js.map +1 -1
  23. package/auth/workload-identity-auth.mjs +80 -7
  24. package/auth/workload-identity-auth.mjs.map +1 -1
  25. package/auth/x509-transport.d.mts +5 -0
  26. package/auth/x509-transport.d.mts.map +1 -0
  27. package/auth/x509-transport.d.ts +5 -0
  28. package/auth/x509-transport.d.ts.map +1 -0
  29. package/auth/x509-transport.js +39 -0
  30. package/auth/x509-transport.js.map +1 -0
  31. package/auth/x509-transport.mjs +36 -0
  32. package/auth/x509-transport.mjs.map +1 -0
  33. package/azure.d.mts +11 -2
  34. package/azure.d.mts.map +1 -1
  35. package/azure.d.ts +11 -2
  36. package/azure.d.ts.map +1 -1
  37. package/azure.js +7 -1
  38. package/azure.js.map +1 -1
  39. package/azure.mjs +7 -1
  40. package/azure.mjs.map +1 -1
  41. package/bedrock.d.mts +6 -2
  42. package/bedrock.d.mts.map +1 -1
  43. package/bedrock.d.ts +6 -2
  44. package/bedrock.d.ts.map +1 -1
  45. package/bedrock.js +34 -4
  46. package/bedrock.js.map +1 -1
  47. package/bedrock.mjs +35 -5
  48. package/bedrock.mjs.map +1 -1
  49. package/beta/realtime/internal-base.d.mts +2 -0
  50. package/beta/realtime/internal-base.d.mts.map +1 -1
  51. package/beta/realtime/internal-base.d.ts +2 -0
  52. package/beta/realtime/internal-base.d.ts.map +1 -1
  53. package/beta/realtime/internal-base.js +15 -0
  54. package/beta/realtime/internal-base.js.map +1 -1
  55. package/beta/realtime/internal-base.mjs +14 -0
  56. package/beta/realtime/internal-base.mjs.map +1 -1
  57. package/beta/realtime/websocket.d.mts +11 -5
  58. package/beta/realtime/websocket.d.mts.map +1 -1
  59. package/beta/realtime/websocket.d.ts +11 -5
  60. package/beta/realtime/websocket.d.ts.map +1 -1
  61. package/beta/realtime/websocket.js +106 -31
  62. package/beta/realtime/websocket.js.map +1 -1
  63. package/beta/realtime/websocket.mjs +107 -32
  64. package/beta/realtime/websocket.mjs.map +1 -1
  65. package/beta/realtime/ws.d.mts +0 -2
  66. package/beta/realtime/ws.d.mts.map +1 -1
  67. package/beta/realtime/ws.d.ts +0 -2
  68. package/beta/realtime/ws.d.ts.map +1 -1
  69. package/beta/realtime/ws.js +24 -5
  70. package/beta/realtime/ws.js.map +1 -1
  71. package/beta/realtime/ws.mjs +25 -6
  72. package/beta/realtime/ws.mjs.map +1 -1
  73. package/client.d.mts +17 -2
  74. package/client.d.mts.map +1 -1
  75. package/client.d.ts +17 -2
  76. package/client.d.ts.map +1 -1
  77. package/client.js +422 -70
  78. package/client.js.map +1 -1
  79. package/client.mjs +423 -71
  80. package/client.mjs.map +1 -1
  81. package/core/EventEmitter.d.mts.map +1 -1
  82. package/core/EventEmitter.d.ts.map +1 -1
  83. package/core/EventEmitter.js +67 -9
  84. package/core/EventEmitter.js.map +1 -1
  85. package/core/EventEmitter.mjs +68 -10
  86. package/core/EventEmitter.mjs.map +1 -1
  87. package/core/streaming.d.mts +1 -0
  88. package/core/streaming.d.mts.map +1 -1
  89. package/core/streaming.d.ts +1 -0
  90. package/core/streaming.d.ts.map +1 -1
  91. package/core/streaming.js +58 -19
  92. package/core/streaming.js.map +1 -1
  93. package/core/streaming.mjs +58 -19
  94. package/core/streaming.mjs.map +1 -1
  95. package/helpers/audio.d.mts +5 -5
  96. package/helpers/audio.d.mts.map +1 -1
  97. package/helpers/audio.d.ts +5 -5
  98. package/helpers/audio.d.ts.map +1 -1
  99. package/helpers/audio.js +93 -22
  100. package/helpers/audio.js.map +1 -1
  101. package/helpers/audio.mjs +93 -22
  102. package/helpers/audio.mjs.map +1 -1
  103. package/helpers/standard-schema.d.mts.map +1 -1
  104. package/helpers/standard-schema.d.ts.map +1 -1
  105. package/helpers/standard-schema.js +34 -14
  106. package/helpers/standard-schema.js.map +1 -1
  107. package/helpers/standard-schema.mjs +35 -15
  108. package/helpers/standard-schema.mjs.map +1 -1
  109. package/helpers/zod-v3-strict-schema.d.mts +7 -0
  110. package/helpers/zod-v3-strict-schema.d.mts.map +1 -0
  111. package/helpers/zod-v3-strict-schema.d.ts +7 -0
  112. package/helpers/zod-v3-strict-schema.d.ts.map +1 -0
  113. package/helpers/zod-v3-strict-schema.js +403 -0
  114. package/helpers/zod-v3-strict-schema.js.map +1 -0
  115. package/helpers/zod-v3-strict-schema.mjs +399 -0
  116. package/helpers/zod-v3-strict-schema.mjs.map +1 -0
  117. package/helpers/zod.d.mts.map +1 -1
  118. package/helpers/zod.d.ts.map +1 -1
  119. package/helpers/zod.js +11 -3
  120. package/helpers/zod.js.map +1 -1
  121. package/helpers/zod.mjs +12 -4
  122. package/helpers/zod.mjs.map +1 -1
  123. package/index.d.mts +1 -1
  124. package/index.d.mts.map +1 -1
  125. package/index.d.ts +1 -1
  126. package/index.d.ts.map +1 -1
  127. package/index.js.map +1 -1
  128. package/index.mjs.map +1 -1
  129. package/internal/assistant-stream-delta.d.mts.map +1 -1
  130. package/internal/assistant-stream-delta.d.ts.map +1 -1
  131. package/internal/assistant-stream-delta.js +28 -8
  132. package/internal/assistant-stream-delta.js.map +1 -1
  133. package/internal/assistant-stream-delta.mjs +28 -8
  134. package/internal/assistant-stream-delta.mjs.map +1 -1
  135. package/internal/auth/x509-token-exchange.d.mts +16 -0
  136. package/internal/auth/x509-token-exchange.d.mts.map +1 -0
  137. package/internal/auth/x509-token-exchange.d.ts +16 -0
  138. package/internal/auth/x509-token-exchange.d.ts.map +1 -0
  139. package/internal/auth/x509-token-exchange.js +246 -0
  140. package/internal/auth/x509-token-exchange.js.map +1 -0
  141. package/internal/auth/x509-token-exchange.mjs +243 -0
  142. package/internal/auth/x509-token-exchange.mjs.map +1 -0
  143. package/internal/auth/x509-transport-capability.d.mts +37 -0
  144. package/internal/auth/x509-transport-capability.d.mts.map +1 -0
  145. package/internal/auth/x509-transport-capability.d.ts +37 -0
  146. package/internal/auth/x509-transport-capability.d.ts.map +1 -0
  147. package/internal/auth/x509-transport-capability.js +169 -0
  148. package/internal/auth/x509-transport-capability.js.map +1 -0
  149. package/internal/auth/x509-transport-capability.mjs +164 -0
  150. package/internal/auth/x509-transport-capability.mjs.map +1 -0
  151. package/internal/auth/x509-transport-registry.d.mts +63 -0
  152. package/internal/auth/x509-transport-registry.d.mts.map +1 -0
  153. package/internal/auth/x509-transport-registry.d.ts +63 -0
  154. package/internal/auth/x509-transport-registry.d.ts.map +1 -0
  155. package/internal/auth/x509-transport-registry.js +53 -0
  156. package/internal/auth/x509-transport-registry.js.map +1 -0
  157. package/internal/auth/x509-transport-registry.mjs +48 -0
  158. package/internal/auth/x509-transport-registry.mjs.map +1 -0
  159. package/internal/auth/x509-transport-state-browser.d.mts +21 -0
  160. package/internal/auth/x509-transport-state-browser.d.mts.map +1 -0
  161. package/internal/auth/x509-transport-state-browser.d.ts +21 -0
  162. package/internal/auth/x509-transport-state-browser.d.ts.map +1 -0
  163. package/internal/auth/x509-transport-state-browser.js +30 -0
  164. package/internal/auth/x509-transport-state-browser.js.map +1 -0
  165. package/internal/auth/x509-transport-state-browser.mjs +27 -0
  166. package/internal/auth/x509-transport-state-browser.mjs.map +1 -0
  167. package/internal/auth/x509-transport-state.cjs +30 -0
  168. package/internal/auth/x509-transport-state.cjs.map +1 -0
  169. package/internal/auth/x509-transport-state.d.cts +21 -0
  170. package/internal/auth/x509-transport-state.d.cts.map +1 -0
  171. package/internal/auth/x509-workload-identity-auth.d.mts +128 -0
  172. package/internal/auth/x509-workload-identity-auth.d.mts.map +1 -0
  173. package/internal/auth/x509-workload-identity-auth.d.ts +128 -0
  174. package/internal/auth/x509-workload-identity-auth.d.ts.map +1 -0
  175. package/internal/auth/x509-workload-identity-auth.js +767 -0
  176. package/internal/auth/x509-workload-identity-auth.js.map +1 -0
  177. package/internal/auth/x509-workload-identity-auth.mjs +757 -0
  178. package/internal/auth/x509-workload-identity-auth.mjs.map +1 -0
  179. package/internal/bedrock.d.mts +2 -0
  180. package/internal/bedrock.d.mts.map +1 -1
  181. package/internal/bedrock.d.ts +2 -0
  182. package/internal/bedrock.d.ts.map +1 -1
  183. package/internal/bedrock.js +169 -5
  184. package/internal/bedrock.js.map +1 -1
  185. package/internal/bedrock.mjs +168 -5
  186. package/internal/bedrock.mjs.map +1 -1
  187. package/internal/data-residency.d.mts +12 -0
  188. package/internal/data-residency.d.mts.map +1 -0
  189. package/internal/data-residency.d.ts +12 -0
  190. package/internal/data-residency.d.ts.map +1 -0
  191. package/internal/data-residency.js +34 -0
  192. package/internal/data-residency.js.map +1 -0
  193. package/internal/data-residency.mjs +30 -0
  194. package/internal/data-residency.mjs.map +1 -0
  195. package/internal/multipart-encoding.d.mts +8 -0
  196. package/internal/multipart-encoding.d.mts.map +1 -0
  197. package/internal/multipart-encoding.d.ts +8 -0
  198. package/internal/multipart-encoding.d.ts.map +1 -0
  199. package/internal/multipart-encoding.js +51 -0
  200. package/internal/multipart-encoding.js.map +1 -0
  201. package/internal/multipart-encoding.mjs +48 -0
  202. package/internal/multipart-encoding.mjs.map +1 -0
  203. package/internal/qs/utils.d.mts.map +1 -1
  204. package/internal/qs/utils.d.ts.map +1 -1
  205. package/internal/qs/utils.js +307 -10
  206. package/internal/qs/utils.js.map +1 -1
  207. package/internal/qs/utils.mjs +307 -10
  208. package/internal/qs/utils.mjs.map +1 -1
  209. package/internal/request-options.d.mts +7 -0
  210. package/internal/request-options.d.mts.map +1 -1
  211. package/internal/request-options.d.ts +7 -0
  212. package/internal/request-options.d.ts.map +1 -1
  213. package/internal/request-options.js +43 -1
  214. package/internal/request-options.js.map +1 -1
  215. package/internal/request-options.mjs +42 -1
  216. package/internal/request-options.mjs.map +1 -1
  217. package/internal/responses/canonical-output-text.js +3 -3
  218. package/internal/responses/canonical-output-text.js.map +1 -1
  219. package/internal/responses/canonical-output-text.mjs +3 -3
  220. package/internal/responses/canonical-output-text.mjs.map +1 -1
  221. package/internal/responses/response-accumulator.d.mts +1 -1
  222. package/internal/responses/response-accumulator.d.mts.map +1 -1
  223. package/internal/responses/response-accumulator.d.ts +1 -1
  224. package/internal/responses/response-accumulator.d.ts.map +1 -1
  225. package/internal/responses/response-accumulator.js +331 -25
  226. package/internal/responses/response-accumulator.js.map +1 -1
  227. package/internal/responses/response-accumulator.mjs +331 -25
  228. package/internal/responses/response-accumulator.mjs.map +1 -1
  229. package/internal/tslib.js +6 -0
  230. package/internal/tslib.mjs +6 -1
  231. package/internal/uploads.d.mts +10 -6
  232. package/internal/uploads.d.mts.map +1 -1
  233. package/internal/uploads.d.ts +10 -6
  234. package/internal/uploads.d.ts.map +1 -1
  235. package/internal/uploads.js +65 -24
  236. package/internal/uploads.js.map +1 -1
  237. package/internal/uploads.mjs +65 -24
  238. package/internal/uploads.mjs.map +1 -1
  239. package/internal/utils/env.d.mts.map +1 -1
  240. package/internal/utils/env.d.ts.map +1 -1
  241. package/internal/utils/env.js +9 -4
  242. package/internal/utils/env.js.map +1 -1
  243. package/internal/utils/env.mjs +9 -4
  244. package/internal/utils/env.mjs.map +1 -1
  245. package/internal/ws.d.mts.map +1 -1
  246. package/internal/ws.d.ts.map +1 -1
  247. package/internal/ws.js +23 -1
  248. package/internal/ws.js.map +1 -1
  249. package/internal/ws.mjs +23 -1
  250. package/internal/ws.mjs.map +1 -1
  251. package/lib/AbstractChatCompletionRunner.d.mts.map +1 -1
  252. package/lib/AbstractChatCompletionRunner.d.ts.map +1 -1
  253. package/lib/AbstractChatCompletionRunner.js +32 -7
  254. package/lib/AbstractChatCompletionRunner.js.map +1 -1
  255. package/lib/AbstractChatCompletionRunner.mjs +34 -9
  256. package/lib/AbstractChatCompletionRunner.mjs.map +1 -1
  257. package/lib/AssistantStream.d.mts.map +1 -1
  258. package/lib/AssistantStream.d.ts.map +1 -1
  259. package/lib/AssistantStream.js +211 -23
  260. package/lib/AssistantStream.js.map +1 -1
  261. package/lib/AssistantStream.mjs +211 -23
  262. package/lib/AssistantStream.mjs.map +1 -1
  263. package/lib/ChatCompletionStream.d.mts +1 -1
  264. package/lib/ChatCompletionStream.d.mts.map +1 -1
  265. package/lib/ChatCompletionStream.d.ts +1 -1
  266. package/lib/ChatCompletionStream.d.ts.map +1 -1
  267. package/lib/ChatCompletionStream.js +1118 -67
  268. package/lib/ChatCompletionStream.js.map +1 -1
  269. package/lib/ChatCompletionStream.mjs +1119 -68
  270. package/lib/ChatCompletionStream.mjs.map +1 -1
  271. package/lib/EventEmitter.d.mts.map +1 -1
  272. package/lib/EventEmitter.d.ts.map +1 -1
  273. package/lib/EventEmitter.js +67 -9
  274. package/lib/EventEmitter.js.map +1 -1
  275. package/lib/EventEmitter.mjs +68 -10
  276. package/lib/EventEmitter.mjs.map +1 -1
  277. package/lib/EventStream.d.mts +4 -0
  278. package/lib/EventStream.d.mts.map +1 -1
  279. package/lib/EventStream.d.ts +4 -0
  280. package/lib/EventStream.d.ts.map +1 -1
  281. package/lib/EventStream.js +1096 -11
  282. package/lib/EventStream.js.map +1 -1
  283. package/lib/EventStream.mjs +1097 -12
  284. package/lib/EventStream.mjs.map +1 -1
  285. package/lib/ResponsesParser.d.mts.map +1 -1
  286. package/lib/ResponsesParser.d.ts.map +1 -1
  287. package/lib/ResponsesParser.js +1 -1
  288. package/lib/ResponsesParser.js.map +1 -1
  289. package/lib/ResponsesParser.mjs +1 -1
  290. package/lib/ResponsesParser.mjs.map +1 -1
  291. package/lib/Util.d.mts +2 -0
  292. package/lib/Util.d.mts.map +1 -1
  293. package/lib/Util.d.ts +2 -0
  294. package/lib/Util.d.ts.map +1 -1
  295. package/lib/Util.js +7 -4
  296. package/lib/Util.js.map +1 -1
  297. package/lib/Util.mjs +7 -4
  298. package/lib/Util.mjs.map +1 -1
  299. package/lib/assistant-run-polling.d.mts +12 -0
  300. package/lib/assistant-run-polling.d.mts.map +1 -0
  301. package/lib/assistant-run-polling.d.ts +12 -0
  302. package/lib/assistant-run-polling.d.ts.map +1 -0
  303. package/lib/assistant-run-polling.js +17 -0
  304. package/lib/assistant-run-polling.js.map +1 -0
  305. package/lib/assistant-run-polling.mjs +14 -0
  306. package/lib/assistant-run-polling.mjs.map +1 -0
  307. package/lib/embeddings.d.mts +18 -0
  308. package/lib/embeddings.d.mts.map +1 -0
  309. package/lib/embeddings.d.ts +18 -0
  310. package/lib/embeddings.d.ts.map +1 -0
  311. package/lib/embeddings.js +50 -0
  312. package/lib/embeddings.js.map +1 -0
  313. package/lib/embeddings.mjs +47 -0
  314. package/lib/embeddings.mjs.map +1 -0
  315. package/lib/file-processing.d.mts +10 -0
  316. package/lib/file-processing.d.mts.map +1 -0
  317. package/lib/file-processing.d.ts +10 -0
  318. package/lib/file-processing.d.ts.map +1 -0
  319. package/lib/file-processing.js +30 -0
  320. package/lib/file-processing.js.map +1 -0
  321. package/lib/file-processing.mjs +27 -0
  322. package/lib/file-processing.mjs.map +1 -0
  323. package/lib/parser.d.mts.map +1 -1
  324. package/lib/parser.d.ts.map +1 -1
  325. package/lib/parser.js +16 -4
  326. package/lib/parser.js.map +1 -1
  327. package/lib/parser.mjs +16 -4
  328. package/lib/parser.mjs.map +1 -1
  329. package/lib/polling.d.mts +20 -0
  330. package/lib/polling.d.mts.map +1 -0
  331. package/lib/polling.d.ts +20 -0
  332. package/lib/polling.d.ts.map +1 -0
  333. package/lib/polling.js +130 -0
  334. package/lib/polling.js.map +1 -0
  335. package/lib/polling.mjs +127 -0
  336. package/lib/polling.mjs.map +1 -0
  337. package/lib/responses/ResponseStream.d.mts.map +1 -1
  338. package/lib/responses/ResponseStream.d.ts.map +1 -1
  339. package/lib/responses/ResponseStream.js +21 -11
  340. package/lib/responses/ResponseStream.js.map +1 -1
  341. package/lib/responses/ResponseStream.mjs +21 -11
  342. package/lib/responses/ResponseStream.mjs.map +1 -1
  343. package/lib/vector-store-polling.d.mts +22 -0
  344. package/lib/vector-store-polling.d.mts.map +1 -0
  345. package/lib/vector-store-polling.d.ts +22 -0
  346. package/lib/vector-store-polling.d.ts.map +1 -0
  347. package/lib/vector-store-polling.js +24 -0
  348. package/lib/vector-store-polling.js.map +1 -0
  349. package/lib/vector-store-polling.mjs +20 -0
  350. package/lib/vector-store-polling.mjs.map +1 -0
  351. package/lib/vector-store-upload.d.mts +17 -0
  352. package/lib/vector-store-upload.d.mts.map +1 -0
  353. package/lib/vector-store-upload.d.ts +17 -0
  354. package/lib/vector-store-upload.d.ts.map +1 -0
  355. package/lib/vector-store-upload.js +37 -0
  356. package/lib/vector-store-upload.js.map +1 -0
  357. package/lib/vector-store-upload.mjs +34 -0
  358. package/lib/vector-store-upload.mjs.map +1 -0
  359. package/lib/webhook-signature.d.mts +15 -0
  360. package/lib/webhook-signature.d.mts.map +1 -0
  361. package/lib/webhook-signature.d.ts +15 -0
  362. package/lib/webhook-signature.d.ts.map +1 -0
  363. package/lib/webhook-signature.js +125 -0
  364. package/lib/webhook-signature.js.map +1 -0
  365. package/lib/webhook-signature.mjs +121 -0
  366. package/lib/webhook-signature.mjs.map +1 -0
  367. package/package.json +14 -1
  368. package/realtime/internal-base.d.mts +2 -0
  369. package/realtime/internal-base.d.mts.map +1 -1
  370. package/realtime/internal-base.d.ts +2 -0
  371. package/realtime/internal-base.d.ts.map +1 -1
  372. package/realtime/internal-base.js +15 -0
  373. package/realtime/internal-base.js.map +1 -1
  374. package/realtime/internal-base.mjs +14 -0
  375. package/realtime/internal-base.mjs.map +1 -1
  376. package/realtime/websocket.d.mts +3 -2
  377. package/realtime/websocket.d.mts.map +1 -1
  378. package/realtime/websocket.d.ts +3 -2
  379. package/realtime/websocket.d.ts.map +1 -1
  380. package/realtime/websocket.js +90 -26
  381. package/realtime/websocket.js.map +1 -1
  382. package/realtime/websocket.mjs +91 -27
  383. package/realtime/websocket.mjs.map +1 -1
  384. package/realtime/ws.d.mts.map +1 -1
  385. package/realtime/ws.d.ts.map +1 -1
  386. package/realtime/ws.js +9 -1
  387. package/realtime/ws.js.map +1 -1
  388. package/realtime/ws.mjs +10 -2
  389. package/realtime/ws.mjs.map +1 -1
  390. package/resources/admin/organization/index.d.mts +2 -2
  391. package/resources/admin/organization/index.d.mts.map +1 -1
  392. package/resources/admin/organization/index.d.ts +2 -2
  393. package/resources/admin/organization/index.d.ts.map +1 -1
  394. package/resources/admin/organization/index.js.map +1 -1
  395. package/resources/admin/organization/index.mjs.map +1 -1
  396. package/resources/admin/organization/organization.d.mts +4 -4
  397. package/resources/admin/organization/organization.d.mts.map +1 -1
  398. package/resources/admin/organization/organization.d.ts +4 -4
  399. package/resources/admin/organization/organization.d.ts.map +1 -1
  400. package/resources/admin/organization/organization.js.map +1 -1
  401. package/resources/admin/organization/organization.mjs.map +1 -1
  402. package/resources/admin/organization/projects/index.d.mts +1 -1
  403. package/resources/admin/organization/projects/index.d.mts.map +1 -1
  404. package/resources/admin/organization/projects/index.d.ts +1 -1
  405. package/resources/admin/organization/projects/index.d.ts.map +1 -1
  406. package/resources/admin/organization/projects/index.js.map +1 -1
  407. package/resources/admin/organization/projects/index.mjs.map +1 -1
  408. package/resources/admin/organization/projects/projects.d.mts +22 -5
  409. package/resources/admin/organization/projects/projects.d.mts.map +1 -1
  410. package/resources/admin/organization/projects/projects.d.ts +22 -5
  411. package/resources/admin/organization/projects/projects.d.ts.map +1 -1
  412. package/resources/admin/organization/projects/projects.js.map +1 -1
  413. package/resources/admin/organization/projects/projects.mjs.map +1 -1
  414. package/resources/admin/organization/usage.d.mts +62 -1
  415. package/resources/admin/organization/usage.d.mts.map +1 -1
  416. package/resources/admin/organization/usage.d.ts +62 -1
  417. package/resources/admin/organization/usage.d.ts.map +1 -1
  418. package/resources/admin/organization/usage.js.map +1 -1
  419. package/resources/admin/organization/usage.mjs.map +1 -1
  420. package/resources/beta/responses/internal-base.d.mts.map +1 -1
  421. package/resources/beta/responses/internal-base.d.ts.map +1 -1
  422. package/resources/beta/responses/internal-base.js +2 -0
  423. package/resources/beta/responses/internal-base.js.map +1 -1
  424. package/resources/beta/responses/internal-base.mjs +2 -0
  425. package/resources/beta/responses/internal-base.mjs.map +1 -1
  426. package/resources/beta/responses/responses.d.mts +9 -12
  427. package/resources/beta/responses/responses.d.mts.map +1 -1
  428. package/resources/beta/responses/responses.d.ts +9 -12
  429. package/resources/beta/responses/responses.d.ts.map +1 -1
  430. package/resources/beta/responses/responses.js.map +1 -1
  431. package/resources/beta/responses/responses.mjs.map +1 -1
  432. package/resources/beta/threads/runs/index.d.mts +1 -1
  433. package/resources/beta/threads/runs/index.d.mts.map +1 -1
  434. package/resources/beta/threads/runs/index.d.ts +1 -1
  435. package/resources/beta/threads/runs/index.d.ts.map +1 -1
  436. package/resources/beta/threads/runs/runs.d.mts.map +1 -1
  437. package/resources/beta/threads/runs/runs.d.ts.map +1 -1
  438. package/resources/beta/threads/runs/runs.js +2 -43
  439. package/resources/beta/threads/runs/runs.js.map +1 -1
  440. package/resources/beta/threads/runs/runs.mjs +2 -43
  441. package/resources/beta/threads/runs/runs.mjs.map +1 -1
  442. package/resources/chat/completions/completions.d.mts +6 -0
  443. package/resources/chat/completions/completions.d.mts.map +1 -1
  444. package/resources/chat/completions/completions.d.ts +6 -0
  445. package/resources/chat/completions/completions.d.ts.map +1 -1
  446. package/resources/chat/completions/completions.js.map +1 -1
  447. package/resources/chat/completions/completions.mjs.map +1 -1
  448. package/resources/embeddings.d.mts.map +1 -1
  449. package/resources/embeddings.d.ts.map +1 -1
  450. package/resources/embeddings.js +2 -36
  451. package/resources/embeddings.js.map +1 -1
  452. package/resources/embeddings.mjs +2 -36
  453. package/resources/embeddings.mjs.map +1 -1
  454. package/resources/files.d.mts.map +1 -1
  455. package/resources/files.d.ts.map +1 -1
  456. package/resources/files.js +2 -15
  457. package/resources/files.js.map +1 -1
  458. package/resources/files.mjs +2 -15
  459. package/resources/files.mjs.map +1 -1
  460. package/resources/images.d.mts +12 -22
  461. package/resources/images.d.mts.map +1 -1
  462. package/resources/images.d.ts +12 -22
  463. package/resources/images.d.ts.map +1 -1
  464. package/resources/realtime/calls.d.mts +23 -1
  465. package/resources/realtime/calls.d.mts.map +1 -1
  466. package/resources/realtime/calls.d.ts +23 -1
  467. package/resources/realtime/calls.d.ts.map +1 -1
  468. package/resources/realtime/calls.js +24 -0
  469. package/resources/realtime/calls.js.map +1 -1
  470. package/resources/realtime/calls.mjs +24 -0
  471. package/resources/realtime/calls.mjs.map +1 -1
  472. package/resources/realtime/index.d.mts +1 -1
  473. package/resources/realtime/index.d.mts.map +1 -1
  474. package/resources/realtime/index.d.ts +1 -1
  475. package/resources/realtime/index.d.ts.map +1 -1
  476. package/resources/realtime/index.js.map +1 -1
  477. package/resources/realtime/index.mjs +1 -1
  478. package/resources/realtime/index.mjs.map +1 -1
  479. package/resources/realtime/realtime.d.mts +5 -4
  480. package/resources/realtime/realtime.d.mts.map +1 -1
  481. package/resources/realtime/realtime.d.ts +5 -4
  482. package/resources/realtime/realtime.d.ts.map +1 -1
  483. package/resources/realtime/realtime.js.map +1 -1
  484. package/resources/realtime/realtime.mjs.map +1 -1
  485. package/resources/responses/internal-base.d.mts.map +1 -1
  486. package/resources/responses/internal-base.d.ts.map +1 -1
  487. package/resources/responses/internal-base.js +2 -0
  488. package/resources/responses/internal-base.js.map +1 -1
  489. package/resources/responses/internal-base.mjs +2 -0
  490. package/resources/responses/internal-base.mjs.map +1 -1
  491. package/resources/responses/responses.d.mts +9 -12
  492. package/resources/responses/responses.d.mts.map +1 -1
  493. package/resources/responses/responses.d.ts +9 -12
  494. package/resources/responses/responses.d.ts.map +1 -1
  495. package/resources/responses/responses.js.map +1 -1
  496. package/resources/responses/responses.mjs.map +1 -1
  497. package/resources/vector-stores/file-batches.d.mts.map +1 -1
  498. package/resources/vector-stores/file-batches.d.ts.map +1 -1
  499. package/resources/vector-stores/file-batches.js +4 -61
  500. package/resources/vector-stores/file-batches.js.map +1 -1
  501. package/resources/vector-stores/file-batches.mjs +4 -61
  502. package/resources/vector-stores/file-batches.mjs.map +1 -1
  503. package/resources/vector-stores/files.d.mts +1 -1
  504. package/resources/vector-stores/files.d.mts.map +1 -1
  505. package/resources/vector-stores/files.d.ts +1 -1
  506. package/resources/vector-stores/files.d.ts.map +1 -1
  507. package/resources/vector-stores/files.js +2 -35
  508. package/resources/vector-stores/files.js.map +1 -1
  509. package/resources/vector-stores/files.mjs +2 -35
  510. package/resources/vector-stores/files.mjs.map +1 -1
  511. package/resources/webhooks/webhooks.d.mts.map +1 -1
  512. package/resources/webhooks/webhooks.d.ts.map +1 -1
  513. package/resources/webhooks/webhooks.js +5 -43
  514. package/resources/webhooks/webhooks.js.map +1 -1
  515. package/resources/webhooks/webhooks.mjs +5 -43
  516. package/resources/webhooks/webhooks.mjs.map +1 -1
  517. package/src/auth/index.ts +6 -1
  518. package/src/auth/subject-token-providers.ts +196 -4
  519. package/src/auth/types.ts +24 -0
  520. package/src/auth/workload-identity-auth.ts +104 -7
  521. package/src/auth/x509-transport.ts +49 -0
  522. package/src/azure.ts +22 -1
  523. package/src/bedrock.ts +49 -5
  524. package/src/beta/realtime/internal-base.ts +15 -0
  525. package/src/beta/realtime/websocket.ts +148 -40
  526. package/src/beta/realtime/ws.ts +31 -13
  527. package/src/client.ts +536 -92
  528. package/src/core/EventEmitter.ts +91 -7
  529. package/src/core/streaming.ts +71 -20
  530. package/src/helpers/audio.ts +93 -22
  531. package/src/helpers/standard-schema.ts +50 -16
  532. package/src/helpers/zod-v3-strict-schema.ts +514 -0
  533. package/src/helpers/zod.ts +17 -4
  534. package/src/index.ts +1 -1
  535. package/src/internal/assistant-stream-delta.ts +33 -10
  536. package/src/internal/auth/x509-token-exchange.ts +310 -0
  537. package/src/internal/auth/x509-transport-capability.ts +219 -0
  538. package/src/internal/auth/x509-transport-registry.ts +118 -0
  539. package/src/internal/auth/x509-transport-state-browser.ts +36 -0
  540. package/src/internal/auth/x509-transport-state.cts +36 -0
  541. package/src/internal/auth/x509-workload-identity-auth.ts +958 -0
  542. package/src/internal/bedrock.ts +182 -5
  543. package/src/internal/data-residency.ts +44 -0
  544. package/src/internal/multipart-encoding.ts +53 -0
  545. package/src/internal/qs/utils.ts +375 -13
  546. package/src/internal/request-options.ts +52 -1
  547. package/src/internal/responses/canonical-output-text.ts +3 -3
  548. package/src/internal/responses/response-accumulator.ts +411 -24
  549. package/src/internal/uploads.ts +81 -26
  550. package/src/internal/utils/env.ts +9 -5
  551. package/src/internal/ws.ts +24 -3
  552. package/src/lib/AbstractChatCompletionRunner.ts +33 -6
  553. package/src/lib/AssistantStream.ts +262 -22
  554. package/src/lib/ChatCompletionStream.ts +1467 -103
  555. package/src/lib/EventEmitter.ts +91 -7
  556. package/src/lib/EventStream.ts +1483 -10
  557. package/src/lib/ResponsesParser.ts +4 -1
  558. package/src/lib/Util.ts +7 -5
  559. package/src/lib/assistant-run-polling.ts +27 -0
  560. package/src/lib/embeddings.ts +65 -0
  561. package/src/lib/file-processing.ts +36 -0
  562. package/src/lib/parser.ts +19 -4
  563. package/src/lib/polling.ts +144 -0
  564. package/src/lib/responses/ResponseStream.ts +22 -10
  565. package/src/lib/vector-store-polling.ts +46 -0
  566. package/src/lib/vector-store-upload.ts +53 -0
  567. package/src/lib/webhook-signature.ts +164 -0
  568. package/src/realtime/internal-base.ts +15 -0
  569. package/src/realtime/websocket.ts +130 -27
  570. package/src/realtime/ws.ts +15 -2
  571. package/src/resources/admin/organization/index.ts +2 -0
  572. package/src/resources/admin/organization/organization.ts +4 -0
  573. package/src/resources/admin/organization/projects/index.ts +1 -0
  574. package/src/resources/admin/organization/projects/projects.ts +37 -4
  575. package/src/resources/admin/organization/usage.ts +83 -0
  576. package/src/resources/beta/responses/internal-base.ts +2 -0
  577. package/src/resources/beta/responses/responses.ts +9 -12
  578. package/src/resources/beta/threads/runs/index.ts +1 -1
  579. package/src/resources/beta/threads/runs/runs.ts +2 -45
  580. package/src/resources/chat/completions/completions.ts +7 -0
  581. package/src/resources/embeddings.ts +2 -42
  582. package/src/resources/files.ts +2 -19
  583. package/src/resources/images.ts +12 -22
  584. package/src/resources/realtime/api.md +1 -0
  585. package/src/resources/realtime/calls.ts +46 -0
  586. package/src/resources/realtime/index.ts +7 -1
  587. package/src/resources/realtime/realtime.ts +5 -3
  588. package/src/resources/responses/internal-base.ts +2 -0
  589. package/src/resources/responses/responses.ts +9 -12
  590. package/src/resources/vector-stores/file-batches.ts +8 -77
  591. package/src/resources/vector-stores/files.ts +3 -43
  592. package/src/resources/webhooks/webhooks.ts +5 -61
  593. package/src/version.ts +1 -1
  594. package/version.d.mts +1 -1
  595. package/version.d.ts +1 -1
  596. package/version.js +1 -1
  597. package/version.mjs +1 -1
@@ -1,16 +1,28 @@
1
1
  "use strict";
2
- var _ChatCompletionStream_instances, _ChatCompletionStream_params, _ChatCompletionStream_audioDoneChoiceIndexes, _ChatCompletionStream_choiceEventStates, _ChatCompletionStream_currentChatCompletionSnapshot, _ChatCompletionStream_beginRequest, _ChatCompletionStream_getChoiceEventState, _ChatCompletionStream_addChunk, _ChatCompletionStream_emitToolCallDoneEvent, _ChatCompletionStream_emitContentDoneEvents, _ChatCompletionStream_endRequest, _ChatCompletionStream_accumulateChatCompletion;
2
+ var _ChatCompletionStream_instances, _ChatCompletionStream_params, _ChatCompletionStream_audioDoneChoiceIndexes, _ChatCompletionStream_choiceEventStates, _ChatCompletionStream_currentChatCompletionSnapshot, _ChatCompletionStream_hasAutoParseableTool, _ChatCompletionStream_partialJSONParseBudget, _ChatCompletionStream_beginRequest, _ChatCompletionStream_getChoiceEventState, _ChatCompletionStream_addChunk, _ChatCompletionStream_emitToolCallDoneEvent, _ChatCompletionStream_emitContentDoneEvents, _ChatCompletionStream_validateStructuredSnapshots, _ChatCompletionStream_endRequest, _ChatCompletionStream_accumulateChatCompletion;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.ChatCompletionStream = void 0;
5
5
  exports.makeChatCompletionReadableStreamMessageChunk = makeChatCompletionReadableStreamMessageChunk;
6
6
  const tslib_1 = require("../internal/tslib.js");
7
7
  const parser_1 = require("../_vendor/partial-json-parser/parser.js");
8
8
  const error_1 = require("../error.js");
9
+ const request_options_1 = require("../internal/request-options.js");
9
10
  const uuid_1 = require("../internal/utils/uuid.js");
10
11
  const values_1 = require("../internal/utils/values.js");
11
12
  const parser_2 = require("../lib/parser.js");
12
13
  const streaming_1 = require("../streaming.js");
13
14
  const AbstractChatCompletionRunner_1 = require("./AbstractChatCompletionRunner.js");
15
+ function parseStructuredStreamingJSON(content) {
16
+ try {
17
+ return (0, parser_1.partialParse)(content);
18
+ }
19
+ catch (error) {
20
+ if (error instanceof parser_1.MalformedJSON || error instanceof SyntaxError) {
21
+ return (0, parser_2.parseResponseFormatContent)({ type: 'json_schema', $parseRaw: undefined }, content);
22
+ }
23
+ throw error;
24
+ }
25
+ }
14
26
  // Keep message records readable as empty chunks by older SDKs. Their finalizer
15
27
  // overwrites `object`, so the encoded payload does not leak into completions.
16
28
  const CHAT_COMPLETION_READABLE_STREAM_MESSAGE_PREFIX = 'chat.completion.chunk.message:';
@@ -46,6 +58,232 @@ function getChatCompletionReadableStreamMessage(item) {
46
58
  // conservative ceiling to prevent sparse tool-call arrays from growing unbounded.
47
59
  const MAX_STREAM_CHOICES = 128;
48
60
  const MAX_STREAM_TOOL_CALLS = 128;
61
+ const MAX_PARTIAL_JSON_BYTES = 16 * 1024 * 1024;
62
+ const MAX_PARTIAL_JSON_FRAGMENTS = 65536;
63
+ const MAX_PARTIAL_JSON_DEPTH = 128;
64
+ const MAX_PARTIAL_JSON_PARSE_WORK = 64 * 1024 * 1024;
65
+ const EAGER_PARTIAL_JSON_BYTES = 1024;
66
+ function createPartialJSONParseState() {
67
+ return {
68
+ bytes: 0,
69
+ depth: 0,
70
+ fragments: 0,
71
+ work: 0,
72
+ escaped: false,
73
+ has_non_whitespace: false,
74
+ in_string: false,
75
+ last_parsed_bytes: 0,
76
+ pending_high_surrogate: false,
77
+ };
78
+ }
79
+ function recordPartialJSONFragment(state, budget, fragment, validationWorkBudget) {
80
+ if (budget.fragments >= MAX_PARTIAL_JSON_FRAGMENTS) {
81
+ throw new error_1.OpenAIError('Chat completion stream exceeded its structured JSON fragment limit');
82
+ }
83
+ let bytes = 0;
84
+ let { depth, escaped, has_non_whitespace: hasNonWhitespace, in_string: inString } = state;
85
+ let completed = false;
86
+ let firstCharacter = true;
87
+ for (const character of fragment) {
88
+ const previousBytes = bytes;
89
+ const codePoint = character.codePointAt(0);
90
+ if (firstCharacter && state.pending_high_surrogate && codePoint >= 56320 && codePoint <= 57343) {
91
+ bytes += 1;
92
+ }
93
+ else if (codePoint <= 0x7f) {
94
+ bytes += 1;
95
+ }
96
+ else if (codePoint <= 2047) {
97
+ bytes += 2;
98
+ }
99
+ else if (codePoint <= 65535) {
100
+ bytes += 3;
101
+ }
102
+ else {
103
+ bytes += 4;
104
+ }
105
+ firstCharacter = false;
106
+ if (budget.bytes + bytes > MAX_PARTIAL_JSON_BYTES) {
107
+ throw new error_1.OpenAIError('Chat completion stream exceeded its structured JSON byte limit');
108
+ }
109
+ if (validationWorkBudget && validationWorkBudget.work + bytes > MAX_PARTIAL_JSON_PARSE_WORK) {
110
+ validationWorkBudget.work += previousBytes;
111
+ throw new error_1.OpenAIError('Chat completion stream exceeded its structured JSON parse-work limit');
112
+ }
113
+ if (character !== ' ' && character !== '\n' && character !== '\r' && character !== '\t') {
114
+ hasNonWhitespace = true;
115
+ }
116
+ if (inString) {
117
+ if (escaped) {
118
+ escaped = false;
119
+ }
120
+ else if (character === '\\') {
121
+ escaped = true;
122
+ }
123
+ else if (character === '"') {
124
+ inString = false;
125
+ completed || (completed = depth === 0);
126
+ }
127
+ continue;
128
+ }
129
+ if (character === '"') {
130
+ inString = true;
131
+ }
132
+ else if (character === '{' || character === '[') {
133
+ depth += 1;
134
+ if (depth > MAX_PARTIAL_JSON_DEPTH) {
135
+ throw new error_1.OpenAIError('Chat completion stream exceeded its structured JSON nesting depth limit');
136
+ }
137
+ }
138
+ else if ((character === '}' || character === ']') && depth > 0) {
139
+ depth -= 1;
140
+ completed || (completed = depth === 0);
141
+ }
142
+ }
143
+ state.bytes += bytes;
144
+ state.fragments += 1;
145
+ state.depth = depth;
146
+ state.escaped = escaped;
147
+ state.has_non_whitespace = hasNonWhitespace;
148
+ state.in_string = inString;
149
+ if (fragment.length > 0) {
150
+ const finalCodeUnit = fragment.codePointAt(fragment.length - 1) ?? 0;
151
+ state.pending_high_surrogate = finalCodeUnit >= 55296 && finalCodeUnit <= 56319;
152
+ }
153
+ budget.bytes += bytes;
154
+ budget.fragments += 1;
155
+ if (validationWorkBudget) {
156
+ validationWorkBudget.work += bytes;
157
+ }
158
+ if (!hasNonWhitespace || bytes === 0) {
159
+ return false;
160
+ }
161
+ const minimumGrowth = Math.max(EAGER_PARTIAL_JSON_BYTES, Math.floor(state.last_parsed_bytes / 2));
162
+ if (state.bytes > EAGER_PARTIAL_JSON_BYTES &&
163
+ !completed &&
164
+ state.bytes - state.last_parsed_bytes < minimumGrowth) {
165
+ return false;
166
+ }
167
+ return true;
168
+ }
169
+ function reservePartialJSONParse(state, budget) {
170
+ if (budget.work + state.bytes > MAX_PARTIAL_JSON_PARSE_WORK) {
171
+ return false;
172
+ }
173
+ budget.work += state.bytes;
174
+ state.work += state.bytes;
175
+ state.last_parsed_bytes = state.bytes;
176
+ return true;
177
+ }
178
+ function captureStructuredJSONSnapshot(snapshot, property) {
179
+ const descriptor = Object.getOwnPropertyDescriptor(snapshot, property);
180
+ if (!descriptor) {
181
+ let prototype = Object.getPrototypeOf(snapshot);
182
+ for (let depth = 0; prototype !== null; depth += 1) {
183
+ if (depth >= MAX_PARTIAL_JSON_DEPTH || Object.getOwnPropertyDescriptor(prototype, property)) {
184
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
185
+ }
186
+ prototype = Object.getPrototypeOf(prototype);
187
+ }
188
+ return undefined;
189
+ }
190
+ if (!('value' in descriptor) ||
191
+ (typeof descriptor.value !== 'string' && descriptor.value !== null && descriptor.value !== undefined)) {
192
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
193
+ }
194
+ return descriptor.value;
195
+ }
196
+ function captureStructuredMessageSnapshot(choice) {
197
+ const descriptor = Object.getOwnPropertyDescriptor(choice, 'message');
198
+ if (!descriptor ||
199
+ !('value' in descriptor) ||
200
+ typeof descriptor.value !== 'object' ||
201
+ descriptor.value === null) {
202
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
203
+ }
204
+ return descriptor.value;
205
+ }
206
+ function captureSnapshotArray(snapshot, property, maximum, kind) {
207
+ const descriptor = Object.getOwnPropertyDescriptor(snapshot, property);
208
+ if (!descriptor) {
209
+ let prototype = Object.getPrototypeOf(snapshot);
210
+ for (let depth = 0; prototype !== null; depth += 1) {
211
+ if (depth >= MAX_PARTIAL_JSON_DEPTH || Object.getOwnPropertyDescriptor(prototype, property)) {
212
+ throw new error_1.OpenAIError(`Chat completion stream contains an unsafe snapshot ${kind} collection`);
213
+ }
214
+ prototype = Object.getPrototypeOf(prototype);
215
+ }
216
+ return undefined;
217
+ }
218
+ if (!('value' in descriptor) || !Array.isArray(descriptor.value)) {
219
+ throw new error_1.OpenAIError(`Chat completion stream contains an unsafe snapshot ${kind} collection`);
220
+ }
221
+ const length = Object.getOwnPropertyDescriptor(descriptor.value, 'length');
222
+ if (!length || !('value' in length) || !Number.isSafeInteger(length.value) || length.value > maximum) {
223
+ throw new error_1.OpenAIError(`Chat completion stream exceeded its snapshot ${kind} limit`);
224
+ }
225
+ return descriptor.value;
226
+ }
227
+ function captureSnapshotArrayItem(array, index) {
228
+ const descriptor = Object.getOwnPropertyDescriptor(array, index);
229
+ if (!descriptor) {
230
+ return undefined;
231
+ }
232
+ if (!('value' in descriptor)) {
233
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
234
+ }
235
+ return descriptor.value;
236
+ }
237
+ function mapCapturedSnapshotArray(array, maximum, kind, map) {
238
+ const descriptor = Object.getOwnPropertyDescriptor(array, 'length');
239
+ const length = descriptor && 'value' in descriptor ? descriptor.value : undefined;
240
+ if (typeof length !== 'number' || !Number.isSafeInteger(length) || length < 0 || length > maximum) {
241
+ throw new error_1.OpenAIError(`Chat completion stream exceeded its snapshot ${kind} limit`);
242
+ }
243
+ const mapped = [];
244
+ mapped.length = length;
245
+ for (let index = 0; index < length; index += 1) {
246
+ const item = Object.getOwnPropertyDescriptor(array, index);
247
+ if (!item) {
248
+ continue;
249
+ }
250
+ if (!('value' in item)) {
251
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
252
+ }
253
+ mapped[index] = map(item.value, index);
254
+ }
255
+ return mapped;
256
+ }
257
+ function validateStructuredJSONSnapshot(value, budget, validationWorkBudget) {
258
+ const state = createPartialJSONParseState();
259
+ const parseBudget = budget ?? { bytes: 0, fragments: 0, work: 0 };
260
+ recordPartialJSONFragment(state, parseBudget, value, validationWorkBudget);
261
+ if (!reservePartialJSONParse(state, parseBudget)) {
262
+ throw new error_1.OpenAIError('Chat completion stream exceeded its structured JSON parse-work limit');
263
+ }
264
+ return value;
265
+ }
266
+ function ownFunctionToolIdentity(toolCall) {
267
+ const type = Object.getOwnPropertyDescriptor(toolCall, 'type');
268
+ const fn = Object.getOwnPropertyDescriptor(toolCall, 'function');
269
+ if (!type || !('value' in type) || type.value !== 'function' || !fn || !('value' in fn)) {
270
+ return undefined;
271
+ }
272
+ if (typeof fn.value !== 'object' || fn.value === null) {
273
+ return undefined;
274
+ }
275
+ const name = Object.getOwnPropertyDescriptor(fn.value, 'name');
276
+ if (!name || !('value' in name) || typeof name.value !== 'string' || name.value.length === 0) {
277
+ return undefined;
278
+ }
279
+ return { type: 'function', name: name.value };
280
+ }
281
+ function assertBoundToolCallIdentity(toolCall, identity) {
282
+ const current = ownFunctionToolIdentity(toolCall);
283
+ if (!current || current.name !== identity.name || current.type !== identity.type) {
284
+ throw new error_1.OpenAIError('Chat completion stream contains a changed tool call identity');
285
+ }
286
+ }
49
287
  function assignOwnProperties(target, source) {
50
288
  if (Object.prototype.propertyIsEnumerable.call(source, '__proto__') && !(0, values_1.hasOwn)(target, '__proto__')) {
51
289
  Object.defineProperty(target, '__proto__', {
@@ -57,6 +295,474 @@ function assignOwnProperties(target, source) {
57
295
  }
58
296
  return Object.assign(target, source);
59
297
  }
298
+ function cloneParserConfigObject(value, stableFields = []) {
299
+ const descriptors = Object.getOwnPropertyDescriptors(value);
300
+ for (const field of stableFields) {
301
+ const descriptor = descriptors[field];
302
+ if (!descriptor && !(field in value)) {
303
+ continue;
304
+ }
305
+ descriptors[field] = {
306
+ value: descriptor && 'value' in descriptor ? descriptor.value : Reflect.get(value, field, value),
307
+ enumerable: descriptor?.enumerable ?? false,
308
+ configurable: descriptor?.configurable ?? true,
309
+ writable: descriptor && 'writable' in descriptor ? descriptor.writable : false,
310
+ };
311
+ }
312
+ return Object.create(Object.getPrototypeOf(value), descriptors);
313
+ }
314
+ function snapshotChatCompletionParserParams(params) {
315
+ const snapshot = cloneParserConfigObject(params);
316
+ if (params.tools) {
317
+ const stableTools = [];
318
+ const lengthDescriptor = Object.getOwnPropertyDescriptor(params.tools, 'length');
319
+ const length = lengthDescriptor && 'value' in lengthDescriptor ? lengthDescriptor.value : undefined;
320
+ const toolCount = typeof length === 'number' && Number.isSafeInteger(length) && length >= 0
321
+ ? Math.min(length, MAX_STREAM_TOOL_CALLS)
322
+ : 0;
323
+ for (let index = 0; index < toolCount; index += 1) {
324
+ const item = Object.getOwnPropertyDescriptor(params.tools, String(index));
325
+ if (!item || !('value' in item)) {
326
+ stableTools.length = index + 1;
327
+ continue;
328
+ }
329
+ const tool = item.value;
330
+ const stableTool = cloneParserConfigObject(tool, [
331
+ 'type',
332
+ '$brand',
333
+ '$parseRaw',
334
+ '$callback',
335
+ 'function',
336
+ ]);
337
+ const descriptors = Object.getOwnPropertyDescriptors(stableTool);
338
+ if ((0, parser_2.isChatCompletionFunctionTool)(stableTool)) {
339
+ const descriptor = descriptors.function;
340
+ descriptors.function = {
341
+ ...(descriptor && 'value' in descriptor
342
+ ? descriptor
343
+ : { configurable: true, enumerable: true, writable: true }),
344
+ value: cloneParserConfigObject(stableTool.function, ['name', 'strict']),
345
+ };
346
+ }
347
+ stableTools[index] = Object.create(Object.getPrototypeOf(tool), descriptors);
348
+ }
349
+ snapshot.tools = stableTools;
350
+ }
351
+ if (params.response_format) {
352
+ snapshot.response_format = cloneParserConfigObject(params.response_format, [
353
+ 'type',
354
+ '$brand',
355
+ '$parseRaw',
356
+ ]);
357
+ }
358
+ return snapshot;
359
+ }
360
+ const MAX_SERIALIZED_PARSER_SCHEMA_NODES = 4096;
361
+ const stringifyParserSchemaValue = JSON.stringify;
362
+ const MAX_SERIALIZED_PARSER_SCHEMA_BYTES = 1024 * 1024;
363
+ const MAX_SERIALIZED_PARSER_SCHEMA_DEPTH = 64;
364
+ const OMITTED_SERIALIZED_PARSER_VALUE = Symbol('omitted serialized parser value');
365
+ const UNSAFE_SERIALIZED_PARSER_VALUE = Symbol('unsafe serialized parser value');
366
+ function canonicalSerializedParserSchema(value, budget) {
367
+ const ancestors = new WeakSet();
368
+ const charge = (bytes) => {
369
+ if (!Number.isSafeInteger(bytes) ||
370
+ bytes < 0 ||
371
+ budget.bytes + bytes > MAX_SERIALIZED_PARSER_SCHEMA_BYTES) {
372
+ return false;
373
+ }
374
+ budget.bytes += bytes;
375
+ return true;
376
+ };
377
+ const visit = (current, depth) => {
378
+ if (depth > MAX_SERIALIZED_PARSER_SCHEMA_DEPTH || budget.nodes >= MAX_SERIALIZED_PARSER_SCHEMA_NODES) {
379
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
380
+ }
381
+ budget.nodes += 1;
382
+ if (current === undefined || typeof current === 'function' || typeof current === 'symbol') {
383
+ return OMITTED_SERIALIZED_PARSER_VALUE;
384
+ }
385
+ if (typeof current === 'bigint') {
386
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
387
+ }
388
+ if (current === null || typeof current === 'boolean' || typeof current === 'number') {
389
+ const serialized = stringifyParserSchemaValue(current);
390
+ return typeof serialized === 'string' && charge(serialized.length)
391
+ ? serialized
392
+ : UNSAFE_SERIALIZED_PARSER_VALUE;
393
+ }
394
+ if (typeof current === 'string') {
395
+ if (!charge(current.length * 6 + 2)) {
396
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
397
+ }
398
+ return stringifyParserSchemaValue(current);
399
+ }
400
+ if (typeof current !== 'object' || ancestors.has(current)) {
401
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
402
+ }
403
+ const array = Array.isArray(current);
404
+ const prototype = Object.getPrototypeOf(current);
405
+ if ((array && prototype !== Array.prototype) ||
406
+ (!array && prototype !== null && prototype !== Object.prototype)) {
407
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
408
+ }
409
+ for (let owner = current; owner !== null; owner = Object.getPrototypeOf(owner)) {
410
+ const serializer = Object.getOwnPropertyDescriptor(owner, 'toJSON');
411
+ if (!serializer) {
412
+ continue;
413
+ }
414
+ if (!('value' in serializer) || typeof serializer.value === 'function') {
415
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
416
+ }
417
+ break;
418
+ }
419
+ ancestors.add(current);
420
+ try {
421
+ if (!charge(2)) {
422
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
423
+ }
424
+ if (array) {
425
+ const lengthDescriptor = Object.getOwnPropertyDescriptor(current, 'length');
426
+ const length = lengthDescriptor && 'value' in lengthDescriptor ? lengthDescriptor.value : undefined;
427
+ if (typeof length !== 'number' ||
428
+ !Number.isSafeInteger(length) ||
429
+ length < 0 ||
430
+ length > MAX_SERIALIZED_PARSER_SCHEMA_NODES - budget.nodes) {
431
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
432
+ }
433
+ const items = [];
434
+ for (let index = 0; index < length; index += 1) {
435
+ const key = String(index);
436
+ const descriptor = Object.getOwnPropertyDescriptor(current, key);
437
+ if (!descriptor) {
438
+ if (Object.getOwnPropertyDescriptor(Array.prototype, key) ||
439
+ Object.getOwnPropertyDescriptor(Object.prototype, key)) {
440
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
441
+ }
442
+ budget.nodes += 1;
443
+ if (!charge(4)) {
444
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
445
+ }
446
+ items.push('null');
447
+ continue;
448
+ }
449
+ if (!('value' in descriptor)) {
450
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
451
+ }
452
+ const item = visit(descriptor.value, depth + 1);
453
+ if (item === UNSAFE_SERIALIZED_PARSER_VALUE) {
454
+ return item;
455
+ }
456
+ items.push(item === OMITTED_SERIALIZED_PARSER_VALUE ? 'null' : item);
457
+ }
458
+ return `[${items.join(',')}]`;
459
+ }
460
+ const keys = Reflect.ownKeys(current);
461
+ if (keys.length > MAX_SERIALIZED_PARSER_SCHEMA_NODES - budget.nodes) {
462
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
463
+ }
464
+ const entries = [];
465
+ for (const key of keys) {
466
+ if (typeof key !== 'string') {
467
+ continue;
468
+ }
469
+ const descriptor = Object.getOwnPropertyDescriptor(current, key);
470
+ if (!descriptor) {
471
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
472
+ }
473
+ if (!descriptor.enumerable) {
474
+ continue;
475
+ }
476
+ if (!('value' in descriptor)) {
477
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
478
+ }
479
+ entries.push([key, descriptor.value]);
480
+ }
481
+ entries.sort(([left], [right]) => {
482
+ if (left === right) {
483
+ return 0;
484
+ }
485
+ return left < right ? -1 : 1;
486
+ });
487
+ const fields = [];
488
+ for (const [key, entry] of entries) {
489
+ const normalized = visit(entry, depth + 1);
490
+ if (normalized === UNSAFE_SERIALIZED_PARSER_VALUE) {
491
+ return normalized;
492
+ }
493
+ if (normalized === OMITTED_SERIALIZED_PARSER_VALUE) {
494
+ continue;
495
+ }
496
+ if (!charge(key.length * 6 + 3)) {
497
+ return UNSAFE_SERIALIZED_PARSER_VALUE;
498
+ }
499
+ fields.push(`${stringifyParserSchemaValue(key)}:${normalized}`);
500
+ }
501
+ return `{${fields.join(',')}}`;
502
+ }
503
+ finally {
504
+ ancestors.delete(current);
505
+ }
506
+ };
507
+ try {
508
+ const normalized = visit(value, 0);
509
+ return typeof normalized === 'string' ? normalized : undefined;
510
+ }
511
+ catch {
512
+ return undefined;
513
+ }
514
+ }
515
+ function rememberSerializedParserSchema(signatures, source, holder, key) {
516
+ const parser = Object.getOwnPropertyDescriptor(source, '$parseRaw');
517
+ const schema = Object.getOwnPropertyDescriptor(holder, key);
518
+ if (!parser ||
519
+ !('value' in parser) ||
520
+ typeof parser.value !== 'function' ||
521
+ !schema ||
522
+ !('value' in schema)) {
523
+ return;
524
+ }
525
+ const normalized = canonicalSerializedParserSchema(schema.value, { nodes: 0, bytes: 0 });
526
+ if (normalized !== undefined) {
527
+ signatures.set(source, normalized);
528
+ }
529
+ }
530
+ function hasMatchingSerializedParserSchema(signatures, source, holder, key, value) {
531
+ const expected = source && signatures.get(source);
532
+ const descriptor = Object.getOwnPropertyDescriptor(holder, key);
533
+ return (expected !== undefined &&
534
+ descriptor !== undefined &&
535
+ 'value' in descriptor &&
536
+ canonicalSerializedParserSchema(value, { nodes: 0, bytes: 0 }) === expected);
537
+ }
538
+ function serializedParserDescriptor(descriptor, value) {
539
+ return descriptor && 'value' in descriptor
540
+ ? { ...descriptor, value }
541
+ : { configurable: true, enumerable: true, writable: true, value };
542
+ }
543
+ function shadowSerializedParserMetadata(descriptors, source, fields) {
544
+ for (const field of fields) {
545
+ const descriptor = descriptors[field];
546
+ if (!descriptor && !(field in source)) {
547
+ continue;
548
+ }
549
+ descriptors[field] =
550
+ descriptor && 'value' in descriptor
551
+ ? { ...descriptor, value: undefined }
552
+ : {
553
+ configurable: descriptor?.configurable ?? true,
554
+ enumerable: descriptor?.enumerable ?? false,
555
+ writable: false,
556
+ value: undefined,
557
+ };
558
+ }
559
+ }
560
+ function snapshotSerializedParserTool(serialized) {
561
+ const source = serialized.source ??
562
+ {
563
+ type: serialized.type,
564
+ ...(serialized.type === 'function' ? { function: {} } : {}),
565
+ };
566
+ const descriptors = Object.getOwnPropertyDescriptors(source);
567
+ descriptors.type = serializedParserDescriptor(descriptors.type, serialized.type);
568
+ if (serialized.type !== 'function' || !serialized.function) {
569
+ if (descriptors.function) {
570
+ descriptors.function = serializedParserDescriptor(descriptors.function, undefined);
571
+ }
572
+ shadowSerializedParserMetadata(descriptors, source, ['$brand', '$parseRaw', '$callback']);
573
+ return Object.create(Object.getPrototypeOf(source), descriptors);
574
+ }
575
+ const descriptor = descriptors.function;
576
+ const original = descriptor && 'value' in descriptor && typeof descriptor.value === 'object' && descriptor.value !== null
577
+ ? descriptor.value
578
+ : {};
579
+ const functionDescriptors = Object.getOwnPropertyDescriptors(original);
580
+ functionDescriptors['name'] = serializedParserDescriptor(functionDescriptors['name'], serialized.function.name);
581
+ functionDescriptors['strict'] = serializedParserDescriptor(functionDescriptors['strict'], serialized.function.strict);
582
+ descriptors.function = serializedParserDescriptor(descriptor, Object.create(Object.getPrototypeOf(original), functionDescriptors));
583
+ if (!serialized.function.schemaMatches) {
584
+ shadowSerializedParserMetadata(descriptors, source, ['$brand', '$parseRaw', '$callback']);
585
+ }
586
+ return Object.create(Object.getPrototypeOf(source), descriptors);
587
+ }
588
+ function snapshotSerializedResponseFormat(serialized) {
589
+ const source = serialized.source ?? { type: serialized.type };
590
+ const descriptors = Object.getOwnPropertyDescriptors(source);
591
+ descriptors.type = serializedParserDescriptor(descriptors.type, serialized.type);
592
+ if (serialized.type !== 'json_schema' || !serialized.source || !serialized.schemaMatches) {
593
+ shadowSerializedParserMetadata(descriptors, source, ['$brand', '$parseRaw']);
594
+ }
595
+ return Object.create(Object.getPrototypeOf(source), descriptors);
596
+ }
597
+ function ownSerializedParserObject(holder, key) {
598
+ const descriptor = Object.getOwnPropertyDescriptor(holder, key);
599
+ if (!descriptor || !('value' in descriptor)) {
600
+ return undefined;
601
+ }
602
+ const { value } = descriptor;
603
+ return typeof value === 'object' && value !== null ? value : undefined;
604
+ }
605
+ function observeSerializedChatCompletionParserParams(body, initial, update) {
606
+ const originalToolOwners = new WeakMap();
607
+ const originalSchemaSignatures = new WeakMap();
608
+ // At most 128 tools plus one response format each receive an independent
609
+ // 4,096-node/1 MiB source and wire allowance (129 MiB maximum per pass).
610
+ if (body.tools) {
611
+ for (let index = 0; index < body.tools.length && index < MAX_STREAM_TOOL_CALLS; index += 1) {
612
+ const owner = ownSerializedParserObject(body.tools, String(index));
613
+ const source = initial.tools?.[index];
614
+ if (owner && source) {
615
+ originalToolOwners.set(owner, source);
616
+ const originalFunction = ownSerializedParserObject(source, 'function');
617
+ if (originalFunction) {
618
+ rememberSerializedParserSchema(originalSchemaSignatures, source, originalFunction, 'parameters');
619
+ }
620
+ }
621
+ }
622
+ }
623
+ if (initial.response_format) {
624
+ rememberSerializedParserSchema(originalSchemaSignatures, initial.response_format, initial.response_format, 'json_schema');
625
+ }
626
+ let root;
627
+ let tools;
628
+ let responseFormat;
629
+ let responseFrame;
630
+ let frames = [];
631
+ let toolFrames = new WeakMap();
632
+ let actualToolOwners = new Map();
633
+ let functionFrames = new WeakMap();
634
+ return (0, request_options_1.observeJSONRequestBody)(body, {
635
+ value(holder, key, value) {
636
+ if (!root && key === '' && typeof value === 'object' && value !== null) {
637
+ root = value;
638
+ tools = undefined;
639
+ responseFormat = undefined;
640
+ responseFrame = undefined;
641
+ frames = [];
642
+ toolFrames = new WeakMap();
643
+ actualToolOwners = new Map();
644
+ functionFrames = new WeakMap();
645
+ return;
646
+ }
647
+ if (holder === root && key === 'response_format') {
648
+ if (typeof value === 'object' && value !== null) {
649
+ responseFormat = value;
650
+ const owner = ownSerializedParserObject(holder, key);
651
+ responseFrame = {
652
+ source: owner === body.response_format ? initial.response_format : undefined,
653
+ schemaMatches: false,
654
+ };
655
+ }
656
+ return;
657
+ }
658
+ if (holder === root && key === 'tools') {
659
+ if (Array.isArray(value)) {
660
+ tools = new Proxy(value, {
661
+ get(target, property) {
662
+ const actual = Reflect.get(target, property, target);
663
+ if (typeof property === 'string') {
664
+ const index = Number(property);
665
+ if (Number.isSafeInteger(index) &&
666
+ index >= 0 &&
667
+ index < MAX_STREAM_TOOL_CALLS &&
668
+ String(index) === property) {
669
+ actualToolOwners.set(index, typeof actual === 'object' && actual !== null ? actual : undefined);
670
+ }
671
+ }
672
+ return actual;
673
+ },
674
+ });
675
+ return tools;
676
+ }
677
+ return;
678
+ }
679
+ if (holder === tools) {
680
+ const index = Number(key);
681
+ if (!Number.isSafeInteger(index) ||
682
+ index < 0 ||
683
+ index >= MAX_STREAM_TOOL_CALLS ||
684
+ typeof value !== 'object' ||
685
+ value === null) {
686
+ return;
687
+ }
688
+ const owner = actualToolOwners.get(index);
689
+ const source = owner ? originalToolOwners.get(owner) : undefined;
690
+ const frame = { source };
691
+ frames[index] = frame;
692
+ toolFrames.set(value, frame);
693
+ return;
694
+ }
695
+ const tool = toolFrames.get(holder);
696
+ if (tool) {
697
+ if (key === 'type' && typeof value === 'string') {
698
+ tool.type = value;
699
+ }
700
+ else if (key === 'function' && typeof value === 'object' && value !== null) {
701
+ const fn = { source: tool.source, schemaMatches: false };
702
+ tool.function = fn;
703
+ functionFrames.set(value, fn);
704
+ }
705
+ return;
706
+ }
707
+ if (holder === responseFormat && responseFrame) {
708
+ if (key === 'type' && typeof value === 'string') {
709
+ responseFrame.type = value;
710
+ }
711
+ else if (key === 'json_schema') {
712
+ responseFrame.schemaMatches = hasMatchingSerializedParserSchema(originalSchemaSignatures, responseFrame.source, holder, key, value);
713
+ }
714
+ return;
715
+ }
716
+ const fn = functionFrames.get(holder);
717
+ if (fn) {
718
+ if (key === 'name' && typeof value === 'string') {
719
+ fn.name = value;
720
+ }
721
+ else if (key === 'strict' && typeof value === 'boolean') {
722
+ fn.strict = value;
723
+ }
724
+ else if (key === 'parameters') {
725
+ fn.schemaMatches = hasMatchingSerializedParserSchema(originalSchemaSignatures, fn.source, holder, key, value);
726
+ }
727
+ }
728
+ return undefined;
729
+ },
730
+ complete() {
731
+ if (!root) {
732
+ return;
733
+ }
734
+ const snapshot = cloneParserConfigObject(initial);
735
+ if (tools) {
736
+ const serializedTools = [];
737
+ for (let index = 0; index < frames.length; index += 1) {
738
+ const frame = frames[index];
739
+ if (frame) {
740
+ serializedTools[index] = snapshotSerializedParserTool(frame);
741
+ }
742
+ }
743
+ snapshot.tools = serializedTools;
744
+ }
745
+ else {
746
+ delete snapshot.tools;
747
+ }
748
+ if (responseFrame) {
749
+ snapshot.response_format = snapshotSerializedResponseFormat(responseFrame);
750
+ }
751
+ else {
752
+ delete snapshot.response_format;
753
+ }
754
+ update(snapshot);
755
+ root = undefined;
756
+ tools = undefined;
757
+ responseFormat = undefined;
758
+ responseFrame = undefined;
759
+ frames = [];
760
+ toolFrames = new WeakMap();
761
+ actualToolOwners = new Map();
762
+ functionFrames = new WeakMap();
763
+ },
764
+ });
765
+ }
60
766
  /** Streams chat completion chunks while accumulating snapshots, parsed output, and events. */
61
767
  class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCompletionRunner {
62
768
  /** Creates an unstarted stream, retaining request parameters for structured-output parsing. */
@@ -67,9 +773,30 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
67
773
  _ChatCompletionStream_audioDoneChoiceIndexes.set(this, void 0);
68
774
  _ChatCompletionStream_choiceEventStates.set(this, void 0);
69
775
  _ChatCompletionStream_currentChatCompletionSnapshot.set(this, void 0);
776
+ _ChatCompletionStream_hasAutoParseableTool.set(this, void 0);
777
+ _ChatCompletionStream_partialJSONParseBudget.set(this, void 0);
70
778
  tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_params, params, "f");
71
779
  tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_audioDoneChoiceIndexes, new Set(), "f");
72
780
  tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_choiceEventStates, [], "f");
781
+ tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_hasAutoParseableTool, false, "f");
782
+ const tools = params?.tools;
783
+ const lengthDescriptor = tools && Object.getOwnPropertyDescriptor(tools, 'length');
784
+ const length = lengthDescriptor && 'value' in lengthDescriptor ? lengthDescriptor.value : undefined;
785
+ if (tools && typeof length === 'number' && Number.isSafeInteger(length) && length >= 0) {
786
+ for (let index = 0; index < Math.min(length, MAX_STREAM_TOOL_CALLS); index += 1) {
787
+ const descriptor = Object.getOwnPropertyDescriptor(tools, String(index));
788
+ if (!descriptor || !('value' in descriptor)) {
789
+ continue;
790
+ }
791
+ const tool = descriptor.value;
792
+ if ((0, parser_2.isChatCompletionFunctionTool)(tool) &&
793
+ ((0, parser_2.isAutoParsableTool)(tool) || tool.function.strict === true)) {
794
+ tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_hasAutoParseableTool, true, "f");
795
+ break;
796
+ }
797
+ }
798
+ }
799
+ tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_partialJSONParseBudget, { bytes: 0, fragments: 0, work: 0 }, "f");
73
800
  }
74
801
  /** The latest accumulated completion, or `undefined` before a chunk arrives or after finalization. */
75
802
  get currentChatCompletionSnapshot() {
@@ -95,8 +822,25 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
95
822
  }
96
823
  async _createChatCompletion(client, params, options) {
97
824
  this._listenForAbort(options?.signal);
825
+ const requestParams = { ...params, stream: true };
826
+ tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_params, requestParams, "f");
98
827
  tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_beginRequest).call(this);
99
- const stream = await client.chat.completions.create({ ...params, stream: true }, { ...options, signal: this.controller.signal });
828
+ const parserParams = snapshotChatCompletionParserParams(requestParams);
829
+ tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_params, parserParams, "f");
830
+ tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_hasAutoParseableTool, parserParams.tools?.some((tool) => (0, parser_2.isChatCompletionFunctionTool)(tool) && ((0, parser_2.isAutoParsableTool)(tool) || tool.function.strict === true)) ?? false, "f");
831
+ const stopObserving = requestParams.tools || requestParams.response_format
832
+ ? observeSerializedChatCompletionParserParams(requestParams, parserParams, (serialized) => {
833
+ tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_params, serialized, "f");
834
+ tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_hasAutoParseableTool, serialized.tools?.some((tool) => (0, parser_2.isChatCompletionFunctionTool)(tool) &&
835
+ ((0, parser_2.isAutoParsableTool)(tool) || tool.function.strict === true)) ?? false, "f");
836
+ })
837
+ : undefined;
838
+ const stream = await client.chat.completions
839
+ .create(requestParams, {
840
+ ...options,
841
+ signal: this.controller.signal,
842
+ })
843
+ .finally(stopObserving);
100
844
  this._connected();
101
845
  for await (const chunk of stream) {
102
846
  tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_addChunk).call(this, chunk);
@@ -152,12 +896,13 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
152
896
  throw new error_1.OpenAIError(`request ended without sending any chunks`);
153
897
  }
154
898
  /** Iterates over raw API chunks; stopping iteration early aborts the underlying request. */
155
- [(_ChatCompletionStream_params = new WeakMap(), _ChatCompletionStream_audioDoneChoiceIndexes = new WeakMap(), _ChatCompletionStream_choiceEventStates = new WeakMap(), _ChatCompletionStream_currentChatCompletionSnapshot = new WeakMap(), _ChatCompletionStream_instances = new WeakSet(), _ChatCompletionStream_beginRequest = function _ChatCompletionStream_beginRequest() {
899
+ [(_ChatCompletionStream_params = new WeakMap(), _ChatCompletionStream_audioDoneChoiceIndexes = new WeakMap(), _ChatCompletionStream_choiceEventStates = new WeakMap(), _ChatCompletionStream_currentChatCompletionSnapshot = new WeakMap(), _ChatCompletionStream_hasAutoParseableTool = new WeakMap(), _ChatCompletionStream_partialJSONParseBudget = new WeakMap(), _ChatCompletionStream_instances = new WeakSet(), _ChatCompletionStream_beginRequest = function _ChatCompletionStream_beginRequest() {
156
900
  if (this.ended) {
157
901
  return;
158
902
  }
159
903
  tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_audioDoneChoiceIndexes, new Set(), "f");
160
904
  tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, "f");
905
+ tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_partialJSONParseBudget, { bytes: 0, fragments: 0, work: 0 }, "f");
161
906
  }, _ChatCompletionStream_getChoiceEventState = function _ChatCompletionStream_getChoiceEventState(choice) {
162
907
  let state = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index];
163
908
  if (state) {
@@ -165,11 +910,14 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
165
910
  }
166
911
  state = {
167
912
  content_done: false,
913
+ content_parse_state: undefined,
168
914
  refusal_done: false,
169
915
  logprobs_content_done: false,
170
916
  logprobs_refusal_done: false,
171
917
  done_tool_calls: new Set(),
172
918
  current_tool_call_index: null,
919
+ tool_call_parse_states: new Map(),
920
+ tool_call_identities: new Map(),
173
921
  };
174
922
  tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index] = state;
175
923
  return state;
@@ -177,36 +925,47 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
177
925
  if (this.ended) {
178
926
  return;
179
927
  }
180
- const completion = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_accumulateChatCompletion).call(this, chunk);
928
+ const capturedChoiceFrames = new WeakMap();
929
+ const completion = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_accumulateChatCompletion).call(this, chunk, capturedChoiceFrames);
181
930
  this._emit('chunk', chunk, completion);
182
931
  for (const choice of chunk.choices) {
183
- const choiceSnapshot = completion.choices[choice.index];
932
+ const capturedChoice = capturedChoiceFrames.get(choice);
933
+ const choiceSnapshot = completion.choices[capturedChoice?.index ?? choice.index];
934
+ const capturedToolCalls = capturedChoice?.tool_calls ?? [];
184
935
  const { delta } = choice;
185
- if (delta?.content != null &&
186
- choiceSnapshot.message?.role === 'assistant' &&
187
- choiceSnapshot.message?.content) {
188
- this._emit('content', delta.content, choiceSnapshot.message.content);
936
+ const structuredResponse = (0, parser_2.isParseableResponseFormat)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format);
937
+ const boundedSnapshot = structuredResponse || tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_hasAutoParseableTool, "f");
938
+ const messageSnapshot = boundedSnapshot
939
+ ? captureStructuredMessageSnapshot(choiceSnapshot)
940
+ : choiceSnapshot.message;
941
+ const refusal = boundedSnapshot
942
+ ? captureStructuredJSONSnapshot(messageSnapshot, 'refusal')
943
+ : messageSnapshot.refusal;
944
+ const parseableContent = !refusal && structuredResponse;
945
+ const messageContent = parseableContent
946
+ ? captureStructuredJSONSnapshot(messageSnapshot, 'content')
947
+ : messageSnapshot.content;
948
+ if (delta?.content != null && messageSnapshot.role === 'assistant' && messageContent) {
949
+ this._emit('content', delta.content, messageContent);
189
950
  this._emit('content.delta', {
190
951
  delta: delta.content,
191
- snapshot: choiceSnapshot.message.content,
192
- parsed: choiceSnapshot.message.parsed,
952
+ snapshot: messageContent,
953
+ parsed: messageSnapshot.parsed,
193
954
  });
194
955
  }
195
- if (delta?.refusal != null &&
196
- choiceSnapshot.message?.role === 'assistant' &&
197
- choiceSnapshot.message?.refusal) {
956
+ if (delta?.refusal != null && messageSnapshot.role === 'assistant' && refusal) {
198
957
  this._emit('refusal.delta', {
199
958
  delta: delta.refusal,
200
- snapshot: choiceSnapshot.message.refusal,
959
+ snapshot: refusal,
201
960
  });
202
961
  }
203
- if (choice.logprobs?.content != null && choiceSnapshot.message?.role === 'assistant') {
962
+ if (choice.logprobs?.content != null && messageSnapshot.role === 'assistant') {
204
963
  this._emit('logprobs.content.delta', {
205
964
  content: choice.logprobs?.content,
206
965
  snapshot: choiceSnapshot.logprobs?.content ?? [],
207
966
  });
208
967
  }
209
- if (choice.logprobs?.refusal != null && choiceSnapshot.message?.role === 'assistant') {
968
+ if (choice.logprobs?.refusal != null && messageSnapshot.role === 'assistant') {
210
969
  this._emit('logprobs.refusal.delta', {
211
970
  refusal: choice.logprobs?.refusal,
212
971
  snapshot: choiceSnapshot.logprobs?.refusal ?? [],
@@ -219,7 +978,7 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
219
978
  tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);
220
979
  }
221
980
  }
222
- for (const toolCall of delta?.tool_calls ?? []) {
981
+ for (const toolCall of capturedToolCalls) {
223
982
  if (state.current_tool_call_index !== toolCall.index) {
224
983
  tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);
225
984
  // new tool call started, the previous one is done
@@ -229,18 +988,30 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
229
988
  }
230
989
  state.current_tool_call_index = toolCall.index;
231
990
  }
232
- for (const toolCallDelta of delta?.tool_calls ?? []) {
233
- const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallDelta.index];
991
+ for (const toolCallDelta of capturedToolCalls) {
992
+ const toolCallSnapshot = messageSnapshot.tool_calls?.[toolCallDelta.index];
234
993
  if (!toolCallSnapshot?.type) {
235
994
  continue;
236
995
  }
237
996
  if (toolCallSnapshot.type === 'function') {
997
+ const boundIdentity = state.tool_call_identities.get(toolCallDelta.index);
998
+ let argumentsSnapshot;
999
+ if (boundIdentity?.parseable) {
1000
+ const capturedArguments = captureStructuredJSONSnapshot(toolCallSnapshot.function, 'arguments');
1001
+ if (typeof capturedArguments !== 'string') {
1002
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
1003
+ }
1004
+ argumentsSnapshot = capturedArguments;
1005
+ }
1006
+ else {
1007
+ argumentsSnapshot = toolCallSnapshot.function.arguments;
1008
+ }
238
1009
  this._emit('tool_calls.function.arguments.delta', {
239
1010
  name: toolCallSnapshot.function.name,
240
1011
  index: toolCallDelta.index,
241
- arguments: toolCallSnapshot.function.arguments,
1012
+ arguments: argumentsSnapshot,
242
1013
  parsed_arguments: toolCallSnapshot.function.parsed_arguments,
243
- arguments_delta: toolCallDelta.function?.arguments ?? '',
1014
+ arguments_delta: toolCallDelta.arguments_delta,
244
1015
  });
245
1016
  }
246
1017
  else if (toolCallSnapshot.type !== 'custom') {
@@ -254,26 +1025,48 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
254
1025
  // we've already fired the done event
255
1026
  return;
256
1027
  }
257
- const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallIndex];
1028
+ const messageSnapshot = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_hasAutoParseableTool, "f")
1029
+ ? captureStructuredMessageSnapshot(choiceSnapshot)
1030
+ : choiceSnapshot.message;
1031
+ const toolCallSnapshot = messageSnapshot.tool_calls?.[toolCallIndex];
258
1032
  if (!toolCallSnapshot) {
259
1033
  throw new Error('no tool call snapshot');
260
1034
  }
1035
+ const boundIdentity = state.tool_call_identities.get(toolCallIndex);
1036
+ if (boundIdentity) {
1037
+ assertBoundToolCallIdentity(toolCallSnapshot, boundIdentity);
1038
+ }
261
1039
  if (!toolCallSnapshot.type) {
262
1040
  throw new Error('tool call snapshot missing `type`');
263
1041
  }
264
1042
  if (toolCallSnapshot.type === 'function') {
265
1043
  const inputTool = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.tools?.find((tool) => (0, parser_2.isChatCompletionFunctionTool)(tool) && tool.function.name === toolCallSnapshot.function.name); // TS doesn't narrow based on isChatCompletionTool
266
1044
  let parsedArguments = null;
1045
+ const parseable = (0, parser_2.isAutoParsableTool)(inputTool) || inputTool?.function.strict === true;
1046
+ let argumentsSnapshot;
1047
+ if (parseable) {
1048
+ if (tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f")) {
1049
+ tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_validateStructuredSnapshots).call(this, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f"));
1050
+ }
1051
+ const capturedArguments = captureStructuredJSONSnapshot(toolCallSnapshot.function, 'arguments');
1052
+ if (typeof capturedArguments !== 'string') {
1053
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
1054
+ }
1055
+ argumentsSnapshot = capturedArguments;
1056
+ }
1057
+ else {
1058
+ argumentsSnapshot = toolCallSnapshot.function.arguments;
1059
+ }
267
1060
  if ((0, parser_2.isAutoParsableTool)(inputTool)) {
268
- parsedArguments = inputTool.$parseRaw(toolCallSnapshot.function.arguments);
1061
+ parsedArguments = inputTool.$parseRaw(validateStructuredJSONSnapshot(argumentsSnapshot));
269
1062
  }
270
1063
  else if (inputTool?.function.strict) {
271
- parsedArguments = JSON.parse(toolCallSnapshot.function.arguments);
1064
+ parsedArguments = (0, parser_2.parseResponseFormatContent)({ type: 'json_schema', $parseRaw: undefined }, validateStructuredJSONSnapshot(argumentsSnapshot));
272
1065
  }
273
1066
  this._emit('tool_calls.function.arguments.done', {
274
1067
  name: toolCallSnapshot.function.name,
275
1068
  index: toolCallIndex,
276
- arguments: toolCallSnapshot.function.arguments,
1069
+ arguments: argumentsSnapshot,
277
1070
  parsed_arguments: parsedArguments,
278
1071
  });
279
1072
  }
@@ -282,23 +1075,36 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
282
1075
  }
283
1076
  }, _ChatCompletionStream_emitContentDoneEvents = function _ChatCompletionStream_emitContentDoneEvents(choiceSnapshot) {
284
1077
  const state = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
285
- if (choiceSnapshot.message.content != null &&
286
- (choiceSnapshot.message.content !== '' ||
287
- (!choiceSnapshot.message.refusal &&
288
- !choiceSnapshot.message.tool_calls?.length &&
289
- !choiceSnapshot.message.function_call)) &&
1078
+ const structuredResponse = (0, parser_2.isParseableResponseFormat)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format);
1079
+ const boundedSnapshot = structuredResponse || tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_hasAutoParseableTool, "f");
1080
+ const messageSnapshot = boundedSnapshot
1081
+ ? captureStructuredMessageSnapshot(choiceSnapshot)
1082
+ : choiceSnapshot.message;
1083
+ const refusal = boundedSnapshot
1084
+ ? captureStructuredJSONSnapshot(messageSnapshot, 'refusal')
1085
+ : messageSnapshot.refusal;
1086
+ const parseableContent = !refusal && structuredResponse;
1087
+ const content = parseableContent
1088
+ ? captureStructuredJSONSnapshot(messageSnapshot, 'content')
1089
+ : messageSnapshot.content;
1090
+ if (content != null &&
1091
+ (content !== '' ||
1092
+ (!refusal && !messageSnapshot.tool_calls?.length && !messageSnapshot.function_call)) &&
290
1093
  !state.content_done) {
1094
+ if (parseableContent && tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f")) {
1095
+ tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_validateStructuredSnapshots).call(this, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f"));
1096
+ }
291
1097
  state.content_done = true;
292
1098
  this._emit('content.done', {
293
- content: choiceSnapshot.message.content,
294
- parsed: choiceSnapshot.message.refusal
1099
+ content,
1100
+ parsed: refusal
295
1101
  ? null
296
- : (0, parser_2.parseResponseFormatContent)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format, choiceSnapshot.message.content),
1102
+ : (0, parser_2.parseResponseFormatContent)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format, parseableContent ? validateStructuredJSONSnapshot(content) : content),
297
1103
  });
298
1104
  }
299
- if (choiceSnapshot.message.refusal && !state.refusal_done) {
1105
+ if (refusal && !state.refusal_done) {
300
1106
  state.refusal_done = true;
301
- this._emit('refusal.done', { refusal: choiceSnapshot.message.refusal });
1107
+ this._emit('refusal.done', { refusal });
302
1108
  }
303
1109
  if (choiceSnapshot.logprobs?.content && !state.logprobs_content_done) {
304
1110
  state.logprobs_content_done = true;
@@ -308,6 +1114,97 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
308
1114
  state.logprobs_refusal_done = true;
309
1115
  this._emit('logprobs.refusal.done', { refusal: choiceSnapshot.logprobs.refusal });
310
1116
  }
1117
+ }, _ChatCompletionStream_validateStructuredSnapshots = function _ChatCompletionStream_validateStructuredSnapshots(snapshot) {
1118
+ const finalJSONBudget = { bytes: 0, fragments: 0, work: 0 };
1119
+ const parseableContent = (0, parser_2.isParseableResponseFormat)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format);
1120
+ const validatedMessages = new WeakMap();
1121
+ const choices = captureSnapshotArray(snapshot, 'choices', MAX_STREAM_CHOICES, 'choice');
1122
+ if (!choices) {
1123
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe snapshot choice collection');
1124
+ }
1125
+ for (let choiceIndex = 0; choiceIndex < choices.length; choiceIndex += 1) {
1126
+ const choice = captureSnapshotArrayItem(choices, choiceIndex);
1127
+ if (!choice) {
1128
+ continue;
1129
+ }
1130
+ const message = captureStructuredMessageSnapshot(choice);
1131
+ const refusal = captureStructuredJSONSnapshot(message, 'refusal');
1132
+ const content = captureStructuredJSONSnapshot(message, 'content');
1133
+ const validatedTools = new Map();
1134
+ const toolCalls = captureSnapshotArray(message, 'tool_calls', MAX_STREAM_TOOL_CALLS, 'tool-call');
1135
+ validatedMessages.set(choice, Object.freeze({
1136
+ message,
1137
+ content,
1138
+ refusal,
1139
+ toolCallCollection: toolCalls,
1140
+ toolCalls: validatedTools,
1141
+ }));
1142
+ const state = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index];
1143
+ if (parseableContent && !refusal && typeof content === 'string') {
1144
+ validateStructuredJSONSnapshot(content, finalJSONBudget, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f"));
1145
+ }
1146
+ for (const [index, identity] of state?.tool_call_identities ?? []) {
1147
+ const toolCall = toolCalls && captureSnapshotArrayItem(toolCalls, index);
1148
+ if (!toolCall) {
1149
+ throw new error_1.OpenAIError('Chat completion stream contains a changed tool call identity');
1150
+ }
1151
+ assertBoundToolCallIdentity(toolCall, identity);
1152
+ }
1153
+ if (!tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_hasAutoParseableTool, "f")) {
1154
+ continue;
1155
+ }
1156
+ for (let toolCallIndex = 0; toolCallIndex < (toolCalls?.length ?? 0); toolCallIndex += 1) {
1157
+ const toolCall = captureSnapshotArrayItem(toolCalls, toolCallIndex);
1158
+ if (!toolCall) {
1159
+ continue;
1160
+ }
1161
+ const identity = ownFunctionToolIdentity(toolCall);
1162
+ if (!identity) {
1163
+ const type = Object.getOwnPropertyDescriptor(toolCall, 'type');
1164
+ if (type && !('value' in type)) {
1165
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
1166
+ }
1167
+ if (type?.value !== 'function') {
1168
+ continue;
1169
+ }
1170
+ const fn = Object.getOwnPropertyDescriptor(toolCall, 'function');
1171
+ if (fn && !('value' in fn)) {
1172
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
1173
+ }
1174
+ if (fn && typeof fn.value === 'object' && fn.value !== null) {
1175
+ const name = Object.getOwnPropertyDescriptor(fn.value, 'name');
1176
+ if (name && !('value' in name)) {
1177
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
1178
+ }
1179
+ }
1180
+ continue;
1181
+ }
1182
+ if (!(0, parser_2.shouldParseToolCall)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"), {
1183
+ type: identity.type,
1184
+ function: { name: identity.name },
1185
+ })) {
1186
+ continue;
1187
+ }
1188
+ const descriptor = Object.getOwnPropertyDescriptor(toolCall, 'function');
1189
+ if (!descriptor || !('value' in descriptor)) {
1190
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
1191
+ }
1192
+ const fn = descriptor.value;
1193
+ const argumentsSnapshot = captureStructuredJSONSnapshot(fn, 'arguments');
1194
+ if (typeof argumentsSnapshot !== 'string') {
1195
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
1196
+ }
1197
+ validateStructuredJSONSnapshot(argumentsSnapshot, finalJSONBudget, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f"));
1198
+ validatedTools.set(toolCallIndex, Object.freeze({
1199
+ tool: toolCall,
1200
+ function: fn,
1201
+ type: identity.type,
1202
+ name: identity.name,
1203
+ arguments: argumentsSnapshot,
1204
+ }));
1205
+ }
1206
+ }
1207
+ return validatedMessages;
311
1208
  }, _ChatCompletionStream_endRequest = function _ChatCompletionStream_endRequest() {
312
1209
  if (this.ended) {
313
1210
  throw new error_1.OpenAIError(`stream has ended, this shouldn't happen`);
@@ -316,15 +1213,16 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
316
1213
  if (!snapshot) {
317
1214
  throw new error_1.OpenAIError(`request ended without sending any chunks`);
318
1215
  }
1216
+ const validatedMessages = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_validateStructuredSnapshots).call(this, snapshot);
319
1217
  const audioDoneChoiceIndexes = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_audioDoneChoiceIndexes, "f");
320
1218
  tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_audioDoneChoiceIndexes, new Set(), "f");
321
1219
  tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, "f");
322
1220
  tslib_1.__classPrivateFieldSet(this, _ChatCompletionStream_choiceEventStates, [], "f");
323
- return finalizeChatCompletion(snapshot, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"), audioDoneChoiceIndexes);
324
- }, _ChatCompletionStream_accumulateChatCompletion = function _ChatCompletionStream_accumulateChatCompletion(chunk) {
1221
+ return finalizeChatCompletion(snapshot, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"), audioDoneChoiceIndexes, validatedMessages);
1222
+ }, _ChatCompletionStream_accumulateChatCompletion = function _ChatCompletionStream_accumulateChatCompletion(chunk, capturedChoiceFrames) {
325
1223
  var _a, _b, _c, _d, _e;
326
1224
  let snapshot = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
327
- const { choices, ...rest } = chunk;
1225
+ const { choices, obfuscation: _obfuscation, ...rest } = chunk;
328
1226
  if (!snapshot) {
329
1227
  const newSnapshot = {
330
1228
  ...rest,
@@ -342,16 +1240,22 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
342
1240
  requestedChoiceCount > 0
343
1241
  ? Math.min(requestedChoiceCount, MAX_STREAM_CHOICES)
344
1242
  : MAX_STREAM_CHOICES;
345
- for (const { delta, finish_reason, index, logprobs = null, ...other } of chunk.choices) {
1243
+ for (const chunkChoice of chunk.choices) {
1244
+ const { delta, finish_reason, index, logprobs = null, ...other } = chunkChoice;
1245
+ const capturedToolCalls = [];
1246
+ capturedChoiceFrames.set(chunkChoice, Object.freeze({ index, tool_calls: capturedToolCalls }));
346
1247
  if (!Number.isSafeInteger(index) || index < 0 || index >= maxChoices) {
347
1248
  throw new error_1.OpenAIError(`Chat completion stream contains an invalid choice index: ${index}`);
348
1249
  }
349
1250
  let choice = snapshot.choices[index];
350
1251
  if (!choice) {
351
- const newChoice = { finish_reason, index, message: {}, logprobs, ...other };
1252
+ const newChoice = { finish_reason, index, message: {}, logprobs: null, ...other };
352
1253
  snapshot.choices[index] = newChoice;
353
1254
  choice = newChoice;
354
1255
  }
1256
+ if ((0, parser_2.isParseableResponseFormat)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format) || tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_hasAutoParseableTool, "f")) {
1257
+ captureStructuredJSONSnapshot(captureStructuredMessageSnapshot(choice), 'refusal');
1258
+ }
355
1259
  if (logprobs) {
356
1260
  if (choice.logprobs) {
357
1261
  const { content, refusal, ...rest } = logprobs;
@@ -368,6 +1272,12 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
368
1272
  }
369
1273
  else {
370
1274
  choice.logprobs = { ...logprobs };
1275
+ if (logprobs.content) {
1276
+ choice.logprobs.content = [...logprobs.content];
1277
+ }
1278
+ if (logprobs.refusal) {
1279
+ choice.logprobs.refusal = [...logprobs.refusal];
1280
+ }
371
1281
  }
372
1282
  }
373
1283
  if (finish_reason) {
@@ -383,10 +1293,13 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
383
1293
  }
384
1294
  assignOwnProperties(choice, other);
385
1295
  if (!delta) {
1296
+ Object.freeze(capturedToolCalls);
386
1297
  continue;
387
1298
  } // Shouldn't happen; just in case.
388
1299
  tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_audioDoneChoiceIndexes, "f").delete(index);
389
- const { audio, content, refusal, function_call, role, tool_calls, ...rest } = delta;
1300
+ const { audio, content, refusal, function_call, role, ...capturedDeltaFields } = delta;
1301
+ const { tool_calls: capturedToolCallDelta, ...rest } = capturedDeltaFields;
1302
+ const tool_calls = (0, values_1.hasOwn)(capturedDeltaFields, 'tool_calls') ? capturedToolCallDelta : delta.tool_calls;
390
1303
  assertIsEmpty(rest);
391
1304
  assignOwnProperties(choice.message, rest);
392
1305
  if (audio?.expires_at != null &&
@@ -437,10 +1350,26 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
437
1350
  }
438
1351
  }
439
1352
  if (content != null) {
440
- choice.message.content = (choice.message.content || '') + content;
441
1353
  if (!choice.message.refusal && (0, parser_2.isParseableResponseFormat)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format)) {
442
- // The partial parser does not accept whitespace-only input.
443
- choice.message.parsed = choice.message.content.trim() ? (0, parser_1.partialParse)(choice.message.content) : null;
1354
+ const eventState = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choice);
1355
+ const parseState = (eventState.content_parse_state ?? (eventState.content_parse_state = createPartialJSONParseState()));
1356
+ const shouldParse = recordPartialJSONFragment(parseState, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f"), content);
1357
+ choice.message.content = (captureStructuredJSONSnapshot(choice.message, 'content') || '') + content;
1358
+ // The partial parser does not accept whitespace-only input. Once output
1359
+ // grows, coalesce prefix reparses while preserving every raw snapshot.
1360
+ if (!parseState.has_non_whitespace) {
1361
+ choice.message.parsed = null;
1362
+ }
1363
+ else if (shouldParse && reservePartialJSONParse(parseState, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f"))) {
1364
+ tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_validateStructuredSnapshots).call(this, snapshot);
1365
+ choice.message.parsed = parseStructuredStreamingJSON(validateStructuredJSONSnapshot(choice.message.content));
1366
+ }
1367
+ else if (content.length > 0) {
1368
+ choice.message.parsed = null;
1369
+ }
1370
+ }
1371
+ else {
1372
+ choice.message.content = (choice.message.content || '') + content;
444
1373
  }
445
1374
  }
446
1375
  if (tool_calls) {
@@ -448,11 +1377,23 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
448
1377
  // entries are only partially filled in; they match `ChatCompletionSnapshot.Choice.Message.ToolCall`
449
1378
  // once every delta for them has been accumulated.
450
1379
  const toolCallSnapshots = ((_e = choice.message).tool_calls ?? (_e.tool_calls = []));
451
- for (const { index, id, type, function: fn, custom, ...rest } of tool_calls) {
1380
+ for (const toolCallDelta of tool_calls) {
1381
+ const { index, id, type, function: fn, custom, ...rest } = toolCallDelta;
452
1382
  if (!Number.isSafeInteger(index) || index < 0 || index >= MAX_STREAM_TOOL_CALLS) {
453
1383
  throw new error_1.OpenAIError(`Chat completion stream contains an invalid tool call index: ${index}`);
454
1384
  }
1385
+ let argumentsDelta = '';
455
1386
  const tool_call = (toolCallSnapshots[index] ?? (toolCallSnapshots[index] = {}));
1387
+ const functionName = fn?.name;
1388
+ const eventState = tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_hasAutoParseableTool, "f") ? tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choice) : undefined;
1389
+ let boundIdentity = eventState?.tool_call_identities.get(index);
1390
+ if (boundIdentity) {
1391
+ assertBoundToolCallIdentity(tool_call, boundIdentity);
1392
+ if ((type !== undefined && type !== boundIdentity.type) ||
1393
+ (functionName !== undefined && functionName !== boundIdentity.name)) {
1394
+ throw new error_1.OpenAIError('Chat completion stream contains a changed tool call identity');
1395
+ }
1396
+ }
456
1397
  assignOwnProperties(tool_call, rest);
457
1398
  if (id) {
458
1399
  tool_call.id = id;
@@ -470,19 +1411,69 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
470
1411
  }
471
1412
  }
472
1413
  if (fn) {
473
- const functionSnapshot = (tool_call.function ?? (tool_call.function = { name: fn.name ?? '', arguments: '' }));
474
- if (fn.name) {
475
- functionSnapshot.name = fn.name;
1414
+ const functionSnapshot = (tool_call.function ?? (tool_call.function = { name: functionName ?? '', arguments: '' }));
1415
+ if (functionName) {
1416
+ functionSnapshot.name = functionName;
476
1417
  }
477
- if (fn.arguments) {
478
- functionSnapshot.arguments += fn.arguments;
479
- if ((0, parser_2.shouldParseToolCall)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"), tool_call)) {
480
- functionSnapshot.parsed_arguments = (0, parser_1.partialParse)(functionSnapshot.arguments);
1418
+ if (eventState && !boundIdentity) {
1419
+ const identity = ownFunctionToolIdentity(tool_call);
1420
+ const configuredTool = identity &&
1421
+ tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.tools?.find((tool) => (0, parser_2.isChatCompletionFunctionTool)(tool) && tool.function.name === identity.name);
1422
+ if (identity) {
1423
+ boundIdentity = {
1424
+ ...identity,
1425
+ parseable: configuredTool !== undefined &&
1426
+ (0, parser_2.shouldParseToolCall)(tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"), {
1427
+ type: identity.type,
1428
+ function: { name: identity.name },
1429
+ }),
1430
+ };
1431
+ eventState.tool_call_identities.set(index, boundIdentity);
1432
+ if (!boundIdentity.parseable) {
1433
+ const provisionalState = eventState.tool_call_parse_states.get(index);
1434
+ if (provisionalState) {
1435
+ tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f").bytes -= provisionalState.bytes;
1436
+ tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f").fragments -= provisionalState.fragments;
1437
+ tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f").work -= provisionalState.work;
1438
+ eventState.tool_call_parse_states.delete(index);
1439
+ }
1440
+ }
1441
+ }
1442
+ }
1443
+ const argumentFragment = fn.arguments;
1444
+ if (argumentFragment != null) {
1445
+ argumentsDelta = argumentFragment;
1446
+ if (eventState && boundIdentity?.parseable !== false) {
1447
+ let parseState = eventState.tool_call_parse_states.get(index);
1448
+ if (!parseState) {
1449
+ parseState = createPartialJSONParseState();
1450
+ eventState.tool_call_parse_states.set(index, parseState);
1451
+ }
1452
+ const shouldParse = recordPartialJSONFragment(parseState, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f"), argumentFragment);
1453
+ const previousArguments = captureStructuredJSONSnapshot(functionSnapshot, 'arguments');
1454
+ if (typeof previousArguments !== 'string') {
1455
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
1456
+ }
1457
+ functionSnapshot.arguments = previousArguments + argumentFragment;
1458
+ if (shouldParse &&
1459
+ boundIdentity?.parseable === true &&
1460
+ reservePartialJSONParse(parseState, tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_partialJSONParseBudget, "f"))) {
1461
+ tslib_1.__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_validateStructuredSnapshots).call(this, snapshot);
1462
+ functionSnapshot.parsed_arguments = parseStructuredStreamingJSON(validateStructuredJSONSnapshot(functionSnapshot.arguments));
1463
+ }
1464
+ else if (argumentFragment.length > 0 && (0, values_1.hasOwn)(functionSnapshot, 'parsed_arguments')) {
1465
+ functionSnapshot.parsed_arguments = undefined;
1466
+ }
1467
+ }
1468
+ else {
1469
+ functionSnapshot.arguments += argumentFragment;
481
1470
  }
482
1471
  }
483
1472
  }
1473
+ capturedToolCalls.push(Object.freeze({ index, arguments_delta: argumentsDelta }));
484
1474
  }
485
1475
  }
1476
+ Object.freeze(capturedToolCalls);
486
1477
  }
487
1478
  return snapshot;
488
1479
  }, Symbol.asyncIterator)]() {
@@ -499,12 +1490,36 @@ class ChatCompletionStream extends AbstractChatCompletionRunner_1.AbstractChatCo
499
1490
  }
500
1491
  }
501
1492
  exports.ChatCompletionStream = ChatCompletionStream;
502
- function finalizeChatCompletion(snapshot, params, audioDoneChoiceIndexes) {
1493
+ function finalizeChatCompletion(snapshot, params, audioDoneChoiceIndexes, validatedMessages) {
503
1494
  const { id, choices, created, model, system_fingerprint, ...rest } = snapshot;
504
1495
  const completion = {
505
1496
  ...rest,
506
1497
  id,
507
- choices: choices.map(({ message, finish_reason, index, logprobs, ...choiceRest }) => {
1498
+ choices: mapCapturedSnapshotArray(choices, MAX_STREAM_CHOICES, 'choice', (choice) => {
1499
+ const validated = validatedMessages.get(choice);
1500
+ if (!validated) {
1501
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
1502
+ }
1503
+ const stableChoice = new Proxy(choice, {
1504
+ get(target, property, receiver) {
1505
+ return property === 'message' ? validated.message : Reflect.get(target, property, receiver);
1506
+ },
1507
+ });
1508
+ const { message: sourceMessage, finish_reason, index, logprobs, ...choiceRest } = stableChoice;
1509
+ const message = new Proxy(sourceMessage, {
1510
+ get(target, property, receiver) {
1511
+ if (property === 'content') {
1512
+ return validated.content;
1513
+ }
1514
+ if (property === 'refusal') {
1515
+ return validated.refusal;
1516
+ }
1517
+ if (property === 'tool_calls') {
1518
+ return validated.toolCallCollection;
1519
+ }
1520
+ return Reflect.get(target, property, receiver);
1521
+ },
1522
+ });
508
1523
  const { content = null, function_call, tool_calls, audio, ...messageRest } = message;
509
1524
  // Audio streams can end with an expires_at-only chunk after the
510
1525
  // generated audio, without a separate finish_reason.
@@ -551,15 +1566,54 @@ function finalizeChatCompletion(snapshot, params, audioDoneChoiceIndexes) {
551
1566
  role,
552
1567
  content,
553
1568
  refusal: message.refusal ?? null,
554
- tool_calls: tool_calls.map((tool_call, i) => {
555
- if (tool_call.type == null) {
556
- throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].type\n${str(snapshot)}`);
1569
+ tool_calls: mapCapturedSnapshotArray(tool_calls, MAX_STREAM_TOOL_CALLS, 'tool-call', (tool_call, i) => {
1570
+ const captured = validated.toolCalls.get(i);
1571
+ if (!captured) {
1572
+ const identity = ownFunctionToolIdentity(tool_call);
1573
+ if (identity &&
1574
+ (0, parser_2.shouldParseToolCall)(params, {
1575
+ type: identity.type,
1576
+ function: { name: identity.name },
1577
+ })) {
1578
+ throw new error_1.OpenAIError('Chat completion stream contains an unsafe structured JSON snapshot');
1579
+ }
557
1580
  }
558
- if (tool_call.type === 'custom') {
559
- const { custom, type, id, ...toolRest } = tool_call;
1581
+ if (captured && captured.tool !== tool_call) {
1582
+ throw new error_1.OpenAIError('Chat completion stream contains a changed tool call identity');
1583
+ }
1584
+ const stableFunction = captured &&
1585
+ new Proxy(captured.function, {
1586
+ get(target, property, receiver) {
1587
+ if (property === 'arguments') {
1588
+ return captured.arguments;
1589
+ }
1590
+ if (property === 'name') {
1591
+ return captured.name;
1592
+ }
1593
+ return Reflect.get(target, property, receiver);
1594
+ },
1595
+ });
1596
+ const stableTool = captured && stableFunction
1597
+ ? new Proxy(tool_call, {
1598
+ get(target, property, receiver) {
1599
+ if (property === 'type') {
1600
+ return captured.type;
1601
+ }
1602
+ if (property === 'function') {
1603
+ return stableFunction;
1604
+ }
1605
+ return Reflect.get(target, property, receiver);
1606
+ },
1607
+ })
1608
+ : tool_call;
1609
+ if (stableTool.type == null) {
1610
+ throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].type`);
1611
+ }
1612
+ if (stableTool.type === 'custom') {
1613
+ const { custom, type, id, ...toolRest } = stableTool;
560
1614
  const { input = '', name, ...customRest } = custom || {};
561
1615
  if (name == null) {
562
- throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].custom.name\n${str(snapshot)}`);
1616
+ throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].custom.name`);
563
1617
  }
564
1618
  return {
565
1619
  ...toolRest,
@@ -568,13 +1622,13 @@ function finalizeChatCompletion(snapshot, params, audioDoneChoiceIndexes) {
568
1622
  custom: { ...customRest, name, input },
569
1623
  };
570
1624
  }
571
- const { function: fn, type, id, ...toolRest } = tool_call;
1625
+ const { function: fn, type, id, ...toolRest } = stableTool;
572
1626
  const { arguments: args, name, ...fnRest } = fn || {};
573
1627
  if (name == null) {
574
- throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].function.name\n${str(snapshot)}`);
1628
+ throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].function.name`);
575
1629
  }
576
1630
  if (args == null) {
577
- throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].function.arguments\n${str(snapshot)}`);
1631
+ throw new error_1.OpenAIError(`missing choices[${index}].tool_calls[${i}].function.arguments`);
578
1632
  }
579
1633
  return {
580
1634
  ...toolRest,
@@ -604,9 +1658,6 @@ function finalizeChatCompletion(snapshot, params, audioDoneChoiceIndexes) {
604
1658
  function isCompleteAudio(audio) {
605
1659
  return audio?.id != null && audio.data != null && audio.transcript != null && audio.expires_at != null;
606
1660
  }
607
- function str(x) {
608
- return JSON.stringify(x);
609
- }
610
1661
  /**
611
1662
  * Ensures the given argument is an empty object, useful for
612
1663
  * asserting that all known properties on an object have been