actrone-memory
    Preparing search index...

    Function langchainChatHistory

    • 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.

      import { HumanMessage, AIMessage } from "@langchain/core/messages";
      const history = langchainChatHistory(mm, { agentId, sessionId }, { human: HumanMessage, ai: AIMessage });

      Parameters

      Returns {
          getMessages(): Promise<LcChatMessage[]>;
          addMessage(message: LcChatMessage): Promise<void>;
          addMessages(messages: LcChatMessage[]): Promise<void>;
          clear(): Promise<void>;
      }