DeveloperPlugin Iframe Protocol

Plugin Iframe Protocol Reference

Bidirectional postMessage between the StudioBrain host and plugin iframes. This is the panel contract — the UI artifact that runs on desktop, mobile, and cloud.

Guest execution is a different path. See Architecture.

Where does it run?

KindDesktopMobileCloud
Official universalComponent (Cranelift) + these panelsSame Component (Pulley) + these panelsOfficial Worker at our deploy + these panels
Official desktop-onlyComponent + network:local + panels——
Community pluginComponent + panelsSame Component + panelsThese panels and/or the author’s backend. No Worker on BiloxiStudios Cloudflare.
Any data kindFiles in the projectFiles in the projectFiles in the project. No Worker.

Community cloud inside StudioBrain is this protocol. The host never WebAssembly.compiles fetched plugin bytes to “make the panel’s guest run.”

Types live in frontend/src/lib/plugin-message-protocol.ts in studiobrain-core.

⚠️

The type file defines the full message set, but the shipped host components only act on a subset. Today the host sends entity-context (on iframe load, panel blocks only) and answers studiobrain-theme-request; it accepts resize from panel blocks. theme-change, entity-updated, entity-modified, navigate, toast and request-entity-data are typed and covered by the guards below, but no core host component handles or sends them yet. Do not rely on them.

Host to plugin

entity-context

Sent by PluginBlockIframe when the iframe loads.

{
  type: 'entity-context';
  entityType: string;       // e.g. "character"
  entityId: string;         // e.g. "rex_marshall"
  data: Record<string, any>;
}

The theme is not in this message. The iframe URL carries entity_type, entity_id and theme query parameters, and theme variables arrive through the theme sync below. Defensive panels can also read entityData.

studiobrain-theme

Reply to studiobrain-theme-request (sent by the panel, no payload beyond type). Only answered when event.source is the host’s own iframe.

{
  type: 'studiobrain-theme';
  vars: Record<string, string>;  // --surface-*-* variables, shadows, --font-family, --plugin-accent
  isDark: boolean;
}

theme-change

Defined in the protocol types; not currently sent by the core host.

{
  type: 'theme-change';
  theme: 'light' | 'dark';
}

entity-updated

Defined in the protocol types; not currently sent by the core host.

{
  type: 'entity-updated';
  entityType: string;
  entityId: string;
  entityData: Record<string, any>;
}

Plugin to host

Defined in the protocol types; the core host has no handler for these five messages (entity-modified, navigate, toast, request-entity-data) yet. resize is handled.

entity-modified

{
  type: 'entity-modified';
  changes: Record<string, any>;
}

The type file names the field changes.

{
  type: 'navigate';
  path: string;  // e.g. "/characters/rex_marshall"
}

toast

{
  type: 'toast';
  message: string;
  level: 'success' | 'error' | 'info';
}

resize

{
  type: 'resize';
  height: number;  // pixels
}

Handled by PluginBlockIframe; the height is clamped to 200-800px.

request-entity-data

Intended to ask the host to send entity-context again (not handled by the core host today).

{
  type: 'request-entity-data';
}

Security

  • Panels, tabs and widgets load through the same-origin /plugin-proxy/... rewrite (backend /api/plugins/...).
  • Iframes use sandbox="allow-scripts allow-forms", without allow-same-origin, so the iframe has an opaque origin and cannot read the parent’s DOM or CSS. Theme data is pushed by postMessage instead.
  • The host checks event.source against its own iframe’s contentWindow before acting on resize, sb-field-widget-* and studiobrain-theme-request. Host-to-iframe messages are posted with target origin '*', so do not send secrets through them.
  • This protocol does not grant a cloud Worker. Community guests still do not run on BiloxiStudios Cloudflare.

Type guards

import { isPluginMessage, isHostMessage } from '@/lib/plugin-message-protocol';
 
window.addEventListener('message', (event) => {
  if (isPluginMessage(event.data)) {
    // PluginToHostMessage
  }
});
 
window.addEventListener('message', (event) => {
  if (isHostMessage(event.data)) {
    // HostToPluginMessage
  }
});

Panel usage

Receive context

window.addEventListener('message', (event) => {
  if (event.data?.type === 'entity-context') {
    renderPluginUI(event.data.data ?? event.data.entityData);
  }
  if (event.data?.type === 'studiobrain-theme') {
    applyThemeVars(event.data.vars, event.data.isDark);
  }
});
 
window.parent.postMessage({ type: 'studiobrain-theme-request' }, '*');

Resize

new ResizeObserver(() => {
  window.parent.postMessage({
    type: 'resize',
    height: document.body.scrollHeight
  }, '*');
}).observe(document.body);

Use semantic --surface-* variables. See Plugin theme compliance.

Registration

Declare panels in plugin.json:

{
  "id": "my-plugin",
  "capabilities": {
    "frontend": {
      "panels": [
        {
          "id": "analysis-panel",
          "title": "Entity Analysis",
          "location": "entity-sidebar",
          "entity_types": ["character", "location"],
          "url": "/panels/analysis.html"
        }
      ]
    }
  }
}
LocationRenders as
entity-sidebarCollapsible section
entity-tabEntity edit tab
entity-footerBelow editor (reserved; no renderer yet)

Layout block id: plugin:{pluginId}:{panelId} → PluginBlockIframe.

Field widget protocol

Smaller iframe, loaded from /plugin-proxy/{pluginId}/widgets/{widgetId} with sandbox="allow-scripts". The host also sends entityType and entityId in the update message.

sb-field-widget-update (host → widget)

{
  "type": "sb-field-widget-update",
  "value": "current value",
  "disabled": false,
  "options": [],
  "entityType": "character",
  "entityId": "rex_marshall"
}

sb-field-widget-change (widget → host)

{
  "type": "sb-field-widget-change",
  "value": "new value"
}

sb-field-widget-resize (widget → host)

Height clamped to 40–300px.

{
  "type": "sb-field-widget-resize",
  "height": 120
}

See also