import { InputValues, MemoryVariables, getBufferString } from "./base.js"; import { BaseChatMemory, BaseChatMemoryInput } from "./chat_memory.js"; export interface BufferMemoryInput extends BaseChatMemoryInput { humanPrefix?: string; aiPrefix?: string; memoryKey?: string; } export class BufferMemory extends BaseChatMemory implements BufferMemoryInput { humanPrefix = "Human"; aiPrefix = "AI"; memoryKey = "history"; constructor(fields?: BufferMemoryInput) { super({ chatHistory: fields?.chatHistory, returnMessages: fields?.returnMessages ?? false, inputKey: fields?.inputKey, outputKey: fields?.outputKey, }); this.humanPrefix = fields?.humanPrefix ?? this.humanPrefix; this.aiPrefix = fields?.aiPrefix ?? this.aiPrefix; this.memoryKey = fields?.memoryKey ?? this.memoryKey; } get memoryKeys() { return [this.memoryKey]; } async loadMemoryVariables(_values: InputValues): Promise { const messages = await this.chatHistory.getMessages(); if (this.returnMessages) { const result = { [this.memoryKey]: messages, }; return result; } const result = { [this.memoryKey]: getBufferString( messages, this.humanPrefix, this.aiPrefix ), }; return result; } }