clay.js
  1. ClayJS
  2. Advanced
  3. Plugins
  4. Advanced blockeditor

Advanced blockeditor

Internal decorations, lazy menu descriptors, the blockeditor-tool wire protocol, the view-mode reader, mode leases, and boot marks.

For host authors, editor integrators, and performance harnesses; for using the block editor in a document, see blockeditor.

Empty placeholders are drawn, never written#

The help an empty page shows is a decoration over the editor: it is drawn while a node is empty and it is never in the saved article, so an empty state can never become saved content. The title’s placeholder and the block’s Type / for blocks are the same mechanism:

JavaScript
if (!node.content.size) decoration = Decoration.node(offset, offset + node.nodeSize, { class: "blockeditor-title-placeholder", "data-placeholder": TITLE_PLACEHOLDER });

The block placeholder is drawn the same way for an empty paragraph at the caret. The words live in the decoration and in the page’s stylesheet, not in the document, so printing, exporting and a reader never see them.

The block menu names every command before its code loads#

The block menu is plain data: a label, an icon, a description and either a block type or the name of the action. A row whose feature is not in the boot names it instead of importing it, so discovery works before anything optional is downloaded and only the picked row loads code. The toolbar’s current block type is read from the same list:

JavaScript
const MENU_ITEMS = [...BLOCK_TYPES, ...INSERT_ITEMS];

function blockTypeOf(node) {
  const t = node.type.name;
  return BLOCK_TYPES.find((b) => b.type === t && (!b.attrs
    || (t === "heading" || t === "toggle" ? (b.attrs.level ?? null) === (node.attrs.level ?? null)
      : t === "list_item" ? b.attrs.kind === node.attrs.kind : true))) ?? BLOCK_TYPES[0];
}

A row such as Mention carries command: "mentions", and its picker arrives on the pick. insertItemsFor adds a row only when the document can use it, so the chart row appears with the first table.

Host capability#

A host enables tool generation by listing { "name": "blockeditor-tool", "state": "ready" } (or "unavailable" when AI editing is turned off in the host) in /_/meta document.helpers. Without blockeditor-tool in document.helpers, /ai does not appear in the block menu and Revise… does not appear in Tool options. Listed as unavailable, the composer opens with the host’s setup explanation and Send disabled.

What goes on the wire#

Sending calls clay.wire.send(payload, { helper: "blockeditor-tool", document: "none" }) with a blockeditor-tool/1 payload (src/page/rich/page-tool.js, src/page/tool-transport.js):

  • prompt: the description typed in the composer (up to 8,192 UTF-8 bytes).
  • context: { title, outline, tables } built from the current document (title from the page title, outline from headings as { level, text }, and tables as { id, name, cols, rows }). Before sending, trimmedContext trims context to fit 16,384 UTF-8 bytes by dropping rows from the last table first and then outline entries.
  • target and current: null for /ai. For Revise…, target is { toolId, rev } and current is { artifact, state }, where artifact holds the tool block’s six string fields (title, html, css, javascript, fallbackHtml, summary) and state holds the tool’s live data at clay.live("blockeditor.tools").data[toolId][rev] (or {}).

When /ai is chosen, the editor records a bookmark on the block where /ai was typed (hostId, slashText, slashOffset) so the preview and insert find that block again even if the slash text stays on the line while assets load.

Preview, Keep and Discard#

When the wire/done result { requestId, artifact, state, model } arrives, the editor validates artifact (an object with those exact six string fields, encoded JSON at most 24,576 bytes) and state (cloneable JSON data, encoded at most 8,192 bytes). It then mounts the candidate in a view-mode tool host (createToolHost with mode: "view", seed: state, and the document’s tables) inside div.blockeditor-ai-preview. For a revision, or when /ai was run in an empty or slash-only text block, the preview hides that block in place; when /ai was run in a block that already has other text, the preview renders right after that block. Nothing is written to the document or the blockeditor.tools live store while the preview is up.

Discard disposes the preview host, clears the decoration, and writes nothing.

Keep runs a two-stage commit so a save failure never leaves a broken tool block in the page:

  1. For a revision, it checks that the tool’s live data at clay.live("blockeditor.tools").data[toolId][rev] still matches the JSON snapshot taken when the request started. If the tool’s data changed while the new version was being made, Keep refuses with This tool's data changed while AI was working, so it was not replaced. Ask again to include the latest data.
  2. It allocates a fresh rev (and a fresh block id for a new tool), writes state to blockeditor.tools at [id, rev], and awaits clay.save.flush(). If writing or flushing fails, the preview clears, no block is inserted or updated, and the bar reports The tool could not be saved, so it was not added. Try again.
  3. It checks again that the target tool block is still in the document at the same rev (or, for /ai, that the bookmarked block is still in the document) and that the live data has not moved. If the block moved or changed, Keep refuses (This tool changed while you were revising, so nothing was replaced. Ask again. or The place for this tool moved. Close this and run /ai where you want the tool.).
  4. It clears the preview and dispatches one undoable step (asStep): for a revision, updating the tool block’s artifact and rev; for /ai, removing any remaining /ai text at the bookmark and replacing the empty block or inserting the new tool block after it. It then flushes the save again.

