openai 7.24.0 → 7.26.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 (339) hide show
  1. package/CHANGELOG.md +38 -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/standard-schema.d.mts +7 -0
  19. package/helpers/beta/agents/standard-schema.d.mts.map +1 -0
  20. package/helpers/beta/agents/standard-schema.d.ts +7 -0
  21. package/helpers/beta/agents/standard-schema.d.ts.map +1 -0
  22. package/helpers/beta/agents/standard-schema.js +12 -0
  23. package/helpers/beta/agents/standard-schema.js.map +1 -0
  24. package/helpers/beta/agents/standard-schema.mjs +9 -0
  25. package/helpers/beta/agents/standard-schema.mjs.map +1 -0
  26. package/helpers/beta/agents/zod.d.mts +10 -0
  27. package/helpers/beta/agents/zod.d.mts.map +1 -0
  28. package/helpers/beta/agents/zod.d.ts +10 -0
  29. package/helpers/beta/agents/zod.d.ts.map +1 -0
  30. package/helpers/beta/agents/zod.js +12 -0
  31. package/helpers/beta/agents/zod.js.map +1 -0
  32. package/helpers/beta/agents/zod.mjs +9 -0
  33. package/helpers/beta/agents/zod.mjs.map +1 -0
  34. package/internal/realtime-credentials.d.mts +22 -1
  35. package/internal/realtime-credentials.d.mts.map +1 -1
  36. package/internal/realtime-credentials.d.ts +22 -1
  37. package/internal/realtime-credentials.d.ts.map +1 -1
  38. package/internal/realtime-credentials.js +48 -4
  39. package/internal/realtime-credentials.js.map +1 -1
  40. package/internal/realtime-credentials.mjs +45 -4
  41. package/internal/realtime-credentials.mjs.map +1 -1
  42. package/internal/responses-ws-credentials.d.mts +23 -0
  43. package/internal/responses-ws-credentials.d.mts.map +1 -0
  44. package/internal/responses-ws-credentials.d.ts +23 -0
  45. package/internal/responses-ws-credentials.d.ts.map +1 -0
  46. package/internal/responses-ws-credentials.js +108 -0
  47. package/internal/responses-ws-credentials.js.map +1 -0
  48. package/internal/responses-ws-credentials.mjs +104 -0
  49. package/internal/responses-ws-credentials.mjs.map +1 -0
  50. package/internal/ws-adapter-node.d.mts +4 -0
  51. package/internal/ws-adapter-node.d.mts.map +1 -1
  52. package/internal/ws-adapter-node.d.ts +4 -0
  53. package/internal/ws-adapter-node.d.ts.map +1 -1
  54. package/internal/ws-adapter-node.js +56 -0
  55. package/internal/ws-adapter-node.js.map +1 -1
  56. package/internal/ws-adapter-node.mjs +55 -0
  57. package/internal/ws-adapter-node.mjs.map +1 -1
  58. package/internal/ws.d.mts +27 -4
  59. package/internal/ws.d.mts.map +1 -1
  60. package/internal/ws.d.ts +27 -4
  61. package/internal/ws.d.ts.map +1 -1
  62. package/internal/ws.js +105 -5
  63. package/internal/ws.js.map +1 -1
  64. package/internal/ws.mjs +102 -4
  65. package/internal/ws.mjs.map +1 -1
  66. package/lib/agents/agent-session-stream.d.mts +13 -4
  67. package/lib/agents/agent-session-stream.d.mts.map +1 -1
  68. package/lib/agents/agent-session-stream.d.ts +13 -4
  69. package/lib/agents/agent-session-stream.d.ts.map +1 -1
  70. package/lib/agents/agent-session-stream.js +24 -21
  71. package/lib/agents/agent-session-stream.js.map +1 -1
  72. package/lib/agents/agent-session-stream.mjs +24 -21
  73. package/lib/agents/agent-session-stream.mjs.map +1 -1
  74. package/lib/beta/agents/agent-session-create-stream.d.mts +15 -0
  75. package/lib/beta/agents/agent-session-create-stream.d.mts.map +1 -0
  76. package/lib/beta/agents/agent-session-create-stream.d.ts +15 -0
  77. package/lib/beta/agents/agent-session-create-stream.d.ts.map +1 -0
  78. package/lib/beta/agents/agent-session-create-stream.js +25 -0
  79. package/lib/beta/agents/agent-session-create-stream.js.map +1 -0
  80. package/lib/beta/agents/agent-session-create-stream.mjs +22 -0
  81. package/lib/beta/agents/agent-session-create-stream.mjs.map +1 -0
  82. package/lib/beta/agents/agent-turn-result-collector.d.mts +17 -0
  83. package/lib/beta/agents/agent-turn-result-collector.d.mts.map +1 -0
  84. package/lib/beta/agents/agent-turn-result-collector.d.ts +17 -0
  85. package/lib/beta/agents/agent-turn-result-collector.d.ts.map +1 -0
  86. package/lib/beta/agents/agent-turn-result-collector.js +99 -0
  87. package/lib/beta/agents/agent-turn-result-collector.js.map +1 -0
  88. package/lib/beta/agents/agent-turn-result-collector.mjs +95 -0
  89. package/lib/beta/agents/agent-turn-result-collector.mjs.map +1 -0
  90. package/lib/beta/agents/agent-turn-result-error.d.mts +16 -0
  91. package/lib/beta/agents/agent-turn-result-error.d.mts.map +1 -0
  92. package/lib/beta/agents/agent-turn-result-error.d.ts +16 -0
  93. package/lib/beta/agents/agent-turn-result-error.d.ts.map +1 -0
  94. package/lib/beta/agents/agent-turn-result-error.js +22 -0
  95. package/lib/beta/agents/agent-turn-result-error.js.map +1 -0
  96. package/lib/beta/agents/agent-turn-result-error.mjs +18 -0
  97. package/lib/beta/agents/agent-turn-result-error.mjs.map +1 -0
  98. package/lib/beta/agents/agent-turn-result.d.mts +12 -0
  99. package/lib/beta/agents/agent-turn-result.d.mts.map +1 -0
  100. package/lib/beta/agents/agent-turn-result.d.ts +12 -0
  101. package/lib/beta/agents/agent-turn-result.d.ts.map +1 -0
  102. package/lib/beta/agents/agent-turn-result.js +22 -0
  103. package/lib/beta/agents/agent-turn-result.js.map +1 -0
  104. package/lib/beta/agents/agent-turn-result.mjs +18 -0
  105. package/lib/beta/agents/agent-turn-result.mjs.map +1 -0
  106. package/lib/beta/agents/function-tool.d.mts +27 -0
  107. package/lib/beta/agents/function-tool.d.mts.map +1 -0
  108. package/lib/beta/agents/function-tool.d.ts +27 -0
  109. package/lib/beta/agents/function-tool.d.ts.map +1 -0
  110. package/lib/beta/agents/function-tool.js +48 -0
  111. package/lib/beta/agents/function-tool.js.map +1 -0
  112. package/lib/beta/agents/function-tool.mjs +45 -0
  113. package/lib/beta/agents/function-tool.mjs.map +1 -0
  114. package/lib/beta/agents/output-format-types.d.mts +11 -0
  115. package/lib/beta/agents/output-format-types.d.mts.map +1 -0
  116. package/lib/beta/agents/output-format-types.d.ts +11 -0
  117. package/lib/beta/agents/output-format-types.d.ts.map +1 -0
  118. package/lib/beta/agents/output-format-types.js +3 -0
  119. package/lib/beta/agents/output-format-types.js.map +1 -0
  120. package/lib/beta/agents/output-format-types.mjs +2 -0
  121. package/lib/beta/agents/output-format-types.mjs.map +1 -0
  122. package/lib/beta/agents/output-format.d.mts +8 -0
  123. package/lib/beta/agents/output-format.d.mts.map +1 -0
  124. package/lib/beta/agents/output-format.d.ts +8 -0
  125. package/lib/beta/agents/output-format.d.ts.map +1 -0
  126. package/lib/beta/agents/output-format.js +20 -0
  127. package/lib/beta/agents/output-format.js.map +1 -0
  128. package/lib/beta/agents/output-format.mjs +14 -0
  129. package/lib/beta/agents/output-format.mjs.map +1 -0
  130. package/lib/beta/agents/output-parse-error.d.mts +11 -0
  131. package/lib/beta/agents/output-parse-error.d.mts.map +1 -0
  132. package/lib/beta/agents/output-parse-error.d.ts +11 -0
  133. package/lib/beta/agents/output-parse-error.d.ts.map +1 -0
  134. package/lib/beta/agents/output-parse-error.js +22 -0
  135. package/lib/beta/agents/output-parse-error.js.map +1 -0
  136. package/lib/beta/agents/output-parse-error.mjs +18 -0
  137. package/lib/beta/agents/output-parse-error.mjs.map +1 -0
  138. package/lib/beta/agents/parse-result.d.mts +95 -0
  139. package/lib/beta/agents/parse-result.d.mts.map +1 -0
  140. package/lib/beta/agents/parse-result.d.ts +95 -0
  141. package/lib/beta/agents/parse-result.d.ts.map +1 -0
  142. package/lib/beta/agents/parse-result.js +116 -0
  143. package/lib/beta/agents/parse-result.js.map +1 -0
  144. package/lib/beta/agents/parse-result.mjs +110 -0
  145. package/lib/beta/agents/parse-result.mjs.map +1 -0
  146. package/lib/beta/agents/parsed-agent-turn-result.d.mts +8 -0
  147. package/lib/beta/agents/parsed-agent-turn-result.d.mts.map +1 -0
  148. package/lib/beta/agents/parsed-agent-turn-result.d.ts +8 -0
  149. package/lib/beta/agents/parsed-agent-turn-result.d.ts.map +1 -0
  150. package/lib/beta/agents/parsed-agent-turn-result.js +14 -0
  151. package/lib/beta/agents/parsed-agent-turn-result.js.map +1 -0
  152. package/lib/beta/agents/parsed-agent-turn-result.mjs +10 -0
  153. package/lib/beta/agents/parsed-agent-turn-result.mjs.map +1 -0
  154. package/lib/beta/agents/result-collection.d.mts +15 -0
  155. package/lib/beta/agents/result-collection.d.mts.map +1 -0
  156. package/lib/beta/agents/result-collection.d.ts +15 -0
  157. package/lib/beta/agents/result-collection.d.ts.map +1 -0
  158. package/lib/beta/agents/result-collection.js +110 -0
  159. package/lib/beta/agents/result-collection.js.map +1 -0
  160. package/lib/beta/agents/result-collection.mjs +106 -0
  161. package/lib/beta/agents/result-collection.mjs.map +1 -0
  162. package/lib/beta/agents/tool-output.d.mts +7 -0
  163. package/lib/beta/agents/tool-output.d.mts.map +1 -0
  164. package/lib/beta/agents/tool-output.d.ts +7 -0
  165. package/lib/beta/agents/tool-output.d.ts.map +1 -0
  166. package/lib/beta/agents/tool-output.js +26 -0
  167. package/lib/beta/agents/tool-output.js.map +1 -0
  168. package/lib/beta/agents/tool-output.mjs +23 -0
  169. package/lib/beta/agents/tool-output.mjs.map +1 -0
  170. package/lib/responses/responses-websocket-session.d.mts +1 -1
  171. package/lib/responses/responses-websocket-session.d.mts.map +1 -1
  172. package/lib/responses/responses-websocket-session.d.ts +1 -1
  173. package/lib/responses/responses-websocket-session.d.ts.map +1 -1
  174. package/lib/responses/responses-websocket-session.js +12 -2
  175. package/lib/responses/responses-websocket-session.js.map +1 -1
  176. package/lib/responses/responses-websocket-session.mjs +12 -2
  177. package/lib/responses/responses-websocket-session.mjs.map +1 -1
  178. package/package.json +1 -1
  179. package/resources/audio/speech.d.mts +1 -1
  180. package/resources/audio/speech.d.ts +1 -1
  181. package/resources/audio/speech.js +1 -1
  182. package/resources/audio/speech.mjs +1 -1
  183. package/resources/beta/agents/agents.d.mts +574 -11
  184. package/resources/beta/agents/agents.d.mts.map +1 -1
  185. package/resources/beta/agents/agents.d.ts +574 -11
  186. package/resources/beta/agents/agents.d.ts.map +1 -1
  187. package/resources/beta/agents/agents.js.map +1 -1
  188. package/resources/beta/agents/agents.mjs.map +1 -1
  189. package/resources/beta/agents/environments/templates.d.mts +56 -3
  190. package/resources/beta/agents/environments/templates.d.mts.map +1 -1
  191. package/resources/beta/agents/environments/templates.d.ts +56 -3
  192. package/resources/beta/agents/environments/templates.d.ts.map +1 -1
  193. package/resources/beta/agents/index.d.mts +1 -1
  194. package/resources/beta/agents/index.d.mts.map +1 -1
  195. package/resources/beta/agents/index.d.ts +1 -1
  196. package/resources/beta/agents/index.d.ts.map +1 -1
  197. package/resources/beta/agents/index.js.map +1 -1
  198. package/resources/beta/agents/index.mjs.map +1 -1
  199. package/resources/beta/agents/sessions/events.d.mts +16 -12
  200. package/resources/beta/agents/sessions/events.d.mts.map +1 -1
  201. package/resources/beta/agents/sessions/events.d.ts +16 -12
  202. package/resources/beta/agents/sessions/events.d.ts.map +1 -1
  203. package/resources/beta/agents/sessions/events.js +16 -12
  204. package/resources/beta/agents/sessions/events.js.map +1 -1
  205. package/resources/beta/agents/sessions/events.mjs +16 -12
  206. package/resources/beta/agents/sessions/events.mjs.map +1 -1
  207. package/resources/beta/agents/sessions/index.d.mts +1 -0
  208. package/resources/beta/agents/sessions/index.d.mts.map +1 -1
  209. package/resources/beta/agents/sessions/index.d.ts +1 -0
  210. package/resources/beta/agents/sessions/index.d.ts.map +1 -1
  211. package/resources/beta/agents/sessions/index.js +3 -1
  212. package/resources/beta/agents/sessions/index.js.map +1 -1
  213. package/resources/beta/agents/sessions/index.mjs +1 -0
  214. package/resources/beta/agents/sessions/index.mjs.map +1 -1
  215. package/resources/beta/agents/sessions/sessions.d.mts +16 -4
  216. package/resources/beta/agents/sessions/sessions.d.mts.map +1 -1
  217. package/resources/beta/agents/sessions/sessions.d.ts +16 -4
  218. package/resources/beta/agents/sessions/sessions.d.ts.map +1 -1
  219. package/resources/beta/agents/sessions/sessions.js +17 -4
  220. package/resources/beta/agents/sessions/sessions.js.map +1 -1
  221. package/resources/beta/agents/sessions/sessions.mjs +17 -4
  222. package/resources/beta/agents/sessions/sessions.mjs.map +1 -1
  223. package/resources/beta/agents/sessions/traces.d.mts +68 -0
  224. package/resources/beta/agents/sessions/traces.d.mts.map +1 -0
  225. package/resources/beta/agents/sessions/traces.d.ts +68 -0
  226. package/resources/beta/agents/sessions/traces.d.ts.map +1 -0
  227. package/resources/beta/agents/sessions/traces.js +81 -0
  228. package/resources/beta/agents/sessions/traces.js.map +1 -0
  229. package/resources/beta/agents/sessions/traces.mjs +77 -0
  230. package/resources/beta/agents/sessions/traces.mjs.map +1 -0
  231. package/resources/beta/beta.d.mts +2 -2
  232. package/resources/beta/beta.d.mts.map +1 -1
  233. package/resources/beta/beta.d.ts +2 -2
  234. package/resources/beta/beta.d.ts.map +1 -1
  235. package/resources/beta/beta.js.map +1 -1
  236. package/resources/beta/beta.mjs.map +1 -1
  237. package/resources/beta/index.d.mts +1 -1
  238. package/resources/beta/index.d.mts.map +1 -1
  239. package/resources/beta/index.d.ts +1 -1
  240. package/resources/beta/index.d.ts.map +1 -1
  241. package/resources/beta/index.js.map +1 -1
  242. package/resources/beta/index.mjs.map +1 -1
  243. package/resources/beta/responses/ws-base.d.mts +3 -1
  244. package/resources/beta/responses/ws-base.d.mts.map +1 -1
  245. package/resources/beta/responses/ws-base.d.ts +3 -1
  246. package/resources/beta/responses/ws-base.d.ts.map +1 -1
  247. package/resources/beta/responses/ws-base.js +35 -6
  248. package/resources/beta/responses/ws-base.js.map +1 -1
  249. package/resources/beta/responses/ws-base.mjs +35 -6
  250. package/resources/beta/responses/ws-base.mjs.map +1 -1
  251. package/resources/beta/responses/ws.d.mts +10 -0
  252. package/resources/beta/responses/ws.d.mts.map +1 -1
  253. package/resources/beta/responses/ws.d.ts +10 -0
  254. package/resources/beta/responses/ws.d.ts.map +1 -1
  255. package/resources/beta/responses/ws.js +23 -22
  256. package/resources/beta/responses/ws.js.map +1 -1
  257. package/resources/beta/responses/ws.mjs +24 -23
  258. package/resources/beta/responses/ws.mjs.map +1 -1
  259. package/resources/chat/completions/completions.d.mts +1 -1
  260. package/resources/chat/completions/completions.d.mts.map +1 -1
  261. package/resources/chat/completions/completions.d.ts +1 -1
  262. package/resources/chat/completions/completions.d.ts.map +1 -1
  263. package/resources/evals/runs/runs.js +1 -1
  264. package/resources/evals/runs/runs.js.map +1 -1
  265. package/resources/evals/runs/runs.mjs +1 -1
  266. package/resources/evals/runs/runs.mjs.map +1 -1
  267. package/resources/live/forks/forks.d.mts +5 -1
  268. package/resources/live/forks/forks.d.mts.map +1 -1
  269. package/resources/live/forks/forks.d.ts +5 -1
  270. package/resources/live/forks/forks.d.ts.map +1 -1
  271. package/resources/live/live.d.mts +5 -1
  272. package/resources/live/live.d.mts.map +1 -1
  273. package/resources/live/live.d.ts +5 -1
  274. package/resources/live/live.d.ts.map +1 -1
  275. package/resources/live/live.js.map +1 -1
  276. package/resources/live/live.mjs.map +1 -1
  277. package/resources/responses/ws-base.d.mts +3 -1
  278. package/resources/responses/ws-base.d.mts.map +1 -1
  279. package/resources/responses/ws-base.d.ts +3 -1
  280. package/resources/responses/ws-base.d.ts.map +1 -1
  281. package/resources/responses/ws-base.js +35 -6
  282. package/resources/responses/ws-base.js.map +1 -1
  283. package/resources/responses/ws-base.mjs +35 -6
  284. package/resources/responses/ws-base.mjs.map +1 -1
  285. package/resources/responses/ws.d.mts +10 -0
  286. package/resources/responses/ws.d.mts.map +1 -1
  287. package/resources/responses/ws.d.ts +10 -0
  288. package/resources/responses/ws.d.ts.map +1 -1
  289. package/resources/responses/ws.js +23 -22
  290. package/resources/responses/ws.js.map +1 -1
  291. package/resources/responses/ws.mjs +24 -23
  292. package/resources/responses/ws.mjs.map +1 -1
  293. package/src/client.ts +55 -19
  294. package/src/core/streaming.ts +9 -0
  295. package/src/helpers/beta/agents/standard-schema.ts +13 -0
  296. package/src/helpers/beta/agents/zod.ts +10 -0
  297. package/src/internal/realtime-credentials.ts +70 -4
  298. package/src/internal/responses-ws-credentials.ts +160 -0
  299. package/src/internal/ws-adapter-node.ts +66 -0
  300. package/src/internal/ws.ts +139 -11
  301. package/src/lib/agents/agent-session-stream.ts +36 -24
  302. package/src/lib/beta/agents/agent-session-create-stream.ts +36 -0
  303. package/src/lib/beta/agents/agent-turn-result-collector.ts +122 -0
  304. package/src/lib/beta/agents/agent-turn-result-error.ts +33 -0
  305. package/src/lib/beta/agents/agent-turn-result.ts +22 -0
  306. package/src/lib/beta/agents/function-tool.ts +60 -0
  307. package/src/lib/beta/agents/output-format-types.ts +12 -0
  308. package/src/lib/beta/agents/output-format.ts +20 -0
  309. package/src/lib/beta/agents/output-parse-error.ts +16 -0
  310. package/src/lib/beta/agents/parse-result.ts +133 -0
  311. package/src/lib/beta/agents/parsed-agent-turn-result.ts +12 -0
  312. package/src/lib/beta/agents/result-collection.ts +115 -0
  313. package/src/lib/beta/agents/tool-output.ts +22 -0
  314. package/src/lib/responses/responses-websocket-session.ts +14 -3
  315. package/src/resources/audio/speech.ts +1 -1
  316. package/src/resources/beta/agents/agents.ts +697 -6
  317. package/src/resources/beta/agents/environments/templates.ts +64 -3
  318. package/src/resources/beta/agents/index.ts +3 -0
  319. package/src/resources/beta/agents/sessions/events.ts +16 -12
  320. package/src/resources/beta/agents/sessions/index.ts +1 -0
  321. package/src/resources/beta/agents/sessions/sessions.ts +48 -18
  322. package/src/resources/beta/agents/sessions/traces.ts +298 -0
  323. package/src/resources/beta/beta.ts +6 -0
  324. package/src/resources/beta/index.ts +3 -0
  325. package/src/resources/beta/responses/ws-base.ts +40 -6
  326. package/src/resources/beta/responses/ws.ts +37 -27
  327. package/src/resources/chat/completions/completions.ts +1 -1
  328. package/src/resources/evals/runs/runs.ts +1 -1
  329. package/src/resources/live/forks/forks.ts +6 -1
  330. package/src/resources/live/live.ts +6 -1
  331. package/src/resources/responses/ws-base.ts +40 -6
  332. package/src/resources/responses/ws.ts +37 -27
  333. package/src/tsconfig.dist-src-node.d.ts +7 -0
  334. package/src/tsconfig.dist-src.d.ts +3 -1
  335. package/src/version.ts +1 -1
  336. package/version.d.mts +1 -1
  337. package/version.d.ts +1 -1
  338. package/version.js +1 -1
  339. package/version.mjs +1 -1
