From fb44d5bad66d5da11eb373b9ed0c46cf388168be Mon Sep 17 00:00:00 2001 From: QiuYang Shen <1741258352@qq.com> Date: Fri, 1 Aug 2025 17:22:39 +0800 Subject: [PATCH] feat: remove the initialization of tool policy with useEffect, use the defaultToolPolicy as a fallback if a value has not been set by the user in selectActiveTools.ts --- gui/src/pages/gui/Chat.tsx | 18 ------------------ gui/src/redux/selectors/selectActiveTools.ts | 15 ++++++++++----- gui/src/redux/slices/uiSlice.ts | 8 -------- 3 files changed, 10 insertions(+), 31 deletions(-) diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index d7e359cfc..7b05087d8 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -48,7 +48,6 @@ import { cancelStream } from "../../redux/thunks/cancelStream"; import { EmptyChatBody } from "./EmptyChatBody"; import { ExploreDialogWatcher } from "./ExploreDialogWatcher"; import { useAutoScroll } from "./useAutoScroll"; -import { initTool } from "../../redux/slices/uiSlice"; // Helper function to find the index of the latest conversation summary function findLatestSummaryIndex(history: ChatHistoryItem[]): number { @@ -126,23 +125,6 @@ export function Chat() { return isJetBrains(); }, []); - const availableTools = useAppSelector((state) => state.config.config.tools); - // Mark if initialized - const [toolSettingsInitialized, setToolSettingsInitialized] = useState(false); - - useEffect(() => { - if ( - !toolSettingsInitialized && - Array.isArray(availableTools) && - availableTools.length > 0 - ) { - for (const tool of availableTools) { - dispatch(initTool(tool)); - } - setToolSettingsInitialized(true); - } - }, [availableTools, toolSettingsInitialized, dispatch]); - useAutoScroll(stepsDivRef, history); useEffect(() => { diff --git a/gui/src/redux/selectors/selectActiveTools.ts b/gui/src/redux/selectors/selectActiveTools.ts index 2e90bbfd5..b28a3d903 100644 --- a/gui/src/redux/selectors/selectActiveTools.ts +++ b/gui/src/redux/selectors/selectActiveTools.ts @@ -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, diff --git a/gui/src/redux/slices/uiSlice.ts b/gui/src/redux/slices/uiSlice.ts index 65b4eb7d6..e350d9f72 100644 --- a/gui/src/redux/slices/uiSlice.ts +++ b/gui/src/redux/slices/uiSlice.ts @@ -73,13 +73,6 @@ export const uiSlice = createSlice({ ) => { state.isExploreDialogOpen = action.payload; }, - // Initialize when there is no toolSetting data in the cache - initTool: (state, action: PayloadAction) => { - if (!state.toolSettings[action.payload.function.name]) { - state.toolSettings[action.payload.function.name] = - action.payload.defaultToolPolicy ?? DEFAULT_TOOL_SETTING; - } - }, // Tools addTool: (state, action: PayloadAction) => { state.toolSettings[action.payload.function.name] = @@ -159,7 +152,6 @@ export const { clearToolPolicy, toggleToolGroupSetting, addTool, - initTool, addRule, toggleRuleSetting, setTTSActive,