"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryZep = void 0;
const zep_1 = require("@langchain/community/memory/zep");
const zep_cloud_1 = require("@langchain/community/memory/zep_cloud");
const n8n_workflow_1 = require("n8n-workflow");
const helpers_1 = require("../../../utils/helpers");
const ai_utilities_1 = require("@n8n/ai-utilities");
const descriptions_1 = require("../descriptions");
class WhiteSpaceTrimmedZepCloudMemory extends zep_cloud_1.ZepCloudMemory {
    async loadMemoryVariables(values) {
        const memoryVariables = await super.loadMemoryVariables(values);
        memoryVariables.chat_history = memoryVariables.chat_history.filter((m) => m.content.toString().trim());
        return memoryVariables;
    }
}
class MemoryZep {
    constructor() {
        this.description = {
            displayName: 'Zep',
            name: 'memoryZep',
            hidden: true,
            icon: 'file:zep.png',
            group: ['transform'],
            version: [1, 1.1, 1.2, 1.3, 1.4],
            description: 'Use Zep Memory',
            defaults: {
                name: 'Zep',
            },
            codex: {
                categories: ['AI'],
                subcategories: {
                    AI: ['Memory'],
                    Memory: ['Other memories'],
                },
                resources: {
                    primaryDocumentation: [
                        {
                            url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memoryzep/',
                        },
                    ],
                },
            },
            inputs: [],
            outputs: [n8n_workflow_1.NodeConnectionTypes.AiMemory],
            outputNames: ['Memory'],
            credentials: [
                {
                    name: 'zepApi',
                    required: true,
                },
            ],
            properties: [
                {
                    displayName: 'This Zep integration is deprecated and will be removed in a future version.',
                    name: 'deprecationNotice',
                    type: 'notice',
                    default: '',
                },
                (0, ai_utilities_1.getConnectionHintNoticeField)([n8n_workflow_1.NodeConnectionTypes.AiAgent]),
                {
                    displayName: 'Only works with Zep Cloud and Community edition <= v0.27.2',
                    name: 'supportedVersions',
                    type: 'notice',
                    default: '',
                },
                {
                    displayName: 'Session ID',
                    name: 'sessionId',
                    type: 'string',
                    required: true,
                    default: '',
                    displayOptions: {
                        show: {
                            '@version': [1],
                        },
                    },
                },
                {
                    displayName: 'Session ID',
                    name: 'sessionId',
                    type: 'string',
                    default: '={{ $json.sessionId }}',
                    description: 'The key to use to store the memory',
                    displayOptions: {
                        show: {
                            '@version': [1.1],
                        },
                    },
                },
                {
                    ...descriptions_1.sessionIdOption,
                    displayOptions: {
                        show: {
                            '@version': [{ _cnd: { gte: 1.2 } }],
                        },
                    },
                },
                (0, descriptions_1.expressionSessionKeyProperty)(1.3),
                (0, descriptions_1.scopedSessionHint)(1.4),
                descriptions_1.sessionKeyProperty,
            ],
        };
    }
    async supplyData(itemIndex) {
        const credentials = await this.getCredentials('zepApi');
        const nodeVersion = this.getNode().typeVersion;
        let sessionId;
        if (nodeVersion >= 1.2) {
            sessionId = (0, helpers_1.getSessionId)(this, itemIndex);
        }
        else {
            sessionId = this.getNodeParameter('sessionId', itemIndex);
        }
        let memory;
        if (credentials.cloud) {
            if (!credentials.apiKey) {
                throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'API key is required to use Zep Cloud');
            }
            memory = new WhiteSpaceTrimmedZepCloudMemory({
                sessionId,
                apiKey: credentials.apiKey,
                memoryType: 'perpetual',
                memoryKey: 'chat_history',
                returnMessages: true,
                inputKey: 'input',
                outputKey: 'output',
                separateMessages: false,
            });
        }
        else {
            if (!credentials.apiUrl) {
                throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'API url is required to use Zep Open Source');
            }
            memory = new zep_1.ZepMemory({
                sessionId,
                baseURL: credentials.apiUrl,
                apiKey: credentials.apiKey,
                memoryKey: 'chat_history',
                returnMessages: true,
                inputKey: 'input',
                outputKey: 'output',
            });
        }
        return {
            response: (0, ai_utilities_1.logWrapper)(memory, this),
        };
    }
}
exports.MemoryZep = MemoryZep;
//# sourceMappingURL=MemoryZep.node.js.map