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: fix agent app is not support (#45505)

wangxiaolei70c187f53108
diff --git a/api/core/memory/token_buffer_memory.py b/api/core/memory/token_buffer_memory.py
index 51813d5de36..c1f16e932ce 100644
--- a/api/core/memory/token_buffer_memory.py
+++ b/api/core/memory/token_buffer_memory.py
@@ -21,10 +21,13 @@ from graphon.model_runtime.entities import (
UserPromptMessage,
)
from graphon.model_runtime.entities.message_entities import PromptMessageContentUnionTypes
+from models.agent_config_entities import AgentSoulConfig
+from models.enums import ConversationFromSource
from models.model import App, AppMode, Conversation, Message, MessageFile
from models.workflow import Workflow
from repositories.api_workflow_run_repository import APIWorkflowRunRepository
from repositories.factory import DifyAPIRepositoryFactory
+from services.agent.runtime_config_service import AgentRuntimeConfigService
_file_access_controller = DatabaseFileAccessController()
@@ -154,7 +157,25 @@ class TokenBufferMemory:
session: Session,
workflow_run_repo: APIWorkflowRunRepository | None,
) -> FileUploadConfig | None:
+ """Read attachment settings using the caller's session without file I/O.
+
+ Agent and workflow modes require the owning app. Agent settings come
+ from the conversation's Soul, never the legacy model configuration.
+ Disabled uploads return None; missing required records raise ValueError
+ and unsupported modes raise AssertionError. No records are changed.
+ """
match conversation.mode:
+ case AppMode.AGENT:
+ if not app_record:
+ raise ValueError("App not found for conversation")
+ soul = AgentRuntimeConfigService(session).resolve_conversation_soul(
+ app_model=app_record,
+ conversation=conversation,
+ account_id=conversation.from_account_id,
+ use_debug_draft=conversation.from_source == ConversationFromSource.CONSOLE,
+ )
+ features = (soul or AgentSoulConfig()).app_features.model_dump(mode="json", exclude_none=True)
+ return FileUploadConfigManager.convert(features)
case AppMode.AGENT_CHAT | AppMode.COMPLETION | AppMode.CHAT:
return FileUploadConfigManager.convert(conversation.model_config_with_session(session=session))
case AppMode.ADVANCED_CHAT | AppMode.WORKFLOW:
@@ -211,7 +232,7 @@ class TokenBufferMemory:
"""Read ordered history and attachment configuration without provider or file I/O.
``None`` for message_limit uses the existing 500-message ceiling. A missing
- app keeps text-only legacy history available; workflow attachments still
+ app keeps text-only legacy history available; Agent and workflow attachments still
require the app. An omitted workflow repository uses the configured factory.
"""
# fetch limited messages, and return reversed
diff --git a/api/tests/unit_tests/core/memory/test_token_buffer_memory.py b/api/tests/unit_tests/core/memory/test_token_buffer_memory.py
index da9449b7d6a..c85864bc4f2 100644
--- a/api/tests/unit_tests/core/memory/test_token_buffer_memory.py
+++ b/api/tests/unit_tests/core/memory/test_token_buffer_memory.py
@@ -27,6 +27,15 @@ from graphon.model_runtime.entities import (
TextPromptMessageContent,
UserPromptMessage,
)
+from models.agent import (
+ AgentConfigDraft,
+ AgentConfigDraftType,
+ AgentConfigSnapshot,
+ AgentConfigVersionKind,
+ AgentDebugConversation,
+ AgentWorkspaceBinding,
+)
+from models.agent_config_entities import AgentSoulConfig
from models.base import TypeBase
from models.enums import ConversationFromSource, CreatorUserRole, MessageFileBelongsTo
from models.model import App, AppAnnotationSetting, AppMode, AppModelConfig, Conversation, Message, MessageFile
@@ -64,6 +73,10 @@ def database(sqlite_engine: Engine, monkeypatch: pytest.MonkeyPatch) -> Iterator
Workflow.__table__,
AppModelConfig.__table__,
AppAnnotationSetting.__table__,
+ AgentConfigDraft.__table__,
+ AgentConfigSnapshot.__table__,
+ AgentDebugConversation.__table__,
+ AgentWorkspaceBinding.__table__,
],
)
statements: list[tuple[str, object]] = []
@@ -570,6 +583,73 @@ class TestGetHistoryPromptText:
class TestPreparedHistory:
+ @pytest.mark.parametrize("enabled", [True, False])
+ @pytest.mark.parametrize("debug", [True, False])
+ def test_agent_attachments_use_conversation_soul(self, database: Database, enabled: bool, debug: bool) -> None:
+ """Persisted Agent settings govern both attachment roles, independently of legacy settings."""
+ conversation = _persist_conversation(database, AppMode.AGENT)
+ _enable_file_uploads(database, conversation, enabled=not enabled)
+ app = database.session.get(App, conversation.app_id)
+ assert app is not None
+ agent_id, account_id = str(uuid4()), str(uuid4())
+ snapshot = AgentConfigSnapshot(
+ tenant_id=app.tenant_id,
+ agent_id=agent_id,
+ version=1,
+ config_snapshot=AgentSoulConfig.model_validate(
+ {"app_features": {"file_upload": {"enabled": not enabled if debug else enabled}}}
+ ),
+ )
+ binding = AgentWorkspaceBinding(
+ tenant_id=app.tenant_id,
+ app_id=app.id,
+ workspace_id=str(uuid4()),
+ backend_binding_ref="memory-test",
+ agent_id=agent_id,
+ agent_config_version_id=snapshot.id,
+ agent_config_version_kind=AgentConfigVersionKind.SNAPSHOT,
+ )
+ conversation.agent_workspace_binding_id = binding.id
+ database.session.add_all([snapshot, binding])
+ if debug:
+ conversation.from_source = ConversationFromSource.CONSOLE
+ conversation.from_account_id = account_id
+ database.session.add_all(
+ [
+ AgentConfigDraft(
+ tenant_id=app.tenant_id,
+ agent_id=agent_id,
+ account_id=account_id,
+ draft_type=AgentConfigDraftType.DEBUG_BUILD,
+ config_snapshot=AgentSoulConfig.model_validate(
+ {"app_features": {"file_upload": {"enabled": enabled}}}
+ ),
+ ),
+ AgentDebugConversation(
+ tenant_id=app.tenant_id,
+ app_id=app.id,
+ agent_id=agent_id,
+ account_id=account_id,
+ conversation_id=conversation.id,
+ draft_type=AgentConfigDraftType.DEBUG_BUILD,
+ ),
+ ]
+ )
+ database.session.commit()
+ message = _persist_message(database, conversation.id)
+ user_file = _persist_message_file(database, message, belongs_to=MessageFileBelongsTo.USER)
+ assistant_file = _persist_message_file(database, message, belongs_to=MessageFileBelongsTo.ASSISTANT)
+ expected_ids = [(user_file.id,), (assistant_file.id,)] if enabled else [(), ()]
+
+ history = TokenBufferMemory.load_history(
+ conversation=conversation, app_record=app, session=database.session, message_limit=3
+ )
+ database.session.close()
+
+ assert [prompt.text for prompt in history.prompts] == ["user query", "hello"]
+ assert [tuple(file.id for file in prompt.files) for prompt in history.prompts] == expected_ids
+ assert all(prompt.image_detail == ImagePromptMessageContent.DETAIL.HIGH for prompt in history.prompts)
+
@pytest.mark.parametrize("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.COMPLETION])
@pytest.mark.parametrize("detail", ["low", "high"])
def test_load_detaches_history_before_attachment_and_token_io(