---
title: Package format
summary: ExtensionPackage, the hash, the bundle's module shape, the modules the frame provides (HOST_MODULES), CSS and design tokens, size limits and allowed imports.
audience: both
stability: stable
since: protocol 1
---

# Package format

Every extension is installed, published and updated as one JSON file: an `ExtensionPackage`. It holds the manifest, one JavaScript bundle and one CSS file. Nothing else is loaded: no other scripts, no assets, no network. The build script (`scripts/build-extensions.mjs`) and the in-app Studio (`apps/desktop/src/lib/extensionBuild.ts`) both produce it. Don't write one by hand.

## `ExtensionPackage`

```ts
const PACKAGE_FORMAT = 2;

interface ExtensionPackage {
  format: typeof PACKAGE_FORMAT;
  manifest: ExtensionManifest;
  js: string;
  css: string;
  readme: string;
  hash: string;
  builtAt: number;
  source?: Record<string, string>;
}
```

| Field | Meaning |
|---|---|
| `format` | `PACKAGE_FORMAT`, which is `2`. The server, the registry and the Studio refuse any other value, so packages from before the rename to SpaceMIX (format `1`) need rebuilding: "This isn't a format 2 package; packages from before the rename to SpaceMIX are format 1. Rebuild the extension with this version of SpaceMIX." |
| `manifest` | The [manifest](manifest.md) as JSON. The build reads it out of the bundle, so it is exactly what the module exports. |
| `js` | One script that assigns the module to `globalThis.__opExtension` (`BUNDLE_GLOBAL`). See [The bundle](#the-bundle). |
| `css` | The Tailwind utilities the extension's own files use, plus any CSS it imports. See [CSS](#css). |
| `readme` | The folder's `README.md`, markdown, shown in the store. Empty string when there is none. Required (may be empty). |
| `hash` | Lower-case hex SHA-256 of the id, version, bundle, CSS and canonical manifest, each as a 4-byte big-endian length then its UTF-8 bytes. |
| `builtAt` | Milliseconds since the epoch. Required. |
| `source` | The extension's text files by path relative to its folder (`index.ts`, `view/Board.tsx`, `README.md`), when the package carries them. Both builders include it. Used for review and remixing in the Studio; never run. |

### The hash

The hash covers the id, the version, the bundle, the CSS and the canonical manifest. Each goes in as a 4-byte big-endian length then its UTF-8 bytes, so bytes can't move from one field to the next. The canonical manifest is the manifest as JSON with every object's keys sorted by name and no whitespace (`canonical_json` in the sign-in crate, `canonicalJson` in `build-extensions.mjs` and `lib/extensions.ts`); keep key names ASCII and numbers plain, since very large numbers and keys outside the basic plane could be spelled or sorted differently by the two sides, and such a package is refused. It does not cover `readme`, `builtAt` or `source`.

- Import from file, dev installs and publishing refuse a package whose hash doesn't match.
- A registry download is accepted only when SpaceMIX Hub's signature over `spacemix-extension-package\n{id}\n{version}\n{hash}` verifies and the hash matches. Since the hash covers the manifest, so does the signature (see [Registry](registry.md#signedpackage)).
- An installed package whose stated `hash` isn't the one its install names is treated as missing on every read, so it doesn't run (see [The check at upgrade](permissions.md#the-check-at-upgrade)).
- Running frames are reloaded whenever the installed hash changes.

The host computes it with WebCrypto, or a built-in SHA-256 where WebCrypto isn't available (`packageHash` in `lib/extensions.ts`); the build script and the Rust side compute the same value.

## The bundle

`js` is one IIFE. When it runs in the frame, it must set `globalThis.__opExtension` to an `ExtensionModule`:

```ts
interface ExtensionModule {
  manifest: ExtensionManifest;
  View: ComponentType;
  actions?: Record<string, ActionHandler>;
  mcp?: (host: SpacemixHost) => Promise<McpServer[]>;
}
```

The entry file (`index.ts` or `index.tsx`) default-exports it:

```ts
import type { ExtensionModule } from "@spacemix/sdk";
import { manifest } from "./manifest";
import { actions } from "./actions";
import { Board } from "./view/Board";

export default { manifest, View: Board, actions } satisfies ExtensionModule;
```

The frame runtime refuses a module that:

- throws while running: `<Name>'s code failed to run: <error>`
- doesn't set the global: `<Name>'s code didn't set __opExtension.`
- has no `manifest` with a string `id`: `<Name>'s module has no manifest.`
- has no `View` (a function or an object, such as a `memo` component): `<Name>'s module has no View.`

The host then refuses a module whose `manifest.id` and `manifest.version` differ from the installed package's.

### What runs when

- The bundle runs once per frame, before anything is mounted. `host` is connected before the first line runs, so module-level code may read `host.manifest` or `host.env`. Don't call host methods at import time: the build script runs the bundle in a plain VM with stubbed modules to read the manifest, where nothing is connected.
- `View` is mounted as `<View />` with no props, inside an error boundary, in an element that fills the extension's box.
- `actions[name]` runs when the assistant, a widget or `host.tools.call` calls the action, whether or not the view is showing.
- `mcp(host)` runs whenever the host lists tools (a chat turn starts, an extension calls `host.tools.list` or `host.tools.call`), and only for extensions with a `net:` permission. It starts the frame if it isn't running.

## Host modules

The bundle doesn't contain React, the SDK or the UI kit. It imports them, and the frame provides them as globals before the bundle runs. The table is `HOST_MODULES`:

| Import | Global |
|---|---|
| `react` | `__opReact` |
| `react/jsx-runtime` | `__opJsxRuntime` |
| `react/jsx-dev-runtime` | `__opJsxRuntime` |
| `react-dom` | `__opReactDom` |
| `@spacemix/sdk` | `__opSdk` |
| `@spacemix/sdk/react` | `__opSdkReact` |
| `@spacemix/ui` | `__opUi` |
| `lucide-react` | `__opLucide` |
| `motion/react` | `__opMotion` |

Each global is the module's namespace object with a `default` export added (the namespace itself when the module has no default), so both `import React from "react"` and `import { useState } from "react"` work. `jsxDEV` is mapped to the production `jsx`/`jsxs`, so a development-mode bundle still runs.

This is the most important compatibility fact in the contract: **an extension runs against the host's copy of these modules, not the one it was built with.** `host`, `defineExtension`, the React hooks and every `@spacemix/ui` component come from the SpaceMIX version the server runs. See [Compatibility](../contract.md#compatibility-rules).

The versions provided with protocol 1: React 19, `lucide-react` 1.x, `motion` 14 (`motion/react`), `@spacemix/sdk` 0.2.0 and `@spacemix/ui` 0.1.0.

### Allowed imports

An extension may import only:

- the modules in `HOST_MODULES`, and
- its own files.

The Studio refuses any other bare import: "Extensions can only import react, react/jsx-runtime, react-dom, @spacemix/sdk, @spacemix/sdk/react, @spacemix/ui, lucide-react and motion/react; copy anything else into the project." It loads `.ts`, `.tsx`, `.js`, `.jsx`, `.mjs`, `.json`, `.css` as code and `.md`, `.txt`, `.svg`, `.html` (or any file with `?raw`) as text.

The build script doesn't refuse other packages: it bundles them into `js`. A package bundled this way is your code from the host's point of view, and must not reach the network or the DOM outside the frame (it can't). Two imports are wrong in either builder:

- `@spacemix/sdk/runtime` and `@spacemix/sdk/protocol` are exported by the SDK's `package.json` but are **not** host modules. Importing `@spacemix/sdk/runtime` bundles a second, never-connected `host`. Use `@spacemix/sdk`.
- `react-dom/client` isn't provided. The frame mounts the view; an extension never calls `createRoot`.

## CSS

`css` is applied as one `<style>` element in the frame's `<head>`, after the frame's own styles.

The frame already provides:

- Tailwind's preflight (base reset);
- the design tokens from `@spacemix/ui/tokens.css`: the `--op-*` variables on `:root`, switched by `data-theme="dark"` on `<html>`, and the Tailwind theme names that point at them (`bg-bg`, `bg-surface`, `bg-elevated`, `bg-hover`, `bg-sunk`, `text-fg`, `text-muted`, `text-subtle`, `text-accent`, `bg-accent`, `text-accent-fg`, `bg-accent-soft`, `bg-accent-hover`, `text-accent-text`, `border-border`, `border-border-strong`, `border-control-border`, `text-danger`, `rounded-sm` … `rounded-xl`, `shadow-pop`, `font-sans`, `font-display`, `font-mono`, `ease-out`);
- the styles `@spacemix/ui`'s components use;
- the Figtree font (with Inter Variable for scripts outside Latin), which `font-display` also uses, and `body` set to `bg`/`fg` with the sans font;
- `font-medium` and `font-semibold` at Figtree's 550 and 650 (not Tailwind's 500 and 600), and `shadow-xs` and `shadow-sm` tinted ink in light and near-black in dark.

The build compiles only the utilities your files use, against Tailwind's theme and the tokens, without emitting the theme variables themselves. Two consequences you must know about:

- **Theme variables can be missing.** A utility like `max-w-3xl`, `text-xl` or `bg-amber-500` compiles to `var(--container-3xl)`, `var(--text-xl)`, `var(--color-amber-500)`. Those variables exist in the frame only if SpaceMIX's own frame code happens to use the same utility. Today, for example, `--container-3xl`, `--text-xl` and `--color-amber-*` are not defined, so those utilities do nothing. Prefer the token utilities above, arbitrary values (`max-w-[48rem]`, `text-[17px]`), and plain CSS. This is listed in the contract's open questions.
- **`dark:` follows the operating system, not SpaceMIX.** Tailwind's `dark:` variant is a `prefers-color-scheme` media query. SpaceMIX's theme is `data-theme` on `<html>`, which the token colors already follow. Use the tokens instead of `dark:`; if you must branch, use `[[data-theme=dark]_&]:…` or read `document.documentElement.dataset.theme`.

The theme the view opens with is in the frame URL and on `<html>` before the bundle runs, and changes arrive as the `theme` message; the runtime updates `data-theme` itself.

## Size

| Where | Limit |
|---|---|
| Studio build | `js` + `css` at most 2 MB: "The extension is <n> MB built; the limit is 2 MB." |
| Publishing | The whole package JSON at most 8 MB: "Packages can be at most 8 MB." (413) |
| Server download from the registry | 9 MB, signature and envelope included. |
| Install on this server (from the Studio) | 9 MB request body over HTTP (a browser); no limit over the desktop app's IPC. |
| Build script | No limit of its own. |

A typical bundle is a few tens of KB, because the host modules aren't in it.

## Building

| Builder | Command | Output |
|---|---|---|
| Build script | `npm run build:extensions` in `apps/desktop` (`--only <folder>`, `--watch`); `npm run dev` runs the watcher | `dist/extensions/<id>.json` and `dist/extensions/index.json` at the repo root |
| Studio | **Build** in an extension session of the Studio | The same `ExtensionPackage`, kept with the project; **Download package** saves it |

The build script bundles with Vite (target `es2022`, production mode, minified); the Studio with esbuild (targets `es2022`, Chrome 111, Safari 16.4, Firefox 128). Both produce an IIFE assigning `__opExtension`, treat `HOST_MODULES` as globals, and compile CSS with Tailwind v4 against the tokens.

## Stability

`ExtensionPackage`, `PACKAGE_FORMAT`, `HOST_MODULES`, `BUNDLE_GLOBAL` and `ExtensionModule` are **stable**. Adding a host module is additive. Removing one, changing a global's name, or a major version change of React, `lucide-react` or `motion` is breaking, and changes `PACKAGE_FORMAT` (see [Contract](../contract.md#versions)). Removing a Lucide icon in a minor `lucide-react` update would break bundles that import it; see the contract's open questions.
