From 2653cb62da9d51b877d63ed126d97f489f145d99 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 25 Oct 2023 12:39:35 +0100 Subject: [PATCH] update credentials --- .../credentials/RedisApi.credential.ts | 43 ------------------- .../RedisBackedChatMemory.ts | 43 +++++++++++-------- 2 files changed, 25 insertions(+), 61 deletions(-) delete mode 100644 packages/components/credentials/RedisApi.credential.ts diff --git a/packages/components/credentials/RedisApi.credential.ts b/packages/components/credentials/RedisApi.credential.ts deleted file mode 100644 index f65f3f44..00000000 --- a/packages/components/credentials/RedisApi.credential.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { INodeParams, INodeCredential } from '../src/Interface' - -class RedisApi implements INodeCredential { - label: string - name: string - version: number - description: string - inputs: INodeParams[] - - constructor() { - this.label = 'Redis API' - this.name = 'redisApi' - this.version = 1.0 - this.inputs = [ - { - label: 'Redis Host', - name: 'redisHost', - type: 'string', - default: '127.0.0.1' - }, - { - label: 'Port', - name: 'redisPort', - type: 'number', - default: '6789' - }, - { - label: 'User', - name: 'redisUser', - type: 'string', - placeholder: '' - }, - { - label: 'Password', - name: 'redisPwd', - type: 'password', - placeholder: '' - } - ] - } -} - -module.exports = { credClass: RedisApi } diff --git a/packages/components/nodes/memory/RedisBackedChatMemory/RedisBackedChatMemory.ts b/packages/components/nodes/memory/RedisBackedChatMemory/RedisBackedChatMemory.ts index f192fd12..3e3697d1 100644 --- a/packages/components/nodes/memory/RedisBackedChatMemory/RedisBackedChatMemory.ts +++ b/packages/components/nodes/memory/RedisBackedChatMemory/RedisBackedChatMemory.ts @@ -31,7 +31,7 @@ class RedisBackedChatMemory_Memory implements INode { name: 'credential', type: 'credential', optional: true, - credentialNames: ['redisApi'] + credentialNames: ['redisCacheApi', 'redisCacheUrlApi'] } this.inputs = [ { @@ -81,25 +81,32 @@ const initalizeRedis = async (nodeData: INodeData, options: ICommonObject): Prom const memoryKey = nodeData.inputs?.memoryKey as string const chatId = options?.chatId as string - const credentialData = await getCredentialData(nodeData.credential ?? '', options) - const username = getCredentialParam('redisUser', credentialData, nodeData) - const password = getCredentialParam('redisPwd', credentialData, nodeData) - const portStr = getCredentialParam('redisPort', credentialData, nodeData) - const host = getCredentialParam('redisHost', credentialData, nodeData) - let isSessionIdUsingChatMessageId = false if (!sessionId && chatId) isSessionIdUsingChatMessageId = true - const redisClient = new Redis({ - port: portStr ? parseInt(portStr) : 6379, - host, - username, - password - }) + const credentialData = await getCredentialData(nodeData.credential ?? '', options) + const redisUrl = getCredentialParam('redisUrl', credentialData, nodeData) + + let client: Redis + if (!redisUrl || redisUrl === '') { + const username = getCredentialParam('redisCacheUser', credentialData, nodeData) + const password = getCredentialParam('redisCachePwd', credentialData, nodeData) + const portStr = getCredentialParam('redisCachePort', credentialData, nodeData) + const host = getCredentialParam('redisCacheHost', credentialData, nodeData) + + client = new Redis({ + port: portStr ? parseInt(portStr) : 6379, + host, + username, + password + }) + } else { + client = new Redis(redisUrl) + } let obj: RedisChatMessageHistoryInput = { sessionId: sessionId ? sessionId : chatId, - client: redisClient + client } if (sessionTTL) { @@ -112,21 +119,21 @@ const initalizeRedis = async (nodeData: INodeData, options: ICommonObject): Prom const redisChatMessageHistory = new RedisChatMessageHistory(obj) redisChatMessageHistory.getMessages = async (): Promise => { - const rawStoredMessages = await redisClient.lrange(sessionId ? sessionId : chatId, 0, -1) + const rawStoredMessages = await client.lrange(sessionId ? sessionId : chatId, 0, -1) const orderedMessages = rawStoredMessages.reverse().map((message) => JSON.parse(message)) return orderedMessages.map(mapStoredMessageToChatMessage) } redisChatMessageHistory.addMessage = async (message: BaseMessage): Promise => { const messageToAdd = [message].map((msg) => msg.toDict()) - await redisClient.lpush(sessionId ? sessionId : chatId, JSON.stringify(messageToAdd[0])) + await client.lpush(sessionId ? sessionId : chatId, JSON.stringify(messageToAdd[0])) if (sessionTTL) { - await redisClient.expire(sessionId ? sessionId : chatId, sessionTTL) + await client.expire(sessionId ? sessionId : chatId, sessionTTL) } } redisChatMessageHistory.clear = async (): Promise => { - await redisClient.del(sessionId ? sessionId : chatId) + await client.del(sessionId ? sessionId : chatId) } const memory = new BufferMemoryExtended({