Getting Started
The API is split into two layers: what you provide at the call site (the base context) and what the action handler receives at runtime (the base context enriched with config and trigger). You only need to think about the base context when calling the dispatcher.
1. Define Your Base Context and Trigger
type MyTrigger = 'submit' | 'change';
interface BaseContext {
id: string;
data: Record<string, unknown>;
}2. Define the Action Config
interface LogActionConfig extends ActionConfigBase<'log'> {
message: string;
}3. Create the Action Registration
The execute handler receives an ActionRuntimeContext: your BaseContext enriched with config and trigger. You do not need to define that merged type manually — it is inferred from the generics.
import * as z from 'zod/mini';
const logAction: ActionRegistration<LogActionConfig, BaseContext, MyTrigger> = {
$type: 'log',
schema: z.strictObject({
id: z.string(),
$type: z.literal('log'),
message: z.string(),
enabled: z.optional(z.boolean()),
triggers: z.optional(z.array(z.string())),
}),
metadata: {
name: 'Log Action',
defaultTriggers: ['submit'],
},
execute: (context) => {
console.log(context.trigger, context.config.message, context.data);
return { success: true };
},
};4. Register and Execute
const registry = new ActionRegistry<BaseContext, MyTrigger>([logAction]);
const dispatcher = new ActionDispatcher<BaseContext, MyTrigger>(registry);
const baseContext: BaseContext = {
id: 'ctx-1',
data: {},
};
await dispatcher.dispatch({
actions: [
{
id: 'log-1',
$type: 'log',
message: 'Hello world',
},
],
trigger: 'submit',
context: baseContext,
});Note on Typing
There are two distinct context shapes:
BaseContext— what you pass todispatch()ordispatchSync().ActionRuntimeContext<LogActionConfig, BaseContext, MyTrigger>— what the action handler receives, automatically constructed by the dispatcher.
The generics on ActionRegistration and ActionDispatcher are all centered on the base context. The runtime enrichment happens internally.