API / @aihu/plugin

@aihu/plugin

Plugins

Plugin substrate shared by @aihu/server and the meta-framework — runtime hook surface.

version
0.1.0
exports
36
values
5
types
31
01

definePlugin

function
function definePlugin(config: PluginConfig): Plugin

Define a aihu plugin.

02

resetValidationState

function
function resetValidationState(): void

Reset the duplicate-namespace tracker.

03

validatePlugin

function
function validatePlugin(plugin: Plugin): ValidationResult

Validate a plugin per spec §8.1.

04

AIHU_VERSION

const
const AIHU_VERSION

Current aihu framework version against which `aihuVersion` ranges are checked.

05

RESERVED_NAMESPACES

const
const RESERVED_NAMESPACES: ReadonlyArray<string>

Reserved plugin namespaces per spec §1.3.

06

BlockContext

interface
interface BlockContext extends SfcContext {
  readonly blockType: string
  readonly blockName: string
}

Per-block context provided to `transformBlock` hooks and macro lowering (spec §4.2, §5.2).

07

BuildContext

interface
interface BuildContext {
  /** Resolved plugin configuration (per spec §3). */
  readonly config: Readonly<Record<string, unknown>>
  readonly mode: BuildMode
  readonly outputDir: string
  readonly projectRoot: string
}

Build-time context provided to `beforeCompile` hooks (spec §4.2).

08

ConfigFieldSpec

interface
interface ConfigFieldSpec {
  readonly type: ConfigFieldType
  readonly default?: unknown
  readonly description?: string
}
09

Contributes

interface
interface Contributes {
  readonly blocks?: ReadonlyArray<string>
  readonly macros?: ReadonlyArray<Macro>
  readonly components?: ReadonlyArray<string>
  readonly transforms?: ReadonlyArray<Transform>
  /** Server-only runtime helpers (spec §6.5.1). */
  readonly serverRuntime?: ServerRuntime
  /** Server middleware (spec §6.5.3, provisional). */
  readonly middleware?: ReadonlyArray<Middleware>
}

Plugin contributions (spec §2 + §6.5).

10

Hooks

interface
interface Hooks {
  readonly beforeCompile?: BeforeCompileHook
  readonly afterParse?: AfterParseHook
  readonly transformBlock?: TransformBlockHook
  readonly afterCompile?: AfterCompileHook
}
11

ImportSpec

interface
interface ImportSpec {
  readonly from: string
  readonly names: ReadonlyArray<string>
}
12

LoweringResult

interface
interface LoweringResult {
  readonly code: string
  readonly imports?: ReadonlyArray<ImportSpec>
  readonly hoist?: ReadonlyArray<string>
  /**
   * Explicit emission target per Block Structure Spec §11.5. When omitted,
   * the compiler infers based on enclosing context.
   */
  readonly target?: BuildTarget
}

Complex emission result for macros that emit multiple statements or require imports/hoisted declarations (spec §5.3).

13

Macro

interface
interface Macro {
  readonly name: string
  readonly validIn: ReadonlyArray<string>
  readonly lowering: MacroLowering
  readonly validation?: MacroValidation
  /**
   * When true, the macro emits server-only output (per spec §6.5.2): the
   * lowered output goes to the server artifact and is elided from the client
   * bundle. (The `$server` macro that this once mirrored has been retired —
   * see Macro Vocabulary spec §2.12.)
   */
  readonly serverOnly?: boolean
}

Macro contribution declaration (spec §2.2).

14

MacroContext

interface
interface MacroContext {
  readonly pluginConfig: Readonly<Record<string, unknown>>
  readonly sfc: SfcContext
  readonly block: BlockContext
  /** Request an import. Returns the local name to use in emitted code. */
  readonly imports: (spec: string) => string
  /** Request a runtime helper. Returns the local name to use in emitted code. */
  readonly runtime: (name: string) => string
}

Macro lowering context (spec §5.2).

15

Middleware

interface
interface Middleware {
  readonly name: string
  readonly stage: MiddlewareStage
  /** Path to the middleware handler module, relative to the plugin root. */
  readonly handler: string
}
16

Plugin

interface
interface Plugin extends PluginConfig {
  /** Brand to distinguish constructed plugins from raw config objects. */
  readonly __aihu_plugin: true
}

