# Lua in bone bone runs a separate LuaJIT state for the core and for each frontend: | File | Runs in | Controls | |---|---|---| | `~/.bone/core.lua` | the core (server) | providers, system prompt, tools, hooks | | `~/.bone/ui.lua` | each frontend (TUI and GUI) | shared options, keys, commands, colors, events | | `~/.bone/tui.lua` | the TUI | terminal-specific behavior and appearance | | `~/.bone/gui.lua` | the GUI (`bone --gui`) | desktop-specific behavior and node views | `$BONE_CONFIG_DIR` replaces `~/.bone`. These states never share Lua values; frontends communicate with the core through the bone protocol. Lua has full trust (no sandbox). Tool calls run without asking; asking first is a customization using `bone.ask` (see below). Numbering, everywhere: indexes into lists start at 1, as in Lua (chat item `index`, `first`/`last`, `from`/`to`); positions start at 0 (screen cells `x`, `y`; prompt `row`, `col` and highlight ranges). Chat buffer numbers also start at 0; they are identifiers, not list indexes. Settings are fields you assign (`bone.ui.statusline = fn`, `bone.ui.layout = {…}`, `bone.ui.spinner = {…}`, `bone.o.name = value`); actions are calls (`bone.chat.add`, `bone.ui.popup`, `bone.notify`). The core loads `runtime/core/api.lua`, `runtime/core/defaults.lua`, plugins, then `core.lua`. Each frontend loads `runtime/ui/api.lua`, its own API and defaults, plugins, then your `ui.lua` and frontend-specific file, followed by trusted project config. `bare` skips the user and project files, not plugins. Saved options apply before user config in the TUI but after it in the GUI (see [Settings](#settings)). The runtime is built into the binary and is read only from there: nothing in the config dir replaces a built-in file. Changes to how an app looks go in a UI library of your own (`~/.bone/libraries//`, see [TUI libraries](#tui-libraries) and [GUI libraries](customizing/customizing-gui.md#libraries)); a file in the library in use replaces the built-in one of the same path. Start such a file from the built-in one and change only what you need: ```lua -- ~/.bone/libraries//lua/bone/ui/layout.lua local M = bone.builtin("bone.ui.layout") -- the built-in module, not this file local setup = M.setup function M.setup(opts) setup(opts) bone.ui.regions.top = function() return { "my header" } end end return M ``` `bone.builtin(name)` runs the built-in version of a module (`"bone.ui.layout"`) or file (`"tui/defaults.lua"`) and returns what it returns, whatever the library holds. A new library's entry file is just `bone.builtin("tui/defaults.lua")` (or `"gui/defaults.lua"`), so it starts as the standard UI and keeps getting its updates. Copies of the built-in files to read are in `~/.bone/docs/builtin/`, rewritten at every start; bone never loads them. `require("x.y")` looks in the library in use (`lua/x/y.lua` or `lua/x/y/init.lua`), then the built-in modules, then `~/.bone/lua/`, then each plugin's `lua/`; so a stray file in `~/.bone/lua/bone/` never replaces a built-in module. `bone.ui_libraries()` (core and frontends) gives `{ tui, gui, mine = { { name, tui, gui } }, dir }`: the library each app uses (`"standard"`, `"bare"` or a name) and the user's own, with whether each has files for that app. ## Extension contract (API v1) `bone.version` is the binary/package version. It is not an extension compatibility check. Every Lua state also reports the version of the Lua contract and the features implemented by that state: ```lua bone.api_version -- integer; currently 1 bone.capabilities["tui.keymaps"] -- true when this feature exists bone.has_capability("tui.panels") -- false for an unknown/unavailable feature local info = bone.api_info() -- { version = 1, side = "tui", os = "linux", capabilities = { ... } } -- os: "linux", "macos" or "windows" (Rust's std::env::consts::OS) ``` `bone.api_version` changes only for a breaking change to an existing API meaning or call shape. New functions, fields, events and capabilities are additive. Plugins should test `bone.has_capability(name)` before using a newer extension point rather than comparing `bone.version`. Core and frontend states are separate; a capability in one state does not make it available in another. The current capability names include `core.config`, `core.tools`, `core.hooks`, `core.providers`, `core.jobs`, `core.http_stream`, `core.ask`, `core.health`, `core.ready`, `core.reload`, `core.hooks.system`, `core.hooks.context`, `core.hooks.errors`, `core.hooks.stream`, `core.hooks.session`, `core.session_write`, `core.queue`, `core.model`, `core.mcp`, `core.rpc`, `plugins.state` (core and frontends), and, in the TUI, `plugins.lifecycle`, `tui.project`, `tui.model`, `tui.rpc`, `tui.keymaps`, `tui.input`, `tui.events`, `tui.local_events`, `tui.commands`, `tui.command_specs`, `tui.options`, `tui.dynamic_options`, `tui.request`, `tui.prompt`, `tui.prompt_edit`, `tui.chat`, `tui.chat_data`, `tui.windows`, `tui.panels`, `tui.regions`, `tui.views`, `tui.pickers`, `tui.themes`, `tui.jobs`, `jobs.streaming` and `tui.session`. All states also report `lua`, `json`, `modules` and `fs`. The GUI reports `jobs.streaming`, `plugins.lifecycle` and the shared `ui.*` names below instead of the `tui.*` ones. Capability names describe the contract, not the internal Rust module layout. ### Frontends: the shared UI API The TUI and the desktop GUI (`bone --gui`) load the same Lua UI API, `runtime/ui/api.lua`, then the GUI its own (`runtime/gui/api.lua`), the defaults, and your config: each plugin's `ui.lua` then `tui.lua` or `gui.lua`, then `~/.bone/ui.lua` and `tui.lua` or `gui.lua`. Put UI code meant for both in `ui.lua`. `bone.api_info().side` is `"tui"` or `"gui"`. An API marked shared has one implementation used by both frontends and is checked by the same scripts in each (`tests/ui/`). Its `ui.*` capability is reported by every frontend that has it; the TUI also keeps its older `tui.*` names. In the GUI, a call to an API it does not have raises an error naming the operation. | Capability | API | Status | |---|---|---| | `ui.options` | `bone.o`: get, set, `define`, `del`, `names`, `info`, `apply`, `list` | shared | | `ui.events` | `bone.on`, `bone.off`; server events and `ready` | shared | | `ui.commands` | `bone.cmd` (running, `create`, `del`, `list`, `find`, `complete`), typed arguments | shared | | `ui.keymaps` | `bone.keymap.set`, `del`, `context`, `list`, `current`, `focus`, `clear`, `del_context`, `raw`, `raw_del` | shared | | `ui.themes` | `bone.hl`, `bone.colorscheme`, `bone.api.colors_name` | shared | | `ui.chat_data` | `bone.chat.items`, `add`, `update`, `remove`, `turns`, `session`, `sessions`, `redraw`, `refresh_in` | shared | | `ui.views` | `bone.ui.views` drawing chat items, `bone.ui.refresh` | shared | | `ui.request` | `bone.request`, `bone.system`, `bone.http`, `bone.defer`, `bone.notify`, `bone.settings` | shared | | `ui.layout` | `bone.ui.layout`, regions, `bone.ui.statusline`/`divider`, `bone.ui.prompt` (its box), `bone.ui.geometry` (character units), `bone.ui.refresh_in` | TUI only: the GUI draws a node tree (`gui.nodes`); its standard library takes `bone.ui.statusline` and `bone.ui.prompt`'s placeholder, prefix, min and max, and its prompt `bone.ui.prompt_highlight` | | `ui.windows` | `bone.ui.popup`/`win`, `update`, `close`, `is_open` | shared | | `ui.panels` | `bone.ui.panel` (open, update, lines, close, info, list, focus, scroll) | TUI only; the GUI keeps their state and the library docks panels drawn as nodes (`node = fn`) | | `ui.input` | keys (windows', panels', raw, keymaps and sequences), `bone.press`, `bone.action`, `bone.ui.actions`, the `mouse` event and what is under it | shared; the GUI's text box edits the prompt itself (raw interceptors and the `key` event still see its keys), and its `mouse` events carry the cell and pixels but no chat item or layout leaf | | `ui.prompt` | `bone.prompt` (cursor, range and selection edits, `info`, `empty`, `images`, `attach`, `paste`, `history_add`), `bone.api.prompt_get`/`prompt_set`; the `submit`, `prompt/changed`, `paste` and `image/attached` events | shared; the GUI's text box keeps its own cursor and selection, reported to Lua | | `ui.jobs` | `bone.job`, `job/*` events, `bone.processes` (`list`, `screen`) | shared | | `ui.plugins` | `bone.plugin` (`list`, `load`, `unload`, `reload`, `on_shutdown`, `state`), trusted project config, `bone.library` | shared | | `ui.session` | `bone.api.session`, `open_session`, `bone.chat.view`/`at`/`scroll` | shared; the GUI scrolls in pixels (a row is 20 px) and `at` finds nothing | | `ui.local_events` | the local `prompt/changed`, `focus/changed`, `resize`, `key`, `paste` and `panel/*` events | shared | | `ui.command_specs` | command aliases, completion callbacks and typed arguments | shared | | `ui.dynamic_options` | `bone.o.define` options with defaults, choices, `on_change` and deletion | shared | | `ui.pickers` | `bone.ui.select` | shared | | `ui.elements` | `view = fn` on windows and panels: elements drawn by each frontend's library (`runtime/lua/bone/elements.lua`) | shared | | `ui.model` | `bone.model` (`list`, `complete`) | shared | | `ui.rpc` | `bone.rpc.call` into core Lua | shared | | `ui.project` | `bone.project` (`info`, `trust`) | shared | | `gui.nodes`, `gui.window`, `gui.notifications`, `gui.dialogs`, `gui.window_nodes` | `bone.gui` (`view`, scroll, window, notify, file dialogs, library), node views, and windows and panels drawn as nodes (`node = fn` in their spec) | GUI only (see `customizing/customizing-gui.md`) | The TUI and the GUI fire the same local event names, including `server/disconnected` and `server/reconnected` for socket clients. Both reload the same way: `ready` receives `{ reload = true, panels }` after a file reload (no data at startup), the outgoing state's shutdown hooks run, and plugin state is saved first. See [GUI reloading](customizing/customizing-gui.md#reloading) for the watched files and limitations. ### Ownership and compatibility The core remains the authority for sessions, turns, providers, tools and protocol state. Frontend Lua can render, handle input, and make protocol requests, but it cannot become a second session authority. A Lua error is reported to the user and does not crash the process. The following v1 entry points are compatibility APIs and will keep working as newer, more structured APIs are added around them: - `bone.keymap.set` / `bone.keymap.del` - callable `bone.cmd`, `bone.cmd.create` / `bone.cmd.del` - `bone.o` option reads and writes - `bone.on` / `bone.off` - `bone.ui.win`, including its `bone.ui.popup` and update/close helpers Their current argument and return behavior is documented below. New APIs must not silently change an existing wrapper's meaning; a wrapper may delegate to the newer implementation internally. ### TUI extension points (API v1) The following table records the compatibility boundary and the additive capabilities currently implemented. Use `bone.has_capability` to check whether the running version provides a capability before using it. | Extension point | Compatibility behavior | Current additive extension | |---|---|---| | Input contexts | `main` is the prompt context and `popup` is the fallback context for a focused Lua window. A picker is a Lua window, not a third keymap context. Unmapped main text enters the prompt. | Named contexts, priority/fallback, key sequences, raw interception and real sequence timeouts are available through `tui.input`. | | Events | `bone.on` receives server notifications by method plus `ready` and `submit`. Server callbacks run after the TUI applies the notification; `submit` can cancel or replace text. | Local prompt/focus/resize/key/paste and panel lifecycle events are available through `tui.local_events`. | | Commands | Built-ins and user commands are slash commands. `bone.cmd.create` accepts a callback receiving `{ args = "..." }` and options such as `{ desc }`; standard slash commands are Lua commands and can be replaced (`/model` among them: `bone.models` does the switching). | Canonical names, aliases, alias-aware completion/execution/deletion, completion callbacks and typed argument metadata are available through `tui.command_specs`. | | Options | `bone.o` exposes typed options; unknown names and wrong types are errors. | Dynamic boolean, integer, number and string options, defaults, metadata, deletion and change callbacks are available through `tui.dynamic_options`. | | Panels | `bone.ui.win` is a Lua-drawn overlay, and regions/views customize the existing chat layout. Both keep working unchanged. | Persistent panels with IDs, docking, sizing, scrolling, focus, render/key callbacks and lifecycle events are available through `tui.panels` (`bone.ui.panel`). | | Prompt and chat | `bone.api.prompt_get`/`prompt_set` read and replace the whole prompt; `bone.chat.items({ kind, last })` lists the on-screen chat's items. | Cursor, range and selection edits (`tui.prompt_edit`, `bone.prompt`) and structured read-only items, turns and sessions with filters (`tui.chat_data`) are available. The TUI stays a prompt, not a file editor. | | Jobs | TUI `bone.system`, `bone.http` and `bone.defer` run one background operation and invoke a callback. Core waits are coroutine-based and turn cancellation stops them. | Streaming jobs with handles, stdout/stderr callbacks (chunks or lines), stdin writes, cancellation of the whole process group, timeouts, exit results, status and `job/*` events are available in the TUI through `jobs.streaming` (`bone.job`). | | Plugin state | Folder plugins load in name order between runtime defaults and the user's config; `bone.plugins` reports names and `.`/`_` folders are disabled. | In both frontends, plugins own what they create; `bone.plugin.on_shutdown`, load/unload/reload (`/plugins`), auto-saved `bone.plugin.state` and trusted project config (`/plugins trust`) are available through `plugins.lifecycle` and `ui.project` (also `tui.project` in the TUI). File-reload lifecycle differs; see above. Core and frontends have `bone.state` and `bone.plugin.current()` (`plugins.state`). | The existing detailed sections describe both the compatibility calls and the new additive APIs. A plugin may use a compatibility API today and opt into each later capability independently. ## Plugins A plugin is a folder in `~/.bone/plugins/`: ```text ~/.bone/plugins/myplugin/ core.lua runs in the core (tools, hooks) ui.lua runs in each frontend (shared keys, commands, events) tui.lua runs in the TUI after ui.lua (terminal views) gui.lua runs in the GUI after ui.lua (node views) lua/ modules for require() colors/ colorschemes ``` The [Bone catalog](https://github.com/vincentm65/bone-catalog) is the source of installable plugins. Use `/catalog` for packages such as `mcp`, `skill`, `usage`, `themes` and `review`; none is installed by default. Every part is optional. Plugins load in name order, after the runtime defaults and before your own config (`core.lua`, or `ui.lua` then `tui.lua` / `gui.lua`), so your config can change anything a plugin set up. Rename a folder to start with `_` or `.` to disable it. `bone.plugins` lists the loaded names. Install published packages with `/catalog`. Your own plugins can be placed directly in `~/.bone/plugins/`. ### Plugin lifecycle and state `bone.plugin.current()` is `{ name, dir, kind }` while a plugin's file runs, and in either frontend while its owned callbacks run, else nil. `bone.state.load(name)` returns the table saved under `name` (empty if none) and `bone.state.save(name, value)` saves one, as JSON in `~/.bone/state//.json`. Names are letters, digits, `_`, `-` and `.`. Core, TUI and GUI have it, with separate `core`, `tui` and `gui` directories (a save replaces the file atomically). In either frontend a plugin owns what it creates while its `ui.lua`, frontend-specific file or one of its callbacks runs: keymaps, user commands, `bone.on` handlers, panels, windows, jobs, dynamic options, raw key interceptors and new keymap contexts. Unloading it removes all of that (a key or command the user has since redefined is left alone), cancels its running jobs without calling back, and forgets the modules it `require`d, so loading it again runs fresh code. Anything else it changed (views, highlights, `bone.ui` fields) is for its shutdown hook to undo. ```lua -- in plugins/todo/tui.lua local st = bone.plugin.state() -- ~/.bone/state/tui/todo.json, saved on unload and quit st.items = st.items or {} bone.plugin.on_shutdown(function() ... end) -- unload, reload and quit ``` - `bone.plugin.list()`: `{ name, dir, kind, loaded, error }` for each plugin seen this session. - `bone.plugin.load(name)` (a folder in `plugins/`, even one added after startup or disabled with `_`), `bone.plugin.unload(name)`, `bone.plugin.reload(name)`; also `/plugins load|unload|reload name`. A plugin cannot unload itself from its own code. - `bone.plugin.state(name)` returns a table that is loaded once and saved when its plugin unloads and when bone quits; `bone.plugin.save_state(name)` saves it now. `name` defaults to the current plugin. - `bone.plugin.carry([key,] fn)` and `bone.plugin.carried([key])`: what a Lua reload hands over in memory (see [Reloading](#reloading-1)). - Events: `plugin/loaded` and `plugin/unloaded` with the plugin's info. Explicit plugin unload/reload, file reloads and frontend quit run shutdown hooks and save plugin state. ### Project config A project can carry `.bone/ui.lua`, then `.bone/tui.lua` or `.bone/gui.lua` for the respective frontend (and modules in `.bone/lua/`). bone looks in the working directory and its parents, but Lua has full trust, so these files run only after you trust that directory: `/plugins trust` runs them now and on later starts there, `/plugins untrust` unloads them and forgets the trust, and `/plugins list` lists the project (as `project`). Trust is shared by both frontends. Project config loads after your own config as a plugin named `project`, so it can be unloaded like any other; `bare` skips it. `bone.project.info()` returns `{ root, file, trusted, loaded }` or nil. The TUI watches trusted project files for changes; the GUI currently does not. ## Shared - `bone.side`: `"core"`, `"tui"` or `"gui"`. `bone.version`, `bone.config_dir`. - `bone.json.encode(value)`, `bone.json.decode(string)`. `bone.json.null` represents JSON null in a Lua table (unlike `nil`, which removes its key). Lua has one empty table where JSON has `[]` and `{}`: an empty table is written as `{}`, unless it is a list that came from JSON (decoded, or handed to a hook or a tool), which stays `[]` also once emptied. bone marks those lists with a metatable that Lua cannot replace; a table built anew does not carry it. - `bone.inspect(value)`: readable dump of any value. - `bone.util`: `split(s, sep)`, `trim(s)`, `startswith(s, prefix)`, `extend(t1, t2, ...)`, `front_matter(text)` → `meta, body` for Markdown files that start with `---` / `key: value` lines / `---`. - `bone.fs.list(dir)` → `{ { name, type = "file" | "dir", size, modified } }`, sorted (`~/` works), or `nil` and an error. `size` is bytes and `modified` is the modification time in Unix seconds; metadata fields may be absent when the filesystem cannot provide them. - `bone.docs.list()` → `{ { name, desc } }`, the customization guides (see `docs/customizing.md`); `bone.docs.read(name)` → the guide's text, or `nil` and an error. `name` is the guide's path, such as `customizing/customizing-tui.md`. bone also writes the guides to `~/.bone/docs/` at startup, and the agent's system prompt points it there. ### Settings `~/.bone/settings.json` holds the choices made in the app, as opposed to `core.lua`, `ui.lua`, `tui.lua` and `gui.lua`, which are yours and which bone never writes. The core owns the file: it checks each change, writes the file whole, and tells every client (`settings/changed`), so connected frontends receive the changes. Delete the file to go back to the defaults; if it is not valid JSON, bone ignores it, says so, and refuses to change it until you fix it. ```json { "provider": "qwen", "providers": { "qwen": { "model": "Qwen3.8-27B-exl3-3.8bpw" }, "work": { "base_url": "https://api.example.com/v1", "model": "big", "api_key_env": "WORK_KEY" } }, "tui": { "tool_detail": "rows", "show_reasoning": true }, "web_search": { "num_results": 5 } } ``` - `provider` picks the default provider, over `core.lua`'s own `bone.config.provider`. Sessions keep their selected provider and model; choosing a provider or explicitly setting the selected provider's model through the TUI also updates the session on screen, not other sessions. Resetting a model field changes the configured default, not an existing session's saved selection. `providers..` changes one field of a provider (`model`, `base_url`, `type`, `api`, `reasoning_effort`, `stream_usage`, `replay_reasoning`, `supports_images`, `compact_limit`, `headers`, `body`, `cache`, `api_key_env`), over what `core.lua` gives it; `providers.` as a whole (`{ base_url, model, … }`) adds a provider that `core.lua` does not have, as `/setup` and `/config` do. So `core.lua` defines providers and their defaults, settings change and add them, and `BONE_*` environment variables override both for one run. Changing any of this reloads the core's configuration at once, and is undone if that fails (an unknown provider is refused). A provider's key is the one saved in `~/.bone/secrets.json` (`secrets/set`; readable only by you, never sent back to clients, never in `settings.json`), else its `api_key_env` variable, else `core.lua`'s. Adding a provider while `settings.json` names none also chooses it. With no provider at all the core still starts, and turns say to run `/setup`; with several and none chosen it asks you to choose. - `tui.