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.

refactor: convert appmode misc if/elif to match/case (#30001) (#34869)

dataCenter4301117b6e72d76
diff --git a/api/core/memory/token_buffer_memory.py b/api/core/memory/token_buffer_memory.py
index 09c84538a9a..5809d6f74a7 100644
--- a/api/core/memory/token_buffer_memory.py
+++ b/api/core/memory/token_buffer_memory.py
@@ -61,27 +61,28 @@ class TokenBufferMemory:
:param is_user_message: whether this is a user message
:return: PromptMessage
"""
- if self.conversation.mode in {AppMode.AGENT_CHAT, AppMode.COMPLETION, AppMode.CHAT}:
- file_extra_config = FileUploadConfigManager.convert(self.conversation.model_config)
- elif self.conversation.mode in {AppMode.ADVANCED_CHAT, AppMode.WORKFLOW}:
- app = self.conversation.app
- if not app:
- raise ValueError("App not found for conversation")
-
- if not message.workflow_run_id:
- raise ValueError("Workflow run ID not found")
-
- workflow_run = self.workflow_run_repo.get_workflow_run_by_id(
- tenant_id=app.tenant_id, app_id=app.id, run_id=message.workflow_run_id
- )
- if not workflow_run:
- raise ValueError(f"Workflow run not found: {message.workflow_run_id}")
- workflow = db.session.scalar(select(Workflow).where(Workflow.id == workflow_run.workflow_id))
- if not workflow:
- raise ValueError(f"Workflow not found: {workflow_run.workflow_id}")
- file_extra_config = FileUploadConfigManager.convert(workflow.features_dict, is_vision=False)
- else:
- raise AssertionError(f"Invalid app mode: {self.conversation.mode}")
+ match self.conversation.mode:
+ case AppMode.AGENT_CHAT | AppMode.COMPLETION | AppMode.CHAT:
+ file_extra_config = FileUploadConfigManager.convert(self.conversation.model_config)
+ case AppMode.ADVANCED_CHAT | AppMode.WORKFLOW:
+ app = self.conversation.app
+ if not app:
+ raise ValueError("App not found for conversation")
+
+ if not message.workflow_run_id:
+ raise ValueError("Workflow run ID not found")
+
+ workflow_run = self.workflow_run_repo.get_workflow_run_by_id(
+ tenant_id=app.tenant_id, app_id=app.id, run_id=message.workflow_run_id
+ )
+ if not workflow_run:
+ raise ValueError(f"Workflow run not found: {message.workflow_run_id}")
+ workflow = db.session.scalar(select(Workflow).where(Workflow.id == workflow_run.workflow_id))
+ if not workflow:
+ raise ValueError(f"Workflow not found: {workflow_run.workflow_id}")
+ file_extra_config = FileUploadConfigManager.convert(workflow.features_dict, is_vision=False)
+ case _:
+ raise AssertionError(f"Invalid app mode: {self.conversation.mode}")
detail = ImagePromptMessageContent.DETAIL.HIGH
if file_extra_config and app_record:
diff --git a/api/models/utils/file_input_compat.py b/api/models/utils/file_input_compat.py
index f71583c1cde..8b767779ce4 100644
--- a/api/models/utils/file_input_compat.py
+++ b/api/models/utils/file_input_compat.py
@@ -66,12 +66,15 @@ def build_file_from_stored_mapping(
record_id = resolve_file_record_id(mapping)
transfer_method = FileTransferMethod.value_of(mapping["transfer_method"])
- if transfer_method == FileTransferMethod.TOOL_FILE and record_id:
- mapping["tool_file_id"] = record_id
- elif transfer_method in [FileTransferMethod.LOCAL_FILE, FileTransferMethod.REMOTE_URL] and record_id:
- mapping["upload_file_id"] = record_id
- elif transfer_method == FileTransferMethod.DATASOURCE_FILE and record_id:
- mapping["datasource_file_id"] = record_id
+ match transfer_method:
+ case FileTransferMethod.TOOL_FILE if record_id:
+ mapping["tool_file_id"] = record_id
+ case FileTransferMethod.LOCAL_FILE | FileTransferMethod.REMOTE_URL if record_id:
+ mapping["upload_file_id"] = record_id
+ case FileTransferMethod.DATASOURCE_FILE if record_id:
+ mapping["datasource_file_id"] = record_id
+ case _:
+ pass
if transfer_method == FileTransferMethod.REMOTE_URL and record_id is None:
remote_url = mapping.get("remote_url")
diff --git a/api/services/app_dsl_service.py b/api/services/app_dsl_service.py
index c6c8a151093..40e1e5f8ab8 100644
--- a/api/services/app_dsl_service.py
+++ b/api/services/app_dsl_service.py
@@ -467,61 +467,67 @@ class AppDslService:
)
# Initialize app based on mode
- if app_mode in {AppMode.ADVANCED_CHAT, AppMode.WORKFLOW}:
- workflow_data = data.get("workflow")
- if not workflow_data or not isinstance(workflow_data, dict):
- raise ValueError("Missing workflow data for workflow/advanced chat app")
-
- environment_variables_list = workflow_data.get("environment_variables", [])
- environment_variables = [
- variable_factory.build_environment_variable_from_mapping(obj) for obj in environment_variables_list
- ]
- conversation_variables_list = workflow_data.get("conversation_variables", [])
- conversation_variables = [
- variable_factory.build_conversation_variable_from_mapping(obj) for obj in conversation_variables_list
- ]
-
- workflow_service = WorkflowService()
- current_draft_workflow = workflow_service.get_draft_workflow(app_model=app)
- if current_draft_workflow:
- unique_hash = current_draft_workflow.unique_hash
- else:
- unique_hash = None
- graph = workflow_data.get("graph", {})
- for node in graph.get("nodes", []):
- if node.get("data", {}).get("type", "") == BuiltinNodeTypes.KNOWLEDGE_RETRIEVAL:
- dataset_ids = node["data"].get("dataset_ids", [])
- node["data"]["dataset_ids"] = [
- decrypted_id
- for dataset_id in dataset_ids
- if (decrypted_id := self.decrypt_dataset_id(encrypted_data=dataset_id, tenant_id=app.tenant_id))
- ]
- workflow_service.sync_draft_workflow(
- app_model=app,
- graph=workflow_data.get("graph", {}),
- features=workflow_data.get("features", {}),
- unique_hash=unique_hash,
- account=account,
- environment_variables=environment_variables,
- conversation_variables=conversation_variables,
- )
- elif app_mode in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.COMPLETION}:
- # Initialize model config
- model_config = data.get("model_config")
- if not model_config or not isinstance(model_config, dict):
- raise ValueError("Missing model_config for chat/agent-chat/completion app")
- # Initialize or update model config
- if not app.app_model_config:
- app_model_config = AppModelConfig(
- app_id=app.id, created_by=account.id, updated_by=account.id
- ).from_model_config_dict(cast(AppModelConfigDict, model_config))
- app_model_config.id = str(uuid4())
- app.app_model_config_id = app_model_config.id
-
- self._session.add(app_model_config)
- app_model_config_was_updated.send(app, app_model_config=app_model_config)
- else:
- raise ValueError("Invalid app mode")
+ match app_mode:
+ case AppMode.ADVANCED_CHAT | AppMode.WORKFLOW:
+ workflow_data = data.get("workflow")
+ if not workflow_data or not isinstance(workflow_data, dict):
+ raise ValueError("Missing workflow data for workflow/advanced chat app")
+
+ environment_variables_list = workflow_data.get("environment_variables", [])
+ environment_variables = [
+ variable_factory.build_environment_variable_from_mapping(obj) for obj in environment_variables_list
+ ]
+ conversation_variables_list = workflow_data.get("conversation_variables", [])
+ conversation_variables = [
+ variable_factory.build_conversation_variable_from_mapping(obj)
+ for obj in conversation_variables_list
+ ]
+
+ workflow_service = WorkflowService()
+ current_draft_workflow = workflow_service.get_draft_workflow(app_model=app)
+ if current_draft_workflow:
+ unique_hash = current_draft_workflow.unique_hash
+ else:
+ unique_hash = None
+ graph = workflow_data.get("graph", {})
+ for node in graph.get("nodes", []):
+ if node.get("data", {}).get("type", "") == BuiltinNodeTypes.KNOWLEDGE_RETRIEVAL:
+ dataset_ids = node["data"].get("dataset_ids", [])
+ node["data"]["dataset_ids"] = [
+ decrypted_id
+ for dataset_id in dataset_ids
+ if (
+ decrypted_id := self.decrypt_dataset_id(
+ encrypted_data=dataset_id, tenant_id=app.tenant_id
+ )
+ )
+ ]
+ workflow_service.sync_draft_workflow(
+ app_model=app,
+ graph=workflow_data.get("graph", {}),
+ features=workflow_data.get("features", {}),
+ unique_hash=unique_hash,
+ account=account,
+ environment_variables=environment_variables,
+ conversation_variables=conversation_variables,
+ )
+ case AppMode.CHAT | AppMode.AGENT_CHAT | AppMode.COMPLETION:
+ # Initialize model config
+ model_config = data.get("model_config")
+ if not model_config or not isinstance(model_config, dict):
+ raise ValueError("Missing model_config for chat/agent-chat/completion app")
+ # Initialize or update model config
+ if not app.app_model_config:
+ app_model_config = AppModelConfig(
+ app_id=app.id, created_by=account.id, updated_by=account.id
+ ).from_model_config_dict(cast(AppModelConfigDict, model_config))
+ app_model_config.id = str(uuid4())
+ app.app_model_config_id = app_model_config.id
+
+ self._session.add(app_model_config)
+ app_model_config_was_updated.send(app, app_model_config=app_model_config)
+ case _:
+ raise ValueError("Invalid app mode")
return app
@classmethod