Official

chat

Built-in chat UI (JavaScript UI plugin).

The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/chat@0.2.7

Permissions in 0.2.7

  • Control chats chats.controlHighCreates chats, sends prompts and cancels runs.Send prompts, answer agent questions, and create, change, fork, revert, stop and group chats
  • Run other plugins' commands commands.runHighRuns commands of other plugins.Run the sidebar's New chat command from the title bar
  • Read chats chats.readMediumReads your transcripts and chat lists.Show workspaces, tasks, chats and transcripts, and follow them as agents work
  • Read files fs.readMediumReads files in the listed places.Suggest workspace files for @ mentionsPlaces: the open workspace
  • Show panels ui.slotsAlways grantedShows views in the listed parts of the window.Draw the chat grid, the title bar, and the Session panel with its rail buttonSlots: centertitlerailright

Files

layout.test.ts15.6 KB
// The chat plugin's reading of the host's layouts: which pane is where,
// what shows after a close, and when a split has room.
import assert from "node:assert/strict";
import { test } from "node:test";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import type { Layout, Pane, Tab, Task } from "divergence/protocol";
import { canCloseTabs, canSplit, closeOtherTabs, focusedTarget, layoutKeys, neighbour, reopenTab, noteTask, emptyLayouts, showAfterClosing, showTab, splitPane, switchable, topPanes, forgetTask, isLaidOut } from "./layout.ts";
import { registeredPane } from "./state.ts";
import type { Controller } from "./controller.ts";
import { HostCallFailed } from "divergence/effect";
import { renderTaskCenter, renderTaskTitle } from "./views/layout.tsx";
import { paneReady, renderCenterPresentation } from "./views/panes.tsx";
import { chat as makeChat, harness, nodes } from "./ui-for-tests.test.ts";

const chat = (chatId: string) => ({ kind: "chat" as const, chatId });
const pane = (id: string, ...chats: string[]): Layout & Pane => ({ kind: "pane", id, tabs: chats.map(chat), active: chat(chats[0]!) });
const split = (id: string, direction: "row" | "column", ...children: Layout[]): Layout => ({
  kind: "split",
  id,
  direction,
  children: children.map((layout) => ({ share: 1 / children.length, layout })),
});
const task = (root: Layout, focused: string): Task => ({
  id: "t",
  layout: { root, focused },
  createdAt: "2026-11-01T00:00:00Z",
  updatedAt: "2026-11-01T00:00:00Z",
});

// a | b over c
const grid = task(split("s1", "row", pane("p1", "a", "a2"), split("s2", "column", pane("p2", "b"), pane("p3", "c"))), "p1");

test("the panes along the top are the ones whose strips sit in the title strip", () => {
  assert.deepEqual(topPanes(grid.layout.root).map((p) => p.id), ["p1", "p2"]);
});

test("closing the tab on screen shows its neighbour, or the pane that takes its place", () => {
  assert.equal(showAfterClosing(grid, chat("a")), "a2", "the right neighbour");
  assert.equal(showAfterClosing(grid, chat("a2")), "a", "the left one at the end");
  assert.equal(showAfterClosing(grid, chat("b")), "a", "a pane's last tab gives way to another pane");
  const file = { kind: "item" as const, id: "f", owner: "tabs", itemKind: "tabs.file", key: "f", title: "main.rs" };
  const beside = task(split("s", "row", { kind: "pane", id: "p1", tabs: [chat("a"), file, chat("a3")], active: chat("a") }, pane("p2", "b")), "p1");
  assert.equal(showAfterClosing(beside, chat("a")), "b", "a file next to it shows in its pane, and the chat another pane shows is selected");
  const alone = task({ kind: "pane", id: "p1", tabs: [chat("a"), file, chat("a3")], active: chat("a") }, "p1");
  assert.equal(showAfterClosing(alone, chat("a")), "a3", "with no other pane, the nearest chat of its own pane");
  assert.equal(showAfterClosing(grid, chat("x")), "a", "a chat of the side split gives way to a chat of the task");
});

test("focus moves to the neighbouring pane by the tree", () => {
  assert.equal(neighbour(grid.layout.root, "p1", "right"), "p2");
  assert.equal(neighbour(grid.layout.root, "p2", "down"), "p3");
  assert.equal(neighbour(grid.layout.root, "p3", "left"), "p1");
  assert.equal(neighbour(grid.layout.root, "p1", "left"), null);
});

