Reducer function for combining two sets of messages in LangGraph's state system.
This reducer handles several tasks:
Normalizes both left and right message inputs to arrays.
Coerces any message-like objects into real BaseMessage instances.
Ensures all messages have unique, stable IDs by generating missing ones.
If a RemoveMessage instance is encountered in right with the ID REMOVE_ALL_MESSAGES,
all previous messages are discarded and only the subsequent messages in right are returned.
Otherwise, merges left and right messages together following these rules:
If a message in right shares an ID with a message in left:
If it is a RemoveMessage, that message (by ID) is marked for removal.
If it is a normal message, it replaces the message with the same ID from left.
If a message in rightdoes not exist in left:
If it is a RemoveMessage, this is considered an error (cannot remove non-existent ID).
Otherwise, the message is appended.
Messages flagged for removal are omitted from the final output.
Reducer function for combining two sets of messages in LangGraph's state system.
This reducer handles several tasks:
leftandrightmessage inputs to arrays.BaseMessageinstances.RemoveMessageinstance is encountered inrightwith the IDREMOVE_ALL_MESSAGES, all previous messages are discarded and only the subsequent messages inrightare returned.leftandrightmessages together following these rules:rightshares an ID with a message inleft:RemoveMessage, that message (by ID) is marked for removal.left.rightdoes not exist inleft:RemoveMessage, this is considered an error (cannot remove non-existent ID).