@openrouter/sdk 0.3.2 → 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 (294) 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 +2 -2
  43. package/esm/lib/config.js +3 -2
  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 +1 -0
  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.js +1 -0
  203. package/esm/models/outputmessage.js +1 -0
  204. package/esm/models/outputmodality.js +1 -0
  205. package/esm/models/parameter.js +1 -0
  206. package/esm/models/payloadtoolargeresponseerrordata.js +1 -0
  207. package/esm/models/paymentrequiredresponseerrordata.js +1 -0
  208. package/esm/models/pdfparserengine.js +1 -0
  209. package/esm/models/pdfparseroptions.js +1 -0
  210. package/esm/models/perrequestlimits.js +1 -0
  211. package/esm/models/providername.js +1 -0
  212. package/esm/models/provideroverloadedresponseerrordata.js +1 -0
  213. package/esm/models/providerpreferences.js +1 -0
  214. package/esm/models/providersort.js +1 -0
  215. package/esm/models/providersortconfig.js +1 -0
  216. package/esm/models/providersortunion.js +1 -0
  217. package/esm/models/publicendpoint.js +1 -0
  218. package/esm/models/publicpricing.js +1 -0
  219. package/esm/models/quantization.js +1 -0
  220. package/esm/models/reasoningsummarytext.js +1 -0
  221. package/esm/models/reasoningsummaryverbosity.js +1 -0
  222. package/esm/models/reasoningtextcontent.js +1 -0
  223. package/esm/models/requesttimeoutresponseerrordata.js +1 -0
  224. package/esm/models/responseformatjsonschema.js +1 -0
  225. package/esm/models/responseformattextconfig.js +1 -0
  226. package/esm/models/responseformattextgrammar.js +1 -0
  227. package/esm/models/responseinputaudio.js +1 -0
  228. package/esm/models/responseinputfile.js +1 -0
  229. package/esm/models/responseinputimage.js +1 -0
  230. package/esm/models/responseinputtext.js +1 -0
  231. package/esm/models/responseoutputtext.js +1 -0
  232. package/esm/models/responseserrorfield.js +1 -0
  233. package/esm/models/responsesformatjsonobject.js +1 -0
  234. package/esm/models/responsesformattext.js +1 -0
  235. package/esm/models/responsesformattextjsonschemaconfig.js +1 -0
  236. package/esm/models/responsesimagegenerationcall.js +1 -0
  237. package/esm/models/responsesoutputitem.js +1 -0
  238. package/esm/models/responsesoutputitemfilesearchcall.js +1 -0
  239. package/esm/models/responsesoutputitemfunctioncall.js +1 -0
  240. package/esm/models/responsesoutputitemreasoning.js +1 -0
  241. package/esm/models/responsesoutputmessage.js +1 -0
  242. package/esm/models/responsessearchcontextsize.js +1 -0
  243. package/esm/models/responseswebsearchcalloutput.js +1 -0
  244. package/esm/models/responseswebsearchuserlocation.js +1 -0
  245. package/esm/models/responsetextconfig.js +1 -0
  246. package/esm/models/schema0.js +1 -0
  247. package/esm/models/schema3.js +1 -0
  248. package/esm/models/security.js +1 -0
  249. package/esm/models/serviceunavailableresponseerrordata.js +1 -0
  250. package/esm/models/systemmessage.js +1 -0
  251. package/esm/models/toolcallstatus.js +1 -0
  252. package/esm/models/tooldefinitionjson.js +1 -0
  253. package/esm/models/toolresponsemessage.js +1 -0
  254. package/esm/models/toomanyrequestsresponseerrordata.js +1 -0
  255. package/esm/models/topproviderinfo.js +1 -0
  256. package/esm/models/unauthorizedresponseerrordata.js +1 -0
  257. package/esm/models/unprocessableentityresponseerrordata.js +1 -0
  258. package/esm/models/urlcitation.js +1 -0
  259. package/esm/models/usermessage.js +1 -0
  260. package/esm/models/websearchengine.js +1 -0
  261. package/esm/models/websearchpreviewtooluserlocation.js +1 -0
  262. package/esm/models/websearchstatus.js +1 -0
  263. package/esm/sdk/analytics.js +1 -0
  264. package/esm/sdk/apikeys.js +1 -0
  265. package/esm/sdk/beta.js +1 -0
  266. package/esm/sdk/chat.js +1 -0
  267. package/esm/sdk/completions.js +1 -0
  268. package/esm/sdk/credits.js +1 -0
  269. package/esm/sdk/embeddings.js +1 -0
  270. package/esm/sdk/endpoints.js +1 -0
  271. package/esm/sdk/generations.js +1 -0
  272. package/esm/sdk/index.js +1 -0
  273. package/esm/sdk/models.js +1 -0
  274. package/esm/sdk/oauth.js +1 -0
  275. package/esm/sdk/parameters.js +1 -0
  276. package/esm/sdk/providers.js +1 -0
  277. package/esm/sdk/responses.js +1 -0
  278. package/esm/sdk/sdk.d.ts +3 -3
  279. package/esm/sdk/sdk.js +2 -1
  280. package/esm/types/async.js +1 -0
  281. package/esm/types/blobs.js +1 -0
  282. package/esm/types/constdatetime.js +1 -0
  283. package/esm/types/discriminatedUnion.js +1 -0
  284. package/esm/types/enums.js +1 -0
  285. package/esm/types/fp.js +1 -0
  286. package/esm/types/index.js +1 -0
  287. package/esm/types/operations.js +1 -0
  288. package/esm/types/rfcdate.js +1 -0
  289. package/esm/types/streams.js +1 -0
  290. package/esm/types/unrecognized.js +1 -0
  291. package/jsr.json +1 -1
  292. package/package.json +1 -1
  293. package/esm/funcs/callModel.d.ts +0 -87
  294. package/esm/funcs/callModel.js +0 -215
