Reducers¶
Merge the updates of nodes into state fields. See State.
| Name | What it is |
|---|---|
| Merge functions | |
add | Reducer that appends the items of a list update to a list. |
add_messages | Reducer that appends chat messages and edits or removes them by id. |
merge_dict | Reducer that merges the top-level keys of a dict update into a dict. |
replace | Reducer that keeps the new value. |
| Update values | |
RemoveMessage | An add_messages update item that removes the message with id. |
Replace | An update value that sets a field directly, bypassing its reducer. |
Merge functions¶
add¶
add(current, update) -> list[Any]
Reducer that appends the items of a list update to a list.
current | Any |
The field's value, or None when the field has no value yet. |
update | Any |
Items to append; a single item must be wrapped in a list. |
list |
A new list. |
TypeError |
If |
add_messages¶
add_messages(current, update) -> list[Any]
Reducer that appends chat messages and edits or removes them by id.
A message whose id is already in the list replaces that message in place,
a RemoveMessage removes the message with its id, and every other
message is appended. Messages without an id get a new one.
current | Any |
The field's value, or None when the field has no value yet. |
update | Any |
Chat messages and |
list |
A new list. |
TypeError |
If |
StateUpdateError |
If two items of |
merge_dict¶
merge_dict(current, update) -> dict[Any, Any]
Reducer that merges the top-level keys of a dict update into a dict.
replace¶
replace(current, update) -> Any
Reducer that keeps the new value.
Update values¶
RemoveMessage¶
RemoveMessage(id)
An add_messages update item that removes the message with id.
Works in node updates, Command.update, Graph.update_state and
input on an existing thread.
id | str |
Id of the message to remove. |
Examples
>>> from nodestep import RemoveMessage
>>> update = {"messages": [RemoveMessage("m1")]}
Replace¶
Replace(value)
An update value that sets a field directly, bypassing its reducer.
Works in node updates, Command.update, Graph.update_state and run
input, not in Send payloads.
value | Any |
The field's new value. |
Examples
>>> from nodestep import Replace
>>> update = {"messages": Replace([])}