@ai-digital-factory/state-flow
v0.2.0
Published
React library for flow-style state (states, transitions, and related UI).
Readme
@ai-digital-factory/state-flow
React library for flow-style state (states, transitions, and related UI). See repository CONTEXT.md for domain vocabulary.
Flow authoring (TSX, no React app)
Flow-only projects declare flows as .tsx modules using Flow, State, FinalState, Transition and To, plus ParallelState, Region and FlowList for composition. They do not install react or @types/react.
A Flow is a static graph (ADR 0087). Each Transition lists its allowed destinations as <To> children, each describing when that destination is selected:
import {
FinalState,
Flow,
State,
To,
Transition,
} from "@ai-digital-factory/state-flow";
export default function PlaceOrder() {
return (
<Flow name="place-order" description="One broker order." default="idle">
<State name="idle" description="Not yet submitted.">
<Transition name="submit-order" description="Send the order.">
<To state="placed" description="The broker accepted it." />
<To state="rejected" description="The broker rejected it." />
</Transition>
</State>
<State name="placed" description="Open at the broker.">
<Transition name="record-fill" description="The order filled.">
<To state="filled" description="Always." />
</Transition>
</State>
<FinalState name="rejected" description="Refused by the broker." />
<FinalState name="filled" description="Filled." />
</Flow>
);
}- One
<To>makes a fixed destination; itsonTransitionhandler is optional. Several make the Transition branch; its handler module must exportonTransitionreturning one of the destination names. <FinalState>has no outgoing Transitions and no handlers. Reaching it completes the instance.- Flow check and the
state-flow/flow-moduleESLint rule reject Transitions without a<To>, unknown or repeated destinations, empty To descriptions, Transitions on a FinalState, and graphs that are not static JSX (conditional children,.map(...), computed props or elements built outside the Flow). - The diagram draws a branching Transition as one named segment into a split, with one branch per
<To>labelled by its description. Final states use a distinct colour and aFINALtag.
Composition: compound, parallel and FlowList (ADR 0095, 0096)
Larger lifecycles compose Flows declaratively. Nested States, regions and placed Flows are all part of the static graph; handlers never start child Flows.
<State name="signed-in" description="A user is signed in." default="dashboard">
<State name="dashboard" description="Overview.">
<Transition name="start-upload-batch" description="Upload files.">
<To state="uploading" description="Always." />
</Transition>
</State>
<State name="uploading" description="Each file uploads independently.">
<FlowList name="uploads" flow="file-upload" description="One per file." />
<Transition name="batch-finished" description="All done." trigger="done">
<To state="dashboard" description="Always." />
</Transition>
</State>
<ParallelState name="order-desk" description="Order and watchlist together.">
<Region name="order" description="One order." flow="place-order" />
<Region name="watchlist" description="Prices." default="watching">
<State name="watching" description="Live prices." />
</Region>
</ParallelState>
<Transition name="sign-out" description="Busy while work runs.">
<To state="signed-out" description="Always." />
</Transition>
<Transition
name="force-sign-out"
description="Interrupt work."
childWork="interrupt"
>
<To state="signed-out" description="Always." />
</Transition>
</State>- A
<State>with nested States and adefaultis compound;<State flow="x">places one execution of Flowxfrom the same flows package. A<ParallelState>activates every<Region>and<FlowList>it contains. - Transitions reach only siblings in their own region. Declare a Transition that leaves a containing State on that State; it is available from every nested configuration.
childWork="block"(default) answersbusywhile descendant work is running or unresolved;childWork="interrupt"cancels it and proceeds. Neither undoes an external effect.trigger="done"fires once every region completes, with each region's FinalState (and each FlowList member's) as payload. Completing a child never completes the root.- State and Transition names are unique across the whole flow file. The diagram tags composite States
COMPOUNDorPARALLEL, lists placed Flows and FlowList templates, and brackets nested States under their parent.
In the handlers package, a Flow declares its invocation input in src/<flow-id>/input.ts (export const inputSchema = z.strictObject({...})), read as context.input. A placing parent supplies it for a child, and the application passes it to ensureInstance({ instanceId, input }) for a root instance (ADR 0102). A composite State's handler exports children: { run, recovery }, which receives the entering Transition's payload and returns child inputs and FlowList members ({ uploads: [{ key, input }] }). Generated ChildInputs_<state> and Completion_<state> types describe both.
Migrating from the to prop
The to prop on Transition has been removed. Move the target into a <To> child and describe when it applies:
-<Transition to="present" name="unlock-succeeds" description="Correct credentials." />
+<Transition name="unlock-succeeds" description="Correct credentials.">
+ <To state="present" description="Always." />
+</Transition>Recompile the flows package and its handlers package together: compiled flow artifacts move to schema version 2, generated TransitionContext types expose destinations instead of toState, and callback contexts expose dependencies instead of config. In each handlers package, rename src/<flow-id>/config.ts to dependencies.ts and its Config type to Dependencies; services import Dependencies instead of FlowConfig from the compiled module. Runtime types also lose their Durable prefix (ADR 0089), for example TransitionCallback instead of DurableTransitionCallback. Durable definition identities of flows without branching or final states are unchanged, so existing instances reopen.
The same release (ADRs 0089 and 0090) also renames and removes, without aliases:
@ai-digital-factory/state-flow-chartis now@ai-digital-factory/state-flow-definition; update dependencies and imports.- Presets: extend
@ai-digital-factory/state-flow/tsconfig.flows.jsonand importeslint.flows.config.js(formerlytsconfig.charts.json,eslint.charts.config.js). The ESLint rule isstate-flow/flow-module. - Rule IDs in
state-flow.rules.json:state-chart.*isflow.*,application-hook.*ishandler.*, andtransition.name-unique-across-chartistransition.name-unique-across-flow. - Identifiers drop
Applicationand use Flow/Handler, for exampleFlowDefinition,defineFlow,HandlerContext; data fieldschartName/chartIdareflowName/flowId, and compile output is underdist/flows/. - Services:
await runtime.createFlow(compiled, dependencies),await flow.ensureInstance({ instanceId }), thenwhen(state).transition(name).payload(value).run(handlers)(ADR 0107). The builder.handleFailure(...).run(),runtime.flow,handleCreate/handleOpen/handleEnsure, directwhen(state).dispatchandDispatchError/OperationErrorare gone.
Extend the flow preset (TypeScript requires jsx for .tsx; the preset supplies it):
{
"extends": "@ai-digital-factory/state-flow/tsconfig.flows.json",
"include": ["src/**/*.tsx"]
}Or set the same options yourself, including "types": ["@ai-digital-factory/state-flow/types"] and "jsx": "react-jsx".
Preview and in-app rendering supply React at runtime via the State Flow CLI or your React consumer.
License
Proprietary software. All third-party use requires prior written permission from [email protected]. See LICENSE.
