Skip to main content

Plugin module contract

A plugin's code is one bundled ES module named entry.js. Its default export is the widget, or an async function that returns the widget, and FINOS Workspace uses nothing else that the module exports. The contract is the one that Anywidget defines (AFM 0.11), so the same render({ model, el }) function that Jupyter widgets use works here.

export default {
async initialize({ model, signal, experimental }) {},
async render({ model, el, signal, host, experimental }) {
const draw = () => {
el.textContent = JSON.stringify(model.get("data"));
};
model.on("change:data", draw);
draw();
return () => model.off("change:data", draw);
},
};

The initialize function runs once for each frame, before render. It may return nothing, a cleanup function, or an object of exports, which a later section describes.

Lifecycle​

A widget goes through four stages.

  1. The frame starts and checks its own sandbox and policy. It then receives the verified files and the initial value of every bound trait.
  2. The frame awaits initialize and then awaits render. initialize has 15 seconds and render has 15 seconds. When one of them throws or rejects, the next one does not run, signal aborts, and the card shows an error that contains the module's message.
  3. While the widget runs, host pushes fire change:<trait> events. The module writes with set and save_changes, it calls host commands with experimental.invoke, and the host calls the module's exports.
  4. When the widget is removed or reloaded, signal aborts and every cleanup function that the module returned runs once. The host then empties el and removes the frame.

Every frame runs its own initialize, and that includes a fullscreen view of the same widget. A module therefore must not rely on state that is shared between views. The module sizes itself with a ResizeObserver on el, because the host sizes the frame.

The render function can run before the frame has a size. A dashboard places its cards after their frames start, so el may measure 0×0 at first, and text measures zero width in a frame that has no size. A library that measures text when it draws, such as a chart that lays out its legend or its axis labels, and that keeps the measurement, would draw wrongly from then on. The module should therefore draw once el has a size and draw again whenever it gets a new one.

The model​

The model holds the widget's traits. A trait is a named value that is bound to some piece of host state, and the module reads and writes traits through these methods:

MethodWhat it does
model.get(name)It returns a copy of the trait's value. To change the value, the module calls set.
model.set(name, value)It sets the local value and fires change:<name>.
model.save_changes()It sends the changed traits that are bound and writable. The host echoes back what it accepted. A refused write is echoed with the previous value, which fires change: again.
model.on(event, callback) and model.off(event?, callback?)They add and remove listeners. The callbacks of change:<name> receive no arguments. Calling off() removes every listener, and off(event) removes the listeners of that event.
model.send(content, callbacks, buffers)It sends a message to the host. The host answers only anywidget's own invoke message, which has kind: "anywidget-command", and it answers with a msg:custom event that has the same id. The host drops every other message.
model.on("msg:custom", (content, buffers) => ...)It receives host events. The host sends { kind: "workspace-event", name: "refresh" } when the user refreshes the widget, { kind: "workspace-event", name: "visibility", visible } when the widget becomes visible or hidden, and { kind: "workspace-event", name: "action", id } when the user picks one of the widget's actions, with pressed when the action toggles a setting.

When the manifest has no model.traits entry, the model offers these traits: data, params, definition, config, adminSettings, state, theme, capabilities and meta.

  • dataread onlyneeds core:data:read

    The JSON that the widget's backend endpoint returned, taken after dataKey, or the widget's rows while the user shows them

  • definitionread only

    The data.<type> object of the backend widget, which has been checked against the definition schema

  • paramswritablewriting needs core:params:write

    The widget's parameters as an object of { name: value }

  • configwritablewriting needs core:config:write

    The settings of this widget instance

  • adminSettingsread only

    The settings that the organization's admin made

  • statewritablewriting needs core:state:write

    The module's own instance state, which is saved with the dashboard and may hold at most 65536 bytes of JSON

  • themeread only

    An object of the form { mode, tokens, locale, decimals }. The module should follow it and react to change:theme. It is meant for the plugin's own interface, whereas the content that a backend returns is drawn as its author made it

  • capabilitiesread only

    The module's effective permissions. The module should check them before it calls a command

  • metaread only

    An object of the form { pluginId, version, widgetId, instanceId }

A module that already uses other trait names can keep them by binding them in the manifest:

"model": { "traits": { "rows": { "bind": "data" }, "filter": { "bind": "param:filter", "access": "readwrite" } } }

A binding names one of the traits above, or it has the form param:<name>, config:<key> or adminSettings:<key>. Only params, param:<name>, state, config and config:<key> can be readwrite. A trait without a binding exists only inside the frame.

A write to config or config:<key> needs the permission core:config:write. The host takes only settings that the widget declares, with values that satisfy the setting, and it saves them with the dashboard. The AI agent reads the same settings with read_widget, as ui_args.config, and sets them with update_widget, so a setting that the module writes is one value that the user, the module and the agent share.