package/esm/core.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: f431fdbcd144
3
4
  */
4
5
  import { ClientSDK } from "./lib/sdks.js";
5
6
  /**
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 6525bcc66ab5
3
4
  */
4
5
  import { encodeFormQuery } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 133e2c5f487c
3
4
  */
4
5
  import { encodeJSON } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: c151c399f75f
3
4
  */
4
5
  import { encodeSimple } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 12a5c2abbddd
3
4
  */
4
5
  import { encodeSimple } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: d9449cb31931
3
4
  */
4
5
  import * as M from "../lib/matchers.js";
5
6
  import { compactMap } from "../lib/primitives.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 2afc8d8f1ce0
3
4
  */
4
5
  import { encodeFormQuery } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 05700884934b
3
4
  */
4
5
  import { encodeJSON, encodeSimple } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: c5c4ab0c3f76
3
4
  */
4
5
  import { encodeJSON } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -0,0 +1,33 @@
1
+ import type { OpenRouterCore } from "../core.js";
2
+ import type { RequestOptions } from "../lib/sdks.js";
3
+ import type { Tool, MaxToolRounds } from "../lib/tool-types.js";
4
+ import type * as models from "../models/index.js";
5
+ import { ModelResult } from "../lib/model-result.js";
6
+ /**
7
+ * Get a response with multiple consumption patterns
8
+ *
9
+ * @remarks
10
+ * Creates a response using the OpenResponses API and returns
11
+ * a wrapper that allows consuming the response in multiple ways:
12
+ *
13
+ * - `await response.getText()` - Get just the text content (tools auto-executed)
14
+ * - `await response.getResponse()` - Get full response with usage data (inputTokens, cachedTokens, etc.)
15
+ * - `for await (const delta of response.getTextStream())` - Stream text deltas
16
+ * - `for await (const delta of response.getReasoningStream())` - Stream reasoning deltas
17
+ * - `for await (const event of response.getToolStream())` - Stream tool events (incl. preliminary results)
18
+ * - `for await (const toolCall of response.getToolCallsStream())` - Stream structured tool calls
19
+ * - `await response.getToolCalls()` - Get all tool calls from completed response
20
+ * - `for await (const msg of response.getNewMessagesStream())` - Stream incremental message updates
21
+ * - `for await (const event of response.getFullResponsesStream())` - Stream all events (incl. tool preliminary)
22
+ *
23
+ * All consumption patterns can be used concurrently on the same response.
24
+ *
25
+ * For message format conversion, use the helper functions:
26
+ * - `fromChatMessages()` / `toChatMessage()` for OpenAI chat format
27
+ * - `fromClaudeMessages()` / `toClaudeMessage()` for Anthropic Claude format
28
+ */
29
+ export declare function callModel(client: OpenRouterCore, request: Omit<models.OpenResponsesRequest, "stream" | "tools"> & {
30
+ tools?: Tool[];
31
+ maxToolRounds?: MaxToolRounds;
32
+ }, options?: RequestOptions): ModelResult;
33
+ //# sourceMappingURL=call-model.d.ts.map
@@ -0,0 +1,155 @@
1
+ import { ModelResult } from "../lib/model-result.js";
2
+ import { convertToolsToAPIFormat } from "../lib/tool-executor.js";
3
+ /**
4
+ * Checks if a message looks like a Claude-style message
5
+ */
6
+ function isClaudeStyleMessage(msg) {
7
+ if (!msg || typeof msg !== 'object')
8
+ return false;
9
+ // Check if it has a role field that's user or assistant
10
+ const role = msg.role;
11
+ if (role !== 'user' && role !== 'assistant')
12
+ return false;
13
+ // Check if content is an array with Claude-style content blocks
14
+ if (Array.isArray(msg.content)) {
15
+ return msg.content.some((block) => block &&
16
+ typeof block === 'object' &&
17
+ block.type &&
18
+ // Claude content block types (not OpenRouter types)
19
+ (block.type === 'text' || block.type === 'image' || block.type === 'tool_use' || block.type === 'tool_result'));
20
+ }
21
+ return false;
22
+ }
23
+ /**
24
+ * Converts Claude-style content blocks to OpenRouter format
25
+ */
26
+ function convertClaudeContentBlock(block) {
27
+ if (!block || typeof block !== 'object' || !('type' in block)) {
28
+ return null;
29
+ }
30
+ switch (block.type) {
31
+ case 'text': {
32
+ const textBlock = block;
33
+ return {
34
+ type: 'input_text',
35
+ text: textBlock.text,
36
+ };
37
+ }
38
+ case 'image': {
39
+ const imageBlock = block;
40
+ if (imageBlock.source.type === 'url') {
41
+ return {
42
+ type: 'input_image',
43
+ detail: 'auto',
44
+ imageUrl: imageBlock.source.url,
45
+ };
46
+ }
47
+ else if (imageBlock.source.type === 'base64') {
48
+ const dataUri = `data:${imageBlock.source.media_type};base64,${imageBlock.source.data}`;
49
+ return {
50
+ type: 'input_image',
51
+ detail: 'auto',
52
+ imageUrl: dataUri,
53
+ };
54
+ }
55
+ return null;
56
+ }
57
+ case 'tool_use':
58
+ case 'tool_result':
59
+ // tool_use and tool_result are not handled here as they map to different input types
60
+ return null;
61
+ default:
62
+ return null;
63
+ }
64
+ }
65
+ /**
66
+ * Converts a Claude-style message to OpenRouter EasyInputMessage format
67
+ */
68
+ function convertClaudeMessage(msg) {
69
+ const { role, content } = msg;
70
+ if (typeof content === 'string') {
71
+ return {
72
+ role: role === 'user' ? 'user' : 'assistant',
73
+ content,
74
+ };
75
+ }
76
+ // Convert array of content blocks
77
+ const convertedBlocks = [];
78
+ for (const block of content) {
79
+ const converted = convertClaudeContentBlock(block);
80
+ if (converted) {
81
+ convertedBlocks.push(converted);
82
+ }
83
+ }
84
+ // If all blocks were text, concatenate them into a string
85
+ const allText = convertedBlocks.every(b => b.type === 'input_text');
86
+ if (allText) {
87
+ const text = convertedBlocks
88
+ .map(b => b.text)
89
+ .join('');
90
+ return {
91
+ role: role === 'user' ? 'user' : 'assistant',
92
+ content: text,
93
+ };
94
+ }
95
+ // Otherwise, return as array
96
+ return {
97
+ role: role === 'user' ? 'user' : 'assistant',
98
+ content: convertedBlocks,
99
+ };
100
+ }
101
+ /**
102
+ * Get a response with multiple consumption patterns
103
+ *
104
+ * @remarks
105
+ * Creates a response using the OpenResponses API and returns
106
+ * a wrapper that allows consuming the response in multiple ways:
107
+ *
108
+ * - `await response.getText()` - Get just the text content (tools auto-executed)
109
+ * - `await response.getResponse()` - Get full response with usage data (inputTokens, cachedTokens, etc.)
110
+ * - `for await (const delta of response.getTextStream())` - Stream text deltas
111
+ * - `for await (const delta of response.getReasoningStream())` - Stream reasoning deltas
112
+ * - `for await (const event of response.getToolStream())` - Stream tool events (incl. preliminary results)
113
+ * - `for await (const toolCall of response.getToolCallsStream())` - Stream structured tool calls
114
+ * - `await response.getToolCalls()` - Get all tool calls from completed response
115
+ * - `for await (const msg of response.getNewMessagesStream())` - Stream incremental message updates
116
+ * - `for await (const event of response.getFullResponsesStream())` - Stream all events (incl. tool preliminary)
117
+ *
118
+ * All consumption patterns can be used concurrently on the same response.
119
+ *
120
+ * For message format conversion, use the helper functions:
121
+ * - `fromChatMessages()` / `toChatMessage()` for OpenAI chat format
122
+ * - `fromClaudeMessages()` / `toClaudeMessage()` for Anthropic Claude format
123
+ */
124
+ export function callModel(client, request, options) {
125
+ const { tools, maxToolRounds, ...apiRequest } = request;
126
+ // Auto-convert Claude-style messages if detected
127
+ let processedInput = apiRequest.input;
128
+ if (Array.isArray(apiRequest.input)) {
129
+ const hasClaudeMessages = apiRequest.input.some(isClaudeStyleMessage);
130
+ if (hasClaudeMessages) {
131
+ processedInput = apiRequest.input.map((msg) => {
132
+ if (isClaudeStyleMessage(msg)) {
133
+ return convertClaudeMessage(msg);
134
+ }
135
+ return msg;
136
+ });
137
+ }
138
+ }
139
+ // Convert tools to API format and extract enhanced tools if present
140
+ const apiTools = tools ? convertToolsToAPIFormat(tools) : undefined;
141
+ // Build the request with converted tools and input
142
+ const finalRequest = {
143
+ ...apiRequest,
144
+ ...(processedInput !== undefined && { input: processedInput }),
145
+ ...(apiTools !== undefined && { tools: apiTools }),
146
+ };
147
+ return new ModelResult({
148
+ client,
149
+ request: finalRequest,
150
+ options: options ?? {},
151
+ tools: tools ?? [],
152
+ ...(maxToolRounds !== undefined && { maxToolRounds }),
153
+ });
154
+ }
155
+ //# sourceMappingURL=call-model.js.map
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 8c3aa3c963bf
3
4
  */
