openai 7.25.0 → 7.27.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 (368) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/client.d.mts +5 -6
  3. package/client.d.mts.map +1 -1
  4. package/client.d.ts +5 -6
  5. package/client.d.ts.map +1 -1
  6. package/client.js +38 -10
  7. package/client.js.map +1 -1
  8. package/client.mjs +39 -11
  9. package/client.mjs.map +1 -1
  10. package/core/streaming.d.mts +4 -0
  11. package/core/streaming.d.mts.map +1 -1
  12. package/core/streaming.d.ts +4 -0
  13. package/core/streaming.d.ts.map +1 -1
  14. package/core/streaming.js +6 -0
  15. package/core/streaming.js.map +1 -1
  16. package/core/streaming.mjs +6 -0
  17. package/core/streaming.mjs.map +1 -1
  18. package/helpers/beta/agents/filesystem.d.mts +17 -0
  19. package/helpers/beta/agents/filesystem.d.mts.map +1 -0
  20. package/helpers/beta/agents/filesystem.d.ts +17 -0
  21. package/helpers/beta/agents/filesystem.d.ts.map +1 -0
  22. package/helpers/beta/agents/filesystem.js +168 -0
  23. package/helpers/beta/agents/filesystem.js.map +1 -0
  24. package/helpers/beta/agents/filesystem.mjs +162 -0
  25. package/helpers/beta/agents/filesystem.mjs.map +1 -0
  26. package/helpers/beta/agents/standard-schema.d.mts +7 -0
  27. package/helpers/beta/agents/standard-schema.d.mts.map +1 -0
  28. package/helpers/beta/agents/standard-schema.d.ts +7 -0
  29. package/helpers/beta/agents/standard-schema.d.ts.map +1 -0
  30. package/helpers/beta/agents/standard-schema.js +12 -0
  31. package/helpers/beta/agents/standard-schema.js.map +1 -0
  32. package/helpers/beta/agents/standard-schema.mjs +9 -0
  33. package/helpers/beta/agents/standard-schema.mjs.map +1 -0
  34. package/helpers/beta/agents/zod.d.mts +10 -0
  35. package/helpers/beta/agents/zod.d.mts.map +1 -0
  36. package/helpers/beta/agents/zod.d.ts +10 -0
  37. package/helpers/beta/agents/zod.d.ts.map +1 -0
  38. package/helpers/beta/agents/zod.js +12 -0
  39. package/helpers/beta/agents/zod.js.map +1 -0
  40. package/helpers/beta/agents/zod.mjs +9 -0
  41. package/helpers/beta/agents/zod.mjs.map +1 -0
  42. package/internal/realtime-credentials.d.mts +22 -1
  43. package/internal/realtime-credentials.d.mts.map +1 -1
  44. package/internal/realtime-credentials.d.ts +22 -1
  45. package/internal/realtime-credentials.d.ts.map +1 -1
  46. package/internal/realtime-credentials.js +48 -4
  47. package/internal/realtime-credentials.js.map +1 -1
  48. package/internal/realtime-credentials.mjs +45 -4
  49. package/internal/realtime-credentials.mjs.map +1 -1
  50. package/internal/responses-ws-credentials.d.mts +23 -0
  51. package/internal/responses-ws-credentials.d.mts.map +1 -0
  52. package/internal/responses-ws-credentials.d.ts +23 -0
  53. package/internal/responses-ws-credentials.d.ts.map +1 -0
  54. package/internal/responses-ws-credentials.js +108 -0
  55. package/internal/responses-ws-credentials.js.map +1 -0
  56. package/internal/responses-ws-credentials.mjs +104 -0
  57. package/internal/responses-ws-credentials.mjs.map +1 -0
  58. package/internal/shim-types.d.mts +9 -0
  59. package/internal/shim-types.d.mts.map +1 -1
  60. package/internal/shim-types.d.ts +9 -0
  61. package/internal/shim-types.d.ts.map +1 -1
  62. package/internal/tslib.js +4 -0
  63. package/internal/ws-adapter-node.d.mts +4 -0
  64. package/internal/ws-adapter-node.d.mts.map +1 -1
  65. package/internal/ws-adapter-node.d.ts +4 -0
  66. package/internal/ws-adapter-node.d.ts.map +1 -1
  67. package/internal/ws-adapter-node.js +56 -0
  68. package/internal/ws-adapter-node.js.map +1 -1
  69. package/internal/ws-adapter-node.mjs +55 -0
  70. package/internal/ws-adapter-node.mjs.map +1 -1
  71. package/internal/ws.d.mts +27 -4
  72. package/internal/ws.d.mts.map +1 -1
  73. package/internal/ws.d.ts +27 -4
  74. package/internal/ws.d.ts.map +1 -1
  75. package/internal/ws.js +105 -5
  76. package/internal/ws.js.map +1 -1
  77. package/internal/ws.mjs +102 -4
  78. package/internal/ws.mjs.map +1 -1
  79. package/lib/agents/agent-session-stream.d.mts +13 -4
  80. package/lib/agents/agent-session-stream.d.mts.map +1 -1
  81. package/lib/agents/agent-session-stream.d.ts +13 -4
  82. package/lib/agents/agent-session-stream.d.ts.map +1 -1
  83. package/lib/agents/agent-session-stream.js +24 -21
  84. package/lib/agents/agent-session-stream.js.map +1 -1
  85. package/lib/agents/agent-session-stream.mjs +24 -21
  86. package/lib/agents/agent-session-stream.mjs.map +1 -1
  87. package/lib/beta/agents/agent-session-create-stream.d.mts +15 -0
  88. package/lib/beta/agents/agent-session-create-stream.d.mts.map +1 -0
  89. package/lib/beta/agents/agent-session-create-stream.d.ts +15 -0
  90. package/lib/beta/agents/agent-session-create-stream.d.ts.map +1 -0
  91. package/lib/beta/agents/agent-session-create-stream.js +25 -0
  92. package/lib/beta/agents/agent-session-create-stream.js.map +1 -0
  93. package/lib/beta/agents/agent-session-create-stream.mjs +22 -0
  94. package/lib/beta/agents/agent-session-create-stream.mjs.map +1 -0
  95. package/lib/beta/agents/agent-turn-result-collector.d.mts +17 -0
  96. package/lib/beta/agents/agent-turn-result-collector.d.mts.map +1 -0
  97. package/lib/beta/agents/agent-turn-result-collector.d.ts +17 -0
  98. package/lib/beta/agents/agent-turn-result-collector.d.ts.map +1 -0
  99. package/lib/beta/agents/agent-turn-result-collector.js +99 -0
  100. package/lib/beta/agents/agent-turn-result-collector.js.map +1 -0
  101. package/lib/beta/agents/agent-turn-result-collector.mjs +95 -0
  102. package/lib/beta/agents/agent-turn-result-collector.mjs.map +1 -0
  103. package/lib/beta/agents/agent-turn-result-error.d.mts +16 -0
  104. package/lib/beta/agents/agent-turn-result-error.d.mts.map +1 -0
  105. package/lib/beta/agents/agent-turn-result-error.d.ts +16 -0
  106. package/lib/beta/agents/agent-turn-result-error.d.ts.map +1 -0
  107. package/lib/beta/agents/agent-turn-result-error.js +22 -0
  108. package/lib/beta/agents/agent-turn-result-error.js.map +1 -0
  109. package/lib/beta/agents/agent-turn-result-error.mjs +18 -0
  110. package/lib/beta/agents/agent-turn-result-error.mjs.map +1 -0
  111. package/lib/beta/agents/agent-turn-result.d.mts +12 -0
  112. package/lib/beta/agents/agent-turn-result.d.mts.map +1 -0
  113. package/lib/beta/agents/agent-turn-result.d.ts +12 -0
  114. package/lib/beta/agents/agent-turn-result.d.ts.map +1 -0
  115. package/lib/beta/agents/agent-turn-result.js +22 -0
  116. package/lib/beta/agents/agent-turn-result.js.map +1 -0
  117. package/lib/beta/agents/agent-turn-result.mjs +18 -0
  118. package/lib/beta/agents/agent-turn-result.mjs.map +1 -0
  119. package/lib/beta/agents/files.d.mts +33 -0
  120. package/lib/beta/agents/files.d.mts.map +1 -0
  121. package/lib/beta/agents/files.d.ts +33 -0
  122. package/lib/beta/agents/files.d.ts.map +1 -0
  123. package/lib/beta/agents/files.js +93 -0
  124. package/lib/beta/agents/files.js.map +1 -0
  125. package/lib/beta/agents/files.mjs +86 -0
  126. package/lib/beta/agents/files.mjs.map +1 -0
  127. package/lib/beta/agents/function-tool.d.mts +27 -0
  128. package/lib/beta/agents/function-tool.d.mts.map +1 -0
  129. package/lib/beta/agents/function-tool.d.ts +27 -0
  130. package/lib/beta/agents/function-tool.d.ts.map +1 -0
  131. package/lib/beta/agents/function-tool.js +48 -0
  132. package/lib/beta/agents/function-tool.js.map +1 -0
  133. package/lib/beta/agents/function-tool.mjs +45 -0
  134. package/lib/beta/agents/function-tool.mjs.map +1 -0
  135. package/lib/beta/agents/output-format-types.d.mts +11 -0
  136. package/lib/beta/agents/output-format-types.d.mts.map +1 -0
  137. package/lib/beta/agents/output-format-types.d.ts +11 -0
  138. package/lib/beta/agents/output-format-types.d.ts.map +1 -0
  139. package/lib/beta/agents/output-format-types.js +3 -0
  140. package/lib/beta/agents/output-format-types.js.map +1 -0
  141. package/lib/beta/agents/output-format-types.mjs +2 -0
  142. package/lib/beta/agents/output-format-types.mjs.map +1 -0
  143. package/lib/beta/agents/output-format.d.mts +8 -0
  144. package/lib/beta/agents/output-format.d.mts.map +1 -0
  145. package/lib/beta/agents/output-format.d.ts +8 -0
  146. package/lib/beta/agents/output-format.d.ts.map +1 -0
  147. package/lib/beta/agents/output-format.js +20 -0
  148. package/lib/beta/agents/output-format.js.map +1 -0
  149. package/lib/beta/agents/output-format.mjs +14 -0
  150. package/lib/beta/agents/output-format.mjs.map +1 -0
  151. package/lib/beta/agents/output-parse-error.d.mts +11 -0
  152. package/lib/beta/agents/output-parse-error.d.mts.map +1 -0
  153. package/lib/beta/agents/output-parse-error.d.ts +11 -0
  154. package/lib/beta/agents/output-parse-error.d.ts.map +1 -0
  155. package/lib/beta/agents/output-parse-error.js +22 -0
  156. package/lib/beta/agents/output-parse-error.js.map +1 -0
  157. package/lib/beta/agents/output-parse-error.mjs +18 -0
  158. package/lib/beta/agents/output-parse-error.mjs.map +1 -0
  159. package/lib/beta/agents/pages.d.mts +9 -0
  160. package/lib/beta/agents/pages.d.mts.map +1 -0
  161. package/lib/beta/agents/pages.d.ts +9 -0
  162. package/lib/beta/agents/pages.d.ts.map +1 -0
  163. package/lib/beta/agents/pages.js +32 -0
  164. package/lib/beta/agents/pages.js.map +1 -0
  165. package/lib/beta/agents/pages.mjs +29 -0
  166. package/lib/beta/agents/pages.mjs.map +1 -0
  167. package/lib/beta/agents/parse-result.d.mts +95 -0
  168. package/lib/beta/agents/parse-result.d.mts.map +1 -0
  169. package/lib/beta/agents/parse-result.d.ts +95 -0
  170. package/lib/beta/agents/parse-result.d.ts.map +1 -0
  171. package/lib/beta/agents/parse-result.js +116 -0
  172. package/lib/beta/agents/parse-result.js.map +1 -0
  173. package/lib/beta/agents/parse-result.mjs +110 -0
  174. package/lib/beta/agents/parse-result.mjs.map +1 -0
  175. package/lib/beta/agents/parsed-agent-turn-result.d.mts +8 -0
  176. package/lib/beta/agents/parsed-agent-turn-result.d.mts.map +1 -0
  177. package/lib/beta/agents/parsed-agent-turn-result.d.ts +8 -0
  178. package/lib/beta/agents/parsed-agent-turn-result.d.ts.map +1 -0
  179. package/lib/beta/agents/parsed-agent-turn-result.js +14 -0
  180. package/lib/beta/agents/parsed-agent-turn-result.js.map +1 -0
  181. package/lib/beta/agents/parsed-agent-turn-result.mjs +10 -0
  182. package/lib/beta/agents/parsed-agent-turn-result.mjs.map +1 -0
  183. package/lib/beta/agents/result-artifacts.d.mts +19 -0
  184. package/lib/beta/agents/result-artifacts.d.mts.map +1 -0
  185. package/lib/beta/agents/result-artifacts.d.ts +19 -0
  186. package/lib/beta/agents/result-artifacts.d.ts.map +1 -0
  187. package/lib/beta/agents/result-artifacts.js +67 -0
  188. package/lib/beta/agents/result-artifacts.js.map +1 -0
  189. package/lib/beta/agents/result-artifacts.mjs +63 -0
  190. package/lib/beta/agents/result-artifacts.mjs.map +1 -0
  191. package/lib/beta/agents/result-collection.d.mts +15 -0
  192. package/lib/beta/agents/result-collection.d.mts.map +1 -0
  193. package/lib/beta/agents/result-collection.d.ts +15 -0
  194. package/lib/beta/agents/result-collection.d.ts.map +1 -0
  195. package/lib/beta/agents/result-collection.js +110 -0
  196. package/lib/beta/agents/result-collection.js.map +1 -0
  197. package/lib/beta/agents/result-collection.mjs +106 -0
  198. package/lib/beta/agents/result-collection.mjs.map +1 -0
  199. package/lib/beta/agents/tool-output.d.mts +7 -0
  200. package/lib/beta/agents/tool-output.d.mts.map +1 -0
  201. package/lib/beta/agents/tool-output.d.ts +7 -0
  202. package/lib/beta/agents/tool-output.d.ts.map +1 -0
  203. package/lib/beta/agents/tool-output.js +26 -0
  204. package/lib/beta/agents/tool-output.js.map +1 -0
  205. package/lib/beta/agents/tool-output.mjs +23 -0
  206. package/lib/beta/agents/tool-output.mjs.map +1 -0
  207. package/lib/responses/responses-websocket-session.d.mts +1 -1
  208. package/lib/responses/responses-websocket-session.d.mts.map +1 -1
  209. package/lib/responses/responses-websocket-session.d.ts +1 -1
  210. package/lib/responses/responses-websocket-session.d.ts.map +1 -1
  211. package/lib/responses/responses-websocket-session.js +12 -2
  212. package/lib/responses/responses-websocket-session.js.map +1 -1
  213. package/lib/responses/responses-websocket-session.mjs +12 -2
  214. package/lib/responses/responses-websocket-session.mjs.map +1 -1
  215. package/package.json +1 -1
  216. package/resources/beta/agents/agents.d.mts +9 -0
  217. package/resources/beta/agents/agents.d.mts.map +1 -1
  218. package/resources/beta/agents/agents.d.ts +9 -0
  219. package/resources/beta/agents/agents.d.ts.map +1 -1
  220. package/resources/beta/agents/agents.js.map +1 -1
  221. package/resources/beta/agents/agents.mjs.map +1 -1
  222. package/resources/beta/agents/environments/files.d.mts +12 -0
  223. package/resources/beta/agents/environments/files.d.mts.map +1 -1
  224. package/resources/beta/agents/environments/files.d.ts +12 -0
  225. package/resources/beta/agents/environments/files.d.ts.map +1 -1
  226. package/resources/beta/agents/environments/files.js +9 -0
  227. package/resources/beta/agents/environments/files.js.map +1 -1
  228. package/resources/beta/agents/environments/files.mjs +9 -0
  229. package/resources/beta/agents/environments/files.mjs.map +1 -1
  230. package/resources/beta/agents/sessions/artifacts.d.mts +4 -0
  231. package/resources/beta/agents/sessions/artifacts.d.mts.map +1 -1
  232. package/resources/beta/agents/sessions/artifacts.d.ts +4 -0
  233. package/resources/beta/agents/sessions/artifacts.d.ts.map +1 -1
  234. package/resources/beta/agents/sessions/artifacts.js +5 -0
  235. package/resources/beta/agents/sessions/artifacts.js.map +1 -1
  236. package/resources/beta/agents/sessions/artifacts.mjs +5 -0
  237. package/resources/beta/agents/sessions/artifacts.mjs.map +1 -1
  238. package/resources/beta/agents/sessions/events.d.mts +6 -5
  239. package/resources/beta/agents/sessions/events.d.mts.map +1 -1
  240. package/resources/beta/agents/sessions/events.d.ts +6 -5
  241. package/resources/beta/agents/sessions/events.d.ts.map +1 -1
  242. package/resources/beta/agents/sessions/events.js +6 -5
  243. package/resources/beta/agents/sessions/events.js.map +1 -1
  244. package/resources/beta/agents/sessions/events.mjs +6 -5
  245. package/resources/beta/agents/sessions/events.mjs.map +1 -1
  246. package/resources/beta/agents/sessions/index.d.mts +1 -0
  247. package/resources/beta/agents/sessions/index.d.mts.map +1 -1
  248. package/resources/beta/agents/sessions/index.d.ts +1 -0
  249. package/resources/beta/agents/sessions/index.d.ts.map +1 -1
  250. package/resources/beta/agents/sessions/index.js +3 -1
  251. package/resources/beta/agents/sessions/index.js.map +1 -1
  252. package/resources/beta/agents/sessions/index.mjs +1 -0
  253. package/resources/beta/agents/sessions/index.mjs.map +1 -1
  254. package/resources/beta/agents/sessions/sessions.d.mts +16 -4
  255. package/resources/beta/agents/sessions/sessions.d.mts.map +1 -1
  256. package/resources/beta/agents/sessions/sessions.d.ts +16 -4
  257. package/resources/beta/agents/sessions/sessions.d.ts.map +1 -1
  258. package/resources/beta/agents/sessions/sessions.js +17 -4
  259. package/resources/beta/agents/sessions/sessions.js.map +1 -1
  260. package/resources/beta/agents/sessions/sessions.mjs +17 -4
  261. package/resources/beta/agents/sessions/sessions.mjs.map +1 -1
  262. package/resources/beta/agents/sessions/traces.d.mts +68 -0
  263. package/resources/beta/agents/sessions/traces.d.mts.map +1 -0
  264. package/resources/beta/agents/sessions/traces.d.ts +68 -0
  265. package/resources/beta/agents/sessions/traces.d.ts.map +1 -0
  266. package/resources/beta/agents/sessions/traces.js +81 -0
  267. package/resources/beta/agents/sessions/traces.js.map +1 -0
  268. package/resources/beta/agents/sessions/traces.mjs +77 -0
  269. package/resources/beta/agents/sessions/traces.mjs.map +1 -0
  270. package/resources/beta/responses/ws-base.d.mts +3 -1
  271. package/resources/beta/responses/ws-base.d.mts.map +1 -1
  272. package/resources/beta/responses/ws-base.d.ts +3 -1
  273. package/resources/beta/responses/ws-base.d.ts.map +1 -1
  274. package/resources/beta/responses/ws-base.js +35 -6
  275. package/resources/beta/responses/ws-base.js.map +1 -1
  276. package/resources/beta/responses/ws-base.mjs +35 -6
  277. package/resources/beta/responses/ws-base.mjs.map +1 -1
  278. package/resources/beta/responses/ws.d.mts +10 -0
  279. package/resources/beta/responses/ws.d.mts.map +1 -1
  280. package/resources/beta/responses/ws.d.ts +10 -0
  281. package/resources/beta/responses/ws.d.ts.map +1 -1
  282. package/resources/beta/responses/ws.js +23 -22
  283. package/resources/beta/responses/ws.js.map +1 -1
  284. package/resources/beta/responses/ws.mjs +24 -23
  285. package/resources/beta/responses/ws.mjs.map +1 -1
  286. package/resources/chat/completions/completions.d.mts +1 -1
  287. package/resources/chat/completions/completions.d.mts.map +1 -1
  288. package/resources/chat/completions/completions.d.ts +1 -1
  289. package/resources/chat/completions/completions.d.ts.map +1 -1
  290. package/resources/evals/runs/runs.js +1 -1
  291. package/resources/evals/runs/runs.js.map +1 -1
  292. package/resources/evals/runs/runs.mjs +1 -1
  293. package/resources/evals/runs/runs.mjs.map +1 -1
  294. package/resources/live/forks/forks.d.mts +5 -1
  295. package/resources/live/forks/forks.d.mts.map +1 -1
  296. package/resources/live/forks/forks.d.ts +5 -1
  297. package/resources/live/forks/forks.d.ts.map +1 -1
  298. package/resources/live/live.d.mts +5 -1
  299. package/resources/live/live.d.mts.map +1 -1
  300. package/resources/live/live.d.ts +5 -1
  301. package/resources/live/live.d.ts.map +1 -1
  302. package/resources/live/live.js.map +1 -1
  303. package/resources/live/live.mjs.map +1 -1
  304. package/resources/responses/ws-base.d.mts +3 -1
  305. package/resources/responses/ws-base.d.mts.map +1 -1
  306. package/resources/responses/ws-base.d.ts +3 -1
  307. package/resources/responses/ws-base.d.ts.map +1 -1
  308. package/resources/responses/ws-base.js +35 -6
  309. package/resources/responses/ws-base.js.map +1 -1
  310. package/resources/responses/ws-base.mjs +35 -6
  311. package/resources/responses/ws-base.mjs.map +1 -1
  312. package/resources/responses/ws.d.mts +10 -0
  313. package/resources/responses/ws.d.mts.map +1 -1
  314. package/resources/responses/ws.d.ts +10 -0
  315. package/resources/responses/ws.d.ts.map +1 -1
  316. package/resources/responses/ws.js +23 -22
  317. package/resources/responses/ws.js.map +1 -1
  318. package/resources/responses/ws.mjs +24 -23
  319. package/resources/responses/ws.mjs.map +1 -1
  320. package/src/client.ts +55 -19
  321. package/src/core/streaming.ts +9 -0
  322. package/src/helpers/beta/agents/filesystem.ts +187 -0
  323. package/src/helpers/beta/agents/standard-schema.ts +13 -0
  324. package/src/helpers/beta/agents/zod.ts +10 -0
  325. package/src/internal/realtime-credentials.ts +70 -4
  326. package/src/internal/responses-ws-credentials.ts +160 -0
  327. package/src/internal/shim-types.ts +17 -0
  328. package/src/internal/ws-adapter-node.ts +66 -0
  329. package/src/internal/ws.ts +139 -11
  330. package/src/lib/agents/agent-session-stream.ts +36 -24
  331. package/src/lib/beta/agents/agent-session-create-stream.ts +36 -0
  332. package/src/lib/beta/agents/agent-turn-result-collector.ts +122 -0
  333. package/src/lib/beta/agents/agent-turn-result-error.ts +33 -0
  334. package/src/lib/beta/agents/agent-turn-result.ts +22 -0
  335. package/src/lib/beta/agents/files.ts +119 -0
  336. package/src/lib/beta/agents/function-tool.ts +60 -0
  337. package/src/lib/beta/agents/output-format-types.ts +12 -0
  338. package/src/lib/beta/agents/output-format.ts +20 -0
  339. package/src/lib/beta/agents/output-parse-error.ts +16 -0
  340. package/src/lib/beta/agents/pages.ts +32 -0
  341. package/src/lib/beta/agents/parse-result.ts +133 -0
  342. package/src/lib/beta/agents/parsed-agent-turn-result.ts +12 -0
  343. package/src/lib/beta/agents/result-artifacts.ts +76 -0
  344. package/src/lib/beta/agents/result-collection.ts +115 -0
  345. package/src/lib/beta/agents/tool-output.ts +22 -0
  346. package/src/lib/responses/responses-websocket-session.ts +14 -3
  347. package/src/resources/beta/agents/agents.ts +11 -0
  348. package/src/resources/beta/agents/environments/files.ts +16 -0
  349. package/src/resources/beta/agents/sessions/artifacts.ts +7 -0
  350. package/src/resources/beta/agents/sessions/events.ts +6 -5
  351. package/src/resources/beta/agents/sessions/index.ts +1 -0
  352. package/src/resources/beta/agents/sessions/sessions.ts +48 -18
  353. package/src/resources/beta/agents/sessions/traces.ts +298 -0
  354. package/src/resources/beta/responses/ws-base.ts +40 -6
  355. package/src/resources/beta/responses/ws.ts +37 -27
  356. package/src/resources/chat/completions/completions.ts +1 -1
  357. package/src/resources/evals/runs/runs.ts +1 -1
  358. package/src/resources/live/forks/forks.ts +6 -1
  359. package/src/resources/live/live.ts +6 -1
  360. package/src/resources/responses/ws-base.ts +40 -6
  361. package/src/resources/responses/ws.ts +37 -27
  362. package/src/tsconfig.dist-src-node.d.ts +7 -0
  363. package/src/tsconfig.dist-src.d.ts +3 -1
  364. package/src/version.ts +1 -1
  365. package/version.d.mts +1 -1
  366. package/version.d.ts +1 -1
  367. package/version.js +1 -1
  368. package/version.mjs +1 -1
