/** * Per-type handler interface for `ElementInstanceStore`. * * Adding a future element type (overlay / tour / form) is one new * `ElementTypeHandler` implementation registered in the store — * `ElementInstanceStore` itself stays type-neutral. * * Handlers own their per-type lifecycle. They receive the current * `MountedElement` snapshot and the runtime's `ActionEngine` and either * call `applyBatch` to drive DOM changes or hold on to returned * `ActionHandle`s for later cleanup. * * Design choices the handlers MUST honor: * * - **Imperative actions never re-fire on boot.** When the store * hydrates an element with `lifecycle === 'imperative'` and `state === * 'applied'`, the handler's `mount()` is NOT called. The backend * records the prior invocation for audit but the SDK does not replay * it (would re-scroll, re-navigate, etc.). * - **Defense-in-depth content validation.** `validate()` is called * before every mount/patch. The backend already validated, but the * handler should refuse malformed content so a buggy server can't * crash the runtime. */ import type { ActionEngine } from '../types'; import type { ElementMutation, MountedElement } from './types'; /** Lifecycle classification used to decide whether to apply on boot rehydrate. */ export type ElementLifecycle = 'stateful' | 'imperative' | 'ephemeral'; export interface ElementHandlerContext { /** Runtime's ActionEngine — handlers use `applyBatch` to drive DOM * changes via existing executors (`core:mountWidget`, etc.). */ actions: ActionEngine; /** Runtime's event bus — handlers publish typed events that container * widgets subscribe to. Used by `ItemHandler` to broadcast * `element.compositional_append` so chip-strip / FAQ / nav-tips * containers can insert LLM-authored items. * Optional so test fixtures that exercise only DOM action handlers * don't need to stub the bus. */ events?: { publish: (name: string, props?: Record) => void; }; } export type ValidateResult = { accepted: true; } | { accepted: false; reason: string; detail?: string; }; export interface ElementTypeHandler { readonly typeName: 'tile' | 'action' | 'item'; /** Whether the handler's elements survive a boot rehydrate. Imperative * handlers (action handler in v1) DO NOT replay applied instances. */ readonly lifecycle: ElementLifecycle; /** Defense-in-depth — refuse malformed content/placement before the * handler touches the DOM. The store calls this before `mount` and * `patch`; a rejection is logged but does not throw — the store * records the rejection on the instance and skips the apply. */ validate(instance: MountedElement, ctx: ElementHandlerContext): ValidateResult; /** Apply a new mount to the DOM. Called once per mount; the store * passes the instance object so the handler can read `placement`, * `content`, `template_id`, etc. */ mount(instance: MountedElement, ctx: ElementHandlerContext): Promise; /** Apply a patch — typically by re-mounting the widget with merged * content. v1 has no fine-grained reactivity; full re-mount is * expected. */ patch(instance: MountedElement, ctx: ElementHandlerContext): Promise; /** Remove the element from the DOM. Idempotent — called for both * user-initiated unmounts and TTL-triggered cleanup. */ unmount(instance: MountedElement, ctx: ElementHandlerContext): Promise; } export interface ElementMutationContext extends ElementHandlerContext { /** Op being processed — handler-side telemetry / debug surface. */ mutation: ElementMutation; } //# sourceMappingURL=ElementTypeHandler.d.ts.map