Skip to content

Reducers

Merge the updates of nodes into state fields. See State.

Merge functions

add

add(current, update) -> list[Any]

Reducer that appends the items of a list update to a list.

Parameters
currentAny

The field's value, or None when the field has no value yet.

updateAny

Items to append; a single item must be wrapped in a list.

Returns
list

A new list.

Raises
TypeError

If update is not a list, or current is neither a list nor None.

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.

Parameters
currentAny

The field's value, or None when the field has no value yet.

updateAny

Chat messages and RemoveMessage items.

Returns
list

A new list.

Raises
TypeError

If update is not a list, or holds an item that is neither a chat message nor a RemoveMessage.

StateUpdateError

If two items of update have the same id, or a RemoveMessage names an id that is not in current.

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.

Parameters
idstr

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.

Parameters
valueAny

The field's new value.

Examples

>>> from nodestep import Replace
>>> update = {"messages": Replace([])}