Skip to content

πŸ™Œ OpenHands: AI-Driven Development

[codex] Add copy action to conversation error banner (#1432)

Graham Neubig25496fd66306
diff --git a/__tests__/components/chat/error-message-banner.test.tsx b/__tests__/components/chat/error-message-banner.test.tsx
index 251be62daa..1535d793dd 100644
--- a/__tests__/components/chat/error-message-banner.test.tsx
+++ b/__tests__/components/chat/error-message-banner.test.tsx
@@ -1,9 +1,22 @@
-import { render, screen } from "@testing-library/react";
+import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
-import { describe, expect, it, vi } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import { ErrorMessageBanner } from "#/components/features/chat/error-message-banner";
+const toastMocks = vi.hoisted(() => ({
+ displayErrorToast: vi.fn(),
+}));
+
+vi.mock("#/utils/custom-toast-handlers", () => ({
+ displayErrorToast: toastMocks.displayErrorToast,
+}));
+
describe("ErrorMessageBanner", () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ toastMocks.displayErrorToast.mockClear();
+ });
+
it("calls onDismiss when the close button is clicked", async () => {
const user = userEvent.setup();
const onDismiss = vi.fn();
@@ -64,6 +77,42 @@ describe("ErrorMessageBanner", () => {
expect(toggle).toHaveTextContent("COMMON$VIEW_LESS");
});
+ it("copies the full error message when the copy button is clicked", async () => {
+ const user = userEvent.setup();
+ const longMessage = `first line\n${"long error detail ".repeat(40)}`;
+ const writeText = vi.fn().mockResolvedValue(undefined);
+ vi.spyOn(navigator.clipboard, "writeText").mockImplementation(writeText);
+
+ render(<ErrorMessageBanner message={longMessage} onDismiss={vi.fn()} />);
+
+ await user.click(screen.getByTestId("error-message-banner-copy"));
+
+ expect(writeText).toHaveBeenCalledWith(longMessage);
+ await waitFor(() =>
+ expect(
+ screen.getByTestId("error-message-banner-copy"),
+ ).toHaveAccessibleName("BUTTON$COPIED"),
+ );
+ });
+
+ it("shows an error toast when the copy button cannot write to the clipboard", async () => {
+ const user = userEvent.setup();
+ const writeText = vi.fn().mockRejectedValue(new Error("denied"));
+ vi.spyOn(navigator.clipboard, "writeText").mockImplementation(writeText);
+
+ render(<ErrorMessageBanner message="copy failed" />);
+
+ await user.click(screen.getByTestId("error-message-banner-copy"));
+
+ expect(writeText).toHaveBeenCalledWith("copy failed");
+ expect(toastMocks.displayErrorToast).toHaveBeenCalledWith(
+ "CHAT_INTERFACE$CHAT_MESSAGE_COPY_FAILED",
+ );
+ expect(
+ screen.getByTestId("error-message-banner-copy"),
+ ).toHaveAccessibleName("BUTTON$COPY");
+ });
+
it("renders a code-specific header for a known ACP error code", () => {
render(
<ErrorMessageBanner
@@ -75,9 +124,9 @@ describe("ErrorMessageBanner", () => {
const header = screen.getByTestId("error-message-banner-header");
expect(header).toHaveTextContent("ERROR$ACP_AUTH_REQUIRED_TITLE");
// The detail is still shown verbatim below the header.
- expect(screen.getByTestId("error-message-banner-content")).toHaveTextContent(
- "invalid x-api-key",
- );
+ expect(
+ screen.getByTestId("error-message-banner-content"),
+ ).toHaveTextContent("invalid x-api-key");
});
it("renders no header for an unknown or absent code", () => {
diff --git a/src/components/features/chat/error-message-banner.tsx b/src/components/features/chat/error-message-banner.tsx
index 6f2e7ba8bd..888f0fa116 100644
--- a/src/components/features/chat/error-message-banner.tsx
+++ b/src/components/features/chat/error-message-banner.tsx
@@ -1,8 +1,9 @@
import React from "react";
import { Trans, useTranslation } from "react-i18next";
-import { CircleX, X } from "lucide-react";
+import { Check, CircleX, Copy, X } from "lucide-react";
import { OH_STATUS_ERROR_COLOR } from "#/constants/status-colors";
import { I18nKey } from "#/i18n/declaration";
+import { displayErrorToast } from "#/utils/custom-toast-handlers";
import { getAcpErrorHeaderKey } from "#/utils/acp-error-codes";
import { cn } from "#/utils/utils";
@@ -29,6 +30,7 @@ export function ErrorMessageBanner({
const headerKey = getAcpErrorHeaderKey(code);
const [isExpanded, setIsExpanded] = React.useState(false);
const [isMultiLine, setIsMultiLine] = React.useState(false);
+ const [isCopied, setIsCopied] = React.useState(false);
const contentRef = React.useRef<HTMLDivElement>(null);
const isI18nKey = i18n.exists(message, { ns: "openhands" });
@@ -38,6 +40,29 @@ export function ErrorMessageBanner({
const isCollapsed = shouldShowToggle && !isExpanded;
+ const handleCopy = async () => {
+ try {
+ await navigator.clipboard.writeText(displayTextForLength);
+ setIsCopied(true);
+ } catch {
+ displayErrorToast(t(I18nKey.CHAT_INTERFACE$CHAT_MESSAGE_COPY_FAILED));
+ }
+ };
+
+ React.useEffect(() => {
+ if (!isCopied) return undefined;
+
+ const timeout = setTimeout(() => {
+ setIsCopied(false);
+ }, 2000);
+
+ return () => clearTimeout(timeout);
+ }, [isCopied]);
+
+ React.useEffect(() => {
+ setIsCopied(false);
+ }, [displayTextForLength]);
+
React.useLayoutEffect(() => {
const content = contentRef.current;
if (!content) {
@@ -141,6 +166,20 @@ export function ErrorMessageBanner({
</button>
)}
+ <button
+ type="button"
+ onClick={handleCopy}
+ className="shrink-0 cursor-pointer rounded-md p-1 text-[var(--oh-muted)] hover:bg-[var(--oh-interactive-hover)] hover:text-[var(--oh-foreground)]"
+ aria-label={t(isCopied ? I18nKey.BUTTON$COPIED : I18nKey.BUTTON$COPY)}
+ data-testid="error-message-banner-copy"
+ >
+ {isCopied ? (
+ <Check className="h-4 w-4" aria-hidden />
+ ) : (
+ <Copy className="h-4 w-4" aria-hidden />
+ )}
+ </button>
+
{onDismiss && (
<button
type="button"