Multiplayer agent harness for work.
Toggle a class for copy feedback instead of rewriting lit-owned buttons (#1306)
Sina Matianb2be41649fa1
diff --git a/plugins/web-ui/src/chat.ts b/plugins/web-ui/src/chat.tsindex b86062971..59df43bf4 100644--- a/plugins/web-ui/src/chat.ts+++ b/plugins/web-ui/src/chat.ts@@ -126,7 +126,7 @@ import {harnessSupportsEffort,harnessSupportsFastMode,} from "./model-options";-import { browserRenderableImage, chipBadge, formatBytes, icon, relTime, waveLoader } from "./ui";+import { browserRenderableImage, chipBadge, copyText, formatBytes, icon, relTime, waveLoader } from "./ui";import { appState, renderSidebarTop, switchView, syncUrlFromState } from "./shell";import { contextsState, scopeTitle } from "./contexts";import { openProjectPage, scopeToolCount, sessionTopbarTpl, setScopedSession } from "./session-scope";@@ -1719,9 +1719,9 @@ export function createChatSurface(type="button"${tip("Copy")}aria-label="Copy message"- @click=${(e: Event) => void copyMessage(text, e.currentTarget as HTMLButtonElement)}+ @click=${(e: Event) => void copyText(text, e.currentTarget as HTMLButtonElement)}>- ${icon(Copy, 13)}+ ${icon(Copy, 13)}${icon(Check, 13)}</button>`: nothing}@@ -1789,21 +1789,6 @@ export function createChatSurface(}}- async function copyMessage(text: string, btn: HTMLButtonElement): Promise<void> {- try {- await navigator.clipboard.writeText(text);- } catch {- return;- }- btn.classList.add("copied");- btn.replaceChildren(icon(Check, 13));- setTimeout(() => {- if (!btn.isConnected) return;- btn.classList.remove("copied");- btn.replaceChildren(icon(Copy, 13));- }, 1200);- }-function withReturnTo(url: string): string {const returnTo = deepLinkPath(UI_BASE, "chats", chatState.sessionId);const sep = url.includes("?") ? "&" : "?";diff --git a/plugins/web-ui/src/shared-session.ts b/plugins/web-ui/src/shared-session.tsindex 078b4cb66..9e3c07872 100644--- a/plugins/web-ui/src/shared-session.ts+++ b/plugins/web-ui/src/shared-session.ts@@ -1,7 +1,7 @@import "./shell.css";import "@mariozechner/mini-lit/dist/ThemeToggle.js";import { html, render } from "lit";-import { Lock, ArrowUpRight, Copy, File, FileImage } from "lucide";+import { Lock, ArrowUpRight, Check, Copy, File, FileImage } from "lucide";import { createTranscriptViewport } from "./transcript-viewport";import { decorateTextCodeBlocks } from "./text-code";import { markdown } from "./message-markdown";@@ -75,9 +75,9 @@ render(</div>`: ""}- ${message.role === "assistant" ? html`<div class="message-meta"><button class="msg-copy" aria-label="Copy message" title="Copy" @click=${(e: Event) => void copyText(message.text, e.currentTarget as HTMLButtonElement)}>${icon(Copy, 13)}</button></div>` : ""}+ ${message.role === "assistant" ? html`<div class="message-meta"><button class="msg-copy" aria-label="Copy message" title="Copy" @click=${(e: Event) => void copyText(message.text, e.currentTarget as HTMLButtonElement)}>${icon(Copy, 13)}${icon(Check, 13)}</button></div>` : ""}</div>- ${message.role === "user" ? html`<div class="message-meta"><button class="msg-copy" aria-label="Copy message" title="Copy" @click=${(e: Event) => void copyText(message.text, e.currentTarget as HTMLButtonElement)}>${icon(Copy, 13)}</button></div>` : ""}+ ${message.role === "user" ? html`<div class="message-meta"><button class="msg-copy" aria-label="Copy message" title="Copy" @click=${(e: Event) => void copyText(message.text, e.currentTarget as HTMLButtonElement)}>${icon(Copy, 13)}${icon(Check, 13)}</button></div>` : ""}</article>`,)diff --git a/plugins/web-ui/src/shell.css b/plugins/web-ui/src/shell.cssindex 05e431aa9..5f3f98fb1 100644--- a/plugins/web-ui/src/shell.css+++ b/plugins/web-ui/src/shell.css@@ -1799,6 +1799,10 @@ button.subagent-chip:hover {.msg-copy.copied {color: var(--foreground);}+.msg-copy.copied > svg:first-of-type,+.msg-copy:not(.copied) > svg:nth-of-type(2) {+ display: none;+}@media (prefers-reduced-motion: reduce) {.message-meta {transition: none;@@ -4388,6 +4392,12 @@ qm-markdown .tok-in {.btn:hover {background: var(--secondary);}+.btn.copied > * {+ display: none;+}+.btn.copied::after {+ content: "Copied";+}.btn.primary {background: var(--cta);color: var(--cta-foreground);diff --git a/plugins/web-ui/src/ui.ts b/plugins/web-ui/src/ui.tsindex 2adec7a8d..1135c6bd5 100644--- a/plugins/web-ui/src/ui.ts+++ b/plugins/web-ui/src/ui.ts@@ -335,25 +335,21 @@ export function browserRenderableImage(mimeType?: string): boolean {return RENDERABLE_IMAGE_TYPES.has((mimeType ?? "").split(";")[0]!.trim().toLowerCase());}-const copyFeedback = new WeakMap<HTMLButtonElement, { html: string; timer: ReturnType<typeof setTimeout> }>();+const copyFeedback = new WeakMap<HTMLButtonElement, ReturnType<typeof setTimeout>>();export async function copyText(text: string, btn?: HTMLButtonElement): Promise<void> {try {await navigator.clipboard.writeText(text);- if (btn) {- const active = copyFeedback.get(btn);- if (active) clearTimeout(active.timer);- const html = active?.html ?? btn.innerHTML;- btn.textContent = "Copied";- const timer = setTimeout(() => {- btn.innerHTML = html;- copyFeedback.delete(btn);- }, 1200);- copyFeedback.set(btn, { html, timer });- }} catch {- void 0;+ return;}+ if (!btn) return;+ clearTimeout(copyFeedback.get(btn));+ btn.classList.add("copied");+ copyFeedback.set(+ btn,+ setTimeout(() => btn.classList.remove("copied"), 1200),+ );}export function actionSnippet(action: string): string {diff --git a/plugins/web-ui/src/webhooks.ts b/plugins/web-ui/src/webhooks.tsindex 1607f5799..a8da2d486 100644--- a/plugins/web-ui/src/webhooks.ts+++ b/plugins/web-ui/src/webhooks.ts@@ -202,7 +202,7 @@ function copyRow(text: string) {<div class="copyrow"><code class="mono">${text}</code><button class="btn" @click=${(e: Event) => void copyText(text, e.currentTarget as HTMLButtonElement)}>- Copy+ <span>Copy</span></button></div>`;diff --git a/plugins/web-ui/test/copy-button-lit.test.ts b/plugins/web-ui/test/copy-button-lit.test.tsnew file mode 100644index 000000000..1ea274d1d--- /dev/null+++ b/plugins/web-ui/test/copy-button-lit.test.ts@@ -0,0 +1,67 @@+import assert from "node:assert/strict";+import { readFileSync } from "node:fs";+import test, { type TestContext } from "node:test";+import { JSDOM } from "jsdom";+import type { TemplateResult } from "lit";++const dom = new JSDOM("<!doctype html><body></body>", { url: "http://localhost/" });+const globals = {+ window: dom.window,+ document: dom.window.document,+ navigator: { clipboard: { writeText: async () => {} } },+ HTMLElement: dom.window.HTMLElement,+ Node: dom.window.Node,+ Event: dom.window.Event,+ customElements: dom.window.customElements,+};+for (const [key, value] of Object.entries(globals))+ Object.defineProperty(globalThis, key, { configurable: true, writable: true, value });++const { html, render } = await import("lit");+const { Check, Copy } = await import("lucide");+const { copyText, icon } = await import("../src/ui.ts");+const css = readFileSync(new URL("../src/shell.css", import.meta.url), "utf8");++const copy = (event: Event) => void copyText("hello", event.currentTarget as HTMLButtonElement);+const flush = () => new Promise((resolve) => setImmediate(resolve));++async function copyThenRerender(t: TestContext, view: () => TemplateResult): Promise<HTMLButtonElement> {+ t.mock.timers.enable({ apis: ["setTimeout"] });+ const host = document.createElement("div");+ render(view(), host);+ const button = host.querySelector("button")!;+ button.click();+ await flush();+ render(view(), host);+ assert.equal(host.querySelector("button"), button, "re-rendering keeps the same button");+ assert.ok(button.classList.contains("copied"), "the copied state is a class on the button");+ return button;+}++test("copying from an icon-only message button survives the next lit render", async (t) => {+ const button = await copyThenRerender(+ t,+ () => html`<button class="msg-copy" @click=${copy}>${icon(Copy, 13)}${icon(Check, 13)}</button>`,+ );+ assert.equal(button.querySelectorAll("svg").length, 2, "both icons stay rendered by lit");+ t.mock.timers.tick(1200);+ assert.ok(!button.classList.contains("copied"));+});++test("copying from a labelled button survives the next lit render", async (t) => {+ const button = await copyThenRerender(+ t,+ () => html`<button class="btn" @click=${copy}>${icon(Copy, 14)}<span>Copy URL</span></button>`,+ );+ assert.equal(button.querySelectorAll("svg").length, 1);+ assert.equal(button.querySelector("span")?.textContent, "Copy URL");+});++test("the copied state is drawn by CSS instead of rewriting button children", () => {+ assert.match(+ css,+ /\.msg-copy\.copied > svg:first-of-type,\s*\.msg-copy:not\(\.copied\) > svg:nth-of-type\(2\) \{\s*display: none;/,+ );+ assert.match(css, /\.btn\.copied > \* \{\s*display: none;/);+ assert.match(css, /\.btn\.copied::after \{\s*content: "Copied";/);+});diff --git a/plugins/web-ui/test/ui-copy.test.ts b/plugins/web-ui/test/ui-copy.test.tsindex 6f3ff1e41..f2bb7d389 100644--- a/plugins/web-ui/test/ui-copy.test.ts+++ b/plugins/web-ui/test/ui-copy.test.ts@@ -31,7 +31,8 @@ test("brand name follows the server-injected deployment label", () => {}});-test("rapid copy feedback restores the original button markup once", async () => {+test("rapid copy feedback toggles a class and never rewrites the button markup", async (t) => {+ t.mock.timers.enable({ apis: ["setTimeout"] });const navigatorDescriptor = Object.getOwnPropertyDescriptor(globalThis, "navigator");Object.defineProperty(globalThis, "navigator", {configurable: true,@@ -44,9 +45,12 @@ test("rapid copy feedback restores the original button markup once", async () =>const original = button.innerHTML;await copyText("first", button);await copyText("second", button);- assert.equal(button.textContent, "Copied");- await new Promise((resolve) => setTimeout(resolve, 1250));+ assert.ok(button.classList.contains("copied"));assert.equal(button.innerHTML, original);+ t.mock.timers.tick(1199);+ assert.ok(button.classList.contains("copied"), "the second copy restarts the feedback window");+ t.mock.timers.tick(1);+ assert.ok(!button.classList.contains("copied"));} finally {if (navigatorDescriptor) Object.defineProperty(globalThis, "navigator", navigatorDescriptor);else delete (globalThis as { navigator?: Navigator }).navigator;