openai 4.53.2 → 4.55.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 (463) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +2 -2
  3. package/_vendor/partial-json-parser/parser.d.ts +3 -0
  4. package/_vendor/partial-json-parser/parser.d.ts.map +1 -0
  5. package/_vendor/partial-json-parser/parser.js +226 -0
  6. package/_vendor/partial-json-parser/parser.js.map +1 -0
  7. package/_vendor/partial-json-parser/parser.mjs +223 -0
  8. package/_vendor/partial-json-parser/parser.mjs.map +1 -0
  9. package/_vendor/zod-to-json-schema/Options.d.ts +32 -0
  10. package/_vendor/zod-to-json-schema/Options.d.ts.map +1 -0
  11. package/_vendor/zod-to-json-schema/Options.js +36 -0
  12. package/_vendor/zod-to-json-schema/Options.js.map +1 -0
  13. package/_vendor/zod-to-json-schema/Options.mjs +32 -0
  14. package/_vendor/zod-to-json-schema/Options.mjs.map +1 -0
  15. package/_vendor/zod-to-json-schema/Refs.d.ts +15 -0
  16. package/_vendor/zod-to-json-schema/Refs.d.ts.map +1 -0
  17. package/_vendor/zod-to-json-schema/Refs.js +26 -0
  18. package/_vendor/zod-to-json-schema/Refs.js.map +1 -0
  19. package/_vendor/zod-to-json-schema/Refs.mjs +22 -0
  20. package/_vendor/zod-to-json-schema/Refs.mjs.map +1 -0
  21. package/_vendor/zod-to-json-schema/errorMessages.d.ts +12 -0
  22. package/_vendor/zod-to-json-schema/errorMessages.d.ts.map +1 -0
  23. package/_vendor/zod-to-json-schema/errorMessages.js +20 -0
  24. package/_vendor/zod-to-json-schema/errorMessages.js.map +1 -0
  25. package/_vendor/zod-to-json-schema/errorMessages.mjs +15 -0
  26. package/_vendor/zod-to-json-schema/errorMessages.mjs.map +1 -0
  27. package/_vendor/zod-to-json-schema/index.d.ts +38 -0
  28. package/_vendor/zod-to-json-schema/index.d.ts.map +1 -0
  29. package/_vendor/zod-to-json-schema/index.js +54 -0
  30. package/_vendor/zod-to-json-schema/index.js.map +1 -0
  31. package/_vendor/zod-to-json-schema/index.mjs +38 -0
  32. package/_vendor/zod-to-json-schema/index.mjs.map +1 -0
  33. package/_vendor/zod-to-json-schema/parseDef.d.ts +38 -0
  34. package/_vendor/zod-to-json-schema/parseDef.d.ts.map +1 -0
  35. package/_vendor/zod-to-json-schema/parseDef.js +168 -0
  36. package/_vendor/zod-to-json-schema/parseDef.js.map +1 -0
  37. package/_vendor/zod-to-json-schema/parseDef.mjs +164 -0
  38. package/_vendor/zod-to-json-schema/parseDef.mjs.map +1 -0
  39. package/_vendor/zod-to-json-schema/parsers/any.d.ts +3 -0
  40. package/_vendor/zod-to-json-schema/parsers/any.d.ts.map +1 -0
  41. package/_vendor/zod-to-json-schema/parsers/any.js +8 -0
  42. package/_vendor/zod-to-json-schema/parsers/any.js.map +1 -0
  43. package/_vendor/zod-to-json-schema/parsers/any.mjs +4 -0
  44. package/_vendor/zod-to-json-schema/parsers/any.mjs.map +1 -0
  45. package/_vendor/zod-to-json-schema/parsers/array.d.ts +13 -0
  46. package/_vendor/zod-to-json-schema/parsers/array.d.ts.map +1 -0
  47. package/_vendor/zod-to-json-schema/parsers/array.js +30 -0
  48. package/_vendor/zod-to-json-schema/parsers/array.js.map +1 -0
  49. package/_vendor/zod-to-json-schema/parsers/array.mjs +26 -0
  50. package/_vendor/zod-to-json-schema/parsers/array.mjs.map +1 -0
  51. package/_vendor/zod-to-json-schema/parsers/bigint.d.ts +15 -0
  52. package/_vendor/zod-to-json-schema/parsers/bigint.d.ts.map +1 -0
  53. package/_vendor/zod-to-json-schema/parsers/bigint.js +54 -0
  54. package/_vendor/zod-to-json-schema/parsers/bigint.js.map +1 -0
  55. package/_vendor/zod-to-json-schema/parsers/bigint.mjs +50 -0
  56. package/_vendor/zod-to-json-schema/parsers/bigint.mjs.map +1 -0
  57. package/_vendor/zod-to-json-schema/parsers/boolean.d.ts +5 -0
  58. package/_vendor/zod-to-json-schema/parsers/boolean.d.ts.map +1 -0
  59. package/_vendor/zod-to-json-schema/parsers/boolean.js +10 -0
  60. package/_vendor/zod-to-json-schema/parsers/boolean.js.map +1 -0
  61. package/_vendor/zod-to-json-schema/parsers/boolean.mjs +6 -0
  62. package/_vendor/zod-to-json-schema/parsers/boolean.mjs.map +1 -0
  63. package/_vendor/zod-to-json-schema/parsers/branded.d.ts +4 -0
  64. package/_vendor/zod-to-json-schema/parsers/branded.d.ts.map +1 -0
  65. package/_vendor/zod-to-json-schema/parsers/branded.js +9 -0
  66. package/_vendor/zod-to-json-schema/parsers/branded.js.map +1 -0
  67. package/_vendor/zod-to-json-schema/parsers/branded.mjs +5 -0
  68. package/_vendor/zod-to-json-schema/parsers/branded.mjs.map +1 -0
  69. package/_vendor/zod-to-json-schema/parsers/catch.d.ts +4 -0
  70. package/_vendor/zod-to-json-schema/parsers/catch.d.ts.map +1 -0
  71. package/_vendor/zod-to-json-schema/parsers/catch.js +9 -0
  72. package/_vendor/zod-to-json-schema/parsers/catch.js.map +1 -0
  73. package/_vendor/zod-to-json-schema/parsers/catch.mjs +5 -0
  74. package/_vendor/zod-to-json-schema/parsers/catch.mjs.map +1 -0
  75. package/_vendor/zod-to-json-schema/parsers/date.d.ts +16 -0
  76. package/_vendor/zod-to-json-schema/parsers/date.d.ts.map +1 -0
  77. package/_vendor/zod-to-json-schema/parsers/date.js +51 -0
  78. package/_vendor/zod-to-json-schema/parsers/date.js.map +1 -0
  79. package/_vendor/zod-to-json-schema/parsers/date.mjs +47 -0
  80. package/_vendor/zod-to-json-schema/parsers/date.mjs.map +1 -0
  81. package/_vendor/zod-to-json-schema/parsers/default.d.ts +7 -0
  82. package/_vendor/zod-to-json-schema/parsers/default.d.ts.map +1 -0
  83. package/_vendor/zod-to-json-schema/parsers/default.js +12 -0
  84. package/_vendor/zod-to-json-schema/parsers/default.js.map +1 -0
  85. package/_vendor/zod-to-json-schema/parsers/default.mjs +8 -0
  86. package/_vendor/zod-to-json-schema/parsers/default.mjs.map +1 -0
  87. package/_vendor/zod-to-json-schema/parsers/effects.d.ts +5 -0
  88. package/_vendor/zod-to-json-schema/parsers/effects.d.ts.map +1 -0
  89. package/_vendor/zod-to-json-schema/parsers/effects.js +9 -0
  90. package/_vendor/zod-to-json-schema/parsers/effects.js.map +1 -0
  91. package/_vendor/zod-to-json-schema/parsers/effects.mjs +5 -0
  92. package/_vendor/zod-to-json-schema/parsers/effects.mjs.map +1 -0
  93. package/_vendor/zod-to-json-schema/parsers/enum.d.ts +7 -0
  94. package/_vendor/zod-to-json-schema/parsers/enum.d.ts.map +1 -0
  95. package/_vendor/zod-to-json-schema/parsers/enum.js +11 -0
  96. package/_vendor/zod-to-json-schema/parsers/enum.js.map +1 -0
  97. package/_vendor/zod-to-json-schema/parsers/enum.mjs +7 -0
  98. package/_vendor/zod-to-json-schema/parsers/enum.mjs.map +1 -0
  99. package/_vendor/zod-to-json-schema/parsers/intersection.d.ts +9 -0
  100. package/_vendor/zod-to-json-schema/parsers/intersection.d.ts.map +1 -0
  101. package/_vendor/zod-to-json-schema/parsers/intersection.js +54 -0
  102. package/_vendor/zod-to-json-schema/parsers/intersection.js.map +1 -0
  103. package/_vendor/zod-to-json-schema/parsers/intersection.mjs +50 -0
  104. package/_vendor/zod-to-json-schema/parsers/intersection.mjs.map +1 -0
  105. package/_vendor/zod-to-json-schema/parsers/literal.d.ts +10 -0
  106. package/_vendor/zod-to-json-schema/parsers/literal.d.ts.map +1 -0
  107. package/_vendor/zod-to-json-schema/parsers/literal.js +26 -0
  108. package/_vendor/zod-to-json-schema/parsers/literal.js.map +1 -0
  109. package/_vendor/zod-to-json-schema/parsers/literal.mjs +22 -0
  110. package/_vendor/zod-to-json-schema/parsers/literal.mjs.map +1 -0
  111. package/_vendor/zod-to-json-schema/parsers/map.d.ts +16 -0
  112. package/_vendor/zod-to-json-schema/parsers/map.d.ts.map +1 -0
  113. package/_vendor/zod-to-json-schema/parsers/map.js +30 -0
  114. package/_vendor/zod-to-json-schema/parsers/map.js.map +1 -0
  115. package/_vendor/zod-to-json-schema/parsers/map.mjs +26 -0
  116. package/_vendor/zod-to-json-schema/parsers/map.mjs.map +1 -0
  117. package/_vendor/zod-to-json-schema/parsers/nativeEnum.d.ts +7 -0
  118. package/_vendor/zod-to-json-schema/parsers/nativeEnum.d.ts.map +1 -0
  119. package/_vendor/zod-to-json-schema/parsers/nativeEnum.js +21 -0
  120. package/_vendor/zod-to-json-schema/parsers/nativeEnum.js.map +1 -0
  121. package/_vendor/zod-to-json-schema/parsers/nativeEnum.mjs +17 -0
  122. package/_vendor/zod-to-json-schema/parsers/nativeEnum.mjs.map +1 -0
  123. package/_vendor/zod-to-json-schema/parsers/never.d.ts +5 -0
  124. package/_vendor/zod-to-json-schema/parsers/never.d.ts.map +1 -0
  125. package/_vendor/zod-to-json-schema/parsers/never.js +10 -0
  126. package/_vendor/zod-to-json-schema/parsers/never.js.map +1 -0
  127. package/_vendor/zod-to-json-schema/parsers/never.mjs +6 -0
  128. package/_vendor/zod-to-json-schema/parsers/never.mjs.map +1 -0
  129. package/_vendor/zod-to-json-schema/parsers/null.d.ts +6 -0
  130. package/_vendor/zod-to-json-schema/parsers/null.d.ts.map +1 -0
  131. package/_vendor/zod-to-json-schema/parsers/null.js +15 -0
  132. package/_vendor/zod-to-json-schema/parsers/null.js.map +1 -0
  133. package/_vendor/zod-to-json-schema/parsers/null.mjs +11 -0
  134. package/_vendor/zod-to-json-schema/parsers/null.mjs.map +1 -0
  135. package/_vendor/zod-to-json-schema/parsers/nullable.d.ts +11 -0
  136. package/_vendor/zod-to-json-schema/parsers/nullable.d.ts.map +1 -0
  137. package/_vendor/zod-to-json-schema/parsers/nullable.js +35 -0
  138. package/_vendor/zod-to-json-schema/parsers/nullable.js.map +1 -0
  139. package/_vendor/zod-to-json-schema/parsers/nullable.mjs +31 -0
  140. package/_vendor/zod-to-json-schema/parsers/nullable.mjs.map +1 -0
  141. package/_vendor/zod-to-json-schema/parsers/number.d.ts +14 -0
  142. package/_vendor/zod-to-json-schema/parsers/number.d.ts.map +1 -0
  143. package/_vendor/zod-to-json-schema/parsers/number.js +57 -0
  144. package/_vendor/zod-to-json-schema/parsers/number.js.map +1 -0
  145. package/_vendor/zod-to-json-schema/parsers/number.mjs +53 -0
  146. package/_vendor/zod-to-json-schema/parsers/number.mjs.map +1 -0
  147. package/_vendor/zod-to-json-schema/parsers/object.d.ts +11 -0
  148. package/_vendor/zod-to-json-schema/parsers/object.d.ts.map +1 -0
  149. package/_vendor/zod-to-json-schema/parsers/object.js +51 -0
  150. package/_vendor/zod-to-json-schema/parsers/object.js.map +1 -0
  151. package/_vendor/zod-to-json-schema/parsers/object.mjs +47 -0
  152. package/_vendor/zod-to-json-schema/parsers/object.mjs.map +1 -0
  153. package/_vendor/zod-to-json-schema/parsers/optional.d.ts +5 -0
  154. package/_vendor/zod-to-json-schema/parsers/optional.d.ts.map +1 -0
  155. package/_vendor/zod-to-json-schema/parsers/optional.js +25 -0
  156. package/_vendor/zod-to-json-schema/parsers/optional.js.map +1 -0
  157. package/_vendor/zod-to-json-schema/parsers/optional.mjs +21 -0
  158. package/_vendor/zod-to-json-schema/parsers/optional.mjs.map +1 -0
  159. package/_vendor/zod-to-json-schema/parsers/pipeline.d.ts +6 -0
  160. package/_vendor/zod-to-json-schema/parsers/pipeline.d.ts.map +1 -0
  161. package/_vendor/zod-to-json-schema/parsers/pipeline.js +25 -0
  162. package/_vendor/zod-to-json-schema/parsers/pipeline.js.map +1 -0
  163. package/_vendor/zod-to-json-schema/parsers/pipeline.mjs +21 -0
  164. package/_vendor/zod-to-json-schema/parsers/pipeline.mjs.map +1 -0
  165. package/_vendor/zod-to-json-schema/parsers/promise.d.ts +5 -0
  166. package/_vendor/zod-to-json-schema/parsers/promise.d.ts.map +1 -0
  167. package/_vendor/zod-to-json-schema/parsers/promise.js +9 -0
  168. package/_vendor/zod-to-json-schema/parsers/promise.js.map +1 -0
  169. package/_vendor/zod-to-json-schema/parsers/promise.mjs +5 -0
  170. package/_vendor/zod-to-json-schema/parsers/promise.mjs.map +1 -0
  171. package/_vendor/zod-to-json-schema/parsers/readonly.d.ts +4 -0
  172. package/_vendor/zod-to-json-schema/parsers/readonly.d.ts.map +1 -0
  173. package/_vendor/zod-to-json-schema/parsers/readonly.js +9 -0
  174. package/_vendor/zod-to-json-schema/parsers/readonly.js.map +1 -0
  175. package/_vendor/zod-to-json-schema/parsers/readonly.mjs +5 -0
  176. package/_vendor/zod-to-json-schema/parsers/readonly.mjs.map +1 -0
  177. package/_vendor/zod-to-json-schema/parsers/record.d.ts +14 -0
  178. package/_vendor/zod-to-json-schema/parsers/record.d.ts.map +1 -0
  179. package/_vendor/zod-to-json-schema/parsers/record.js +50 -0
  180. package/_vendor/zod-to-json-schema/parsers/record.js.map +1 -0
  181. package/_vendor/zod-to-json-schema/parsers/record.mjs +46 -0
  182. package/_vendor/zod-to-json-schema/parsers/record.mjs.map +1 -0
  183. package/_vendor/zod-to-json-schema/parsers/set.d.ts +14 -0
  184. package/_vendor/zod-to-json-schema/parsers/set.d.ts.map +1 -0
  185. package/_vendor/zod-to-json-schema/parsers/set.js +25 -0
  186. package/_vendor/zod-to-json-schema/parsers/set.js.map +1 -0
  187. package/_vendor/zod-to-json-schema/parsers/set.mjs +21 -0
  188. package/_vendor/zod-to-json-schema/parsers/set.mjs.map +1 -0
  189. package/_vendor/zod-to-json-schema/parsers/string.d.ts +70 -0
  190. package/_vendor/zod-to-json-schema/parsers/string.d.ts.map +1 -0
  191. package/_vendor/zod-to-json-schema/parsers/string.js +316 -0
  192. package/_vendor/zod-to-json-schema/parsers/string.js.map +1 -0
  193. package/_vendor/zod-to-json-schema/parsers/string.mjs +312 -0
  194. package/_vendor/zod-to-json-schema/parsers/string.mjs.map +1 -0
  195. package/_vendor/zod-to-json-schema/parsers/tuple.d.ts +14 -0
  196. package/_vendor/zod-to-json-schema/parsers/tuple.d.ts.map +1 -0
  197. package/_vendor/zod-to-json-schema/parsers/tuple.js +37 -0
  198. package/_vendor/zod-to-json-schema/parsers/tuple.js.map +1 -0
  199. package/_vendor/zod-to-json-schema/parsers/tuple.mjs +33 -0
  200. package/_vendor/zod-to-json-schema/parsers/tuple.mjs.map +1 -0
  201. package/_vendor/zod-to-json-schema/parsers/undefined.d.ts +5 -0
  202. package/_vendor/zod-to-json-schema/parsers/undefined.d.ts.map +1 -0
  203. package/_vendor/zod-to-json-schema/parsers/undefined.js +10 -0
  204. package/_vendor/zod-to-json-schema/parsers/undefined.js.map +1 -0
  205. package/_vendor/zod-to-json-schema/parsers/undefined.mjs +6 -0
  206. package/_vendor/zod-to-json-schema/parsers/undefined.mjs.map +1 -0
  207. package/_vendor/zod-to-json-schema/parsers/union.d.ts +24 -0
  208. package/_vendor/zod-to-json-schema/parsers/union.d.ts.map +1 -0
  209. package/_vendor/zod-to-json-schema/parsers/union.js +77 -0
  210. package/_vendor/zod-to-json-schema/parsers/union.js.map +1 -0
  211. package/_vendor/zod-to-json-schema/parsers/union.mjs +73 -0
  212. package/_vendor/zod-to-json-schema/parsers/union.mjs.map +1 -0
  213. package/_vendor/zod-to-json-schema/parsers/unknown.d.ts +3 -0
  214. package/_vendor/zod-to-json-schema/parsers/unknown.d.ts.map +1 -0
  215. package/_vendor/zod-to-json-schema/parsers/unknown.js +8 -0
  216. package/_vendor/zod-to-json-schema/parsers/unknown.js.map +1 -0
  217. package/_vendor/zod-to-json-schema/parsers/unknown.mjs +4 -0
  218. package/_vendor/zod-to-json-schema/parsers/unknown.mjs.map +1 -0
  219. package/_vendor/zod-to-json-schema/zodToJsonSchema.d.ts +11 -0
  220. package/_vendor/zod-to-json-schema/zodToJsonSchema.d.ts.map +1 -0
  221. package/_vendor/zod-to-json-schema/zodToJsonSchema.js +53 -0
  222. package/_vendor/zod-to-json-schema/zodToJsonSchema.js.map +1 -0
  223. package/_vendor/zod-to-json-schema/zodToJsonSchema.mjs +50 -0
  224. package/_vendor/zod-to-json-schema/zodToJsonSchema.mjs.map +1 -0
  225. package/error.d.ts +6 -0
  226. package/error.d.ts.map +1 -1
  227. package/error.js +13 -1
  228. package/error.js.map +1 -1
  229. package/error.mjs +10 -0
  230. package/error.mjs.map +1 -1
  231. package/helpers/zod.d.ts +57 -0
  232. package/helpers/zod.d.ts.map +1 -0
  233. package/helpers/zod.js +79 -0
  234. package/helpers/zod.js.map +1 -0
  235. package/helpers/zod.mjs +74 -0
  236. package/helpers/zod.mjs.map +1 -0
  237. package/index.d.mts +8 -0
  238. package/index.d.ts +8 -0
  239. package/index.d.ts.map +1 -1
  240. package/index.js +1 -0
  241. package/index.js.map +1 -1
  242. package/index.mjs +1 -0
  243. package/index.mjs.map +1 -1
  244. package/lib/AbstractChatCompletionRunner.d.ts +15 -68
  245. package/lib/AbstractChatCompletionRunner.d.ts.map +1 -1
  246. package/lib/AbstractChatCompletionRunner.js +47 -200
  247. package/lib/AbstractChatCompletionRunner.js.map +1 -1
  248. package/lib/AbstractChatCompletionRunner.mjs +48 -201
  249. package/lib/AbstractChatCompletionRunner.mjs.map +1 -1
  250. package/lib/AssistantStream.d.ts +10 -6
  251. package/lib/AssistantStream.d.ts.map +1 -1
  252. package/lib/AssistantStream.js +18 -6
  253. package/lib/AssistantStream.js.map +1 -1
  254. package/lib/AssistantStream.mjs +18 -6
  255. package/lib/AssistantStream.mjs.map +1 -1
  256. package/lib/ChatCompletionRunner.d.ts +8 -6
  257. package/lib/ChatCompletionRunner.d.ts.map +1 -1
  258. package/lib/ChatCompletionRunner.js +4 -4
  259. package/lib/ChatCompletionRunner.js.map +1 -1
  260. package/lib/ChatCompletionRunner.mjs +4 -4
  261. package/lib/ChatCompletionRunner.mjs.map +1 -1
  262. package/lib/ChatCompletionStream.d.ts +71 -12
  263. package/lib/ChatCompletionStream.d.ts.map +1 -1
  264. package/lib/ChatCompletionStream.js +233 -42
  265. package/lib/ChatCompletionStream.js.map +1 -1
  266. package/lib/ChatCompletionStream.mjs +234 -43
  267. package/lib/ChatCompletionStream.mjs.map +1 -1
  268. package/lib/ChatCompletionStreamingRunner.d.ts +8 -6
  269. package/lib/ChatCompletionStreamingRunner.d.ts.map +1 -1
  270. package/lib/ChatCompletionStreamingRunner.js +9 -7
  271. package/lib/ChatCompletionStreamingRunner.js.map +1 -1
  272. package/lib/ChatCompletionStreamingRunner.mjs +9 -7
  273. package/lib/ChatCompletionStreamingRunner.mjs.map +1 -1
  274. package/lib/EventStream.d.ts +63 -0
  275. package/lib/EventStream.d.ts.map +1 -0
  276. package/lib/EventStream.js +200 -0
  277. package/lib/EventStream.js.map +1 -0
  278. package/lib/EventStream.mjs +196 -0
  279. package/lib/EventStream.mjs.map +1 -0
  280. package/lib/RunnableFunction.d.ts +4 -2
  281. package/lib/RunnableFunction.d.ts.map +1 -1
  282. package/lib/RunnableFunction.js.map +1 -1
  283. package/lib/RunnableFunction.mjs.map +1 -1
  284. package/lib/parser.d.ts +37 -0
  285. package/lib/parser.d.ts.map +1 -0
  286. package/lib/parser.js +133 -0
  287. package/lib/parser.js.map +1 -0
  288. package/lib/parser.mjs +121 -0
  289. package/lib/parser.mjs.map +1 -0
  290. package/package.json +10 -2
  291. package/resources/audio/audio.d.ts +4 -0
  292. package/resources/audio/audio.d.ts.map +1 -1
  293. package/resources/audio/audio.js.map +1 -1
  294. package/resources/audio/audio.mjs.map +1 -1
  295. package/resources/audio/index.d.ts +2 -2
  296. package/resources/audio/index.d.ts.map +1 -1
  297. package/resources/audio/index.js.map +1 -1
  298. package/resources/audio/index.mjs.map +1 -1
  299. package/resources/audio/speech.d.ts +3 -1
  300. package/resources/audio/speech.d.ts.map +1 -1
  301. package/resources/audio/speech.js.map +1 -1
  302. package/resources/audio/speech.mjs.map +1 -1
  303. package/resources/audio/transcriptions.d.ts +2 -1
  304. package/resources/audio/transcriptions.d.ts.map +1 -1
  305. package/resources/audio/transcriptions.js.map +1 -1
  306. package/resources/audio/transcriptions.mjs.map +1 -1
  307. package/resources/audio/translations.d.ts +2 -1
  308. package/resources/audio/translations.d.ts.map +1 -1
  309. package/resources/audio/translations.js.map +1 -1
  310. package/resources/audio/translations.mjs.map +1 -1
  311. package/resources/beta/assistants.d.ts +19 -3
  312. package/resources/beta/assistants.d.ts.map +1 -1
  313. package/resources/beta/assistants.js.map +1 -1
  314. package/resources/beta/assistants.mjs.map +1 -1
  315. package/resources/beta/beta.d.ts +0 -1
  316. package/resources/beta/beta.d.ts.map +1 -1
  317. package/resources/beta/beta.js.map +1 -1
  318. package/resources/beta/beta.mjs.map +1 -1
  319. package/resources/beta/chat/completions.d.ts +25 -5
  320. package/resources/beta/chat/completions.d.ts.map +1 -1
  321. package/resources/beta/chat/completions.js +17 -5
  322. package/resources/beta/chat/completions.js.map +1 -1
  323. package/resources/beta/chat/completions.mjs +17 -5
  324. package/resources/beta/chat/completions.mjs.map +1 -1
  325. package/resources/beta/index.d.ts +1 -1
  326. package/resources/beta/index.d.ts.map +1 -1
  327. package/resources/beta/index.js.map +1 -1
  328. package/resources/beta/index.mjs.map +1 -1
  329. package/resources/beta/threads/index.d.ts +2 -2
  330. package/resources/beta/threads/index.d.ts.map +1 -1
  331. package/resources/beta/threads/index.js.map +1 -1
  332. package/resources/beta/threads/index.mjs.map +1 -1
  333. package/resources/beta/threads/messages.d.ts +28 -2
  334. package/resources/beta/threads/messages.d.ts.map +1 -1
  335. package/resources/beta/threads/messages.js.map +1 -1
  336. package/resources/beta/threads/messages.mjs.map +1 -1
  337. package/resources/beta/threads/runs/runs.d.ts +12 -1
  338. package/resources/beta/threads/runs/runs.d.ts.map +1 -1
  339. package/resources/beta/threads/runs/runs.js.map +1 -1
  340. package/resources/beta/threads/runs/runs.mjs.map +1 -1
  341. package/resources/beta/threads/threads.d.ts +22 -8
  342. package/resources/beta/threads/threads.d.ts.map +1 -1
  343. package/resources/beta/threads/threads.js.map +1 -1
  344. package/resources/beta/threads/threads.mjs.map +1 -1
  345. package/resources/beta/vector-stores/files.d.ts +1 -1
  346. package/resources/beta/vector-stores/files.d.ts.map +1 -1
  347. package/resources/chat/chat.d.ts +2 -1
  348. package/resources/chat/chat.d.ts.map +1 -1
  349. package/resources/chat/chat.js.map +1 -1
  350. package/resources/chat/chat.mjs.map +1 -1
  351. package/resources/chat/completions.d.ts +37 -26
  352. package/resources/chat/completions.d.ts.map +1 -1
  353. package/resources/chat/completions.js.map +1 -1
  354. package/resources/chat/completions.mjs.map +1 -1
  355. package/resources/chat/index.d.ts +1 -1
  356. package/resources/chat/index.d.ts.map +1 -1
  357. package/resources/chat/index.js.map +1 -1
  358. package/resources/chat/index.mjs.map +1 -1
  359. package/resources/fine-tuning/jobs/jobs.d.ts +3 -3
  360. package/resources/fine-tuning/jobs/jobs.d.ts.map +1 -1
  361. package/resources/images.d.ts +5 -3
  362. package/resources/images.d.ts.map +1 -1
  363. package/resources/images.js.map +1 -1
  364. package/resources/images.mjs.map +1 -1
  365. package/resources/index.d.ts +3 -3
  366. package/resources/index.d.ts.map +1 -1
  367. package/resources/index.js.map +1 -1
  368. package/resources/index.mjs +1 -1
  369. package/resources/index.mjs.map +1 -1
  370. package/resources/moderations.d.ts +3 -1
  371. package/resources/moderations.d.ts.map +1 -1
  372. package/resources/moderations.js.map +1 -1
  373. package/resources/moderations.mjs.map +1 -1
  374. package/resources/shared.d.ts +53 -0
  375. package/resources/shared.d.ts.map +1 -1
  376. package/src/_vendor/partial-json-parser/README.md +3 -0
  377. package/src/_vendor/partial-json-parser/parser.ts +264 -0
  378. package/src/_vendor/zod-to-json-schema/Options.ts +73 -0
  379. package/src/_vendor/zod-to-json-schema/Refs.ts +39 -0
  380. package/src/_vendor/zod-to-json-schema/errorMessages.ts +31 -0
  381. package/src/_vendor/zod-to-json-schema/index.ts +37 -0
  382. package/src/_vendor/zod-to-json-schema/parseDef.ts +231 -0
  383. package/src/_vendor/zod-to-json-schema/parsers/any.ts +5 -0
  384. package/src/_vendor/zod-to-json-schema/parsers/array.ts +36 -0
  385. package/src/_vendor/zod-to-json-schema/parsers/bigint.ts +60 -0
  386. package/src/_vendor/zod-to-json-schema/parsers/boolean.ts +9 -0
  387. package/src/_vendor/zod-to-json-schema/parsers/branded.ts +7 -0
  388. package/src/_vendor/zod-to-json-schema/parsers/catch.ts +7 -0
  389. package/src/_vendor/zod-to-json-schema/parsers/date.ts +83 -0
  390. package/src/_vendor/zod-to-json-schema/parsers/default.ts +10 -0
  391. package/src/_vendor/zod-to-json-schema/parsers/effects.ts +7 -0
  392. package/src/_vendor/zod-to-json-schema/parsers/enum.ts +13 -0
  393. package/src/_vendor/zod-to-json-schema/parsers/intersection.ts +64 -0
  394. package/src/_vendor/zod-to-json-schema/parsers/literal.ts +37 -0
  395. package/src/_vendor/zod-to-json-schema/parsers/map.ts +42 -0
  396. package/src/_vendor/zod-to-json-schema/parsers/nativeEnum.ts +27 -0
  397. package/src/_vendor/zod-to-json-schema/parsers/never.ts +9 -0
  398. package/src/_vendor/zod-to-json-schema/parsers/null.ts +16 -0
  399. package/src/_vendor/zod-to-json-schema/parsers/nullable.ts +49 -0
  400. package/src/_vendor/zod-to-json-schema/parsers/number.ts +62 -0
  401. package/src/_vendor/zod-to-json-schema/parsers/object.ts +63 -0
  402. package/src/_vendor/zod-to-json-schema/parsers/optional.ts +25 -0
  403. package/src/_vendor/zod-to-json-schema/parsers/pipeline.ts +28 -0
  404. package/src/_vendor/zod-to-json-schema/parsers/promise.ts +7 -0
  405. package/src/_vendor/zod-to-json-schema/parsers/readonly.ts +7 -0
  406. package/src/_vendor/zod-to-json-schema/parsers/record.ts +73 -0
  407. package/src/_vendor/zod-to-json-schema/parsers/set.ts +36 -0
  408. package/src/_vendor/zod-to-json-schema/parsers/string.ts +400 -0
  409. package/src/_vendor/zod-to-json-schema/parsers/tuple.ts +54 -0
  410. package/src/_vendor/zod-to-json-schema/parsers/undefined.ts +9 -0
  411. package/src/_vendor/zod-to-json-schema/parsers/union.ts +119 -0
  412. package/src/_vendor/zod-to-json-schema/parsers/unknown.ts +5 -0
  413. package/src/_vendor/zod-to-json-schema/zodToJsonSchema.ts +91 -0
  414. package/src/error.ts +12 -0
  415. package/src/helpers/zod.ts +102 -0
  416. package/src/index.ts +8 -0
  417. package/src/lib/AbstractChatCompletionRunner.ts +72 -258
  418. package/src/lib/AssistantStream.ts +47 -19
  419. package/src/lib/ChatCompletionRunner.ts +16 -12
  420. package/src/lib/ChatCompletionStream.ts +423 -47
  421. package/src/lib/ChatCompletionStreamingRunner.ts +19 -15
  422. package/src/lib/{AbstractAssistantStreamRunner.ts → EventStream.ts} +37 -138
  423. package/src/lib/RunnableFunction.ts +4 -2
  424. package/src/lib/parser.ts +235 -0
  425. package/src/resources/audio/audio.ts +5 -0
  426. package/src/resources/audio/index.ts +2 -2
  427. package/src/resources/audio/speech.ts +4 -1
  428. package/src/resources/audio/transcriptions.ts +2 -1
  429. package/src/resources/audio/translations.ts +2 -1
  430. package/src/resources/beta/assistants.ts +19 -26
  431. package/src/resources/beta/beta.ts +0 -1
  432. package/src/resources/beta/chat/completions.ts +77 -27
  433. package/src/resources/beta/index.ts +0 -1
  434. package/src/resources/beta/threads/index.ts +2 -1
  435. package/src/resources/beta/threads/messages.ts +41 -2
  436. package/src/resources/beta/threads/runs/runs.ts +12 -25
  437. package/src/resources/beta/threads/threads.ts +26 -32
  438. package/src/resources/beta/vector-stores/files.ts +1 -1
  439. package/src/resources/chat/chat.ts +2 -0
  440. package/src/resources/chat/completions.ts +47 -27
  441. package/src/resources/chat/index.ts +1 -0
  442. package/src/resources/fine-tuning/jobs/jobs.ts +3 -3
  443. package/src/resources/images.ts +6 -3
  444. package/src/resources/index.ts +9 -2
  445. package/src/resources/moderations.ts +4 -1
  446. package/src/resources/shared.ts +62 -0
  447. package/src/version.ts +1 -1
  448. package/version.d.ts +1 -1
  449. package/version.js +1 -1
  450. package/version.mjs +1 -1
  451. package/lib/AbstractAssistantStreamRunner.d.ts +0 -74
  452. package/lib/AbstractAssistantStreamRunner.d.ts.map +0 -1
  453. package/lib/AbstractAssistantStreamRunner.js +0 -246
  454. package/lib/AbstractAssistantStreamRunner.js.map +0 -1
  455. package/lib/AbstractAssistantStreamRunner.mjs +0 -242
  456. package/lib/AbstractAssistantStreamRunner.mjs.map +0 -1
  457. package/lib/ChatCompletionRunFunctions.test.d.ts +0 -2
  458. package/lib/ChatCompletionRunFunctions.test.d.ts.map +0 -1
  459. package/lib/ChatCompletionRunFunctions.test.js +0 -2177
  460. package/lib/ChatCompletionRunFunctions.test.js.map +0 -1
  461. package/lib/ChatCompletionRunFunctions.test.mjs +0 -2172
  462. package/lib/ChatCompletionRunFunctions.test.mjs.map +0 -1
  463. package/src/lib/ChatCompletionRunFunctions.test.ts +0 -2328
