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.8
Permissions in 0.2.8
Files
events.ts13.1 KB
// Event listeners update only the affected chat pane, matching the streaming baseline.
// Per-chat FIFO releases only on host settlement. run_finished is transcript
// progress, never evidence that required persistence/checkpoints have finished.
import * as Effect from "effect/Effect";
import * as Stream from "effect/Stream";
import { Events } from "divergence/effect";
import type { HostEvents } from "divergence";
import type { Controller } from "./controller.ts";
import { applyEvent, followSession, noteSession, startRun } from "./transcript.ts";
import { isLive } from "./subagents.ts";
import { loadAgents, loadCheckpoints, reconcileOutgoing, setStarting } from "./data.ts";
import { forgetTask, noteTask, redraw } from "./layout.ts";
import { followShortcuts } from "./shortcuts.ts";
import { endTurns, followTurnChanges } from "./changes.ts";
import { followNotifications } from "./notifications.ts";
import { registeredPane } from "./state.ts";
export const onAgent = Effect.fnUntraced(function* (c: Controller, event: HostEvents["agent"]) {
const composer = c.state.composers[event.chatId],
kind = event.kind;
if (composer) {
if (kind.event === "config_options") {
composer.options = kind.options;
const agentId = c.state.chats[event.chatId]?.agentId;
if (agentId && composer.agentId === agentId) c.state.agentOptions[agentId] = kind.options;
c.view.update();
c.updateSession(event.chatId);
} else if (kind.event === "commands") {
composer.commands = kind.commands;
c.view.update();
} else if (kind.event === "skills") {
composer.skills = kind.skills;
c.view.update();
}
}
if (kind.event === "run_finished") c.view.update();
// The host takes a prompt's checkpoint after it shows the message, and Revert waits for the run to end: the checkpoints
// are read then, so Revert shows only under a message that has one.
if (kind.event === "run_finished" && !event.taskId && c.state.transcripts[event.chatId])
yield* Effect.forkIn(loadCheckpoints(c, event.chatId), c.scope, { startImmediately: true });
if (kind.event === "usage_blocked" && !event.taskId) {
c.scheduler.activeLimits[event.chatId] = {
runId: c.scheduler.snapshots[event.chatId]?.runId,
evidence: kind.recovery,
};
c.refresh(event.chatId);
}
const transcript = c.state.transcripts[event.chatId];
if (transcript) transcript.version++;
if (transcript && kind.event === "usage" && !event.taskId) {
transcript.usage = kind;
if (c.api.app.state().chatId === event.chatId) c.titleView?.update();
c.updateSession(event.chatId);
return;
}
if (!transcript || !applyEvent(transcript, kind, event.taskId)) return;
if (kind.event === "question_resolved" || kind.event === "run_finished")
yield* Effect.forkIn(c.answers.prune(event.chatId), c.scope, { startImmediately: true });
if (event.taskId || kind.event === "task" || kind.event === "approval_resolved") {
c.refreshSubagentTabs(event.chatId);
if (kind.event === "task") {
c.presentations?.updateTitle(event.chatId);
c.updateSession(event.chatId);
if (isLive(kind)) yield* Effect.forkIn(c.tickSubagents(), c.scope, { startImmediately: true });
}
}
const chat = c.state.chats[event.chatId],
pane = chat ? c.paneOf(chat) : null;
if (pane) c.updatePane(pane);
else if (!pane && c.api.app.state().chatId === event.chatId) c.center?.update();
});
export const onChatChanged = Effect.fn("Chat.onChatChanged")(function* (
c: Controller,
event: HostEvents["chat_changed"],
) {
const before = c.state.chats[event.chat.id];
c.state.chats[event.chat.id] = event.chat;
const transcript = c.state.transcripts[event.chat.id];
if (transcript && followSession(transcript, before, event.chat, c.isRunning(event.chat))) transcript.version++;
if (event.chat.archived && !before?.archived) c.removePane(event.chat.id);
const moved = before && (before.agentId !== event.chat.agentId || before.workspaceId !== event.chat.workspaceId);
if (moved && c.state.composers[event.chat.id])
yield* Effect.forkIn(c.loadOptions(event.chat.id), c.scope, { startImmediately: true });
if (c.scheduler.ready && (!c.scheduler.snapshots[event.chat.id] || moved))
yield* Effect.forkIn(c.scheduler.load(event.chat.id), c.scope, { startImmediately: true });
yield* c.syncDraftTitle(event.chat.id);
// A queued draft starts once no chat runs; the one just sent holds the
// queue until the host reports it running.
if (c.state.releasing === event.chat.id && (c.isRunning(event.chat) || event.chat.sessionId || event.chat.archived))
c.state.releasing = null;
// The host reports the run itself now.
if (c.isRunning(event.chat)) setStarting(c, event.chat.id, false);
if (event.chat.archived) yield* c.unqueueChat(event.chat.id);
yield* Effect.forkIn(c.pumpChatQueue(), c.scope, { startImmediately: true });
// Stream consumers run after the slot listener; repaint with the updated
// chat metadata. The changed chat's own pane renders now if it is on
// screen, else when it shows again.
c.presentations?.updateTitle(event.chat.id);
c.updateSession(event.chat.id);
if (event.chat.id === c.api.app.state().chatId)
yield* Effect.forkIn(c.followApp(), c.scope, { startImmediately: true });
c.view.update();
c.refresh(event.chat.id);
});
export const onAgentsChanged = Effect.fn("Chat.onAgentsChanged")(function* (c: Controller) {
yield* Effect.forkIn(loadAgents(c), c.scope, {
startImmediately: true,
});
for (const [chatId, composer] of Object.entries(c.state.composers))
if (!composer.options.length && !composer.loading)
yield* Effect.forkIn(c.loadOptions(chatId), c.scope, { startImmediately: true });
});
export const onChatRemoved = Effect.fn("Chat.onChatRemoved")(function* (
c: Controller,
event: HostEvents["chat_removed"],
) {
delete c.state.transcripts[event.chatId];
delete c.state.composers[event.chatId];
if (c.state.restored) delete c.state.restored[event.chatId];
yield* Effect.forkIn(c.saveUnsent(), c.scope, { startImmediately: true });
delete c.state.chats[event.chatId];
if (c.state.releasing === event.chatId) c.state.releasing = null;
yield* c.unqueueChat(event.chatId);
yield* Effect.forkIn(c.pumpChatQueue(), c.scope, { startImmediately: true });
delete c.scheduler.snapshots[event.chatId];
delete c.scheduler.activeLimits[event.chatId];
c.removePane(event.chatId);
});
// All streams and background listeners end with the owning plugin scope.
/** The host's layouts: a task drawn again, gone, or the side split. */
const listenLayouts = Effect.fn("Chat.listenLayouts")(function* (c: Controller) {
const events = yield* Events;
yield* events.stream("task_changed").pipe(
Stream.runForEach((event) =>
Effect.sync(() => {
noteTask(c.state.layouts, event.task);
redraw(c, event.task.id);
}),
),
Effect.forkScoped({ startImmediately: true }),
);
yield* events.stream("task_removed").pipe(
Stream.runForEach((event) =>
Effect.sync(() => {
forgetTask(c.state.layouts, event.taskId);
redraw(c, event.taskId);
}),
),
Effect.forkScoped({ startImmediately: true }),
);
// A tab's owner closed it and its kind asks first: the question shows in its pane.
yield* events.stream("tab_close_asked").pipe(
Stream.runForEach((event) =>
Effect.sync(() => {
c.state.layouts.asking = { tab: event.tab, question: event.question };
c.center?.update();
}),
),
Effect.forkScoped({ startImmediately: true }),
);
yield* events.stream("side_split_changed").pipe(
Stream.runForEach((event) =>
Effect.sync(() => {
c.state.layouts.side = event.side;
redraw(c);
}),
),
Effect.forkScoped({ startImmediately: true }),
);
});
export const listen = Effect.fn("Chat.listen")(function* (c: Controller) {
const events = yield* Events;
const fork = <A, E>(effect: Effect.Effect<A, E>) => Effect.forkIn(effect, c.scope, { startImmediately: true });
for (const event of ["plugins_changed", "settings_changed"] as const)
yield* events.stream(event).pipe(
Stream.runForEach(() => fork(c.loadContextPolicy())),
Effect.forkScoped({ startImmediately: true }),
);
yield* events.stream("draft_changed").pipe(
Stream.runForEach((event) => Effect.sync(() => c.drafts.observe(event.draft))),
Effect.forkScoped({ startImmediately: true }),
);
yield* events.stream("inputs_changed").pipe(
Stream.runForEach((event) => {
c.scheduler.receive(event.snapshot);
return fork(c.scheduler.evaluate());
}),
Effect.forkScoped({ startImmediately: true }),
);
yield* events.stream("run_settled").pipe(
Stream.runForEach((event) => fork(c.scheduler.load(event.chatId).pipe(Effect.andThen(c.scheduler.evaluate())))),
Effect.forkScoped({ startImmediately: true }),
);
yield* events.stream("workspaces_changed").pipe(
Stream.runForEach(() =>
fork(
c.loadWorkspaces().pipe(Effect.catch((error) => Effect.logWarning(`workspaces unavailable: ${error.message}`))),
),
),
Effect.forkScoped({ startImmediately: true }),
);
yield* listenLayouts(c);
yield* followTurnChanges(c);
yield* followShortcuts(c);
yield* followNotifications(c);
yield* events.stream("chat_changed").pipe(
Stream.runForEach((event) => fork(onChatChanged(c, event))),
Effect.forkScoped({ startImmediately: true }),
);
yield* events.stream("agents_changed").pipe(
Stream.runForEach(() => fork(onAgentsChanged(c))),
Effect.forkScoped({ startImmediately: true }),
);
yield* events.stream("agent").pipe(
Stream.runForEach((event) => onAgent(c, event)),
Effect.forkScoped({ startImmediately: true }),
);
yield* events.stream("user_message").pipe(
Stream.runForEach((event) =>
fork(
Effect.gen(function* () {
const transcript = c.state.transcripts[event.chatId];
if (!transcript) return;
transcript.version++;
const index = transcript.items.findIndex((item) => item.id === event.item.id);
if (index < 0) {
transcript.items.push(event.item);
endTurns(transcript, event.item);
} else transcript.items[index] = event.item;
noteSession(transcript, event.item, c.state.chats[event.chatId]);
reconcileOutgoing(c, event.chatId);
startRun(transcript, event.item);
c.refresh(event.chatId);
if (c.api.app.state().chatId === event.chatId) yield* c.loadImages(event.chatId);
}),
),
),
Effect.forkScoped({ startImmediately: true }),
);
yield* events.stream("transcript_reset").pipe(
Stream.runForEach((event) =>
fork(
Effect.gen(function* () {
const transcript = c.state.transcripts[event.chatId];
if (!transcript) return;
transcript.version++;
// Keep the retained rows/composer while replacement history loads.
// A load already in flight notices this version and reads again.
// Every pane on screen reads again, a split pane that is not selected too.
if (c.shown(event.chatId)) yield* c.loadTranscript(event.chatId);
else transcript.snapshot = { kind: "unread" };
}),
),
),
Effect.forkScoped({ startImmediately: true }),
);
yield* events.stream("security_request").pipe(
Stream.runForEach(({ request }) =>
Effect.sync(() => {
const transcript = request.chatId ? c.state.transcripts[request.chatId] : null;
if (!transcript) return;
transcript.requests = [...transcript.requests.filter((held) => held.id !== request.id), request];
c.refresh(request.chatId);
}),
),
Effect.forkScoped({ startImmediately: true }),
);
yield* events.stream("security_resolved").pipe(
Stream.runForEach(({ id }) =>
Effect.sync(() => {
for (const [chatId, transcript] of Object.entries(c.state.transcripts)) {
if (!transcript.requests.some((request) => request.id === id)) continue;
transcript.requests = transcript.requests.filter((request) => request.id !== id);
c.refresh(chatId);
}
}),
),
Effect.forkScoped({ startImmediately: true }),
);
yield* events.stream("chat_removed").pipe(
Stream.runForEach((event) => fork(onChatRemoved(c, event))),
Effect.forkScoped({ startImmediately: true }),
);
yield* fork(followDays(c));
});
/** A message's time says "Yesterday" from the next day on, and a row draws only when what it reads changes: when the day
* turns, every pane draws again, a hidden one once it shows. A check each minute holds across the Mac sleeping through
* midnight, which a timer set for midnight would not. */
const followDays = (c: Controller) => {
let day = new Date().toDateString();
return Effect.forever(
Effect.sleep("1 minute").pipe(
Effect.andThen(
Effect.sync(() => {
const today = new Date().toDateString();
if (today === day) return;
day = today;
for (const kept of Object.values(c.state.panes)) {
const pane = registeredPane(kept);
if (pane) c.updatePane(pane);
}
}),
),
),
);
};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.