mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-21 01:07:46 +00:00
bug(core): Do not throw validation errors when building tools in nonInteractiveToolExecutor. (#6363)
This commit is contained in:
@@ -200,6 +200,64 @@ export abstract class DeclarativeTool<
|
||||
const invocation = this.build(params);
|
||||
return invocation.execute(signal, updateOutput);
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to `build` but never throws.
|
||||
* @param params The raw, untrusted parameters from the model.
|
||||
* @returns A `ToolInvocation` instance.
|
||||
*/
|
||||
private silentBuild(
|
||||
params: TParams,
|
||||
): ToolInvocation<TParams, TResult> | Error {
|
||||
try {
|
||||
return this.build(params);
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
return e;
|
||||
}
|
||||
return new Error(String(e));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A convenience method that builds and executes the tool in one step.
|
||||
* Never throws.
|
||||
* @param params The raw, untrusted parameters from the model.
|
||||
* @params abortSignal a signal to abort.
|
||||
* @returns The result of the tool execution.
|
||||
*/
|
||||
async validateBuildAndExecute(
|
||||
params: TParams,
|
||||
abortSignal: AbortSignal,
|
||||
): Promise<ToolResult> {
|
||||
const invocationOrError = this.silentBuild(params);
|
||||
if (invocationOrError instanceof Error) {
|
||||
const errorMessage = invocationOrError.message;
|
||||
return {
|
||||
llmContent: `Error: Invalid parameters provided. Reason: ${errorMessage}`,
|
||||
returnDisplay: errorMessage,
|
||||
error: {
|
||||
message: errorMessage,
|
||||
type: ToolErrorType.INVALID_TOOL_PARAMS,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
return await invocationOrError.execute(abortSignal);
|
||||
} catch (error) {
|
||||
const errorMessage =
|
||||
error instanceof Error ? error.message : String(error);
|
||||
return {
|
||||
llmContent: `Error: Tool call execution failed. Reason: ${errorMessage}`,
|
||||
returnDisplay: errorMessage,
|
||||
error: {
|
||||
message: errorMessage,
|
||||
type: ToolErrorType.EXECUTION_FAILED,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user