@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.
- 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 +3 -3
- package/esm/lib/config.js +4 -3
- 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 +2 -1
- 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.d.ts +1 -1
- 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/tsconfig.json +1 -1
- package/esm/funcs/callModel.d.ts +0 -87
- 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,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
|
package/esm/lib/config.d.ts
CHANGED
|
@@ -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.
|
|
49
|
-
readonly genVersion: "2.
|
|
50
|
-
readonly userAgent: "speakeasy-sdk/typescript 0.3.
|
|
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.
|
|
29
|
-
genVersion: "2.
|
|
30
|
-
userAgent: "speakeasy-sdk/typescript 0.3.
|
|
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
package/esm/lib/encodings.js
CHANGED
package/esm/lib/env.js
CHANGED
package/esm/lib/event-streams.js
CHANGED
|
@@ -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
|
|
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
package/esm/lib/http.js
CHANGED
package/esm/lib/logger.js
CHANGED
package/esm/lib/matchers.js
CHANGED
|
@@ -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
|
|
2
|
-
import type * as models from
|
|
3
|
-
import type { RequestOptions } from
|
|
4
|
-
import type { ChatStreamEvent, EnhancedResponseStreamEvent, Tool, MaxToolRounds, ParsedToolCall, ToolStreamEvent } from
|
|
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
|
|
17
|
-
* - `await
|
|
18
|
-
* - `for await (const delta of
|
|
19
|
-
* - `for await (const msg of
|
|
20
|
-
* - `for await (const event of
|
|
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
|
|
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
|
|
91
|
-
* Returns
|
|
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.
|
|
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=
|
|
127
|
+
//# sourceMappingURL=model-result.d.ts.map
|