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.9

Permissions in 0.2.9

  • 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, and its Close Chat and Reopen Closed Chat for Cmd+W and Cmd+Shift+T on a chat's only tab
  • 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

presentations.tsx12.3 KB
// Native app-state routing among complete retained chat presentations. The
// ordinary dynamic views remain the fallback for cold chats and older hosts.
import * as Effect from "effect/Effect";
import * as Deferred from "effect/Deferred";
import type { Element, ViewHandle } from "divergence";
import type { Controller } from "./controller.ts";
import { presentationChats, presentationLayout, tabsOf } from "./layout.ts";
import type { PresentationLayout } from "./layout.ts";
import { prehydratePresentation } from "./panes.ts";
import { renderCenter, renderCenterPresentation, renderSwitcher } from "./views/panes.tsx";
import { renderTitle, renderTitlePresentation } from "./views/title.tsx";
import { registeredPane } from "./state.ts";

type Updatable = Pick<ViewHandle, "update">;
type Part = "center" | "title";
type Dependencies = { layout: PresentationLayout; side: string; panes: Set<string>; members: Set<string> };
type Destination = { dependencies: Dependencies; published: Dependencies | null };
const complete = (c: Controller, panes: Iterable<string>) => [...panes].every((id) => c.state.panes[id]?.ready);

function dependencies(c: Controller, chatId: string): Dependencies {
  const layout = presentationLayout(c, chatId);
  const { task, side } = layout;
  const members = new Set([chatId]);
  for (const root of [task?.layout.root, side?.task.layout.root])
    if (root) for (const tab of tabsOf(root)) if (tab.chatId) members.add(tab.chatId);
  return {
    layout,
    side: JSON.stringify({
      visible: side && { layout: side.task.layout, width: side.split.width, resized: side.split.resized },
      toggle: layout.hasSideTabs,
    }),
    panes: presentationChats(c, chatId),
    members,
  };
}

const target = (view: ViewHandle, focus?: string) => (focus ? { view: view.id, focus } : { view: view.id });

export class PresentationRoutes {
  readonly center: Updatable;
  readonly title: Updatable;
  private readonly c: Controller;
  private readonly centerFallback: ViewHandle;
  private readonly centerCases: ViewHandle;
  private readonly titleFallback: ViewHandle;
  private readonly titleCases: ViewHandle;
  private readonly titleOffRoute: ViewHandle;
  private readonly destinations = new Map<string, Destination>();
  private readonly pending = new Map<string, Set<Part>>();
  private selectable: { chatId: string; drawn: Deferred.Deferred<void> } | null = null;

  constructor(
    c: Controller,
    centerFallback: ViewHandle,
    centerCases: ViewHandle,
    titleFallback: ViewHandle,
    titleCases: ViewHandle,
    titleOffRoute: ViewHandle,
  ) {
    this.c = c;
    this.centerFallback = centerFallback;
    this.centerCases = centerCases;
    this.titleFallback = titleFallback;
    this.titleCases = titleCases;
    this.titleOffRoute = titleOffRoute;
    this.center = {
      update: () => {
        centerFallback.update();
        const chatId = c.api.app.state().chatId;
        if (chatId) this.update(chatId, "center");
      },
    };
    this.title = {
      update: () => {
        titleFallback.update();
        const chatId = c.api.app.state().chatId;
        if (chatId) this.update(chatId, "title");
      },
    };
  }

  updateTitle(chatId: string): void {
    const selected = this.c.api.app.state().chatId;
    for (const [id, entry] of this.destinations) {
      if (!entry.dependencies.members.has(chatId)) continue;
      if (id === selected) this.titleFallback.update();
      this.update(id, "title");
    }
  }

  updateAllTitles(): void {
    this.titleFallback.update();
    for (const id of this.destinations.keys()) this.update(id, "title");
  }

  /** Every strip, the top panes' in the titles and the lower panes' in the centers, hidden ones too: a tab shown while
   * Alt is still held already shows the numbers. */
  updateStrips(): void {
    this.centerFallback.update();
    this.titleFallback.update();
    for (const id of this.destinations.keys()) {
      this.update(id, "title");
      this.update(id, "center");
    }
  }

  updateSwitcher(): void {
    this.title.update();
    this.titleOffRoute.update();
  }

