UNPKG

@n8n/n8n-nodes-langchain

Version:

![Banner image](https://user-images.githubusercontent.com/10284570/173569848-c624317f-42b1-45a6-ab09-f0ea3c247648.png)

132 lines 5.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MemoryMongoDbChat = void 0; const mongodb_1 = require("@langchain/mongodb"); const memory_1 = require("@langchain/classic/memory"); const mongodb_2 = require("mongodb"); 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 MemoryMongoDbChat { constructor() { this.description = { displayName: 'MongoDB Chat Memory', name: 'memoryMongoDbChat', icon: 'file:mongodb.svg', group: ['transform'], version: [1], description: 'Stores the chat history in MongoDB collection.', defaults: { name: 'MongoDB Chat Memory', }, credentials: [ { name: 'mongoDb', required: true, }, ], 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.memorymongochat/', }, ], }, }, inputs: [], outputs: [n8n_workflow_1.NodeConnectionTypes.AiMemory], outputNames: ['Memory'], properties: [ (0, ai_utilities_1.getConnectionHintNoticeField)([n8n_workflow_1.NodeConnectionTypes.AiAgent]), descriptions_1.sessionIdOption, (0, descriptions_1.expressionSessionKeyProperty)(1), descriptions_1.sessionKeyProperty, { displayName: 'Collection Name', name: 'collectionName', type: 'string', default: 'n8n_chat_histories', description: 'The collection name to store the chat history in. If collection does not exist, it will be created.', }, { displayName: 'Database Name', name: 'databaseName', type: 'string', default: '', description: 'The database name to store the chat history in. If not provided, the database from credentials will be used.', }, descriptions_1.contextWindowLengthProperty, ], }; } async supplyData(itemIndex) { const credentials = await this.getCredentials('mongoDb'); const collectionName = this.getNodeParameter('collectionName', itemIndex, 'n8n_chat_histories'); const databaseName = this.getNodeParameter('databaseName', itemIndex, ''); const sessionId = (0, helpers_1.getSessionId)(this, itemIndex); let connectionString; let dbName; if (credentials.configurationType === 'connectionString') { connectionString = credentials.connectionString; dbName = databaseName || credentials.database; } else { const host = credentials.host; const port = credentials.port; const user = credentials.user ? encodeURIComponent(credentials.user) : ''; const password = credentials.password ? encodeURIComponent(credentials.password) : ''; const authString = user && password ? `${user}:${password}@` : ''; const tls = credentials.tls; connectionString = `mongodb://${authString}${host}:${port}/?appname=n8n`; if (tls) { connectionString += '&ssl=true'; } dbName = databaseName || credentials.database; } if (!dbName) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Database name must be provided either in credentials or in node parameters'); } const client = new mongodb_2.MongoClient(connectionString, { minPoolSize: 0, maxPoolSize: 1, maxIdleTimeMS: 30000, }); try { await client.connect(); const db = client.db(dbName); const collection = db.collection(collectionName); const mongoDBChatHistory = new mongodb_1.MongoDBChatMessageHistory({ collection, sessionId, }); const memory = new memory_1.BufferWindowMemory({ memoryKey: 'chat_history', chatHistory: mongoDBChatHistory, returnMessages: true, inputKey: 'input', outputKey: 'output', k: this.getNodeParameter('contextWindowLength', itemIndex, 5), }); async function closeFunction() { await client.close(); } return { closeFunction, response: (0, ai_utilities_1.logWrapper)(memory, this), }; } catch (error) { void client.close(); throw new n8n_workflow_1.NodeOperationError(this.getNode(), `MongoDB connection error: ${error.message}`); } } } exports.MemoryMongoDbChat = MemoryMongoDbChat; //# sourceMappingURL=MemoryMongoDbChat.node.js.map