Host Functions API Reference
A StudioBrain plugin Component imports the plugin world in packages/plugin-sdk/wit/plugin.wit (studiobrain:plugin@0.1.0, studiobrain-core). The host implements these interfaces:
| WIT import | Functions |
|---|---|
host-entities | read-entity, write-entity, query-entities, delete-entity |
host-storage | read-file, write-file, list-files, delete-file |
host-ai | embed, generate, search-similar |
host-settings | get-setting, set-setting, get-all-settings, delete-setting |
host-http | fetch |
host-log | log |
There is no host::entity_read, host::entity_list, host::asset_read, host::http_request, host::ai_generate, host::storage_get, or capabilities.host_functions array. Rust guests call the generated bindings, for example host_entities::read_entity (see packages/plugin-sdk/templates/rust/README.md).
Declare permissions from the core installer allowlist. The core installer rejects entity_read, entity_write, asset_read, asset_write, file_read, file_write, ai_generate, http_request, and colon-style names such as entity:read (network:local is the one colon string it accepts).
Older WIT comments and the desktop sidecar (sb-client-plugins) used colon names such as entity:read, filesystem:read, settings:own, and network:external. Those are not installer permissions. The current core WIT comments, core host_state.rs, and the cloud Worker all use the underscore names from the installer allowlist.
Two hosts, two permission sets
Core wasmtime (crates/sb-plugin-wasmtime/src/host_state.rs) checks only the flags in the table below. plugin_supervisor.rs sets those flags from ALLOWED_PLUGIN_PERMISSIONS and ignores every other manifest string. serve_routes is on the installer allowlist but is not a supervisor flag; the ext-routes gate reads that string itself.
| WIT import | Flag host_state.rs checks |
|---|---|
host-entities read-entity, query-entities | read_entities |
host-entities write-entity, delete-entity | write_entities |
host-storage read-file, list-files | read_data |
host-storage write-file, delete-file | write_data |
host-http fetch | net_http |
host-settings get-setting, get-all-settings | read_settings |
host-settings set-setting, delete-setting | write_settings |
host-ai embed, generate, search-similar | none. Core returns internal. call_ai is not read. |
host-log log | none |
net_http is in ALLOWED_PLUGIN_PERMISSIONS (crates/sb-server/src/routes/plugins.rs line 82). Declare net_http in the manifest’s permissions array to enable outbound HTTP. Core fetch also accepts only GET; any other method returns validation-failed.
read_assets, write_assets, read_templates, write_templates, send_notifications, and call_ai are accepted at install and stored. This page does not assign them to imports, because this host does not read them.
Cloud Worker (required_permission in packages/cloudflare-worker/sb-server-worker/src/plugin_host.rs) is a separate host that checks the same underscore permission names against the installation’s granted permissions:
| Worker action | Permission that action requires |
|---|---|
log | none |
read-entity, query-entities | read_entities |
write-entity | write_entities |
get-setting | read_settings |
set-setting, delete-setting | write_settings |
That function does not list delete-entity, get-all-settings, storage, HTTP, or AI; unknown actions fail closed.
The desktop sidecar clients/desktop/rust-backend/sb-client-plugins is a separate client host with its own permission vocabulary (a manifest object with entity_read, entity_write, fs_read, fs_write, network booleans, and colon names such as network:external in permission.rs). That vocabulary is not the core allowlist. This page does not map it onto either host. Tracked as SBAI-13530.
Shared types
Errors are the WIT host-error enum, not string codes such as capability_not_declared or file_read:
| Variant | Meaning in the WIT comment |
|---|---|
permission-denied | The host refused the call. |
not-found | The resource was not found. |
validation-failed | The host rejected the input. |
internal | Host failure. Core host-ai returns this for every call. |
timeout | The operation timed out. |
rate-limited | Rate limit exceeded. |
entity-record is entity-type, entity-id, data (a JSON string), created-at, updated-at. entity-json is that JSON string, not a flattened object with _body.
host-entities
read-entity: func(entity-type: string, entity-id: string) -> result<entity-record, host-error>;
write-entity: func(entity-type: string, entity-id: string, data: entity-json) -> result<entity-record, host-error>;
query-entities: func(filter: entity-query-filter) -> result<entity-query-result, host-error>;
delete-entity: func(entity-type: string, entity-id: string) -> result<bool, host-error>;entity-query-filter fields: entity-type, filter-json, limit, offset, order-by, order-desc (all optional). entity-query-result is records, total, offset, limit.
Core checks: read_entities for read and query, write_entities for write and delete. The cloud Worker checks read_entities for read-entity and query-entities and write_entities for write-entity; it has no delete-entity action.
use studiobrain::plugin::host_entities;
use studiobrain::plugin::host_log::{self, LogLevel};
let entity = host_entities::read_entity("character", "ch_alice").unwrap();
host_log::log(LogLevel::Info, "read entity");entity.data is the JSON string. The binding style matches packages/plugin-sdk/templates/rust/README.md.
host-storage
read-file: func(path: string) -> result<list<u8>, host-error>;
write-file: func(path: string, content: list<u8>) -> result<file-info, host-error>;
list-files: func(directory: string, pattern: option<string>) -> result<list<file-info>, host-error>;
delete-file: func(path: string) -> result<bool, host-error>;file-info is path, size, content-type, modified-at.
Core checks: read_data for read and list, write_data for write and delete. filesystem:read and filesystem:write are not installer permissions. There is no asset_read / asset_write import.
host-settings
get-setting: func(key: string) -> result<option<string>, host-error>;
set-setting: func(key: string, value: string) -> result<_, host-error>;
get-all-settings: func() -> result<string, host-error>;
delete-setting: func(key: string) -> result<bool, host-error>;get-setting returns a JSON string, or none when unset. get-all-settings returns one JSON object string.
The core host checks read_settings for get-setting and get-all-settings, and write_settings for set-setting and delete-setting (crates/sb-plugin-wasmtime/src/host_state.rs lines 228, 239, 250, 261). The cloud Worker checks read_settings for get-setting and write_settings for set-setting and delete-setting. It does not list get-all-settings.
host-http
fetch: func(
url: string,
method: http-method,
headers: list<kv-pair>,
body: option<list<u8>>,
) -> result<http-fetch-response, host-error>;http-method is get, post, put, patch, delete, head, options. http-fetch-response is status, headers, body (list<u8>).
network:external is not an installer permission. The core host checks net_http (granted by declaring net_http in the manifest’s permissions array), and it implements GET only. The domain allowlist is network.allowed_domains in the manifest. The cloud Worker permission table has no HTTP permission.
host-ai
embed: func(text: string) -> result<list<f64>, host-error>;
generate: func(prompt: string, max-tokens: option<u32>) -> result<string, host-error>;
search-similar: func(query: string, entity-type: option<string>, limit: option<u32>) -> result<list<search-result>, host-error>;search-result is entity-type, entity-id, score, snippet.
The WIT comment says these are not implemented in core. The core host returns internal for all three and does not read call_ai. There is no model or options argument. ai_generate is not an import and not an installer permission.
host-log
log: func(level: log-level, message: string);log-level is debug, info, warn, error. The function does not return a result. Neither host checks a permission for log.
Exports
The same world exports metadata (required: get-info), optional hooks (on-entity-save, on-entity-create, on-entity-delete, on-entity-validate, on-project-init), and optional routes (list-routes, handle-request). The core ext-routes gate mounts routes under /api/ext/{plugin-id}/ only when the manifest includes serve_routes.
Inbound http-request on handle-request is a record (method, path, headers, query-params, body). It is not the outbound host-http fetch function.
Next Steps
- Permissions Reference — installer allowlist and the cloud Worker names
- Creating Plugins — manifest and host-import table
- Getting Started — scaffold a Component
- Plugin Examples — official proofs