@@ -1,10 +1,16 @@
1
1
  import * as Core from "../core";
2
- import { OpenAIError, APIUserAbortError } from "../error";
3
2
  import {
4
- Completions,
3
+ OpenAIError,
4
+ APIUserAbortError,
5
+ LengthFinishReasonError,
6
+ ContentFilterFinishReasonError,
7
+ } from "../error";
8
+ import {
9
+ ChatCompletionTokenLogprob,
5
10
  type ChatCompletion,
6
11
  type ChatCompletionChunk,
7
12
  type ChatCompletionCreateParams,
13
+ type ChatCompletionCreateParamsStreaming,
8
14
  type ChatCompletionCreateParamsBase,
9
15
  } from "../resources/chat/completions";
10
16
  import {
@@ -13,22 +19,125 @@ import {
13
19
  } from './AbstractChatCompletionRunner';
14
20
  import { type ReadableStream } from "../_shims/index";
15
21
  import { Stream } from "../streaming";
22
+ import OpenAI from "../index";
23
+ import { ParsedChatCompletion } from "../resources/beta/chat/completions";
24
+ import {
25
+ AutoParseableResponseFormat,
26
+ hasAutoParseableInput,
27
+ isAutoParsableResponseFormat,
28
+ isAutoParsableTool,
29
+ maybeParseChatCompletion,
30
+ shouldParseToolCall,
31
+ } from "./parser";
32
+ import { partialParse } from '../_vendor/partial-json-parser/parser';
33
+
34
+ export interface ContentDeltaEvent {
35
+ delta: string;
36
+ snapshot: string;
37
+ parsed: unknown | null;
38
+ }
39
+
40
+ export interface ContentDoneEvent<ParsedT = null> {
41
+ content: string;
42
+ parsed: ParsedT | null;
43
+ }
44
+
45
+ export interface RefusalDeltaEvent {
46
+ delta: string;
47
+ snapshot: string;
48
+ }
49
+
50
+ export interface RefusalDoneEvent {
51
+ refusal: string;
52
+ }
53
+
54
+ export interface FunctionToolCallArgumentsDeltaEvent {
55
+ name: string;
56
+
57
+ index: number;
58
+
59
+ arguments: string;
60
+
61
+ parsed_arguments: unknown;
62
+
63
+ arguments_delta: string;
64
+ }
65
+
66
+ export interface FunctionToolCallArgumentsDoneEvent {
67
+ name: string;
68
+
69
+ index: number;
70
+
71
+ arguments: string;
16
72
 
17
- export interface ChatCompletionStreamEvents extends AbstractChatCompletionRunnerEvents {
73
+ parsed_arguments: unknown;
74
+ }
75
+
76
+ export interface LogProbsContentDeltaEvent {
77
+ content: Array<ChatCompletionTokenLogprob>;
78
+ snapshot: Array<ChatCompletionTokenLogprob>;
79
+ }
80
+
81
+ export interface LogProbsContentDoneEvent {
82
+ content: Array<ChatCompletionTokenLogprob>;
83
+ }
84
+
85
+ export interface LogProbsRefusalDeltaEvent {
86
+ refusal: Array<ChatCompletionTokenLogprob>;
87
+ snapshot: Array<ChatCompletionTokenLogprob>;
88
+ }
89
+
90
+ export interface LogProbsRefusalDoneEvent {
91
+ refusal: Array<ChatCompletionTokenLogprob>;
92
+ }
93
+
94
+ export interface ChatCompletionStreamEvents<ParsedT = null> extends AbstractChatCompletionRunnerEvents {
18
95
  content: (contentDelta: string, contentSnapshot: string) => void;
19
96
  chunk: (chunk: ChatCompletionChunk, snapshot: ChatCompletionSnapshot) => void;
97
+
98
+ 'content.delta': (props: ContentDeltaEvent) => void;
99
+ 'content.done': (props: ContentDoneEvent<ParsedT>) => void;
100
+
101
+ 'refusal.delta': (props: RefusalDeltaEvent) => void;
102
+ 'refusal.done': (props: RefusalDoneEvent) => void;
103
+
104
+ 'tool_calls.function.arguments.delta': (props: FunctionToolCallArgumentsDeltaEvent) => void;
105
+ 'tool_calls.function.arguments.done': (props: FunctionToolCallArgumentsDoneEvent) => void;
106
+
107
+ 'logprobs.content.delta': (props: LogProbsContentDeltaEvent) => void;
108
+ 'logprobs.content.done': (props: LogProbsContentDoneEvent) => void;
109
+
110
+ 'logprobs.refusal.delta': (props: LogProbsRefusalDeltaEvent) => void;
111
+ 'logprobs.refusal.done': (props: LogProbsRefusalDoneEvent) => void;
20
112
  }
21
113
 
22
114
  export type ChatCompletionStreamParams = Omit<ChatCompletionCreateParamsBase, 'stream'> & {
23
115
  stream?: true;
24
116
  };
25
117
 
26
- export class ChatCompletionStream
27
- extends AbstractChatCompletionRunner<ChatCompletionStreamEvents>
118
+ interface ChoiceEventState {
119
+ content_done: boolean;
120
+ refusal_done: boolean;
121
+ logprobs_content_done: boolean;
122
+ logprobs_refusal_done: boolean;
123
+ current_tool_call_index: number | null;
124
+ done_tool_calls: Set<number>;
125
+ }
126
+
127
+ export class ChatCompletionStream<ParsedT = null>
128
+ extends AbstractChatCompletionRunner<ChatCompletionStreamEvents<ParsedT>, ParsedT>
28
129
  implements AsyncIterable<ChatCompletionChunk>
29
130
  {
131
+ #params: ChatCompletionCreateParams | null;
132
+ #choiceEventStates: ChoiceEventState[];
30
133
  #currentChatCompletionSnapshot: ChatCompletionSnapshot | undefined;
31
134
 
135
+ constructor(params: ChatCompletionCreateParams | null) {
136
+ super();
137
+ this.#params = params;
138
+ this.#choiceEventStates = [];
139
+ }
140
+
32
141
  get currentChatCompletionSnapshot(): ChatCompletionSnapshot | undefined {
33
142
  return this.#currentChatCompletionSnapshot;
34
143
  }
@@ -40,21 +149,21 @@ export class ChatCompletionStream
40
149
  * Note that messages sent to the model do not appear in `.on('message')`
41
150
  * in this context.
42
151
  */
43
- static fromReadableStream(stream: ReadableStream): ChatCompletionStream {
44
- const runner = new ChatCompletionStream();
152
+ static fromReadableStream(stream: ReadableStream): ChatCompletionStream<null> {
153
+ const runner = new ChatCompletionStream(null);
45
154
  runner._run(() => runner._fromReadableStream(stream));
46
155
  return runner;
47
156
  }
48
157
 
49
- static createChatCompletion(
50
- completions: Completions,
158
+ static createChatCompletion<ParsedT>(
159
+ client: OpenAI,
51
160
  params: ChatCompletionStreamParams,
52
161
  options?: Core.RequestOptions,
53
- ): ChatCompletionStream {
54
- const runner = new ChatCompletionStream();
162
+ ): ChatCompletionStream<ParsedT> {
163
+ const runner = new ChatCompletionStream<ParsedT>(params as ChatCompletionCreateParamsStreaming);
55
164
  runner._run(() =>
56
165
  runner._runChatCompletion(
57
- completions,
166
+ client,
58
167
  { ...params, stream: true },
59
168
  { ...options, headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' } },
60
169
  ),
@@ -66,17 +175,184 @@ export class ChatCompletionStream
66
175
  if (this.ended) return;
67
176
  this.#currentChatCompletionSnapshot = undefined;
68
177
  }
69
- #addChunk(chunk: ChatCompletionChunk) {
178
+
179
+ #getChoiceEventState(choice: ChatCompletionSnapshot.Choice): ChoiceEventState {
180
+ let state = this.#choiceEventStates[choice.index];
181
+ if (state) {
182
+ return state;
183
+ }
184
+
185
+ state = {
186
+ content_done: false,
187
+ refusal_done: false,
188
+ logprobs_content_done: false,
189
+ logprobs_refusal_done: false,
190
+ done_tool_calls: new Set(),
191
+ current_tool_call_index: null,
192
+ };
193
+ this.#choiceEventStates[choice.index] = state;
194
+ return state;
195
+ }
196
+
197
+ #addChunk(this: ChatCompletionStream<ParsedT>, chunk: ChatCompletionChunk) {
70
198
  if (this.ended) return;
199
+
71
200
  const completion = this.#accumulateChatCompletion(chunk);
72
201
  this._emit('chunk', chunk, completion);
73
- const delta = chunk.choices[0]?.delta?.content;
74
- const snapshot = completion.choices[0]?.message;
75
- if (delta != null && snapshot?.role === 'assistant' && snapshot?.content) {
76
- this._emit('content', delta, snapshot.content);
202
+
203
+ for (const choice of chunk.choices) {
204
+ const choiceSnapshot = completion.choices[choice.index]!;
205
+
206
+ if (
207
+ choice.delta.content != null &&
208
+ choiceSnapshot.message?.role === 'assistant' &&
209
+ choiceSnapshot.message?.content
210
+ ) {
211
+ this._emit('content', choice.delta.content, choiceSnapshot.message.content);
212
+ this._emit('content.delta', {
213
+ delta: choice.delta.content,
214
+ snapshot: choiceSnapshot.message.content,
215
+ parsed: choiceSnapshot.message.parsed,
216
+ });
217
+ }
218
+
219
+ if (
220
+ choice.delta.refusal != null &&
221
+ choiceSnapshot.message?.role === 'assistant' &&
222
+ choiceSnapshot.message?.refusal
223
+ ) {
224
+ this._emit('refusal.delta', {
225
+ delta: choice.delta.refusal,
226
+ snapshot: choiceSnapshot.message.refusal,
227
+ });
228
+ }
229
+
230
+ if (choice.logprobs?.content != null && choiceSnapshot.message?.role === 'assistant') {
231
+ this._emit('logprobs.content.delta', {
232
+ content: choice.logprobs?.content,
233
+ snapshot: choiceSnapshot.logprobs?.content ?? [],
234
+ });
235
+ }
236
+
237
+ if (choice.logprobs?.refusal != null && choiceSnapshot.message?.role === 'assistant') {
238
+ this._emit('logprobs.refusal.delta', {
239
+ refusal: choice.logprobs?.refusal,
240
+ snapshot: choiceSnapshot.logprobs?.refusal ?? [],
241
+ });
242
+ }
243
+
244
+ const state = this.#getChoiceEventState(choiceSnapshot);
245
+
246
+ if (choiceSnapshot.finish_reason) {
247
+ this.#emitContentDoneEvents(choiceSnapshot);
248
+
249
+ if (state.current_tool_call_index != null) {
250
+ this.#emitToolCallDoneEvent(choiceSnapshot, state.current_tool_call_index);
251
+ }
252
+ }
253
+
254
+ for (const toolCall of choice.delta.tool_calls ?? []) {
255
+ if (state.current_tool_call_index !== toolCall.index) {
256
+ this.#emitContentDoneEvents(choiceSnapshot);
257
+
258
+ // new tool call started, the previous one is done
259
+ if (state.current_tool_call_index != null) {
260
+ this.#emitToolCallDoneEvent(choiceSnapshot, state.current_tool_call_index);
261
+ }
262
+ }
263
+
264
+ state.current_tool_call_index = toolCall.index;
265
+ }
266
+
267
+ for (const toolCallDelta of choice.delta.tool_calls ?? []) {
268
+ const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallDelta.index];
269
+ if (!toolCallSnapshot?.type) {
270
+ continue;
271
+ }
272
+
273
+ if (toolCallSnapshot?.type === 'function') {
274
+ this._emit('tool_calls.function.arguments.delta', {
275
+ name: toolCallSnapshot.function?.name,
276
+ index: toolCallDelta.index,
277
+ arguments: toolCallSnapshot.function.arguments,
278
+ parsed_arguments: toolCallSnapshot.function.parsed_arguments,
279
+ arguments_delta: toolCallDelta.function?.arguments ?? '',
280
+ });
281
+ } else {
282
+ assertNever(toolCallSnapshot?.type);
283
+ }
284
+ }
77
285
  }
78
286
  }
79
- #endRequest(): ChatCompletion {
287
+
288
+ #emitToolCallDoneEvent(choiceSnapshot: ChatCompletionSnapshot.Choice, toolCallIndex: number) {
289
+ const state = this.#getChoiceEventState(choiceSnapshot);
290
+ if (state.done_tool_calls.has(toolCallIndex)) {
291
+ // we've already fired the done event
292
+ return;
293
+ }
294
+
295
+ const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallIndex];
296
+ if (!toolCallSnapshot) {
297
+ throw new Error('no tool call snapshot');
298
+ }
299
+ if (!toolCallSnapshot.type) {
300
+ throw new Error('tool call snapshot missing `type`');
301
+ }
302
+
303
+ if (toolCallSnapshot.type === 'function') {
304
+ const inputTool = this.#params?.tools?.find(
305
+ (tool) => tool.type === 'function' && tool.function.name === toolCallSnapshot.function.name,
306
+ );
307
+
308
+ this._emit('tool_calls.function.arguments.done', {
309
+ name: toolCallSnapshot.function.name,
310
+ index: toolCallIndex,
311
+ arguments: toolCallSnapshot.function.arguments,
312
+ parsed_arguments:
313
+ isAutoParsableTool(inputTool) ? inputTool.$parseRaw(toolCallSnapshot.function.arguments)
314
+ : inputTool?.function.strict ? JSON.parse(toolCallSnapshot.function.arguments)
315
+ : null,
316
+ });
317
+ } else {
318
+ assertNever(toolCallSnapshot.type);
319
+ }
320
+ }
321
+
322
+ #emitContentDoneEvents(choiceSnapshot: ChatCompletionSnapshot.Choice) {
323
+ const state = this.#getChoiceEventState(choiceSnapshot);
324
+
325
+ if (choiceSnapshot.message.content && !state.content_done) {
326
+ state.content_done = true;
327
+
328
+ const responseFormat = this.#getAutoParseableResponseFormat();
329
+
330
+ this._emit('content.done', {
331
+ content: choiceSnapshot.message.content,
332
+ parsed: responseFormat ? responseFormat.$parseRaw(choiceSnapshot.message.content) : (null as any),
333
+ });
334
+ }
335
+
336
+ if (choiceSnapshot.message.refusal && !state.refusal_done) {
337
+ state.refusal_done = true;
338
+
339
+ this._emit('refusal.done', { refusal: choiceSnapshot.message.refusal });
340
+ }
341
+
342
+ if (choiceSnapshot.logprobs?.content && !state.logprobs_content_done) {
343
+ state.logprobs_content_done = true;
344
+
345
+ this._emit('logprobs.content.done', { content: choiceSnapshot.logprobs.content });
346
+ }
347
+
348
+ if (choiceSnapshot.logprobs?.refusal && !state.logprobs_refusal_done) {
349
+ state.logprobs_refusal_done = true;
350
+
351
+ this._emit('logprobs.refusal.done', { refusal: choiceSnapshot.logprobs.refusal });
352
+ }
353
+ }
354
+
355
+ #endRequest(): ParsedChatCompletion<ParsedT> {
80
356
  if (this.ended) {
81
357
  throw new OpenAIError(`stream has ended, this shouldn't happen`);
82
358
  }
@@ -85,21 +361,24 @@ export class ChatCompletionStream
85
361
  throw new OpenAIError(`request ended without sending any chunks`);
86
362
  }
