@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
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type * as models from '../models/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Type guards for OpenResponses stream events
|
|
4
|
+
* These enable proper TypeScript narrowing without type casts
|
|
5
|
+
*/
|
|
6
|
+
export declare function isOutputTextDeltaEvent(event: models.OpenResponsesStreamEvent): event is models.OpenResponsesStreamEventResponseOutputTextDelta;
|
|
7
|
+
export declare function isReasoningDeltaEvent(event: models.OpenResponsesStreamEvent): event is models.OpenResponsesReasoningDeltaEvent;
|
|
8
|
+
export declare function isFunctionCallArgumentsDeltaEvent(event: models.OpenResponsesStreamEvent): event is models.OpenResponsesStreamEventResponseFunctionCallArgumentsDelta;
|
|
9
|
+
export declare function isOutputItemAddedEvent(event: models.OpenResponsesStreamEvent): event is models.OpenResponsesStreamEventResponseOutputItemAdded;
|
|
10
|
+
export declare function isOutputItemDoneEvent(event: models.OpenResponsesStreamEvent): event is models.OpenResponsesStreamEventResponseOutputItemDone;
|
|
11
|
+
export declare function isResponseCompletedEvent(event: models.OpenResponsesStreamEvent): event is models.OpenResponsesStreamEventResponseCompleted;
|
|
12
|
+
export declare function isResponseFailedEvent(event: models.OpenResponsesStreamEvent): event is models.OpenResponsesStreamEventResponseFailed;
|
|
13
|
+
export declare function isResponseIncompleteEvent(event: models.OpenResponsesStreamEvent): event is models.OpenResponsesStreamEventResponseIncomplete;
|
|
14
|
+
export declare function isFunctionCallArgumentsDoneEvent(event: models.OpenResponsesStreamEvent): event is models.OpenResponsesStreamEventResponseFunctionCallArgumentsDone;
|
|
15
|
+
export declare function isOutputMessage(item: unknown): item is models.ResponsesOutputMessage;
|
|
16
|
+
export declare function isFunctionCallOutputItem(item: unknown): item is models.ResponsesOutputItemFunctionCall;
|
|
17
|
+
export declare function isReasoningOutputItem(item: unknown): item is models.ResponsesOutputItemReasoning;
|
|
18
|
+
export declare function isWebSearchCallOutputItem(item: unknown): item is models.ResponsesWebSearchCallOutput;
|
|
19
|
+
export declare function isFileSearchCallOutputItem(item: unknown): item is models.ResponsesOutputItemFileSearchCall;
|
|
20
|
+
export declare function isImageGenerationCallOutputItem(item: unknown): item is models.ResponsesImageGenerationCall;
|
|
21
|
+
export declare function isOutputTextPart(part: unknown): part is models.ResponseOutputText;
|
|
22
|
+
export declare function isRefusalPart(part: unknown): part is models.OpenAIResponsesRefusalContent;
|
|
23
|
+
export declare function isFileCitationAnnotation(annotation: unknown): annotation is models.FileCitation;
|
|
24
|
+
export declare function isURLCitationAnnotation(annotation: unknown): annotation is models.URLCitation;
|
|
25
|
+
export declare function isFilePathAnnotation(annotation: unknown): annotation is models.FilePath;
|
|
26
|
+
export declare function hasTypeProperty(item: unknown): item is {
|
|
27
|
+
type: string;
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=stream-type-guards.d.ts.map
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guards for OpenResponses stream events
|
|
3
|
+
* These enable proper TypeScript narrowing without type casts
|
|
4
|
+
*/
|
|
5
|
+
// Stream event type guards
|
|
6
|
+
export function isOutputTextDeltaEvent(event) {
|
|
7
|
+
return 'type' in event && event.type === 'response.output_text.delta';
|
|
8
|
+
}
|
|
9
|
+
export function isReasoningDeltaEvent(event) {
|
|
10
|
+
return 'type' in event && event.type === 'response.reasoning_text.delta';
|
|
11
|
+
}
|
|
12
|
+
export function isFunctionCallArgumentsDeltaEvent(event) {
|
|
13
|
+
return 'type' in event && event.type === 'response.function_call_arguments.delta';
|
|
14
|
+
}
|
|
15
|
+
export function isOutputItemAddedEvent(event) {
|
|
16
|
+
return 'type' in event && event.type === 'response.output_item.added';
|
|
17
|
+
}
|
|
18
|
+
export function isOutputItemDoneEvent(event) {
|
|
19
|
+
return 'type' in event && event.type === 'response.output_item.done';
|
|
20
|
+
}
|
|
21
|
+
export function isResponseCompletedEvent(event) {
|
|
22
|
+
return 'type' in event && event.type === 'response.completed';
|
|
23
|
+
}
|
|
24
|
+
export function isResponseFailedEvent(event) {
|
|
25
|
+
return 'type' in event && event.type === 'response.failed';
|
|
26
|
+
}
|
|
27
|
+
export function isResponseIncompleteEvent(event) {
|
|
28
|
+
return 'type' in event && event.type === 'response.incomplete';
|
|
29
|
+
}
|
|
30
|
+
export function isFunctionCallArgumentsDoneEvent(event) {
|
|
31
|
+
return 'type' in event && event.type === 'response.function_call_arguments.done';
|
|
32
|
+
}
|
|
33
|
+
// Output item type guards
|
|
34
|
+
export function isOutputMessage(item) {
|
|
35
|
+
return (typeof item === 'object' &&
|
|
36
|
+
item !== null &&
|
|
37
|
+
'type' in item &&
|
|
38
|
+
item.type === 'message');
|
|
39
|
+
}
|
|
40
|
+
export function isFunctionCallOutputItem(item) {
|
|
41
|
+
return (typeof item === 'object' &&
|
|
42
|
+
item !== null &&
|
|
43
|
+
'type' in item &&
|
|
44
|
+
item.type === 'function_call');
|
|
45
|
+
}
|
|
46
|
+
export function isReasoningOutputItem(item) {
|
|
47
|
+
return (typeof item === 'object' &&
|
|
48
|
+
item !== null &&
|
|
49
|
+
'type' in item &&
|
|
50
|
+
item.type === 'reasoning');
|
|
51
|
+
}
|
|
52
|
+
export function isWebSearchCallOutputItem(item) {
|
|
53
|
+
return (typeof item === 'object' &&
|
|
54
|
+
item !== null &&
|
|
55
|
+
'type' in item &&
|
|
56
|
+
item.type === 'web_search_call');
|
|
57
|
+
}
|
|
58
|
+
export function isFileSearchCallOutputItem(item) {
|
|
59
|
+
return (typeof item === 'object' &&
|
|
60
|
+
item !== null &&
|
|
61
|
+
'type' in item &&
|
|
62
|
+
item.type === 'file_search_call');
|
|
63
|
+
}
|
|
64
|
+
export function isImageGenerationCallOutputItem(item) {
|
|
65
|
+
return (typeof item === 'object' &&
|
|
66
|
+
item !== null &&
|
|
67
|
+
'type' in item &&
|
|
68
|
+
item.type === 'image_generation_call');
|
|
69
|
+
}
|
|
70
|
+
// Content part type guards
|
|
71
|
+
export function isOutputTextPart(part) {
|
|
72
|
+
return (typeof part === 'object' &&
|
|
73
|
+
part !== null &&
|
|
74
|
+
'type' in part &&
|
|
75
|
+
part.type === 'output_text');
|
|
76
|
+
}
|
|
77
|
+
export function isRefusalPart(part) {
|
|
78
|
+
return (typeof part === 'object' &&
|
|
79
|
+
part !== null &&
|
|
80
|
+
'type' in part &&
|
|
81
|
+
part.type === 'refusal');
|
|
82
|
+
}
|
|
83
|
+
// Annotation type guards for Claude conversion
|
|
84
|
+
export function isFileCitationAnnotation(annotation) {
|
|
85
|
+
return (typeof annotation === 'object' &&
|
|
86
|
+
annotation !== null &&
|
|
87
|
+
'type' in annotation &&
|
|
88
|
+
annotation.type === 'file_citation');
|
|
89
|
+
}
|
|
90
|
+
export function isURLCitationAnnotation(annotation) {
|
|
91
|
+
return (typeof annotation === 'object' &&
|
|
92
|
+
annotation !== null &&
|
|
93
|
+
'type' in annotation &&
|
|
94
|
+
annotation.type === 'url_citation');
|
|
95
|
+
}
|
|
96
|
+
export function isFilePathAnnotation(annotation) {
|
|
97
|
+
return (typeof annotation === 'object' &&
|
|
98
|
+
annotation !== null &&
|
|
99
|
+
'type' in annotation &&
|
|
100
|
+
annotation.type === 'file_path');
|
|
101
|
+
}
|
|
102
|
+
// Helper to check if output has a type property
|
|
103
|
+
export function hasTypeProperty(item) {
|
|
104
|
+
return (typeof item === 'object' &&
|
|
105
|
+
item !== null &&
|
|
106
|
+
'type' in item &&
|
|
107
|
+
typeof item.type === 'string');
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=stream-type-guards.js.map
|
|
@@ -6,8 +6,9 @@ import type { APITool, Tool, ParsedToolCall, ToolExecutionResult, TurnContext }
|
|
|
6
6
|
export declare function convertZodToJsonSchema(zodSchema: ZodType): Record<string, unknown>;
|
|
7
7
|
/**
|
|
8
8
|
* Convert tools to OpenRouter API format
|
|
9
|
+
* Accepts readonly arrays for better type compatibility
|
|
9
10
|
*/
|
|
10
|
-
export declare function convertToolsToAPIFormat(tools: Tool[]): APITool[];
|
|
11
|
+
export declare function convertToolsToAPIFormat(tools: readonly Tool[]): APITool[];
|
|
11
12
|
/**
|
|
12
13
|
* Validate tool input against Zod schema
|
|
13
14
|
* @throws ZodError if validation fails
|
|
@@ -25,19 +26,19 @@ export declare function parseToolCallArguments(argumentsString: string): unknown
|
|
|
25
26
|
/**
|
|
26
27
|
* Execute a regular (non-generator) tool
|
|
27
28
|
*/
|
|
28
|
-
export declare function executeRegularTool(tool: Tool, toolCall: ParsedToolCall
|
|
29
|
+
export declare function executeRegularTool(tool: Tool, toolCall: ParsedToolCall<Tool>, context: TurnContext): Promise<ToolExecutionResult<Tool>>;
|
|
29
30
|
/**
|
|
30
31
|
* Execute a generator tool and collect preliminary and final results
|
|
31
32
|
* - Intermediate yields are validated against eventSchema (preliminary events)
|
|
32
33
|
* - Last yield is validated against outputSchema (final result sent to model)
|
|
33
34
|
* - Generator must emit at least one value
|
|
34
35
|
*/
|
|
35
|
-
export declare function executeGeneratorTool(tool: Tool, toolCall: ParsedToolCall
|
|
36
|
+
export declare function executeGeneratorTool(tool: Tool, toolCall: ParsedToolCall<Tool>, context: TurnContext, onPreliminaryResult?: (toolCallId: string, result: unknown) => void): Promise<ToolExecutionResult<Tool>>;
|
|
36
37
|
/**
|
|
37
38
|
* Execute a tool call
|
|
38
39
|
* Automatically detects if it's a regular or generator tool
|
|
39
40
|
*/
|
|
40
|
-
export declare function executeTool(tool: Tool, toolCall: ParsedToolCall
|
|
41
|
+
export declare function executeTool(tool: Tool, toolCall: ParsedToolCall<Tool>, context: TurnContext, onPreliminaryResult?: (toolCallId: string, result: unknown) => void): Promise<ToolExecutionResult<Tool>>;
|
|
41
42
|
/**
|
|
42
43
|
* Find a tool by name in the tools array
|
|
43
44
|
*/
|
|
@@ -45,9 +46,9 @@ export declare function findToolByName(tools: Tool[], name: string): Tool | unde
|
|
|
45
46
|
/**
|
|
46
47
|
* Format tool execution result as a string for sending to the model
|
|
47
48
|
*/
|
|
48
|
-
export declare function formatToolResultForModel(result: ToolExecutionResult): string;
|
|
49
|
+
export declare function formatToolResultForModel(result: ToolExecutionResult<Tool>): string;
|
|
49
50
|
/**
|
|
50
51
|
* Create a user-friendly error message for tool execution errors
|
|
51
52
|
*/
|
|
52
|
-
export declare function formatToolExecutionError(error: Error, toolCall: ParsedToolCall): string;
|
|
53
|
+
export declare function formatToolExecutionError(error: Error, toolCall: ParsedToolCall<Tool>): string;
|
|
53
54
|
//# sourceMappingURL=tool-executor.d.ts.map
|
package/esm/lib/tool-executor.js
CHANGED
|
@@ -11,6 +11,7 @@ export function convertZodToJsonSchema(zodSchema) {
|
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
13
13
|
* Convert tools to OpenRouter API format
|
|
14
|
+
* Accepts readonly arrays for better type compatibility
|
|
14
15
|
*/
|
|
15
16
|
export function convertToolsToAPIFormat(tools) {
|
|
16
17
|
return tools.map((tool) => ({
|
|
@@ -55,6 +56,8 @@ export async function executeRegularTool(tool, toolCall, context) {
|
|
|
55
56
|
}
|
|
56
57
|
try {
|
|
57
58
|
// Validate input - the schema validation ensures type safety at runtime
|
|
59
|
+
// validateToolInput returns z.infer<typeof tool.function.inputSchema>
|
|
60
|
+
// which is exactly the type expected by execute
|
|
58
61
|
const validatedInput = validateToolInput(tool.function.inputSchema, toolCall.arguments);
|
|
59
62
|
// Execute tool with context
|
|
60
63
|
const result = await Promise.resolve(tool.function.execute(validatedInput, context));
|
|
@@ -94,6 +97,7 @@ export async function executeGeneratorTool(tool, toolCall, context, onPreliminar
|
|
|
94
97
|
}
|
|
95
98
|
try {
|
|
96
99
|
// Validate input - the schema validation ensures type safety at runtime
|
|
100
|
+
// The inputSchema's inferred type matches the execute function's parameter type by construction
|
|
97
101
|
const validatedInput = validateToolInput(tool.function.inputSchema, toolCall.arguments);
|
|
98
102
|
// Execute generator and collect all results
|
|
99
103
|
const preliminaryResults = [];
|
|
@@ -4,7 +4,6 @@ import type { APITool, Tool, ToolExecutionResult } from './tool-types.js';
|
|
|
4
4
|
* Options for tool execution
|
|
5
5
|
*/
|
|
6
6
|
export interface ToolExecutionOptions {
|
|
7
|
-
maxRounds?: number;
|
|
8
7
|
onPreliminaryResult?: (toolCallId: string, result: unknown) => void;
|
|
9
8
|
}
|
|
10
9
|
/**
|
|
@@ -13,7 +12,7 @@ export interface ToolExecutionOptions {
|
|
|
13
12
|
export interface ToolOrchestrationResult {
|
|
14
13
|
finalResponse: models.OpenResponsesNonStreamingResponse;
|
|
15
14
|
allResponses: models.OpenResponsesNonStreamingResponse[];
|
|
16
|
-
toolExecutionResults: ToolExecutionResult[];
|
|
15
|
+
toolExecutionResults: ToolExecutionResult<Tool>[];
|
|
17
16
|
conversationInput: models.OpenResponsesInput;
|
|
18
17
|
}
|
|
19
18
|
/**
|
|
@@ -22,29 +21,30 @@ export interface ToolOrchestrationResult {
|
|
|
22
21
|
*
|
|
23
22
|
* @param sendRequest - Function to send a request and get a response
|
|
24
23
|
* @param initialInput - Starting input for the conversation
|
|
24
|
+
* @param initialRequest - Full initial request with all parameters
|
|
25
25
|
* @param tools - Enhanced tools with Zod schemas and execute functions
|
|
26
26
|
* @param apiTools - Converted tools in API format (JSON Schema)
|
|
27
27
|
* @param options - Execution options
|
|
28
28
|
* @returns Result containing final response and all execution data
|
|
29
29
|
*/
|
|
30
|
-
export declare function executeToolLoop(sendRequest: (input: models.OpenResponsesInput, tools: APITool[]) => Promise<models.OpenResponsesNonStreamingResponse>, initialInput: models.OpenResponsesInput, tools: Tool[], apiTools: APITool[], options?: ToolExecutionOptions): Promise<ToolOrchestrationResult>;
|
|
30
|
+
export declare function executeToolLoop(sendRequest: (input: models.OpenResponsesInput, tools: APITool[]) => Promise<models.OpenResponsesNonStreamingResponse>, initialInput: models.OpenResponsesInput, initialRequest: models.OpenResponsesRequest, tools: Tool[], apiTools: APITool[], options?: ToolExecutionOptions): Promise<ToolOrchestrationResult>;
|
|
31
31
|
/**
|
|
32
32
|
* Convert tool execution results to a map for easy lookup
|
|
33
33
|
*/
|
|
34
|
-
export declare function toolResultsToMap(results: ToolExecutionResult[]): Map<string, {
|
|
34
|
+
export declare function toolResultsToMap(results: ToolExecutionResult<Tool>[]): Map<string, {
|
|
35
35
|
result: unknown;
|
|
36
36
|
preliminaryResults?: unknown[];
|
|
37
37
|
}>;
|
|
38
38
|
/**
|
|
39
39
|
* Build a summary of tool executions for debugging/logging
|
|
40
40
|
*/
|
|
41
|
-
export declare function summarizeToolExecutions(results: ToolExecutionResult[]): string;
|
|
41
|
+
export declare function summarizeToolExecutions(results: ToolExecutionResult<Tool>[]): string;
|
|
42
42
|
/**
|
|
43
43
|
* Check if any tool executions had errors
|
|
44
44
|
*/
|
|
45
|
-
export declare function hasToolExecutionErrors(results: ToolExecutionResult[]): boolean;
|
|
45
|
+
export declare function hasToolExecutionErrors(results: ToolExecutionResult<Tool>[]): boolean;
|
|
46
46
|
/**
|
|
47
47
|
* Get all tool execution errors
|
|
48
48
|
*/
|
|
49
|
-
export declare function getToolExecutionErrors(results: ToolExecutionResult[]): Error[];
|
|
49
|
+
export declare function getToolExecutionErrors(results: ToolExecutionResult<Tool>[]): Error[];
|
|
50
50
|
//# sourceMappingURL=tool-orchestrator.d.ts.map
|
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
import { extractToolCallsFromResponse, responseHasToolCalls } from './stream-transformers.js';
|
|
2
|
+
import { isFunctionCallOutputItem } from './stream-type-guards.js';
|
|
2
3
|
import { executeTool, findToolByName } from './tool-executor.js';
|
|
3
4
|
import { hasExecuteFunction } from './tool-types.js';
|
|
5
|
+
import { buildTurnContext } from './turn-context.js';
|
|
6
|
+
import { executeNextTurnParamsFunctions, applyNextTurnParamsToRequest } from './next-turn-params.js';
|
|
4
7
|
/**
|
|
5
8
|
* Execute tool calls and manage multi-turn conversations
|
|
6
9
|
* This orchestrates the loop of: request -> tool calls -> execute -> send results -> repeat
|
|
7
10
|
*
|
|
8
11
|
* @param sendRequest - Function to send a request and get a response
|
|
9
12
|
* @param initialInput - Starting input for the conversation
|
|
13
|
+
* @param initialRequest - Full initial request with all parameters
|
|
10
14
|
* @param tools - Enhanced tools with Zod schemas and execute functions
|
|
11
15
|
* @param apiTools - Converted tools in API format (JSON Schema)
|
|
12
16
|
* @param options - Execution options
|
|
13
17
|
* @returns Result containing final response and all execution data
|
|
14
18
|
*/
|
|
15
|
-
export async function executeToolLoop(sendRequest, initialInput, tools, apiTools, options = {}) {
|
|
16
|
-
const maxRounds = options.maxRounds ?? 5;
|
|
19
|
+
export async function executeToolLoop(sendRequest, initialInput, initialRequest, tools, apiTools, options = {}) {
|
|
17
20
|
const onPreliminaryResult = options.onPreliminaryResult;
|
|
18
21
|
const allResponses = [];
|
|
19
22
|
const toolExecutionResults = [];
|
|
20
23
|
let conversationInput = initialInput;
|
|
24
|
+
let currentRequest = { ...initialRequest };
|
|
21
25
|
let currentRound = 0;
|
|
22
26
|
let currentResponse;
|
|
23
27
|
// Initial request
|
|
24
28
|
currentResponse = await sendRequest(conversationInput, apiTools);
|
|
25
29
|
allResponses.push(currentResponse);
|
|
26
|
-
// Loop until no more tool calls
|
|
27
|
-
while (responseHasToolCalls(currentResponse)
|
|
30
|
+
// Loop until no more tool calls (model decides when to stop)
|
|
31
|
+
while (responseHasToolCalls(currentResponse)) {
|
|
28
32
|
currentRound++;
|
|
29
33
|
// Extract tool calls from response
|
|
30
34
|
const toolCalls = extractToolCallsFromResponse(currentResponse);
|
|
@@ -56,11 +60,26 @@ export async function executeToolLoop(sendRequest, initialInput, tools, apiTools
|
|
|
56
60
|
// Tool has no execute function - return null to filter out
|
|
57
61
|
return null;
|
|
58
62
|
}
|
|
59
|
-
//
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
// Find the raw tool call from the response output
|
|
64
|
+
const rawToolCall = currentResponse.output.find((item) => isFunctionCallOutputItem(item) && item.callId === toolCall.id);
|
|
65
|
+
if (!rawToolCall) {
|
|
66
|
+
throw new Error(`Could not find raw tool call for ${toolCall.id}`);
|
|
67
|
+
}
|
|
68
|
+
// Convert to OpenResponsesFunctionToolCall format
|
|
69
|
+
const openResponsesToolCall = {
|
|
70
|
+
type: 'function_call',
|
|
71
|
+
callId: rawToolCall.callId,
|
|
72
|
+
name: rawToolCall.name,
|
|
73
|
+
arguments: rawToolCall.arguments,
|
|
74
|
+
id: rawToolCall.callId,
|
|
75
|
+
status: rawToolCall.status,
|
|
63
76
|
};
|
|
77
|
+
// Build turn context with full information
|
|
78
|
+
const turnContext = buildTurnContext({
|
|
79
|
+
numberOfTurns: currentRound,
|
|
80
|
+
toolCall: openResponsesToolCall,
|
|
81
|
+
turnRequest: currentRequest,
|
|
82
|
+
});
|
|
64
83
|
// Execute the tool
|
|
65
84
|
return executeTool(tool, toolCall, turnContext, onPreliminaryResult);
|
|
66
85
|
});
|
|
@@ -90,10 +109,17 @@ export async function executeToolLoop(sendRequest, initialInput, tools, apiTools
|
|
|
90
109
|
}
|
|
91
110
|
});
|
|
92
111
|
toolExecutionResults.push(...roundResults);
|
|
112
|
+
// Execute nextTurnParams functions for tools that were called
|
|
113
|
+
const computedParams = await executeNextTurnParamsFunctions(toolCalls, tools, currentRequest);
|
|
114
|
+
// Apply computed parameters to request
|
|
115
|
+
if (Object.keys(computedParams).length > 0) {
|
|
116
|
+
currentRequest = applyNextTurnParamsToRequest(currentRequest, computedParams);
|
|
117
|
+
conversationInput = currentRequest.input ?? conversationInput;
|
|
118
|
+
}
|
|
93
119
|
// Build array input with all output from previous response plus tool results
|
|
94
120
|
// The API expects continuation via previousResponseId, not by including outputs
|
|
95
121
|
// For now, we'll keep the conversation going via previousResponseId
|
|
96
|
-
conversationInput
|
|
122
|
+
// conversationInput is updated above if nextTurnParams modified it
|
|
97
123
|
// Note: The OpenRouter Responses API uses previousResponseId for continuation
|
|
98
124
|
// Tool results are automatically associated with the previous response's tool calls
|
|
99
125
|
// Send updated conversation to API - this should use previousResponseId
|
|
@@ -147,6 +173,8 @@ export function hasToolExecutionErrors(results) {
|
|
|
147
173
|
* Get all tool execution errors
|
|
148
174
|
*/
|
|
149
175
|
export function getToolExecutionErrors(results) {
|
|
150
|
-
return results
|
|
176
|
+
return results
|
|
177
|
+
.filter((result) => result.error !== undefined)
|
|
178
|
+
.map((result) => result.error);
|
|
151
179
|
}
|
|
152
180
|
//# sourceMappingURL=tool-orchestrator.js.map
|
package/esm/lib/tool-types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ZodObject, ZodRawShape, ZodType, z } from 'zod/v4';
|
|
2
2
|
import type * as models from '../models/index.js';
|
|
3
3
|
import type { OpenResponsesStreamEvent } from '../models/index.js';
|
|
4
|
-
import type {
|
|
4
|
+
import type { ModelResult } from './model-result.js';
|
|
5
5
|
/**
|
|
6
6
|
* Tool type enum for enhanced tools
|
|
7
7
|
*/
|
|
@@ -9,19 +9,47 @@ export declare enum ToolType {
|
|
|
9
9
|
Function = "function"
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
|
-
* Turn context passed to tool execute functions
|
|
12
|
+
* Turn context passed to tool execute functions and async parameter resolution
|
|
13
13
|
* Contains information about the current conversation state
|
|
14
14
|
*/
|
|
15
15
|
export interface TurnContext {
|
|
16
|
-
/**
|
|
16
|
+
/** The specific tool call being executed (only available during tool execution) */
|
|
17
|
+
toolCall?: models.OpenResponsesFunctionToolCall;
|
|
18
|
+
/** Number of tool execution turns so far (1-indexed: first turn = 1, 0 = initial request) */
|
|
17
19
|
numberOfTurns: number;
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
-
/** Model name if request.model is set */
|
|
21
|
-
model?: string;
|
|
22
|
-
/** Model names if request.models is set */
|
|
23
|
-
models?: string[];
|
|
20
|
+
/** The full request being sent to the API (only available during tool execution) */
|
|
21
|
+
turnRequest?: models.OpenResponsesRequest;
|
|
24
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Context passed to nextTurnParams functions
|
|
25
|
+
* Contains current request state for parameter computation
|
|
26
|
+
* Allows modification of key request parameters between turns
|
|
27
|
+
*/
|
|
28
|
+
export type NextTurnParamsContext = {
|
|
29
|
+
/** Current input (messages) */
|
|
30
|
+
input: models.OpenResponsesInput;
|
|
31
|
+
/** Current model selection */
|
|
32
|
+
model: string;
|
|
33
|
+
/** Current models array */
|
|
34
|
+
models: string[];
|
|
35
|
+
/** Current temperature */
|
|
36
|
+
temperature: number | null;
|
|
37
|
+
/** Current maxOutputTokens */
|
|
38
|
+
maxOutputTokens: number | null;
|
|
39
|
+
/** Current topP */
|
|
40
|
+
topP: number | null;
|
|
41
|
+
/** Current topK */
|
|
42
|
+
topK?: number | undefined;
|
|
43
|
+
/** Current instructions */
|
|
44
|
+
instructions: string | null;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Functions to compute next turn parameters
|
|
48
|
+
* Each function receives the tool's input params and current request context
|
|
49
|
+
*/
|
|
50
|
+
export type NextTurnParamsFunctions<TInput> = {
|
|
51
|
+
[K in keyof NextTurnParamsContext]?: (params: TInput, context: NextTurnParamsContext) => NextTurnParamsContext[K] | Promise<NextTurnParamsContext[K]>;
|
|
52
|
+
};
|
|
25
53
|
/**
|
|
26
54
|
* Base tool function interface with inputSchema
|
|
27
55
|
*/
|
|
@@ -29,6 +57,7 @@ export interface BaseToolFunction<TInput extends ZodObject<ZodRawShape>> {
|
|
|
29
57
|
name: string;
|
|
30
58
|
description?: string;
|
|
31
59
|
inputSchema: TInput;
|
|
60
|
+
nextTurnParams?: NextTurnParamsFunctions<z.infer<TInput>>;
|
|
32
61
|
}
|
|
33
62
|
/**
|
|
34
63
|
* Regular tool with synchronous or asynchronous execute function and optional outputSchema
|
|
@@ -42,6 +71,10 @@ export interface ToolFunctionWithExecute<TInput extends ZodObject<ZodRawShape>,
|
|
|
42
71
|
* Emits preliminary events (validated by eventSchema) during execution
|
|
43
72
|
* and a final output (validated by outputSchema) as the last emission
|
|
44
73
|
*
|
|
74
|
+
* The generator can yield both events and the final output.
|
|
75
|
+
* All yields are validated against eventSchema (which should be a union of event and output types),
|
|
76
|
+
* and the last yield is additionally validated against outputSchema.
|
|
77
|
+
*
|
|
45
78
|
* @example
|
|
46
79
|
* ```typescript
|
|
47
80
|
* {
|
|
@@ -58,7 +91,7 @@ export interface ToolFunctionWithExecute<TInput extends ZodObject<ZodRawShape>,
|
|
|
58
91
|
export interface ToolFunctionWithGenerator<TInput extends ZodObject<ZodRawShape>, TEvent extends ZodType = ZodType<unknown>, TOutput extends ZodType = ZodType<unknown>> extends BaseToolFunction<TInput> {
|
|
59
92
|
eventSchema: TEvent;
|
|
60
93
|
outputSchema: TOutput;
|
|
61
|
-
execute: (params: z.infer<TInput>, context?: TurnContext) => AsyncGenerator<z.infer<TEvent>>;
|
|
94
|
+
execute: (params: z.infer<TInput>, context?: TurnContext) => AsyncGenerator<z.infer<TEvent> | z.infer<TOutput>>;
|
|
62
95
|
}
|
|
63
96
|
/**
|
|
64
97
|
* Manual tool without execute function - requires manual handling by developer
|
|
@@ -91,6 +124,62 @@ export type ManualTool<TInput extends ZodObject<ZodRawShape> = ZodObject<ZodRawS
|
|
|
91
124
|
* Union type of all enhanced tool types
|
|
92
125
|
*/
|
|
93
126
|
export type Tool = ToolWithExecute<ZodObject<ZodRawShape>, ZodType<unknown>> | ToolWithGenerator<ZodObject<ZodRawShape>, ZodType<unknown>, ZodType<unknown>> | ManualTool<ZodObject<ZodRawShape>, ZodType<unknown>>;
|
|
127
|
+
/**
|
|
128
|
+
* Extracts the input type from a tool definition
|
|
129
|
+
*/
|
|
130
|
+
export type InferToolInput<T> = T extends {
|
|
131
|
+
function: {
|
|
132
|
+
inputSchema: infer S;
|
|
133
|
+
};
|
|
134
|
+
} ? S extends ZodType ? z.infer<S> : unknown : unknown;
|
|
135
|
+
/**
|
|
136
|
+
* Extracts the output type from a tool definition
|
|
137
|
+
*/
|
|
138
|
+
export type InferToolOutput<T> = T extends {
|
|
139
|
+
function: {
|
|
140
|
+
outputSchema: infer S;
|
|
141
|
+
};
|
|
142
|
+
} ? S extends ZodType ? z.infer<S> : unknown : unknown;
|
|
143
|
+
/**
|
|
144
|
+
* A tool call with typed arguments based on the tool's inputSchema
|
|
145
|
+
*/
|
|
146
|
+
export type TypedToolCall<T extends Tool> = {
|
|
147
|
+
id: string;
|
|
148
|
+
name: T extends {
|
|
149
|
+
function: {
|
|
150
|
+
name: infer N;
|
|
151
|
+
};
|
|
152
|
+
} ? N : string;
|
|
153
|
+
arguments: InferToolInput<T>;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Union of typed tool calls for a tuple of tools
|
|
157
|
+
*/
|
|
158
|
+
export type TypedToolCallUnion<T extends readonly Tool[]> = {
|
|
159
|
+
[K in keyof T]: T[K] extends Tool ? TypedToolCall<T[K]> : never;
|
|
160
|
+
}[number];
|
|
161
|
+
/**
|
|
162
|
+
* Union of typed tool execution results for a tuple of tools
|
|
163
|
+
*/
|
|
164
|
+
export type ToolExecutionResultUnion<T extends readonly Tool[]> = {
|
|
165
|
+
[K in keyof T]: T[K] extends Tool ? ToolExecutionResult<T[K]> : never;
|
|
166
|
+
}[number];
|
|
167
|
+
/**
|
|
168
|
+
* Extracts the event type from a generator tool definition
|
|
169
|
+
* Returns `never` for non-generator tools
|
|
170
|
+
*/
|
|
171
|
+
export type InferToolEvent<T> = T extends {
|
|
172
|
+
function: {
|
|
173
|
+
eventSchema: infer S;
|
|
174
|
+
};
|
|
175
|
+
} ? S extends ZodType ? z.infer<S> : never : never;
|
|
176
|
+
/**
|
|
177
|
+
* Union of event types for all generator tools in a tuple
|
|
178
|
+
* Filters out non-generator tools (which return `never`)
|
|
179
|
+
*/
|
|
180
|
+
export type InferToolEventsUnion<T extends readonly Tool[]> = {
|
|
181
|
+
[K in keyof T]: T[K] extends Tool ? InferToolEvent<T[K]> : never;
|
|
182
|
+
}[number];
|
|
94
183
|
/**
|
|
95
184
|
* Type guard to check if a tool has an execute function
|
|
96
185
|
*/
|
|
@@ -103,34 +192,75 @@ export declare function isGeneratorTool(tool: Tool): tool is ToolWithGenerator;
|
|
|
103
192
|
* Type guard to check if a tool is a regular execution tool (not generator)
|
|
104
193
|
*/
|
|
105
194
|
export declare function isRegularExecuteTool(tool: Tool): tool is ToolWithExecute;
|
|
195
|
+
/**
|
|
196
|
+
* Type guard to check if a tool is a manual tool (no execute function)
|
|
197
|
+
*/
|
|
198
|
+
export declare function isManualTool(tool: Tool): tool is ManualTool;
|
|
106
199
|
/**
|
|
107
200
|
* Parsed tool call from API response
|
|
201
|
+
* @template T - The tool type to infer argument types from
|
|
108
202
|
*/
|
|
109
|
-
export interface ParsedToolCall {
|
|
203
|
+
export interface ParsedToolCall<T extends Tool> {
|
|
110
204
|
id: string;
|
|
111
|
-
name:
|
|
112
|
-
|
|
205
|
+
name: T extends {
|
|
206
|
+
function: {
|
|
207
|
+
name: infer N;
|
|
208
|
+
};
|
|
209
|
+
} ? N : string;
|
|
210
|
+
arguments: InferToolInput<T>;
|
|
113
211
|
}
|
|
114
212
|
/**
|
|
115
213
|
* Result of tool execution
|
|
214
|
+
* @template T - The tool type to infer result types from
|
|
116
215
|
*/
|
|
117
|
-
export interface ToolExecutionResult {
|
|
216
|
+
export interface ToolExecutionResult<T extends Tool> {
|
|
118
217
|
toolCallId: string;
|
|
119
218
|
toolName: string;
|
|
120
|
-
result: unknown;
|
|
121
|
-
preliminaryResults?:
|
|
219
|
+
result: T extends ToolWithExecute<any, infer O> | ToolWithGenerator<any, any, infer O> ? z.infer<O> : unknown;
|
|
220
|
+
preliminaryResults?: T extends ToolWithGenerator<any, infer E, any> ? z.infer<E>[] : undefined;
|
|
122
221
|
error?: Error;
|
|
123
222
|
}
|
|
124
223
|
/**
|
|
125
|
-
*
|
|
224
|
+
* Warning from step execution
|
|
225
|
+
*/
|
|
226
|
+
export interface Warning {
|
|
227
|
+
type: string;
|
|
228
|
+
message: string;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Result of a single step in the tool execution loop
|
|
232
|
+
* Compatible with Vercel AI SDK pattern
|
|
233
|
+
*/
|
|
234
|
+
export interface StepResult<TTools extends readonly Tool[] = readonly Tool[]> {
|
|
235
|
+
readonly stepType: 'initial' | 'continue';
|
|
236
|
+
readonly text: string;
|
|
237
|
+
readonly toolCalls: TypedToolCallUnion<TTools>[];
|
|
238
|
+
readonly toolResults: ToolExecutionResultUnion<TTools>[];
|
|
239
|
+
readonly response: models.OpenResponsesNonStreamingResponse;
|
|
240
|
+
readonly usage?: models.OpenResponsesUsage | undefined;
|
|
241
|
+
readonly finishReason?: string | undefined;
|
|
242
|
+
readonly warnings?: Warning[] | undefined;
|
|
243
|
+
readonly experimental_providerMetadata?: Record<string, unknown> | undefined;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* A condition function that determines whether to stop tool execution
|
|
247
|
+
* Returns true to STOP execution, false to CONTINUE
|
|
248
|
+
* (Matches Vercel AI SDK semantics)
|
|
249
|
+
*/
|
|
250
|
+
export type StopCondition<TTools extends readonly Tool[] = readonly Tool[]> = (options: {
|
|
251
|
+
readonly steps: ReadonlyArray<StepResult<TTools>>;
|
|
252
|
+
}) => boolean | Promise<boolean>;
|
|
253
|
+
/**
|
|
254
|
+
* Stop condition configuration
|
|
255
|
+
* Can be a single condition or array of conditions
|
|
126
256
|
*/
|
|
127
|
-
export type
|
|
257
|
+
export type StopWhen<TTools extends readonly Tool[] = readonly Tool[]> = StopCondition<TTools> | ReadonlyArray<StopCondition<TTools>>;
|
|
128
258
|
/**
|
|
129
259
|
* Result of executeTools operation
|
|
130
260
|
*/
|
|
131
|
-
export interface ExecuteToolsResult {
|
|
132
|
-
finalResponse:
|
|
133
|
-
allResponses:
|
|
261
|
+
export interface ExecuteToolsResult<TTools extends readonly Tool[]> {
|
|
262
|
+
finalResponse: ModelResult<TTools>;
|
|
263
|
+
allResponses: ModelResult<TTools>[];
|
|
134
264
|
toolResults: Map<string, {
|
|
135
265
|
result: unknown;
|
|
136
266
|
preliminaryResults?: unknown[];
|
|
@@ -151,39 +281,43 @@ export interface APITool {
|
|
|
151
281
|
}
|
|
152
282
|
/**
|
|
153
283
|
* Tool preliminary result event emitted during generator tool execution
|
|
284
|
+
* @template TEvent - The event type from the tool's eventSchema
|
|
154
285
|
*/
|
|
155
|
-
export type ToolPreliminaryResultEvent = {
|
|
286
|
+
export type ToolPreliminaryResultEvent<TEvent = unknown> = {
|
|
156
287
|
type: 'tool.preliminary_result';
|
|
157
288
|
toolCallId: string;
|
|
158
|
-
result:
|
|
289
|
+
result: TEvent;
|
|
159
290
|
timestamp: number;
|
|
160
291
|
};
|
|
161
292
|
/**
|
|
162
293
|
* Enhanced stream event types for getFullResponsesStream
|
|
163
294
|
* Extends OpenResponsesStreamEvent with tool preliminary results
|
|
295
|
+
* @template TEvent - The event type from generator tools
|
|
164
296
|
*/
|
|
165
|
-
export type
|
|
297
|
+
export type ResponseStreamEvent<TEvent = unknown> = OpenResponsesStreamEvent | ToolPreliminaryResultEvent<TEvent>;
|
|
166
298
|
/**
|
|
167
299
|
* Type guard to check if an event is a tool preliminary result event
|
|
168
300
|
*/
|
|
169
|
-
export declare function isToolPreliminaryResultEvent(event:
|
|
301
|
+
export declare function isToolPreliminaryResultEvent<TEvent = unknown>(event: ResponseStreamEvent<TEvent>): event is ToolPreliminaryResultEvent<TEvent>;
|
|
170
302
|
/**
|
|
171
303
|
* Tool stream event types for getToolStream
|
|
172
304
|
* Includes both argument deltas and preliminary results
|
|
305
|
+
* @template TEvent - The event type from generator tools
|
|
173
306
|
*/
|
|
174
|
-
export type ToolStreamEvent = {
|
|
307
|
+
export type ToolStreamEvent<TEvent = unknown> = {
|
|
175
308
|
type: 'delta';
|
|
176
309
|
content: string;
|
|
177
310
|
} | {
|
|
178
311
|
type: 'preliminary_result';
|
|
179
312
|
toolCallId: string;
|
|
180
|
-
result:
|
|
313
|
+
result: TEvent;
|
|
181
314
|
};
|
|
182
315
|
/**
|
|
183
316
|
* Chat stream event types for getFullChatStream
|
|
184
317
|
* Includes content deltas, completion events, and tool preliminary results
|
|
318
|
+
* @template TEvent - The event type from generator tools
|
|
185
319
|
*/
|
|
186
|
-
export type ChatStreamEvent = {
|
|
320
|
+
export type ChatStreamEvent<TEvent = unknown> = {
|
|
187
321
|
type: 'content.delta';
|
|
188
322
|
delta: string;
|
|
189
323
|
} | {
|
|
@@ -192,7 +326,7 @@ export type ChatStreamEvent = {
|
|
|
192
326
|
} | {
|
|
193
327
|
type: 'tool.preliminary_result';
|
|
194
328
|
toolCallId: string;
|
|
195
|
-
result:
|
|
329
|
+
result: TEvent;
|
|
196
330
|
} | {
|
|
197
331
|
type: string;
|
|
198
332
|
event: OpenResponsesStreamEvent;
|