AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

[Bug] 开启流式输出时 reply_with_quote / reply_with_mention 失效(所有平台)

Open td1336065617 opened this issue 1 month ago • 1 comments

发生了什么

开启 platform_settings.reply_with_quote(或 reply_with_mention)后,只要该会话开启了流式输出, LLM 回复就不会引用 / @ 发送人;而插件直接 event.send() 发送的消息、以及内置命令(/reset、/help)能正常引用。

复现步骤

  1. 开启流式输出(provider_settings.streaming_response = true)
  2. 打开 platform_settings.reply_with_quote(或 reply_with_mention)
  3. 向机器人发消息 —— 回复不显示引用 / 不 @ 人

关掉流式输出后立即恢复。这条边界非常明确。

根因

result_decorate 对流式结果直接返回,不会执行插入 Reply / At 的逻辑:

# astrbot/core/pipeline/result_decorate/stage.py
131     if result is None or not result.chain:
132         return
133
134     if result.result_content_type == ResultContentType.STREAMING_RESULT:
135         return                       # 流式「进行中」直接返回
...
191     # 流式输出不执行下面的逻辑
192     if is_stream:                    # is_stream = STREAMING_FINISH
193         return                       # 流式「结束」也跳过全部装饰
...
444         if self.reply_with_quote:    # 不会被执行到
445             result.chain.insert(0, Reply(id=event.message_obj.message_id))

而 respond 对流式结果不使用 result.chain,直接发 async_stream:

# astrbot/core/pipeline/respond/stage.py
217     if result.result_content_type == ResultContentType.STREAMING_RESULT:
229         logger.info(f"Applying streaming output ({event.get_platform_id()}).")
230         await event.send_streaming(result.async_stream, realtime_segmenting)
231         return                       # result.chain 被完全忽略

因此流式路径上消息链里既不会生成 Reply/At,发送端也不会去读它。

影响范围

这是流水线层面的逻辑,与具体适配器无关:凡是没有显式标记为「不支持流式」的平台都会走到这条路径。

register_platform_adapter 的默认值是:

# astrbot/core/platform/register.py:17
support_streaming_message: bool = True

qq_official 未传该参数,沿用默认值 True,因此会走流式路径(已在本地 4.28.2 上复现)。

说明:以上「影响所有平台」是读代码得出的结论(result_decorate / respond 都是通用阶段), 我在本地只实测了 qq_official。若其它平台的实际表现不同,欢迎在此补充。

与 #3431 / #3547 的关系(重要)

  • #3431「回复时引用发送人消息选项开启无效」描述的正是本问题,并且明确写了 「如何复现?开启流式回复后会引发该问题」。
  • 该 issue 被 #3547 关闭,但 #3547 的标题与内容是 perf: improve streaming fallback strategy for streaming-unsupported platform, 它做的是让显式标记为不支持流式的适配器退化为非流式发送 (wecom / discord / line / weixin_oc / mattermost 等), 没有处理「支持流式的适配器在流式时如何应用装饰」。
  • qq_official 未传 support_streaming_message,沿用默认 True,因此不在 #3547 的覆盖范围内, 问题至今仍在。

也就是说 #3431 属于被误判为已修复。

期望行为

流式输出与引用 / @ 回复应当可以共存。可行的方向(供讨论):

  1. 在 result_decorate 中处理 STREAMING_RESULT:不提前返回,而是包装 result.async_stream, 把 Reply/At 注入到第一个 yield 的消息链里;或
  2. 把「是否需要引用 / @」作为一种发送期装饰,下沉到 respond 阶段或适配器层,使其不依赖 result.chain。

注意 result_decorate/stage.py 中已有一条官方提示,说明流式下依赖 on_decorating_result 的插件可能行为异常, 说明这块确实存在已知的时序问题:

169     logger.warning(
170         "Plugins that depend on the pre-send event hook may not work "
171         "correctly when streaming output is enabled.",
172     )

版本与部署

  • AstrBot 版本:4.28.2
  • 消息平台适配器:qq_official(本地实测)
  • 流式:provider_settings.streaming_response = true

附:一个独立的、同为出站引用的问题

即使关掉流式(消息链里能正常生成 Reply),qq_official 适配器也从不填充 message_reference —— 这是另一个 bug,已单独提交 PR #10489。 两者叠加,导致 qq_official + 流式下引用完全不可用。

td1336065617 avatar Oct 10 '26 12:10 td1336065617

根因已定位并修复,PR:#10491

根因(与原分析略有出入,实测后修正)

之前认为卡在 result_decorate 的 STREAMING_RESULT / STREAMING_FINISH 两个提前 return。 实际更靠前一步 —— 流式结果的 result.chain 恒为空,第 131 行就返回了:

130     result = event.get_result()
131     if result is None or not result.chain:
132         return                              # ← 流式结果在这里就返回了
133
134     if result.result_content_type == ResultContentType.STREAMING_RESULT:
135         return                              # 后面的装饰插入都执行不到

在 result_decorate.process 入口加临时日志实测:

content_type=None                  chain_len=0    × 39
content_type=STREAMING_RESULT      chain_len=0    ×  1   ← 进来了,但 chain 为空
content_type=STREAMING_FINISH      chain_len=1    ×  1

修复方式

为避免重蹈 #3501 / #6018 的覆辙(两者都因改动共享阶段 / 增加基类参数而被卡):

  1. 共享阶段只记录决策(写入 event 扩展字段),不改 result.chain、不改任何签名;
  2. 由平台自己的 send_streaming 读取并在首个分片应用。

实测结果

[DecoProbe] stashed {'quote': True, 'mention': False} ct=STREAMING_RESULT
[DecoProbe] apply   deco={'quote': True, ...} chain_types=['Plain']
[DecoProbe] apply   deco=None                                    ← 后续分片不重复

客户端表现:回复带引用气泡,同时正文仍逐字流式输出 ✓

td1336065617 avatar Oct 10 '26 13:10 td1336065617