diff --git a/core/core.ts b/core/core.ts index 760466eb2..464015327 100644 --- a/core/core.ts +++ b/core/core.ts @@ -1003,6 +1003,28 @@ export class Core { this.handleToolCall(toolCall), ); + on("tools/evaluatePolicy", async ({ data: { toolName, basePolicy, args } }) => { + const { config } = await this.configHandler.loadConfig(); + if (!config) { + throw new Error("Config not loaded"); + } + + const tool = config.tools.find((t) => t.function.name === toolName); + if (!tool) { + // Tool not found, return base policy + return { policy: basePolicy }; + } + + // If tool has evaluateToolCallPolicy function, use it + if (tool.evaluateToolCallPolicy) { + const evaluatedPolicy = tool.evaluateToolCallPolicy(basePolicy, args); + return { policy: evaluatedPolicy }; + } + + // Otherwise return base policy unchanged + return { policy: basePolicy }; + }); + on("isItemTooBig", async ({ data: { item } }) => { return this.isItemTooBig(item); }); diff --git a/core/index.d.ts b/core/index.d.ts index 90039f025..3988a651c 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -1099,6 +1099,10 @@ export interface Tool { exampleArgs?: Array<[string, string | number]>; }; defaultToolPolicy?: ToolPolicy; + evaluateToolCallPolicy?: ( + basePolicy: ToolPolicy, + parsedArgs: Record, + ) => ToolPolicy; } interface ToolChoice { diff --git a/core/protocol/core.ts b/core/protocol/core.ts index 8fb4a9a7e..d63b6f2e7 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -39,6 +39,7 @@ import { SlashCommandDescWithSource, StreamDiffLinesPayload, ToolCall, + ToolPolicy, } from "../"; import { AutocompleteCodeSnippet } from "../autocomplete/snippets/types"; import { GetLspDefinitionsFunction } from "../autocomplete/types"; @@ -292,6 +293,10 @@ export type ToCoreFromIdeOrWebviewProtocol = { { toolCall: ToolCall }, { contextItems: ContextItem[]; errorMessage?: string }, ]; + "tools/evaluatePolicy": [ + { toolName: string; basePolicy: ToolPolicy; args: Record }, + { policy: ToolPolicy }, + ]; "clipboardCache/add": [{ content: string }, void]; "controlPlane/openUrl": [{ path: string; orgSlug?: string }, void]; "controlPlane/getEnvironment": [undefined, ControlPlaneEnv]; diff --git a/core/tools/definitions/runTerminalCommand.ts b/core/tools/definitions/runTerminalCommand.ts index b2dc74316..565eec1c1 100644 --- a/core/tools/definitions/runTerminalCommand.ts +++ b/core/tools/definitions/runTerminalCommand.ts @@ -1,5 +1,5 @@ import os from "os"; -import { Tool } from "../.."; +import { Tool, ToolPolicy } from "../.."; import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; /** @@ -56,6 +56,22 @@ export const runTerminalCommandTool: Tool = { }, }, defaultToolPolicy: "allowedWithPermission", + evaluateToolCallPolicy: (basePolicy: ToolPolicy, parsedArgs: Record): ToolPolicy => { + // If tool is disabled, keep it disabled + if (basePolicy === "disabled") { + return "disabled"; + } + + const command = parsedArgs.command as string; + + // Always require permission for echo commands + if (command && command.trim().toLowerCase().startsWith("echo")) { + return "allowedWithPermission"; + } + + // For all other commands, use the base policy + return basePolicy; + }, systemMessageDescription: { prefix: `To run a terminal command, use the ${BuiltInToolNames.RunTerminalCommand} tool ${RUN_COMMAND_NOTES}