Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions astrbot/core/agent/runners/tool_loop_agent_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,35 @@ async def _handle_function_tools(
)
continue

# 参数过滤:只传递函数实际需要的参数
import inspect
valid_params = {}

# 获取实际的 handler 函数
handler_func = func_tool.handler
if handler_func:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): 我们发现了这些问题:

Suggested change
# 获取实际的 handler 函数
handler_func = func_tool.handler
if handler_func:
if handler_func := func_tool.handler:


解释
此函数的质量分数低于 25% 的质量阈值。
此分数是方法长度、认知复杂度和工作内存的组合。

如何解决这个问题?

重构此函数以使其更短、更易读可能是有益的。

  • 通过将部分功能提取到自己的函数中来减少函数长度。这是您可以做的最重要的事情 - 理想情况下,一个函数应该少于 10 行。
  • 减少嵌套,也许通过引入守卫子句来提前返回。
  • 确保变量的作用域紧密,以便使用相关概念的代码在函数中坐在一起,而不是分散。
Original comment in English

suggestion (code-quality): We've found these issues:

Suggested change
# 获取实际的 handler 函数
handler_func = func_tool.handler
if handler_func:
if handler_func := func_tool.handler:


Explanation
The quality score for this function is below the quality threshold of 25%.
This score is a combination of the method length, cognitive complexity and working memory.

How can you solve this?

It might be worth refactoring this function to make it shorter and more readable.

  • Reduce the function length by extracting pieces of functionality out into
    their own functions. This is the most important thing you can do - ideally a
    function should be less than 10 lines.
  • Reduce nesting, perhaps by introducing guard clauses to return early.
  • Ensure that variables are tightly scoped, so that code using related concepts
    sits together within the function rather than being scattered.

sig = inspect.signature(handler_func)
for param_name, param_value in func_tool_args.items():
if param_name in sig.parameters:
valid_params[param_name] = param_value
else:
logger.warning(f"工具 {func_tool_name} 忽略未知参数: {param_name}")
else:
# 如果没有 handler(如 MCP 工具),使用所有参数
valid_params = func_tool_args
logger.warning(f"工具 {func_tool_name} 没有 handler,使用所有参数")

try:
await self.agent_hooks.on_tool_start(
self.run_context, func_tool, func_tool_args
self.run_context, func_tool, valid_params
)
except Exception as e:
logger.error(f"Error in on_tool_start hook: {e}", exc_info=True)

executor = self.tool_executor.execute(
tool=func_tool,
run_context=self.run_context,
**func_tool_args,
**valid_params,
)

_final_resp: CallToolResult | None = None
Expand Down