fix: fix tool execute abnormal when there is no tool policy data in the local cache

This commit is contained in:
QiuYang Shen 2025-08-07 02:21:01 +08:00
parent 55027295bb
commit e251289d06
3 changed files with 28 additions and 15 deletions

View file

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

View file

@ -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",

View file

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