xmachines-machine
The contract of a typed machine: one XState v6 machine, usually generated by xmachines-build, whose guards, actions, delays and invoked actors are Rust compiled into a guest of its own, a WASI component (xmachines-machine-wasi) or a Node.js addon (xmachines-machine-node).
When to use it
A machine crate implements TypedMachine once; each guest crate's export_machine! turns it into that guest's exports, so the same implementation builds a WASI component and a Node.js addon. Use this crate directly when you write that implementation, or a guest for another target.
What the guests share does not depend on the target, so it is here too: the machine compiled once per guest (compiled), its id and version (info), and JsonMachine and JsonActor, the machine and actor resources of the typed world xmachines:machine with values as JSON text. Event inputs are deserialized into the machine's Event type at the boundary; a chunk with any other is refused (WORKFLOW_INVALID_INPUT).
Example
use ;
use ;
use ;
;
With codegen, Context and Event are the generated types, and DEFINITION, SCHEMAS and machine() are the generated module's (examples/checkout-wasi).
Contract
TypedMachine is the Rust side of the WIT package xmachines:machine@0.3.0 (wit/machine.wit of xmachines-machine-wasi): its info interface reports id, version, DEFINITION and SCHEMAS, JsonMachine implements its machine resource and JsonActor its actor resource. JsonMachine::load takes load options { "maxSteps"?, "maxTimers"?, "observeSystemEmitted"? } (a typed machine compiles its children and actions in, so it takes no children or hostActions; options that do not parse are the contract's WORKFLOW_INVALID_INPUT, a machine that does not compile WORKFLOW_INVALID_MACHINE), start takes { "input"?, "id"? } (the root's id root unless it names one; a root that fails as it starts is the contract's WORKFLOW_MACHINE_FAILED, and no actor) and restore the snapshot alone (one that does not restore is WORKFLOW_INVALID_SNAPSHOT); JsonActor::apply takes the contract's chunks and gives its steps and apply errors, a rejection { "rejected": { "message", "data"? } } or a fault { "code", "message" }, the JSON that the packages' JSON Schemas describe.
A typed machine cannot declare host actions: TypedMachine and the code xmachines-build generates have no hostActions, so every custom action runs inside the guest and no step has work of kind action. A machine whose host does some of its actions runs on the generic runtime.
Related crates
xmachines-machine-wasi:export_machine!for a WASI component.xmachines-machine-node:export_machine!for a Node.js addon.xmachines-runtime: the runtime, andhost, the option and chunk parsing every host in another language shares.
License
MIT