2424
2525
2626async def create_model_config_list (tenant_id ):
27- main_model_config = tenant_config_manager .get_model_config (key = "LLM_ID" , tenant_id = tenant_id )
28- sub_model_config = tenant_config_manager .get_model_config (key = "LLM_SECONDARY_ID" , tenant_id = tenant_id )
27+ main_model_config = tenant_config_manager .get_model_config (
28+ key = "LLM_ID" , tenant_id = tenant_id )
29+ sub_model_config = tenant_config_manager .get_model_config (
30+ key = "LLM_SECONDARY_ID" , tenant_id = tenant_id )
2931
3032 return [ModelConfig (cite_name = "main_model" ,
3133 api_key = main_model_config .get ("api_key" , "" ),
@@ -40,10 +42,12 @@ async def create_model_config_list(tenant_id):
4042
4143
4244async def create_agent_config (agent_id , tenant_id , user_id , language : str = 'zh' , last_user_query : str = None ):
43- agent_info = search_agent_info_by_agent_id (agent_id = agent_id , tenant_id = tenant_id )
45+ agent_info = search_agent_info_by_agent_id (
46+ agent_id = agent_id , tenant_id = tenant_id )
4447
4548 # create sub agent
46- sub_agent_id_list = query_sub_agents_id_list (main_agent_id = agent_id , tenant_id = tenant_id )
49+ sub_agent_id_list = query_sub_agents_id_list (
50+ main_agent_id = agent_id , tenant_id = tenant_id )
4751 managed_agents = []
4852 for sub_agent_id in sub_agent_id_list :
4953 sub_agent_config = await create_agent_config (
@@ -55,19 +59,22 @@ async def create_agent_config(agent_id, tenant_id, user_id, language: str = 'zh'
5559 managed_agents .append (sub_agent_config )
5660
5761 tool_list = await create_tool_config_list (agent_id , tenant_id , user_id )
58-
62+
5963 # Build system prompt: prioritize segmented fields, fallback to original prompt field if not available
6064 duty_prompt = agent_info .get ("duty_prompt" , "" )
6165 constraint_prompt = agent_info .get ("constraint_prompt" , "" )
6266 few_shots_prompt = agent_info .get ("few_shots_prompt" , "" )
63-
67+
6468 # Get template content
65- prompt_template = get_agent_prompt_template (is_manager = len (managed_agents ) > 0 , language = language )
69+ prompt_template = get_agent_prompt_template (
70+ is_manager = len (managed_agents ) > 0 , language = language )
6671
6772 # Get app information
6873 default_app_description = 'Nexent 是一个开源智能体SDK和平台' if language == 'zh' else 'Nexent is an open-source agent SDK and platform'
69- app_name = tenant_config_manager .get_app_config ('APP_NAME' , tenant_id = tenant_id ) or "Nexent"
70- app_description = tenant_config_manager .get_app_config ('APP_DESCRIPTION' , tenant_id = tenant_id ) or default_app_description
74+ app_name = tenant_config_manager .get_app_config (
75+ 'APP_NAME' , tenant_id = tenant_id ) or "Nexent"
76+ app_description = tenant_config_manager .get_app_config (
77+ 'APP_DESCRIPTION' , tenant_id = tenant_id ) or default_app_description
7178
7279 # Get memory list
7380 memory_context = build_memory_context (user_id , tenant_id , agent_id )
@@ -84,7 +91,8 @@ async def create_agent_config(agent_id, tenant_id, user_id, language: str = 'zh'
8491 memory_levels .remove ("user_agent" )
8592
8693 search_res = await search_memory_in_levels (
87- query_text = last_user_query if last_user_query else agent_info .get ("name" ),
94+ query_text = last_user_query if last_user_query else agent_info .get (
95+ "name" ),
8896 memory_config = memory_context .memory_config ,
8997 tenant_id = memory_context .tenant_id ,
9098 user_id = memory_context .user_id ,
@@ -100,7 +108,8 @@ async def create_agent_config(agent_id, tenant_id, user_id, language: str = 'zh'
100108 try :
101109 for tool in tool_list :
102110 if "KnowledgeBaseSearchTool" == tool .class_name :
103- knowledge_info_list = get_selected_knowledge_list (tenant_id = tenant_id , user_id = user_id )
111+ knowledge_info_list = get_selected_knowledge_list (
112+ tenant_id = tenant_id , user_id = user_id )
104113 if knowledge_info_list :
105114 for knowledge_info in knowledge_info_list :
106115 knowledge_name = knowledge_info .get ("index_name" )
@@ -109,13 +118,14 @@ async def create_agent_config(agent_id, tenant_id, user_id, language: str = 'zh'
109118 summary = message .get ("summary" , "" )
110119 knowledge_base_summary += f"**{ knowledge_name } **: { summary } \n \n "
111120 except Exception as e :
112- logger .warning (f"Failed to get summary for knowledge base { knowledge_name } : { e } " )
121+ logger .warning (
122+ f"Failed to get summary for knowledge base { knowledge_name } : { e } " )
113123 else :
114124 knowledge_base_summary = "当前没有可用的知识库索引。\n " if language == 'zh' else "No knowledge base indexes are currently available.\n "
115125 break # Only process the first KnowledgeBaseSearchTool found
116126 except Exception as e :
117127 logger .error (f"Failed to build knowledge base summary: { e } " )
118-
128+
119129 # Assemble system_prompt
120130 if duty_prompt or constraint_prompt or few_shots_prompt :
121131 system_prompt = Template (prompt_template ["system_prompt" ], undefined = StrictUndefined ).render ({
@@ -133,7 +143,7 @@ async def create_agent_config(agent_id, tenant_id, user_id, language: str = 'zh'
133143 })
134144 else :
135145 system_prompt = agent_info .get ("prompt" , "" )
136-
146+
137147 agent_config = AgentConfig (
138148 name = "undefined" if agent_info ["name" ] is None else agent_info ["name" ],
139149 description = "undefined" if agent_info ["description" ] is None else agent_info ["description" ],
@@ -182,25 +192,27 @@ async def create_tool_config_list(agent_id, tenant_id, user_id):
182192
183193 # special logic for knowledge base search tool
184194 if tool_config .class_name == "KnowledgeBaseSearchTool" :
185- knowledge_info_list = get_selected_knowledge_list (tenant_id = tenant_id , user_id = user_id )
186- index_names = [knowledge_info .get ("index_name" ) for knowledge_info in knowledge_info_list ]
195+ knowledge_info_list = get_selected_knowledge_list (
196+ tenant_id = tenant_id , user_id = user_id )
197+ index_names = [knowledge_info .get (
198+ "index_name" ) for knowledge_info in knowledge_info_list ]
187199 tool_config .metadata = {"index_names" : index_names ,
188200 "es_core" : elastic_core ,
189201 "embedding_model" : get_embedding_model (tenant_id = tenant_id )}
190202 tool_config_list .append (tool_config )
191-
203+
192204 return tool_config_list
193205
194206
195207async def discover_langchain_tools ():
196208 """
197209 Discover LangChain tools implemented with the `@tool` decorator.
198-
210+
199211 Returns:
200212 list: List of discovered LangChain tool instances
201213 """
202214 from utils .langchain_utils import discover_langchain_modules
203-
215+
204216 langchain_tools = []
205217
206218 # ----------------------------------------------
@@ -210,18 +222,21 @@ async def discover_langchain_tools():
210222 try :
211223 # Use the utility function to discover all BaseTool objects
212224 discovered_tools = discover_langchain_modules ()
213-
225+
214226 for obj , filename in discovered_tools :
215227 try :
216228 # Log successful tool discovery
217- logger .info (f"Loaded LangChain tool '{ obj .name } ' from { filename } " )
229+ logger .info (
230+ f"Loaded LangChain tool '{ obj .name } ' from { filename } " )
218231 langchain_tools .append (obj )
219232 except Exception as e :
220- logger .error (f"Error processing LangChain tool from { filename } : { e } " )
221-
233+ logger .error (
234+ f"Error processing LangChain tool from { filename } : { e } " )
235+
222236 except Exception as e :
223- logger .error (f"Unexpected error scanning LangChain tools directory: { e } " )
224-
237+ logger .error (
238+ f"Unexpected error scanning LangChain tools directory: { e } " )
239+
225240 return langchain_tools
226241
227242
@@ -269,7 +284,8 @@ def check_agent_tools(agent_config: AgentConfig):
269284 # Check current agent tools
270285 for tool in agent_config .tools :
271286 if tool .source == "mcp" and tool .usage in mcp_info_dict :
272- used_mcp_urls .add (mcp_info_dict [tool .usage ]["remote_mcp_server" ])
287+ used_mcp_urls .add (
288+ mcp_info_dict [tool .usage ]["remote_mcp_server" ])
273289
274290 # Recursively check sub-agent
275291 for sub_agent_config in agent_config .managed_agents :
@@ -295,13 +311,15 @@ async def create_agent_run_info(agent_id, minio_files, query, history, authoriza
295311 )
296312
297313 remote_mcp_list = await get_remote_mcp_server_list (tenant_id = tenant_id )
298- default_mcp_url = urljoin (config_manager .get_config ("NEXENT_MCP_SERVER" ), "sse" )
314+ default_mcp_url = urljoin (
315+ config_manager .get_config ("NEXENT_MCP_SERVER" ), "sse" )
299316 remote_mcp_list .append ({
300317 "remote_mcp_server_name" : "nexent" ,
301318 "remote_mcp_server" : default_mcp_url ,
302319 "status" : True
303320 })
304- remote_mcp_dict = {record ["remote_mcp_server_name" ]: record for record in remote_mcp_list if record ["status" ]}
321+ remote_mcp_dict = {record ["remote_mcp_server_name" ]
322+ : record for record in remote_mcp_list if record ["status" ]}
305323
306324 # Filter MCP servers and tools
307325 mcp_host = filter_mcp_servers_and_tools (agent_config , remote_mcp_dict )
0 commit comments