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
Files
views/panes.tsx16.6 KB
// Panes rendering preserves the chat's retained tree and interaction keys.
import { VISIBLE, reconcile as reconcileCycle, visible } from "../switcher.ts";
import type { Chat, DivProps, Element } from "divergence";
import { chatMarkOf, chatRow } from "../../sdk/ui/chat-row.ts";
import { workspaceMarkOf } from "../../sdk/ui/workspace.ts";
import { registeredPane } from "../state.ts";
import { adoptSelected } from "../input-schema.ts";
import type { Pane } from "../state.ts";
import type { ChatView } from "../controller.ts";
import { renderTranscript, reading } from "./rows.tsx";
import {
dock,
landingChips,
landingHeadline,
renderComposer,
renderLanding,
risingFrom,
stageLook,
stageTop,
} from "./composer.tsx";
import { column, row, fill, full, TOP_FADE_HEIGHT } from "./styles.tsx";
import { panesOf } from "../layout.ts";
import type { PresentationLayout } from "../layout.ts";
import type { Task } from "divergence/protocol";
/** A chat row's one pane. */
const taskPane = (task: Task) => panesOf(task.layout.root)[0] ?? null;
import { drawsPanes, dropPreview, paneDrop, renderTaskCenter } from "./layout.tsx";
import { measureRoom, roomOf } from "./room.tsx";
import { renderFindBar } from "./find.tsx";
import { newChat } from "./title.tsx";
import { NEW_CHAT } from "../shortcuts.ts";
// Each chat's row as the chat search (⌘P) draws it, with the selection
// filled as the search fills its own.
export function switcherRow(v: ChatView, id: string, chosen: boolean, now: number): Element | null {
const { chats, workspaces, layouts } = v.controller.state;
const chat = chats[id];
if (!chat) return null;
const workspace = workspaces[chat.workspaceId];
const done = !!chat.task && !!layouts.tasks[chat.task]?.completedAt;
const frame = {
id: `switch-${id}`,
group: "hover",
paddingX: 10,
paddingY: 6,
borderRadius: "sm",
background: chosen ? "accent" : undefined,
hover: chosen ? undefined : { background: "hover" },
onClick: () => {
v.controller.state.switcher = { list: [id], selected: id, input: "overlay" };
v.ctx.run(v.controller.commitSwitcher());
},
} satisfies DivProps;
const title = (
<div {...row} {...fill} overflow="hidden">
<text style="body" size={13} tone="muted" truncate>
{v.controller.titleOf(chat) || "New Chat"}
</text>
</div>
);
const marks = { status: chatMarkOf(chat, done), updatedAt: chat.updatedAt, workspace: workspaceMarkOf(workspace) };
return chatRow(frame, title, marks, workspace?.name ?? "", now);
}
export function renderSwitcher(v: ChatView): Element | null {
// The list is checked against the chats the app still has: one closed
// while the cycle is open must not leave a row behind.
const open = reconcileCycle(v.controller.state.switcher, v.controller.state.chats);
v.controller.state.switcher = open;
const { rows } = open ? visible(open.list, open.selected, VISIBLE) : { rows: [] };
const now = Date.now();
const keys = {
up: () => v.controller.switchBy(-1),
left: () => v.controller.switchBy(-1),
down: () => v.controller.switchBy(1),
right: () => v.controller.switchBy(1),
enter: () => v.ctx.run(v.controller.commitSwitcher()),
escape: v.controller.closeSwitcher,
};
// The overlay draws the card around the column, at the chat search's
// width and height in the window; only the rows are ours.
return (
<overlay
id="chat-switcher"
open={!!open}
width={620}
top={0.12}
hold="ctrl"
onRelease={() => {
if (v.controller.state.switcher?.input === "overlay") v.ctx.run(v.controller.commitSwitcher());
}}
onDismiss={() => {
if (v.controller.state.switcher?.input === "overlay") v.controller.closeSwitcher();
}}
keys={keys}
>
<div {...column} gap={2} padding={2}>
{rows.map((id) => switcherRow(v, id, id === open?.selected, now))}
</div>
</overlay>
);
}
// How far above the prompt box the blur starts, and the backdrop under the
// composer: no blur at its top, 8px at the pane's edge; the fade reaches the
// pane's colour where the controls under the prompt box start.
const FOOTER_LEAD = 16;
const FOOTER_BACKDROP = { blur: [0, 8], fade: [0, 1.35] } as const;
// The transcript's top edge, under the tab: the rows scrolling up fade into
// the pane instead of being cut off on a hard line.
const TOP_FADE = (
<div position="absolute" left={0} right={0} top={0} height={TOP_FADE_HEIGHT} background={{ linearGradient: { angle: 180, stops: [["background", 0], ["background/0", 1]] } }} />
);
// The pane shows its chat, not a skeleton waiting for its rows.
export function paneReady(c: ChatView["controller"], chatId: string): boolean {
const chat = c.state.chats[chatId];
if (!chat) return false;
const t = c.transcript(chatId);
const submitting = c.state.composers[chatId]?.submitting;
return t.snapshot.kind !== "unread" || t.items.length > 0 || !!submitting;
}
// One chat: the landing page while it is fresh, else its transcript with
// the composer floating over its bottom.
export function renderPane(v: ChatView, chatId: string): Element | null {
const chat = v.controller.state.chats[chatId];
if (!chat) return null;
const t = v.controller.transcript(chat.id);
const submitting = v.controller.state.composers[chat.id]?.submitting;
const landing = !t.items.length && !t.running && !submitting && t.snapshot.kind === "ready";
// The page on screen sending its prompt is a departure: the page's
// background fades, its headline and chip ride down with the prompt box
// as they fade, and the sent text rises into its message.
const pane = registeredPane(v.controller.state.panes[chat.id]);
if (pane) {
if (pane.landing && !landing && submitting && v.controller.state.onScreen === chat.id) pane.departing = true;
pane.landing = landing;
}
const departing = !landing && !!pane?.departing;
// A narrow or short pane gives the chat small insets above and below; at its sides
// the reading column keeps the same small room in every pane, about what the prompt
// box has under it.
const { compact } = roomOf(chat.id);
const inset = compact ? 0 : "small";
// The landing page draws its own composer.
const composer = () => (
<div {...column} gap="small" paddingTop={compact ? "small" : "medium"}>
{reading([dock(chat, composerOf(v, chat), departing ? departingPage(v, chat) : [])])}
</div>
);
// The transcript scrolls under the composer. Over its bottom, from a
// little above the prompt box down to the pane's edge, the blur grows
// row by row and the chat fades into the pane, gone before the controls
// under the prompt box.
const footer = () => (
<div {...column} paddingTop={FOOTER_LEAD} backdrop={FOOTER_BACKDROP}>
{composer()}
</div>
);
const body = landing ? (
renderLanding(v, chat, composerOf(v, chat))
) : (
<div {...column} {...fill} {...full} paddingY={inset}>
{t.snapshot.kind === "failed" ? reading([
<div {...row} gap="small" paddingY="small" id={`transcript-error-${chat.id}`}>
<text size="xs" tone="warning">{t.snapshot.error}</text>
<button id={`transcript-retry-${chat.id}`} label="Retry" variant="ghost" small disabled={!!t.loading}
onClick={() => {
if (!t.loading) v.ctx.run(v.controller.loadTranscript(chat.id, { history: false }));
}} />
</div>,
]) : null}
{t.snapshot.kind === "unread" && !t.items.length && !submitting
? [renderSkeleton(v), composer()]
: <div {...column} {...fill} position="relative">{renderTranscript(v, chat, footer())}{TOP_FADE}{renderFindBar(v, chat.id)}</div>}
</div>
);
// The landing page's stage runs to the pane's edges and draws its own
// background; the transcript keeps the pane's inset, over grain or the
// theme's backdrop for chats.
const grain = !landing && v.controller.api.settings.get("transcriptGrain") === true;
return (
<div
{...column}
{...fill}
{...full}
id={`pane-${chat.id}`}
paddingY={landing ? undefined : inset}
position="relative"
grain={grain}
themeBackdrop={landing ? undefined : "chat"}
>
{landing ? null : measureRoom(v, chat.id)}
{departing && pane ? fadingStage(v, pane) : null}
{body}
</div>
);
}
// The page's headline and chip over the prompt box on its way down, where
// they sat above it on the page, taking no room; and, until the sent text
// has its message, where that text rises from.
function departingPage(v: ChatView, chat: Chat): (Element | null)[] {
const fade = { to: { opacity: 0 }, duration: 160 };
const t = v.controller.transcript(chat.id);
const sent = t.items.some((item) => item.role === "user") || !!v.controller.composer(chat.id).outgoing?.preview;
return [
<div height={0} position="relative">
<div
{...column}
position="absolute"
left={0}
right={0}
bottom={0}
gap={18.75}
paddingBottom="small"
alignItems="center"
>
{landingHeadline(v, chat, fade)}
{landingChips(v, chat, fade)}
</div>
</div>,
sent ? null : risingFrom(chat),
];
}
// The page's background, still running under the transcript as it fades;
// the departure ends with it. It fades as long as the prompt takes to rise
// into its place (the glide spring is within a pixel by 300 ms), so the two
// end together.
function fadingStage(v: ChatView, pane: Pane): Element {
const { ambient, grain } = stageLook(v);
const onDone = () => {
pane.departing = false;
pane.view.update();
};
return (
<div
id="landing-stage"
position="absolute"
left={0}
right={0}
top={0}
bottom={0}
background={grain ? undefined : "stage"}
grain={grain}
ambient={ambient}
themeBackdrop="newChat"
motion={{ to: { opacity: 0 }, duration: 300, onDone }}
>
{stageTop()}
</div>
);
}
// The chat's composer view, which renders on its own as the user types; a
// pane drawn without one (as the tests draw it) draws the composer itself.
function composerOf(v: ChatView, chat: Chat): Element | null {
const view = registeredPane(v.controller.state.panes[chat.id])?.composer;
return view ? <view view={view.id} fit /> : renderComposer(v, chat);
}
// The composer view of one chat.
export function renderComposerView(v: ChatView, chatId: string): Element | null {
const chat = v.controller.state.chats[chatId];
return chat ? renderComposer(v, chat) : null;
}
// A chat whose rows are on their way: the shapes of a conversation where
// they will be, in the same frame as the click, so the pane is never empty
// and nothing jumps when the rows come.
export function renderSkeleton(v: ChatView): Element {
const line = (width: string) => <div width={width} height={9} borderRadius="sm" background="muted" />;
const activity = (width: string) => <div width={width} height={22} borderRadius="sm" background="muted" />;
return (
<div {...column} {...fill} {...full} id="chat-skeleton" paddingY="large">
{reading([
<div {...row} justifyContent="end">
<div width="46%" height={40} borderRadius="xl" background="muted" />
</div>,
<div {...column} gap={9} paddingY="medium">
{["94%", "88%", "63%"].map(line)}
</div>,
<div {...column} gap="small">
{["34%", "41%"].map(activity)}
</div>,
<div {...column} gap={9} paddingY="medium">
{["91%", "76%"].map(line)}
</div>,
])}
</div>
);
}
// The center with no chat selected: what to do next, with New chat's key.
function noChat(v: ChatView): Element {
const key = v.controller.state.shortcuts.keys[NEW_CHAT];
return (
<div {...column} {...fill} {...full} id="center-empty" alignItems="center" justifyContent="center" gap="medium">
<text style="heading" text="No chat open" />
<div {...row} gap="small">
<button id="center-new-chat" icon="plus" label="New chat" onClick={() => newChat(v)} />
{key ? <text keys text={key} /> : null}
</div>
</div>
);
}
// The skeleton where the pane will be, with the pane's own insets.
export const centerSkeleton = (v: ChatView) => (
<div {...column} {...fill} {...full} paddingY="small">
<div {...column} {...fill} {...full} paddingY="small">
{renderSkeleton(v)}
</div>
</div>
);
// Three frames at 60 Hz: a chat ready sooner replaces the last one directly.
export const SKELETON_AFTER = 50;
function syncMainPrompt(v: ChatView, chatId: string | null): void {
// Kept panes render only when their chat changes, so the panes that
// gain and lose the main prompt box render now.
const mainPrompt = chatId;
const was = v.controller.state.mainPrompt;
if (was !== mainPrompt) {
v.controller.state.mainPrompt = mainPrompt;
for (const chatId of [was, mainPrompt]) {
const pane = chatId ? registeredPane(v.controller.state.panes[chatId]) : undefined;
(pane?.composer ?? pane?.view)?.update();
}
}
}
// The dynamic fallback for a cold chat and an older host without native
// app-state selection.
export function renderCenter(v: ChatView): Element | null {
const app = v.ctx.app;
if (app.route !== "chat") {
syncMainPrompt(v, null);
return null;
}
if (!app.chatId) {
return noChat(v);
}
// The center draws for a new selection before the selection is followed:
// a chat passed along with it holds the chat on screen until it is ready,
// like any switch, rather than drawing a skeleton between the two.
const chat = adoptSelected(v.controller.state.chats, app);
if (!chat) return centerSkeleton(v);
return renderChatCenter(v, chat.id, true, false);
}
/** A retained complete center for one navigation context. */
export function renderCenterPresentation(v: ChatView, chatId: string, layout?: PresentationLayout): Element {
return renderChatCenter(v, chatId, v.ctx.app.route === "chat" && v.ctx.app.chatId === chatId, true, layout);
}
function renderChatCenter(v: ChatView, chatId: string, active: boolean, retained: boolean, layout?: PresentationLayout): Element {
const chat = v.controller.state.chats[chatId];
if (!chat) return centerSkeleton(v);
if (active) syncMainPrompt(v, chatId);
// A task of more than one chat draws its panes; a chat row draws as it
// always has, one pane in the shell's own surface.
const home = v.controller.state.layouts.homes[chat.id];
const task = layout ? layout.task : home ? v.controller.state.layouts.tasks[home] : undefined;
const { state } = v.controller;
const pane = registeredPane(state.panes[chat.id]);
if (task && drawsPanes(v, task)) {
// A new task's panes have shown nothing before, so the chat on screen
// stays, whole, until the selected one is ready. A retained
// presentation is drawn ahead and shown only once its chat is ready.
const held = !active || retained || pane?.ready ? undefined : heldChat(v, chat.id)?.centerPresentation;
if (held) return <view view={held.id} />;
if (active && (pane?.ready || !retained)) {
state.switching = null;
state.onScreen = chat.id;
}
return renderTaskCenter(v, task, chat.id, retained ? chat.id : undefined, layout);
}
if (!active) {
if (!pane?.ready) return centerSkeleton(v);
return taskCenter(v, task, pane);
}
const shown = pane?.ready ? pane : heldChat(v, chat.id);
if (!shown) return centerSkeleton(v);
if (shown.chatId === chat.id) state.switching = null;
state.onScreen = shown.chatId;
return taskCenter(v, task, shown);
}
// The center keeps the chat it showed until the selected one is ready, so a
// switch is one step with nothing between. Only a chat that takes longer than
// SKELETON_AFTER shows its skeleton; with no chat before, it shows at once.
function heldChat(v: ChatView, chatId: string): Pane | undefined {
const { state } = v.controller;
const before = state.onScreen && state.onScreen !== chatId ? registeredPane(state.panes[state.onScreen]) : undefined;
if (!before?.ready) return undefined;
if (state.switching?.chatId !== chatId) {
state.switching = { chatId, at: Date.now() };
setTimeout(() => v.controller.center?.update(), SKELETON_AFTER);
}
return Date.now() - state.switching.at < SKELETON_AFTER ? before : undefined;
}
function taskCenter(v: ChatView, task: Task | undefined, shown: Pane): Element {
// A chat dropped on the chat on screen joins its row: by an edge in a
// pane of its own, in the middle as its next tab (document 4).
const own = task ? taskPane(task) : null;
if (!task || !own) return <view view={shown.view.id} />;
return (
<div {...column} {...fill} {...full} position="relative" drop={paneDrop(v, task, own)}>
<view view={shown.view.id} />
{dropPreview(own.id)}
</div>
);
}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.