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.6
Permissions in 0.2.6
Files
usage.test.ts10 KB
// The limits and context meters under the prompt box, and the combined model picker.
import assert from "node:assert/strict";
import test from "node:test";
import type { AgentInfo } from "divergence";
import { headlineWindows, limitRow, windowGone, resetsIn, ringCommands, usageMeter, contextMeter } from "./views/usage.tsx";
import { harness, tree as inspect, nodes, chat as makeChat } from "./ui-for-tests.test.ts";
const texts = (element: unknown) => nodes(element).flatMap((node) => (node.text ? [node.text] : []));
const agent = (fields: Partial<AgentInfo>) =>
({ id: "one", name: "One", capabilities: { usageLimits: true }, status: { state: "ready" }, ...fields }) as AgentInfo;
test("an agent that reports limits always gets a meter, empty until the first report", () => {
const h = harness();
const chat = makeChat({ id: "c", agentId: "one" });
h.controller.state.agents.one = agent({});
assert.deepEqual(texts(usageMeter(h.v, chat)).slice(0, 1), ["—"]);
h.controller.state.agents.one = agent({
usageLimits: {
windows: [
{ id: "five_hour", label: "5 hours", usedPercent: 23.4, windowMinutes: 300 },
{ id: "seven_day", label: "7 days", usedPercent: 91, windowMinutes: 10080 },
],
},
});
// Beside the prompt: rings and their lengths, never the share. The
// numbers are a press away in the card.
assert.deepEqual(texts(usageMeter(h.v, chat)).slice(0, 2), ["5h", "7d"]);
// Beside the prompt, one window per length: a model's own weekly window
// and windows of no fixed length are in the card only.
assert.deepEqual(
headlineWindows([
{ id: "five_hour", label: "5 hours", usedPercent: 9, windowMinutes: 300 },
{ id: "overage", label: "Extra Usage", usedPercent: 0 },
{ id: "seven_day", label: "7 days", usedPercent: 37, windowMinutes: 10080 },
{ id: "seven_day_fable", label: "7 days (Fable)", usedPercent: 0, windowMinutes: 10080 },
]).map((window) => window.id),
["five_hour", "seven_day"],
);
// Below 10% the card keeps one decimal.
h.controller.state.agents.one = agent({
usageLimits: { windows: [{ id: "five_hour", label: "5 hours", usedPercent: 99.6, windowMinutes: 300 }] },
});
assert.equal(texts(usageMeter(h.v, chat))[0], "5h");
assert.ok(texts(usageMeter(h.v, chat)).includes("0.4% remaining"));
// A spend allowance alone still gives the card a trigger.
assert.deepEqual(
headlineWindows([{ id: "overage", label: "Extra Usage", usedPercent: 12 }]).map((window) => window.id),
["overage"],
);
h.controller.state.agents.one = agent({ capabilities: {} });
assert.equal(usageMeter(h.v, chat), null);
});
test("the context meter is a ring, the share and its name", () => {
const h = harness();
const chat = makeChat({ id: "c" });
assert.equal(contextMeter(h.v, chat), null);
h.controller.transcript("c").usage = { usedTokens: 25_000, contextWindow: 100_000 };
assert.deepEqual(texts(contextMeter(h.v, chat)).slice(0, 2), ["25%", "Ctx"]);
assert.ok(nodes(contextMeter(h.v, chat)).some((node) => node.type === "canvas"));
});
test("the context card shows what fills the window and compacts from there", async () => {
const h = harness();
const chat = makeChat({ id: "c", agentId: "one" });
h.controller.state.agents.one = agent({ capabilities: { compact: true } });
h.controller.transcript("c").usage = {
usedTokens: 25_000,
contextWindow: 100_000,
autoCompactAt: 90_000,
categories: [
{ name: "System prompt", tokens: 3_000 },
{ name: "Messages", tokens: 22_000, parts: [{ name: "Tool results", tokens: 20_000 }] },
],
};
const shown = texts(contextMeter(h.v, chat));
for (const text of ["System prompt", "3,000", "Messages", "Tool results", "20,000", "Compacts automatically at", "90,000"])
assert.ok(shown.includes(text), text);
// The compact button lives in the card, not the title strip, and closes it.
h.controller.state.wsPicker["context-c"] = true;
nodes(contextMeter(h.v, chat)).find((node) => node.id === "compact-c")!.onClick();
await h.settle();
assert.deepEqual(h.calls.filter((call) => call.method === "host/chats.compact").map((call) => call.params), [{ chatId: "c" }]);
assert.equal(h.controller.state.wsPicker["context-c"], false);
// An agent that cannot compact gets no button.
h.controller.state.agents.one = agent({ capabilities: {} });
assert.equal(nodes(contextMeter(h.v, chat)).some((node) => node.id === "compact-c"), false);
});
test("the windows under the prompt are set apart from one another", () => {
const h = harness();
const chat = makeChat({ id: "c", agentId: "one" });
const windows = [
{ id: "five_hour", label: "5 hours", usedPercent: 23.4, windowMinutes: 300 },
{ id: "seven_day", label: "7 days", usedPercent: 91, windowMinutes: 10080 },
];
h.controller.state.agents.one = agent({ usageLimits: { windows } });
const strip = () => nodes(usageMeter(h.v, chat)).find((node) => node.type === "div" && node.children?.every((child) => child.type === "div" && child.children.some((part) => part.type === "canvas")))!;
const at = () => strip().children;
assert.equal(at().length, 2);
// Ring to length stays tight; window to window opens up, so a 5h ring
// is never read as the 7d one's.
assert.ok(Number(strip().gap) > Number(at()[0].gap));
h.controller.state.agents.one = agent({ usageLimits: { windows: windows.slice(0, 1) } });
assert.equal(at().length, 1);
assert.equal(Number(strip().gap), Number(at()[0].gap));
});
test("reset times read as the time left", () => {
const now = Date.parse("2026-01-01T00:00:00Z");
assert.equal(resetsIn("2026-01-01T02:05:00Z", now), "2h 5m");
assert.equal(resetsIn("2026-01-03T03:00:00Z", now), "2d 3h");
assert.equal(resetsIn("2025-12-31T00:00:00Z", now), "now");
assert.equal(resetsIn(undefined, now), null);
});
test("the share of a window gone by comes from its length and its reset", () => {
const now = Date.parse("2026-01-01T00:00:00Z");
const day = 86_400_000;
const week = (resetsInDays: number) => ({
id: "seven_day",
label: "7 days",
usedPercent: 30,
windowMinutes: 10080,
resetsAt: new Date(now + resetsInDays * day).toISOString(),
});
assert.ok(Math.abs(windowGone(week(3.5), now)! - 0.5) < 1e-9);
assert.equal(windowGone(week(-1), now), 1, "a reset gone by is the whole window");
assert.equal(windowGone(week(8), now), 0, "a reset further off than the window is long");
assert.equal(windowGone({ ...week(3), windowMinutes: undefined }, now), null, "no length");
assert.equal(windowGone({ ...week(3), resetsAt: undefined }, now), null, "no reset");
});
test("a limit's card row shows only what is left, of the limit and of its window, with a line where the time has got to", () => {
const at = (usedPercent: number, windowMinutes: number, resetsInHours: number, blocked = false) =>
limitRow({
id: "window",
label: "Window",
usedPercent,
windowMinutes,
blocked,
resetsAt: new Date(Date.now() + resetsInHours * 3_600_000).toISOString(),
});
// Three and a half of seven days left, 70% of the limit left.
const week = at(30, 10080, 84);
assert.deepEqual(texts(week).slice(0, 3), ["Window", "70% remaining", "50% of the week left"]);
assert.ok(!texts(week).some((text) => text.includes("used") || text.includes("pace")), texts(week).join(" | "));
// The fill is the share left, against the right end, and the dashed line
// stands halfway, where the week has got to.
const all = nodes(week);
assert.ok(all.some((node) => node.justifyContent === "end" && nodes(node).some((child) => child.width === "70%")));
const line = all.find((node) => node.position === "absolute")!;
assert.ok(String(line.left).startsWith("50"), String(line.left));
assert.equal(nodes(line).filter((node) => node !== line).length, 3, "three dashes");
assert.ok(texts(at(60, 300, 3)).includes("60% of the 5h window left"));
assert.ok(texts(at(100, 300, 3, true)).includes("Used up"));
// Without a reset there is no time to show, so no line and no time left.
const unknown = limitRow({ id: "overage", label: "Extra Usage", usedPercent: 12 });
assert.deepEqual(texts(unknown), ["Extra Usage", "88% remaining"]);
assert.ok(!nodes(unknown).some((node) => node.position === "absolute"));
});
test("a ring is a full faint track and an arc clockwise from the top for its share", () => {
const [track, arc] = ringCommands(0.25, "warning") as { op: string; d: (string | number)[][]; stroke: string }[];
assert.equal(track.stroke, "mutedForeground/25");
assert.equal(arc.stroke, "warning");
const end = (d: (string | number)[][]) => d.at(-1)!.slice(-2).map((n) => Math.round(Number(n) * 100) / 100);
assert.deepEqual(arc.d[0].map((n) => (typeof n === "number" ? Math.round(n * 100) / 100 : n)), ["moveTo", 7, 1]);
assert.deepEqual(end(arc.d), [13, 7]);
assert.deepEqual(end(track.d), [7, 1]);
assert.equal(ringCommands(0, "danger").length, 1);
});
test("a limit's ring ends at the top and empties clockwise, like its bar, with a tick clockwise where the window's time has got to", () => {
const round = (n: unknown) => Math.round(Number(n) * 100) / 100;
// 75% left: the arc runs clockwise from three o'clock round to the top.
const [, arc, mark] = ringCommands(0.75, "mutedForeground", { drain: true, mark: 0.25 }) as { op: string; d: (string | number)[][]; stroke: string }[];
assert.deepEqual([arc.d[0][0], round(arc.d[0][1]), round(arc.d[0][2])], ["moveTo", 13, 7]);
assert.deepEqual(arc.d.at(-1)!.slice(-2).map(round), [7, 1]);
// A quarter through the window the tick stands at three o'clock: one solid
// stroke on the horizontal through the centre, across the ring's line
// (x = 13) as far inside it as outside, clear of the centre.
assert.equal(mark.stroke, "foreground");
assert.ok(mark.d.every((step) => round(step[2]) === 7));
const xs = mark.d.map((step) => round(step[1]));
assert.deepEqual(xs, [10.5, 15.5]);
assert.ok(Math.min(...xs) > 7, "the tick stays clear of the centre");
assert.equal(ringCommands(0.75, "mutedForeground", { drain: true }).length, 2, "no tick without a reset");
});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.