Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Render Assistant errors as a recoverable block with Retry

rcourtman182e317acfa0
diff --git a/frontend-modern/src/components/AI/Chat/ChatMessages.tsx b/frontend-modern/src/components/AI/Chat/ChatMessages.tsx
index 7e17a335ad..dfb81e47bb 100644
--- a/frontend-modern/src/components/AI/Chat/ChatMessages.tsx
+++ b/frontend-modern/src/components/AI/Chat/ChatMessages.tsx
@@ -13,6 +13,7 @@ interface ChatMessagesProps {
answers: Array<{ id: string; value: string }>,
) => void;
onSkipQuestion: (messageId: string, questionId: string) => void;
+ onRetry?: (messageId: string) => void;
// Dashboard props
recentSessions?: ChatSession[];
onLoadSession?: (sessionId: string) => void;
@@ -91,6 +92,7 @@ export const ChatMessages: Component<ChatMessagesProps> = (props) => {
props.onAnswerQuestion(message.id, question, answers)
}
onSkipQuestion={(questionId) => props.onSkipQuestion(message.id, questionId)}
+ onRetry={props.onRetry}
/>
)}
</For>
diff --git a/frontend-modern/src/components/AI/Chat/MessageItem.tsx b/frontend-modern/src/components/AI/Chat/MessageItem.tsx
index 787ead63d9..b401dfa27a 100644
--- a/frontend-modern/src/components/AI/Chat/MessageItem.tsx
+++ b/frontend-modern/src/components/AI/Chat/MessageItem.tsx
@@ -21,6 +21,7 @@ interface MessageItemProps {
answers: Array<{ id: string; value: string }>,
) => void;
onSkipQuestion: (questionId: string) => void;
+ onRetry?: (messageId: string) => void;
}
/**
@@ -172,6 +173,53 @@ export const MessageItem: Component<MessageItemProps> = (props) => {
/>
</Show>
+ {/* Error block - distinct, recoverable */}
+ <Show when={props.message.error}>
+ <div
+ class="mt-2 flex items-start gap-2.5 rounded-md border border-red-200 dark:border-red-900/60 bg-red-50 dark:bg-red-950/30 px-3 py-2.5"
+ role="alert"
+ >
+ <svg
+ class="w-4 h-4 mt-0.5 shrink-0 text-red-500 dark:text-red-400"
+ fill="none"
+ stroke="currentColor"
+ viewBox="0 0 24 24"
+ >
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="1.8"
+ d="M12 9v3.75m9-.75a9 9 0 11-18 0 9 9 0 0118 0zm-9 3.75h.008v.008H12v-.008z"
+ />
+ </svg>
+ <div class="flex-1 min-w-0">
+ <p class="text-sm text-red-700 dark:text-red-300">{props.message.error}</p>
+ <Show when={props.onRetry}>
+ <button
+ type="button"
+ onClick={() => props.onRetry?.(props.message.id)}
+ class="mt-2 inline-flex items-center gap-1.5 rounded-md border border-red-300 dark:border-red-800 px-2 py-1 text-xs font-medium text-red-700 dark:text-red-300 transition-colors hover:bg-red-100 dark:hover:bg-red-900/40"
+ >
+ <svg
+ class="w-3.5 h-3.5"
+ fill="none"
+ stroke="currentColor"
+ viewBox="0 0 24 24"
+ >
+ <path
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="2"
+ d="M16.023 9.348h4.992V4.356M2.985 19.644v-4.992h4.992m-4.875 0a8.25 8.25 0 0013.803 3.7l3.181-3.182m-16.991-3.7a8.25 8.25 0 0113.803-3.7l3.181 3.182"
+ />
+ </svg>
+ Try again
+ </button>
+ </Show>
+ </div>
+ </div>
+ </Show>
+
{/* Streaming cursor */}
<Show when={isStreamingText()}>
<span class="inline-block w-1.5 h-4 ml-0.5 align-middle bg-blue-500 dark:bg-blue-400 animate-pulse rounded-full" />
diff --git a/frontend-modern/src/components/AI/Chat/__tests__/MessageItem.test.tsx b/frontend-modern/src/components/AI/Chat/__tests__/MessageItem.test.tsx
index afca2546e0..4e5e3a57c2 100644
--- a/frontend-modern/src/components/AI/Chat/__tests__/MessageItem.test.tsx
+++ b/frontend-modern/src/components/AI/Chat/__tests__/MessageItem.test.tsx
@@ -1,5 +1,5 @@
import { describe, expect, it, vi, afterEach } from 'vitest';
-import { cleanup, render, screen } from '@solidjs/testing-library';
+import { cleanup, render, screen, fireEvent } from '@solidjs/testing-library';
import { MessageItem } from '../MessageItem';
import type { ChatMessage, PendingApproval, PendingQuestion, StreamDisplayEvent } from '../types';
@@ -127,6 +127,54 @@ describe('MessageItem', () => {
});
});
+ describe('error block', () => {
+ it('renders a distinct error block with the message and a retry button', () => {
+ const onRetry = vi.fn();
+ render(() => (
+ <MessageItem
+ message={makeMessage({
+ role: 'assistant',
+ content: '',
+ error: 'The AI provider rejected the request for billing or quota reasons.',
+ })}
+ {...makeHandlers()}
+ onRetry={onRetry}
+ />
+ ));
+
+ const alert = screen.getByRole('alert');
+ expect(alert).toBeInTheDocument();
+ expect(alert.textContent).toContain('billing or quota reasons');
+
+ const retry = screen.getByRole('button', { name: /try again/i });
+ fireEvent.click(retry);
+ expect(onRetry).toHaveBeenCalledWith('msg-1');
+ });
+
+ it('does not show a retry button when no onRetry handler is provided', () => {
+ render(() => (
+ <MessageItem
+ message={makeMessage({ role: 'assistant', content: '', error: 'Something failed.' })}
+ {...makeHandlers()}
+ />
+ ));
+
+ expect(screen.getByRole('alert')).toBeInTheDocument();
+ expect(screen.queryByRole('button', { name: /try again/i })).not.toBeInTheDocument();
+ });
+
+ it('renders no error block when there is no error', () => {
+ render(() => (
+ <MessageItem
+ message={makeMessage({ role: 'assistant', content: 'All good.' })}
+ {...makeHandlers()}
+ />
+ ));
+
+ expect(screen.queryByRole('alert')).not.toBeInTheDocument();
+ });
+ });
+
describe('assistant message rendering', () => {
it('renders assistant indicator with label', () => {
render(() => (
diff --git a/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts b/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts
index d4eb1d68ec..dc3e35df7e 100644
--- a/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts
+++ b/frontend-modern/src/components/AI/Chat/__tests__/useChat.test.ts
@@ -77,6 +77,7 @@ describe('useChat', () => {
expect(chat.sessionId()).toBe('');
expect(chat.model()).toBe('');
expect(typeof chat.sendMessage).toBe('function');
+ expect(typeof chat.retryMessage).toBe('function');
expect(typeof chat.stop).toBe('function');
expect(typeof chat.clearMessages).toBe('function');
expect(typeof chat.loadSession).toBe('function');
@@ -191,7 +192,7 @@ describe('useChat', () => {
expect(chat.messages()[0]).toMatchObject({ role: 'user', content: 'hello' });
expect(chat.messages()[1]).toMatchObject({
role: 'assistant',
- content: 'Error: Failed to create chat session',
+ error: 'Could not start a chat session. Check your connection and try again.',
isStreaming: false,
});
expect(chat.isLoading()).toBe(false);
@@ -207,15 +208,36 @@ describe('useChat', () => {
expect(result).toBe(false);
expect(mockNotifyError).toHaveBeenCalledWith('server error');
- // Assistant message should have error content
+ // Assistant message should carry the error in its dedicated error field
const msgs = chat.messages();
const assistant = msgs.find((m) => m.role === 'assistant');
- expect(assistant?.content).toContain('Error: server error');
+ expect(assistant?.error).toContain('server error');
expect(assistant?.isStreaming).toBe(false);
expect(chat.isLoading()).toBe(false);
dispose();
});
+ it('retryMessage drops the failed turn and re-sends the prompt', async () => {
+ mockChat.mockRejectedValueOnce(new Error('server error'));
+ const { value: chat, dispose } = withRoot(() => useChat({ sessionId: 'sess' }));
+ await chat.sendMessage('hello');
+
+ const failed = chat.messages().find((m) => m.role === 'assistant');
+ expect(failed?.error).toContain('server error');
+ expect(chat.messages()).toHaveLength(2);
+
+ mockChat.mockResolvedValueOnce(undefined);
+ chat.retryMessage(failed!.id);
+ await new Promise((r) => setTimeout(r, 0));
+
+ const msgs = chat.messages();
+ // The failed user+assistant pair is replaced by a single clean attempt.
+ expect(msgs.filter((m) => m.role === 'user')).toHaveLength(1);
+ expect(msgs.some((m) => m.error)).toBe(false);
+ expect(mockChat).toHaveBeenCalledTimes(2);
+ dispose();
+ });
+
it('handles AbortError silently (returns false, no notification)', async () => {
const abortError = new Error('Aborted');
abortError.name = 'AbortError';
@@ -848,7 +870,7 @@ describe('useChat', () => {
const assistant = chat.messages().find((m) => m.role === 'assistant')!;
expect(assistant.isStreaming).toBe(false);
- expect(assistant.content).toBe('Error: Rate limited');
+ expect(assistant.error).toBe('Rate limited');
expect(assistant.pendingTools).toHaveLength(0);
dispose();
});
@@ -863,7 +885,7 @@ describe('useChat', () => {
fire({ type: 'error', data: 'Something went wrong' });
const assistant = chat.messages().find((m) => m.role === 'assistant')!;
- expect(assistant.content).toBe('Error: Something went wrong');
+ expect(assistant.error).toBe('Something went wrong');
dispose();
});
@@ -877,7 +899,7 @@ describe('useChat', () => {
fire({ type: 'error', data: null });
const assistant = chat.messages().find((m) => m.role === 'assistant')!;
- expect(assistant.content).toBe('Error: Request failed');
+ expect(assistant.error).toBe('Request failed');
dispose();
});
diff --git a/frontend-modern/src/components/AI/Chat/hooks/useChat.ts b/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
index 60cdb2cd32..f4e26af828 100644
--- a/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
+++ b/frontend-modern/src/components/AI/Chat/hooks/useChat.ts
@@ -435,11 +435,13 @@ export function useChat(options: UseChatOptions = {}) {
case 'error': {
const errorMsg = extractErrorMessage(event.data);
+ // Keep any content streamed before the failure; surface the error
+ // as a distinct, recoverable block rather than overwriting the answer.
return {
...msg,
isStreaming: false,
pendingTools: [],
- content: `Error: ${errorMsg || 'Request failed'}`,
+ error: errorMsg || 'Request failed',
};
}
@@ -506,7 +508,8 @@ export function useChat(options: UseChatOptions = {}) {
{
id: generateId(),
role: 'assistant',
- content: 'Error: Failed to create chat session',
+ content: '',
+ error: 'Could not start a chat session. Check your connection and try again.',
timestamp: new Date(),
isStreaming: false,
},
@@ -564,7 +567,7 @@ export function useChat(options: UseChatOptions = {}) {
setMessages((prev) =>
prev.map((msg) =>
msg.id === assistantId
- ? { ...msg, isStreaming: false, content: `Error: ${errorMessage}` }
+ ? { ...msg, isStreaming: false, error: errorMessage }
: msg,
),
);
@@ -785,6 +788,23 @@ export function useChat(options: UseChatOptions = {}) {
});
};
+ // Retry a failed assistant turn: drop the failed assistant message and the
+ // user prompt that triggered it from the view, then re-send that prompt so the
+ // conversation shows a single clean attempt instead of a dead-end error.
+ const retryMessage = (assistantMessageId: string) => {
+ const msgs = messages();
+ const idx = msgs.findIndex((m) => m.id === assistantMessageId);
+ if (idx < 0) return;
+ let userIdx = idx - 1;
+ while (userIdx >= 0 && msgs[userIdx].role !== 'user') userIdx--;
+ if (userIdx < 0) return;
+ const prompt = msgs[userIdx].content;
+ if (!prompt.trim()) return;
+ const removeIds = new Set([msgs[userIdx].id, assistantMessageId]);
+ setMessages((prev) => prev.filter((m) => !removeIds.has(m.id)));
+ void sendMessage(prompt);
+ };
+
return {
messages,
isLoading,
@@ -792,6 +812,7 @@ export function useChat(options: UseChatOptions = {}) {
model,
setModel,
sendMessage,
+ retryMessage,
stop,
clearMessages,
loadSession,
diff --git a/frontend-modern/src/components/AI/Chat/index.tsx b/frontend-modern/src/components/AI/Chat/index.tsx
index a61c54a873..b948a2a3bb 100644
--- a/frontend-modern/src/components/AI/Chat/index.tsx
+++ b/frontend-modern/src/components/AI/Chat/index.tsx
@@ -1547,6 +1547,7 @@ export const AIChat: Component<AIChatProps> = (props) => {
onSkip={handleSkip}
onAnswerQuestion={handleAnswerQuestion}
onSkipQuestion={handleSkipQuestion}
+ onRetry={(messageId) => chat.retryMessage(messageId)}
recentSessions={sessions()
.filter((s) => s.id !== chat.sessionId() && s.message_count > 0)
.slice(0, 3)}
diff --git a/frontend-modern/src/components/AI/Chat/types.ts b/frontend-modern/src/components/AI/Chat/types.ts
index 402f06c13a..04636bb6f4 100644
--- a/frontend-modern/src/components/AI/Chat/types.ts
+++ b/frontend-modern/src/components/AI/Chat/types.ts
@@ -106,6 +106,10 @@ export interface ChatMessage {
id: string;
role: 'user' | 'assistant';
content: string;
+ // Clean, user-facing error for a failed turn. Rendered as a distinct error
+ // block (not as answer content) so partial streamed content is preserved and
+ // the failure is unmistakable and recoverable.
+ error?: string;
thinking?: string;
thinkingChunks?: string[];
streamEvents?: StreamDisplayEvent[];