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
Files
presentations.tsx11 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 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>>();
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");
}
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]));
}
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)]] : [];
}),
);
}
const chatSelector = (c: Controller, fallback: ViewHandle, kind: "center" | "title"): Element =>
<view view={fallback.id} select={{ key: "chatId", cases: chatCases(c, kind) }} />;
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
| 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.