Skip to content

refactor: chat bloc#7904

Merged
appflowy merged 4 commits intomainfrom
chat_bloc_refactor
May 10, 2025
Merged

refactor: chat bloc#7904
appflowy merged 4 commits intomainfrom
chat_bloc_refactor

Conversation

@appflowy
Copy link
Contributor

@appflowy appflowy commented May 9, 2025

Separate chat_bloc logic into multiple files

@appflowy appflowy requested a review from Copilot May 9, 2025 14:00
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented May 9, 2025

Reviewer's Guide

The ChatBloc has been refactored by decomposing its core functionalities into three specialized classes: ChatMessageHandler for message operations, ChatStreamManager for handling data streams, and ChatSettingsManager for managing chat settings. This delegation simplifies ChatBloc, enhances modularity, and centralizes specific logic within the new dedicated handlers.

File-Level Changes

Change Details Files
Introduced dedicated manager/handler classes to modularize chat functionalities.
  • Created ChatMessageHandler to manage message creation, parsing, temporary ID mapping, and clearing specific message types (errors, related questions).
  • Created ChatStreamManager to manage the lifecycle of AnswerStream and QuestionStream and handle backend communication for streaming operations.
  • Created ChatSettingsManager to handle loading, updating, and notifying changes for chat-specific settings like selected RAG sources.
frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_bloc.dart
frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_message_handler.dart
frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_stream_manager.dart
frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_settings_manager.dart
Refined ChatBloc by delegating responsibilities and restructuring event processing.
  • Updated ChatBloc to initialize and utilize instances of the new manager/handler classes for their respective functionalities.
  • Restructured the main event handler in ChatBloc to use event.map with dedicated private methods for each chat event, improving readability.
  • Moved internal state related to streaming (e.g., answerStream, answerStreamMessageId, temporaryMessageIDMap) and settings (selectedSourcesNotifier) to the corresponding manager classes.
  • Ensured proper resource cleanup by having ChatBloc.close() call the dispose methods of the newly introduced manager classes.
frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_bloc.dart

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

@appflowy appflowy requested a review from richardshiue May 9, 2025 14:00
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the chat bloc functionality by introducing dedicated managers for managing chat streams, settings, and message handling. The key changes include:

  • Introducing ChatStreamManager for handling streaming operations.
  • Adding ChatSettingsManager for managing chat settings.
  • Implementing ChatMessageHandler for message creation, temporary ID mapping, and cleanup operations.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_stream_manager.dart Implements chat streams setup, disposal, and payload building for streaming and regenerate operations.
frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_settings_manager.dart Manages settings retrieval and updates for chat sessions.
frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_message_handler.dart Handles message creation, temporary ID mapping, and cleanup of error/related messages in chat.
Comments suppressed due to low confidence (1)

frontend/appflowy_flutter/lib/plugins/ai_chat/application/chat_message_handler.dart:30

  • [nitpick] For improved readability and consistency, consider renaming 'temporaryMessageIDMap' to 'temporaryMessageIdMap'.
final HashMap<String, String> temporaryMessageIDMap = HashMap();

if (temporaryMessageIDMap.containsKey(messageId)) {
messageId = temporaryMessageIDMap[messageId]!;
}
final metadata = message.metadata == 'null' ? '[]' : message.metadata;
Copy link

Copilot AI May 9, 2025

Choose a reason for hiding this comment

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

[nitpick] Comparing message.metadata to the string 'null' may be fragile. Consider performing a proper null check (or handling a nullable type) to ensure robust metadata processing.

Suggested change
final metadata = message.metadata == 'null' ? '[]' : message.metadata;
final metadata = (message.metadata == null || message.metadata.isEmpty) ? '[]' : message.metadata;

Copilot uses AI. Check for mistakes.
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.

Hey @appflowy - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Conflict markers remain in the _dispatch method. (link)

  • Resolve the merge conflicts (e.g., <<<<<<< Updated upstream) present in the diff.

  • Consider moving state flags like isLoadingPreviousMessages and isFetchingRelatedQuestions into the relevant new manager classes to further consolidate their related logic.

Here's what I looked at during the review
  • 🔴 General issues: 1 blocking issue
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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.

@appflowy appflowy force-pushed the chat_bloc_refactor branch from 98b7b73 to aaa45ba Compare May 9, 2025 14:28
Copy link
Collaborator

@richardshiue richardshiue left a comment

Choose a reason for hiding this comment

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

LGTM

@appflowy appflowy merged commit 2c8060c into main May 10, 2025
20 checks passed
@appflowy appflowy deleted the chat_bloc_refactor branch May 10, 2025 14:07
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.

3 participants