Skip to content

fix: fix get_message_event_type return wrong message type#32019

Open
fatelei wants to merge 1 commit intolanggenius:mainfrom
fatelei:fix_get_message_event_type
Open

fix: fix get_message_event_type return wrong message type#32019
fatelei wants to merge 1 commit intolanggenius:mainfrom
fatelei:fix_get_message_event_type

Conversation

@fatelei
Copy link
Contributor

@fatelei fatelei commented Feb 5, 2026

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

fix #31888

Screenshots

Before After
... ...

before

Screen.Recording.2026-02-05.at.22.32.47.mov

after

Screen.Recording.2026-02-05.at.22.30.28.mov

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint and make type-check (backend) and cd web && npx lint-staged (frontend) to appease the lint gods

Copilot AI review requested due to automatic review settings February 5, 2026 14:34
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Feb 5, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

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

This pull request resolves an issue where the system was incorrectly classifying messages containing user-uploaded files as having a MESSAGE_FILE event. By introducing a more precise filter in the message event type determination logic, the system now accurately distinguishes between user-uploaded and AI-generated files, ensuring that only AI-generated content triggers the MESSAGE_FILE event. This correction prevents display anomalies and improves the overall accuracy of event streaming.

Highlights

  • Refined Message Event Type Determination: The get_message_event_type function in message_cycle_manager.py has been updated to specifically filter for MessageFile entries where belongs_to is 'assistant'. This ensures that only files generated by the AI assistant trigger a MESSAGE_FILE event, preventing user-uploaded files from being incorrectly categorized.
  • Enhanced Test Coverage for File Ownership: New and updated unit tests have been added to explicitly verify the correct behavior of get_message_event_type based on file ownership. This includes tests for assistant-generated files, user-uploaded files, and specific regression tests to ensure user-uploaded images do not trigger MESSAGE_FILE events, while AI-generated images do.

🧠 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
  • api/core/app/task_pipeline/message_cycle_manager.py
    • Modified get_message_event_type to add a where(MessageFile.belongs_to == "assistant") clause to the database query for message files.
  • api/tests/unit_tests/core/app/task_pipeline/test_message_cycle_manager_optimization.py
    • Renamed test_get_message_event_type_with_message_file to test_get_message_event_type_with_assistant_file and updated its mock to reflect assistant ownership.
    • Added test_get_message_event_type_with_user_file to confirm MESSAGE event for user-only files.
    • Added test_user_uploaded_image_should_not_trigger_message_file_event as a regression test for user-uploaded images.
    • Added test_assistant_generated_image_should_trigger_message_file_event to verify correct MESSAGE_FILE event for AI-generated images.
    • Updated mock setup in test_message_to_stream_response_with_precomputed_event_type to specify belongs_to = "assistant".
Activity
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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

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 fixes a bug where user-uploaded images were incorrectly triggering MESSAGE_FILE stream events, causing the LLM text response to not render in the chat interface. The root cause was that get_message_event_type() was checking for any file associated with a message, without distinguishing between user-uploaded files and assistant-generated files.

Changes:

  • Added a filter to query only assistant-generated files (belongs_to == "assistant") when determining message event type
  • Added comprehensive test coverage for both user and assistant file scenarios
  • Added regression tests to prevent this issue from recurring

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
api/core/app/task_pipeline/message_cycle_manager.py Added belongs_to == "assistant" filter to SQL query in get_message_event_type() to only check for AI-generated files
api/tests/unit_tests/core/app/task_pipeline/test_message_cycle_manager_optimization.py Updated and added tests to cover user files, assistant files, and regression scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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

This pull request correctly addresses a bug where the message event type was being incorrectly determined when a user uploads a file. The fix in get_message_event_type to filter for assistant-owned files is appropriate. The accompanying tests are thorough and validate the fix, including regression tests for the specific issue. I've noted an opportunity to reduce some duplication in the new tests for better long-term maintainability.

@fatelei fatelei force-pushed the fix_get_message_event_type branch from 3284a7c to 7181aa3 Compare February 5, 2026 14:37
Copilot AI review requested due to automatic review settings February 5, 2026 14:39
Copy link
Contributor

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@fatelei fatelei force-pushed the fix_get_message_event_type branch from d0c3e4b to dad9006 Compare February 5, 2026 14:40
Copilot AI review requested due to automatic review settings February 5, 2026 14:42
Copy link
Contributor

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@fatelei fatelei force-pushed the fix_get_message_event_type branch from 61e7956 to d36dd6d Compare February 5, 2026 14:42
@Stormhand
Copy link

Hi @fatelei, thanks for fixing the get_message_event_type issue. The filter for belongs_to == "assistant" is correct and prevents user-uploaded files from triggering wrong event types as described in #31888

However, there's still a missing piece that prevents vision responses from rendering. The _message_end_to_stream_response() method in EasyUIBasedGenerateTaskPipeline doesn't populate the files field in the message_end SSE event:

def _message_end_to_stream_response(self) -> MessageEndStreamResponse:
    self._task_state.metadata.usage = self._task_state.llm_result.usage
    metadata_dict = self._task_state.metadata.model_dump()
    return MessageEndStreamResponse(
        task_id=self._application_generate_entity.task_id,
        id=self._message_id,
        metadata=metadata_dict,
        # files field is missing
    )

Even with your fix, the frontend can't render vision responses because:

  • message_file events are correctly emitted (your fix works)
  • message_end event has "files": null instead of the files array
  • Frontend's onMessageEnd handler (line 510 in hooks.ts) processes messageEnd.files || [] but gets empty
  • Result: empty chat container, no text or images

I've implemented the fix in PR #31925 which:

  • Queries MessageFile records by message_id
  • Fetches UploadFile objects in batch (avoids N+1)
  • Formats files with proper URLs (signed for local, direct for remote/tool files)
  • Includes unit tests

The two PRs complement each other - both are needed to fully resolve #31888. Should I add this to your PR or keep it separate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chat app vision responses not rendering - message_file event handler bug (regression from #22552)

2 participants