22from typing import Dict , List , Union
33
44from consts .const import (
5- MEMORY_SWITCH_KEY ,
6- MEMORY_AGENT_SHARE_KEY ,
7- DISABLE_AGENT_ID_KEY ,
8- DISABLE_USERAGENT_ID_KEY ,
9- DEFAULT_MEMORY_SWITCH_KEY ,
10- DEFAULT_MEMORY_AGENT_SHARE_KEY ,
5+ MEMORY_SWITCH_KEY ,
6+ MEMORY_AGENT_SHARE_KEY ,
7+ DISABLE_AGENT_ID_KEY ,
8+ DISABLE_USERAGENT_ID_KEY ,
9+ DEFAULT_MEMORY_SWITCH_KEY ,
10+ DEFAULT_MEMORY_AGENT_SHARE_KEY ,
1111)
1212from consts .model import MemoryAgentShareMode
1313from database .memory_config_db import (
14- get_all_configs_by_user_id ,
15- get_memory_config_info ,
16- insert_config ,
17- delete_config_by_config_id ,
18- update_config_by_id ,
14+ get_all_configs_by_user_id ,
15+ get_memory_config_info ,
16+ insert_config ,
17+ delete_config_by_config_id ,
18+ update_config_by_id ,
1919)
2020from nexent .core .agents .agent_model import MemoryContext , MemoryUserConfig
2121from utils .memory_utils import build_memory_config
3131# ---------------------------------------------------------------------------
3232
3333def _aggregate_records (records : List [Dict [str , Union [str , int ]]]) -> Dict [str , Union [str , List [str ]]]:
34- """Aggregate DB rows -> {config_key: value_or_list}"""
35- aggregated : Dict [str , Union [str , List [str ]]] = {}
36- for r in records :
37- key = r ["config_key" ]
38- if r .get ("value_type" ) == _MULTI_TYPE :
39- aggregated .setdefault (key , []).append (r ["config_value" ])
40- else :
41- aggregated [key ] = r ["config_value" ]
42- return aggregated
34+ """Aggregate DB rows -> {config_key: value_or_list}"""
35+ aggregated : Dict [str , Union [str , List [str ]]] = {}
36+ for r in records :
37+ key = r ["config_key" ]
38+ if r .get ("value_type" ) == _MULTI_TYPE :
39+ aggregated .setdefault (key , []).append (r ["config_value" ])
40+ else :
41+ aggregated [key ] = r ["config_value" ]
42+ return aggregated
4343
4444
4545# ---------------------------------------------------------------------------
@@ -64,154 +64,154 @@ def get_user_configs(user_id: str) -> Dict[str, Union[str, List[str]]]:
6464
6565
6666def _update_single_config (user_id : str , config_key : str , config_value : str ) -> bool :
67- """Create or update a single-type configuration entry."""
68- record_list = get_memory_config_info (user_id , config_key )
69-
70- if not record_list :
71- # Insert new record
72- result = insert_config ({
73- "user_id" : user_id ,
74- "config_key" : config_key ,
75- "config_value" : config_value ,
76- "value_type" : _SINGLE_TYPE ,
77- "created_by" : user_id ,
78- "updated_by" : user_id ,
79- })
80- if not result :
81- logger .error (
82- f"insert_config failed, user_id={ user_id } , key={ config_key } , value={ config_value } "
83- )
84- return False
85- else :
86- # Update first record (there should be max one for single type)
87- config_id = record_list [0 ]["config_id" ]
88- result = update_config_by_id (config_id , {
89- "config_value" : config_value ,
90- "updated_by" : user_id ,
91- })
92- if not result :
93- logger .error (
94- f"update_config_by_id failed, user_id={ user_id } , key={ config_key } , value={ config_value } "
95- )
96- return False
97- return True
67+ """Create or update a single-type configuration entry."""
68+ record_list = get_memory_config_info (user_id , config_key )
69+
70+ if not record_list :
71+ # Insert new record
72+ result = insert_config ({
73+ "user_id" : user_id ,
74+ "config_key" : config_key ,
75+ "config_value" : config_value ,
76+ "value_type" : _SINGLE_TYPE ,
77+ "created_by" : user_id ,
78+ "updated_by" : user_id ,
79+ })
80+ if not result :
81+ logger .error (
82+ f"insert_config failed, user_id={ user_id } , key={ config_key } , value={ config_value } "
83+ )
84+ return False
85+ else :
86+ # Update first record (there should be max one for single type)
87+ config_id = record_list [0 ]["config_id" ]
88+ result = update_config_by_id (config_id , {
89+ "config_value" : config_value ,
90+ "updated_by" : user_id ,
91+ })
92+ if not result :
93+ logger .error (
94+ f"update_config_by_id failed, user_id={ user_id } , key={ config_key } , value={ config_value } "
95+ )
96+ return False
97+ return True
9898
9999
100100def _add_multi_value (user_id : str , config_key : str , value : str ) -> bool :
101- """Add a value to a multi-type list if it does not exist."""
102- record_list = get_memory_config_info (user_id , config_key )
103- if any (r ["config_value" ] == value for r in record_list ):
104- # Already exists, nothing to do
105- return True
106-
107- ok = insert_config ({
108- "user_id" : user_id ,
109- "config_key" : config_key ,
110- "config_value" : value ,
111- "value_type" : _MULTI_TYPE ,
112- "created_by" : user_id ,
113- "updated_by" : user_id ,
114- })
115- if not ok :
116- logger .error (
117- f"insert_config failed, user_id={ user_id } , key={ config_key } , value={ value } "
118- )
119- return ok
101+ """Add a value to a multi-type list if it does not exist."""
102+ record_list = get_memory_config_info (user_id , config_key )
103+ if any (r ["config_value" ] == value for r in record_list ):
104+ # Already exists, nothing to do
105+ return True
106+
107+ ok = insert_config ({
108+ "user_id" : user_id ,
109+ "config_key" : config_key ,
110+ "config_value" : value ,
111+ "value_type" : _MULTI_TYPE ,
112+ "created_by" : user_id ,
113+ "updated_by" : user_id ,
114+ })
115+ if not ok :
116+ logger .error (
117+ f"insert_config failed, user_id={ user_id } , key={ config_key } , value={ value } "
118+ )
119+ return ok
120120
121121
122122def _remove_multi_value (user_id : str , config_key : str , value : str ) -> bool :
123- """Soft-delete a specific value from a multi-type configuration list."""
124- record_list = get_memory_config_info (user_id , config_key )
125- for r in record_list :
126- if r ["config_value" ] == value :
127- ok = delete_config_by_config_id (r ["config_id" ], updated_by = user_id )
128- if not ok :
129- logger .error (
130- f"delete_config_by_config_id failed, user_id={ user_id } , key={ config_key } , value={ value } "
131- )
132- return ok
133- # Value not found → treat as success
134- return True
123+ """Soft-delete a specific value from a multi-type configuration list."""
124+ record_list = get_memory_config_info (user_id , config_key )
125+ for r in record_list :
126+ if r ["config_value" ] == value :
127+ ok = delete_config_by_config_id (r ["config_id" ], updated_by = user_id )
128+ if not ok :
129+ logger .error (
130+ f"delete_config_by_config_id failed, user_id={ user_id } , key={ config_key } , value={ value } "
131+ )
132+ return ok
133+ # Value not found → treat as success
134+ return True
135135
136136
137137# ---------------------------------------------------------------------------
138138# Public service helpers used by API layer
139139# ---------------------------------------------------------------------------
140140
141141def get_memory_switch (user_id : str ) -> bool :
142- configs = get_user_configs (user_id )
143- return configs .get (MEMORY_SWITCH_KEY , "N" ) == "Y"
142+ configs = get_user_configs (user_id )
143+ return configs .get (MEMORY_SWITCH_KEY , "N" ) == "Y"
144144
145145
146146def set_memory_switch (user_id : str , enabled : bool ) -> bool :
147- return _update_single_config (user_id , MEMORY_SWITCH_KEY , "Y" if enabled else "N" )
147+ return _update_single_config (user_id , MEMORY_SWITCH_KEY , "Y" if enabled else "N" )
148148
149149
150150# Agent share (single string among always/ask/never)
151151def get_agent_share (user_id : str ) -> MemoryAgentShareMode :
152- configs = get_user_configs (user_id )
153- mode_str = configs .get (MEMORY_AGENT_SHARE_KEY , MemoryAgentShareMode .NEVER .value )
154- try :
155- return MemoryAgentShareMode (mode_str )
156- except ValueError :
157- # Unexpected value, default to NEVER
158- return MemoryAgentShareMode .NEVER
152+ configs = get_user_configs (user_id )
153+ mode_str = configs .get (MEMORY_AGENT_SHARE_KEY , MemoryAgentShareMode .NEVER .value )
154+ try :
155+ return MemoryAgentShareMode (mode_str )
156+ except ValueError :
157+ # Unexpected value, default to NEVER
158+ return MemoryAgentShareMode .NEVER
159159
160160
161161def set_agent_share (user_id : str , mode : MemoryAgentShareMode ) -> bool :
162- return _update_single_config (user_id , MEMORY_AGENT_SHARE_KEY , mode .value )
162+ return _update_single_config (user_id , MEMORY_AGENT_SHARE_KEY , mode .value )
163163
164164
165165# Disable agent id list (multi)
166166def get_disabled_agent_ids (user_id : str ) -> List [str ]:
167- configs = get_user_configs (user_id )
168- return configs .get (DISABLE_AGENT_ID_KEY , []) # type: ignore[return-value]
167+ configs = get_user_configs (user_id )
168+ return configs .get (DISABLE_AGENT_ID_KEY , []) # type: ignore[return-value]
169169
170170
171171def add_disabled_agent_id (user_id : str , agent_id : str ) -> bool :
172- return _add_multi_value (user_id , DISABLE_AGENT_ID_KEY , agent_id )
172+ return _add_multi_value (user_id , DISABLE_AGENT_ID_KEY , agent_id )
173173
174174
175175def remove_disabled_agent_id (user_id : str , agent_id : str ) -> bool :
176- return _remove_multi_value (user_id , DISABLE_AGENT_ID_KEY , agent_id )
176+ return _remove_multi_value (user_id , DISABLE_AGENT_ID_KEY , agent_id )
177177
178178
179179# Disable user-agent id list (multi)
180180
181181def get_disabled_useragent_ids (user_id : str ) -> List [str ]:
182- configs = get_user_configs (user_id )
183- return configs .get (DISABLE_USERAGENT_ID_KEY , []) # type: ignore[return-value]
182+ configs = get_user_configs (user_id )
183+ return configs .get (DISABLE_USERAGENT_ID_KEY , []) # type: ignore[return-value]
184184
185185
186186def add_disabled_useragent_id (user_id : str , ua_id : str ) -> bool :
187- return _add_multi_value (user_id , DISABLE_USERAGENT_ID_KEY , ua_id )
187+ return _add_multi_value (user_id , DISABLE_USERAGENT_ID_KEY , ua_id )
188188
189189
190190def remove_disabled_useragent_id (user_id : str , ua_id : str ) -> bool :
191- return _remove_multi_value (user_id , DISABLE_USERAGENT_ID_KEY , ua_id )
191+ return _remove_multi_value (user_id , DISABLE_USERAGENT_ID_KEY , ua_id )
192192
193193
194194def build_memory_context (user_id : str , tenant_id : str , agent_id : str | int ) -> MemoryContext :
195- memory_user_config = MemoryUserConfig (
196- memory_switch = get_memory_switch (user_id ),
197- agent_share_option = get_agent_share (user_id ).value ,
198- disable_agent_ids = get_disabled_agent_ids (user_id ),
199- disable_user_agent_ids = get_disabled_useragent_ids (user_id ),
200- )
201- # If user turn off the memory function, return minimum context directly
202- if not memory_user_config .memory_switch :
203- return MemoryContext (
204- user_config = memory_user_config ,
205- memory_config = dict (),
206- tenant_id = tenant_id ,
207- user_id = user_id ,
208- agent_id = str (agent_id ),
209- )
210-
211- return MemoryContext (
212- user_config = memory_user_config ,
213- memory_config = build_memory_config (tenant_id ),
214- tenant_id = tenant_id ,
215- user_id = user_id ,
216- agent_id = str (agent_id ),
217- )
195+ memory_user_config = MemoryUserConfig (
196+ memory_switch = get_memory_switch (user_id ),
197+ agent_share_option = get_agent_share (user_id ).value ,
198+ disable_agent_ids = get_disabled_agent_ids (user_id ),
199+ disable_user_agent_ids = get_disabled_useragent_ids (user_id ),
200+ )
201+ # If user turn off the memory function, return minimum context directly
202+ if not memory_user_config .memory_switch :
203+ return MemoryContext (
204+ user_config = memory_user_config ,
205+ memory_config = dict (),
206+ tenant_id = tenant_id ,
207+ user_id = user_id ,
208+ agent_id = str (agent_id ),
209+ )
210+
211+ return MemoryContext (
212+ user_config = memory_user_config ,
213+ memory_config = build_memory_config (tenant_id ),
214+ tenant_id = tenant_id ,
215+ user_id = user_id ,
216+ agent_id = str (agent_id ),
217+ )
0 commit comments