4
5
  import { encodeJSON } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 54a6e7c9b712
3
4
  */
4
5
  import { encodeJSON } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: e07ee6831da3
3
4
  */
4
5
  import { encodeJSON } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 88fdc1315137
3
4
  */
4
5
  import * as M from "../lib/matchers.js";
5
6
  import { compactMap } from "../lib/primitives.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: f6455fcfe2c8
3
4
  */
4
5
  import { encodeJSON } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: dbe9751f8369
3
4
  */
4
5
  import * as M from "../lib/matchers.js";
5
6
  import { compactMap } from "../lib/primitives.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: ea932ec09987
3
4
  */
4
5
  import { encodeSimple } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 4efb7ea3e48f
3
4
  */
4
5
  import * as M from "../lib/matchers.js";
5
6
  import { compactMap } from "../lib/primitives.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: dac2d205a08d
3
4
  */
4
5
  import { encodeFormQuery } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 371adfc67b48
3
4
  */
4
5
  import * as M from "../lib/matchers.js";
5
6
  import { compactMap } from "../lib/primitives.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: deff050b46c5
3
4
  */
4
5
  import { encodeFormQuery } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: db628dd3c179
3
4
  */
4
5
  import * as M from "../lib/matchers.js";
5
6
  import { compactMap } from "../lib/primitives.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 8a79f27722c0
