Skip to content

fix(frontend): make copy buttons work without the Clipboard API - #1463

Open
openhands-agent wants to merge 2 commits into
mainfrom
dev-coordinator/1462-clipboard-fallback
Open

openhands-agent wants to merge 2 commits into
mainfrom
dev-coordinator/1462-clipboard-fallback

Conversation

@openhands-agent

@openhands-agent openhands-agent commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

In plain words

OSS: The copy buttons copy the text, even when the page is served over plain HTTP.
OHE: On a plain-HTTP install, the copy buttons in chat and in code blocks fail, and so does the share-link copy.

HUMAN:

  • A human has tested these changes.

AGENT:


Why

navigator.clipboard exists only in a secure context. On a page served over plain HTTP from a non-loopback host (for example a Compose install reached by IP), it is undefined, so navigator.clipboard.writeText(...) throws. In OHE's legacy frontend/, three copy controls called it directly: the chat-message copy button, the code-block copy button and the "copy share link" menu entry. They did nothing, and the share-link entry threw before its success toast. OSS fixed this in OpenHands/OpenHands#18583; the Canvas pin bump does not touch these OHE files, so the fix is a port.

Summary

  • Add frontend/src/utils/copy-to-clipboard.ts, ported as-is from OSS main (includes the focus-restoring follow-up). It tries the async Clipboard API, falls back to a hidden textarea + document.execCommand("copy"), and returns whether the copy succeeded.
  • Use it in chat-message.tsx, copyable-content-wrapper.tsx (code blocks) and use-conversation-name-context-menu.ts (share link). "Copied" / the success toast now appear only after a real copy; otherwise the existing CHAT_INTERFACE$CHAT_MESSAGE_COPY_FAILED error toast is shown, as in OSS.
  • Tests: helper unit tests (ported from OSS, including the run with navigator.clipboard removed that checks the fallback copies and returns true), plus fallback and failure tests for all three call sites. The new component tests fail on main and pass here.
before: onClick -> navigator.clipboard.writeText(text)  // throws when clipboard is undefined
after:  onClick -> copyToClipboard(text) -> Clipboard API ? : textarea + execCommand("copy")
                    ok  -> "copied" state / success toast
                    !ok -> error toast

The five settings/admin call sites that also call navigator.clipboard.writeText (API keys, invite link, etc.) have no OSS counterpart in #18583 and are left for a follow-up.

Issue Number

Closes #1462

How to Test

cd frontend && npx vitest run __tests__/utils/copy-to-clipboard.test.ts __tests__/components/buttons/copyable-content-wrapper.test.tsx __tests__/components/chat-message.test.tsx __tests__/components/features/conversation/conversation-name.test.tsx (64 passed locally). npm run lint and npm run build pass.

How to verify on a preview

The preview is served over HTTPS, where the Clipboard API exists, so remove it in the browser first to reproduce a plain-HTTP install.

  1. Log in and open a conversation in the legacy UI (/conversations/<id>, not /canvas). Send: Reply with a short python code block that prints hello. Wait for the agent reply with a code block.
  2. Open DevTools → Console and run:
    Object.defineProperty(navigator, "clipboard", { value: undefined, configurable: true });
    window.__copies = [];
    const _exec = document.execCommand.bind(document);
    document.execCommand = (cmd, ...a) => { if (cmd === "copy") window.__copies.push(document.activeElement?.value); return _exec(cmd, ...a); };
    Check navigator.clipboard → undefined.
  3. Hover your own chat message and click its copy button. Expected: the icon switches to the "copied" check for ~2 s, no uncaught TypeError in the console, and window.__copies ends with your message text.
  4. Hover the code block in the agent reply and click its copy button. Expected: same "copied" state, and window.__copies ends with the code text.
  5. (Optional) Paste with Ctrl/Cmd+V into the chat input: the copied text appears.
  6. Open the conversation-name menu (the … next to the conversation title), turn on Share publicly, then click the copy-share-link icon. Expected: toast "Link copied to clipboard", and window.__copies ends with https://<preview-host>/shared/conversations/<id>.
  7. Failure path: run document.execCommand = () => false; and click a copy button again. Expected: an error toast "Failed to copy message to clipboard" and the button does not switch to "copied".

On main, steps 3, 4 and 6 throw TypeError: Cannot read properties of undefined (reading 'writeText') in the console, the button does not switch to "copied", and no share-link toast appears.

