feat(frontend): système de plugins — runtime, menus, layouts custom (#43)

Lots F1-F4 : runtime de chargement/registre plugin, extension des menus
existants, panneau de gestion des plugins, types de layout custom
(sélecteur, fallback, cellule dédiée) branchés sur le port plugin.
Suite npm typecheck/test verte (947/947).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-22 07:37:11 +02:00
parent bb35641715
commit ac726d075e
41 changed files with 3245 additions and 24 deletions

View File

@ -0,0 +1,99 @@
/**
* `PluginRuntimeProvider` — bootstraps the plugin runtime once per app session
* (ticket #43, F1, carnet §1.3): fetches the runtime catalog (already filtered
* to `enabled && !pendingUninstall` by the backend) and loads every bundle via
* {@link loadPlugins}, then exposes the resulting {@link PluginRuntimeRegistry}
* to the rest of the tree (menus in F3, layouts in F4).
*
* Mounted once near the app root, inside `<DIProvider>` (it reads gateways via
* `useGateways()`). Never re-runs within a session — carnet §1.3: disable/
* uninstall only mask contributions in-session; the actual reload happens on
* the next app start (a fresh mount of this provider).
*/
import { createContext, useContext, useEffect, useState, type ReactNode } from "react";
import { loadPlugins, PluginRuntimeRegistry, type PluginLoadFailure } from "@/plugins/runtime";
import { useGateways } from "@/app/di";
export interface PluginRuntimeContextValue {
registry: PluginRuntimeRegistry;
failures: PluginLoadFailure[];
/** True until the initial catalog fetch + bundle loads have settled. */
loading: boolean;
}
/**
* Default value used outside a `<PluginRuntimeProvider>` (e.g. `ProjectsView`
* rendered directly in tests/Storybook without the full app shell): an empty,
* already-settled registry rather than a hard requirement to wrap every call
* site. Plugin contributions are strictly additive, so their absence must
* never be a reason a surface fails to render.
*/
const EMPTY_PLUGIN_RUNTIME: PluginRuntimeContextValue = {
registry: new PluginRuntimeRegistry(),
failures: [],
loading: false,
};
const PluginRuntimeContext = createContext<PluginRuntimeContextValue>(EMPTY_PLUGIN_RUNTIME);
interface PluginRuntimeProviderProps {
children: ReactNode;
/** Test/Storybook escape hatch — skips the gateway fetch and uses this value as-is. */
value?: PluginRuntimeContextValue;
}
export function PluginRuntimeProvider({ children, value: injected }: PluginRuntimeProviderProps) {
const gateways = useGateways();
const [value, setValue] = useState<PluginRuntimeContextValue>(
injected ?? {
registry: new PluginRuntimeRegistry(),
failures: [],
loading: true,
},
);
useEffect(() => {
if (injected) return;
let cancelled = false;
gateways.plugin
.listRuntimeContributions()
.then((catalog) =>
loadPlugins(catalog.plugins, {
project: gateways.project,
git: gateways.git,
terminal: gateways.terminal,
agents: gateways.agent,
system: gateways.system,
}),
)
.then((result) => {
if (cancelled) return;
setValue({ registry: result.registry, failures: result.failures, loading: false });
})
.catch(() => {
// No plugin gateway / catalog fetch failed: run with zero plugins
// rather than blocking the app (full-trust plugins are additive).
if (!cancelled) setValue((prev) => ({ ...prev, loading: false }));
});
return () => {
cancelled = true;
};
// Gateways are a stable singleton for the app session (from `useGateways`);
// re-running on every render would reload every plugin bundle.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<PluginRuntimeContext.Provider value={value}>{children}</PluginRuntimeContext.Provider>
);
}
/**
* Reads the loaded plugin runtime registry. Outside a `<PluginRuntimeProvider>`
* this is the empty registry (see {@link EMPTY_PLUGIN_RUNTIME}), never a throw.
*/
export function usePluginRuntime(): PluginRuntimeContextValue {
return useContext(PluginRuntimeContext);
}