Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix: support fallback copy for webui replies
chengyongru3e98a0318847
diff --git a/webui/src/components/MessageBubble.tsx b/webui/src/components/MessageBubble.tsxindex 6fbd29a4d..d3d3b8ac5 100644--- a/webui/src/components/MessageBubble.tsx+++ b/webui/src/components/MessageBubble.tsx@@ -14,6 +14,7 @@ import { CliAppMentionText } from "@/components/CliAppMentionText";import { ImageLightbox } from "@/components/ImageLightbox";import { MarkdownText, preloadMarkdownText } from "@/components/MarkdownText";import { cn } from "@/lib/utils";+import { copyTextToClipboard } from "@/lib/clipboard";import { formatTurnLatency } from "@/lib/format";import { toMediaAttachment } from "@/lib/media";import type {@@ -71,8 +72,8 @@ export function MessageBubble({}, []);const onCopyAssistantReply = useCallback(() => {- if (!navigator.clipboard) return;- void navigator.clipboard.writeText(message.content).then(() => {+ void copyTextToClipboard(message.content).then((ok) => {+ if (!ok) return;setCopied(true);if (copyResetRef.current !== null) {window.clearTimeout(copyResetRef.current);diff --git a/webui/src/lib/clipboard.ts b/webui/src/lib/clipboard.tsnew file mode 100644index 000000000..53ffdf78c--- /dev/null+++ b/webui/src/lib/clipboard.ts@@ -0,0 +1,40 @@+export async function copyTextToClipboard(text: string): Promise<boolean> {+ try {+ if (navigator.clipboard?.writeText) {+ await navigator.clipboard.writeText(text);+ return true;+ }+ } catch {+ // Fall through to the legacy path for browsers/WebViews where the+ // Clipboard API exists but rejects outside a secure context.+ }++ return copyTextWithTextarea(text);+}++function copyTextWithTextarea(text: string): boolean {+ if (typeof document.execCommand !== "function") {+ return false;+ }++ const textarea = document.createElement("textarea");+ textarea.value = text;+ textarea.setAttribute("readonly", "");+ textarea.style.position = "fixed";+ textarea.style.top = "-9999px";+ textarea.style.left = "-9999px";+ textarea.style.opacity = "0";++ document.body.appendChild(textarea);+ textarea.focus();+ textarea.select();+ textarea.setSelectionRange(0, textarea.value.length);++ try {+ return document.execCommand("copy");+ } catch {+ return false;+ } finally {+ document.body.removeChild(textarea);+ }+}diff --git a/webui/src/tests/message-bubble.test.tsx b/webui/src/tests/message-bubble.test.tsxindex 1caaf0194..060f0374a 100644--- a/webui/src/tests/message-bubble.test.tsx+++ b/webui/src/tests/message-bubble.test.tsx@@ -167,6 +167,72 @@ describe("MessageBubble", () => {);});+ it("copies completed assistant replies with the textarea fallback", async () => {+ Object.defineProperty(navigator, "clipboard", {+ configurable: true,+ value: undefined,+ });+ const execCommand = vi.fn().mockReturnValue(true);+ Object.defineProperty(document, "execCommand", {+ configurable: true,+ value: execCommand,+ });+ const message: UIMessage = {+ id: "a-copy-fallback",+ role: "assistant",+ content: "Fallback copy reply.",+ createdAt: Date.now(),+ };++ try {+ render(<MessageBubble message={message} />);++ fireEvent.click(screen.getByRole("button", { name: "Copy reply" }));++ await waitFor(() => expect(execCommand).toHaveBeenCalledWith("copy"));+ await waitFor(() =>+ expect(screen.getByRole("button", { name: "Copied reply" })).toBeInTheDocument(),+ );+ } finally {+ Reflect.deleteProperty(navigator, "clipboard");+ Reflect.deleteProperty(document, "execCommand");+ }+ });++ it("falls back when the Clipboard API rejects assistant reply copy", async () => {+ const writeText = vi.fn().mockRejectedValue(new Error("not allowed"));+ Object.defineProperty(navigator, "clipboard", {+ configurable: true,+ value: { writeText },+ });+ const execCommand = vi.fn().mockReturnValue(true);+ Object.defineProperty(document, "execCommand", {+ configurable: true,+ value: execCommand,+ });+ const message: UIMessage = {+ id: "a-copy-reject",+ role: "assistant",+ content: "Rejected clipboard copy.",+ createdAt: Date.now(),+ };++ try {+ render(<MessageBubble message={message} />);++ fireEvent.click(screen.getByRole("button", { name: "Copy reply" }));++ expect(writeText).toHaveBeenCalledWith("Rejected clipboard copy.");+ await waitFor(() => expect(execCommand).toHaveBeenCalledWith("copy"));+ await waitFor(() =>+ expect(screen.getByRole("button", { name: "Copied reply" })).toBeInTheDocument(),+ );+ } finally {+ Reflect.deleteProperty(navigator, "clipboard");+ Reflect.deleteProperty(document, "execCommand");+ }+ });+it("does not show copy actions for streaming placeholders", () => {const message: UIMessage = {id: "a-streaming",