test("a split needs room for two halves, however many panes the task has", () => {
  const one = task(pane("p", "a"), "p");
  assert.equal(canSplit(one, "right", { width: 240, height: 600 }), true);
  assert.equal(canSplit(one, "right", { width: 230, height: 600 }), false, "two halves under 120px");
  assert.equal(canSplit(one, "down", { width: 800, height: 190 }), false, "two halves under 100px");
  const four = task(split("s", "row", pane("p1", "a"), pane("p2", "b"), pane("p3", "c"), pane("p4", "d")), "p1");
  assert.equal(canSplit(four, "right", { width: 4000, height: 4000 }), true, "a fifth pane and more");
});

test("the homes follow each task the host sends, and a chat row draws as it always did", () => {
  const layouts = emptyLayouts();
  noteTask(layouts, grid);
  assert.equal(layouts.homes["c"], "t");
  noteTask(layouts, task(pane("p1", "a"), "p1"));
  assert.equal(layouts.homes["c"], undefined, "a chat that left the task has no home here");
  assert.equal(isLaidOut(layouts.tasks["t"]!), false);
  forgetTask(layouts, "t");
  assert.deepEqual(layouts.homes, {});
});

// The plugin's view of the layouts with a chat on screen, as the commands read it.
function onScreen(chatId: string, layouts: Partial<ReturnType<typeof emptyLayouts>>, drafts: string[] = []): Controller {
  const chats = Object.fromEntries(drafts.map((id) => [id, { id }]));
  return {
    api: { app: { state: () => ({ chatId }) } },
    state: { layouts: { ...emptyLayouts(), ...layouts }, chats },
    isDraft: (chat: { id: string }) => drafts.includes(chat.id),
  } as unknown as Controller;
}
const side = (root: Layout) => ({ layout: { root, focused: (root as Pane).id }, remembered: [], open: true, width: 400 });

test("Ctrl+Tab cycles through the task's chat tabs, never a chat in the side split", () => {
  const c = onScreen("a", { tasks: { t: grid }, homes: { a: "t", a2: "t", b: "t", c: "t" }, side: side(pane("s", "x")) });
  const allowed = switchable(c);
  assert.deepEqual(["a", "b", "c", "other", "x"].filter(allowed), ["a", "b", "c"]);
  const alone = onScreen("solo", { tasks: { t: task(pane("p", "solo"), "p") }, homes: { solo: "t" }, side: side(pane("s", "x")) });
  assert.deepEqual(["solo", "other", "x"].filter(switchable(alone)), ["solo", "other"], "a lone chat switches among every chat but the side split's");
});

test("Ctrl+Tab from a task of several tabs also reaches the drafts, but not one the side split shows", () => {
  const c = onScreen("a", { tasks: { t: grid }, homes: { a: "t", a2: "t", b: "t", c: "t" }, side: side(pane("s", "x")) }, ["draft", "x"]);
  assert.deepEqual(["a", "b", "draft", "other", "x"].filter(switchable(c)), ["a", "b", "draft"]);
});

test("Cmd+W acts on the pane last focused, the side split's too, and a chat's only tab cannot close", () => {
  const solo = task(pane("p", "solo"), "p");
  const c = onScreen("solo", { tasks: { t: solo }, homes: { solo: "t", x: "t" }, side: side(pane("s", "x")) });
  assert.equal(focusedTarget(c)?.pane.id, "p");
  assert.equal(canCloseTabs(focusedTarget(c)!.task), false, "the only tab of a chat row stays");
  c.state.layouts.sideFocused = true;
  assert.equal(focusedTarget(c)?.pane.id, "s");
  assert.equal(canCloseTabs(focusedTarget(c)!.task), true);
});

test("the pane keys and the layout keys never reach a side split tab that does not show with the task on screen", () => {
  // The side split's focused pane holds only a chat of another task, so it is not drawn beside this one.
  const solo = task(pane("p", "solo"), "p");
  const sideRoot = split("ss", "column", pane("s1", "mine"), pane("s2", "theirs"));
  const c = onScreen("solo", { tasks: { t: solo }, homes: { solo: "t", mine: "t", theirs: "other" }, side: { ...side(sideRoot), layout: { root: sideRoot, focused: "s2" } } });
  c.state.layouts.sideFocused = true;
  assert.equal(focusedTarget(c)?.pane.id, "s1", "the side split's pane that shows takes the keys");
  assert.deepEqual(layoutKeys(c), { tabKind: "chat", tabInSideSplit: true, paneCount: 1, taskOnScreen: false });
  c.state.layouts.homes.mine = "other";
  assert.equal(focusedTarget(c)?.pane.id, "p", "with nothing of this task in the side split, the task's pane does");
  assert.equal(layoutKeys(c).tabInSideSplit, false);
});

