API Reference
Complete API reference for @prototyperai/stream-ui
This is the full API reference for @prototyperai/stream-ui, organized by import path.
Main Entry
import { ... } from "@prototyperai/stream-ui"The main entry re-exports everything from core, catalog, and react, plus:
| Export | Type | Description |
|---|---|---|
prototyperComponents | ComponentRegistry | Pre-built registry of 20 Prototyper UI component renderers |
specToJSX(spec, options?) | Function | Convert a spec to copy-paste JSX code |
Core
import { ... } from "@prototyperai/stream-ui/core"Types
Spec & Elements
Prop
Type
UIElement
Prop
Type
Expression Types
All 8 expression types that can appear in prop values:
| Type | Shape | Description |
|---|---|---|
StateExpression | { $state: string } | Read from global state by JSON Pointer path |
ItemExpression | { $item: string } | Read from current repeat item ("" for whole item) |
IndexExpression | { $index: true } | Current repeat index (zero-based) |
BindStateExpression | { $bindState: string } | Two-way binding to global state path |
BindItemExpression | { $bindItem: string } | Two-way binding to repeat item field |
CondExpression | { $cond, $then, $else? } | Conditional value based on a visibility condition |
ComputedExpression | { $computed: string, args? } | Call a registered computed function |
TemplateExpression | { $template: string } | String interpolation with ${/path} references |
The union type Expression includes all 8. DynamicValue<T> is T | Expression. Convenience aliases: DynamicString, DynamicNumber, DynamicBoolean.
VisibilityCondition
type VisibilityCondition =
| boolean
| SingleCondition
| SingleCondition[] // implicit AND
| { $and: VisibilityCondition[] }
| { $or: VisibilityCondition[] }SingleCondition is StateCondition | ItemCondition | IndexCondition, each supporting comparison operators: eq, neq, gt, gte, lt, lte, not.
RepeatBinding
Prop
Type
ComparisonValue
A numeric comparison value used in visibility conditions. Can be a literal number or a dynamic state reference:
type ComparisonValue = number | { $state: string }Used by the gt, gte, lt, and lte operators on visibility conditions and in the visibility helper functions.
ActionBinding
Prop
Type
ActionOnSuccess
Follow-up action to run on success. One of three variants:
type ActionOnSuccess =
| { navigate: string } // Redirect to a URL
| { set: Record<string, unknown> } // Set state values
| { action: string; params?: Record<string, DynamicValue<unknown>> } // Chain another actionActionOnError
Follow-up action to run on error. One of two variants:
type ActionOnError =
| { set: Record<string, unknown> } // Set state values
| { action: string; params?: Record<string, DynamicValue<unknown>> } // Chain another actionFlatElement
A UIElement enriched with its own key and optional parent key. Useful for tree traversal and debugging:
interface FlatElement extends UIElement {
key: string
parentKey?: string | null
}ActionConfirm
Prop
Type
ValidationCheck
Prop
Type
ValidationConfig
Prop
Type
StateStore
Prop
Type
JsonPatch
Prop
Type
Path Utilities
| Function | Signature | Description |
|---|---|---|
getByPath | (obj: unknown, path: string) => unknown | Get a value by JSON Pointer path |
setByPath | (obj: Record, path: string, value: unknown) => void | Set a value by path (mutating) |
addByPath | (obj: Record, path: string, value: unknown) => void | Add per RFC 6902 semantics (array splice) |
removeByPath | (obj: Record, path: string) => void | Remove per RFC 6902 semantics |
parseJsonPointer | (pointer: string) => string[] | Parse a JSON Pointer into unescaped segments |
Patch Utilities
| Function | Signature | Description |
|---|---|---|
applyPatch | (target: Record, patch: JsonPatch) => void | Apply a single RFC 6902 patch operation |
applyPatches | (target: Record, patches: JsonPatch[]) => void | Apply multiple patches in order |
deepEqual | (a: unknown, b: unknown) => boolean | Deep equality check (used by test operations) |
State
| Function | Signature | Description |
|---|---|---|
createStateStore | (initialState: Record) => StateStore | Create an in-memory state store with change notification |
immutableSetByPath | (obj: Record, path: string, value: unknown) => Record | Immutable set (returns new object) |
flattenToPointers | (obj: Record, prefix?: string, maxDepth?: number) => Record<string, unknown> | Flatten a nested object into a flat map of JSON Pointer paths to values |
Store Adapter
Bridge an external state store (Zustand, Redux, Jotai, etc.) to the StateStore interface:
| Function | Signature | Description |
|---|---|---|
createStoreAdapter | (config: StoreAdapterConfig) => StateStore | Create a StateStore adapter wrapping an external store |
StoreAdapterConfig
Prop
Type
Prop Resolution
| Function | Signature | Description |
|---|---|---|
resolvePropValue | (value: unknown, ctx: PropResolutionContext) => unknown | Resolve a single prop value (handles all 8 expression types) |
resolveElementProps | (props: Record, ctx: PropResolutionContext) => Record | Resolve all props in an element |
resolveBindings | (props: Record, ctx?: PropResolutionContext) => Record<string, string> | Extract $bindState/$bindItem paths from props |
resolveActionParam | (value: unknown, ctx: PropResolutionContext) => unknown | Resolve action param ($item yields path, not value) |
PropResolutionContext
Prop
Type
Type guard functions are exported for each expression type: isStateExpression, isItemExpression, isIndexExpression, isBindStateExpression, isBindItemExpression, isCondExpression, isComputedExpression, isTemplateExpression.
Visibility
| Function | Signature | Description |
|---|---|---|
evaluateVisibility | (condition: VisibilityCondition, ctx: VisibilityContext) => boolean | Evaluate a visibility condition |
evaluateCondition | (condition: SingleCondition, ctx: VisibilityContext) => boolean | Evaluate a single condition |
VisibilityContext
Prop
Type
Type guards: isStateCondition, isItemCondition, isIndexCondition, isAndCondition, isOrCondition, isSingleCondition.
visibility.item
Convenience helpers for building ItemCondition objects inside repeat contexts:
| Method | Signature | Description |
|---|---|---|
visibility.item.when | (field: string) => ItemCondition | Truthy check on an item field |
visibility.item.unless | (field: string) => ItemCondition | Falsy check (not: true) |
visibility.item.eq | (field: string, value: unknown) => ItemCondition | Item field equals value |
visibility.item.neq | (field: string, value: unknown) => ItemCondition | Item field not equal |
visibility.item.gt | (field: string, value: ComparisonValue) => ItemCondition | Greater than |
visibility.item.gte | (field: string, value: ComparisonValue) => ItemCondition | Greater than or equal |
visibility.item.lt | (field: string, value: ComparisonValue) => ItemCondition | Less than |
visibility.item.lte | (field: string, value: ComparisonValue) => ItemCondition | Less than or equal |
visibility.index
Convenience helpers for building IndexCondition objects inside repeat contexts:
| Method | Signature | Description |
|---|---|---|
visibility.index.eq | (value: number) => IndexCondition | Index equals value |
visibility.index.neq | (value: number) => IndexCondition | Index not equal |
visibility.index.gt | (value: number) => IndexCondition | Index greater than |
visibility.index.gte | (value: number) => IndexCondition | Index greater than or equal |
visibility.index.lt | (value: number) => IndexCondition | Index less than |
visibility.index.lte | (value: number) => IndexCondition | Index less than or equal |
Actions
| Function | Signature | Description |
|---|---|---|
resolveAction | (binding: ActionBinding, stateModel: StateModel) => ResolvedAction | Resolve dynamic params and interpolate strings |
executeAction | (resolved: ResolvedAction, handlers: Record, ctx: ActionExecutionContext) => Promise<void> | Execute a resolved action with chaining |
interpolateString | (template: string, stateModel: StateModel) => string | Replace ${/path} references in a string |
builtInActions | Record<string, ActionHandler> | Default handlers: setState, pushState, removeState, toggleState, navigate |
ActionExecutionContext
Prop
Type
Built-in Actions
| Action | Params | Description |
|---|---|---|
setState | { path, value } | Set a value at a state path |
pushState | { path, value } | Append to a state array |
removeState | { path, index } | Remove from a state array by index |
toggleState | { path } | Toggle a boolean at a state path |
navigate | { url } | Navigate via ctx.navigate |
Action Helpers
The actionBinding object provides convenience constructors for building ActionBinding objects:
| Method | Signature | Description |
|---|---|---|
actionBinding.simple | (action: string, params?: Record) => ActionBinding | Simple action with optional params |
actionBinding.withConfirm | (action: string, confirm: ActionConfirm, params?: Record) => ActionBinding | Action with confirmation dialog |
actionBinding.withSuccess | (action: string, onSuccess: ActionOnSuccess, params?: Record) => ActionBinding | Action with success handler |
actionBinding.withError | (action: string, onError: ActionOnError, params?: Record) => ActionBinding | Action with error handler |
actionBinding.setState | (path: string, value: unknown) => ActionBinding | Set a state value |
actionBinding.pushState | (path: string, value: unknown) => ActionBinding | Append to a state array |
actionBinding.removeState | (path: string, index: unknown) => ActionBinding | Remove from a state array by index |
actionBinding.toggleState | (path: string) => ActionBinding | Toggle a boolean |
actionBinding.navigate | (url: string) => ActionBinding | Navigate to a URL |
Check Helpers
The check object provides convenience constructors for building ValidationCheck objects with sensible default messages:
| Method | Signature | Description |
|---|---|---|
check.required | (message?: string) => ValidationCheck | Required field |
check.email | (message?: string) => ValidationCheck | Email format |
check.minLength | (min: number, message?: string) => ValidationCheck | Minimum string/array length |
check.maxLength | (max: number, message?: string) => ValidationCheck | Maximum string/array length |
check.pattern | (pattern: string, message?: string) => ValidationCheck | Regex pattern match |
check.min | (min: number | { $state: string }, message?: string) => ValidationCheck | Minimum numeric value (supports dynamic) |
check.max | (max: number | { $state: string }, message?: string) => ValidationCheck | Maximum numeric value (supports dynamic) |
check.numeric | (message?: string) => ValidationCheck | Must be a number |
check.url | (message?: string) => ValidationCheck | URL format |
check.matches | (statePath: string, message?: string) => ValidationCheck | Must match value at state path |
check.equalTo | (value: unknown, message?: string) => ValidationCheck | Must equal a specific value |
check.lessThan | (statePath: string, message?: string) => ValidationCheck | Must be less than value at state path |
check.greaterThan | (statePath: string, message?: string) => ValidationCheck | Must be greater than value at state path |
check.requiredIf | (fieldPath: string, message?: string) => ValidationCheck | Required when field is truthy |
Validation
| Function | Signature | Description |
|---|---|---|
runValidation | (checks, value, customValidators?, ctx?, evaluateEnabled?) => ValidationResult | Run all checks and collect errors |
runValidationCheck | (check, value, customValidators?, ctx?, evaluateEnabled?) => ValidationCheckResult | Run a single check |
builtInValidators | Record<string, ValidationFunction> | 14 built-in validator functions |
ValidationResult
Prop
Type
Streaming
| Function / Type | Signature | Description |
|---|---|---|
createStreamCompiler | () => StreamCompiler | Create a stateful JSONL stream compiler |
parseStreamLine | (line: string) => JsonPatch | null | Parse a single JSONL line into a patch |
nestedToFlat | (tree: Record) => Spec | Convert a nested element tree to flat spec format |
StreamCompiler
Prop
Type
React
import { ... } from "@prototyperai/stream-ui/react"
// or from the main entry:
import { ... } from "@prototyperai/stream-ui"Renderer
The top-level component that renders a spec.
<Renderer
spec={spec}
registry={registry}
handlers={customHandlers}
functions={computedFunctions}
navigate={(url) => router.push(url)}
onConfirm={(confirm) => window.confirm(confirm.message)}
onStateChange={(state) => console.log(state)}
loading={isStreaming}
/>Prop
Type
StreamUIProvider
Composes all context providers. Use this when you need to render elements manually or access hooks outside the Renderer.
<StreamUIProvider spec={spec} registry={registry} handlers={handlers}>
<ElementRenderer elementKey={spec.root} />
</StreamUIProvider>Props are the same as RendererProps plus children, minus loading.
ElementRenderer
Renders a single element by its key. Must be used inside a StreamUIProvider.
<ElementRenderer elementKey="myElement" loading={false} />ComponentRenderProps
Props passed to each component renderer function:
Prop
Type
Hooks
useUIStream
Connect to a streaming JSONL endpoint:
const { spec, isStreaming, error, send, clear } = useUIStream({
url: "/api/generate-ui",
method: "POST",
autoStart: false,
})Prop
Type
Returns:
Prop
Type
useBoundProp
Two-way binding for a single prop value:
const [value, setValue] = useBoundProp<string>(propValue, bindingPath)When bindingPath is provided and the component is inside a StreamUIProvider, reads/writes go through the state store. Otherwise, falls back to local state.
useStateBinding
Direct two-way binding to a state path:
const [value, setValue] = useStateBinding<number>("/counter")Must be used inside a StreamUIProvider.
useFieldValidation
Client-side field validation tied to a state path:
const { errors, validate, touch, clear } = useFieldValidation("/form/email", {
checks: [
{ validator: "required", message: "Required" },
{ validator: "email", message: "Invalid email" },
],
validateOn: "change",
})See the Validation page for full documentation.
DevTools
StreamUIDevTools
Debug overlay with spec tree, state inspector, and action log.
<StreamUIDevTools defaultOpen={false} actionLog={externalLog} />See the DevTools page for full documentation.
useDevToolsActionLog
Hook for external action logging:
const { log, record, clear } = useDevToolsActionLog()Context Hooks
| Hook | Returns | Description |
|---|---|---|
useStreamUIState() | { store, snapshot } | Access the state store and current snapshot |
useStreamUIStateOptional() | { store, snapshot } | null | Safe version that returns null outside provider |
useStreamUIActions() | ActionContextValue | Access action handlers and execution |
useRepeatScope() | { repeatItem, repeatIndex, repeatBasePath } | null | Access current repeat scope |
useStreamUIFunctions() | Record<string, ComputedFunction> | Access registered computed functions |
useSpecContext() | { spec, registry } | Access the current spec and component registry |
Catalog
import { ... } from "@prototyperai/stream-ui/catalog"Builder Functions
| Function | Signature | Description |
|---|---|---|
defineComponent | (def: ComponentDefinition<T>) => ComponentDefinition<T> | Define a component with type inference |
defineAction | (def: ActionDefinition<T>) => ActionDefinition<T> | Define an action with type inference |
defineCatalog | (config: CatalogConfig) => Catalog | Create a compiled catalog |
defineRegistry | (catalog: Catalog, componentMap: Record<string, R>) => Registry<R> | Wire catalog to React renderers |
Schema Utilities
| Function / Type | Signature | Description |
|---|---|---|
buildSpecZodSchema | (catalog: Catalog) => z.ZodType | Build a Zod schema that validates a complete Spec against a catalog |
validateElementProps | (element: { type, props }, catalog: Catalog) => { valid, errors } | Validate an element's literal props against its component schema (expressions are stripped) |
expressionSchema | z.ZodType | Zod schema matching any of the 8 expression types |
visibilityConditionSchema | z.ZodType | Zod schema matching any visibility condition |
dynamicOf | <T>(baseSchema: z.ZodType<T>) => z.ZodType | Wrap a base schema to also accept expression objects |
Type Inference
Type-level utilities for extracting TypeScript types from catalog definitions:
import type {
InferCatalogComponents,
InferCatalogActions,
InferComponentProps,
InferActionParams,
} from "@prototyperai/stream-ui/catalog"| Type | Description |
|---|---|
InferCatalogComponents<C> | Extract the components map type from a Catalog |
InferCatalogActions<C> | Extract the actions map type from a Catalog |
InferComponentProps<C, K> | Extract the inferred props type for component name K |
InferActionParams<C, K> | Extract the inferred params type for action name K |
Spec Builder
import { createSpecBuilder } from "@prototyperai/stream-ui/catalog"
const builder = createSpecBuilder(catalog)
const { key, element } = builder.element("submitBtn", "Button", { label: "Submit" }, {
on: { press: { action: "submitForm" } },
})
const spec = builder.spec("root", { root: rootElement, submitBtn: element })| Function | Signature | Description |
|---|---|---|
createSpecBuilder | (catalog: Catalog) => SpecBuilder | Create a type-safe spec builder for programmatic spec construction |
Prompt Generation
| Function | Signature | Description |
|---|---|---|
buildSystemPrompt | (catalog: Catalog, options?: PromptOptions) => string | Generate a complete LLM system prompt |
buildUserPrompt | (prompt: string, options?: UserPromptOptions) => string | Wrap a user prompt with optional context |
UserPromptOptions
Prop
Type
PromptOptions
Options for customizing prompt generation:
Prop
Type
PromptTemplate
A function that generates a prompt string from context:
type PromptTemplate = (context: PromptContext) => stringPromptContext
Context passed to a prompt template function:
Prop
Type
CatalogValidationResult
Result of catalog.validate(spec):
Prop
Type
SpecIssue
A single issue found during spec validation:
Prop
Type
See the Catalog page for full documentation.
Codegen
import { specToJSX } from "@prototyperai/stream-ui"
// or
import { specToJSX } from "@prototyperai/stream-ui/codegen"| Function | Signature | Description |
|---|---|---|
specToJSX | (spec: Spec, options?: CodegenOptions) => string | Convert a spec to JSX source code |
CodegenOptions
Prop
Type
See the Code Export page for full documentation.
Components
import { prototyperComponents } from "@prototyperai/stream-ui/components"prototyperComponents is a ComponentRegistry containing renderers for 20 Prototyper UI components:
Button, Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter, Heading, Text, Input, Textarea, Select, Checkbox, Switch, RadioGroup, Tabs, TabsList, TabsTrigger, TabsContent, Dialog, Accordion, Slider, Avatar, Separator, Progress, Tooltip, Badge, Alert.
Each renderer wraps the corresponding Prototyper UI component, mapping the ComponentRenderProps interface to the component's native props, handling two-way bindings, and forwarding events.