87
363
  this.#currentChatCompletionSnapshot = undefined;
88
- return finalizeChatCompletion(snapshot);
364
+ this.#choiceEventStates = [];
365
+ return finalizeChatCompletion(snapshot, this.#params);
89
366
  }
90
367
 
91
368
  protected override async _createChatCompletion(
92
- completions: Completions,
369
+ client: OpenAI,
93
370
  params: ChatCompletionCreateParams,
94
371
  options?: Core.RequestOptions,
95
- ): Promise<ChatCompletion> {
372
+ ): Promise<ParsedChatCompletion<ParsedT>> {
373
+ super._createChatCompletion;
96
374
  const signal = options?.signal;
97
375
  if (signal) {
98
376
  if (signal.aborted) this.controller.abort();
99
377
  signal.addEventListener('abort', () => this.controller.abort());
100
378
  }
101
379
  this.#beginRequest();
102
- const stream = await completions.create(
380
+
381
+ const stream = await client.chat.completions.create(
103
382
  { ...params, stream: true },
104
383
  { ...options, signal: this.controller.signal },
105
384
  );
@@ -141,6 +420,15 @@ export class ChatCompletionStream
141
420
  return this._addChatCompletion(this.#endRequest());
142
421
  }
143
422
 
423
+ #getAutoParseableResponseFormat(): AutoParseableResponseFormat<ParsedT> | null {
424
+ const responseFormat = this.#params?.response_format;
425
+ if (isAutoParsableResponseFormat<ParsedT>(responseFormat)) {
426
+ return responseFormat;
427
+ }
428
+
429
+ return null;
430
+ }
431
+
144
432
  #accumulateChatCompletion(chunk: ChatCompletionChunk): ChatCompletionSnapshot {
145
433
  let snapshot = this.#currentChatCompletionSnapshot;
146
434
  const { choices, ...rest } = chunk;
@@ -163,23 +451,48 @@ export class ChatCompletionStream
163
451
  if (!choice.logprobs) {
164
452
  choice.logprobs = Object.assign({}, logprobs);
165
453
  } else {
166
- const { content, ...rest } = logprobs;
454
+ const { content, refusal, ...rest } = logprobs;
455
+ assertIsEmpty(rest);
167
456
  Object.assign(choice.logprobs, rest);
457
+
168
458
  if (content) {
169
459
  choice.logprobs.content ??= [];
170
460
  choice.logprobs.content.push(...content);
171
461
  }
462
+
463
+ if (refusal) {
464
+ choice.logprobs.refusal ??= [];
465
+ choice.logprobs.refusal.push(...refusal);
466
+ }
467
+ }
468
+ }
469
+
470
+ if (finish_reason) {
471
+ choice.finish_reason = finish_reason;
472
+
473
+ if (this.#params && hasAutoParseableInput(this.#params)) {
474
+ if (finish_reason === 'length') {
475
+ throw new LengthFinishReasonError();
476
+ }
477
+
478
+ if (finish_reason === 'content_filter') {
479
+ throw new ContentFilterFinishReasonError();
480
+ }
172
481
  }
173
482
  }
