@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
|
@@ -1,26 +1,60 @@
|
|
|
1
|
-
import { betaResponsesSend } from
|
|
2
|
-
import { ReusableReadableStream } from
|
|
3
|
-
import {
|
|
4
|
-
import { executeTool } from
|
|
5
|
-
import { hasExecuteFunction } from
|
|
1
|
+
import { betaResponsesSend } from "../funcs/betaResponsesSend.js";
|
|
2
|
+
import { ReusableReadableStream } from "./reusable-stream.js";
|
|
3
|
+
import { buildResponsesMessageStream, buildToolCallStream, consumeStreamForCompletion, extractReasoningDeltas, extractResponsesMessageFromResponse, extractTextDeltas, extractTextFromResponse, extractToolCallsFromResponse, extractToolDeltas, } from "./stream-transformers.js";
|
|
4
|
+
import { executeTool } from "./tool-executor.js";
|
|
5
|
+
import { hasExecuteFunction } from "./tool-types.js";
|
|
6
|
+
/**
|
|
7
|
+
* Type guard for stream event with toReadableStream method
|
|
8
|
+
*/
|
|
9
|
+
function isEventStream(value) {
|
|
10
|
+
return (value !== null &&
|
|
11
|
+
typeof value === "object" &&
|
|
12
|
+
"toReadableStream" in value &&
|
|
13
|
+
typeof value.toReadableStream ===
|
|
14
|
+
"function");
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Type guard for response.output_text.delta events
|
|
18
|
+
*/
|
|
19
|
+
function isOutputTextDeltaEvent(event) {
|
|
20
|
+
return "type" in event && event.type === "response.output_text.delta";
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Type guard for response.completed events
|
|
24
|
+
*/
|
|
25
|
+
function isResponseCompletedEvent(event) {
|
|
26
|
+
return "type" in event && event.type === "response.completed";
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Type guard for output items with a type property
|
|
30
|
+
*/
|
|
31
|
+
function hasTypeProperty(item) {
|
|
32
|
+
return (typeof item === "object" &&
|
|
33
|
+
item !== null &&
|
|
34
|
+
"type" in item &&
|
|
35
|
+
typeof item.type === "string");
|
|
36
|
+
}
|
|
6
37
|
/**
|
|
7
38
|
* A wrapper around a streaming response that provides multiple consumption patterns.
|
|
8
39
|
*
|
|
9
40
|
* Allows consuming the response in multiple ways:
|
|
10
|
-
* - `await
|
|
11
|
-
* - `await
|
|
12
|
-
* - `for await (const delta of
|
|
13
|
-
* - `for await (const msg of
|
|
14
|
-
* - `for await (const event of
|
|
41
|
+
* - `await result.getText()` - Get just the text
|
|
42
|
+
* - `await result.getResponse()` - Get the full response object
|
|
43
|
+
* - `for await (const delta of result.getTextStream())` - Stream text deltas
|
|
44
|
+
* - `for await (const msg of result.getNewMessagesStream())` - Stream incremental message updates
|
|
45
|
+
* - `for await (const event of result.getFullResponsesStream())` - Stream all response events
|
|
46
|
+
*
|
|
47
|
+
* For message format conversion, use the helper functions:
|
|
48
|
+
* - `toChatMessage(response)` for OpenAI chat format
|
|
49
|
+
* - `toClaudeMessage(response)` for Anthropic Claude format
|
|
15
50
|
*
|
|
16
51
|
* All consumption patterns can be used concurrently thanks to the underlying
|
|
17
52
|
* ReusableReadableStream implementation.
|
|
18
53
|
*/
|
|
19
|
-
export class
|
|
54
|
+
export class ModelResult {
|
|
20
55
|
constructor(options) {
|
|
21
56
|
this.reusableStream = null;
|
|
22
57
|
this.streamPromise = null;
|
|
23
|
-
this.messagePromise = null;
|
|
24
58
|
this.textPromise = null;
|
|
25
59
|
this.initPromise = null;
|
|
26
60
|
this.toolExecutionPromise = null;
|
|
@@ -78,14 +112,14 @@ export class ResponseWrapper {
|
|
|
78
112
|
this.toolExecutionPromise = (async () => {
|
|
79
113
|
await this.initStream();
|
|
80
114
|
if (!this.reusableStream) {
|
|
81
|
-
throw new Error(
|
|
115
|
+
throw new Error("Stream not initialized");
|
|
82
116
|
}
|
|
83
117
|
// Get the initial response
|
|
84
118
|
const initialResponse = await consumeStreamForCompletion(this.reusableStream);
|
|
85
119
|
// Check if we have tools and if auto-execution is enabled
|
|
86
120
|
const shouldAutoExecute = this.options.tools &&
|
|
87
121
|
this.options.tools.length > 0 &&
|
|
88
|
-
initialResponse.output.some((item) =>
|
|
122
|
+
initialResponse.output.some((item) => hasTypeProperty(item) && item.type === "function_call");
|
|
89
123
|
if (!shouldAutoExecute) {
|
|
90
124
|
// No tools to execute, use initial response
|
|
91
125
|
this.finalResponse = initialResponse;
|
|
@@ -121,12 +155,12 @@ export class ResponseWrapper {
|
|
|
121
155
|
break;
|
|
122
156
|
}
|
|
123
157
|
// Check if we should continue based on maxToolRounds
|
|
124
|
-
if (typeof maxToolRounds ===
|
|
158
|
+
if (typeof maxToolRounds === "number") {
|
|
125
159
|
if (currentRound >= maxToolRounds) {
|
|
126
160
|
break;
|
|
127
161
|
}
|
|
128
162
|
}
|
|
129
|
-
else if (typeof maxToolRounds ===
|
|
163
|
+
else if (typeof maxToolRounds === "function") {
|
|
130
164
|
// Function signature: (context: TurnContext) => boolean
|
|
131
165
|
const turnContext = {
|
|
132
166
|
numberOfTurns: currentRound + 1,
|
|
@@ -169,11 +203,12 @@ export class ResponseWrapper {
|
|
|
169
203
|
}
|
|
170
204
|
const result = await executeTool(tool, toolCall, turnContext);
|
|
171
205
|
// Store preliminary results
|
|
172
|
-
if (result.preliminaryResults &&
|
|
206
|
+
if (result.preliminaryResults &&
|
|
207
|
+
result.preliminaryResults.length > 0) {
|
|
173
208
|
this.preliminaryResults.set(toolCall.id, result.preliminaryResults);
|
|
174
209
|
}
|
|
175
210
|
toolResults.push({
|
|
176
|
-
type:
|
|
211
|
+
type: "function_call_output",
|
|
177
212
|
id: `output_${toolCall.id}`,
|
|
178
213
|
callId: toolCall.id,
|
|
179
214
|
output: result.error
|
|
@@ -188,9 +223,7 @@ export class ResponseWrapper {
|
|
|
188
223
|
const newInput = [
|
|
189
224
|
...(Array.isArray(currentResponse.output)
|
|
190
225
|
? currentResponse.output
|
|
191
|
-
: [
|
|
192
|
-
currentResponse.output,
|
|
193
|
-
]),
|
|
226
|
+
: [currentResponse.output]),
|
|
194
227
|
...toolResults,
|
|
195
228
|
];
|
|
196
229
|
// Update current input for next iteration
|
|
@@ -207,7 +240,7 @@ export class ResponseWrapper {
|
|
|
207
240
|
}
|
|
208
241
|
// Handle the result - it might be a stream or a response
|
|
209
242
|
const value = newResult.value;
|
|
210
|
-
if (value
|
|
243
|
+
if (isEventStream(value)) {
|
|
211
244
|
// It's a stream, consume it
|
|
212
245
|
const stream = new ReusableReadableStream(value);
|
|
213
246
|
currentResponse = await consumeStreamForCompletion(stream);
|
|
@@ -225,23 +258,14 @@ export class ResponseWrapper {
|
|
|
225
258
|
throw new Error("Invalid final response: missing required fields");
|
|
226
259
|
}
|
|
227
260
|
// Ensure the response is in a completed state (has output content)
|
|
228
|
-
if (!Array.isArray(currentResponse.output) ||
|
|
261
|
+
if (!Array.isArray(currentResponse.output) ||
|
|
262
|
+
currentResponse.output.length === 0) {
|
|
229
263
|
throw new Error("Invalid final response: empty or invalid output");
|
|
230
264
|
}
|
|
231
265
|
this.finalResponse = currentResponse;
|
|
232
266
|
})();
|
|
233
267
|
return this.toolExecutionPromise;
|
|
234
268
|
}
|
|
235
|
-
/**
|
|
236
|
-
* Internal helper to get the message after tool execution
|
|
237
|
-
*/
|
|
238
|
-
async getMessageInternal() {
|
|
239
|
-
await this.executeToolsIfNeeded();
|
|
240
|
-
if (!this.finalResponse) {
|
|
241
|
-
throw new Error("Response not available");
|
|
242
|
-
}
|
|
243
|
-
return extractMessageFromResponse(this.finalResponse);
|
|
244
|
-
}
|
|
245
269
|
/**
|
|
246
270
|
* Internal helper to get the text after tool execution
|
|
247
271
|
*/
|
|
@@ -252,18 +276,6 @@ export class ResponseWrapper {
|
|
|
252
276
|
}
|
|
253
277
|
return extractTextFromResponse(this.finalResponse);
|
|
254
278
|
}
|
|
255
|
-
/**
|
|
256
|
-
* Get the completed message from the response.
|
|
257
|
-
* This will consume the stream until completion, execute any tools, and extract the first message.
|
|
258
|
-
* Returns an AssistantMessage in chat format.
|
|
259
|
-
*/
|
|
260
|
-
getMessage() {
|
|
261
|
-
if (this.messagePromise) {
|
|
262
|
-
return this.messagePromise;
|
|
263
|
-
}
|
|
264
|
-
this.messagePromise = this.getMessageInternal();
|
|
265
|
-
return this.messagePromise;
|
|
266
|
-
}
|
|
267
279
|
/**
|
|
268
280
|
* Get just the text content from the response.
|
|
269
281
|
* This will consume the stream until completion, execute any tools, and extract the text.
|
|
@@ -283,7 +295,7 @@ export class ResponseWrapper {
|
|
|
283
295
|
async getResponse() {
|
|
284
296
|
await this.executeToolsIfNeeded();
|
|
285
297
|
if (!this.finalResponse) {
|
|
286
|
-
throw new Error(
|
|
298
|
+
throw new Error("Response not available");
|
|
287
299
|
}
|
|
288
300
|
return this.finalResponse;
|
|
289
301
|
}
|
|
@@ -296,7 +308,7 @@ export class ResponseWrapper {
|
|
|
296
308
|
return async function* () {
|
|
297
309
|
await this.initStream();
|
|
298
310
|
if (!this.reusableStream) {
|
|
299
|
-
throw new Error(
|
|
311
|
+
throw new Error("Stream not initialized");
|
|
300
312
|
}
|
|
301
313
|
const consumer = this.reusableStream.createConsumer();
|
|
302
314
|
// Yield original events directly
|
|
@@ -309,7 +321,7 @@ export class ResponseWrapper {
|
|
|
309
321
|
for (const [toolCallId, results] of this.preliminaryResults) {
|
|
310
322
|
for (const result of results) {
|
|
311
323
|
yield {
|
|
312
|
-
type:
|
|
324
|
+
type: "tool.preliminary_result",
|
|
313
325
|
toolCallId,
|
|
314
326
|
result,
|
|
315
327
|
timestamp: Date.now(),
|
|
@@ -326,28 +338,28 @@ export class ResponseWrapper {
|
|
|
326
338
|
return async function* () {
|
|
327
339
|
await this.initStream();
|
|
328
340
|
if (!this.reusableStream) {
|
|
329
|
-
throw new Error(
|
|
341
|
+
throw new Error("Stream not initialized");
|
|
330
342
|
}
|
|
331
343
|
yield* extractTextDeltas(this.reusableStream);
|
|
332
344
|
}.call(this);
|
|
333
345
|
}
|
|
334
346
|
/**
|
|
335
|
-
* Stream incremental message updates as content is added.
|
|
347
|
+
* Stream incremental message updates as content is added in responses format.
|
|
336
348
|
* Each iteration yields an updated version of the message with new content.
|
|
337
|
-
* Also yields
|
|
338
|
-
* Returns
|
|
349
|
+
* Also yields OpenResponsesFunctionCallOutput after tool execution completes.
|
|
350
|
+
* Returns ResponsesOutputMessage or OpenResponsesFunctionCallOutput compatible with OpenAI Responses API format.
|
|
339
351
|
*/
|
|
340
352
|
getNewMessagesStream() {
|
|
341
353
|
return async function* () {
|
|
342
354
|
await this.initStream();
|
|
343
355
|
if (!this.reusableStream) {
|
|
344
|
-
throw new Error(
|
|
356
|
+
throw new Error("Stream not initialized");
|
|
345
357
|
}
|
|
346
|
-
// First yield
|
|
347
|
-
yield*
|
|
358
|
+
// First yield messages from the stream in responses format
|
|
359
|
+
yield* buildResponsesMessageStream(this.reusableStream);
|
|
348
360
|
// Execute tools if needed
|
|
349
361
|
await this.executeToolsIfNeeded();
|
|
350
|
-
// Yield
|
|
362
|
+
// Yield function call output for each executed tool
|
|
351
363
|
for (const round of this.allToolExecutionRounds) {
|
|
352
364
|
for (const toolCall of round.toolCalls) {
|
|
353
365
|
// Find the tool to check if it was executed
|
|
@@ -360,20 +372,21 @@ export class ResponseWrapper {
|
|
|
360
372
|
const result = prelimResults && prelimResults.length > 0
|
|
361
373
|
? prelimResults[prelimResults.length - 1] // Last result is the final output
|
|
362
374
|
: undefined;
|
|
363
|
-
// Yield
|
|
375
|
+
// Yield function call output in responses format
|
|
364
376
|
yield {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
377
|
+
type: "function_call_output",
|
|
378
|
+
id: `output_${toolCall.id}`,
|
|
379
|
+
callId: toolCall.id,
|
|
380
|
+
output: result !== undefined ? JSON.stringify(result) : "",
|
|
368
381
|
};
|
|
369
382
|
}
|
|
370
383
|
}
|
|
371
|
-
// If tools were executed, yield the final
|
|
384
|
+
// If tools were executed, yield the final message (if there is one)
|
|
372
385
|
if (this.finalResponse && this.allToolExecutionRounds.length > 0) {
|
|
373
386
|
// Check if the final response contains a message
|
|
374
|
-
const hasMessage = this.finalResponse.output.some((item) =>
|
|
387
|
+
const hasMessage = this.finalResponse.output.some((item) => hasTypeProperty(item) && item.type === "message");
|
|
375
388
|
if (hasMessage) {
|
|
376
|
-
yield
|
|
389
|
+
yield extractResponsesMessageFromResponse(this.finalResponse);
|
|
377
390
|
}
|
|
378
391
|
}
|
|
379
392
|
}.call(this);
|
|
@@ -386,7 +399,7 @@ export class ResponseWrapper {
|
|
|
386
399
|
return async function* () {
|
|
387
400
|
await this.initStream();
|
|
388
401
|
if (!this.reusableStream) {
|
|
389
|
-
throw new Error(
|
|
402
|
+
throw new Error("Stream not initialized");
|
|
390
403
|
}
|
|
391
404
|
yield* extractReasoningDeltas(this.reusableStream);
|
|
392
405
|
}.call(this);
|
|
@@ -401,12 +414,12 @@ export class ResponseWrapper {
|
|
|
401
414
|
return async function* () {
|
|
402
415
|
await this.initStream();
|
|
403
416
|
if (!this.reusableStream) {
|
|
404
|
-
throw new Error(
|
|
417
|
+
throw new Error("Stream not initialized");
|
|
405
418
|
}
|
|
406
419
|
// Yield tool deltas as structured events
|
|
407
420
|
for await (const delta of extractToolDeltas(this.reusableStream)) {
|
|
408
421
|
yield {
|
|
409
|
-
type:
|
|
422
|
+
type: "delta",
|
|
410
423
|
content: delta,
|
|
411
424
|
};
|
|
412
425
|
}
|
|
@@ -416,7 +429,7 @@ export class ResponseWrapper {
|
|
|
416
429
|
for (const [toolCallId, results] of this.preliminaryResults) {
|
|
417
430
|
for (const result of results) {
|
|
418
431
|
yield {
|
|
419
|
-
type:
|
|
432
|
+
type: "preliminary_result",
|
|
420
433
|
toolCallId,
|
|
421
434
|
result,
|
|
422
435
|
};
|
|
@@ -438,27 +451,24 @@ export class ResponseWrapper {
|
|
|
438
451
|
return async function* () {
|
|
439
452
|
await this.initStream();
|
|
440
453
|
if (!this.reusableStream) {
|
|
441
|
-
throw new Error(
|
|
454
|
+
throw new Error("Stream not initialized");
|
|
442
455
|
}
|
|
443
456
|
const consumer = this.reusableStream.createConsumer();
|
|
444
457
|
for await (const event of consumer) {
|
|
445
|
-
if (!(
|
|
458
|
+
if (!("type" in event)) {
|
|
446
459
|
continue;
|
|
447
460
|
}
|
|
448
|
-
// Transform responses events to chat-like format
|
|
449
|
-
|
|
450
|
-
if (event.type === 'response.output_text.delta') {
|
|
451
|
-
const deltaEvent = event;
|
|
461
|
+
// Transform responses events to chat-like format using type guards
|
|
462
|
+
if (isOutputTextDeltaEvent(event)) {
|
|
452
463
|
yield {
|
|
453
|
-
type:
|
|
454
|
-
delta:
|
|
464
|
+
type: "content.delta",
|
|
465
|
+
delta: event.delta,
|
|
455
466
|
};
|
|
456
467
|
}
|
|
457
|
-
else if (event
|
|
458
|
-
const completedEvent = event;
|
|
468
|
+
else if (isResponseCompletedEvent(event)) {
|
|
459
469
|
yield {
|
|
460
|
-
type:
|
|
461
|
-
response:
|
|
470
|
+
type: "message.complete",
|
|
471
|
+
response: event.response,
|
|
462
472
|
};
|
|
463
473
|
}
|
|
464
474
|
else {
|
|
@@ -475,7 +485,7 @@ export class ResponseWrapper {
|
|
|
475
485
|
for (const [toolCallId, results] of this.preliminaryResults) {
|
|
476
486
|
for (const result of results) {
|
|
477
487
|
yield {
|
|
478
|
-
type:
|
|
488
|
+
type: "tool.preliminary_result",
|
|
479
489
|
toolCallId,
|
|
480
490
|
result,
|
|
481
491
|
};
|
|
@@ -492,7 +502,7 @@ export class ResponseWrapper {
|
|
|
492
502
|
async getToolCalls() {
|
|
493
503
|
await this.initStream();
|
|
494
504
|
if (!this.reusableStream) {
|
|
495
|
-
throw new Error(
|
|
505
|
+
throw new Error("Stream not initialized");
|
|
496
506
|
}
|
|
497
507
|
const completedResponse = await consumeStreamForCompletion(this.reusableStream);
|
|
498
508
|
return extractToolCallsFromResponse(completedResponse);
|
|
@@ -505,7 +515,7 @@ export class ResponseWrapper {
|
|
|
505
515
|
return async function* () {
|
|
506
516
|
await this.initStream();
|
|
507
517
|
if (!this.reusableStream) {
|
|
508
|
-
throw new Error(
|
|
518
|
+
throw new Error("Stream not initialized");
|
|
509
519
|
}
|
|
510
520
|
yield* buildToolCallStream(this.reusableStream);
|
|
511
521
|
}.call(this);
|
|
@@ -519,4 +529,4 @@ export class ResponseWrapper {
|
|
|
519
529
|
}
|
|
520
530
|
}
|
|
521
531
|
}
|
|
522
|
-
//# sourceMappingURL=
|
|
532
|
+
//# sourceMappingURL=model-result.js.map
|
package/esm/lib/primitives.js
CHANGED
package/esm/lib/retries.js
CHANGED
package/esm/lib/schemas.js
CHANGED
package/esm/lib/sdks.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
3
|
+
* @generated-id: 8a6d91f1218d
|
|
3
4
|
*/
|
|
4
5
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
5
6
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
package/esm/lib/security.js
CHANGED
|
@@ -13,6 +13,11 @@ export declare function extractReasoningDeltas(stream: ReusableReadableStream<mo
|
|
|
13
13
|
* Extract tool call argument deltas from responses stream events
|
|
14
14
|
*/
|
|
15
15
|
export declare function extractToolDeltas(stream: ReusableReadableStream<models.OpenResponsesStreamEvent>): AsyncIterableIterator<string>;
|
|
16
|
+
/**
|
|
17
|
+
* Build incremental message updates from responses stream events
|
|
18
|
+
* Returns ResponsesOutputMessage (assistant/responses format)
|
|
19
|
+
*/
|
|
20
|
+
export declare function buildResponsesMessageStream(stream: ReusableReadableStream<models.OpenResponsesStreamEvent>): AsyncIterableIterator<models.ResponsesOutputMessage>;
|
|
16
21
|
/**
|
|
17
22
|
* Build incremental message updates from responses stream events
|
|
18
23
|
* Returns AssistantMessage (chat format) instead of ResponsesOutputMessage
|
|
@@ -23,9 +28,13 @@ export declare function buildMessageStream(stream: ReusableReadableStream<models
|
|
|
23
28
|
*/
|
|
24
29
|
export declare function consumeStreamForCompletion(stream: ReusableReadableStream<models.OpenResponsesStreamEvent>): Promise<models.OpenResponsesNonStreamingResponse>;
|
|
25
30
|
/**
|
|
26
|
-
* Extract the first message from a completed response
|
|
31
|
+
* Extract the first message from a completed response (chat format)
|
|
27
32
|
*/
|
|
28
33
|
export declare function extractMessageFromResponse(response: models.OpenResponsesNonStreamingResponse): models.AssistantMessage;
|
|
34
|
+
/**
|
|
35
|
+
* Extract the first message from a completed response (responses format)
|
|
36
|
+
*/
|
|
37
|
+
export declare function extractResponsesMessageFromResponse(response: models.OpenResponsesNonStreamingResponse): models.ResponsesOutputMessage;
|
|
29
38
|
/**
|
|
30
39
|
* Extract text from a response, either from outputText or by concatenating message content
|
|
31
40
|
*/
|
|
@@ -44,4 +53,21 @@ export declare function buildToolCallStream(stream: ReusableReadableStream<model
|
|
|
44
53
|
* Check if a response contains any tool calls
|
|
45
54
|
*/
|
|
46
55
|
export declare function responseHasToolCalls(response: models.OpenResponsesNonStreamingResponse): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Convert OpenResponsesNonStreamingResponse to ClaudeMessage format
|
|
58
|
+
* Compatible with the Anthropic SDK BetaMessage type
|
|
59
|
+
*/
|
|
60
|
+
export declare function convertToClaudeMessage(response: models.OpenResponsesNonStreamingResponse): models.ClaudeMessage;
|
|
61
|
+
/**
|
|
62
|
+
* Extract unsupported content by original type
|
|
63
|
+
*/
|
|
64
|
+
export declare function extractUnsupportedContent(message: models.ClaudeMessage, originalType: string): models.UnsupportedContent[];
|
|
65
|
+
/**
|
|
66
|
+
* Check if message has any unsupported content
|
|
67
|
+
*/
|
|
68
|
+
export declare function hasUnsupportedContent(message: models.ClaudeMessage): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Get summary of unsupported content types
|
|
71
|
+
*/
|
|
72
|
+
export declare function getUnsupportedContentSummary(message: models.ClaudeMessage): Record<string, number>;
|
|
47
73
|
//# sourceMappingURL=stream-transformers.d.ts.map
|