feat: dynamic tool permissions

This commit is contained in:
Tomasz Stefaniak 2025-08-25 15:11:26 -07:00
parent 505c9d8b92
commit 9258d3de64
4 changed files with 48 additions and 1 deletions

View file

@ -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);
});

4
core/index.d.ts vendored
View file

@ -1099,6 +1099,10 @@ export interface Tool {
exampleArgs?: Array<[string, string | number]>;
};
defaultToolPolicy?: ToolPolicy;
evaluateToolCallPolicy?: (
basePolicy: ToolPolicy,
parsedArgs: Record<string, unknown>,
) => ToolPolicy;
}
interface ToolChoice {

View file

@ -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<string, unknown> },
{ policy: ToolPolicy },
];
"clipboardCache/add": [{ content: string }, void];
"controlPlane/openUrl": [{ path: string; orgSlug?: string }, void];
"controlPlane/getEnvironment": [undefined, ControlPlaneEnv];

View file

@ -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<string, unknown>): 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}