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 input is the transition's input Schema, and its JSON Schema adds the bounds and domain formats metakit reads from it (Inspect.toJsonSchema);
  • it is available unless the transition's guard is False for the current state (Transition.can). A guard that needs the input to decide, or reads an opaque term, is Unknown and stays available;
  • a call whose input the guard refuses in that state is refused before dispatch, as CapabilityUnavailableError, after authorize;
  • a tool given authorize is always available, and its guard is asked only after authorize allows 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') })