Plugin instance — the registered, validated plugin.

17

PluginConfig

interface
interface PluginConfig {
  readonly name: string
  readonly version: string
  readonly namespace: string
  /** Compatible aihu versions (semver range). Checked at registration (spec §7.3). */
  readonly aihuVersion?: string
  readonly configSchema?: ConfigSchema
  readonly contributes?: Contributes
  readonly hooks?: Hooks
  readonly parsers?: Readonly<Record<string, BlockParser>>
  /** Other plugin namespaces this plugin depends on (spec §10.1). */
  readonly dependencies?: ReadonlyArray<string>
  /**
   * If `true`, this plugin's contributions are lowered to the server bundle
   * only (Amendment 03 §6.5). Client code that references server-only
   * contributions receives RPC stubs instead of the real implementation.
   * PROVISIONAL in v1.0 — behaviour may evolve through the v1.x series.
   */
  readonly serverOnly?: boolean
}

Plugin definition input — what authors pass to `definePlugin`.

18

SfcContext

interface
interface SfcContext extends BuildContext {
  readonly sfcPath: string
  readonly componentName: string
  /**
   * Symbol table contents (per Block Structure Spec §11.3). Opaque shape
   * here; the compiler defines exact contents.
   */
  readonly symbolTable: Readonly<Record<string, unknown>>
}

Per-SFC compilation context provided to `afterParse` and `afterCompile` hooks (spec §4.2).

19

Transform

interface
interface Transform {
  readonly stage: TransformStage
  readonly fn: (input: unknown) => unknown
}
20

ValidationError

interface
interface ValidationError {
  /** Stable error code corresponding to a row in spec §8.1 / §8.2. */
  readonly code:
    | 'invalid-namespace'
    | 'reserved-namespace'
    | 'duplicate-namespace'
    | 'missing-required-field'
    | 'aihu-version-mismatch'
  readonly message: string
}
21

AfterCompileHook

type
type AfterCompileHook = (
  ctx: SfcContext,
  output: unknown,
) => Promise<unknown | undefined> | unknown | undefined
22

AfterParseHook

type
type AfterParseHook = (
  ctx: SfcContext,
  ast: unknown,
) => Promise<unknown | undefined> | unknown | undefined
23

BeforeCompileHook

type
type BeforeCompileHook = (ctx: BuildContext) => Promise<void> | void
24

BlockParser

type
type BlockParser = (body: string, ctx: BlockContext) => unknown

Block parser signature (spec §2.1).

25

BuildMode

type
type BuildMode = 'dev' | 'build'

Build mode (dev vs production build).

26

BuildTarget

type
type BuildTarget = 'client' | 'server' | 'universal'

Build target semantics per Block Structure Spec §11.5.

27

ConfigFieldType

type
type ConfigFieldType = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'function'
28

ConfigSchema

type
type ConfigSchema = Readonly<Record<string, ConfigFieldSpec>>
29

MacroArgs

type
type MacroArgs = Readonly<Record<string, unknown>>

Macro arguments — opaque shape; per-macro semantics.

30

MacroLowering

type
type MacroLowering = (ctx: MacroContext, args: MacroArgs) => string | LoweringResult

Macro lowering function signature (spec §5.1).

31

MacroValidation

type
type MacroValidation = (
  ctx: { readonly error: (msg: string) => void },
  args: MacroArgs,
) => void

Macro validation function signature (spec §5.4).

32

MiddlewareStage

type
type MiddlewareStage = 'before-handler' | 'after-handler' | 'on-error'

Server middleware contribution (spec §6.5.3).

33

ServerRuntime

type
type ServerRuntime = Readonly<Record<string, string>>

Server-only runtime helper map (spec §6.5.1).

34

TransformBlockHook

type
type TransformBlockHook = (
  ctx: BlockContext,
  block: unknown,
) => Promise<unknown | undefined> | unknown | undefined
35

TransformStage

type
type TransformStage = 'after-parse' | 'before-lower' | 'after-lower'

Transform stages (spec §2.4).

36

ValidationResult

type
type ValidationResult =
  | { readonly ok: true }
  | { readonly ok: false; readonly errors: ReadonlyArray<ValidationError> }