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 convergence/chat@0.2.4
Permissions in 0.2.4
Files
panes.ts9.7 KB
// Each chat has one pane view, kept while it is one of the last few
// shown so switching back is instant. Detail views (a subagent, a
// workflow) open in tabs through the convergence/tabs service.
import * as Scope from "effect/Scope";
import * as Effect from "effect/Effect";
import type { Chat, Element, ViewHandle } from "convergence";
import type { Controller, ChatError, ChatView } from "./controller.ts";
import { paneReady, renderComposerView, renderPane } from "./views/panes.ts";
import { renderSubagentPane, renderWorkflowPane } from "./views/tasks.ts";
import { findTask, nameOf } from "./subagents.ts";
import { unknownMethod } from "./scheduler.ts";
import { errorMessage, loadOptions, loadRest, loadTranscript, loadRequests, noteRecent, refreshHistory } from "./data.ts";
/** Chats whose panes stay mounted: the last ten shown, so switching among recent chats shows one that is already laid out. */
const RETAINED = 10;
export const ensureComposer = Effect.fn("Chat.ensureComposer")(function* (c: Controller, chatId: string) {
if (!c.state.composers[chatId]) {
c.composer(chatId);
if (c.state.restored) delete c.state.restored[chatId];
yield* Effect.forkIn(
c.drafts.load(chatId).pipe(Effect.catch((error) => c.notify.error(`Draft unavailable: ${error.message}`))),
c.scope,
{ startImmediately: true },
);
yield* Effect.forkIn(loadOptions(c, chatId), c.scope, { startImmediately: true });
}
});
export const ensureChat = Effect.fn("Chat.ensureChat")(function* (c: Controller, chatId: string) {
const kept = c.state.transcripts[chatId];
if (!kept || kept.carried) {
if (kept) kept.carried = false;
else c.transcript(chatId);
yield* Effect.forkIn(loadRequests(c, chatId), c.scope, { startImmediately: true });
}
const transcript = c.transcript(chatId);
if (!transcript.loaded && !transcript.loading)
yield* Effect.forkIn(loadTranscript(c, chatId), c.scope, { startImmediately: true });
// Read ahead of time without the agent's history: that is read now.
else if (transcript.prefetched) {
transcript.prefetched = false;
yield* Effect.forkIn(refreshHistory(c, chatId), c.scope, { startImmediately: true });
yield* Effect.forkIn(loadRest(c, chatId), c.scope, { startImmediately: true });
}
});
// The chat's pane view, made the first time it shows; the oldest beyond
// the retained few goes.
export const ensurePane = Effect.fn("Chat.ensurePane")(function* (c: Controller, chatId: string) {
const pane = c.state.panes[chatId];
if (!pane) {
const view = yield* c.views
.slot(
"pane",
(ctx) => {
const tree = renderPane({ controller: c, ctx }, chatId);
// Until now the center showed a skeleton: a view with no tree yet
// draws nothing, and the center waited a render for it.
const drawn = c.state.panes[chatId];
const ready = paneReady(c, chatId);
if (drawn && (!drawn.drawn || drawn.ready !== ready)) {
drawn.drawn = true;
drawn.ready = ready;
c.center?.update();
}
return tree;
},
// Not `chat_changed`: it re-rendered every kept pane for any chat's
// change, before the listener updated the chat. `onChatChanged`
// renders the pane on screen, after.
{ key: `chat:${chatId}`, on: ["agents_changed"] },
)
.pipe(Effect.provideService(Scope.Scope, c.scope));
const composer = yield* c.views
.slot("composer", (ctx) => renderComposerView({ controller: c, ctx }, chatId), {
key: `composer:${chatId}`,
on: ["agents_changed"],
})
.pipe(Effect.provideService(Scope.Scope, c.scope));
c.state.panes[chatId] = { chatId, view, composer };
} else if (pane.stale) {
pane.stale = false;
pane.view.update();
pane.composer?.update();
}
c.state.recentPanes = [chatId, ...c.state.recentPanes.filter((id) => id !== chatId)];
c.state.prefetched = c.state.prefetched.filter((id) => id !== chatId);
for (const old of c.state.recentPanes.splice(RETAINED)) removePane(c, old);
});
/** Transcripts read ahead of time that no pane shows yet, at most. */
export const PREFETCHED = 3;
/// Reads a chat's newest page before it shows: when the pointer rests on its
/// sidebar row or the switcher selects it, so that opening it draws at once
/// instead of waiting for the read. Its requests and the agent's history are
/// read when it shows (`carried`). The oldest prefetch beyond the few kept
/// goes, as a pane beyond the retained ones does.
export const prefetchTranscript = Effect.fn("Chat.prefetchTranscript")(function* (c: Controller, chatId: string) {
if (c.state.transcripts[chatId] || !c.state.chats[chatId]) return;
const transcript = c.transcript(chatId);
transcript.carried = true;
transcript.prefetched = true;
c.state.prefetched = [chatId, ...c.state.prefetched.filter((id) => id !== chatId)];
for (const old of c.state.prefetched.splice(PREFETCHED)) if (!c.state.panes[old]) removePane(c, old);
yield* loadTranscript(c, chatId, { history: false });
});
// A chat whose pane goes keeps no transcript here; the host keeps less of
// it too. It is read again, newest page first, when it shows.
export function removePane(c: Controller, chatId: string) {
c.state.panes[chatId]?.view.remove();
c.state.panes[chatId]?.composer?.remove();
delete c.state.panes[chatId];
c.state.recentPanes = c.state.recentPanes.filter((id) => id !== chatId);
if (c.api.app.state().chatId === chatId) return;
const transcript = c.state.transcripts[chatId];
if (transcript && !transcript.loading) {
for (const item of transcript.items) delete c.state.itemImages[item.id];
delete c.state.transcripts[chatId];
}
// An older host has no `chats.close`: it keeps everything, as before.
Effect.runFork(c.host.call("host/chats.close", { chatId }).pipe(Effect.catch(() => Effect.void)));
}
// A host without the method is an older one: nothing to prepare. A failed
// preparation arrives as `prepareError` and is toasted; any other failure is toasted here.
const prepareChat = (c: Controller, chatId: string) =>
c.host
.call("host/chats.prepare", { chatId })
.pipe(
Effect.catch((error) =>
unknownMethod(error)
? Effect.void
: c.notify.error(`Preparing the conversation failed: ${errorMessage(error)}`),
),
);
export const followApp = Effect.fn("Chat.followApp")(function* (c: Controller): Effect.fn.Return<void, ChatError> {
const app = c.api.app.state();
if (app.route !== "chat" || !app.chatId) return;
const chat = c.state.chats[app.chatId];
if (!chat) return;
yield* ensurePane(c, chat.id);
yield* Effect.forkIn(noteRecent(c, chat.id), c.scope, { startImmediately: true });
// Once per show: the host resumes the chat's session now, so its first Send finds it ready.
if (chat.id !== c.state.preparedChat) {
c.state.preparedChat = chat.id;
yield* Effect.forkIn(prepareChat(c, chat.id), c.scope, { startImmediately: true });
}
if (chat.id !== c.state.promptedChat && !c.state.switcher) {
c.state.promptedChat = chat.id;
yield* c.views.focus(`prompt-${chat.id}`);
}
if (chat.id !== c.state.historyChat) {
c.state.historyChat = chat.id;
yield* Effect.forkIn(refreshHistory(c, chat.id), c.scope, { startImmediately: true });
}
});
const paneView = Effect.fn("Chat.paneView")(function* (
c: Controller,
key: string,
chatId: string,
render: (v: ChatView) => Element | null,
) {
let entry = c.state.paneViews[key];
if (!entry) {
const view = yield* c.views
.slot("subagent", (ctx) => render({ controller: c, ctx }), { key, on: ["chat_changed", "agents_changed"] })
.pipe(Effect.provideService(Scope.Scope, c.scope));
entry = c.state.paneViews[key] = { chatId, view };
}
return entry.view;
});
const showInPane = Effect.fn("Chat.showInPane")(function* (
c: Controller,
chat: Chat,
key: string,
title: string,
icon: string,
pane: ViewHandle,
) {
const client = c.tabs;
// Without the tabs plugin there is no detail pane; the shared request the
// baseline wrote would have had no reader either.
if (!client) return;
yield* client.open({ kind: "view", workspaceId: chat.workspaceId, path: key, title, icon, view: pane.id }).pipe(
// A call that loses its provider on the way behaves like no reader.
Effect.ignore,
);
});
export const openWorkflow = Effect.fn("Chat.openWorkflow")(function* (c: Controller, chat: Chat, taskId: string) {
const key = `workflow/${chat.id}/${taskId}`,
pane = yield* paneView(c, key, chat.id, (v) => renderWorkflowPane(v, chat.id, taskId));
const item = findTask(c.transcript(chat.id).items, taskId);
yield* showInPane(
c,
chat,
key,
item ? item.task.name || item.task.title || "Workflow" : "Workflow",
"workflow",
pane,
);
});
export const openSubagent = Effect.fn("Chat.openSubagent")(function* (
c: Controller,
chat: Chat,
taskId: string,
): Effect.fn.Return<void, ChatError> {
if (findTask(c.transcript(chat.id).items, taskId)?.task.workflow) return yield* openWorkflow(c, chat, taskId);
const key = `subagent/${chat.id}/${taskId}`,
pane = yield* paneView(c, key, chat.id, (v) => renderSubagentPane(v, chat.id, taskId));
const item = findTask(c.transcript(chat.id).items, taskId);
// A reconcile leaves a settled subagent's transcript unread; the tab reads it.
if (item && !item.items?.length)
yield* Effect.forkIn(
c.host
.call("host/chats.load_task", { chatId: chat.id, taskId })
.pipe(Effect.catch((error) => Effect.logWarning(`subagent history unavailable: ${errorMessage(error)}`))),
c.scope,
{ startImmediately: true },
);
yield* showInPane(c, chat, key, item ? item.task.title || nameOf(item.task) : "Subagent", "bot", pane);
});Versions
| Version | Published | Plugin API | Size | Permissions | Status |
|---|---|---|---|---|---|
| 0.2.5latest | Nov 21, 2026 | >=2 <4 | 174.6 KB | 5 permissions | Listed |
| 0.2.4 | Oct 25, 2026 | >=2 <4 | 174.2 KB | 5 permissions | Listed |
| 0.2.3 | Oct 24, 2026 | >=2 <4 | 165.4 KB | 5 permissions | Listed |
| 0.2.2 | Oct 23, 2026 | >=2 <4 | 165 KB | 5 permissions | Listed |
| 0.2.1 | Oct 6, 2026 | >=2 <3 | 111.8 KB | 5 permissions | Listed |
| 0.2.0 | Oct 5, 2026 | >=2 <3 | 105.1 KB | 5 permissions | Listed |
No comments yet.