  updateCases(): void {
    this.centerCases.update();
    this.titleCases.update();
  }

  /** The native trees keep their published dependencies while a replacement loads. */
  canSelect(chatId: string): boolean {
    const published = this.destinations.get(chatId)?.published;
    return !!published && [...published.panes].every((id) => registeredPane(this.c.state.panes[id]));
  }

  /** Done once the center selector drawn for the kernel holds this chat's case, so app state naming it shows it natively.
   * One chat waits at a time, the next new chat: asking for another lets the earlier wait go. */
  whenSelectable(chatId: string): Effect.Effect<void> {
    if (this.selectable) Deferred.doneUnsafe(this.selectable.drawn, Effect.void);
    const drawn = Deferred.makeUnsafe<void>();
    this.selectable = { chatId, drawn };
    this.centerCases.update();
    return Deferred.await(drawn);
  }

  centerCasesDrawn(cases: Record<string, unknown>): void {
    if (!this.selectable || !cases[this.selectable.chatId]) return;
    Deferred.doneUnsafe(this.selectable.drawn, Effect.void);
    this.selectable = null;
  }

  layoutFor(chatId: string): PresentationLayout | undefined {
    const entry = this.destinations.get(chatId);
    return entry?.published?.layout ?? entry?.dependencies.layout;
  }

  private update(chatId: string, part: Part): void {
    const entry = this.destinations.get(chatId);
    if (!entry) return;
    const parts = this.pending.get(chatId) ?? new Set<Part>();
    parts.add(part);
    this.pending.set(chatId, parts);
    if (!this.publish(chatId) && entry.published) {
      const pane = registeredPane(this.c.state.panes[chatId]);
      if (part === "center") pane?.centerPresentation?.update();
      else pane?.titlePresentation?.update();
    }
  }

  private publish(chatId: string): boolean {
    const entry = this.destinations.get(chatId)!;
    if (!complete(this.c, entry.dependencies.panes)) return false;
    const pane = registeredPane(this.c.state.panes[chatId]);
    if (!pane) return false;
    const parts = entry.published === entry.dependencies ? this.pending.get(chatId)! : new Set<Part>(["center", "title"]);
    entry.published = entry.dependencies;
    this.pending.delete(chatId);
    for (const kind of parts) {
      if (kind === "center") pane.centerPresentation?.update();
      else pane.titlePresentation?.update();
    }
    return true;
  }

  /** A bounded index of task, side-split and pane dependencies, never a tree cache. */
  updateLayouts(taskId?: string): void {
    const selected = this.c.api.app.state().chatId;
    for (const [chatId, pane] of Object.entries(this.c.state.panes)) {
      if (!registeredPane(pane)) continue;
      const previous = this.destinations.get(chatId);
      if (taskId && previous?.dependencies.layout.task?.id !== taskId && this.c.state.layouts.homes[chatId] !== taskId) continue;
      const next = dependencies(this.c, chatId);
      if (previous && previous.dependencies.layout.task === next.layout.task && previous.dependencies.side === next.side) continue;
      this.destinations.set(chatId, { dependencies: next, published: previous?.published ?? null });
      this.pending.set(chatId, new Set(["center", "title"]));
      this.publish(chatId);
      if (chatId === selected) {
        this.centerFallback.update();
        this.titleFallback.update();
      }
      if (this.pending.has(chatId))
        Effect.runFork(Effect.forkIn(prehydratePresentation(this.c, chatId), this.c.scope, { startImmediately: true }));
    }
  }

  /** Registration and readiness affect only presentations that embed this pane. */
  paneChanged(chatId: string): void {
    if (!this.destinations.has(chatId) && registeredPane(this.c.state.panes[chatId])) this.updateLayouts();
    for (const id of [...this.pending.keys()]) {
      if (!this.destinations.get(id)?.dependencies.panes.has(chatId)) continue;
      this.publish(id);
    }
    const selected = this.c.api.app.state().chatId;
    if (selected && this.destinations.get(selected)?.dependencies.panes.has(chatId)) this.centerFallback.update();
    this.updateCases();
  }

  remove(chatId: string): void {
    this.destinations.delete(chatId);
    this.pending.delete(chatId);
    for (const [id, entry] of this.destinations) {
      if (!entry.published?.panes.has(chatId)) continue;
      entry.published = null;
      this.pending.set(id, new Set(["center", "title"]));
    }
    this.updateCases();
  }
}

