npm.io
0.25.0 • Published yesterday

@floegence/floeterm-terminal-web

Licence
MIT
Version
0.25.0
Deps
0
Vulns
0
Weekly
0

@floegence/floeterm-terminal-web

Framework-neutral browser components for FloeTerm's semantic terminal protocol.

The package does not contain a VT parser, Ghostty WASM, a raw replay/checkpoint pipeline, or a WebGL terminal renderer. terminal-go owns terminal state and emits complete immutable Presentations. The browser owns only view-local rendering and interaction.

Install

npm install @floegence/floeterm-terminal-web@0.25.0

WheelRouter routes one vertical gesture to either view-local history or the live application. Pass the renderer's getGridClientRect() and a history row callback to WheelRouter.route so both destinations use the same signed fractional motion. A stroke is bounded to eight 32-step application packets; the live connection checks ownership before writing. bindApplicationPointer forwards primary mouse presses, drags and releases only with explicit pointerReporting; Shift preserves local selection. A live frame advertises wheelGeneration only while mouse reporting or alternate scroll is active. Wheel frames carry the observed generation and controller epoch, do not activate a view, and are never replayed on reconnect.

Exports

Subpath API
./semantic RendererSurface, HistoryViewportController, TerminalInputBridge, validators, semantic types, themes
./live semantic WebSocket codec, client, transport, lifecycle and geometry events
./sessions session coordinator and display-only metadata normalization
. semantic aggregation plus shell-integration parsing utilities

Render a Presentation

import {
  RendererSurface,
  getThemeColors,
  validatePresentation,
} from '@floegence/floeterm-terminal-web/semantic';

const renderer = new RendererSurface(canvas, error => showInlineError(error));
renderer.setPalette(getThemeColors('tokyoNight'));
const metrics = renderer.setTypography({
  fontSizeCssPx: 14,
  fontFamily: '"JetBrains Mono", monospace',
});
renderer.apply(validatePresentation(payload));

RendererSurface owns one 2D canvas. It keeps the latest immutable Presentation, rejects sequence or geometry regressions, follows host CSS bounds, tracks DPR changes, fills every backing pixel before painting cells/graphics/cursor, and never stretches the authoritative cell grid to an observer viewport.

Typography is view-local. setTypography() returns the exact CSS cell metrics used for glyphs, graphics, selection hit-testing, cursor painting, and IME anchoring. Use the same metrics to calculate desired columns and rows before sending a canonical resize request. Font changes repaint the latest Presentation without replacing the canvas or creating terminal state in the browser.

Pointer events continue to call beginSelection(), updateSelection(), and endSelection() for drag selection. Because pointerdown.detail is always zero, handle the completed click event separately: when MouseEvent.detail is at least two, pass it as the third argument to beginSelection() and immediately call endSelection(). A double click selects the semantic word and a triple click selects the logical row. Both preserve wide-cell boundaries and use the same transformed coordinate mapping as drag selection. A drag must begin on the terminal grid, while captured pointer updates and release outside the grid clamp to the nearest visible cell so a fast edge crossing does not interrupt selection.

For keep-mounted panes, call renderer.setVisible(false) before hiding the host. After the active host has its real content-box bounds, call renderer.setVisible(true). The visible call synchronously commits the current DPR backing, full background, and latest Presentation before exposing the same canvas, so an old bitmap is never stretched by CSS during a tab or Workbench switch.

Themes are view-local. Changing the palette repaints the latest Presentation without requesting output, resizing the PTY, or affecting another view. Explicit ANSI/RGB colors stay semantic; only default foreground/background/cursor fallbacks change.

Input and IME

import { TerminalInputBridge } from '@floegence/floeterm-terminal-web/semantic';

const bridge = new TerminalInputBridge({
  inputHost,
  inputElement: textarea,
  onData: data => transport.sendInput(sessionId, data),
  onPreedit: text => renderer.setPreeditText(text),
  onInputIntent: intent => transport.sendInputIntent(sessionId, intent),
  onPaste: data => transport.sendPaste(sessionId, data),
  syncInputGeometry: () => positionTextarea(renderer.getCursorLayoutRect()),
});

The textarea must remain editable and positioned in the visible terminal viewport. The optional preedit callback is for local display only. The renderer places underlined marked text beside its canvas in the same positioned container. Neither the semantic frame nor the native candidate anchor changes. Do not forward, log or persist this callback. Hosts with a known private input mode must clear/suppress the preview in that mode; PTY echo alone is not a reliable password indicator. Composition preedit is never sent to the PTY. The final Unicode composition commit is sent exactly once across Chrome/Safari event orderings. Non-text keys are emitted as structured W3C key intents so the actor-owned native Ghostty encoder, not the browser, resolves cursor modes, modifiers, and terminal escape sequences. Paste, dead-key/emoji input, copy shortcuts, focus, and controller ownership remain separate from composition state. Supply onPaste so the live transport can send one bounded paste transaction; the SessionActor-owned native encoder applies the current bracketed-paste mode and newline rules. Selection copy handles the browser copy event synchronously when its clipboardData argument is present, preserving native Cmd+C / Ctrl+Shift+C behavior.

Both cursor rectangle APIs use CSS pixels; canvas backing DPR is never multiplied into the IME anchor. Use getCursorLayoutRect() when the editable element is absolutely positioned in the canvas containing block. Use getCursorClientRect() only for a fixed or portal element positioned in viewport coordinates.

