Multiplayer agent harness for work.
Allow 16 multiview panes and Chrome tab shortcuts in the desktop app (#2153)
Regan Bell518c9e5208b6
diff --git a/desktop/main.mjs b/desktop/main.mjsindex 87fcda2d7..ce594669f 100644--- a/desktop/main.mjs+++ b/desktop/main.mjs@@ -3,6 +3,7 @@ import { readFile, writeFile } from "node:fs/promises";import path from "node:path";import { fileURLToPath, pathToFileURL } from "node:url";import { createLogin, loginCallback } from "./login.mjs";+import { tabStep } from "./shortcuts.mjs";import { instanceUrl, externalUrl, browserLoginUrl, loginDestination, internalUrl } from "./url.mjs";const directory = path.dirname(fileURLToPath(import.meta.url));@@ -180,6 +181,12 @@ async function showInstance(url) {page.webContents.on("will-navigate", navigate);page.webContents.on("will-redirect", navigate);page.webContents.on("will-attach-webview", (event) => event.preventDefault());+ page.webContents.on("before-input-event", (event, input) => {+ const step = tabStep(input);+ if (!step) return;+ event.preventDefault();+ page.webContents.send("qm:cycle-tab", step);+ });page.webContents.setWindowOpenHandler(({ url: destination }) => {if (!active()) return { action: "deny" };if (browserLoginUrl(destination, origin)) signIn(destination);diff --git a/desktop/shortcuts.mjs b/desktop/shortcuts.mjsnew file mode 100644index 000000000..1c77e2b9c--- /dev/null+++ b/desktop/shortcuts.mjs@@ -0,0 +1,17 @@+export function tabStep({ type, key, code, control, meta, alt, shift, isComposing }, platform = process.platform) {+ if (type !== "keyDown" || isComposing) return 0;+ if (control && !meta && !alt) {+ if (key === "Tab") return shift ? -1 : 1;+ if (!shift && key === "PageDown") return 1;+ if (!shift && key === "PageUp") return -1;+ }+ if (platform === "darwin" && meta && alt && !control && !shift) {+ if (key === "ArrowRight") return 1;+ if (key === "ArrowLeft") return -1;+ }+ if (platform === "darwin" && meta && shift && !alt && !control) {+ if (code === "BracketRight") return 1;+ if (code === "BracketLeft") return -1;+ }+ return 0;+}diff --git a/desktop/test/main.test.mjs b/desktop/test/main.test.mjsindex 38200c5e9..6bd09175d 100644--- a/desktop/test/main.test.mjs+++ b/desktop/test/main.test.mjs@@ -104,6 +104,24 @@ test(assert.equal(windows[0].url, "https://old.example/");assert.deepEqual(windows[0].popup({ url: "file:///tmp/private" }), { action: "deny" });assert.equal(opened.length, 0);+ const sent = [];+ windows[0].webContents.send = (...args) => sent.push(args);+ let reserved = false;+ const input = (key, control) =>+ windows[0].webContents.emit(+ "before-input-event",+ {+ preventDefault() {+ reserved = true;+ },+ },+ { type: "keyDown", key, control },+ );+ input("Tab", false);+ assert.equal(reserved, false);+ input("Tab", true);+ assert.equal(reserved, true);+ assert.deepEqual(sent, [["qm:cycle-tab", 1]]);const frame = { url: "https://old.example/" };windows[0].webContents.mainFrame = frame;const browserEvent = { sender: windows[0].webContents, senderFrame: frame };diff --git a/desktop/test/shortcuts.test.mjs b/desktop/test/shortcuts.test.mjsnew file mode 100644index 000000000..d487ef9c9--- /dev/null+++ b/desktop/test/shortcuts.test.mjs@@ -0,0 +1,30 @@+import assert from "node:assert/strict";+import { test } from "node:test";+import { tabStep } from "../shortcuts.mjs";++const press = (key, modifiers = {}, extra = {}) => ({ type: "keyDown", key, ...modifiers, ...extra });++test("Chrome tab-cycling keys map to next and previous on every platform", () => {+ for (const platform of ["darwin", "linux", "win32"]) {+ assert.equal(tabStep(press("Tab", { control: true }), platform), 1);+ assert.equal(tabStep(press("Tab", { control: true, shift: true }), platform), -1);+ assert.equal(tabStep(press("PageDown", { control: true }), platform), 1);+ assert.equal(tabStep(press("PageUp", { control: true }), platform), -1);+ }+ assert.equal(tabStep(press("ArrowRight", { meta: true, alt: true }), "darwin"), 1);+ assert.equal(tabStep(press("ArrowLeft", { meta: true, alt: true }), "darwin"), -1);+ assert.equal(tabStep(press("}", { meta: true, shift: true }, { code: "BracketRight" }), "darwin"), 1);+ assert.equal(tabStep(press("{", { meta: true, shift: true }, { code: "BracketLeft" }), "darwin"), -1);+});++test("editing keys, other modifiers, key-up and IME composition are left to the page", () => {+ for (const [input, platform] of [+ [press("Tab", { control: true }, { type: "keyUp" }), "linux"],+ [press("Tab", { control: true }, { isComposing: true }), "linux"],+ [press("ArrowRight", { meta: true, alt: true }), "linux"],+ [press("Tab", { control: true, alt: true }), "linux"],+ [press("Tab"), "darwin"],+ ]) {+ assert.equal(tabStep(input, platform), 0, JSON.stringify(input));+ }+});diff --git a/desktop/test/workspace.test.mjs b/desktop/test/workspace.test.mjsindex f45f4a25d..57e4e762a 100644--- a/desktop/test/workspace.test.mjs+++ b/desktop/test/workspace.test.mjs@@ -9,7 +9,11 @@ async function workspace(platform = "darwin") {const dom = new JSDOM('<div id="sidebar-body"></div><textarea></textarea>', { runScripts: "outside-only" });const { window } = dom;window.process = { platform };- window.require = () => ({ contextBridge: { exposeInMainWorld() {} }, ipcRenderer: { invoke() {} } });+ const listeners = new Map();+ window.require = () => ({+ contextBridge: { exposeInMainWorld() {} },+ ipcRenderer: { invoke() {}, on: (name, listener) => listeners.set(name, listener) },+ });window.HTMLElement.prototype.checkVisibility = function () {return !this.closest("[hidden], .collapsed, .sidebar-closed");};@@ -42,6 +46,7 @@ async function workspace(platform = "darwin") {opened,add,key,+ cycle: (step) => listeners.get("qm:cycle-tab")({}, step),close: () => {window.dispatchEvent(new window.Event("blur"));window.close();@@ -145,3 +150,21 @@ test("the compact sidebar remains a shortcut target when it is an accessible modapp.close();}});++test("cycle-tab commands reach the page unless a modal dialog is open", async () => {+ const app = await workspace();+ try {+ const steps = [];+ app.window.addEventListener("qm:cycle-tab", (event) => steps.push(event.detail));+ app.cycle(1);+ const dialog = app.window.document.createElement("dialog");+ dialog.open = true;+ app.window.document.body.append(dialog);+ app.cycle(1);+ dialog.remove();+ app.cycle(-1);+ assert.deepEqual(steps, [1, -1]);+ } finally {+ app.close();+ }+});diff --git a/desktop/workspace-preload.cjs b/desktop/workspace-preload.cjsindex 695f6a774..38bc752f7 100644--- a/desktop/workspace-preload.cjs+++ b/desktop/workspace-preload.cjs@@ -106,6 +106,10 @@ window.addEventListener("DOMContentLoaded", () => {link.dataset.qmShortcut = `${mac ? "⌘" : "Ctrl "}${index + 1}`;});};+ ipcRenderer.on("qm:cycle-tab", (_event, step) => {+ if (document.querySelector('dialog[open], [aria-modal="true"]:not(.sidebar)')) return;+ window.dispatchEvent(new CustomEvent("qm:cycle-tab", { detail: step }));+ });window.addEventListener("keydown",(event) => {diff --git a/plugins/web-ui/src/split-layout.ts b/plugins/web-ui/src/split-layout.tsindex 5d85b2159..42ea85cf1 100644--- a/plugins/web-ui/src/split-layout.ts+++ b/plugins/web-ui/src/split-layout.ts@@ -2,9 +2,9 @@ export type DropEdge = "left" | "right" | "top" | "bottom" | "center";export type SplitEdge = Exclude<DropEdge, "center">;-export const MAX_TILES = 4;+export const MAX_TILES = 16;-export const MAX_PANES = 12;+export const MAX_PANES = 16;const WALK_BUDGET = 10_000;diff --git a/plugins/web-ui/src/split.ts b/plugins/web-ui/src/split.tsindex 8a5778876..98964afaa 100644--- a/plugins/web-ui/src/split.ts+++ b/plugins/web-ui/src/split.ts@@ -1597,3 +1597,13 @@ function notePaneSession(paneId: string, sessionId: string | null, threadRef: stdocument.addEventListener("keydown", (e) => {if (e.key === "Escape" && splitState.active && dockApi?.hasMaximizedGroup()) dockApi.exitMaximizedGroup();});++window.addEventListener("qm:cycle-tab", (e) => {+ const step = (e as CustomEvent<unknown>).detail;+ if (!dockApi || !canvasHost?.isConnected || (step !== 1 && step !== -1)) return;+ const group = dockApi.groups.find((g) => g.element.contains(document.activeElement)) ?? dockApi.activeGroup;+ const panels = group?.panels ?? [];+ const at = group?.activePanel ? panels.indexOf(group.activePanel) : -1;+ if (panels.length < 2 || at < 0) return;+ activatePanel(panels[(at + step + panels.length) % panels.length]);+});diff --git a/plugins/web-ui/test/new-chat-placement.test.ts b/plugins/web-ui/test/new-chat-placement.test.tsindex 557e79073..1212b90e4 100644--- a/plugins/web-ui/test/new-chat-placement.test.ts+++ b/plugins/web-ui/test/new-chat-placement.test.ts@@ -3,12 +3,15 @@ import test from "node:test";import { JSDOM } from "jsdom";import { createServer } from "vite";import type { Conversation } from "../src/conv-types.ts";+import { MAX_PANES, MAX_TILES } from "../src/split-layout.ts";interface Canvas {panes: () => number;tiles: () => number;newChat: () => boolean;tabCounts: () => number[];+ activeTabs: () => number[];+ cycleTab: (step: number) => void;focusTile: (index: number) => void;splitTile: (index: number) => void;seededChat: (threadRef?: string) => Conversation | null;@@ -125,6 +128,11 @@ async function withCanvas(panes: () => document.querySelectorAll(".dv-tab").length,tabCounts: () =>Array.from(document.querySelectorAll(".dv-groupview"), (g) => g.querySelectorAll(".dv-tab").length),+ activeTabs: () =>+ Array.from(document.querySelectorAll(".dv-groupview"), (g) =>+ Array.from(g.querySelectorAll(".dv-tab")).findIndex((t) => t.classList.contains("dv-active-tab")),+ ),+ cycleTab: (step) => window.dispatchEvent(new dom.window.CustomEvent("qm:cycle-tab", { detail: step })),focusTile: (index) => {document.querySelectorAll(".dv-groupview")@@ -234,10 +242,23 @@ test("seeded chats return from another view without losing the grid or suppliedtest("new chat replaces the focused conversation at capacity without dropping the grid", async () => {await withCanvas(async (canvas) => {await canvas.split();- for (let i = 2; i < 12; i++) assert.equal(canvas.newChat(), true);- assert.deepEqual([canvas.panes(), canvas.tiles()], [12, 3]);+ for (let i = 2; i < MAX_PANES; i++) assert.equal(canvas.newChat(), true);+ assert.deepEqual([canvas.panes(), canvas.tiles()], [MAX_PANES, 3]);assert.ok(canvas.seededChat()?.state.agent);- assert.deepEqual([canvas.panes(), canvas.tiles()], [12, 3]);+ assert.deepEqual([canvas.panes(), canvas.tiles()], [MAX_PANES, 3]);+ });+});++test("explicit splitting reaches the tile cap and stops at capacity", async () => {+ await withCanvas(async (canvas) => {+ await canvas.split();+ for (let i = 2; i < MAX_TILES; i++) canvas.splitTile(0);+ assert.deepEqual([canvas.panes(), canvas.tiles()], [MAX_TILES, MAX_TILES]);+ canvas.splitTile(0);+ assert.deepEqual([canvas.panes(), canvas.tiles()], [MAX_TILES, MAX_TILES]);+ await canvas.closeTile(0);+ canvas.splitTile(0);+ assert.deepEqual([canvas.panes(), canvas.tiles()], [MAX_TILES, MAX_TILES]);});});@@ -316,3 +337,24 @@ test("app edit chats suppress the general welcome and suggestions", async () =>assert.deepEqual(conv.state.agent.state.messages, []);});});++test("cycle-tab steps through the focused pane's tabs in order and wraps", async () => {+ await withCanvas(async (canvas) => {+ await canvas.split();+ canvas.newChat();+ canvas.newChat();+ canvas.newChat();+ assert.deepEqual(canvas.tabCounts(), [1, 1, 3]);+ const start = canvas.activeTabs()[2];+ canvas.cycleTab(1);+ assert.deepEqual(canvas.activeTabs(), [0, 0, (start + 1) % 3]);+ canvas.cycleTab(1);+ canvas.cycleTab(1);+ assert.deepEqual(canvas.activeTabs(), [0, 0, start]);+ canvas.cycleTab(-1);+ assert.deepEqual(canvas.activeTabs(), [0, 0, (start + 2) % 3]);+ canvas.focusTile(0);+ canvas.cycleTab(1);+ assert.deepEqual(canvas.activeTabs(), [0, 0, (start + 2) % 3], "a single-tab pane has nothing to cycle");+ });+});diff --git a/plugins/web-ui/test/split-tiles-and-titles.test.ts b/plugins/web-ui/test/split-tiles-and-titles.test.tsindex 5670e1e24..7bbb107a2 100644--- a/plugins/web-ui/test/split-tiles-and-titles.test.ts+++ b/plugins/web-ui/test/split-tiles-and-titles.test.ts@@ -21,7 +21,7 @@ test("the tile cap counts tiles and turns the overflow into a tab", () => {assert.ok(splitFn, "splitPane not found");assert.match(splitFn, /roomForAnotherPane\(\)/, "and it must bind splits too, not only tabs");assert.match(split, /function roomForAnotherPane\(\): boolean \{[\s\S]*?panels\.length \?\? 0\) < MAX_PANES/);- assert.ok(MAX_PANES > MAX_TILES * 2, "the ceiling must sit well clear of ordinary stacking");+ assert.ok(MAX_PANES >= MAX_TILES, "the conversation ceiling must allow every tile to be used");});test("a revived canvas is bounded by both caps and cannot blow the stack", () => {@@ -29,8 +29,9 @@ test("a revived canvas is bounded by both caps and cannot blow the stack", () =>const tiles = (...kids: object[]): object => ({ grid: { root: branch(...kids) } });assert.equal(serializedTileCount(tiles(leaf(["a", "b", "c"]), leaf(["d"]))), 2, "tabs are not tiles");+ assert.equal(serializedTileCount(tiles(branch(leaf(["a"]), leaf(["b"])), branch(leaf(["c"]), leaf(["d"])))), 4);assert.equal(- serializedTileCount(tiles(branch(leaf(["a"]), leaf(["b"])), branch(leaf(["c"]), leaf(["d"])))),+ serializedTileCount(tiles(...Array.from({ length: MAX_TILES }, (_, i) => leaf([String(i)])))),MAX_TILES,);assert.equal(serializedTileCount(null), 0);