function chatCases(c: Controller, kind: "center" | "title") {
  return Object.fromEntries(
    Object.entries(c.state.panes).flatMap(([chatId, entry]) => {
      const pane = registeredPane(entry);
      if (!pane) return [];
      const view = kind === "center" ? pane.centerPresentation : pane.titlePresentation;
      const ready = kind === "center" ? pane.centerPresented : pane.titlePresented;
      const complete = c.presentations?.canSelect(chatId);
      return view && ready && complete ? [[chatId, target(view, kind === "center" ? `prompt-${chatId}` : undefined)]] : [];
    }),
  );
}

function chatSelector(c: Controller, fallback: ViewHandle, kind: "center" | "title"): Element {
  const cases = chatCases(c, kind);
  if (kind === "center") c.presentations?.centerCasesDrawn(cases);
  return <view view={fallback.id} select={{ key: "chatId", cases }} />;
}

const routeSelector = (fallback: ViewHandle, chat: ViewHandle, otherwise = { view: 0 }): Element =>
  <view view={fallback.id} select={{ key: "route", cases: { chat: target(chat) }, default: otherwise }} />;

export const registerPresentationRoutes = Effect.fn("Chat.registerPresentationRoutes")(function* (c: Controller) {
  let centerFallback: ViewHandle | undefined;
  let centerCases: ViewHandle | undefined;
  const centerRoot = yield* c.views.add("center", {
    slot: "center",
    on: ["plugin_changed"],
    render: (ctx) => {
      c.keepTranscripts(ctx.state);
      return centerFallback && centerCases
        ? routeSelector(centerFallback, centerCases)
        : renderCenter({ controller: c, ctx });
    },
  });
  centerFallback = yield* c.views.add("center-fallback", {
    slot: "center-presentation",
    render: (ctx) => renderCenter({ controller: c, ctx }),
    on: ["chat_changed", "agents_changed", "app_state"],
  });
  centerCases = yield* c.views.add("center-cases", {
    slot: "center-presentation",
    on: ["plugin_changed"],
    render: () => chatSelector(c, centerFallback, "center"),
  });
  centerRoot.update();

  const titleFallback = yield* c.views.add("title-fallback", {
    slot: "title-presentation",
    render: (ctx) => renderTitle({ controller: c, ctx }),
    on: ["chat_changed", "agents_changed", "app_state", "workspaces_changed"],
  });
  const titleCases = yield* c.views.add("title-cases", {
    slot: "title-presentation",
    on: ["plugin_changed"],
    render: () => chatSelector(c, titleFallback, "title"),
  });
  const titleOffRoute = yield* c.views.add("title-off-route", {
    slot: "title-presentation",
    render: (ctx) => renderSwitcher({ controller: c, ctx }),
  });
  yield* c.views.add("title", {
    slot: "title",
    on: ["plugin_changed"],
    render: () => routeSelector(titleFallback, titleCases, target(titleOffRoute)),
  });
  return new PresentationRoutes(c, centerFallback, centerCases, titleFallback, titleCases, titleOffRoute);
});

export function centerPresentation(c: Controller, chatId: string) {
  return c.views.add(`center:${chatId}`, {
    slot: "center-presentation",
    on: ["plugin_changed"],
    render: (ctx) => {
      const tree = renderCenterPresentation({ controller: c, ctx }, chatId, c.presentations?.layoutFor(chatId));
      const pane = registeredPane(c.state.panes[chatId]);
      if (pane && !pane.centerPresented && complete(c, presentationChats(c, chatId))) {
        pane.centerPresented = true;
        c.presentations?.updateCases();
      }
      return tree;
    },
  });
}

export function titlePresentation(c: Controller, chatId: string) {
  return c.views.add(`title:${chatId}`, {
    slot: "title-presentation",
    on: ["plugin_changed"],
    render: (ctx) => {
      const tree = renderTitlePresentation({ controller: c, ctx }, chatId, c.presentations?.layoutFor(chatId));
      const pane = registeredPane(c.state.panes[chatId]);
      if (pane && !pane.titlePresented && complete(c, presentationChats(c, chatId))) {
        pane.titlePresented = true;
        c.presentations?.updateCases();
      }
      return tree;
    },
  });
}

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.