From e251289d06cdfcac7e5b501849f92fc277ce2d25 Mon Sep 17 00:00:00 2001 From: QiuYang Shen <1741258352@qq.com> Date: Thu, 7 Aug 2025 02:21:01 +0800 Subject: [PATCH] fix: fix tool execute abnormal when there is no tool policy data in the local cache --- extensions/vscode/e2e/tests/GUI.test.ts | 1 - gui/src/redux/thunks/callToolById.ts | 9 ++++--- gui/src/redux/thunks/streamNormalInput.ts | 33 +++++++++++++++-------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/extensions/vscode/e2e/tests/GUI.test.ts b/extensions/vscode/e2e/tests/GUI.test.ts index 421e17772..589e37ff4 100644 --- a/extensions/vscode/e2e/tests/GUI.test.ts +++ b/extensions/vscode/e2e/tests/GUI.test.ts @@ -287,7 +287,6 @@ 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); diff --git a/gui/src/redux/thunks/callToolById.ts b/gui/src/redux/thunks/callToolById.ts index 468838ec6..32898f30e 100644 --- a/gui/src/redux/thunks/callToolById.ts +++ b/gui/src/redux/thunks/callToolById.ts @@ -14,6 +14,7 @@ import { import { ThunkApiType } from "../store"; import { findToolCallById, logToolUsage } from "../util"; import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall"; +import { DEFAULT_TOOL_SETTING } from "../slices/uiSlice"; export const callToolById = createAsyncThunk< void, @@ -36,9 +37,11 @@ export const callToolById = createAsyncThunk< // Check if this is an auto-approved tool const toolSettings = state.ui.toolSettings; - const isAutoApproved = - toolSettings[toolCallState.toolCall.function.name] === - "allowedWithoutPermission"; + const toolPolicy = + toolSettings[toolCallState.toolCall.function.name] ?? + toolCallState.tool?.defaultToolPolicy ?? + DEFAULT_TOOL_SETTING; + const isAutoApproved = toolPolicy === "allowedWithoutPermission"; posthog.capture("gui_tool_call_decision", { decision: isAutoApproved ? "auto_accept" : "accept", diff --git a/gui/src/redux/thunks/streamNormalInput.ts b/gui/src/redux/thunks/streamNormalInput.ts index 6e3939a5b..769f4e82e 100644 --- a/gui/src/redux/thunks/streamNormalInput.ts +++ b/gui/src/redux/thunks/streamNormalInput.ts @@ -28,6 +28,7 @@ import { interceptSystemToolCalls } from "core/tools/systemMessageTools/intercep import { selectCurrentToolCalls } from "../selectors/selectToolCalls"; import { getBaseSystemMessage } from "../util/getBaseSystemMessage"; import { callToolById } from "./callToolById"; +import { DEFAULT_TOOL_SETTING } from "../slices/uiSlice"; /** * Handles the execution of tool calls that may be automatically accepted. * Sets all tools as generated first, then executes auto-approved tool calls. @@ -35,6 +36,7 @@ import { callToolById } from "./callToolById"; async function handleToolCallExecution( dispatch: AppThunkDispatch, getState: () => RootState, + activeTools: Tool[], ): Promise { const newState = getState(); const toolSettings = newState.ui.toolSettings; @@ -51,11 +53,15 @@ async function handleToolCallExecution( } // Check if ALL tool calls are auto-approved - if not, wait for user approval - const allAutoApproved = toolCallStates.every( - (toolCallState) => - toolSettings[toolCallState.toolCall.function.name] === - "allowedWithoutPermission", - ); + const allAutoApproved = toolCallStates.every((toolCallState) => { + const toolPolicy = + toolSettings[toolCallState.toolCall.function.name] ?? + activeTools.find( + (tool) => tool.function.name === toolCallState.toolCall.function.name, + )?.defaultToolPolicy ?? + DEFAULT_TOOL_SETTING; + return toolPolicy == "allowedWithoutPermission"; + }); // Set all tools as generated first toolCallStates.forEach((toolCallState) => { @@ -289,11 +295,16 @@ export const streamNormalInput = createAsyncThunk< // Check if ALL generating tool calls are auto-approved const allAutoApproved = generatingToolCalls.length > 0 && - generatingToolCalls.every( - (toolCallState) => - toolSettings[toolCallState.toolCall.function.name] === - "allowedWithoutPermission", - ); + generatingToolCalls.every((toolCallState) => { + const toolPolicy = + toolSettings[toolCallState.toolCall.function.name] ?? + activeTools.find( + (tool) => + tool.function.name === toolCallState.toolCall.function.name, + )?.defaultToolPolicy ?? + DEFAULT_TOOL_SETTING; + return toolPolicy == "allowedWithoutPermission"; + }); // Only set inactive if: // 1. There are no tool calls, OR @@ -303,6 +314,6 @@ export const streamNormalInput = createAsyncThunk< dispatch(setInactive()); } - await handleToolCallExecution(dispatch, getState); + await handleToolCallExecution(dispatch, getState, activeTools); }, );