Skip to content

[Feature Request] 插件主动消息保存到对话历史 / WebUI 记录 #9240

Description

@fqf060420

Description / 描述

功能描述

希望为插件提供 API,将主动发送的消息(如定时任务、邮件推送)保存到对话历史中,使:

  1. WebUI "对话数据" 页面能显示这些消息
  2. LLM 在后续对话中能感知这些消息内容

使用场景

开发了一个邮件推送插件(EmailNotixion),通过 context.send_message()target_event.send() 向用户推送邮件摘要。但存在以下问题:

  • WebUI 不显示:推送的消息在 WebUI 对话历史中完全不可见
  • LLM 无上下文:用户后续询问"刚才那封邮件是谁发的",LLM 不知道邮件内容
  • 无法持久化:插件侧修改 conversation.history 后没有 save() 方法,重启后丢失

已尝试的方案

方案 1:直接修改 conversation.history

conversation = self.context.get_conversation(uid)
history = json.loads(conversation.history)
history.append({"role": "user", "content": "..."})
history.append({"role": "assistant", "content": "..."})
conversation.history = json.dumps(history)

结果:修改仅存在于内存,重启后丢失。Conversation 对象没有 save() 方法。

方案 2:ConversationManager.update_conversation()

await conv_mgr.update_conversation(conversation)

结果:抛出 TypeError: unhashable type: 'Conversation',API 参数类型不匹配。

方案 3:context.send_message()

await self.context.send_message(umo, message)

结果:消息能发送给用户,但不保存到对话历史(设计行为)。

期望的解决方案
提供以下任一 API:
1.conversation.save() 或 await conversation.persist()
将内存中的 history 变更同步到数据库
2.await context.add_message_pair(umo, user_message, assistant_message)
类似于内部使用的 add_message_pair,但暴露给插件
3.修改 context.send_message() 行为
增加参数 save_to_history=True,允许插件选择是否保存

环境信息
AstrBot 版本:v4.26.5
部署方式:Docker / proot-distro Debian

相关日志
[EmailNotixion] !! DIAG: update_conversation FAILED: TypeError: unhashable type: 'Conversation'
[EmailNotixion] !! DIAG: reloaded has inject=False
[EmailNotixion] !! DIAG: reloaded len=28535 # 与修改前相同,未持久化

补充
类似需求可能适用于所有"主动推送"类插件:
邮件/日历提醒
系统监控告警
定时任务报告
如果已有解决方案请指出,感谢!
数据库:SQLite(默认)

Use Case / 使用场景

邮件推送、定时任务等插件主动发送的消息

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

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

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:coreThe bug / feature is about astrbot's core, backendenhancementNew feature or requestfeature:pluginThe bug / feature is about AstrBot plugin system.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions