Agent / foldkit-agent/metakit
machineTools
function · packages/agent/src/metakit.ts:180
function machineTools<
State,
Transitions extends Readonly<Record<string, ServiceFreeTransition>>,
Model,
Principal,
>(
options: {
readonly agent: BoundAgentTypes<Model, Principal>
readonly machine: Machine<State, Transitions>
readonly foreign?: Expr.ReaderOptions['foreign'] | undefined
readonly operations?: ReadonlyArray<OperationDescription> | undefined
} & ([Model] extends [State]
? {
readonly state?: ((model: Model) => State) | undefined
}
: {
readonly state: (model: Model) => State
}),
): MachineTools<Transitions, Model, Principal>A variant for Agent.expose from each transition of a metakit Machine:
- its
inputis the transition's input Schema, and its JSON Schema adds the bounds and domain formats metakit reads from it (Inspect.toJsonSchema); - it is
availableunless the transition's guard isFalsefor the current state (Transition.can). A guard that needs the input to decide, or reads an opaque term, isUnknownand stays available; - a call whose input the guard refuses in that state is refused before
dispatch, as
CapabilityUnavailableError, afterauthorize; - a tool given
authorizeis always available, and its guard is asked only afterauthorizeallows the call: a caller it refuses gets the same list and the same answer whatever the state.
Give the Message it dispatches by its key in expose; update applies the
transition when that Message arrives (machine.update).
ts
const Tools = machineTools({ agent: BankAgent, machine: AccountMachine, state: model => model.account })
BankAgent.expose(Message, { Withdraw: Tools.Withdraw('Withdraw from the account') })