@openrouter/sdk 0.3.1 → 0.3.7

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 (296) hide show
  1. package/esm/core.js +1 -0
  2. package/esm/funcs/analyticsGetUserActivity.js +1 -0
  3. package/esm/funcs/apiKeysCreate.js +1 -0
  4. package/esm/funcs/apiKeysDelete.js +1 -0
  5. package/esm/funcs/apiKeysGet.js +1 -0
  6. package/esm/funcs/apiKeysGetCurrentKeyMetadata.js +1 -0
  7. package/esm/funcs/apiKeysList.js +1 -0
  8. package/esm/funcs/apiKeysUpdate.js +1 -0
  9. package/esm/funcs/betaResponsesSend.js +1 -0
  10. package/esm/funcs/call-model.d.ts +33 -0
  11. package/esm/funcs/call-model.js +155 -0
  12. package/esm/funcs/chatSend.js +1 -0
  13. package/esm/funcs/completionsGenerate.js +1 -0
  14. package/esm/funcs/creditsCreateCoinbaseCharge.js +1 -0
  15. package/esm/funcs/creditsGetCredits.js +1 -0
  16. package/esm/funcs/embeddingsGenerate.js +1 -0
  17. package/esm/funcs/embeddingsListModels.js +1 -0
  18. package/esm/funcs/endpointsList.js +1 -0
  19. package/esm/funcs/endpointsListZdrEndpoints.js +1 -0
  20. package/esm/funcs/generationsGetGeneration.js +1 -0
  21. package/esm/funcs/modelsCount.js +1 -0
  22. package/esm/funcs/modelsList.js +1 -0
  23. package/esm/funcs/modelsListForUser.js +1 -0
  24. package/esm/funcs/oAuthCreateAuthCode.js +1 -0
  25. package/esm/funcs/oAuthExchangeAuthCodeForAPIKey.js +1 -0
  26. package/esm/funcs/parametersGetParameters.js +1 -0
  27. package/esm/funcs/providersList.js +1 -0
  28. package/esm/hooks/hooks.js +1 -0
  29. package/esm/hooks/index.js +1 -0
  30. package/esm/hooks/types.js +1 -0
  31. package/esm/index.d.ts +3 -0
  32. package/esm/index.js +4 -0
  33. package/esm/lib/anthropic-compat.d.ts +46 -0
  34. package/esm/lib/anthropic-compat.js +204 -0
  35. package/esm/lib/anthropic-compat.test.d.ts +2 -0
  36. package/esm/lib/anthropic-compat.test.js +479 -0
  37. package/esm/lib/base64.js +1 -0
  38. package/esm/lib/chat-compat.d.ts +46 -0
  39. package/esm/lib/chat-compat.js +108 -0
  40. package/esm/lib/chat-compat.test.d.ts +2 -0
  41. package/esm/lib/chat-compat.test.js +282 -0
  42. package/esm/lib/config.d.ts +3 -3
  43. package/esm/lib/config.js +4 -3
  44. package/esm/lib/dlv.js +1 -0
  45. package/esm/lib/encodings.js +1 -0
  46. package/esm/lib/env.js +1 -0
  47. package/esm/lib/event-streams.js +2 -1
  48. package/esm/lib/files.js +1 -0
  49. package/esm/lib/http.js +1 -0
  50. package/esm/lib/is-plain-object.js +1 -0
  51. package/esm/lib/logger.js +1 -0
  52. package/esm/lib/matchers.js +1 -0
  53. package/esm/lib/{response-wrapper.d.ts → model-result.d.ts} +19 -26
  54. package/esm/lib/{response-wrapper.js → model-result.js} +93 -83
  55. package/esm/lib/primitives.js +1 -0
  56. package/esm/lib/retries.js +1 -0
  57. package/esm/lib/schemas.js +1 -0
  58. package/esm/lib/sdks.js +1 -0
  59. package/esm/lib/security.js +1 -0
  60. package/esm/lib/stream-transformers.d.ts +27 -1
  61. package/esm/lib/stream-transformers.js +319 -1
  62. package/esm/lib/tool-types.d.ts +3 -3
  63. package/esm/lib/url.js +1 -0
  64. package/esm/models/activityitem.js +1 -0
  65. package/esm/models/assistantmessage.js +1 -0
  66. package/esm/models/badgatewayresponseerrordata.js +1 -0
  67. package/esm/models/badrequestresponseerrordata.js +1 -0
  68. package/esm/models/chatcompletionfinishreason.js +1 -0
  69. package/esm/models/chaterror.js +1 -0
  70. package/esm/models/chatgenerationparams.js +1 -0
  71. package/esm/models/chatgenerationtokenusage.js +1 -0
  72. package/esm/models/chatmessagecontentitem.js +1 -0
  73. package/esm/models/chatmessagecontentitemaudio.js +1 -0
  74. package/esm/models/chatmessagecontentitemcachecontrol.js +1 -0
  75. package/esm/models/chatmessagecontentitemimage.js +1 -0
  76. package/esm/models/chatmessagecontentitemtext.js +1 -0
  77. package/esm/models/chatmessagecontentitemvideo.js +1 -0
  78. package/esm/models/chatmessagetokenlogprob.js +1 -0
  79. package/esm/models/chatmessagetokenlogprobs.js +1 -0
  80. package/esm/models/chatmessagetoolcall.js +1 -0
  81. package/esm/models/chatresponse.js +1 -0
  82. package/esm/models/chatresponsechoice.js +1 -0
  83. package/esm/models/chatstreamingchoice.js +1 -0
  84. package/esm/models/chatstreamingmessagechunk.js +1 -0
  85. package/esm/models/chatstreamingmessagetoolcall.js +1 -0
  86. package/esm/models/chatstreamingresponsechunk.js +1 -0
  87. package/esm/models/chatstreamoptions.js +1 -0
  88. package/esm/models/claude-message.d.ts +218 -0
  89. package/esm/models/claude-message.js +6 -0
  90. package/esm/models/completionchoice.js +1 -0
  91. package/esm/models/completioncreateparams.js +1 -0
  92. package/esm/models/completionlogprobs.js +1 -0
  93. package/esm/models/completionresponse.js +1 -0
  94. package/esm/models/completionusage.js +1 -0
  95. package/esm/models/createchargerequest.js +1 -0
  96. package/esm/models/datacollection.js +1 -0
  97. package/esm/models/defaultparameters.js +1 -0
  98. package/esm/models/edgenetworktimeoutresponseerrordata.js +1 -0
  99. package/esm/models/endpointstatus.js +1 -0
  100. package/esm/models/errors/badgatewayresponseerror.js +1 -0
  101. package/esm/models/errors/badrequestresponseerror.js +1 -0
  102. package/esm/models/errors/chaterror.js +1 -0
  103. package/esm/models/errors/edgenetworktimeoutresponseerror.js +1 -0
  104. package/esm/models/errors/forbiddenresponseerror.js +1 -0
  105. package/esm/models/errors/httpclienterrors.js +1 -0
  106. package/esm/models/errors/index.js +1 -0
  107. package/esm/models/errors/internalserverresponseerror.js +1 -0
  108. package/esm/models/errors/notfoundresponseerror.js +1 -0
  109. package/esm/models/errors/openrouterdefaulterror.js +1 -0
  110. package/esm/models/errors/openroutererror.js +1 -0
  111. package/esm/models/errors/payloadtoolargeresponseerror.js +1 -0
  112. package/esm/models/errors/paymentrequiredresponseerror.js +1 -0
  113. package/esm/models/errors/provideroverloadedresponseerror.js +1 -0
  114. package/esm/models/errors/requesttimeoutresponseerror.js +1 -0
  115. package/esm/models/errors/responsevalidationerror.js +1 -0
  116. package/esm/models/errors/sdkvalidationerror.js +1 -0
  117. package/esm/models/errors/serviceunavailableresponseerror.js +1 -0
  118. package/esm/models/errors/toomanyrequestsresponseerror.js +1 -0
  119. package/esm/models/errors/unauthorizedresponseerror.js +1 -0
  120. package/esm/models/errors/unprocessableentityresponseerror.js +1 -0
  121. package/esm/models/filecitation.js +1 -0
  122. package/esm/models/filepath.js +1 -0
  123. package/esm/models/forbiddenresponseerrordata.js +1 -0
  124. package/esm/models/imagegenerationstatus.js +1 -0
  125. package/esm/models/index.d.ts +1 -0
  126. package/esm/models/index.js +2 -0
  127. package/esm/models/inputmodality.js +1 -0
  128. package/esm/models/instructtype.js +1 -0
  129. package/esm/models/internalserverresponseerrordata.js +1 -0
  130. package/esm/models/jsonschemaconfig.js +1 -0
  131. package/esm/models/listendpointsresponse.js +1 -0
  132. package/esm/models/message.js +1 -0
  133. package/esm/models/model.js +1 -0
  134. package/esm/models/modelarchitecture.js +1 -0
  135. package/esm/models/modelgroup.js +1 -0
  136. package/esm/models/modelscountresponse.js +1 -0
  137. package/esm/models/modelslistresponse.js +1 -0
  138. package/esm/models/namedtoolchoice.js +1 -0
  139. package/esm/models/notfoundresponseerrordata.js +1 -0
  140. package/esm/models/openairesponsesannotation.js +1 -0
  141. package/esm/models/openairesponsesincludable.js +1 -0
  142. package/esm/models/openairesponsesincompletedetails.js +1 -0
  143. package/esm/models/openairesponsesinputunion.js +1 -0
  144. package/esm/models/openairesponsesprompt.js +1 -0
  145. package/esm/models/openairesponsesreasoningconfig.js +1 -0
  146. package/esm/models/openairesponsesreasoningeffort.js +1 -0
  147. package/esm/models/openairesponsesrefusalcontent.js +1 -0
  148. package/esm/models/openairesponsesresponsestatus.js +1 -0
  149. package/esm/models/openairesponsesservicetier.js +1 -0
  150. package/esm/models/openairesponsestoolchoiceunion.js +1 -0
  151. package/esm/models/openairesponsestruncation.js +1 -0
  152. package/esm/models/openresponseseasyinputmessage.js +1 -0
  153. package/esm/models/openresponseserrorevent.js +1 -0
  154. package/esm/models/openresponsesfunctioncalloutput.js +1 -0
  155. package/esm/models/openresponsesfunctiontoolcall.js +1 -0
  156. package/esm/models/openresponsesimagegencallcompleted.js +1 -0
  157. package/esm/models/openresponsesimagegencallgenerating.js +1 -0
  158. package/esm/models/openresponsesimagegencallinprogress.js +1 -0
  159. package/esm/models/openresponsesimagegencallpartialimage.js +1 -0
  160. package/esm/models/openresponsesinput.js +1 -0
  161. package/esm/models/openresponsesinputmessageitem.js +1 -0
  162. package/esm/models/openresponseslogprobs.js +1 -0
  163. package/esm/models/openresponsesnonstreamingresponse.js +1 -0
  164. package/esm/models/openresponsesreasoning.js +1 -0
  165. package/esm/models/openresponsesreasoningconfig.js +1 -0
  166. package/esm/models/openresponsesreasoningdeltaevent.js +1 -0
  167. package/esm/models/openresponsesreasoningdoneevent.js +1 -0
  168. package/esm/models/openresponsesreasoningsummarypartaddedevent.js +1 -0
  169. package/esm/models/openresponsesreasoningsummarytextdeltaevent.js +1 -0
  170. package/esm/models/openresponsesreasoningsummarytextdoneevent.js +1 -0
  171. package/esm/models/openresponsesrequest.js +1 -0
  172. package/esm/models/openresponsesresponsetext.js +1 -0
  173. package/esm/models/openresponsesstreamevent.js +1 -0
  174. package/esm/models/openresponsestoplogprobs.js +1 -0
  175. package/esm/models/openresponsesusage.js +1 -0
  176. package/esm/models/openresponseswebsearch20250826tool.js +1 -0
  177. package/esm/models/openresponseswebsearchpreview20250311tool.js +1 -0
  178. package/esm/models/openresponseswebsearchpreviewtool.js +1 -0
  179. package/esm/models/openresponseswebsearchtool.js +1 -0
  180. package/esm/models/operations/createauthkeyscode.js +1 -0
  181. package/esm/models/operations/createcoinbasecharge.js +1 -0
  182. package/esm/models/operations/createembeddings.js +1 -0
  183. package/esm/models/operations/createkeys.js +1 -0
  184. package/esm/models/operations/createresponses.js +1 -0
  185. package/esm/models/operations/deletekeys.js +1 -0
  186. package/esm/models/operations/exchangeauthcodeforapikey.js +1 -0
  187. package/esm/models/operations/getcredits.js +1 -0
  188. package/esm/models/operations/getcurrentkey.js +1 -0
  189. package/esm/models/operations/getgeneration.js +1 -0
  190. package/esm/models/operations/getkey.js +1 -0
  191. package/esm/models/operations/getmodels.js +1 -0
  192. package/esm/models/operations/getparameters.js +1 -0
  193. package/esm/models/operations/getuseractivity.js +1 -0
  194. package/esm/models/operations/index.js +1 -0
  195. package/esm/models/operations/list.js +1 -0
  196. package/esm/models/operations/listendpoints.js +1 -0
  197. package/esm/models/operations/listendpointszdr.js +1 -0
  198. package/esm/models/operations/listmodelsuser.js +1 -0
  199. package/esm/models/operations/listproviders.js +1 -0
  200. package/esm/models/operations/sendchatcompletionrequest.js +1 -0
  201. package/esm/models/operations/updatekeys.js +1 -0
  202. package/esm/models/outputitemimagegenerationcall.d.ts +1 -1
  203. package/esm/models/outputitemimagegenerationcall.js +1 -0
  204. package/esm/models/outputmessage.js +1 -0
  205. package/esm/models/outputmodality.js +1 -0
  206. package/esm/models/parameter.js +1 -0
  207. package/esm/models/payloadtoolargeresponseerrordata.js +1 -0
  208. package/esm/models/paymentrequiredresponseerrordata.js +1 -0
  209. package/esm/models/pdfparserengine.js +1 -0
  210. package/esm/models/pdfparseroptions.js +1 -0
  211. package/esm/models/perrequestlimits.js +1 -0
  212. package/esm/models/providername.js +1 -0
  213. package/esm/models/provideroverloadedresponseerrordata.js +1 -0
  214. package/esm/models/providerpreferences.js +1 -0
  215. package/esm/models/providersort.js +1 -0
  216. package/esm/models/providersortconfig.js +1 -0
  217. package/esm/models/providersortunion.js +1 -0
  218. package/esm/models/publicendpoint.js +1 -0
  219. package/esm/models/publicpricing.js +1 -0
  220. package/esm/models/quantization.js +1 -0
  221. package/esm/models/reasoningsummarytext.js +1 -0
  222. package/esm/models/reasoningsummaryverbosity.js +1 -0
  223. package/esm/models/reasoningtextcontent.js +1 -0
  224. package/esm/models/requesttimeoutresponseerrordata.js +1 -0
  225. package/esm/models/responseformatjsonschema.js +1 -0
  226. package/esm/models/responseformattextconfig.js +1 -0
  227. package/esm/models/responseformattextgrammar.js +1 -0
  228. package/esm/models/responseinputaudio.js +1 -0
  229. package/esm/models/responseinputfile.js +1 -0
  230. package/esm/models/responseinputimage.js +1 -0
  231. package/esm/models/responseinputtext.js +1 -0
  232. package/esm/models/responseoutputtext.js +1 -0
  233. package/esm/models/responseserrorfield.js +1 -0
  234. package/esm/models/responsesformatjsonobject.js +1 -0
  235. package/esm/models/responsesformattext.js +1 -0
  236. package/esm/models/responsesformattextjsonschemaconfig.js +1 -0
  237. package/esm/models/responsesimagegenerationcall.js +1 -0
  238. package/esm/models/responsesoutputitem.js +1 -0
  239. package/esm/models/responsesoutputitemfilesearchcall.js +1 -0
  240. package/esm/models/responsesoutputitemfunctioncall.js +1 -0
  241. package/esm/models/responsesoutputitemreasoning.js +1 -0
  242. package/esm/models/responsesoutputmessage.js +1 -0
  243. package/esm/models/responsessearchcontextsize.js +1 -0
  244. package/esm/models/responseswebsearchcalloutput.js +1 -0
  245. package/esm/models/responseswebsearchuserlocation.js +1 -0
  246. package/esm/models/responsetextconfig.js +1 -0
  247. package/esm/models/schema0.js +1 -0
  248. package/esm/models/schema3.js +1 -0
  249. package/esm/models/security.js +1 -0
  250. package/esm/models/serviceunavailableresponseerrordata.js +1 -0
  251. package/esm/models/systemmessage.js +1 -0
  252. package/esm/models/toolcallstatus.js +1 -0
  253. package/esm/models/tooldefinitionjson.js +1 -0
  254. package/esm/models/toolresponsemessage.js +1 -0
  255. package/esm/models/toomanyrequestsresponseerrordata.js +1 -0
  256. package/esm/models/topproviderinfo.js +1 -0
  257. package/esm/models/unauthorizedresponseerrordata.js +1 -0
  258. package/esm/models/unprocessableentityresponseerrordata.js +1 -0
  259. package/esm/models/urlcitation.js +1 -0
  260. package/esm/models/usermessage.js +1 -0
  261. package/esm/models/websearchengine.js +1 -0
  262. package/esm/models/websearchpreviewtooluserlocation.js +1 -0
  263. package/esm/models/websearchstatus.js +1 -0
  264. package/esm/sdk/analytics.js +1 -0
  265. package/esm/sdk/apikeys.js +1 -0
  266. package/esm/sdk/beta.js +1 -0
  267. package/esm/sdk/chat.js +1 -0
  268. package/esm/sdk/completions.js +1 -0
  269. package/esm/sdk/credits.js +1 -0
  270. package/esm/sdk/embeddings.js +1 -0
  271. package/esm/sdk/endpoints.js +1 -0
  272. package/esm/sdk/generations.js +1 -0
  273. package/esm/sdk/index.js +1 -0
  274. package/esm/sdk/models.js +1 -0
  275. package/esm/sdk/oauth.js +1 -0
  276. package/esm/sdk/parameters.js +1 -0
  277. package/esm/sdk/providers.js +1 -0
  278. package/esm/sdk/responses.js +1 -0
  279. package/esm/sdk/sdk.d.ts +3 -3
  280. package/esm/sdk/sdk.js +2 -1
  281. package/esm/types/async.js +1 -0
  282. package/esm/types/blobs.js +1 -0
  283. package/esm/types/constdatetime.js +1 -0
  284. package/esm/types/discriminatedUnion.js +1 -0
  285. package/esm/types/enums.js +1 -0
  286. package/esm/types/fp.js +1 -0
  287. package/esm/types/index.js +1 -0
  288. package/esm/types/operations.js +1 -0
  289. package/esm/types/rfcdate.js +1 -0
  290. package/esm/types/streams.js +1 -0
  291. package/esm/types/unrecognized.js +1 -0
  292. package/jsr.json +1 -1
  293. package/package.json +1 -1
  294. package/tsconfig.json +1 -1
  295. package/esm/funcs/callModel.d.ts +0 -87
  296. package/esm/funcs/callModel.js +0 -215
