@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
package/jsr.json CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  {
4
4
  "name": "@openrouter/sdk",
5
- "version": "0.3.1",
5
+ "version": "0.3.7",
6
6
  "exports": {
7
7
  ".": "./src/index.ts",
8
8
  "./models/errors": "./src/models/errors/index.ts",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openrouter/sdk",
3
- "version": "0.3.1",
3
+ "version": "0.3.7",
4
4
  "author": "OpenRouter",
5
5
  "description": "The OpenRouter TypeScript SDK is a type-safe toolkit for building AI applications with access to 300+ language models through a unified API.",
6
6
  "keywords": [
package/tsconfig.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "incremental": true,
3
+ "incremental": false,
4
4
  "target": "ES2020",
5
5
  "lib": ["ES2022", "DOM", "DOM.Iterable"],
6
6
  "jsx": "react-jsx",
@@ -1,87 +0,0 @@
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 { ResponseWrapper } from '../lib/response-wrapper.js';
6
- /**
7
- * Input type that accepts both chat-style messages and responses-style input
8
- */
9
- export type CallModelInput = models.OpenResponsesInput | models.Message[];
10
- /**
11
- * Tool type that accepts chat-style, responses-style, or enhanced tools
12
- */
13
- export type CallModelTools = Tool[] | models.ToolDefinitionJson[] | models.OpenResponsesRequest['tools'];
14
- /**
15
- * Get a response with multiple consumption patterns
16
- *
17
- * @remarks
18
- * Creates a response using the OpenResponses API in streaming mode and returns
19
- * a wrapper that allows consuming the response in multiple ways:
20
- *
21
- * - `await response.getMessage()` - Get the completed message (tools auto-executed)
22
- * - `await response.getText()` - Get just the text content (tools auto-executed)
23
- * - `await response.getResponse()` - Get full response with usage data (inputTokens, cachedTokens, etc.)
24
- * - `for await (const delta of response.getTextStream())` - Stream text deltas
25
- * - `for await (const delta of response.getReasoningStream())` - Stream reasoning deltas
26
- * - `for await (const event of response.getToolStream())` - Stream tool events (incl. preliminary results)
27
- * - `for await (const toolCall of response.getToolCallsStream())` - Stream structured tool calls
28
- * - `await response.getToolCalls()` - Get all tool calls from completed response
29
- * - `for await (const msg of response.getNewMessagesStream())` - Stream incremental message updates
30
- * - `for await (const event of response.getFullResponsesStream())` - Stream all events (incl. tool preliminary)
31
- * - `for await (const event of response.getFullChatStream())` - Stream in chat format (incl. tool preliminary)
32
- *
33
- * All consumption patterns can be used concurrently on the same response.
34
- *
35
- * @example
36
- * ```typescript
37
- * import { z } from 'zod';
38
- *
39
- * // Simple text extraction
40
- * const response = openrouter.callModel({
41
- * model: "openai/gpt-4",
42
- * input: "Hello!"
43
- * });
44
- * const text = await response.getText();
45
- * console.log(text);
46
- *
47
- * // With tools (automatic execution)
48
- * const response = openrouter.callModel({
49
- * model: "openai/gpt-4",
50
- * input: "What's the weather in SF?",
51
- * tools: [{
52
- * type: "function",
53
- * function: {
54
- * name: "get_weather",
55
- * description: "Get current weather",
56
- * inputSchema: z.object({
57
- * location: z.string()
58
- * }),
59
- * outputSchema: z.object({
60
- * temperature: z.number(),
61
- * description: z.string()
62
- * }),
63
- * execute: async (params) => {
64
- * return { temperature: 72, description: "Sunny" };
65
- * }
66
- * }
67
- * }],
68
- * maxToolRounds: 5, // or function: (context: TurnContext) => boolean
69
- * });
70
- * const message = await response.getMessage(); // Tools auto-executed!
71
- *
72
- * // Stream with preliminary results
73
- * for await (const event of response.getFullChatStream()) {
74
- * if (event.type === "content.delta") {
75
- * process.stdout.write(event.delta);
76
- * } else if (event.type === "tool.preliminary_result") {
77
- * console.log("Tool progress:", event.result);
78
- * }
79
- * }
80
- * ```
81
- */
82
- export declare function callModel(client: OpenRouterCore, request: Omit<models.OpenResponsesRequest, 'stream' | 'tools' | 'input'> & {
83
- input?: CallModelInput;
84
- tools?: CallModelTools;
85
- maxToolRounds?: MaxToolRounds;
86
- }, options?: RequestOptions): ResponseWrapper;
87
- //# sourceMappingURL=callModel.d.ts.map
@@ -1,215 +0,0 @@
1
- import { ResponseWrapper } from '../lib/response-wrapper.js';
2
- import { convertToolsToAPIFormat } from '../lib/tool-executor.js';
3
- /**
4
- * Check if input is chat-style messages (Message[])
5
- */
6
- function isChatStyleMessages(input) {
7
- if (!Array.isArray(input)) {
8
- return false;
9
- }
10
- if (input.length === 0) {
11
- return false;
12
- }
13
- const first = input[0];
14
- // Chat-style messages have role but no 'type' field at top level
15
- // Responses-style items have 'type' field (like 'message', 'function_call', etc.)
16
- return first && 'role' in first && !('type' in first);
17
- }
18
- /**
19
- * Check if tools are chat-style (ToolDefinitionJson[])
20
- */
21
- function isChatStyleTools(tools) {
22
- if (!Array.isArray(tools)) {
23
- return false;
24
- }
25
- if (tools.length === 0) {
26
- return false;
27
- }
28
- const first = tools[0];
29
- // Chat-style tools have nested 'function' property with 'name' inside
30
- // Enhanced tools have 'function' with 'inputSchema'
31
- // Responses-style tools have 'name' at top level
32
- const fn = first?.['function'];
33
- return (first &&
34
- 'function' in first &&
35
- fn !== undefined &&
36
- fn !== null &&
37
- 'name' in fn &&
38
- !('inputSchema' in fn));
39
- }
40
- /**
41
- * Convert chat-style tools to responses-style
42
- */
43
- function convertChatToResponsesTools(tools) {
44
- return tools.map((tool) => ({
45
- type: 'function',
46
- name: tool.function.name,
47
- description: tool.function.description ?? null,
48
- strict: tool.function.strict ?? null,
49
- parameters: tool.function.parameters ?? null,
50
- }));
51
- }
52
- /**
53
- * Convert chat-style messages to responses-style input
54
- */
55
- function convertChatToResponsesInput(messages) {
56
- return messages.map((msg) => {
57
- // Extract extra fields like cache_control
58
- const { role, content, ...extraFields } = msg;
59
- if (role === 'tool') {
60
- const toolMsg = msg;
61
- return {
62
- type: 'function_call_output',
63
- callId: toolMsg.toolCallId,
64
- output: typeof toolMsg.content === 'string' ? toolMsg.content : JSON.stringify(toolMsg.content),
65
- ...extraFields,
66
- };
67
- }
68
- // Handle assistant messages with tool calls
69
- if (role === 'assistant') {
70
- const assistantMsg = msg;
71
- // If it has tool calls, we need to convert them
72
- // For now, just convert the content part
73
- return {
74
- role: 'assistant',
75
- content: typeof assistantMsg.content === 'string'
76
- ? assistantMsg.content
77
- : assistantMsg.content === null
78
- ? ''
79
- : JSON.stringify(assistantMsg.content),
80
- ...extraFields,
81
- };
82
- }
83
- // System, user, developer messages
84
- const convertedContent = typeof content === 'string'
85
- ? content
86
- : content === null || content === undefined
87
- ? ''
88
- : JSON.stringify(content);
89
- return {
90
- role: role,
91
- content: convertedContent,
92
- ...extraFields,
93
- };
94
- });
95
- }
96
- /**
97
- * Get a response with multiple consumption patterns
98
- *
99
- * @remarks
100
- * Creates a response using the OpenResponses API in streaming mode and returns
101
- * a wrapper that allows consuming the response in multiple ways:
102
- *
103
- * - `await response.getMessage()` - Get the completed message (tools auto-executed)
104
- * - `await response.getText()` - Get just the text content (tools auto-executed)
105
- * - `await response.getResponse()` - Get full response with usage data (inputTokens, cachedTokens, etc.)
106
- * - `for await (const delta of response.getTextStream())` - Stream text deltas
107
- * - `for await (const delta of response.getReasoningStream())` - Stream reasoning deltas
108
- * - `for await (const event of response.getToolStream())` - Stream tool events (incl. preliminary results)
109
- * - `for await (const toolCall of response.getToolCallsStream())` - Stream structured tool calls
110
- * - `await response.getToolCalls()` - Get all tool calls from completed response
111
- * - `for await (const msg of response.getNewMessagesStream())` - Stream incremental message updates
112
- * - `for await (const event of response.getFullResponsesStream())` - Stream all events (incl. tool preliminary)
113
- * - `for await (const event of response.getFullChatStream())` - Stream in chat format (incl. tool preliminary)
114
- *
115
- * All consumption patterns can be used concurrently on the same response.
116
- *
117
- * @example
118
- * ```typescript
119
- * import { z } from 'zod';
120
- *
121
- * // Simple text extraction
122
- * const response = openrouter.callModel({
123
- * model: "openai/gpt-4",
124
- * input: "Hello!"
125
- * });
126
- * const text = await response.getText();
127
- * console.log(text);
128
- *
129
- * // With tools (automatic execution)
130
- * const response = openrouter.callModel({
131
- * model: "openai/gpt-4",
132
- * input: "What's the weather in SF?",
133
- * tools: [{
134
- * type: "function",
135
- * function: {
136
- * name: "get_weather",
137
- * description: "Get current weather",
138
- * inputSchema: z.object({
139
- * location: z.string()
140
- * }),
141
- * outputSchema: z.object({
142
- * temperature: z.number(),
143
- * description: z.string()
144
- * }),
145
- * execute: async (params) => {
146
- * return { temperature: 72, description: "Sunny" };
147
- * }
148
- * }
149
- * }],
150
- * maxToolRounds: 5, // or function: (context: TurnContext) => boolean
151
- * });
152
- * const message = await response.getMessage(); // Tools auto-executed!
153
- *
154
- * // Stream with preliminary results
155
- * for await (const event of response.getFullChatStream()) {
156
- * if (event.type === "content.delta") {
157
- * process.stdout.write(event.delta);
158
- * } else if (event.type === "tool.preliminary_result") {
159
- * console.log("Tool progress:", event.result);
160
- * }
161
- * }
162
- * ```
163
- */
164
- export function callModel(client, request, options) {
165
- const { tools, maxToolRounds, input, ...restRequest } = request;
166
- // Convert chat-style messages to responses-style input if needed
167
- const convertedInput = input && isChatStyleMessages(input) ? convertChatToResponsesInput(input) : input;
168
- const apiRequest = {
169
- ...restRequest,
170
- input: convertedInput,
171
- };
172
- // Determine tool type and convert as needed
173
- let isEnhancedTools = false;
174
- let isChatTools = false;
175
- if (tools && Array.isArray(tools) && tools.length > 0) {
176
- const firstTool = tools[0];
177
- const fn = firstTool?.['function'];
178
- isEnhancedTools =
179
- 'function' in firstTool && fn !== undefined && fn !== null && 'inputSchema' in fn;
180
- isChatTools = !isEnhancedTools && isChatStyleTools(tools);
181
- }
182
- const enhancedTools = isEnhancedTools ? tools : undefined;
183
- // Convert tools to API format based on their type
184
- let apiTools;
185
- if (enhancedTools) {
186
- apiTools = convertToolsToAPIFormat(enhancedTools);
187
- }
188
- else if (isChatTools) {
189
- apiTools = convertChatToResponsesTools(tools);
190
- }
191
- else {
192
- apiTools = tools;
193
- }
194
- // Build the request with converted tools
195
- const finalRequest = {
196
- ...apiRequest,
197
- ...(apiTools && {
198
- tools: apiTools,
199
- }),
200
- };
201
- const wrapperOptions = {
202
- client,
203
- request: finalRequest,
204
- options: options ?? {},
205
- };
206
- // Only pass enhanced tools to wrapper (needed for auto-execution)
207
- if (enhancedTools) {
208
- wrapperOptions.tools = enhancedTools;
209
- }
210
- if (maxToolRounds !== undefined) {
211
- wrapperOptions.maxToolRounds = maxToolRounds;
212
- }
213
- return new ResponseWrapper(wrapperOptions);
214
- }
215
- //# sourceMappingURL=callModel.js.map