mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-29 07:01:04 +03:00
Feature/Add dedicated agent memory nodes (#3649)
add dedicated agent memory nodes
This commit is contained in:
@@ -2,10 +2,10 @@ import path from 'path'
|
|||||||
import { getBaseClasses, getCredentialData, getCredentialParam, getUserHome } from '../../../src/utils'
|
import { getBaseClasses, getCredentialData, getCredentialParam, getUserHome } from '../../../src/utils'
|
||||||
import { SaverOptions } from './interface'
|
import { SaverOptions } from './interface'
|
||||||
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeParams } from '../../../src/Interface'
|
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||||
import { SqliteSaver } from './sqliteSaver'
|
import { SqliteSaver } from './SQLiteAgentMemory/sqliteSaver'
|
||||||
import { DataSource } from 'typeorm'
|
import { DataSource } from 'typeorm'
|
||||||
import { PostgresSaver } from './pgSaver'
|
import { PostgresSaver } from './PostgresAgentMemory/pgSaver'
|
||||||
import { MySQLSaver } from './mysqlSaver'
|
import { MySQLSaver } from './MySQLAgentMemory/mysqlSaver'
|
||||||
|
|
||||||
class AgentMemory_Memory implements INode {
|
class AgentMemory_Memory implements INode {
|
||||||
label: string
|
label: string
|
||||||
@@ -29,6 +29,7 @@ class AgentMemory_Memory implements INode {
|
|||||||
this.category = 'Memory'
|
this.category = 'Memory'
|
||||||
this.description = 'Memory for agentflow to remember the state of the conversation'
|
this.description = 'Memory for agentflow to remember the state of the conversation'
|
||||||
this.baseClasses = [this.type, ...getBaseClasses(SqliteSaver)]
|
this.baseClasses = [this.type, ...getBaseClasses(SqliteSaver)]
|
||||||
|
this.badge = 'DEPRECATING'
|
||||||
this.credential = {
|
this.credential = {
|
||||||
label: 'Connect Credential',
|
label: 'Connect Credential',
|
||||||
name: 'credential',
|
name: 'credential',
|
||||||
|
|||||||
@@ -0,0 +1,112 @@
|
|||||||
|
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../../src/utils'
|
||||||
|
import { SaverOptions } from '../interface'
|
||||||
|
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeParams } from '../../../../src/Interface'
|
||||||
|
import { DataSource } from 'typeorm'
|
||||||
|
import { MySQLSaver } from './mysqlSaver'
|
||||||
|
|
||||||
|
class MySQLAgentMemory_Memory implements INode {
|
||||||
|
label: string
|
||||||
|
name: string
|
||||||
|
version: number
|
||||||
|
description: string
|
||||||
|
type: string
|
||||||
|
icon: string
|
||||||
|
category: string
|
||||||
|
badge: string
|
||||||
|
baseClasses: string[]
|
||||||
|
inputs: INodeParams[]
|
||||||
|
credential: INodeParams
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.label = 'MySQL Agent Memory'
|
||||||
|
this.name = 'mySQLAgentMemory'
|
||||||
|
this.version = 1.0
|
||||||
|
this.type = 'AgentMemory'
|
||||||
|
this.icon = 'mysql.png'
|
||||||
|
this.category = 'Memory'
|
||||||
|
this.description = 'Memory for agentflow to remember the state of the conversation using MySQL database'
|
||||||
|
this.baseClasses = [this.type, ...getBaseClasses(MySQLSaver)]
|
||||||
|
this.credential = {
|
||||||
|
label: 'Connect Credential',
|
||||||
|
name: 'credential',
|
||||||
|
type: 'credential',
|
||||||
|
credentialNames: ['MySQLApi'],
|
||||||
|
optional: true
|
||||||
|
}
|
||||||
|
this.inputs = [
|
||||||
|
{
|
||||||
|
label: 'Host',
|
||||||
|
name: 'host',
|
||||||
|
type: 'string'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Database',
|
||||||
|
name: 'database',
|
||||||
|
type: 'string'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Port',
|
||||||
|
name: 'port',
|
||||||
|
type: 'number',
|
||||||
|
default: '3306'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Additional Connection Configuration',
|
||||||
|
name: 'additionalConfig',
|
||||||
|
type: 'json',
|
||||||
|
additionalParams: true,
|
||||||
|
optional: true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
|
||||||
|
const additionalConfig = nodeData.inputs?.additionalConfig as string
|
||||||
|
const databaseEntities = options.databaseEntities as IDatabaseEntity
|
||||||
|
const chatflowid = options.chatflowid as string
|
||||||
|
const appDataSource = options.appDataSource as DataSource
|
||||||
|
|
||||||
|
let additionalConfiguration = {}
|
||||||
|
if (additionalConfig) {
|
||||||
|
try {
|
||||||
|
additionalConfiguration = typeof additionalConfig === 'object' ? additionalConfig : JSON.parse(additionalConfig)
|
||||||
|
} catch (exception) {
|
||||||
|
throw new Error('Invalid JSON in the Additional Configuration: ' + exception)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const threadId = options.sessionId || options.chatId
|
||||||
|
|
||||||
|
let datasourceOptions: ICommonObject = {
|
||||||
|
...additionalConfiguration,
|
||||||
|
type: '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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { nodeClass: MySQLAgentMemory_Memory }
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 3.7 KiB |
+3
-3
@@ -2,9 +2,9 @@ import { BaseCheckpointSaver, Checkpoint, CheckpointMetadata } from '@langchain/
|
|||||||
import { RunnableConfig } from '@langchain/core/runnables'
|
import { RunnableConfig } from '@langchain/core/runnables'
|
||||||
import { BaseMessage } from '@langchain/core/messages'
|
import { BaseMessage } from '@langchain/core/messages'
|
||||||
import { DataSource } from 'typeorm'
|
import { DataSource } from 'typeorm'
|
||||||
import { CheckpointTuple, SaverOptions, SerializerProtocol } from './interface'
|
import { CheckpointTuple, SaverOptions, SerializerProtocol } from '../interface'
|
||||||
import { IMessage, MemoryMethods } from '../../../src/Interface'
|
import { IMessage, MemoryMethods } from '../../../../src/Interface'
|
||||||
import { mapChatMessageToBaseMessage } from '../../../src/utils'
|
import { mapChatMessageToBaseMessage } from '../../../../src/utils'
|
||||||
|
|
||||||
export class MySQLSaver extends BaseCheckpointSaver implements MemoryMethods {
|
export class MySQLSaver extends BaseCheckpointSaver implements MemoryMethods {
|
||||||
protected isSetup: boolean
|
protected isSetup: boolean
|
||||||
+111
@@ -0,0 +1,111 @@
|
|||||||
|
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../../src/utils'
|
||||||
|
import { SaverOptions } from '../interface'
|
||||||
|
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeParams } from '../../../../src/Interface'
|
||||||
|
import { DataSource } from 'typeorm'
|
||||||
|
import { PostgresSaver } from './pgSaver'
|
||||||
|
|
||||||
|
class PostgresAgentMemory_Memory implements INode {
|
||||||
|
label: string
|
||||||
|
name: string
|
||||||
|
version: number
|
||||||
|
description: string
|
||||||
|
type: string
|
||||||
|
icon: string
|
||||||
|
category: string
|
||||||
|
badge: string
|
||||||
|
baseClasses: string[]
|
||||||
|
inputs: INodeParams[]
|
||||||
|
credential: INodeParams
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.label = 'Postgres Agent Memory'
|
||||||
|
this.name = 'postgresAgentMemory'
|
||||||
|
this.version = 1.0
|
||||||
|
this.type = 'AgentMemory'
|
||||||
|
this.icon = 'postgres.svg'
|
||||||
|
this.category = 'Memory'
|
||||||
|
this.description = 'Memory for agentflow to remember the state of the conversation using Postgres database'
|
||||||
|
this.baseClasses = [this.type, ...getBaseClasses(PostgresSaver)]
|
||||||
|
this.credential = {
|
||||||
|
label: 'Connect Credential',
|
||||||
|
name: 'credential',
|
||||||
|
type: 'credential',
|
||||||
|
credentialNames: ['PostgresApi'],
|
||||||
|
optional: true
|
||||||
|
}
|
||||||
|
this.inputs = [
|
||||||
|
{
|
||||||
|
label: 'Host',
|
||||||
|
name: 'host',
|
||||||
|
type: 'string'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Database',
|
||||||
|
name: 'database',
|
||||||
|
type: 'string'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Port',
|
||||||
|
name: 'port',
|
||||||
|
type: 'number',
|
||||||
|
default: '5432'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Additional Connection Configuration',
|
||||||
|
name: 'additionalConfig',
|
||||||
|
type: 'json',
|
||||||
|
additionalParams: true,
|
||||||
|
optional: true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
|
||||||
|
const additionalConfig = nodeData.inputs?.additionalConfig as string
|
||||||
|
const databaseEntities = options.databaseEntities as IDatabaseEntity
|
||||||
|
const chatflowid = options.chatflowid as string
|
||||||
|
const appDataSource = options.appDataSource as DataSource
|
||||||
|
|
||||||
|
let additionalConfiguration = {}
|
||||||
|
if (additionalConfig) {
|
||||||
|
try {
|
||||||
|
additionalConfiguration = typeof additionalConfig === 'object' ? additionalConfig : JSON.parse(additionalConfig)
|
||||||
|
} catch (exception) {
|
||||||
|
throw new Error('Invalid JSON in the Additional Configuration: ' + exception)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const threadId = options.sessionId || options.chatId
|
||||||
|
|
||||||
|
let datasourceOptions: ICommonObject = {
|
||||||
|
...additionalConfiguration,
|
||||||
|
type: '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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { nodeClass: PostgresAgentMemory_Memory }
|
||||||
+3
-3
@@ -2,9 +2,9 @@ import { BaseCheckpointSaver, Checkpoint, CheckpointMetadata } from '@langchain/
|
|||||||
import { RunnableConfig } from '@langchain/core/runnables'
|
import { RunnableConfig } from '@langchain/core/runnables'
|
||||||
import { BaseMessage } from '@langchain/core/messages'
|
import { BaseMessage } from '@langchain/core/messages'
|
||||||
import { DataSource } from 'typeorm'
|
import { DataSource } from 'typeorm'
|
||||||
import { CheckpointTuple, SaverOptions, SerializerProtocol } from './interface'
|
import { CheckpointTuple, SaverOptions, SerializerProtocol } from '../interface'
|
||||||
import { IMessage, MemoryMethods } from '../../../src/Interface'
|
import { IMessage, MemoryMethods } from '../../../../src/Interface'
|
||||||
import { mapChatMessageToBaseMessage } from '../../../src/utils'
|
import { mapChatMessageToBaseMessage } from '../../../../src/utils'
|
||||||
|
|
||||||
export class PostgresSaver extends BaseCheckpointSaver implements MemoryMethods {
|
export class PostgresSaver extends BaseCheckpointSaver implements MemoryMethods {
|
||||||
protected isSetup: boolean
|
protected isSetup: boolean
|
||||||
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 6.8 KiB |
@@ -0,0 +1,87 @@
|
|||||||
|
import path from 'path'
|
||||||
|
import { getBaseClasses, 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'
|
||||||
|
|
||||||
|
class SQLiteAgentMemory_Memory implements INode {
|
||||||
|
label: string
|
||||||
|
name: string
|
||||||
|
version: number
|
||||||
|
description: string
|
||||||
|
type: string
|
||||||
|
icon: string
|
||||||
|
category: string
|
||||||
|
badge: string
|
||||||
|
baseClasses: string[]
|
||||||
|
inputs: INodeParams[]
|
||||||
|
credential: INodeParams
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.label = 'SQLite Agent Memory'
|
||||||
|
this.name = 'sqliteAgentMemory'
|
||||||
|
this.version = 1.0
|
||||||
|
this.type = 'SQLiteAgentMemory'
|
||||||
|
this.icon = 'sqlite.png'
|
||||||
|
this.category = 'Memory'
|
||||||
|
this.description = 'Memory for agentflow to remember the state of the conversation using SQLite database'
|
||||||
|
this.baseClasses = [this.type, ...getBaseClasses(SqliteSaver)]
|
||||||
|
this.inputs = [
|
||||||
|
/*{
|
||||||
|
label: 'Database File Path',
|
||||||
|
name: 'databaseFilePath',
|
||||||
|
type: 'string',
|
||||||
|
placeholder: 'C:\\Users\\User\\.flowise\\database.sqlite',
|
||||||
|
description: 'Path to the SQLite database file. Leave empty to use default application database',
|
||||||
|
optional: true
|
||||||
|
},*/
|
||||||
|
{
|
||||||
|
label: 'Additional Connection Configuration',
|
||||||
|
name: 'additionalConfig',
|
||||||
|
type: 'json',
|
||||||
|
additionalParams: true,
|
||||||
|
optional: true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
|
||||||
|
const additionalConfig = nodeData.inputs?.additionalConfig as string
|
||||||
|
const databaseEntities = options.databaseEntities as IDatabaseEntity
|
||||||
|
const chatflowid = options.chatflowid as string
|
||||||
|
const appDataSource = options.appDataSource as DataSource
|
||||||
|
|
||||||
|
let additionalConfiguration = {}
|
||||||
|
if (additionalConfig) {
|
||||||
|
try {
|
||||||
|
additionalConfiguration = typeof additionalConfig === 'object' ? additionalConfig : JSON.parse(additionalConfig)
|
||||||
|
} catch (exception) {
|
||||||
|
throw new Error('Invalid JSON in the Additional Configuration: ' + exception)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const threadId = options.sessionId || options.chatId
|
||||||
|
|
||||||
|
const database = path.join(process.env.DATABASE_PATH ?? path.join(getUserHome(), '.flowise'), 'database.sqlite')
|
||||||
|
|
||||||
|
let datasourceOptions: ICommonObject = {
|
||||||
|
database,
|
||||||
|
...additionalConfiguration,
|
||||||
|
type: 'sqlite'
|
||||||
|
}
|
||||||
|
|
||||||
|
const args: SaverOptions = {
|
||||||
|
datasourceOptions,
|
||||||
|
threadId,
|
||||||
|
appDataSource,
|
||||||
|
databaseEntities,
|
||||||
|
chatflowid
|
||||||
|
}
|
||||||
|
|
||||||
|
const recordManager = new SqliteSaver(args)
|
||||||
|
return recordManager
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { nodeClass: SQLiteAgentMemory_Memory }
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 49 KiB |
+3
-3
@@ -2,9 +2,9 @@ import { BaseCheckpointSaver, Checkpoint, CheckpointMetadata } from '@langchain/
|
|||||||
import { RunnableConfig } from '@langchain/core/runnables'
|
import { RunnableConfig } from '@langchain/core/runnables'
|
||||||
import { BaseMessage } from '@langchain/core/messages'
|
import { BaseMessage } from '@langchain/core/messages'
|
||||||
import { DataSource, QueryRunner } from 'typeorm'
|
import { DataSource, QueryRunner } from 'typeorm'
|
||||||
import { CheckpointTuple, SaverOptions, SerializerProtocol } from './interface'
|
import { CheckpointTuple, SaverOptions, SerializerProtocol } from '../interface'
|
||||||
import { IMessage, MemoryMethods } from '../../../src/Interface'
|
import { IMessage, MemoryMethods } from '../../../../src/Interface'
|
||||||
import { mapChatMessageToBaseMessage } from '../../../src/utils'
|
import { mapChatMessageToBaseMessage } from '../../../../src/utils'
|
||||||
|
|
||||||
export class SqliteSaver extends BaseCheckpointSaver implements MemoryMethods {
|
export class SqliteSaver extends BaseCheckpointSaver implements MemoryMethods {
|
||||||
protected isSetup: boolean
|
protected isSetup: boolean
|
||||||
@@ -98,7 +98,8 @@ export const buildAgentGraph = async (
|
|||||||
|
|
||||||
/*** Get Memory Node for Chat History ***/
|
/*** Get Memory Node for Chat History ***/
|
||||||
let chatHistory: IMessage[] = []
|
let chatHistory: IMessage[] = []
|
||||||
const memoryNode = nodes.find((node) => node.data.name === 'agentMemory')
|
const agentMemoryList = ['agentMemory', 'sqliteAgentMemory', 'postgresAgentMemory', 'mySQLAgentMemory']
|
||||||
|
const memoryNode = nodes.find((node) => agentMemoryList.includes(node.data.name))
|
||||||
if (memoryNode) {
|
if (memoryNode) {
|
||||||
chatHistory = await getSessionChatHistory(
|
chatHistory = await getSessionChatHistory(
|
||||||
chatflowid,
|
chatflowid,
|
||||||
|
|||||||
@@ -56,15 +56,17 @@ function a11yProps(index) {
|
|||||||
|
|
||||||
const blacklistCategoriesForAgentCanvas = ['Agents', 'Memory', 'Record Manager', 'Utilities']
|
const blacklistCategoriesForAgentCanvas = ['Agents', 'Memory', 'Record Manager', 'Utilities']
|
||||||
|
|
||||||
|
const agentMemoryNodes = ['agentMemory', 'sqliteAgentMemory', 'postgresAgentMemory', 'mySQLAgentMemory']
|
||||||
|
|
||||||
// Show blacklisted nodes (exceptions) for agent canvas
|
// Show blacklisted nodes (exceptions) for agent canvas
|
||||||
const exceptionsForAgentCanvas = {
|
const exceptionsForAgentCanvas = {
|
||||||
Memory: ['agentMemory'],
|
Memory: agentMemoryNodes,
|
||||||
Utilities: ['getVariable', 'setVariable', 'stickyNote']
|
Utilities: ['getVariable', 'setVariable', 'stickyNote']
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hide some nodes from the chatflow canvas
|
// Hide some nodes from the chatflow canvas
|
||||||
const blacklistForChatflowCanvas = {
|
const blacklistForChatflowCanvas = {
|
||||||
Memory: ['agentMemory']
|
Memory: agentMemoryNodes
|
||||||
}
|
}
|
||||||
|
|
||||||
const AddNodes = ({ nodesData, node, isAgentCanvas }) => {
|
const AddNodes = ({ nodesData, node, isAgentCanvas }) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user