@@ -0,0 +1,160 @@
1
+ import { OpenAIError } from '../core/error';
2
+ import type { OpenAI } from '../client';
3
+ import { resolveRealtimeAPIKey } from './realtime-credentials';
4
+ import {
5
+ buildWebSocketOptions,
6
+ mergeWebSocketAuthHeaders,
7
+ snapshotWebSocketCredentials,
8
+ WEBSOCKET_METADATA_HEADERS,
9
+ } from './ws';
10
+ import type { CredentialedWebSocketOptions } from './ws';
11
+
12
+ /**
13
+ * Keep caller credential admission distinct from the hook's final provider-key transform.
14
+ * The latter reuses captured base headers so no caller getters are reread after the refresh.
15
+ */
16
+ function buildResponsesWebSocketOptions<Options extends CredentialedWebSocketOptions>(
17
+ client: OpenAI,
18
+ authHeaders: Record<string, string>,
19
+ options: Options | null | undefined,
20
+ prepared?: {
21
+ options: ReturnType<typeof buildWebSocketOptions>;
22
+ removedHeaders: Set<string>;
23
+ apiKey?: string | null | undefined;
24
+ },
25
+ ) {
26
+ if (prepared) {
27
+ return prepared.apiKey === undefined
28
+ ? mergeWebSocketAuthHeaders(prepared.options, authHeaders, prepared.removedHeaders)
29
+ : buildWebSocketOptions(client, authHeaders, prepared.options, prepared.removedHeaders, true);
30
+ }
31
+ if (!client._hasApiKeyProvider()) {
32
+ return buildWebSocketOptions(client, authHeaders, options);
33
+ }
34
+ const removedHeaders = new Set<string>();
35
+ const provided = buildWebSocketOptions(client, {}, options, removedHeaders);
36
+ return snapshotWebSocketCredentials(provided, WEBSOCKET_METADATA_HEADERS)
37
+ ? provided
38
+ : buildWebSocketOptions(client, authHeaders, provided, removedHeaders, true);
39
+ }
40
+
41
+ /**
42
+ * Classify reconnect credentials without treating the previous SDK key as a caller override.
43
+ * Capture routing and caller options before any asynchronous key refresh.
44
+ */
45
+ function prepareResponsesWebSocketReconnect<Options extends CredentialedWebSocketOptions>(
46
+ client: OpenAI,
47
+ socketOptions: Options | null | undefined,
48
+ initialAuthUsedAPIKey: boolean,
49
+ ) {
50
+ const removedHeaders = new Set<string>();
51
+ const options = buildWebSocketOptions(client, {}, socketOptions, removedHeaders);
52
+ let authentication: 'provided' | 'cached' | 'refresh';
53
+ if (snapshotWebSocketCredentials(options, WEBSOCKET_METADATA_HEADERS)) {
54
+ authentication = 'provided';
55
+ } else if (
56
+ removedHeaders.has('authorization') ||
57
+ options.headers?.['authorization'] !== undefined ||
58
+ !initialAuthUsedAPIKey
59
+ ) {
60
+ authentication = 'cached';
61
+ } else {
62
+ authentication = 'refresh';
63
+ }
64
+ return { options, removedHeaders, authentication };
65
+ }
66
+
67
+ /** Per-connection Responses credential state shared by the stable and beta Node transports. */
68
+ export class ResponsesWebSocketCredentials {
69
+ private initialAuthUsedAPIKey = false;
70
+ private initialAPIKey: string | undefined;
71
+ private prepared:
72
+ | {
73
+ options: ReturnType<typeof buildWebSocketOptions>;
74
+ removedHeaders: Set<string>;
75
+ apiKey?: string | null | undefined;
76
+ }
77
+ | undefined;
78
+
79
+ get preparedAPIKey(): string | null | undefined {
80
+ return this.prepared?.apiKey;
81
+ }
82
+
83
+ trackInitialHeaders(client: OpenAI, headers: Record<string, string>, hasSocket: () => boolean): void {
84
+ if (!hasSocket() && client._hasApiKeyProvider()) {
85
+ this.initialAPIKey = headers['Authorization']?.slice('Bearer '.length);
86
+ }
87
+ }
88
+
89
+ usesAPIKey(client: OpenAI, headers: Record<string, string>): boolean {
90
+ // Hooks can read the SDK key without delegating. Only use an ordinary cached property here:
91
+ // invoking custom/Bedrock getters could validate credentials a caller-owned socket never sends.
92
+ const apiKey =
93
+ this.initialAPIKey ??
94
+ (client._hasApiKeyProvider() ? Object.getOwnPropertyDescriptor(client, 'apiKey')?.value : undefined);
95
+ if (!apiKey) {
96
+ return false;
97
+ }
98
+ // Metadata containing a copy of a credential does not make it the socket's authentication.
99
+ return snapshotWebSocketCredentials(
100
+ {
101
+ headers: Object.fromEntries(
102
+ Object.entries(headers).map(([name, value]) => [name, value?.includes(apiKey) ? value : '']),
103
+ ),
104
+ },
105
+ WEBSOCKET_METADATA_HEADERS,
106
+ );
107
+ }
108
+
109
+ build<Options extends CredentialedWebSocketOptions>(
110
+ client: OpenAI,
111
+ authHeaders: Record<string, string>,
112
+ options: Options | null | undefined,
113
+ initialAuthUsedAPIKey?: boolean,
114
+ ) {
115
+ if (initialAuthUsedAPIKey !== undefined) {
116
+ this.initialAuthUsedAPIKey = initialAuthUsedAPIKey;
117
+ this.initialAPIKey = undefined;
118
+ }
119
+ const socketOptions = buildResponsesWebSocketOptions(client, authHeaders, options, this.prepared);
120
+ if (
121
+ client._hasApiKeyProvider() &&
122
+ !authHeaders['Authorization'] &&
123
+ !snapshotWebSocketCredentials(socketOptions, WEBSOCKET_METADATA_HEADERS)
124
+ ) {
125
+ throw new OpenAIError(
126
+ 'Cannot open a Responses WebSocket with an unresolved function-based apiKey. Resolve it before constructing the WebSocket or provide explicit WebSocket credentials.',
127
+ );
128
+ }
129
+ return socketOptions;
130
+ }
131
+
132
+ async prepare<Socket, Options extends CredentialedWebSocketOptions>(
133
+ client: OpenAI,
134
+ socketOptions: Options | null | undefined,
135
+ authHeaders: (apiKey?: string | null) => Record<string, string>,
136
+ createSocket: (url: URL, headers: Record<string, string>) => Socket,
137
+ ): Promise<(url: URL) => Socket> {
138
+ const { options, removedHeaders, authentication } = prepareResponsesWebSocketReconnect(
139
+ client,
140
+ socketOptions,
141
+ this.initialAuthUsedAPIKey,
142
+ );
143
+ const createPreparedSocket = (url: URL, headers: Record<string, string>, apiKey?: string | null) => {
144
+ const previous = this.prepared;
145
+ this.prepared = { options, removedHeaders, apiKey };
146
+ try {
147
+ // Resolve headers inside the scope so legacy zero-argument overrides see this attempt's key.
148
+ return createSocket(url, apiKey === undefined ? headers : authHeaders(apiKey));
149
+ } finally {
150
+ this.prepared = previous;
151
+ }
152
+ };
153
+ if (authentication !== 'refresh') {
154
+ const headers = authentication === 'provided' ? {} : authHeaders();
155
+ return (url) => createPreparedSocket(url, headers);
156
+ }
157
+ const { commit } = await resolveRealtimeAPIKey(client, true);
158
+ return (url) => createPreparedSocket(url, {}, commit());
159
+ }
160
+ }
@@ -23,3 +23,20 @@ type _ReadableStream<R = any> = NeverToAny<
23
23
  >;
