diff --git a/apps/server/src/guide/topics.ts b/apps/server/src/guide/topics.ts index 3dbeb8dd0..b23072be6 100644 --- a/apps/server/src/guide/topics.ts +++ b/apps/server/src/guide/topics.ts @@ -532,7 +532,7 @@ The ↓ count is only as fresh as the last fetch, and {place:sourceControl.fetch id: "pages-and-command-requests", title: "Agent pages and command requests", keywords: ["page", "chart", "diagram", "mockup", "run command", "card", "artifact"], - body: `With {place:threads.agentPages} on, an agent can show a chart, diagram, mockup or document as a page in its reply. {place:threads.claudeArtifacts} lets Claude also publish one to claude.ai for a link you can share. + body: `With {place:threads.agentPages} on, an agent can show a chart, diagram, mockup or document as a page in its reply. Select text in a page to copy it or add a note on it for your next message, as in the chat's own text. {place:threads.claudeArtifacts} lets Claude also publish one to claude.ai for a link you can share. With {place:threads.commandRequests} on, an agent can ask you to run a command it can't run itself (one that needs your password or sign-in, or a long-running server): a card in the chat with Run, Put in terminal and Skip, and the output comes back to the agent.`, }, diff --git a/apps/web/src/components/chat/AgentPageFrame.browser.tsx b/apps/web/src/components/chat/AgentPageFrame.browser.tsx index a3590dd76..4d19d463f 100644 --- a/apps/web/src/components/chat/AgentPageFrame.browser.tsx +++ b/apps/web/src/components/chat/AgentPageFrame.browser.tsx @@ -10,11 +10,13 @@ import { import { AGENT_PAGE_MIN_HEIGHT, type AgentPageContentSize } from "@threadlines/shared/agentPages"; import { page, userEvent } from "vite-plus/test/browser"; import { afterEach, describe, expect, it, vi } from "vite-plus/test"; +import { useRef } from "react"; import { render } from "vitest-browser-react"; import { AgentPageFrame } from "./AgentPageFrame"; import { AgentPageRow } from "./AgentPageRow"; import { ExpandedImageViewerHost } from "./ExpandedImageViewerHost"; +import { TranscriptSelectionLayer } from "./TranscriptSelectionPopover"; // Stored pages by version id: what `pages.read` would return. const storedPages = vi.hoisted(() => new Map()); @@ -69,6 +71,13 @@ const picture = (color: string) => /** The frame decides what a load means as it happens; this is time for the answer to show. */ const settled = () => new Promise((resolve) => setTimeout(resolve, 150)); +/** + * Focus landing in a page soon after typing in the app counts as the page + * taking it (AgentPageFrame's focus guard): a reader who just typed clicks + * into a page a moment later. + */ +const pastTypingWindow = () => new Promise((resolve) => setTimeout(resolve, 1_600)); + /** Which of a frame's edges fade out, read from its mask. */ function fadedEdges(frame: HTMLElement) { const mask = frame.style.maskImage; @@ -283,4 +292,169 @@ describe("AgentPageRow", () => { window.removeEventListener("message", onReport); } }); + + it("offers copy and add-note on text the reader selects in a page, as in the chat", async () => { + const version = versionOf( + "selectable", + `

Part 2: the products

edwards-est-vigilant-gsa-ct2.html

` + + // A page cannot raise the actions by itself, without the reader in it. + ``, + ); + const other = versionOf("other", "

Another page

"); + const onAddHighlight = vi.fn(); + const pageProps = (ref: typeof version, title: string) => ({ + pageId: ref.pageId, + versionId: ref.versionId, + version: 1, + turnId: TurnId.make("turn-1"), + participantId: null, + title, + kind: "html" as const, + height: 120, + createdAt: "2026-10-23T12:00:00.000Z", + updatedAt: "2026-10-23T12:00:00.000Z", + }); + function Timeline() { + const containerRef = useRef(null); + return ( +
+ + + +
+ ); + } + let forged = false; + const onForged = (event: MessageEvent) => { + if (event.data?.params?.text === "forged") forged = true; + }; + window.addEventListener("message", onForged); + await render(); + const addNote = page.getByRole("button", { name: "Add note" }); + const pageText = page + .frameLocator(page.getByTitle("Fixes")) + .getByText("edwards-est-vigilant-gsa-ct2.html"); + await vi.waitFor(() => expect(forged).toBe(true)); + window.removeEventListener("message", onForged); + await settled(); + expect(addNote.elements()).toHaveLength(0); + + await pageText.tripleClick(); + await expect.element(addNote).toBeVisible(); + // Beside the selected line, not somewhere else on the page. + const line = page.getByTitle("Fixes").element().getBoundingClientRect(); + const actions = addNote.element().getBoundingClientRect(); + expect(actions.top).toBeGreaterThan(line.top); + expect(actions.top).toBeLessThan(line.top + 120); + + // A click into another page leaves this page's selection behind, and its actions. + await page.frameLocator(page.getByTitle("Other")).getByText("Another page").click(); + await page.frameLocator(page.getByTitle("Other")).getByText("Another page").click(); + await expect.poll(() => addNote.elements()).toHaveLength(0); + + await pageText.tripleClick(); + await expect.element(addNote).toBeVisible(); + + await addNote.click(); + await userEvent.keyboard("This one failed"); + await page.getByRole("button", { name: "Add", exact: true }).click(); + expect(onAddHighlight).toHaveBeenCalledWith({ + sourceMessageId: version.pageId, + sourceRole: "page", + selectedText: "edwards-est-vigilant-gsa-ct2.html", + note: "This one failed", + }); + }); + + it("offers the same actions in the full-size view, beside the text at any zoom", async () => { + const version = versionOf( + "full-size", + `

fire-lite-ann-rly.html

`, + ); + const onAddHighlight = vi.fn(); + await render( + , + ); + await page.getByRole("button", { name: "Open Products full size" }).click(); + const dialog = page.getByRole("dialog"); + const addNote = dialog.getByRole("button", { name: "Add note" }); + const frame = dialog.getByTitle("Products"); + const line = page.frameLocator(frame).getByText("fire-lite-ann-rly.html"); + const actionsBelowFrameTop = () => + addNote.element().getBoundingClientRect().top - frame.element().getBoundingClientRect().top; + const zoomIn = dialog.getByRole("button", { name: "Zoom in" }); + // The view has loaded the page and settled its focus. + await expect.element(zoomIn).toBeEnabled(); + // The test before typed its note just now. + await pastTypingWindow(); + await line.tripleClick(); + await expect.element(addNote).toBeVisible(); + // The line sits 200px down the page. + expect(actionsBelowFrameTop()).toBeGreaterThan(210); + expect(actionsBelowFrameTop()).toBeLessThan(260); + + // Zoomed to 125% it is drawn 250px down, and the actions follow it. + await zoomIn.click(); + await zoomIn.click(); + await expect.element(dialog.getByText("125%")).toBeVisible(); + await expect.poll(actionsBelowFrameTop).toBeGreaterThan(265); + + // Escape in the note, even on its buttons, closes the note, not the full-size view. + await addNote.click(); + await userEvent.keyboard("Discontinued"); + await userEvent.keyboard("{Tab}"); + await expect.element(dialog.getByRole("button", { name: "Cancel" })).toHaveFocus(); + await userEvent.keyboard("{Escape}"); + await expect + .poll(() => dialog.getByRole("button", { name: "Add", exact: true }).elements()) + .toHaveLength(0); + await expect.element(dialog).toBeVisible(); + + await dialog.getByRole("button", { name: /^Zoom 125%/ }).click(); + await pastTypingWindow(); + await line.tripleClick(); + await addNote.click(); + await userEvent.keyboard("Discontinued"); + // A look at the source keeps the note being written. + const source = dialog.getByRole("button", { name: "Source" }); + await source.click(); + await source.click(); + await expect + .element(dialog.getByPlaceholder("Add context for this highlight")) + .toHaveValue("Discontinued"); + await dialog.getByRole("button", { name: "Add", exact: true }).click(); + expect(onAddHighlight).toHaveBeenCalledWith({ + sourceMessageId: version.pageId, + sourceRole: "page", + selectedText: "fire-lite-ann-rly.html", + note: "Discontinued", + }); + }); }); diff --git a/apps/web/src/components/chat/AgentPageFrame.tsx b/apps/web/src/components/chat/AgentPageFrame.tsx index 99db5dc81..fee39e89e 100644 --- a/apps/web/src/components/chat/AgentPageFrame.tsx +++ b/apps/web/src/components/chat/AgentPageFrame.tsx @@ -275,6 +275,7 @@ export function AgentPageFrame(props: {