174
483
 
175
- if (finish_reason) choice.finish_reason = finish_reason;
176
484
  Object.assign(choice, other);
177
485
 
178
486
  if (!delta) continue; // Shouldn't happen; just in case.
179
- const { content, function_call, role, tool_calls, ...rest } = delta;
487
+
488
+ const { content, refusal, function_call, role, tool_calls, ...rest } = delta;
489
+ assertIsEmpty(rest);
180
490
  Object.assign(choice.message, rest);
181
491
 
182
- if (content) choice.message.content = (choice.message.content || '') + content;
492
+ if (refusal) {
493
+ choice.message.refusal = (choice.message.refusal || '') + refusal;
494
+ }
495
+
183
496
  if (role) choice.message.role = role;
184
497
  if (function_call) {
185
498
  if (!choice.message.function_call) {
@@ -192,23 +505,39 @@ export class ChatCompletionStream
192
505
  }
193
506
  }
194
507
  }
508
+ if (content) {
509
+ choice.message.content = (choice.message.content || '') + content;
510
+
511
+ if (!choice.message.refusal && this.#getAutoParseableResponseFormat()) {
512
+ choice.message.parsed = partialParse(choice.message.content);
513
+ }
514
+ }
515
+
195
516
  if (tool_calls) {
196
517
  if (!choice.message.tool_calls) choice.message.tool_calls = [];
518
+
197
519
  for (const { index, id, type, function: fn, ...rest } of tool_calls) {
198
- const tool_call = (choice.message.tool_calls[index] ??= {});
520
+ const tool_call = (choice.message.tool_calls[index] ??=
521
+ {} as ChatCompletionSnapshot.Choice.Message.ToolCall);
199
522
  Object.assign(tool_call, rest);
200
523
  if (id) tool_call.id = id;
201
524
  if (type) tool_call.type = type;
202
- if (fn) tool_call.function ??= { arguments: '' };
525
+ if (fn) tool_call.function ??= { name: fn.name ?? '', arguments: '' };
203
526
  if (fn?.name) tool_call.function!.name = fn.name;
204
- if (fn?.arguments) tool_call.function!.arguments += fn.arguments;
527
+ if (fn?.arguments) {
528
+ tool_call.function!.arguments += fn.arguments;
529
+
530
+ if (shouldParseToolCall(this.#params, tool_call)) {
531
+ tool_call.function!.parsed_arguments = partialParse(tool_call.function!.arguments);
532
+ }
533
+ }
205
534
  }
206
535
  }
207
536
  }
