Official
sidebar
Built-in sidebar 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/sidebar@0.2.6
Permissions in 0.2.6
Files
peek.ts3.4 KB
// The latest model activity for a chat's live line: the end of the chat,
// read once and only for chats a row actually showed, never the whole
// transcript (an older host without `chats.peek` gives the whole one). Any
// other failure of the peek is shown once, not once per row, and the row
// shows its status until the chat changes and is read again.
import * as Effect from "effect/Effect";
import { Host, Notify } from "divergence/effect";
import type { HostError } from "divergence/effect";
import { createPreview, transcriptPreview } from "./preview.ts";
import type { SidebarState } from "./sections.ts";
const describe = (error: unknown): string => (error instanceof Error && error.message ? error.message : "unknown error");
// A host that does not know a method answers it with JSON-RPC's
// method-not-found code, or, before it had one for host methods, with an
// internal error that names the method as unknown.
export const isUnknownMethod = (error: HostError): boolean =>
error._tag === "HostCallFailed" && (error.code === -32601 || error.message.startsWith("unknown host method"));
export const makePeek = Effect.fn("Sidebar.peeking")(function* (state: SidebarState, redrawSoon: () => void) {
const host = yield* Host;
const notify = yield* Notify;
let previewNotice = false;
return Effect.fn("Sidebar.ensurePreview")(function* (chatId: string) {
const preview = (state.previews[chatId] ??= createPreview());
if (preview.loading || preview.revision) return;
preview.loading = true;
yield* host.call("host/chats.peek", { chatId, limit: 20 }).pipe(
Effect.catchIf(isUnknownMethod, () => host.call("host/chats.transcript", { chatId })),
Effect.tap((result) => Effect.gen(function* () {
// Streaming activity or a reset since this read takes precedence.
previewNotice = false;
if (state.previews[chatId] !== preview) return;
const saved = transcriptPreview(result.items);
if (!preview.revision) Object.assign(preview, saved, { loading: true });
else if (!preview.assistantLine) preview.assistantLine = saved.assistantLine;
redrawSoon();
// A bounded peek can end in many tool rows. Look further back for the
// last reply only for a settled chat and only while this read still owns the preview.
if (preview.assistantLine || result.items.length < 20 || state.chats.find((chat) => chat.id === chatId)?.status === "running") return;
let before: number | undefined;
while (state.previews[chatId] === preview) {
const revision = preview.revision;
const page = yield* host.call("host/chats.transcript", { chatId, limit: 100, before });
if (state.previews[chatId] !== preview || preview.revision !== revision) return;
const line = transcriptPreview(page.items).assistantLine;
if (line) { preview.assistantLine = line; redrawSoon(); return; }
if (!page.start) return;
before = page.start;
}
})),
Effect.catch((error: unknown) => {
// A reset, a removal or streamed activity since this read began
// made it moot.
if (state.previews[chatId] !== preview || preview.revision) return Effect.void;
preview.failed = true;
if (previewNotice) return Effect.void;
previewNotice = true;
return notify.error(`Chat preview unavailable: ${describe(error)}`);
}),
);
});
});
No comments yet.