|
| 1 | +import { IMessage, INode, INodeData, INodeParams, MemoryMethods, MessageType } from '../../../src/Interface' |
| 2 | +import { convertBaseMessagetoIMessage, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' |
| 3 | +import { ZepMemory, ZepMemoryInput } from '@getzep/zep-cloud/langchain' |
| 4 | + |
| 5 | +import { ICommonObject } from '../../../src' |
| 6 | +import { InputValues, MemoryVariables, OutputValues } from 'langchain/memory' |
| 7 | +import { BaseMessage } from 'langchain/schema' |
| 8 | + |
| 9 | +class ZepMemoryCloud_Memory implements INode { |
| 10 | + label: string |
| 11 | + name: string |
| 12 | + version: number |
| 13 | + description: string |
| 14 | + type: string |
| 15 | + icon: string |
| 16 | + category: string |
| 17 | + baseClasses: string[] |
| 18 | + credential: INodeParams |
| 19 | + inputs: INodeParams[] |
| 20 | + |
| 21 | + constructor() { |
| 22 | + this.label = 'Zep Memory - Cloud' |
| 23 | + this.name = 'ZepMemoryCloud' |
| 24 | + this.version = 2.0 |
| 25 | + this.type = 'ZepMemory' |
| 26 | + this.icon = 'zep.svg' |
| 27 | + this.category = 'Memory' |
| 28 | + this.description = 'Summarizes the conversation and stores the memory in zep server' |
| 29 | + this.baseClasses = [this.type, ...getBaseClasses(ZepMemory)] |
| 30 | + this.credential = { |
| 31 | + label: 'Connect Credential', |
| 32 | + name: 'credential', |
| 33 | + type: 'credential', |
| 34 | + optional: true, |
| 35 | + description: 'Configure JWT authentication on your Zep instance (Optional)', |
| 36 | + credentialNames: ['zepMemoryApi'] |
| 37 | + } |
| 38 | + this.inputs = [ |
| 39 | + { |
| 40 | + label: 'Session Id', |
| 41 | + name: 'sessionId', |
| 42 | + type: 'string', |
| 43 | + description: |
| 44 | + 'If not specified, a random id will be used. Learn <a target="_blank" href="https://docs.flowiseai.com/memory/long-term-memory#ui-and-embedded-chat">more</a>', |
| 45 | + default: '', |
| 46 | + additionalParams: true, |
| 47 | + optional: true |
| 48 | + }, |
| 49 | + { |
| 50 | + label: 'Memory Type', |
| 51 | + name: 'memoryType', |
| 52 | + type: 'string', |
| 53 | + default: 'perpetual', |
| 54 | + description: 'Zep Memory Type, can be perpetual or message_window', |
| 55 | + additionalParams: true |
| 56 | + }, |
| 57 | + { |
| 58 | + label: 'AI Prefix', |
| 59 | + name: 'aiPrefix', |
| 60 | + type: 'string', |
| 61 | + default: 'ai', |
| 62 | + additionalParams: true |
| 63 | + }, |
| 64 | + { |
| 65 | + label: 'Human Prefix', |
| 66 | + name: 'humanPrefix', |
| 67 | + type: 'string', |
| 68 | + default: 'human', |
| 69 | + additionalParams: true |
| 70 | + }, |
| 71 | + { |
| 72 | + label: 'Memory Key', |
| 73 | + name: 'memoryKey', |
| 74 | + type: 'string', |
| 75 | + default: 'chat_history', |
| 76 | + additionalParams: true |
| 77 | + }, |
| 78 | + { |
| 79 | + label: 'Input Key', |
| 80 | + name: 'inputKey', |
| 81 | + type: 'string', |
| 82 | + default: 'input', |
| 83 | + additionalParams: true |
| 84 | + }, |
| 85 | + { |
| 86 | + label: 'Output Key', |
| 87 | + name: 'outputKey', |
| 88 | + type: 'string', |
| 89 | + default: 'text', |
| 90 | + additionalParams: true |
| 91 | + } |
| 92 | + ] |
| 93 | + } |
| 94 | + |
| 95 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 96 | + return await initializeZep(nodeData, options) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +const initializeZep = async (nodeData: INodeData, options: ICommonObject): Promise<ZepMemory> => { |
| 101 | + const aiPrefix = nodeData.inputs?.aiPrefix as string |
| 102 | + const humanPrefix = nodeData.inputs?.humanPrefix as string |
| 103 | + const memoryKey = nodeData.inputs?.memoryKey as string |
| 104 | + const inputKey = nodeData.inputs?.inputKey as string |
| 105 | + |
| 106 | + const memoryType = nodeData.inputs?.memoryType as 'perpetual' | 'message_window' |
| 107 | + const sessionId = nodeData.inputs?.sessionId as string |
| 108 | + |
| 109 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 110 | + const apiKey = getCredentialParam('apiKey', credentialData, nodeData) |
| 111 | + const obj: ZepMemoryInput & ZepMemoryExtendedInput = { |
| 112 | + apiKey, |
| 113 | + aiPrefix, |
| 114 | + humanPrefix, |
| 115 | + memoryKey, |
| 116 | + sessionId, |
| 117 | + inputKey, |
| 118 | + memoryType: memoryType, |
| 119 | + returnMessages: true |
| 120 | + } |
| 121 | + |
| 122 | + return new ZepMemoryExtended(obj) |
| 123 | +} |
| 124 | + |
| 125 | +interface ZepMemoryExtendedInput { |
| 126 | + memoryType?: 'perpetual' | 'message_window' |
| 127 | +} |
| 128 | + |
| 129 | +class ZepMemoryExtended extends ZepMemory implements MemoryMethods { |
| 130 | + memoryType: 'perpetual' | 'message_window' |
| 131 | + |
| 132 | + constructor(fields: ZepMemoryInput & ZepMemoryExtendedInput) { |
| 133 | + super(fields) |
| 134 | + this.memoryType = fields.memoryType ?? 'perpetual' |
| 135 | + } |
| 136 | + |
| 137 | + async loadMemoryVariables(values: InputValues, overrideSessionId = ''): Promise<MemoryVariables> { |
| 138 | + if (overrideSessionId) { |
| 139 | + this.sessionId = overrideSessionId |
| 140 | + } |
| 141 | + return super.loadMemoryVariables({ ...values, memoryType: this.memoryType }) |
| 142 | + } |
| 143 | + |
| 144 | + async saveContext(inputValues: InputValues, outputValues: OutputValues, overrideSessionId = ''): Promise<void> { |
| 145 | + if (overrideSessionId) { |
| 146 | + this.sessionId = overrideSessionId |
| 147 | + } |
| 148 | + return super.saveContext(inputValues, outputValues) |
| 149 | + } |
| 150 | + |
| 151 | + async clear(overrideSessionId = ''): Promise<void> { |
| 152 | + if (overrideSessionId) { |
| 153 | + this.sessionId = overrideSessionId |
| 154 | + } |
| 155 | + return super.clear() |
| 156 | + } |
| 157 | + |
| 158 | + async getChatMessages(overrideSessionId = '', returnBaseMessages = false): Promise<IMessage[] | BaseMessage[]> { |
| 159 | + const id = overrideSessionId ? overrideSessionId : this.sessionId |
| 160 | + const memoryVariables = await this.loadMemoryVariables({}, id) |
| 161 | + const baseMessages = memoryVariables[this.memoryKey] |
| 162 | + return returnBaseMessages ? baseMessages : convertBaseMessagetoIMessage(baseMessages) |
| 163 | + } |
| 164 | + |
| 165 | + async addChatMessages(msgArray: { text: string; type: MessageType }[], overrideSessionId = ''): Promise<void> { |
| 166 | + const id = overrideSessionId ? overrideSessionId : this.sessionId |
| 167 | + const input = msgArray.find((msg) => msg.type === 'userMessage') |
| 168 | + const output = msgArray.find((msg) => msg.type === 'apiMessage') |
| 169 | + const inputValues = { [this.inputKey ?? 'input']: input?.text } |
| 170 | + const outputValues = { output: output?.text } |
| 171 | + |
| 172 | + await this.saveContext(inputValues, outputValues, id) |
| 173 | + } |
| 174 | + |
| 175 | + async clearChatMessages(overrideSessionId = ''): Promise<void> { |
| 176 | + const id = overrideSessionId ? overrideSessionId : this.sessionId |
| 177 | + await this.clear(id) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +module.exports = { nodeClass: ZepMemoryCloud_Memory } |
0 commit comments