openai 7.10.0 → 7.12.1

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 (421) hide show
  1. package/CHANGELOG.md +196 -0
  2. package/README.md +7 -5
  3. package/_vendor/partial-json-parser/parser.d.mts.map +1 -1
  4. package/_vendor/partial-json-parser/parser.d.ts.map +1 -1
  5. package/_vendor/partial-json-parser/parser.js +4 -3
  6. package/_vendor/partial-json-parser/parser.js.map +1 -1
  7. package/_vendor/partial-json-parser/parser.mjs +4 -3
  8. package/_vendor/partial-json-parser/parser.mjs.map +1 -1
  9. package/_vendor/zod-to-json-schema/parsers/string.js +1 -1
  10. package/_vendor/zod-to-json-schema/parsers/string.js.map +1 -1
  11. package/_vendor/zod-to-json-schema/parsers/string.mjs +1 -1
  12. package/_vendor/zod-to-json-schema/parsers/string.mjs.map +1 -1
  13. package/_vendor/zod-to-json-schema/parsers/tuple.d.mts.map +1 -1
  14. package/_vendor/zod-to-json-schema/parsers/tuple.d.ts.map +1 -1
  15. package/_vendor/zod-to-json-schema/parsers/tuple.js +19 -12
  16. package/_vendor/zod-to-json-schema/parsers/tuple.js.map +1 -1
  17. package/_vendor/zod-to-json-schema/parsers/tuple.mjs +19 -12
  18. package/_vendor/zod-to-json-schema/parsers/tuple.mjs.map +1 -1
  19. package/_vendor/zod-to-json-schema/zodToJsonSchema.d.mts.map +1 -1
  20. package/_vendor/zod-to-json-schema/zodToJsonSchema.d.ts.map +1 -1
  21. package/_vendor/zod-to-json-schema/zodToJsonSchema.js +6 -2
  22. package/_vendor/zod-to-json-schema/zodToJsonSchema.js.map +1 -1
  23. package/_vendor/zod-to-json-schema/zodToJsonSchema.mjs +6 -2
  24. package/_vendor/zod-to-json-schema/zodToJsonSchema.mjs.map +1 -1
  25. package/auth/subject-token-providers.d.mts.map +1 -1
  26. package/auth/subject-token-providers.d.ts.map +1 -1
  27. package/auth/subject-token-providers.js +10 -5
  28. package/auth/subject-token-providers.js.map +1 -1
  29. package/auth/subject-token-providers.mjs +10 -5
  30. package/auth/subject-token-providers.mjs.map +1 -1
  31. package/auth/workload-identity-auth.d.mts.map +1 -1
  32. package/auth/workload-identity-auth.d.ts.map +1 -1
  33. package/auth/workload-identity-auth.js +21 -11
  34. package/auth/workload-identity-auth.js.map +1 -1
  35. package/auth/workload-identity-auth.mjs +21 -11
  36. package/auth/workload-identity-auth.mjs.map +1 -1
  37. package/azure.d.mts.map +1 -1
  38. package/azure.d.ts.map +1 -1
  39. package/azure.js +31 -2
  40. package/azure.js.map +1 -1
  41. package/azure.mjs +32 -3
  42. package/azure.mjs.map +1 -1
  43. package/beta/realtime/internal-base.d.mts.map +1 -1
  44. package/beta/realtime/internal-base.d.ts.map +1 -1
  45. package/beta/realtime/internal-base.js +3 -2
  46. package/beta/realtime/internal-base.js.map +1 -1
  47. package/beta/realtime/internal-base.mjs +3 -2
  48. package/beta/realtime/internal-base.mjs.map +1 -1
  49. package/beta/realtime/websocket.d.mts +2 -0
  50. package/beta/realtime/websocket.d.mts.map +1 -1
  51. package/beta/realtime/websocket.d.ts +2 -0
  52. package/beta/realtime/websocket.d.ts.map +1 -1
  53. package/beta/realtime/websocket.js +12 -7
  54. package/beta/realtime/websocket.js.map +1 -1
  55. package/beta/realtime/websocket.mjs +12 -7
  56. package/beta/realtime/websocket.mjs.map +1 -1
  57. package/beta/realtime/ws.d.mts +2 -0
  58. package/beta/realtime/ws.d.mts.map +1 -1
  59. package/beta/realtime/ws.d.ts +2 -0
  60. package/beta/realtime/ws.d.ts.map +1 -1
  61. package/beta/realtime/ws.js +7 -5
  62. package/beta/realtime/ws.js.map +1 -1
  63. package/beta/realtime/ws.mjs +7 -5
  64. package/beta/realtime/ws.mjs.map +1 -1
  65. package/client.d.mts +9 -1
  66. package/client.d.mts.map +1 -1
  67. package/client.d.ts +9 -1
  68. package/client.d.ts.map +1 -1
  69. package/client.js +17 -4
  70. package/client.js.map +1 -1
  71. package/client.mjs +17 -4
  72. package/client.mjs.map +1 -1
  73. package/core/EventEmitter.d.mts +2 -1
  74. package/core/EventEmitter.d.mts.map +1 -1
  75. package/core/EventEmitter.d.ts +2 -1
  76. package/core/EventEmitter.d.ts.map +1 -1
  77. package/core/EventEmitter.js +2 -1
  78. package/core/EventEmitter.js.map +1 -1
  79. package/core/EventEmitter.mjs +2 -1
  80. package/core/EventEmitter.mjs.map +1 -1
  81. package/core/pagination.js +1 -1
  82. package/core/pagination.js.map +1 -1
  83. package/core/pagination.mjs +1 -1
  84. package/core/pagination.mjs.map +1 -1
  85. package/core/streaming.d.mts +8 -0
  86. package/core/streaming.d.mts.map +1 -1
  87. package/core/streaming.d.ts +8 -0
  88. package/core/streaming.d.ts.map +1 -1
  89. package/core/streaming.js +68 -15
  90. package/core/streaming.js.map +1 -1
  91. package/core/streaming.mjs +68 -15
  92. package/core/streaming.mjs.map +1 -1
  93. package/helpers/audio.d.mts +1 -1
  94. package/helpers/audio.d.mts.map +1 -1
  95. package/helpers/audio.d.ts +1 -1
  96. package/helpers/audio.d.ts.map +1 -1
  97. package/helpers/audio.js +25 -5
  98. package/helpers/audio.js.map +1 -1
  99. package/helpers/audio.mjs +25 -5
  100. package/helpers/audio.mjs.map +1 -1
  101. package/helpers/zod.d.mts +28 -15
  102. package/helpers/zod.d.mts.map +1 -1
  103. package/helpers/zod.d.ts +28 -15
  104. package/helpers/zod.d.ts.map +1 -1
  105. package/helpers/zod.js +30 -23
  106. package/helpers/zod.js.map +1 -1
  107. package/helpers/zod.mjs +30 -23
  108. package/helpers/zod.mjs.map +1 -1
  109. package/index.js +4 -5
  110. package/internal/bedrock.d.mts +11 -0
  111. package/internal/bedrock.d.mts.map +1 -1
  112. package/internal/bedrock.d.ts +11 -0
  113. package/internal/bedrock.d.ts.map +1 -1
  114. package/internal/bedrock.js +71 -55
  115. package/internal/bedrock.js.map +1 -1
  116. package/internal/bedrock.mjs +70 -55
  117. package/internal/bedrock.mjs.map +1 -1
  118. package/internal/detect-platform.js +2 -2
  119. package/internal/detect-platform.js.map +1 -1
  120. package/internal/detect-platform.mjs +2 -2
  121. package/internal/detect-platform.mjs.map +1 -1
  122. package/internal/errors.d.mts.map +1 -1
  123. package/internal/errors.d.ts.map +1 -1
  124. package/internal/errors.js +3 -2
  125. package/internal/errors.js.map +1 -1
  126. package/internal/errors.mjs +3 -2
  127. package/internal/errors.mjs.map +1 -1
  128. package/internal/headers.d.mts.map +1 -1
  129. package/internal/headers.d.ts.map +1 -1
  130. package/internal/headers.js +4 -5
  131. package/internal/headers.js.map +1 -1
  132. package/internal/headers.mjs +4 -5
  133. package/internal/headers.mjs.map +1 -1
  134. package/internal/parse.js +1 -1
  135. package/internal/parse.js.map +1 -1
  136. package/internal/parse.mjs +1 -1
  137. package/internal/parse.mjs.map +1 -1
  138. package/internal/realtime-credentials.d.mts +15 -0
  139. package/internal/realtime-credentials.d.mts.map +1 -0
  140. package/internal/realtime-credentials.d.ts +15 -0
  141. package/internal/realtime-credentials.d.ts.map +1 -0
  142. package/internal/realtime-credentials.js +23 -0
  143. package/internal/realtime-credentials.js.map +1 -0
  144. package/internal/realtime-credentials.mjs +19 -0
  145. package/internal/realtime-credentials.mjs.map +1 -0
  146. package/internal/responses/response-accumulator.d.mts.map +1 -1
  147. package/internal/responses/response-accumulator.d.ts.map +1 -1
  148. package/internal/responses/response-accumulator.js +2 -1
  149. package/internal/responses/response-accumulator.js.map +1 -1
  150. package/internal/responses/response-accumulator.mjs +2 -1
  151. package/internal/responses/response-accumulator.mjs.map +1 -1
  152. package/internal/to-file.d.mts +2 -1
  153. package/internal/to-file.d.mts.map +1 -1
  154. package/internal/to-file.d.ts +2 -1
  155. package/internal/to-file.d.ts.map +1 -1
  156. package/internal/to-file.js +5 -4
  157. package/internal/to-file.js.map +1 -1
  158. package/internal/to-file.mjs +6 -5
  159. package/internal/to-file.mjs.map +1 -1
  160. package/internal/types.d.mts +3 -3
  161. package/internal/types.d.mts.map +1 -1
  162. package/internal/types.d.ts +3 -3
  163. package/internal/types.d.ts.map +1 -1
  164. package/internal/uploads.d.mts +2 -0
  165. package/internal/uploads.d.mts.map +1 -1
  166. package/internal/uploads.d.ts +2 -0
  167. package/internal/uploads.d.ts.map +1 -1
  168. package/internal/uploads.js +12 -1
  169. package/internal/uploads.js.map +1 -1
  170. package/internal/uploads.mjs +11 -1
  171. package/internal/uploads.mjs.map +1 -1
  172. package/internal/utils/base64.d.mts.map +1 -1
  173. package/internal/utils/base64.d.ts.map +1 -1
  174. package/internal/utils/base64.js +3 -0
  175. package/internal/utils/base64.js.map +1 -1
  176. package/internal/utils/base64.mjs +3 -0
  177. package/internal/utils/base64.mjs.map +1 -1
  178. package/lib/AbstractChatCompletionRunner.d.mts +1 -1
  179. package/lib/AbstractChatCompletionRunner.d.mts.map +1 -1
  180. package/lib/AbstractChatCompletionRunner.d.ts +1 -1
  181. package/lib/AbstractChatCompletionRunner.d.ts.map +1 -1
  182. package/lib/AbstractChatCompletionRunner.js +31 -9
  183. package/lib/AbstractChatCompletionRunner.js.map +1 -1
  184. package/lib/AbstractChatCompletionRunner.mjs +31 -9
  185. package/lib/AbstractChatCompletionRunner.mjs.map +1 -1
  186. package/lib/AssistantStream.d.mts +4 -1
  187. package/lib/AssistantStream.d.mts.map +1 -1
  188. package/lib/AssistantStream.d.ts +4 -1
  189. package/lib/AssistantStream.d.ts.map +1 -1
  190. package/lib/AssistantStream.js +23 -8
  191. package/lib/AssistantStream.js.map +1 -1
  192. package/lib/AssistantStream.mjs +24 -9
  193. package/lib/AssistantStream.mjs.map +1 -1
  194. package/lib/ChatCompletionRunner.d.mts +6 -3
  195. package/lib/ChatCompletionRunner.d.mts.map +1 -1
  196. package/lib/ChatCompletionRunner.d.ts +6 -3
  197. package/lib/ChatCompletionRunner.d.ts.map +1 -1
  198. package/lib/ChatCompletionRunner.js +7 -4
  199. package/lib/ChatCompletionRunner.js.map +1 -1
  200. package/lib/ChatCompletionRunner.mjs +7 -4
  201. package/lib/ChatCompletionRunner.mjs.map +1 -1
  202. package/lib/ChatCompletionStream.d.mts.map +1 -1
  203. package/lib/ChatCompletionStream.d.ts.map +1 -1
  204. package/lib/ChatCompletionStream.js +6 -0
  205. package/lib/ChatCompletionStream.js.map +1 -1
  206. package/lib/ChatCompletionStream.mjs +7 -1
  207. package/lib/ChatCompletionStream.mjs.map +1 -1
  208. package/lib/ChatCompletionStreamingRunner.d.mts.map +1 -1
  209. package/lib/ChatCompletionStreamingRunner.d.ts.map +1 -1
  210. package/lib/ChatCompletionStreamingRunner.js +1 -0
  211. package/lib/ChatCompletionStreamingRunner.js.map +1 -1
  212. package/lib/ChatCompletionStreamingRunner.mjs +1 -0
  213. package/lib/ChatCompletionStreamingRunner.mjs.map +1 -1
  214. package/lib/EventEmitter.d.mts +3 -1
  215. package/lib/EventEmitter.d.mts.map +1 -1
  216. package/lib/EventEmitter.d.ts +3 -1
  217. package/lib/EventEmitter.d.ts.map +1 -1
  218. package/lib/EventEmitter.js +2 -1
  219. package/lib/EventEmitter.js.map +1 -1
  220. package/lib/EventEmitter.mjs +2 -1
  221. package/lib/EventEmitter.mjs.map +1 -1
  222. package/lib/EventStream.d.mts +2 -1
  223. package/lib/EventStream.d.mts.map +1 -1
  224. package/lib/EventStream.d.ts +2 -1
  225. package/lib/EventStream.d.ts.map +1 -1
  226. package/lib/EventStream.js +99 -46
  227. package/lib/EventStream.js.map +1 -1
  228. package/lib/EventStream.mjs +99 -46
  229. package/lib/EventStream.mjs.map +1 -1
  230. package/lib/ResponsesParser.d.mts +5 -4
  231. package/lib/ResponsesParser.d.mts.map +1 -1
  232. package/lib/ResponsesParser.d.ts +5 -4
  233. package/lib/ResponsesParser.d.ts.map +1 -1
  234. package/lib/ResponsesParser.js +21 -7
  235. package/lib/ResponsesParser.js.map +1 -1
  236. package/lib/ResponsesParser.mjs +21 -7
  237. package/lib/ResponsesParser.mjs.map +1 -1
  238. package/lib/RunnableFunction.d.mts +2 -2
  239. package/lib/RunnableFunction.d.mts.map +1 -1
  240. package/lib/RunnableFunction.d.ts +2 -2
  241. package/lib/RunnableFunction.d.ts.map +1 -1
  242. package/lib/embeddings.d.mts.map +1 -1
  243. package/lib/embeddings.d.ts.map +1 -1
  244. package/lib/embeddings.js +18 -12
  245. package/lib/embeddings.js.map +1 -1
  246. package/lib/embeddings.mjs +18 -12
  247. package/lib/embeddings.mjs.map +1 -1
  248. package/lib/file-processing.d.mts +3 -2
  249. package/lib/file-processing.d.mts.map +1 -1
  250. package/lib/file-processing.d.ts +3 -2
  251. package/lib/file-processing.d.ts.map +1 -1
  252. package/lib/file-processing.js +5 -4
  253. package/lib/file-processing.js.map +1 -1
  254. package/lib/file-processing.mjs +5 -4
  255. package/lib/file-processing.mjs.map +1 -1
  256. package/lib/parser.d.mts +16 -4
  257. package/lib/parser.d.mts.map +1 -1
  258. package/lib/parser.d.ts +16 -4
  259. package/lib/parser.d.ts.map +1 -1
  260. package/lib/parser.js +1 -1
  261. package/lib/parser.js.map +1 -1
  262. package/lib/parser.mjs +1 -1
  263. package/lib/parser.mjs.map +1 -1
  264. package/lib/polling.d.mts +2 -2
  265. package/lib/polling.d.mts.map +1 -1
  266. package/lib/polling.d.ts +2 -2
  267. package/lib/polling.d.ts.map +1 -1
  268. package/lib/polling.js +5 -4
  269. package/lib/polling.js.map +1 -1
  270. package/lib/polling.mjs +5 -4
  271. package/lib/polling.mjs.map +1 -1
  272. package/lib/responses/EventTypes.d.mts +2 -2
  273. package/lib/responses/EventTypes.d.mts.map +1 -1
  274. package/lib/responses/EventTypes.d.ts +2 -2
  275. package/lib/responses/EventTypes.d.ts.map +1 -1
  276. package/lib/responses/ResponseInputItems.d.mts +3 -2
  277. package/lib/responses/ResponseInputItems.d.mts.map +1 -1
  278. package/lib/responses/ResponseInputItems.d.ts +3 -2
  279. package/lib/responses/ResponseInputItems.d.ts.map +1 -1
  280. package/lib/responses/ResponseInputItems.js +27 -4
  281. package/lib/responses/ResponseInputItems.js.map +1 -1
  282. package/lib/responses/ResponseInputItems.mjs +27 -4
  283. package/lib/responses/ResponseInputItems.mjs.map +1 -1
  284. package/lib/transform.d.mts.map +1 -1
  285. package/lib/transform.d.ts.map +1 -1
  286. package/lib/transform.js +1 -0
  287. package/lib/transform.js.map +1 -1
  288. package/lib/transform.mjs +1 -0
  289. package/lib/transform.mjs.map +1 -1
  290. package/lib/vector-store-polling.d.mts +2 -2
  291. package/lib/vector-store-polling.d.ts +2 -2
  292. package/lib/vector-store-polling.js +3 -3
  293. package/lib/vector-store-polling.js.map +1 -1
  294. package/lib/vector-store-polling.mjs +3 -3
  295. package/lib/vector-store-polling.mjs.map +1 -1
  296. package/lib/vector-store-upload.d.mts +2 -1
  297. package/lib/vector-store-upload.d.mts.map +1 -1
  298. package/lib/vector-store-upload.d.ts +2 -1
  299. package/lib/vector-store-upload.d.ts.map +1 -1
  300. package/lib/vector-store-upload.js +5 -1
  301. package/lib/vector-store-upload.js.map +1 -1
  302. package/lib/vector-store-upload.mjs +5 -1
  303. package/lib/vector-store-upload.mjs.map +1 -1
  304. package/package.json +1 -1
  305. package/providers/bedrock/aws.d.mts.map +1 -1
  306. package/providers/bedrock/aws.d.ts.map +1 -1
  307. package/providers/bedrock/aws.js +14 -16
  308. package/providers/bedrock/aws.js.map +1 -1
  309. package/providers/bedrock/aws.mjs +15 -17
  310. package/providers/bedrock/aws.mjs.map +1 -1
  311. package/realtime/internal-base.d.mts.map +1 -1
  312. package/realtime/internal-base.d.ts.map +1 -1
  313. package/realtime/internal-base.js +3 -5
  314. package/realtime/internal-base.js.map +1 -1
  315. package/realtime/internal-base.mjs +3 -5
  316. package/realtime/internal-base.mjs.map +1 -1
  317. package/realtime/websocket.d.mts +2 -0
  318. package/realtime/websocket.d.mts.map +1 -1
  319. package/realtime/websocket.d.ts +2 -0
  320. package/realtime/websocket.d.ts.map +1 -1
  321. package/realtime/websocket.js +12 -6
  322. package/realtime/websocket.js.map +1 -1
  323. package/realtime/websocket.mjs +12 -6
  324. package/realtime/websocket.mjs.map +1 -1
  325. package/realtime/ws.d.mts +2 -0
  326. package/realtime/ws.d.mts.map +1 -1
  327. package/realtime/ws.d.ts +2 -0
  328. package/realtime/ws.d.ts.map +1 -1
  329. package/realtime/ws.js +7 -4
  330. package/realtime/ws.js.map +1 -1
  331. package/realtime/ws.mjs +7 -4
  332. package/realtime/ws.mjs.map +1 -1
  333. package/resources/admin/organization/projects/service-accounts/api-keys.d.mts +9 -0
  334. package/resources/admin/organization/projects/service-accounts/api-keys.d.mts.map +1 -1
  335. package/resources/admin/organization/projects/service-accounts/api-keys.d.ts +9 -0
  336. package/resources/admin/organization/projects/service-accounts/api-keys.d.ts.map +1 -1
  337. package/resources/beta/responses/responses.d.mts +112 -33
  338. package/resources/beta/responses/responses.d.mts.map +1 -1
  339. package/resources/beta/responses/responses.d.ts +112 -33
  340. package/resources/beta/responses/responses.d.ts.map +1 -1
  341. package/resources/beta/responses/responses.js.map +1 -1
  342. package/resources/beta/responses/responses.mjs.map +1 -1
  343. package/resources/conversations/items.d.mts +9 -0
  344. package/resources/conversations/items.d.mts.map +1 -1
  345. package/resources/conversations/items.d.ts +9 -0
  346. package/resources/conversations/items.d.ts.map +1 -1
  347. package/resources/images.d.mts +73 -58
  348. package/resources/images.d.mts.map +1 -1
  349. package/resources/images.d.ts +73 -58
  350. package/resources/images.d.ts.map +1 -1
  351. package/resources/images.js.map +1 -1
  352. package/resources/images.mjs.map +1 -1
  353. package/resources/responses/responses.d.mts +112 -32
  354. package/resources/responses/responses.d.mts.map +1 -1
  355. package/resources/responses/responses.d.ts +112 -32
  356. package/resources/responses/responses.d.ts.map +1 -1
  357. package/resources/responses/responses.js.map +1 -1
  358. package/resources/responses/responses.mjs.map +1 -1
  359. package/resources/webhooks/webhooks.d.mts +6 -4
  360. package/resources/webhooks/webhooks.d.mts.map +1 -1
  361. package/resources/webhooks/webhooks.d.ts +6 -4
  362. package/resources/webhooks/webhooks.d.ts.map +1 -1
  363. package/src/_vendor/partial-json-parser/parser.ts +4 -3
  364. package/src/_vendor/zod-to-json-schema/parsers/string.ts +1 -1
  365. package/src/_vendor/zod-to-json-schema/parsers/tuple.ts +23 -16
  366. package/src/_vendor/zod-to-json-schema/zodToJsonSchema.ts +11 -6
  367. package/src/auth/subject-token-providers.ts +12 -7
  368. package/src/auth/workload-identity-auth.ts +27 -12
  369. package/src/azure.ts +34 -3
  370. package/src/beta/realtime/internal-base.ts +3 -2
  371. package/src/beta/realtime/websocket.ts +17 -7
  372. package/src/beta/realtime/ws.ts +9 -5
  373. package/src/client.ts +19 -4
  374. package/src/core/EventEmitter.ts +5 -8
  375. package/src/core/pagination.ts +1 -1
  376. package/src/core/streaming.ts +69 -13
  377. package/src/helpers/audio.ts +27 -6
  378. package/src/helpers/zod.ts +78 -29
  379. package/src/internal/bedrock.ts +84 -58
  380. package/src/internal/detect-platform.ts +2 -2
  381. package/src/internal/errors.ts +3 -2
  382. package/src/internal/headers.ts +7 -5
  383. package/src/internal/parse.ts +1 -1
  384. package/src/internal/realtime-credentials.ts +27 -0
  385. package/src/internal/responses/response-accumulator.ts +1 -0
  386. package/src/internal/to-file.ts +6 -5
  387. package/src/internal/uploads.ts +12 -1
  388. package/src/internal/utils/base64.ts +3 -0
  389. package/src/lib/AbstractChatCompletionRunner.ts +31 -6
  390. package/src/lib/AssistantStream.ts +25 -11
  391. package/src/lib/ChatCompletionRunner.ts +8 -4
  392. package/src/lib/ChatCompletionStream.ts +8 -0
  393. package/src/lib/ChatCompletionStreamingRunner.ts +1 -0
  394. package/src/lib/EventEmitter.ts +17 -8
  395. package/src/lib/EventStream.ts +105 -52
  396. package/src/lib/ResponsesParser.ts +33 -12
  397. package/src/lib/RunnableFunction.ts +2 -2
  398. package/src/lib/embeddings.ts +18 -12
  399. package/src/lib/file-processing.ts +5 -4
  400. package/src/lib/parser.ts +35 -11
  401. package/src/lib/polling.ts +5 -4
  402. package/src/lib/responses/EventTypes.ts +3 -69
  403. package/src/lib/responses/ResponseInputItems.ts +29 -4
  404. package/src/lib/transform.ts +1 -0
  405. package/src/lib/vector-store-polling.ts +3 -3
  406. package/src/lib/vector-store-upload.ts +5 -1
  407. package/src/providers/bedrock/aws.ts +23 -24
  408. package/src/realtime/internal-base.ts +3 -5
  409. package/src/realtime/websocket.ts +14 -6
  410. package/src/realtime/ws.ts +9 -4
  411. package/src/resources/admin/organization/projects/service-accounts/api-keys.ts +11 -0
  412. package/src/resources/beta/responses/responses.ts +143 -31
  413. package/src/resources/conversations/items.ts +11 -0
  414. package/src/resources/images.ts +76 -57
  415. package/src/resources/responses/responses.ts +143 -30
  416. package/src/resources/webhooks/webhooks.ts +6 -4
  417. package/src/version.ts +1 -1
  418. package/version.d.mts +1 -1
  419. package/version.d.ts +1 -1
  420. package/version.js +1 -1
  421. package/version.mjs +1 -1
