SpaceMIX v0.1.0

Building a widget

experimentalfor bothsince protocol 1

Making a widget in the Studio (describing it, the starters, review, saving, versions, unsaved widgets), writing one by hand as a widget file, importing and exporting, the limits, and how WIDGET_FRAME_VERSION versions a saved widget.

Most widgets are made in a widget session of the Studio: you describe what you want, the assistant writes it, and you watch it beside the chat. You can also write a widget by hand as a widget file and import it. Either way the result is the same thing: a name, a description, sources and an HTML fragment, stored in the core's widgets table and shared with the whole team. Making, changing, deleting or duplicating a widget, and importing a widget file, take the server:widgets permission; see Who may create extensions and widgets.

Open a widget session

A widget session is a tab of the Studio, the window where widgets and extensions are built: a tab with a chat beside a live preview of the widget it builds, the preview on the left, the chat on the right (stacked on a narrow window). The Studio takes the whole window and keeps one tab per session; its tabs, and switching a session to an extension, are in The Studio. Open a widget session from:

Where Control What it opens
Widgets, in the server menu at the top left (under Current server) Create widget A session for a new widget.
A dashboard Create widget A session for a new widget that goes on that dashboard when you first save it.
A widget's menu in the gallery or on a dashboard Edit in Studio in either The widget's own session if its chat still exists, otherwise a new one that the widget moves to.
The command palette "New widget" A session for a new widget.
The Studio's + New widget A session for a new widget, in a new tab.
The Studio's + Edit a widget… A searchable list of the server's widgets, those open in a tab first; picking one opens it as Edit in Studio does.
The Studio's start screen New widget or Edit a widget… The same as in the Studio's +.

Choosing Create widget again for the same dashboard reuses a session you opened and never typed in, so empty chats don't pile up. Studio chats don't show in the chat list; you reach them through their tab in the Studio, the Studio's start screen (Recent sessions), or their widget. Closing the tab deletes nothing.

Describe it

Say what the widget should show, in plain words ("my open tasks by column", "a count of unread mail"). The assistant picks sources from the read-only tools it has, writes the HTML, runs the sources once and reads their real results, so it can fix its code in the same turn. While it works the preview shows Working….

The preview shows the widget at the size it takes on a dashboard, measured on a dashboard about 1,100 px wide. Under it are the size presets: Small 3×3, Medium 4×5, Large 6×8, Wide 12×4 and Tall 3×8 (columns × rows of the 12-column grid). A new widget is 4×5. The size is only where it starts when added to a dashboard; each placement can be resized there.

Click the widget's name in the header to rename it; the Studio's chat takes the same name.

When the widget shows an error, a bar under the preview shows it with Ask to fix, which sends The widget shows this error: <error> and Please fix it. to the assistant.

How long the widget keeps its data (refreshSeconds) has no control in the Studio. Ask the assistant for a different lifetime, or set it in a widget file; see The data cache.

The starters

An empty Studio offers four suggestions to start from: "My open tasks by column", "Notes I edited this week", "Latest commits in my repos" and "A count of unread mail". Clicking one sends it as your first message. They only help when the extension they mention is installed.

A new server also gets a Main dashboard holding starter widgets, once: Open tasks (when Tasks is installed) and Recent notes (when Notes is installed). They wait for the first person who holds server:widgets and may use Dashboards, while the server has no widgets, and go beside any dashboard already there. They are ordinary widgets you can change or delete, and they are written in the house style, so they are good examples. Their code is in apps/desktop/src/lib/widgetStarters.ts.

Save, discard, and unsaved widgets

Everything the assistant does in the Studio is a draft. The header says Not saved yet for a widget that was never saved and Unsaved changes for a saved one with a draft.

  • Save (or Ctrl+S, ⌘S on a Mac) makes the draft the widget. A new widget opened from a dashboard goes onto that dashboard on its first save.
  • Discard throws the draft away. A widget that was never saved is deleted; a saved one goes back to how it was saved. The chat stays either way.
  • Until you save, dashboards, the gallery and Team Chat keep showing the saved version.

A widget that was never saved stays in its Studio. It runs only in the Studio preview. In the gallery it carries a Not saved badge and shows "Not saved yet — open it in the Studio." in place of the widget, and its menu offers only Edit in Studio and Delete: it can't be added to a dashboard, exported, refreshed or picked in Team Chat, and dashboard_add_widget refuses it. Someone without server:widgets sees "Not saved yet." and no menu.

Widgets are team-wide, but chats belong to one account. Opening someone else's never-saved widget from the gallery first shows Open <name>?: the tools it reads ("· not installed here" for tools this server doesn't have), that its script can call any read-only tool you have access to, and that it runs with the access of whoever views it. Open in Studio opens it in your own Studio.

A widget made by the assistant in an ordinary chat, outside the Studio, is saved at once, with no draft. It goes on the dashboard the assistant names, or on the only dashboard when the server has exactly one; otherwise it waits in the gallery.

Versions

