AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

[Bug] Tool Call Cleaner 插件在 DeepSeek v4 flash 下清空所有工具调用上下文

Open z454963058-sudo opened this issue 5 months ago • 3 comments

Tool Call Cleaner 插件:DeepSeek v4 flash 兼容性导致工具调用上下文被完全清空

描述

astrbot_plugin_tool_use_cleaner(工具调用清洗器)插件在与 DeepSeek v4 flash 配合使用时,存在兼容性 bug,导致所有工具调用轮次的上下文被完全清空,LLM 在后续对话中丢失之前所有工具调用的记忆。

具体表现:在密集使用工具调用的场景中(如连续多次调用搜索、文件读写、图片生成等工具),清洗器会在某次清洗中一次性清空所有历史工具调用信息,使 LLM 无从得知之前执行过哪些工具调用、得到了什么结果。

影响范围

  • 插件: astrbot_plugin_tool_use_cleaner
  • 模型: DeepSeek v4 flash(以及任何使用类似消息格式:空 content + tool_calls 的模型)
  • AstrBot 版本: 经测试存在于 v4.22.1
  • 严重性: 高 — 导致多步推理链断裂、工具调用结果丢失、LLM 行为异常

根因分析

Bug 1:cutoff_index 计算逻辑错误

清洗器通过 round_ends 列表定位需要保留的最近一轮工具调用。round_ends 记录了每一轮对话(user→assistant→tool 消息链)的起始和结束索引。

错误代码(伪码):

cutoff_index = round_ends[-1]  # 取最后一位
# 在 DeepSeek v4 flash 消息结构下,这个值恰好覆盖所有历史轮次

对于 DeepSeek v4 flash,一条工具调用消息的结构是:

{"role": "assistant", "content": "", "tool_calls": [...]}

这种空 content + 非空 tool_calls 的结构与 GPT 系列模型的格式不同,导致 round_ends 的构建方式偏移,最终 round_ends[-1] 取到了一个覆盖所有历史轮次的位置。

修复后:

cutoff_index = round_ends[-2] if len(round_ends) >= 2 else round_ends[-1]

Bug 2:if not ctx.get("content") 判空逻辑不完善

if not ctx.get("content"):  # 只检查 content 是否为 None 或空字符串
    continue

对于 DeepSeek v4 flash,返回的 content 可能是:

{"content": [{"type": "text", "text": ""}]}

——一个包含空文本的列表,bool(ctx.get("content")) 为 True,不会被跳过,但实际上内容为空。

Bug 3:删除 tool_calls 后未兜底检查 content

在清洗过程中,某些分支会删除消息的 tool_calls 字段但不同步清理 content,导致最终发送给 API 的消息结构违反 DeepSeek v4 flash 的 API 要求(content 空且 tool_calls 空),可能触发 400 Bad Request 错误。

配置参数

插件 _conf_schema.json 中已有 tool_context_keep_rounds 参数(默认 3),但在代码修复前,修改此参数无效——Bug 1 导致无论设什么值,所有轮次都会被清空。

修复方案

修复 1:修正 cutoff_index 计算

keep_rounds = self.tool_context_keep_rounds
if len(round_ends) > keep_rounds:
    cutoff_index = round_ends[-keep_rounds]
else:
    cutoff_index = 0

修复 2:增加 _is_content_empty 静态方法

@staticmethod
def _is_content_empty(content) -> bool:
    if not content:
        return True
    if isinstance(content, list):
        return all(
            not isinstance(item, dict) or
            item.get("text", "") == ""
            for item in content
        )
    if isinstance(content, str):
        return content.strip() == ""
    return False

修复 3:删除 tool_calls 后补充空消息清理

在清洗流程末尾增加对 content 和 tool_calls 同时为空的消息的兜底清理。

重现步骤

  1. 配置 AstrBot 使用 DeepSeek v4 flash 作为 LLM
  2. 安装并启用 astrbot_plugin_tool_use_cleaner 插件
  3. 发起一条需要连续多次工具调用的对话(如搜索 X,读取 Y 文件,再搜索 Z)
  4. 前几轮工具调用正常,在第 3-4 轮工具调用后,上下文清洗器触发
  5. 后续对话中 LLM 对之前已完成的工具调用完全无知,仿佛从未执行过

环境

  • AstrBot 版本: 4.22.1
  • 操作系统: Linux (Docker)
  • LLM: DeepSeek v4 flash
  • 插件: astrbot_plugin_tool_use_cleaner(从 AstrBot 插件市场安装)

z454963058-sudo avatar Jun 12 '26 04:06 z454963058-sudo

请直接提交到这个插件的仓库。

Soulter avatar Jun 13 '26 11:06 Soulter

请直接提交到这个插件的仓库。

作者好像已经停止维护了

z454963058-sudo avatar Jun 14 '26 04:06 z454963058-sudo

astrbot_plugin_tool_use_cleaner插件缺陷,分析详尽但应转插件作者

anka-afk avatar Nov 05 '26 00:11 anka-afk