# Plugins
A plugin adds commands, keys, parts of the screen, pages, dialogs and renderers to `opencode-native`. This guide
covers the model, writing a plugin as a script or in TypeScript, waiting, limits, and how the client finds
and runs plugins. [`plugin-abi.md`](plugin-abi.md) is the reference, and `cargo doc -p opencode-plugin-abi --open`
documents every type. Paths are relative to `packages/cli-rs`.
## The model
A plugin never holds a widget, the TUI or the store. It registers what it adds in a `setup` function, reads through
a read handle, asks for effects through a host handle, and returns trees of nodes that the host draws. That interface
is the *contract*: the crate `crates/plugin-abi` (`opencode-plugin-abi`, ABI 2.0), whose messages have one JSON
Schema, `crates/plugin-abi/schema/v2.json`. A *carrier* runs a plugin against it:
| Carrier | A plugin is | A call crosses as | Host code |
| --- | --- | --- | --- |
| Linked Rust | an `abi::Plugin { id, setup }` compiled into the client | Rust values | `Builtin` in `crates/plugin/src/lib.rs` |
| Script | a directory with `plugin.toml` and `init.ts` | copies of plain values, into and out of a confined JavaScript VM for each activation | `crates/plugin/src/script` |
| Pipe | a TypeScript TUI plugin, unmodified | framed messages to one Bun process for all TS plugins, the *JS host* | `crates/plugin/src/pipe`, and `loader/requests.rs` |
- **Every built-in is a linked plugin on the public contract** (`crates/tui/src/plugins`, `crates/latex`,
`crates/mermaid`), the plugin manager (`/plugins`) and the diff viewer included.
- **The first two run on the UI thread, inside the frame.** No frame waits for the JS host, so a TS plugin's tree
and reads arrive a frame or more late.
- **A TS plugin gets the context the TypeScript client gives it** from a compatibility layer, which turns its JSX
into contract trees. [`F16-v2-compat-layer.md`](../parity/deviations/F16-v2-compat-layer.md) lists what does not
map.
- **The JS host and script plugins are cargo features**, `ts` and `script` (of `opencode-cli`, `opencode-tui` and
`opencode-plugin`), both on by default. `OPENCODE_PLUGIN_JS=0` and `OPENCODE_PLUGIN_SCRIPT=0` turn them off at
run time. `--no-plugins` loads no external plugin at all for one run ([Failures](#failures)). A
plugin whose carrier is missing or off is listed as skipped.
- **Native plugin libraries do not load.** A `plugin.so` (`plugin.dylib`, `plugin.dll`) or a configured library is
listed as skipped. Third-party native code may return as a supervised helper process; it will not run in the
client's process.
`setup` runs once for each activation. The same registrations on each language:
| What | Rust (`Setup`, `Host`) | Script (`setup`, `host`) | TypeScript (`context`) |
| --- | --- | --- | --- |
| A command: palette entry, `/slash`, keybind | `setup.command(Command::new(..), run)` | `setup.command({ name: .. }, run)` | `keymap.layer(() => ({ commands }))` |
| Keys in a scope or a mode | `setup.layer(Layer, handler)`, `Surface::layers` | `setup.layer(layer, handler)` | a layer's `mode`, `target` and each command's `bind` |
| A slot claim | `setup.slot(Placement, render)` | `setup.slot({ path, kind }, render)` | `ui.slot({ append: path, render })` |
| A route, a page of its own | `setup.route(name, render)` | `setup.route(name, render)` | `ui.router.register(page)` |
| A surface: a keyed tree with its own input, as a route, slot, panel, overlay or dialog body | `setup.surface(v2::Surface)` | `setup.surface(spec, view, handler)` | each slot, route, panel and `ui.dialog.show`, through the compat layer |
| A dialog | `host.dialog(Dialog::..)` | `host.dialog({ type: .. })` | `ui.dialog.prompt`, `select`, `confirm`, `alert`, `show` |
| Storage, by declared key | `setup.storage_key`, `read.storage_get`, `host.storage_set` | `setup.storageKey`, `read.storageGet`, `host.storageSet` | `storage.store(key, ..)` |
| Server events | `setup.on_event(types, run)`, `setup.on_deltas` | `setup.onEvent(types, run)` | `data.on(type, ..)` |
| A code fence or tool call renderer | `setup.code_block`, `setup.claim` | `setup.codeBlock` | `markdown.registerCodeBlockRenderer` |
| Mount and disposal hooks | `on_mount`, `on_route_mount`, `on_dispose` | `onMount`, `onRouteMount`, `onDispose` | the cleanup `setup` returns |
Every call into a plugin gets one of two handles:
- **`Read`**: sessions, messages, models, the route, theme colors, the composer, tabs, storage. Reads are pure, and
all reads of one call see one snapshot of the store. A render or a view gets only this.
- **`Host`**: the reads, plus effects such as a toast, a dialog, a navigation, a fetch, a storage write or a focus.
Effects run in order once the outermost call into the plugin returns, so a plugin never re-enters itself, and a
call that panics leaves none.
A render reads and returns data; it never draws. A slot or route render returns `Render::Keep`, `Hide` or
`Show(node)`. A surface's view returns a tree of `v2::Node`, which the host reconciles by `id`: a node whose id and
kind stay keeps its widget and its state (a scroll offset, the focus, half-typed text). `View::memo` returns
`Node::Keep` for a subtree whose input has not changed, so neither side rebuilds it. A render runs again when
something it could show changed, and `Changed` says what. [When claims render](plugin-abi.md#when-claims-render) and
[Surfaces](plugin-abi.md#surfaces) have the rules.
## Writing a plugin
The same plugin twice: a command, in the palette and as `/hello`, that shows a toast, and a `hello` at the end
of the home screen's footer that runs the command when clicked. Each file is in the repository, and a test loads and
runs it. The built-ins are the same contract in Rust (`abi::Plugin { id, setup }`), linked into the client.
### Script
`crates/plugin/examples/script/hello/init.ts`:
```ts
// The quick start: a command in the palette and as `/hello`, and a line in the home footer that runs it.
import { click, define, show, span, text } from "opencode:plugin"
export default define((setup) => {
// A command in the palette and as `/hello`.
setup.command({ name: "hello.greet", title: "Say hello", slash: "hello", palette: true }, (host) => {
host.toast({ variant: "success", message: "Hello from a plugin" })
})
// A line at the end of the home screen's footer; a click runs the command.
setup.slot({ path: "home.footer" }, () =>
show(text([span("hello", { fg: "text.muted" })], { on_click: click("hello.greet") })),
)
})
```
Beside it, `plugin.toml`:
```toml
# The quick start as a script plugin: a command and a footer line that runs it.
id = "example.script-hello"
name = "Hello"
version = "0.1.0"
contract = "2.0"
description = "A command in the palette, and a line in the home footer that runs it"
```
Copy the directory to `~/.config/opencode/plugins/hello/`. There is no build, and the files run on every platform.
[Script plugins](#script-plugins) has the language, modules, types and errors.
### TypeScript
`crates/plugin/examples/pipe/hello/tui.tsx`:
```tsx
/** @jsxImportSource @opentui/solid */
// The quick start as a TypeScript TUI plugin, which the JS host runs.
import { Plugin } from "@opencode/plugin/tui"
export default Plugin.define({
id: "example.ts-hello",
setup(context) {
// A command in the palette and as `/hello`.
context.keymap.layer(() => ({
mode: "global",
commands: [
{
id: "hello.greet",
title: "Say hello",
palette: true,
slash: { name: "hello" },
run: () => context.ui.toast.show({ variant: "success", message: "Hello from a plugin" }),
},
],
}))
// A line at the end of the home screen's footer; a click runs the command.
context.ui.slot({
append: "home.footer",
render: () => (
context.keymap.dispatch("hello.greet")}>
hello
),
})
},
})
```
Copy the file to `~/.config/opencode/plugins/hello/tui.tsx`, or publish it to npm and run
`opencode-native plugin add `. It needs Bun ([TypeScript plugins](#typescript-plugins)).
### Examples
Under `crates/plugin/examples`:
- `script/hello` and `script/counter`: the quick start, and a sidebar counter with storage, a command, a server
event, a fence renderer, a confirm dialog and a module of its own;
- `pipe`: fixtures for the JS host's tests.
The separate `plugins` repository ports ten published TS plugins to script plugins (`/script`), checked
against frozen trace fixtures ([`testing.md`](testing.md#plugin-twins)). Four
show the most: queue-manager (chained dialogs and ordered writes as one task), session-usage (paged typed calls, a
tick), session-manager (a route with two key layers, a search field, a debounce) and goal-plugin (a sidebar read from
tool results, with a tick that runs only while the sidebar shows).
## Script plugins
A script plugin is a set of ES modules written against the host's module `opencode:plugin`. Its language is
psychoscript, a confined JavaScript that also reads TypeScript's type syntax. The carrier is
`crates/plugin/src/script`, and `prelude.ts` there is `opencode:plugin`. A script plugin is not a TypeScript TUI
plugin: it has no JSX, no npm packages and no Bun, and it speaks the contract directly.
### The files
- **The entry** is `init.ts`, else `init.js`; `entry = "lib/main.ts"` in `plugin.toml` names another file of the
plugin.
- **`plugin.toml`** is flat `key = "string"` lines: `id`, which it must have, and `name`, `version`, `description`,
`contract` (a 2.x) and `entry`.
- **What loads** is `plugin.toml` and every `.ts` and `.js` file of the directory, at most 1 MiB in all and 8
directories deep. Declaration files (`.d.ts`) and hidden entries are skipped, and a link out of the directory is
refused. The files are read once for each build; nothing is read from the disk after that.
- **The entry's default export is `define(setup)`.** `setup` runs once for each activation, registers everything, and
cannot wait. A registration after `setup` returned throws.
### The language
- **Strict-mode JS, confined to the host:** classes, generators, `async`/`await`, destructuring, `Map`, `Set`,
`JSON`, `RegExp`, `Date` and `URL`. No file system, network, process, `eval`, `fetch` or `setTimeout` (use
`read`, `host`, `sleep` and `every`).
- **Omitted JS features:** `Intl`, `Proxy`, `Reflect`, `WeakRef`, `Symbol()`, typed arrays other than `Uint8Array`,
private `#x` members, static blocks, and `new` on a plain function. Missing syntax fails at load with its line.
- **Erasable TypeScript only:** in `.ts` files, type annotations, interfaces, aliases, `as`, `satisfies`, generics
and `import type` are stripped on load; `enum`, value `namespace`, parameter properties and decorators fail at
load. `.js` files run as written.
- **Contract names & copy crossing:** camelCase methods (`setup.command`, `read.storageGet`), schema field names
(`on_click`, `session_id`), omitted fields as `undefined`, JSON `null` as `null`. Values cross by copy; functions
throw `TypeError`. `console.{log,info,warn,error,debug}` write one escaped line (≤ 4 KiB) to the client log.
### A larger example
`crates/plugin/examples/script/counter` counts in the sidebar. Storage keeps the count, `/count` and a click raise
it, `/count-reset` asks before it clears it, a finished session shows a toast, and a `shout` fence draws its lines in
capitals. Its `init.ts`:
```ts
// A counter in the sidebar that `/count` and a click raise and storage keeps,
// `/count-reset` asks before it clears it, a toast on a finished session, and a `shout` fence.
import { KEEP, click, column, command, define, show, span, text } from "opencode:plugin"
import type { Read } from "opencode:plugin"
import shout from "./shout.ts"
const COUNT = "count"
function stored(read: Read): number {
const value = read.storageGet(COUNT) as { value?: number } | undefined
return value && typeof value.value === "number" ? value.value : 0
}
export default define((setup) => {
setup.storageKey(COUNT)
setup.command({ name: "counter.count", title: "Count", slash: "count", palette: true }, (host) => {
host.storageSet(COUNT, { value: stored(host) + 1 })
})
setup.command({ name: "counter.reset", title: "Reset the count", slash: "count-reset", palette: true }, (host) => {
host.dialog({
type: "confirm",
title: "Reset the count?",
message: `The count is ${stored(host)}.`,
action: command("counter.reset.answer"),
})
})
// The confirm's answer: "true" for Confirm, "false" for Cancel and escape.
setup.command({ name: "counter.reset.answer" }, (host, answer) => {
if (answer === "true") {
host.storageSet(COUNT, { value: 0 })
host.toast({ variant: "info", title: "Counter", message: "The count is 0" })
}
})
setup.slot({ path: "sidebar.content" }, (read, _, changed) => {
if (!(changed.all || changed.local)) {
return KEEP
}
const count = text([span(`Counted ${stored(read)}`, { fg: "text.muted" })])
return show(column([count], { on_click: click("counter.count") }))
})
setup.onEvent(["session.execution.succeeded"], (host) => {
host.toast({ variant: "info", title: "Counter", message: "A session finished" })
})
setup.codeBlock("shout", shout)
})
```
- **Storage is declared.** `setup.storageKey` names a key, and `read.storageGet` and `host.storageSet` then use it.
A variable does not outlive a reload; storage does.
- **A render is told what changed.** `changed` names what moved since the last render, and `KEEP` leaves what is
drawn. A list of it (`sessions`, `lists`, `messages`, `locations`) is copied into the plugin only when the render
reads it.
- **A dialog's answer is a command.** `host.dialog` returns at once, and the answer runs `counter.reset.answer`.
`await host.confirm(..)` does the same in a straight line ([Waiting](#waiting-tasks-await-and-timers)).
### Modules
The counter's fence renderer is a module of its own, `shout.ts`, which `init.ts` imports:
```ts
// The `shout` fence: each line of the block in capitals, bold. A module of its own, which `init.ts` imports.
import type { Fence, Fenced, Read } from "opencode:plugin"
export default function shout(_read: Read, fence: Fence): Fenced {
const lines = fence.code
.split("\n")
.filter((line) => line.length > 0)
.map((line) => [{ text: line.toUpperCase(), bold: true }])
return { type: "lines", lines }
}
```
- **A specifier names a file of the plugin**, relative to the importer and with its extension: `./shout.ts`,
`../lib/util.js`. `opencode:plugin` is the host's module. Anything else is a load error: a package name, an
absolute path, a path that leaves the plugin, a path without its extension.
- **Modules are linked as the plugin loads**, from the bytes read then. Each runs once, before the modules that
import it, and a cycle is a load error that names its files.
- **`.ts` and `.js` files mix** in one plugin.
### Types
The client embeds both declaration files: `opencode-plugin.d.ts` (generated into
`crates/plugin/src/script/opencode-plugin.d.ts` from `v2.json`, `openapi.json` and `operations.json`) and
`psychoscript.d.ts` (`psychoscript::GLOBALS_DTS`, replacing TypeScript's default libs under `noLib`).
| Command | What it does |
| --- | --- |
| `opencode-native plugin types [dir]` | Prints `opencode-plugin.d.ts`, or writes both `.d.ts` files into `dir` |
| `opencode-native plugin check ` | Validates manifest/sources, runs `tsc` (`PATH` or `OPENCODE_TSC`), and compiles the plugin |
| `bun run check:plugin-types` | In-repo: type-checks every script plugin and verifies `script/script-expect/*.ts` refusals |
`plugin check` runs `tsc` with these options (use the same in a plugin's `tsconfig.json` for editor checks):
```json
{
"compilerOptions": {
"strict": true, "noEmit": true, "noLib": true, "allowJs": true, "checkJs": true,
"target": "es2023", "types": [],
"module": "preserve", "moduleResolution": "bundler", "allowImportingTsExtensions": true,
"erasableSyntaxOnly": true, "verbatimModuleSyntax": true
},
"files": ["psychoscript.d.ts", "opencode-plugin.d.ts", "init.ts"]
}
```
This rejects misspelled contract/API/`client` fields, omitted globals (`setTimeout`, `Intl`, `Proxy`, `WeakRef`),
and non-erasable TypeScript (`enum`). Unsupported JS syntax (`#x`) is caught by the compile step of `plugin check`.
### Errors and locations
Errors name the plugin, file, line, column and plugin frames (host frames in `opencode:plugin` are omitted; messages
are one escaped line ≤ 4 KiB; tracebacks over 20 frames keep the first 10 and last 10):
```text
example.report: lib/util.ts:4:11: TypeError: Cannot read properties of null (reading 'spans').
stack traceback:
lib/util.ts:4:11: in function 'width'
init.ts:11:51: in function ''
```
- **Build errors** (syntax, non-erasable TS, bad import, cycle) fail before `setup`; on reload the prior build stays.
- **Runtime errors** (thrown `Error`, `TypeError`, effect in a render, wait outside a task) fail the plugin and show
in `/plugins` and the log ([Failures](#failures)). Throw `new Error(..)` for a location.
- **Uncaught `HostError` / `AbortError`** in a task ends only that task and logs it; the plugin keeps running.
## Waiting: tasks, `await` and timers
A handler often waits: for a dialog's answer, a server reply or a timer. A *task* lets it wait in a straight line,
as `async` code does in TypeScript, while every stretch of it still runs inside a host call on the UI thread.
- **A task has no thread.** When it waits it returns to the host, and the answer (a reply, a dialog's answer or
close, a timer, a wake) is another call into the plugin that resumes it.
- **A waiting task costs nothing.** Each stretch between waits is part of one host call, timed and bounded as any
call is ([Limits](#limits)). What it asks for takes effect when that call returns.
- **Tasks interleave at their waits,** as `async` calls do.
**Rust**, for the built-ins (`crates/plugin-abi/src/task.rs`). `Setup::tasks()` returns a `Tasks` handle, and
`tasks.spawn(host, f)` starts a task. It is a plain `Future` with real wakers, so other crates' combinators work;
`task::join_all` waits for several. Its `Cx` has the waits (`select`, `prompt`, `confirm`, `alert`, `fetch`,
`foreground`, `sleep`, `blocking`, and the typed `call` and `send`). `Cx::with` lends the current call's host to a
closure; the borrow cannot be held across a wait. This is the module's own doc-tested example:
```rust
fn setup(setup: &mut Setup) -> Result<(), String> {
let tasks = setup.tasks();
setup.command(Command::new("x.clear").palette(), move |host, _| tasks.spawn(host, clear));
Ok(())
}
async fn clear(cx: Cx) {
if cx.confirm(Confirm::ask("Clear?", "Clear the list?")).await != Some(true) {
return;
}
match cx.fetch(Fetch::new("DELETE", "/api/x")).await {
Ok(_) => cx.with(|host| host.toast(Toast::new(Variant::Success, "Cleared"))),
Err(error) => cx.alert(Alert::new("Clear failed", error)).await,
}
}
```
**Script** (`crates/plugin/src/script/prelude.ts`). A handler called with a `host` may be `async`, and is then a
task. A waiting call returns a promise: a request resolves to its value or rejects, and a dialog resolves to its
answer, or to `undefined` when it closed without one. `spawn(f)` starts another task and `all([f, ..])` waits for
several. The same handler, which `the_guides_script_wait_runs_as_written` runs from this file:
```ts
setup.command({ name: "x.clear", palette: true }, async (host) => {
if (!(await host.confirm({ title: "Clear?", message: "Clear the list?" }))) return
try {
await host.fetch({ method: "DELETE", path: "/api/x" })
host.toast({ variant: "success", message: "Cleared" })
} catch (error) {
await host.alert({ title: "Clear failed", message: (error as Error).message })
}
})
```
| A call into a script plugin | May it wait? |
| --- | --- |
| A command, `onEvent`, `onMount`, `onRouteMount` | Yes. It runs inside the call until it first waits on the host, and goes on in the call that brings the answer |
| A layer's or a surface's key handler | Yes. What it returns before its call ends decides its key: `"consume"`, or `"pass"`, which nothing returned also means. One that still waits then has consumed it |
| A slot or route render, a surface's view, a memo's build, a code block renderer, `onDispose` | No. One that returns a promise fails the plugin, and a waiting call in one throws |
| `setup` | No. A `setup` that returns a promise fails the load, and a waiting call in it throws |
- **Only a wait on the host leaves the call:** the VM drains all microtasks before returning.
- **Rejected waits** throw `HostError` (host error text) or `AbortError` (cancelled); letting either escape ends only
that task, while any other uncaught error fails the plugin. Returning `"pass"` from a command declines its key.
**TypeScript** plugins on the JS host await promises as in the TS client (`await context.ui.dialog.confirm(..)`);
each is an effect whose answer comes in a later message.
The SDKs send typed API calls, generated from the server's OpenAPI document, so a wrong path or field fails the
build or the type check: `cx.call(client::session_inbox_list(&input)).await?` in Rust and
`await client.session.inbox.list({ sessionID: id })` in a script.
### What ends a wait
| When | The task |
| --- | --- |
| Its dialog is answered | goes on with the answer |
| Its dialog closes any other way (escape, the backdrop, another dialog) | goes on with `None` (Rust) or `undefined` (script); a confirm answers `false` on escape |
| Its reply comes | goes on with the body or the error; in a script the error is a rejection |
| The mount that owns its request closes first | the request is cancelled, and the task goes on once with the error `cancelled` (`task::CANCELLED`; in a script, an `AbortError`) |
| The plugin is disabled, fails or is replaced, or the client exits | is dropped and never goes on; in Rust its destructors run |
### Ownership and cancel
A fetch, a foreground program and a timer are *requests*, and each belongs to what asked for it:
- **A mount's.** A request from a mount's call (a command of the route it shows, a handler of its input) is the
mount's. Once the mount closes, an unsent request is not sent, a program does not start, and a reply is dropped,
even when the surface opens again under the same key.
- **The activation's.** Every other request (from `setup`, a global command, an event, a wake, a mount's begin and
end notification) is the activation's, and its reply comes unless the plugin is disposed first.
- **A task keeps the owner of the call that started it** (`Host::owner`), before and after every wait, and so does a
task it starts. A task a mount's handler started ends with the mount; an activation's task stays the activation's
even when a mount's call wakes it.
- **In a script, the owner follows the host's waits.** A task that awaits another task's promise, not a wait of its
own on the host, goes on under the owner of the wait that woke it. `all` keeps its caller's owner.
- **Cancel is told once:** as the error above in a task, in `Setup::on_cancelled` for a Rust plugin without tasks,
and as an `AbortError` in a script and in TypeScript.
`plugin_ownership.rs` in `crates/tui/tests/all` holds these rules on all three carriers.
### Timers
A timer is a request (`Host::try_timer`) on the host's clock, owned and cancelled as a fetch is.
| | Rust | Script |
| --- | --- | --- |
| Wait once | `cx.sleep(duration).await`: `Ok(())` or the error | `await sleep(ms)`: resolves, or rejects with the error |
| Tick | `tasks.every(host, period, \|cx\| async { .. })` until the future gives `ControlFlow::Break` | `every(ms, (host) => { .. })` until it returns `false` |
- A delay is at least 20 ms (`MIN_TIMER`). In a script it keeps fractions (`sleep(20.9)`), `Infinity` never wakes,
and a delay that is not a positive number is the minimum.
- At most 32 timers of a plugin wait at once (`MAX_TIMERS`); one more fails its sleep. A sleep dropped early, such
as the loser of a race, releases its timer.
- `every` is a sleep in a loop: a late tick is one tick, and an `every` a mount started ends with the mount.
- A tick that changes nothing draws no frame. A tick that asks for no paint closes its frame 17 ms later
(`IDLE_CLOSE`), so each tick is judged alone against the frame budget and cheap ticks do not add up to a slow
plugin.
- A script has no `setTimeout`: `sleep` and `every` are its timers. TS plugins use the JS host's own `setTimeout`
and `setInterval`.
## Limits
A plugin is code the user chose to run, and a script plugin runs in the client's process, so none of this is a
security boundary. The limits keep one plugin from freezing or starving the client, and name the culprit.
| What is bounded | Linked | Script | Pipe (TS) |
| --- | --- | --- | --- |
| Time in one frame | 1 ms a plugin, 4 ms for all. Over it in more than 12 of its last 120 frames, the plugin is slow, which the user is told once a session | the same | no frame waits |
| One call | an external plugin over 100 ms of CPU time fails when the call returns; a built-in's overrun is logged as a host bug | the VM stops each stretch between two waits at 100 ms of CPU time, and the plugin fails | a JS host that owes an answer and is silent for 1 s (10 s for a load or setup) is restarted; the plugin it was running fails |
| A call that never returns | a built-in's bug: nothing stops it | stopped by the CPU slice | as above |
| Memory | the client's | 32 MiB of live values a VM; 1 MiB of sources | 512 MiB resident for the JS host |
| One message | none: nothing crosses | 320,000 keys and values, 32 MiB of strings, 128 levels of nesting | 256 KiB a control message, 8 MiB a tree; 512 unread messages and 8 unread trees |
| Waiting work | 256 tasks, 32 timers, 4 `blocking` threads, 64 unanswered dialogs | 256 pending waits, 32 timers, 64 dialogs | 256 effects a call, 128 watched reads |
On every carrier, a tree may make the host build at most 20,000 nodes and 3,000 rows, 1 MiB of text a node and 32 MiB
of content a plugin (`crates/plugin/src/limits.rs`); the node past a limit shows a placeholder, and the rest draws. Storage is 128 keys a
plugin and 256 KiB a value. At most 1,024 effects run in one frame for all plugins (`FRAME_EFFECTS`).
The time limits are `Limits` in `crates/plugin/src/contain/mod.rs`, and `OPENCODE_PLUGIN_LIMITS` overrides them, as
in `frame=2ms,overrun=250ms`. A script plugin has nothing to take away: its language has no file
system, network, process or `eval`, and `import` reaches only the plugin's own files and `opencode:plugin`.
### What stops a script plugin
The CPU slice (`used over 100 ms of CPU: stopped by the script budget`, moved by `OPENCODE_PLUGIN_LIMITS=overrun=…`),
the 32 MiB live-value heap cap (`out of memory: an allocation went past its 32 MiB script heap cap`), and host-side
crossing refusals (`the plugin's value is refused: …`) are uncatchable: `try` cannot swallow them, no effects of that
call apply, and the plugin fails.
- **CPU slice:** resets after each wait; stops infinite loops, backtracking regexes, and heavy built-ins (`repeat`,
`replace`, `sort`). The host's outer 100 ms overrun check also counts time spent inside host functions
([`script-gates.md`](script-gates.md)).
- **Crossing & waits:** the VM checks crossing bounds first (throwing `RangeError`), then the host re-checks on
decode. Exceeding 256 pending waits throws; a 65th concurrent dialog resolves to `undefined` at once. Sources
compile off the UI thread.
## Running plugins
### Where plugins are found
In the places the TypeScript client looks, in this order:
1. Each subdirectory, or link to one, of the `plugins` directories: the global one (`~/.config/opencode/plugins`, or
`$OPENCODE_CONFIG_DIR/plugins`), then each `.opencode/plugins` from the project root (the nearest directory with
`.git` or `.hg`) down to the working directory.
2. The TUI parts of the server's active plugins (`GET /api/plugin` with `features.tui`). One whose package or path
does not resolve is left out.
3. Each entry of `plugins` in the global `cli.json` that names no built-in: a string, or
`{ "package": …, "options": { … } }`. A local path is absolute, starts with `./` or `../` (from the config
directory), or is a `file://` URL. `~` is not expanded.
Targets merge by local path; a later entry replaces an earlier one in its place. The target decides the carrier:
- a shared library, or a directory with `plugin.so` (`plugin.dylib`, `plugin.dll`): none, it is listed as skipped;
- a directory with `plugin.toml`: a script plugin (`init.ts` or `init.js`, or `entry` in `plugin.toml`);
- any other directory, and an npm package: the JS host.
An npm spec (`name`, `name@1.2.3`, a range, a tag, `@scope/name`, a git spec) resolves to the package the TypeScript
client installed in its cache. The client installs nothing: a spec missing from the cache is skipped with a hint to
run `opencode-native plugin add `.
**Enabling and disabling.** A `plugins` entry `-` disables plugins by ID; `*` and `prefix.*` select several, and a
later entry without `-` enables them again. The entries after a plugin's own (all of them, for a discovered plugin)
decide whether it starts enabled. `opencode.*` IDs are the built-ins. An external plugin with a built-in's ID replaces
it in its place until it is unloaded. A toggle in `/plugins` lasts until the TUI exits and is not saved.
### TypeScript plugins
The JS host is one Bun process for all TypeScript plugins, started when the first one needs it. Bun is the first of:
1. `OPENCODE_BUN`, an absolute path;
2. the `opencode` binary of a local server (a background service or standalone server, not `--server`), run as the
Bun it embeds (`BUN_BE_BUN=1`), when that Bun is 1.4.0 or newer. The client finds that binary from the server's
process on Linux and Windows;
3. the `opencode` installed beside the client, for a remote server too;
4. where the server's binary is unknown, the first `opencode` on `PATH` that answers as Bun (by real path, so a
version manager's shim is passed over);
5. `bun` on `PATH`.
Only absolute `PATH` entries outside the project count. Without a Bun, TypeScript plugins are skipped and `/plugins`
says why.
Bun runs in a private directory with `--no-env-file`, `--no-install`, its own `bunfig.toml`, and an allowlisted
environment, so nothing of the project loads as it starts and nothing is installed. A plugin that imports a package
its directory does not hold fails to load. The log has each failure with the stack the plugin threw, and its
`console` output as `js host: …` lines. `OPENCODE_JS_HOST_TRACE=` appends every frame the JS host reads and
writes. [The JS host](plugin-abi.md#the-js-host) has the protocol.
### Approval of a project's plugins
A plugin in a project's `.opencode/plugins` runs only once the user approves it, whatever its carrier; the TypeScript
client loads it at once ([`F16-project-plugin-approval.md`](../parity/deviations/F16-project-plugin-approval.md)).
The global directory and `cli.json` need no approval, because a project cannot write them.
- **Until approved, the plugin claims nothing** and does not replace a built-in. The loader hashes its files and runs
nothing. `/plugins` lists it `pending` under its directory's name, and a toast "Plugin needs approval:
" says so once a build.
- **Enter on its row asks "Run project plugin?"** with what would run and the start of its SHA-256. Decline is the
default button and lists it `declined` until the build changes. Escape, Ctrl+C, the header and the backdrop decide
nothing. Enter on a declined row asks again.
- **A decision covers what the user was shown:** the directory and a SHA-256 of every file in it, and a copy of the
approved files is what runs. A changed build is pending again, and a running build of it stops.
- **A decision names the carrier.** A script plugin's is kept as `script:` and the hash, and a TypeScript plugin's
as the hash alone, so approving a directory as a script plugin does not approve it as TypeScript. The question says
which: "This project wants to run a script plugin inside opencode".
- **Decisions are kept in `plugin-approvals.json`** in the client's state directory, written under a lock as a private
file. A store inside the project, at a relative path, or in a directory other users can write is not used, and
decisions then last only as long as the TUI. A store that cannot be read approves nothing. Remove an entry, or the
file, to forget a decision. Forgetting does not undo what approved code already did with the user's permissions.
### Reload
The TUI watches the `plugins` directories, each plugin directory and the directory of each configured path, and
loads a new build 100 ms after the last change (`OPENCODE_FILEWATCHER_DISABLE` turns this off). The new build starts
before the running one stops. If it fails to load or set up, the running version stays and `/plugins` says
`… (previous version still active)`. A plugin keeps its place among the claims across a reload and a toggle.
Script files are read, hashed and compiled off the UI thread (newest save wins); each reload starts a fresh VM
and `setup`, and a build that fails to compile never starts.
### Failures
A panic, a script error, or a thrown exception fails that plugin alone. It is not called again, and none of
the failed call's effects apply. Its commands leave the keymap, its fences fall back to the code view, and its slots
and routes draw empty or the host's fallback. Enabling it again in `/plugins` starts a fresh instance.
`/plugins` lists TUI plugins under "TUI" and the server's under "Server", each active, inactive, skipped, failed,
pending or declined; `ctrl+a` shows the built-ins. Enter enables or disables a plugin, or asks about a pending one. A
failed plugin has an `x`, and Space ("view error") shows its error (Enter, on a target that did not load). A toast
"Plugin failed: " offers "Open plugins", and the home footer counts failed plugins. The log (`opencode.log` in the
directory `opencode-native debug paths` prints) has every failure.
A crash of the process ends the client, and there is no crash guard. The script VM is safe Rust but for its read of
the thread's CPU clock, so a fault in it is a panic, which fails the plugin as any panic does.
If a plugin keeps the client from starting, `opencode-native --no-plugins` starts the TUI with no external plugin
for that run: no script, TypeScript or local plugin, and the built-ins as always. It is not in the TypeScript CLI
([`D01-no-plugins.md`](../parity/deviations/D01-no-plugins.md)). `OPENCODE_PLUGIN_SCRIPT=0` and
`OPENCODE_PLUGIN_JS=0` turn off one carrier for as long as they are set.