API / @aihu/runtime

@aihu/runtime

Runtime core

Single File Component (.aihu) runtime — registers custom elements compiled by @aihu/compiler.

version
3.0.0
exports
39
values
26
types
13
01

_ensureA11yStyles

function
function _ensureA11yStyles(): void
02

_hmrReplace

function
function _hmrReplace(element: HTMLElement, newSetup: Setup): void
03

_registerAgentDispatcher

functionagent
function _registerAgentDispatcher( element: Element | null | undefined, dispatcher: InstanceAgentDispatcher, ): void

Register a mounted instance's agent dispatcher.

04

_registerAgentServerBinding

functionagent
function _registerAgentServerBinding( element: Element | null | undefined, binding: InstanceAgentServerBinding, ): void

Register a mounted instance's FULL server agent binding.

05

_setHydrate

function
function _setHydrate(fn: HydrateFn): void
06

_setMount

function
function _setMount(fn: MountFn): void
07

_setSignal

function
function _setSignal(s: typeof SignalFactory): void
08

_takeAgentDispatcher

function
function _takeAgentDispatcher( element: Element | null | undefined, ): InstanceAgentDispatcher | undefined

Look up the instance dispatcher registered for `element` (the mounted host custom element).

09

_takeAgentServerBinding

function
function _takeAgentServerBinding( element: Element | null | undefined, ): InstanceAgentServerBinding | undefined

Take (read) the server binding registered for `element`.

10

announce

function
function announce(message: string): void

Programmatic ARIA announcement.

11

createFocusTrap

function
function createFocusTrap( active: boolean | (() => boolean), returnFocus: boolean, initialFocus: string | null, childFn: () => Branch, ): Branch

Focus-trap boundary.

12

createResource

function
function createResource<T>(factory: () => Promise<T>): ResourceHandle<T>

Create a reactive resource backed by an async `factory`.

13

createStream

function
function createStream(factory: () => Promise<ReadableStream<string> | null>): StreamHandle

Create a reactive stream handle backed by a `ReadableStream<string>` factory.

14

defineComponent

function
function defineComponent(setupOrOptions: Setup | ComponentOptions): typeof HTMLElement
15

defineElement

function
function defineElement( name: string, Ctor: typeof HTMLElement, options?: DefineOptions, ): void
16

onAdopt

function
function _onAdopt(fn: () => void): void
17

onAttributeChange

function
function _onAttributeChange( fn: (name: string, oldValue: string | null, newValue: string | null) => void, ): void
18

onCleanup

function
function _onCleanup(fn: () => void): void
19

onMount

function
function _onMount(fn: () => void | (() => void)): void
20

__aihu_cpath

const
const __aihu_cpath = (p: string): string => …

Comment-safe path for structural markers — the walker's `_commentPath` (`-` → `_` so arbitrary list keys can't terminate the comment early).

21

__aihu_eattr

const
const __aihu_eattr = (v: unknown): string => …

Attribute-value escape (`&` and `"`), the walker's `escapeAttr`.

22

__aihu_key

const
const __aihu_key = (v: unknown): string => …

List-item key normalization — the walker's `String(key).replace(/\./g, '_')` (dots would splice into the path grammar; see `_structuralSubtrees` in

23

__aihu_sattr

const
const __aihu_sattr = (k: string, v: unknown): string => …

One serialized attribute (` k="v"` / ` k` / nothing), mirroring the walker's `serializeAttrs` value rules for a RESOLVED value: functions (event handlers) never serialize, `true` renders the bare attribute, `false`/`undefined` render nothing, everything else stringifies escaped.

24

__aihu_stext

const
const __aihu_stext = (v: unknown): string => …

Escaped text hole for a REACTIVE leaf — the walker reads `String(get())`, so `null`/`undefined` stringify ("null"/"undefined"), exactly like a client-side reactive text binding would render them.

25

__aihu_stext0

const
const __aihu_stext0 = (v: unknown): string => …

Escaped text hole for an EAGER leaf — the walker's `leafText` renders nullish static values as the empty string.

26

_hydrateOnVisible

const
const _hydrateOnVisible = (element: HTMLElement, hydrate: () => void): void => …
27

ComponentOptions

