Official
palette
Built-in command palette (⌘⇧P) and chat search (⌘P).
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/palette@0.2.2
Permissions in 0.2.2
Files
model.ts5.2 KB
// The palette's model: the order of what it lists, by the fuzzy matcher
// the sidebar's search shares, and where the selection sits. Pure
// functions over plain objects, so the tests run them in Node.
import type { Chat, CommandInfo, PaletteProviderInfo, Workspace } from "divergence";
import { fuzzy } from "../sdk/ui/fuzzy.ts";
export type Command = Pick<CommandInfo, "id" | "title" | "category"> &
Partial<Pick<CommandInfo, "enabled" | "registered">>;
export type ChatEntry = Pick<Chat, "id" | "workspaceId"> & Partial<Pick<Chat, "title" | "updatedAt" | "archived">>;
export type Provider = Pick<PaletteProviderInfo, "plugin" | "id"> & Partial<Pick<PaletteProviderInfo, "prefix">>;
export type Mode<P> = { mode: "commands" | "chats"; query: string } | { mode: "provider"; provider: P; query: string };
/// How many rows the palette draws at once.
export const VISIBLE = 12;
/// How many commands the palette remembers as recently run.
export const RECENT_LIMIT = 20;
/// The text a command is searched and shown by: `Category: Title`.
export function commandLabel(command: Pick<CommandInfo, "title" | "category">) {
return command.category ? `${command.category}: ${command.title}` : command.title;
}
/// The commands the palette lists for `query`: those whose `when` holds and
/// whose plugin runs, fuzzy over the title and the category; with no
/// query, recently run first, then by label.
export function rankCommands<C extends Command>(
commands: readonly C[],
query: unknown,
recent: readonly string[] = [],
) {
const shown = commands.filter((command) => command.enabled !== false && command.registered !== false);
const recency = (command: C) => {
const at = recent.indexOf(command.id);
return at < 0 ? Infinity : at;
};
if (!String(query ?? "").trim()) {
return shown
.map((command) => ({ command, positions: [] }))
.sort(
(a, b) =>
recency(a.command) - recency(b.command) || commandLabel(a.command).localeCompare(commandLabel(b.command)),
);
}
return shown
.map((command) => {
const label = commandLabel(command);
const match = fuzzy(query, label) ?? fuzzy(query, command.title);
return match
? { command, positions: match.positions, score: match.score + (recency(command) < Infinity ? 3 : 0) }
: null;
})
.filter((candidate) => candidate !== null)
.sort((a, b) => b.score - a.score || recency(a.command) - recency(b.command));
}
/// The chats the palette lists for `query`: by title and workspace name,
/// recently active first.
export function rankChats<C extends ChatEntry>(
chats: readonly C[],
workspaces: Readonly<Record<string, Pick<Workspace, "name">>>,
query: unknown,
) {
const open = chats.filter((chat) => !chat.archived);
const name = (chat: C) => workspaces[chat.workspaceId]?.name ?? "";
const newest = (a: C, b: C) => String(b.updatedAt ?? "").localeCompare(String(a.updatedAt ?? ""));
if (!String(query ?? "").trim())
return open
.slice()
.sort(newest)
.map((chat) => ({ chat, positions: [] }));
return open
.map((chat) => {
const title = fuzzy(query, chat.title ?? "");
const workspace = fuzzy(query, name(chat));
const both = fuzzy(query, `${name(chat)} ${chat.title ?? ""}`);
const best = [
title && { ...title, positions: title.positions },
workspace && { score: workspace.score - 2, positions: [] },
both && { score: both.score - 4, positions: [] },
]
.filter((candidate) => candidate !== null)
.sort((a, b) => b.score - a.score)[0];
return best ? { chat, positions: best.positions, score: best.score } : null;
})
.filter((candidate) => candidate !== null)
.sort((a, b) => b.score - a.score || newest(a.chat, b.chat));
}
/// `recent` with `id` first, at most RECENT_LIMIT long.
export function remember(recent: readonly string[], id: string) {
return [id, ...recent.filter((known) => known !== id)].slice(0, RECENT_LIMIT);
}
/// The mode a query asks for in the chat palette (⌘P): `>` switches to
/// commands, a provider's prefix to that provider, anything else searches
/// chats. Returns `{ mode, provider?, query }` with the prefix taken off.
export function modeOf<P extends Provider>(query: unknown, providers: readonly P[] = []): Mode<P> {
const text = String(query ?? "");
if (text.startsWith(">")) return { mode: "commands", query: text.slice(1).trimStart() };
const provider = providers
.filter((p): p is P & { prefix: string } => !!p.prefix && text.startsWith(p.prefix))
.sort((a, b) => b.prefix.length - a.prefix.length)[0];
if (provider) return { mode: "provider", provider, query: text.slice(provider.prefix.length).trimStart() };
return { mode: "chats", query: text };
}
/// Moves the selection by `delta` rows, wrapping around.
export function move(selected: number, count: number, delta: number) {
if (!count) return 0;
return (((selected + delta) % count) + count) % count;
}
/// The rows to draw with the selection in sight.
export function window(count: number, selected: number, max = VISIBLE) {
if (count <= max) return { from: 0, to: count };
const from = Math.min(Math.max(0, selected - Math.floor(max / 2)), count - max);
return { from, to: from + max };
}
No comments yet.