@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
@@ -0,0 +1,46 @@
1
+ import type * as models from "../models/index.js";
2
+ import { extractMessageFromResponse } from "./stream-transformers.js";
3
+ /**
4
+ * Convert OpenAI chat-style messages to OpenResponses input format.
5
+ *
6
+ * This function transforms Message[] (OpenAI chat format) to OpenResponsesInput
7
+ * format that can be passed directly to callModel().
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { fromChatMessages } from '@openrouter/sdk';
12
+ *
13
+ * const chatMessages = [
14
+ * { role: "system", content: "You are a helpful assistant." },
15
+ * { role: "user", content: "Hello!" },
16
+ * ];
17
+ *
18
+ * const response = openrouter.callModel({
19
+ * model: "openai/gpt-4",
20
+ * input: fromChatMessages(chatMessages),
21
+ * });
22
+ * ```
23
+ */
24
+ export declare function fromChatMessages(messages: models.Message[]): models.OpenResponsesInput;
25
+ /**
26
+ * Convert an OpenResponses response to OpenAI chat message format.
27
+ *
28
+ * This function transforms OpenResponsesNonStreamingResponse to AssistantMessage
29
+ * (OpenAI chat format) for compatibility with code expecting chat responses.
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * import { toChatMessage } from '@openrouter/sdk';
34
+ *
35
+ * const response = await openrouter.callModel({
36
+ * model: "openai/gpt-4",
37
+ * input: "Hello!",
38
+ * });
39
+ *
40
+ * const openResponsesResult = await response.getResponse();
41
+ * const chatMessage = toChatMessage(openResponsesResult);
42
+ * // chatMessage is now { role: "assistant", content: "..." }
43
+ * ```
44
+ */
45
+ export declare const toChatMessage: typeof extractMessageFromResponse;
46
+ //# sourceMappingURL=chat-compat.d.ts.map
@@ -0,0 +1,108 @@
1
+ import { OpenResponsesEasyInputMessageRoleUser, OpenResponsesEasyInputMessageRoleSystem, OpenResponsesEasyInputMessageRoleAssistant, OpenResponsesEasyInputMessageRoleDeveloper, } from "../models/openresponseseasyinputmessage.js";
2
+ import { OpenResponsesFunctionCallOutputType } from "../models/openresponsesfunctioncalloutput.js";
3
+ import { extractMessageFromResponse } from "./stream-transformers.js";
4
+ /**
5
+ * Type guard for ToolResponseMessage
6
+ */
7
+ function isToolResponseMessage(msg) {
8
+ return msg.role === "tool";
9
+ }
10
+ /**
11
+ * Type guard for AssistantMessage
12
+ */
13
+ function isAssistantMessage(msg) {
14
+ return msg.role === "assistant";
15
+ }
16
+ /**
17
+ * Maps chat role strings to OpenResponses role types
18
+ */
19
+ function mapChatRole(role) {
20
+ switch (role) {
21
+ case "user":
22
+ return OpenResponsesEasyInputMessageRoleUser.User;
23
+ case "system":
24
+ return OpenResponsesEasyInputMessageRoleSystem.System;
25
+ case "assistant":
26
+ return OpenResponsesEasyInputMessageRoleAssistant.Assistant;
27
+ case "developer":
28
+ return OpenResponsesEasyInputMessageRoleDeveloper.Developer;
29
+ }
30
+ }
31
+ /**
32
+ * Convert message content to a string representation.
33
+ * Handles string, null, undefined, and object content types.
34
+ */
35
+ function contentToString(content) {
36
+ if (typeof content === "string") {
37
+ return content;
38
+ }
39
+ if (content === null || content === undefined) {
40
+ return "";
41
+ }
42
+ return JSON.stringify(content);
43
+ }
44
+ /**
45
+ * Convert OpenAI chat-style messages to OpenResponses input format.
46
+ *
47
+ * This function transforms Message[] (OpenAI chat format) to OpenResponsesInput
48
+ * format that can be passed directly to callModel().
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * import { fromChatMessages } from '@openrouter/sdk';
53
+ *
54
+ * const chatMessages = [
55
+ * { role: "system", content: "You are a helpful assistant." },
56
+ * { role: "user", content: "Hello!" },
57
+ * ];
58
+ *
59
+ * const response = openrouter.callModel({
60
+ * model: "openai/gpt-4",
61
+ * input: fromChatMessages(chatMessages),
62
+ * });
63
+ * ```
64
+ */
65
+ export function fromChatMessages(messages) {
66
+ return messages.map((msg) => {
67
+ if (isToolResponseMessage(msg)) {
68
+ return {
69
+ type: OpenResponsesFunctionCallOutputType.FunctionCallOutput,
70
+ callId: msg.toolCallId,
71
+ output: contentToString(msg.content),
72
+ };
73
+ }
74
+ if (isAssistantMessage(msg)) {
75
+ return {
76
+ role: mapChatRole("assistant"),
77
+ content: contentToString(msg.content),
78
+ };
79
+ }
80
+ // System, user, developer messages
81
+ return {
82
+ role: mapChatRole(msg.role),
83
+ content: contentToString(msg.content),
84
+ };
85
+ });
86
+ }
87
+ /**
88
+ * Convert an OpenResponses response to OpenAI chat message format.
89
+ *
90
+ * This function transforms OpenResponsesNonStreamingResponse to AssistantMessage
91
+ * (OpenAI chat format) for compatibility with code expecting chat responses.
92
+ *
93
+ * @example
94
+ * ```typescript
95
+ * import { toChatMessage } from '@openrouter/sdk';
96
+ *
97
+ * const response = await openrouter.callModel({
98
+ * model: "openai/gpt-4",
99
+ * input: "Hello!",
100
+ * });
101
+ *
102
+ * const openResponsesResult = await response.getResponse();
103
+ * const chatMessage = toChatMessage(openResponsesResult);
104
+ * // chatMessage is now { role: "assistant", content: "..." }
105
+ * ```
106
+ */
107
+ export const toChatMessage = extractMessageFromResponse;
108
+ //# sourceMappingURL=chat-compat.js.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=chat-compat.test.d.ts.map
@@ -0,0 +1,282 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { fromChatMessages, toChatMessage } from "./chat-compat.js";
3
+ /**
4
+ * Creates a properly typed mock OpenResponsesNonStreamingResponse for testing.
5
+ * This factory provides all required fields with sensible defaults.
6
+ */
7
+ function createMockResponse(overrides) {
8
+ return {
9
+ id: "resp_test",
10
+ object: "response",
11
+ createdAt: Date.now(),
12
+ model: "openai/gpt-4",
13
+ status: "completed",
14
+ error: null,
15
+ incompleteDetails: null,
16
+ temperature: null,
17
+ topP: null,
18
+ metadata: null,
19
+ tools: [],
20
+ toolChoice: "auto",
21
+ parallelToolCalls: false,
22
+ ...overrides,
23
+ };
24
+ }
25
+ describe("fromChatMessages", () => {
26
+ describe("basic message conversion", () => {
27
+ it("converts user message with string content", () => {
28
+ const messages = [
29
+ { role: "user", content: "Hello, how are you?" },
30
+ ];
31
+ const result = fromChatMessages(messages);
32
+ expect(result).toEqual([
33
+ { role: "user", content: "Hello, how are you?" },
34
+ ]);
35
+ });
36
+ it("converts assistant message with string content", () => {
37
+ const messages = [
38
+ { role: "assistant", content: "I am doing well, thank you!" },
39
+ ];
40
+ const result = fromChatMessages(messages);
41
+ expect(result).toEqual([
42
+ { role: "assistant", content: "I am doing well, thank you!" },
43
+ ]);
44
+ });
45
+ it("converts system message with string content", () => {
46
+ const messages = [
47
+ { role: "system", content: "You are a helpful assistant." },
48
+ ];
49
+ const result = fromChatMessages(messages);
50
+ expect(result).toEqual([
51
+ { role: "system", content: "You are a helpful assistant." },
52
+ ]);
53
+ });
54
+ it("converts developer message with string content", () => {
55
+ const messages = [
56
+ { role: "developer", content: "Developer instructions here." },
57
+ ];
58
+ const result = fromChatMessages(messages);
59
+ expect(result).toEqual([
60
+ { role: "developer", content: "Developer instructions here." },
61
+ ]);
62
+ });
63
+ it("converts multiple messages in conversation", () => {
64
+ const messages = [
65
+ { role: "system", content: "You are helpful." },
66
+ { role: "user", content: "Hi" },
67
+ { role: "assistant", content: "Hello!" },
68
+ { role: "user", content: "How are you?" },
69
+ ];
70
+ const result = fromChatMessages(messages);
71
+ expect(result).toEqual([
72
+ { role: "system", content: "You are helpful." },
73
+ { role: "user", content: "Hi" },
74
+ { role: "assistant", content: "Hello!" },
75
+ { role: "user", content: "How are you?" },
76
+ ]);
77
+ });
78
+ });
79
+ describe("tool response message conversion", () => {
80
+ it("converts tool message to function_call_output", () => {
81
+ const messages = [
82
+ {
83
+ role: "tool",
84
+ content: "The weather is sunny and 72F",
85
+ toolCallId: "call_abc123",
86
+ },
87
+ ];
88
+ const result = fromChatMessages(messages);
89
+ expect(result).toEqual([
90
+ {
91
+ type: "function_call_output",
92
+ callId: "call_abc123",
93
+ output: "The weather is sunny and 72F",
94
+ },
95
+ ]);
96
+ });
97
+ it("converts tool message with object content by stringifying", () => {
98
+ const messages = [
99
+ {
100
+ role: "tool",
101
+ content: [{ type: "text", text: "Structured response" }],
102
+ toolCallId: "call_def456",
103
+ },
104
+ ];
105
+ const result = fromChatMessages(messages);
106
+ expect(result).toEqual([
107
+ {
108
+ type: "function_call_output",
109
+ callId: "call_def456",
110
+ output: JSON.stringify([{ type: "text", text: "Structured response" }]),
111
+ },
112
+ ]);
113
+ });
114
+ });
115
+ describe("content array handling", () => {
116
+ it("stringifies array content for user messages", () => {
117
+ const messages = [
118
+ {
119
+ role: "user",
120
+ content: [{ type: "text", text: "Hello from array" }],
121
+ },
122
+ ];
123
+ const result = fromChatMessages(messages);
124
+ expect(result).toEqual([
125
+ {
126
+ role: "user",
127
+ content: JSON.stringify([{ type: "text", text: "Hello from array" }]),
128
+ },
129
+ ]);
130
+ });
131
+ it("stringifies array content for assistant messages", () => {
132
+ const messages = [
133
+ {
134
+ role: "assistant",
135
+ content: [{ type: "text", text: "Response in array" }],
136
+ },
137
+ ];
138
+ const result = fromChatMessages(messages);
139
+ expect(result).toEqual([
140
+ {
141
+ role: "assistant",
142
+ content: JSON.stringify([{ type: "text", text: "Response in array" }]),
143
+ },
144
+ ]);
145
+ });
146
+ });
147
+ describe("null and empty content handling", () => {
148
+ it("handles null content in assistant message", () => {
149
+ const messages = [
150
+ { role: "assistant", content: null },
151
+ ];
152
+ const result = fromChatMessages(messages);
153
+ expect(result).toEqual([{ role: "assistant", content: "" }]);
154
+ });
155
+ it("handles empty string content", () => {
156
+ const messages = [{ role: "user", content: "" }];
157
+ const result = fromChatMessages(messages);
158
+ expect(result).toEqual([{ role: "user", content: "" }]);
159
+ });
160
+ it("handles empty messages array", () => {
161
+ const result = fromChatMessages([]);
162
+ expect(result).toEqual([]);
163
+ });
164
+ });
165
+ });
166
+ describe("toChatMessage", () => {
167
+ describe("basic message conversion", () => {
168
+ it("converts response with text output to AssistantMessage", () => {
169
+ const response = createMockResponse({
170
+ id: "resp_123",
171
+ output: [
172
+ {
173
+ id: "msg_1",
174
+ type: "message",
175
+ role: "assistant",
176
+ status: "completed",
177
+ content: [
178
+ {
179
+ type: "output_text",
180
+ text: "Hello! How can I help you?",
181
+ annotations: [],
182
+ },
183
+ ],
184
+ },
185
+ ],
186
+ usage: {
187
+ inputTokens: 10,
188
+ outputTokens: 20,
189
+ totalTokens: 30,
190
+ inputTokensDetails: { cachedTokens: 0 },
191
+ outputTokensDetails: { reasoningTokens: 0 },
192
+ },
193
+ });
194
+ const result = toChatMessage(response);
195
+ expect(result).toEqual({
196
+ role: "assistant",
197
+ content: "Hello! How can I help you?",
198
+ });
199
+ });
200
+ it("combines multiple text parts into single content string", () => {
201
+ const response = createMockResponse({
202
+ id: "resp_456",
203
+ output: [
204
+ {
205
+ id: "msg_1",
206
+ type: "message",
207
+ role: "assistant",
208
+ status: "completed",
209
+ content: [
210
+ { type: "output_text", text: "Part 1. ", annotations: [] },
211
+ { type: "output_text", text: "Part 2.", annotations: [] },
212
+ ],
213
+ },
214
+ ],
215
+ usage: {
216
+ inputTokens: 5,
217
+ outputTokens: 10,
218
+ totalTokens: 15,
219
+ inputTokensDetails: { cachedTokens: 0 },
220
+ outputTokensDetails: { reasoningTokens: 0 },
221
+ },
222
+ });
223
+ const result = toChatMessage(response);
224
+ expect(result).toEqual({
225
+ role: "assistant",
226
+ content: "Part 1. Part 2.",
227
+ });
228
+ });
229
+ it("returns null content when message has no text", () => {
230
+ const response = createMockResponse({
231
+ id: "resp_789",
232
+ output: [
233
+ {
234
+ id: "msg_1",
235
+ type: "message",
236
+ role: "assistant",
237
+ status: "completed",
238
+ content: [],
239
+ },
240
+ ],
241
+ usage: {
242
+ inputTokens: 5,
243
+ outputTokens: 0,
244
+ totalTokens: 5,
245
+ inputTokensDetails: { cachedTokens: 0 },
246
+ outputTokensDetails: { reasoningTokens: 0 },
247
+ },
248
+ });
249
+ const result = toChatMessage(response);
250
+ expect(result).toEqual({
251
+ role: "assistant",
252
+ content: null,
253
+ });
254
+ });
255
+ });
256
+ describe("error handling", () => {
257
+ it("throws error when no message found in output", () => {
258
+ const response = createMockResponse({
259
+ id: "resp_err",
260
+ output: [
261
+ {
262
+ type: "function_call",
263
+ callId: "call_1",
264
+ name: "test_tool",
265
+ arguments: "{}",
266
+ id: "fc_1",
267
+ status: "completed",
268
+ },
269
+ ],
270
+ usage: {
271
+ inputTokens: 5,
272
+ outputTokens: 10,
273
+ totalTokens: 15,
274
+ inputTokensDetails: { cachedTokens: 0 },
275
+ outputTokensDetails: { reasoningTokens: 0 },
276
+ },
277
+ });
278
+ expect(() => toChatMessage(response)).toThrow("No message found in response output");
279
+ });
280
+ });
281
+ });
282
+ //# sourceMappingURL=chat-compat.test.js.map
@@ -45,8 +45,8 @@ export declare function serverURLFromOptions(options: SDKOptions): URL | null;
45
45
  export declare const SDK_METADATA: {
46
46
  readonly language: "typescript";
47
47
  readonly openapiDocVersion: "1.0.0";
48
- readonly sdkVersion: "0.3.1";
49
- readonly genVersion: "2.768.0";
50
- readonly userAgent: "speakeasy-sdk/typescript 0.3.1 2.768.0 1.0.0 @openrouter/sdk";
48
+ readonly sdkVersion: "0.3.7";
49
+ readonly genVersion: "2.788.4";
50
+ readonly userAgent: "speakeasy-sdk/typescript 0.3.7 2.788.4 1.0.0 @openrouter/sdk";
51
51
  };
