Skip to content
Open
Changes from all commits
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
9 changes: 7 additions & 2 deletions astrbot/core/agent/runners/tool_loop_agent_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,9 @@ async def step(self):
# 如果有工具调用,还需处理工具调用
if llm_resp.tools_call_name:
if self.tool_schema_mode == "skills_like":
llm_resp, _ = await self._resolve_tool_exec(llm_resp)
if not llm_resp.tools_call_name:
requery_resp, _ = await self._resolve_tool_exec(llm_resp)
if not requery_resp.tools_call_name:
Comment on lines +819 to +820
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

skills_like 模式下,重新查询(re-query)产生的 token 消耗目前没有被计入 self.stats.token_usage。这会导致代理运行的 token 统计不准确。建议在获取 requery_resp 后,如果它是一个新的响应对象,则累加其 usage,并同步更新会话的 token 使用量。

Suggested change
requery_resp, _ = await self._resolve_tool_exec(llm_resp)
if not requery_resp.tools_call_name:
requery_resp, _ = await self._resolve_tool_exec(llm_resp)
if requery_resp is not llm_resp and requery_resp.usage:
self.stats.token_usage += requery_resp.usage
if self.req.conversation:
self.req.conversation.token_usage = requery_resp.usage.total
if not requery_resp.tools_call_name:

llm_resp = requery_resp
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

如果 re-query 返回了错误响应(role == "err"),当前逻辑在 fallback 路径下会将其直接赋值给 llm_resp 并继续执行,最终会调用 _complete_with_assistant_response 将代理状态设置为 DONE。这会导致错误被当作正常的助手回复处理,建议在此处增加对 requery_resp.role == "err" 的判断,并按照标准的错误流程处理(例如跳转到错误状态并返回错误响应)。

logger.warning(
"skills_like tool re-query returned no tool calls; fallback to assistant response."
)
Expand Down Expand Up @@ -845,6 +846,10 @@ async def step(self):

await self._complete_with_assistant_response(llm_resp)
return
else:
llm_resp.tools_call_name = requery_resp.tools_call_name
llm_resp.tools_call_args = requery_resp.tools_call_args
llm_resp.tools_call_ids = requery_resp.tools_call_ids

tool_call_result_blocks = []
cached_images = [] # Collect cached images for LLM visibility
Expand Down