RuneStone 1.0.0
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
# Completion plugin API
|
||||
|
||||
[Plugin development](plugin-development.md) · [Plugin API](plugin-development.md#plugin-api)
|
||||
|
||||
`api.completion` (`version: 1`) lets plugins show autocomplete in RuneStone editors and in their own text fields, and supply the suggestions.
|
||||
|
||||
`api.completion` is optional. Older hosts do not have it, and `transport` can be absent. Make sure that a member exists before you call it.
|
||||
The SDK type is `CompletionApi`. See [Install the SDK](plugin-development.md#install-the-sdk).
|
||||
|
||||
| Member | Contract |
|
||||
| --- | --- |
|
||||
| `registerInput(element, options)` | Add autocomplete to an `<input>` or `<textarea>`. Returns a handle. |
|
||||
| `registerSurface(surface)` | Add autocomplete to a custom editor. Returns a handle. |
|
||||
| `registerProvider(provider)` | Supply suggestions. Returns a cleanup function. |
|
||||
| `invalidate()` | Dismiss all visible suggestions. |
|
||||
| `transport(request, signal, onLine)` | Optional. Send a request to a local model server. |
|
||||
|
||||
The host adds the plugin ID as a prefix to field and provider IDs. Each field ID must be unique among the active fields.
|
||||
The host disposes all registrations when the plugin stops.
|
||||
|
||||
## Register a field
|
||||
|
||||
```javascript
|
||||
const completion = api.completion;
|
||||
if (completion?.version === 1) {
|
||||
const field = completion.registerInput(textarea, {
|
||||
id: "draft",
|
||||
kind: "prose",
|
||||
sensitive: false
|
||||
});
|
||||
api.onDispose(() => field.dispose());
|
||||
}
|
||||
```
|
||||
|
||||
`registerInput` options are `id`, `kind` (`"prose"` default, or `"code"`), and `sensitive`.
|
||||
The host shows the suggestion in a box below the field. It treats inputs of types other than `text` and `search` as sensitive.
|
||||
|
||||
| Key | Action |
|
||||
| --- | --- |
|
||||
| Tab | Accept the suggestion. |
|
||||
| Escape | Dismiss the suggestion. |
|
||||
| Ctrl+Alt+Space (Cmd+Alt+Space on macOS) | Request a suggestion now. |
|
||||
| Ctrl+Alt+Right (Cmd+Alt+Right on macOS) | Accept the next word. |
|
||||
|
||||
The RuneStone Markdown editor and code editor are also completion surfaces. They show the suggestion as faded italic text at the caret.
|
||||
|
||||
### Custom editors
|
||||
|
||||
`registerSurface({ id, snapshot, preview, insert })` connects a custom editor.
|
||||
|
||||
- `snapshot()` returns the current state: `documentId`, `revision`, `text`, `caret`, `selectionEnd`, `focused`, `composing`, `sensitive`, `readOnly`, `kind`, and `language`.
|
||||
- `preview(text)` shows a suggestion. `null` removes it.
|
||||
- `insert(text, expected)` inserts the accepted text. Do it as one undoable edit. Return `false` when the editor state no longer matches `expected`.
|
||||
|
||||
The returned handle has `changed()`, `request()`, `accept(word?)`, `dismiss()`, and `dispose()`. Call `changed()` after each edit.
|
||||
|
||||
### When the host asks for a suggestion
|
||||
|
||||
The host asks only when all of these conditions are true:
|
||||
|
||||
- The field has focus, and no selection.
|
||||
- The field is not read-only or sensitive, and no IME composition is in progress.
|
||||
- The text before the caret contains more than spaces.
|
||||
- For prose, the caret is not in a table row, a link target, or an HTML tag.
|
||||
- For prose, the caret is not in inline code, a code fence, or front matter.
|
||||
|
||||
## Register a provider
|
||||
|
||||
```javascript
|
||||
const stop = api.completion.registerProvider({
|
||||
id: "local",
|
||||
delayMs: 200,
|
||||
timeoutMs: 1000,
|
||||
async complete(request, publish) {
|
||||
const text = await suggest(request.prefix, request.suffix, request.signal);
|
||||
return text;
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
Only one provider can be active in a window. A second registration throws an error.
|
||||
|
||||
| Field | Rule |
|
||||
| --- | --- |
|
||||
| `delayMs` | Wait after the last edit. Default 200, from 100 to 1,000 milliseconds. |
|
||||
| `timeoutMs` | Time limit for an automatic request. Default 1,000, from 300 to 5,000 milliseconds. A manual request has 10 seconds. |
|
||||
| `complete(request, publish)` | Returns the final text, or `null`. Call `publish(text)` to show partial text before the end. |
|
||||
|
||||
The request contains `surfaceId`, `documentId`, `revision`, `prefix`, `suffix`, `kind`, `language`, `manual`, and an abort `signal`.
|
||||
`prefix` has at most 3,000 characters before the caret. `suffix` has at most 600 characters after the caret.
|
||||
The host ignores a suggestion longer than 2,000 characters or one with control characters.
|
||||
No text goes into the document until the user accepts it. Then the host makes sure that the document revision and the caret did not change.
|
||||
Call `invalidate()` when the provider settings or its context change.
|
||||
|
||||
## Local transport
|
||||
|
||||
`transport(request, signal, onLine)` sends one request to a model server on the same computer, through the desktop host.
|
||||
It calls `onLine` once for each response line. It is not a general HTTP API.
|
||||
|
||||
| Item | Rule |
|
||||
| --- | --- |
|
||||
| `endpoint` | `http` or `https` on `127.0.0.1`, `[::1]`, or `localhost`, with path `/` or `/v1`. No credentials, query, or fragment. |
|
||||
| `route` | `/v1/models`, `/v1/chat/completions`, `/api/tags`, or `/api/generate`. |
|
||||
| `body` | Optional JSON text, at most 32 KiB. With a body, the host sends POST. Without one, it sends GET. |
|
||||
| `timeoutMs` | Default 5,000, from 100 to 15,000 milliseconds. The connection limit is 2 seconds. |
|
||||
| Response | At most 64 KiB for each line and 256 KiB in total. |
|
||||
| Concurrent requests | At most 16 across all windows. |
|
||||
|
||||
The host does not follow redirects and does not use a system proxy. It sends no credentials.
|
||||
Abort stops the display at once. A blocked network read can continue until the request timeout.
|
||||
|
||||
## Trust
|
||||
|
||||
Plugins keep full application access. This API does not add a permission sandbox.
|
||||
The API does not include context sources, remote model servers, or credential storage.
|
||||
|
||||
For tests, see `tests/unit/plugins/completion.spec.ts`.
|
||||
Reference in New Issue
Block a user