@n8n/n8n-nodes-langchain
Version:

158 lines • 5.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryRedisChat = void 0;
const redis_1 = require("@langchain/redis");
const memory_1 = require("@langchain/classic/memory");
const n8n_workflow_1 = require("n8n-workflow");
const redis_2 = require("redis");
const helpers_1 = require("../../../utils/helpers");
const ai_utilities_1 = require("@n8n/ai-utilities");
const descriptions_1 = require("../descriptions");
class MemoryRedisChat {
constructor() {
this.description = {
displayName: 'Redis Chat Memory',
name: 'memoryRedisChat',
icon: 'file:redis.svg',
group: ['transform'],
version: [1, 1.1, 1.2, 1.3, 1.4, 1.5],
description: 'Stores the chat history in Redis.',
defaults: {
name: 'Redis Chat Memory',
},
credentials: [
{
name: 'redis',
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.memoryredischat/',
},
],
},
},
inputs: [],
outputs: [n8n_workflow_1.NodeConnectionTypes.AiMemory],
outputNames: ['Memory'],
properties: [
(0, ai_utilities_1.getConnectionHintNoticeField)([n8n_workflow_1.NodeConnectionTypes.AiAgent]),
{
displayName: 'Session Key',
name: 'sessionKey',
type: 'string',
default: 'chat_history',
description: 'The key to use to store the memory in the workflow data',
displayOptions: {
show: {
'@version': [1],
},
},
},
{
displayName: 'Session ID',
name: 'sessionKey',
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.4),
descriptions_1.sessionKeyProperty,
{
displayName: 'Session Time To Live',
name: 'sessionTTL',
type: 'number',
default: 0,
description: 'For how long the session should be stored in seconds. If set to 0 it will not expire.',
},
{
...descriptions_1.contextWindowLengthProperty,
displayOptions: { hide: { '@version': [{ _cnd: { lt: 1.3 } }] } },
},
],
};
}
async supplyData(itemIndex) {
const credentials = await this.getCredentials('redis');
const nodeVersion = this.getNode().typeVersion;
const sessionTTL = this.getNodeParameter('sessionTTL', itemIndex, 0);
let sessionId;
if (nodeVersion >= 1.2) {
sessionId = (0, helpers_1.getSessionId)(this, itemIndex);
}
else {
sessionId = this.getNodeParameter('sessionKey', itemIndex);
}
const redisOptions = {
socket: {
host: credentials.host,
port: credentials.port,
tls: credentials.ssl === true,
},
database: credentials.database,
};
if (credentials.user && nodeVersion >= 1.5) {
redisOptions.username = credentials.user;
}
if (credentials.password) {
redisOptions.password = credentials.password;
}
const client = (0, redis_2.createClient)({
...redisOptions,
});
client.on('error', async (error) => {
await client.quit();
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Redis Error: ' + error.message);
});
const redisChatConfig = {
client,
sessionId,
};
if (sessionTTL > 0) {
redisChatConfig.sessionTTL = sessionTTL;
}
const redisChatHistory = new redis_1.RedisChatMessageHistory(redisChatConfig);
const memClass = this.getNode().typeVersion < 1.3 ? memory_1.BufferMemory : memory_1.BufferWindowMemory;
const kOptions = this.getNode().typeVersion < 1.3
? {}
: { k: this.getNodeParameter('contextWindowLength', itemIndex) };
const memory = new memClass({
memoryKey: 'chat_history',
chatHistory: redisChatHistory,
returnMessages: true,
inputKey: 'input',
outputKey: 'output',
...kOptions,
});
async function closeFunction() {
void client.disconnect();
}
return {
closeFunction,
response: (0, ai_utilities_1.logWrapper)(memory, this),
};
}
}
exports.MemoryRedisChat = MemoryRedisChat;
//# sourceMappingURL=MemoryRedisChat.node.js.map