24
24
 
25
25
  export type { _ReadableStream as ReadableStream };
26
+
27
+ /** @ts-ignore */
28
+ type _DOMWritableStream<R = any> = globalThis.WritableStream<R>;
29
+
30
+ /** @ts-ignore */
31
+ type _NodeWritableStream<R = any> = import('stream/web').WritableStream<R>;
32
+
33
+ type _ConditionalNodeWritableStream<R = any> = typeof globalThis extends { WritableStream: any }
34
+ ? never
35
+ : _NodeWritableStream<R>;
36
+
37
+ type _WritableStream<R = any> = NeverToAny<
38
+ | ([0] extends [1 & _DOMWritableStream<R>] ? never : _DOMWritableStream<R>)
39
+ | ([0] extends [1 & _ConditionalNodeWritableStream<R>] ? never : _ConditionalNodeWritableStream<R>)
40
+ >;
41
+
42
+ export type { _WritableStream as WritableStream };
@@ -1,10 +1,76 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
1
2
  import type * as WS from 'ws';
2
3
  import type { WebSocketLike } from './ws-adapter';
3
4
  import { protectWebSocketOptionsFromCredentialRedirects } from './ws';
5
+ import { setRealtimeAPIKeyCacheContext } from './realtime-credentials';
6
+ import type { DeferredAPIKeyCache } from './realtime-credentials';
7
+
8
+ setRealtimeAPIKeyCacheContext(new AsyncLocalStorage<DeferredAPIKeyCache | undefined>());
4
9
 
