The plugin API
A plugin is a folder with plugin.json and one main file. Plugins in the user guide has the install and the panel.
The manifest
{
"id": "word-count",
"name": "Word count",
"version": "1.0.0",
"kind": "tcl",
"main": "main.tcl",
"description": "Counts the words and the blocks of the page.",
"grants": ["read", "read-pages", "commands", "notify"]
}
| Key | Value |
|---|---|
id | Lower case letters, digits, ., _, -. It names the plugin in the log |
kind | tcl or wasm |
main | The script, or the .wasm file |
grants | A list of read, read-pages, write, commands, notify, network:HOST, files |
Ask for the fewest grants. The user sees the list before the plugin runs.
The rules of each call
The app checks each call in this order. The first failed check blocks the call, and the log gets the reason.
- The manifest has the grant of the call.
- The plugin is on for the page of the call.
- The page and the block are not private.
A blocked call is an error in a Tcl plugin and the result -1 in a WebAssembly plugin. Each call that passes is in the log with the page and the block.
A run of a plugin has a compute limit of 5,000,000 units. A plugin keeps no data after it is off: each session starts a new runtime.
Tcl plugins
The script runs in a safe interpreter. It has the Tcl language and the command outliner. It has no open, socket, exec, source, or load.
| Command | Grant | Result |
|---|---|---|
outliner page current | read | The name of the open page, or an empty string |
outliner page list | read | The names of the pages that the plugin is on |
outliner page blocks NAME | read-pages | The handles of the blocks of a page, with no private block |
outliner page read NAME | read-pages | The page as Markdown text, with no private block |
outliner page append NAME TEXT | write | Adds a block at the end of the page. Gives its handle |
outliner block get HANDLE | read | A dict: id, content, level, page, marker, priority, properties, refs, uuid |
outliner block set HANDLE TEXT | write | Sets the content of a block |
outliner block insert HANDLE WHERE TEXT | write | A new block before, after, or as the first child. Gives its handle |
outliner block delete HANDLE | write | Deletes a block with its children |
outliner query TEXT | read | The handles of the blocks of a simple query, only from pages that the plugin is on |
outliner command LABEL SCRIPT | commands | Adds a command to the command list. The script runs with the variable block empty |
outliner slash LABEL SCRIPT | commands | Adds a slash command. The script runs with the handle of the block of the editor in block |
outliner on change SCRIPT, outliner on open SCRIPT | none | Runs the script with the page name in page, for a page that the plugin is on |
outliner notify TEXT | notify | Shows a message in the status line |
outliner log TEXT | none | Writes a line to the activity log |
outliner http URL | network:HOST | Blocked in this version |
outliner file read PATH | files | Blocked in this version |
A handle of a block is valid until the block is deleted or the graph is opened again.
outliner command "Count the words of this page" {
set page [outliner page current]
set words 0
foreach id [outliner page blocks $page] {
incr words [llength [regexp -all -inline {\S+} [dict get [outliner block get $id] content]]]
}
outliner notify "$page has $words words."
}
How a call crosses to the app
outliner does not run code of the app. It yields a request, a list of words, out of a coroutine. The app reads the request as data, checks it, writes the log entry, does the work, and resumes the plugin with {ok VALUE} or {err MESSAGE}. The app runs no text that a plugin gives. A runtime in another process or in a virtual machine needs the same two messages, so the rules stay the same when the transport changes.
WebAssembly plugins
A module imports its host functions from the module outliner. It exports memory, and can export init and run.
| Export | When the app calls it |
|---|---|
init() | After the load |
run(id: i32) | For the command with the id of command_add |
on_change(), on_open() | For a page that the plugin is on |
A text goes to the host as an address and a length in the memory of the module. A text from the host goes into a buffer of the module: the function takes the address and the size of the buffer and gives the length of the whole text. A text longer than the buffer is cut. A result of -1 is a blocked call.
| Import | Grant |
|---|---|
log(ptr, len) | none |
notify(ptr, len) -> i32 | notify |
page_current(buf, cap) -> i32 | read |
page_list(buf, cap) -> i32, names on lines | read |
page_read(name, name_len, buf, cap) -> i32 | read-pages |
page_append(name, name_len, text, text_len) -> i32 | write |
block_set(name, name_len, index, text, text_len) -> i32 | read-pages and write |
query(text, text_len, buf, cap) -> i32, lines of page, tab, first line | read |
command_add(label, label_len, id) -> i32 | commands |
http_get(url, url_len, buf, cap) -> i32 | Blocked in this version |
file_read(path, path_len, buf, cap) -> i32 | Blocked in this version |
An import with another name links to a function that gives -1 and writes a blocked entry.
examples/plugins/task-counter/plugin.zig is a module in Zig:
zig build-exe plugin.zig -target wasm32-freestanding -O ReleaseSmall -fno-entry -rdynamic -femit-bin=plugin.wasm
C, Rust, and other languages that make a wasm32 module with these imports work the same way. The engine reads core WebAssembly 2.0.
The activity log
ol::plugin::Record adds an entry: seq, time (milliseconds), plugin, action, target, result, detail. ol::plugin::flushlog writes the new entries to the log file as JSON lines. Each line also has prev, the hash of the line before it, and hash, the SHA-256 of prev, a newline, and the fields of the entry. ol::plugin::verify checks the chain.
The chain shows a changed, removed, or added line. It does not stop a person who writes the whole file again with new hashes: the file has no signature in this version.
Isolation levels
ol::plugin::isolation gives the level of this program: sandbox today. vm and process need features of the ztk program that it does not have yet. The plugin panel shows the level of each plugin.