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.7
Permissions in 0.2.7
Files
main.ts5.7 KB
// Register the sidebar synchronously, its commands, and the event
// subscriptions that keep its state current; the initial refresh runs in
// the plugin scope.
import * as Effect from "effect/Effect";
import * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
import { App, Commands, Events, Services, Views, runPlugin } from "divergence/effect";
import { SidebarService } from "./contract.ts";
import { Sidebar } from "./controller.ts";
import { completedOf, rowOfTask } from "./sections.ts";
import { renderSidebar, renderTitleLead } from "./views.tsx";
const program = Effect.gen(function* () {
const sidebar = yield* Sidebar;
const views = yield* Views;
// Views register synchronously at the start of the program so reload
// keeps the view's identity (same slot, same order).
sidebar.views.push(
yield* views.add("left", { slot: "left", render: (ctx) => renderSidebar(ctx, sidebar), on: ["workspaces_changed", "chat_changed", "chat_removed", "agents_changed", "app_state"] }),
// The top controls in the title strip, after the sidebar toggle, while they fit there.
yield* views.add("title-lead", { slot: "title", render: (ctx) => renderTitleLead(ctx, sidebar), on: ["workspaces_changed", "app_state"] }),
);
const commands = yield* Commands;
const app = yield* App;
yield* commands.register("sidebar.newChat", sidebar.newChatCommand());
yield* commands.register("sidebar.toggleChatCompleted", sidebar.toggleCompletedCommand());
yield* commands.register("sidebar.closeChat", sidebar.closeChatCommand());
// Cmd+Shift+T hands a chat Cmd+W closed here as the row target.
yield* commands.register("sidebar.reopenChat", (info) => sidebar.reopenChat(info.target?.kind === "row" ? info.target.chatId : undefined));
yield* commands.register("sidebar.addWorkspace", sidebar.addWorkspace());
// The workspace picker in the sidebar's top row, which Settings replaces.
yield* commands.register(
"sidebar.chooseWorkspaces",
Effect.gen(function* () {
const { route } = yield* app.state;
if (route && route !== "chat") yield* sidebar.route("chat");
yield* views.focus("workspace-filter");
}),
);
yield* commands.register("sidebar.togglePin", sidebar.onScreenRow((chat) => sidebar.togglePin(chat)));
yield* commands.register("sidebar.snoozeChat", sidebar.onScreenRow((chat) => completedOf(sidebar.state, chat) ? Effect.void : sidebar.snoozeRow(chat, null)));
yield* commands.register("sidebar.archiveRow", sidebar.onScreenRow((chat, task) => (task ? sidebar.archiveTask(task.id) : sidebar.archive(chat))));
yield* commands.register("sidebar.openRowMenu", sidebar.onScreenRow((chat) => sidebar.openRowMenu(chat.id, false)));
for (let n = 1; n <= 9; n++) yield* commands.register(`sidebar.openChat${n}`, sidebar.selectListedCommand(n - 1));
sidebar.declareChatActions();
const events = yield* Events;
// Event followers are fire-and-forget like the original's async handlers:
// a failed refresh is logged and leaves the state for the next event, and
// the rest of the plugin keeps working.
const follow = <A, E>(
stream: Stream.Stream<A, E>,
handle: (event: A) => Effect.Effect<void, unknown>,
): Effect.Effect<void, never, Scope.Scope> =>
stream.pipe(
Stream.mapEffect(
(event) =>
handle(event).pipe(Effect.ignore({ log: "Warn", message: "The sidebar could not follow a host event" })),
{ concurrency: "unbounded" },
),
Stream.runDrain,
Effect.forkScoped,
);
yield* follow(events.stream("workspaces_changed"), () => sidebar.refresh());
yield* follow(events.stream("agents_changed"), () => sidebar.refresh());
// A row brought back from the archive takes its place once the host reports it whole.
yield* follow(events.stream("chat_changed"), (event) => sidebar.chatChanged(event).pipe(Effect.andThen(sidebar.land())));
yield* follow(events.stream("chat_removed"), (event) => sidebar.chatRemoved(event));
yield* follow(events.stream("task_changed"), (event) => sidebar.taskChanged(event).pipe(Effect.andThen(sidebar.land())));
yield* follow(events.stream("task_removed"), (event) => sidebar.taskRemoved(event));
yield* follow(events.stream("transcript_reset"), (event) => sidebar.transcriptReset(event));
yield* follow(events.stream("agent"), (event) => sidebar.agentEvent(event));
// The search button's tooltip names the key Cmd+P is bound to now.
yield* follow(events.stream("keybindings_changed"), () => sidebar.loadKeys());
yield* follow(events.stream("plugins_changed"), () => sidebar.loadKeys());
yield* Effect.forkScoped(sidebar.loadKeys().pipe(Effect.ignore({ log: "Warn", message: "The sidebar could not read the keyboard shortcuts" })));
// The first load runs in the program fiber, like the original's refresh()
// at the end of activate: its list calls leave before activate returns.
// A failed load says so in place of the list, and the next event retries.
yield* sidebar.refresh().pipe(Effect.catch(sidebar.loadFailed));
// Other plugins snooze rows by the task id their row menu entries get,
// once the first load has told the sidebar which rows there are.
const services = yield* Services;
yield* services.provide(SidebarService, {
snooze: ({ taskId, until }) => {
const chat = rowOfTask(sidebar.state, taskId);
if (!chat) return Effect.fail({ _tag: "UnknownRow" as const, message: `The sidebar lists no row for ${taskId}.` });
return sidebar.snoozeRow(chat, until).pipe(Effect.as(null));
},
unsnooze: ({ taskId }) => {
const chat = rowOfTask(sidebar.state, taskId);
return chat ? sidebar.unsnoozeRow(chat).pipe(Effect.as(null)) : Effect.succeed(null);
},
});
});
export default runPlugin(Sidebar.layer, program);
No comments yet.