52
52
  //# sourceMappingURL=config.d.ts.map
package/esm/lib/config.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 320761608fb3
3
4
  */
4
5
  import { pathToFunc } from "./url.js";
5
6
  /**
@@ -25,8 +26,8 @@ export function serverURLFromOptions(options) {
25
26
  export const SDK_METADATA = {
26
27
  language: "typescript",
27
28
  openapiDocVersion: "1.0.0",
28
- sdkVersion: "0.3.1",
29
- genVersion: "2.768.0",
30
- userAgent: "speakeasy-sdk/typescript 0.3.1 2.768.0 1.0.0 @openrouter/sdk",
29
+ sdkVersion: "0.3.7",
30
+ genVersion: "2.788.4",
31
+ userAgent: "speakeasy-sdk/typescript 0.3.7 2.788.4 1.0.0 @openrouter/sdk",
31
32
  };
32
33
  //# sourceMappingURL=config.js.map
package/esm/lib/dlv.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: b1988214835a
3
4
  */
4
5
  /*
5
6
  MIT License
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 3bd8ead98afd
3
4
  */
4
5
  import { bytesToBase64 } from "./base64.js";
5
6
  import { isPlainObject } from "./is-plain-object.js";
package/esm/lib/env.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: c52972a3b198
3
4
  */
