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
notifications.ts4.7 KB
// System notifications about chats (docs/features/chat/notifications.md). A turn that ends or
// fails, or stops for an approval or an answer, while the user cannot see its chat (the window
// is not focused, or the chat is not on screen) gets a notification naming the chat; the kernel
// posts it and opens the chat when it is clicked. What is announced and how it reads follow
// t3code's notifications, in Divergence's words.
import * as Effect from "effect/Effect";
import * as Stream from "effect/Stream";
import { Events } from "divergence/effect";
import type { KernelCallFailed, PermissionNotGranted } from "divergence/effect";
import type { HostEvents, SettledRun } from "divergence";
import type { Controller } from "./controller.ts";
import { chatsOnScreen } from "./layout.ts";
/** Each notification's title; its body is the chat's title. */
export const TITLES = {
finished: "Chat finished",
failed: "Chat failed",
limited: "Usage limit reached",
approval: "Approval needed",
question: "Input needed",
} as const;
export type Notice = keyof typeof TITLES;
/** The host events notifications follow. */
export type Observed = HostEvents["run_settled"] | HostEvents["agent"] | HostEvents["chat_removed"];
/** What was announced, so that nothing is announced twice. */
export class Announced {
/** The waiting requests of each chat already announced, by subagent and request id. */
private readonly asked = new Map<string, Set<string>>();
/** A turn that ended before the plugin started (one the host settles at launch) is old news. */
private readonly since: number;
constructor(since = Date.now()) {
this.since = since;
}
/** What one event is news of, if anything. */
notice(event: Observed): Notice | null {
if (event.event === "run_settled") return this.settled(event.chatId, event.run);
if (event.event === "chat_removed") {
this.asked.delete(event.chatId);
return null;
}
const kind = event.kind;
if (kind.event === "approval" || kind.event === "question")
return this.asks(event.chatId, kind.event, `${event.taskId ?? ""}/${kind.id}`);
if (kind.event === "approval_resolved" || kind.event === "question_resolved")
this.asked.get(event.chatId)?.delete(`${event.taskId ?? ""}/${kind.id}`);
return null;
}
/** The host settles each turn once: one that finished or failed is news, one you stopped is not. */
private settled(chatId: string, run: SettledRun): Notice | null {
this.asked.delete(chatId);
if (Date.parse(run.stoppedAt) < this.since) return null;
if (run.outcome.status === "completed") return "finished";
if (run.outcome.status === "failed") return run.usageLimit ? "limited" : "failed";
return null;
}
/** The same request again, as an agent repeats one it still waits on, is not news. */
private asks(chatId: string, kind: "approval" | "question", key: string): Notice | null {
const asked = this.asked.get(chatId) ?? new Set<string>();
this.asked.set(chatId, asked);
if (asked.has(key)) return null;
asked.add(key);
return kind;
}
}
/** An app older than notifications has no `notification.show`; it shows none. */
const olderApp = (error: KernelCallFailed | PermissionNotGranted): boolean =>
error._tag === "KernelCallFailed" && /method not found|unknown kernel method/.test(error.message);
/**
* Posts a notice about a chat, unless notifications are off in Settings or the chat is archived.
* Whether the window is focused is the kernel's to tell: for a chat on screen it posts only while
* the window is not focused (`unlessFocused`), and for any other chat either way.
*/
export const announce = Effect.fn("Chat.announce")(function* (c: Controller, chatId: string, notice: Notice) {
const chat = c.state.chats[chatId];
if (!chat || chat.archived || c.api.settings.get("notifications") === false) return;
const unlessFocused = chatsOnScreen(c).has(chatId);
yield* c.kernel
.call("notification.show", { chatId, title: TITLES[notice], body: c.titleOf(chat), unlessFocused })
.pipe(Effect.catchIf(olderApp, () => Effect.void));
});
export const followNotifications = Effect.fn("Chat.followNotifications")(function* (c: Controller) {
const events = yield* Events;
const announced = new Announced();
const observe = (event: Observed) => {
const notice = announced.notice(event);
if (!notice) return Effect.void;
const posted = announce(c, event.chatId, notice).pipe(
Effect.catch((error) => c.notify.error(`The notification could not be shown: ${error.message}`)),
);
return Effect.forkIn(posted, c.scope, { startImmediately: true });
};
for (const name of ["run_settled", "agent", "chat_removed"] as const)
yield* events.stream(name).pipe(Stream.runForEach(observe), Effect.forkScoped({ startImmediately: true }));
});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.