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.5
Permissions in 0.2.5
Files
peek.ts2.5 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.sync(() => {
// Streaming activity or a reset since this read takes precedence.
previewNotice = false;
if (state.previews[chatId] === preview && !preview.revision) {
Object.assign(preview, transcriptPreview(result.items), { loading: true });
redrawSoon();
}
}),
),
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.