Files
RuneStone/packages/plugin-sdk/src/features.ts
T
Knotty Wyvern d3218b2501 Make Git a native feature with an off switch
- Add the native feature runtime in src/lib/features/: the catalog, the switches, the first-start migration, the loader, and the feature sessions.
- Add Settings → Features and the features settings section.
- Add features_legacy. It reads the old plugin packages and changes nothing.
- Move Git from the plugin into src/lib/features/git/, on host internals.
- Remove api.git, the git contribution key, and the SDK Git types.
- Add pnpm check:features. It fails when a feature module loads at start.
- Update the tests, the menu inventory, the docs, and UI-055.
2026-10-29 15:51:21 -07:00

118 lines
5.5 KiB
TypeScript

import type { FilesApi, PluginApi, PtyApi, ServicesApi, SettingsApi, UiApi, WorkspaceApi } from "./api.ts";
import type { BackgroundApi } from "./backgrounds.ts";
import type { CompletionApi } from "./completion.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;
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",
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);
}