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/pending.tsx15.1 KB
// The list of held inputs over the prompt box and a semantic usage-limit
// notice. A steer the agent was handed is taken back only through the
// agent's own withdrawal (`capabilities.withdraw`); otherwise admission and
// unknown delivery stay visible without fictitious edit/withdraw.
import * as Effect from "effect/Effect";
import type { Chat, ContentBlock, DropEvent, Element, PendingInput } from "divergence";
import type { ChatView } from "../controller.ts";
import { autoSend, editable, inputLabel, pending, policyOf } from "../scheduler-policy.ts";
import { editableComposition, steeringReason } from "../composition.ts";
import { column, fill, HOVER, revealOnHover, row, RAISED } from "./styles.tsx";
import { resetsIn } from "./usage.tsx";
import { takeBack } from "../composer.ts";
import { knownTokens } from "../message-text.ts";
import { deriveSegments, filePaths, isFolder, pathName } from "../segments.ts";
// A row's side inset, its leading mark lane and the gap after it: the
// heading and an input's attachment line start where they put the text.
const ROW_X = 6;
const LANE = 16;
const GAP = 7;
// What the line cannot show: files and skills stand in its text as pills.
const descriptions = (blocks: readonly ContentBlock[]) => blocks.flatMap((block) => {
switch (block.type) {
case "image": return [`Image · ${block.mimeType}`];
case "audio": return [`Audio · ${block.mimeType}`];
case "resource": return [block.path];
default: return [];
}
});
// An input's text on one line, its tags (files, skills, agents, a command)
// as pills where they stand, as the prompt box drew them. A file counts
// only when the input carries it.
function inline(v: ChatView, chat: Chat, text: string, blocks: readonly ContentBlock[]): Element {
const paths = filePaths(blocks), files = new Set(paths);
const segments = deriveSegments(text, knownTokens(v.controller, chat.id), paths)
.filter((segment) => segment.kind !== "file" || files.has(text.slice(segment.start + 1, segment.end)));
if (!segments.length) return <text style="body" size="xs" truncate text={text} />;
const parts: Element[] = [];
let at = 0;
for (const segment of segments) {
if (segment.start > at) parts.push(<text style="body" size="xs" truncate text={text.slice(at, segment.start)} />);
const word = text.slice(segment.start, segment.end);
parts.push(
<div {...row} gap={3} paddingX={4} borderRadius="sm" background="hover" flexShrink={0}>
{segment.kind === "file" ? <icon name={isFolder(word) ? "folder" : "file"} small tone="muted" /> : null}
<text style="body" size="xs" text={segment.kind === "file" ? pathName(word.slice(1)) : word} />
</div>,
);
at = segment.end;
}
if (at < text.length) parts.push(<text style="body" size="xs" truncate text={text.slice(at)} />);
return <div {...row} overflow="hidden">{parts}</div>;
}
/// The editor a held line opens into. Save goes against the chat as it is
/// now, so other changes in the chat since Edit are no reason to refuse it;
/// a change to this prompt is. It needs text or an attachment, since an
/// edit with neither would save nothing.
function heldEditor(v: ChatView, chat: Chat, input: PendingInput, edit: { revision: number; text: string }, act: (effect: Effect.Effect<unknown, unknown>) => void): Element {
const scheduler = v.controller.scheduler, composer = v.controller.composer(chat.id);
const savable = (text: string) => !!text.trim() || editableComposition(input).carriedBlocks.length > 0;
const save = Effect.fn("Chat.savePendingEdit")(function* () {
if (!savable(edit.text)) return;
const now = scheduler.snapshots[chat.id], current = now?.inputs.find((held) => held.id === input.id);
if (!now || current?.revision !== edit.revision)
return yield* Effect.fail({ message: "this prompt changed while you edited it; cancel and edit it again" });
yield* scheduler.edit(current, now.revision, edit.text);
composer.pendingEdit = undefined;
v.ctx.update();
});
const stopEditing = () => { composer.pendingEdit = undefined; v.ctx.update(); };
return (
<div {...column} gap="small" paddingX={ROW_X} paddingY="small">
<input id={`edit-pending-${input.id}`} multiline submitOnEnter keys={["escape"]} value={edit.text} onChange={(event: { value: string }) => { const was = savable(edit.text); edit.text = event.value; if (savable(edit.text) !== was) v.ctx.update(); }} onSubmit={() => act(save())} onKey={(event: { key: string }) => { if (event.key === "escape") stopEditing(); }} />
<div {...row} gap="small" justifyContent="end">
<button id={`cancel-pending-${input.id}`} label="Cancel" small variant="ghost" onClick={stopEditing} />
<button id={`save-pending-${input.id}`} label="Save" small variant="primary" disabled={!savable(edit.text)} onClick={() => act(save())} />
</div>
</div>
);
}
export function renderPending(v: ChatView, chat: Chat): Element[] {
const scheduler = v.controller.scheduler, snapshot = scheduler.snapshots[chat.id], composer = v.controller.composer(chat.id);
// A failure of what the user just did shows each time, even when it says what the last one said.
const act = <A, E>(effect: Effect.Effect<A, E>) => {
delete scheduler.errors[chat.id];
v.ctx.run(effect.pipe(Effect.catch((error) => scheduler.report(chat.id, error))));
};
const reason = steeringReason(v.controller, chat);
const withdraws = !!v.controller.state.agents[chat.agentId]?.capabilities?.withdraw;
// Input the transcript shows needs no card: a send joins it when it is
// handed over, a steer when the agent picks it up.
const shown = new Set(v.controller.state.transcripts[chat.id]?.items.map((item) => item.id));
const handed = (input: PendingInput) =>
(input.id === composer.outgoing?.id && composer.outgoing.preview && !input.error) ||
(shown.has(input.id) && !editable(input) && !input.error);
const listed = snapshot ? pending(snapshot).filter((input) => !handed(input)) : [];
// Queued inputs reorder by the grip at their start, once there are two.
const dragType = `queued-input:${chat.id}`;
const reorders = listed.filter((input) => input.intent !== "steer" && editable(input)).length > 1;
const place = (dragged: string, target: PendingInput, zone: DropEvent["zone"]) => {
if (!snapshot || dragged === target.id) return;
const ids = snapshot.inputs.map((input) => input.id), at = ids.indexOf(target.id);
// An older kernel reports `into`: the dragged input takes the target's place.
const after = zone === "after" || (zone === "into" && ids.indexOf(dragged) < at);
const before = after ? (ids[at + 1] ?? null) : target.id;
if (before !== dragged) act(scheduler.change({ chatId: chat.id, inputId: dragged, revision: snapshot.revision, before }));
};
// One line per input: its mark (or grip), what it says cut to the width,
// and what makes it more than a prompt waiting its turn. Its actions
// follow on the right, the one that sends it now always visible.
const line = (lead: Element, text: string | Element, status: string, actions: (Element | null)[] = [], target?: PendingInput) => (
<div {...row} group="hover" gap={GAP} minHeight={28} paddingX={ROW_X} borderRadius="sm" hover={HOVER} drop={target && reorders ? { types: [dragType], reorder: "vertical", onDrop: (e: DropEvent) => { if ("data" in e) place(String(e.data), target, e.zone); } } : undefined}>
{lead}
<div {...fill}>{typeof text === "string" ? <text style="body" size="xs" truncate text={text} /> : text}</div>
{status ? <text style="body" size="xs" tone="muted" text={status} /> : null}
{actions}
</div>
);
const mark = (icon: string) => <div {...row} width={LANE} justifyContent="center"><icon name={icon} small tone="muted" /></div>;
const grip = (input: PendingInput, text: string) => <div {...row} width={LANE} justifyContent="center" cursor="grab" drag={{ type: dragType, data: input.id, label: text }}><icon name="grip-vertical" small tone="faint" /></div>;
const ghost = (id: string, icon: string, tooltip: string, onClick: () => void, disabled = false) =>
<button id={id} icon={icon} small variant="ghost" tooltip={tooltip} disabled={disabled} onClick={onClick} />;
// A steer the agent was handed waits here until it picks it up; while
// the run goes on that wait is the normal case and needs no words.
const status = (input: PendingInput) =>
input.intent === "steer" && snapshot?.running && !input.error && ["dispatching", "native_admitted", "delivery_unknown"].includes(input.state)
? ""
: inputLabel(input);
const extras = (texts: string[]) =>
texts.length ? <div {...row} gap="small" paddingLeft={ROW_X + LANE + GAP} flexWrap="wrap">{texts.map((text) => <text style="body" size="xs" tone="muted" text={text} />)}</div> : null;
const rows = snapshot ? listed.map((input): [boolean, Element] => [input.intent === "steer", (() => {
const steer = input.intent === "steer", text = v.controller.plainText(input.blocks);
const canEdit = editable(input), edit = canEdit && composer.pendingEdit?.id === input.id ? composer.pendingEdit : undefined;
if (edit) return heldEditor(v, chat, input, edit, act);
// Only a steer the agent holds and can drop is taken back from it.
const withdrawable = steer && withdraws && ["native_admitted", "delivery_unknown"].includes(input.state);
const actions = canEdit ? [
(
<div {...row} {...revealOnHover}>
{ghost(`edit-input-${input.id}`, "pencil", "Edit", () => { composer.pendingEdit = { id: input.id, revision: input.revision, text: editableComposition(input).text }; v.ctx.update(); })}
{steer ? ghost(`demote-${input.id}`, "list-plus", "Move to the queue", () => act(scheduler.change({ chatId: chat.id, inputId: input.id, revision: snapshot.revision, intent: "queue" }))) : null}
{ghost(`remove-input-${input.id}`, "x", "Cancel", () => act(scheduler.change({ chatId: chat.id, inputId: input.id, revision: snapshot.revision, remove: true })))}
</div>
),
ghost(`promote-${input.id}`, "corner-down-left", reason ?? (v.controller.isRunning(chat) ? "Steer now; your draft stays" : "Send now; your draft stays"),
() => act(scheduler.promote(chat.id, input.id, snapshot.revision)), !!reason),
] : withdrawable ? [
(
<div {...row} {...revealOnHover}>
{ghost(`withdraw-${input.id}`, "x", "Cancel", () => act(scheduler.withdraw(chat.id, input.id)))}
</div>
),
] : [];
const lead = !steer && canEdit && reorders ? grip(input, text) : mark(steer ? "corner-down-left" : "list-plus");
return (
<div {...column}>
{line(lead, inline(v, chat, text, input.blocks), status(input), actions, steer ? undefined : input)}
{extras(descriptions(input.blocks))}
</div>
);
})()]) : [];
const outgoing = composer.outgoing;
// Local intent is visible before persistence, but only host evidence grants
// edit/remove authority. Clearing the submission identity acknowledges even
// a steer merged into another input; an exact-id event can take over sooner.
if (outgoing?.intent && composer.submission && !shown.has(outgoing.id) && !snapshot?.inputs.some((input) => input.id === outgoing.id) && !composer.queue.some((input) => input.id === outgoing.id)) {
rows.push([outgoing.intent === "steer", (
<div {...column}>
{line(mark(outgoing.intent === "steer" ? "corner-down-left" : "list-plus"), outgoing.text, "Saving")}
{extras([...descriptions(outgoing.blocks ?? []), ...outgoing.attachments.map((image) => `Image · ${image.mimeType}`)])}
</div>
)]);
}
// Enter took these from the box before the chat could send them; each
// goes by itself once it can, and cancelling one puts it back in the box.
const waiting = (composer.waiting ?? []).map((prompt, index): ["Sending" | "Steer" | "Queued", Element] => [
prompt.queue ? "Queued" : v.controller.isRunning(chat) ? "Steer" : "Sending",
(
<div {...column}>
{line(mark(prompt.queue ? "list-plus" : "corner-down-left"), prompt.text.trim(), "When ready", [
<div {...row} {...revealOnHover}>{ghost(`unwait-${chat.id}-${index}`, "x", "Cancel", () => v.ctx.run(takeBack(v.controller, chat.id, prompt)))}</div>,
])}
{extras([...descriptions(prompt.carriedBlocks), ...prompt.attachments.map((image) => `Image · ${image.mimeType}`)])}
</div>
),
]);
// Steers first, the agent reads them before anything queued.
const all = [...rows.map(([steer, element]) => [steer ? "Steer" : "Queued", element] as const), ...waiting];
const groups = (["Sending", "Steer", "Queued"] as const)
.map((title) => [title, all.filter(([group]) => group === title).map(([, element]) => element)] as const)
.filter(([, elements]) => elements.length);
if (!groups.length) return [];
// The list sits on the prompt box's top edge, inset from its sides.
return [(
<div {...column} marginX={9.5} padding="small" background="muted" borderTopLeftRadius="xl" borderTopRightRadius="xl" {...RAISED}>
{groups.flatMap(([title, elements]) => [
<div {...row} minHeight={24} paddingX={ROW_X}><text style="body" size="xs" weight={500} tone="muted" text={title} /></div>,
...elements,
])}
</div>
)];
}
export function renderLimit(v: ChatView, chat: Chat): Element | null {
const scheduler = v.controller.scheduler, snapshot = scheduler.snapshots[chat.id];
const state = snapshot ? policyOf(snapshot) : undefined, wait = state?.wait;
const active = scheduler.activeLimits[chat.id];
const grace = !!active && (snapshot?.running || v.controller.isRunning(chat)) && (!active.runId || active.runId === snapshot?.runId);
if (!wait && !grace) return null;
const enabled = autoSend(scheduler.preferences, chat.agentId);
const reset = wait?.resetAt ?? (grace ? active?.evidence.resetsAt : undefined);
const countdown = resetsIn(reset);
const resetText = countdown && reset ? `Reset ${countdown === "now" ? "reported for now" : `in ${countdown}`} · ${new Date(reset).toLocaleString()}` : "Reset time unavailable";
const status = grace ? "Usage limit reported · still working / retrying" : scheduler.checking.has(chat.id) ? "Checking availability…" :
state?.pause || wait?.status === "paused" ? "Automatic send paused" : !enabled ? "Auto-send disabled for this agent" : wait?.status === "attempting" ? wait.reason : wait?.status === "failed" ? "Recovery check failed" : "Waiting for usage-limit recovery";
return (
<div {...column} gap="small" padding="medium" background="warning/10" borderRadius="md" border={{ width: 1, color: "warning/30" }}>
<div {...row} gap="small">
<icon name="clock" small tone="warning" />
<text style="body" size="xs" tone="warning">{status}</text>
</div>
<text style="body" size="xs" tone="muted">{resetText}</text>
<text style="body" size="xs" tone="muted">{state?.pause ?? wait?.reason ?? active?.evidence.reason}</text>
<checkbox id={`auto-limit-${chat.id}`} label="Auto-send pending input after recovery · all chats for this agent" checked={enabled} onChange={(event: { value: boolean }) => v.ctx.run(scheduler.setAutoSend(chat.agentId, event.value))} />
</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.