Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
import { useEffect, useMemo, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { useAppStore } from "../../stores/app-store";
import { api } from "../../lib/api";
import { useRowBusy } from "../../hooks/use-row-busy";
import { usePluginBrowseState } from "./browse-state";
import { useMarketBrowse } from "./useMarketBrowse";
import type {
MarketPluginDetail,
MarketPluginSummary,
PluginPermissionReview,
PluginReviewSummary,
PluginReviewTicker,
PluginServiceStatus,
PluginSummary,
ProjectRecord,
} from "@pi-desktop/shared";
import {
GROUP_ORDER,
type GroupId,
TEMPLATE_IDS,
type TemplateId,
groupOf,
matchesQuery,
orderPermissions,
versionInstallable,
versionWithdrawn,
} from "./model";
import {
canCancelInstall,
isInstallCancelled,
newInstallJob,
nextInstallSample,
withInstallProgress,
type InstallSample,
type PluginInstallJob,
type PluginInstallRequest,
} from "./install-progress";
import { nextSummaryMap } from "./reviews";
export function usePluginsPage() {
const { t, i18n } = useTranslation();
const locale = i18n.language;
const plugins = useAppStore((s) => s.plugins);
const refreshPlugins = useAppStore((s) => s.refreshPlugins);
const showToast = useAppStore((s) => s.showToast);
const activateProject = useAppStore((s) => s.activateProject);
/**
* The folder open in this window. Scoping something to "this project" is only
* meaningful relative to it, so the control needs it as its default target.
*/
const currentProjectPath = useAppStore((s) => s.workspace?.path ?? null);
const {
tab,
setTab,
installedQuery,
setInstalledQuery,
query,
setQuery,
category,
setCategory,
sort,
setSort,
} = usePluginBrowseState();
const [projects, setProjects] = useState<ProjectRecord[]>([]);
const {
market,
marketLoading,
marketLoadingMore,
marketLoadMoreFailed,
marketTotal,
marketHasMore,
browseOrigin,
categories,
reloadMarket,
loadMoreMarket,
markMarketInstalled,
} = useMarketBrowse({
active: tab === "market",
locale,
onError: (e) => showToast(e instanceof Error ? e.message : String(e), { variant: "error" }),
});
// One busy key per plugin row: the first request to settle must not re-arm
// a row that is still in flight, and a busy row does not lock the others
// (the same contract the capability pages pin).
const rowBusy = useRowBusy();
const [installJob, setInstallJob] = useState<PluginInstallJob | null>(null);
/**
* The job, and the reading a transfer speed is measured against, are mirrored
* in refs: the progress subscription and the request's own answer must both
* see the install as it is now, not as it was when it started.
*/
const installJobRef = useRef<PluginInstallJob | null>(null);
const installSampleRef = useRef<InstallSample | null>(null);
const [reloadingId, setReloadingId] = useState<string | null>(null);
const [pendingInstall, setPendingInstall] = useState<{
id: string;
name: string;
permissions: string[];
newPermissions: string[];
version?: string;
} | null>(null);
/**
* A development plugin whose folder was chosen but not yet granted. Loading a
* folder is a request: nothing is registered until the user answers, so the
* declaration waits here the same way an install does.
*/
const [pendingReview, setPendingReview] = useState<PluginPermissionReview | null>(null);
const [autoUpdate, setAutoUpdate] = useState(true);
const [templatePick, setTemplatePick] = useState<TemplateId | null>(null);
const [creating, setCreating] = useState(false);
const [headerMenu, setHeaderMenu] = useState(false);
const [rowMenu, setRowMenu] = useState<string | null>(null);
const [selectedId, setSelectedId] = useState<string | null>(null);
const [detail, setDetail] = useState<MarketPluginDetail | null>(null);
const [detailLoading, setDetailLoading] = useState(false);
const [services, setServices] = useState<PluginServiceStatus[]>([]);
const [selectedVersion, setSelectedVersion] = useState("");
const [settingsPlugin, setSettingsPlugin] = useState<PluginSummary | null>(null);
/**
* Public rating summaries for every installed and marketplace plugin id.
* Batched so the installed tab can show a rating without a marketplace
* refresh, and kept as a map because it is a read that never holds anything
* else on the page back. `reviewsStatus` is `unavailable` — never zeros —
* when the centre cannot answer.
*/
const [reviewSummaries, setReviewSummaries] = useState<
Record<string, PluginReviewSummary>
>({});
/**
* The center's selected review line per plugin, batched with the summaries.
* Kept separate because it is optional decoration: a center that predates
* the ticker route must leave the cards intact, not put them in an error
* state.
*/
const [reviewTickers, setReviewTickers] = useState<
Record<string, PluginReviewTicker>
>({});
const [reviewsStatus, setReviewsStatus] = useState<
"idle" | "loading" | "ready" | "unavailable"
>("idle");
/** The plugin whose public review drawer is open over its detail sheet. */
const [reviewsId, setReviewsId] = useState<string | null>(null);
/**
* The install dialog is the top layer while it is open: the permission review
* behind it and the detail sheet below that both leave Escape to it.
*/
const installDialogOpen = installJob !== null;
/** Apply a change to the open install; reports whether a dialog was there. */
const updateInstallJob = (update: (job: PluginInstallJob) => PluginInstallJob): boolean => {
const current = installJobRef.current;
if (!current) return false;
const next = update(current);
installJobRef.current = next;
setInstallJob(next);
return true;
};
const closeInstallDialog = () => {
installJobRef.current = null;
installSampleRef.current = null;
setInstallJob(null);
};
/**
* Re-read the current page. An explicit refresh first pulls the catalog
* from the plugin center, which also drops the host's cached pages.
*/
const refreshMarket = async (opts?: { refreshRemote?: boolean }) => {
if (opts?.refreshRemote) {
try {
const meta = await api.marketRefresh(true);
showToast(
t("plugins.marketRefreshed", {
count: meta.pluginCount,
defaultValue: `Marketplace refreshed (${meta.pluginCount} plugins)`,
}),
{ variant: "success" },
);
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), { variant: "error" });
}
}
await reloadMarket();
};
const openDetail = async (id: string) => {
setSelectedId(id);
setDetailLoading(true);
try {
const res = await api.marketGetDetail(id);
const raw = res.plugin as MarketPluginDetail & {
summary?: MarketPluginSummary;
};
const plugin: MarketPluginDetail = raw.summary
? {
...raw.summary,
readmeMarkdown: raw.readmeMarkdown,
versions: raw.versions ?? [],
screenshots: raw.screenshots,
homepage: raw.homepage,
repository: raw.repository,
permissions: raw.permissions ?? raw.summary.permissionSummary ?? [],
safetyNotes: raw.safetyNotes,
}
: raw;
setDetail(plugin);
setSelectedVersion(plugin.versions?.[0]?.version || plugin.latestVersion || "");
} catch (e) {
setDetail(null);
showToast(e instanceof Error ? e.message : String(e), { variant: "error" });
} finally {
setDetailLoading(false);
}
};
const closeDetail = () => {
// Escape leaves the opener focused; match pointer dismissal without
// removing the focus indicator used by subsequent keyboard navigation.
const focused = document.activeElement;
if (focused instanceof HTMLElement && focused.matches(".plugins-card-hit")) {
focused.blur();
}
// The review drawer only ever covers this sheet, so it leaves with it.
setReviewsId(null);
setSelectedId(null);
setDetail(null);
setSelectedVersion("");
};
const openReviews = (id: string) => setReviewsId(id);
const closeReviews = () => setReviewsId(null);
// Every scope control offers the same folder list, so it is fetched once here
// and handed down rather than re-queried per row.
useEffect(() => {
void api
.listProjects()
.then((res) => setProjects(res.projects ?? []))
.catch(() => setProjects([]));
}, []);
// Refresh installed-plugin update metadata whenever this surface opens. The
// host keeps the last valid catalog for offline use, so a failed check is
// intentionally silent and never hides the installed list.
useEffect(() => {
void (async () => {
try {
await api.marketCheckUpdates(false);
await refreshPlugins();
} catch {
// Marketplace availability must not block local plugin management.
}
})();
}, [refreshPlugins]);
// The marketplace page (query, category, sort, page, locale) is loaded by
// useMarketBrowse: typing is debounced, every other control asks at once.
// Escape closes the detail sheet, but only while it owns the top layer: the
// permission dialog, the install dialog, and the review drawer in front of
// it handle their own dismissal.
useEffect(() => {
if (!selectedId || pendingInstall || installDialogOpen || reviewsId) return;
const onKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") closeDetail();
};
document.addEventListener("keydown", onKeyDown);
return () => document.removeEventListener("keydown", onKeyDown);
}, [selectedId, pendingInstall, installDialogOpen, reviewsId]);
useEffect(() => {
if (!pendingInstall) return;
const onKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") setPendingInstall(null);
};
document.addEventListener("keydown", onKeyDown);
return () => document.removeEventListener("keydown", onKeyDown);
}, [pendingInstall]);
// An install reports itself while it runs: the phase, the mirror it is trying
// and the bytes that have arrived. Reports for another plugin are ignored, and
// the request's own answer still decides how the install ends.
useEffect(() => {
return api.onPluginInstallProgress((event) => {
const job = installJobRef.current;
if (!job || job.request.id !== event.pluginId) return;
// A cancellation is the user's own action, not a failure to report: the
// dialog leaves quietly instead of showing it as an error.
if (event.error && isInstallCancelled(event.error)) {
closeInstallDialog();
return;
}
const sample = nextInstallSample(installSampleRef.current, event, Date.now());
installSampleRef.current = sample;
const next = withInstallProgress(job, event, sample);
installJobRef.current = next;
setInstallJob(next);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// Service state changes arrive as pluginChanged events, so the list stays
// truthful while the supervisor restarts a crashed worker.
useEffect(() => {
const refresh = () => {
void api
.listPluginServices()
.then(setServices)
.catch(() => setServices([]));
};
refresh();
return api.onPluginChanged(refresh);
}, []);
/**
* Public rating summaries are one batched read for every id either tab can
* show, debounced so a marketplace search that also refreshes the installed
* list still asks once. The installed tab therefore reads the same map
* without waiting for a marketplace refresh, and a centre that predates the
* review API fails this read alone — install and update never depend on it.
*/
useEffect(() => {
const ids = [
...new Set([...plugins.map((plugin) => plugin.id), ...market.map((item) => item.id)]),
];
if (!ids.length) {
setReviewsStatus("idle");
return;
}
let cancelled = false;
const handle = window.setTimeout(() => {
setReviewsStatus("loading");
void (async () => {
try {
const summaries = await api.marketReviewSummaries(ids);
if (cancelled) return;
// A success answers every id just requested, so it replaces the map
// for them: the centre omits ids it no longer serves, and a rating
// that stopped existing must stop rendering (see nextSummaryMap).
setReviewSummaries(nextSummaryMap(ids, summaries));
setReviewsStatus("ready");
} catch {
// Unsupported or unreachable service: an explicit unavailable state,
// never fabricated zeros.
if (cancelled) return;
setReviewsStatus("unavailable");
}
})();
}, 200);
return () => {
cancelled = true;
window.clearTimeout(handle);
};
}, [plugins, market]);
/**
* The review line is decoration, so it has its own read and its own failure
* mode. A center without the ticker route (or a transient failure) leaves the
* map empty and the cards render exactly as they did before this feature.
*/
useEffect(() => {
const ids = [
...new Set([...plugins.map((plugin) => plugin.id), ...market.map((item) => item.id)]),
];
if (!ids.length) {
setReviewTickers({});
return;
}
let cancelled = false;
const handle = window.setTimeout(() => {
void (async () => {
try {
const tickers = await api.marketReviewTickers(ids);
if (cancelled) return;
const next: Record<string, PluginReviewTicker> = {};
for (const id of ids) {
const ticker = tickers[id];
if (ticker && ticker.rows.length > 0) next[id] = ticker;
}
setReviewTickers(next);
} catch {
if (cancelled) return;
setReviewTickers({});
}
})();
}, 200);
return () => {
cancelled = true;
window.clearTimeout(handle);
};
}, [plugins, market]);
const servicesByPlugin = useMemo(() => {
const map = new Map<string, PluginServiceStatus[]>();
for (const status of services) {
const list = map.get(status.pluginId);
if (list) list.push(status);
else map.set(status.pluginId, [status]);
}
return map;
}, [services]);
const installedById = useMemo(() => { const map = new Map<string, PluginSummary>();
for (const plugin of plugins) map.set(plugin.id, plugin);
return map;
}, [plugins]);
const stats = useMemo(() => {
let updates = 0;
for (const plugin of plugins) {
if (plugin.updateAvailable) updates += 1;
}
return { total: plugins.length, updates };
}, [plugins]);
const filteredInstalled = useMemo(
() =>
plugins.filter((plugin) =>
matchesQuery(
installedQuery,
plugin.name,
plugin.id,
plugin.description,
plugin.author,
),
),
[plugins, installedQuery],
);
const installedGroups = useMemo(() => {
const buckets = new Map<GroupId, PluginSummary[]>();
for (const plugin of filteredInstalled) {
const id = groupOf(plugin);
const bucket = buckets.get(id);
if (bucket) bucket.push(plugin);
else buckets.set(id, [plugin]);
}
return GROUP_ORDER.flatMap((id) => {
const rows = buckets.get(id);
if (!rows?.length) return [];
rows.sort((a, b) => a.name.localeCompare(b.name) || a.id.localeCompare(b.id));
return [{ id, rows }];
});
}, [filteredInstalled]);
// The host filters by category, so the page is already what is visible.
const visibleMarket = market;
const activeVersion = useMemo(() => {
if (!detail?.versions?.length) return null;
return (
detail.versions.find((v) => v.version === selectedVersion) ||
detail.versions[0] ||
null
);
}, [detail, selectedVersion]);
const run = async (action: () => Promise<unknown>) => {
try {
await action();
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), { variant: "error" });
}
};
const loadDev = () =>
run(async () => {
const result = await api.loadDevPlugin();
// The folder picker reports the declaration; the review is the grant.
if (result.canceled || !result.review) return;
setPendingReview(result.review);
});
// A pi CLI extension becomes a development plugin holding `agent.extension`
// (spec 07-plugins/16 §3); the confirm is the trust decision. Declared npm
// dependencies are installed (scripts disabled) before the first load.
const importExtension = () =>
run(async () => {
if (!window.confirm(t("plugins.agentExtension.importConfirm"))) return;
const result = await api.importPiExtension();
if (result.canceled) return;
await refreshPlugins();
if (result.dependencies.state === "failed") {
showToast(
t("plugins.importExtensionDepsFailed", { id: result.id, error: result.dependencies.error }),
{ variant: "warning" },
);
return;
}
showToast(t("plugins.importExtensionDone", { id: result.id }), { variant: "success" });
});
const reloadPlugin = (id: string) =>
run(async () => {
if (!rowBusy.begin(id)) return;
setReloadingId(id);
try {
const result = await api.reloadPlugin(id);
// The manifest asks for more than it is running with: ask first, and
// load only after the answer.
if (result.review) {
setPendingReview(result.review);
return;
}
await refreshPlugins();
showToast(t("plugins.reloadDone"), { variant: "success" });
} finally {
rowBusy.end(id);
setReloadingId(null);
}
});
const installPackage = () =>
run(async () => {
await api.installPluginFromPackage();
await refreshPlugins();
showToast(t("plugins.installPackageDone"), { variant: "success" });
});
const createFromTemplate = async (template: TemplateId) => {
setCreating(true);
try {
const created = await api.createPluginFromTemplate(template);
setTemplatePick(null);
// A canceled folder picker is not a failure: leave the page untouched.
if (created.canceled) return;
// Scaffolding writes files; loading waits for the same review a folder
// picked by hand goes through.
if (created.review) {
await activateTemplateProject(created);
setPendingReview(created.review);
return;
}
await finishTemplate(created);
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), { variant: "error" });
} finally {
setCreating(false);
}
};
/** Open the scaffolded folder so the sources land in the workspace. */
const activateTemplateProject = async (created: { dir?: string }) => {
try {
return created.dir ? await activateProject(created.dir) : null;
} catch (e) {
// The plugin exists on disk either way; a failed open is reported on its
// own instead of replacing the result.
return e;
}
};
const finishTemplate = async (created: { dir?: string; name?: string }) => {
await refreshPlugins();
const opened = await activateTemplateProject(created);
showToast(
t(
opened && !(opened instanceof Error)
? "plugins.newFromTemplateOpened"
: "plugins.newFromTemplateDone",
{ name: created.name ?? "" },
),
{ variant: "success" },
);
if (opened instanceof Error) {
showToast(opened.message, { variant: "error" });
}
};
/**
* The answer to a development permission review. The accepted set is what the
* user just saw, and it becomes the ceiling every later hot reload is measured
* against — a folder or a manifest edit can never widen it on its own.
*/
const confirmReview = async () => {
const review = pendingReview;
if (!review) return;
if (!rowBusy.begin(review.id)) return;
try {
if (review.kind === "reload") {
await api.confirmReloadPlugin({
id: review.id,
grantedPermissions: review.permissions,
});
await refreshPlugins();
showToast(t("plugins.reloadDone"), { variant: "success" });
} else {
await api.confirmLoadDevPlugin({
path: review.path,
grantedPermissions: review.permissions,
});
await refreshPlugins();
showToast(t("plugins.loadDevDone"), { variant: "success" });
}
setPendingReview(null);
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), { variant: "error" });
} finally {
rowBusy.end(review.id);
}
};
const checkUpdates = () =>
run(async () => {
const res = await api.marketCheckUpdates();
await refreshPlugins();
const count = res.updates?.length ?? 0;
showToast(t("plugins.updatesFound", { count }), {
variant: count > 0 ? "success" : "info",
});
});
const applyAutoUpdates = () =>
run(async () => {
const res = await api.marketApplyUpdates(true);
await refreshPlugins();
showToast(t("plugins.autoUpdatesApplied", { count: res.results?.length ?? 0 }), {
variant: "success",
});
});
const queueInstall = (input: {
id: string;
name: string;
permissions: readonly string[];
newPermissions?: readonly string[];
version?: string;
}) => {
const newPermissions = orderPermissions(input.newPermissions);
setPendingInstall({
id: input.id,
name: input.name,
version: input.version,
permissions: orderPermissions([...input.permissions, ...newPermissions]),
newPermissions,
});
setAutoUpdate(true);
setRowMenu(null);
};
/**
* Run one install: the request the review approved, or the same request again
* after a failure. The dialog opens before the request is sent, so a slow
* first step is visible, and the request's own answer is what ends it.
*/
const runInstall = async (request: PluginInstallRequest) => {
if (!rowBusy.begin(request.id)) return;
installSampleRef.current = null;
const job = newInstallJob(request);
installJobRef.current = job;
setInstallJob(job);
try {
await api.marketInstall({
id: request.id,
version: request.version,
enable: true,
autoUpdate: request.autoUpdate,
grantedPermissions: request.grantedPermissions,
});
await refreshPlugins();
markMarketInstalled(request.id, request.version);
if (selectedId === request.id) await openDetail(request.id);
showToast(t("plugins.installed", { name: request.name }), {
variant: "success",
});
updateInstallJob((current) => ({
...current,
status: "success",
phase: "enable",
speed: 0,
error: null,
}));
} catch (e) {
const message = e instanceof Error ? e.message : String(e);
if (isInstallCancelled(e)) {
// The user stopped it: the dialog leaves quietly, without an error.
closeInstallDialog();
return;
}
const shown = updateInstallJob((current) => ({
...current,
status: "failed",
speed: 0,
error: message,
}));
// The dialog was dismissed while the install ran, so the failure has to
// be reported where the user is looking.
if (!shown) showToast(message, { variant: "error" });
} finally {
rowBusy.end(request.id);
}
};
const confirmInstall = async () => {
if (!pendingInstall) return;
const request: PluginInstallRequest = {
id: pendingInstall.id,
name: pendingInstall.name,
version: pendingInstall.version,
autoUpdate,
grantedPermissions: pendingInstall.permissions,
};
// The review is answered, so it steps aside for the install it approved.
setPendingInstall(null);
await runInstall(request);
};
/** Ask the host to stop the running download, at most once. */
const cancelInstallDownload = async () => {
const job = installJobRef.current;
if (!job || !canCancelInstall(job)) return;
updateInstallJob((current) => ({ ...current, cancelling: true }));
try {
const res = await api.marketCancelInstall(job.request.id);
// Nothing was running: the install is past its last safe stop, so the
// dialog keeps watching it instead of pretending it stopped.
if (!res.cancelled) updateInstallJob((current) => ({ ...current, cancelling: false }));
} catch (e) {
updateInstallJob((current) => ({ ...current, cancelling: false }));
showToast(e instanceof Error ? e.message : String(e), { variant: "error" });
}
};
const retryInstall = async () => {
const job = installJobRef.current;
if (!job || job.status === "running") return;
await runInstall(job.request);
};
const overflowActions = [
{ key: "checkUpdates", run: checkUpdates },
{ key: "applyAutoUpdates", run: applyAutoUpdates },
{ key: "installPackage", run: installPackage },
{ key: "loadDev", run: loadDev },
{ key: "importExtension", run: importExtension },
{
key: "newFromTemplate",
run: async () => {
setTemplatePick(TEMPLATE_IDS[0]);
},
},
];
const installTarget = activeVersion?.version || detail?.latestVersion;
const installedDetail = detail ? installedById.get(detail.id) : undefined;
const detailPermissions = orderPermissions(
activeVersion?.permissions ?? detail?.permissions ?? [],
);
const detailUpToDate = !!installedDetail && installedDetail.version === installTarget;
// The sheet knows each version's package fields, so it judges the selected
// version rather than the summary's latest one.
const detailPackagePending = detail
? activeVersion
? !versionInstallable(activeVersion)
: detail.installable === false
: false;
// Withdrawn and not-yet-published both block the install, and they are not
// the same news: one is over, the other is pending.
const detailWithdrawn = versionWithdrawn(activeVersion);
return {
t,
locale,
plugins,
refreshPlugins,
showToast,
activateProject,
currentProjectPath,
tab,
setTab,
installedQuery,
setInstalledQuery,
projects,
query,
setQuery,
category,
setCategory,
sort,
setSort,
market,
marketLoading,
marketLoadingMore,
marketLoadMoreFailed,
marketTotal,
marketHasMore,
loadMoreMarket,
browseOrigin,
isPluginBusy: rowBusy.isBusy,
reloadingId,
pendingInstall,
setPendingInstall,
autoUpdate,
setAutoUpdate,
templatePick,
setTemplatePick,
creating,
headerMenu,
setHeaderMenu,
rowMenu,
setRowMenu,
selectedId,
detail,
detailLoading,
servicesByPlugin,
selectedVersion,
setSelectedVersion,
settingsPlugin,
setSettingsPlugin,
reviewSummaries,
reviewTickers,
reviewsStatus,
reviewsId,
openReviews,
closeReviews,
refreshMarket,
openDetail,
closeDetail,
stats,
filteredInstalled,
installedGroups,
installedById,
categories,
visibleMarket,
activeVersion,
run,
loadDev,
importExtension,
reloadPlugin,
installPackage,
createFromTemplate,
checkUpdates,
applyAutoUpdates,
queueInstall,
confirmInstall,
installJob,
cancelInstallDownload,
retryInstall,
closeInstallDialog,
overflowActions,
installTarget,
installedDetail,
detailPermissions,
detailUpToDate,
detailPackagePending,
detailWithdrawn,
pendingReview,
setPendingReview,
confirmReview,
};
}
export type PluginsPageModel = ReturnType<typeof usePluginsPage>;