@@ -9,22 +9,34 @@ import type { OpenAI } from '../client';
9
9
 
10
10
  type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
11
11
 
12
+ function isTransportAbortError(error: unknown): boolean {
13
+ return !(error instanceof APIError) && isAbortError(error);
14
+ }
15
+
12
16
  type StreamTeeQueue<Item> = {
13
17
  readonly length: number;
18
+ readonly canceled: boolean;
14
19
  enqueue: (value: Promise<IteratorResult<Item>>) => void;
15
20
  dequeue: () => Promise<IteratorResult<Item>> | undefined;
21
+ cancel: () => void;
16
22
  };
17
23
 
18
24
  function createStreamTeeQueue<Item>(): StreamTeeQueue<Item> {
19
25
  let entries: (Promise<IteratorResult<Item>> | undefined)[] = [];
20
26
  let head = 0;
27
+ let canceled = false;
21
28
 
22
29
  return {
23
30
  get length() {
24
31
  return entries.length - head;
25
32
  },
33
+ get canceled() {
34
+ return canceled;
35
+ },
26
36
  enqueue(value) {
27
- entries.push(value);
37
+ if (!canceled) {
38
+ entries.push(value);
39
+ }
28
40
  },
29
41
  dequeue() {
30
42
  if (head === entries.length) {
@@ -45,6 +57,11 @@ function createStreamTeeQueue<Item>(): StreamTeeQueue<Item> {
45
57
 
46
58
  return value;
47
59
  },
60
+ cancel() {
61
+ canceled = true;
62
+ entries.length = 0;
63
+ head = 0;
64
+ },
48
65
  };
49
66
  }
50
67
 
@@ -69,6 +86,7 @@ export class Stream<Item> implements AsyncIterable<Item> {
69
86
  /** Abort controller for the underlying request and all branches created with `tee()`. */
70
87
  controller: AbortController;
71
88
  #client: OpenAI | undefined;
89
+ #isTeeBranch = false;
72
90
  private iterator: () => AsyncIterator<Item>;
73
91
 
74
92
  /** Wraps an asynchronous event iterator and the controller that owns its request. */
@@ -101,8 +119,17 @@ export class Stream<Item> implements AsyncIterable<Item> {
101
119
  consumed = true;
102
120
  let done = false;
103
121
  let receivedCompletionSentinel = false;
122
+ const messages = _iterSSEMessages(response, controller);
123
+ const closeMessages = messages.return.bind(messages);
124
+ messages.return = (value) => {
125
+ // Abort before nested iterator cleanup can wait on the response body's cancellation.
126
+ if (!receivedCompletionSentinel) {
127
+ controller.abort();
128
+ }
129
+ return closeMessages(value);
130
+ };
104
131
  try {
105
- for await (const sse of _iterSSEMessages(response, controller)) {
132
+ for await (const sse of messages) {
106
133
  if (sse.data === '[DONE]') {
107
134
  receivedCompletionSentinel = true;
108
135
  break;
@@ -145,7 +172,7 @@ export class Stream<Item> implements AsyncIterable<Item> {
145
172
  // Abort errors and cleanup failures after the completion sentinel are non-fatal.
146
173
  if (
147
174
  receivedCompletionSentinel ||
148
- isAbortError(e) ||
175
+ isTransportAbortError(e) ||
149
176
  (controller.signal.aborted && e === controller.signal.reason)
150
177
  ) {
151
178
  return;
@@ -234,9 +261,6 @@ export class Stream<Item> implements AsyncIterable<Item> {
234
261
  let done = false;
235
262
  try {
236
263
  for await (const line of iterLines()) {
237
- if (done) {
238
- continue;
239
- }
240
264
  if (line) {
241
265
  let data: Item;
242
266
  try {
@@ -278,14 +302,24 @@ export class Stream<Item> implements AsyncIterable<Item> {
278
302
  /**
279
303
  * Splits the stream into two streams which can be
280
304
  * independently read from at different speeds.
305
+ * Closing a branch discards its buffered events without stopping its sibling.
306
+ * Future reads on that branch finish immediately; previously issued `next()`
307
+ * promises remain shared with its sibling and may still resolve with events.
308
+ * Closing both branches invokes the source iterator's `return()` when available.
309
+ * For {@link Stream.fromReadableStream}, closing both branches before iteration
310
+ * starts does not cancel the supplied readable; cancel that readable directly.
281
311
  */
282
312
  tee(): [Stream<Item>, Stream<Item>] {
313
+ const { controller } = this;
283
314
  const left = createStreamTeeQueue<Item>();
284
315
  const right = createStreamTeeQueue<Item>();
285
316
  const iterator = this.iterator();
286
317
 
287
318
  const teeIterator = (queue: StreamTeeQueue<Item>): AsyncIterator<Item> => ({
288
319
  next: () => {
320
+ if (queue.canceled) {
321
+ return Promise.resolve({ value: undefined, done: true });
322
+ }
289
323
  if (queue.length === 0) {
290
324
  const result = iterator.next();
291
325
  left.enqueue(result);
@@ -293,20 +327,34 @@ export class Stream<Item> implements AsyncIterable<Item> {
293
327
  }
294
328
  return queue.dequeue()!;
295
329
  },
330
+ return: async () => {
331
+ if (!queue.canceled) {
332
+ queue.cancel();
333
+ if (left.canceled && right.canceled) {
334
+ await this.#cancelIterator(iterator, controller);
335
+ }
336
+ }
337
+ return { value: undefined, done: true };
338
+ },
296
339
  });
297
340
 
298
- return [
299
- new Stream(() => teeIterator(left), this.controller, this.#client),
300
- new Stream(() => teeIterator(right), this.controller, this.#client),
301
- ];
341
+ const branch = (queue: StreamTeeQueue<Item>) => {
342
+ const stream = new Stream(() => teeIterator(queue), controller, this.#client);
343
+ stream.#isTeeBranch = true;
344
+ return stream;
345
+ };
346
+ return [branch(left), branch(right)];
302
347
  }
303
348
 
304
349
  /**
305
350
  * Converts this stream to a newline-separated ReadableStream of
306
351
  * JSON stringified values in the stream
307
352
  * which can be turned back into a Stream with `Stream.fromReadableStream()`.
353
+ * Canceling a response-backed readable aborts its request. Canceling a tee
354
+ * branch discards its buffered events and leaves sibling consumers running.
308
355
  */
309
356
  toReadableStream(): ReadableStream {
357
+ const { controller } = this;
310
358
  let iter: AsyncIterator<Item>;
311
359
 
312
360
  return makeReadableStream({
@@ -327,11 +375,19 @@ export class Stream<Item> implements AsyncIterable<Item> {
327
375
  ctrl.error(err);
328
376
  }
329
377
  },
330
- async cancel() {
331
- await iter.return?.();
332
- },
378
+ cancel: () => this.#cancelIterator(iter, controller),
333
379
  });
334
380
  }
381
+
382
+ async #cancelIterator(iterator: AsyncIterator<Item>, controller: AbortController): Promise<void> {
383
+ const returnMethod = iterator.return;
384
+ if (returnMethod) {
385
+ if (!this.#isTeeBranch) {
386
+ controller.abort();
387
+ }
388
+ await Reflect.apply(returnMethod, iterator, []);
389
+ }
390
+ }
335
391
  }
336
392
 
337
393
  function createAbortableSSESource(body: NonNullable<Response['body']>, signal: AbortSignal) {
@@ -24,7 +24,17 @@ const recordingProviders: Record<NodeJS.Platform, string> = {
24
24
  };
25
25
 
26
26
  function isResponse(stream: NodeJS.ReadableStream | Response | File): stream is Response {
27
- return (stream as any).body !== undefined;
27
+ // Readables have event and flow-control methods, even after they have ended.
28
+ const nodeReadable =
29
+ 'pipe' in stream &&
30
+ typeof stream.pipe === 'function' &&
31
+ 'on' in stream &&
32
+ typeof stream.on === 'function' &&
33
+ 'pause' in stream &&
34
+ typeof stream.pause === 'function' &&
35
+ 'resume' in stream &&
36
+ typeof stream.resume === 'function';
37
+ return !nodeReadable && 'body' in stream && stream.body !== undefined;
28
38
  }
29
39
 
30
40
  function isFile(stream: NodeJS.ReadableStream | Response | File): stream is File {
@@ -57,10 +67,18 @@ async function nodejsPlayAudio(stream: NodeJS.ReadableStream | Response | File):
57
67
  ffplay.stderr?.resume();
58
68
  ffplay.on('error', reject);
59
69
 
70
+ // The player can exit before the input stream finishes its cleanup.
71
+ let inputFinished = false;
72
+ let processClosed = false;
60
73
  pipeline(source, ffplay.stdin, (error) => {
61
74
  if (error) {
62
75
  ffplay.kill();
63
76
  reject(error);
77
+ return;
78
+ }
79
+ inputFinished = true;
80
+ if (processClosed) {
81
+ resolve();
64
82
  }
65
83
  });
66
84
 
@@ -69,7 +87,10 @@ async function nodejsPlayAudio(stream: NodeJS.ReadableStream | Response | File):
69
87
  reject(new Error(`ffplay process exited with code ${code}`));
70
88
  return;
71
89
  }
72
- resolve();
90
+ processClosed = true;
91
+ if (inputFinished) {
92
+ resolve();
93
+ }
73
94
  });
74
95
  } catch (error) {
75
96
  reject(error);
@@ -103,7 +124,7 @@ type RecordAudioOptions = {
103
124
  /** Stops recording when aborted; successful termination returns the captured audio. */
104
125
  signal?: AbortSignal;
105
126
 
106
- /** Zero-based audio-input device number passed to FFmpeg; defaults to `0`. */
127
+ /** Zero-based audio-input index (ALSA card number on Linux); defaults to `0`. */
107
128
  device?: number;
108
129
 
109
130
  /** Positive recording duration in milliseconds; nonpositive values disable the timeout. */
@@ -137,7 +158,7 @@ function nodejsRecordAudio({ signal, device, timeout }: RecordAudioOptions = {})
137
158
  }
138
159
  };
139
160
  const stopRecording = () => {
140
- if (!settled && ffmpeg) {
161
+ if (!settled && ffmpeg?.pid) {
141
162
  try {
142
163
  wasStopped ||= ffmpeg.kill('SIGTERM');
143
164
  } catch (error) {
@@ -223,7 +244,7 @@ function nodejsRecordAudio({ signal, device, timeout }: RecordAudioOptions = {})
223
244
  '-f',
224
245
  provider,
225
246
  '-i',
226
- `:${device ?? 0}`, // default audio input device; adjust as needed
247
+ provider === 'alsa' ? `hw:${device ?? 0}` : `:${device ?? 0}`,
227
248
  '-ar',
228
249
  DEFAULT_SAMPLE_RATE.toString(),
229
250
  '-ac',
@@ -270,7 +291,7 @@ function nodejsRecordAudio({ signal, device, timeout }: RecordAudioOptions = {})
270
291
  }
271
292
 
272
293
  try {
273
- if (!wasStopped) {
294
+ if (!wasStopped && ffmpeg?.pid) {
274
295
  ffmpeg?.kill('SIGTERM');
275
296
  }
276
297
  } catch {
@@ -82,11 +82,11 @@ function validateSchemaDefinitions(schemaDefinitions: ZodSchemaDefinitions | und
82
82
 
83
83
  function escapeSchemaDefinitionRefs<T extends object>(
84
84
  schema: T,
85
- schemaDefinitions: ZodSchemaDefinitions | undefined,
85
+ definitionNames: ReadonlyMap<string, string>,
86
86
  ): T {
87
87
  const refReplacements = new Map(
88
- Object.keys(schemaDefinitions ?? {}).map((name) => [
89
- `#/definitions/${name}`,
88
+ [...definitionNames].map(([id, name]) => [
89
+ `#/definitions/${id}`,
90
90
  `#/definitions/${encodeSchemaDefinitionRefToken(name)}`,
91
91
  ]),
92
92
  );
@@ -147,7 +147,10 @@ function zodV3ToJsonSchema(
147
147
  : undefined),
148
148
  });
149
149
 
150
- const escapedSchema = escapeSchemaDefinitionRefs(jsonSchema, options.schemaDefinitions);
150
+ const escapedSchema = escapeSchemaDefinitionRefs(
151
+ jsonSchema,
152
+ new Map(Object.keys(options.schemaDefinitions ?? {}).map((name) => [name, name])),
153
+ );
151
154
  assertJSONSerializableSchema(escapedSchema);
152
155
  return escapedSchema;
153
156
  }
@@ -157,16 +160,29 @@ function zodV4ToJsonSchema(
157
160
  options: { schemaDefinitions?: ZodSchemaDefinitions | undefined } = {},
158
161
  ): Record<string, unknown> {
159
162
  const metadata = options.schemaDefinitions ? z4.registry<Record<string, unknown>>() : undefined;
163
+ const definitionNames = new Map<string, string>();
160
164
  for (const [name, definition] of Object.entries(options.schemaDefinitions ?? {})) {
161
- metadata?.add(definition as unknown as z4.ZodType, { id: name });
165
+ // Avoid `/` and `~` so Zod versions that escape JSON Pointer tokens emit the same IDs.
166
+ const id = encodeURIComponent(name).replace(/~/g, '%7E');
167
+ definitionNames.set(id, name);
168
+ metadata?.add(definition as unknown as z4.ZodType, { id });
162
169
  }
163
170
 
164
171
  const jsonSchema = z4.toJSONSchema(schema, {
165
172
  target: 'draft-7',
166
173
  ...(metadata ? { metadata } : undefined),
167
- override: ({ zodSchema, jsonSchema }) => {
174
+ override: ({ zodSchema, jsonSchema, path }) => {
168
175
  const def = zodSchema._zod.def;
169
176
 
177
+ if (def.type === 'object' && hasOwn(def.shape, '__proto__')) {
178
+ const propertyPath = [...path, 'properties', '__proto__']
179
+ .map((part) => encodeSchemaDefinitionRefToken(String(part)))
180
+ .join('/');
181
+ throw new Error(
182
+ `Zod field at \`#/${propertyPath}\` uses unsupported property name \`__proto__\`, which Zod omits from parsed output.`,
183
+ );
184
+ }
185
+
170
186
  if (def.type === 'union' && 'discriminator' in def && Array.isArray(jsonSchema.oneOf)) {
171
187
  if (jsonSchema.anyOf !== undefined) {
172
188
  throw new Error(
@@ -182,7 +198,15 @@ function zodV4ToJsonSchema(
182
198
  },
183
199
  }) as JSONSchema;
184
200
 
185
- const escapedSchema = escapeSchemaDefinitionRefs(jsonSchema, options.schemaDefinitions);
201
+ const escapedSchema = escapeSchemaDefinitionRefs(jsonSchema, definitionNames);
202
+ if (escapedSchema.definitions) {
203
+ escapedSchema.definitions = Object.fromEntries(
204
+ Object.entries(escapedSchema.definitions).map(([id, definition]) => [
205
+ definitionNames.get(id) ?? id,
206
+ definition,
207
+ ]),
208
+ );
209
+ }
186
210
 
187
211
  return toStrictJsonSchema(escapedSchema) as Record<string, unknown>;
188
212
  }
@@ -333,18 +357,8 @@ export function zodTextFormat<ZodInput extends ZodTypeLike>(
333
357
  );
334
358
  }
335
359
 
336
- /**
337
- * Creates a chat completion `function` tool that can be invoked
338
- * automatically by the chat completion `.runTools()` method or automatically
339
- * parsed by `.parse()` / `.stream()`.
340
- *
341
- * Arguments are converted to strict JSON Schema and validated with the supplied
342
- * Zod schema before the optional callback receives them.
343
- *
344
- * @param options Model-visible function name, Zod parameter schema, description,
345
- * and optional callback used by `chat.completions.runTools()`.
346
- */
347
- export function zodFunction<Parameters extends ZodTypeLike>(options: {
360
+ /** Model-facing settings and an optional execution callback for a Zod function tool. */
361
+ interface ZodFunctionOptions<Parameters extends ZodTypeLike> {
348
362
  /** Model-visible function name used to identify matching tool calls. */
349
363
  name: string;
350
364
 
@@ -356,19 +370,53 @@ export function zodFunction<Parameters extends ZodTypeLike>(options: {
356
370
 
357
371
  /** Optional model-visible explanation of when and how the function should be used. */
358
372
  description?: string | undefined;
359
- }): AutoParseableTool<{
373
+ }
374
+
375
+ /** A Zod function tool retaining its inferred argument and callback types. */
376
+ type ZodFunctionTool<
377
+ Parameters extends ZodTypeLike,
378
+ Callback extends ZodFunctionOptions<Parameters>['function'],
379
+ > = AutoParseableTool<{
360
380
  /** Inferred argument type produced by the Zod parameter schema. */
361
381
  arguments: InferZodType<Parameters>;
362
-
363
382
  /** Model-visible name used to match generated function calls. */
364
383
  name: string;
384
+ /** Callback availability determines whether the tool can be executed. */
385
+ function: Callback;
386
+ }>;
365
387
 
366
- /** Callback signature associated with validated function-call arguments. */
367
- function: (args: InferZodType<Parameters>) => unknown;
368
- }> {
369
- const zodSchema = options.parameters as unknown as ZodSchema;
388
+ /**
389
+ * Creates a chat completion `function` tool that can be invoked automatically
390
+ * by `.runTools()` or parsed by `.parse()` / `.stream()`.
391
+ *
392
+ * Arguments are converted to strict JSON Schema and validated with the supplied
393
+ * Zod schema before the callback receives them.
394
+ *
395
+ * @param options Model-visible function name, Zod parameter schema, description,
396
+ * and callback used by `chat.completions.runTools()`.
397
+ */
398
+ export function zodFunction<Parameters extends ZodTypeLike>(
399
+ options: ZodFunctionOptions<Parameters> & {
400
+ /** Callback invoked with validated arguments by chat `runTools()`. */
401
+ function: NonNullable<ZodFunctionOptions<Parameters>['function']>;
402
+ },
403
+ ): ZodFunctionTool<Parameters, NonNullable<ZodFunctionOptions<Parameters>['function']>>;
404
+
405
+ /**
406
+ * Creates a strict chat completion function tool for `.parse()` / `.stream()`.
407
+ * Without a guaranteed callback, the tool cannot be executed by `.runTools()`.
408
+ *
409
+ * @param options Model-visible function details and an optional callback.
410
+ */
411
+ export function zodFunction<Parameters extends ZodTypeLike>(
412
+ options: ZodFunctionOptions<Parameters>,
413
+ ): ZodFunctionTool<Parameters, ZodFunctionOptions<Parameters>['function']>;
414
+
415
+ /** Builds a strict Chat Completions function tool from the supplied Zod schema. */
416
+ export function zodFunction<Parameters extends ZodTypeLike>(options: ZodFunctionOptions<Parameters>) {
417
+ const parameters = options.parameters;
418
+ const zodSchema = parameters as unknown as ZodSchema;
370
419
 
371
- // @ts-expect-error TODO
372
420
  return makeParseableTool<any>(
373
421
  {
374
422
  type: 'function',
@@ -383,7 +431,7 @@ export function zodFunction<Parameters extends ZodTypeLike>(options: {
383
431
  },
384
432
  {
385
433
  callback: options.function,
386
- parser: (args) => parseZodObject(options.parameters, args),
434
+ parser: (args) => parseZodObject(parameters, args),
387
435
  },
388
436
  );
389
437
  }
@@ -421,7 +469,8 @@ export function zodResponsesFunction<Parameters extends ZodTypeLike>(options: {
421
469
  /** Callback signature associated with validated function-call arguments. */
422
470
  function: (args: InferZodType<Parameters>) => unknown;
423
471
  }> {
424
- const zodSchema = options.parameters as unknown as ZodSchema;
472
+ const parameters = options.parameters;
473
+ const zodSchema = parameters as unknown as ZodSchema;
425
474
 
426
475
  return makeParseableResponseTool<any>(
427
476
  {
@@ -435,7 +484,7 @@ export function zodResponsesFunction<Parameters extends ZodTypeLike>(options: {
435
484
  },
436
485
  {
437
486
  callback: options.function,
438
- parser: (args) => parseZodObject(options.parameters, args),
487
+ parser: (args) => parseZodObject(parameters, args),
439
488
  },
440
489
  );
441
490
  }
@@ -290,7 +290,7 @@ export function assertValidBedrockBearerCredential(credential: string): void {
290
290
  }
291
291
  }
292
292
 
293
- interface BedrockBearerSignalFailure {
293
+ interface BedrockAuthSignalFailure {
294
294
  error?: { value: unknown };
295
295
  removeListeners?: () => void;
296
296
  }
@@ -313,13 +313,13 @@ function removeBedrockAbortListener(signal: AbortSignal, listener: () => void):
313
313
  }
314
314
  }
315
315
 
316
- function resolveAbortableBedrockBearerToken(
317
- tokenProvider: ApiKeySetter,
316
+ function resolveAbortableBedrockAuth<T>(
317
+ operation: () => Promise<T>,
318
318
  signals: readonly AbortSignal[],
319
- failure: BedrockBearerSignalFailure,
320
- ): Promise<string> {
319
+ failure: BedrockAuthSignalFailure,
320
+ ): Promise<T> {
321
321
  // oxlint-disable-next-line promise/avoid-new -- AbortSignal events require a Promise callback bridge.
322
- return new Promise<string>((resolve, reject) => {
322
+ return new Promise<T>((resolve, reject) => {
323
323
  let settled = false;
324
324
  const listeners: { signal: AbortSignal; listener: () => void }[] = [];
325
325
 
@@ -333,13 +333,13 @@ function resolveAbortableBedrockBearerToken(
333
333
  };
334
334
  failure.removeListeners = removeListeners;
335
335
 
336
- const settle = (result: { token: string } | { error: unknown }) => {
336
+ const settle = (result: { value: T } | { error: unknown }) => {
337
337
  if (settled) {
338
338
  return;
339
339
  }
340
340
  settled = true;
341
- if ('token' in result) {
342
- resolve(result.token);
341
+ if ('value' in result) {
342
+ resolve(result.value);
343
343
  } else {
344
344
  removeListeners();
345
345
  reject(result.error);
@@ -399,26 +399,75 @@ function resolveAbortableBedrockBearerToken(
399
399
  }
400
400
  }
401
401
 
402
- let token: Promise<string>;
402
+ let pending: Promise<T>;
403
403
  try {
404
- token = tokenProvider();
404
+ pending = operation();
405
405
  } catch (error) {
406
406
  settle({ error });
407
407
  return;
408
408
  }
409
409
 
410
- const observeToken = async () => {
410
+ const observeResult = async () => {
411
411
  try {
412
- settle({ token: await token });
412
+ settle({ value: await pending });
413
413
  } catch (error) {
414
414
  settle({ error });
415
415
  }
416
416
  };
417
417
  // Observe the result even if the provider synchronously triggered cancellation.
418
- void observeToken();
418
+ void observeResult();
419
419
  });
420
420
  }
421
421
 
422
+ /**
423
+ * Resolves Bedrock authentication work with caller cancellation, then applies
424
+ * its result synchronously after the final cancellation checks.
425
+ *
426
+ * @internal
427
+ */
428
+ export async function prepareBedrockAuth<T>(
429
+ request: FinalizedRequestInit,
430
+ context: ProviderRequestContext,
431
+ operation: {
432
+ resolve: () => Promise<T>;
433
+ failureMessage: string;
434
+ apply: (value: T) => void;
435
+ },
436
+ ): Promise<void> {
437
+ const signals: AbortSignal[] = [];
438
+ for (const signal of [context.options.signal, request.signal]) {
439
+ if (signal != null && !signals.includes(signal)) {
440
+ signals.push(signal);
441
+ }
442
+ }
443
+ const signalFailure: BedrockAuthSignalFailure = {};
444
+ let value: T;
445
+ try {
446
+ try {
447
+ value =
448
+ signals.length > 0
449
+ ? await resolveAbortableBedrockAuth(operation.resolve, signals, signalFailure)
450
+ : await operation.resolve();
451
+ } catch (cause) {
452
+ if (signalFailure.error && Object.is(cause, signalFailure.error.value)) {
453
+ throw cause;
454
+ }
455
+ throw errorWithCause(operation.failureMessage, cause);
456
+ }
457
+ if (signalFailure.error) {
458
+ throw signalFailure.error.value;
459
+ }
460
+ for (const signal of signals) {
461
+ if (signal.aborted) {
462
+ throw createBedrockUserAbortError(signal);
463
+ }
464
+ }
465
+ } finally {
466
+ signalFailure.removeListeners?.();
467
+ }
468
+ operation.apply(value);
469
+ }
470
+
422
471
  class BedrockBearerAuth implements BedrockRequestAuth {
423
472
  private readonly tokenProvider: ApiKeySetter;
424
473
 
@@ -430,52 +479,29 @@ class BedrockBearerAuth implements BedrockRequestAuth {
430
479
  const headers = new Headers(request.headers);
431
480
  assertProviderOwnsAuthorization(headers);
432
481
 
433
- const signals: AbortSignal[] = [];
434
- for (const signal of [context.options.signal, request.signal]) {
435
- if (signal != null && !signals.includes(signal)) {
436
- signals.push(signal);
437
- }
438
- }
439
- const signalFailure: BedrockBearerSignalFailure = {};
440
- let token: unknown;
441
- try {
442
- try {
443
- token =
444
- signals.length > 0
445
- ? await resolveAbortableBedrockBearerToken(() => this.tokenProvider(), signals, signalFailure)
446
- : await this.tokenProvider();
447
- } catch (cause) {
448
- if (signalFailure.error && Object.is(cause, signalFailure.error.value)) {
449
- throw cause;
482
+ await prepareBedrockAuth(request, context, {
483
+ resolve: () => this.tokenProvider(),
484
+ failureMessage: 'Failed to resolve a bearer credential for Bedrock.',
485
+ apply: (token) => {
486
+ if (typeof token !== 'string' || !token.trim()) {
487
+ throw new Errors.OpenAIError(
488
+ 'The Bedrock bearer credential provider must return a non-empty string.',
489
+ );
450
490
  }
451
- throw errorWithCause('Failed to resolve a bearer credential for Bedrock.', cause);
452
- }
453
- if (signalFailure.error) {
454
- throw signalFailure.error.value;
455
- }
456
- for (const signal of signals) {
457
- if (signal.aborted) {
458
- throw createBedrockUserAbortError(signal);
491
+ assertValidBedrockBearerCredential(token);
492
+ try {
493
+ headers.set('authorization', `Bearer ${token}`);
494
+ } catch (error) {
495
+ if (error instanceof TypeError) {
496
+ // oxlint-disable-next-line eslint/preserve-caught-error -- The original error contains the bearer credential.
497
+ throw new TypeError('Bedrock bearer credential contains an invalid HTTP header value.');
498
+ }
499
+ throw error;
459
500
  }
460
- }
461
- } finally {
462
- signalFailure.removeListeners?.();
463
- }
464
- if (typeof token !== 'string' || !token.trim()) {
465
- throw new Errors.OpenAIError('The Bedrock bearer credential provider must return a non-empty string.');
466
- }
467
- assertValidBedrockBearerCredential(token);
468
- try {
469
- headers.set('authorization', `Bearer ${token}`);
470
- } catch (error) {
471
- if (error instanceof TypeError) {
472
- // oxlint-disable-next-line eslint/preserve-caught-error -- The original error contains the bearer credential.
473
- throw new TypeError('Bedrock bearer credential contains an invalid HTTP header value.');
474
- }
475
- throw error;
476
- }
477
- request.redirect = 'manual';
478
- request.headers = headers;
501
+ request.redirect = 'manual';
502
+ request.headers = headers;
503
+ },
504
+ });
479
505
  }
480
506
  }
481
507
 
@@ -75,7 +75,7 @@ const getPlatformProperties = (): PlatformProperties => {
75
75
  'X-Stainless-OS': 'Unknown',
76
76
  'X-Stainless-Arch': `other:${EdgeRuntime}`,
77
77
  'X-Stainless-Runtime': 'edge',
78
- 'X-Stainless-Runtime-Version': (globalThis as any).process.version,
78
+ 'X-Stainless-Runtime-Version': (globalThis as any).process?.version ?? 'unknown',
79
79
  };
80
80
  }
81
81
  // Check if Node.js
@@ -128,7 +128,7 @@ function getBrowserInfo(): BrowserInfo | null {
128
128
 
129
129
  // NOTE: The order matters here!
130
130
  const browserPatterns = [
131
- { key: 'edge' as const, pattern: /Edge(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
131
+ { key: 'edge' as const, pattern: /\bEdg(?:e|A|iOS)?\b(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
132
132
  { key: 'ie' as const, pattern: /MSIE(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
133
133
  { key: 'ie' as const, pattern: /Trident(?:.*rv\:(\d+)\.(\d+)(?:\.(\d+))?)?/ },
134
134
  { key: 'chrome' as const, pattern: /Chrome(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
@@ -14,11 +14,12 @@ export const castToError = (err: any): Error => {
14
14
  if (typeof err === 'object' && err !== null) {
15
15
  try {
16
16
  if (Object.prototype.toString.call(err) === '[object Error]') {
17
+ const hasCause = 'cause' in err;
17
18
  // @ts-ignore - not all envs have native support for cause yet
18
- const error = new Error(err.message, err.cause ? { cause: err.cause } : {});
19
+ const error = new Error(err.message, hasCause ? { cause: err.cause } : {});
19
20
  if (err.stack) error.stack = err.stack;
20
21
  // @ts-ignore - not all envs have native support for cause yet
21
- if (err.cause && !error.cause) error.cause = err.cause;
22
+ if (hasCause && !Object.prototype.hasOwnProperty.call(error, 'cause')) error.cause = err.cause;
22
23
  if (err.name) error.name = err.name;
23
24
  return error;
24
25
  }