@@ -1,26 +1,60 @@
1
- import { betaResponsesSend } from '../funcs/betaResponsesSend.js';
2
- import { ReusableReadableStream } from './reusable-stream.js';
3
- import { buildMessageStream, buildToolCallStream, consumeStreamForCompletion, extractMessageFromResponse, extractReasoningDeltas, extractTextDeltas, extractTextFromResponse, extractToolCallsFromResponse, extractToolDeltas, } from './stream-transformers.js';
4
- import { executeTool } from './tool-executor.js';
5
- import { hasExecuteFunction } from './tool-types.js';
1
+ import { betaResponsesSend } from "../funcs/betaResponsesSend.js";
2
+ import { ReusableReadableStream } from "./reusable-stream.js";
3
+ import { buildResponsesMessageStream, buildToolCallStream, consumeStreamForCompletion, extractReasoningDeltas, extractResponsesMessageFromResponse, extractTextDeltas, extractTextFromResponse, extractToolCallsFromResponse, extractToolDeltas, } from "./stream-transformers.js";
4
+ import { executeTool } from "./tool-executor.js";
5
+ import { hasExecuteFunction } from "./tool-types.js";
6
+ /**
7
+ * Type guard for stream event with toReadableStream method
8
+ */
9
+ function isEventStream(value) {
10
+ return (value !== null &&
11
+ typeof value === "object" &&
12
+ "toReadableStream" in value &&
13
+ typeof value.toReadableStream ===
14
+ "function");
15
+ }
16
+ /**
17
+ * Type guard for response.output_text.delta events
18
+ */
19
+ function isOutputTextDeltaEvent(event) {
20
+ return "type" in event && event.type === "response.output_text.delta";
21
+ }
22
+ /**
23
+ * Type guard for response.completed events
24
+ */
25
+ function isResponseCompletedEvent(event) {
26
+ return "type" in event && event.type === "response.completed";
27
+ }
28
+ /**
29
+ * Type guard for output items with a type property
30
+ */
31
+ function hasTypeProperty(item) {
32
+ return (typeof item === "object" &&
33
+ item !== null &&
34
+ "type" in item &&
35
+ typeof item.type === "string");
36
+ }
6
37
  /**
7
38
  * A wrapper around a streaming response that provides multiple consumption patterns.
8
39
  *
9
40
  * Allows consuming the response in multiple ways:
10
- * - `await response.getMessage()` - Get the completed message
11
- * - `await response.getText()` - Get just the text
12
- * - `for await (const delta of response.getTextStream())` - Stream text deltas
13
- * - `for await (const msg of response.getNewMessagesStream())` - Stream incremental message updates
14
- * - `for await (const event of response.getFullResponsesStream())` - Stream all response events
41
+ * - `await result.getText()` - Get just the text
42
+ * - `await result.getResponse()` - Get the full response object
43
+ * - `for await (const delta of result.getTextStream())` - Stream text deltas
44
+ * - `for await (const msg of result.getNewMessagesStream())` - Stream incremental message updates
45
+ * - `for await (const event of result.getFullResponsesStream())` - Stream all response events
46
+ *
47
+ * For message format conversion, use the helper functions:
48
+ * - `toChatMessage(response)` for OpenAI chat format
49
+ * - `toClaudeMessage(response)` for Anthropic Claude format
15
50
  *
16
51
  * All consumption patterns can be used concurrently thanks to the underlying
17
52
  * ReusableReadableStream implementation.
18
53
  */
