diff --git a/langchain-core/src/tools/index.ts b/langchain-core/src/tools/index.ts index 37046abca0ac..121833938663 100644 --- a/langchain-core/src/tools/index.ts +++ b/langchain-core/src/tools/index.ts @@ -57,6 +57,11 @@ export interface ToolParams extends BaseLangChainParams { verboseParsingErrors?: boolean; } +export type ToolRunnableConfig< + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ConfigurableFieldType extends Record = Record +> = RunnableConfig & { toolCall?: ToolCall }; + /** * Schema for defining tools. * @@ -159,7 +164,7 @@ export abstract class StructuredTool< protected abstract _call( arg: z.output, runManager?: CallbackManagerForToolRun, - parentConfig?: RunnableConfig & { toolCall?: ToolCall } + parentConfig?: ToolRunnableConfig ): Promise; /** @@ -182,8 +187,7 @@ export abstract class StructuredTool< | ToolCall | undefined; - let enrichedConfig: RunnableConfig & { toolCall?: ToolCall } = - ensureConfig(config); + let enrichedConfig: ToolRunnableConfig = ensureConfig(config); if (_isToolCall(input)) { tool_call_id = input.id; toolInput = input.args; @@ -215,7 +219,7 @@ export abstract class StructuredTool< */ async call( arg: (z.output extends string ? string : never) | z.input, - configArg?: Callbacks | (RunnableConfig & { toolCall?: ToolCall }), + configArg?: Callbacks | ToolRunnableConfig, /** @deprecated */ tags?: string[] ): Promise { @@ -353,7 +357,7 @@ export interface DynamicToolInput extends BaseDynamicToolInput { func: ( input: string, runManager?: CallbackManagerForToolRun, - config?: RunnableConfig & { toolCall?: ToolCall } + config?: ToolRunnableConfig ) => Promise; } @@ -403,7 +407,7 @@ export class DynamicTool extends Tool { */ async call( arg: string | undefined | z.input | ToolCall, - configArg?: (RunnableConfig & { toolCall?: ToolCall }) | Callbacks + configArg?: ToolRunnableConfig | Callbacks ): Promise { const config = parseCallbackConfigArg(configArg); if (config.runName === undefined) { @@ -416,7 +420,7 @@ export class DynamicTool extends Tool { async _call( input: string, runManager?: CallbackManagerForToolRun, - parentConfig?: RunnableConfig & { toolCall?: ToolCall } + parentConfig?: ToolRunnableConfig ): Promise { return this.func(input, runManager, parentConfig); } @@ -556,30 +560,18 @@ interface ToolWrapperParams< * @returns {DynamicStructuredTool} A new StructuredTool instance. */ export function tool( - func: RunnableFunc< - z.output, - ToolReturnType, - RunnableConfig & { toolCall?: ToolCall } - >, + func: RunnableFunc, ToolReturnType, ToolRunnableConfig>, fields: ToolWrapperParams ): DynamicTool; export function tool( - func: RunnableFunc< - z.output, - ToolReturnType, - RunnableConfig & { toolCall?: ToolCall } - >, + func: RunnableFunc, ToolReturnType, ToolRunnableConfig>, fields: ToolWrapperParams ): DynamicStructuredTool; // eslint-disable-next-line @typescript-eslint/no-explicit-any export function tool>( - func: RunnableFunc< - T, - ToolReturnType, - RunnableConfig & { toolCall?: ToolCall } - >, + func: RunnableFunc, fields: ToolWrapperParams ): DynamicStructuredTool; @@ -590,7 +582,7 @@ export function tool< func: RunnableFunc< T extends ZodObjectAny ? z.output : T, ToolReturnType, - RunnableConfig & { toolCall?: ToolCall } + ToolRunnableConfig >, fields: ToolWrapperParams ):