Add the versioned plugin SDK package and its release script

The new package packages/plugin-sdk (@runestone/plugin-sdk) is the one source of the public plugin types.
It contains PluginApi and its members, the manifest type, API_VERSION, and feature checks
(hasFeature, requireFeature, requireFeatures, missingFeatures). It also contains the action icons and
the tooltip helper as DOM functions, because RuneStone has no API for them. Menus stay host-drawn through
api.ui.showActionMenu, so the SDK contains only the menu types. The SDK uses the Apache License 2.0.

The host imports the SDK with relative paths in src/lib/plugins/sdk.ts, src/lib/ui/actionIcons.ts, and
src/lib/ui/tooltip.ts. The completion, Git, background, and contribution contract files export the SDK types.
The menu files keep their types, because the Terminal plugin keeps copies of them. A unit test makes sure
that those types and the SDK types are the same. The menu inventory now scans the SDK source.

pnpm sdk:build compiles the SDK to dist/. pnpm release:sdk --version <v> builds it, then commits and tags
v<v> in a local clone of the public repository. It never pushes, and it prints the push commands.
It stops before a change in these conditions:
- The clone is missing, has changes, or is not on a branch.
- The tag exists, or the version is not greater than the current version.
- The package has no LICENSE file, or the SDK source has changes that are not committed.
A failed attempt restores the version and the clone.

