Merge pull request #6852 from xiaohuanxiong3/init-tool-policy-in-tools-definitions

Init tool policy in tools definitions
This commit is contained in:
Dallin Romney 2025-08-05 13:02:58 -07:00 • committed by GitHub
commit 315bc0dc8d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 49 additions and 34 deletions

6
core/index.d.ts vendored
View file

@ -1069,6 +1069,11 @@ export interface ToolExtras {
codeBaseIndexer?: CodebaseIndexer;
}
export type ToolPolicy =
| "allowedWithPermission"
| "allowedWithoutPermission"
| "disabled";
export interface Tool {
type: "function";
function: {
@ -1089,6 +1094,7 @@ export interface Tool {
group: string;
originalFunctionName?: string;
systemMessageDescription?: string;
defaultToolPolicy?: ToolPolicy;
}
interface ToolChoice {

View file

@ -27,6 +27,7 @@ export const codebaseTool: Tool = {
},
},
},
defaultToolPolicy: "allowedWithPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.CodebaseTool,
`To search the codebase, use the ${BuiltInToolNames.CodebaseTool} tool with a natural language query. For example, to find authentication logic, you might respond with:`,

View file

@ -31,6 +31,7 @@ export const createNewFileTool: Tool = {
},
},
},
defaultToolPolicy: "allowedWithPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.CreateNewFile,
`To create a NEW file, use the ${BuiltInToolNames.CreateNewFile} tool with the relative filepath and new contents. For example, to create a file located at 'path/to/file.txt', you would respond with:`,

View file

@ -60,6 +60,7 @@ export const createRuleBlock: Tool = {
},
},
},
defaultToolPolicy: "allowedWithPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.CreateRuleBlock,
`Sometimes the user will provide feedback or guidance on your output. If you were not aware of these "rules", consider using the ${BuiltInToolNames.CreateRuleBlock} tool to persist the rule for future interactions.

View file

@ -40,6 +40,7 @@ export const editFileTool: Tool = {
},
},
},
defaultToolPolicy: "allowedWithPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.EditExistingFile,
`To edit an EXISTING file, use the ${BuiltInToolNames.EditExistingFile} tool with

View file

@ -26,6 +26,7 @@ export const fetchUrlContentTool: Tool = {
},
},
},
defaultToolPolicy: "allowedWithPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.FetchUrlContent,
`To fetch the content of a URL, use the ${BuiltInToolNames.FetchUrlContent} tool. For example, to read the contents of a webpage, you might respond with:`,

View file

@ -26,6 +26,7 @@ export const globSearchTool: Tool = {
},
},
},
defaultToolPolicy: "allowedWithoutPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.FileGlobSearch,
`To return a list of files based on a glob search pattern, use the ${BuiltInToolNames.FileGlobSearch} tool`,

View file

@ -27,6 +27,7 @@ export const grepSearchTool: Tool = {
},
},
},
defaultToolPolicy: "allowedWithoutPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.GrepSearch,
`To perform a grep search within the project, call the ${BuiltInToolNames.GrepSearch} tool with the query pattern to match. For example:`,

View file

@ -31,6 +31,7 @@ export const lsTool: Tool = {
},
},
},
defaultToolPolicy: "allowedWithoutPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.LSTool,
`To list files and folders in a given directory, call the ${BuiltInToolNames.LSTool} tool with "dirPath" and "recursive". For example:`,

View file

@ -20,6 +20,7 @@ export const readCurrentlyOpenFileTool: Tool = {
properties: {},
},
},
defaultToolPolicy: "allowedWithPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.ReadCurrentlyOpenFile,
`To view the user's currently open file, use the ${BuiltInToolNames.ReadCurrentlyOpenFile} tool.

View file

@ -27,6 +27,7 @@ export const readFileTool: Tool = {
},
},
},
defaultToolPolicy: "allowedWithoutPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.ReadFile,
`To read a file with a known filepath, use the ${BuiltInToolNames.ReadFile} tool. For example, to read a file located at 'path/to/file.txt', you would respond with this:`,

View file

@ -60,6 +60,7 @@ export const requestRuleTool: GetTool = ({ rules }) => ({
},
},
},
defaultToolPolicy: "disabled",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.RequestRule,
getRequestRuleSystemMessageDescription(rules),

View file

@ -56,6 +56,7 @@ export const runTerminalCommandTool: Tool = {
},
},
},
defaultToolPolicy: "allowedWithPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.RunTerminalCommand,
`To run a terminal command, use the ${BuiltInToolNames.RunTerminalCommand} tool

View file

@ -120,4 +120,5 @@ ${SEARCH_AND_REPLACE_RULES}
],
],
),
defaultToolPolicy: "allowedWithPermission",
};

View file

@ -26,6 +26,7 @@ export const searchWebTool: Tool = {
},
},
},
defaultToolPolicy: "allowedWithoutPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.SearchWeb,
`To search the web, use the ${BuiltInToolNames.SearchWeb} tool with a natural language query. For example, to search for the current weather, you would respond with:`,

View file

@ -20,6 +20,7 @@ export const viewDiffTool: Tool = {
properties: {},
},
},
defaultToolPolicy: "allowedWithoutPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.ViewDiff,
`To view the current git diff, use the ${BuiltInToolNames.ViewDiff} tool. This will show you the changes made in the working directory compared to the last commit.`,

View file

@ -20,6 +20,7 @@ export const viewRepoMapTool: Tool = {
properties: {},
},
},
defaultToolPolicy: "allowedWithPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.ViewRepoMap,
`To view the repository map, use the ${BuiltInToolNames.ViewRepoMap} tool. This will provide a visual representation of the project's structure and organization.`,

