Defines the structure of callbacks and provides utilities for executing them.
See LangChain.Chains.ChainCallbacks for the list of callbacks that can be
used.
Summary
Functions
Fire a named callback with the list of arguments to pass. Takes a list of callback handlers and will execute the callback for each handler that defines a handler function for it.
Fold a decision-returning callback across the attached handler maps.
Functions
Fire a named callback with the list of arguments to pass. Takes a list of callback handlers and will execute the callback for each handler that defines a handler function for it.
@spec reduce([map()], atom(), acc, (([any()] -> any()), acc -> {:cont, acc} | {:halt, acc})) :: acc when acc: term()
Fold a decision-returning callback across the attached handler maps.
Where fire/3 discards what a handler returns, this collects it. A handler
map that does not define callback_name is skipped without consulting the
reducer.
reducer receives two arguments: an invoke function that applies the handler
to a list of arguments, and the current accumulator. It returns {:cont, acc}
to consult the next handler or {:halt, acc} to stop. Passing invoke rather
than the raw handler lets the reducer choose the arguments per handler, so a
handler can be shown what an earlier one changed, while the exception wrapping
stays here.
This is the primitive the module is built on. fire/3 is this function with
the same arguments given to every handler and nothing accumulated.
Example
Callbacks.reduce(callbacks, :on_thing_reviewed, :allowed, fn invoke, acc ->
case invoke.([subject]) do
:ok -> {:cont, acc}
{:denied, _reason} = denial -> {:halt, denial}
end
end)