mirror of
https://github.com/1Panel-dev/MaxKB.git
synced 2025-12-30 09:42:48 +00:00
feat: refactor MCP code generation and update cleanup logic
This commit is contained in:
parent
1837512abc
commit
541f1f26c8
|
|
@ -281,7 +281,7 @@ class BaseChatNode(IChatNode):
|
|||
for tool_id in tool_ids:
|
||||
tool = QuerySet(Tool).filter(id=tool_id).first()
|
||||
executor = ToolExecutor()
|
||||
code = executor.get_exec_code(tool.code)
|
||||
code = executor.generate_mcp_server_code(tool.code)
|
||||
code_path = f'{executor.sandbox_path}/execute/{tool_id}.py'
|
||||
with open(code_path, 'w') as f:
|
||||
f.write(code)
|
||||
|
|
@ -340,8 +340,8 @@ class BaseChatNode(IChatNode):
|
|||
# 清理工具代码文件,延时删除,避免文件被占用
|
||||
for tool_id in self.context.get('tool_ids'):
|
||||
code_path = f'{executor.sandbox_path}/execute/{tool_id}.py'
|
||||
if os.path.exists(code_path):
|
||||
os.remove(code_path)
|
||||
# if os.path.exists(code_path):
|
||||
# os.remove(code_path)
|
||||
return {
|
||||
'name': self.node.properties.get('stepName'),
|
||||
"index": index,
|
||||
|
|
|
|||
|
|
@ -116,14 +116,13 @@ except Exception as e:
|
|||
|
||||
return "\n".join(code_parts)
|
||||
|
||||
def get_exec_code(self, code_str):
|
||||
def generate_mcp_server_code(self, code_str):
|
||||
python_paths = CONFIG.get_sandbox_python_package_paths().split(',')
|
||||
code = self._generate_mcp_server_code(code_str)
|
||||
return f"""
|
||||
try:
|
||||
import os
|
||||
import sys
|
||||
import pickle
|
||||
path_to_exclude = ['/opt/py3/lib/python3.11/site-packages', '/opt/maxkb-app/apps']
|
||||
sys.path = [p for p in sys.path if p not in path_to_exclude]
|
||||
sys.path += {python_paths}
|
||||
|
|
@ -131,12 +130,7 @@ try:
|
|||
for key in list(env.keys()):
|
||||
if key in os.environ and (key.startswith('MAXKB') or key.startswith('POSTGRES') or key.startswith('PG') or key.startswith('REDIS') or key == 'PATH'):
|
||||
del os.environ[key]
|
||||
locals_v={'{}'}
|
||||
globals_v=globals()
|
||||
exec({dedent(code)!a}, globals_v, locals_v)
|
||||
f_name, f = locals_v.popitem()
|
||||
for local in locals_v:
|
||||
globals_v[local] = locals_v[local]
|
||||
exec({dedent(code)!a})
|
||||
except Exception as e:
|
||||
pass
|
||||
"""
|
||||
|
|
|
|||
Loading…
Reference in New Issue