fix: make copy buttons work over plain http and surface the restart command

`navigator.clipboard` only exists in a secure context, so it is undefined
over plain http — which is how T3 is reached on a LAN or Tailscale host.
Every copy button on such an origin threw before copying, so nothing landed
on the clipboard and no success state ever rendered. That included the
button handing over a pairing command the user cannot reasonably retype.

Fall back to the legacy execCommand path when the async API is missing, and
also when a secure-context write is rejected, since a permissions-policy or
user-activation refusal can still succeed the old way. Focus restore is
duck-typed rather than `instanceof HTMLElement`: that global is absent in
non-DOM environments, where a bare reference throws instead of being falsy,
and losing focus is never worth failing a copy over.

The restart step spelled its command inline as prose while the two steps
above it had copy buttons, so the one command that activates the pairing was
the one you had to retype. Give it the same treatment, and say plainly that
running in the foreground is expected without an installed service, since it
looks like a hang.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
Ben DavisandClaude Opus 5 committed 2026-07-24 23:50:20 -07:00
1 parent e78baddda1
commit 422cf087ee
4 files changed
+172 -17

No files matched your search

@@ -41,6 +41,12 @@ export function createHermesProviderInstanceId(label: string, randomUuid: () =>
*/
export const HERMES_PLUGIN_INSTALL_COMMAND = "./integrations/hermes-t3-gateway/install.sh" as const;
/**
* Activates the pairing. The gateway only constructs the platform adapter at
* startup, so it keeps reporting offline until it is restarted.
*/
export const HERMES_GATEWAY_RESTART_COMMAND = "hermes gateway restart" as const;
export function isHermesInstanceRemovedError(error: unknown): boolean {
return (
typeof error === "object" &&
@@ -37,6 +37,7 @@ import { AnimatedHeight } from "../AnimatedHeight";
import {
ADD_PROVIDER_WIZARD_STEPS,
createHermesProviderInstanceId,
HERMES_GATEWAY_RESTART_COMMAND,
HERMES_PLUGIN_INSTALL_COMMAND,
isHermesInstanceRemovedError,
isOwnedHermesEnrollmentRetry,
@@ -208,6 +209,16 @@ export function AddProviderInstanceDialog({ open, onOpenChange }: AddProviderIns
description: "Run it from the T3 Code checkout on the Hermes host.",
}),
});
const { copyToClipboard: copyRestartCommand, isCopied: isRestartCommandCopied } =
useCopyToClipboard({
target: "Hermes gateway restart command",
onCopy: () =>
toastManager.add({
type: "success",
title: "Restart command copied",
description: "Run it on the Hermes host to activate the pairing.",
}),
});
// Rollback of a failed Hermes enrollment must survive the dialog closing
// mid-failure, so the pending undo lives in a ref that an unmount-safe
@@ -675,10 +686,31 @@ export function AddProviderInstanceDialog({ open, onOpenChange }: AddProviderIns
3. Restart the gateway on that host
</p>
<p className="text-[11px] text-muted-foreground">
Run <code>hermes gateway restart</code>. This step is required — the plugin
only dials T3 Code after the restart, so nothing is wrong while this panel
still says {hermesGatewayStatusLabel(hermesWaitingState.statusLabelState)}.
This page updates itself the moment Hermes connects.
This step is required — the plugin only dials T3 Code after the restart, so
nothing is wrong while this panel still says{" "}
{hermesGatewayStatusLabel(hermesWaitingState.statusLabelState)}. This page
updates itself the moment Hermes connects.
</p>
<div className="flex min-w-0 items-center gap-2">
<code className="min-w-0 flex-1 overflow-x-auto whitespace-nowrap rounded bg-muted px-2 py-2 text-[11px]">
{HERMES_GATEWAY_RESTART_COMMAND}
</code>
<Button
type="button"
size="icon-sm"
variant="outline"
onClick={() =>
copyRestartCommand(HERMES_GATEWAY_RESTART_COMMAND, undefined)
}
aria-label="Copy Hermes gateway restart command"
>
{isRestartCommandCopied ? <CheckIcon /> : <CopyIcon />}
</Button>
</div>
<p className="text-[11px] text-muted-foreground">
Without an installed service this runs in the foreground and keeps the
terminal — that is expected. Use <code>hermes gateway install</code> to run
it as a background service instead.
</p>
</li>
</ol>
@@ -55,4 +55,72 @@ describe("writeTextToClipboard", () => {
await expect(writeTextToClipboard("", "plan")).resolves.toBe(false);
expect(writeText).not.toHaveBeenCalled();
});
// `navigator.clipboard` only exists in a secure context, so it is undefined
// over plain http — which is how T3 is reached on a LAN or Tailscale host.
// Without the legacy fallback every copy button on such an origin silently
// does nothing, including the one handing over a pairing command.
it("falls back to execCommand in an insecure context", async () => {
const execCommand = vi.fn(() => true);
vi.stubGlobal("window", {});
vi.stubGlobal("navigator", {});
const appended: Array<Record<string, unknown>> = [];
const makeElement = () => {
const element: Record<string, unknown> = {
style: {},
value: "",
setAttribute: () => {},
select: () => {},
setSelectionRange: () => {},
remove: () => {},
};
return element;
};
vi.stubGlobal("document", {
createElement: () => {
const element = makeElement();
appended.push(element);
return element;
},
body: { appendChild: () => {} },
activeElement: null,
execCommand,
});
await expect(writeTextToClipboard("hermes t3 connect --url x", "command")).resolves.toBe(true);
expect(execCommand).toHaveBeenCalledWith("copy");
});
it("falls back to execCommand when a secure-context write is rejected", async () => {
const writeText = vi.fn(() => Promise.reject(new Error("denied")));
const execCommand = vi.fn(() => true);
vi.stubGlobal("window", {});
vi.stubGlobal("navigator", { clipboard: { writeText } });
const appended: Array<Record<string, unknown>> = [];
const makeElement = () => {
const element: Record<string, unknown> = {
style: {},
value: "",
setAttribute: () => {},
select: () => {},
setSelectionRange: () => {},
remove: () => {},
};
return element;
};
vi.stubGlobal("document", {
createElement: () => {
const element = makeElement();
appended.push(element);
return element;
},
body: { appendChild: () => {} },
activeElement: null,
execCommand,
});
await expect(writeTextToClipboard("value", "command")).resolves.toBe(true);
expect(writeText).toHaveBeenCalled();
expect(execCommand).toHaveBeenCalledWith("copy");
});
});
+62 -13
View File
@@ -24,12 +24,51 @@ export class ClipboardWriteError extends Schema.TaggedErrorClass<ClipboardWriteE
}
}
/**
* Copy via the legacy `document.execCommand` path.
*
* `navigator.clipboard` only exists in a secure context, so it is undefined
* over plain http — which is exactly how T3 is reached on a LAN or Tailscale
* host. Without this fallback every copy button on such an origin silently
* does nothing, including the ones handing over a pairing command the user
* cannot reasonably retype.
*/
function writeTextWithExecCommand(value: string): boolean {
if (typeof document === "undefined") return false;
const textarea = document.createElement("textarea");
textarea.value = value;
// Keep it off-screen and non-interactive, but still focusable: a hidden or
// display:none element cannot be selected, which is what execCommand needs.
textarea.setAttribute("readonly", "");
textarea.setAttribute("aria-hidden", "true");
textarea.style.position = "fixed";
textarea.style.top = "-9999px";
textarea.style.opacity = "0";
const previouslyFocused = document.activeElement;
document.body.appendChild(textarea);
try {
textarea.select();
textarea.setSelectionRange(0, value.length);
return document.execCommand("copy");
} catch {
return false;
} finally {
textarea.remove();
// Duck-typed rather than `instanceof HTMLElement`: the global is absent in
// non-DOM environments, where a bare reference throws instead of being
// falsy — and losing focus is never worth failing a copy over.
const restoreFocus = (previouslyFocused as unknown as { focus?: unknown } | null)?.focus;
if (typeof restoreFocus === "function") {
restoreFocus.call(previouslyFocused);
}
}
}
export async function writeTextToClipboard(value: string, target = "text") {
if (
typeof window === "undefined" ||
typeof navigator === "undefined" ||
!navigator.clipboard?.writeText
) {
const hasClipboardApi = typeof navigator !== "undefined" && !!navigator.clipboard?.writeText;
// `document` is only needed for the legacy fallback, so it is not required
// when the async Clipboard API is present.
if (typeof window === "undefined" || (!hasClipboardApi && typeof document === "undefined")) {
throw new ClipboardApiUnavailableError({
target,
});
@@ -37,15 +76,25 @@ export async function writeTextToClipboard(value: string, target = "text") {
if (!value) return false;
try {
await navigator.clipboard.writeText(value);
return true;
} catch (cause) {
throw new ClipboardWriteError({
target,
cause,
});
if (hasClipboardApi) {
try {
await navigator.clipboard.writeText(value);
return true;
} catch (cause) {
// A rejected write (permissions policy, no user activation) can still
// succeed through the legacy path, so fall through rather than fail.
if (writeTextWithExecCommand(value)) return true;
throw new ClipboardWriteError({
target,
cause,
});
}
}
if (writeTextWithExecCommand(value)) return true;
throw new ClipboardApiUnavailableError({
target,
});
}
export function useCopyToClipboard<TContext = void>({