|
| 1 | +import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../../src/utils' |
| 2 | +import { SaverOptions } from '../interface' |
| 3 | +import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeParams } from '../../../../src/Interface' |
| 4 | +import { DataSource } from 'typeorm' |
| 5 | +import { PostgresSaver } from './pgSaver' |
| 6 | + |
| 7 | +class PostgresAgentMemory_Memory implements INode { |
| 8 | + label: string |
| 9 | + name: string |
| 10 | + version: number |
| 11 | + description: string |
| 12 | + type: string |
| 13 | + icon: string |
| 14 | + category: string |
| 15 | + badge: string |
| 16 | + baseClasses: string[] |
| 17 | + inputs: INodeParams[] |
| 18 | + credential: INodeParams |
| 19 | + |
| 20 | + constructor() { |
| 21 | + this.label = 'Postgres Agent Memory' |
| 22 | + this.name = 'postgresAgentMemory' |
| 23 | + this.version = 1.0 |
| 24 | + this.type = 'AgentMemory' |
| 25 | + this.icon = 'postgres.svg' |
| 26 | + this.category = 'Memory' |
| 27 | + this.description = 'Memory for agentflow to remember the state of the conversation using Postgres database' |
| 28 | + this.baseClasses = [this.type, ...getBaseClasses(PostgresSaver)] |
| 29 | + this.credential = { |
| 30 | + label: 'Connect Credential', |
| 31 | + name: 'credential', |
| 32 | + type: 'credential', |
| 33 | + credentialNames: ['PostgresApi'], |
| 34 | + optional: true |
| 35 | + } |
| 36 | + this.inputs = [ |
| 37 | + { |
| 38 | + label: 'Host', |
| 39 | + name: 'host', |
| 40 | + type: 'string' |
| 41 | + }, |
| 42 | + { |
| 43 | + label: 'Database', |
| 44 | + name: 'database', |
| 45 | + type: 'string' |
| 46 | + }, |
| 47 | + { |
| 48 | + label: 'Port', |
| 49 | + name: 'port', |
| 50 | + type: 'number', |
| 51 | + default: '5432' |
| 52 | + }, |
| 53 | + { |
| 54 | + label: 'Additional Connection Configuration', |
| 55 | + name: 'additionalConfig', |
| 56 | + type: 'json', |
| 57 | + additionalParams: true, |
| 58 | + optional: true |
| 59 | + } |
| 60 | + ] |
| 61 | + } |
| 62 | + |
| 63 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 64 | + const additionalConfig = nodeData.inputs?.additionalConfig as string |
| 65 | + const databaseEntities = options.databaseEntities as IDatabaseEntity |
| 66 | + const chatflowid = options.chatflowid as string |
| 67 | + const appDataSource = options.appDataSource as DataSource |
| 68 | + |
| 69 | + let additionalConfiguration = {} |
| 70 | + if (additionalConfig) { |
| 71 | + try { |
| 72 | + additionalConfiguration = typeof additionalConfig === 'object' ? additionalConfig : JSON.parse(additionalConfig) |
| 73 | + } catch (exception) { |
| 74 | + throw new Error('Invalid JSON in the Additional Configuration: ' + exception) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + const threadId = options.sessionId || options.chatId |
| 79 | + |
| 80 | + let datasourceOptions: ICommonObject = { |
| 81 | + ...additionalConfiguration, |
| 82 | + type: 'postgres' |
| 83 | + } |
| 84 | + |
| 85 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 86 | + const user = getCredentialParam('user', credentialData, nodeData) |
| 87 | + const password = getCredentialParam('password', credentialData, nodeData) |
| 88 | + const _port = (nodeData.inputs?.port as string) || '5432' |
| 89 | + const port = parseInt(_port) |
| 90 | + datasourceOptions = { |
| 91 | + ...datasourceOptions, |
| 92 | + host: nodeData.inputs?.host as string, |
| 93 | + port, |
| 94 | + database: nodeData.inputs?.database as string, |
| 95 | + username: user, |
| 96 | + user: user, |
| 97 | + password: password |
| 98 | + } |
| 99 | + const args: SaverOptions = { |
| 100 | + datasourceOptions, |
| 101 | + threadId, |
| 102 | + appDataSource, |
| 103 | + databaseEntities, |
| 104 | + chatflowid |
| 105 | + } |
| 106 | + const recordManager = new PostgresSaver(args) |
| 107 | + return recordManager |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +module.exports = { nodeClass: PostgresAgentMemory_Memory } |
0 commit comments