interface
interface ComponentOptions<A extends ReadonlyArray<string> = ReadonlyArray<string>> {
  attrs?: A
  /**
   * R1 ($prop reactivity, template-syntax-v2 round 5): rich per-prop
   * metadata bag describing the Lit-style `attribute` / `reflect` /
   * `converter` lowering. When non-empty, the runtime synthesizes
   * `observedAttributes` from prop entries with `attribute !== false`,
   * wires `attributeChangedCallback`, allocates one `Signal` per prop
   * initialized to `value`, defines a JS property accessor on the class
   * prototype, and (when `reflect: true`) reflects signal writes back to
   * the attribute with a re-entrancy guard.
   *
   * Value flows through to the setup function via `ctx.props.<name>`,
   * a per-name `Signal<unknown>`. Setup callers are expected to read via
   * the signal-getter call (e.g. `props.title()`); writes via
   * `props.title.set(...)` or via the JS property accessor on the host
   * element (`el.title = newValue`) flow back through the same signal.
   */
  props?: PropsConfig
  /**
   * Recipe class-extension (master spec §9.4): the generated element class
   * extends this base custom-element class instead of `HTMLElement`, so the
   * base's `connectedCallback` (role/ARIA/keyboard, form-control inheritance,
   * cross-piece context provision) runs on the host. Emitted by the compiler's
   * `$extends:` macro. Options-form only — base-extending recipes always carry
   * `$prop` declarations, so they compile to the options-form. The base's
   * `connectedCallback` runs BEFORE the template mounts (so a context-providing
   * primitive registers before its slotted child pieces upgrade), and its
   * `disconnectedCallback` runs on teardown; `observedAttributes` are unioned
   * and `attributeChangedCallback` is forwarded to the base.
   */
  base?: typeof HTMLElement
  setup: (ctx: SetupContext & AttrContext<A> & PropsContext) => Branch | Leaf
}

Options passed to the overloaded `defineComponent` when typed `observedAttributes` + per-attribute signals are desired.

28

DefineOptions

interface
interface DefineOptions {
  shadowMode?: ShadowMode
  /** When true, connectedCallback checks window.__aihu_state__[name] and calls the injected hydrate fn. Plan 3.2. */
  hydrate?: boolean
}
29

InstanceAgentDispatcher

interface
interface InstanceAgentDispatcher {
  readonly tag: string
  /** opaqueId → action invoker (called with the positional args array). */
  readonly actions: Record<string, (args: unknown[]) => unknown>
  /** opaqueId → read accessor (current signal value). */
  readonly reads: Record<string, () => unknown>
  /** opaqueId → write accessor. */
  readonly writes: Record<string, (value: unknown) => void>
}

The instance-bound dispatcher shape.

30

InstanceAgentServerBinding

interfaceagent
interface InstanceAgentServerBinding {
  readonly tag: string
  /** memberName → action invoker (called with the positional args array). */
  readonly actions: Record<string, (args: unknown) => unknown>
  /** memberName → read accessor (current signal/computed value). */
  readonly reads: Record<string, () => unknown>
  /** memberName → write accessor (prop signal setter). */
  readonly writes: Record<string, (value: unknown) => void>
  /** `$scope` from the `@agent` block, or undefined when unscoped. */
  readonly scope: string | undefined
  /** `$rate-limit` as `'<n>/min'`, or undefined when unlimited. */
  readonly rateLimit: string | undefined
}

The full, per-instance agent binding the SERVER build injects into the setup body.

31

PropDef

interface
interface PropDef {
  value?: unknown
  attribute?: boolean | string
  reflect?: boolean
  converter?: (raw: string | null) => unknown
}

R1 — single `$prop` definition.

32

PropSignal

interfaceagent
interface PropSignal {
  (): unknown
  set(v: unknown): void
}

R1 — per-prop signal handle exposed to setup.

33

ResourceHandle

interface
interface ResourceHandle<T> {
  /** True while a fetch is in flight (initial load or a refetch). */
  readonly loading: boolean
  /** The latest resolved value, or `null` before the first success. */
  readonly data: T | null
  /** The error from the most recent failed run, or `null`. */
  readonly error: Error | null
  /** Re-run the factory. Resolves when the run settles. */
  refetch(): Promise<void>
}
34

SetupContext

interface
interface SetupContext {
  readonly host: ShadowRoot | Element
  readonly element: HTMLElement
}

Context passed to a `defineComponent` setup function.

35

StreamHandle

interface
interface StreamHandle {
  readonly value: string
  readonly delta: string
  readonly status: StreamStatus
  readonly error: Error | null
  start(
    source?: ReadableStream<string> | (() => Promise<ReadableStream<string> | null>),
  ): Promise<void>
  stop(): void
}
36

PropsConfig

type
type PropsConfig = Record<string, PropDef>
37

Setup

type
type Setup = (ctx: SetupContext) => Branch | Leaf

A `defineComponent` setup function: receives a `SetupContext`, returns the arbor tree to mount.

38

ShadowMode

type
type ShadowMode = 'light' | 'shadow'

Rendering mode for the custom element — a BINARY choice (DA4 #437).

39

StreamStatus

type
type StreamStatus = 'idle' | 'streaming' | 'done' | 'error'