@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
package/jsr.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openrouter/sdk",
|
|
3
|
-
"version": "0.3.
|
|
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
package/esm/funcs/callModel.d.ts
DELETED
|
@@ -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
|
package/esm/funcs/callModel.js
DELETED
|
@@ -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
|