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
chat-queue.test.ts7.6 KB
// A queued draft stays a draft, its prompt in the composer, until no chat
// runs; then the queue sends it and it becomes a running chat, one at a time.
import assert from "node:assert/strict";
import test from "node:test";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import { DraftLedger, InputLedger, withController } from "./testing.ts";
import { onSubmit, queueChat } from "./composer.ts";
import { onChatChanged } from "./events.ts";
import { chat } from "./ui-for-tests.test.ts";
import type { Controller } from "./controller.ts";
import type { Chat } from "divergence";
const settle = Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 20)));
const change = (c: Controller, id: string, overrides: Partial<Chat>) =>
onChatChanged(c, { event: "chat_changed", chat: { ...c.state.chats[id]!, ...overrides, updatedAt: new Date().toISOString() } });
const queued = (c: Controller) => c.state.chatQueue.map((entry) => entry.chatId);
for (const fail of [false, true]) {
test(`draft queue hydration owns one release and retains failed reads (fail=${fail})`, async () => {
const ledger = new DraftLedger();
delete ledger.snapshots.c1.sessionId;
ledger.draft = { chatId: "c1", revision: 1, blocks: [{ type: "text", text: "Read README.md" }] };
const entered = Promise.withResolvers<void>(), gate = Promise.withResolvers<void>();
const call = ledger.call.bind(ledger);
let reads = 0;
ledger.call = async (method, params) => {
if (method === "host/drafts.get") {
reads++;
entered.resolve();
await gate.promise;
if (fail && reads === 1) throw new Error("draft read failed");
}
return call(method, params);
};
await withController(ledger, (c) => Effect.gen(function* () {
delete c.state.composers.c1;
c.state.chatQueue = [{ chatId: "c1" }];
const first = yield* Effect.forkChild(c.pumpChatQueue());
yield* Effect.promise(() => entered.promise);
assert.equal(c.state.releasing, "c1");
yield* c.pumpChatQueue();
assert.equal(reads, 1, "another pump cannot acquire the same queued draft");
gate.resolve();
yield* Fiber.join(first);
if (fail) {
assert.equal(c.state.releasing, null);
assert.deepEqual(queued(c), ["c1"]);
assert.deepEqual(ledger.deliveries, []);
assert.ok(ledger.notices.some((notice) => notice.includes("draft read failed")));
yield* c.pumpChatQueue();
}
assert.equal(ledger.deliveries.length, 1);
assert.deepEqual(ledger.deliveries[0].blocks, [{ type: "text", text: "Read README.md" }]);
assert.deepEqual(queued(c), []);
}));
});
}
test("a restored draft queue loads its durable image-only prompt before release", async () => {
const ledger = new DraftLedger();
delete ledger.snapshots.c1.sessionId;
const blocks = [{ type: "image" as const, data: "saved-image-bytes", mimeType: "image/png" }];
ledger.draft = { chatId: "c1", revision: 1, blocks };
ledger.storage.queues = { chats: [{ chatId: "c1" }] };
await withController(ledger, (c) => Effect.gen(function* () {
delete c.state.composers.c1;
c.state.chats.busy = chat({ id: "busy", status: "running", sessionId: "busy-session" });
yield* c.restoreQueues();
yield* settle;
assert.deepEqual(queued(c), ["c1"]);
assert.equal(ledger.deliveries.length, 0);
c.state.chats.busy = { ...c.state.chats.busy, status: "idle" };
yield* c.pumpChatQueue();
assert.deepEqual(ledger.deliveries.map((delivery) => delivery.blocks), [blocks]);
assert.deepEqual(queued(c), []);
assert.deepEqual(ledger.draft.blocks, []);
assert.deepEqual(ledger.notices, []);
}));
});
test("a queued draft keeps its prompt and starts only when no chat runs", async () => {
const ledger = new InputLedger(["busy", "d1"]);
await withController(ledger, (c) =>
Effect.gen(function* () {
c.state.chats.busy = chat({ id: "busy", sessionId: "s", status: "running" });
c.state.chats.d1 = chat({ id: "d1" });
c.composer("d1").loading = false;
c.composer("d1").text = "Fix the tests";
yield* queueChat(c, "d1");
yield* settle;
assert.deepEqual(queued(c), ["d1"]);
assert.equal(c.composer("d1").text, "Fix the tests", "the prompt stays in the draft");
assert.equal(ledger.deliveries.length, 0, "another chat runs");
// Still editable while it waits: the edit is what is sent.
c.composer("d1").text = "Fix the failing tests";
yield* change(c, "busy", { status: "idle" });
yield* settle;
assert.deepEqual(queued(c), []);
assert.deepEqual(ledger.deliveries.map((d) => [d.chatId, d.blocks]), [
["d1", [{ type: "text", text: "Fix the failing tests" }]],
]);
}),
);
});
test("queued drafts start one at a time, in order", async () => {
const ledger = new InputLedger(["d1", "d2"]);
await withController(ledger, (c) =>
Effect.gen(function* () {
for (const id of ["d1", "d2"]) {
c.state.chats[id] = chat({ id });
c.composer(id).loading = false;
c.composer(id).text = `prompt ${id}`;
}
// Nothing runs: the first starts at once, the second waits for it.
yield* queueChat(c, "d1");
yield* queueChat(c, "d2");
yield* settle;
assert.deepEqual(ledger.deliveries.map((d) => d.chatId), ["d1"]);
assert.deepEqual(queued(c), ["d2"]);
yield* change(c, "d1", { status: "running" });
yield* settle;
assert.deepEqual(ledger.deliveries.map((d) => d.chatId), ["d1"]);
yield* change(c, "d1", { status: "idle", sessionId: "s1" });
yield* settle;
assert.deepEqual(ledger.deliveries.map((d) => d.chatId), ["d1", "d2"]);
assert.deepEqual(queued(c), []);
}),
);
});
test("a queued draft whose command starts no run lets the next queued draft start", async () => {
const ledger = new InputLedger(["d1", "d2"]);
await withController(ledger, (c) =>
Effect.gen(function* () {
for (const [id, text] of [["d1", "/no-prompt"], ["d2", "prompt d2"]]) {
c.state.chats[id] = chat({ id });
c.composer(id).loading = false;
c.composer(id).text = text;
}
yield* queueChat(c, "d1");
yield* queueChat(c, "d2");
yield* settle;
assert.deepEqual(ledger.deliveries.map((d) => d.chatId), ["d1", "d2"]);
assert.deepEqual(queued(c), []);
assert.equal(c.state.releasing, "d2", "the started draft holds the queue until it shows running");
}),
);
});
test("sending a queued draft by hand takes it out of the queue", async () => {
const ledger = new InputLedger(["busy", "d1"]);
await withController(ledger, (c) =>
Effect.gen(function* () {
c.state.chats.busy = chat({ id: "busy", sessionId: "s", status: "running" });
c.state.chats.d1 = chat({ id: "d1" });
c.composer("d1").loading = false;
c.composer("d1").text = "Now";
yield* queueChat(c, "d1");
yield* onSubmit(c, "d1", { value: "Now", secondary: false, shift: false });
assert.deepEqual(queued(c), []);
assert.deepEqual(ledger.deliveries.map((d) => d.chatId), ["d1"]);
}),
);
});
test("a queue saved with its prompt puts the prompt back in the draft", async () => {
const ledger = new InputLedger(["d1"]);
ledger.storage.queues = { chats: [{ chatId: "d1", text: "Old prompt", started: false }] };
await withController(ledger, (c) =>
Effect.gen(function* () {
yield* c.restoreQueues();
assert.deepEqual(queued(c), ["d1"]);
assert.equal(c.composer("d1").text, "Old prompt");
assert.deepEqual(ledger.storage.queues, { prompts: {}, chats: [{ chatId: "d1" }] });
}),
);
});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.