Files
RuneStone/docs/plugin-development.md
T
Knotty Wyvern a6d6e96d6d Give plugins SDK APIs for terminals, granted folders, and plugin services
Plugins can now do through the SDK what they did with direct native calls.
The SDK checks plugin shutdown, so these APIs can move to an isolated
plugin context later.

- api.pty opens shell sessions. pty_open accepts an optional channel.
  A channel session sends its output as raw bytes to its own webview
  and sends no pty-data or pty-exit events. Without a channel, the
  events stay, so the released Terminal plugin keeps working. The host
  closes the open sessions of a plugin when the plugin stops.
- api.files gives folder grants. The user selects the folder, the
  host keeps the grants of each plugin in its retained data, and the
  plugin uses an opaque grant ID. The host rejects names with folder
  parts, links, and files over the size limit. Sidecar JSON files
  replace the per-image settings commands.
- api.services lets one plugin offer data to another plugin in the
  same window. The host copies arguments and results.
- The folder methods of api.backgrounds are deprecated. They stay for
  released Background Slideshow versions.
- The plugin reference lists the native calls of each plugin and the
  SDK member that replaces each call.
2026-10-28 01:46:55 -07:00

527 lines
32 KiB
Markdown

# Plugin development reference
[Development](development.md) · [Install and manage plugins](plugins.md) · [Plugin host internals](plugin-host.md)
This reference gives the package contract, the build procedure, and the API for RuneStone plugin authors.
- [Package contract](#package-contract)
- [Build and package a plugin](#build-and-package-a-plugin)
- [Build examples](#build-examples)
- [Versions and releases](#versions-and-releases)
- [Plugin API](#plugin-api)
- [Use the SDK instead of native calls](#use-the-sdk-instead-of-native-calls)
- [Terminal API](#terminal-api)
- [Folder grants](#folder-grants)
- [Plugin services](#plugin-services)
- [Git API](#git-api)
- [Background API](#background-api)
## Package contract
A plugin is one standard `.zip` file with `plugin.json` at the archive root.
All plugins use the same package format and the same install, enable, disable, and removal lifecycle.
The interface types are in `src/lib/plugins/sdk.ts`.
```json
{
"schemaVersion": 1,
"id": "org.example.hello",
"name": "Hello",
"author": "Example author",
"description": "Open a small example view.",
"version": "1.0.0",
"apiVersion": 1,
"entry": "main.js",
"access": "full-app"
}
```
| Field | Rule |
| --- | --- |
| `schemaVersion` | Must be `1`. |
| `id` | Lowercase letters, digits, dots, and hyphens. Starts with a letter. At most 120 characters. Cannot end with a dot. |
| `name`, `author`, `description` | Required text, at most 1,000 bytes each. |
| `version` | Three numeric parts with an optional lowercase letter suffix, such as `1.0.0` or `0.2.28a`. |
| `apiVersion` | Must be `1`. |
| `entry` | Path of a compiled `.js` or `.mjs` module in the package. |
| `access` | Must be `"full-app"`. |
The host rejects any other manifest field.
The `runestone.*` ID prefix follows the same rules as other IDs. An ID does not prove the identity of the publisher.
### Paths and limits
Package paths use ASCII letters, digits, dots, underscores, and hyphens, with `/` between folders.
Each path segment has at most 120 characters, and the full path has at most 180 characters.
The host rejects Windows device names, `.` and `..` segments, and segments that end with a dot.
It also rejects absolute paths, links, junctions, and paths that differ only in letter case.
| Limit | Value |
| --- | --- |
| Entries in the package | 5,000 |
| Expanded data | 100 MiB |
| One file | 25 MiB |
| Compressed ZIP | 100 MiB |
| `plugin.json` | 64 KiB |
| Installed path (application data folder, plugin ID, and package path) | 240 characters |
### Entry module
The entry is one compiled JavaScript module. Bundle all dependencies into it.
The host loads the module from a Blob URL, so relative imports do not resolve. Read packaged files with `api.assets.read`.
The host does not accept raw TypeScript, Svelte source, native libraries, dependencies on other plugins, or automatic updates.
### Package files
```text
org.example.hello-1.0.0.zip
plugin.json
main.js
assets/
README.md
LICENSE
```
The manifest and the entry module are required. Assets, a README, and a license are optional.
In the README, give the RuneStone version that the plugin needs, its purpose, the install steps, and a help contact.
Keep JavaScript readable. Do not minify or obfuscate code in a way that makes inspection difficult.
## Build and package a plugin
Use this procedure for every plugin. Each plugin project defines its own compile step.
1. Compile the plugin into a clean folder that contains `plugin.json`, the entry module, and the assets that it needs.
2. From the RuneStone repository, run `pnpm package:plugin <compiled-folder> [output-folder]`.
3. Install the ZIP in RuneStone through [Settings → Plug-ins](plugins.md#install-a-plugin).
4. Test the plugin features, then test Disable and Uninstall.
From another repository, run `node <RuneStone>/scripts/packagePlugin.mjs <compiled-folder> [output-folder]`.
The packager needs Node.js 24 or later.
The packager (`scripts/packagePlugin.mjs`) does these steps:
- It applies the [package contract](#package-contract) checks to the manifest, paths, entry module, and limits.
- It adds every file in the compiled folder. Keep dependencies, credentials, and unrelated build files out of that folder.
- It writes `<id>-<version>.zip`. Without an output folder, it uses `releases/` in the nearest parent folder that contains a `package.json` file.
- It stops when the output folder is inside the compiled folder.
- It writes the same bytes for the same input. It uses sorted paths, fixed timestamps, and DEFLATE compression.
- It keeps an existing ZIP with identical contents. It stops when an existing ZIP has different contents. It never overwrites a ZIP.
Keep each completed plugin ZIP in the `releases/` folder of its own plugin project, also when the RuneStone repository starts the build.
Use an explicit output folder only for checks and staging.
Other build tools can make the same ZIP layout. Do not put a parent folder above `plugin.json`.
The native installer applies the package checks again before installation.
In user instructions, do not tell users to load JSON, select a build folder, rename a release file, or install dependencies.
## Build examples
Each example is specific to one plugin. Read the package scripts of a plugin before you build it.
For the RuneStone source setup, see [Run from source](development.md#run-from-source).
### Example: Terminal
Source: [Terminal repository](https://git.knottywyvern.com/kwyvern/RuneStone_Terminal), its `package.json`, and its `scripts/build.mjs`.
| Item | Value |
| --- | --- |
| Prerequisites | Node.js 24 and pnpm 10. Run `pnpm install --frozen-lockfile` in the Terminal folder. |
| Working folder | The Terminal repository root |
| Command | `pnpm build` |
| Output | `releases/runestone.terminal-<version>.zip` in the Terminal folder |
The build compiles the Svelte interface and its dependencies into `dist/` (`main.js`, `style.css`, `plugin.json`, and `README.md`). Then it makes the ZIP.
The build stops when the versions in `package.json` and `plugin.json` are different.
To compile into `dist/` without a ZIP, run `node scripts/build.mjs --no-package`.
RuneStone supplies the native terminal sessions. The plugin supplies the interface, shortcuts, and session controls.
For use of the terminal, see the [Terminal guide](guides/terminal.md).
### Example: Workspace overview
Source: `fixtures/plugins/workspace-overview/` in this repository. It contains `plugin.json` and a `main.js` that needs no compile step.
| Item | Value |
| --- | --- |
| Prerequisites | Node.js 24 and the RuneStone source setup |
| Working folder | The RuneStone repository root |
| Command | `pnpm package:plugin fixtures/plugins/workspace-overview <output-folder>` |
| Output | `<output-folder>/org.runestone-example.workspace-overview-1.0.0.zip` |
Use an output folder outside the repository. Without one, the ZIP goes into the RuneStone `releases/` folder, which is for installers.
After you install and enable the ZIP, open **Plug-in views → Workspace overview**, or run **Commands → Show workspace overview**.
## Versions and releases
For version numbers and hotfix suffixes, see [Versions](releases.md#versions).
To publish a plugin ZIP, see [Publish a plugin release](releases.md#publish-a-plugin-release).
For the release format that Gitea-link installation accepts, see [Gitea downloads](plugin-host.md#gitea-downloads).
## Plugin API
The entry module exports `activate(api)`. Activation can return a cleanup function, or a promise for one.
```js
export function activate(api) {
api.registerView({
id: "hello",
title: "Hello",
mount(container) {
const text = document.createElement("p");
text.textContent = "Hello from a RuneStone plugin.";
container.append(text);
return () => text.remove();
}
});
api.registerCommand({
id: "show-hello",
title: "Show hello",
run: () => api.openView("hello")
});
}
```
### Compatibility
Members marked optional can be absent on older RuneStone hosts.
Before you call an optional member, make sure that it exists. If the plugin cannot work without it, show an error that tells the user to update RuneStone.
```js
if (!api.workspace.readBytes) throw new Error("Update RuneStone to use this plugin.");
```
Older hosts ignore unknown fields in objects that you pass to the host, such as a view `icon` or `launch` handler.
### Activation and cleanup
- The host limits activation, each view mount, and each cleanup callback to five seconds. A timeout reports an error.
After a cleanup timeout or failure, the user cannot enable or remove the plugin until RuneStone restarts.
- Plugins activate in every RuneStone window except browser windows.
- `api.signal` aborts when shutdown starts. Use it to stop pending work.
- After shutdown starts, every API call throws an error. Ignore results that arrive after shutdown.
- On disable, the host removes contributions, commands, views, and background frames first. Then it runs view cleanups and `onDispose` callbacks in reverse order.
- Remove timers, listeners, and other resources in cleanup. Closing a view and disabling the plugin share the same cleanup promise.
- Plugins have full application access. The SDK does not isolate plugin JavaScript.
### API reference
| Member | Optional | Contract |
| --- | --- | --- |
| `api.id` | | The installed plugin ID. |
| `api.signal` | | An `AbortSignal` that aborts when shutdown starts. |
| `onDispose(callback)` | | Register synchronous or asynchronous cleanup. |
| `contribute(key, value)` | | Register a host feature. See [Contributions](#contributions). |
| `registerCommand(command)` | | Add a command. See [Commands](#commands). |
| `registerView(view)` | | Add a view. See [Views](#views). |
| `openView(id, options?)` | | Open a view of this plugin. `options` is `{ split: "right" \| "down", relativeTo }`, where `relativeTo` is the ID of an open editor view of the same plugin. |
| `closeView(id)` | yes | Close the editor tabs of a view of this plugin. |
| `replaceView(currentId, nextId)` | yes | Show another editor view in the current tab. The tab keeps its position and pin state. |
| `replaceDocumentWithView(document, nextId)` | yes | Show an editor view in place of a document tab of this plugin. |
| `updateViewTitle(id, title)` | yes | Change a view title. At most 200 characters. |
| `updateViewIcon(id, image)` | yes | Set a tab icon from a base64 `data:image/...` URL (PNG, JPEG, GIF, WebP, ICO, or SVG) of at most 90,000 characters. `null` removes it. |
| `registerTabDrop(receive)` | yes | Accept a plugin tab that the user drags from another window. One receiver for each plugin. |
| `registerDocumentType(type)` | | Recognize workspace files and open them in a plugin editor. See [Document types](#document-types). |
| `openDocument(path)` | | Open a recognized document in its own tab. Rejects when no enabled plugin recognizes the file. |
| `openFile(path)` | yes | Open a workspace file in the normal RuneStone editor. |
| `renameDocument(document, name)` | yes | Rename a recognized document. Returns the new workspace path. |
| `workspace.*` | | Read and write files in the active workspace. See [Workspace files](#workspace-files). |
| `settings.read()` | | Read the saved JSON settings of the plugin. Returns `null` before the first write. |
| `settings.write(value)` | | Save JSON settings, at most 1 MiB. |
| `settings.subscribe(listener)` | yes | Receive a call when the saved settings change in any window. Returns a cleanup function. |
| `assets.read(path)` | | Read a package file as a `Uint8Array`. |
| `ui.showActionMenu(options)` | yes | Open the shared RuneStone action menu. See [Menus](#menus). |
| `completion` | yes | Autocomplete fields and providers. See the [completion API](completion.md). |
| `git` | yes | Git operations for the active workspace. See [Git API](#git-api). |
| `pty` | yes | Terminal sessions. See [Terminal API](#terminal-api). |
| `files` | yes | Folders outside the workspace that the user granted to the plugin. See [Folder grants](#folder-grants). |
| `services` | yes | Data that one plugin offers to other plugins. See [Plugin services](#plugin-services). |
| `backgrounds` | yes | Background image frames. See [Background API](#background-api). |
### Commands
```js
api.registerCommand({ id: "next", title: "Next image", icon: "skip-forward", run: () => next() });
```
| Field | Rule |
| --- | --- |
| `id` | Lowercase letters, digits, dots, and hyphens. Starts with a letter. Unique among the commands of the plugin. |
| `title` | Required text. |
| `run` | Required function. Can return a promise. |
| `category` | `"action"` (default) adds the command to the **Commands** menu. `"open"` adds it to the **Plug-in views** menu. |
| `icon` | Optional. A name from `src/lib/ui/actionIcons.ts`, such as `play`, `pause`, `skip-forward`, or `refresh`, or a view icon name. |
Give each command a recognizable icon.
### Views
| Field | Rule |
| --- | --- |
| `id`, `title` | Same rules as for commands. View IDs are unique among the views of the plugin. |
| `mount(container, context?)` | Required. Draws the view and can return a cleanup function or a promise. `context.surface` is `"view"` or `"settings"`. |
| `placement` | `"dialog"` (default), `"editor"` for a tab in the editor area, or `"sidebar"` for the left pane. |
| `navigation` | `true` adds an icon button. See [Navigation buttons](#navigation-buttons). |
| `icon` | `list`, `kanban`, `puzzle` (default), `globe`, or `git`. |
| `settings` | `true` makes the view a configuration page in Settings. See [Settings pages](#settings-pages). |
| `launcher` | `false` keeps the view out of the **Plug-in views** menu. Use it for dynamic views that already have tabs. |
| `launch()` | Optional. Runs instead of `openView` when the user opens the view from a menu or a navigation button. |
| `pinned` | Initial pin state of an editor tab. |
| `tabActions({ pinned })` | Returns extra `ActionMenuItem` entries for the tab menu. |
| `find()` | Runs when the user starts Find in this tab. |
| `tabDragData({ pinned })` | Returns a string that moves with the tab to another window. The receiver is `registerTabDrop`. |
| `saveTabState()` / `restoreTabState(state)` | Save and restore view state when an editor tab moves to another window. |
| `toolbar` | Accepted. In the current host, it only removes the view from the ribbon. |
Dialog views open in a modal window. Editor views open as tabs beside documents. Document tabs keep their buffers when the user changes views.
Closing an editor tab or disabling the plugin disposes the view.
A `launch()` handler can select a default target and call `api.openView(id)`.
Explicit `api.openView` calls and restored tabs do not call `launch()`.
Example: [Project Management](https://git.knottywyvern.com/kwyvern/RuneStone_PM/src/branch/main/development.md#workspace-default-board) uses `launch()` to open the default board of the workspace.
### Navigation buttons
- A view with `navigation: true` and `placement: "sidebar"` gets a button beside Files, Search, and Bookmarks at the top of the left pane. It opens in the left pane, and the **Plug-in views** menu does not show it.
- A view with `navigation: true` and another placement gets a button in the ribbon at the left edge of the window. The **Plug-in views** menu also shows it, unless `launcher` is `false`.
- Each button shows the view title as a tooltip on hover and keyboard focus.
### Settings pages
Register a view with `settings: true` to add a configuration page under the plugin name in Settings.
The host mounts that view in the Settings window with `context.surface` set to `"settings"`. The view does not appear in the **Plug-in views** menu.
The Settings window loads enabled plugins, so the page is available there.
To react to a change from another window, call `settings.subscribe(listener)`. Then read the new value with `settings.read()`.
The notification contains no data. Ignore reads that finish after shutdown, and do not let an older read replace newer settings.
RuneStone translates only its own interface. It does not translate plugin text or supply a translation API.
### Menus
`ui.showActionMenu({ x, y, items, label?, parent? })` opens the shared action menu at a screen position. It returns `{ close() }`.
Each item is `{ label, icon?, shortcut?, run?, children?, disabled?, checked?, radio?, title? }` or `{ separator: true }`. The type is `ActionMenuItem` in `src/lib/ui/actionMenu.ts`.
The menu closes on selection, outside click, resize, window blur, scroll, and plugin shutdown.
Use this menu instead of a custom menu. See the [interface design reference](ui-design.md).
### Document types
A document type lets a plugin open some workspace files in its own editor.
| Field | Rule |
| --- | --- |
| `id`, `title` | Same rules as for views. Unique among the document types of the plugin. |
| `extensions` | Required list of lowercase extensions without the dot, such as `["md"]`. |
| `identify(path, source)` | Returns `{ id }` with a stable document ID, or `null`. |
| `mount(container, { id, path })` | Draws the editor. Can return a cleanup function. |
| `icon` | `"file"` (default) or `"kanban"`. The file tree shows it. |
| `content` | `"text"` (default) or `"binary"`. |
| `create` | Optional `{ label, defaultName, extension, contents() }`. `extension` must be in `extensions`. `contents()` returns the complete file text. |
- Files that `identify` rejects, or that cause an error, stay available in the text editor.
- Each document opens in its own tab. A second open of the same document selects its tab.
- Closing a tab disposes only that mount. Disabling the plugin disposes all its mounts and removes its file tree icons.
- Creation never replaces an existing file.
- For binary types, `identify` receives an empty `source`. Identify the file from its path, then read bytes in `mount` with `workspace.readBytes`. Show errors about invalid data in the view.
`renameDocument({ id, path }, name)` renames a document that this plugin recognizes.
The host checks the document ID and the owner. It keeps the folder and the extension, rejects name conflicts, and updates open tabs.
Names can contain dots. Hidden names and reserved filename characters are invalid.
### Workspace files
All paths are relative to the active workspace of the window. The host rejects paths outside the workspace.
| Member | Optional | Contract |
| --- | --- | --- |
| `list(path?)` | | List one folder. Returns `{ name, rel_path, kind }` entries, where `kind` is `"file"` or `"folder"`. |
| `listMarkdown()` | yes | Return all `.md`, `.markdown`, and `.mdown` paths in sorted order, in one request. Skips names that start with a dot, and links. |
| `read(path)` | | Read a UTF-8 text file. |
| `readOptional(path)` | yes | Read a text file, or return `null` when the file does not exist. |
| `readMany(paths)` | yes | Read at most 64 text files with a total of at most 16 MiB. Returns `{ path, source }` or `{ path, error }` for each path. |
| `readBytes(path)` | yes | Read a file of at most 64 MiB as a `Uint8Array`. |
| `create(path, contents)` | yes | Create a text file. Fails when the file exists. |
| `write(path, contents, expected)` | | Save the file only if it still contains `expected`. `expected` is required. |
| `subscribe(listener)` | yes | Receive a call when a plugin or the host changes workspace files. Returns a cleanup function. |
In `readMany`, one failed file does not stop the other reads. More than 64 paths make the whole call fail.
`write` and `renameDocument` fail when the file is open in RuneStone with unsaved edits.
### Contributions
`contribute(key, value)` registers a host feature. The types are in `src/lib/plugins/contracts.ts`.
Only one enabled plugin can own each key. The host removes contributions before cleanup starts.
| Key | Purpose | Owner |
| --- | --- | --- |
| `terminal` | Mounts terminal controls in a host element. Returns update, command, and dispose functions. | Terminal plugin |
| `git` | Supplies line blame for the editor. | Git plugin |
| `browser` | The built-in browser. | RuneStone |
A terminal contribution must stop its controls and native sessions before its cleanup finishes.
## Use the SDK instead of native calls
Do not call `invoke`, `listen`, or other `@tauri-apps/api` functions from a plugin. Do not read the private files of other plugins.
Use the `api` members below. They check plugin shutdown, and they will stay available when a later host runs each plugin in its own isolated context.
Make sure that an optional member exists before you use it. Keep the native call only as a fallback for older hosts.
The host still accepts the native calls of released plugins. A later host can remove them.
| Plugin | Current call | Purpose | Use instead |
| --- | --- | --- | --- |
| Terminal | `invoke("pty_open")`, `listen("pty-data")`, `listen("pty-exit")` | Start a shell and receive its output and exit code | `api.pty.open({ ..., onData, onExit })` |
| Terminal | `invoke("pty_write")`, `invoke("pty_resize")`, `invoke("pty_close")` | Send input, change the size, stop the shell | `write`, `resize`, and `close` of the session from `api.pty.open` |
| Terminal | `invoke("pty_profiles")` | List the installed shells | `api.pty.profiles()` |
| Terminal | `invoke("pty_new_window")` | Open a terminal window | `api.pty.openWindow()` |
| Terminal | `invoke("pty_dictation")` | Start Windows voice typing | `api.pty.dictation()` |
| Terminal | `invoke("workspace_read", { rel: ".runestone/tasks.json" })` | Read the task list of the workspace | `api.workspace.read(path)` |
| Background Slideshow | `api.backgrounds.chooseFolder`, `list`, `read`, `defaultFolder`, `readImageSettings`, `writeImageSettings` | Read images and per-image settings from a folder outside the workspace | `api.files` with a folder grant |
| Background Slideshow | `api.workspace.readOptional(".runestone/pm/workspace.json")` | Read the board names of Project Management | A service of Project Management through `api.services` |
The Git, Project Management, Local Autocomplete, PDF Viewer, and CSV Viewer plugins make no native calls.
Their test scripts replace `window.__TAURI_INTERNALS__` with a test double. That is test code and does not go into the package.
## Terminal API
`api.pty` (`version: 1`) starts shells in pseudo terminals. The types are `PtyApi` and `PtySession` in `src/lib/plugins/sdk.ts`.
| Member | Contract |
| --- | --- |
| `profiles()` | Returns the installed shells as `{ id, title, kind }`. |
| `open(options)` | Starts a shell and returns a session. See the options below. |
| `openWindow()` | Opens a terminal window for the workspace of the current window. |
| `dictation()` | Starts Windows voice typing. Focus the terminal first. Fails on other systems. |
| Option | Rule |
| --- | --- |
| `cwd` | Required. The workspace root of the window. The start fails when the workspace changed. |
| `cols`, `rows` | Required. Positive integers. |
| `profile` | A profile `id` from `profiles()`. Without it, the host uses the first installed shell. |
| `directory` | A start folder other than the workspace root. |
| `onData(data)` | Required. Receives the output as a `Uint8Array`. A UTF-8 character can be split between two calls. |
| `onExit(code)` | Receives the exit code, or `null`. |
A session has `id`, `write(text)`, `resize(cols, rows)`, and `close()`. `close()` stops the shell, and more calls return the same promise.
Output goes only to the session that started the shell, as raw bytes.
When the plugin stops, the host stops the output, runs the plugin cleanup, and then closes the sessions that are still open.
## Folder grants
`api.files` (`version: 1`) gives access to folders outside the workspace. The user selects each folder. The plugin cannot supply a path.
A grant is `{ id, path, name }`. `id` is opaque. `path` and `name` are for display only.
| Member | Contract |
| --- | --- |
| `chooseFolder({ title? })` | Opens the folder picker. Returns a grant, or `null` on cancel. The same folder gives the same grant again. |
| `pictureFolder(name)` | Creates `RuneStone <name>` in the Pictures folder of the user if necessary, and returns a grant for it. `name` has letters, digits, spaces, hyphens, and underscores, at most 64 characters. |
| `grants()` | Returns the grants of this plugin. |
| `revoke(grant)` | Removes a grant. Returns `false` when the plugin has no such grant. |
| `list(grant)` | Returns `{ name, size }` for the ordinary files directly in the folder. Skips folders and links. |
| `readBytes(grant, name)` | Reads one file of at most 64 MiB as a `Uint8Array`. |
| `readSidecar(grant, name)` | Reads a sidecar file as text, or returns `null` when it does not exist. |
| `writeSidecar(grant, name, contents, expected)` | Saves a sidecar file only if it still contains `expected`. Use `null` when the file does not exist. |
Each plugin has its own grants. The host keeps them with the retained data of the plugin, so they stay after a restart, an update, or a removal.
A file name is one name without folder parts. The host rejects `..`, links, and files that resolve outside the granted folder.
When the granted folder is removed or becomes a link, its grant stops working. Ask the user to choose the folder again.
A sidecar is a small JSON file that the plugin keeps beside the files of the user. Its name has the form `.runestone-<name>.json`, where `<name>` has letters, digits, hyphens, and underscores.
A sidecar holds valid JSON of at most 1 MiB. The host writes no other files in a granted folder.
Store the grant `id` in the plugin settings. Before you use a saved grant, look for it in `grants()`.
## Plugin services
`api.services` (`version: 1`) lets a plugin offer data to other plugins in the same window. The host passes the calls, so no plugin reads the private files of another plugin.
| Member | Contract |
| --- | --- |
| `provide(name, methods)` | Offers an object of functions under a name. Returns `{ notify(), dispose() }`. One plugin can offer each name once. |
| `get(plugin, name)` | Returns the methods of a service of another plugin, or `null` when it is not available. |
| `subscribe(listener)` | Calls `listener(plugin, name)` when a service starts, stops, or calls `notify()`. Returns a cleanup function. |
A service name uses lowercase letters, digits, dots, and hyphens, and starts with a letter.
Each method call returns a promise. The host copies the arguments and the result with `structuredClone`. Use only data that this function can copy, not functions or DOM nodes.
When the provider stops, the host removes its services and notifies the subscribers. Calls through an old service object then fail. Call `get` again after a notification.
Make the data of a service a documented contract of the provider. Give its name, methods, and result shapes in the documentation of the provider plugin.
## Git API
`api.git` gives Git operations for the active workspace of the window. The host runs the `git` command, one operation at a time.
The types are in `src/lib/git/contracts.ts`. Every method checks for plugin shutdown first.
| Method | Contract |
| --- | --- |
| `status()` | Returns `{ available, repository, branch, changes }`. Each change has `path`, `originalPath?`, `index`, and `worktree`. |
| `diff(path, staged)` | Returns the diff text of one file. |
| `stage(paths)` / `unstage(paths)` | Change the index for 1 to 10,000 paths. |
| `commit(message)` | Commit the staged changes. The message has 1 to 65,536 bytes. |
| `push()` / `pull()` | Push or pull the current branch. |
| `graph()` | Returns `{ commits }` with at most 100 commits from all refs. |
| `commitDetails(commit)` | Returns `{ id, parents, author, date, message, files }` for a full commit ID. |
| `commitDiff(commit, path)` | Returns the diff of one file in a commit, compared to the first parent. |
| `blame(path, line)` | Returns `{ commit, author, date, summary }` for one line, or `null`. `date` is ISO 8601. |
Each graph commit is `{ id, parents, refs, summary, author, date }`. Parent IDs can refer to commits outside the 100 returned commits.
Older hosts can have `git` without `push`, `pull`, `graph`, `commitDetails`, or `commitDiff`. Make sure that a method exists before you call it.
Example: the [Git plugin](https://git.knottywyvern.com/kwyvern/RuneStone_Git/src/branch/main/docs/usage.md#commit-graph) draws the graph from these commits.
For its native service and test setup, see the [Git plugin development guide](https://git.knottywyvern.com/kwyvern/RuneStone_Git/src/branch/main/docs/development.md).
## Background API
`api.backgrounds` (`version: 1`) lets a plugin show image frames behind the RuneStone interface.
Frames stay in the current window and do not change the saved appearance settings.
The folder methods are deprecated. They accept a folder path from the plugin. Use [folder grants](#folder-grants) in new code.
The host keeps them for released versions of Background Slideshow.
| Method | Optional | Contract |
| --- | --- | --- |
| `chooseFolder()` | | Deprecated. Use `files.chooseFolder()`. Open the folder picker. Returns an absolute path, or `null` on cancel. |
| `defaultFolder()` | yes | Deprecated. Use `files.pictureFolder("Backgrounds")`. Create `RuneStone Backgrounds` in the Pictures folder of the user and return its path. |
| `list(folder)` | | Deprecated. Use `files.list(grant)`. Return `{ name }` for PNG, JPEG, GIF, and WebP files directly in the folder. |
| `read(folder, name)` | | Deprecated. Use `files.readBytes(grant, name)`. Read one image as bytes, at most 50 MiB. |
| `readImageSettings(folder)` | yes | Deprecated. Use `files.readSidecar(grant, ".runestone-backgrounds.json")`. Read the per-image settings file text, or `null`. |
| `writeImageSettings(folder, contents, expected)` | yes | Deprecated. Use `files.writeSidecar`. Save the per-image settings file. See [Per-image settings](#per-image-settings). |
| `set(target, frame)` | | Show a frame for a target. `null` removes the frame of this plugin. |
| `get(target)` | | Return the active frame for a target, or `null`. |
| `subscribe(listener)` | | Receive the target name when a frame changes. Returns a cleanup function. |
Folders must be absolute paths without `..` segments.
The host rejects links, files with other extensions, files that are not raster images, and files over the size limit.
File access needs an enabled plugin in the current window. Pending reads fail after the plugin stops.
### Targets and frames
| Target | Use |
| --- | --- |
| `app` | The host shows this frame behind the app. |
| `pm-all` | Read by Project Management for all boards. |
| `pm-board:<board UUID>` | Read by Project Management for one board. |
| Frame field | Rule |
| --- | --- |
| `url` | Required `blob:` URL. |
| `blurredUrl` | Optional `blob:` URL for a blurred layer. Each target scales it separately from the foreground. |
| `overlay` | From -1 to 1. Negative values make the image lighter. Positive values make it darker. |
| `position` | `center`, `top left`, `top`, `top right`, `left`, `right`, `bottom left`, `bottom`, or `bottom right`. |
| `size` | `cover`, `contain`, or `auto`. |
| `previous` | Optional earlier frame with the same fields, without `previous` or `transitionMs`. |
| `transitionMs` | Optional fade time from 0 to 5,000 milliseconds. |
The host fades from `previous` to the new frame, unless the user turns on reduced motion.
The plugin owns all frame URLs. Keep them valid until a new frame replaces them or the plugin stops, then release them.
When more than one plugin sets a frame for a target, the most recent frame is shown. When it is removed, the host shows the earlier frame or the saved background.
Disable removes the frames and subscriptions of the plugin before cleanup.
To share saved configuration across windows, use the plugin settings API.
Example: [Background Slideshow](https://git.knottywyvern.com/kwyvern/RuneStone_BG_Slideshow/src/branch/main/docs/development.md#integration) sets frames from local image folders.
Example: [Project Management](https://git.knottywyvern.com/kwyvern/RuneStone_PM/src/branch/main/development.md#pm-0229-slideshow-backgrounds) reads frames for board backgrounds.
### Per-image settings
The per-image settings file is `.runestone-backgrounds.json` in the image folder.
It must be JSON with `"schema": 1` and an `images` object, at most 1 MiB.
`writeImageSettings` compares the current file to `expected` (`null` when the file does not exist). It fails when the file changed, so external edits are not lost.
`files.writeSidecar` does the same comparison on the same file, but the host checks only that the text is JSON. The plugin checks the schema.
Example: the [Background Slideshow image settings](https://git.knottywyvern.com/kwyvern/RuneStone_BG_Slideshow/src/branch/main/docs/development.md#per-image-settings) give the storage format and the rendering behavior.