Semantic Live Transport

import {
  createSemanticTerminalLiveTransport,
} from '@floegence/floeterm-terminal-web/live';

const { transport, eventSource } = createSemanticTerminalLiveTransport({
  connectionId,
  openStream,
  control,
  onError: reportError,
});

await transport.attach(sessionId, cols, rows);
const stop = eventSource.onTerminalPresentation(sessionId, value => {
  renderer.apply(validatePresentation(value));
});

// Only a real user activation may transfer controller ownership and PTY size.
await transport.activate(sessionId, desiredCols, desiredRows);

// A real keyboard interaction can atomically apply the latest measured viewport
// and reach the PTY without waiting for a separate activation acknowledgement.
await transport.interactInput(sessionId, desiredCols, desiredRows, 'x');

The transport carries attach, structured input, canonical resize settlement, explicit same-principal view activation, controller ownership events, generation-bound semantic clear settlement, Presentation, geometry, and lifecycle frames. Observer resize reports its local viewport but cannot change canonical PTY geometry. A real activation validates attachment, principal, transport generation, and controller epoch, then transfers ownership, applies geometry, and captures the matching Presentation in one actor ordering window. A racing stale epoch has zero effect and is resettled a bounded number of times without detaching or reconnecting the stream.

Use interactInput, interactInputIntent, or interactPaste when a real user interaction must claim a view. Each method keeps terminal state authoritative in the server-owned Ghostty actor while removing the extra activation acknowledgement round trip from the input path. Ordinary sendInput methods remain available for an already settled controller.

The transport does not carry raw PTY output. clearSemanticContent(sessionId) invokes the native SessionActor clear control through the current transport generation and rejects a settlement if that generation was superseded. Unknown input is not replayed after a disconnect. A new transport generation does not write through an old connection.

Semantic History

HistoryViewportController is the single view-local owner for scroll targets, wheel residuals, one serial request lane, and a bounded cache of complete semantic frames. The control plane returns bounded chunks from one actor-owned snapshot; the live transport verifies their identity, byte count, and SHA-256 digest before it decodes and returns one canonical-size viewport. Missing, stale, or corrupt chunks never replace the last complete frame.

Each navigation request carries the current opaque frontier and snapshot identity, so a direct scrollbar seek is one server capture regardless of distance and a stale response cannot overwrite a newer target. HistorySearchController owns a separate, bounded server search lane. Search never replaces the navigation frontier, and every resolved match is returned as one complete atomic viewport before it can be projected. Search results identify every occurrence with its absolute row and exact semantic cell span, including wide and multi-code-unit cells. Use semanticHistorySearchDecorationsForViewport() with the active match ID, then pass the result to renderer.setSearchDecorations(). Decorations remain view-local, do not alter selection or copy state, and distinguish all visible matches from the active match. Caches account for semantic bytes as well as cells, use a 4 MiB per-view budget and a 16 MiB global soft budget, and evict hidden-view extras first.

Normal live output continues to advance in the background while a view browses an immutable history frame. Returning to the bottom reveals the latest Presentation.

History snapshots always pass their declared length and SHA-256 checks before decoding. Hashing uses @noble/hashes and works on ordinary HTTP origins without WebCrypto SubtleCrypto; unavailable browser secure-context APIs never disable integrity validation. HTTP does not authenticate or encrypt network transport; the host application remains responsible for its connection security policy. Content epoch, geometry, attachment, and transport-generation changes invalidate the view-local cache; an observer's scrolling never sends input, resizes the PTY, or takes controller ownership.

Session Metadata

The ./sessions entry is independent of rendering. It normalizes session list, foreground command, output activity, execution context, and semantic work metadata. These values are display-only and must not be used for authorization.

Lifecycle

Dispose each view's subscriptions, input bridge, renderer, and live transport. Disposal cancels RAF/blink/DPR/font listeners and releases graphics bitmaps without detaching or resizing another view.

Development

npm ci
npm run lint
npm test
npm run test:browser
npm run build
npm run check:package-artifact

The package-artifact check installs the packed tarball into a fresh consumer and rejects removed parser, checkpoint, renderer, and WASM contracts.

History interaction and retention

The host creates one HistoryScrollbar with its track, thumb and existing HistoryViewportController. Call scrollbar.update() from the controller's onState callback, and dispose it with the view. Supply localized aria-label and aria-controls on the track. The component owns pointer capture, grab-point preservation, one navigation per animation frame, page clicks, keyboard navigation and ARIA values. Its pixel minimum keeps the thumb usable for long histories; it remeasures on resize. An optional onPointerComplete returns pointer users to the host input. Keyboard scrolling retains focus. Hosts own visual styles, focus rings and coarse-pointer policy.

Call controller.handleUserInput(intent, followEnabled) immediately before accepted user input; omit intent for committed text or paste. The default follows live output. Releases and disabled follow do not navigate. Do not call it for background PTY output, copy, selection, composition-in-progress, or modifier-only browser events. Returning to live cancels pending wheel work and invalidates old history results.

The live transport exposes optional historyRetention and configureHistoryRetention control-plane adapters. Mutations are bound to the current attachment generation, validate safe integer limits and responses, and never retry uncertain effects. The host owns authorization, persistence and explicit destructive-action confirmation.

Keywords