{"version":3,"file":"buffer_token_memory.cjs","names":["BaseChatMemory"],"sources":["../../src/memory/buffer_token_memory.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport {\n  InputValues,\n  MemoryVariables,\n  OutputValues,\n} 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 `BufferTokenMemory` class.\n */\n\nexport interface ConversationTokenBufferMemoryInput extends BaseChatMemoryInput {\n  /* Prefix for human messages in the buffer. */\n  humanPrefix?: string;\n\n  /* Prefix for AI messages in the buffer. */\n  aiPrefix?: string;\n\n  /* The LLM for this instance. */\n  llm: BaseLanguageModelInterface;\n\n  /* Memory key for buffer instance. */\n  memoryKey?: string;\n\n  /* Maximmum number of tokens allowed in the buffer. */\n  maxTokenLimit?: number;\n}\n\n/**\n * Class that represents a conversation chat memory with a token buffer.\n * It extends the `BaseChatMemory` class and implements the\n * `ConversationTokenBufferMemoryInput` interface.\n * @example\n * ```typescript\n * const memory = new ConversationTokenBufferMemory({\n *   llm: new ChatOpenAI({ model: \"gpt-4o-mini\" }),\n *   maxTokenLimit: 10,\n * });\n *\n * // Save conversation context\n * await memory.saveContext({ input: \"hi\" }, { output: \"whats up\" });\n * await memory.saveContext({ input: \"not much you\" }, { output: \"not much\" });\n *\n * // Load memory variables\n * const result = await memory.loadMemoryVariables({});\n * console.log(result);\n * ```\n */\n\nexport class ConversationTokenBufferMemory\n  extends BaseChatMemory\n  implements ConversationTokenBufferMemoryInput\n{\n  humanPrefix = \"Human\";\n\n  aiPrefix = \"AI\";\n\n  memoryKey = \"history\";\n\n  maxTokenLimit = 2000; // Default max token limit of 2000 which can be overridden\n\n  llm: BaseLanguageModelInterface;\n\n  constructor(fields: ConversationTokenBufferMemoryInput) {\n    super(fields);\n    this.llm = fields.llm;\n    this.humanPrefix = fields?.humanPrefix ?? this.humanPrefix;\n    this.aiPrefix = fields?.aiPrefix ?? this.aiPrefix;\n    this.memoryKey = fields?.memoryKey ?? this.memoryKey;\n    this.maxTokenLimit = fields?.maxTokenLimit ?? this.maxTokenLimit;\n  }\n\n  get memoryKeys() {\n    return [this.memoryKey];\n  }\n\n  /**\n   * Loads the memory variables. It takes an `InputValues` object as a\n   * parameter and returns a `Promise` that resolves with a\n   * `MemoryVariables` object.\n   * @param _values `InputValues` object.\n   * @returns A `Promise` that resolves with 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,\n      };\n      return result;\n    }\n    const result = {\n      [this.memoryKey]: getBufferString(\n        messages,\n        this.humanPrefix,\n        this.aiPrefix\n      ),\n    };\n    return result;\n  }\n\n  /**\n   * Saves the context from this conversation to buffer. If the amount\n   * of tokens required to save the buffer exceeds MAX_TOKEN_LIMIT,\n   * prune it.\n   */\n  async saveContext(inputValues: InputValues, outputValues: OutputValues) {\n    await super.saveContext(inputValues, outputValues);\n\n    // Prune buffer if it exceeds the max token limit set for this instance.\n    const buffer = await this.chatHistory.getMessages();\n    let currBufferLength = await this.llm.getNumTokens(\n      getBufferString(buffer, this.humanPrefix, this.aiPrefix)\n    );\n\n    if (currBufferLength > this.maxTokenLimit) {\n      const prunedMemory = [];\n      while (currBufferLength > this.maxTokenLimit) {\n        prunedMemory.push(buffer.shift());\n        currBufferLength = await this.llm.getNumTokens(\n          getBufferString(buffer, this.humanPrefix, this.aiPrefix)\n        );\n      }\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAmDA,IAAa,gCAAb,cACUA,2BAAAA,eAEV;CACE,cAAc;CAEd,WAAW;CAEX,YAAY;CAEZ,gBAAgB;CAEhB;CAEA,YAAY,QAA4C;AACtD,QAAM,OAAO;AACb,OAAK,MAAM,OAAO;AAClB,OAAK,cAAc,QAAQ,eAAe,KAAK;AAC/C,OAAK,WAAW,QAAQ,YAAY,KAAK;AACzC,OAAK,YAAY,QAAQ,aAAa,KAAK;AAC3C,OAAK,gBAAgB,QAAQ,iBAAiB,KAAK;;CAGrD,IAAI,aAAa;AACf,SAAO,CAAC,KAAK,UAAU;;;;;;;;;CAUzB,MAAM,oBAAoB,SAAgD;EACxE,MAAM,WAAW,MAAM,KAAK,YAAY,aAAa;AACrD,MAAI,KAAK,eAIP,QAHe,GACZ,KAAK,YAAY,UACnB;AAUH,SAPe,GACZ,KAAK,aAAA,GAAA,yBAAA,iBACJ,UACA,KAAK,aACL,KAAK,SACN,EACF;;;;;;;CASH,MAAM,YAAY,aAA0B,cAA4B;AACtE,QAAM,MAAM,YAAY,aAAa,aAAa;EAGlD,MAAM,SAAS,MAAM,KAAK,YAAY,aAAa;EACnD,IAAI,mBAAmB,MAAM,KAAK,IAAI,cAAA,GAAA,yBAAA,iBACpB,QAAQ,KAAK,aAAa,KAAK,SAAS,CACzD;AAED,MAAI,mBAAmB,KAAK,eAAe;GACzC,MAAM,eAAe,EAAE;AACvB,UAAO,mBAAmB,KAAK,eAAe;AAC5C,iBAAa,KAAK,OAAO,OAAO,CAAC;AACjC,uBAAmB,MAAM,KAAK,IAAI,cAAA,GAAA,yBAAA,iBAChB,QAAQ,KAAK,aAAa,KAAK,SAAS,CACzD"}