From a5fdbab0baf2127e38dc10611d858d7813f735b7 Mon Sep 17 00:00:00 2001 From: Badcuban <108198679+badcuban@users.noreply.github.com> Date: Mon, 23 Nov 2026 09:30:26 -0500 Subject: [PATCH] fix(web): agent pills in the model menu no longer flicker on hover MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pointing at an agent pill showed its rename and remove buttons, which made the pill wider. A pill at the end of the line then wrapped to the next one, out from under the pointer, shrank back, and flickered between the two. Pills now hold only their icon, name and "main" tag. Pointing at one swaps its icon for a ⋯ button in the same box, which opens a menu with Rename and Remove from thread. A right-click opens the same menu, and so does a long press on touch. No pill changes size on hover, focus, or while its menu is open. The model shortcut keys do nothing while a pill's menu is open, so they can't switch the model underneath it. --- apps/server/src/guide/topics.ts | 2 +- .../components/ChatView.agents.browser.tsx | 57 +++ .../components/chat/ModelPickerContent.tsx | 9 +- .../web/src/components/chat/RoomAgentsBar.tsx | 419 +++++++++++------- 4 files changed, 331 insertions(+), 156 deletions(-) diff --git a/apps/server/src/guide/topics.ts b/apps/server/src/guide/topics.ts index 606b35fcf..9ea07a1f2 100644 --- a/apps/server/src/guide/topics.ts +++ b/apps/server/src/guide/topics.ts @@ -89,7 +89,7 @@ Some agents lack a level; they run at the next safer one they have, and the comp keywords: ["room", "multiple agents", "reviewer", "second opinion", "hand off", "teammate"], body: `A room is a thread with more than one agent: for example one agent doing the work and another reviewing it. Rooms are turned on with {place:threads.enableRooms}; {place:threads.room} sets which agents new threads start with. -You add and remove agents at the top of the model button's panel in the composer: one pill per agent, the picked one being who your next message goes to (typing @ and picking an agent does the same). The model and effort under the pills are that agent's. +You add and remove agents at the top of the model button's panel in the composer: one pill per agent, the picked one being who your next message goes to (typing @ and picking an agent does the same). The model and effort under the pills are that agent's. Pointing at a pill turns its icon into ⋯, a menu to rename the agent or remove it; a right-click, or a long press on a touch screen, opens the same menu, and a double-click renames. Only one agent works at a time, in the same checkout. Agents in a room can ask each other a question on the side, ask for an independent review of the changes, or hand the next turn to another agent. Up to three agents can answer on the side at once, one answer each, so you can ask a second agent while the first is still answering, and an agent can ask two or three for a review of the same changes in one go. diff --git a/apps/web/src/components/ChatView.agents.browser.tsx b/apps/web/src/components/ChatView.agents.browser.tsx index d3ec5c61c..3da57231c 100644 --- a/apps/web/src/components/ChatView.agents.browser.tsx +++ b/apps/web/src/components/ChatView.agents.browser.tsx @@ -568,6 +568,41 @@ describe("ChatView (full app): agent browser tabs and rooms", () => { picker.click(); await waitForElement(() => pill(astraId), "Unable to find astra's pill."); + // Pointing at a pill swaps its icon for ⋯ without resizing it: a pill + // that grew could wrap onto the next line, out from under the pointer, + // and flicker between the two. (offsetWidth: the picker's opening + // animation scales bounding boxes.) + const options = () => + pill(astraId)!.querySelector('button[aria-label$=" options"]')!; + const widthBefore = pill(astraId)!.offsetWidth; + // At rest no pill shows its ⋯, the picked one included. + for (const button of document.querySelectorAll( + '[data-room-agents-bar] button[aria-label$=" options"]', + )) { + expect(getComputedStyle(button).visibility).toBe("hidden"); + } + await userEvent.hover(pill(astraId)!); + await vi.waitFor(() => expect(getComputedStyle(options()).visibility).toBe("visible")); + expect(pill(astraId)!.offsetWidth).toBe(widthBefore); + + // Its menu renames without closing the picker or changing who is picked. + options().click(); + const menuItem = (text: string) => + [...document.querySelectorAll('[role="menuitem"]')].find( + (item) => item.textContent === text, + ) ?? null; + (await waitForElement(() => menuItem("Rename…"), "The pill's menu should open.")).click(); + const nameBox = await waitForElement( + () => pill(astraId)!.querySelector("input"), + "Rename should open the pill's name box.", + ); + await vi.waitFor(() => expect(document.activeElement).toBe(nameBox)); + expect(document.querySelector("[data-model-picker-variant]")).not.toBeNull(); + expect(pill("primary")?.getAttribute("aria-checked")).toBe("true"); + expect(pill(astraId)?.getAttribute("aria-checked")).toBe("false"); + await userEvent.keyboard("{Escape}"); + await vi.waitFor(() => expect(pill(astraId)!.querySelector("input")).toBeNull()); + // "+" starts adding; picking an agent instead means not a new one after // all, and the list below is that agent's again. document @@ -597,6 +632,28 @@ describe("ChatView (full app): agent browser tabs and rooms", () => { }); // Still open: picking an agent doesn't close the picker. expect(document.querySelector("[data-model-picker-variant]")).not.toBeNull(); + + // A right-click opens the same menu, which removes the agent. + pill(astraId)!.dispatchEvent( + new MouseEvent("contextmenu", { bubbles: true, cancelable: true, button: 2 }), + ); + ( + await waitForElement( + () => menuItem("Remove from thread"), + "Right-click should open the menu.", + ) + ).click(); + await vi.waitFor(() => + expect( + wsRequests.find( + (request) => + request._tag === ORCHESTRATION_WS_METHODS.dispatchCommand && + (request as { type?: string }).type === "thread.participant.remove", + ), + ).toMatchObject({ participantId: astraId }), + ); + expect(document.querySelector("[data-model-picker-variant]")).not.toBeNull(); + expect(pill("primary")?.getAttribute("aria-checked")).toBe("true"); } finally { useRoomRecipientStore.getState().choose(THREAD_REF, null); await mounted.cleanup(); diff --git a/apps/web/src/components/chat/ModelPickerContent.tsx b/apps/web/src/components/chat/ModelPickerContent.tsx index c4741852a..e53b874c8 100644 --- a/apps/web/src/components/chat/ModelPickerContent.tsx +++ b/apps/web/src/components/chat/ModelPickerContent.tsx @@ -752,11 +752,14 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: { return; } - // A dropdown open over the settings rows, or an agent's name being - // typed, owns the keys: a jump would change the model underneath it. + // A dropdown open over the settings rows, an agent pill's menu, or an + // agent's name being typed, owns the keys: a jump would change the model + // underneath it. if ( event.target instanceof Element && - event.target.closest("[data-slot='select-popup'], [data-room-agents-bar] input") + event.target.closest( + "[data-slot='select-popup'], [data-slot='menu-popup'], [data-room-agents-bar] input", + ) ) { return; } diff --git a/apps/web/src/components/chat/RoomAgentsBar.tsx b/apps/web/src/components/chat/RoomAgentsBar.tsx index dd5383064..8b74ddecb 100644 --- a/apps/web/src/components/chat/RoomAgentsBar.tsx +++ b/apps/web/src/components/chat/RoomAgentsBar.tsx @@ -4,12 +4,21 @@ * list and settings under it are that agent's. Picking a pill keeps the * picker open, so its model and effort can be changed in the same visit. * "+" adds an agent: the next model picked joins as a new one. + * + * A pill holds only its icon, name and "main" tag. Pointing at one swaps the + * icon for a ⋯ button in the same box, which opens Rename and Remove; so do a + * right-click and, on touch, a long press. Nothing appears beside the name, so + * a pill never changes size under the pointer: one that grew could wrap to + * the next line, out from under it, and flicker between the two. */ import { ROOM_AGENT_ROLE_MAX_LENGTH } from "@threadlines/contracts"; -import { PencilIcon, UserRoundPlusIcon, XIcon } from "lucide-react"; -import { memo, useState } from "react"; +import { EllipsisIcon, UserRoundPlusIcon, XIcon } from "lucide-react"; +import { memo, useCallback, useRef, useState } from "react"; +import type * as React from "react"; import { cn } from "~/lib/utils"; +import { useTouchHoldMenu } from "../../hooks/useTouchHoldMenu"; +import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; import { failureNotice, toastManager } from "../ui/toast"; import { ProviderInstanceIcon } from "./ProviderInstanceIcon"; import { roomAgentKey } from "../../rooms"; @@ -20,7 +29,9 @@ import { THREAD_STATUS_DOT_CLASSES } from "../Sidebar.logic"; const PILL_CLASS = "group/agent relative flex h-7 min-w-0 max-w-48 shrink-0 cursor-pointer items-center gap-1.5 rounded-md px-2 text-xs outline-none transition-colors focus-visible:ring-2 focus-visible:ring-ring pointer-coarse:h-9 pointer-coarse:text-sm"; -const PILL_ARROW_STEPS: Partial> = { +type PillStep = 1 | -1 | "first" | "last"; + +const PILL_ARROW_STEPS: Partial> = { ArrowRight: 1, ArrowDown: 1, ArrowLeft: -1, @@ -29,10 +40,13 @@ const PILL_ARROW_STEPS: Partial> = { End: "last", }; -/** The pill's own small buttons: on the pointed-at, focused, or picked-on-touch pill. */ +/** The "New agent" pill's ×. */ const PILL_ACTION_CLASS = "rounded-sm p-0.5 text-muted-foreground hover:text-foreground focus-visible:text-foreground pointer-coarse:p-1.5"; +/** The icon's box, which the ⋯ shares. */ +const PILL_ICON_SIZE = "size-3.5 pointer-coarse:size-4"; + export const RoomAgentsBar = memo(function RoomAgentsBar(props: { agents: RoomAgents; /** "+" was picked: the list below adds its pick as a new agent. */ @@ -93,158 +107,37 @@ export const RoomAgentsBar = memo(function RoomAgentsBar(props: { > {agents.rows.map((row, index) => { const selected = !props.adding && row.id === agents.recipient.id; - // One tab stop for the group: the picked pill, or the first while adding. - const tabStop = props.adding ? index === 0 : selected; const isRenaming = renaming === roomAgentKey(row.id); - const busy = row.status !== null; - const canEdit = agents.editable && !isRenaming && !props.adding; return ( -
choose(row)} - onDoubleClick={() => { - if (canEdit) setRenaming(roomAgentKey(row.id)); - }} - onKeyDown={(event) => { - // Keys on the pill's own buttons and name box are theirs. - if (event.target !== event.currentTarget) return; - const step = PILL_ARROW_STEPS[event.key]; - if (step !== undefined) { - // Like any radio group: arrows move and pick, wrapping round. - event.preventDefault(); - const count = agents.rows.length; - const next = - step === "first" - ? 0 - : step === "last" - ? count - 1 - : (index + step + count) % count; - const nextRow = agents.rows[next]; - if (!nextRow) return; - choose(nextRow); - event.currentTarget.parentElement - ?.querySelector( - `[data-room-agent-row="${nextRow.id ?? "primary"}"]`, - ) - ?.focus(); - } else if (event.key === "Enter" || event.key === " ") { - event.preventDefault(); - choose(row); - } else if (event.key === "F2" && canEdit) { - event.preventDefault(); - setRenaming(roomAgentKey(row.id)); - } else if ((event.key === "Delete" || event.key === "Backspace") && canEdit) { - if (row.id !== null && !busy) { - event.preventDefault(); - remove(row); - } - } + row={row} + selected={selected} + // One tab stop for the group: the picked pill, or the first while adding. + tabStop={props.adding ? index === 0 : selected} + isRenaming={isRenaming} + hasMenu={agents.editable && !props.adding} + onChoose={() => choose(row)} + onStep={(step, pill) => { + // Like any radio group: arrows move and pick, wrapping round. + const count = agents.rows.length; + const next = + step === "first" + ? 0 + : step === "last" + ? count - 1 + : (index + step + count) % count; + const nextRow = agents.rows[next]; + if (!nextRow) return; + choose(nextRow); + pill.parentElement + ?.querySelector(`[data-room-agent-row="${nextRow.id ?? "primary"}"]`) + ?.focus(); }} - > - {row.entry ? ( - - - {busy ? ( - // "working" reads blue, like the sidebar; amber is for warnings. - - ) : null} - - ) : null} - {isRenaming ? ( - // The model's name stays; the user names the agent after it. - event.stopPropagation()} - onDoubleClick={(event) => event.stopPropagation()} - onKeyDown={(event) => { - event.stopPropagation(); - if (event.key === "Escape") { - event.currentTarget.value = row.role ?? ""; - event.currentTarget.blur(); - } else if (event.key === "Enter") { - event.preventDefault(); - event.currentTarget.blur(); - } - }} - onBlur={(event) => rename(row, event.currentTarget.value)} - /> - ) : ( - {row.role ?? row.modelName} - )} - {row.id === null && !isRenaming ? ( - - main - - ) : null} - {canEdit ? ( - - - {row.id !== null && !busy ? ( - - ) : null} - - ) : null} -
+ onStartRename={() => setRenaming(roomAgentKey(row.id))} + onRename={(typed) => rename(row, typed)} + onRemove={() => remove(row)} + /> ); })} {props.adding ? ( @@ -285,3 +178,225 @@ export const RoomAgentsBar = memo(function RoomAgentsBar(props: { ); }); + +/** + * One agent's pill. Its menu (Rename, Remove) opens from the ⋯ that takes the + * icon's place under the pointer or keyboard focus, from a right-click, or + * from a long press on touch, and always drops from the pill's left edge. + */ +function AgentPill(props: { + row: RoomAgentRow; + selected: boolean; + tabStop: boolean; + isRenaming: boolean; + /** The user may rename and remove agents here (not while adding one). */ + hasMenu: boolean; + onChoose: () => void; + onStep: (step: PillStep, pill: HTMLElement) => void; + onStartRename: () => void; + onRename: (typed: string) => void; + onRemove: () => void; +}) { + const { row, selected, isRenaming, hasMenu } = props; + const pillRef = useRef(null); + const [menuOpen, setMenuOpen] = useState(false); + // Rename from the menu: the menu must not take focus back from the name box. + const renameFromMenu = useRef(false); + const busy = row.status !== null; + const canEdit = hasMenu && !isRenaming; + // Events from the menu reach the pill through React's tree, though the menu + // sits elsewhere on the page: only the pill's own count as touching it. + const onPill = (event: React.SyntheticEvent) => + event.currentTarget.contains(event.target as Node); + + const openMenu = useCallback(() => setMenuOpen(true), []); + const onContextMenu = useCallback( + (event: React.MouseEvent) => { + if (!event.currentTarget.contains(event.target as Node)) return; + event.preventDefault(); + if (canEdit) openMenu(); + }, + [canEdit, openMenu], + ); + const touchHold = useTouchHoldMenu({ open: openMenu, onContextMenu }); + // A press on the open menu is the menu's: as a hold on the pill it would + // swallow the tap on Rename or Remove. + const pillTouchHold = { + ...touchHold, + onPointerDownCapture: (event: React.PointerEvent) => { + if (onPill(event)) touchHold.onPointerDownCapture(event); + }, + onClickCapture: (event: React.MouseEvent) => { + if (onPill(event)) touchHold.onClickCapture(event); + }, + }; + + return ( +
{ + if (onPill(event)) props.onChoose(); + }} + onDoubleClick={(event) => { + if (onPill(event) && canEdit) props.onStartRename(); + }} + onKeyDown={(event) => { + // Keys on the pill's ⋯, its menu and its name box are theirs. + if (event.target !== event.currentTarget) return; + const step = PILL_ARROW_STEPS[event.key]; + if (step !== undefined) { + event.preventDefault(); + props.onStep(step, event.currentTarget); + } else if (event.key === "Enter" || event.key === " ") { + event.preventDefault(); + props.onChoose(); + } else if (event.key === "F2" && canEdit) { + event.preventDefault(); + props.onStartRename(); + } else if ((event.key === "Delete" || event.key === "Backspace") && canEdit) { + if (row.id !== null && !busy) { + event.preventDefault(); + props.onRemove(); + } + } + }} + > + {row.entry || hasMenu ? ( + + {row.entry ? ( + + + + ) : ( +
+ ); +}