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.8
Permissions in 0.2.8
Files
main.ts8.7 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)),
prepareNewChat: ({ chat }) => c.prepareNewChat(chat).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
| Version | Published | Plugin API | Size | Permissions | Status |
|---|---|---|---|---|---|
| 0.2.9latest | Nov 21, 2026 | >=5 <6 | 349 KB | 5 permissions | Listed |
| 0.2.8 | Nov 16, 2026 | >=5 <6 | 348.6 KB | 5 permissions | Listed |
| 0.2.7 | Nov 11, 2026 | >=5 <6 | 314.3 KB | 5 permissions | Listed |
| 0.2.6 | Nov 9, 2026 | >=5 <6 | 306.2 KB | 5 permissions | Listed |
| 0.2.5 | Oct 27, 2026 | >=2 <4 | 178.6 KB | 5 permissions | Listed |
No comments yet.