Versions in the Studio's header lists the saved version and the earlier ones the widget keeps, newest first: at most 10. A version is kept each time a save changes the description, the sources or the HTML; renaming alone doesn't make one. A change through widget_update in an ordinary chat keeps one too.

Preview loads a version into the Studio's draft. Save then keeps it (the version it replaces goes into the list); Discard goes back. If the draft held changes that aren't in any version, you are asked Replace your unsaved changes? first. Unsaved size and refresh changes stay either way.

The header's other controls, for a saved widget: Add to dashboard (or On <dashboard>) lists the dashboards to put it on, plus New dashboard. The … menu has Rename, Settings, Open in Widgets, Export widget file and Go to <dashboard>.

Settings and saved data

A widget can keep data of its own: a checklist's ticks, a note, a choice. It is the widget's store, one JSON object per widget on the server, shared by everyone who sees the widget; the widget reads it as spacemix.store and writes it with spacemix.save (widget frame 2). How a widget saves is in Saving data; the exact rules are in The widget's store.

A widget may also declare settings: fields of a form (text, several lines, number, on/off switch, choice) whose values live in the same store. The assistant writes them with the widget, as it writes the code; defaultStore is what the store holds before anyone saves. See Settings.

Settings opens the widget's settings panel (apps/desktop/src/shell/widgets/WidgetSettingsDialog.tsx). It is in a saved widget's menu in the gallery, and in the Studio's … menu (also for a widget not saved yet):

  • The Settings tab is the form drawn from settings; Save writes only the fields you changed, over the current store.
  • Advanced: JSON shows the whole store as JSON; Save replaces it with what you wrote, at most 256 KB. A widget without settings shows only this view.
  • The store is live: what someone else saves while the panel is open shows in it.

Anyone who can see the widget can change its store this way, even someone who can't change the widget. In the Studio, a draft's preview works on a copy of a saved widget's store, so trying the draft never changes the team's data; saving the draft doesn't write the copy. The store isn't part of the widget's versions, and it isn't in an exported widget file.

Writing one by hand

The Studio has no code editor. To write a widget yourself, write a widget file and import it: the Studio then shows it live, and you save it there.

The document

A widget's html is one HTML fragment: markup, <style> and <script>, inserted into the frame's document. Scripts run in order after the markup is inserted, once per frame; a change to the HTML starts a new frame. Start from .op-widget and render from spacemix.onData:

<div class="op-widget" id="w"></div>
<script>
  const { html, render, empty, error, failed } = spacemix.ui;
  spacemix.onData(({ tasks }) => {
    if (failed(tasks)) return render("#w", error(tasks));
    if (!tasks.total) return render("#w", empty("Nothing open."));
    render("#w", html`<div class="op-stat"><div class="op-stat-value">${tasks.total}</div><div class="op-stat-label">open tasks</div></div>`);
  });
</script>

The frame gives the script one global, spacemix:

Member In short
spacemix.onData(fn) fn(data) now and after every update. Render from this.
spacemix.data Each source's result by key; a failed source is { error }.
spacemix.call(tool, args) Runs another read-only tool; rejects on error or after 10 seconds.
spacemix.open(extensionId) Shows that extension; data-open on an element does the same on click.
spacemix.theme "light" or "dark".
spacemix.ui The widget kit's helpers; see Look and feel.
spacemix.store The widget's store, starting from its defaults. Frame 2.
spacemix.save(value, { merge }) Saves the store: replaces it, or with merge: true puts value's top-level keys over it. Frame 2.
spacemix.onStore(fn) fn(store) whenever someone else changes the store (another viewer, the settings panel, the assistant). Frame 2.
spacemix.version The widget frame version, 3.

The exact behaviour of each is in spacemix in the widget. Sources and spacemix.call are in Data.

The widget file

A widget file is JSON, named <name>.widget.json by export (any .json imports):

{
  "format": 1,
  "kind": "spacemix-widget",
  "frameVersion": 3,
  "widget": {
    "name": "Open tasks",
    "description": "How many tasks are still open.",
    "sources": [{ "key": "tasks", "tool": "tasks_items_list", "args": { "where": { "done": false }, "limit": 6 } }],
    "html": "<div class=\"op-widget\" id=\"w\"></div><script>…</script>",
    "size": { "w": 4, "h": 5 },
    "refreshSeconds": 86400
  }
}
Field Type Required What it is
format number yes 1, the file format.
kind string yes "spacemix-widget".
frameVersion integer no, default 1 The WIDGET_FRAME_VERSION the HTML was written for. Import refuses anything below 3, so in practice it is required.
widget.name string yes Non-empty, at most 120 characters after runs of whitespace become one space.
widget.description string yes One line on what it shows; may be empty; at most 1,000 characters.
widget.sources array yes At most 20 of { key, tool, args, extension? }; see Sources. args is required here, {} for none. extension is { id, name } (each at most 80 characters, else dropped) and is only a label.
widget.html string yes At most 60,000 characters.
widget.size { w, h } yes Grid cells; clamped to 2–12 columns and 2–24 rows.
widget.refreshSeconds number yes The data cache's lifetime in seconds; less than 30 becomes 30.
widget.settings array no The settings form's fields: at most 30 of { key, label, type?, options?, default?, help? }, keys unique identifiers of at most 64 characters, labels at most 80 characters, help at most 300, a choice with 1–50 options, a text default at most 2,000 characters. See Settings.
widget.defaultStore object no What the store holds before anyone saves: a JSON object, at most 256 KB as JSON.