View file

@ -26,6 +26,7 @@ export const viewSubdirectoryTool: Tool = {
},
},
},
defaultToolPolicy: "allowedWithPermission",
systemMessageDescription: createSystemMessageExampleCall(
BuiltInToolNames.ViewSubdirectory,
`To view a map of a specific folder within the project, you can use the ${BuiltInToolNames.ViewSubdirectory} tool. This will provide a visual representation of the folder's structure and organization.`,

View file

@ -287,6 +287,7 @@ describe("GUI Test", () => {
}).timeout(DEFAULT_TIMEOUT.MD);
it("should render tool call", async () => {
await GUIActions.toggleToolPolicy(view, "view_diff", 0);
const [messageInput] = await GUISelectors.getMessageInputFields(view);
await messageInput.sendKeys("Hello");
await messageInput.sendKeys(Key.ENTER);

View file

@ -10,7 +10,10 @@ import { generateToolCallButtonTestId } from "../../../components/mainInput/Lump
import { waitFor } from "@testing-library/dom";
import { act } from "@testing-library/react";
import { ChatMessage } from "core";
import { toggleToolSetting } from "../../../redux/slices/uiSlice";
import {
setToolPolicy,
toggleToolSetting,
} from "../../../redux/slices/uiSlice";
import {
getElementByTestId,
getElementByText,
@ -173,7 +176,12 @@ test("Edit run with no policy and yolo mode", { timeout: 15000 }, async () => {
addAndSelectMockLlm(store, ideMessenger);
// Enable automatic edit and yolo mode
store.dispatch(toggleToolSetting(BuiltInToolNames.EditExistingFile));
store.dispatch(
setToolPolicy({
toolName: BuiltInToolNames.EditExistingFile,
policy: "allowedWithoutPermission",
}),
);
triggerConfigUpdate({
store,
ideMessenger,

View file

@ -1,6 +1,7 @@
import { createSelector } from "@reduxjs/toolkit";
import { Tool } from "core";
import { BUILT_IN_GROUP_NAME } from "core/tools/builtIn";
import { DEFAULT_TOOL_SETTING } from "../slices/uiSlice";
import { RootState } from "../store";
export const selectActiveTools = createSelector(
@ -14,11 +15,15 @@ export const selectActiveTools = createSelector(
if (mode === "chat") {
return [];
} else {
const enabledTools = tools.filter(
(tool) =>
policies[tool.function.name] !== "disabled" &&
groupPolicies[tool.group] !== "exclude",
);
const enabledTools = tools.filter((tool) => {
const toolPolicy =
policies[tool.function.name] ??
tool.defaultToolPolicy ??
DEFAULT_TOOL_SETTING;
return (
toolPolicy !== "disabled" && groupPolicies[tool.group] !== "exclude"
);
});
if (mode === "plan") {
return enabledTools.filter(
(t) => t.group !== BUILT_IN_GROUP_NAME || t.readonly,

View file

@ -1,5 +1,5 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { RuleWithSource, Tool } from "core";
import { RuleWithSource, Tool, ToolPolicy } from "core";
import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "core/tools/builtIn";
import {
defaultOnboardingCardState,
@ -7,11 +7,6 @@ import {
} from "../../components/OnboardingCard";
import { getLocalStorage, LocalStorageKey } from "../../util/localStorage";
export type ToolPolicy =
| "allowedWithPermission"
| "allowedWithoutPermission"
| "disabled";
export type RulePolicy = "on" | "off";
export type ToolGroupPolicy = "include" | "exclude";
@ -50,21 +45,7 @@ export const uiSlice = createSlice({
),
shouldAddFileForEditing: false,
ttsActive: false,
toolSettings: {
[BuiltInToolNames.ReadFile]: "allowedWithoutPermission",
[BuiltInToolNames.EditExistingFile]: "allowedWithPermission",
[BuiltInToolNames.CreateNewFile]: "allowedWithPermission",
[BuiltInToolNames.RunTerminalCommand]: "allowedWithPermission",
[BuiltInToolNames.GrepSearch]: "allowedWithoutPermission",
[BuiltInToolNames.FileGlobSearch]: "allowedWithoutPermission",
[BuiltInToolNames.SearchWeb]: "allowedWithoutPermission",
[BuiltInToolNames.FetchUrlContent]: "allowedWithPermission",
[BuiltInToolNames.ViewDiff]: "allowedWithoutPermission",
[BuiltInToolNames.LSTool]: "allowedWithoutPermission",
[BuiltInToolNames.CreateRuleBlock]: "allowedWithPermission",
[BuiltInToolNames.RequestRule]: "disabled",
[BuiltInToolNames.SearchAndReplaceInFile]: "allowedWithPermission",
},
toolSettings: {},
toolGroupSettings: {
[BUILT_IN_GROUP_NAME]: "include",
},
@ -95,7 +76,7 @@ export const uiSlice = createSlice({
// Tools
addTool: (state, action: PayloadAction<Tool>) => {
state.toolSettings[action.payload.function.name] =
"allowedWithPermission";
action.payload.defaultToolPolicy ?? DEFAULT_TOOL_SETTING;
},
setToolPolicy: (
state,

View file

@ -1,9 +1,6 @@
import { ToolPolicy } from "core";
import { BuiltInToolNames } from "core/tools/builtIn";
import {
clearToolPolicy,
setToolPolicy,
ToolPolicy,
} from "../redux/slices/uiSlice";
import { clearToolPolicy, setToolPolicy } from "../redux/slices/uiSlice";
import { AppDispatch } from "../redux/store";
const validPolicyValues: ToolPolicy[] = [