From f5b67ffb9298f5499505f8459a929636ccbb3291 Mon Sep 17 00:00:00 2001 From: Gil Birman Date: Mon, 23 Nov 2026 10:27:34 -0600 Subject: [PATCH 1/3] feat(plugins): offer the host's action menu to plugins Adds Menu to the optional ui service: a host-rendered trigger (label or icon button) and plain items with label, onSelect, destructive tone and disabled. The host owns the portal, highlight, keyboard, typeahead, Escape and focus return. Menu is optional in the type so plugins check ui.Menu on hosts from before it. Co-authored-by: Claude Opus 5.5 Signed-off-by: Gil Birman --- docs/plugin-authoring-guide.md | 4 +- examples/plugins/README.md | 2 +- examples/plugins/host-controls/plugin.js | 21 +++++- src/features/plugin-ui/contract.ts | 31 ++++++++ src/features/plugin-ui/service.test.tsx | 92 ++++++++++++++++++++++++ src/features/plugin-ui/service.tsx | 71 +++++++++++++++++- src/plugins/author.test.mjs | 5 ++ src/plugins/author.ts | 2 + 8 files changed, 223 insertions(+), 5 deletions(-) diff --git a/docs/plugin-authoring-guide.md b/docs/plugin-authoring-guide.md index 92770a3494..6c0110b78c 100644 --- a/docs/plugin-authoring-guide.md +++ b/docs/plugin-authoring-guide.md @@ -74,7 +74,7 @@ Each line is the main call. Read the types for every field. | `decisions` | Typed judgments about text | `await ctx.decisions.decide({ state, questions, community })`. It never rejects. | | `browser` | An embedded website (macOS) | `ctx.browser.View` inside a panel | | `notices` (optional) | A notice on any page, with up to three buttons | `await ctx.reflect.get("notices")?.show({ title, description?, actions? }, { signal? })` resolves with the picked action's `id`, or `undefined` if dismissed or aborted. Proceed only on an allow `id`. Pass a tool call's `client.signal` to ask the owner before it runs. | -| `ui` (optional) | The host's own controls | `Button`, `Input`, `Field`, `Switch`, `Dialog`. Fall back to plain elements when missing. | +| `ui` (optional) | The host's own controls | `Button`, `Input`, `Field`, `Switch`, `Dialog`, and `Menu` (newer hosts: check `ui.Menu`). `Menu` takes `items` (`label`, `onSelect`, `tone: "destructive"`, `disabled`), a `label` or `aria-label`, `icon` and `align`. Fall back to plain elements when missing. | | `host` | Commands, HTTPS, processes, files | Needs `manifest.host`, below | Agent tool names match `/^[a-z][a-z0-9_]{0,63}$/` and are unique across plugins; prefix them with your plugin. Changing a tool's name, description or schema restarts some agent sessions. `addInstructions` text is sent on every turn, so keep it short. @@ -136,7 +136,7 @@ Other grants (`mcp`, `managedFiles`, `publicImages`, `publicPages`, `agentHooks` ## UI - The host supplies the shell, navigation, error boundaries, and the outer surface of panels and Settings. Don't add your own page frame, overlays, global styles or theme. Panels sit in a host card, so render only the body. Pages own their padding and scrolling. -- Use the `ui` controls for buttons, fields, switches and dialogs. Use `className="ui-card"` for a card, `var(--space-panel-inset)` and `var(--space-2)` for spacing, and inline styles for layout. Plain `h1`–`h3`, `p` and `button` get host styling. +- Use the `ui` controls for buttons, fields, switches, dialogs and action menus. Use `className="ui-card"` for a card, `var(--space-panel-inset)` and `var(--space-2)` for spacing, and inline styles for layout. Plain `h1`–`h3`, `p` and `button` get host styling. - Use host color tokens (`var(--...)`) so light and dark themes both work. Rely only on the `core` roles in `src/shared/design-system/tokens/registry.ts`; other custom properties, classes and `data-*` attributes are internal. - Avoid fixed widths; pages can be narrow. - For `ctx.agents.ui.Transcript`, set `endedAt` on a finished turn, or supply `outcome` if its end time is unknown. Use `at: 0` for an unknown item timestamp. Omit `completedAt` when a tool's start time is unknown. diff --git a/examples/plugins/README.md b/examples/plugins/README.md index 2e7c231fe9..56ec576981 100644 --- a/examples/plugins/README.md +++ b/examples/plugins/README.md @@ -6,7 +6,7 @@ All example plugins live here with `manifest.json` and prebuilt `plugin.js` file - `shortcut-counter`: offline keyboard-shortcut consumer; Command+Shift+G / Control+Shift+G increments through the injected host service. Requires `shortcuts`. - `broken-page`: intentionally fails when its page renders to exercise error handling. -- `host-controls`: offline switch and name dialog built from the host's `ui` controls, with a +- `host-controls`: offline switch, name dialog and actions menu built from the host's `ui` controls, with a notice on hosts without them. ## Try the offline playgrounds diff --git a/examples/plugins/host-controls/plugin.js b/examples/plugins/host-controls/plugin.js index ec89d219bb..8cea1664ef 100644 --- a/examples/plugins/host-controls/plugin.js +++ b/examples/plugins/host-controls/plugin.js @@ -62,7 +62,26 @@ export function apply(ctx) { checked: alerts, onCheckedChange: setAlerts, }), - h(ui.Button, { variant: "prominent", onClick: edit }, "Edit name"), + h( + "div", + { style: { display: "flex", gap: "var(--space-2)" } }, + h(ui.Button, { variant: "prominent", onClick: edit }, "Edit name"), + // Newer hosts only; the Edit button still works without it. + ui.Menu && + h(ui.Menu, { + "aria-label": "Name actions", + items: [ + { label: "Edit name", onSelect: edit }, + { label: "Use “Ada”", onSelect: () => setName("Ada") }, + { + label: "Clear name", + tone: "destructive", + disabled: !name, + onSelect: () => setName(""), + }, + ], + }), + ), h( ui.Dialog, { diff --git a/src/features/plugin-ui/contract.ts b/src/features/plugin-ui/contract.ts index f5ccef3b77..3328b85326 100644 --- a/src/features/plugin-ui/contract.ts +++ b/src/features/plugin-ui/contract.ts @@ -92,12 +92,43 @@ export type PluginDialogProps = Readonly<{ preventClose?: boolean | undefined; }>; +export type PluginMenuItem = Readonly<{ + /** Unique within the menu. Typeahead matches it. */ + label: string; + /** Called with no arguments. The menu closes, so it can open a `Dialog`. */ + onSelect: () => void; + /** Default `default`. `destructive` is for an item that deletes or discards. */ + tone?: "default" | "destructive" | undefined; + disabled?: boolean | undefined; +}>; + +/** + * An action menu on its own trigger. The host owns the popup above clipping + * containers, highlight, arrow keys, typeahead, Escape and focus return. + */ +export type PluginMenuProps = Readonly< + { + items: readonly PluginMenuItem[]; + /** Shown before `label`, or alone. Default the host's More icon. */ + icon?: ReactElement | undefined; + disabled?: boolean | undefined; + /** Default `start`. `end` lines the menu up with the trigger's end edge. */ + align?: "start" | "end" | undefined; + } & ( + | { label: string; "aria-label"?: string | undefined } + /** Without a visible `label`, the trigger is an icon button with this name and hint. */ + | { label?: undefined; "aria-label": string } + ) +>; + export type PluginUi = Readonly<{ Button: (props: PluginButtonProps) => ReactElement; Input: (props: PluginInputProps) => ReactElement; Field: (props: PluginFieldProps) => ReactElement; Switch: (props: PluginSwitchProps) => ReactElement; Dialog: (props: PluginDialogProps) => ReactElement; + /** Absent on hosts from before it was added; fall back when it is missing. */ + Menu?: ((props: PluginMenuProps) => ReactElement) | undefined; }>; declare module "@deepseek-ai/cordis" { diff --git a/src/features/plugin-ui/service.test.tsx b/src/features/plugin-ui/service.test.tsx index 7a44227bc4..b3a2f1c0d1 100644 --- a/src/features/plugin-ui/service.test.tsx +++ b/src/features/plugin-ui/service.test.tsx @@ -10,6 +10,10 @@ import { pluginUi } from "./service"; afterEach(cleanup); +const fail = (message: string): never => { + throw new Error(message); +}; + /** Reads `ui` the way a plugin does: optionally, from its own context. */ async function readUi(host: Context) { let ui: PluginUi | undefined; @@ -168,6 +172,80 @@ it("lets a plugin build a form in a dialog from host controls", async () => { await vi.waitFor(() => expect(document.body.style.overflowY).toBe("")); }); +it("lets a plugin offer actions in a host menu that leads to a dialog", async () => { + const { Dialog, Button } = pluginUi; + const Menu = pluginUi.Menu ?? fail("Menu was not provided"); + const snoozed = vi.fn(); + const user = userEvent.setup(); + function Card() { + const [confirming, setConfirming] = useState(false); + return h( + // A card that clips its overflow, like a scrolling sheet. + "article", + { "data-testid": "card", style: { overflow: "hidden", height: 24 } }, + h(Menu, { + "aria-label": "More", + align: "end", + items: [ + { label: "Snooze", onSelect: snoozed }, + { label: "Archive", onSelect: snoozed, disabled: true }, + { + label: "Dismiss without answering", + tone: "destructive", + onSelect: () => setConfirming(true), + }, + ], + }), + h( + Dialog, + { + open: confirming, + onOpenChange: setConfirming, + title: "Dismiss item?", + actions: h(Button, { onClick: () => setConfirming(false) }, "Cancel"), + }, + "The agent will see you chose not to answer.", + ), + ); + } + render(h(Card)); + const trigger = screen.getByRole("button", { name: "More" }); + + // Keyboard: open, move past the disabled item, and choose by typeahead. + trigger.focus(); + await user.keyboard("{Enter}"); + const menu = await screen.findByRole("menu", { name: "More" }); + // Rendered outside the card, so the card's overflow cannot clip it. + expect(screen.getByTestId("card")).not.toContainElement(menu); + expect(screen.getByRole("menuitem", { name: "Archive" })).toHaveAttribute( + "aria-disabled", + "true", + ); + await user.keyboard("{ArrowDown}"); + await user.keyboard("s"); + await vi.waitFor(() => + expect(screen.getByRole("menuitem", { name: "Snooze" })).toHaveFocus(), + ); + await user.keyboard("{Enter}"); + expect(snoozed).toHaveBeenCalledExactlyOnceWith(); + await vi.waitFor(() => expect(screen.queryByRole("menu")).toBeNull()); + expect(trigger).toHaveFocus(); + + // A destructive item opens a confirmation; Escape returns to the trigger. + await user.click(trigger); + await user.click( + await screen.findByRole("menuitem", { name: "Dismiss without answering" }), + ); + await screen.findByRole("dialog", { name: "Dismiss item?" }); + await vi.waitFor(() => + expect(screen.getByRole("button", { name: "Cancel" })).toHaveFocus(), + ); + await user.keyboard("{Escape}"); + await vi.waitFor(() => expect(screen.queryByRole("dialog")).toBeNull()); + expect(screen.queryByRole("menu")).toBeNull(); + expect(trigger).toHaveFocus(); +}); + it("treats plain-JS values of the wrong type as absent", async () => { const loose = pluginUi as unknown as Record< keyof PluginUi, @@ -182,6 +260,15 @@ it("treats plain-JS values of the wrong type as absent", async () => { h(loose.Input, { "aria-label": "Name", disabled: 1, maxLength: "4" }), h(loose.Switch, { label: "Alerts", disabled: 1, checked: "yes" }), h(loose.Switch, { label: null, "aria-label": "Sync" }), + h(loose.Menu, { + label: "Actions", + icon: "x", + items: [ + null, + { label: 5 }, + { label: "Go", onSelect: "x", disabled: 1 }, + ], + }), ), ); const go = screen.getByRole("button", { name: "Go" }); @@ -194,4 +281,9 @@ it("treats plain-JS values of the wrong type as absent", async () => { expect(alerts).not.toHaveAttribute("aria-disabled"); expect(alerts).not.toBeChecked(); expect(screen.getByRole("switch", { name: "Sync" })).toBeInTheDocument(); + await user.click(screen.getByRole("button", { name: "Actions" })); + const item = await screen.findByRole("menuitem"); + expect(item).toHaveAccessibleName("Go"); + expect(item).not.toHaveAttribute("aria-disabled"); + await user.click(item); }); diff --git a/src/features/plugin-ui/service.tsx b/src/features/plugin-ui/service.tsx index 8c44294353..7d9ef7db09 100644 --- a/src/features/plugin-ui/service.tsx +++ b/src/features/plugin-ui/service.tsx @@ -1,17 +1,26 @@ // Hands plugins the host's own controls. Each wrapper forwards only the props in // `contract.ts`, so plugins (usually plain JS) cannot come to depend on internal // variants or on Base UI's event details. -import { type RefObject, useRef } from "react"; +import { isValidElement, type RefObject, useRef } from "react"; +import { DotsThreeIcon } from "../../shared/design-system/icons"; import { Button } from "../../shared/design-system/ui/Button"; import { Dialog } from "../../shared/design-system/ui/Dialog"; import { Field } from "../../shared/design-system/ui/Field"; +import { IconButton } from "../../shared/design-system/ui/IconButton"; import { Input } from "../../shared/design-system/ui/Input"; +import { + MenuItem, + MenuPopup, + MenuRoot, + MenuTrigger, +} from "../../shared/design-system/ui/Menu"; import { Switch } from "../../shared/design-system/ui/Switch"; import type { PluginButtonProps, PluginDialogProps, PluginFieldProps, PluginInputProps, + PluginMenuProps, PluginSwitchProps, PluginUi, } from "./contract"; @@ -177,4 +186,64 @@ export const pluginUi: PluginUi = Object.freeze({ ); }, + Menu: (props: PluginMenuProps) => { + const label = str(props.label); + const name = str(props["aria-label"]); + const disabled = props.disabled === true; + const icon = isValidElement(props.icon) ? props.icon : undefined; + const items = Array.isArray(props.items) ? props.items : []; + return ( + + + ); + }, }); diff --git a/src/plugins/author.test.mjs b/src/plugins/author.test.mjs index 65ced30701..7d3b13cdf4 100644 --- a/src/plugins/author.test.mjs +++ b/src/plugins/author.test.mjs @@ -93,6 +93,11 @@ export function apply(ctx: Context) { void createElement(ui.Switch, { label: "Alerts", onCheckedChange: (checked: boolean) => void checked }); // @ts-expect-error Internal and legacy variants are not part of the plugin contract. void createElement(ui.Button, { variant: "glass", children: "Glass" }); + // @ts-expect-error Menu is missing on hosts from before it was added and must be checked. + void createElement(ui.Menu, { "aria-label": "More", items: [] }); + if (ui.Menu) + void createElement(ui.Menu, { "aria-label": "More", align: "end", + items: [{ label: "Dismiss", tone: "destructive", onSelect: () => {} }] }); } const unread: UnreadCapability = ctx.relay.snapshot().session.unread; const conversations: ReturnType = unread.inbox(); diff --git a/src/plugins/author.ts b/src/plugins/author.ts index 8d70d0e3f4..1b2dfe9bdb 100644 --- a/src/plugins/author.ts +++ b/src/plugins/author.ts @@ -39,6 +39,8 @@ export type { PluginDialogProps, PluginFieldProps, PluginInputProps, + PluginMenuItem, + PluginMenuProps, PluginSwitchProps, PluginUi, } from "../features/plugin-ui/contract"; From eae7a13356e102844ecbf5921819dcbc101200bf Mon Sep 17 00:00:00 2001 From: Gil Birman Date: Mon, 23 Nov 2026 11:32:36 -0600 Subject: [PATCH 2/3] Address review feedback round 1 Prove arrow keys and typeahead move focus, and that a disabled item does nothing. Validated with vitest src/features/plugin-ui and a disabled-item mutation check. Co-authored-by: Claude Opus 5.5 Signed-off-by: Gil Birman --- src/features/plugin-ui/service.test.tsx | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/features/plugin-ui/service.test.tsx b/src/features/plugin-ui/service.test.tsx index b3a2f1c0d1..803cfee5a8 100644 --- a/src/features/plugin-ui/service.test.tsx +++ b/src/features/plugin-ui/service.test.tsx @@ -211,21 +211,24 @@ it("lets a plugin offer actions in a host menu that leads to a dialog", async () render(h(Card)); const trigger = screen.getByRole("button", { name: "More" }); - // Keyboard: open, move past the disabled item, and choose by typeahead. + // Keyboard: open, move with the arrows, and choose by typeahead. trigger.focus(); await user.keyboard("{Enter}"); const menu = await screen.findByRole("menu", { name: "More" }); // Rendered outside the card, so the card's overflow cannot clip it. expect(screen.getByTestId("card")).not.toContainElement(menu); - expect(screen.getByRole("menuitem", { name: "Archive" })).toHaveAttribute( - "aria-disabled", - "true", - ); + const item = (name: string) => screen.getByRole("menuitem", { name }); + await vi.waitFor(() => expect(item("Snooze")).toHaveFocus()); await user.keyboard("{ArrowDown}"); + await vi.waitFor(() => expect(item("Snooze")).not.toHaveFocus()); await user.keyboard("s"); - await vi.waitFor(() => - expect(screen.getByRole("menuitem", { name: "Snooze" })).toHaveFocus(), - ); + await vi.waitFor(() => expect(item("Snooze")).toHaveFocus()); + // A disabled item does nothing and keeps the menu open. + await user.click(item("Archive")); + expect(snoozed).not.toHaveBeenCalled(); + expect(menu).toBeInTheDocument(); + await user.keyboard("s"); + await vi.waitFor(() => expect(item("Snooze")).toHaveFocus()); await user.keyboard("{Enter}"); expect(snoozed).toHaveBeenCalledExactlyOnceWith(); await vi.waitFor(() => expect(screen.queryByRole("menu")).toBeNull()); From c5b473dc3787046f3a6eb0b546fd6747404ca909 Mon Sep 17 00:00:00 2001 From: Gil Birman Date: Mon, 23 Nov 2026 11:53:36 -0600 Subject: [PATCH 3/3] Simplify reviewed changes Drop the redundant MenuItem label; Base UI types ahead on the item's text. Validated with vitest src/features/plugin-ui. Co-authored-by: Claude Opus 5.5 Signed-off-by: Gil Birman --- src/features/plugin-ui/service.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/features/plugin-ui/service.tsx b/src/features/plugin-ui/service.tsx index 7d9ef7db09..e3bc349fa0 100644 --- a/src/features/plugin-ui/service.tsx +++ b/src/features/plugin-ui/service.tsx @@ -233,7 +233,6 @@ export const pluginUi: PluginUi = Object.freeze({ return ( select())}