Feature/Postgres agent memory (#3495)

* add postgres to agent memory

* add mysql agent memory
This commit is contained in:
Henry Heng
2024-11-09 14:15:44 +00:00
committed by GitHub
parent b01772302e
commit 0153735704
3 changed files with 589 additions and 3 deletions
@@ -1,9 +1,11 @@
import path from 'path'
import { getBaseClasses, getUserHome } from '../../../src/utils'
import { getBaseClasses, getCredentialData, getCredentialParam, getUserHome } from '../../../src/utils'
import { SaverOptions } from './interface'
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeParams } from '../../../src/Interface'
import { SqliteSaver } from './sqliteSaver'
import { DataSource } from 'typeorm'
import { PostgresSaver } from './pgSaver'
import { MySQLSaver } from './mysqlSaver'
class AgentMemory_Memory implements INode {
label: string
@@ -16,16 +18,24 @@ class AgentMemory_Memory implements INode {
badge: string
baseClasses: string[]
inputs: INodeParams[]
credential: INodeParams
constructor() {
this.label = 'Agent Memory'
this.name = 'agentMemory'
this.version = 1.0
this.version = 2.0
this.type = 'AgentMemory'
this.icon = 'agentmemory.svg'
this.category = 'Memory'
this.description = 'Memory for agentflow to remember the state of the conversation'
this.baseClasses = [this.type, ...getBaseClasses(SqliteSaver)]
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['PostgresApi', 'MySQLApi'],
optional: true
}
this.inputs = [
{
label: 'Database',
@@ -35,6 +45,14 @@ class AgentMemory_Memory implements INode {
{
label: 'SQLite',
name: 'sqlite'
},
{
label: 'PostgreSQL',
name: 'postgres'
},
{
label: 'MySQL',
name: 'mysql'
}
],
default: 'sqlite'
@@ -49,6 +67,30 @@ class AgentMemory_Memory implements INode {
additionalParams: true,
optional: true
},
{
label: 'Host',
name: 'host',
type: 'string',
description: 'If PostgresQL/MySQL is selected, provide the host of the database',
additionalParams: true,
optional: true
},
{
label: 'Database',
name: 'database',
type: 'string',
description: 'If PostgresQL/MySQL is selected, provide the name of the database',
additionalParams: true,
optional: true
},
{
label: 'Port',
name: 'port',
type: 'number',
description: 'If PostgresQL/MySQL is selected, provide the port of the database',
additionalParams: true,
optional: true
},
{
label: 'Additional Connection Configuration',
name: 'additionalConfig',
@@ -78,7 +120,7 @@ class AgentMemory_Memory implements INode {
const threadId = options.sessionId || options.chatId
const datasourceOptions: ICommonObject = {
let datasourceOptions: ICommonObject = {
...additionalConfiguration,
type: databaseType
}
@@ -96,6 +138,55 @@ class AgentMemory_Memory implements INode {
}
const recordManager = new SqliteSaver(args)
return recordManager
} else if (databaseType === 'postgres') {
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const user = getCredentialParam('user', credentialData, nodeData)
const password = getCredentialParam('password', credentialData, nodeData)
const _port = (nodeData.inputs?.port as string) || '5432'
const port = parseInt(_port)
datasourceOptions = {
...datasourceOptions,
host: nodeData.inputs?.host as string,
port,
database: nodeData.inputs?.database as string,
username: user,
user: user,
password: password
}
const args: SaverOptions = {
datasourceOptions,
threadId,
appDataSource,
databaseEntities,
chatflowid
}
const recordManager = new PostgresSaver(args)
return recordManager
} else if (databaseType === 'mysql') {
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const user = getCredentialParam('user', credentialData, nodeData)
const password = getCredentialParam('password', credentialData, nodeData)
const _port = (nodeData.inputs?.port as string) || '3306'
const port = parseInt(_port)
datasourceOptions = {
...datasourceOptions,
host: nodeData.inputs?.host as string,
port,
database: nodeData.inputs?.database as string,
username: user,
user: user,
password: password,
charset: 'utf8mb4'
}
const args: SaverOptions = {
datasourceOptions,
threadId,
appDataSource,
databaseEntities,
chatflowid
}
const recordManager = new MySQLSaver(args)
return recordManager
}
return undefined