Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: preserve inline image for chatflow messages (#37455)

Yunlu Wen4b15b0e6a6f3
diff --git a/api/core/app/apps/advanced_chat/generate_task_pipeline.py b/api/core/app/apps/advanced_chat/generate_task_pipeline.py
index 6f8117ee590..d8ca19b5fc3 100644
--- a/api/core/app/apps/advanced_chat/generate_task_pipeline.py
+++ b/api/core/app/apps/advanced_chat/generate_task_pipeline.py
@@ -1,6 +1,5 @@
import json
import logging
-import re
import time
from collections.abc import Callable, Generator, Mapping
from contextlib import contextmanager
@@ -1010,11 +1009,7 @@ class AdvancedChatAppGenerateTaskPipeline(GraphRuntimeStateSupport):
if message.status == MessageStatus.PAUSED:
message.status = MessageStatus.NORMAL
- # If there are assistant files, remove markdown image links from answer
answer_text = self._task_state.answer
- if self._recorded_files:
- # Remove markdown image links since we're storing files separately
- answer_text = re.sub(r"!\[.*?\]\(.*?\)", "", answer_text).strip()
message.answer = answer_text
message.updated_at = naive_utc_now()
diff --git a/api/tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline_core.py b/api/tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline_core.py
index 9726c939e94..b75f6d44943 100644
--- a/api/tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline_core.py
+++ b/api/tests/unit_tests/core/app/apps/advanced_chat/test_generate_task_pipeline_core.py
@@ -562,7 +562,7 @@ class TestAdvancedChatGenerateTaskPipeline:
assert list(pipeline._handle_human_input_form_timeout_event(timeout_event)) == ["timeout"]
assert persisted == ["saved"]
- def test_save_message_strips_markdown_and_sets_usage(self):
+ def test_save_message_preserves_full_answer_and_sets_usage(self):
pipeline = _make_pipeline()
pipeline._recorded_files = [
{
@@ -572,7 +572,8 @@ class TestAdvancedChatGenerateTaskPipeline:
"related_id": "file-id",
}
]
- pipeline._task_state.answer = "![img](url) hello"
+ # The answer is stored verbatim; markdown image links are never stripped.
+ pipeline._task_state.answer = "![img](http://example.com/file.png) hello ![inline](http://llm.com/img.jpg)"
pipeline._task_state.is_streaming_response = True
pipeline._task_state.first_token_time = pipeline._base_task_pipeline.start_at + 0.1
pipeline._task_state.last_token_time = pipeline._base_task_pipeline.start_at + 0.2
@@ -614,7 +615,7 @@ class TestAdvancedChatGenerateTaskPipeline:
pipeline._save_message(session=_Session(), graph_runtime_state=graph_runtime_state)
assert message.status == MessageStatus.NORMAL
- assert message.answer == "hello"
+ assert message.answer == "![img](http://example.com/file.png) hello ![inline](http://llm.com/img.jpg)"
assert message.message_metadata
def test_handle_stop_event_saves_message_for_moderation(self, monkeypatch: pytest.MonkeyPatch):