Official
sidebar
Built-in sidebar 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/sidebar@0.2.3
Permissions in 0.2.3
Files
views.ts17.5 KB
// The left sidebar's trees: search and actions, the workspace picker, the list
// of chats, and the footer. Handlers only run controller effects or mutate
// state and update; the preview hydration fires from rendering, guarded so
// each chat reads once.
// HTM whitespace is significant; keep these templates out of Prettier's
// HTML formatter.
import { html } from "divergence";
import * as kit from "divergence";
import type { EffectViewContext } from "divergence/effect";
import type { ButtonClickEvent, Chat, Element, Modifiers, PresetColor, Workspace } from "divergence";
import type { Sidebar } from "./controller.ts";
import {
PAGE,
chatStatus,
chatTitleOf,
elapsed,
isDraft,
sections,
shownChats,
workingSubagents,
workspaceLooks,
workspaceMarkOf,
} from "./sections.ts";
import type { ChatRowStatus } from "./sections.ts";
// The shared surface looks from the runtime; an older runtime without them
// gets the same `elevation`, which its kernel already draws.
const RAISED = kit.RAISED ?? { elevation: "raised" };
const EDGED = kit.EDGED ?? { elevation: "edge" };
const FLAT = kit.FLAT ?? { elevation: "none" };
// The chats scroll as a list that opens at its first row and lays out only
// the rows on screen. A runtime without it scrolls a column of every row.
const LIST = kit.LIST;
// Layout for the templates below: a flex column, a row that centres its
// children across, a child that takes the free space (and may shrink below
// its content), the whole of the parent, the hover wash, and the hover
// group and pointer of something clickable.
const column = { display: "flex", flexDirection: "column" };
const row = { display: "flex", flexDirection: "row", alignItems: "center" };
// The open chat is raised like the panes (the kernel's `elevation`). Every
// row keeps the edge's width, so opening a chat never moves its text.
const fill = { flexGrow: 1, flexShrink: 1, flexBasis: "0%", minWidth: 0, minHeight: 0 };
const full = { width: "100%", height: "100%" };
const HOVER = { background: "hover" };
const clickable = { group: "hover", cursor: "pointer" };
// Shown only while the pointer is on a chat's status mark (the mark is the
// hover group `glyph`), or only while it is not: the check shows only
// where a click checks the chat off.
const revealOnGlyph = { visibility: "hidden", groupHover: { group: "glyph", visibility: "visible" } };
const HIDE_ON_GLYPH = { group: "glyph", visibility: "hidden" };
// A layer over the whole of its parent that centres what it holds.
const layer = {
position: "absolute",
top: 0,
right: 0,
bottom: 0,
left: 0,
display: "flex",
alignItems: "center",
justifyContent: "center",
};
const TONE: Record<ChatRowStatus, "warning" | "info" | "danger" | "success" | "faint"> = {
blocked: "warning",
running: "info",
failed: "danger",
review: "success",
idle: "faint",
};
const LIVE_TONE: Record<ChatRowStatus, "warning" | "info" | "danger" | "muted"> = {
blocked: "warning",
running: "info",
failed: "danger",
review: "muted",
idle: "muted",
};
// Rows other plugins add to the footer, above Settings (the market's
// Marketplace row): each publishes the id of a view of its own in the app's
// shared state under a key starting with `sidebarItem.`, and clears it when
// it goes. In the order of their keys, so they keep their places.
const ITEM_PREFIX = "sidebarItem.";
export function sidebarItems(shared: Readonly<Record<string, unknown>> | undefined): number[] {
return Object.entries(shared ?? {})
.filter((entry): entry is [string, number] => entry[0].startsWith(ITEM_PREFIX) && typeof entry[1] === "number")
.sort(([a], [b]) => a.localeCompare(b))
.map(([, view]) => view);
}
export function renderSidebar(ctx: EffectViewContext<{ queueDraft?: boolean }>, sidebar: Sidebar["Service"]): Element | null {
const { state } = sidebar;
const app = ctx.app;
if (!state.loaded) {
return html`<div ...${column} gap="small" padding="medium"><text style="muted">Loading…</text></div>`;
}
// Latest model activity; status is the fallback for an empty transcript.
function liveLine(chat: Chat): string {
const working = workingSubagents(state, chat.id);
if (working && chat.status !== "running") return `${working} ${working === 1 ? "agent" : "agents"} working`;
// The transcript is read once per chat, when a row first shows it. The
// check runs here, synchronously: a forked check could run after the
// chat was removed and resurrect its preview with a stray read.
const preview = state.previews[chat.id];
if (!preview?.loading && !preview?.revision) ctx.run(sidebar.ensurePreview(chat.id));
const status = chatStatus(state, chat);
// A failed turn says so before anything it said.
if (status === "failed") {
const ago = elapsed(Date.now(), chat.updatedAt);
return ago ? `Failed · ${ago}` : "Failed";
}
if (preview?.line) return preview.line;
const base =
status === "blocked"
? "Waiting on you"
: status === "running"
? "Working…"
: status === "review"
? "Finished"
: chat.sessionId
? "Idle"
: "Not started";
// A review always stamped its finish when the run ended; an idle chat
// shows how long ago it was touched.
const finishedAt = state.finished[chat.id];
const since =
status === "review" && finishedAt
? new Date(finishedAt).toISOString()
: status === "idle" && chat.sessionId
? chat.updatedAt
: null;
const ago = since ? elapsed(Date.now(), since) : "";
return ago ? `${base} · ${ago}` : base;
}
// --- rows -----------------------------------------------------------------
// Each workspace's mark (its project icon, the app's mark, or its
// coloured folder), for the rows of its chats.
const workspaceMarks = new Map<string, Workspace>(state.workspaces.map((w) => [w.id, w]));
const workspaceMark = (w: Workspace | undefined) => {
if (!w) return null;
const mark = workspaceMarkOf(w);
return mark.image
? html`<image path=${mark.image} width=${14} height=${14} radius=${3} />`
: html`<icon name=${mark.icon ?? "folder"} color=${mark.color} small tone="faint" />`;
};
function renameField(editing: { id: string; title: string }) {
return html`
<input id=${`rename-${editing.id}`} value=${editing.title}
onChange=${(e: { value: string }) => {
editing.title = e.value;
}}
onSubmit=${() => ctx.run(sidebar.commitRename())} />`;
}
// The status mark: a dot or a spinner, and a check while the pointer is
// on the mark; clicking the check checks the chat off (or opens it again).
function glyph(chat: Chat, status: ChatRowStatus) {
const done = !!chat.completedAt;
if (!done && isDraft(state, chat)) return html`
<button id=${`play-${chat.id}`} icon=${ctx.state.queueDraft ? "list-plus" : "play"} variant="ghost" small
disabled=${!state.chat || state.submitting.has(chat.id)} loading=${state.submitting.has(chat.id)}
tooltip=${ctx.state.queueDraft ? "Queue draft" : "Send draft (⌘click to queue)"}
onClick=${(event?: ButtonClickEvent) => ctx.run(sidebar.submitDraft(chat.id, event?.modifiers.meta ?? !!ctx.state.queueDraft))} />`;
if (!done && state.queued.has(chat.id)) return html`
<button id=${`unqueue-${chat.id}`} icon="list-minus" variant="ghost" small disabled=${!state.chat}
tooltip="Remove from queue" onClick=${() => ctx.run(sidebar.unqueue(chat.id))} />`;
// An empty circle reads as a box to tick; its colour says what the
// chat is doing. Running shows the spinner, complete the ticked circle.
const mark = done
? html`<icon name="circle-check" small tone="muted" />`
: status === "running"
? html`<spinner activity />`
: html`<icon name="circle" small tone=${TONE[status]} />`;
// The mark and the check lie over each other; the pointer on the mark
// swaps them.
return html`
<div ...${column} ...${clickable} group="glyph" id=${`glyph-${chat.id}`} width=${17} height=${17} flexShrink=${0}
borderRadius="full" hover=${HOVER} position="relative" stopPropagation
onClick=${() => ctx.run(sidebar.toggleCompleted(chat))}>
<div ...${layer}>
<div ...${column} groupHover=${done ? undefined : HIDE_ON_GLYPH}>${mark}</div>
</div>
${
done
? null
: html`
<div ...${layer}>
<div ...${column} ...${revealOnGlyph}><icon name="circle-check" small tone="muted" /></div>
</div>`
}
</div>`;
}
// The rows glide to their new places as chats come, go and change
// section, but not while a search narrows the list as it is typed.
const moves = !state.query;
// A chat: its status mark (the check), its title and live line, its
// agent at the end, and rename and archive while the pointer is on it. A
// chat being renamed is the rename field.
function chatRow(chat: Chat) {
const selected = app.chatId === chat.id;
const status = chatStatus(state, chat);
const editing = state.editing?.id === chat.id ? state.editing : null;
const agent = state.agents.find((a) => a.id === chat.agentId);
// A checked-off chat is one struck line, a draft or a queued chat one
// plain line: no live line under any of them.
const done = !!chat.completedAt;
const compact = done || isDraft(state, chat) || state.queued.has(chat.id);
const rename = () => {
state.editing = { id: chat.id, title: chatTitleOf(state, chat) };
ctx.update();
};
return html`
<item id=${`row-${chat.id}`} label=${editing ? "" : chatTitleOf(state, chat)} selected=${selected}
...${FLAT} background=${selected ? "transparent" : undefined} layoutMotion=${moves}
sublabel=${editing || compact ? undefined : liveLine(chat)} sublabelTone=${LIVE_TONE[status]} strike=${done} stacked
actions=${
editing
? undefined
: [
{ icon: "pencil", tooltip: "Rename", onClick: rename },
{ icon: "archive", tooltip: "Archive", onClick: () => ctx.run(sidebar.archive(chat)) },
]
}
onClick=${() => ctx.run(sidebar.select(chat.id, chat.workspaceId))}
onPointerEnter=${selected ? undefined : () => ctx.run(sidebar.prefetch(chat.id))}>
<slot name="leading">${glyph(chat, status)}</slot>
${
editing
? renameField(editing)
: html`
<div ...${row} gap=${6}>
${workspaceMark(workspaceMarks.get(chat.workspaceId))}
<icon name="bot" small tone="faint" svg=${agent?.icon} />
</div>`
}
</item>`;
}
// The selected chat's fill: one box under the rows that follows the
// selected row, so it slides to the next chat chosen.
function selection() {
if (!app.chatId) return null;
return html`
<div id="chat-selection" follows=${`row-${app.chatId}`} width="100%" height="100%" background="accent"
borderRadius="sm" ...${RAISED} layoutMotion=${moves} />`;
}
// The chats by status: Drafts, Queued, Needs you, Running, Finished,
// Open, Complete. Drafts and Complete show a page, then "Show more".
// One flat run of rows, so the list draws only the ones on screen: each
// section's heading, with the room between sections above it, then its
// chats and its "Show more".
function renderRows() {
const out = sections(state).flatMap((section, index) => {
// A search shows its matches even in a folded section.
const folded = !!state.collapsed[section.id] && !state.query;
const shown = shownChats(state, section);
const more = folded ? 0 : section.chats.length - shown.length;
return [
html`
<div ...${column} id=${`heading-${section.id}`} layoutMotion=${moves} paddingTop=${index ? "0.75rem" : 0}>
<item heading id=${`section-${section.id}`} label=${section.label} tone=${section.tone}
onClick=${() => ctx.run(sidebar.toggleSection(section.id))}>
<div ...${row} gap=${4}>
<text style="body" size=${9.84} tone="muted">${String(section.chats.length)}</text>
<icon name=${folded ? "chevron-right" : "chevron-down"} small tone="faint" />
</div>
</item>
</div>`,
...shown.map(chatRow),
more > 0
? html`
<item id=${`more-${section.id}`} icon="chevron-down" label="Show more" badge=${String(more)} layoutMotion=${moves}
onClick=${() => {
state.showAll[section.id] = true;
ctx.update();
}} />`
: !folded && state.showAll[section.id] && !state.query && section.chats.length > PAGE
? html`
<item id=${`less-${section.id}`} icon="chevron-up" label="Show less" layoutMotion=${moves}
onClick=${() => {
state.showAll[section.id] = false;
ctx.update();
}} />`
: null,
].filter((row) => row !== null);
});
if (!out.length) {
out.push(
html`<div ...${column} gap="small" padding="medium"><text style="body" size="xs" tone="muted">${state.query ? "No matches" : "No chats yet"}</text></div>`,
);
}
return out;
}
// --- chips and top actions ---------------------------------------------------
// The workspaces to list, a row like the ones around it: none chosen is
// all of them (every box checked, the folders mark); a choice shows its
// workspaces' own marks and names.
function renderWorkspacePicker() {
const chosen = state.workspaces.filter((w: Workspace) => state.filter.includes(w.id));
return html`
<combobox id="workspace-filter" look="row" multiple values=${state.filter} all="All workspaces"
label=${chosen.length ? chosen.map((w: Workspace) => w.name).join(", ") : "All workspaces"}
marks=${chosen.length ? chosen.slice(0, 3).map(workspaceMarkOf) : [{ icon: "folders" }]}
search=${state.workspaces.length > 6 ? "Search workspaces…" : undefined}
options=${state.workspaces.map((w: Workspace) => ({
value: w.id,
label: w.name,
description: w.path,
...workspaceMarkOf(w),
submenu: state.colors.length ? { context: true, ...workspaceLooks(w, state.colors) } : undefined,
}))}
actions=${[{ label: "Add workspace folder…", icon: "folder-plus", onClick: () => ctx.run(sidebar.addWorkspace()) }]}
onChange=${(e: { values?: readonly string[]; value?: string; option?: string }) =>
// A choice in a workspace's right-click menu is its look; any
// other change is which workspaces show.
e.option
? ctx.run(sidebar.setColor(e.option, e.value === "icon" ? null : (e.value as PresetColor)))
: ctx.run(sidebar.setFilter([...(e.values ?? [])]))} />`;
}
// A top action such as New chat: 12.2px medium text, a key cap at the end.
// An action row's icon and label sit where a text field puts its icon
// and text, so the search field above lines up.
function actionRow({
icon,
label,
key,
trailing,
onClick,
}: {
icon: string;
label: string;
key?: string;
trailing?: Element;
onClick: () => void;
}) {
return html`
<item icon=${icon} label=${label} onClick=${onClick}>${key ? html`<text keys>${key}</text>` : (trailing ?? null)}</item>`;
}
const [onlyId] = state.filter;
const filtered = state.filter.length === 1 ? (state.workspaces.find((w) => w.id === onlyId) ?? null) : null;
const onSettings = app.route === "settings";
// The search field's edge reads level with the top of the chat pane
// beside it: a 1px outline at the pane's very top looks higher than the
// pane's filled edge, so it sits a point lower.
return html`
<div id="sidebar" ...${column} ...${fill} ...${full} padding="small" paddingX="small" paddingTop=${0}
onModifiersChanged=${({ modifiers }: { modifiers: Modifiers }) => {
if (!!ctx.state.queueDraft === modifiers.meta) return;
ctx.state.queueDraft = modifiers.meta;
ctx.update();
}}>
<div ...${column} gap="small" padding="small" paddingY="small" paddingTop=${2}>
<input id="chat-search" value=${state.query} placeholder="Search chats..." icon="search"
onChange=${(e: { value: string }) => {
state.query = e.value;
ctx.update();
}} />
${actionRow({ icon: "plus-large", label: "New chat", key: "⌘N", onClick: () => ctx.run(sidebar.runCommand("sidebar.newChat")) })}
${renderWorkspacePicker()}
</div>
${
LIST
? html`
<div ...${column} ...${fill} paddingX="small">
<scroller id="chat-list" ...${LIST}>${selection()}${renderRows()}</scroller>
</div>`
: html`
<div ...${column} ...${fill} paddingX="small" paddingY="medium" overflowY="scroll">
${selection()}
${renderRows()}
</div>`
}
<div ...${column} padding="small" paddingY="small">
${
filtered
? html`
<item id="remove-workspace" icon="folder-x" label=${`Remove ${filtered.name}`} onClick=${() => ctx.run(sidebar.removeWorkspace(filtered))} />`
: null
}
${sidebarItems(app.shared).map((view) => html`<view view=${view} fit />`)}
<item id="sidebar-settings" icon=${onSettings ? "arrow-left" : "settings"} label=${onSettings ? "Back" : "Settings"}
onClick=${() => ctx.run(sidebar.route(onSettings ? "chat" : "settings"))} />
</div>
</div>`;
}
No comments yet.