Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

feat(gui): add copy action to assistant responses

yxlyxff5019b633c2
diff --git a/gui/src/components/chat/AssistantResponseCopyButton.tsx b/gui/src/components/chat/AssistantResponseCopyButton.tsx
new file mode 100644
index 00000000..c7b3eca4
--- /dev/null
+++ b/gui/src/components/chat/AssistantResponseCopyButton.tsx
@@ -0,0 +1,37 @@
+import { Check, Copy } from "lucide-react";
+
+import { Button } from "@/components/ui/Button";
+import { useCopyToClipboard } from "@/hooks/useCopyToClipboard";
+
+interface AssistantResponseCopyButtonProps {
+ text: string;
+}
+
+export function AssistantResponseCopyButton({
+ text,
+}: AssistantResponseCopyButtonProps) {
+ const { copied, copy } = useCopyToClipboard();
+
+ if (text.length === 0) {
+ return null;
+ }
+
+ const label = copied ? "Copied" : "Copy response";
+
+ return (
+ <Button
+ type="button"
+ variant="ghost"
+ size="xs"
+ aria-label={label}
+ title={label}
+ onClick={() => {
+ void copy(text);
+ }}
+ className="-ml-1 rounded-full px-1.5 text-muted-foreground hover:text-foreground"
+ >
+ {copied ? <Check className="size-3" /> : <Copy className="size-3" />}
+ <span aria-live="polite">{label}</span>
+ </Button>
+ );
+}
diff --git a/gui/src/components/chat/ChatMarkdownMessage.test.tsx b/gui/src/components/chat/ChatMarkdownMessage.test.tsx
new file mode 100644
index 00000000..3ee17d73
--- /dev/null
+++ b/gui/src/components/chat/ChatMarkdownMessage.test.tsx
@@ -0,0 +1,197 @@
+import {
+ afterAll,
+ beforeAll,
+ beforeEach,
+ describe,
+ expect,
+ mock,
+ test,
+} from "bun:test";
+import type { ReactNode } from "react";
+import ReactDOM from "react-dom";
+
+const { flushSync } = ReactDOM;
+
+let unregisterDom: (() => Promise<void>) | null = null;
+let createRoot: typeof import("react-dom/client").createRoot;
+let ChatMarkdownMessage: typeof import("./ChatMarkdownMessage").ChatMarkdownMessage;
+
+beforeAll(async () => {
+ const { GlobalRegistrator } = await import("@happy-dom/global-registrator");
+ GlobalRegistrator.register();
+ unregisterDom = () => GlobalRegistrator.unregister();
+ Object.defineProperty(document, "compatMode", {
+ value: "CSS1Compat",
+ configurable: true,
+ });
+ ({ createRoot } = await import("react-dom/client"));
+ ({ ChatMarkdownMessage } = await import("./ChatMarkdownMessage"));
+});
+
+afterAll(async () => {
+ await unregisterDom?.();
+});
+
+beforeEach(() => {
+ document.body.replaceChildren();
+ Object.defineProperty(navigator, "clipboard", {
+ value: undefined,
+ configurable: true,
+ });
+});
+
+function mount(node: ReactNode) {
+ const container = document.createElement("div");
+ document.body.appendChild(container);
+ const root = createRoot(container);
+ flushSync(() => {
+ root.render(node);
+ });
+
+ return {
+ container,
+ rerender: (nextNode: ReactNode) => {
+ flushSync(() => {
+ root.render(nextNode);
+ });
+ },
+ unmount: () => {
+ flushSync(() => root.unmount());
+ container.remove();
+ },
+ };
+}
+
+function mountMessage(text: string, copyText?: string) {
+ return mount(
+ <ChatMarkdownMessage
+ copyText={copyText}
+ text={text}
+ toneClassName="text-foreground"
+ workspacePath={null}
+ />,
+ );
+}
+
+function setClipboard(writeText: (value: string) => Promise<void>) {
+ Object.defineProperty(navigator, "clipboard", {
+ value: { writeText },
+ configurable: true,
+ });
+}
+
+async function flushUpdates(delayMs = 0) {
+ await new Promise((resolve) => window.setTimeout(resolve, delayMs));
+}
+
+async function click(button: HTMLButtonElement) {
+ button.click();
+ await flushUpdates();
+}
+
+describe("assistant response copy action", () => {
+ test("copies the exact raw output, including Markdown, whitespace, and Unicode", async () => {
+ const text = "## Result\n\n```ts\nconst café = \"☕\";\n```\n\nDone. ";
+ const writeText = mock(async () => {});
+ setClipboard(writeText);
+ const { container, unmount } = mountMessage(text, text);
+
+ try {
+ const button = container.querySelector<HTMLButtonElement>(
+ 'button[aria-label="Copy response"]',
+ );
+ expect(button).not.toBeNull();
+
+ await click(button!);
+
+ expect(writeText).toHaveBeenCalledTimes(1);
+ expect(writeText).toHaveBeenCalledWith(text);
+ expect(button!.getAttribute("aria-label")).toBe("Copied");
+ expect(button!.textContent).toBe("Copied");
+ } finally {
+ unmount();
+ }
+ });
+
+ test("does not add a copy action when no assistant copy payload is provided", () => {
+ const { container, unmount } = mountMessage("Private reasoning text");
+ try {
+ expect(container.querySelector('button[aria-label="Copy response"]')).toBeNull();
+ } finally {
+ unmount();
+ }
+ });
+
+ test("adds the action during streaming and copies the latest output exactly", async () => {
+ const writeText = mock(async () => {});
+ setClipboard(writeText);
+ const message = mountMessage("", "");
+
+ try {
+ expect(message.container.querySelector("button")).toBeNull();
+
+ message.rerender(
+ <ChatMarkdownMessage
+ copyText={" \n\t "}
+ text={" \n\t "}
+ toneClassName="text-foreground"
+ workspacePath={null}
+ />,
+ );
+
+ const button = message.container.querySelector<HTMLButtonElement>(
+ 'button[aria-label="Copy response"]',
+ );
+ expect(button).not.toBeNull();
+ await click(button!);
+ expect(writeText).toHaveBeenCalledWith(" \n\t ");
+ } finally {
+ message.unmount();
+ }
+ });
+
+ test("keeps the default label when clipboard access is unavailable or rejects", async () => {
+ const unavailable = mountMessage("Hello", "Hello");
+ try {
+ const button = unavailable.container.querySelector<HTMLButtonElement>("button");
+ expect(button).not.toBeNull();
+ await click(button!);
+ expect(button!.getAttribute("aria-label")).toBe("Copy response");
+ } finally {
+ unavailable.unmount();
+ }
+
+ setClipboard(async () => {
+ throw new Error("clipboard denied");
+ });
+ const rejected = mountMessage("Hello again", "Hello again");
+ try {
+ const button = rejected.container.querySelector<HTMLButtonElement>("button");
+ expect(button).not.toBeNull();
+ await click(button!);
+ expect(button!.getAttribute("aria-label")).toBe("Copy response");
+ expect(button!.textContent).toBe("Copy response");
+ } finally {
+ rejected.unmount();
+ }
+ });
+
+ test("returns copied feedback to the default label after the reset delay", async () => {
+ setClipboard(async () => {});
+ const { container, unmount } = mountMessage("Reset me", "Reset me");
+
+ try {
+ const button = container.querySelector<HTMLButtonElement>("button");
+ expect(button).not.toBeNull();
+ await click(button!);
+ expect(button!.getAttribute("aria-label")).toBe("Copied");
+
+ await flushUpdates(1_250);
+
+ expect(button!.getAttribute("aria-label")).toBe("Copy response");
+ expect(button!.textContent).toBe("Copy response");
+ } finally {
+ unmount();
+ }
+ });
+});
diff --git a/gui/src/components/chat/ChatMarkdownMessage.tsx b/gui/src/components/chat/ChatMarkdownMessage.tsx
new file mode 100644
index 00000000..d45ba0fc
--- /dev/null
+++ b/gui/src/components/chat/ChatMarkdownMessage.tsx
@@ -0,0 +1,29 @@
+import { AssistantResponseCopyButton } from "./AssistantResponseCopyButton";
+import { ChatMarkdown } from "./ChatMarkdown";
+
+interface ChatMarkdownMessageProps {
+ copyText?: string;
+ text: string;
+ toneClassName: string;
+ workspacePath: string | null;
+}
+
+export function ChatMarkdownMessage({
+ copyText,
+ text,
+ toneClassName,
+ workspacePath,
+}: ChatMarkdownMessageProps) {
+ return (
+ <article className="flex max-w-3xl flex-col items-start gap-2">
+ <ChatMarkdown
+ text={text}
+ className={`cg-stream-in ${toneClassName}`}
+ workspacePath={workspacePath}
+ />
+ {copyText == null ? null : (
+ <AssistantResponseCopyButton text={copyText} />
+ )}
+ </article>
+ );
+}
diff --git a/gui/src/components/chat/ChatMessageRow.tsx b/gui/src/components/chat/ChatMessageRow.tsx
index a48b5ba6..0faaa0d2 100644
--- a/gui/src/components/chat/ChatMessageRow.tsx
+++ b/gui/src/components/chat/ChatMessageRow.tsx
@@ -2,7 +2,7 @@ import {
CHAT_BODY_TONE_CLASS,
CHAT_REASONING_TONE_CLASS,
} from "./constants/chatStyles";
-import { ChatMarkdown } from "./ChatMarkdown";
+import { ChatMarkdownMessage } from "./ChatMarkdownMessage";
import { ChatInlineText } from "./ChatInlineText";
import { AttachmentTray } from "@/components/attachments/AttachmentTray";
import {
@@ -12,7 +12,6 @@ import {
import { openPathDefault } from "@/services/desktop/client";
import type {
ChatMessageRowProps,
- MarkdownChatMessageProps,
TextOnlyMessageProps,
} from "./types/chatComponents";
@@ -53,29 +52,14 @@ function UserChatMessage({ text, workspacePath }: UserChatMessageProps) {
);
}
-function MarkdownChatMessage({
- text,
- toneClassName,
- workspacePath,
-}: MarkdownChatMessageProps) {
- return (
- <article className="max-w-3xl">
- <ChatMarkdown
- text={text}
- className={`cg-stream-in ${toneClassName ?? ""}`}
- workspacePath={workspacePath}
- />
- </article>
- );
-}
-
export function ChatMessageRow({ message, workspacePath }: ChatMessageRowProps) {
switch (message.kind) {
case "user":
return <UserChatMessage text={message.text} workspacePath={workspacePath} />;
case "assistant":
return (
- <MarkdownChatMessage
+ <ChatMarkdownMessage
+ copyText={message.text}
text={message.text}
toneClassName={CHAT_BODY_TONE_CLASS}
workspacePath={workspacePath}
@@ -83,7 +67,7 @@ export function ChatMessageRow({ message, workspacePath }: ChatMessageRowProps)
);
case "reasoning":
return (
- <MarkdownChatMessage
+ <ChatMarkdownMessage
text={message.text}
toneClassName={CHAT_REASONING_TONE_CLASS}
workspacePath={workspacePath}
diff --git a/gui/src/components/chat/types/chatComponents.ts b/gui/src/components/chat/types/chatComponents.ts
index dfea505f..74bda118 100644
--- a/gui/src/components/chat/types/chatComponents.ts
+++ b/gui/src/components/chat/types/chatComponents.ts
@@ -157,12 +157,6 @@ export interface FileDiffResultProps {
workspacePath: string | null;
}
-export interface MarkdownChatMessageProps {
- text: string;
- toneClassName: string;
- workspacePath: string | null;
-}
-
export interface FilePathButtonProps {
label: string;
path: string;