feat(core): 可选地将合并转发聊天记录 ID 附加到上下文,并追加[聊天记录]标记#714
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
Walkthrough在配置接口中新增布尔字段 Changes
Sequence Diagram(s)sequenceDiagram
participant Client as Incoming Message
participant Middleware as read_chat_message
participant Transformer as MessageTransformer
participant Message as transformedMessage
Client->>Middleware: 发送含转发元素的原始消息
Middleware->>Transformer: 注册/触发 forward/message 拦截钩子
Transformer->>Middleware: 初始化或更新内部 state.ids
Middleware->>Middleware: 遍历元素,调用 isForwardMessageElement -> pickForwardMessageId -> normalizeForwardMessageId,收集 state.ids
alt config.attachForwardMsgIdToContext && state.ids 非空
Middleware->>Message: 确保 additional_kwargs 存在
Middleware->>Message: 写入 additional_kwargs.forwardMessageIds = state.ids
Middleware->>Message: 调用 addMessageContent("[聊天记录]")
Middleware->>Message: 删除内部键 forwardHistoryInternalKey
end
Middleware->>Transformer: 将处理后的 transformedMessage 继续下游
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @CookSleep, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在解决 ChatLuna 在处理合并转发聊天记录时可能丢失关键信息的问题。通过引入一个可选的配置项,它允许系统在检测到转发消息时,将其相关的 ID 附加到消息上下文中,并向消息内容追加一个 Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/core/src/middlewares/chat/read_chat_message.ts`:
- Around line 449-467: 当前采集逻辑在 collectForwardMessageIds(使用
normalizeForwardMessageId)里无差别地遍历包括 'id' 的所有键,导致当元素同时存在 forward=true 与自身 id
时可能误取元素自身 ID;为修复,要改为优先使用专用键
['forward_id','forwardId','res_id','resId','message_id','messageId'] 并在找到第一个有效
normalizedId 后停止(或仅在上述专用键全部缺失时才回退到 'id'),或者直接移除 'id' 并在 isForwardMessageElement
中同步将 'id' 纳入判断以保持语义一致;定位到使用 normalizeForwardMessageId 的循环并调整键优先级与短路逻辑(或移除 'id'
并更新 isForwardMessageElement)以避免采集到错误的 ID。
🧹 Nitpick comments (2)
packages/core/src/middlewares/chat/read_chat_message.ts (2)
29-35: 两次递归遍历可合并为一次。
hasForwardMessageElement和collectForwardMessageIds都递归遍历同一棵元素树。可以只调用collectForwardMessageIds,根据返回结果是否为空来判断hasForwardHistory,避免重复遍历。♻️ 建议合并
- const hasForwardHistory = - config.attachForwardMsgIdToContext && - hasForwardMessageElement(message as h[]) - - const forwardMessageIds = hasForwardHistory - ? collectForwardMessageIds(message as h[]) - : [] + const forwardMessageIds = config.attachForwardMsgIdToContext + ? collectForwardMessageIds(message as h[]) + : [] + + const hasForwardHistory = forwardMessageIds.length > 0注意:此修改会改变语义——如果存在转发元素但未收集到任何 ID,
hasForwardHistory将为false,[聊天记录]标记也不会被追加。如果希望"只要存在转发元素就追加标记",可以改为使用collectForwardMessageIds内部同时返回一个布尔标志。
434-438:children访问方式可以简化。koishi 的
h类型本身就有children: h[]属性,无需通过unknown中转。当前写法可以正常工作,但不够简洁。♻️ 简化 children 访问
- const children = (element as unknown as { children?: unknown }).children - return ( - Array.isArray(children) && - children.length > 0 && - hasForwardMessageElement(children as h[]) - ) + return ( + element.children?.length > 0 && + hasForwardMessageElement(element.children) + )
collectForwardMessageIds中同理:- const children = (element as unknown as { children?: unknown }).children - if (Array.isArray(children) && children.length > 0) { - ;(children as h[]).forEach(visit) - } + if (element.children?.length > 0) { + element.children.forEach(visit) + }Also applies to: 470-473
dingyi222666
left a comment
There was a problem hiding this comment.
使用 https://chatluna.chat/development/connect-to-core-services/message-read.html 相关 API 实现,稍加修改后,伪装也能支持读取。
ctx.chatluna.messageTransformer.intercept(
'at',
async (session, element, message) => {
const name = element.attrs['name']
const id = element.attrs['id']
if (id !== session.bot.selfId) {
addMessageContent(
message,
`<at ${name != null ? `name="${name}"` : ''} id="${id}"/>`
)
}
}
)```There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/core/src/middlewares/chat/read_chat_message.ts`:
- Around line 467-478: pickForwardMessageId currently only looks for
message_id/messageId while isForwardMessageElement treats
forward_id/forwardId/res_id/resId as valid forward markers; update
pickForwardMessageId to check the full set of keys (forward_id, forwardId,
res_id, resId, then fallback to message_id, messageId) in that priority order
and keep using normalizeForwardMessageId for each candidate; also apply the same
key-range change to the other similar extractor referenced around the 480-494
block so forward-detected elements have their IDs collected into state.ids
consistently with isForwardMessageElement.
Extract pickForwardMessageId, isForwardMessageElement, and normalizeForwardMessageId functions from read_chat_message.ts to koishi.ts utils module. This allows these utilities to be reused across different middleware and modules. The functions are now exported from the shared utils module and imported where needed.
背景
在使用 Napcat/合并转发聊天记录时,ChatLuna 的输入消息转换链路可能会过滤掉转发记录元素,导致上下文中无法保留“存在聊天记录”这一信息。
改动
attachForwardMsgIdToContext(位于“对话行为选项”末尾,默认关闭)。id、message_id、messageId、res_id、resId、forward_id、forwardId)。inputMessage.additional_kwargs.forwardMessageIds。[聊天记录],避免在过滤后消息为空而被中间件直接丢弃。兼容性