LangChain.js: a governed BaseListChatMessageHistory implementation. Drop it
in anywhere LangChain persists chat history (e.g. RunnableWithMessageHistory),
getMessages reads the recent turns, and addMessage(s) pairs a human message
with the following AI message into one governed turn (clear clears the session).
Pass messageClasses to emit real HumanMessage/AIMessage instances (recommended for
consumers that call BaseMessage methods); without it, structural { _getType, content }
messages are returned (fine for reads). This returns a plain object that duck-types the
interface: it is not a BaseListChatMessageHistory subclass, so passing it to APIs typed
to BaseChatMessageHistory (e.g. RunnableWithMessageHistory) needs a cast:
langchainChatHistory(...) as unknown as BaseChatMessageHistory.
LangChain.js: a governed
BaseListChatMessageHistoryimplementation. Drop it in anywhere LangChain persists chat history (e.g.RunnableWithMessageHistory),getMessagesreads the recent turns, andaddMessage(s)pairs a human message with the following AI message into one governed turn (clearclears the session). PassmessageClassesto emit realHumanMessage/AIMessageinstances (recommended for consumers that callBaseMessagemethods); without it, structural{ _getType, content }messages are returned (fine for reads). This returns a plain object that duck-types the interface: it is not aBaseListChatMessageHistorysubclass, so passing it to APIs typed toBaseChatMessageHistory(e.g.RunnableWithMessageHistory) needs a cast:langchainChatHistory(...) as unknown as BaseChatMessageHistory.