Export writes the current WIDGET_FRAME_VERSION as frameVersion. The store itself is never in the file: on import the widget starts from its defaultStore.

Anything else in the file is dropped. The parser is parseWidgetFile in apps/desktop/src/lib/widgetFile.ts.

Importing

In Widgets, choose Import and pick the file. SpaceMIX checks it before anything is created:

  • The file is at most 1 MB (1,048,576 characters; the check counts characters), is JSON, has kind "spacemix-widget" and format 1, and every field above has the right type.
  • frameVersion is a whole number, at least 3 and not newer than this SpaceMIX's widget frame. A lower one, or none, or the old kind "operator-widget", is a file from before the rename to SpaceMIX, and the error says so. This check is only for importing files; it doesn't apply to saved widgets.
  • settings and defaultStore, when present, follow the rules in the table above; otherwise the error starts That widget file is damaged:.
  • No source uses one of the assistant's widget tools.
  • A source whose tool is installed here must be read-only, and its args must fit the tool's schema. A source whose tool isn't here is allowed; the widget shows the missing-extension pill until the extension is installed.
  • A web source (web-get) must follow the URL rules, and there are at most 5; its URL is stored normalized. For example That widget can't be imported: its source "btc" won't do (args.url must be an https URL, not http.)

When a check fails, the error shows next to the button, for example That widget file is damaged: source 2 has no valid key., That widget can't be imported: its source "tasks" uses tasks_items_delete, which changes data here; widgets may only read. or That widget was made for widget frame 4, by a newer SpaceMIX; this one runs widget frame 3.

Otherwise Import <name>? lists the tools it reads, every web source's full URL ("fetched by this server"), and says whose access it runs with. Importing is what lets the server fetch those URLs, so read them. Import creates a new, never-saved widget and opens it in the Studio; nothing existing is overwritten. Save it there to keep it, or Discard to drop it.

Exporting

Export widget file is in a saved widget's menu in the gallery and in the Studio's … menu. It saves the saved version (never the draft) as <slugified name>.widget.json, as a download. The file carries the name, description, sources, HTML, size, refreshSeconds, and settings and defaultStore when the widget has them, and none of the store, the draft, the versions, the ids, the chat or the timestamps.

Limits

What Limit Enforced by
html 60,000 characters (the assistant is asked to stay under about 15 KB) widget_create, widget_update, import
Sources 20 per widget in a file; the assistant's tools set no count import
Web sources (web-get) 5 per widget; URL at most 2,048 characters widget_create, widget_update, import
Source key /^[a-zA-Z_][a-zA-Z0-9_]*$/, unique within the widget widget_create, widget_update, import
Size 2–12 columns × 2–24 rows; default 4×5 everywhere (clamped or refused)
refreshSeconds at least 30; default 86,400 (a day) everywhere (raised to 30)
Versions kept 10 the app (apps/desktop/src/lib/widgets.ts)
A widget's store 256 KB as JSON; one write per widget every 200 ms on the server, at most two a second from one window the server, the app
Settings fields 30 per widget widget_create, widget_update, import
Widget file 1 MB; name 120 characters; description 1,000 characters import
Each source run 15 seconds the runtime
spacemix.call 10 seconds the frame and the runtime
Widgets in one Team Chat message 3 shown live Team Chat

How a saved widget is versioned

A saved widget is never rebuilt: the same HTML runs against whatever widget frame the viewer's SpaceMIX ships. So the frame has a version of its own, WIDGET_FRAME_VERSION in packages/sdk/src/widgets.ts, today 3, which a widget can read as spacemix.version. Version 2 added the store (operator.store, operator.save, operator.onStore, as the global was called then). Version 3 renamed the global to spacemix with no alias, so HTML written for an older version has to call spacemix.*; saved widgets were carried over by a one-time migration. Within a version the frame's surface only grows, so a widget saved today keeps rendering on later releases; removing or changing any of it needs a new version. See The widget contract.

A widget file records the version it was written for in frameVersion, and a SpaceMIX whose frame is older refuses it with the error above.

Stability

Stability: experimental. The Studio, the gallery, the widget file format and the limits above are app behaviour (apps/desktop/src/shell/widgets/WidgetStudio.tsx, ImportWidget.tsx, ReviewWidget.tsx, VersionsDialog.tsx, apps/desktop/src/shell/studio/StudioPicker.tsx, WidgetPicker.tsx, apps/desktop/src/lib/widgetFile.ts, apps/desktop/src/lib/widgets.ts). What the HTML may use is the stable frame surface in The widget frame.

This page as markdown · All pages as one file