diff --git a/web/frontend/src/components/agent/tools/tools-page.tsx b/web/frontend/src/components/agent/tools/tools-page.tsx index 634dd1b7f..927a5645e 100644 --- a/web/frontend/src/components/agent/tools/tools-page.tsx +++ b/web/frontend/src/components/agent/tools/tools-page.tsx @@ -1,15 +1,15 @@ import { IconSearch } from "@tabler/icons-react" import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" -import { useEffect, useMemo, useState } from "react" +import { useMemo, useState } from "react" import { useTranslation } from "react-i18next" import { toast } from "sonner" import { + type ToolSupportItem, + type WebSearchConfigResponse, getTools, getWebSearchConfig, setToolEnabled, - type ToolSupportItem, - type WebSearchConfigResponse, updateWebSearchConfig, } from "@/api/tools" import { PageHeader } from "@/components/page-header" @@ -54,14 +54,9 @@ export function ToolsPage() { const [searchQuery, setSearchQuery] = useState("") const [statusFilter, setStatusFilter] = useState("all") - const [webSearchDraft, setWebSearchDraft] = + const [webSearchDraftOverride, setWebSearchDraftOverride] = useState(null) - - useEffect(() => { - if (webSearchData) { - setWebSearchDraft(webSearchData) - } - }, [webSearchData]) + const webSearchDraft = webSearchDraftOverride ?? webSearchData ?? null const toggleMutation = useMutation({ mutationFn: async ({ name, enabled }: { name: string; enabled: boolean }) => @@ -87,9 +82,12 @@ export function ToolsPage() { const webSearchMutation = useMutation({ mutationFn: updateWebSearchConfig, onSuccess: (updated) => { - setWebSearchDraft(updated) + queryClient.setQueryData(["tools", "web-search-config"], updated) + setWebSearchDraftOverride(null) toast.success(t("pages.agent.tools.web_search.save_success")) - void queryClient.invalidateQueries({ queryKey: ["tools", "web-search-config"] }) + void queryClient.invalidateQueries({ + queryKey: ["tools", "web-search-config"], + }) void queryClient.invalidateQueries({ queryKey: ["tools"] }) void refreshGatewayState({ force: true }) }, @@ -148,7 +146,10 @@ export function ToolsPage() { const updateDraft = ( updater: (current: WebSearchConfigResponse) => WebSearchConfigResponse, ) => { - setWebSearchDraft((current) => (current ? updater(current) : current)) + setWebSearchDraftOverride((current) => { + const draft = current ?? webSearchData + return draft ? updater(draft) : current + }) } return ( @@ -161,7 +162,9 @@ export function ToolsPage() { {t("pages.agent.tools.web_search.title")} - {t("pages.agent.tools.web_search.load_error")} + + {t("pages.agent.tools.web_search.load_error")} + ) : isWebSearchLoading || !webSearchDraft ? ( @@ -201,7 +204,10 @@ export function ToolsPage() { - updateDraft((current) => ({ - ...current, - settings: { - ...current.settings, - [providerId]: { - ...current.settings[providerId], - max_results: Number(e.target.value) || 0, - }, - }, - })) - } - /> - - {(providerId === "tavily" || - providerId === "searxng" || - providerId === "glm_search" || - providerId === "baidu_search") && ( + +
- {t("pages.agent.tools.web_search.base_url")} + {t("pages.agent.tools.web_search.max_results")}
updateDraft((current) => ({ ...current, @@ -329,46 +320,74 @@ export function ToolsPage() { ...current.settings, [providerId]: { ...current.settings[providerId], - base_url: e.target.value, + max_results: + Number(e.target.value) || 0, }, }, })) } - placeholder={t("pages.agent.tools.web_search.base_url_placeholder")} />
- )} - {(providerId === "brave" || - providerId === "tavily" || - providerId === "perplexity" || - providerId === "glm_search" || - providerId === "baidu_search") && ( -
-
- {t("pages.agent.tools.web_search.api_key")} + {(providerId === "tavily" || + providerId === "searxng" || + providerId === "glm_search" || + providerId === "baidu_search") && ( +
+
+ {t("pages.agent.tools.web_search.base_url")} +
+ + updateDraft((current) => ({ + ...current, + settings: { + ...current.settings, + [providerId]: { + ...current.settings[providerId], + base_url: e.target.value, + }, + }, + })) + } + placeholder={t( + "pages.agent.tools.web_search.base_url_placeholder", + )} + />
- - updateDraft((current) => ({ - ...current, - settings: { - ...current.settings, - [providerId]: { - ...current.settings[providerId], - api_key: value, + )} + {(providerId === "brave" || + providerId === "tavily" || + providerId === "perplexity" || + providerId === "glm_search" || + providerId === "baidu_search") && ( +
+
+ {t("pages.agent.tools.web_search.api_key")} +
+ + updateDraft((current) => ({ + ...current, + settings: { + ...current.settings, + [providerId]: { + ...current.settings[providerId], + api_key: value, + }, }, - }, - })) - } - placeholder={apiKeyPlaceholder} - /> -
- )} - - - ) - })} + })) + } + placeholder={apiKeyPlaceholder} + /> +
+ )} + + + ) + }, + )}