{"version":3,"file":"buffer_window_memory.cjs","names":["BaseChatMemory"],"sources":["../../src/memory/buffer_window_memory.ts"],"sourcesContent":["import { InputValues, MemoryVariables } from \"@langchain/core/memory\";\nimport { getBufferString } from \"@langchain/core/messages\";\nimport { BaseChatMemory, BaseChatMemoryInput } from \"./chat_memory.js\";\n\n/**\n * Interface for the input parameters of the BufferWindowMemory class.\n */\nexport interface BufferWindowMemoryInput extends BaseChatMemoryInput {\n  humanPrefix?: string;\n  aiPrefix?: string;\n  memoryKey?: string;\n  k?: number;\n}\n\n/**\n * Class for managing and storing previous chat messages. It extends the\n * BaseChatMemory class and implements the BufferWindowMemoryInput\n * interface. This class is stateful and stores messages in a buffer. When\n * called in a chain, it returns all of the messages it has stored.\n * @example\n * ```typescript\n * const prompt =\n *   PromptTemplate.fromTemplate(`The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n * Current conversation:\n * {chat_history}\n * Human: {input}\n * AI:`);\n *\n * const chain = new LLMChain({\n *   llm: new ChatOpenAI({ model: \"gpt-4o-mini\", temperature: 0.9 }),\n *   prompt,\n *   memory: new BufferWindowMemory({ memoryKey: \"chat_history\", k: 1 }),\n * });\n *\n * // Example of initiating a conversation with the AI\n * const res1 = await chain.call({ input: \"Hi! I'm Jim.\" });\n * console.log({ res1 });\n *\n * // Example of following up with another question\n * const res2 = await chain.call({ input: \"What's my name?\" });\n * console.log({ res2 });\n * ```\n */\nexport class BufferWindowMemory\n  extends BaseChatMemory\n  implements BufferWindowMemoryInput\n{\n  humanPrefix = \"Human\";\n\n  aiPrefix = \"AI\";\n\n  memoryKey = \"history\";\n\n  k = 5;\n\n  constructor(fields?: BufferWindowMemoryInput) {\n    super({\n      returnMessages: fields?.returnMessages ?? false,\n      chatHistory: fields?.chatHistory,\n      inputKey: fields?.inputKey,\n      outputKey: fields?.outputKey,\n    });\n    this.humanPrefix = fields?.humanPrefix ?? this.humanPrefix;\n    this.aiPrefix = fields?.aiPrefix ?? this.aiPrefix;\n    this.memoryKey = fields?.memoryKey ?? this.memoryKey;\n    this.k = fields?.k ?? this.k;\n  }\n\n  get memoryKeys() {\n    return [this.memoryKey];\n  }\n\n  /**\n   * Method to load the memory variables. Retrieves the chat messages from\n   * the history, slices the last 'k' messages, and stores them in the\n   * memory under the memoryKey. If the returnMessages property is set to\n   * true, the method returns the messages as they are. Otherwise, it\n   * returns a string representation of the messages.\n   * @param _values InputValues object.\n   * @returns Promise that resolves to a MemoryVariables object.\n   */\n  async loadMemoryVariables(_values: InputValues): Promise<MemoryVariables> {\n    const messages = await this.chatHistory.getMessages();\n    if (this.returnMessages) {\n      const result = {\n        [this.memoryKey]: messages.slice(-this.k * 2),\n      };\n      return result;\n    }\n    const result = {\n      [this.memoryKey]: getBufferString(\n        messages.slice(-this.k * 2),\n        this.humanPrefix,\n        this.aiPrefix\n      ),\n    };\n    return result;\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,IAAa,qBAAb,cACUA,2BAAAA,eAEV;CACE,cAAc;CAEd,WAAW;CAEX,YAAY;CAEZ,IAAI;CAEJ,YAAY,QAAkC;AAC5C,QAAM;GACJ,gBAAgB,QAAQ,kBAAkB;GAC1C,aAAa,QAAQ;GACrB,UAAU,QAAQ;GAClB,WAAW,QAAQ;GACpB,CAAC;AACF,OAAK,cAAc,QAAQ,eAAe,KAAK;AAC/C,OAAK,WAAW,QAAQ,YAAY,KAAK;AACzC,OAAK,YAAY,QAAQ,aAAa,KAAK;AAC3C,OAAK,IAAI,QAAQ,KAAK,KAAK;;CAG7B,IAAI,aAAa;AACf,SAAO,CAAC,KAAK,UAAU;;;;;;;;;;;CAYzB,MAAM,oBAAoB,SAAgD;EACxE,MAAM,WAAW,MAAM,KAAK,YAAY,aAAa;AACrD,MAAI,KAAK,eAIP,QAHe,GACZ,KAAK,YAAY,SAAS,MAAM,CAAC,KAAK,IAAI,EAAE,EAC9C;AAUH,SAPe,GACZ,KAAK,aAAA,GAAA,yBAAA,iBACJ,SAAS,MAAM,CAAC,KAAK,IAAI,EAAE,EAC3B,KAAK,aACL,KAAK,SACN,EACF"}