Skip to content

Deduplicate bot motivation display#49

Merged
Dax233 merged 2 commits intodevfrom
Domain-Models
Aug 11, 2025
Merged

Deduplicate bot motivation display#49
Dax233 merged 2 commits intodevfrom
Domain-Models

Conversation

@Dax233
Copy link
Member

@Dax233 Dax233 commented Aug 11, 2025

Sourcery 总结

重构聊天历史记录格式化器,以提取并集中机器人动机的格式化和去重逻辑

增强功能:

  • 引入 last_displayed_bot_motive 状态来跟踪并抑制重复的机器人动机
  • 将动机格式化和跟踪提取到一个专门的 _format_and_track_motivation 辅助函数中
  • format_chat_log_block 中内联的动机处理替换为对新辅助函数的调用
Original summary in English

Summary by Sourcery

Refactor chat history formatter to extract and centralize bot motive formatting and deduplication logic

Enhancements:

  • Introduce last_displayed_bot_motive state to track and suppress duplicate bot motivations
  • Extract motive formatting and tracking into a dedicated _format_and_track_motivation helper
  • Replace inline motive handling in format_chat_log_block with calls to the new helper

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 11, 2025

审阅者指南

通过引入状态变量、将机器人动机格式化提取到具有副作用的辅助方法中,并重构主格式化器以使用该方法而不是内联逻辑,实现了机器人动机消息的去重。

聊天日志格式化中机器人动机去重的序列图

sequenceDiagram
    participant ChatHistoryFormatter
    participant Stimulus
    participant log_lines
    participant sender_uid
    ChatHistoryFormatter->>Stimulus: Get sender_id and motivation
    ChatHistoryFormatter->>ChatHistoryFormatter: _format_and_track_motivation(stimulus, sender_uid)
    alt sender_uid == "U0" and motivation present
        alt motivation != last_displayed_bot_motive
            ChatHistoryFormatter->>log_lines: Append formatted motivation
            ChatHistoryFormatter->>ChatHistoryFormatter: Update last_displayed_bot_motive
        else motivation == last_displayed_bot_motive
            ChatHistoryFormatter->>ChatHistoryFormatter: Update last_displayed_bot_motive
        end
    else sender_uid != "U0" or no motivation
        ChatHistoryFormatter->>ChatHistoryFormatter: Reset last_displayed_bot_motive
    end
Loading

更新后的 ChatHistoryFormatter 动机跟踪的类图

classDiagram
    class ChatHistoryFormatter {
        - last_valid_text_message: str | None
        - image_ref_counter: int
        - last_displayed_bot_motive: str | None
        + _build_user_maps()
        + _is_unread(stimulus: Stimulus): bool
        + _format_and_track_motivation(stimulus: Stimulus, sender_uid: str): str | None
        + format_chat_log_block(): str
    }
    ChatHistoryFormatter --> Stimulus
Loading

文件级更改

更改 详情 文件
引入状态以跟踪上次显示的机器人动机
  • init 中添加 last_displayed_bot_motive 属性
  • 当非机器人消息或缺少动机时重置跟踪器
  • 在新对话中将跟踪器初始化为 None
src/common/focus_chat_history_builder/chat_history_formatter.py
提取并集中动机格式化逻辑
  • 创建 _format_and_track_motivation 方法
  • 实现去重逻辑和状态更新
  • 记录方法行为和副作用
src/common/focus_chat_history_builder/chat_history_formatter.py
重构 format_chat_log_block 以使用辅助方法处理动机
  • 移除内联动机处理和局部跟踪器变量
  • 调用 _format_and_track_motivation 并附加其输出
  • 通过将动机检查委托给新辅助方法来简化循环
src/common/focus_chat_history_builder/chat_history_formatter.py

提示和命令

与 Sourcery 互动

  • 触发新的审阅: 在拉取请求上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub issue: 通过回复审阅评论,要求 Sourcery 从中创建 issue。您也可以回复审阅评论并带上 @sourcery-ai issue 来创建 issue。
  • 生成拉取请求标题: 在拉取请求标题的任意位置写入 @sourcery-ai 即可随时生成标题。您也可以在拉取请求上评论 @sourcery-ai title 来随时(重新)生成标题。
  • 生成拉取请求摘要: 在拉取请求正文的任意位置写入 @sourcery-ai summary 即可随时在您想要的位置生成 PR 摘要。您也可以在拉取请求上评论 @sourcery-ai summary 来随时(重新)生成摘要。
  • 生成审阅者指南: 在拉取请求上评论 @sourcery-ai guide 即可随时(重新)生成审阅者指南。
  • 解决所有 Sourcery 评论: 在拉取请求上评论 @sourcery-ai resolve 即可解决所有 Sourcery 评论。如果您已经处理了所有评论并且不想再看到它们,这会很有用。
  • 关闭所有 Sourcery 审阅: 在拉取请求上评论 @sourcery-ai dismiss 即可关闭所有现有 Sourcery 审阅。如果您想从新的审阅开始,这特别有用——别忘了评论 @sourcery-ai review 来触发新的审阅!

自定义您的体验

访问您的 仪表盘 以:

  • 启用或禁用审阅功能,例如 Sourcery 生成的拉取请求摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、删除或编辑自定义审阅说明。
  • 调整其他审阅设置。

获取帮助

Original review guide in English

Reviewer's Guide

Adds de-duplication of bot motivation messages by introducing a state variable, extracting motivation formatting into a helper method with side effects, and refactoring the main formatter to use it instead of inline logic.

