@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 5.22 kB
Source Map (JSON)
{"version":3,"file":"chat_history.cjs","names":["Serializable","HumanMessage","AIMessage"],"sources":["../src/chat_history.ts"],"sourcesContent":["import { Serializable } from \"./load/serializable.js\";\nimport { type BaseMessage, HumanMessage, AIMessage } from \"./messages/index.js\";\n\n// TODO: Combine into one class for 0.2\n\n/**\n * Base class for all chat message histories. All chat message histories\n * should extend this class.\n */\nexport abstract class BaseChatMessageHistory extends Serializable {\n public abstract getMessages(): Promise<BaseMessage[]>;\n\n public abstract addMessage(message: BaseMessage): Promise<void>;\n\n public abstract addUserMessage(message: string): Promise<void>;\n\n public abstract addAIMessage(message: string): Promise<void>;\n\n /**\n * Add a list of messages.\n *\n * Implementations should override this method to handle bulk addition of messages\n * in an efficient manner to avoid unnecessary round-trips to the underlying store.\n *\n * @param messages - A list of BaseMessage objects to store.\n */\n public async addMessages(messages: BaseMessage[]): Promise<void> {\n for (const message of messages) {\n await this.addMessage(message);\n }\n }\n\n public abstract clear(): Promise<void>;\n}\n\n/**\n * Base class for all list chat message histories. All list chat message\n * histories should extend this class.\n */\nexport abstract class BaseListChatMessageHistory extends Serializable {\n /** Returns a list of messages stored in the store. */\n public abstract getMessages(): Promise<BaseMessage[]>;\n\n /**\n * Add a message object to the store.\n */\n public abstract addMessage(message: BaseMessage): Promise<void>;\n\n /**\n * This is a convenience method for adding a human message string to the store.\n * Please note that this is a convenience method. Code should favor the\n * bulk addMessages interface instead to save on round-trips to the underlying\n * persistence layer.\n * This method may be deprecated in a future release.\n */\n public addUserMessage(message: string): Promise<void> {\n return this.addMessage(new HumanMessage(message));\n }\n\n /**\n * This is a convenience method for adding an AI message string to the store.\n * Please note that this is a convenience method. Code should favor the bulk\n * addMessages interface instead to save on round-trips to the underlying\n * persistence layer.\n * This method may be deprecated in a future release.\n */\n public addAIMessage(message: string): Promise<void> {\n return this.addMessage(new AIMessage(message));\n }\n\n /**\n * Add a list of messages.\n *\n * Implementations should override this method to handle bulk addition of messages\n * in an efficient manner to avoid unnecessary round-trips to the underlying store.\n *\n * @param messages - A list of BaseMessage objects to store.\n */\n public async addMessages(messages: BaseMessage[]): Promise<void> {\n for (const message of messages) {\n await this.addMessage(message);\n }\n }\n\n /**\n * Remove all messages from the store.\n */\n public clear(): Promise<void> {\n throw new Error(\"Not implemented.\");\n }\n}\n\n/**\n * Class for storing chat message history in-memory. It extends the\n * BaseListChatMessageHistory class and provides methods to get, add, and\n * clear messages.\n */\nexport class InMemoryChatMessageHistory extends BaseListChatMessageHistory {\n lc_namespace = [\"langchain\", \"stores\", \"message\", \"in_memory\"];\n\n private messages: BaseMessage[] = [];\n\n constructor(messages?: BaseMessage[]) {\n super(...arguments);\n this.messages = messages ?? [];\n }\n\n /**\n * Method to get all the messages stored in the ChatMessageHistory\n * instance.\n * @returns Array of stored BaseMessage instances.\n */\n async getMessages(): Promise<BaseMessage[]> {\n return this.messages;\n }\n\n /**\n * Method to add a new message to the ChatMessageHistory instance.\n * @param message The BaseMessage instance to add.\n * @returns A promise that resolves when the message has been added.\n */\n async addMessage(message: BaseMessage) {\n this.messages.push(message);\n }\n\n /**\n * Method to clear all the messages from the ChatMessageHistory instance.\n * @returns A promise that resolves when all messages have been cleared.\n */\n async clear() {\n this.messages = [];\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AASA,IAAsB,yBAAtB,cAAqDA,uCAAa;;;;;;;;;CAiBhE,MAAa,YAAY,UAAwC;AAC/D,OAAK,MAAM,WAAW,SACpB,OAAM,KAAK,WAAW,QAAQ;;;;;;;AAWpC,IAAsB,6BAAtB,cAAyDA,uCAAa;;;;;;;;CAgBpE,AAAO,eAAe,SAAgC;AACpD,SAAO,KAAK,WAAW,IAAIC,2BAAa,QAAQ,CAAC;;;;;;;;;CAUnD,AAAO,aAAa,SAAgC;AAClD,SAAO,KAAK,WAAW,IAAIC,qBAAU,QAAQ,CAAC;;;;;;;;;;CAWhD,MAAa,YAAY,UAAwC;AAC/D,OAAK,MAAM,WAAW,SACpB,OAAM,KAAK,WAAW,QAAQ;;;;;CAOlC,AAAO,QAAuB;AAC5B,QAAM,IAAI,MAAM,mBAAmB;;;;;;;;AASvC,IAAa,6BAAb,cAAgD,2BAA2B;CACzE,eAAe;EAAC;EAAa;EAAU;EAAW;EAAY;CAE9D,AAAQ,WAA0B,EAAE;CAEpC,YAAY,UAA0B;AACpC,QAAM,GAAG,UAAU;AACnB,OAAK,WAAW,YAAY,EAAE;;;;;;;CAQhC,MAAM,cAAsC;AAC1C,SAAO,KAAK;;;;;;;CAQd,MAAM,WAAW,SAAsB;AACrC,OAAK,SAAS,KAAK,QAAQ;;;;;;CAO7B,MAAM,QAAQ;AACZ,OAAK,WAAW,EAAE"}