Release RuneStone 0.1.4 with installable plugins and settings

This commit is contained in:
Knotty Wyvern
2026-10-04 15:05:51 -07:00
parent 0268c5c9f0
commit 6b7af0d527
82 changed files with 4150 additions and 212 deletions
+127
View File
@@ -0,0 +1,127 @@
import { writable } from "svelte/store";
import { invoke, isTauri } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
import { workspaceList, workspaceRead, workspaceWrite } from "../workspace/api";
import { ExternalSession, deadline } from "./externalSession";
import type { PluginModule, PluginSnapshot, InstalledPlugin, Dispose } from "./sdk";
export const installedPlugins = writable<PluginSnapshot>({ root: "", plugins: [] });
export const externalErrors = writable<Record<string, string>>({});
export const pluginTools = writable<{ owner: string; id: string; title: string; kind: "command" | "view" }[]>([]);
export const pluginView = writable<{ owner: string; id: string; title: string } | null>(null);
export const pluginServiceError = writable("");
export const restartPlugins = writable<string[]>([]);
const sessions = new Map<string, { hash: string; session: ExternalSession }>();
const failures = new Map<string, string>();
const errors: Record<string, string> = {};
const restarted = new Set<string>();
let queue: Promise<void> = Promise.resolve();
let started = false;
let generation = 0;
function publish() {
pluginTools.set([...sessions].flatMap(([owner, { session }]) => [
...[...session.commands.values()].map(({ id, title }) => ({ owner, id, title, kind: "command" as const })),
...[...session.views.values()].map(({ id, title }) => ({ owner, id, title, kind: "view" as const })),
]));
externalErrors.set({ ...errors });
}
function report(id: string, reason: unknown) { errors[id] = String(reason); publish(); }
async function release(id: string, error?: string) {
await invoke("plugins_release", { id, error: error ?? null });
}
async function stop(id: string) {
const loaded = sessions.get(id);
if (!loaded) return;
sessions.delete(id);
pluginView.update(view => view?.owner === id ? null : view);
restarted.add(id); restartPlugins.set([...restarted]);
let error: string | undefined;
try { await loaded.session.stop(); } catch (reason) { error = String(reason); report(id, reason); }
await release(id, error); publish();
}
function show(owner: string, id: string) {
const view = sessions.get(owner)?.session.views.get(id);
if (view) pluginView.set({ owner, id, title: view.title });
}
async function activate(row: InstalledPlugin) {
const id = row.manifest.id;
let leased = false;
const session = new ExternalSession(id, {
workspace: { list: path => workspaceList(path ?? ""), read: workspaceRead, write: workspaceWrite },
settings: {
read: async () => JSON.parse(await invoke<string>("plugins_data", { id, value: null })),
write: async value => { const json = JSON.stringify(value); if (json === undefined) throw new Error("Plugin settings must be JSON data."); await invoke("plugins_data", { id, value: json }); },
},
assets: { read: async path => Uint8Array.from(atob(await invoke<string>("plugins_asset", { id, path })), char => char.charCodeAt(0)) },
}, publish, view => show(id, view));
try {
const code = await invoke<string>("plugins_load", { id, hash: row.hash }); leased = true;
const url = URL.createObjectURL(new Blob([code], { type: "text/javascript" }));
let module: PluginModule;
try { module = await deadline(import(/* @vite-ignore */ url)); }
catch (reason) { await release(id, String(reason)); leased = false; throw reason; }
finally { URL.revokeObjectURL(url); }
sessions.set(id, { hash: row.hash, session });
await session.start(module);
delete errors[id]; publish();
} catch (reason) {
failures.set(id, row.hash); report(id, reason);
let cleanupError: string | undefined;
try { await session.stop(); } catch (error) { cleanupError = String(error); }
sessions.delete(id);
if (leased) await release(id, cleanupError);
publish();
}
}
export function refreshPlugins(): Promise<void> {
if (!isTauri()) return Promise.resolve();
const version = generation;
const job = async () => {
try {
const snapshot = await invoke<PluginSnapshot | null>("plugins_snapshot");
if (!snapshot || version !== generation) return;
installedPlugins.set(snapshot); pluginServiceError.set("");
for (const id of sessions.keys()) {
const row = snapshot.plugins.find(row => row.manifest.id === id);
if (!row?.enabled || row.hash !== sessions.get(id)?.hash) await stop(id);
}
for (const row of snapshot.plugins) {
const id = row.manifest.id;
if (!row.enabled) { failures.delete(id); continue; }
if (!sessions.has(id) && failures.get(id) !== row.hash && version === generation) await activate(row);
}
} catch (reason) { pluginServiceError.set(String(reason)); }
};
queue = queue.then(job, job);
return queue;
}
export async function setPluginEnabled(id: string, enabled: boolean, hash?: string) {
await invoke("plugins_set_enabled", { id, enabled, hash: hash ?? null });
await refreshPlugins();
}
export async function runPluginCommand(owner: string, id: string) {
try { await sessions.get(owner)?.session.run(id); } catch (error) { report(owner, error); throw error; }
}
export function openPluginView(owner: string, id: string) { show(owner, id); }
export async function mountPluginView(owner: string, id: string, element: HTMLElement): Promise<Dispose> {
const session = sessions.get(owner)?.session;
if (!session) throw new Error("The plugin is disabled.");
return session.mount(id, element);
}
export function startExternalPlugins(): () => void {
if (!isTauri() || started) return () => {};
started = true;
let disposed = false;
let off: (() => void) | undefined;
void listen("plugins-changed", () => { void refreshPlugins(); }).then(unlisten => { if (disposed) unlisten(); else off = unlisten; }).catch(reason => pluginServiceError.set(String(reason)));
void refreshPlugins();
const focus = () => { void refreshPlugins(); };
window.addEventListener("focus", focus);
// Also detects changes made in a file manager while this window stays open.
const timer = setInterval(focus, 5000);
return () => {
disposed = true; started = false; generation++; off?.(); clearInterval(timer); window.removeEventListener("focus", focus);
for (const id of [...sessions.keys()]) void stop(id).catch(reason => report(id, reason));
};
}