Concepts
Lifecycle
- Build an
ActionRegistryfrom your action registrations. - Check the workflow with
parseActions. A workflow that arrives as JSON takes the same path as one written in code. - Call
dispatchwith the parsed actions, a trigger and the base context. - For each entry in order, the dispatcher finds the actions whose trigger matches and resolves their registrations.
- It enriches the base context with
config,triggerand a cancellationsignal, and callsexecute. Actions in an inner list run concurrently. dispatchreturns oneActionDispatchResultper action that ran.
Registration
An action registration defines one action type. It extends the registry's BaseRegistration, so it has a $type, a Standard Schema schema and metadata, and it adds execute:
import * as z from 'zod/mini';
import type { ActionConfigBase, ActionRegistration } from '@schema-engine/actions';
interface LogConfig extends ActionConfigBase<'log'> {
message: string;
}
export const log: ActionRegistration<LogConfig, { userId: string }, 'submit' | 'change'> = {
$type: 'log',
schema: z.object({
message: z.string(),
}),
metadata: { name: 'Log message', defaultTriggers: ['submit'] },
execute: ({ config, trigger, userId }) => {
console.log(userId, trigger, config.message);
return { success: true };
},
};The schema lists the action's own arguments and nothing else: parseActions checks id, $type, enabled and triggers itself. schema is a plain Standard Schema value, so its type does not tie to LogConfig: the config type is ActionConfigBase plus what you claim the schema returns.
Action entry
An action entry is one object in a workflow, validated against the schema of the registration its $type names.
| Field | Meaning |
|---|---|
id | Identifies this entry. Results carry it as actionId |
$type | The registered action to run |
enabled | false skips the entry. Omitted or true runs it |
triggers | The triggers this entry answers. Omitted, the registration's defaultTriggers apply |
parseActions checks these four keys itself, so a registration's schema never lists them. triggers may be checked against a list of names the domain accepts, passed as { triggers } to parseActions.
Trigger matching
An action runs when its trigger matches:
- The entry defines
triggersand the current trigger is in the list. - Otherwise the registration's
metadata.defaultTriggersincludes the current trigger.
triggers on an entry override the defaults. An entry whose $type has no registration always matches, so dispatch can report the missing handler as a failed result instead of skipping it silently. hasMatching answers whether any entry would run or be reported on a trigger, so a caller can skip building a context for a trigger no action answers.
Context
Callers pass a base context, TBaseContext: the data and services every action of the domain needs. The dispatcher builds the runtime context a handler receives:
execute({
...context,
config: action,
trigger,
signal,
...(triggeredBy ? { triggeredBy } : {}),
});config is the parsed entry, trigger is the trigger of this dispatch, signal is the cancellation signal for this action and triggeredBy is the entry that started a nested dispatch. When the call passes metadata, the dispatcher shallow-merges it into context.metadata first.
Results
A handler returns an ActionExecutionResult: success, and optionally message, error, cause, data and shouldContinue. The dispatcher turns a thrown error into a failed result with shouldContinue: true. It adds actionId, sequenceIndex, groupIndex, status and cancellationRequested and returns the list.
| Field | Meaning |
|---|---|
status | succeeded, failed, or cancelled when an abort error was seen after cancellation was requested |
shouldContinue | false on a failed result stops the rest of the workflow. Omitted or true continues |
See Sequences, parallel groups, cancellation for what a stop does to a group.