@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.
- package/esm/core.js +1 -0
- package/esm/funcs/analyticsGetUserActivity.js +1 -0
- package/esm/funcs/apiKeysCreate.js +1 -0
- package/esm/funcs/apiKeysDelete.js +1 -0
- package/esm/funcs/apiKeysGet.js +1 -0
- package/esm/funcs/apiKeysGetCurrentKeyMetadata.js +1 -0
- package/esm/funcs/apiKeysList.js +1 -0
- package/esm/funcs/apiKeysUpdate.js +1 -0
- package/esm/funcs/betaResponsesSend.js +1 -0
- package/esm/funcs/call-model.d.ts +33 -0
- package/esm/funcs/call-model.js +155 -0
- package/esm/funcs/chatSend.js +1 -0
- package/esm/funcs/completionsGenerate.js +1 -0
- package/esm/funcs/creditsCreateCoinbaseCharge.js +1 -0
- package/esm/funcs/creditsGetCredits.js +1 -0
- package/esm/funcs/embeddingsGenerate.js +1 -0
- package/esm/funcs/embeddingsListModels.js +1 -0
- package/esm/funcs/endpointsList.js +1 -0
- package/esm/funcs/endpointsListZdrEndpoints.js +1 -0
- package/esm/funcs/generationsGetGeneration.js +1 -0
- package/esm/funcs/modelsCount.js +1 -0
- package/esm/funcs/modelsList.js +1 -0
- package/esm/funcs/modelsListForUser.js +1 -0
- package/esm/funcs/oAuthCreateAuthCode.js +1 -0
- package/esm/funcs/oAuthExchangeAuthCodeForAPIKey.js +1 -0
- package/esm/funcs/parametersGetParameters.js +1 -0
- package/esm/funcs/providersList.js +1 -0
- package/esm/hooks/hooks.js +1 -0
- package/esm/hooks/index.js +1 -0
- package/esm/hooks/types.js +1 -0
- package/esm/index.d.ts +3 -0
- package/esm/index.js +4 -0
- package/esm/lib/anthropic-compat.d.ts +46 -0
- package/esm/lib/anthropic-compat.js +204 -0
- package/esm/lib/anthropic-compat.test.d.ts +2 -0
- package/esm/lib/anthropic-compat.test.js +479 -0
- package/esm/lib/base64.js +1 -0
- package/esm/lib/chat-compat.d.ts +46 -0
- package/esm/lib/chat-compat.js +108 -0
- package/esm/lib/chat-compat.test.d.ts +2 -0
- package/esm/lib/chat-compat.test.js +282 -0
- package/esm/lib/config.d.ts +2 -2
- package/esm/lib/config.js +3 -2
- package/esm/lib/dlv.js +1 -0
- package/esm/lib/encodings.js +1 -0
- package/esm/lib/env.js +1 -0
- package/esm/lib/event-streams.js +1 -0
- package/esm/lib/files.js +1 -0
- package/esm/lib/http.js +1 -0
- package/esm/lib/is-plain-object.js +1 -0
- package/esm/lib/logger.js +1 -0
- package/esm/lib/matchers.js +1 -0
- package/esm/lib/{response-wrapper.d.ts → model-result.d.ts} +19 -26
- package/esm/lib/{response-wrapper.js → model-result.js} +93 -83
- package/esm/lib/primitives.js +1 -0
- package/esm/lib/retries.js +1 -0
- package/esm/lib/schemas.js +1 -0
- package/esm/lib/sdks.js +1 -0
- package/esm/lib/security.js +1 -0
- package/esm/lib/stream-transformers.d.ts +27 -1
- package/esm/lib/stream-transformers.js +319 -1
- package/esm/lib/tool-types.d.ts +3 -3
- package/esm/lib/url.js +1 -0
- package/esm/models/activityitem.js +1 -0
- package/esm/models/assistantmessage.js +1 -0
- package/esm/models/badgatewayresponseerrordata.js +1 -0
- package/esm/models/badrequestresponseerrordata.js +1 -0
- package/esm/models/chatcompletionfinishreason.js +1 -0
- package/esm/models/chaterror.js +1 -0
- package/esm/models/chatgenerationparams.js +1 -0
- package/esm/models/chatgenerationtokenusage.js +1 -0
- package/esm/models/chatmessagecontentitem.js +1 -0
- package/esm/models/chatmessagecontentitemaudio.js +1 -0
- package/esm/models/chatmessagecontentitemcachecontrol.js +1 -0
- package/esm/models/chatmessagecontentitemimage.js +1 -0
- package/esm/models/chatmessagecontentitemtext.js +1 -0
- package/esm/models/chatmessagecontentitemvideo.js +1 -0
- package/esm/models/chatmessagetokenlogprob.js +1 -0
- package/esm/models/chatmessagetokenlogprobs.js +1 -0
- package/esm/models/chatmessagetoolcall.js +1 -0
- package/esm/models/chatresponse.js +1 -0
- package/esm/models/chatresponsechoice.js +1 -0
- package/esm/models/chatstreamingchoice.js +1 -0
- package/esm/models/chatstreamingmessagechunk.js +1 -0
- package/esm/models/chatstreamingmessagetoolcall.js +1 -0
- package/esm/models/chatstreamingresponsechunk.js +1 -0
- package/esm/models/chatstreamoptions.js +1 -0
- package/esm/models/claude-message.d.ts +218 -0
- package/esm/models/claude-message.js +6 -0
- package/esm/models/completionchoice.js +1 -0
- package/esm/models/completioncreateparams.js +1 -0
- package/esm/models/completionlogprobs.js +1 -0
- package/esm/models/completionresponse.js +1 -0
- package/esm/models/completionusage.js +1 -0
- package/esm/models/createchargerequest.js +1 -0
- package/esm/models/datacollection.js +1 -0
- package/esm/models/defaultparameters.js +1 -0
- package/esm/models/edgenetworktimeoutresponseerrordata.js +1 -0
- package/esm/models/endpointstatus.js +1 -0
- package/esm/models/errors/badgatewayresponseerror.js +1 -0
- package/esm/models/errors/badrequestresponseerror.js +1 -0
- package/esm/models/errors/chaterror.js +1 -0
- package/esm/models/errors/edgenetworktimeoutresponseerror.js +1 -0
- package/esm/models/errors/forbiddenresponseerror.js +1 -0
- package/esm/models/errors/httpclienterrors.js +1 -0
- package/esm/models/errors/index.js +1 -0
- package/esm/models/errors/internalserverresponseerror.js +1 -0
- package/esm/models/errors/notfoundresponseerror.js +1 -0
- package/esm/models/errors/openrouterdefaulterror.js +1 -0
- package/esm/models/errors/openroutererror.js +1 -0
- package/esm/models/errors/payloadtoolargeresponseerror.js +1 -0
- package/esm/models/errors/paymentrequiredresponseerror.js +1 -0
- package/esm/models/errors/provideroverloadedresponseerror.js +1 -0
- package/esm/models/errors/requesttimeoutresponseerror.js +1 -0
- package/esm/models/errors/responsevalidationerror.js +1 -0
- package/esm/models/errors/sdkvalidationerror.js +1 -0
- package/esm/models/errors/serviceunavailableresponseerror.js +1 -0
- package/esm/models/errors/toomanyrequestsresponseerror.js +1 -0
- package/esm/models/errors/unauthorizedresponseerror.js +1 -0
- package/esm/models/errors/unprocessableentityresponseerror.js +1 -0
- package/esm/models/filecitation.js +1 -0
- package/esm/models/filepath.js +1 -0
- package/esm/models/forbiddenresponseerrordata.js +1 -0
- package/esm/models/imagegenerationstatus.js +1 -0
- package/esm/models/index.d.ts +1 -0
- package/esm/models/index.js +2 -0
- package/esm/models/inputmodality.js +1 -0
- package/esm/models/instructtype.js +1 -0
- package/esm/models/internalserverresponseerrordata.js +1 -0
- package/esm/models/jsonschemaconfig.js +1 -0
- package/esm/models/listendpointsresponse.js +1 -0
- package/esm/models/message.js +1 -0
- package/esm/models/model.js +1 -0
- package/esm/models/modelarchitecture.js +1 -0
- package/esm/models/modelgroup.js +1 -0
- package/esm/models/modelscountresponse.js +1 -0
- package/esm/models/modelslistresponse.js +1 -0
- package/esm/models/namedtoolchoice.js +1 -0
- package/esm/models/notfoundresponseerrordata.js +1 -0
- package/esm/models/openairesponsesannotation.js +1 -0
- package/esm/models/openairesponsesincludable.js +1 -0
- package/esm/models/openairesponsesincompletedetails.js +1 -0
- package/esm/models/openairesponsesinputunion.js +1 -0
- package/esm/models/openairesponsesprompt.js +1 -0
- package/esm/models/openairesponsesreasoningconfig.js +1 -0
- package/esm/models/openairesponsesreasoningeffort.js +1 -0
- package/esm/models/openairesponsesrefusalcontent.js +1 -0
- package/esm/models/openairesponsesresponsestatus.js +1 -0
- package/esm/models/openairesponsesservicetier.js +1 -0
- package/esm/models/openairesponsestoolchoiceunion.js +1 -0
- package/esm/models/openairesponsestruncation.js +1 -0
- package/esm/models/openresponseseasyinputmessage.js +1 -0
- package/esm/models/openresponseserrorevent.js +1 -0
- package/esm/models/openresponsesfunctioncalloutput.js +1 -0
- package/esm/models/openresponsesfunctiontoolcall.js +1 -0
- package/esm/models/openresponsesimagegencallcompleted.js +1 -0
- package/esm/models/openresponsesimagegencallgenerating.js +1 -0
- package/esm/models/openresponsesimagegencallinprogress.js +1 -0
- package/esm/models/openresponsesimagegencallpartialimage.js +1 -0
- package/esm/models/openresponsesinput.js +1 -0
- package/esm/models/openresponsesinputmessageitem.js +1 -0
- package/esm/models/openresponseslogprobs.js +1 -0
- package/esm/models/openresponsesnonstreamingresponse.js +1 -0
- package/esm/models/openresponsesreasoning.js +1 -0
- package/esm/models/openresponsesreasoningconfig.js +1 -0
- package/esm/models/openresponsesreasoningdeltaevent.js +1 -0
- package/esm/models/openresponsesreasoningdoneevent.js +1 -0
- package/esm/models/openresponsesreasoningsummarypartaddedevent.js +1 -0
- package/esm/models/openresponsesreasoningsummarytextdeltaevent.js +1 -0
- package/esm/models/openresponsesreasoningsummarytextdoneevent.js +1 -0
- package/esm/models/openresponsesrequest.js +1 -0
- package/esm/models/openresponsesresponsetext.js +1 -0
- package/esm/models/openresponsesstreamevent.js +1 -0
- package/esm/models/openresponsestoplogprobs.js +1 -0
- package/esm/models/openresponsesusage.js +1 -0
- package/esm/models/openresponseswebsearch20250826tool.js +1 -0
- package/esm/models/openresponseswebsearchpreview20250311tool.js +1 -0
- package/esm/models/openresponseswebsearchpreviewtool.js +1 -0
- package/esm/models/openresponseswebsearchtool.js +1 -0
- package/esm/models/operations/createauthkeyscode.js +1 -0
- package/esm/models/operations/createcoinbasecharge.js +1 -0
- package/esm/models/operations/createembeddings.js +1 -0
- package/esm/models/operations/createkeys.js +1 -0
- package/esm/models/operations/createresponses.js +1 -0
- package/esm/models/operations/deletekeys.js +1 -0
- package/esm/models/operations/exchangeauthcodeforapikey.js +1 -0
- package/esm/models/operations/getcredits.js +1 -0
- package/esm/models/operations/getcurrentkey.js +1 -0
- package/esm/models/operations/getgeneration.js +1 -0
- package/esm/models/operations/getkey.js +1 -0
- package/esm/models/operations/getmodels.js +1 -0
- package/esm/models/operations/getparameters.js +1 -0
- package/esm/models/operations/getuseractivity.js +1 -0
- package/esm/models/operations/index.js +1 -0
- package/esm/models/operations/list.js +1 -0
- package/esm/models/operations/listendpoints.js +1 -0
- package/esm/models/operations/listendpointszdr.js +1 -0
- package/esm/models/operations/listmodelsuser.js +1 -0
- package/esm/models/operations/listproviders.js +1 -0
- package/esm/models/operations/sendchatcompletionrequest.js +1 -0
- package/esm/models/operations/updatekeys.js +1 -0
- package/esm/models/outputitemimagegenerationcall.js +1 -0
- package/esm/models/outputmessage.js +1 -0
- package/esm/models/outputmodality.js +1 -0
- package/esm/models/parameter.js +1 -0
- package/esm/models/payloadtoolargeresponseerrordata.js +1 -0
- package/esm/models/paymentrequiredresponseerrordata.js +1 -0
- package/esm/models/pdfparserengine.js +1 -0
- package/esm/models/pdfparseroptions.js +1 -0
- package/esm/models/perrequestlimits.js +1 -0
- package/esm/models/providername.js +1 -0
- package/esm/models/provideroverloadedresponseerrordata.js +1 -0
- package/esm/models/providerpreferences.js +1 -0
- package/esm/models/providersort.js +1 -0
- package/esm/models/providersortconfig.js +1 -0
- package/esm/models/providersortunion.js +1 -0
- package/esm/models/publicendpoint.js +1 -0
- package/esm/models/publicpricing.js +1 -0
- package/esm/models/quantization.js +1 -0
- package/esm/models/reasoningsummarytext.js +1 -0
- package/esm/models/reasoningsummaryverbosity.js +1 -0
- package/esm/models/reasoningtextcontent.js +1 -0
- package/esm/models/requesttimeoutresponseerrordata.js +1 -0
- package/esm/models/responseformatjsonschema.js +1 -0
- package/esm/models/responseformattextconfig.js +1 -0
- package/esm/models/responseformattextgrammar.js +1 -0
- package/esm/models/responseinputaudio.js +1 -0
- package/esm/models/responseinputfile.js +1 -0
- package/esm/models/responseinputimage.js +1 -0
- package/esm/models/responseinputtext.js +1 -0
- package/esm/models/responseoutputtext.js +1 -0
- package/esm/models/responseserrorfield.js +1 -0
- package/esm/models/responsesformatjsonobject.js +1 -0
- package/esm/models/responsesformattext.js +1 -0
- package/esm/models/responsesformattextjsonschemaconfig.js +1 -0
- package/esm/models/responsesimagegenerationcall.js +1 -0
- package/esm/models/responsesoutputitem.js +1 -0
- package/esm/models/responsesoutputitemfilesearchcall.js +1 -0
- package/esm/models/responsesoutputitemfunctioncall.js +1 -0
- package/esm/models/responsesoutputitemreasoning.js +1 -0
- package/esm/models/responsesoutputmessage.js +1 -0
- package/esm/models/responsessearchcontextsize.js +1 -0
- package/esm/models/responseswebsearchcalloutput.js +1 -0
- package/esm/models/responseswebsearchuserlocation.js +1 -0
- package/esm/models/responsetextconfig.js +1 -0
- package/esm/models/schema0.js +1 -0
- package/esm/models/schema3.js +1 -0
- package/esm/models/security.js +1 -0
- package/esm/models/serviceunavailableresponseerrordata.js +1 -0
- package/esm/models/systemmessage.js +1 -0
- package/esm/models/toolcallstatus.js +1 -0
- package/esm/models/tooldefinitionjson.js +1 -0
- package/esm/models/toolresponsemessage.js +1 -0
- package/esm/models/toomanyrequestsresponseerrordata.js +1 -0
- package/esm/models/topproviderinfo.js +1 -0
- package/esm/models/unauthorizedresponseerrordata.js +1 -0
- package/esm/models/unprocessableentityresponseerrordata.js +1 -0
- package/esm/models/urlcitation.js +1 -0
- package/esm/models/usermessage.js +1 -0
- package/esm/models/websearchengine.js +1 -0
- package/esm/models/websearchpreviewtooluserlocation.js +1 -0
- package/esm/models/websearchstatus.js +1 -0
- package/esm/sdk/analytics.js +1 -0
- package/esm/sdk/apikeys.js +1 -0
- package/esm/sdk/beta.js +1 -0
- package/esm/sdk/chat.js +1 -0
- package/esm/sdk/completions.js +1 -0
- package/esm/sdk/credits.js +1 -0
- package/esm/sdk/embeddings.js +1 -0
- package/esm/sdk/endpoints.js +1 -0
- package/esm/sdk/generations.js +1 -0
- package/esm/sdk/index.js +1 -0
- package/esm/sdk/models.js +1 -0
- package/esm/sdk/oauth.js +1 -0
- package/esm/sdk/parameters.js +1 -0
- package/esm/sdk/providers.js +1 -0
- package/esm/sdk/responses.js +1 -0
- package/esm/sdk/sdk.d.ts +3 -3
- package/esm/sdk/sdk.js +2 -1
- package/esm/types/async.js +1 -0
- package/esm/types/blobs.js +1 -0
- package/esm/types/constdatetime.js +1 -0
- package/esm/types/discriminatedUnion.js +1 -0
- package/esm/types/enums.js +1 -0
- package/esm/types/fp.js +1 -0
- package/esm/types/index.js +1 -0
- package/esm/types/operations.js +1 -0
- package/esm/types/rfcdate.js +1 -0
- package/esm/types/streams.js +1 -0
- package/esm/types/unrecognized.js +1 -0
- package/jsr.json +1 -1
- package/package.json +1 -1
- package/esm/funcs/callModel.d.ts +0 -87
- package/esm/funcs/callModel.js +0 -215
package/esm/core.js
CHANGED
package/esm/funcs/apiKeysGet.js
CHANGED
package/esm/funcs/apiKeysList.js
CHANGED
|
@@ -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
|
package/esm/funcs/chatSend.js
CHANGED
package/esm/funcs/modelsCount.js
CHANGED
package/esm/funcs/modelsList.js
CHANGED
package/esm/hooks/hooks.js
CHANGED
package/esm/hooks/index.js
CHANGED
package/esm/hooks/types.js
CHANGED
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
|