19
- export class ResponseWrapper {
54
+ export class ModelResult {
20
55
  constructor(options) {
21
56
  this.reusableStream = null;
22
57
  this.streamPromise = null;
23
- this.messagePromise = null;
24
58
  this.textPromise = null;
25
59
  this.initPromise = null;
26
60
  this.toolExecutionPromise = null;
@@ -78,14 +112,14 @@ export class ResponseWrapper {
78
112
  this.toolExecutionPromise = (async () => {
79
113
  await this.initStream();
80
114
  if (!this.reusableStream) {
81
- throw new Error('Stream not initialized');
115
+ throw new Error("Stream not initialized");
82
116
  }
83
117
  // Get the initial response
84
118
  const initialResponse = await consumeStreamForCompletion(this.reusableStream);
85
119
  // Check if we have tools and if auto-execution is enabled
86
120
  const shouldAutoExecute = this.options.tools &&
87
121
  this.options.tools.length > 0 &&
88
- initialResponse.output.some((item) => 'type' in item && item.type === 'function_call');
122
+ initialResponse.output.some((item) => hasTypeProperty(item) && item.type === "function_call");
89
123
  if (!shouldAutoExecute) {
90
124
  // No tools to execute, use initial response
91
125
  this.finalResponse = initialResponse;
@@ -121,12 +155,12 @@ export class ResponseWrapper {
121
155
  break;
122
156
  }
123
157
  // Check if we should continue based on maxToolRounds
124
- if (typeof maxToolRounds === 'number') {
158
+ if (typeof maxToolRounds === "number") {
125
159
  if (currentRound >= maxToolRounds) {
126
160
  break;
127
161
  }
128
162
  }
129
- else if (typeof maxToolRounds === 'function') {
163
+ else if (typeof maxToolRounds === "function") {
130
164
  // Function signature: (context: TurnContext) => boolean
131
165
  const turnContext = {
132
166
  numberOfTurns: currentRound + 1,
@@ -169,11 +203,12 @@ export class ResponseWrapper {
169
203
  }
170
204
  const result = await executeTool(tool, toolCall, turnContext);
171
205
  // Store preliminary results
172
- if (result.preliminaryResults && result.preliminaryResults.length > 0) {
206
+ if (result.preliminaryResults &&
207
+ result.preliminaryResults.length > 0) {
173
208
  this.preliminaryResults.set(toolCall.id, result.preliminaryResults);
174
209
  }
175
210
  toolResults.push({
176
- type: 'function_call_output',
211
+ type: "function_call_output",
177
212
  id: `output_${toolCall.id}`,
178
213
  callId: toolCall.id,
179
214
  output: result.error
@@ -188,9 +223,7 @@ export class ResponseWrapper {
188
223
  const newInput = [
189
224
  ...(Array.isArray(currentResponse.output)
190
225
  ? currentResponse.output
191
- : [
192
- currentResponse.output,
193
- ]),
226
+ : [currentResponse.output]),
194
227
  ...toolResults,
195
228
  ];
196
229
  // Update current input for next iteration
@@ -207,7 +240,7 @@ export class ResponseWrapper {
207
240
  }
208
241
  // Handle the result - it might be a stream or a response
209
242
  const value = newResult.value;
210
- if (value && typeof value === 'object' && 'toReadableStream' in value) {
243
+ if (isEventStream(value)) {
211
244
  // It's a stream, consume it
212
245
  const stream = new ReusableReadableStream(value);
213
246
  currentResponse = await consumeStreamForCompletion(stream);
@@ -225,23 +258,14 @@ export class ResponseWrapper {
225
258
  throw new Error("Invalid final response: missing required fields");
226
259
  }
227
260
  // Ensure the response is in a completed state (has output content)
228
- if (!Array.isArray(currentResponse.output) || currentResponse.output.length === 0) {
261
+ if (!Array.isArray(currentResponse.output) ||
262
+ currentResponse.output.length === 0) {
229
263
  throw new Error("Invalid final response: empty or invalid output");
230
264
  }
231
265
  this.finalResponse = currentResponse;
232
266
  })();
233
267
  return this.toolExecutionPromise;
234
268
  }
235
- /**
236
- * Internal helper to get the message after tool execution
237
- */
238
- async getMessageInternal() {
239
- await this.executeToolsIfNeeded();
240
- if (!this.finalResponse) {
241
- throw new Error("Response not available");
242
- }
243
- return extractMessageFromResponse(this.finalResponse);
244
- }
245
269
  /**
246
270
  * Internal helper to get the text after tool execution
247
271
  */
@@ -252,18 +276,6 @@ export class ResponseWrapper {
252
276
  }
253
277
  return extractTextFromResponse(this.finalResponse);
254
278
  }
255
- /**
256
- * Get the completed message from the response.
257
- * This will consume the stream until completion, execute any tools, and extract the first message.
258
- * Returns an AssistantMessage in chat format.
259
- */
260
- getMessage() {
261
- if (this.messagePromise) {
262
- return this.messagePromise;
263
- }
264
- this.messagePromise = this.getMessageInternal();
265
- return this.messagePromise;
266
- }
267
279
  /**
268
280
  * Get just the text content from the response.
269
281
  * This will consume the stream until completion, execute any tools, and extract the text.
@@ -283,7 +295,7 @@ export class ResponseWrapper {
283
295
  async getResponse() {
284
296
  await this.executeToolsIfNeeded();
285
297
  if (!this.finalResponse) {
286
- throw new Error('Response not available');
298
+ throw new Error("Response not available");
287
299
  }
288
300
  return this.finalResponse;
289
301
  }
@@ -296,7 +308,7 @@ export class ResponseWrapper {
296
308
  return async function* () {
297
309
  await this.initStream();
298
310
  if (!this.reusableStream) {
299
- throw new Error('Stream not initialized');
311
+ throw new Error("Stream not initialized");
300
312
  }
301
313
  const consumer = this.reusableStream.createConsumer();
302
314
  // Yield original events directly
@@ -309,7 +321,7 @@ export class ResponseWrapper {
309
321
  for (const [toolCallId, results] of this.preliminaryResults) {
310
322
  for (const result of results) {
311
323
  yield {
312
- type: 'tool.preliminary_result',
324
+ type: "tool.preliminary_result",
313
325
  toolCallId,
314
326
  result,
315
327
  timestamp: Date.now(),
@@ -326,28 +338,28 @@ export class ResponseWrapper {
326
338
  return async function* () {
327
339
  await this.initStream();
328
340
  if (!this.reusableStream) {
329
- throw new Error('Stream not initialized');
341
+ throw new Error("Stream not initialized");
330
342
  }
331
343
  yield* extractTextDeltas(this.reusableStream);
332
344
  }.call(this);
333
345
  }
334
346
  /**
335
- * Stream incremental message updates as content is added.
347
+ * Stream incremental message updates as content is added in responses format.
336
348
  * Each iteration yields an updated version of the message with new content.
337
- * Also yields ToolResponseMessages after tool execution completes.
338
- * Returns AssistantMessage or ToolResponseMessage in chat format.
349
+ * Also yields OpenResponsesFunctionCallOutput after tool execution completes.
350
+ * Returns ResponsesOutputMessage or OpenResponsesFunctionCallOutput compatible with OpenAI Responses API format.
339
351
  */
340
352
  getNewMessagesStream() {
341
353
  return async function* () {
342
354
  await this.initStream();
343
355
  if (!this.reusableStream) {
344
- throw new Error('Stream not initialized');
356
+ throw new Error("Stream not initialized");
345
357
  }
346
- // First yield assistant messages from the stream
347
- yield* buildMessageStream(this.reusableStream);
358
+ // First yield messages from the stream in responses format
359
+ yield* buildResponsesMessageStream(this.reusableStream);
348
360
  // Execute tools if needed
349
361
  await this.executeToolsIfNeeded();
350
- // Yield tool response messages for each executed tool
362
+ // Yield function call output for each executed tool
351
363
  for (const round of this.allToolExecutionRounds) {
352
364
  for (const toolCall of round.toolCalls) {
353
365
  // Find the tool to check if it was executed
@@ -360,20 +372,21 @@ export class ResponseWrapper {
360
372
  const result = prelimResults && prelimResults.length > 0
361
373
  ? prelimResults[prelimResults.length - 1] // Last result is the final output
362
374
  : undefined;
363
- // Yield tool response message
375
+ // Yield function call output in responses format
364
376
  yield {
365
- role: 'tool',
366
- content: result !== undefined ? JSON.stringify(result) : '',
367
- toolCallId: toolCall.id,
377
+ type: "function_call_output",
378
+ id: `output_${toolCall.id}`,
379
+ callId: toolCall.id,
380
+ output: result !== undefined ? JSON.stringify(result) : "",
368
381
  };
369
382
  }
370
383
  }
371
- // If tools were executed, yield the final assistant message (if there is one)
384
+ // If tools were executed, yield the final message (if there is one)
372
385
  if (this.finalResponse && this.allToolExecutionRounds.length > 0) {
373
386
  // Check if the final response contains a message
374
- const hasMessage = this.finalResponse.output.some((item) => 'type' in item && item.type === 'message');
387
+ const hasMessage = this.finalResponse.output.some((item) => hasTypeProperty(item) && item.type === "message");
375
388
  if (hasMessage) {
376
- yield extractMessageFromResponse(this.finalResponse);
389
+ yield extractResponsesMessageFromResponse(this.finalResponse);
377
390
  }
378
391
  }
379
392
  }.call(this);
@@ -386,7 +399,7 @@ export class ResponseWrapper {
386
399
  return async function* () {
387
400
  await this.initStream();
388
401
  if (!this.reusableStream) {
389
- throw new Error('Stream not initialized');
402
+ throw new Error("Stream not initialized");
390
403
  }
391
404
  yield* extractReasoningDeltas(this.reusableStream);
392
405
  }.call(this);
@@ -401,12 +414,12 @@ export class ResponseWrapper {
401
414
  return async function* () {
402
415
  await this.initStream();
403
416
  if (!this.reusableStream) {
404
- throw new Error('Stream not initialized');
417
+ throw new Error("Stream not initialized");
405
418
  }
406
419
  // Yield tool deltas as structured events
407
420
  for await (const delta of extractToolDeltas(this.reusableStream)) {
408
421
  yield {
409
- type: 'delta',
422
+ type: "delta",
410
423
  content: delta,
411
424
  };
412
425
  }
@@ -416,7 +429,7 @@ export class ResponseWrapper {
416
429
  for (const [toolCallId, results] of this.preliminaryResults) {
417
430
  for (const result of results) {
418
431
  yield {
419
- type: 'preliminary_result',
432
+ type: "preliminary_result",
420
433
  toolCallId,
421
434
  result,
422
435
  };
@@ -438,27 +451,24 @@ export class ResponseWrapper {
438
451
  return async function* () {
439
452
  await this.initStream();
440
453
  if (!this.reusableStream) {
441
- throw new Error('Stream not initialized');
454
+ throw new Error("Stream not initialized");
442
455
  }
443
456
  const consumer = this.reusableStream.createConsumer();
444
457
  for await (const event of consumer) {
445
- if (!('type' in event)) {
458
+ if (!("type" in event)) {
446
459
  continue;
447
460
  }
448
- // Transform responses events to chat-like format
449
- // This is a simplified transformation - you may need to adjust based on your needs
450
- if (event.type === 'response.output_text.delta') {
451
- const deltaEvent = event;
461
+ // Transform responses events to chat-like format using type guards
462
+ if (isOutputTextDeltaEvent(event)) {
452
463
  yield {
453
- type: 'content.delta',
454
- delta: deltaEvent.delta,
464
+ type: "content.delta",
465
+ delta: event.delta,
455
466
  };
456
467
  }
457
- else if (event.type === 'response.completed') {
458
- const completedEvent = event;
468
+ else if (isResponseCompletedEvent(event)) {
459
469
  yield {
460
- type: 'message.complete',
461
- response: completedEvent.response,
470
+ type: "message.complete",
471
+ response: event.response,
462
472
  };
463
473
  }
464
474
  else {
@@ -475,7 +485,7 @@ export class ResponseWrapper {
475
485
  for (const [toolCallId, results] of this.preliminaryResults) {
476
486
  for (const result of results) {
477
487
  yield {
478
- type: 'tool.preliminary_result',
488
+ type: "tool.preliminary_result",
479
489
  toolCallId,
480
490
  result,
481
491
  };
@@ -492,7 +502,7 @@ export class ResponseWrapper {
492
502
  async getToolCalls() {
493
503
  await this.initStream();
494
504
  if (!this.reusableStream) {
495
- throw new Error('Stream not initialized');
505
+ throw new Error("Stream not initialized");
496
506
  }
497
507
  const completedResponse = await consumeStreamForCompletion(this.reusableStream);
498
508
  return extractToolCallsFromResponse(completedResponse);
@@ -505,7 +515,7 @@ export class ResponseWrapper {
505
515
  return async function* () {
506
516
  await this.initStream();
507
517
  if (!this.reusableStream) {
508
- throw new Error('Stream not initialized');
518
+ throw new Error("Stream not initialized");
509
519
  }
510
520
  yield* buildToolCallStream(this.reusableStream);
511
521
  }.call(this);
@@ -519,4 +529,4 @@ export class ResponseWrapper {
519
529
  }
520
530
  }
521
531
  }
522
- //# sourceMappingURL=response-wrapper.js.map
532
+ //# sourceMappingURL=model-result.js.map
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 74859f750b28
3
4
  */
4
5
  class InvariantError extends Error {
5
6
  constructor(message) {
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: b24a69d15639
3
4
  */
4
5
  import { isConnectionError, isTimeoutError } from "./http.js";
5
6
  const defaultBackoff = {
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: deb4b531fae1
3
4
  */
4
5
  import * as z from "zod/v4";
5
6
  import { SDKValidationError } from "../models/errors/sdkvalidationerror.js";
package/esm/lib/sdks.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 8a6d91f1218d
3
4
  */
4
5
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
5
6
  if (kind === "m") throw new TypeError("Private method is not writable");
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 0502afa7922e
3
4
  */
4
5
  import { env } from "./env.js";
5
6
  export var SecurityErrorCode;
@@ -13,6 +13,11 @@ export declare function extractReasoningDeltas(stream: ReusableReadableStream<mo
13
13
  * Extract tool call argument deltas from responses stream events
14
14
  */
15
15
  export declare function extractToolDeltas(stream: ReusableReadableStream<models.OpenResponsesStreamEvent>): AsyncIterableIterator<string>;
16
+ /**
17
+ * Build incremental message updates from responses stream events
18
+ * Returns ResponsesOutputMessage (assistant/responses format)
19
+ */
20
+ export declare function buildResponsesMessageStream(stream: ReusableReadableStream<models.OpenResponsesStreamEvent>): AsyncIterableIterator<models.ResponsesOutputMessage>;
16
21
  /**
17
22
  * Build incremental message updates from responses stream events
18
23
  * Returns AssistantMessage (chat format) instead of ResponsesOutputMessage
@@ -23,9 +28,13 @@ export declare function buildMessageStream(stream: ReusableReadableStream<models
23
28
  */
24
29
  export declare function consumeStreamForCompletion(stream: ReusableReadableStream<models.OpenResponsesStreamEvent>): Promise<models.OpenResponsesNonStreamingResponse>;
25
30
  /**
26
- * Extract the first message from a completed response
31
+ * Extract the first message from a completed response (chat format)
27
32
  */
28
33
  export declare function extractMessageFromResponse(response: models.OpenResponsesNonStreamingResponse): models.AssistantMessage;
34
+ /**
35
+ * Extract the first message from a completed response (responses format)
36
+ */
37
+ export declare function extractResponsesMessageFromResponse(response: models.OpenResponsesNonStreamingResponse): models.ResponsesOutputMessage;
29
38
  /**
30
39
  * Extract text from a response, either from outputText or by concatenating message content
31
40
  */
@@ -44,4 +53,21 @@ export declare function buildToolCallStream(stream: ReusableReadableStream<model
44
53
  * Check if a response contains any tool calls
45
54
  */
46
55
  export declare function responseHasToolCalls(response: models.OpenResponsesNonStreamingResponse): boolean;
56
+ /**
57
+ * Convert OpenResponsesNonStreamingResponse to ClaudeMessage format
58
+ * Compatible with the Anthropic SDK BetaMessage type
59
+ */
60
+ export declare function convertToClaudeMessage(response: models.OpenResponsesNonStreamingResponse): models.ClaudeMessage;
61
+ /**
62
+ * Extract unsupported content by original type
63
+ */
64
+ export declare function extractUnsupportedContent(message: models.ClaudeMessage, originalType: string): models.UnsupportedContent[];
65
+ /**
66
+ * Check if message has any unsupported content
67
+ */
68
+ export declare function hasUnsupportedContent(message: models.ClaudeMessage): boolean;
69
+ /**
70
+ * Get summary of unsupported content types
71
+ */
72
+ export declare function getUnsupportedContentSummary(message: models.ClaudeMessage): Record<string, number>;
47
73
  //# sourceMappingURL=stream-transformers.d.ts.map