@openrouter/sdk 0.3.2 → 0.3.10
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/.zed/settings.json +10 -0
- package/_speakeasy/.github/action-inputs-config.json +53 -0
- package/_speakeasy/.github/action-security-config.json +88 -0
- 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 +118 -0
- package/esm/funcs/call-model.js +137 -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 +20 -5
- package/esm/index.js +21 -4
- package/esm/lib/anthropic-compat.d.ts +50 -0
- package/esm/lib/anthropic-compat.js +223 -0
- package/esm/lib/anthropic-compat.test.d.ts +2 -0
- package/esm/lib/anthropic-compat.test.js +479 -0
- package/esm/lib/async-params.d.ts +53 -0
- package/esm/lib/async-params.js +76 -0
- package/esm/lib/base64.js +1 -0
- package/esm/lib/chat-compat.d.ts +46 -0
- package/esm/lib/chat-compat.js +112 -0
- package/esm/lib/chat-compat.test.d.ts +2 -0
- package/esm/lib/chat-compat.test.js +282 -0
- package/esm/lib/claude-constants.d.ts +22 -0
- package/esm/lib/claude-constants.js +20 -0
- package/esm/lib/claude-type-guards.d.ts +10 -0
- package/esm/lib/claude-type-guards.js +70 -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} +29 -43
- package/esm/lib/{response-wrapper.js → model-result.js} +146 -175
- package/esm/lib/next-turn-params.d.ts +30 -0
- package/esm/lib/next-turn-params.js +129 -0
- package/esm/lib/primitives.js +1 -0
- package/esm/lib/retries.js +1 -0
- package/esm/lib/reusable-stream.js +10 -10
- package/esm/lib/schemas.js +1 -0
- package/esm/lib/sdks.js +1 -0
- package/esm/lib/security.js +1 -0
- package/esm/lib/stop-conditions.d.ts +80 -0
- package/esm/lib/stop-conditions.js +104 -0
- package/esm/lib/stream-transformers.d.ts +30 -4
- package/esm/lib/stream-transformers.js +467 -98
- package/esm/lib/stream-type-guards.d.ts +29 -0
- package/esm/lib/stream-type-guards.js +109 -0
- package/esm/lib/tool-executor.d.ts +7 -6
- package/esm/lib/tool-executor.js +4 -0
- package/esm/lib/tool-orchestrator.d.ts +7 -7
- package/esm/lib/tool-orchestrator.js +38 -10
- package/esm/lib/tool-types.d.ts +163 -29
- package/esm/lib/tool-types.js +6 -0
- package/esm/lib/tool.d.ts +99 -0
- package/esm/lib/tool.js +71 -0
- package/esm/lib/turn-context.d.ts +50 -0
- package/esm/lib/turn-context.js +59 -0
- 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 +4 -10
- 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 +6 -3
- package/esm/funcs/callModel.d.ts +0 -87
- package/esm/funcs/callModel.js +0 -215
|
@@ -1,31 +1,37 @@
|
|
|
1
1
|
import type { OpenRouterCore } from '../core.js';
|
|
2
2
|
import type * as models from '../models/index.js';
|
|
3
|
+
import type { CallModelInput } from './async-params.js';
|
|
3
4
|
import type { RequestOptions } from './sdks.js';
|
|
4
|
-
import type {
|
|
5
|
-
export interface GetResponseOptions {
|
|
6
|
-
request:
|
|
5
|
+
import type { ResponseStreamEvent, InferToolEventsUnion, ParsedToolCall, StopWhen, Tool, ToolStreamEvent } from './tool-types.js';
|
|
6
|
+
export interface GetResponseOptions<TTools extends readonly Tool[]> {
|
|
7
|
+
request: CallModelInput<TTools>;
|
|
7
8
|
client: OpenRouterCore;
|
|
8
9
|
options?: RequestOptions;
|
|
9
|
-
tools?:
|
|
10
|
-
|
|
10
|
+
tools?: TTools;
|
|
11
|
+
stopWhen?: StopWhen<TTools>;
|
|
11
12
|
}
|
|
12
13
|
/**
|
|
13
14
|
* A wrapper around a streaming response that provides multiple consumption patterns.
|
|
14
15
|
*
|
|
15
16
|
* 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
|
|
17
|
+
* - `await result.getText()` - Get just the text
|
|
18
|
+
* - `await result.getResponse()` - Get the full response object
|
|
19
|
+
* - `for await (const delta of result.getTextStream())` - Stream text deltas
|
|
20
|
+
* - `for await (const msg of result.getNewMessagesStream())` - Stream incremental message updates
|
|
21
|
+
* - `for await (const event of result.getFullResponsesStream())` - Stream all response events
|
|
22
|
+
*
|
|
23
|
+
* For message format conversion, use the helper functions:
|
|
24
|
+
* - `toChatMessage(response)` for OpenAI chat format
|
|
25
|
+
* - `toClaudeMessage(response)` for Anthropic Claude format
|
|
21
26
|
*
|
|
22
27
|
* All consumption patterns can be used concurrently thanks to the underlying
|
|
23
28
|
* ReusableReadableStream implementation.
|
|
29
|
+
*
|
|
30
|
+
* @template TTools - The tools array type to enable typed tool calls and results
|
|
24
31
|
*/
|
|
25
|
-
export declare class
|
|
32
|
+
export declare class ModelResult<TTools extends readonly Tool[]> {
|
|
26
33
|
private reusableStream;
|
|
27
34
|
private streamPromise;
|
|
28
|
-
private messagePromise;
|
|
29
35
|
private textPromise;
|
|
30
36
|
private options;
|
|
31
37
|
private initPromise;
|
|
@@ -33,7 +39,8 @@ export declare class ResponseWrapper {
|
|
|
33
39
|
private finalResponse;
|
|
34
40
|
private preliminaryResults;
|
|
35
41
|
private allToolExecutionRounds;
|
|
36
|
-
|
|
42
|
+
private resolvedRequest;
|
|
43
|
+
constructor(options: GetResponseOptions<TTools>);
|
|
37
44
|
/**
|
|
38
45
|
* Type guard to check if a value is a non-streaming response
|
|
39
46
|
*/
|
|
@@ -48,20 +55,10 @@ export declare class ResponseWrapper {
|
|
|
48
55
|
* This is idempotent - multiple calls will return the same promise
|
|
49
56
|
*/
|
|
50
57
|
private executeToolsIfNeeded;
|
|
51
|
-
/**
|
|
52
|
-
* Internal helper to get the message after tool execution
|
|
53
|
-
*/
|
|
54
|
-
private getMessageInternal;
|
|
55
58
|
/**
|
|
56
59
|
* Internal helper to get the text after tool execution
|
|
57
60
|
*/
|
|
58
61
|
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
62
|
/**
|
|
66
63
|
* Get just the text content from the response.
|
|
67
64
|
* This will consume the stream until completion, execute any tools, and extract the text.
|
|
@@ -78,19 +75,19 @@ export declare class ResponseWrapper {
|
|
|
78
75
|
* Multiple consumers can iterate over this stream concurrently.
|
|
79
76
|
* Includes preliminary tool result events after tool execution.
|
|
80
77
|
*/
|
|
81
|
-
getFullResponsesStream(): AsyncIterableIterator<
|
|
78
|
+
getFullResponsesStream(): AsyncIterableIterator<ResponseStreamEvent<InferToolEventsUnion<TTools>>>;
|
|
82
79
|
/**
|
|
83
80
|
* Stream only text deltas as they arrive.
|
|
84
81
|
* This filters the full event stream to only yield text content.
|
|
85
82
|
*/
|
|
86
83
|
getTextStream(): AsyncIterableIterator<string>;
|
|
87
84
|
/**
|
|
88
|
-
* Stream incremental message updates as content is added.
|
|
85
|
+
* Stream incremental message updates as content is added in responses format.
|
|
89
86
|
* Each iteration yields an updated version of the message with new content.
|
|
90
|
-
* Also yields
|
|
91
|
-
* Returns
|
|
87
|
+
* Also yields OpenResponsesFunctionCallOutput after tool execution completes.
|
|
88
|
+
* Returns ResponsesOutputMessage or OpenResponsesFunctionCallOutput compatible with OpenAI Responses API format.
|
|
92
89
|
*/
|
|
93
|
-
getNewMessagesStream(): AsyncIterableIterator<models.
|
|
90
|
+
getNewMessagesStream(): AsyncIterableIterator<models.ResponsesOutputMessage | models.OpenResponsesFunctionCallOutput>;
|
|
94
91
|
/**
|
|
95
92
|
* Stream only reasoning deltas as they arrive.
|
|
96
93
|
* This filters the full event stream to only yield reasoning content.
|
|
@@ -102,33 +99,22 @@ export declare class ResponseWrapper {
|
|
|
102
99
|
* - Tool call argument deltas as { type: "delta", content: string }
|
|
103
100
|
* - Preliminary results as { type: "preliminary_result", toolCallId, result }
|
|
104
101
|
*/
|
|
105
|
-
getToolStream(): AsyncIterableIterator<ToolStreamEvent
|
|
106
|
-
/**
|
|
107
|
-
* Stream events in chat format (compatibility layer).
|
|
108
|
-
* Note: This transforms responses API events into a chat-like format.
|
|
109
|
-
* Includes preliminary tool result events after tool execution.
|
|
110
|
-
*
|
|
111
|
-
* @remarks
|
|
112
|
-
* This is a compatibility method that attempts to transform the responses API
|
|
113
|
-
* stream into a format similar to the chat API. Due to differences in the APIs,
|
|
114
|
-
* this may not be a perfect mapping.
|
|
115
|
-
*/
|
|
116
|
-
getFullChatStream(): AsyncIterableIterator<ChatStreamEvent>;
|
|
102
|
+
getToolStream(): AsyncIterableIterator<ToolStreamEvent<InferToolEventsUnion<TTools>>>;
|
|
117
103
|
/**
|
|
118
104
|
* Get all tool calls from the completed response (before auto-execution).
|
|
119
105
|
* Note: If tools have execute functions, they will be automatically executed
|
|
120
106
|
* and this will return the tool calls from the initial response.
|
|
121
107
|
* Returns structured tool calls with parsed arguments.
|
|
122
108
|
*/
|
|
123
|
-
getToolCalls(): Promise<ParsedToolCall[]>;
|
|
109
|
+
getToolCalls(): Promise<ParsedToolCall<TTools[number]>[]>;
|
|
124
110
|
/**
|
|
125
111
|
* Stream structured tool call objects as they're completed.
|
|
126
112
|
* Each iteration yields a complete tool call with parsed arguments.
|
|
127
113
|
*/
|
|
128
|
-
getToolCallsStream(): AsyncIterableIterator<ParsedToolCall
|
|
114
|
+
getToolCallsStream(): AsyncIterableIterator<ParsedToolCall<TTools[number]>>;
|
|
129
115
|
/**
|
|
130
116
|
* Cancel the underlying stream and all consumers
|
|
131
117
|
*/
|
|
132
118
|
cancel(): Promise<void>;
|
|
133
119
|
}
|
|
134
|
-
//# sourceMappingURL=
|
|
120
|
+
//# sourceMappingURL=model-result.d.ts.map
|
|
@@ -1,32 +1,60 @@
|
|
|
1
1
|
import { betaResponsesSend } from '../funcs/betaResponsesSend.js';
|
|
2
|
+
import { hasAsyncFunctions, resolveAsyncFunctions, } from './async-params.js';
|
|
2
3
|
import { ReusableReadableStream } from './reusable-stream.js';
|
|
3
|
-
import {
|
|
4
|
+
import { buildResponsesMessageStream, buildToolCallStream, consumeStreamForCompletion, extractReasoningDeltas, extractResponsesMessageFromResponse, extractTextDeltas, extractTextFromResponse, extractToolCallsFromResponse, extractToolDeltas, } from './stream-transformers.js';
|
|
4
5
|
import { executeTool } from './tool-executor.js';
|
|
6
|
+
import { executeNextTurnParamsFunctions, applyNextTurnParamsToRequest } from './next-turn-params.js';
|
|
5
7
|
import { hasExecuteFunction } from './tool-types.js';
|
|
8
|
+
import { isStopConditionMet } from './stop-conditions.js';
|
|
9
|
+
/**
|
|
10
|
+
* Type guard for stream event with toReadableStream method
|
|
11
|
+
*/
|
|
12
|
+
function isEventStream(value) {
|
|
13
|
+
return (value !== null &&
|
|
14
|
+
typeof value === 'object' &&
|
|
15
|
+
'toReadableStream' in value &&
|
|
16
|
+
typeof value.toReadableStream === 'function');
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Type guard for output items with a type property
|
|
20
|
+
*/
|
|
21
|
+
function hasTypeProperty(item) {
|
|
22
|
+
return (typeof item === 'object' &&
|
|
23
|
+
item !== null &&
|
|
24
|
+
'type' in item &&
|
|
25
|
+
typeof item.type === 'string');
|
|
26
|
+
}
|
|
6
27
|
/**
|
|
7
28
|
* A wrapper around a streaming response that provides multiple consumption patterns.
|
|
8
29
|
*
|
|
9
30
|
* 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
|
|
31
|
+
* - `await result.getText()` - Get just the text
|
|
32
|
+
* - `await result.getResponse()` - Get the full response object
|
|
33
|
+
* - `for await (const delta of result.getTextStream())` - Stream text deltas
|
|
34
|
+
* - `for await (const msg of result.getNewMessagesStream())` - Stream incremental message updates
|
|
35
|
+
* - `for await (const event of result.getFullResponsesStream())` - Stream all response events
|
|
36
|
+
*
|
|
37
|
+
* For message format conversion, use the helper functions:
|
|
38
|
+
* - `toChatMessage(response)` for OpenAI chat format
|
|
39
|
+
* - `toClaudeMessage(response)` for Anthropic Claude format
|
|
15
40
|
*
|
|
16
41
|
* All consumption patterns can be used concurrently thanks to the underlying
|
|
17
42
|
* ReusableReadableStream implementation.
|
|
43
|
+
*
|
|
44
|
+
* @template TTools - The tools array type to enable typed tool calls and results
|
|
18
45
|
*/
|
|
19
|
-
export class
|
|
46
|
+
export class ModelResult {
|
|
20
47
|
constructor(options) {
|
|
21
48
|
this.reusableStream = null;
|
|
22
49
|
this.streamPromise = null;
|
|
23
|
-
this.messagePromise = null;
|
|
24
50
|
this.textPromise = null;
|
|
25
51
|
this.initPromise = null;
|
|
26
52
|
this.toolExecutionPromise = null;
|
|
27
53
|
this.finalResponse = null;
|
|
28
54
|
this.preliminaryResults = new Map();
|
|
29
55
|
this.allToolExecutionRounds = [];
|
|
56
|
+
// Track resolved request after async function resolution
|
|
57
|
+
this.resolvedRequest = null;
|
|
30
58
|
this.options = options;
|
|
31
59
|
}
|
|
32
60
|
/**
|
|
@@ -34,11 +62,11 @@ export class ResponseWrapper {
|
|
|
34
62
|
*/
|
|
35
63
|
isNonStreamingResponse(value) {
|
|
36
64
|
return (value !== null &&
|
|
37
|
-
typeof value ===
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
!(
|
|
65
|
+
typeof value === 'object' &&
|
|
66
|
+
'id' in value &&
|
|
67
|
+
'object' in value &&
|
|
68
|
+
'output' in value &&
|
|
69
|
+
!('toReadableStream' in value));
|
|
42
70
|
}
|
|
43
71
|
/**
|
|
44
72
|
* Initialize the stream if not already started
|
|
@@ -49,16 +77,40 @@ export class ResponseWrapper {
|
|
|
49
77
|
return this.initPromise;
|
|
50
78
|
}
|
|
51
79
|
this.initPromise = (async () => {
|
|
52
|
-
//
|
|
53
|
-
|
|
54
|
-
|
|
80
|
+
// Resolve async functions before initial request
|
|
81
|
+
// Build initial turn context (turn 0 for initial request)
|
|
82
|
+
const initialContext = {
|
|
83
|
+
numberOfTurns: 0,
|
|
84
|
+
};
|
|
85
|
+
// Resolve any async functions first
|
|
86
|
+
let baseRequest;
|
|
87
|
+
if (hasAsyncFunctions(this.options.request)) {
|
|
88
|
+
baseRequest = await resolveAsyncFunctions(this.options.request, initialContext);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
// Already resolved, extract non-function fields
|
|
92
|
+
// Since request is CallModelInput, we need to filter out stopWhen
|
|
93
|
+
// Note: tools are already in API format at this point (converted in callModel())
|
|
94
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
95
|
+
const { stopWhen, ...rest } = this.options.request;
|
|
96
|
+
// Cast to ResolvedCallModelInput - we know it's resolved if hasAsyncFunctions returned false
|
|
97
|
+
baseRequest = rest;
|
|
98
|
+
}
|
|
99
|
+
// Store resolved request with stream mode
|
|
100
|
+
this.resolvedRequest = {
|
|
101
|
+
...baseRequest,
|
|
55
102
|
stream: true,
|
|
56
103
|
};
|
|
104
|
+
// Force stream mode for initial request
|
|
105
|
+
const request = this.resolvedRequest;
|
|
57
106
|
// Create the stream promise
|
|
58
107
|
this.streamPromise = betaResponsesSend(this.options.client, request, this.options.options).then((result) => {
|
|
59
108
|
if (!result.ok) {
|
|
60
109
|
throw result.error;
|
|
61
110
|
}
|
|
111
|
+
// When stream: true, the API returns EventStream
|
|
112
|
+
// TypeScript can't narrow the union type based on runtime parameter values,
|
|
113
|
+
// so we assert the type here based on our knowledge that stream=true
|
|
62
114
|
return result.value;
|
|
63
115
|
});
|
|
64
116
|
// Wait for the stream and create the reusable stream
|
|
@@ -80,12 +132,13 @@ export class ResponseWrapper {
|
|
|
80
132
|
if (!this.reusableStream) {
|
|
81
133
|
throw new Error('Stream not initialized');
|
|
82
134
|
}
|
|
135
|
+
// Note: Async functions already resolved in initStream()
|
|
83
136
|
// Get the initial response
|
|
84
137
|
const initialResponse = await consumeStreamForCompletion(this.reusableStream);
|
|
85
138
|
// Check if we have tools and if auto-execution is enabled
|
|
86
139
|
const shouldAutoExecute = this.options.tools &&
|
|
87
140
|
this.options.tools.length > 0 &&
|
|
88
|
-
initialResponse.output.some((item) =>
|
|
141
|
+
initialResponse.output.some((item) => hasTypeProperty(item) && item.type === 'function_call');
|
|
89
142
|
if (!shouldAutoExecute) {
|
|
90
143
|
// No tools to execute, use initial response
|
|
91
144
|
this.finalResponse = initialResponse;
|
|
@@ -103,12 +156,34 @@ export class ResponseWrapper {
|
|
|
103
156
|
this.finalResponse = initialResponse;
|
|
104
157
|
return;
|
|
105
158
|
}
|
|
106
|
-
// Get maxToolRounds configuration
|
|
107
|
-
const maxToolRounds = this.options.maxToolRounds ?? 5;
|
|
108
159
|
let currentResponse = initialResponse;
|
|
109
160
|
let currentRound = 0;
|
|
110
|
-
let currentInput = this.options.request.input || [];
|
|
111
161
|
while (true) {
|
|
162
|
+
// Check stopWhen conditions
|
|
163
|
+
if (this.options.stopWhen) {
|
|
164
|
+
const stopConditions = Array.isArray(this.options.stopWhen)
|
|
165
|
+
? this.options.stopWhen
|
|
166
|
+
: [this.options.stopWhen];
|
|
167
|
+
const shouldStop = await isStopConditionMet({
|
|
168
|
+
stopConditions,
|
|
169
|
+
steps: this.allToolExecutionRounds.map((round) => ({
|
|
170
|
+
stepType: 'continue',
|
|
171
|
+
text: extractTextFromResponse(round.response),
|
|
172
|
+
toolCalls: round.toolCalls,
|
|
173
|
+
toolResults: round.toolResults.map((tr) => ({
|
|
174
|
+
toolCallId: tr.callId,
|
|
175
|
+
toolName: round.toolCalls.find((tc) => tc.id === tr.callId)?.name ?? '',
|
|
176
|
+
result: JSON.parse(tr.output),
|
|
177
|
+
})),
|
|
178
|
+
response: round.response,
|
|
179
|
+
usage: round.response.usage,
|
|
180
|
+
finishReason: undefined, // OpenResponsesNonStreamingResponse doesn't have finishReason
|
|
181
|
+
})),
|
|
182
|
+
});
|
|
183
|
+
if (shouldStop) {
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
112
187
|
const currentToolCalls = extractToolCallsFromResponse(currentResponse);
|
|
113
188
|
if (currentToolCalls.length === 0) {
|
|
114
189
|
break;
|
|
@@ -120,46 +195,19 @@ export class ResponseWrapper {
|
|
|
120
195
|
if (!hasExecutable) {
|
|
121
196
|
break;
|
|
122
197
|
}
|
|
123
|
-
//
|
|
124
|
-
if (typeof maxToolRounds === 'number') {
|
|
125
|
-
if (currentRound >= maxToolRounds) {
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
else if (typeof maxToolRounds === 'function') {
|
|
130
|
-
// Function signature: (context: TurnContext) => boolean
|
|
131
|
-
const turnContext = {
|
|
132
|
-
numberOfTurns: currentRound + 1,
|
|
133
|
-
messageHistory: currentInput,
|
|
134
|
-
...(this.options.request.model && {
|
|
135
|
-
model: this.options.request.model,
|
|
136
|
-
}),
|
|
137
|
-
...(this.options.request.models && {
|
|
138
|
-
models: this.options.request.models,
|
|
139
|
-
}),
|
|
140
|
-
};
|
|
141
|
-
const shouldContinue = maxToolRounds(turnContext);
|
|
142
|
-
if (!shouldContinue) {
|
|
143
|
-
break;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
// Store execution round info
|
|
147
|
-
this.allToolExecutionRounds.push({
|
|
148
|
-
round: currentRound,
|
|
149
|
-
toolCalls: currentToolCalls,
|
|
150
|
-
response: currentResponse,
|
|
151
|
-
});
|
|
152
|
-
// Build turn context for tool execution
|
|
198
|
+
// Build turn context for this round (for async parameter resolution only)
|
|
153
199
|
const turnContext = {
|
|
154
200
|
numberOfTurns: currentRound + 1, // 1-indexed
|
|
155
|
-
messageHistory: currentInput,
|
|
156
|
-
...(this.options.request.model && {
|
|
157
|
-
model: this.options.request.model,
|
|
158
|
-
}),
|
|
159
|
-
...(this.options.request.models && {
|
|
160
|
-
models: this.options.request.models,
|
|
161
|
-
}),
|
|
162
201
|
};
|
|
202
|
+
// Resolve async functions for this turn
|
|
203
|
+
if (hasAsyncFunctions(this.options.request)) {
|
|
204
|
+
const resolved = await resolveAsyncFunctions(this.options.request, turnContext);
|
|
205
|
+
// Update resolved request with new values
|
|
206
|
+
this.resolvedRequest = {
|
|
207
|
+
...resolved,
|
|
208
|
+
stream: false, // Tool execution turns don't need streaming
|
|
209
|
+
};
|
|
210
|
+
}
|
|
163
211
|
// Execute all tool calls
|
|
164
212
|
const toolResults = [];
|
|
165
213
|
for (const toolCall of currentToolCalls) {
|
|
@@ -183,6 +231,24 @@ export class ResponseWrapper {
|
|
|
183
231
|
: JSON.stringify(result.result),
|
|
184
232
|
});
|
|
185
233
|
}
|
|
234
|
+
// Store execution round info including tool results
|
|
235
|
+
this.allToolExecutionRounds.push({
|
|
236
|
+
round: currentRound,
|
|
237
|
+
toolCalls: currentToolCalls,
|
|
238
|
+
response: currentResponse,
|
|
239
|
+
toolResults,
|
|
240
|
+
});
|
|
241
|
+
// Execute nextTurnParams functions for tools that were called
|
|
242
|
+
if (this.options.tools && currentToolCalls.length > 0) {
|
|
243
|
+
if (!this.resolvedRequest) {
|
|
244
|
+
throw new Error('Request not initialized');
|
|
245
|
+
}
|
|
246
|
+
const computedParams = await executeNextTurnParamsFunctions(currentToolCalls, this.options.tools, this.resolvedRequest);
|
|
247
|
+
// Apply computed parameters to the resolved request for next turn
|
|
248
|
+
if (Object.keys(computedParams).length > 0) {
|
|
249
|
+
this.resolvedRequest = applyNextTurnParamsToRequest(this.resolvedRequest, computedParams);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
186
252
|
// Build new input with tool results
|
|
187
253
|
// For the Responses API, we need to include the tool results in the input
|
|
188
254
|
const newInput = [
|
|
@@ -193,11 +259,12 @@ export class ResponseWrapper {
|
|
|
193
259
|
]),
|
|
194
260
|
...toolResults,
|
|
195
261
|
];
|
|
196
|
-
// Update current input for next iteration
|
|
197
|
-
currentInput = newInput;
|
|
198
262
|
// Make new request with tool results
|
|
263
|
+
if (!this.resolvedRequest) {
|
|
264
|
+
throw new Error('Request not initialized');
|
|
265
|
+
}
|
|
199
266
|
const newRequest = {
|
|
200
|
-
...this.
|
|
267
|
+
...this.resolvedRequest,
|
|
201
268
|
input: newInput,
|
|
202
269
|
stream: false,
|
|
203
270
|
};
|
|
@@ -207,7 +274,7 @@ export class ResponseWrapper {
|
|
|
207
274
|
}
|
|
208
275
|
// Handle the result - it might be a stream or a response
|
|
209
276
|
const value = newResult.value;
|
|
210
|
-
if (value
|
|
277
|
+
if (isEventStream(value)) {
|
|
211
278
|
// It's a stream, consume it
|
|
212
279
|
const stream = new ReusableReadableStream(value);
|
|
213
280
|
currentResponse = await consumeStreamForCompletion(stream);
|
|
@@ -216,54 +283,32 @@ export class ResponseWrapper {
|
|
|
216
283
|
currentResponse = value;
|
|
217
284
|
}
|
|
218
285
|
else {
|
|
219
|
-
throw new Error(
|
|
286
|
+
throw new Error('Unexpected response type from API');
|
|
220
287
|
}
|
|
221
288
|
currentRound++;
|
|
222
289
|
}
|
|
223
290
|
// Validate the final response has required fields
|
|
224
291
|
if (!currentResponse || !currentResponse.id || !currentResponse.output) {
|
|
225
|
-
throw new Error(
|
|
292
|
+
throw new Error('Invalid final response: missing required fields');
|
|
226
293
|
}
|
|
227
294
|
// Ensure the response is in a completed state (has output content)
|
|
228
295
|
if (!Array.isArray(currentResponse.output) || currentResponse.output.length === 0) {
|
|
229
|
-
throw new Error(
|
|
296
|
+
throw new Error('Invalid final response: empty or invalid output');
|
|
230
297
|
}
|
|
231
298
|
this.finalResponse = currentResponse;
|
|
232
299
|
})();
|
|
233
300
|
return this.toolExecutionPromise;
|
|
234
301
|
}
|
|
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
302
|
/**
|
|
246
303
|
* Internal helper to get the text after tool execution
|
|
247
304
|
*/
|
|
248
305
|
async getTextInternal() {
|
|
249
306
|
await this.executeToolsIfNeeded();
|
|
250
307
|
if (!this.finalResponse) {
|
|
251
|
-
throw new Error(
|
|
308
|
+
throw new Error('Response not available');
|
|
252
309
|
}
|
|
253
310
|
return extractTextFromResponse(this.finalResponse);
|
|
254
311
|
}
|
|
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
312
|
/**
|
|
268
313
|
* Get just the text content from the response.
|
|
269
314
|
* This will consume the stream until completion, execute any tools, and extract the text.
|
|
@@ -311,7 +356,7 @@ export class ResponseWrapper {
|
|
|
311
356
|
yield {
|
|
312
357
|
type: 'tool.preliminary_result',
|
|
313
358
|
toolCallId,
|
|
314
|
-
result,
|
|
359
|
+
result: result,
|
|
315
360
|
timestamp: Date.now(),
|
|
316
361
|
};
|
|
317
362
|
}
|
|
@@ -332,10 +377,10 @@ export class ResponseWrapper {
|
|
|
332
377
|
}.call(this);
|
|
333
378
|
}
|
|
334
379
|
/**
|
|
335
|
-
* Stream incremental message updates as content is added.
|
|
380
|
+
* Stream incremental message updates as content is added in responses format.
|
|
336
381
|
* Each iteration yields an updated version of the message with new content.
|
|
337
|
-
* Also yields
|
|
338
|
-
* Returns
|
|
382
|
+
* Also yields OpenResponsesFunctionCallOutput after tool execution completes.
|
|
383
|
+
* Returns ResponsesOutputMessage or OpenResponsesFunctionCallOutput compatible with OpenAI Responses API format.
|
|
339
384
|
*/
|
|
340
385
|
getNewMessagesStream() {
|
|
341
386
|
return async function* () {
|
|
@@ -343,37 +388,22 @@ export class ResponseWrapper {
|
|
|
343
388
|
if (!this.reusableStream) {
|
|
344
389
|
throw new Error('Stream not initialized');
|
|
345
390
|
}
|
|
346
|
-
// First yield
|
|
347
|
-
yield*
|
|
391
|
+
// First yield messages from the stream in responses format
|
|
392
|
+
yield* buildResponsesMessageStream(this.reusableStream);
|
|
348
393
|
// Execute tools if needed
|
|
349
394
|
await this.executeToolsIfNeeded();
|
|
350
|
-
// Yield
|
|
395
|
+
// Yield function call outputs for each executed tool
|
|
351
396
|
for (const round of this.allToolExecutionRounds) {
|
|
352
|
-
for (const
|
|
353
|
-
|
|
354
|
-
const tool = this.options.tools?.find((t) => t.function.name === toolCall.name);
|
|
355
|
-
if (!tool || !hasExecuteFunction(tool)) {
|
|
356
|
-
continue;
|
|
357
|
-
}
|
|
358
|
-
// Get the result from preliminary results or construct from the response
|
|
359
|
-
const prelimResults = this.preliminaryResults.get(toolCall.id);
|
|
360
|
-
const result = prelimResults && prelimResults.length > 0
|
|
361
|
-
? prelimResults[prelimResults.length - 1] // Last result is the final output
|
|
362
|
-
: undefined;
|
|
363
|
-
// Yield tool response message
|
|
364
|
-
yield {
|
|
365
|
-
role: 'tool',
|
|
366
|
-
content: result !== undefined ? JSON.stringify(result) : '',
|
|
367
|
-
toolCallId: toolCall.id,
|
|
368
|
-
};
|
|
397
|
+
for (const toolResult of round.toolResults) {
|
|
398
|
+
yield toolResult;
|
|
369
399
|
}
|
|
370
400
|
}
|
|
371
|
-
// If tools were executed, yield the final
|
|
401
|
+
// If tools were executed, yield the final message (if there is one)
|
|
372
402
|
if (this.finalResponse && this.allToolExecutionRounds.length > 0) {
|
|
373
403
|
// Check if the final response contains a message
|
|
374
|
-
const hasMessage = this.finalResponse.output.some((item) =>
|
|
404
|
+
const hasMessage = this.finalResponse.output.some((item) => hasTypeProperty(item) && item.type === 'message');
|
|
375
405
|
if (hasMessage) {
|
|
376
|
-
yield
|
|
406
|
+
yield extractResponsesMessageFromResponse(this.finalResponse);
|
|
377
407
|
}
|
|
378
408
|
}
|
|
379
409
|
}.call(this);
|
|
@@ -418,66 +448,7 @@ export class ResponseWrapper {
|
|
|
418
448
|
yield {
|
|
419
449
|
type: 'preliminary_result',
|
|
420
450
|
toolCallId,
|
|
421
|
-
result,
|
|
422
|
-
};
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
}.call(this);
|
|
426
|
-
}
|
|
427
|
-
/**
|
|
428
|
-
* Stream events in chat format (compatibility layer).
|
|
429
|
-
* Note: This transforms responses API events into a chat-like format.
|
|
430
|
-
* Includes preliminary tool result events after tool execution.
|
|
431
|
-
*
|
|
432
|
-
* @remarks
|
|
433
|
-
* This is a compatibility method that attempts to transform the responses API
|
|
434
|
-
* stream into a format similar to the chat API. Due to differences in the APIs,
|
|
435
|
-
* this may not be a perfect mapping.
|
|
436
|
-
*/
|
|
437
|
-
getFullChatStream() {
|
|
438
|
-
return async function* () {
|
|
439
|
-
await this.initStream();
|
|
440
|
-
if (!this.reusableStream) {
|
|
441
|
-
throw new Error('Stream not initialized');
|
|
442
|
-
}
|
|
443
|
-
const consumer = this.reusableStream.createConsumer();
|
|
444
|
-
for await (const event of consumer) {
|
|
445
|
-
if (!('type' in event)) {
|
|
446
|
-
continue;
|
|
447
|
-
}
|
|
448
|
-
// Transform responses events to chat-like format
|
|
449
|
-
// This is a simplified transformation - you may need to adjust based on your needs
|
|
450
|
-
if (event.type === 'response.output_text.delta') {
|
|
451
|
-
const deltaEvent = event;
|
|
452
|
-
yield {
|
|
453
|
-
type: 'content.delta',
|
|
454
|
-
delta: deltaEvent.delta,
|
|
455
|
-
};
|
|
456
|
-
}
|
|
457
|
-
else if (event.type === 'response.completed') {
|
|
458
|
-
const completedEvent = event;
|
|
459
|
-
yield {
|
|
460
|
-
type: 'message.complete',
|
|
461
|
-
response: completedEvent.response,
|
|
462
|
-
};
|
|
463
|
-
}
|
|
464
|
-
else {
|
|
465
|
-
// Pass through other events
|
|
466
|
-
yield {
|
|
467
|
-
type: event.type,
|
|
468
|
-
event,
|
|
469
|
-
};
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
// After stream completes, check if tools were executed and emit preliminary results
|
|
473
|
-
await this.executeToolsIfNeeded();
|
|
474
|
-
// Emit all preliminary results
|
|
475
|
-
for (const [toolCallId, results] of this.preliminaryResults) {
|
|
476
|
-
for (const result of results) {
|
|
477
|
-
yield {
|
|
478
|
-
type: 'tool.preliminary_result',
|
|
479
|
-
toolCallId,
|
|
480
|
-
result,
|
|
451
|
+
result: result,
|
|
481
452
|
};
|
|
482
453
|
}
|
|
483
454
|
}
|
|
@@ -519,4 +490,4 @@ export class ResponseWrapper {
|
|
|
519
490
|
}
|
|
520
491
|
}
|
|
521
492
|
}
|
|
522
|
-
//# sourceMappingURL=
|
|
493
|
+
//# sourceMappingURL=model-result.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type * as models from '../models/index.js';
|
|
2
|
+
import type { NextTurnParamsContext, ParsedToolCall, Tool } from './tool-types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Build a NextTurnParamsContext from the current request
|
|
5
|
+
* Extracts relevant fields that can be modified by nextTurnParams functions
|
|
6
|
+
*
|
|
7
|
+
* @param request - The current OpenResponsesRequest
|
|
8
|
+
* @returns Context object with current parameter values
|
|
9
|
+
*/
|
|
10
|
+
export declare function buildNextTurnParamsContext(request: models.OpenResponsesRequest): NextTurnParamsContext;
|
|
11
|
+
/**
|
|
12
|
+
* Execute nextTurnParams functions for all called tools
|
|
13
|
+
* Composes functions when multiple tools modify the same parameter
|
|
14
|
+
*
|
|
15
|
+
* @param toolCalls - Tool calls that were executed in this turn
|
|
16
|
+
* @param tools - All available tools
|
|
17
|
+
* @param currentRequest - The current request
|
|
18
|
+
* @returns Object with computed parameter values
|
|
19
|
+
*/
|
|
20
|
+
export declare function executeNextTurnParamsFunctions(toolCalls: ParsedToolCall<Tool>[], tools: readonly Tool[], currentRequest: models.OpenResponsesRequest): Promise<Partial<NextTurnParamsContext>>;
|
|
21
|
+
/**
|
|
22
|
+
* Apply computed nextTurnParams to the current request
|
|
23
|
+
* Returns a new request object with updated parameters
|
|
24
|
+
*
|
|
25
|
+
* @param request - The current request
|
|
26
|
+
* @param computedParams - Computed parameter values from nextTurnParams functions
|
|
27
|
+
* @returns New request with updated parameters
|
|
28
|
+
*/
|
|
29
|
+
export declare function applyNextTurnParamsToRequest(request: models.OpenResponsesRequest, computedParams: Partial<NextTurnParamsContext>): models.OpenResponsesRequest;
|
|
30
|
+
//# sourceMappingURL=next-turn-params.d.ts.map
|