test("a late pane-focus reply cannot replace the chat selected by the following tab click", async () => {
  const replies = { a: Deferred.makeUnsafe<void>(), b: Deferred.makeUnsafe<void>() };
  const updates: { paneId: string; active: { kind: "chat"; chatId: "a" | "b" }; focused: boolean; onScreen: string }[] = [];
  const h = harness((method, params) => {
    if (method !== "host/layouts.update") return;
    const update = params as (typeof updates)[number];
    updates.push(update);
    return Deferred.await(replies[update.active.chatId]).pipe(Effect.as({}));
  });
  try {
    for (const [index, id] of ["base", "a", "b"].entries()) {
      h.controller.state.chats[id] = makeChat({ id });
      h.controller.transcript(id).snapshot = { kind: "ready" };
      h.controller.composer(id);
      h.controller.state.panes[id] = { chatId: id, view: { id: index + 1, update() {}, remove() {} }, drawn: true, ready: true };
    }
    const layout = task(split("s", "row", pane("p1", "base"), pane("p2", "a", "b")), "p1");
    noteTask(h.controller.state.layouts, layout);
    Effect.runSync(h.controller.app.update({ route: "chat", chatId: "base", workspaceId: "w" }));
    const target = nodes(renderTaskCenter(h.v, layout)).find((node) => node.id === "task-pane-p2")!;
    (target.onPointerDown as () => void)();
    nodes(renderTaskTitle(h.v, layout, null, undefined, false)).find((node) => node.id === "tab-chat-b")!.onClick();
    await new Promise<void>((resolve) => setImmediate(resolve));
    assert.deepEqual(updates, [
      { paneId: "p2", active: chat("a"), focused: true, onScreen: "t" },
      { paneId: "p2", active: chat("b"), focused: true, onScreen: "t" },
    ], "both real callbacks persist their original task and tab intent");
    await Effect.runPromise(h.controller.followApp());
    assert.deepEqual(
      { chatId: h.controller.api.app.state().chatId, focused: h.focused.at(-1) },
      { chatId: "b", focused: "prompt-b" },
      "selection and the real focus path do not wait for persistence",
    );
    Effect.runSync(Deferred.succeed(replies.b, undefined));
    await new Promise<void>((resolve) => setImmediate(resolve));
    await Effect.runPromise(h.controller.followApp());
    assert.equal(h.focused.at(-1), "prompt-b");
    Effect.runSync(Deferred.succeed(replies.a, undefined));
    await h.settle();
    await Effect.runPromise(h.controller.followApp());
    assert.deepEqual(
      { chatId: h.controller.api.app.state().chatId, focused: h.focused.at(-1) },
      { chatId: "b", focused: "prompt-b" },
      "reply completion never owns selection or composer focus",
    );
    noteTask(h.controller.state.layouts, task(layout.layout.root, "p2"));
    const stale = h.controller.state.layouts.tasks.t!;
    const shown = nodes(renderCenterPresentation(h.v, "b")).find((node) => node.id === "task-pane-p2")!;
    assert.equal(shown.children.find((node) => node.type === "view")?.view, 3, "a stale active-tab publication still projects the selected chat");
    const strip = nodes(renderTaskTitle(h.v, stale, null, "b", false)).find((node) => node.id === "tab-surface-p2")!;
    assert.equal(strip.follows, "tab-chat-b", "the selected tab surface follows the same projection");
  } finally {
    Effect.runSync(Deferred.succeed(replies.a, undefined));
    Effect.runSync(Deferred.succeed(replies.b, undefined));
    await h.close();
  }
});

test("an older failed layout save propagates its error without undoing a newer selection", async () => {
  const reply = Deferred.makeUnsafe<void>();
  const error = new HostCallFailed({ method: "host/layouts.update", code: -32000, message: "The layout could not be saved" });
  const h = harness((method, params) => method === "host/layouts.update" && (params as { active: { chatId: string } }).active.chatId === "a"
    ? Deferred.await(reply).pipe(Effect.andThen(Effect.fail(error))) : undefined);
  try {
    h.controller.state.chats.a = makeChat({ id: "a" });
    h.controller.state.chats.b = makeChat({ id: "b" });
    const failed = Effect.runPromise(showTab(h.controller, "p", chat("a")).pipe(Effect.flip));
    await Effect.runPromise(showTab(h.controller, "p", chat("b")));
    Effect.runSync(Deferred.succeed(reply, undefined));
    assert.equal(await failed, error);
    assert.equal(h.controller.api.app.state().chatId, "b");
  } finally {
    Effect.runSync(Deferred.succeed(reply, undefined));
    await h.close();
  }
});