208
537
  return snapshot;
209
538
  }
210
539
 
211
- [Symbol.asyncIterator](): AsyncIterator<ChatCompletionChunk> {
540
+ [Symbol.asyncIterator](this: ChatCompletionStream<ParsedT>): AsyncIterator<ChatCompletionChunk> {
212
541
  const pushQueue: ChatCompletionChunk[] = [];
213
542
  const readQueue: {
214
543
  resolve: (chunk: ChatCompletionChunk | undefined) => void;
@@ -275,29 +604,50 @@ export class ChatCompletionStream
275
604
  }
276
605
  }
277
606
 
278
- function finalizeChatCompletion(snapshot: ChatCompletionSnapshot): ChatCompletion {
607
+ function finalizeChatCompletion<ParsedT>(
608
+ snapshot: ChatCompletionSnapshot,
609
+ params: ChatCompletionCreateParams | null,
610
+ ): ParsedChatCompletion<ParsedT> {
279
611
  const { id, choices, created, model, system_fingerprint, ...rest } = snapshot;
280
- return {
612
+ const completion: ChatCompletion = {
281
613
  ...rest,
282
614
  id,
283
615
  choices: choices.map(
284
616
  ({ message, finish_reason, index, logprobs, ...choiceRest }): ChatCompletion.Choice => {
285
- if (!finish_reason) throw new OpenAIError(`missing finish_reason for choice ${index}`);
617
+ if (!finish_reason) {
618
+ throw new OpenAIError(`missing finish_reason for choice ${index}`);
619
+ }
620
+
286
621
  const { content = null, function_call, tool_calls, ...messageRest } = message;
287
622
  const role = message.role as 'assistant'; // this is what we expect; in theory it could be different which would make our types a slight lie but would be fine.
288
- if (!role) throw new OpenAIError(`missing role for choice ${index}`);
623
+ if (!role) {
624
+ throw new OpenAIError(`missing role for choice ${index}`);
625
+ }
626
+
289
627
  if (function_call) {
290
628
  const { arguments: args, name } = function_call;
291
- if (args == null) throw new OpenAIError(`missing function_call.arguments for choice ${index}`);
292
- if (!name) throw new OpenAIError(`missing function_call.name for choice ${index}`);
629
+ if (args == null) {
630
+ throw new OpenAIError(`missing function_call.arguments for choice ${index}`);
631
+ }
632
+
633
+ if (!name) {
634
+ throw new OpenAIError(`missing function_call.name for choice ${index}`);
635
+ }
636
+
293
637
  return {
294
638
  ...choiceRest,
295
- message: { content, function_call: { arguments: args, name }, role },
639
+ message: {
640
+ content,
641
+ function_call: { arguments: args, name },
642
+ role,
643
+ refusal: message.refusal ?? null,
644
+ },
296
645
  finish_reason,
297
646
  index,
298
647
  logprobs,
299
648
  };
300
649
  }
650
+
301
651
  if (tool_calls) {
302
652
  return {
303
653
  ...choiceRest,
@@ -308,21 +658,26 @@ function finalizeChatCompletion(snapshot: ChatCompletionSnapshot): ChatCompletio
308
658
  ...messageRest,
309
659
  role,
310
660
  content,
661
+ refusal: message.refusal ?? null,
311
662
  tool_calls: tool_calls.map((tool_call, i) => {
312
663
  const { function: fn, type, id, ...toolRest } = tool_call;
313
664
  const { arguments: args, name, ...fnRest } = fn || {};
314
- if (id == null)
665
+ if (id == null) {
315
666
  throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].id\n${str(snapshot)}`);
316
- if (type == null)
667
+ }
668
+ if (type == null) {
317
669
  throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].type\n${str(snapshot)}`);
318
- if (name == null)
670
+ }
671
+ if (name == null) {
319
672
  throw new OpenAIError(
320
673
  `missing choices[${index}].tool_calls[${i}].function.name\n${str(snapshot)}`,
321
674
  );
322
- if (args == null)
675
+ }
676
+ if (args == null) {
323
677
  throw new OpenAIError(
324
678
  `missing choices[${index}].tool_calls[${i}].function.arguments\n${str(snapshot)}`,
325
679
  );
680
+ }
326
681
 
327
682
  return { ...toolRest, id, type, function: { ...fnRest, name, arguments: args } };
328
683
  }),
@@ -331,7 +686,7 @@ function finalizeChatCompletion(snapshot: ChatCompletionSnapshot): ChatCompletio
331
686
  }
332
687
  return {
333
688
  ...choiceRest,
334
- message: { ...messageRest, content, role },
689
+ message: { ...messageRest, content, role, refusal: message.refusal ?? null },
335
690
  finish_reason,
336
691
  index,
337
692
  logprobs,
@@ -343,6 +698,8 @@ function finalizeChatCompletion(snapshot: ChatCompletionSnapshot): ChatCompletio
343
698
  object: 'chat.completion',
344
699
  ...(system_fingerprint ? { system_fingerprint } : {}),
345
700
  };
701
+
702
+ return maybeParseChatCompletion(completion, params);
346
703
  }
347
704
 
348
705
  function str(x: unknown) {
@@ -425,6 +782,10 @@ export namespace ChatCompletionSnapshot {
425
782
  */
426
783
  content?: string | null;
427
784
 
785
+ refusal?: string | null;
786
+
787
+ parsed?: unknown | null;
788
+
428
789
  /**
429
790
  * The name and arguments of a function that should be called, as generated by the
430
791
  * model.
@@ -444,14 +805,14 @@ export namespace ChatCompletionSnapshot {
444
805
  /**
445
806
  * The ID of the tool call.
446
807
  */
447
- id?: string;
808
+ id: string;
448
809
 
449
- function?: ToolCall.Function;
810
+ function: ToolCall.Function;
450
811
 
451
812
  /**
452
813
  * The type of the tool.
453
814
  */
454
- type?: 'function';
815
+ type: 'function';
455
816
  }
456
817
 
457
818
  export namespace ToolCall {
@@ -462,12 +823,14 @@ export namespace ChatCompletionSnapshot {
462
823
  * hallucinate parameters not defined by your function schema. Validate the
463
824
  * arguments in your code before calling your function.
464
825
  */
465
- arguments?: string;
826
+ arguments: string;
827
+
828
+ parsed_arguments?: unknown;
466
829
 
467
830
  /**
468
831
  * The name of the function to call.
469
832
  */
470
- name?: string;
833
+ name: string;
471
834
  }
472
835
  }
473
836
 
@@ -492,3 +855,16 @@ export namespace ChatCompletionSnapshot {
492
855
  }
493
856
  }
494
857
  }
858
+
859
+ type AssertIsEmpty<T extends {}> = keyof T extends never ? T : never;
860
+
861
+ /**
862
+ * Ensures the given argument is an empty object, useful for
863
+ * asserting that all known properties on an object have been
864
+ * destructured.
865
+ */
866
+ function assertIsEmpty<T extends {}>(obj: AssertIsEmpty<T>): asserts obj is AssertIsEmpty<T> {
867
+ return;
868
+ }
869
+
870
+ function assertNever(_x: never) {}