@openrouter/sdk 0.9.11 → 0.10.2
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/FUNCTIONS.md +2 -1
- package/_speakeasy/.github/action-inputs-config.json +4 -0
- package/esm/funcs/analyticsGetUserActivity.js +2 -1
- package/esm/funcs/apiKeysCreate.js +2 -1
- package/esm/funcs/apiKeysDelete.js +2 -1
- package/esm/funcs/apiKeysGet.js +2 -1
- package/esm/funcs/apiKeysGetCurrentKeyMetadata.js +2 -1
- package/esm/funcs/apiKeysList.js +2 -1
- package/esm/funcs/apiKeysUpdate.js +2 -1
- package/esm/funcs/betaResponsesSend.js +2 -1
- package/esm/funcs/call-model.d.ts +19 -70
- package/esm/funcs/call-model.js +29 -71
- package/esm/funcs/chatSend.js +2 -1
- package/esm/funcs/creditsCreateCoinbaseCharge.js +2 -1
- package/esm/funcs/creditsGetCredits.js +2 -1
- package/esm/funcs/embeddingsGenerate.js +2 -1
- package/esm/funcs/embeddingsListModels.js +2 -1
- package/esm/funcs/endpointsList.js +2 -1
- package/esm/funcs/endpointsListZdrEndpoints.js +2 -1
- package/esm/funcs/generationsGetGeneration.js +2 -1
- package/esm/funcs/guardrailsBulkAssignKeys.js +2 -1
- package/esm/funcs/guardrailsBulkAssignMembers.js +2 -1
- package/esm/funcs/guardrailsBulkUnassignKeys.js +2 -1
- package/esm/funcs/guardrailsBulkUnassignMembers.js +2 -1
- package/esm/funcs/guardrailsCreate.js +2 -1
- package/esm/funcs/guardrailsDelete.js +2 -1
- package/esm/funcs/guardrailsGet.js +2 -1
- package/esm/funcs/guardrailsList.js +2 -1
- package/esm/funcs/guardrailsListGuardrailKeyAssignments.js +2 -1
- package/esm/funcs/guardrailsListGuardrailMemberAssignments.js +2 -1
- package/esm/funcs/guardrailsListKeyAssignments.js +2 -1
- package/esm/funcs/guardrailsListMemberAssignments.js +2 -1
- package/esm/funcs/guardrailsUpdate.js +2 -1
- package/esm/funcs/modelsCount.d.ts +1 -1
- package/esm/funcs/modelsCount.js +9 -4
- package/esm/funcs/modelsList.js +3 -1
- package/esm/funcs/modelsListForUser.js +2 -1
- package/esm/funcs/oAuthCreateAuthCode.d.ts +1 -1
- package/esm/funcs/oAuthCreateAuthCode.js +4 -3
- package/esm/funcs/oAuthExchangeAuthCodeForAPIKey.js +2 -1
- package/esm/funcs/providersList.js +2 -1
- package/esm/index.d.ts +22 -23
- package/esm/index.js +20 -17
- package/esm/lib/anthropic-compat.d.ts +2 -1
- package/esm/lib/anthropic-compat.js +1 -2
- package/esm/lib/async-params.d.ts +19 -5
- package/esm/lib/async-params.js +5 -1
- package/esm/lib/chat-compat.d.ts +1 -1
- package/esm/lib/chat-compat.js +1 -2
- package/esm/lib/claude-type-guards.d.ts +2 -2
- package/esm/lib/config.d.ts +8 -10
- package/esm/lib/config.js +2 -2
- package/esm/lib/conversation-state.d.ts +1 -1
- package/esm/lib/conversation-state.js +35 -32
- package/esm/lib/env.d.ts +6 -2
- package/esm/lib/env.js +7 -3
- package/esm/lib/model-result.d.ts +73 -17
- package/esm/lib/model-result.js +329 -125
- package/esm/lib/sdks.d.ts +1 -1
- package/esm/lib/sdks.js +8 -28
- package/esm/lib/stream-transformers.d.ts +5 -4
- package/esm/lib/stream-transformers.js +24 -29
- package/esm/lib/stream-type-guards.js +8 -32
- package/esm/lib/tool-context.d.ts +68 -0
- package/esm/lib/tool-context.js +175 -0
- package/esm/lib/tool-executor.d.ts +9 -5
- package/esm/lib/tool-executor.js +26 -16
- package/esm/lib/tool-orchestrator.d.ts +2 -2
- package/esm/lib/tool-types.d.ts +101 -14
- package/esm/lib/tool-types.js +17 -0
- package/esm/lib/tool.d.ts +49 -60
- package/esm/lib/tool.js +15 -19
- package/esm/lib/turn-context.d.ts +1 -1
- package/esm/models/assistantmessage.d.ts +6 -0
- package/esm/models/assistantmessage.js +3 -0
- package/esm/models/chatcompletionaudiooutput.d.ts +38 -0
- package/esm/models/chatcompletionaudiooutput.js +36 -0
- package/esm/models/chatgenerationparams.d.ts +75 -6
- package/esm/models/chatgenerationparams.js +42 -0
- package/esm/models/chatmessagecontentitem.d.ts +3 -2
- package/esm/models/chatmessagecontentitem.js +3 -0
- package/esm/models/chatmessagecontentitemcachecontrol.d.ts +5 -5
- package/esm/models/chatmessagecontentitemcachecontrol.js +5 -7
- package/esm/models/chatmessagecontentitemfile.d.ts +57 -0
- package/esm/models/chatmessagecontentitemfile.js +59 -0
- package/esm/models/chatmessagetokenlogprobs.d.ts +1 -1
- package/esm/models/chatmessagetokenlogprobs.js +2 -1
- package/esm/models/chatresponse.d.ts +1 -1
- package/esm/models/chatresponse.js +1 -1
- package/esm/models/chatstreamingmessagechunk.d.ts +2 -0
- package/esm/models/chatstreamingmessagechunk.js +2 -0
- package/esm/models/chatstreamingresponsechunk.d.ts +1 -1
- package/esm/models/chatstreamingresponsechunk.js +1 -1
- package/esm/models/compoundfilter.d.ts +36 -0
- package/esm/models/compoundfilter.js +32 -0
- package/esm/models/conflictresponseerrordata.d.ts +17 -0
- package/esm/models/conflictresponseerrordata.js +16 -0
- package/esm/models/contextcompressionengine.d.ts +15 -0
- package/esm/models/contextcompressionengine.js +14 -0
- package/esm/models/datetimeservertool.d.ts +30 -0
- package/esm/models/datetimeservertool.js +22 -0
- package/esm/models/defaultparameters.d.ts +3 -0
- package/esm/models/defaultparameters.js +6 -0
- package/esm/models/errors/conflictresponseerror.d.ts +33 -0
- package/esm/models/errors/conflictresponseerror.js +42 -0
- package/esm/models/errors/index.d.ts +1 -0
- package/esm/models/errors/index.js +1 -0
- package/esm/models/index.d.ts +23 -4
- package/esm/models/index.js +23 -4
- package/esm/models/openairesponsesinputunion.d.ts +32 -1
- package/esm/models/openairesponsesinputunion.js +53 -1
- package/esm/models/openresponsesapplypatchtool.d.ts +20 -0
- package/esm/models/openresponsesapplypatchtool.js +21 -0
- package/esm/models/openresponsescodeinterpretertool.d.ts +68 -0
- package/esm/models/openresponsescodeinterpretertool.js +85 -0
- package/esm/models/openresponsescomputertool.d.ts +39 -0
- package/esm/models/openresponsescomputertool.js +50 -0
- package/esm/models/openresponsescustomtool.d.ts +75 -0
- package/esm/models/openresponsescustomtool.js +95 -0
- package/esm/models/openresponseseasyinputmessage.d.ts +30 -4
- package/esm/models/openresponseseasyinputmessage.js +28 -2
- package/esm/models/openresponsesfilesearchtool.d.ts +111 -0
- package/esm/models/openresponsesfilesearchtool.js +180 -0
- package/esm/models/openresponsesfunctioncalloutput.d.ts +46 -7
- package/esm/models/openresponsesfunctioncalloutput.js +56 -4
- package/esm/models/openresponsesfunctionshelltool.d.ts +20 -0
- package/esm/models/openresponsesfunctionshelltool.js +21 -0
- package/esm/models/openresponsesimagegenerationtool.d.ts +125 -0
- package/esm/models/openresponsesimagegenerationtool.js +153 -0
- package/esm/models/openresponsesinputmessageitem.d.ts +2 -2
- package/esm/models/openresponsesinputmessageitem.js +2 -2
- package/esm/models/openresponsesinputunion.d.ts +207 -0
- package/esm/models/openresponsesinputunion.js +225 -0
- package/esm/models/openresponseslocalshelltool.d.ts +20 -0
- package/esm/models/openresponseslocalshelltool.js +21 -0
- package/esm/models/openresponsesmcptool.d.ts +128 -0
- package/esm/models/openresponsesmcptool.js +173 -0
- package/esm/models/openresponsesnonstreamingresponse.d.ts +11 -2
- package/esm/models/openresponsesnonstreamingresponse.js +27 -0
- package/esm/models/openresponsesrequest.d.ts +53 -11
- package/esm/models/openresponsesrequest.js +51 -2
- package/esm/models/openresponsesstreamevent.d.ts +52 -4
- package/esm/models/openresponsesstreamevent.js +54 -3
- package/esm/models/openresponseswebsearch20250826tool.d.ts +29 -0
- package/esm/models/openresponseswebsearch20250826tool.js +23 -0
- package/esm/models/openresponseswebsearchcallcompleted.d.ts +16 -0
- package/esm/models/openresponseswebsearchcallcompleted.js +24 -0
- package/esm/models/openresponseswebsearchcallinprogress.d.ts +16 -0
- package/esm/models/openresponseswebsearchcallinprogress.js +24 -0
- package/esm/models/openresponseswebsearchcallsearching.d.ts +16 -0
- package/esm/models/openresponseswebsearchcallsearching.js +24 -0
- package/esm/models/openresponseswebsearchpreview20250311tool.d.ts +44 -0
- package/esm/models/openresponseswebsearchpreview20250311tool.js +51 -0
- package/esm/models/openresponseswebsearchpreviewtool.d.ts +44 -0
- package/esm/models/openresponseswebsearchpreviewtool.js +47 -0
- package/esm/models/openresponseswebsearchtool.d.ts +29 -0
- package/esm/models/openresponseswebsearchtool.js +23 -0
- package/esm/models/operations/bulkassignkeystoguardrail.d.ts +16 -3
- package/esm/models/operations/bulkassignkeystoguardrail.js +2 -2
- package/esm/models/operations/bulkassignmemberstoguardrail.d.ts +16 -3
- package/esm/models/operations/bulkassignmemberstoguardrail.js +2 -2
- package/esm/models/operations/bulkunassignkeysfromguardrail.d.ts +16 -3
- package/esm/models/operations/bulkunassignkeysfromguardrail.js +2 -2
- package/esm/models/operations/bulkunassignmembersfromguardrail.d.ts +16 -3
- package/esm/models/operations/bulkunassignmembersfromguardrail.js +2 -2
- package/esm/models/operations/createauthkeyscode.d.ts +16 -3
- package/esm/models/operations/createauthkeyscode.js +2 -2
- package/esm/models/operations/createcoinbasecharge.d.ts +16 -3
- package/esm/models/operations/createcoinbasecharge.js +2 -2
- package/esm/models/operations/createembeddings.d.ts +16 -3
- package/esm/models/operations/createembeddings.js +2 -2
- package/esm/models/operations/createguardrail.d.ts +25 -3
- package/esm/models/operations/createguardrail.js +6 -2
- package/esm/models/operations/createkeys.d.ts +16 -3
- package/esm/models/operations/createkeys.js +2 -2
- package/esm/models/operations/createresponses.d.ts +16 -3
- package/esm/models/operations/createresponses.js +2 -2
- package/esm/models/operations/deleteguardrail.d.ts +16 -3
- package/esm/models/operations/deleteguardrail.js +2 -2
- package/esm/models/operations/deletekeys.d.ts +16 -3
- package/esm/models/operations/deletekeys.js +2 -2
- package/esm/models/operations/exchangeauthcodeforapikey.d.ts +16 -3
- package/esm/models/operations/exchangeauthcodeforapikey.js +2 -2
- package/esm/models/operations/getcredits.d.ts +16 -3
- package/esm/models/operations/getcredits.js +2 -2
- package/esm/models/operations/getcurrentkey.d.ts +16 -3
- package/esm/models/operations/getcurrentkey.js +2 -2
- package/esm/models/operations/getgeneration.d.ts +25 -3
- package/esm/models/operations/getgeneration.js +7 -2
- package/esm/models/operations/getguardrail.d.ts +20 -3
- package/esm/models/operations/getguardrail.js +4 -2
- package/esm/models/operations/getkey.d.ts +16 -3
- package/esm/models/operations/getkey.js +2 -2
- package/esm/models/operations/getmodels.d.ts +21 -3
- package/esm/models/operations/getmodels.js +4 -2
- package/esm/models/operations/getuseractivity.d.ts +16 -3
- package/esm/models/operations/getuseractivity.js +2 -2
- package/esm/models/operations/list.d.ts +16 -3
- package/esm/models/operations/list.js +2 -2
- package/esm/models/operations/listembeddingsmodels.d.ts +16 -3
- package/esm/models/operations/listembeddingsmodels.js +2 -2
- package/esm/models/operations/listendpoints.d.ts +16 -3
- package/esm/models/operations/listendpoints.js +2 -2
- package/esm/models/operations/listendpointszdr.d.ts +16 -3
- package/esm/models/operations/listendpointszdr.js +2 -2
- package/esm/models/operations/listguardrailkeyassignments.d.ts +16 -3
- package/esm/models/operations/listguardrailkeyassignments.js +2 -2
- package/esm/models/operations/listguardrailmemberassignments.d.ts +16 -3
- package/esm/models/operations/listguardrailmemberassignments.js +2 -2
- package/esm/models/operations/listguardrails.d.ts +20 -3
- package/esm/models/operations/listguardrails.js +4 -2
- package/esm/models/operations/listkeyassignments.d.ts +16 -3
- package/esm/models/operations/listkeyassignments.js +2 -2
- package/esm/models/operations/listmemberassignments.d.ts +16 -3
- package/esm/models/operations/listmemberassignments.js +2 -2
- package/esm/models/operations/listmodelscount.d.ts +21 -3
- package/esm/models/operations/listmodelscount.js +4 -2
- package/esm/models/operations/listmodelsuser.d.ts +16 -3
- package/esm/models/operations/listmodelsuser.js +2 -2
- package/esm/models/operations/listproviders.d.ts +16 -3
- package/esm/models/operations/listproviders.js +2 -2
- package/esm/models/operations/sendchatcompletionrequest.d.ts +16 -3
- package/esm/models/operations/sendchatcompletionrequest.js +2 -2
- package/esm/models/operations/updateguardrail.d.ts +25 -3
- package/esm/models/operations/updateguardrail.js +6 -2
- package/esm/models/operations/updatekeys.d.ts +16 -3
- package/esm/models/operations/updatekeys.js +2 -2
- package/esm/models/outputmessage.d.ts +23 -0
- package/esm/models/outputmessage.js +24 -0
- package/esm/models/outputmodality.d.ts +1 -0
- package/esm/models/outputmodality.js +1 -0
- package/esm/models/providername.d.ts +1 -0
- package/esm/models/providername.js +1 -0
- package/esm/models/providerpreferences.d.ts +4 -0
- package/esm/models/providerpreferences.js +4 -0
- package/esm/models/providersort.d.ts +1 -0
- package/esm/models/providersort.js +1 -0
- package/esm/models/providersortconfig.d.ts +1 -0
- package/esm/models/providersortconfig.js +1 -0
- package/esm/models/responseoutputtext.d.ts +8 -8
- package/esm/models/responseoutputtext.js +10 -9
- package/esm/models/responsesoutputitem.d.ts +2 -13
- package/esm/models/responsesoutputitem.js +8 -6
- package/esm/models/responsesoutputitemreasoning.d.ts +2 -31
- package/esm/models/responsesoutputitemreasoning.js +2 -44
- package/esm/models/responsesoutputmessage.d.ts +24 -32
- package/esm/models/responsesoutputmessage.js +26 -47
- package/esm/models/responsesservertooloutput.d.ts +42 -0
- package/esm/models/responsesservertooloutput.js +44 -0
- package/esm/models/responseswebsearchcalloutput.d.ts +90 -5
- package/esm/models/responseswebsearchcalloutput.js +113 -5
- package/esm/models/responseswebsearchservertool.d.ts +35 -0
- package/esm/models/responseswebsearchservertool.js +28 -0
- package/esm/models/tooldefinitionjson.d.ts +38 -20
- package/esm/models/tooldefinitionjson.js +22 -11
- package/esm/models/websearchengine.d.ts +2 -0
- package/esm/models/websearchengine.js +2 -0
- package/esm/models/websearchservertool.d.ts +116 -0
- package/esm/models/websearchservertool.js +77 -0
- package/esm/models/websearchshorthand.d.ts +215 -0
- package/esm/models/websearchshorthand.js +138 -0
- package/esm/sdk/sdk.d.ts +4 -1
- package/esm/sdk/sdk.js +0 -1
- package/esm/types/index.d.ts +0 -2
- package/esm/types/index.js +0 -1
- package/jsr.json +1 -1
- package/package.json +11 -6
- package/turbo.json +10 -0
- package/esm/models/openresponsesinput.d.ts +0 -28
- package/esm/models/openresponsesinput.js +0 -54
- package/esm/models/openresponseslogprobs.d.ts +0 -16
- package/esm/models/openresponseslogprobs.js +0 -22
- package/esm/models/openresponsestoplogprobs.d.ts +0 -14
- package/esm/models/openresponsestoplogprobs.js +0 -15
package/esm/lib/tool-executor.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as z4 from 'zod/v4';
|
|
2
2
|
import { hasExecuteFunction, isGeneratorTool, isRegularExecuteTool } from './tool-types.js';
|
|
3
|
+
import { buildToolExecuteContext } from './tool-context.js';
|
|
3
4
|
// Re-export ZodError for convenience
|
|
4
5
|
export const ZodError = z4.ZodError;
|
|
5
6
|
/**
|
|
@@ -96,30 +97,40 @@ function tryValidate(schema, value) {
|
|
|
96
97
|
return result.success;
|
|
97
98
|
}
|
|
98
99
|
/**
|
|
99
|
-
* Parse tool call arguments from JSON string
|
|
100
|
+
* Parse tool call arguments from JSON string.
|
|
101
|
+
* Treats empty/whitespace-only strings as an empty object — some providers
|
|
102
|
+
* return `arguments: ""` for tools that take no parameters.
|
|
100
103
|
*/
|
|
101
104
|
export function parseToolCallArguments(argumentsString) {
|
|
105
|
+
const trimmed = argumentsString.trim();
|
|
106
|
+
if (!trimmed) {
|
|
107
|
+
return {};
|
|
108
|
+
}
|
|
102
109
|
try {
|
|
103
|
-
return JSON.parse(
|
|
110
|
+
return JSON.parse(trimmed);
|
|
104
111
|
}
|
|
105
112
|
catch (error) {
|
|
106
113
|
throw new Error(`Failed to parse tool call arguments: ${error instanceof Error ? error.message : String(error)}`);
|
|
107
114
|
}
|
|
108
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Build a ToolExecuteContext for a tool from a TurnContext and optional context store
|
|
118
|
+
*/
|
|
119
|
+
function buildExecuteCtx(tool, turnContext, contextStore, sharedSchema) {
|
|
120
|
+
return buildToolExecuteContext(turnContext, contextStore, tool.function.name, tool.function.contextSchema, sharedSchema);
|
|
121
|
+
}
|
|
109
122
|
/**
|
|
110
123
|
* Execute a regular (non-generator) tool
|
|
111
124
|
*/
|
|
112
|
-
export async function executeRegularTool(tool, toolCall, context) {
|
|
125
|
+
export async function executeRegularTool(tool, toolCall, context, contextStore, sharedSchema) {
|
|
113
126
|
if (!isRegularExecuteTool(tool)) {
|
|
114
127
|
throw new Error(`Tool "${toolCall.name}" is not a regular execute tool or has no execute function`);
|
|
115
128
|
}
|
|
116
129
|
try {
|
|
117
|
-
// Validate input - the schema validation ensures type safety at runtime
|
|
118
|
-
// validateToolInput returns z.infer<typeof tool.function.inputSchema>
|
|
119
|
-
// which is exactly the type expected by execute
|
|
120
130
|
const validatedInput = validateToolInput(tool.function.inputSchema, toolCall.arguments);
|
|
131
|
+
const executeContext = buildExecuteCtx(tool, context, contextStore, sharedSchema);
|
|
121
132
|
// Execute tool with context
|
|
122
|
-
const result = await Promise.resolve(tool.function.execute(validatedInput,
|
|
133
|
+
const result = await Promise.resolve(tool.function.execute(validatedInput, executeContext));
|
|
123
134
|
// Validate output if schema is provided
|
|
124
135
|
if (tool.function.outputSchema) {
|
|
125
136
|
const validatedOutput = validateToolOutput(tool.function.outputSchema, result);
|
|
@@ -150,20 +161,19 @@ export async function executeRegularTool(tool, toolCall, context) {
|
|
|
150
161
|
* - Last yield is validated against outputSchema (final result sent to model)
|
|
151
162
|
* - Generator must emit at least one value
|
|
152
163
|
*/
|
|
153
|
-
export async function executeGeneratorTool(tool, toolCall, context, onPreliminaryResult) {
|
|
164
|
+
export async function executeGeneratorTool(tool, toolCall, context, onPreliminaryResult, contextStore, sharedSchema) {
|
|
154
165
|
if (!isGeneratorTool(tool)) {
|
|
155
166
|
throw new Error(`Tool "${toolCall.name}" is not a generator tool`);
|
|
156
167
|
}
|
|
157
168
|
try {
|
|
158
|
-
// Validate input - the schema validation ensures type safety at runtime
|
|
159
|
-
// The inputSchema's inferred type matches the execute function's parameter type by construction
|
|
160
169
|
const validatedInput = validateToolInput(tool.function.inputSchema, toolCall.arguments);
|
|
170
|
+
const executeContext = buildExecuteCtx(tool, context, contextStore, sharedSchema);
|
|
161
171
|
const preliminaryResults = [];
|
|
162
|
-
let finalResult
|
|
172
|
+
let finalResult;
|
|
163
173
|
let hasFinalResult = false;
|
|
164
|
-
let lastEmittedValue
|
|
174
|
+
let lastEmittedValue;
|
|
165
175
|
let hasEmittedValue = false;
|
|
166
|
-
const iterator = tool.function.execute(validatedInput,
|
|
176
|
+
const iterator = tool.function.execute(validatedInput, executeContext);
|
|
167
177
|
let iterResult = await iterator.next();
|
|
168
178
|
while (!iterResult.done) {
|
|
169
179
|
const event = iterResult.value;
|
|
@@ -214,14 +224,14 @@ export async function executeGeneratorTool(tool, toolCall, context, onPreliminar
|
|
|
214
224
|
* Execute a tool call
|
|
215
225
|
* Automatically detects if it's a regular or generator tool
|
|
216
226
|
*/
|
|
217
|
-
export async function executeTool(tool, toolCall, context, onPreliminaryResult) {
|
|
227
|
+
export async function executeTool(tool, toolCall, context, onPreliminaryResult, contextStore, sharedSchema) {
|
|
218
228
|
if (!hasExecuteFunction(tool)) {
|
|
219
229
|
throw new Error(`Tool "${toolCall.name}" has no execute function. Use manual tool execution.`);
|
|
220
230
|
}
|
|
221
231
|
if (isGeneratorTool(tool)) {
|
|
222
|
-
return executeGeneratorTool(tool, toolCall, context, onPreliminaryResult);
|
|
232
|
+
return executeGeneratorTool(tool, toolCall, context, onPreliminaryResult, contextStore, sharedSchema);
|
|
223
233
|
}
|
|
224
|
-
return executeRegularTool(tool, toolCall, context);
|
|
234
|
+
return executeRegularTool(tool, toolCall, context, contextStore, sharedSchema);
|
|
225
235
|
}
|
|
226
236
|
/**
|
|
227
237
|
* Find a tool by name in the tools array
|
|
@@ -13,7 +13,7 @@ export interface ToolOrchestrationResult {
|
|
|
13
13
|
finalResponse: models.OpenResponsesNonStreamingResponse;
|
|
14
14
|
allResponses: models.OpenResponsesNonStreamingResponse[];
|
|
15
15
|
toolExecutionResults: ToolExecutionResult<Tool>[];
|
|
16
|
-
conversationInput: models.
|
|
16
|
+
conversationInput: models.OpenResponsesInputUnion;
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
19
|
* Execute tool calls and manage multi-turn conversations
|
|
@@ -27,7 +27,7 @@ export interface ToolOrchestrationResult {
|
|
|
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.
|
|
30
|
+
export declare function executeToolLoop(sendRequest: (input: models.OpenResponsesInputUnion, tools: APITool[]) => Promise<models.OpenResponsesNonStreamingResponse>, initialInput: models.OpenResponsesInputUnion, 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
|
*/
|
package/esm/lib/tool-types.d.ts
CHANGED
|
@@ -20,6 +20,62 @@ export interface TurnContext {
|
|
|
20
20
|
/** The full request being sent to the API (only available during tool execution) */
|
|
21
21
|
turnRequest?: models.OpenResponsesRequest;
|
|
22
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Extract context schema type from a tool definition
|
|
25
|
+
* Returns the inferred type of the tool's contextSchema, or empty object if none
|
|
26
|
+
*/
|
|
27
|
+
export type InferToolContext<T> = T extends {
|
|
28
|
+
function: {
|
|
29
|
+
contextSchema: infer S;
|
|
30
|
+
};
|
|
31
|
+
} ? S extends $ZodType ? zodInfer<S> : Record<string, never> : Record<string, never>;
|
|
32
|
+
/**
|
|
33
|
+
* Extract tool name from a tool definition
|
|
34
|
+
*/
|
|
35
|
+
type InferToolName<T> = T extends {
|
|
36
|
+
function: {
|
|
37
|
+
name: infer N extends string;
|
|
38
|
+
};
|
|
39
|
+
} ? N : string;
|
|
40
|
+
/**
|
|
41
|
+
* Flat execute context passed as the second argument to tool execute functions.
|
|
42
|
+
* Merges TurnContext fields with a `local` getter (own tool context) and `setContext()`.
|
|
43
|
+
*
|
|
44
|
+
* @template TName - The tool's literal name string
|
|
45
|
+
* @template TContext - The shape of the tool's contextSchema
|
|
46
|
+
* @template TShared - The shape of the sharedContextSchema
|
|
47
|
+
*/
|
|
48
|
+
export type ToolExecuteContext<TName extends string = string, TContext extends Record<string, unknown> = Record<string, unknown>, TShared extends Record<string, unknown> = Record<string, unknown>> = TurnContext & {
|
|
49
|
+
/** The tool's name (type-level only, for generic inference) */
|
|
50
|
+
readonly _toolName?: TName;
|
|
51
|
+
/** This tool's own context (reads from the store, frozen snapshot) */
|
|
52
|
+
local: Readonly<TContext>;
|
|
53
|
+
/** Mutate this tool's context in the shared store (persists across turns) */
|
|
54
|
+
setContext(partial: Partial<TContext>): void;
|
|
55
|
+
/** Shared context visible to all tools */
|
|
56
|
+
shared: Readonly<TShared>;
|
|
57
|
+
/** Mutate the shared context in the store (persists across turns) */
|
|
58
|
+
setSharedContext(partial: Partial<TShared>): void;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Context map keyed by tool name for callModel's `context` option.
|
|
62
|
+
* Each key is a tool's name, each value is that tool's inferred context type.
|
|
63
|
+
*/
|
|
64
|
+
export type ToolContextMap<T extends readonly Tool[]> = {
|
|
65
|
+
[K in T[number] as InferToolName<K>]: InferToolContext<K>;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Context map with an optional `shared` key for shared context.
|
|
69
|
+
* When TShared is provided (non-empty), a `shared` key is added to the map.
|
|
70
|
+
*/
|
|
71
|
+
export type ToolContextMapWithShared<T extends readonly Tool[], TShared extends Record<string, unknown> = Record<string, never>> = ToolContextMap<T> & (TShared extends Record<string, never> ? {} : {
|
|
72
|
+
shared: TShared;
|
|
73
|
+
});
|
|
74
|
+
/**
|
|
75
|
+
* Reserved key in the context store for shared context data.
|
|
76
|
+
* The tool name 'shared' is forbidden — it's reserved for this purpose.
|
|
77
|
+
*/
|
|
78
|
+
export declare const SHARED_CONTEXT_KEY: "shared";
|
|
23
79
|
/**
|
|
24
80
|
* Context passed to nextTurnParams functions
|
|
25
81
|
* Contains current request state for parameter computation
|
|
@@ -27,7 +83,7 @@ export interface TurnContext {
|
|
|
27
83
|
*/
|
|
28
84
|
export type NextTurnParamsContext = {
|
|
29
85
|
/** Current input (messages) */
|
|
30
|
-
input: models.
|
|
86
|
+
input: models.OpenResponsesInputUnion;
|
|
31
87
|
/** Current model selection */
|
|
32
88
|
model: string;
|
|
33
89
|
/** Current models array */
|
|
@@ -64,6 +120,8 @@ export interface BaseToolFunction<TInput extends $ZodObject<$ZodShape>> {
|
|
|
64
120
|
name: string;
|
|
65
121
|
description?: string;
|
|
66
122
|
inputSchema: TInput;
|
|
123
|
+
/** Zod schema declaring the context data this tool needs */
|
|
124
|
+
contextSchema?: $ZodObject<$ZodShape>;
|
|
67
125
|
nextTurnParams?: NextTurnParamsFunctions<zodInfer<TInput>>;
|
|
68
126
|
/**
|
|
69
127
|
* Whether this tool requires human approval before execution
|
|
@@ -73,10 +131,12 @@ export interface BaseToolFunction<TInput extends $ZodObject<$ZodShape>> {
|
|
|
73
131
|
}
|
|
74
132
|
/**
|
|
75
133
|
* Regular tool with synchronous or asynchronous execute function and optional outputSchema
|
|
134
|
+
* @template TContext - Shape of the tool's context (inferred from contextSchema)
|
|
135
|
+
* @template TName - The tool's literal name string
|
|
76
136
|
*/
|
|
77
|
-
export interface ToolFunctionWithExecute<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown
|
|
137
|
+
export interface ToolFunctionWithExecute<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string> extends BaseToolFunction<TInput> {
|
|
78
138
|
outputSchema?: TOutput;
|
|
79
|
-
execute: (params: zodInfer<TInput>, context?:
|
|
139
|
+
execute: (params: zodInfer<TInput>, context?: ToolExecuteContext<TName, TContext>) => Promise<zodInfer<TOutput>> | zodInfer<TOutput>;
|
|
80
140
|
}
|
|
81
141
|
/**
|
|
82
142
|
* Generator-based tool with async generator execute function
|
|
@@ -100,10 +160,10 @@ export interface ToolFunctionWithExecute<TInput extends $ZodObject<$ZodShape>, T
|
|
|
100
160
|
* }
|
|
101
161
|
* ```
|
|
102
162
|
*/
|
|
103
|
-
export interface ToolFunctionWithGenerator<TInput extends $ZodObject<$ZodShape>, TEvent extends $ZodType = $ZodType<unknown>, TOutput extends $ZodType = $ZodType<unknown
|
|
163
|
+
export interface ToolFunctionWithGenerator<TInput extends $ZodObject<$ZodShape>, TEvent extends $ZodType = $ZodType<unknown>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string> extends BaseToolFunction<TInput> {
|
|
104
164
|
eventSchema: TEvent;
|
|
105
165
|
outputSchema: TOutput;
|
|
106
|
-
execute: (params: zodInfer<TInput>, context?:
|
|
166
|
+
execute: (params: zodInfer<TInput>, context?: ToolExecuteContext<TName, TContext>) => AsyncGenerator<zodInfer<TEvent> | zodInfer<TOutput>, zodInfer<TOutput> | void>;
|
|
107
167
|
}
|
|
108
168
|
/**
|
|
109
169
|
* Manual tool without execute function - requires manual handling by developer
|
|
@@ -114,16 +174,16 @@ export interface ManualToolFunction<TInput extends $ZodObject<$ZodShape>, TOutpu
|
|
|
114
174
|
/**
|
|
115
175
|
* Tool with execute function (regular or generator)
|
|
116
176
|
*/
|
|
117
|
-
export type ToolWithExecute<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>> = {
|
|
177
|
+
export type ToolWithExecute<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>> = {
|
|
118
178
|
type: ToolType.Function;
|
|
119
|
-
function: ToolFunctionWithExecute<TInput, TOutput>;
|
|
179
|
+
function: ToolFunctionWithExecute<TInput, TOutput, TContext>;
|
|
120
180
|
};
|
|
121
181
|
/**
|
|
122
182
|
* Tool with generator execute function
|
|
123
183
|
*/
|
|
124
|
-
export type ToolWithGenerator<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TEvent extends $ZodType = $ZodType<unknown>, TOutput extends $ZodType = $ZodType<unknown>> = {
|
|
184
|
+
export type ToolWithGenerator<TInput extends $ZodObject<$ZodShape> = $ZodObject<$ZodShape>, TEvent extends $ZodType = $ZodType<unknown>, TOutput extends $ZodType = $ZodType<unknown>, TContext extends Record<string, unknown> = Record<string, unknown>> = {
|
|
125
185
|
type: ToolType.Function;
|
|
126
|
-
function: ToolFunctionWithGenerator<TInput, TEvent, TOutput>;
|
|
186
|
+
function: ToolFunctionWithGenerator<TInput, TEvent, TOutput, TContext>;
|
|
127
187
|
};
|
|
128
188
|
/**
|
|
129
189
|
* Tool without execute function (manual handling)
|
|
@@ -235,8 +295,8 @@ export interface ParsedToolCall<T extends Tool> {
|
|
|
235
295
|
export interface ToolExecutionResult<T extends Tool> {
|
|
236
296
|
toolCallId: string;
|
|
237
297
|
toolName: string;
|
|
238
|
-
result: T extends ToolWithExecute
|
|
239
|
-
preliminaryResults?: T extends ToolWithGenerator
|
|
298
|
+
result: T extends ToolWithExecute<$ZodObject<$ZodShape>, infer O> | ToolWithGenerator<$ZodObject<$ZodShape>, $ZodType<unknown>, infer O> ? zodInfer<O> : unknown;
|
|
299
|
+
preliminaryResults?: T extends ToolWithGenerator<$ZodObject<$ZodShape>, infer E> ? zodInfer<E>[] : undefined;
|
|
240
300
|
error?: Error;
|
|
241
301
|
}
|
|
242
302
|
/**
|
|
@@ -321,13 +381,31 @@ export type ToolResultEvent<TResult = unknown, TPreliminaryResults = unknown> =
|
|
|
321
381
|
timestamp: number;
|
|
322
382
|
preliminaryResults?: TPreliminaryResults[];
|
|
323
383
|
};
|
|
384
|
+
/**
|
|
385
|
+
* Turn start event emitted at the beginning of each API turn
|
|
386
|
+
* Turn 0 is the initial request, subsequent turns follow tool execution
|
|
387
|
+
*/
|
|
388
|
+
export type TurnStartEvent = {
|
|
389
|
+
type: 'turn.start';
|
|
390
|
+
turnNumber: number;
|
|
391
|
+
timestamp: number;
|
|
392
|
+
};
|
|
393
|
+
/**
|
|
394
|
+
* Turn end event emitted at the end of each API turn
|
|
395
|
+
*/
|
|
396
|
+
export type TurnEndEvent = {
|
|
397
|
+
type: 'turn.end';
|
|
398
|
+
turnNumber: number;
|
|
399
|
+
timestamp: number;
|
|
400
|
+
};
|
|
324
401
|
/**
|
|
325
402
|
* Enhanced stream event types for getFullResponsesStream
|
|
326
|
-
* Extends OpenResponsesStreamEvent with tool preliminary results
|
|
403
|
+
* Extends OpenResponsesStreamEvent with tool preliminary results, tool results,
|
|
404
|
+
* and turn delimiter events for multi-turn streaming
|
|
327
405
|
* @template TEvent - The event type from generator tools
|
|
328
406
|
* @template TResult - The result type from tool execution
|
|
329
407
|
*/
|
|
330
|
-
export type ResponseStreamEvent<TEvent = unknown, TResult = unknown> = OpenResponsesStreamEvent | ToolPreliminaryResultEvent<TEvent> | ToolResultEvent<TResult, TEvent
|
|
408
|
+
export type ResponseStreamEvent<TEvent = unknown, TResult = unknown> = OpenResponsesStreamEvent | ToolPreliminaryResultEvent<TEvent> | ToolResultEvent<TResult, TEvent> | TurnStartEvent | TurnEndEvent;
|
|
331
409
|
/**
|
|
332
410
|
* Type guard to check if an event is a tool preliminary result event
|
|
333
411
|
*/
|
|
@@ -336,6 +414,14 @@ export declare function isToolPreliminaryResultEvent<TEvent = unknown>(event: Re
|
|
|
336
414
|
* Type guard to check if an event is a tool result event
|
|
337
415
|
*/
|
|
338
416
|
export declare function isToolResultEvent<TResult = unknown, TPreliminaryResults = unknown>(event: ResponseStreamEvent<TPreliminaryResults, TResult>): event is ToolResultEvent<TResult, TPreliminaryResults>;
|
|
417
|
+
/**
|
|
418
|
+
* Type guard to check if an event is a turn start event
|
|
419
|
+
*/
|
|
420
|
+
export declare function isTurnStartEvent(event: ResponseStreamEvent): event is TurnStartEvent;
|
|
421
|
+
/**
|
|
422
|
+
* Type guard to check if an event is a turn end event
|
|
423
|
+
*/
|
|
424
|
+
export declare function isTurnEndEvent(event: ResponseStreamEvent): event is TurnEndEvent;
|
|
339
425
|
/**
|
|
340
426
|
* Tool stream event types for getToolStream
|
|
341
427
|
* Includes both argument deltas and preliminary results
|
|
@@ -405,7 +491,7 @@ export interface ConversationState<TTools extends readonly Tool[] = readonly Too
|
|
|
405
491
|
/** Unique identifier for this conversation */
|
|
406
492
|
id: string;
|
|
407
493
|
/** Full message history */
|
|
408
|
-
messages: models.
|
|
494
|
+
messages: models.OpenResponsesInputUnion;
|
|
409
495
|
/** Previous response ID for chaining (OpenRouter server-side optimization) */
|
|
410
496
|
previousResponseId?: string;
|
|
411
497
|
/** Tool calls awaiting human approval */
|
|
@@ -465,4 +551,5 @@ export declare function toolHasApprovalConfigured(tool: Tool): boolean;
|
|
|
465
551
|
* Type guard to check if any tools in array have approval configured at runtime
|
|
466
552
|
*/
|
|
467
553
|
export declare function hasApprovalRequiredTools(tools: readonly Tool[]): boolean;
|
|
554
|
+
export {};
|
|
468
555
|
//# sourceMappingURL=tool-types.d.ts.map
|
package/esm/lib/tool-types.js
CHANGED
|
@@ -5,6 +5,11 @@ export var ToolType;
|
|
|
5
5
|
(function (ToolType) {
|
|
6
6
|
ToolType["Function"] = "function";
|
|
7
7
|
})(ToolType || (ToolType = {}));
|
|
8
|
+
/**
|
|
9
|
+
* Reserved key in the context store for shared context data.
|
|
10
|
+
* The tool name 'shared' is forbidden — it's reserved for this purpose.
|
|
11
|
+
*/
|
|
12
|
+
export const SHARED_CONTEXT_KEY = 'shared';
|
|
8
13
|
/**
|
|
9
14
|
* Type guard to check if a tool has an execute function
|
|
10
15
|
*/
|
|
@@ -41,6 +46,18 @@ export function isToolPreliminaryResultEvent(event) {
|
|
|
41
46
|
export function isToolResultEvent(event) {
|
|
42
47
|
return event.type === 'tool.result';
|
|
43
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Type guard to check if an event is a turn start event
|
|
51
|
+
*/
|
|
52
|
+
export function isTurnStartEvent(event) {
|
|
53
|
+
return event.type === 'turn.start';
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Type guard to check if an event is a turn end event
|
|
57
|
+
*/
|
|
58
|
+
export function isTurnEndEvent(event) {
|
|
59
|
+
return event.type === 'turn.end';
|
|
60
|
+
}
|
|
44
61
|
/**
|
|
45
62
|
* Type guard to check if a tool has approval configured at runtime
|
|
46
63
|
*/
|
package/esm/lib/tool.d.ts
CHANGED
|
@@ -1,55 +1,49 @@
|
|
|
1
1
|
import type { $ZodObject, $ZodShape, $ZodType, infer as zodInfer } from 'zod/v4/core';
|
|
2
|
-
import { type
|
|
2
|
+
import { type Tool, type ToolExecuteContext, type ToolWithExecute, type ToolWithGenerator, type ManualTool, type NextTurnParamsFunctions, type ToolApprovalCheck } from "./tool-types.js";
|
|
3
3
|
/**
|
|
4
4
|
* Configuration for a regular tool with outputSchema
|
|
5
5
|
*/
|
|
6
|
-
type RegularToolConfigWithOutput<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType> = {
|
|
7
|
-
name:
|
|
6
|
+
type RegularToolConfigWithOutput<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string> = {
|
|
7
|
+
name: TName;
|
|
8
8
|
description?: string;
|
|
9
9
|
inputSchema: TInput;
|
|
10
10
|
outputSchema: TOutput;
|
|
11
11
|
eventSchema?: undefined;
|
|
12
|
+
/** Zod schema declaring the context data this tool needs */
|
|
13
|
+
contextSchema?: $ZodObject<$ZodShape>;
|
|
12
14
|
nextTurnParams?: NextTurnParamsFunctions<zodInfer<TInput>>;
|
|
13
|
-
/**
|
|
14
|
-
* Whether this tool requires human approval before execution
|
|
15
|
-
* Can be a boolean or an async function that receives the tool's input params and context
|
|
16
|
-
*/
|
|
17
15
|
requireApproval?: boolean | ToolApprovalCheck<zodInfer<TInput>>;
|
|
18
|
-
execute: (params: zodInfer<TInput>, context?:
|
|
16
|
+
execute: (params: zodInfer<TInput>, context?: ToolExecuteContext<TName, TContext>) => Promise<zodInfer<TOutput>> | zodInfer<TOutput>;
|
|
19
17
|
};
|
|
20
18
|
/**
|
|
21
19
|
* Configuration for a regular tool without outputSchema (infers return type from execute)
|
|
22
20
|
*/
|
|
23
|
-
type RegularToolConfigWithoutOutput<TInput extends $ZodObject<$ZodShape>, TReturn> = {
|
|
24
|
-
name:
|
|
21
|
+
type RegularToolConfigWithoutOutput<TInput extends $ZodObject<$ZodShape>, TReturn, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string> = {
|
|
22
|
+
name: TName;
|
|
25
23
|
description?: string;
|
|
26
24
|
inputSchema: TInput;
|
|
27
25
|
outputSchema?: undefined;
|
|
28
26
|
eventSchema?: undefined;
|
|
27
|
+
/** Zod schema declaring the context data this tool needs */
|
|
28
|
+
contextSchema?: $ZodObject<$ZodShape>;
|
|
29
29
|
nextTurnParams?: NextTurnParamsFunctions<zodInfer<TInput>>;
|
|
30
|
-
/**
|
|
31
|
-
* Whether this tool requires human approval before execution
|
|
32
|
-
* Can be a boolean or an async function that receives the tool's input params and context
|
|
33
|
-
*/
|
|
34
30
|
requireApproval?: boolean | ToolApprovalCheck<zodInfer<TInput>>;
|
|
35
|
-
execute: (params: zodInfer<TInput>, context?:
|
|
31
|
+
execute: (params: zodInfer<TInput>, context?: ToolExecuteContext<TName, TContext>) => Promise<TReturn> | TReturn;
|
|
36
32
|
};
|
|
37
33
|
/**
|
|
38
34
|
* Configuration for a generator tool (with eventSchema)
|
|
39
35
|
*/
|
|
40
|
-
type GeneratorToolConfig<TInput extends $ZodObject<$ZodShape>, TEvent extends $ZodType, TOutput extends $ZodType> = {
|
|
41
|
-
name:
|
|
36
|
+
type GeneratorToolConfig<TInput extends $ZodObject<$ZodShape>, TEvent extends $ZodType, TOutput extends $ZodType, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string> = {
|
|
37
|
+
name: TName;
|
|
42
38
|
description?: string;
|
|
43
39
|
inputSchema: TInput;
|
|
44
40
|
eventSchema: TEvent;
|
|
45
41
|
outputSchema: TOutput;
|
|
42
|
+
/** Zod schema declaring the context data this tool needs */
|
|
43
|
+
contextSchema?: $ZodObject<$ZodShape>;
|
|
46
44
|
nextTurnParams?: NextTurnParamsFunctions<zodInfer<TInput>>;
|
|
47
|
-
/**
|
|
48
|
-
* Whether this tool requires human approval before execution
|
|
49
|
-
* Can be a boolean or an async function that receives the tool's input params and context
|
|
50
|
-
*/
|
|
51
45
|
requireApproval?: boolean | ToolApprovalCheck<zodInfer<TInput>>;
|
|
52
|
-
execute: (params: zodInfer<TInput>, context?:
|
|
46
|
+
execute: (params: zodInfer<TInput>, context?: ToolExecuteContext<TName, TContext>) => AsyncGenerator<zodInfer<TEvent> | zodInfer<TOutput>>;
|
|
53
47
|
};
|
|
54
48
|
/**
|
|
55
49
|
* Configuration for a manual tool (execute: false, no eventSchema or outputSchema)
|
|
@@ -58,14 +52,27 @@ type ManualToolConfig<TInput extends $ZodObject<$ZodShape>> = {
|
|
|
58
52
|
name: string;
|
|
59
53
|
description?: string;
|
|
60
54
|
inputSchema: TInput;
|
|
55
|
+
/** Zod schema declaring the context data this tool needs */
|
|
56
|
+
contextSchema?: $ZodObject<$ZodShape>;
|
|
61
57
|
nextTurnParams?: NextTurnParamsFunctions<zodInfer<TInput>>;
|
|
62
|
-
/**
|
|
63
|
-
* Whether this tool requires human approval before execution
|
|
64
|
-
* Can be a boolean or an async function that receives the tool's input params and context
|
|
65
|
-
*/
|
|
66
58
|
requireApproval?: boolean | ToolApprovalCheck<zodInfer<TInput>>;
|
|
67
59
|
execute: false;
|
|
68
60
|
};
|
|
61
|
+
/**
|
|
62
|
+
* Loose config type for the `tool<TShared>()` overload.
|
|
63
|
+
* Accepts any valid tool config while typing `ctx.shared` from TShared.
|
|
64
|
+
*/
|
|
65
|
+
type ToolConfigWithSharedContext<TShared extends Record<string, unknown>> = {
|
|
66
|
+
name: string;
|
|
67
|
+
description?: string;
|
|
68
|
+
inputSchema: $ZodObject<$ZodShape>;
|
|
69
|
+
outputSchema?: $ZodType;
|
|
70
|
+
eventSchema?: $ZodType;
|
|
71
|
+
contextSchema?: $ZodObject<$ZodShape>;
|
|
72
|
+
nextTurnParams?: NextTurnParamsFunctions<Record<string, unknown>>;
|
|
73
|
+
requireApproval?: boolean | ToolApprovalCheck<Record<string, unknown>>;
|
|
74
|
+
execute: ((params: Record<string, unknown>, context?: ToolExecuteContext<string, Record<string, unknown>, TShared>) => unknown) | ((params: Record<string, unknown>, context?: ToolExecuteContext<string, Record<string, unknown>, TShared>) => AsyncGenerator<unknown>) | false;
|
|
75
|
+
};
|
|
69
76
|
/**
|
|
70
77
|
* Creates a tool with full type inference from Zod schemas.
|
|
71
78
|
*
|
|
@@ -74,46 +81,28 @@ type ManualToolConfig<TInput extends $ZodObject<$ZodShape>> = {
|
|
|
74
81
|
* - **Regular tool**: When `execute` is a function (no `eventSchema`)
|
|
75
82
|
* - **Manual tool**: When `execute: false` is set
|
|
76
83
|
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
* name: "get_weather",
|
|
81
|
-
* description: "Get weather for a location",
|
|
82
|
-
* inputSchema: z.object({ location: z.string() }),
|
|
83
|
-
* outputSchema: z.object({ temperature: z.number() }),
|
|
84
|
-
* execute: async (params) => {
|
|
85
|
-
* // params is typed as { location: string }
|
|
86
|
-
* return { temperature: 72 }; // return type is enforced
|
|
87
|
-
* },
|
|
88
|
-
* });
|
|
89
|
-
* ```
|
|
84
|
+
* Shared context typing: Pass a type parameter to type `ctx.shared`
|
|
85
|
+
* in the execute callback. Runtime validation happens at callModel
|
|
86
|
+
* via `sharedContextSchema`.
|
|
90
87
|
*
|
|
91
|
-
* @example
|
|
88
|
+
* @example Regular tool with typed shared context:
|
|
92
89
|
* ```typescript
|
|
93
|
-
*
|
|
94
|
-
* name: "process_data",
|
|
95
|
-
* inputSchema: z.object({ data: z.string() }),
|
|
96
|
-
* eventSchema: z.object({ progress: z.number() }),
|
|
97
|
-
* outputSchema: z.object({ result: z.string() }),
|
|
98
|
-
* execute: async function* (params) {
|
|
99
|
-
* yield { progress: 50 }; // typed as event
|
|
100
|
-
* yield { result: "done" }; // typed as output
|
|
101
|
-
* },
|
|
102
|
-
* });
|
|
103
|
-
* ```
|
|
90
|
+
* type SharedCtx = z.infer<typeof SharedContextSchema>;
|
|
104
91
|
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
92
|
+
* const execTool = tool<SharedCtx>({
|
|
93
|
+
* name: "sandbox_exec",
|
|
94
|
+
* inputSchema: z.object({ command: z.string() }),
|
|
95
|
+
* execute: async (params, ctx) => {
|
|
96
|
+
* ctx?.shared._sessionId; // string | undefined
|
|
97
|
+
* return { output: '...' };
|
|
98
|
+
* },
|
|
111
99
|
* });
|
|
112
100
|
* ```
|
|
113
101
|
*/
|
|
114
|
-
export declare function tool<TInput extends $ZodObject<$ZodShape>, TEvent extends $ZodType, TOutput extends $ZodType>(config: GeneratorToolConfig<TInput, TEvent, TOutput>): ToolWithGenerator<TInput, TEvent, TOutput>;
|
|
102
|
+
export declare function tool<TInput extends $ZodObject<$ZodShape>, TEvent extends $ZodType, TOutput extends $ZodType, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string>(config: GeneratorToolConfig<TInput, TEvent, TOutput, TContext, TName>): ToolWithGenerator<TInput, TEvent, TOutput, TContext>;
|
|
115
103
|
export declare function tool<TInput extends $ZodObject<$ZodShape>>(config: ManualToolConfig<TInput>): ManualTool<TInput>;
|
|
116
|
-
export declare function tool<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType>(config: RegularToolConfigWithOutput<TInput, TOutput>): ToolWithExecute<TInput, TOutput>;
|
|
117
|
-
export declare function tool<TInput extends $ZodObject<$ZodShape>, TReturn>(config: RegularToolConfigWithoutOutput<TInput, TReturn>): ToolWithExecute<TInput, $ZodType<TReturn
|
|
104
|
+
export declare function tool<TInput extends $ZodObject<$ZodShape>, TOutput extends $ZodType, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string>(config: RegularToolConfigWithOutput<TInput, TOutput, TContext, TName>): ToolWithExecute<TInput, TOutput, TContext>;
|
|
105
|
+
export declare function tool<TInput extends $ZodObject<$ZodShape>, TReturn, TContext extends Record<string, unknown> = Record<string, unknown>, TName extends string = string>(config: RegularToolConfigWithoutOutput<TInput, TReturn, TContext, TName>): ToolWithExecute<TInput, $ZodType<TReturn>, TContext>;
|
|
106
|
+
export declare function tool<TShared extends Record<string, unknown>>(config: ToolConfigWithSharedContext<TShared>): Tool;
|
|
118
107
|
export {};
|
|
119
108
|
//# sourceMappingURL=tool.d.ts.map
|
package/esm/lib/tool.js
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
|
-
import { ToolType, } from "./tool-types.js";
|
|
2
|
-
/**
|
|
3
|
-
* Type guard to check if config is a generator tool config (has eventSchema)
|
|
4
|
-
*/
|
|
5
|
-
function isGeneratorConfig(config) {
|
|
6
|
-
return "eventSchema" in config && config.eventSchema !== undefined;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Type guard to check if config is a manual tool config (execute === false)
|
|
10
|
-
*/
|
|
11
|
-
function isManualConfig(config) {
|
|
12
|
-
return config.execute === false;
|
|
13
|
-
}
|
|
1
|
+
import { ToolType, SHARED_CONTEXT_KEY, } from "./tool-types.js";
|
|
14
2
|
// Implementation
|
|
15
3
|
export function tool(config) {
|
|
4
|
+
// 'shared' is reserved for shared context — forbid it as a tool name
|
|
5
|
+
if (config.name === SHARED_CONTEXT_KEY) {
|
|
6
|
+
throw new Error(`Tool name "${SHARED_CONTEXT_KEY}" is reserved for shared context. Choose a different name.`);
|
|
7
|
+
}
|
|
16
8
|
// Check for manual tool first (execute === false)
|
|
17
|
-
if (
|
|
9
|
+
if (config.execute === false) {
|
|
18
10
|
const fn = {
|
|
19
11
|
name: config.name,
|
|
20
12
|
inputSchema: config.inputSchema,
|
|
@@ -22,6 +14,9 @@ export function tool(config) {
|
|
|
22
14
|
if (config.description !== undefined) {
|
|
23
15
|
fn.description = config.description;
|
|
24
16
|
}
|
|
17
|
+
if (config.contextSchema !== undefined) {
|
|
18
|
+
fn.contextSchema = config.contextSchema;
|
|
19
|
+
}
|
|
25
20
|
if (config.nextTurnParams !== undefined) {
|
|
26
21
|
fn.nextTurnParams = config.nextTurnParams;
|
|
27
22
|
}
|
|
@@ -34,18 +29,20 @@ export function tool(config) {
|
|
|
34
29
|
};
|
|
35
30
|
}
|
|
36
31
|
// Check for generator tool (has eventSchema)
|
|
37
|
-
if (
|
|
32
|
+
if ('eventSchema' in config && config.eventSchema !== undefined) {
|
|
38
33
|
const fn = {
|
|
39
34
|
name: config.name,
|
|
40
35
|
inputSchema: config.inputSchema,
|
|
41
36
|
eventSchema: config.eventSchema,
|
|
42
37
|
outputSchema: config.outputSchema,
|
|
43
|
-
// Types now align - config.execute matches the interface type
|
|
44
38
|
execute: config.execute,
|
|
45
39
|
};
|
|
46
40
|
if (config.description !== undefined) {
|
|
47
41
|
fn.description = config.description;
|
|
48
42
|
}
|
|
43
|
+
if (config.contextSchema !== undefined) {
|
|
44
|
+
fn.contextSchema = config.contextSchema;
|
|
45
|
+
}
|
|
49
46
|
if (config.nextTurnParams !== undefined) {
|
|
50
47
|
fn.nextTurnParams = config.nextTurnParams;
|
|
51
48
|
}
|
|
@@ -58,21 +55,20 @@ export function tool(config) {
|
|
|
58
55
|
};
|
|
59
56
|
}
|
|
60
57
|
// Regular tool (has execute function, no eventSchema)
|
|
61
|
-
// TypeScript can't infer the relationship between TReturn and TOutput
|
|
62
|
-
// So we build the object without type annotation, then return with correct type
|
|
63
58
|
const functionObj = {
|
|
64
59
|
name: config.name,
|
|
65
60
|
inputSchema: config.inputSchema,
|
|
66
61
|
execute: config.execute,
|
|
67
62
|
...(config.description !== undefined && { description: config.description }),
|
|
68
63
|
...(config.outputSchema !== undefined && { outputSchema: config.outputSchema }),
|
|
64
|
+
...(config.contextSchema !== undefined && { contextSchema: config.contextSchema }),
|
|
69
65
|
...(config.nextTurnParams !== undefined && { nextTurnParams: config.nextTurnParams }),
|
|
70
66
|
...(config.requireApproval !== undefined && { requireApproval: config.requireApproval }),
|
|
71
67
|
};
|
|
72
|
-
// The function signature guarantees this is type-safe via overloads
|
|
73
68
|
return {
|
|
74
69
|
type: ToolType.Function,
|
|
75
70
|
function: functionObj,
|
|
76
71
|
};
|
|
77
72
|
}
|
|
73
|
+
//#endregion
|
|
78
74
|
//# sourceMappingURL=tool.js.map
|
|
@@ -46,5 +46,5 @@ export declare function buildTurnContext(options: BuildTurnContextOptions): Turn
|
|
|
46
46
|
* // Returns: [{ role: "user", content: "Hello!" }]
|
|
47
47
|
* ```
|
|
48
48
|
*/
|
|
49
|
-
export declare function normalizeInputToArray(input: models.
|
|
49
|
+
export declare function normalizeInputToArray(input: models.OpenResponsesInputUnion): Array<models.OpenResponsesInputUnion1>;
|
|
50
50
|
//# sourceMappingURL=turn-context.d.ts.map
|
|
@@ -2,6 +2,7 @@ import * as z from "zod/v4";
|
|
|
2
2
|
import { ClosedEnum } from "../types/enums.js";
|
|
3
3
|
import { Result as SafeParseResult } from "../types/fp.js";
|
|
4
4
|
import { AssistantMessageImages, AssistantMessageImages$Outbound } from "./assistantmessageimages.js";
|
|
5
|
+
import { ChatCompletionAudioOutput, ChatCompletionAudioOutput$Outbound } from "./chatcompletionaudiooutput.js";
|
|
5
6
|
import { ChatMessageContentItem, ChatMessageContentItem$Outbound } from "./chatmessagecontentitem.js";
|
|
6
7
|
import { ChatMessageToolCall, ChatMessageToolCall$Outbound } from "./chatmessagetoolcall.js";
|
|
7
8
|
import { SDKValidationError } from "./errors/sdkvalidationerror.js";
|
|
@@ -47,6 +48,10 @@ export type AssistantMessage = {
|
|
|
47
48
|
* Generated images from image generation models
|
|
48
49
|
*/
|
|
49
50
|
images?: Array<AssistantMessageImages> | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Audio output data or reference
|
|
53
|
+
*/
|
|
54
|
+
audio?: ChatCompletionAudioOutput | undefined;
|
|
50
55
|
};
|
|
51
56
|
/** @internal */
|
|
52
57
|
export declare const AssistantMessageRole$inboundSchema: z.ZodEnum<typeof AssistantMessageRole>;
|
|
@@ -72,6 +77,7 @@ export type AssistantMessage$Outbound = {
|
|
|
72
77
|
reasoning?: string | null | undefined;
|
|
73
78
|
reasoning_details?: Array<ReasoningDetailUnion$Outbound> | undefined;
|
|
74
79
|
images?: Array<AssistantMessageImages$Outbound> | undefined;
|
|
80
|
+
audio?: ChatCompletionAudioOutput$Outbound | undefined;
|
|
75
81
|
};
|
|
76
82
|
/** @internal */
|
|
77
83
|
export declare const AssistantMessage$outboundSchema: z.ZodType<AssistantMessage$Outbound, AssistantMessage>;
|