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 isinstance chains to match/case pattern (#36364)

Nicholas-Xiong77333e57a761
diff --git a/api/core/memory/token_buffer_memory.py b/api/core/memory/token_buffer_memory.py
index c41c175ccab..c7db6d986f7 100644
--- a/api/core/memory/token_buffer_memory.py
+++ b/api/core/memory/token_buffer_memory.py
@@ -235,10 +235,11 @@ class TokenBufferMemory:
if isinstance(m.content, list):
inner_msg = ""
for content in m.content:
- if isinstance(content, TextPromptMessageContent):
- inner_msg += f"{content.data}\n"
- elif isinstance(content, ImagePromptMessageContent):
- inner_msg += "[image]\n"
+ match content:
+ case TextPromptMessageContent():
+ inner_msg += f"{content.data}\n"
+ case ImagePromptMessageContent():
+ inner_msg += "[image]\n"
string_messages.append(f"{role}: {inner_msg.strip()}")
else:
diff --git a/api/core/tools/tool_label_manager.py b/api/core/tools/tool_label_manager.py
index d8969a3391d..301ed3e1084 100644
--- a/api/core/tools/tool_label_manager.py
+++ b/api/core/tools/tool_label_manager.py
@@ -79,12 +79,13 @@ class ToolLabelManager:
:return: list of tool labels (str)
"""
- if isinstance(controller, ApiToolProviderController | WorkflowToolProviderController):
- provider_id = controller.provider_id
- elif isinstance(controller, BuiltinToolProviderController):
- return controller.tool_labels
- else:
- raise ValueError("Unsupported tool type")
+ match controller:
+ case ApiToolProviderController() | WorkflowToolProviderController():
+ provider_id = controller.provider_id
+ case BuiltinToolProviderController():
+ return controller.tool_labels
+ case _:
+ raise ValueError("Unsupported tool type")
stmt = select(ToolLabelBinding.label_name).where(
ToolLabelBinding.tool_id == provider_id,
ToolLabelBinding.tool_type == controller.provider_type,
diff --git a/api/services/rag_pipeline/rag_pipeline.py b/api/services/rag_pipeline/rag_pipeline.py
index 91d917b0b17..fd02a44995d 100644
--- a/api/services/rag_pipeline/rag_pipeline.py
+++ b/api/services/rag_pipeline/rag_pipeline.py
@@ -618,26 +618,27 @@ class RagPipelineService:
for key, value in datasource_parameters.items():
param_value = value.get("value")
- if not param_value:
- variables_map[key] = param_value
- elif isinstance(param_value, str):
- # handle string type parameter value, check if it contains variable reference pattern
- pattern = r"\{\{#([a-zA-Z0-9_]{1,50}(?:\.[a-zA-Z0-9_][a-zA-Z0-9_]{0,29}){1,10})#\}\}"
- match = re.match(pattern, param_value)
- if match:
- # extract variable path and try to get value from user inputs
- full_path = match.group(1)
- last_part = full_path.split(".")[-1]
+ match param_value:
+ case None | "" | [] | {}:
+ variables_map[key] = param_value
+ case str():
+ # handle string type parameter value, check if it contains variable reference pattern
+ pattern = r"\{\{#([a-zA-Z0-9_]{1,50}(?:\.[a-zA-Z0-9_][a-zA-Z0-9_]{0,29}){1,10})#\}\}"
+ match_result = re.match(pattern, param_value)
+ if match_result:
+ # extract variable path and try to get value from user inputs
+ full_path = match_result.group(1)
+ last_part = full_path.split(".")[-1]
+ variables_map[key] = user_inputs.get(last_part, param_value)
+ else:
+ variables_map[key] = param_value
+ case list() if param_value:
+ # handle list type parameter value, check if the last element is in user inputs
+ last_part = param_value[-1]
variables_map[key] = user_inputs.get(last_part, param_value)
- else:
+ case _:
+ # other type directly use original value
variables_map[key] = param_value
- elif isinstance(param_value, list) and param_value:
- # handle list type parameter value, check if the last element is in user inputs
- last_part = param_value[-1]
- variables_map[key] = user_inputs.get(last_part, param_value)
- else:
- # other type directly use original value
- variables_map[key] = param_value
from core.datasource.datasource_manager import DatasourceManager
diff --git a/api/services/tools/tools_transform_service.py b/api/services/tools/tools_transform_service.py
index 47aca9b0af3..effbbaad019 100644
--- a/api/services/tools/tools_transform_service.py
+++ b/api/services/tools/tools_transform_service.py
@@ -78,32 +78,33 @@ class ToolTransformService:
:param tenant_id: the tenant id
:param provider: the provider dict
"""
- if isinstance(provider, dict) and "icon" in provider:
- provider["icon"] = ToolTransformService.get_tool_provider_icon_url(
- provider_type=provider["type"], provider_name=provider["name"], icon=provider["icon"]
- )
- elif isinstance(provider, ToolProviderApiEntity):
- if provider.plugin_id:
- if isinstance(provider.icon, str):
- provider.icon = PluginService.get_plugin_icon_url(tenant_id=tenant_id, filename=provider.icon)
- if isinstance(provider.icon_dark, str) and provider.icon_dark:
- provider.icon_dark = PluginService.get_plugin_icon_url(
- tenant_id=tenant_id, filename=provider.icon_dark
- )
- else:
- provider.icon = ToolTransformService.get_tool_provider_icon_url(
- provider_type=provider.type.value, provider_name=provider.name, icon=provider.icon
+ match provider:
+ case dict() if "icon" in provider:
+ provider["icon"] = ToolTransformService.get_tool_provider_icon_url(
+ provider_type=provider["type"], provider_name=provider["name"], icon=provider["icon"]
)
- if provider.icon_dark:
- provider.icon_dark = ToolTransformService.get_tool_provider_icon_url(
- provider_type=provider.type.value, provider_name=provider.name, icon=provider.icon_dark
- )
- elif isinstance(provider, PluginDatasourceProviderEntity):
- if provider.plugin_id:
- if isinstance(provider.declaration.identity.icon, str):
- provider.declaration.identity.icon = PluginService.get_plugin_icon_url(
- tenant_id=tenant_id, filename=provider.declaration.identity.icon
+ case ToolProviderApiEntity():
+ if provider.plugin_id:
+ if isinstance(provider.icon, str):
+ provider.icon = PluginService.get_plugin_icon_url(tenant_id=tenant_id, filename=provider.icon)
+ if isinstance(provider.icon_dark, str) and provider.icon_dark:
+ provider.icon_dark = PluginService.get_plugin_icon_url(
+ tenant_id=tenant_id, filename=provider.icon_dark
+ )
+ else:
+ provider.icon = ToolTransformService.get_tool_provider_icon_url(
+ provider_type=provider.type.value, provider_name=provider.name, icon=provider.icon
)
+ if provider.icon_dark:
+ provider.icon_dark = ToolTransformService.get_tool_provider_icon_url(
+ provider_type=provider.type.value, provider_name=provider.name, icon=provider.icon_dark
+ )
+ case PluginDatasourceProviderEntity():
+ if provider.plugin_id:
+ if isinstance(provider.declaration.identity.icon, str):
+ provider.declaration.identity.icon = PluginService.get_plugin_icon_url(
+ tenant_id=tenant_id, filename=provider.declaration.identity.icon
+ )
@classmethod
def builtin_provider_to_user_provider(