Skip to content

Commit cadc3b8

Browse files
authored
Feature/Add dedicated agent memory nodes (#3649)
add dedicated agent memory nodes
1 parent fe2ed26 commit cadc3b8

File tree

12 files changed

+330
-15
lines changed

12 files changed

+330
-15
lines changed

packages/components/nodes/memory/AgentMemory/AgentMemory.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import path from 'path'
22
import { getBaseClasses, getCredentialData, getCredentialParam, getUserHome } from '../../../src/utils'
33
import { SaverOptions } from './interface'
44
import { ICommonObject, IDatabaseEntity, INode, INodeData, INodeParams } from '../../../src/Interface'
5-
import { SqliteSaver } from './sqliteSaver'
5+
import { SqliteSaver } from './SQLiteAgentMemory/sqliteSaver'
66
import { DataSource } from 'typeorm'
7-
import { PostgresSaver } from './pgSaver'
8-
import { MySQLSaver } from './mysqlSaver'
7+
import { PostgresSaver } from './PostgresAgentMemory/pgSaver'
8+
import { MySQLSaver } from './MySQLAgentMemory/mysqlSaver'
99

1010
class AgentMemory_Memory implements INode {
1111
label: string
@@ -29,6 +29,7 @@ class AgentMemory_Memory implements INode {
2929
this.category = 'Memory'
3030
this.description = 'Memory for agentflow to remember the state of the conversation'
3131
this.baseClasses = [this.type, ...getBaseClasses(SqliteSaver)]
32+
this.badge = 'DEPRECATING'
3233
this.credential = {
3334
label: 'Connect Credential',
3435
name: 'credential',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 { MySQLSaver } from './mysqlSaver'
6+
7+
class MySQLAgentMemory_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 = 'MySQL Agent Memory'
22+
this.name = 'mySQLAgentMemory'
23+
this.version = 1.0
24+
this.type = 'AgentMemory'
25+
this.icon = 'mysql.png'
26+
this.category = 'Memory'
27+
this.description = 'Memory for agentflow to remember the state of the conversation using MySQL database'
28+
this.baseClasses = [this.type, ...getBaseClasses(MySQLSaver)]
29+
this.credential = {
30+
label: 'Connect Credential',
31+
name: 'credential',
32+
type: 'credential',
33+
credentialNames: ['MySQLApi'],
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: '3306'
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: 'mysql'
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) || '3306'
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+
charset: 'utf8mb4'
99+
}
100+
const args: SaverOptions = {
101+
datasourceOptions,
102+
threadId,
103+
appDataSource,
104+
databaseEntities,
105+
chatflowid
106+
}
107+
const recordManager = new MySQLSaver(args)
108+
return recordManager
109+
}
110+
}
111+
112+
module.exports = { nodeClass: MySQLAgentMemory_Memory }
Loading

packages/components/nodes/memory/AgentMemory/mysqlSaver.ts renamed to packages/components/nodes/memory/AgentMemory/MySQLAgentMemory/mysqlSaver.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { BaseCheckpointSaver, Checkpoint, CheckpointMetadata } from '@langchain/
22
import { RunnableConfig } from '@langchain/core/runnables'
33
import { BaseMessage } from '@langchain/core/messages'
44
import { DataSource } from 'typeorm'
5-
import { CheckpointTuple, SaverOptions, SerializerProtocol } from './interface'
6-
import { IMessage, MemoryMethods } from '../../../src/Interface'
7-
import { mapChatMessageToBaseMessage } from '../../../src/utils'
5+
import { CheckpointTuple, SaverOptions, SerializerProtocol } from '../interface'
6+
import { IMessage, MemoryMethods } from '../../../../src/Interface'
7+
import { mapChatMessageToBaseMessage } from '../../../../src/utils'
88

99
export class MySQLSaver extends BaseCheckpointSaver implements MemoryMethods {
1010
protected isSetup: boolean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 }

packages/components/nodes/memory/AgentMemory/pgSaver.ts renamed to packages/components/nodes/memory/AgentMemory/PostgresAgentMemory/pgSaver.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { BaseCheckpointSaver, Checkpoint, CheckpointMetadata } from '@langchain/
22
import { RunnableConfig } from '@langchain/core/runnables'
33
import { BaseMessage } from '@langchain/core/messages'
44
import { DataSource } from 'typeorm'
5-
import { CheckpointTuple, SaverOptions, SerializerProtocol } from './interface'
6-
import { IMessage, MemoryMethods } from '../../../src/Interface'
7-
import { mapChatMessageToBaseMessage } from '../../../src/utils'
5+
import { CheckpointTuple, SaverOptions, SerializerProtocol } from '../interface'
6+
import { IMessage, MemoryMethods } from '../../../../src/Interface'
7+
import { mapChatMessageToBaseMessage } from '../../../../src/utils'
88

99
export class PostgresSaver extends BaseCheckpointSaver implements MemoryMethods {
1010
protected isSetup: boolean
Loading

0 commit comments

Comments
 (0)