Video/Screenshots

Captured from the staging preview of this head (ghcr.io/openhands/enterprise-server:sha-2e0aed5, preview run c46fbb71) with navigator.clipboard removed and document.execCommand hooked as in step 2 above. Full run: preview verification.

Step Observed Screenshot
1. Chat-message copy, no Clipboard API Button switched to the "copied" check (aria-label "Copied to clipboard"); the hook recorded the message text; no TypeError chat copy
2. Code-block copy, no Clipboard API Button switched to the "copied" check; the hook recorded print("hello") code copy
2b. Paste back into the chat input Ctrl+V inserted print("hello"), so the real clipboard held the text paste
3. Copy share link (Public Share on) Toast "Link copied to clipboard"; the hook recorded https://<preview-host>/shared/conversations/<id> share link toast
4. Failure path (document.execCommand = () => false) Toast "Failed to copy message to clipboard"; the button stayed "Copy to clipboard" failure toast

The "Network Error" / "Request failed with status code 500" in the Changes panel comes from the git-changes panel in a conversation with no repository. It is not related to copy.

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

OSS is still refining the helper in OpenHands/OpenHands#19843; this PR ports the version on OSS main today.

This PR was created by an AI agent (OpenHands dev-coordinator) on behalf of the user.


Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-2e0aed5

Port the copyToClipboard helper from OpenHands/OpenHands#18583 (OSS main
version) and use it for the chat message copy button, the code-block copy
button and the "copy share link" menu entry. On a plain-HTTP, non-loopback
page navigator.clipboard is undefined, so these calls threw.

Closes #1462

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions github-actions Bot added the type: fix A bug fix label Nov 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

This PR does not seem to contain any modification to coverable code.

ChangesTab now pulls in custom-toast-handlers through the code-block copy
wrapper, which initialises i18n with initReactI18next.

Co-authored-by: openhands <openhands@all-hands.dev>
@openhands-agent
openhands-agent marked this pull request as ready for review November 19, 2026 10:36
@openhands-agent openhands-agent added the verify-this Triggers Preview Verification on https://app.beta.staging.all-hands-testing.dev/canvas/automations/ label Nov 19, 2026
@all-hands-bot

all-hands-bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Review posted for 2e0aed5255ee

Verdict: ✅ APPROVED. See the review.

This is an automated status.

@all-hands-bot all-hands-bot 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.

This review was posted by an AI agent (OpenHands).

The code looks correct to me. frontend/src/utils/copy-to-clipboard.ts is a byte-for-byte copy of OSS src/utils/copy-to-clipboard.ts on main, and the three call sites listed in #1462 now use it:

  • chat-message.tsx and copyable-content-wrapper.tsx show "copied" only when copyToClipboard returns true. Otherwise they show the existing CHAT_INTERFACE$CHAT_MESSAGE_COPY_FAILED toast.
  • use-conversation-name-context-menu.ts now shows the success toast only after a real copy. On main, it showed the toast without waiting for the result, and a missing navigator.clipboard threw before the toast.
  • When navigator.clipboard is missing, the helper never awaits before calling execCommand("copy"). So the fallback still runs inside the click's user gesture.

I found no correctness or security problems. The changes-tab.test.tsx mock change is only test setup: the components now call useTranslation, and the mock must keep the real react-i18next exports. All 25 CI checks for 2e0aed5 pass, and 4 are skipped. I also ran the four test files in "How to Test" plus changes-tab.test.tsx locally: 60 of 60 tests passed.

Blocking: no live evidence of the UI change

The copy buttons change visible UI: the "copied" icon state, the success toast and the new error toast. The PR template (Video/Screenshots: "Provide a video or screenshots of testing your PR") and .agents/skills/custom-codereview-guide.md ask for screenshots or a video when a change affects UI behavior. This PR says "N/A (behaviour covered by unit tests; preview steps above)". Unit tests and written steps do not replace a capture of the running app, and the human-tested box is not checked.

Please add screenshots or a short recording from a running build of this head (for example, the preview or ghcr.io/openhands/enterprise-server:sha-2e0aed5). Remove navigator.clipboard first, as in step 2 of "How to verify on a preview". The capture should show:

  1. The chat-message copy button switching to the "copied" check, with the copied text in the clipboard.
  2. The same for the code-block copy button.
  3. The "Link copied to clipboard" toast from the conversation-name menu's copy-share-link entry, with the /shared/conversations/<id> URL copied.
  4. The "Failed to copy message to clipboard" toast when execCommand returns false, with the button not switching to "copied".

