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.

Restore Assistant drafts after failed sends

rcourtmand8301eb70b5f
diff --git a/frontend-modern/src/components/AI/Chat/__tests__/AIChat.test.tsx b/frontend-modern/src/components/AI/Chat/__tests__/AIChat.test.tsx
index 4540bec828..9a9499ad13 100644
--- a/frontend-modern/src/components/AI/Chat/__tests__/AIChat.test.tsx
+++ b/frontend-modern/src/components/AI/Chat/__tests__/AIChat.test.tsx
@@ -1065,6 +1065,42 @@ describe('AIChat', () => {
expect(textarea.value).toBe('');
});
+ it('restores the submitted draft when send returns false', async () => {
+ mockChat.sendMessage.mockResolvedValue(false);
+ renderChat();
+ const textarea = screen.getByPlaceholderText(
+ 'Ask about your infrastructure...',
+ ) as HTMLTextAreaElement;
+
+ fireEvent.input(textarea, { target: { value: 'do not lose this draft' } });
+ fireEvent.keyDown(textarea, { key: 'Enter' });
+
+ expect(textarea.value).toBe('');
+ await waitFor(() => expect(textarea.value).toBe('do not lose this draft'));
+ expect(document.activeElement).toBe(textarea);
+ });
+
+ it('does not overwrite a newer draft when a failed send resolves late', async () => {
+ let resolveSend!: (ok: boolean) => void;
+ mockChat.sendMessage.mockReturnValue(
+ new Promise<boolean>((resolve) => {
+ resolveSend = resolve;
+ }),
+ );
+ renderChat();
+ const textarea = screen.getByPlaceholderText(
+ 'Ask about your infrastructure...',
+ ) as HTMLTextAreaElement;
+
+ fireEvent.input(textarea, { target: { value: 'first draft' } });
+ fireEvent.keyDown(textarea, { key: 'Enter' });
+ fireEvent.input(textarea, { target: { value: 'new draft while waiting' } });
+
+ resolveSend(false);
+
+ await waitFor(() => expect(textarea.value).toBe('new draft while waiting'));
+ });
+
it('allows newlines with Shift+Enter', () => {
renderChat();
const textarea = screen.getByPlaceholderText('Ask about your infrastructure...');
diff --git a/frontend-modern/src/components/AI/Chat/index.tsx b/frontend-modern/src/components/AI/Chat/index.tsx
index f960144f01..8a6971ef10 100644
--- a/frontend-modern/src/components/AI/Chat/index.tsx
+++ b/frontend-modern/src/components/AI/Chat/index.tsx
@@ -1386,15 +1386,30 @@ export const AIChat: Component<AIChatProps> = (props) => {
setMentionResources(dedupeMentionResources(mentionCandidates));
});
+ const restoreFailedSubmitDraft = (draft: string, mentions: MentionResource[]) => {
+ if (input().trim() || accumulatedMentions().length > 0) {
+ return;
+ }
+ setInput(draft);
+ setAccumulatedMentions(cloneMentions(mentions));
+ setMentionActive(false);
+ queueMicrotask(() => {
+ resizeTextarea();
+ focusComposer();
+ });
+ };
+
// Handle submit
const handleSubmit = () => {
- const prompt = input().trim();
+ const submittedInput = input();
+ const prompt = submittedInput.trim();
if (!prompt) return;
if (providerReadinessHasBlockingError()) {
focusComposer();
return;
}
const mentions = accumulatedMentions();
+ const submittedMentions = cloneMentions(mentions);
const mentionsForAPI =
mentions.length > 0
? mentions.map((mention) => ({
@@ -1444,16 +1459,24 @@ export const AIChat: Component<AIChatProps> = (props) => {
Boolean(ctx.handoffResources?.length) ||
Boolean(ctx.handoffActions?.length) ||
Boolean(ctx.handoffMetadata);
- sendPromise.then((ok) => {
- if (!ok) return;
- setEditingQueuedFollowUp(null);
- if (!queuedDraft && findingId) {
- aiChatStore.clearFindingId?.();
- }
- if (!queuedDraft && hasRequestHandoffPayload) {
- aiChatStore.clearRequestHandoffPayload?.();
- }
- });
+ sendPromise
+ .then((ok) => {
+ if (!ok) {
+ restoreFailedSubmitDraft(submittedInput, submittedMentions);
+ return;
+ }
+ setEditingQueuedFollowUp(null);
+ if (!queuedDraft && findingId) {
+ aiChatStore.clearFindingId?.();
+ }
+ if (!queuedDraft && hasRequestHandoffPayload) {
+ aiChatStore.clearRequestHandoffPayload?.();
+ }
+ })
+ .catch((error) => {
+ logger.warn('[AIChat] Failed to send Assistant message:', error);
+ restoreFailedSubmitDraft(submittedInput, submittedMentions);
+ });
setInput('');
setAccumulatedMentions([]);
setMentionActive(false);