5
10
  /** A generic event listener callback. */
6
11
  type Listener = (...args: any[]) => void;
7
12
 
13
+ function copyTLSMaterial(value: string | Buffer): string | Buffer {
14
+ return ArrayBuffer.isView(value)
15
+ ? Buffer.from(new Uint8Array(value.buffer, value.byteOffset, value.byteLength))
16
+ : value;
17
+ }
18
+
19
+ /** Snapshot caller headers and TLS identity before an async reconnect credential refresh. */
20
+ export function snapshotNodeWebSocketOptions(
21
+ options: WS.ClientOptions | null | undefined,
22
+ ): Omit<WS.ClientOptions, 'headers'> & { headers: Record<string, string | string[]> } {
23
+ const captured = {
24
+ ...options,
25
+ headers: Object.fromEntries(
26
+ Object.entries(options?.headers ?? {}).map(([name, value]) => [
27
+ name,
28
+ Array.isArray(value) ? [...value] : value,
29
+ ]),
30
+ ),
31
+ };
32
+ const { ca, cert, crl, key, pfx } = captured;
33
+ if (ca !== undefined) {
34
+ captured.ca = Array.isArray(ca) ? ca.map(copyTLSMaterial) : copyTLSMaterial(ca);
35
+ }
36
+ if (cert !== undefined) {
37
+ captured.cert = Array.isArray(cert) ? cert.map(copyTLSMaterial) : copyTLSMaterial(cert);
38
+ }
39
+ if (crl !== undefined) {
40
+ captured.crl = Array.isArray(crl) ? crl.map(copyTLSMaterial) : copyTLSMaterial(crl);
41
+ }
42
+ if (key !== undefined) {
43
+ captured.key = Array.isArray(key)
44
+ ? key.map((value) =>
45
+ typeof value === 'string' || ArrayBuffer.isView(value)
46
+ ? copyTLSMaterial(value)
47
+ : { ...value, pem: copyTLSMaterial(value.pem) },
48
+ )
49
+ : copyTLSMaterial(key);
50
+ }
51
+ if (pfx !== undefined) {
52
+ captured.pfx = Array.isArray(pfx)
53
+ ? pfx.map((value) =>
54
+ typeof value === 'string' || ArrayBuffer.isView(value)
55
+ ? copyTLSMaterial(value)
56
+ : { ...value, buf: copyTLSMaterial(value.buf) },
57
+ )
58
+ : copyTLSMaterial(pfx);
59
+ }
60
+ if ('session' in captured && Buffer.isBuffer(captured.session)) {
61
+ captured.session = Buffer.from(captured.session);
62
+ }
63
+ if ('ALPNProtocols' in captured) {
64
+ if (Array.isArray(captured.ALPNProtocols)) {
65
+ captured.ALPNProtocols = [...captured.ALPNProtocols];
66
+ } else if (ArrayBuffer.isView(captured.ALPNProtocols)) {
67
+ const { buffer, byteOffset, byteLength } = captured.ALPNProtocols;
68
+ captured.ALPNProtocols = Buffer.from(new Uint8Array(buffer, byteOffset, byteLength));
69
+ }
70
+ }
71
+ return captured;
72
+ }
73
+
8
74
  /**
9
75
  * Adapts a Node.js `ws` socket to the SDK's platform-neutral WebSocket API.
10
76
  *
@@ -1,6 +1,7 @@
1
1
  import { concatBytes, encodeUTF8 } from './utils/bytes';
2
2
  import { OpenAIError } from '../core/error';
3
3
  import type { WebSocketLike } from './ws-adapter';
4
+ import type { OpenAI } from '../client';
4
5
 
5
6
  const webSocketErrors = new WeakMap<WebSocketLike, Error>();
6
7
 
@@ -73,7 +74,7 @@ export type ReconnectingOverrides<Parameters = Record<string, unknown>> =
73
74
  */
