AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

[Feature]当前存在两种消息发送路径,建议增加统一的出站文本预处理钩子

Open piexian opened this issue 7 months ago • 5 comments

Description / 描述

目前 AstrBot 的用户可见消息实际上存在两条发送路径:

  1. 普通回复路径 Pipeline -> ResultDecorateStage -> RespondStage
  2. 主动发送路径 Context.send_message() / send_message_to_user / cron / WebChat proactive send / OpenAPI 等

这会带来一个问题: 插件作者很难找到一个稳定、统一的“文本预处理”扩展点。 现在如果插件想处理“最终要发给用户的文本”,通常只能挂在:

  • on_llm_response
  • on_decorating_result

但这两个阶段都不够理想:

  • on_llm_response 只适合处理 LLM 直接输出的文本,覆盖不到很多主动发送场景
  • on_decorating_result 又已经偏后,更适合装饰 result.chain,不完全适合作为通用文本预处理入口

而且目前已经有两条发送路径,未来内部实现还有可能继续调整或新增新的主动发送入口。 如果插件只能依赖现有内部流程做兼容,长期会比较脆弱,也容易重复踩坑。 所以我建议 AstrBot 增加一个面向插件的、稳定的“出站文本预处理钩子”,用于在消息进入最终装饰,发送前,对“即将发给用户的文本”做统一预处理。 这个钩子的目标不是替代 on_decorating_result,而是补上一个更早、更通用、更适合插件处理文本语法,标记,占位符的官方扩展点

Use Case / 使用场景

  • 插件需要统一处理“最终发给用户的文本”,而不仅是 LLM 原始回复
  • 插件希望在装饰阶段前先做文本标记解析、占位符清理或语法预处理
  • 当前存在两条发送管线,插件作者很难稳定适配所有发送入口
  • 未来如果内部继续调整发送流程,提供官方文本预处理钩子会更利于插件生态兼容

Willing to Submit PR? / 是否愿意提交PR?

  • [ ] Yes, I am willing to submit a PR. / 是的,我愿意提交 PR。

Code of Conduct

piexian avatar Apr 11 '26 01:04 piexian

支持

DBJD-CR avatar Apr 12 '26 14:04 DBJD-CR

顺着这个 issue 补充一个相邻的缺口,我觉得它和你说的是同一块设计空间的两端,分开做容易打架。

我这边的问题:插件通过旁路交付内容——TTS 先合成,5~10 秒后 Context.send_message() 发语音条。模型这回合的过渡语(「我这就说给你听」)会先发出去,用户看到的是「文字 + 语音」两条。现在的做法只能在 on_decorating_result 里 chain.clear(),副作用是 astrbot/core/pipeline/respond/stage.py:207-212 直接 return,连 after_message_sent 都不触发(源码注释写得很明确),依赖该钩子的统计/日志插件会漏记。

和本 issue 的关系:你要的是「进装饰阶段之前,插件能统一处理要发出去的文本」;我需要的是「装饰完之后,插件能决定这一回合到底发不发」。两者都在 ResultDecorateStage → RespondStage 这一段,但一个是改内容,一个是取消发送。

如果分开各加一个钩子,插件作者会面对两个语义接近、边界模糊的入口:想改文本的可能误用了 skip;想 skip 的会在 on_decorating_result 里清链,又踩上面那个 after_message_sent 的坑。

建议一起设计:

  1. 出站文本钩子(你提的这个)放在 ResultDecorateStage 之前,拿到「即将发给用户的文本」,可改写、可解析占位符;
  2. 发送决策放在 RespondStage 之前,暴露 event.set_extra("skip_respond", True),或者直接复用框架里已有的标记——AstrMessageEvent._has_send_oper 已经在 astrbot/core/tools/message_tools.py:349-363 被置位了,但目前只有 astr_agent_tool_exec.py:676-682(后台任务收尾)在读,RespondStage 不读它;
  3. 两者都用 event.set_extra() 传递,别再引入第三套约定。

另外提一句现状:RespondStage 现在其实有一个判重,但它要求「本回合纯文本 == 已发送文本」(respond/stage.py:213-235),所以纯媒体交付(只发语音/图片,没有文本可比)和过渡语(文字和交付内容天然不相等)这两种情况都命中不了——这正好是异步交付类插件最常遇到的两种形态。

完整取证(含代码位置、三种可选方案、兼容性说明)我放在 #10870 里了。如果维护者倾向做一个统一钩子同时覆盖「文本预处理」和「发送决策」,我觉得比加两个独立入口更合理,也能顺带把 #6402 / #6005 / #6067 那条判重逻辑收敛掉。

AmeiMiao avatar Oct 30 '26 02:10 AmeiMiao

