fix(webui): cache tool catalogs across settings navigation (#6552)

* fix(webui): cache tool catalogs across settings navigation

* refactor(webui): remove unreachable catalog invalidation condition
This commit is contained in:
chengyongru authored and GitHub committed 2026-11-04 00:30:33 +08:00
1 parent 62c2ed4e7e
commit 8632940a99
15 files changed
+424 -31

No files matched your search

@@ -13,6 +13,7 @@ import { IdleCompactionControl } from "@/components/settings/system/IdleCompacti
import { Input } from "@/components/ui/input";
import { RUNTIME_CONFIG_FIELDS, RUNTIME_CONFIG_GROUPS, type RuntimeConfigField, type RuntimeConfigPage } from "@/components/settings/system/runtime-config-fields";
import { updateRuntimeConfigSettings } from "@/lib/api";
import { invalidateToolsCatalog } from "@/lib/tools-catalog-cache";
import type { NanobotClient } from "@/lib/nanobot-client";
import type { RuntimeConfigValue, SettingsPayload } from "@/lib/types";
import type { ApplySettingsPayload } from "@/components/settings/contracts";
@@ -111,6 +112,9 @@ export function useRuntimeConfigSettings(
setErrors((prev) => ({ ...prev, [group]: "" }));
try {
const payload = await updateRuntimeConfigSettings(client, values);
if (Object.keys(values).some(path => path.startsWith("tools.cli_apps.") || path === "network.proxy")) {
invalidateToolsCatalog(client);
}
applyPayload(payload, { preserveAgentForm: true, preserveCapabilityForms: true });
discard(group);
setSaved((prev) => ({ ...prev, [group]: true }));
@@ -151,6 +155,7 @@ export function useRuntimeConfigSettings(
setSaving("proxy");
try {
const payload = await updateRuntimeConfigSettings(client, { "network.proxy": next.trim() || null });
invalidateToolsCatalog(client);
applyPayload(payload, { preserveAgentForm: true, preserveCapabilityForms: true });
discard("proxy");
setSaved((prev) => ({ ...prev, proxy: true }));
@@ -414,6 +414,7 @@ export function createSystemSettingsActions({
return;
}
state.toolsCatalog.invalidate();
if (flow.status === "authorized" && flow.hot_reload) {
if (flow.hot_reload.requires_restart) {
setPendingRestartSections((prev) => ({ ...prev, runtime: true }));
@@ -487,7 +488,8 @@ export function createSystemSettingsActions({
setMcpOAuthCompleting(false);
setMcpOAuthCallbackError(null);
try {
const flow = await startMcpOAuth(client, name, reset, values);
const flow = await startMcpOAuth(client, name, reset, values)
.finally(() => state.toolsCatalog.invalidate());
mcpOAuthFlowRef.current = flow;
setMcpOAuthFlow(flow);
navigateMcpOAuthPopup(flow);
@@ -9,8 +9,6 @@ import type { SystemSettingsState } from "@/components/settings/system/useSystem
import {
fetchApiService,
fetchAutomations,
fetchCliApps,
fetchMcpPresets,
fetchNanobotFeatures,
} from "@/lib/api";
@@ -36,17 +34,25 @@ export function useSystemSettingsEffects({
setAutomations,
setAutomationsError,
setAutomationsLoading,
setCliApps,
toolsCatalog,
toolsCatalogInvalidation,
setCliAppsError,
setCliAppsLoading,
setMcpError,
setMcpPresets,
setMcpPresetsLoading,
setNanobotFeatures,
setNanobotFeaturesError,
setNanobotFeaturesLoading,
} = state;
useEffect(() => {
setCliAppsError(null);
setMcpError(null);
}, [toolsCatalog]);
const cliPending = Boolean(state.cliApps?.catalog_refresh_pending);
const mcpConnecting = Boolean(state.mcpPresets?.presets.some(preset => preset.runtime_status === "connecting"));
useEffect(() => {
if (activeSection !== "apps") return;
let cancelled = false;
@@ -54,17 +60,16 @@ export function useSystemSettingsEffects({
let retryCount = 0;
const loadCliApps = (showLoading: boolean) => {
if (showLoading) setCliAppsLoading(true);
fetchCliApps(getToken())
.then((payload) => {
toolsCatalog.loadCliApps()
.then(() => {
if (cancelled) return;
if (payload.catalog_refresh_pending && retryCount < CLI_APPS_REFRESH_MAX_RETRIES) {
if (toolsCatalog.getSnapshot().cliApps?.catalog_refresh_pending && retryCount < CLI_APPS_REFRESH_MAX_RETRIES) {
retryCount += 1;
retry = window.setTimeout(() => {
retry = null;
loadCliApps(false);
}, CLI_APPS_REFRESH_RETRY_MS);
}
setCliApps(payload);
setCliAppsError(null);
setCliAppsLoading(false);
})
@@ -75,12 +80,15 @@ export function useSystemSettingsEffects({
}
});
};
loadCliApps(true);
const cached = toolsCatalog.getSnapshot().cliApps;
setCliAppsLoading(!cached);
if (!toolsCatalog.cliFresh()) loadCliApps(!cached);
else if (cliPending) retry = window.setTimeout(() => loadCliApps(false), CLI_APPS_REFRESH_RETRY_MS);
return () => {
cancelled = true;
if (retry !== null) window.clearTimeout(retry);
};
}, [activeSection, getToken]);
}, [activeSection, toolsCatalog, toolsCatalogInvalidation, cliPending]);
useEffect(() => {
if (
@@ -156,12 +164,11 @@ export function useSystemSettingsEffects({
let retry: number | null = null;
const loadMcpPresets = (showLoading: boolean) => {
if (showLoading) setMcpPresetsLoading(true);
fetchMcpPresets(getToken())
.then((payload) => {
toolsCatalog.loadMcpPresets()
.then(() => {
if (cancelled) return;
setMcpPresets(payload);
setMcpError(null);
if (payload.presets.some((preset) => preset.runtime_status === "connecting")) {
if (toolsCatalog.getSnapshot().mcpPresets?.presets.some((preset) => preset.runtime_status === "connecting")) {
retry = window.setTimeout(() => {
retry = null;
loadMcpPresets(false);
@@ -175,12 +182,15 @@ export function useSystemSettingsEffects({
if (!cancelled && showLoading) setMcpPresetsLoading(false);
});
};
loadMcpPresets(true);
const cached = toolsCatalog.getSnapshot().mcpPresets;
setMcpPresetsLoading(!cached);
if (!toolsCatalog.mcpFresh()) loadMcpPresets(!cached);
else if (mcpConnecting) retry = window.setTimeout(() => loadMcpPresets(false), MCP_RUNTIME_STATUS_REFRESH_MS);
return () => {
cancelled = true;
if (retry !== null) window.clearTimeout(retry);
};
}, [activeSection, getToken, pageVisible]);
}, [activeSection, toolsCatalog, toolsCatalogInvalidation, pageVisible, mcpConnecting]);
const refreshAutomations = useCallback(
async (showLoading = false) => {
@@ -1,4 +1,6 @@
import { useRef, useState } from "react";
import { useRef, useState, useSyncExternalStore } from "react";
import type { ToolsCatalog } from "@/lib/tools-catalog-cache";
import type { AutomationFilter } from "@/components/settings/system/AutomationsSettings";
import {
@@ -9,9 +11,7 @@ import {
import type {
ApiServicePayload,
AutomationsPayload,
CliAppsPayload,
McpOAuthFlowPayload,
McpPresetsPayload,
NanobotFeatureInfo,
NanobotFeaturesPayload,
SessionAutomationJob,
@@ -22,10 +22,10 @@ export type NanobotFeatureInstallRequest = {
installOnly: boolean;
};
export function useSystemSettingsState() {
const [cliApps, setCliApps] = useState<CliAppsPayload | null>(null);
export function useSystemSettingsState(toolsCatalog: ToolsCatalog) {
const { cliApps, mcpPresets, invalidation: toolsCatalogInvalidation } = useSyncExternalStore(toolsCatalog.subscribe, toolsCatalog.getSnapshot);
const { setCliApps, setMcpPresets } = toolsCatalog;
const [nanobotFeatures, setNanobotFeatures] = useState<NanobotFeaturesPayload | null>(null);
const [mcpPresets, setMcpPresets] = useState<McpPresetsPayload | null>(null);
const [automations, setAutomations] = useState<AutomationsPayload | null>(null);
const [cliAppsLoading, setCliAppsLoading] = useState(true);
const [nanobotFeaturesLoading, setNanobotFeaturesLoading] = useState(true);
@@ -69,6 +69,8 @@ export function useSystemSettingsState() {
const [mcpConfigImport, setMcpConfigImport] = useState("");
return {
toolsCatalog,
toolsCatalogInvalidation,
apiService,
apiServiceAction,
apiServiceError,
@@ -33,6 +33,7 @@ import { useRuntimeConfigSettings } from "@/components/settings/system/RuntimeCo
import { createSystemSettingsActions } from "@/components/settings/system/createSystemSettingsActions";
import { useSystemSettingsEffects } from "@/components/settings/system/useSystemSettingsEffects";
import { useSystemSettingsState } from "@/components/settings/system/useSystemSettingsState";
import { getToolsCatalog } from "@/lib/tools-catalog-cache";
import { usePageVisibility } from "@/hooks/usePageVisibility";
import { useEditCompletion } from "@/components/settings/shared/useEditCompletion";
import { cancelProviderOAuth, fetchSettings, fetchSettingsUsage } from "@/lib/api";
@@ -113,7 +114,7 @@ export function useSettingsController({
} = capabilityState;
const draftsRef = useRef({ form, imageGenerationForm, networkSafetyForm, transcriptionForm, webSearchForm });
draftsRef.current = { form, imageGenerationForm, networkSafetyForm, transcriptionForm, webSearchForm };
const systemState = useSystemSettingsState();
const systemState = useSystemSettingsState(getToolsCatalog(client, token));
const featureCatalog = systemState.nanobotFeatures?.features ?? [];
useEffect(() => {