This gap is missing evidence, not a code defect. Once the capture is added, I expect this PR to be ready to merge.

🔄 CHANGES REQUESTED

LLM profile: unknown · Model: openhands/claude-opus-5-5

@juanmichelini

Copy link
Copy Markdown
Contributor

🔍 Preview Verification — PASS

Target: saas-deploy PR #2405 App: https://pr-2405.staging.all-hands.dev
Validates: #1463 (head 2e0aed5) — copy buttons (chat message, code block, share link) work without the Clipboard API via a textarea + execCommand("copy") fallback, and show an error toast instead of "copied" when the copy fails.
Timestamp: 2026-11-19T10:58Z
Run ID: c46fbb71-c27f-41a3-b28a-35facabf3752

Result: PASS

Baseline smoke

Step Status Detail
preflight ✅ app -> 200
positive:login ✅ Keycloak login accepted
positive:home ✅ home screen + chat launcher visible
positive:backend ✅ /me 200, role=member
positive:reload ✅ login survived reload
positive:conversation ✅ conversation created
positive:reply ✅ Conversation started in my own session (the smoke's launch token is tab-bound): sandbox_status=RUNNING, execution_status=finished ~40s after send. The reply (a print("hello") code block) is visible in the conversation.
negative:rejected ✅ wrong password rejected by Keycloak

Feature test — clipboard fallback (legacy UI /conversations/<id>)

Reproduced a plain-HTTP install as in the PR's "How to verify on a preview": navigator.clipboard set to undefined (confirmed undefined) and document.execCommand wrapped to record the text of the active element on each copy. Driven with headless Chromium against the preview, logged in as the seeded member.

Step Status Intended effect Observed effect (how verified) Screenshot
Chat-message copy button, no Clipboard API ✅ Text is copied via fallback, button shows "copied", no TypeError Button aria-label went "Copy to clipboard" → "Copied to clipboard" (reverted after ~2s). execCommand("copy") hook recorded exactly the message text. No uncaught errors / unhandled rejections captured. chat copy
Code-block copy button, no Clipboard API ✅ Code text copied, "copied" state Button switched to the check icon / "Copied to clipboard"; hook recorded print("hello"). code copy
Paste copied text (downstream) ✅ Real clipboard holds the copied text Ctrl+V in the chat input inserted print("hello"). paste
Share-link copy (Public Share on, then copy icon) ✅ Success toast only after a real copy; share URL copied Toast "Link copied to clipboard"; hook recorded https://pr-2405.staging.all-hands.dev/shared/conversations/<id>; pasting into the chat input produced the same URL. No error toast for this click. share toast
Failure path (document.execCommand = () => false) ✅ Error toast, button must not switch to "copied" Toast "Failed to copy message to clipboard"; button aria-label stayed "Copy to clipboard". failure

Key screenshots

home
reply visible
share public on
share link toast

Other smoke screenshots: reload conversation negative menu

What was tested

  • Chat-message, code-block and share-link copy with the Clipboard API removed: each copied the right text (execCommand hook + paste-back into the input) and showed the "copied" state / success toast only after the copy.
  • Failure path: with execCommand returning false, the error toast appears and no "copied" state is shown.
  • Baseline auth + UI health + agent reply.

Skipped / manual review

  • None required. Notes: the "before" behaviour on main (TypeError) was not re-run, since the preview runs only this head. The "Changes" panel showed "Network Error" / a "Request failed with status code 500" toast in screenshots; it is the git-changes panel in a conversation with no repo and is unrelated to copy. The test conversation was left with Public Share on.

Evidence

Full log: evidence/run-log.json


Generated by an AI agent (OpenHands).

@juanmichelini juanmichelini removed the verify-this Triggers Preview Verification on https://app.beta.staging.all-hands-testing.dev/canvas/automations/ label Nov 19, 2026
@openhands-agent openhands-agent added the verify-this Triggers Preview Verification on https://app.beta.staging.all-hands-testing.dev/canvas/automations/ label Nov 19, 2026
@openhands-agent

Copy link
Copy Markdown
Contributor Author

Addressed the all-hands-bot review on 2e0aed5. It asked for live evidence of the UI change.

  • No code change. The review found no defects, and CI on 2e0aed5 is green (25 passed, 6 skipped).
  • I replaced "N/A" in the PR description's Video/Screenshots section with screenshots from the staging preview of this head, with the Clipboard API removed. They cover the four captures the review asked for: chat-message copy, code-block copy (plus paste-back), the "Link copied to clipboard" toast, and the failure toast with no "copied" state. Source: the preview verification run.
  • Asked all-hands-bot for a new review and re-applied verify-this.

Note for the coordinator: the evidence linked as the latest verification is the SaaS preview run (target saas-deploy #2405), and it passed. I found no fleet-VM (verify-this-on-vm) run for this PR and no self-hosted failure report, so there was nothing self-hosted-specific to fix. The change is frontend-only. When the Clipboard API is missing, it falls back to a textarea plus execCommand("copy"), which is the plain-HTTP self-hosted case this PR targets.

This comment was posted by an AI agent (OpenHands dev-coordinator) on behalf of the user.

@all-hands-bot all-hands-bot 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.

This review was posted by an AI agent (OpenHands).

The head is still 2e0aed5, so the code is the same as in my last review. That review had one blocking finding: there was no live evidence of the UI change. This finding is now resolved.

The Video/Screenshots section now has captures from the staging preview of this head (preview run c46fbb71, verification comment). I downloaded and checked each screenshot. They come from the running legacy conversation UI with navigator.clipboard removed, and they cover all four captures I asked for:

  1. The chat-message copy button shows the "copied" check.
  2. The code-block copy button shows the "copied" check. Pasting into the chat input then gives print("hello"), so the text really reached the clipboard.
  3. The copy-share-link entry with Public Share on shows the "Link copied to clipboard" toast.
  4. When execCommand returns false, the "Failed to copy message to clipboard" toast appears and the button stays "Copy to clipboard".

The "Network Error" and the 500 toast in the screenshots come from the Changes panel of a conversation with no repository. They are not related to this change. CI on this head has 25 checks passed and 6 skipped. The focused tests also passed locally (60 of 60).

I have no open findings.

✅ APPROVED

LLM profile: unknown · Model: openhands/claude-opus-5-5

@juanmichelini

Copy link
Copy Markdown
Contributor

🔍 Preview Verification — FAIL

Target: saas-deploy PR #2408 App: https://pr-2408.staging.all-hands.dev
Validates: #1463 (head 2e0aed5): copy buttons (chat message, code block, share link) should work when navigator.clipboard is missing.
Timestamp: 2026-11-19T12:35Z
Run ID: 164af3bd-7f35-4a5a-a2f7-564d13c54909

Result: FAIL

The preview does not behave as the fix describes. With the Clipboard API removed (the PR's own repro), the chat-message and code-block copy buttons still throw TypeError: Cannot read properties of undefined (reading 'writeText'). The served frontend JS has no fallback code. Baseline smoke passed.

Baseline smoke

Step Status Detail
preflight ✅ app -> 200
positive:login ✅ Keycloak login accepted
positive:home ✅ home screen + chat launcher visible
positive:backend ✅ /me 200, role=member
positive:reload ✅ login survived reload
positive:conversation ✅ conversation created
positive:reply ✅ execution_status=finished, sandbox_status=RUNNING on the first poll (about 2 min after creation). The agent's reply was visible in the legacy UI.
negative:rejected ✅ wrong-password login rejected by Keycloak

Feature test — copy buttons without the Clipboard API

Procedure (PR "How to verify on a preview"): legacy UI /conversations/<id>. Sent "Reply with a short python code block that prints hello", then waited for execution_status=finished. The agent replied with a code block in about 14s. Then ran the PR's console setup (navigator.clipboard set to undefined, document.execCommand("copy") hooked to record window.__copies, unhandledrejection listener added).

Step Status Intended effect Observed effect (how verified) Screenshot
Chat-message copy, no Clipboard API ❌ Button switches to "copied"; window.__copies gets the message text; no TypeError After the click, the button aria-label was still "Copy to clipboard". window.__copies was []. The page logged an unhandled rejection TypeError: Cannot read properties of undefined (reading 'writeText'). Verified through the page's JS state. chat copy
Code-block copy, no Clipboard API ❌ Same "copied" state; __copies ends with print("hello") aria-label unchanged, __copies still [], a second identical TypeError was logged. code copy
Served bundle contains the fix ❌ Chunks served by the preview use the copyToClipboard helper (textarea + execCommand) The messages-*.js chunk still has await navigator.clipboard.writeText(e),i(!0) in both the chat-message and code-block copy components. The conversation-*.js chunk still has handleCopyShareLink: ... navigator.clipboard.writeText(url), toast(...). CHAT_MESSAGE_COPY_FAILED is only defined in the i18n declaration and never used by a copy handler. These are the pre-fix patterns described in the PR. n/a
Share-link copy (Public Share, then copy icon) ⏭️ Toast "Link copied to clipboard" and __copies ends with the share URL Not exercised. Turning on Public Share sent PATCH /api/v1/app-conversations/<id> with {"public":true}, which returned 502. The conversation stayed public: null and the copy-share-link button never appeared. Missing prerequisite: a working public-share toggle on this preview. The bundle check above shows the handler is unpatched. menu
Failure path (execCommand = () => false gives error toast) ⏭️ Error toast "Failed to copy message to clipboard", no "copied" state Not run. The success path already fails and the bundle has no copyToClipboard helper, so this path does not exist in what is served. n/a
Paste-back into the chat input (optional) ⏭️ Pasted text appears Not run. Nothing was copied in the earlier steps. n/a

Key screenshots

home
baseline reply
reply with code block
chat copy, no clipboard

What was tested

  • Baseline auth, home, /me, reload persistence, conversation creation and agent reply (API execution_status plus visible reply), and negative login.
  • Chat-message and code-block copy with navigator.clipboard removed: the original failure (TypeError, no "copied" state, nothing copied) still reproduces on the preview.
  • Static check of the JS chunks served by the preview: the pre-fix navigator.clipboard.writeText calls are still present in the chat-message, code-block and share-link handlers.

Skipped / manual review

  • Share-link copy and the failure-path toast were not exercised. The public-share toggle returned 502 on this preview, and there is no fallback code in the served build to test.
  • The preview's deployed image = sha-2e0aed5 claim could not be confirmed from outside. The served frontend does not contain the PR's changes. Please check that the deployed enterprise-server image really includes the frontend/ changes from this head. Possible causes are a stale frontend asset layer or a cached build. Then re-run the verification.

Evidence

Full log: evidence/run-log.json (baseline smoke). Feature-test screenshots are attached above.


Generated by an AI agent (OpenHands).

@juanmichelini juanmichelini removed the verify-this Triggers Preview Verification on https://app.beta.staging.all-hands-testing.dev/canvas/automations/ label Nov 19, 2026
@openhands-agent

Copy link
Copy Markdown
Contributor Author

The verification FAIL comes from the preview setup, not from this PR's code. I made no code change. CI on 2e0aed5 is green and all-hands-bot approved it.

What happened. The FAIL run (saas-deploy #2408) did not run this PR's image.

What I checked. I pulled the frontend layer of both images from ghcr.io:

Image messages-*.js
enterprise-server:sha-2e0aed5… (this PR) has the execCommand("copy") fallback
enterprise-server:sha-74daea7 (#1461, staging pin at the time) has await navigator.clipboard.writeText(e),i(!0), the exact pattern the verifier reported, and no fallback

Self-hosted. The change is frontend-only. OHE serves the same frontend/build that is inside this image. The fallback is for the plain-HTTP case, where navigator.clipboard is missing, and that is the self-hosted case.

Why the PR is blocked. Staging still has an openhands.image.tag pin (now sha-9773b7c, for #1461). So if I add verify-this again, the next run will most likely deploy the wrong image again and report a FAIL that is not real. I cannot fix that from this repository.

What a human needs to do. Choose one:

  1. Fix the verify-this automation so that it sets tag: inside the existing openhands.image mapping and does not add a second image: key.
  2. Wait until the chore(logging): add repo-relative file path to JSON logs #1461 staging pin is removed.

Then add verify-this again. Nothing else on this PR needs to change.

This comment was posted by an AI agent (OpenHands dev-coordinator) on behalf of the user.

@openhands-agent openhands-agent added the blocked - agent is stuck dev-coordinator: fix or new-PR attempts ran out, or a worker lacks a credential or environment label Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocked - agent is stuck dev-coordinator: fix or new-PR attempts ran out, or a worker lacks a credential or environment type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[OSS→OHE parity] Make OHE's copy buttons work without the Clipboard API

3 participants