This is an automated email from the ASF dual-hosted git repository.

lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new a42427838 fix(ai): report a failed LLM runtime load on the page (#6827)
a42427838 is described below

commit a42427838da2ff91560e75d25389ef1242b066aa
Author: Apulupie <[email protected]>
AuthorDate: Fri Nov 27 12:43:03 2026 +0800

    fix(ai): report a failed LLM runtime load on the page (#6827)
---
 web/src/pages/ai/__tests__/AiPage.test.tsx   | 21 +++++++++++++++++++++
 web/src/pages/ai/hooks/useLlmRuntime.test.ts | 23 +++++++++++++++++++++++
 web/src/pages/ai/hooks/useLlmRuntime.ts      | 19 +++++++++++++++++--
 web/src/pages/ai/index.tsx                   | 13 +++++++++++++
 4 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/web/src/pages/ai/__tests__/AiPage.test.tsx 
b/web/src/pages/ai/__tests__/AiPage.test.tsx
index 0bd830861..605ecf754 100644
--- a/web/src/pages/ai/__tests__/AiPage.test.tsx
+++ b/web/src/pages/ai/__tests__/AiPage.test.tsx
@@ -910,6 +910,27 @@ describe('AiPage', () => {
     expect(screen.getByTestId('ai-send-stop-button')).toBeDisabled();
   });
 
+  it.each([new Error('runtime unavailable'), {}])(
+    'offers recovery for a failed LLM runtime load: %s',
+    async (failure) => {
+      vi.mocked(getLlmConfig).mockRejectedValueOnce(failure);
+      renderPage();
+
+      const alert = await screen.findByRole('alert');
+      expect(alert).toHaveTextContent('AI 配置加载失败');
+      // The runtime never loaded, so nothing can be sent. The failure used to 
be a transient toast
+      // only, which left the composer looking like a page whose provider was 
never configured —
+      // a dead send button with nothing on the page to act on.
+      expect(screen.getByTestId('ai-send-stop-button')).toBeDisabled();
+
+      await userEvent.setup().click(within(alert).getByRole('button', { name: 
/重\s*试/ }));
+
+      await waitFor(() => 
expect(screen.queryByRole('alert')).not.toBeInTheDocument());
+      expect(getLlmConfig).toHaveBeenCalledTimes(2);
+      expect(getLlmModels).toHaveBeenCalledTimes(1);
+    },
+  );
+
   it('appliesABuiltinPromptTemplateWithItsModeAndEnhancementSettingTest', 
async () => {
     vi.mocked(openRunStream).mockResolvedValue(undefined);
     const user = userEvent.setup();
diff --git a/web/src/pages/ai/hooks/useLlmRuntime.test.ts 
b/web/src/pages/ai/hooks/useLlmRuntime.test.ts
index 21613363d..ef56043b1 100644
--- a/web/src/pages/ai/hooks/useLlmRuntime.test.ts
+++ b/web/src/pages/ai/hooks/useLlmRuntime.test.ts
@@ -220,6 +220,29 @@ describe('useLlmRuntime', () => {
     expect(modelsMock).not.toHaveBeenCalled();
   });
 
+  it('surfaces the failed load for the page and clears it on a successful 
reload', async () => {
+    configMock.mockRejectedValueOnce(new Error('runtime unavailable'));
+    const onError = vi.fn();
+
+    const { result } = renderHook(() => useLlmRuntime({ enabled: true, onError 
}));
+    await act(async () => {});
+
+    // The caller used to get the failure as a toast callback only, so nothing 
on the page could
+    // say why the composer was dead or offer a retry.
+    expect(result.current.error).toBe('runtime unavailable');
+    expect(result.current.llmReady).toBe(false);
+    expect(onError).toHaveBeenCalledTimes(1);
+
+    configMock.mockResolvedValue(config);
+    modelsMock.mockResolvedValue({ status: 0, data: [{ id: 'gpt-test' }] });
+    await act(async () => {
+      await result.current.reload();
+    });
+
+    expect(result.current.error).toBeNull();
+    expect(result.current.llmReady).toBe(true);
+  });
+
   it('keeps the last request winning when reload overlaps', async () => {
     // The initial effect load, the first reload and the second reload each 
suspend on the
     // deferred the mock hands out at call time. Resolving the latest request 
first proves the
diff --git a/web/src/pages/ai/hooks/useLlmRuntime.ts 
b/web/src/pages/ai/hooks/useLlmRuntime.ts
index 4532bd672..55277a807 100644
--- a/web/src/pages/ai/hooks/useLlmRuntime.ts
+++ b/web/src/pages/ai/hooks/useLlmRuntime.ts
@@ -18,6 +18,7 @@
 import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
 import { getLlmConfig, getLlmModels, type LlmConfig } from '../../../api/llm';
 import type { AgentEngine } from '../../../stores/engineStore';
+import { describeThrownMessage } from '../../../utils/apiError';
 
 /**
  * The provider runtime of the AI page: the LLM config, the selectable models 
and the current
@@ -55,6 +56,13 @@ export interface UseLlmRuntimeResult {
   selectedModel: string;
   /** Provider configured, enabled and a model selected: a send would be 
accepted. */
   llmReady: boolean;
+  /**
+   * Empty while the runtime loaded (or was never requested); the failure 
message otherwise. The
+   * caller keeps it on the page next to the composer: a runtime that could 
not be read is not a
+   * runtime that has nothing configured, and the composer is dead either way 
— the difference is
+   * whether the operator is told why and can retry.
+   */
+  error: string | null;
   setSelectedModel: (model: string) => void;
   /** Select a model and make sure it is among the options, e.g. one carried 
by a home-page draft. */
   selectModel: (model: string) => void;
@@ -73,6 +81,7 @@ export function useLlmRuntime(options: UseLlmRuntimeOptions): 
UseLlmRuntimeResul
   const [handoffModelOptions, setHandoffModelOptions] = 
useState<ModelOption[]>([]);
   const [modelsLoading, setModelsLoading] = useState(false);
   const [selectedModel, setSelectedModel] = useState('');
+  const [error, setError] = useState<string | null>(null);
 
   // Callbacks arrive as fresh closures on every render; a ref keeps `load` 
referentially stable so
   // the effect below runs once per `enabled` flip instead of on every render.
@@ -102,12 +111,14 @@ export function useLlmRuntime(options: 
UseLlmRuntimeOptions): UseLlmRuntimeResul
       setHandoffModelOptions([]);
       setSelectedModel('');
       setModelsLoading(false);
+      setError(null);
       return;
     }
     setModelsLoading(true);
     try {
       const loaded = await getLlmConfig();
       if (requestId !== loadSeqRef.current) return;
+      setError(null);
       setConfig(loaded);
       if (isAgentEngine(loaded.engine)) 
optionsRef.current.onEngine?.(loaded.engine);
       if (loaded.model) setSelectedModel((current) => current || loaded.model);
@@ -124,8 +135,11 @@ export function useLlmRuntime(options: 
UseLlmRuntimeOptions): UseLlmRuntimeResul
       } else if (loaded.model) {
         setModelOptions([{ value: loaded.model, label: loaded.model }]);
       }
-    } catch (error) {
-      if (requestId === loadSeqRef.current) 
optionsRef.current.onError?.(error);
+    } catch (failure) {
+      if (requestId === loadSeqRef.current) {
+        setError(describeThrownMessage(failure));
+        optionsRef.current.onError?.(failure);
+      }
     } finally {
       if (requestId === loadSeqRef.current) setModelsLoading(false);
     }
@@ -155,6 +169,7 @@ export function useLlmRuntime(options: 
UseLlmRuntimeOptions): UseLlmRuntimeResul
     modelsLoading,
     selectedModel,
     llmReady,
+    error,
     setSelectedModel,
     selectModel,
     reload: load,
diff --git a/web/src/pages/ai/index.tsx b/web/src/pages/ai/index.tsx
index bdfd2831f..640333941 100644
--- a/web/src/pages/ai/index.tsx
+++ b/web/src/pages/ai/index.tsx
@@ -302,6 +302,19 @@ const AiPage = () => {
                 }
               />
             )}
+            {llm.error !== null && (
+              <Alert
+                type="error"
+                showIcon
+                message={t('ai.runtimeLoadFailed')}
+                description={llm.error || undefined}
+                action={
+                  <Button size="small" loading={llm.modelsLoading} onClick={() 
=> llm.reload()}>
+                    {t('common.retry')}
+                  </Button>
+                }
+              />
+            )}
             <RuntimeNotices mock={useMock} rmqctlAvailable={rmqctlAvailable} 
runError={run.error} />
             <Composer
               value={inputValue}

Reply via email to