74
75
  export type RawWebSocketData = string | ArrayBufferLike | ArrayBufferView | ArrayBufferView[];
75
76
 
76
- interface CredentialedWebSocketOptions {
77
+ export interface CredentialedWebSocketOptions {
77
78
  auth?: string | null | undefined;
78
79
  followRedirects?: boolean | undefined;
79
80
  headers?: object | null | undefined;
@@ -99,19 +100,57 @@ const REDIRECT_SAFE_WEBSOCKET_HEADERS = new Set([
99
100
  'x-trace-id',
100
101
  ]);
101
102
 
103
+ const WEBSOCKET_METADATA_HEADER_NAMES = new Set([
104
+ 'accept',
105
+ 'accept-encoding',
106
+ 'accept-language',
107
+ 'content-type',
108
+ 'x-request-id',
109
+ 'x-client-request-id',
110
+ 'x-correlation-id',
111
+ 'traceparent',
112
+ 'tracestate',
113
+ 'sentry-trace',
114
+ 'x-amzn-trace-id',
115
+ 'x-cloud-trace-context',
116
+ 'x-datadog-trace-id',
117
+ 'x-datadog-parent-id',
118
+ 'x-datadog-sampling-priority',
119
+ 'x-datadog-origin',
120
+ 'x-datadog-tags',
121
+ 'baggage',
122
+ 'b3',
123
+ ]);
124
+
125
+ // Request and tracing metadata do not authenticate a Responses socket, but remain protected on redirects.
126
+ export const WEBSOCKET_METADATA_HEADERS = {
127
+ has: (name: string, values: readonly unknown[]): boolean =>
128
+ WEBSOCKET_METADATA_HEADER_NAMES.has(name) ||
129
+ name.startsWith('x-b3-') ||
130
+ (name === 'sec-websocket-protocol' &&
131
+ !values.some(
132
+ (value) =>
133
+ typeof value === 'string' &&
134
+ value.split(',').some((protocol) => protocol.trim().startsWith('openai-insecure-api-key.')),
135
+ )),
136
+ };
137
+
102
138
  function isWebSocketCredentialHeader(name: string): boolean {
103
139
  return !REDIRECT_SAFE_WEBSOCKET_HEADERS.has(name.toLowerCase().split('_').join('-'));
104
140
  }
105
141
 
106
142
  /**
107
143
  * Snapshots credential values in final socket options before validation and dispatch.
108
- * Reports potential caller authentication, including custom headers; the server
109
- * remains responsible for validating credentials. Noncredential headers are left intact.
144
+ * Reports potential caller authentication, excluding additional metadata when requested.
145
+ * The server remains responsible for validating credentials. Noncredential headers are left intact.
110
146
  */
111
- export function snapshotWebSocketCredentials(options: {
112
- auth?: unknown;
113
- headers?: Record<string, unknown> | undefined;
114
- }): boolean {
147
+ export function snapshotWebSocketCredentials(
148
+ options: {
149
+ auth?: unknown;
150
+ headers?: Record<string, unknown> | undefined;
151
+ },
152
+ metadataHeaders?: { has: (name: string, values: readonly unknown[]) => boolean },
153
+ ): boolean {
115
154
  if (options.auth !== null && options.auth !== undefined) {
116
155
  options.auth = String(options.auth);
117
156
  }
@@ -135,10 +174,12 @@ export function snapshotWebSocketCredentials(options: {
135
174
  }
136
175
  headers[name] = snapshot;
137
176
  const values = Array.isArray(snapshot) ? snapshot : [snapshot];
138
- credentials.set(
139
- name.toLowerCase(),
140
- values.some((item) => typeof item === 'string' && item.trim().length > 0),
141
- );
177
+ if (!metadataHeaders?.has(normalizedName, values)) {
178
+ credentials.set(
179
+ name.toLowerCase(),
180
+ values.some((item) => typeof item === 'string' && item.trim().length > 0),
181
+ );
182
+ }
142
183
  }
143
184
  // Node applies header names case-insensitively, and Authorization overrides Basic auth.
144
185
  return (
@@ -147,6 +188,93 @@ export function snapshotWebSocketCredentials(options: {
147
188
  );
148
189
  }
149
190
 
191
+ /** Merge transport authentication before explicit caller overrides and header removals. */
192
+ export function mergeWebSocketAuthHeaders<Options extends CredentialedWebSocketOptions>(
193
+ options: Options,
194
+ authHeaders: Record<string, string>,
195
+ removedHeaders: ReadonlySet<string>,
196
+ ) {
197
+ const headers = new Map(Object.entries(authHeaders).map(([name, value]) => [name.toLowerCase(), value]));
198
+ for (const name of removedHeaders) {
199
+ headers.delete(name);
200
+ }
201
+ for (const [name, value] of Object.entries(options.headers ?? {})) {
202
+ headers.set(name, value);
203
+ }
204
+ return { ...options, headers: Object.fromEntries(headers) };
205
+ }
206
+
207
+ /** Removal sink for one synchronous header-hook call, including legacy one-argument overrides. */
208
+ export const webSocketHeaderRemovals = new WeakMap<
209
+ OpenAI,
210
+ { removedHeaders: Set<string> | undefined; baseHeaders: Record<string, string> | undefined }
211
+ >();
212
+ const preparedWebSocketHeaders = new WeakMap<
213
+ object,
214
+ { baseHeaders: Record<string, string> | undefined; transportHeaders: Map<string, boolean> }
215
+ >();
216
+
217
+ /** Build a WebSocket handshake with explicit caller headers applied after the client defaults. */
218
+ export function buildWebSocketOptions<Options extends Pick<CredentialedWebSocketOptions, 'headers'>>(
219
+ client: OpenAI,
220
+ authHeaders: Record<string, string>,
221
+ options: Options | null | undefined,
222
+ removedHeaders?: Set<string>,
223
+ usePreparedHeaders = false,
224
+ ) {
225
+ // Capture the base result even through older overrides that forward only one argument.
226
+ const previous = webSocketHeaderRemovals.get(client);
227
+ const prepared = usePreparedHeaders && options ? preparedWebSocketHeaders.get(options) : undefined;
228
+ const context: NonNullable<ReturnType<typeof webSocketHeaderRemovals.get>> = {
229
+ removedHeaders,
230
+ baseHeaders: prepared?.baseHeaders,
231
+ };
232
+ webSocketHeaderRemovals.set(client, context);
233
+ let headers: Map<string, string>;
234
+ try {
235
+ headers = new Map(
236
+ Object.entries(client._buildWebSocketHeaders(authHeaders, removedHeaders)).map(([name, value]) => [
237
+ name.toLowerCase(),
238
+ value,
239
+ ]),
240
+ );
241
+ } finally {
242
+ if (previous) {
243
+ webSocketHeaderRemovals.set(client, previous);
244
+ } else {
245
+ webSocketHeaderRemovals.delete(client);
246
+ }
247
+ }
248
+ // The first hook output is not a caller override of the final, credentialed hook.
249
+ // Save transport names and explicit nulls separately, then reuse their validated values.
250
+ const transportHeaders = new Map<string, boolean>();
251
+ const currentHeaders = new Map(Object.entries(options?.headers ?? {}));
252
+ const overrides = prepared
253
+ ? [...prepared.transportHeaders].map(
254
+ ([name, removed]) => [name, removed ? null : currentHeaders.get(name)] as const,
255
+ )
256
+ : currentHeaders;
257
+ for (const [name, value] of overrides) {
258
+ const normalizedName = name.toLowerCase();
259
+ if (value === null) {
260
+ headers.delete(normalizedName);
261
+ removedHeaders?.add(normalizedName);
262
+ transportHeaders.set(normalizedName, true);
263
+ } else if (value !== undefined) {
264
+ headers.set(normalizedName, value);
265
+ removedHeaders?.delete(normalizedName);
266
+ transportHeaders.set(normalizedName, false);
267
+ }
268
+ }
269
+ const result = {
270
+ ...options,
271
+ headers: Object.fromEntries(headers),
272
+ followRedirects: false,
273
+ };
274
+ preparedWebSocketHeaders.set(result, { baseHeaders: context.baseHeaders, transportHeaders });
275
+ return result;
276
+ }
277
+
150
278
  /** Prevents WebSocket redirects from forwarding caller or SDK credentials to another origin. */
151
279
  export function protectWebSocketOptionsFromCredentialRedirects<Options extends CredentialedWebSocketOptions>(
152
280
  options: Options,
@@ -1,17 +1,20 @@
1
+ import type { AgentOutputFormat, AgentResult } from '../beta/agents/output-format-types';
1
2
  import { TurnState } from './turn-state';
3
+ import { agentFormatParser, parseAgentResultPromise } from '../beta/agents/parse-result';
4
+ import { ResultCollection } from '../beta/agents/result-collection';
2
5
  import { APIUserAbortError, BadRequestError, OpenAIError } from '../../core/error';
3
6
  import type { Stream } from '../../core/streaming';
4
7
  import { buildHeaders } from '../../internal/headers';
5
8
  import type { RequestOptions } from '../../internal/request-options';
6
9
  import { uuid4 } from '../../internal/utils/uuid';
7
- import { hasOwn, isObj } from '../../internal/utils/values';
10
+ import { isObj } from '../../internal/utils/values';
11
+ import { isInputContent } from '../beta/agents/tool-output';
8
12
  import type {
9
13
  AgentFunctionCallItem,
10
14
  AgentFunctionCallOutputParam,
11
15
  AgentSessionEvent,
12
16
  AgentSessionInputMessageParam,
13
17
  AgentSessionInputParam,
14
- InputContentParam,
15
18
  } from '../../resources/beta/agents/agents';
16
19
  import type { Sessions } from '../../resources/beta/agents/sessions/sessions';
17
20
 
@@ -23,33 +26,19 @@ export type AgentToolHandler = (
23
26
  ) => AgentToolOutput | PromiseLike<AgentToolOutput>;
24
27
 
25
28
  /** Input and optional sequential tool handlers for one turn on an idle session. */
26
- export interface AgentSessionStreamParams {
29
+ export type AgentSessionStreamParams<T = never> = {
30
+ /** Beta: parse this turn locally; does not change the existing session schema. */
31
+ outputFormat?: AgentOutputFormat<T>;
27
32
  /** User messages, or text normalized to a single user message. Must not be empty. */
28
33
  input: string | AgentSessionInputMessageParam[];
29
34
  /** Registered functions run after their call event is yielded; unknown functions remain manual. */
30
35
  toolHandlers?: Record<string, AgentToolHandler>;
31
36
  /** Key for the input submission only; request headers take precedence, case-insensitively. */
32
37
  idempotencyKey?: string;
33
- }
38
+ } & ([T] extends [never] ? unknown : { outputFormat: AgentOutputFormat<T> });
34
39
 
35
40
  type ToolResult = AgentSessionInputParam.SessionInputParamAgentSessionInputToolResult;
36
41
 
37
- function isInputContent(value: unknown): value is InputContentParam {
38
- if (!isObj(value)) {
39
- return false;
40
- }
41
- const content = value;
42
- let field: string;
43
- if (content['type'] === 'input_text') {
44
- field = 'text';
45
- } else if (content['type'] === 'input_image') {
46
- field = 'image_url';
47
- } else {
48
- return false;
49
- }
50
- return hasOwn(content, 'type') && hasOwn(content, field) && typeof content[field] === 'string';
51
- }
52
-
53
42
  function normalizedOutput(value: unknown): AgentFunctionCallOutputParam | null {
54
43
  if (value === null) {
55
44
  return null;
@@ -98,10 +87,13 @@ async function cancelBody(response: Response | undefined): Promise<void> {
98
87
  * idempotency key. Breaking iteration or calling abort closes local requests;
99
88
  * neither cancels the backend turn. No work starts until iteration begins.
100
89
  */
101
- export class AgentSessionStream implements AsyncIterable<AgentSessionEvent> {
90
+ export class AgentSessionStream<T = never> implements AsyncIterable<AgentSessionEvent> {
102
91
  /** Aborts local requests and iteration without cancelling the backend turn. */
103
92
  readonly controller = new AbortController();
104
93
  #consumed = false;
94
+ #format: AgentOutputFormat<T> | undefined;
95
+ #parsedResult: Promise<AgentResult<T>> | undefined;
96
+ #collection: ResultCollection;
105
97
  #stream: Stream<AgentSessionEvent> | undefined;
106
98
  #response: Response | undefined;
107
99
  #reading = false;
@@ -116,7 +108,7 @@ export class AgentSessionStream implements AsyncIterable<AgentSessionEvent> {
116
108
  constructor(
117
109
  sessions: Sessions,
118
110
  sessionID: string,
119
- params: AgentSessionStreamParams,
111
+ params: AgentSessionStreamParams<T>,
120
112
  options?: RequestOptions,
121
113
  ) {
122
114
  const input: AgentSessionInputMessageParam[] =
@@ -126,6 +118,10 @@ export class AgentSessionStream implements AsyncIterable<AgentSessionEvent> {
126
118
  if (params.input.length === 0) {
127
119
  throw new OpenAIError('input must not be empty');
128
120
  }
121
+ this.#format = agentFormatParser<T>(params.outputFormat);
122
+ if (params.outputFormat && !this.#format) {
123
+ throw new OpenAIError('outputFormat must have its own parser function');
124
+ }
129
125
  this.#sessions = sessions;
130
126
  this.#sessionID = sessionID;
131
127
  this.#input = { type: 'agent.session.input.message', input };
@@ -141,6 +137,11 @@ export class AgentSessionStream implements AsyncIterable<AgentSessionEvent> {
141
137
  headers.nulls.delete('idempotency-key');
142
138
  const { idempotencyKey: _key, ...rest } = options ?? {};
143
139
  this.#options = { ...rest, headers };
140
+ this.#collection = new ResultCollection(
141
+ () => this.#iterate(),
142
+ (name) => this.#handlers.has(name),
143
+ sessionID,
144
+ );
144
145
  }
145
146
 
146
147
  /** Closes local requests without cancelling the turn; an optional reason becomes the abort error's cause. */
@@ -158,7 +159,18 @@ export class AgentSessionStream implements AsyncIterable<AgentSessionEvent> {
158
159
  throw new OpenAIError('An AgentSessionStream can only be consumed once');
159
160
  }
160
161
  this.#consumed = true;
161
- return this.#iterate();
162
+ return this.#collection.iterate();
163
+ }
164
+
165
+ /** Beta: opt into retaining completed final messages before iterating progress events. */
166
+ withResultCollection(): this {
167
+ this.#collection.enable();
168
+ return this;
169
+ }
170
+
171
+ /** Beta: drain this turn, dispatch registered tools, and collect its final assistant messages. */
172
+ finalResult(): Promise<AgentResult<T>> {
173
+ return (this.#parsedResult ??= parseAgentResultPromise(this.#collection.finalResult(), this.#format));
162
174
  }
163
175
 
164
176
  async *#iterate(): AsyncGenerator<AgentSessionEvent> {
@@ -268,7 +280,7 @@ export class AgentSessionStream implements AsyncIterable<AgentSessionEvent> {
268
280
  return error;
269
281
  }
270
282
 
271
- async #wait<T>(action: () => T | PromiseLike<T>): Promise<T> {
283
+ async #wait<Value>(action: () => Value | PromiseLike<Value>): Promise<Value> {
272
284
  let onAbort: (() => void) | undefined;
273
285
  // oxlint-disable-next-line promise/avoid-new -- Bridge the caller's AbortSignal while a handler or registration delay is pending.
274
286
  const aborted = new Promise<never>((_resolve, reject) => {
@@ -0,0 +1,36 @@
1
+ import type { AgentOutputFormat, AgentResult } from './output-format-types';
2
+ import type { Stream } from '../../../core/streaming';
3
+ import type { AgentSessionEvent } from '../../../resources/beta/agents/agents';
4
+ import { parseAgentResultPromise } from './parse-result';
5
+ import { ResultCollection } from './result-collection';
6
+
7
+ /** Beta: the original creation stream with optional collection of its initial root turn. */
8
+ export type AgentSessionCreateStream<T = never> = Stream<AgentSessionEvent> & {
9
+ /** Drain through the selected turn's completion and idle event, then return its final messages. */
10
+ finalResult: () => Promise<AgentResult<T>>;
11
+ /** Opt into retaining completed final messages before iterating progress events. */
12
+ withResultCollection: () => AgentSessionCreateStream<T>;
13
+ };
14
+
15
+ /** Add beta result collection without replacing custom stream instances.
16
+ * @internal
17
+ */
18
+ export function withAgentTurnResult<T = never>(
19
+ stream: Stream<AgentSessionEvent>,
20
+ format?: AgentOutputFormat<T>,
21
+ ): AgentSessionCreateStream<T> {
22
+ let collection: ResultCollection;
23
+ stream.__betaTransformIterator((source) => {
24
+ collection = new ResultCollection(source, undefined, undefined, stream.controller.signal);
25
+ return () => collection.iterate();
26
+ });
27
+ let parsed: Promise<AgentResult<T>> | undefined;
28
+ const result: AgentSessionCreateStream<T> = Object.assign(stream, {
29
+ finalResult: () => (parsed ??= parseAgentResultPromise(collection.finalResult(), format)),
30
+ withResultCollection: (): AgentSessionCreateStream<T> => {
31
+ collection.enable();
32
+ return result;
33
+ },
34
+ });
35
+ return result;
36
+ }