Experimental. Batch reducer for use with DeltaChannel.
Processes all writes in one pass — dedup by ID and RemoveMessage
tombstoning — without calling messagesStateReducer.
This reducer is batching-invariant, as required by DeltaChannel:
reducer(reducer(state, xs), ys) === reducer(state, xs.concat(ys)).
A RemoveMessage carrying the REMOVE_ALL_MESSAGES sentinel id
clears all messages accumulated so far (prior state plus earlier writes in
the same batch) and keeps only the messages that follow it, mirroring
messagesStateReducer. Clearing happens in the same single linear
pass, so the batching-invariant still holds.
Raw object / string inputs are coerced to typed BaseMessage objects so
that HTTP-driven graphs work without a separate coercion step, and message
chunks are converted to their non-chunk class. This is not
full messagesStateReducer parity — unknown-id RemoveMessage
errors and missing-id UUID assignment are not handled here.
messagesDeltaReducer(
state: BaseMessage<MessageStructure<MessageToolSet>, MessageType>[],
writes: Messages[]
): BaseMessage<MessageStructure<MessageToolSet>, MessageType>[]| Name | Type | Description |
|---|---|---|
state* | BaseMessage<MessageStructure<MessageToolSet>, MessageType>[] | The current accumulated list of messages. |
writes* | Messages[] | Batch of writes, each a single message-like or an array. |
import { DeltaChannel, messagesDeltaReducer } from "@langchain/langgraph";
const channel = new DeltaChannel(messagesDeltaReducer);