4
5
  import * as z from "zod/v4";
5
6
  import { dlv } from "./dlv.js";
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 3f731f808a79
3
4
  */
4
5
  export class EventStream extends ReadableStream {
5
6
  constructor(responseBody, parse) {
@@ -20,7 +21,7 @@ export class EventStream extends ReadableStream {
20
21
  const message = buffer.slice(0, match.index);
21
22
  buffer = buffer.slice(match.index + match.length);
22
23
  const item = parseMessage(message, parse);
23
- if (item?.value)
24
+ if (item && !item.done)
24
25
  return downstream.enqueue(item.value);
25
26
  if (item?.done) {
26
27
  await upstream.cancel("done");
package/esm/lib/files.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: e5efa54fcb95
3
4
  */
4
5
  /**
5
6
  * Consumes a stream and returns a concatenated array buffer. Useful in
package/esm/lib/http.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 63a80782d37e
3
4
  */
4
5
  const DEFAULT_FETCHER = (input, init) => {
5
6
  // If input is a Request and init is undefined, Bun will discard the method,
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: b9862591096a
3
4
  */
4
5
  /*
5
6
  MIT License
package/esm/lib/logger.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: 7ee7741a02bf
3
4
  */
4
5
  export {};
5
6
  //# sourceMappingURL=logger.js.map
@@ -1,5 +1,6 @@
1
1
  /*
2
2
  * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
3
+ * @generated-id: d54b2253b719
3
4
  */
4
5
  import { OpenRouterDefaultError } from "../models/errors/openrouterdefaulterror.js";
5
6
  import { ResponseValidationError } from "../models/errors/responsevalidationerror.js";
@@ -1,7 +1,7 @@
1
- import type { OpenRouterCore } from '../core.js';
2
- import type * as models from '../models/index.js';
3
- import type { RequestOptions } from './sdks.js';
4
- import type { ChatStreamEvent, EnhancedResponseStreamEvent, Tool, MaxToolRounds, ParsedToolCall, ToolStreamEvent } from './tool-types.js';
1
+ import type { OpenRouterCore } from "../core.js";
2
+ import type * as models from "../models/index.js";
3
+ import type { RequestOptions } from "./sdks.js";
4
+ import type { ChatStreamEvent, EnhancedResponseStreamEvent, Tool, MaxToolRounds, ParsedToolCall, ToolStreamEvent } from "./tool-types.js";
5
5
  export interface GetResponseOptions {
6
6
  request: models.OpenResponsesRequest;
7
7
  client: OpenRouterCore;
@@ -13,19 +13,22 @@ export interface GetResponseOptions {
13
13
  * A wrapper around a streaming response that provides multiple consumption patterns.
14
14
  *
15
15
  * Allows consuming the response in multiple ways:
16
- * - `await response.getMessage()` - Get the completed message
17
- * - `await response.getText()` - Get just the text
18
- * - `for await (const delta of response.getTextStream())` - Stream text deltas
19
- * - `for await (const msg of response.getNewMessagesStream())` - Stream incremental message updates
20
- * - `for await (const event of response.getFullResponsesStream())` - Stream all response events
16
+ * - `await result.getText()` - Get just the text
17
+ * - `await result.getResponse()` - Get the full response object
18
+ * - `for await (const delta of result.getTextStream())` - Stream text deltas
19
+ * - `for await (const msg of result.getNewMessagesStream())` - Stream incremental message updates
20
+ * - `for await (const event of result.getFullResponsesStream())` - Stream all response events
21
+ *
22
+ * For message format conversion, use the helper functions:
23
+ * - `toChatMessage(response)` for OpenAI chat format
24
+ * - `toClaudeMessage(response)` for Anthropic Claude format
21
25
  *
22
26
  * All consumption patterns can be used concurrently thanks to the underlying
23
27
  * ReusableReadableStream implementation.
24
28
  */
25
- export declare class ResponseWrapper {
29
+ export declare class ModelResult {
26
30
  private reusableStream;
27
31
  private streamPromise;
28
- private messagePromise;
29
32
  private textPromise;
30
33
  private options;
31
34
  private initPromise;
@@ -48,20 +51,10 @@ export declare class ResponseWrapper {
48
51
  * This is idempotent - multiple calls will return the same promise
49
52
  */
50
53
  private executeToolsIfNeeded;
51
- /**
52
- * Internal helper to get the message after tool execution
53
- */
54
- private getMessageInternal;
55
54
  /**
56
55
  * Internal helper to get the text after tool execution
57
56
  */
58
57
  private getTextInternal;
59
- /**
60
- * Get the completed message from the response.
61
- * This will consume the stream until completion, execute any tools, and extract the first message.
62
- * Returns an AssistantMessage in chat format.
63
- */
64
- getMessage(): Promise<models.AssistantMessage>;
65
58
  /**
66
59
  * Get just the text content from the response.
67
60
  * This will consume the stream until completion, execute any tools, and extract the text.
@@ -85,12 +78,12 @@ export declare class ResponseWrapper {
85
78
  */
86
79
  getTextStream(): AsyncIterableIterator<string>;
87
80
  /**
88
- * Stream incremental message updates as content is added.
81
+ * Stream incremental message updates as content is added in responses format.
89
82
  * Each iteration yields an updated version of the message with new content.
90
- * Also yields ToolResponseMessages after tool execution completes.
91
- * Returns AssistantMessage or ToolResponseMessage in chat format.
83
+ * Also yields OpenResponsesFunctionCallOutput after tool execution completes.
84
+ * Returns ResponsesOutputMessage or OpenResponsesFunctionCallOutput compatible with OpenAI Responses API format.
92
85
  */
93
- getNewMessagesStream(): AsyncIterableIterator<models.AssistantMessage | models.ToolResponseMessage>;
86
+ getNewMessagesStream(): AsyncIterableIterator<models.ResponsesOutputMessage | models.OpenResponsesFunctionCallOutput>;
94
87
  /**
95
88
  * Stream only reasoning deltas as they arrive.
96
89
  * This filters the full event stream to only yield reasoning content.
@@ -131,4 +124,4 @@ export declare class ResponseWrapper {
131
124
  */
132
125
  cancel(): Promise<void>;
133
126
  }
134
- //# sourceMappingURL=response-wrapper.d.ts.map
127
+ //# sourceMappingURL=model-result.d.ts.map