Back to The Bathyal Post
SOURCE / PINNED RELEASE

Made of little things.

The Bathyal Post

Release
fa6ce530b9f3…
Author-recorded commit
b255b3c9fd49…
License
LICENSE
Author’s source reference
nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-55c50b9a042

Archive hash verified: 03cf62191e7476c5…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

.agents/skills/napplet-design/SKILL.md
---
name: napplet-design
description: Use FIRST when planning a napplet (sandboxed Nostr iframe applet), before any code - turns an idea into a short build spec - the sandbox authority contract, which shipped NAP domains own each feature (OUTBOX-first, relay as escape hatch), hard requires vs optional fallbacks, the form-factor plan (project-specific visual direction, any size from widget to full screen, declared minimum only if the UI truly breaks), theme, and data flow. Output is consumed by napplet-build.
---

# Designing a Napplet

Run this before `napplet-build`. Output the spec block at the end; do not write app code here. If the starting point is an existing Nostr app, run `napplet-port` first. Protocol truth is canonical NIP-5D (<https://github.com/nostr-protocol/nips/pull/2303>) and the NAPs track (<https://github.com/napplet/naps>) — never invent surface; flag gaps.

## What a napplet is

A single self-contained `/index.html` that a host **runtime** (shell) loads into an iframe with `sandbox="allow-scripts"` (no `allow-same-origin`, opaque origin) and talks to over `postMessage` using the NIP-5D JSON envelope. The shell owns identity, signing, relays, storage, network, and the window chrome (it shows the napplet's name). The napplet owns UI and logic only. It is an **applet**: small, focused software that may be placed at any size — see `napplet-ui`.

## Sandbox authority contract

Design as if direct browser authority does not exist. A valid spec names the NAP boundary for every capability:

- External bytes (images, avatars, media, fonts, JSON, WASM side files) are bundled into the single-file artifact or flow through `resource`.
- Persistence flows through `storage` (async, 512 KB, scoped) — never browser storage, cookies, or a local database.
- Nostr reads and publishes flow through `outbox`, `common`, `lists`, `count`, `dm`, or a named `relay` escape hatch. The napplet never holds keys or signs.
- External navigation flows through `link`; playback sessions through `media` when the shell owns that boundary.
- Single file: JS inline, assets folded in, no runtime code-splitting, no external `<script src>`.

If the design needs `fetch`, `XMLHttpRequest`, `WebSocket`, `localStorage`, IndexedDB, cookies, external scripts/styles/images, relay pools, `window.nostr`, or app-owned signing, it is not a napplet yet — redesign around a shipped NAP or flag the missing capability.

## Step 1 — Scope one job

Write the single-purpose job in one line. If the request is a whole app, split it into focused napplets (feed, composer, profile, DMs, settings…) unless the user explicitly wants one compound napplet that still works in a small frame. Cross-napplet handoff uses `inc` / `intent` (`napplet-interop`).

## Step 2 — Map features to shipped NAP domains

Use only domains the current `@napplet/sdk` exports (`napplet-sdk` lists them). Default to the highest-level NAP that owns the user intent:

| Feature shape | Prefer | Do not start with |
| --- | --- | --- |
| Feed / profile / event reads that should follow author relay lists | `outbox.query` / `outbox.subscribe` | Manual `relay.subscribe` + app relay discovery |
| Publishing user-authored events | `outbox.publish` | `relay.publish` |
| Follow / unfollow / react / report / profile lookup / NIP-19 | `common` | Hand-built events + publish |
| Mute / pin / bookmark / follow-set / relay-list mutation | `lists` | Editing replaceable lists locally |
| Counts or badges without event bodies | `count.query` | Downloading events to count them |
| Direct messages | `dm` | Implementing NIP-04/17 crypto and routing |
| Current user pubkey / snapshots | `identity` (read-only) | Polling or `window.nostr` |
| App state | `storage` | `localStorage` |
| Avatars, images, JSON, media bytes | `resource` | `fetch`, `<img src=https://…>` |
| Uploads, external links | `upload`, `link` | Direct network / `window.open` |
| Settings, host look | `config`, `theme` | App-owned settings pages |
| Shortcuts | `keys` | Global key capture |
| Notifications, playback, device bridges | `notify`, `media`, `cvm`, `ble`, `serial`, `fs`, `webrtc` | Browser APIs |
| One explicit relay, diagnostics, protocols outside the outbox model | `relay` | Treating relay as the data layer |

Every NAP is voluntary: assume a domain may be absent and name the fallback. `relay` needs an exact written reason; if none exists it is wrong. There is no `shell.ready()` / `shell.supports()` / capability probe — a conforming runtime installs `window.napplet` before module code runs.

## Step 3 — Hard requirements vs optional enhancements

- **Hard** (napplet cannot do its one job without it) → bare domain names in the vite-plugin `requires: [...]` so a shell can refuse the load up front.
- **Optional** → no manifest entry; gate after injection with `if (window.napplet?.domain)` and give a fallback.

Prefer optional + graceful degradation. `keys`, `theme`, `config`, `notify`, `resource`, `inc`, `intent` are almost never hard.

## Step 4 — Visual direction and form factor

Use napplet-ui and docs/napplet-visual-design.md. Choose a direction fitted to the
idea (or preserve the existing creation), covering both content/game scene and
UI/HUD. Record character, palette roles, type, shapes, density, motion and one
representative state. Starter tokens and host branding are not product defaults.

- Plan tiny, phone, regular, wide and short-height layouts around the interaction.
- Declare a minimum only when genuinely needed, with a useful notice below it.
- Choose app-owned colors (default), host-matched UI or a scoped hybrid. State
  which surfaces may change; test host light/dark and missing theme support.
- Plan usable loading, empty, error and signed-out states and touch/keyboard input.

## Step 5 — Write the spec

```
nappletType: <kebab d-tag, e.g. "note-feed">
purpose: <one line, one job>
NAPs used: outbox (req), storage (req), identity (opt), common (opt), resource (opt)
requires: [outbox, storage]        # hard only
optional domains and fallbacks: resource -> initials avatar; common -> hide react/follow; keys -> buttons only
SDK helpers: outbox.query, outbox.subscribe, outbox.publish, storage.getItem/setItem, identity.getPublicKey/onChanged
data flow: <queries / subscriptions / publishes / stored keys>
relay escape hatches: none | <exact reason outbox/common/lists/count/dm cannot cover>
config schema: none | <fields>
interop: none | <see napplet-interop lines>
visual direction: <character; main surface; UI/HUD palette, type, shapes, motion>
density: <suited to the interaction and input devices>
tiers: tiny=<…> compact=<…> regular=<…> wide=<…> short-height=<…>
minimum size: none | <W×H + reason + notice text>
theme policy: app-owned (default) | host-matched UI | scoped hybrid; <affected surfaces and local palette>
states: loading=<…> empty=<…> error=<…> signed-out=<…>
```

Hand this to `napplet-build`; verify with `napplet-test`.

## soyLI manifest metadata

Before publication or changing capabilities/discovery, read docs/napplet-manifest.md. Save metadata in napplet.json; the publisher creates the signed event. Choose optional archetypes in napplet.json for what the napplet does (game for games). Consult https://archetypes.napplet.run/ for observed spellings; new names are valid. Keep descriptive topics separate. For opt-in public rooms follow docs/napplet-backend.md, retain manual entry and never advertise private rooms or guest keys. Advertise only implemented intents and optional integrations, and verify their absent-domain fallbacks.

## soyLI controller input (NAP-GAMEPAD)

Read this before adding, porting or testing USB/Bluetooth gamepad support. Read
docs/napplet-controllers.md for the full guide and use docs/examples/gamepad.ts.

**Protocol.** Controllers follow the NAP-GAMEPAD draft,
https://github.com/napplet/naps/pull/130 (draft revision 0b28f77e3dfa0f07dd7c0f9d61a7090ee99f3a5e). The upstream shim supplies the standard binding over the private NIP-5D port. Controllers are browser-wide devices, so the soyLI
runtime is their only native reader. It denies the native API to napplet frames
(Permissions-Policy `gamepad 'none'`) and pushes `gamepad.state` snapshots to
the napplet that holds input focus. Other napplets see connected pads with zero
axes/buttons and `timestamp` 0.

**How it is patched in.** The host prelude runs before your scripts. Where the
native `navigator.getGamepads()` exists but is denied, it replaces only
`Navigator.prototype.getGamepads` with a reader of the latest snapshot and sends
one `gamepad.subscribe`. Pads keep native `Gamepad`/`GamepadButton` prototypes,
in slot order with `null` gaps. `gamepadconnected`/`gamepaddisconnected` arrive
as `GamepadEvent`s through listeners and `on…` handlers. A host `blocked` state
throws `SecurityError`; `unavailable` returns empty slots. Where native access is
allowed or there is no Gamepad API, the native API is untouched; the standard binding subscribes on first use. The shim also exposes the
optional `window.napplet.gamepad`: `getGamepads()`, `available`, `focused` and
`onChange(callback) → { close }`. It is installed independently of native API replacement.

**Build rules.**
- Write ordinary Gamepad API code: poll `navigator.getGamepads()` once per
  `requestAnimationFrame`, or use `createGamepadInput` from the bundled helper.
  The same code runs on hosts with native access. Feature-detect
  `window.napplet?.gamepad` only for its `focused`/`available` state.
- Gamepad is optional. Do not add `gamepad` (or SERIAL) to `requires`; keep
  complete keyboard and pointer/touch controls. Use NAP-KEYS (`keys`) for named
  keyboard actions when the game has them.
- Treat `focused: false` as idle input: pause or freeze controller-driven
  actions; do not treat the neutral snapshot as release events. Do not fire a
  press for a button already held when focus returns; the helper waits for
  `focused: true` for you.
- Route players by `pad.index`, never array position. Handle disconnect by
  clearing that player's input. Do not use `pad.id` as an identity.
- Expect about one host frame of extra latency and no rumble
  (`vibrationActuator` is `null`). `event.isTrusted` is `false`; do not gate on it.
  Do not rely on own-property shape, `JSON.stringify(pad)` or `[native code]`.
- Show players that the napplet must be clicked/focused to receive controller
  input, for example on the title or pause screen.

**Verify.** Use the Controller tester in soyli dev, then click the napplet to give
it input. Check movement, edges (one press per press), pause on focus loss, no
phantom press on refocus, disconnect/reconnect and keyboard/touch fallback.
Synthetic or simulated input and soyli check are not hardware tests: report
physical-controller and Safari/Firefox coverage separately.