Skip to content

Your lightweight open-source personal agent.

fix(webui): retry mention catalogs after transient failures

chengyongru0856fbf01487
diff --git a/webui/src/components/thread/ThreadShell.tsx b/webui/src/components/thread/ThreadShell.tsx
index a55c28cec..ea9cb9e99 100644
--- a/webui/src/components/thread/ThreadShell.tsx
+++ b/webui/src/components/thread/ThreadShell.tsx
@@ -541,7 +541,7 @@ interface PendingFirstMessage {
}
interface InstalledSettingItemsOptions<Payload, Item> {
- enabled: boolean;
+ requestCount: number;
getToken: () => string;
eventName: string;
fetchPayload: (token: string) => Promise<Payload>;
@@ -550,7 +550,7 @@ interface InstalledSettingItemsOptions<Payload, Item> {
}
function useInstalledSettingItems<Payload, Item>({
- enabled,
+ requestCount,
getToken,
eventName,
fetchPayload,
@@ -558,6 +558,8 @@ function useInstalledSettingItems<Payload, Item>({
selectItems,
}: InstalledSettingItemsOptions<Payload, Item>): Item[] {
const [items, setItems] = useState<Item[]>([]);
+ const loadedRef = useRef(false);
+ const pendingRef = useRef<Promise<Payload> | null>(null);
useEffect(() => {
let cancelled = false;
@@ -569,14 +571,18 @@ function useInstalledSettingItems<Payload, Item>({
if (refreshing) return;
refreshing = true;
const version = payloadVersion;
+ const pending = pendingRef.current ?? fetchPayload(getToken());
+ pendingRef.current = pending;
try {
- const payload = await fetchPayload(getToken());
+ const payload = await pending;
if (!cancelled && version === payloadVersion) {
+ loadedRef.current = true;
setItems(selectItems(payload));
}
} catch {
// Keep the last successful catalog during transient refresh failures.
} finally {
+ if (pendingRef.current === pending) pendingRef.current = null;
refreshing = false;
if (refreshAfterFlight && !cancelled) {
refreshAfterFlight = false;
@@ -585,19 +591,20 @@ function useInstalledSettingItems<Payload, Item>({
}
};
const queueRefresh = () => {
- if (!enabled || document.visibilityState === "hidden" || refreshQueued) return;
+ if (!requestCount || document.visibilityState === "hidden" || refreshQueued) return;
refreshQueued = true;
queueMicrotask(() => {
refreshQueued = false;
if (!cancelled) void refresh();
});
};
- if (enabled) void refresh();
+ if (requestCount && !loadedRef.current) void refresh();
const refreshOnChanged = (event: Event) => {
const payload = (event as CustomEvent<unknown>).detail;
if (isPayload(payload)) {
payloadVersion += 1;
+ loadedRef.current = true;
setItems(selectItems(payload));
return;
}
@@ -613,7 +620,7 @@ function useInstalledSettingItems<Payload, Item>({
cancelled = true;
window.removeEventListener(eventName, refreshOnChanged);
};
- }, [enabled, eventName, fetchPayload, getToken, isPayload, selectItems]);
+ }, [requestCount, eventName, fetchPayload, getToken, isPayload, selectItems]);
return items;
}
@@ -696,10 +703,10 @@ export function ThreadShell({
}, [client]);
const [booting, setBooting] = useState(false);
const [slashCommands, setSlashCommands] = useState<SlashCommand[]>([]);
- const [mentionCatalogsRequested, setMentionCatalogsRequested] = useState(false);
- const requestMentionCatalogs = useCallback(() => setMentionCatalogsRequested(true), []);
+ const [mentionCatalogRequestCount, setMentionCatalogRequestCount] = useState(0);
+ const requestMentionCatalogs = useCallback(() => setMentionCatalogRequestCount((count) => count + 1), []);
const cliApps = useInstalledSettingItems({
- enabled: mentionCatalogsRequested,
+ requestCount: mentionCatalogRequestCount,
getToken,
eventName: CLI_APPS_CHANGED_EVENT,
fetchPayload: fetchInstalledCliApps,
@@ -707,7 +714,7 @@ export function ThreadShell({
selectItems: installedCliAppsFromPayload,
});
const mcpPresets = useInstalledSettingItems({
- enabled: mentionCatalogsRequested,
+ requestCount: mentionCatalogRequestCount,
getToken,
eventName: MCP_PRESETS_CHANGED_EVENT,
fetchPayload: fetchMcpPresets,
@@ -901,11 +908,10 @@ export function ThreadShell({
}, []);
const displayMessages = useMemo(() => projectWebuiThreadMessages(messages), [messages]);
+ const hasAppMentions = displayMessages.some((message) => message.cliApps?.length || message.mcpPresets?.length);
useEffect(() => {
- if (displayMessages.some((message) => message.cliApps?.length || message.mcpPresets?.length)) {
- requestMentionCatalogs();
- }
- }, [displayMessages, requestMentionCatalogs]);
+ if (hasAppMentions) requestMentionCatalogs();
+ }, [hasAppMentions, requestMentionCatalogs]);
const composerContextUsage = useMemo(
() => latestComposerContextUsage(displayMessages),
[displayMessages],
diff --git a/webui/src/tests/thread-shell.test.tsx b/webui/src/tests/thread-shell.test.tsx
index 919e37a25..6fe413461 100644
--- a/webui/src/tests/thread-shell.test.tsx
+++ b/webui/src/tests/thread-shell.test.tsx
@@ -4476,6 +4476,12 @@ describe("ThreadShell", () => {
});
it("loads mention catalogs only on demand and ignores window focus", async () => {
+ const originalFetch = vi.mocked(fetch).getMockImplementation()!;
+ vi.mocked(fetch).mockImplementation((input, init) => {
+ if (String(input).includes("cli-apps")) return Promise.resolve(httpJson({ apps: [], installed_count: 0 }));
+ if (String(input).includes("mcp-presets")) return Promise.resolve(httpJson({ presets: [], installed_count: 0 }));
+ return originalFetch(input, init);
+ });
render(wrap(makeClient(), <ThreadShell
session={session("lazy-mentions")}
title="Lazy mentions"
@@ -4498,6 +4504,29 @@ describe("ThreadShell", () => {
expect(catalogCalls()).toHaveLength(2);
});
+ it("retries a failed mention catalog when the user opens mentions again", async () => {
+ vi.mocked(fetch).mockResolvedValue({ ok: false, status: 503, json: async () => ({}) } as Response);
+ render(wrap(makeClient(), <ThreadShell
+ session={session("retry-mentions")}
+ title="Retry mentions"
+ onToggleSidebar={() => {}}
+ onGoHome={() => {}}
+ onNewChat={() => {}}
+ />));
+ const input = await screen.findByLabelText("Message input");
+ const catalogCalls = () => vi.mocked(fetch).mock.calls.filter(([url]) =>
+ /cli-apps|mcp-presets/.test(String(url)));
+ await act(async () => {
+ fireEvent.change(input, { target: { value: "@", selectionStart: 1 } });
+ });
+ expect(catalogCalls()).toHaveLength(2);
+ fireEvent.change(input, { target: { value: "", selectionStart: 0 } });
+ await act(async () => {
+ fireEvent.change(input, { target: { value: "@", selectionStart: 1 } });
+ });
+ expect(catalogCalls()).toHaveLength(4);
+ });
+
it("updates @ CLI app suggestions when settings broadcasts an install", async () => {
const client = makeClient();
render(wrap(