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

main.ts8.6 KB
// Registers the title, the center and the Session panel synchronously, so retained views keep their identity across reloads.
// The divergence/chat service is provided last; the tabs client is
// followed while its provider runs. The plugin scope owns every listener,
// background load and elapsed-time loop.
import * as Cause from "effect/Cause";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Stream from "effect/Stream";
import * as z from "zod";
import { App, Host, Kernel, Layouts, Notify, Plugin, Services, Settings, Views, runPlugin } from "divergence/effect";
import type { TabContext } from "divergence";
import { renderSubagentPane, renderWorkflowPane } from "./views/tasks.tsx";
import { NEW_TAB, renderNewTab } from "./newtab.tsx";
import { taskTabTitle } from "./panes.ts";
import { Controller } from "./controller.ts";
import { Chat as ChatService } from "./contract.ts";
import type { Tabs } from "../tabs/contract.ts";
import { createModelPicker } from "./models.tsx";
import { registerPresentationRoutes } from "./presentations.tsx";
import { loadLayouts } from "./layout.ts";
import { loadOpenIn } from "./open-in.ts";
import { renderSessionPanel, renderSessionRail } from "./views/session.tsx";
import { registerEarlyCommands } from "./commands.ts";
import { registerAppCommands } from "./app-commands.ts";
import { registerKeyboardCommands } from "./keyboard-commands.ts";
import { listen } from "./events.ts";
import { InputScheduler } from "./scheduler-contract.ts";
import { bindSettings, renderSettings } from "./settings.tsx";
import { tick, type Drawn } from "./clock.ts";
import { ChatContext } from "./context-contract.ts";
import { transcriptPolicy } from "./context-service.ts";
const make = Effect.fn("Chat.controller")(function* () {
  return new Controller({
    api: (yield* Plugin).api,
    host: yield* Host,
    kernel: yield* Kernel,
    app: yield* App,
    notify: yield* Notify,
    views: yield* Views,
    scope: yield* Effect.scope,
  });
});
class ChatController extends Context.Service<ChatController, Controller>()("chat/Controller") {
  static readonly layer = Layer.effect(ChatController, make());
}
const program = Effect.gen(function* () {
  const c = yield* ChatController;
  yield* registerEarlyCommands(c);
  yield* registerAppCommands(c);
  yield* registerKeyboardCommands(c);
  c.presentations = yield* registerPresentationRoutes(c);
  // A subagent and a workflow are tab kinds of this plugin, each opened with
  // its chat, so they show with that chat's task (specs/tasks-and-splits/05).
  const tabView = (ctx: TabContext<object> & { run: (effect: never) => void }) => ({ controller: c, ctx: ctx as never });
  yield* (yield* Layouts).kinds({
    subagent: {
      needsChat: true,
      opens: "side",
      icon: "bot",
      title: (key: string) => taskTabTitle(c, key, "Subagent"),
      render: (ctx) => (ctx.chatId ? renderSubagentPane(tabView(ctx), ctx.chatId, ctx.key) : null),
    },
    workflow: {
      needsChat: true,
      opens: "side",
      icon: "workflow",
      title: (key: string) => taskTabTitle(c, key, "Workflow"),
      render: (ctx) => (ctx.chatId ? renderWorkflowPane(tabView(ctx), ctx.chatId, ctx.key) : null),
    },
    // What a strip's + opens, and the empty side split's page.
    [NEW_TAB]: {
      opens: "side",
      icon: "plus-large",
      title: "New tab",
      render: (ctx) => renderNewTab(tabView(ctx), ctx.key),
    },
  });
  // The Session panel, under Files and Source Control on the rail. It reads
  // the chat in the center; the listeners render it when that chat's
  // options, status, usage or subagents change (`updateSession`).
  c.sessionViews.push(
    yield* c.views.add("rail", { slot: "rail", render: (ctx) => renderSessionRail({ controller: c, ctx }), on: ["app_state"], order: 30 }),
  );
  c.sessionViews.push(
    yield* c.views.add("right", { slot: "right", render: (ctx) => renderSessionPanel({ controller: c, ctx }), on: ["app_state"], order: 30, title: "Session" }),
  );
  yield* listen(c);
  yield* c.followAppChanges();
  const services = yield* Services;
  // Detail views need the tabs plugin; without it they stay closed, as the
  // shared request they replaced would have had no reader.
  yield* services.when<typeof Tabs>("divergence/tabs").pipe(
    Stream.runForEach((option) =>
      Effect.sync(() => {
        c.tabs = option._tag === "Some" ? option.value : undefined;
      }),
    ),
    Effect.forkScoped({ startImmediately: true }),
  );
  const fork = <A, E>(effect: Effect.Effect<A, E>) => Effect.forkIn(effect, c.scope, { startImmediately: true });
  c.modelPicker = createModelPicker(c);
  yield* fork(c.modelPicker.load);
  yield* fork(c.loadRecent());
  yield* fork(loadOpenIn(c));
  yield* fork(c.preloadChats());
  yield* fork(loadLayouts(c).pipe(Effect.catch((error) => c.notify.error(`The task layouts could not be read: ${error.message}`))));
  yield* fork(c.restoreQueues());
  yield* fork(c.restoreUnsent());
  // The service goes last: providing waits for the host, and the loads above
  // must not wait for it. A consumer reads the state with queued() and
  // draftTitles() once the service is there; events count from then on.
  const handle = yield* services.provide(ChatService, {
    queued: () => Effect.succeed(c.state.chatQueue.map((queued) => queued.chatId)),
    draftTitles: () => Effect.succeed({ ...c.state.draftTitles }),
    starting: () => Effect.succeed([...c.state.starting]),
    submitDraft: ({ chatId, queue }) => c.submitDraft(chatId, queue),
    unqueue: ({ chatId }) => c.unqueueChat(chatId).pipe(Effect.as(null)),
    prefetch: ({ chatId }) => c.prefetch(chatId).pipe(Effect.as(null)),
    quote: ({ text }) => c.quoteInOpenChat(text).pipe(Effect.as(null)),
  });
  c.emitQueued = (queued) => handle.emit("queuedChanged", queued);
  c.emitDrafts = (drafts) => handle.emit("draftTitlesChanged", drafts);
  c.emitStarting = (starting) => handle.emit("startingChanged", starting);
  // Restores forked above may have set queue/draft state before the service
  // existed, when their announces were no-ops. Emit what they found, like
  // the baseline's shares did, so consumers see the restored state.
  // Restores still running will emit themselves, since the service is up;
  // this only covers what is already there, so there is no duplicate.
  if (c.state.chatQueue.length) c.emitQueued(c.state.chatQueue.map((queued) => queued.chatId));
  if (Object.keys(c.state.draftTitles).length) c.emitDrafts({ ...c.state.draftTitles });
  yield* services.provide(InputScheduler, {
    policy: () => Effect.succeed({ version: 1 as const, release: "settled-success-fifo" as const }),
  });
  yield* services.provide(ChatContext, transcriptPolicy(c.api)).pipe(
    Effect.catchCause((cause) => {
      const reason = cause.reasons.length === 1 ? cause.reasons[0] : undefined;
      // The host keeps our pinned renderer even when a selected replacement
      // prepares new context. Losing that selection must not stop the chat UI.
      if (
        reason &&
        Cause.isDieReason(reason) &&
        z.object({ tag: z.literal("ProvidedElsewhere"), service: z.literal(ChatContext.name) }).safeParse(reason.defect)
          .success
      )
        return Effect.void;
      return Effect.failCause(cause);
    }),
  );
  yield* fork(c.loadContextPolicy());
  c.settingsView = yield* c.views.add("chat-settings", { slot: "chat-settings", render: (ctx) => renderSettings({ controller: c, ctx }), on: ["agents_changed", "plugin_settings_changed"] });
  yield* Effect.acquireRelease(
    Effect.sync(() => bindSettings(c.settingsView?.id)),
    () => Effect.sync(() => bindSettings(undefined)),
  );
  const settings = yield* Settings;
  yield* settings.changes.pipe(
    Stream.runForEach(() => fork(c.scheduler.updatePreferences().pipe(Effect.andThen(c.loadContextPolicy())))),
    Effect.forkScoped({ startImmediately: true }),
  );
  yield* fork(c.scheduler.restore());
  // A bounded scoped clock reconstructs deadlines after reload and catches
  // sleep/wake. It never polls with prompts. A decision that fails is logged,
  // and the next second decides again.
  yield* fork(
    Effect.forever(
      Effect.sleep("1 second").pipe(
        Effect.andThen(c.scheduler.evaluate()),
        Effect.catchCause((cause) => Effect.logError("The prompt scheduler could not decide", cause)),
      ),
    ),
  );
  // The time on screen has its own clock: the scheduler's decisions wait on
  // the host, and a wait there must not stop a working row from counting.
  const drawn: Drawn = new Map();
  yield* fork(Effect.forever(Effect.sleep("1 second").pipe(Effect.andThen(Effect.sync(() => tick(c, drawn))))));
});
export default runPlugin(ChatController.layer, program);

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.