summaryrefslogtreecommitdiff
path: root/packages/core/src/tools/tools.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/core/src/tools/tools.ts')
-rw-r--r--packages/core/src/tools/tools.ts58
1 files changed, 58 insertions, 0 deletions
diff --git a/packages/core/src/tools/tools.ts b/packages/core/src/tools/tools.ts
index 68ea00af..deb54cf6 100644
--- a/packages/core/src/tools/tools.ts
+++ b/packages/core/src/tools/tools.ts
@@ -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,
+ },
+ };
+ }
+ }
}
/**