3
4
  */
4
5
  import { encodeJSON } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 3386ec12d934
3
4
  */
4
5
  import { encodeJSON } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 565ce9801e34
3
4
  */
4
5
  import { encodeFormQuery, encodeSimple } from "../lib/encodings.js";
5
6
  import * as M from "../lib/matchers.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 8ad87e7210ae
3
4
  */
4
5
  import * as M from "../lib/matchers.js";
5
6
  import { compactMap } from "../lib/primitives.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: a2463fc6f69b
3
4
  */
4
5
  import { initHooks } from "./registration.js";
5
6
  export class SDKHooks {
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 5f2dab62b520
3
4
  */
4
5
  export * from "./hooks.js";
5
6
  export * from "./types.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 2a02d86ec24f
3
4
  */
4
5
  export {};
5
6
  //# sourceMappingURL=types.js.map
package/esm/index.d.ts CHANGED
@@ -3,4 +3,7 @@ export * as files from "./lib/files.js";
3
3
  export { HTTPClient } from "./lib/http.js";
4
4
  export type { Fetcher, HTTPClientOptions } from "./lib/http.js";
5
5
  export * from "./sdk/sdk.js";
6
+ export { fromClaudeMessages, toClaudeMessage } from "./lib/anthropic-compat.js";
7
+ export { fromChatMessages, toChatMessage } from "./lib/chat-compat.js";
8
+ export { extractUnsupportedContent, hasUnsupportedContent, getUnsupportedContentSummary } from "./lib/stream-transformers.js";
6
9
  //# sourceMappingURL=index.d.ts.map
package/esm/index.js CHANGED
@@ -5,4 +5,8 @@ export * from "./lib/config.js";
5
5
  export * as files from "./lib/files.js";
6
6
  export { HTTPClient } from "./lib/http.js";
7
7
  export * from "./sdk/sdk.js";
8
+ // Message format compatibility helpers
9
+ export { fromClaudeMessages, toClaudeMessage } from "./lib/anthropic-compat.js";
10
+ export { fromChatMessages, toChatMessage } from "./lib/chat-compat.js";
11
+ export { extractUnsupportedContent, hasUnsupportedContent, getUnsupportedContentSummary } from "./lib/stream-transformers.js";
8
12
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,46 @@
1
+ import type * as models from "../models/index.js";
2
+ import { convertToClaudeMessage } from "./stream-transformers.js";
3
+ /**
4
+ * Convert Anthropic Claude-style messages to OpenResponses input format.
5
+ *
6
+ * This function transforms ClaudeMessageParam[] (Anthropic SDK format) to
7
+ * OpenResponsesInput format that can be passed directly to callModel().
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { fromClaudeMessages } from '@openrouter/sdk';
12
+ *
13
+ * const claudeMessages = [
14
+ * { role: "user", content: "Hello!" },
15
+ * { role: "assistant", content: "Hi there!" },
16
+ * ];
17
+ *
18
+ * const response = openrouter.callModel({
19
+ * model: "anthropic/claude-3-sonnet",
20
+ * input: fromClaudeMessages(claudeMessages),
21
+ * });
22
+ * ```
23
+ */
24
+ export declare function fromClaudeMessages(messages: models.ClaudeMessageParam[]): models.OpenResponsesInput;
25
+ /**
26
+ * Convert an OpenResponses response to Anthropic Claude message format.
27
+ *
28
+ * This function transforms OpenResponsesNonStreamingResponse to ClaudeMessage
29
+ * (Anthropic SDK format) for compatibility with code expecting Claude responses.
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * import { toClaudeMessage } from '@openrouter/sdk';
34
+ *
35
+ * const response = await openrouter.callModel({
36
+ * model: "anthropic/claude-3-sonnet",
37
+ * input: "Hello!",
38
+ * });
39
+ *
40
+ * const openResponsesResult = await response.getResponse();
41
+ * const claudeMessage = toClaudeMessage(openResponsesResult);
42
+ * // claudeMessage is now compatible with Anthropic SDK types
43
+ * ```
44
+ */
45
+ export declare const toClaudeMessage: typeof convertToClaudeMessage;
46
+ //# sourceMappingURL=anthropic-compat.d.ts.map