Skip to main content

Crate mouser_mode_api

Crate mouser_mode_api 

Source
Expand description

The public API every Mouser mode plugin is built on (docs/protocol.md).

A mode is its own executable. Mouser starts it once, keeps it running and talks to it over its stdin/stdout with length-prefixed frames (codec), MessagePack by default or JSON with MOUSER_MODE_WIRE=json. The first exchange is a handshake (protocol::Hello / protocol::Welcome) that picks a shared protocol version (protocol::negotiate) and registers the plugin’s event listeners. Then events flow in (events) and views, effects, emits and host calls (host_api) flow out. Images travel through a shared-memory ring (shm) with inline bytes as the fallback. The host restarts a crashed plugin with back-off and gives up after a cap (supervisor). Plugins reach host features (remote files, preferences, models, permissions, fetch) through the services traits, answered over RPC.

Commander, chat and preferences are built on this crate exactly like a third-party mode.

Re-exports§

pub use hosted::Hosted;
pub use hosted::HostedItem;
pub use hosted::HostedView;
pub use hosted::ItemIcon;
pub use hosted::ViewLayout;
pub use mode::Context;
pub use mode::Effect;
pub use protocol::API_VERSION;
pub use protocol::ApiRange;
pub use serve::Host;
pub use serve::Mode;
pub use serve::serve;
pub use ui::Action;
pub use ui::DockSize;
pub use ui::MenuEntry;
pub use ui::Panel;
pub use ui::PanelItem;
pub use ui::PanelStyle;
pub use ui::Rect;
pub use ui::Viewport;
pub use ui::dock_chrome_height;

Modules§

codec
Framing: a 4-byte little-endian length, then one message in MessagePack (default) or JSON (MOUSER_MODE_WIRE=json, for authors in any language).
complete
Tab completion for Mouser text fields.
events
Events and listeners (EVENTS.md sections 3, 4, 8 and 11). Host events and plugin-emitted events go through one dispatcher with the same matching, permissions and ordering; a plugin hears its own emits unless a listener opts out. The rules a dispatcher enforces live here so host and tests share them.
host
The host side: start a plugin, run the handshake, pump its messages, and restart it after a crash with back-off (crate::supervisor). Mouser owns one Supervisor per enabled plugin. Nothing here touches a shell: shells live in the session server, so a plugin crash never affects them.
host_api
Calls a plugin makes to the host (EVENTS.md sections 5 and 13). Every call runs with the logged-in user’s rights (or the SSH login’s on a remote target) and never more: elevation is only Escalate, typed into the user’s visible terminal after a host-drawn card, so the OS or sudo asks the user directly and Mouser never sees a password.
hosted
Views and media the host runs for a mode (mode-api 0.4, docs/mode-protocol.md §Host views and media). A mode registers them by id with crate::Host::hosted (crate::protocol::Out::Hosted): where each one goes and what it shows. Mouser then lays it out and draws it, loads the thumbnails, plays the media (pictures, video and sound, inline and fullscreen), takes its keys and clicks and redraws it as it animates or plays. The mode hears what happened as events:
mode
What a mode asks of the host, and where it may draw.
protocol
Wire messages and version negotiation (docs/mode-protocol.md). The shape follows the approved event spec: a Hello that registers listeners and declares emitted events, a Welcome that grants or denies them, events in (crate::events::Event) and Out messages back.
serve
The plugin side: implement Mode and call serve from main. It sends the Hello, waits for the Welcome, maps the shared-memory ring and then feeds every event to the mode on one thread, in order. The Host handle sends views, effects, emits and requests; it can be cloned into worker threads (a worker that emits an event gets it back through the mode’s own listener, in order with input).
services
Host services a plugin may call. The host implements these traits and answers crate::host_api::Call::Service requests with them, checking the permission named on each method first; the plugin SDK offers the same methods as RPC clients. Model calls go through AgentService, so a plugin never sees an API key.
shm
A ring of image slots in shared memory. The host creates it and passes its path in the handshake; the plugin maps the same file, writes RGBA into a slot and sends only an crate::protocol::ImageRef. On Linux the file lives in /dev/shm (memory only); elsewhere in the temp directory, which the OS keeps in its page cache. Where neither can be mapped (some sandboxes) the host passes no ring and images go inline.
supervisor
Crash restarts for a mode plugin. The host respawns a plugin that exited or broke its pipe after a back-off of 1 s, 2 s, then 4 s. More than max_restarts crashes (preference, default 3) within WINDOW and it gives up: the plugin stays disabled and the status bar offers a restart (Restarts::reset). Shells never depend on this: they live in the session/PTY server.
ui
The UI model shared by Mouser and mode plugins: plain data a mode lays out and the host draws.