A concrete production driver for this gap: on aiocqhttp, result_decorate does not cover streaming output, rich/file/forward deliveries, or plugin direct sends. The adapter send and send_streaming paths are the narrowest common seam, but currently have no pre-send extension hook. We need to inject the same outbound policy at that boundary in a downstream build.

An official hook (for example a pre-send event carrying the target session and final MessageChain, including streaming chunks) would let policy plugins remain ordinary plugins instead of patching adapter source. I am not proposing to upstream a downstream-specific rate/risk implementation; the missing reusable capability is the adapter-boundary interception point.

sciman-top avatar Nov 04 '26 15:11 sciman-top

10. A concrete production driver for this gap: on aiocqhttp, result_decorate does not cover streaming output, rich/file/forward deliveries, or plugin direct sends. The adapter send and send_streaming paths are the narrowest common seam, but currently have no pre-send extension hook. We need to inject the same outbound policy at that boundary in a downstream build. An official hook (for example a pre-send event carrying the target session and final MessageChain, including streaming chunks) would let policy plugins remain ordinary plugins instead of patching adapter source. I am not proposing to upstream a downstream-specific rate/risk implementation; the missing reusable capability is the adapter-boundary interception point.

同意,适配器那层才是终点。我 issue 里写的装饰阶段只是当时图省事的写法,现在版本翻这么多轮,该按最终出站重新设计。 所以大家要的其实是同一个东西——出站前拦一下,只是拦完做的事不一样:改文本、拦掉不发、限流。 顺带说一句现在新版本是三条路,不是两条:普通回复有钩子,Context.send_message() 直接发和流式都没有——正好是异步交付最常用的两种。 @AmeiMiao

氕氙 @.***

---原始邮件--- 发件人: @.> 发送时间: 2026年11月4日(周三) 晚上11:09 收件人: @.>; 抄送: @.@.>; 主题: Re: [AstrBotDevs/AstrBot] [Feature]当前存在两种消息发送路径,建议增加统一的出站文本预处理钩子 (Issue #7457)

sciman-top left a comment (AstrBotDevs/AstrBot#7457)

A concrete production driver for this gap: on aiocqhttp, result_decorate does not cover streaming output, rich/file/forward deliveries, or plugin direct sends. The adapter send and send_streaming paths are the narrowest common seam, but currently have no pre-send extension hook. We need to inject the same outbound policy at that boundary in a downstream build.

An official hook (for example a pre-send event carrying the target session and final MessageChain, including streaming chunks) would let policy plugins remain ordinary plugins instead of patching adapter source. I am not proposing to upstream a downstream-specific rate/risk implementation; the missing reusable capability is the adapter-boundary interception point.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

piexian avatar Nov 04 '26 18:11 piexian

补充一个与本 issue 的统一出站扩展点相关、但不能用现有 on_decorating_result 解决的安全点场景(SeekMemory #154):同一会话的新用户消息使旧的、尚未开始发送的回复失效;已开始的发送保留真实结果。

在 AstrBot v4.24.1 和 v4.29.4 中,ResultDecorateStage 的钩子之后仍可能有异步装饰;RespondStage 的分段回复会 await asyncio.sleep(...),再逐段 await event.send(...)。因此钩子检查为“有效”后,新输入仍可能在分段等待期间到来,导致旧段晚发。after_message_sent 又太晚;清空 chain 或 stop_event() 不能充当每段发送前的检查。此点与出站文本预处理和 #10870 的本回合已交付语义不同:我们需要的是每次发送开始前的有效性复核。

建议在设计统一出站钩子时明确区分“可异步改写内容”与“最后一刻是否允许本次发送”。后者需要在所有装饰、等待和其他可 await 的工作之后、每次实际发送调用之前执行;若支持多个守卫,最终检查之间也不能再让出事件循环。可考虑事件范围内注册不做 I/O 的同步 predicate(默认允许、不注册时完全兼容),由宿主紧贴每次 event.send 调用判断;失败/抑制不应伪造已交付的回执。多段发送中,已发送的段不撤销,仅阻止尚未开始的后续段。平台直发与 streaming 的覆盖范围也请在 API 契约中明确,不把仅覆盖标准非流式的实现称为全路径出站钩子。

这不是请求把 SeekMemory 的调度策略放到 AstrBot;希望有一个通用且可测试的宿主安全点,让插件自行判断当前运行是否仍有效。若维护者偏好其他 API 形态,只要能保证上述“最后一次 await 之后、每次发送之前”的时序即可。我们不会在插件里 monkey-patch 平台发送方法。

NeverENG avatar Nov 11 '26 08:11 NeverENG