API Reference
ActionRegistry
class ActionRegistry<TBaseContext, TTrigger>
implements
ReadOnlyRegistry<ActionRegistration<ActionConfigBase, TBaseContext, TTrigger>>,
ActionRegistryProvider<TBaseContext, TTrigger>
{
constructor(registrations: readonly ActionRegistration<ActionConfigBase, TBaseContext, TTrigger>[]);
readonly kind: string;
readonly size: number;
get(key: string): ActionRegistration<ActionConfigBase, TBaseContext, TTrigger> | undefined;
has(key: string): boolean;
all(): readonly ActionRegistration<ActionConfigBase, TBaseContext, TTrigger>[];
keys(): readonly string[];
getAction(type: string): ActionRegistration<ActionConfigBase, TBaseContext, TTrigger> | undefined;
getActionTypes(): string[];
}ActionRegistry is built from a list with createRegistry from @schema-engine/registry. It is read-only and frozen. A later registration with the same $type replaces an earlier one without a warning, and a winning registration whose schema is not a Standard Schema throws at construction, naming its $type. Subclasses may add their own fields. There is no register or clear; to register at runtime, implement ActionRegistryProvider yourself.
ActionDispatcher
class ActionDispatcher<TBaseContext = unknown, TTrigger = string> {
constructor(registry: ActionRegistryProvider<TBaseContext, TTrigger>);
dispatch<TConfig extends ActionConfigBase>(
input: ActionDispatchInput<TConfig, TBaseContext, TTrigger>,
): Promise<ActionDispatchResult[]>;
dispatchSync<TConfig extends ActionConfigBase>(input: ActionDispatchInput<TConfig, TBaseContext, TTrigger>): void;
}When metadata is provided in the input, dispatch() shallow-merges it into context.metadata before execution.
ActionRegistryProvider
Available from the package root:
interface ActionRegistryProvider<
TBaseContext = unknown,
TTrigger = string,
TRegistration extends ActionRegistration<ActionConfigBase, TBaseContext, TTrigger> = ActionRegistration<
ActionConfigBase,
TBaseContext,
TTrigger
>,
> {
getAction(type: string): TRegistration | undefined;
getActionTypes(): string[];
}ActionRegistration
import type { StandardSchemaV1 } from '@standard-schema/spec';
interface ActionRegistration<
TConfig extends ActionConfigBase = ActionConfigBase,
TBaseContext = unknown,
TTrigger = string,
TData = unknown,
> extends BaseRegistration<TConfig, ActionMetadata<TTrigger>> {
$type: TConfig['$type'];
schema: StandardSchemaV1;
metadata: ActionMetadata<TTrigger>;
execute: ActionHandler<TConfig, TBaseContext, TTrigger, TData>;
}Zod Mini, Zod, Valibot, and Effect Schema values implement Standard Schema and remain valid schema values.
execute receives an ActionRuntimeContext — the caller-provided TBaseContext enriched with config, trigger, and signal by the dispatcher.
ActionRuntimeContext
type ActionRuntimeContext<TConfig = unknown, TBaseContext = unknown, TTrigger = string> = TBaseContext & {
config: TConfig;
trigger: TTrigger;
triggeredBy?: TConfig;
signal: AbortSignal;
};The shape that action handlers receive at execution time. The dispatcher creates this by spreading the caller-provided base context with the current config, trigger, and signal, and includes triggeredBy for nested dispatches.
ActionDispatchInput
Object-based input for ActionDispatcher.dispatch() and dispatchSync().
interface ActionDispatchInput<
TConfig extends ActionConfigBase = ActionConfigBase,
TBaseContext = unknown,
TTrigger = string,
> {
trigger: TTrigger;
actions: readonly (TConfig | readonly TConfig[])[];
context: TBaseContext;
triggeredBy?: TConfig;
metadata?: Record<string, unknown>;
}ActionMetadata
interface ActionMetadata<TTrigger = string> extends RegistrationMetadata {
icon?: string;
defaultTriggers?: readonly TTrigger[];
category?: string;
}ActionConfigBase
interface ActionConfigBase<TType extends string = string> {
id: string;
$type: TType;
enabled?: boolean;
triggers?: readonly string[];
}ActionExecutionResult
interface ActionExecutionResult<TData = unknown> {
success: boolean;
message?: string;
error?: string;
cause?: unknown;
data?: TData;
shouldContinue?: boolean;
}ActionDispatchResult
dispatch returns one result per executed action in configuration order. In addition
to the handler's ActionExecutionResult, each result has:
type ActionDispatchResult<TData = unknown> = ActionExecutionResult<TData> & {
actionId: string;
sequenceIndex: number;
groupIndex?: number;
status: 'succeeded' | 'failed' | 'cancelled';
cancellationRequested: boolean;
};Indexes refer to the original action sequence and, for a group member, its index
inside that group. Disabled or nonmatching actions retain their positions in the
input, even though they produce no result. cancelled means an abort error was
observed after cancellation was requested; the remote side effect may still have
occurred. Direct callers constructing ActionRuntimeContext must provide signal.
defineAction
A factory helper for creating strongly typed action registrations scoped to one domain:
function defineAction<TBaseContext, TTrigger = string>(): <TConfig extends ActionConfigBase, TData = unknown>(
registration: ActionRegistration<TConfig, TBaseContext, TTrigger, TData>,
) => ActionRegistration<TConfig, TBaseContext, TTrigger, TData>;Usage:
const define = defineAction<AppContext, AppTrigger>();
const myAction = define({
$type: 'my-action',
schema: mySchema,
metadata: { name: 'My Action' },
execute: async (context) => {
return { success: true };
},
});