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.

Add Assistant copy last answer action

rcourtmaned1e0cee7dc3
diff --git a/frontend-modern/src/components/AI/Chat/MessageItem.tsx b/frontend-modern/src/components/AI/Chat/MessageItem.tsx
index 2de4b38544..a1241c1aad 100644
--- a/frontend-modern/src/components/AI/Chat/MessageItem.tsx
+++ b/frontend-modern/src/components/AI/Chat/MessageItem.tsx
@@ -23,6 +23,7 @@ import { PendingToolBlock, ToolExecutionBlock } from './ToolExecutionBlock';
import { ApprovalCard } from './ApprovalCard';
import { QuestionCard } from './QuestionCard';
import { ThinkingBlock } from './ThinkingBlock';
+import { getAssistantAnswerText } from './assistantAnswerText';
import { stripAssistantOutputArtifacts } from './assistantOutputHygiene';
import { formatAssistantWorkflowStatus } from './activeTurnStatus';
import { groupStreamEventsForDisplay } from './streamEventGrouping';
@@ -224,9 +225,10 @@ export const MessageItem: Component<MessageItemProps> = (props) => {
// Copy-to-clipboard for a completed assistant answer.
const [copied, setCopied] = createSignal(false);
- const canCopy = () => !props.message.isStreaming && !!visibleMessageContent().trim();
+ const copyableMessageText = () => getAssistantAnswerText(props.message);
+ const canCopy = () => !props.message.isStreaming && !!copyableMessageText();
const copyMessage = async () => {
- const text = visibleMessageContent();
+ const text = copyableMessageText();
try {
await navigator.clipboard?.writeText(text);
setCopied(true);
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 617abe3148..0ec5b3f57c 100644
--- a/frontend-modern/src/components/AI/Chat/__tests__/AIChat.test.tsx
+++ b/frontend-modern/src/components/AI/Chat/__tests__/AIChat.test.tsx
@@ -450,10 +450,56 @@ describe('AIChat', () => {
renderChat();
expect(screen.getByRole('button', { name: 'Fork current Assistant session' })).toBeDisabled();
+ expect(screen.getByRole('button', { name: 'Copy last Assistant answer' })).toBeDisabled();
expect(screen.getByRole('button', { name: 'Copy Assistant transcript' })).toBeDisabled();
expect(screen.getByRole('button', { name: 'Export Assistant transcript' })).toBeDisabled();
});
+ it('copies the latest Assistant answer from the header', async () => {
+ const writeText = vi.fn().mockResolvedValue(undefined);
+ Object.defineProperty(navigator, 'clipboard', {
+ value: { writeText },
+ configurable: true,
+ });
+ mockChat.messages.mockReturnValue([
+ {
+ id: 'assistant-1',
+ role: 'assistant',
+ content: 'Earlier answer',
+ timestamp: new Date('2026-06-06T12:34:56Z'),
+ },
+ {
+ id: 'user-1',
+ role: 'user',
+ content: 'what changed now?',
+ timestamp: new Date('2026-06-06T12:35:00Z'),
+ },
+ {
+ id: 'assistant-2',
+ role: 'assistant',
+ content: '',
+ isStreaming: true,
+ streamEvents: [
+ {
+ type: 'content',
+ content: 'Current streamed answer.',
+ },
+ ],
+ timestamp: new Date('2026-06-06T12:35:01Z'),
+ },
+ ]);
+
+ renderChat();
+ fireEvent.click(screen.getByRole('button', { name: 'Copy last Assistant answer' }));
+
+ await waitFor(() => {
+ expect(writeText).toHaveBeenCalledWith('Current streamed answer.');
+ });
+ await waitFor(() => {
+ expect(mockNotificationStore.success).toHaveBeenCalledWith('Assistant answer copied', 2000);
+ });
+ });
+
it('copies the current Assistant transcript from the header', async () => {
const writeText = vi.fn().mockResolvedValue(undefined);
Object.defineProperty(navigator, 'clipboard', {
diff --git a/frontend-modern/src/components/AI/Chat/__tests__/assistantAnswerText.test.ts b/frontend-modern/src/components/AI/Chat/__tests__/assistantAnswerText.test.ts
new file mode 100644
index 0000000000..b2959c56f3
--- /dev/null
+++ b/frontend-modern/src/components/AI/Chat/__tests__/assistantAnswerText.test.ts
@@ -0,0 +1,70 @@
+import { describe, expect, it } from 'vitest';
+import { getAssistantAnswerText, getLastAssistantAnswerText } from '../assistantAnswerText';
+import type { ChatMessage } from '../types';
+
+const makeMessage = (overrides: Partial<ChatMessage>): ChatMessage => ({
+ id: 'message-1',
+ role: 'assistant',
+ content: '',
+ timestamp: new Date('2026-06-06T12:00:00Z'),
+ ...overrides,
+});
+
+describe('assistantAnswerText', () => {
+ it('returns the latest assistant message text without transcript framing', () => {
+ const messages = [
+ makeMessage({ id: 'assistant-1', content: 'Earlier answer' }),
+ makeMessage({ id: 'user-1', role: 'user', content: 'next question' }),
+ makeMessage({ id: 'assistant-2', content: 'Latest answer' }),
+ ];
+
+ expect(getLastAssistantAnswerText(messages)).toBe('Latest answer');
+ });
+
+ it('uses streamed content events when the message body has not been compacted yet', () => {
+ const message = makeMessage({
+ content: '',
+ isStreaming: true,
+ streamEvents: [
+ { type: 'workflow_status', workflowStatus: { message: 'Checking inventory' } },
+ { type: 'content', content: 'Streaming answer part one.' },
+ { type: 'content', content: 'Streaming answer part two.' },
+ ],
+ });
+
+ expect(getAssistantAnswerText(message)).toBe(
+ 'Streaming answer part one.\n\nStreaming answer part two.',
+ );
+ });
+
+ it('does not fall back to an older answer when the current assistant message has no text', () => {
+ const messages = [
+ makeMessage({ id: 'assistant-1', content: 'Earlier answer' }),
+ makeMessage({
+ id: 'assistant-2',
+ content: '',
+ streamEvents: [
+ {
+ type: 'tool',
+ tool: {
+ name: 'pulse_read',
+ input: '{}',
+ output: '',
+ success: true,
+ },
+ },
+ ],
+ }),
+ ];
+
+ expect(getLastAssistantAnswerText(messages)).toBe('');
+ });
+
+ it('keeps raw tool-call artifacts out of copied assistant answers', () => {
+ const message = makeMessage({
+ content: 'Visible answer\n\n{"name":"pulse_read","arguments":{"command":"ls"}}',
+ });
+
+ expect(getAssistantAnswerText(message)).toBe('Visible answer');
+ });
+});
diff --git a/frontend-modern/src/components/AI/Chat/assistantAnswerText.ts b/frontend-modern/src/components/AI/Chat/assistantAnswerText.ts
new file mode 100644
index 0000000000..37d8c7ac4e
--- /dev/null
+++ b/frontend-modern/src/components/AI/Chat/assistantAnswerText.ts
@@ -0,0 +1,29 @@
+import { stripAssistantOutputArtifacts } from './assistantOutputHygiene';
+import type { ChatMessage } from './types';
+
+const normalizeAnswerText = (text?: string): string =>
+ stripAssistantOutputArtifacts(text || '').text.trim();
+
+export const getAssistantAnswerText = (message: ChatMessage): string => {
+ if (message.role !== 'assistant') return '';
+
+ const messageContent = normalizeAnswerText(message.content);
+ if (messageContent) return messageContent;
+
+ return (message.streamEvents || [])
+ .filter((event) => event.type === 'content')
+ .map((event) => normalizeAnswerText(event.content))
+ .filter(Boolean)
+ .join('\n\n')
+ .trim();
+};
+
+export const getLastAssistantAnswerText = (messages: ChatMessage[]): string => {
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
+ const message = messages[index];
+ if (message?.role !== 'assistant') continue;
+ return getAssistantAnswerText(message);
+ }
+
+ return '';
+};
diff --git a/frontend-modern/src/components/AI/Chat/index.tsx b/frontend-modern/src/components/AI/Chat/index.tsx
index e1db827526..be1b70ff7a 100644
--- a/frontend-modern/src/components/AI/Chat/index.tsx
+++ b/frontend-modern/src/components/AI/Chat/index.tsx
@@ -12,6 +12,7 @@ import { unwrap } from 'solid-js/store';
import SendIcon from 'lucide-solid/icons/send';
import SquareIcon from 'lucide-solid/icons/square';
import ClockIcon from 'lucide-solid/icons/clock';
+import ClipboardCopyIcon from 'lucide-solid/icons/clipboard-copy';
import CopyIcon from 'lucide-solid/icons/copy';
import DownloadIcon from 'lucide-solid/icons/download';
import GitForkIcon from 'lucide-solid/icons/git-fork';
@@ -44,6 +45,9 @@ import {
AI_CHAT_CLOSE_LABEL,
AI_CHAT_CONTROL_MODE_LABEL,
AI_CHAT_CONTROL_MODE_MENU_LABEL,
+ AI_CHAT_COPY_LAST_ANSWER_ERROR_MESSAGE,
+ AI_CHAT_COPY_LAST_ANSWER_LABEL,
+ AI_CHAT_COPY_LAST_ANSWER_SUCCESS_MESSAGE,
AI_CHAT_COPY_TRANSCRIPT_LABEL,
AI_CHAT_DISCOVERY_HINT_BODY,
AI_CHAT_DISCOVERY_HINT_DISMISS_LABEL,
@@ -111,6 +115,7 @@ import { ChatMessages } from './ChatMessages';
import { ModelSelector } from './ModelSelector';
import { MentionAutocomplete, type MentionResource } from './MentionAutocomplete';
import { getAssistantActiveTurnStatus } from './activeTurnStatus';
+import { getLastAssistantAnswerText } from './assistantAnswerText';
import {
buildAssistantTranscriptFilename,
downloadAssistantTranscriptFile,
@@ -1272,6 +1277,8 @@ export const AIChat: Component<AIChatProps> = (props) => {
};
const hasCurrentTranscript = createMemo(() => hasAssistantTranscriptContent(chat.messages()));
+ const lastAssistantAnswerText = createMemo(() => getLastAssistantAnswerText(chat.messages()));
+ const hasLastAssistantAnswer = createMemo(() => !!lastAssistantAnswerText());
const canForkCurrentSession = createMemo(
() =>
Boolean(chat.sessionId().trim()) &&
@@ -1319,6 +1326,19 @@ export const AIChat: Component<AIChatProps> = (props) => {
notificationStore.warning('Clipboard blocked; transcript opened for manual copy', 4000);
};
+ const copyLastAssistantAnswer = async () => {
+ const answer = lastAssistantAnswerText();
+ if (!answer) return;
+
+ const copied = await copyToClipboard(answer);
+ if (copied) {
+ notificationStore.success(AI_CHAT_COPY_LAST_ANSWER_SUCCESS_MESSAGE, 2000);
+ return;
+ }
+
+ notificationStore.error(AI_CHAT_COPY_LAST_ANSWER_ERROR_MESSAGE);
+ };
+
const exportAssistantTranscript = () => {
const generatedAt = new Date();
const transcript = buildCurrentTranscript(generatedAt);
@@ -2660,6 +2680,19 @@ export const AIChat: Component<AIChatProps> = (props) => {
</Show>
</button>
+ <button
+ type="button"
+ onClick={() => {
+ void copyLastAssistantAnswer();
+ }}
+ disabled={!hasLastAssistantAnswer()}
+ class="flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-md border border-border bg-surface text-muted transition-colors hover:border-border hover:bg-surface-hover hover:text-base-content disabled:cursor-not-allowed disabled:opacity-50 disabled:hover:bg-surface disabled:hover:text-muted"
+ title={AI_CHAT_COPY_LAST_ANSWER_LABEL}
+ aria-label={AI_CHAT_COPY_LAST_ANSWER_LABEL}
+ >
+ <ClipboardCopyIcon class="h-4 w-4" aria-hidden="true" />
+ </button>
+
<button
type="button"
onClick={() => {
diff --git a/frontend-modern/src/utils/__tests__/aiChatPresentation.test.ts b/frontend-modern/src/utils/__tests__/aiChatPresentation.test.ts
index 3cd48cd763..df0195c969 100644
--- a/frontend-modern/src/utils/__tests__/aiChatPresentation.test.ts
+++ b/frontend-modern/src/utils/__tests__/aiChatPresentation.test.ts
@@ -6,6 +6,9 @@ import {
AI_CHAT_CLOSE_LABEL,
AI_CHAT_CONTROL_MODE_LABEL,
AI_CHAT_CONTROL_MODE_MENU_LABEL,
+ AI_CHAT_COPY_LAST_ANSWER_ERROR_MESSAGE,
+ AI_CHAT_COPY_LAST_ANSWER_LABEL,
+ AI_CHAT_COPY_LAST_ANSWER_SUCCESS_MESSAGE,
AI_CHAT_DISCOVERY_HINT_BODY,
AI_CHAT_DISCOVERY_HINT_DISMISS_LABEL,
AI_CHAT_DISCOVERY_HINT_TITLE,
@@ -49,6 +52,9 @@ describe('aiChatPresentation', () => {
expect(AI_CHAT_NEW_SESSION_BUTTON_TITLE).toBe('Start new Assistant session');
expect(AI_CHAT_NEW_SESSION_MENU_LABEL).toBe('New session');
expect(AI_CHAT_NEW_SESSION_MENU_ARIA_LABEL).toBe('Start new Assistant session');
+ expect(AI_CHAT_COPY_LAST_ANSWER_LABEL).toBe('Copy last Assistant answer');
+ expect(AI_CHAT_COPY_LAST_ANSWER_SUCCESS_MESSAGE).toBe('Assistant answer copied');
+ expect(AI_CHAT_COPY_LAST_ANSWER_ERROR_MESSAGE).toBe('Failed to copy Assistant answer');
expect(AI_CHAT_LAUNCHER_ARIA_LABEL).toBe('Expand Pulse Assistant');
expect(AI_CHAT_CLOSE_LABEL).toBe('Close Pulse Assistant');
expect(AI_CHAT_SESSION_MENU_TITLE).toBe('Pulse Assistant sessions');
diff --git a/frontend-modern/src/utils/aiChatPresentation.ts b/frontend-modern/src/utils/aiChatPresentation.ts
index 766e819ea0..6765684fd3 100644
--- a/frontend-modern/src/utils/aiChatPresentation.ts
+++ b/frontend-modern/src/utils/aiChatPresentation.ts
@@ -16,6 +16,9 @@ export const AI_CHAT_FORK_SESSION_LOAD_ERROR_MESSAGE =
'Forked Assistant session but failed to load it';
export const AI_CHAT_FORK_SESSION_ERROR_MESSAGE = 'Failed to fork Assistant session';
export const AI_CHAT_FORK_SESSION_SUCCESS_MESSAGE = 'Assistant session forked';
+export const AI_CHAT_COPY_LAST_ANSWER_LABEL = 'Copy last Assistant answer';
+export const AI_CHAT_COPY_LAST_ANSWER_SUCCESS_MESSAGE = 'Assistant answer copied';
+export const AI_CHAT_COPY_LAST_ANSWER_ERROR_MESSAGE = 'Failed to copy Assistant answer';
export const AI_CHAT_COPY_TRANSCRIPT_LABEL = 'Copy Assistant transcript';
export const AI_CHAT_EXPORT_TRANSCRIPT_LABEL = 'Export Assistant transcript';
export const AI_CHAT_TRANSCRIPT_FALLBACK_CLOSE_LABEL = 'Close Assistant transcript copy panel';