@@ -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
+ }
@@ -0,0 +1,122 @@
1
+ import type {
2
+ AgentSession,
3
+ AgentSessionEvent,
4
+ AgentSessionAssistantMessage,
5
+ } from '../../../resources/beta/agents/agents';
6
+ import type { Turn } from '../../../resources/beta/agents/sessions/turns';
7
+ import { AgentTurnResult } from './agent-turn-result';
8
+ import { AgentTurnResultError } from './agent-turn-result-error';
9
+
10
+ /** Accumulates completed items independently of transport and tool execution.
11
+ * @internal
12
+ */
13
+ export class AgentTurnResultCollector {
14
+ #sessionID: string | undefined;
15
+ #turn: Turn | undefined;
16
+ #messages = new Map<string, { index: number; message: AgentSessionAssistantMessage }>();
17
+ #requiredActions: AgentSession['required_actions'] = [];
18
+ #sessionFailed = false;
19
+ #terminal = false;
20
+ #idle = false;
21
+
22
+ constructor(sessionID?: string) {
23
+ this.#sessionID = sessionID;
24
+ }
25
+
26
+ accept(event: AgentSessionEvent): void {
27
+ if (this.ready) {
28
+ return;
29
+ }
30
+ if ('session' in event) {
31
+ this.#sessionID ??= event.session.id;
32
+ this.#requiredActions = structuredClone(event.session.required_actions ?? []);
33
+ this.#sessionFailed ||= event.type === 'agent.session.failed';
34
+ this.#idle ||= event.type === 'agent.session.idle' && this.#terminal;
35
+ }
36
+ if (event.type === 'agent.session.turn.created' && event.turn.subagent_id === null && !this.#turn) {
37
+ this.#turn = structuredClone(event.turn);
38
+ this.#sessionID = event.turn.session_id;
39
+ }
40
+ let turnID = 'turn_id' in event ? event.turn_id : undefined;
41
+ if ('item' in event) {
42
+ turnID = event.item.turn_id;
43
+ }
44
+ if (!this.#turn || turnID !== this.#turn.id) {
45
+ return;
46
+ }
47
+ this.#acceptTurn(event);
48
+ }
49
+
50
+ #acceptTurn(event: AgentSessionEvent): void {
51
+ if ('turn' in event) {
52
+ this.#turn = structuredClone(event.turn);
53
+ this.#terminal ||=
54
+ event.type === 'agent.session.turn.completed' ||
55
+ event.type === 'agent.session.turn.failed' ||
56
+ event.type === 'agent.session.turn.cancelled';
57
+ }
58
+ this.#acceptOutput(event);
59
+ }
60
+
61
+ #acceptOutput(event: AgentSessionEvent): void {
62
+ if (
63
+ event.type !== 'agent.session.turn.item.done' ||
64
+ event.item.type !== 'message' ||
65
+ event.item.status !== 'completed' ||
66
+ event.item.phase === 'commentary' ||
67
+ this.#messages.has(event.item.id)
68
+ ) {
69
+ return;
70
+ }
71
+ this.#messages.set(event.item.id, { index: event.output_index, message: structuredClone(event.item) });
72
+ }
73
+
74
+ #finalMessages(): AgentSessionAssistantMessage[] {
75
+ return (
76
+ [...this.#messages.values()]
77
+ // oxlint-disable-next-line unicorn/no-array-sort -- Sort a fresh array; ES2020 declarations do not include toSorted.
78
+ .sort((a, b) => a.index - b.index)
79
+ .map(({ message }) => message)
80
+ );
81
+ }
82
+
83
+ error(reason: AgentTurnResultError['reason'], cause?: unknown): AgentTurnResultError {
84
+ return new AgentTurnResultError(
85
+ reason,
86
+ this.#sessionID,
87
+ this.#turn,
88
+ this.#finalMessages(),
89
+ this.#requiredActions,
90
+ cause,
91
+ );
92
+ }
93
+
94
+ checkAction(canHandle: (name: string) => boolean): void {
95
+ if (this.#sessionFailed || this.#turn?.status === 'failed') {
96
+ throw this.error('failed');
97
+ }
98
+ if (this.#turn?.status === 'cancelled') {
99
+ throw this.error('cancelled');
100
+ }
101
+ if (this.#requiredActions.some((action) => action.type !== 'function_call' || !canHandle(action.name))) {
102
+ throw this.error('requires_action');
103
+ }
104
+ }
105
+
106
+ get ready(): boolean {
107
+ return this.#terminal && this.#idle;
108
+ }
109
+
110
+ release(): void {
111
+ this.#messages.clear();
112
+ this.#requiredActions = [];
113
+ }
114
+
115
+ finish(): AgentTurnResult {
116
+ this.checkAction(() => this.ready);
117
+ if (!this.#terminal || this.#turn?.status !== 'completed' || !this.#idle) {
118
+ throw this.error('observation');
119
+ }
120
+ return new AgentTurnResult(this.#turn, this.#finalMessages());
121
+ }
122
+ }
@@ -0,0 +1,33 @@
1
+ import { OpenAIError } from '../../../core/error';
2
+ import type { AgentSession, AgentSessionAssistantMessage } from '../../../resources/beta/agents/agents';
3
+ import type { Turn } from '../../../resources/beta/agents/sessions/turns';
4
+
5
+ /** Beta: final collection failed; partial state is evidence, not a successful result. */
6
+ export class AgentTurnResultError extends OpenAIError {
7
+ override name = 'AgentTurnResultError';
8
+ readonly reason: 'failed' | 'cancelled' | 'requires_action' | 'observation';
9
+ readonly session_id: string | undefined;
10
+ readonly turn: Turn | undefined;
11
+ readonly messages: AgentSessionAssistantMessage[];
12
+ readonly required_actions: AgentSession['required_actions'];
13
+ readonly cause: unknown;
14
+ constructor(
15
+ reason: 'failed' | 'cancelled' | 'requires_action' | 'observation',
16
+ session_id: string | undefined,
17
+ turn: Turn | undefined,
18
+ messages: AgentSessionAssistantMessage[],
19
+ required_actions: AgentSession['required_actions'] = [],
20
+ cause?: unknown,
21
+ ) {
22
+ super(`Could not collect the agent turn result: ${reason}`);
23
+ this.reason = reason;
24
+ this.session_id = session_id;
25
+ this.turn = turn;
26
+ this.messages = messages;
27
+ this.required_actions = required_actions;
28
+ this.cause = cause;
29
+ }
30
+ get turn_id(): string | undefined {
31
+ return this.turn?.id;
32
+ }
33
+ }
@@ -0,0 +1,22 @@
1
+ import type { AgentSessionAssistantMessage } from '../../../resources/beta/agents/agents';
2
+ import type { Turn } from '../../../resources/beta/agents/sessions/turns';
3
+ import { outputText } from '../../agents/output-text';
4
+
5
+ /** Beta: the completed final assistant messages from one hosted root turn. */
6
+ export class AgentTurnResult {
7
+ readonly turn: Turn;
8
+ readonly messages: AgentSessionAssistantMessage[];
9
+ constructor(turn: Turn, messages: AgentSessionAssistantMessage[]) {
10
+ this.turn = turn;
11
+ this.messages = messages;
12
+ }
13
+ get session_id(): string {
14
+ return this.turn.session_id;
15
+ }
16
+ get turn_id(): string {
17
+ return this.turn.id;
18
+ }
19
+ get output_text(): string {
20
+ return this.messages.map(outputText).join('');
21
+ }
22
+ }
@@ -0,0 +1,60 @@
1
+ import { OpenAIError } from '../../../core/error';
2
+ import type { AgentToolHandler } from '../../agents/agent-session-stream';
3
+ import type { AutoParseableResponseTool } from '../../ResponsesParser';
4
+ import type { AgentToolParam } from '../../../resources/beta/agents/agents';
5
+ import { isInputContent } from './tool-output';
6
+
7
+ /** A beta Agents function definition paired with its local, validating handler. */
8
+ export interface AgentFunctionTool {
9
+ /** Model-visible function name, also used as the key in `toolHandlers`. */
10
+ readonly name: string;
11
+ /** Hosted configuration for `agent.tools`; contains no local callback or parser. */
12
+ readonly definition: AgentToolParam.AgentToolConfigParamFunction;
13
+ /** Validates arguments before invoking the callback through the existing stream dispatcher. */
14
+ readonly handler: AgentToolHandler;
15
+ }
16
+
17
+ /**
18
+ * Adapts a `zodResponsesFunction` or `standardResponsesFunction` to beta Agents.
19
+ * Pass `definition` to `agent.tools` and register `handler` under `name` in
20
+ * `sessions.stream()`'s `toolHandlers`. Creating a definition does not execute it.
21
+ * The existing dispatcher handles callback results, failures, and submission retries.
22
+ * Scalar results and business-data arrays become JSON text; `undefined` becomes
23
+ * the text `undefined`. Supported text/image content arrays retain their format.
24
+ *
25
+ * @throws {OpenAIError} If the tool has no callback or argument schema.
26
+ */
27
+ export function functionTool<Arguments>(
28
+ tool: AutoParseableResponseTool<{ name: string; arguments: Arguments }>,
29
+ ): AgentFunctionTool {
30
+ const execute = tool.$callback;
31
+ const parse = tool.$parseRaw;
32
+ if (!execute || !tool.parameters) {
33
+ throw new OpenAIError('Agents function tools require a callback and argument schema');
34
+ }
35
+ return {
36
+ name: tool.name,
37
+ definition: {
38
+ type: 'function',
39
+ name: tool.name,
40
+ description: tool.description ?? '',
41
+ parameters: tool.parameters,
42
+ ...(tool.defer_loading === undefined ? {} : { defer_loading: tool.defer_loading }),
43
+ },
44
+ handler: async (arguments_) => {
45
+ const result: unknown = await execute(parse(JSON.stringify(arguments_)));
46
+ if (Array.isArray(result)) {
47
+ return result.length > 0 && result.every(isInputContent) ? result : JSON.stringify(result);
48
+ }
49
+ // oxlint-disable-next-line anti-slop/no-runtime-typeof -- Existing schema-tool callbacks return arbitrary application values; adapt them to the Agents dispatcher's output contract.
50
+ if (typeof result === 'object' || typeof result === 'string') {
51
+ return result;
52
+ }
53
+ const text = result === undefined ? 'undefined' : JSON.stringify(result);
54
+ if (text === undefined) {
55
+ throw new OpenAIError('Tool output must be JSON serializable');
56
+ }
57
+ return text;
58
+ },
59
+ };
60
+ }
@@ -0,0 +1,12 @@
1
+ import type { TextFormatParam } from '../../../resources/beta/agents/agents';
2
+ import type { AgentTurnResult } from './agent-turn-result';
3
+ import type { ParsedAgentTurnResult } from './parsed-agent-turn-result';
4
+
5
+ /** Beta: an Agents JSON Schema format with a local output validator. */
6
+ export interface AgentOutputFormat<T> extends TextFormatParam.TextFormatParamJSONSchema {
7
+ /** SDK-only parser; omitted from serialized requests. */
8
+ $parseRaw: (text: string) => T;
9
+ }
10
+
11
+ /** Beta: raw results remain unchanged unless a typed format is supplied. */
12
+ export type AgentResult<T> = [T] extends [never] ? AgentTurnResult : ParsedAgentTurnResult<T>;
@@ -0,0 +1,20 @@
1
+ import type { AgentOutputFormat } from './output-format-types';
2
+
3
+ import type { JSONSchema } from '../../jsonschema';
4
+
5
+ export type { AgentOutputFormat, AgentResult } from './output-format-types';
6
+
7
+ export { ParsedAgentTurnResult } from './parsed-agent-turn-result';
8
+ export { AgentOutputParseError } from './output-parse-error';
9
+
10
+ /** Beta: bind a JSON schema to its local output parser; the API validates schema support. */
11
+ export function agentOutputFormat<T>(schema: JSONSchema, parse: (text: string) => T): AgentOutputFormat<T> {
12
+ // Keep spread-compatible parsing without exposing caller-owned serialization hooks.
13
+ const parser = Object.defineProperty((text: string) => parse(text), 'toJSON', {
14
+ value: () => {
15
+ /* Parser metadata is omitted from JSON requests. */
16
+ },
17
+ });
18
+ // SAFETY: The API schema type exposes JSON Schema keywords as a record; preserve the supplied schema unchanged.
19
+ return { type: 'json_schema', schema: schema as AgentOutputFormat<T>['schema'], $parseRaw: parser };
20
+ }
@@ -0,0 +1,16 @@
1
+ import { OpenAIError } from '../../../core/error';
2
+ import type { AgentTurnResult } from './agent-turn-result';
3
+
4
+ /** Beta: hosted execution completed, but local output parsing failed. */
5
+ export class AgentOutputParseError extends OpenAIError {
6
+ override name = 'AgentOutputParseError';
7
+ readonly #rawResult: AgentTurnResult;
8
+ constructor(result: AgentTurnResult) {
9
+ super('The completed agent output could not be parsed');
10
+ this.#rawResult = result;
11
+ }
12
+ /** Inspect the completed output explicitly; ordinary error logging omits it. */
13
+ get raw_result(): AgentTurnResult {
14
+ return this.#rawResult;
15
+ }
16
+ }
@@ -0,0 +1,133 @@
1
+ import type { RequestOptions } from '../../../internal/request-options';
2
+ import { OpenAIError } from '../../../core/error';
3
+ import type { SessionCreateParams } from '../../../resources/beta/agents/sessions/sessions';
4
+ import type { AgentTurnResult } from './agent-turn-result';
5
+ import type { TextFormatParam } from '../../../resources/beta/agents/agents';
6
+ import type { AgentOutputFormat, AgentResult } from './output-format-types';
7
+ import { ParsedAgentTurnResult } from './parsed-agent-turn-result';
8
+ import { AgentOutputParseError } from './output-parse-error';
9
+
10
+ /** @internal */
11
+ export function parseAgentResult<T>(result: AgentTurnResult, format?: AgentOutputFormat<T>): AgentResult<T> {
12
+ try {
13
+ if (!format) {
14
+ // SAFETY: Absent formats use the default never/raw overload.
15
+ return result as AgentResult<T>;
16
+ }
17
+ let first: { value: T } | undefined;
18
+ for (const message of result.messages) {
19
+ for (const content of message.content) {
20
+ if (content.type === 'output_text') {
21
+ const value = format.$parseRaw(content.text);
22
+ first ??= { value };
23
+ }
24
+ }
25
+ }
26
+ if (!first) {
27
+ throw new AgentOutputParseError(result);
28
+ }
29
+ // SAFETY: Callers preserve the format's inferred T.
30
+ return new ParsedAgentTurnResult(result, first.value) as AgentResult<T>;
31
+ } catch {
32
+ throw new AgentOutputParseError(result);
33
+ }
34
+ }
35
+
36
+ // Inspect only JSON-visible data fields; leave getters and inherited values to normal serialization.
37
+ function ownJSONValue<T extends object, K extends keyof T>(
38
+ object: T | null | undefined,
39
+ key: K,
40
+ ): T[K] | undefined {
41
+ const descriptor = object && Object.getOwnPropertyDescriptor(object, key);
42
+ // SAFETY: The own data descriptor corresponds to the requested property in T.
43
+ return descriptor?.enumerable && 'value' in descriptor ? (descriptor.value as T[K]) : undefined;
44
+ }
45
+
46
+ /** @internal */
47
+ export function agentFormatParser<T = unknown>(
48
+ format: TextFormatParam | null | undefined,
49
+ ): AgentOutputFormat<T> | undefined {
50
+ if (!format) {
51
+ return undefined;
52
+ }
53
+ const descriptor = Object.getOwnPropertyDescriptor(format, '$parseRaw');
54
+ // oxlint-disable-next-line anti-slop/no-runtime-typeof -- An own parser data property is the explicit opt-in marker.
55
+ if (!descriptor || !('value' in descriptor) || typeof descriptor.value !== 'function') {
56
+ return undefined;
57
+ }
58
+ // SAFETY: Inspect the schema data descriptor without evaluating a caller getter; validate its discriminator below.
59
+ const schema = ownJSONValue(format as TextFormatParam.TextFormatParamJSONSchema, 'schema');
60
+ if (ownJSONValue(format, 'type') !== 'json_schema' || !schema) {
61
+ throw new OpenAIError('Typed agent formats require own enumerable type and schema data properties');
62
+ }
63
+ // SAFETY: The own descriptor was checked for a callable parser; retain its receiver and never reread the property.
64
+ const parse = descriptor.value as AgentOutputFormat<T>['$parseRaw'];
65
+ return { type: 'json_schema', schema, $parseRaw: (text) => parse.call(format, text) };
66
+ }
67
+
68
+ // Materialize JSON-visible envelope getters once without evaluating unrelated properties.
69
+ function snapshotJSONProperty<T extends object, K extends keyof T>(
70
+ object: T,
71
+ key: K,
72
+ capture: (value: T[K]) => T[K] = (value) => value,
73
+ ): T {
74
+ const descriptor = Object.getOwnPropertyDescriptor(object, key);
75
+ if (!descriptor?.enumerable) {
76
+ return object;
77
+ }
78
+ const value = capture(object[key]);
79
+ if ('value' in descriptor && value === descriptor.value) {
80
+ return object;
81
+ }
82
+ // SAFETY: Preserve the original properties/prototype, replacing only this captured property's value.
83
+ return Object.create(Object.getPrototypeOf(object), {
84
+ ...Object.getOwnPropertyDescriptors(object),
85
+ [key]: { value, enumerable: true, configurable: descriptor.configurable, writable: true },
86
+ }) as T;
87
+ }
88
+
89
+ /** @internal */
90
+ export function captureAgentOutput(input: SessionCreateParams, options?: RequestOptions) {
91
+ const body = snapshotJSONProperty(input, 'agent', (agent) =>
92
+ agent
93
+ ? snapshotJSONProperty(agent, 'text', (text) => (text ? snapshotJSONProperty(text, 'format') : text))
94
+ : agent,
95
+ );
96
+ const agent = ownJSONValue(body, 'agent');
97
+ const text = ownJSONValue(agent, 'text');
98
+ const format = agentFormatParser(ownJSONValue(text, 'format'));
99
+ if (!format) {
100
+ return { body, options };
101
+ }
102
+ const schema = structuredClone(format.schema);
103
+ for (const envelope of [body, agent, text]) {
104
+ if (envelope && 'toJSON' in envelope) {
105
+ throw new OpenAIError('Typed agent requests cannot customize body, agent, or text serialization');
106
+ }
107
+ }
108
+ // Snapshot options once, matching the own enumerable fields native request spreading uses.
109
+ const capturedOptions = { ...options };
110
+ if (ownJSONValue(capturedOptions, 'body') !== undefined) {
111
+ throw new OpenAIError('Typed agent requests cannot override the body in request options');
112
+ }
113
+ delete capturedOptions.body;
114
+ return {
115
+ options: capturedOptions,
116
+ body: {
117
+ ...body,
118
+ agent: {
119
+ ...agent,
120
+ text: { ...text, format: { type: 'json_schema', schema } },
121
+ },
122
+ },
123
+ format,
124
+ };
125
+ }
126
+
127
+ /** @internal */
128
+ export async function parseAgentResultPromise<T>(
129
+ result: Promise<AgentTurnResult>,
130
+ format?: AgentOutputFormat<T>,
131
+ ): Promise<AgentResult<T>> {
132
+ return parseAgentResult(await result, format);
133
+ }
@@ -0,0 +1,12 @@
1
+ import { AgentTurnResult } from './agent-turn-result';
2
+
3
+ /** Beta: a completed hosted turn whose output passed the local parser. */
4
+ export class ParsedAgentTurnResult<T> extends AgentTurnResult {
5
+ readonly output_parsed: T;
6
+ readonly raw_result: AgentTurnResult;
7
+ constructor(result: AgentTurnResult, parsed: T) {
8
+ super(result.turn, result.messages);
9
+ this.raw_result = result;
10
+ this.output_parsed = parsed;
11
+ }
12
+ }