pnpm check type checks the SDK and the example plugin. The example plugin now has JSDoc types from the SDK.
pnpm test covers the feature checks, the host and SDK parity, and the release script with temporary
Git repositories. The plugin development, release, development, and completion guides describe the SDK.
This commit is contained in:
Knotty Wyvern
2026-10-28 02:33:16 -07:00
parent 9591c251e9
commit bb0b8f88b6
37 changed files with 1710 additions and 438 deletions
+130
View File
@@ -0,0 +1,130 @@
import type { FilesApi, PluginApi, PtyApi, ServicesApi, SettingsApi, UiApi, WorkspaceApi } from "./api.ts";
import type { BackgroundApi } from "./backgrounds.ts";
import type { CompletionApi } from "./completion.ts";
import type { GitApi } from "./git.ts";
/**
* The optional members of `PluginApi`, by path, and the type that `requireFeature` returns for each path.
* Older RuneStone hosts can omit these members. Required members, such as `workspace.read`, are not listed.
*/
export interface PluginFeatures {
pty: PtyApi;
files: FilesApi;
services: ServicesApi;
backgrounds: BackgroundApi;
git: GitApi;
"git.push": GitApi["push"];
"git.pull": GitApi["pull"];
"git.graph": GitApi["graph"];
"git.commitDetails": GitApi["commitDetails"];
"git.commitDiff": GitApi["commitDiff"];
completion: CompletionApi;
"completion.transport": NonNullable<CompletionApi["transport"]>;
"ui.showActionMenu": UiApi["showActionMenu"];
registerTabDrop: NonNullable<PluginApi["registerTabDrop"]>;
updateViewTitle: NonNullable<PluginApi["updateViewTitle"]>;
updateViewIcon: NonNullable<PluginApi["updateViewIcon"]>;
openFile: NonNullable<PluginApi["openFile"]>;
renameDocument: NonNullable<PluginApi["renameDocument"]>;
closeView: NonNullable<PluginApi["closeView"]>;
replaceView: NonNullable<PluginApi["replaceView"]>;
replaceDocumentWithView: NonNullable<PluginApi["replaceDocumentWithView"]>;
"workspace.listMarkdown": NonNullable<WorkspaceApi["listMarkdown"]>;
"workspace.readMany": NonNullable<WorkspaceApi["readMany"]>;
"workspace.readBytes": NonNullable<WorkspaceApi["readBytes"]>;
"workspace.readOptional": NonNullable<WorkspaceApi["readOptional"]>;
"workspace.create": NonNullable<WorkspaceApi["create"]>;
"workspace.subscribe": NonNullable<WorkspaceApi["subscribe"]>;
"settings.subscribe": NonNullable<SettingsApi["subscribe"]>;
}
export type PluginFeature = keyof PluginFeatures;
const descriptions: { [K in PluginFeature]: string } = {
pty: "the terminal API",
files: "the folder grant API",
services: "the plugin service API",
backgrounds: "the background API",
git: "the Git API",
"git.push": "Git push",
"git.pull": "Git pull",
"git.graph": "the Git commit graph",
"git.commitDetails": "Git commit details",
"git.commitDiff": "Git commit diffs",
completion: "the completion API",
"completion.transport": "the completion model transport",
"ui.showActionMenu": "the shared action menu",
registerTabDrop: "tab drops from other windows",
updateViewTitle: "view title changes",
updateViewIcon: "view tab icons",
openFile: "opening files in the RuneStone editor",
renameDocument: "document renames",
closeView: "closing views",
replaceView: "view replacement",
replaceDocumentWithView: "replacing a document tab with a view",
"workspace.listMarkdown": "the Markdown file list",
"workspace.readMany": "reading many workspace files in one request",
"workspace.readBytes": "reading workspace files as bytes",
"workspace.readOptional": "reading optional workspace files",
"workspace.create": "creating workspace files",
"workspace.subscribe": "workspace change notifications",
"settings.subscribe": "settings change notifications",
};
/** All the feature paths that `hasFeature` and `requireFeature` accept. */
export const pluginFeatures = Object.freeze(Object.keys(descriptions) as PluginFeature[]);
/** A readable name of a feature, with its API path, for messages to users. */
export function describeFeature(feature: PluginFeature): string {
if (!Object.hasOwn(descriptions, feature)) throw new TypeError(`Unknown plugin feature: ${String(feature)}`);
return `${descriptions[feature]} (api.${feature})`;
}
/** The error that `requireFeature` and `requireFeatures` throw when the host does not have a feature. */
export class MissingFeatureError extends Error {
readonly features: readonly PluginFeature[];
constructor(features: readonly PluginFeature[]) {
const names = features.map(describeFeature);
super(`This plugin needs ${names.length > 1 ? names.slice(0, -1).join(", ") + " and " + names.at(-1) : names[0]}. Update RuneStone to a version that has ${names.length > 1 ? "these features" : "this feature"}.`);
this.name = "MissingFeatureError";
this.features = Object.freeze([...features]);
}
}
function lookup(api: PluginApi, feature: PluginFeature): { parent: unknown; value: unknown } {
describeFeature(feature);
let parent: unknown;
let value: unknown = api;
for (const key of feature.split(".")) {
parent = value;
value = parent !== null && typeof parent === "object" ? (parent as Record<string, unknown>)[key] : undefined;
}
return { parent, value };
}
/** Returns `true` when the host gives this optional member of the plugin API. */
export function hasFeature(api: PluginApi, feature: PluginFeature): boolean {
const { value } = lookup(api, feature);
return typeof value === "function" || (value !== null && typeof value === "object");
}
/** Returns the features in the list that the host does not give, in list order. */
export function missingFeatures(api: PluginApi, features: readonly PluginFeature[]): PluginFeature[] {
return features.filter(feature => !hasFeature(api, feature));
}
/**
* Returns the member at the feature path, or throws a `MissingFeatureError` that tells the user to update RuneStone.
* A returned function stays bound to its parent object.
*/
export function requireFeature<F extends PluginFeature>(api: PluginApi, feature: F): PluginFeatures[F] {
const { parent, value } = lookup(api, feature);
if (typeof value === "function") return value.bind(parent) as PluginFeatures[F];
if (value !== null && typeof value === "object") return value as PluginFeatures[F];
throw new MissingFeatureError([feature]);
}
/** Throws one `MissingFeatureError` that names every missing feature in the list. */
export function requireFeatures(api: PluginApi, features: readonly PluginFeature[]): void {
const missing = missingFeatures(api, features);
if (missing.length) throw new MissingFeatureError(missing);
}