# The plugin protocol

> What Skwir and a plugin from outside say to each other: the manifest, the messages and their limits. Experimental, and may change.

Page: https://skwir.org/docs/plugin-protocol · Source: https://github.com/skwir-org/skwir/blob/main/docs/plugins/protocol.md

What Skwir and a plugin from outside say to each other (ADR-045, ADR-046,
ADR-047; version 2: ADR-139, 14). The types are in
`skwir_plugin_api::protocol` (`crates/plugin-api/src/protocol.rs`); a Rust
plugin uses them, a plugin in another language writes the same JSON. How to
write a plugin: [`README.md`](https://skwir.org/docs/writing-a-plugin).

## Versions

The protocol is experimental (ADR-047, point 6). Until it is declared
stable, a new version may change what an older one said, and Skwir then
stops speaking the older one; the release notes say so. Once stable, a
plugin written for an older version runs unchanged and is never asked what
its version did not have.

A manifest states the version its plugin speaks (`api`). One that states
none speaks version 1. Skwir speaks versions `OLDEST` to `VERSION`, 1 to 2
today; it reads what a plugin says by the version its manifest states.

**Version 2** (2026-11-18) lets a plugin speak the reader's language:

- Skwir tells it the languages the window speaks (`initialize`'s
  `languages`; a module asks its `languages` import).
- What it says to a person may be a message of its own catalogs instead of
  a string ([Words](https://skwir.org/docs/plugin-protocol#words-version-2)): in its manifest, in what it answers,
  in its errors. Skwir says each message in the reader's language: the
  window's for what the window shows, English for agents.
- Its catalogs lie beside its manifest, `l10n/<language>.ftl`
  ([Catalogs](https://skwir.org/docs/plugin-protocol#catalogs-version-2)).

A plugin on version 1 is shown as it writes: its strings as written, in
every language. It is told no languages, its catalogs (if any) are not read,
and a message in its manifest is refused. Nothing else differs: the
methods, their parameters and the transports are the same.

## Transports

- **Program**: JSON-RPC 2.0, one message per line on its standard input
  (Skwir's messages) and standard output (the plugin's). A line is at most
  64 MiB, newline included (`MESSAGE_LIMIT`). Standard error goes to Skwir's
  log. Skwir closes standard input when it no longer needs the program.
- **Module**: the WIT world `skwir:plugin@0.1.0`
  (`crates/plugin-api/wit/plugin.wit`). Skwir calls the export
  `call(method, params)` with the method's name and its parameters as JSON
  text, and reads the answer as JSON text, or an error message. The plugin
  calls the imports `read(file, offset, length)`, `log(level, message)` and
  `languages()` (version 2: the languages `initialize` tells a program,
  none on version 1; a module built before it imports none, and runs
  unchanged).

One call at a time per plugin: Skwir sends the next once one is answered.
While answering, the plugin may send `read` requests and `log`
notifications.

## Files

A call about files names each as a `File`:

```json
{ "handle": 1, "name": "note.txt", "path": "/home/a/note.txt",
  "mime": "text/plain", "size": 42 }
```

| Field | Type | |
| --- | --- | --- |
| `handle` | u32 | Reads its bytes (`read`); valid while the call is answered. Each call's handles are its own (never assume `1`): a read of a call given up reaches no file of the next. |
| `name` | string | A name that is not UTF-8 is shown lossily. |
| `path` | string | For display only. |
| `mime` | string, optional | When known. |
| `size` | u64, optional | Bytes, when known. |

## Skwir asks

### `initialize` (program only, first)

```json
{ "api": 2, "capabilities": ["read"], "languages": ["fr", "en"] }
```

`api` is the newest version Skwir speaks; `capabilities` what the plugin
was granted. Answer `{ "api": 2 }` with the version the plugin speaks; a
version Skwir does not speak is refused.

`languages` (version 2; none on version 1) are the languages the window
speaks, most wanted first, English last, as BCP 47 tags (`fr`, `zh-Hans`,
`pt-BR`, `en`): what the plugin writes in a language of its own (text that is
not a message of its catalogs) is written in the first of them it can. Once
they change, Skwir ends the program and starts it again at its next call,
told the new ones. A message ([Words](https://skwir.org/docs/plugin-protocol#words-version-2)) is said by Skwir in
each reader's language whatever they are: an agent reads it in English while
the window reads it in French.

### `field`

```json
{ "key": "word-count.words", "file": { … } }
```

`key` is one of the manifest's field keys. Answer `{ "value": 7 }`: a
string, a number, a boolean or a list of them, or `null` (or no `value`)
when the file says nothing.

### `thumbnail`

```json
{ "file": { … }, "size": 256 }
```

`size` is the side, in pixels, of the square the picture must fit. Answer
`{ "png": "<base64>" }`, or `{ "png": null }` for no thumbnail.

### `inspect`

```json
{ "file": { … }, "offset": 4096, "query": "needle", "source": false, "target": null }
```

| Field | Default | |
| --- | --- | --- |
| `offset` | 0 | Where the page asked starts: a byte, a row, a section, as the plugin numbers them (its `previous` and `next`). |
| `query` | null | What to find from `offset` on; the answer's `position` says where. |
| `source` | false | The file's source asked rather than its presentation. |
| `target` | null | A row's own `target`, asked for. |

Answer `null` (nothing to show), or an inspection:

```json
{ "kind": "hex",
  "title": "Hex · 9.8 KB",
  "text": [],
  "columns": ["Offset", "…", "ASCII"],
  "rows": [ { "cells": ["0000000000000000", "…", "…"],
              "bytes": null, "depth": 0, "branch": false, "target": null } ],
  "previous": null, "next": 4096, "position": 0,
  "notice": "Match at 0x64",
  "details": [ { "label": "Version", "value": "1.2" } ] }
```

| Field | Default | |
| --- | --- | --- |
| `kind` | `text` | `text` (paragraphs), `code` (lines, monospaced), `hex` (offset, bytes, characters), `table` (rows and columns). |
| `title` | required | Words. |
| `text` | `[]` | Paragraphs, or lines of code: the file's, as they are. |
| `columns` | `[]` | Each column's header: words. |
| `rows` | `[]` | Each: `cells` (the file's, as they are); `bytes` (a container member's size, optional); `depth` (0); `branch` (false); `target` (what `target` asks for this row, optional). |
| `previous`, `next` | null | Where the page before and after starts, if any. |
| `position` | 0 | Where what was asked (found) is. |
| `notice` | `""` | A line over the page, words: what a search found, what was left out. |
| `details` | `[]` | `{ label, value }` facts the format knows, for Properties: the label words, the value the file's. |

Words are a string, or on version 2 a message of the plugin's catalogs
([Words](https://skwir.org/docs/plugin-protocol#words-version-2)): `"title": { "id": "title", "args": { "size":
{ "size": 9800 } } }`.

A preview that converts or shows pages is not asked `inspect`.

### `convert`

For a preview declared with `converts = true`:

```json
{ "file": { "handle": 1, "name": "sunset.ppm", … } }
```

Answer `null` (nothing to show for this file), or a file in a format Skwir
shows, which it previews as it would that file (a PDF in the reader, a
picture zoomed and fitted, audio or video in the player, Markdown, a table,
a track on the map):

```json
{ "name": "sunset.png", "data": "<base64>" }
```

- Skwir types it by its name (the extension) and bytes, as any file.
- It must fit one message: about 48 MB once in base64.
- Skwir caches it in the plugin's cache folder under the file's version and
  the plugin's, and asks again only when either changes.
- The file's own views stay its own: View as hex shows the original's bytes,
  not the converted file's.
- A link in it is followed only when the plugin has `network`; otherwise a
  click could carry away what it read.

### `page`

For a preview declared with `pages = true`: the file shown a page at a time,
each page a picture the plugin draws when shown, with its text where it
lies (a document format Skwir does not read, a file of several pictures).
Skwir reads it in its own reader, as a PDF: pages scrolled or turned,
zoomed, text selected, copied and searched, links followed, outline listed.

```json
{ "file": { … }, "page": 2, "side": 2048, "text": true }
```

| Field | Default | |
| --- | --- | --- |
| `page` | required | From 1. |
| `side` | required | Longest side to draw at, in pixels. 0 asks no picture: only the page count, or a page's text (a search reads text without drawing). |
| `text` | false | Asks the page's text and links too. |

Answer `null` (nothing to show for this file), or:

```json
{ "pages": 12,
  "png": "<base64>",
  "width": 612, "height": 792,
  "text": [ { "text": "Chapter one", "x": 0.1, "y": 0.08, "width": 0.4, "height": 0.03,
              "edges": [0.1, 0.13, 0.16, …] } ],
  "links": [ { "x": 0.1, "y": 0.9, "width": 0.2, "height": 0.03, "page": 3 },
             { "x": 0.5, "y": 0.9, "width": 0.3, "height": 0.03, "uri": "https://example.com/" } ],
  "outline": [ { "title": "Chapter one", "depth": 0, "page": 1 } ] }
```

Every field but `pages` may be left out.

| Field | |
| --- | --- |
| `pages` | How many pages the file has. |
| `png` | The page drawn; null past the last page, or when `side` is 0. |
| `width`, `height` | The page's own size in CSS pixels (1/96 inch): what 100% shows. Default: the picture's size. |
| `text` | Runs (a word, a line) in reading order. `x`, `y`, `width`, `height` are the run's box as fractions of the page's width and height, from its top left. Optional `edges`: where each character starts, as a fraction of the page's width, one per character, so a selection follows each glyph; without them each character is an equal part of its run. |
| `links` | A box (as for runs) and where it leads: `page` (from 1) of this file, or `uri`. |
| `outline` | The file's headings: `title`, `depth` (0 at the top, default 0), `page` it starts on (optional). |

- Skwir joins two runs on one line with a space and lines with a line
  break, and searches the text case-insensitively.
- An address link is followed only when the plugin has `network`;
  otherwise the panel says why.
- The outline is the file's, the same for any page: Skwir reads it once,
  with the page count.
- Skwir caches what it is answered in the plugin's cache folder, under the
  file's version and the plugin's: the page count and outline, each page's
  picture by side, and its text and links separately. Nothing is asked
  twice.
- With `converts` too, Skwir asks `convert` first and reads in pages a file
  `convert` answers `null` for. The Netpbm plugin converts a single picture
  to PNG and shows a file of several one picture a page.

### `command`

```json
{ "command": "word-count.count", "args": { }, "entries": [ { "handle": 1, … } ] }
```

`command` is one of the manifest's command names; `args` follows its
schema (default null); `entries` are the files it acts on, those its caller
named, else a pane's selection (default `[]`). Answer one of:

```json
{ "answer": "done" }
{ "answer": "value", "value": { "total": 9 } }
{ "answer": "text", "text": "…", "said": "Copied 3 paths" }
{ "answer": "plan", "title": "Rename by date", "note": null,
  "steps": [ { "op": "rename", "file": 1, "name": "2026-10-07.txt" },
             { "op": "trash", "file": 2 },
             { "op": "move", "file": 3, "to": "/home/a/Archive" },
             { "op": "copy", "file": 4, "to": "/home/a/Archive" },
             { "op": "mkdir", "path": "/home/a/Archive" } ] }
```

| Answer | Meaning |
| --- | --- |
| `done` | Nothing to report. |
| `value` | A value for the caller (a script, an agent): data, never translated. |
| `text` | Text for the clipboard; `said` (words) is what Skwir says of it. |
| `plan` | A plan the caller applies or proposes. Needs the `write` capability. `title` and the optional `note` are words. |

A step's `file` is a handle of the call's files: `rename` gives it `name`
in its folder, `trash` trashes it, `move` and `copy` put it into the folder
`to`. `mkdir` makes the folder `path`. `to` and `path` are absolute.

### `$/cancel` (program only, notification)

```json
{ "id": 7 }
```

Skwir stopped waiting for call 7; its `read` requests are refused from then
on.

## The plugin asks

### `read`

```json
{ "file": 1, "offset": 0, "length": 65536 }
```

Answered `{ "data": "<base64>" }`: at most 4 MiB (`READ_LIMIT`; a longer
`length` is cut to it), fewer at the file's end. Refused for a handle that
is not a file of the call being answered, after `$/cancel`, and for a
plugin without the `read` capability.

### `log` (notification)

```json
{ "level": "warn", "message": "…" }
```

`level` is `error`, `warn`, `info` or `debug`.

## Errors

A JSON-RPC error's `message` (for a module, the error `call` returns) is
what Skwir reports: a preview shows it where the page would be; a command's
caller receives it. Skwir does not read the `code`; its own errors and the
Rust guest's use `-32000`. A request to Skwir for a method other than
`read` is answered with an error.

On version 2 an error may say a message of the plugin's catalogs, said in
the reader's language:

- a program puts it in the error's `data`, the `message` being for logs
  (the Rust guest writes the message's id there):

  ```json
  { "jsonrpc": "2.0", "id": 4, "error": { "code": -32000, "message": "pairs",
    "data": { "id": "pairs" } } }
  ```
- a module, whose error is a string, returns the message's JSON as its
  error: `{"id":"pairs"}`.

Any other error is its text, as written. On version 1 the `message` is what
Skwir reports, whatever `data` holds.

## Words (version 2)

What a plugin says to a person (its title, description, a field's label and
a command's title and description in its manifest; an inspection's title,
headers, notice and labels; a command's `said`, a plan's title and note; an
error) is **words**: a string, shown as written in every language, or a
**message** of its catalogs:

```json
{ "id": "count-said", "args": { "count": 3 } }
```

`args` may be left out. Skwir says the message in each reader's language:
the window's for what it shows, English for agents, MCP and scripts (a
command's title and description included: an agent's tool list stays the
same whatever the window speaks). A message the reader's language lacks is
said in English, whole; one the plugin's English lacks too is said as its
id. Its JSON Schema:

```json
{ "oneOf": [
    { "type": "string" },
    { "type": "object", "required": ["id"],
      "properties": {
        "id": { "type": "string", "pattern": "^[A-Za-z][A-Za-z0-9_-]*$" },
        "args": { "type": "object", "additionalProperties": {
          "oneOf": [
            { "type": "integer" },
            { "type": "string" },
            { "type": "object", "required": ["name"], "properties": { "name": { "type": "string" } } },
            { "type": "object", "required": ["folder"], "properties": { "folder": { "type": "string" } } },
            { "type": "object", "required": ["size"], "properties": { "size": { "type": "integer", "minimum": 0 } } },
            { "type": "object", "required": ["decimal"], "properties": { "decimal": {
              "oneOf": [ { "type": "string", "pattern": "^-?[0-9]+(\\.[0-9]+)?$" }, { "type": "number" } ] } } },
            { "type": "object", "required": ["date"], "properties": { "date": { "type": "integer" } } },
            { "type": "object", "required": ["time"], "properties": { "time": { "type": "integer" } } },
            { "type": "object", "required": ["datetime"], "properties": { "datetime": { "type": "integer" } } } ] } } } } ] }
```

An argument is said as its kind is in the reader's language (ADR-139, 5):

| JSON | Kind | Said |
| --- | --- | --- |
| an integer | a count | Chooses the plural (`[one]`, `*[other]`, an exact `[0]`), written with the reader's digits and groups: "1,234", « 1 234 ». |
| a string | text | As written: a path, a hexadecimal offset, a word a selector chooses by. |
| `{ "name": "…" }` | a name | A file's or a place's: quoted as the reader's language quotes (“a.txt”, « a.txt »), its bidirectional controls shown, never obeyed. |
| `{ "folder": "…" }` | a folder | A folder's name where it is a destination ("to Docs"): unquoted, its bidirectional controls shown, never obeyed. |
| `{ "size": 9800 }` | a size | Bytes, as the reader's size setting writes them: "9.8 kB", "9.6 KiB". |
| `{ "decimal": "1.50" }` | a decimal | A number with digits after its point, as many as written (at most nine; a JSON number keeps its own), with the reader's decimal sign and groups: "1.50", « 1,50 ». It chooses no plural. |
| `{ "date": 1700000000 }` | a date | Seconds since 1970 (UTC), written as the reader's dates are, in their time zone: "14 Nov 2023", « 14 nov. 2023 », or ISO 8601 where the window's dates setting says so. |
| `{ "time": … }`, `{ "datetime": … }` | a time, a date and time | The same, the time of day ("22:13"), or the date and the time. |

Anything else is said as its JSON text. In a manifest, a message is an
inline table: `title = { id = "plugin-title" }`.

## Catalogs (version 2)

A plugin's catalogs are `l10n/<language>.ftl` beside its manifest, one a
language, its English `en.ftl`, written by its author (Skwir's own English
is made from its code; a plugin's is a file it brings). They are Fluent, in
the part Skwir speaks (`docs/invariants/words.md`): text, arguments, and
selectors on a count or on a text argument's word. No terms, no message
referring to another, no functions; Skwir formats the arguments.

```ftl
# Said with the number the command gives for the clipboard.
count-said = { $count ->
        [one] One word in all
       *[other] { $count } words in all
    }
```

- A message's id is a letter, then letters, digits, `_` and `-`.
- A catalog is named by its language's BCP 47 tag (`fr.ftl`, `zh-Hans.ftl`,
  `pt-BR.ftl`); a reader finds the closest one as Skwir's own catalogs are
  found (`zh-TW` finds `zh-Hant`, `fr-CA` finds `fr`), among the languages
  the window speaks.
- Skwir reads them when it finds the plugin, as it reads its manifest, and
  says them while the plugin is on; turned off, they go with its other
  contributions.
- What is left out, and the log says why: a catalog over 256 KiB, more than
  64 languages, one that is not UTF-8 (a byte-order mark is dropped), whose
  placeables or calls nest more than 16 deep, that is no regular file, or
  that Fluent's parser cannot read. A message Skwir cannot read is said in
  English.