Sequence diagram for bot motivation de-duplication in chat log formatting

sequenceDiagram
    participant ChatHistoryFormatter
    participant Stimulus
    participant log_lines
    participant sender_uid
    ChatHistoryFormatter->>Stimulus: Get sender_id and motivation
    ChatHistoryFormatter->>ChatHistoryFormatter: _format_and_track_motivation(stimulus, sender_uid)
    alt sender_uid == "U0" and motivation present
        alt motivation != last_displayed_bot_motive
            ChatHistoryFormatter->>log_lines: Append formatted motivation
            ChatHistoryFormatter->>ChatHistoryFormatter: Update last_displayed_bot_motive
        else motivation == last_displayed_bot_motive
            ChatHistoryFormatter->>ChatHistoryFormatter: Update last_displayed_bot_motive
        end
    else sender_uid != "U0" or no motivation
        ChatHistoryFormatter->>ChatHistoryFormatter: Reset last_displayed_bot_motive
    end
Loading

Class diagram for updated ChatHistoryFormatter motivation tracking

classDiagram
    class ChatHistoryFormatter {
        - last_valid_text_message: str | None
        - image_ref_counter: int
        - last_displayed_bot_motive: str | None
        + _build_user_maps()
        + _is_unread(stimulus: Stimulus): bool
        + _format_and_track_motivation(stimulus: Stimulus, sender_uid: str): str | None
        + format_chat_log_block(): str
    }
    ChatHistoryFormatter --> Stimulus
Loading

File-Level Changes

Change Details Files
Introduce state for tracking last displayed bot motivation
  • Add last_displayed_bot_motive attribute in init
  • Reset tracker when non-bot messages or missing motivation
  • Initialize tracker to None on new conversation
src/common/focus_chat_history_builder/chat_history_formatter.py
Extract and centralize motivation formatting logic
  • Create _format_and_track_motivation method
  • Implement de-duplication logic and state updates
  • Document method behavior and side effects
src/common/focus_chat_history_builder/chat_history_formatter.py
Refactor format_chat_log_block to use helper for motivations
  • Remove inline motivation handling and local tracker variable
  • Invoke _format_and_track_motivation and append its output
  • Simplify loop by delegating motive checks to new helper
src/common/focus_chat_history_builder/chat_history_formatter.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai bot changed the title @sourcery-ai Deduplicate bot motivation display Aug 11, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @Dax233, 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!

此拉取请求旨在优化聊天历史记录的格式化,特别是针对机器人动机的显示。通过引入一个机制来追踪并去重连续的机器人动机,它确保了聊天日志的清晰度,避免了重复信息的冗余显示。

Highlights

  • 动机追踪: 引入了 last_displayed_bot_motive 实例变量,用于在 ChatHistoryFormatter 中追踪上一个显示的机器人动机。
  • 新辅助方法: 创建了一个新的私有方法 _format_and_track_motivation,封装了机器人动机的格式化和去重逻辑。
  • 代码重构: 重构了 format_chat_log_block 方法,以利用新的辅助函数来处理机器人动机,从而简化了主格式化逻辑。
Using Gemini Code Assist

The 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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dax233 - 我已经审查了你的更改,它们看起来很棒!

AI 代理提示
请解决此代码审查中的评论:
## 个人评论

### 评论 1
<location> `src/common/focus_chat_history_builder/chat_history_formatter.py:55` </location>
<code_context>
         self.last_valid_text_message: str | None = None
         self.image_ref_counter = 0

+        # 追踪上一个显示的机器人动机
+        self.last_displayed_bot_motive: str | None = None
+
</code_context>

<issue_to_address>
非英文注释可能会降低非中文使用者的可维护性。

请考虑将注释更新为英文,以确保所有团队成员都能理解和维护代码。
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
        # 追踪上一个显示的机器人动机
        self.last_displayed_bot_motive: str | None = None
=======
        # Track the last displayed bot motive
        self.last_displayed_bot_motive: str | None = None
>>>>>>> REPLACE

</suggested_fix>

Sourcery 对开源项目免费 - 如果您喜欢我们的评论,请考虑分享它们 ✨
帮助我更有用!请点击每个评论上的 👍 或 👎,我将根据反馈改进您的评论。
Original comment in English

Hey @Dax233 - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `src/common/focus_chat_history_builder/chat_history_formatter.py:55` </location>
<code_context>
         self.last_valid_text_message: str | None = None
         self.image_ref_counter = 0

+        # 追踪上一个显示的机器人动机
+        self.last_displayed_bot_motive: str | None = None
+
</code_context>

<issue_to_address>
Non-English comments may reduce maintainability for non-Chinese speakers.

Consider updating comments to English to ensure all team members can understand and maintain the code.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
        # 追踪上一个显示的机器人动机
        self.last_displayed_bot_motive: str | None = None
=======
        # Track the last displayed bot motive
        self.last_displayed_bot_motive: str | None = None
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

你好,感谢你的贡献。这次的重构很棒,成功地将机器人动机(motive)的格式化和去重逻辑提取到了一个独立的辅助函数中,使得 format_chat_log_block 的逻辑更加清晰。

我只有一个小建议,关于新的 _format_and_track_motivation 函数。它的实现可以稍微简化一下,以消除一些重复代码并提高可读性。具体请看我的评论。

总体来说,这是一个高质量的改进!

@Dax233 Dax233 merged commit f583aeb into dev Aug 11, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant