Skip to content

feat(agent): add copy button to assistant messages in AI agent chat (closes #371) - #374

Merged
gkhngyk merged 6 commits into
ansvisor:mainfrom
Maqbool61:feat/copy-button-agent-messages
Jul 9, 2026
Merged

gkhngyk merged 6 commits into
ansvisor:mainfrom
Maqbool61:feat/copy-button-agent-messages

Conversation

@Maqbool61

Copy link
Copy Markdown
Contributor

Problem

On the /dashboard/agent page, there was no way to copy an assistant reply.
Users had to hand-select the text, which is especially painful for long
markdown responses with lists, bold, and headings.

Solution

Added a small copy icon button that appears on hover at the bottom-right of
every assistant message bubble. Clicking it copies the raw markdown text to
the clipboard and briefly shows a "Link copied" confirmation toast + check
icon, then resets after 2 seconds.

Implementation details

File changed: web/src/app/[locale]/(dashboard)/dashboard/agent/page.tsx

1. Imports

  • Added Copy and Check from lucide-react (same icon set used throughout the app)
  • Added toast from sonner (same pattern as team-section.tsx → toast.success('Link copied'))

2. MessageBubble changes

  • Added copied state (useState(false))
  • rawText — concatenates only type === 'text' parts, skipping tool-* and
    tool-render_chart parts as specified: (message.parts ?? []).filter(p => p.type === 'text').map(p => p.text).join('\n\n')
  • handleCopy() — calls navigator.clipboard.writeText(rawText), fires
    toast.success('Link copied'), sets copied = true for 2 s then resets
  • Outer bubble wrapper gets group class so child hover states work
  • Copy button: absolute bottom-2 right-2, opacity-0 group-hover:opacity-100,
    flips between <Copy /> and <Check /> based on copied state
  • Only rendered when !isUser && rawText — user messages and empty assistant
    messages never show the button

3. Reuses existing app patterns

  • navigator.clipboard.writeText + toast.success('Link copied') — identical
    to team-section.tsx line 451
  • Copy icon — same as audit-report.tsx and api-keys-section.tsx
  • opacity-0 group-hover:opacity-100 — same hover reveal used on the delete
    button in the conversation sidebar

Verification

How to test manually

  1. Go to /dashboard/agent and ask the assistant a question
  2. Hover over the assistant reply → copy icon appears bottom-right of the bubble
  3. Click it → icon flips to ✓, "Link copied" toast appears
  4. Paste into a doc → markdown is preserved (lists, bold, headings survive)
  5. User messages → no copy button ✅
  6. Tool call disclosures → no copy button ✅

Closes #371

Maqbool61 added 5 commits July 7, 2026 17:52
Closes #365

- Added silent parameter (default false) to loadData
- When silent=true, skips setLoading(true) so existing rows stay
  visible during background refresh
- Initial load and filter-change calls remain non-silent (correct
  full loading state on first paint and filter switch)
- All post-mutation calls use loadData(true): handleSendWebhook,
  handleDismiss, handleBulkSend, handleBulkDone, handleBulkDismiss,
  and pollJob completion handler
- Zero new TypeScript errors introduced (verified via tsc --noEmit diff)

Signed-off-by: Maqbool Ahmed <[email protected]>

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice — the copy button works exactly as described (raw markdown text, tool/chart parts skipped, assistant-only, hover reveal), and it's Prettier-clean. 🙌

One small thing before merge: the toast says 'Link copied', but you're copying a message, not a link — please change it to something like 'Copied' or 'Message copied' (the 'Link copied' string is carried over from the team-invite copy pattern).

Optional a11y nice-to-have (non-blocking): add focus-visible:opacity-100 alongside group-hover:opacity-100 so keyboard users can see the button when it's focused. Thanks!

@Maqbool61

Copy link
Copy Markdown
Contributor Author

Nice — the copy button works exactly as described (raw markdown text, tool/chart parts skipped, assistant-only, hover reveal), and it's Prettier-clean. 🙌

One small thing before merge: the toast says 'Link copied', but you're copying a message, not a link — please change it to something like 'Copied' or 'Message copied' (the 'Link copied' string is carried over from the team-invite copy pattern).

Optional a11y nice-to-have (non-blocking): add focus-visible:opacity-100 alongside group-hover:opacity-100 so keyboard users can see the button when it's focused. Thanks!

Working on it

@Maqbool61
Maqbool61 requested a review from gkhngyk July 8, 2026 20:23
@Maqbool61

Copy link
Copy Markdown
Contributor Author

Nice — the copy button works exactly as described (raw markdown text, tool/chart parts skipped, assistant-only, hover reveal), and it's Prettier-clean. 🙌

One small thing before merge: the toast says 'Link copied', but you're copying a message, not a link — please change it to something like 'Copied' or 'Message copied' (the 'Link copied' string is carried over from the team-invite copy pattern).

Optional a11y nice-to-have (non-blocking): add focus-visible:opacity-100 alongside group-hover:opacity-100 so keyboard users can see the button when it's focused. Thanks!

Done — changed the toast to 'Copied' and added focus-visible:opacity-100 alongside group-hover:opacity-100. Ready for re-review!

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix confirmed — the toast now reads 'Copied' (accurate, since a message is copied, not a link), and you even added the aria-label + focus-visible:opacity-100 a11y touch. Copy logic is correct: raw markdown only (type === 'text'), tool/chart parts skipped, assistant-only, empty messages excluded. Prettier-clean. Thanks for iterating on this! 🙌

@gkhngyk
gkhngyk merged commit 5625386 into ansvisor:main Jul 9, 2026
3 checks passed
@Maqbool61
Maqbool61 deleted the feat/copy-button-agent-messages branch July 9, 2026 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(agent): add a copy button to agent messages in the AI agent chat

2 participants