Rows​

A backend widget that sets raw has a button that shows its rows instead of its view. When the widget has no data.rawType, or when the user may not use the plugin that rawType names, the module itself shows the rows. In that case data holds the rows, usually as an array of objects, and the frame starts again with them. It starts again when the user switches back to the view. The module should show the rows as a table, or it should say that it cannot show them. The AI agent receives the rows in either case, so a widget with raw does not need to provide copilotContext. Widget rows describes the feature.

File answers​

A handle whose response has accepts: "file" gets its endpoint's answer as a file instead of JSON, for a widget that shows a CSV, a workbook or a Parquet file. The host reads the answer's bytes and sets the data trait to { file: { name, mime, base64 } }. The name comes from the answer's Content-Disposition header, or else from the last segment of the URL, and the host replaces every character other than letters, digits, spaces and _ . - ( ). The media type comes from Content-Type, and the bytes are encoded in base64. The widget's dataKey does not apply to a file. The AI agent sees the file's name and media type, never its bytes, so a module that reads the file into rows hands them to the agent with copilotRows.

Exports​

The initialize function may return an object instead of a cleanup function. The host calls the members of that object:

async initialize({ model }) {
return {
async exportOptions() {
return [{ id: "rows", label: "Rows (CSV)", extension: "csv", mime: "text/csv" }];
},
async exportData(id, { title }) {
return toCsv(model.get("data"));
},
async copilotContext() {
return { rows: model.get("data")?.length ?? 0 };
},
async copilotRows() {
return model.get("data") ?? [];
},
};
}

The function exportOptions returns the exports that the widget offers right now, or an empty list when it offers none. The function exportData returns the content of one export as a string, an ArrayBuffer, a typed array or a Blob, or it returns null when there is nothing to export. The card's Export entry appears when the widget has the permission core:export:provide and the module provides both functions.

A widget may offer at most 16 options, and the fields of an option are on Plugin permissions and commands. The exported file may be at most 25 MiB. The function exportOptions has 10 seconds and exportData has 60 seconds.

The host calls copilotContext when the user asks the AI agent about the dashboard, provided that the widget has the permission core:copilot:context. The result may be at most 256 KiB, and the function has 10 seconds to produce it.

The host calls copilotRows, with the same permission, when the AI agent reads the widget's data. It returns every row of the widget, as an array of objects, and the agent loads them as a table that it queries with SQL. A module whose data holds something other than its rows, such as a file that it reads, provides copilotRows so that the agent gets the table instead. The rows may be at most 25 MiB, and the function has 60 seconds to produce them. A widget with raw gives the agent its backend's rows instead.

A module that returns exports from initialize cannot return a cleanup function as well, so it performs its cleanup through signal.

Presets​

A preset is a named combination of a widget's settings, which the module sees as config, and its own state, which it sees as state. The user saves presets of a plugin widget in the card's Settings and applies them to any card of the same widget on any of their dashboards. An organization admin publishes presets for the whole organization, which its users apply but cannot change. A preset's settings must satisfy the widget's settings, and its state may hold at most 64 KiB of JSON.

Each user can mark one of their presets as their default for a widget, and an admin can mark one organization preset as the organization's default. A new card of the widget starts from the user's default, or else from the organization's default.

A module with the permission core:presets can use presets itself, with the commands core:presets:list, core:presets:apply and core:presets:save that Plugin permissions and commands describes. Applying a preset changes config and state, so the module receives the new values of the traits bound to them, as when the user saves the Settings. The Settings presets work for every plugin widget without the permission.

Errors and budgets​

The host logs uncaught errors and unhandled rejections, and it does not treat them as fatal. The frame does have a message budget of about 100 messages per second, with bursts of up to 200, and each message may be at most 1 MiB. The host ends the frame when the module breaks the budget, when it sends a message that the protocol does not define, or when it fails 3 times within 10 seconds. The card then shows the reason. The host also pings the frame, and it ends a frame that stops answering as unresponsive.

Differences from anywidget in Jupyter​

In JupyterIn a FINOS Workspace plugin
A Python class holds the traits, and the kernel answers model.send.There is no kernel. The traits are bound to host state, and a round trip to server code is a proxy or a backend route.
The _esm and _css values may be URLs.The package supplies entry.js and entry.css as bundled files, and the host adds the CSS as a <style> element.
The calls host.getWidget and host.getModel and the property model.widget_manager reach other widgets.They reject with not_available, because a widget cannot reach any other widget.
The views of one model share its initialize.Every frame runs its own initialize, and that includes a fullscreen view.
experimental.invoke calls Python.It calls a host command, which Plugin permissions and commands lists.
The widget can use the page's storage, cookies, popups and network.The widget has none of these. The opaque origin of the frame makes storage and cookies unavailable, the host blocks popups, alerts and navigation, and the network is limited to what the frame's policy allows.