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.
This commit is contained in:
+103
-6
@@ -9,6 +9,10 @@ This reference gives the package contract, the build procedure, and the API for
|
||||
- [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)
|
||||
|
||||
@@ -227,6 +231,9 @@ Older hosts ignore unknown fields in objects that you pass to the host, such as
|
||||
| `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
|
||||
@@ -351,6 +358,92 @@ Only one enabled plugin can own each key. The host removes contributions before
|
||||
|
||||
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.
|
||||
@@ -378,14 +471,17 @@ For its native service and test setup, see the [Git plugin development guide](ht
|
||||
`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()` | | Open the folder picker. Returns an absolute path, or `null` on cancel. |
|
||||
| `defaultFolder()` | yes | Create `RuneStone Backgrounds` in the Pictures folder of the user and return its path. |
|
||||
| `list(folder)` | | Return `{ name }` for PNG, JPEG, GIF, and WebP files directly in the folder. |
|
||||
| `read(folder, name)` | | Read one image as bytes, at most 50 MiB. |
|
||||
| `readImageSettings(folder)` | yes | Read the per-image settings file text, or `null`. |
|
||||
| `writeImageSettings(folder, contents, expected)` | yes | Save the per-image settings file. See [Per-image settings](#per-image-settings). |
|
||||
| `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. |
|
||||
@@ -426,4 +522,5 @@ Example: [Project Management](https://git.knottywyvern.com/kwyvern/RuneStone_PM/
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user