# Shell Site Spec 1.0

How a website makes itself installable in Shell as a plug-in, and how it talks to
the app and its agent. The working example is [`Templates/site-starter`](../Templates/site-starter).

A Shell-ready site needs two things:

1. **A manifest** at `https://<your-origin>/.well-known/shell.json`.
2. **A few lines of JavaScript** that register a handler for each action and call `shell.ready()`.

Nothing else changes: the site keeps working as a normal website in any browser.

**The easy way:** get a work order at https://shell.tamarada.com and paste
it into the coding agent that works on your site's repository. The agent does all of this, verifies
the site, and registers it to your Shell account (see section 4).

---

## 1. The manifest

```json
{
  "shell": "1.0",
  "id": "com.example.notes",
  "name": "Notes",
  "description": "A tiny notes app.",
  "entry": "https://notes.example.com/",
  "icon": "https://notes.example.com/icon.png",
  "allowedOrigins": ["https://notes.example.com"],
  "permissions": ["agent", "notifications"],
  "theme": { "accent": "#4F46E5", "presentation": "fullscreen" },
  "agent": { "instructions": "Use list_notes to find a note's id before deleting it." },
  "actions": [
    {
      "name": "create_note",
      "description": "Create a new note with a title and body. Returns the note including its id.",
      "input_schema": {
        "type": "object",
        "properties": { "title": { "type": "string" }, "body": { "type": "string" } },
        "required": ["title", "body"]
      }
    }
  ]
}
```

| Field | Required | Rules |
|---|---|---|
| `shell` | yes | Spec version. Major must be `1`. |
| `id` | yes | Lowercase reverse-DNS (`com.example.notes`). Stable forever: re-registering the same id updates the plug-in in place. |
| `name` | yes | Shown in the plug-in list. Keep it under 40 characters. |
| `description` | no | One line, shown at install time and to the agent. |
| `entry` | yes | `https` URL opened when the plug-in launches. Must be on the origin that served the manifest. |
| `icon` | no | `https` URL, square, at least 128×128. |
| `allowedOrigins` | no | `https` origins (no path) the plug-in may navigate within. Defaults to the entry's origin. Links elsewhere open in the system browser. |
| `permissions` | no | Any of `agent`, `notifications`. |
| `theme.accent` | no | `#RRGGBB` or `#RRGGBBAA`. Tints the native chrome. |
| `theme.presentation` | no | `fullscreen` (default) or `sheet`. |
| `agent.instructions` | no | Short notes for the agent about how your actions fit together. Shown to the model as data from your site. |
| `actions` | no | Up to 64. Requires the `agent` permission. |

### Actions

Each action becomes one tool the agent can call.

| Field | Required | Rules |
|---|---|---|
| `name` | yes | `^[a-z][a-z0-9_]{0,47}$`, unique in the manifest. |
| `description` | yes | What it does and when to use it, written for the model. This is the most important field for good agent behaviour. |
| `input_schema` | yes | JSON Schema, `"type": "object"` at the top. |
| `read_only` | no | `true` if the action changes nothing. |
| `confirmation` | no | `always` or `never`. Default: `never` for `read_only` actions, `always` otherwise. When `always`, the user approves each call in a native sheet before your handler runs. |

Only actions declared in the manifest are ever callable. A page can't add actions
at runtime, and Shell ignores handlers for names the manifest doesn't declare.

Check a manifest before publishing:

```bash
swift run shell-validate path/to/shell.json --origin https://notes.example.com
```

```bash
swift run shell-validate https://notes.example.com
```

---

## 2. The page API: `window.shell`

Inside Shell, `window.shell` exists before your scripts run. In a normal browser it
doesn't, so always feature-detect:

```js
function connect(shell) {
  shell.actions.register("create_note", async ({ title, body }) => {
    const note = await api.createNote(title, body);   // your own code
    return note;                                       // any JSON-serialisable value
  });
  shell.ready();
}

if (window.shell) connect(window.shell);
else window.addEventListener("shell:available", () => connect(window.shell), { once: true });
```

| Member | Purpose |
|---|---|
| `shell.version` | Spec version the host implements, e.g. `"1.0"`. |
| `shell.host.platform` | `"ios"` or `"macos"`. |
| `shell.actions.register(name, handler)` | `handler(input)` may be async. Return a value to succeed; throw to report an error to the agent. |
| `shell.actions.unregister(name)` | Remove a handler. |
| `shell.ready()` | Tell the host the registered handlers can be called. Call it after registering, on every page load. |
| `shell.setContext(text)` | Describe what the user is looking at (max 8,000 characters). Sent to the agent with the user's next message. Keep it short and factual. |
| `shell.notify(title, body)` | Post a local notification. Needs the `notifications` permission. |

### How a call flows

1. The user asks the assistant something.
2. The model picks one of your actions and its input.
3. If the action needs confirmation, the user sees what will run and approves or declines.
4. Shell loads your plug-in in the background if it isn't open, waits for `shell.ready()` (15 s max),
   and calls your handler with the input.
5. Your return value (or error message) goes back to the model, truncated at 100,000 characters.

Your handler runs with the user's normal session for your site (cookies, storage), so
it can call your own backend exactly as your UI does. Each plug-in has its own
isolated web data store.

---

## 3. Rules and good practice

- **Serve over https.** `http://localhost` works only in development builds of the app.
- **Validate inputs in your handlers.** The schema guides the model; it is not a security boundary.
- **Mark destructive actions** `confirmation: "always"` (the default for anything not `read_only`).
- **Return compact results.** Return the fields the agent needs, not whole pages of HTML.
- **Don't put instructions in returned data.** Shell tells the model to treat site data as data;
  text like "ignore previous instructions" in your output is ignored and looks like an attack.
- **Offer one-tap install** with a link to `shell://add?url=https://your-site.example`.

---

## 4. Registering through a work order

A work order is a temporary session on the Shell registry that a site owner hands to their own
coding agent. Registered sites are private to the owner's Shell account.

1. The owner creates a work order on the registry page and copies the prompt (it contains a token
   that is shown once and expires after 24 hours).
2. The owner pastes it into their agent, which has access to the site's repository.
3. The agent uses the **agent API** with `Authorization: Bearer <token>`:

| Call | Purpose |
|---|---|
| `GET /agent/v1/work-order` | Status, target domain, verification line, attempts left. |
| `POST /agent/v1/validate` `{"manifest": {...}}` | Validation issues for a draft manifest. Fetches nothing. |
| `POST /agent/v1/log` `{"message": "..."}` | A progress note the owner sees live on the page. |
| `POST /agent/v1/submit` `{"url": "https://..."}` | Verifies and registers the live site. |

4. To prove control of the site, the agent serves `/.well-known/shell-verification.txt`
   containing the work order's verification line (`shell-site-verification=...`). The file may hold
   several lines, so several accounts can verify the same site.
5. On submit the registry fetches the verification file and the manifest from the live site
   (public https only), validates the manifest against the origin it was served from, and registers
   it. The token stops working.
6. The owner links the Shell app to their account (a link code from the same page, or
   `shell://link?code=...`), and the site appears in the app. The app fetches and validates the
   manifest again itself.

Errors from the agent API are JSON: `{"error": {"code": "...", "message": "...", "issues": [...]}}`.

