"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "ChatMemoryBuffer", {
    enumerable: true,
    get: function() {
        return ChatMemoryBuffer;
    }
});
const _SimpleChatStore = require("../storage/chatStore/SimpleChatStore.js");
const DEFAULT_TOKEN_LIMIT_RATIO = 0.75;
const DEFAULT_TOKEN_LIMIT = 3000;
class ChatMemoryBuffer {
    tokenLimit;
    chatStore;
    chatStoreKey;
    constructor(init){
        this.chatStore = init?.chatStore ?? new _SimpleChatStore.SimpleChatStore();
        this.chatStoreKey = init?.chatStoreKey ?? "chat_history";
        if (init?.llm) {
            const contextWindow = init.llm.metadata.contextWindow;
            this.tokenLimit = init?.tokenLimit ?? Math.ceil(contextWindow * DEFAULT_TOKEN_LIMIT_RATIO);
        } else {
            this.tokenLimit = init?.tokenLimit ?? DEFAULT_TOKEN_LIMIT;
        }
        if (init?.chatHistory) {
            this.chatStore.setMessages(this.chatStoreKey, init.chatHistory.messages);
        }
    }
    get(initialTokenCount = 0) {
        const chatHistory = this.getAll();
        if (initialTokenCount > this.tokenLimit) {
            throw new Error("Initial token count exceeds token limit");
        }
        let messageCount = chatHistory.length;
        let tokenCount = this._tokenCountForMessageCount(messageCount) + initialTokenCount;
        while(tokenCount > this.tokenLimit && messageCount > 1){
            messageCount -= 1;
            if (chatHistory.at(-messageCount)?.role === "assistant") {
                // we cannot have an assistant message at the start of the chat history
                // if after removal of the first, we have an assistant message,
                // we need to remove the assistant message too
                messageCount -= 1;
            }
            tokenCount = this._tokenCountForMessageCount(messageCount) + initialTokenCount;
        }
        // catch one message longer than token limit
        if (tokenCount > this.tokenLimit || messageCount <= 0) {
            return [];
        }
        return chatHistory.slice(-messageCount);
    }
    getAll() {
        return this.chatStore.getMessages(this.chatStoreKey);
    }
    put(message) {
        this.chatStore.addMessage(this.chatStoreKey, message);
    }
    set(messages) {
        this.chatStore.setMessages(this.chatStoreKey, messages);
    }
    reset() {
        this.chatStore.deleteMessages(this.chatStoreKey);
    }
    _tokenCountForMessageCount(messageCount) {
        if (messageCount <= 0) {
            return 0;
        }
        const chatHistory = this.getAll();
        const msgStr = chatHistory.slice(-messageCount).map((m)=>m.content).join(" ");
        return msgStr.split(" ").length;
    }
}