test("selecting an item tab persists its pane without changing the selected chat", async () => {
  const h = harness();
  try {
    Effect.runSync(h.controller.app.update({ chatId: "a" }));
    const tab = { kind: "item" as const, id: "terminal", owner: "terminal", itemKind: "terminal.shell", key: "shell", title: "Terminal", chatId: "a" };
    await Effect.runPromise(showTab(h.controller, "p", tab));
    assert.equal(h.controller.api.app.state().chatId, "a");
    assert.deepEqual(h.calls.find((call) => call.method === "host/layouts.update")?.params, {
      paneId: "p", active: tab, focused: true, onScreen: null,
    });
  } finally {
    await h.close();
  }
});

test("a split selects its new chat only once that chat's pane is ready, so the center never passes through a skeleton", async () => {
  const selectedAtRead: (string | undefined)[] = [];
  const h = harness((method) => {
    if (method === "host/layouts.split") return Effect.succeed({ chat: makeChat({ id: "new" }) });
    if (method !== "host/chats.transcript") return undefined;
    selectedAtRead.push(h.controller.api.app.state().chatId);
    return Effect.succeed({ items: [], start: 0, pendingApprovals: [], pendingQuestions: [], plan: null, running: false });
  });
  try {
    h.controller.state.chats.a = makeChat({ id: "a" });
    noteTask(h.controller.state.layouts, task(pane("p1", "a"), "p1"));
    Effect.runSync(h.controller.app.update({ route: "chat", chatId: "a", workspaceId: "w" }));
    await Effect.runPromise(splitPane(h.controller, "p1", "right"));
    assert.deepEqual(selectedAtRead, ["a"], "the new chat's transcript is read before it is selected");
    assert.equal(h.controller.api.app.state().chatId, "new");
    assert.ok(registeredPane(h.controller.state.panes.new), "its pane is made before it shows");
    assert.equal(paneReady(h.controller, "new"), true, "and is ready to draw");
  } finally {
    await h.close();
  }
});

test("Close Other Tabs closes only the tabs its pane shows and keeps the center on the kept chat, and a refused reopen says why", async () => {
  const closed: Tab[] = [];
  const reopens: unknown[] = [];
  const h = harness((method, params) => {
    if (method === "host/layouts.close_tab") {
      closed.push((params as { tab: Tab }).tab);
      return Effect.succeed({ changed: [], closed: null, question: null });
    }
    if (method !== "host/layouts.reopen") return undefined;
    reopens.push(params);
    return Effect.fail(new HostCallFailed({ method, code: -32000, message: "That chat has moved or run since it closed" }));
  });
  try {
    for (const id of ["a", "b", "c", "theirs"]) h.controller.state.chats[id] = makeChat({ id });
    noteTask(h.controller.state.layouts, task(pane("p1", "a"), "p1"));
    Object.assign(h.controller.state.layouts.homes, { b: "t", c: "t", theirs: "other" });
    h.controller.state.layouts.side = side({ kind: "pane", id: "s", tabs: [chat("b"), chat("theirs"), chat("c")], active: chat("b") });
    Effect.runSync(h.controller.app.update({ route: "chat", chatId: "b", workspaceId: "w" }));
    await Effect.runPromise(closeOtherTabs(h.controller, "s", chat("c")));
    assert.deepEqual(closed, [chat("b")], "a tab of another task in the same pane is not shown, and stays");
    assert.equal(h.controller.api.app.state().chatId, "c", "the chat on screen closed, so the kept one shows");

    h.controller.state.layouts.closed.t = [
      { chatId: "b", home: "t", paneId: "s", index: 0 },
      { chatId: "a", home: "t", paneId: "p1", index: 0 },
    ];
    await Effect.runPromise(reopenTab(h.controller));
    assert.deepEqual(h.notices, ["The tab could not be reopened: That chat has moved or run since it closed"]);
    assert.equal(reopens.length, 1, "an older closed tab does not come back in its place");
    assert.equal(h.controller.api.app.state().chatId, "c");
    assert.deepEqual(h.controller.state.layouts.closed.t.map((entry) => entry.chatId), ["a"], "the next press takes the one closed before");
  } finally {
    await h.close();
  }
});

Versions

VersionPublishedPlugin APISizePermissionsStatus
0.2.9latestNov 21, 2026>=5 <6349 KB5 permissionsListed
0.2.8Nov 16, 2026>=5 <6348.6 KB5 permissionsListed
0.2.7Nov 11, 2026>=5 <6314.3 KB5 permissionsListed
0.2.6Nov 9, 2026>=5 <6306.2 KB5 permissionsListed
0.2.5Oct 27, 2026>=2 <4178.6 KB5 permissionsListed

Reviews and comments

0 threads · 0 reviews

No comments yet.