From 6be6d85d3bd84b4112791e3a436dd0382c5a4b23 Mon Sep 17 00:00:00 2001 From: SnowindMe <1945743455@qq.com> Date: Sat, 19 Apr 2025 19:17:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E4=B8=8Brun=5Flua?= =?UTF-8?q?=5Fcode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tools/tool_can_use/base_tool.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/tools/tool_can_use/base_tool.py b/src/tools/tool_can_use/base_tool.py index 801a1fe1..781c5879 100644 --- a/src/tools/tool_can_use/base_tool.py +++ b/src/tools/tool_can_use/base_tool.py @@ -124,8 +124,20 @@ def run_lua_code(lua_code: str): Returns: _LuaTable: Lua运行时的全局变量 """ - from lupa import LuaRuntime + try: + from lupa import LuaRuntime + except ImportError as e: + raise ImportError("无法导入lupa模块,请确保已安装lupa库(可通过pip安装)") from e lua = LuaRuntime(unpack_returned_tuples=True) - lua.execute(lua_code) - return lua.globals() + try: + lua.execute(lua_code) + return lua.globals() + except Exception as e: + # 返回包含错误信息的字典而不是直接抛出异常,保持函数接口的稳定性 + return { + "__error__": True, + "type": type(e).__name__, + "message": "Lua 代码执行出错,请检查代码是否正确。", + "lua_code": lua_code # 可选:返回出错的代码片段便于调试 + }