Prompt files and hosts#

The prompt files the hosts feed the model ship in @panphora/clayjs at tool-generation/system.md and tool-generation/context.md (exported as @panphora/clayjs/tool-generation/* and listed in dist/offline-manifest.json). Hosts read the system prompt from their bundled release and never accept one from the client.

On hyperclay.com, blockeditor-tool is listed beside ai-edit for the document’s owner and team editors. It runs DeepSeek V4.1 Flash, bills 2 AI credits per tool, and charges only when a valid tool reply is delivered to the client; a request that is cancelled, times out, is declined by the model or fails validation charges nothing. On Hyperclay Local and HTML Clay, blockeditor-tool runs the person’s own claude or codex CLI under the same AI Editing setting and per-document one-request-at-a-time slot as ai-edit.

The reader#

A Document that boots in view and never enters edit loads the reader, blockeditor-view, not the editor. The loader adds it by itself; no URL names it. The plugin’s implications stay in edit mode: the reader implies no live data service, no generic sync and no upload, so plugins=blockeditor in view mode resolves to blockeditor-view alone.

  • Saved, not live. The reader follows the file the host serves, through a receive-only transport (src/sync/saved-stream.js): a startup GET once the stream opens, then frames, then a refetch and repair for whatever it missed. It never POSTs, never relays a live preview and needs no write credentials.
  • One receipt. A reader holds exactly one: the exact bytes on screen, their ETag and the sequence they cover ({ bytes, etag, seq }). A duplicate or older frame is ignored without moving it, and a refused or failed read leaves both the article and the receipt as they were.
  • Keyed replacement. Each accepted file replaces only the blocks whose saved form changed (src/page/reader-patch.js). An unchanged block keeps its node, so an open toggle, a mounted tool and find-in-page survive an update.
  • Explicit sync. A page that also names sync keeps one stream: the reader opens none of its own and takes the saved files that lane applied. A peer’s unsaved keystrokes never become a reader’s saved content.
  • Who is here. In view mode the reader also draws the roster of who is present, from the stream it already follows (Presence).
  • What it reports. clay:reader-status fires on document for each file it handles: { state: "applied", etag, seq } once one is on screen, { state: "blocked" } when a read or an apply was refused.
  • When it is ready. The reader’s ready settles without waiting for a file: without sync it settles null once its saved transport has started, and with sync it settles to the started reader, because that lane starts the reader before it returns. Either way clay:ready never waits on the network and fires even if the stream never opens, and a page that is not one article[data-blockeditor], or whose reader cannot start, settles null.
  • When the file is here. The reader module’s acquired (src/plugins/page-view.js, beside ready) settles to the started reader on the first accepted saved file, or to null on a read or an apply that was refused or failed, and that first answer is the one it keeps. The stream keeps running either way, so a later accepted file still starts a reader. Await acquired, not ready, for the file.

Across a mode change#

The blockeditor plugin is a participant of the mode lifecycle, with the id clay:blockeditor and order 400. Switching without a reload describes how a participant is prepared, and Leaving edit how an exit refuses.

On a live entry the editor is imported while the tab still shows view, and it takes the article over from the reader: the reader gives the article up (yieldArticle), and takes it back (reclaimArticle) if the entry is refused or the preparation fails. The session’s saves and autosave are held until the commit, so nothing is written before it.

Entering adopts the file already on screen as its baseline, so a clean enter and exit leaves the file byte for byte as the host served it. Anything typed after the commit is saved from there.

The boot marks#

Each boundary is a performance.mark with { detail: { installId } }:

MarkExact boundary
document:bootan installation starts, before the source is validated
document:assets-readythe editor’s code is in hand; a failure here is editor-assets-failed
document:authority-readythe capability and first-save gates passed, the page handle is admitted and the binding is created
document:input-readyadoption succeeded and editable was enabled on the current mounted view

A failed or cancelled installation never emits document:input-ready, and neither mark after document:boot is emitted by a branch that failed. document:authority-ready says the room admitted this tab and the binding exists; it does not say the writer election is over or that the feature views the article demands have installed. Those settle under the adopting phase, before input is enabled.