refactor: convert AppMode if/elif to match/case in service files (#30001) (#34562)

Co-authored-by: agenthaulk <agenthaulk@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
agenthaulk 2026-04-05 03:22:11 -07:00 committed by GitHub
parent 8b992513b8
commit 5ad8c3e249
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 127 additions and 93 deletions

View File

@ -32,22 +32,33 @@ class AdvancedPromptTemplateService:
def get_common_prompt(cls, app_mode: str, model_mode: str, has_context: str): def get_common_prompt(cls, app_mode: str, model_mode: str, has_context: str):
context_prompt = copy.deepcopy(CONTEXT) context_prompt = copy.deepcopy(CONTEXT)
if app_mode == AppMode.CHAT: match app_mode:
if model_mode == "completion": case AppMode.CHAT:
return cls.get_completion_prompt( match model_mode:
copy.deepcopy(CHAT_APP_COMPLETION_PROMPT_CONFIG), has_context, context_prompt case "completion":
) return cls.get_completion_prompt(
elif model_mode == "chat": copy.deepcopy(CHAT_APP_COMPLETION_PROMPT_CONFIG), has_context, context_prompt
return cls.get_chat_prompt(copy.deepcopy(CHAT_APP_CHAT_PROMPT_CONFIG), has_context, context_prompt) )
elif app_mode == AppMode.COMPLETION: case "chat":
if model_mode == "completion": return cls.get_chat_prompt(
return cls.get_completion_prompt( copy.deepcopy(CHAT_APP_CHAT_PROMPT_CONFIG), has_context, context_prompt
copy.deepcopy(COMPLETION_APP_COMPLETION_PROMPT_CONFIG), has_context, context_prompt )
) case _:
elif model_mode == "chat": pass
return cls.get_chat_prompt( case AppMode.COMPLETION:
copy.deepcopy(COMPLETION_APP_CHAT_PROMPT_CONFIG), has_context, context_prompt match model_mode:
) case "completion":
return cls.get_completion_prompt(
copy.deepcopy(COMPLETION_APP_COMPLETION_PROMPT_CONFIG), has_context, context_prompt
)
case "chat":
return cls.get_chat_prompt(
copy.deepcopy(COMPLETION_APP_CHAT_PROMPT_CONFIG), has_context, context_prompt
)
case _:
pass
case _:
pass
# default return empty dict # default return empty dict
return {} return {}
@ -73,25 +84,38 @@ class AdvancedPromptTemplateService:
def get_baichuan_prompt(cls, app_mode: str, model_mode: str, has_context: str): def get_baichuan_prompt(cls, app_mode: str, model_mode: str, has_context: str):
baichuan_context_prompt = copy.deepcopy(BAICHUAN_CONTEXT) baichuan_context_prompt = copy.deepcopy(BAICHUAN_CONTEXT)
if app_mode == AppMode.CHAT: match app_mode:
if model_mode == "completion": case AppMode.CHAT:
return cls.get_completion_prompt( match model_mode:
copy.deepcopy(BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG), has_context, baichuan_context_prompt case "completion":
) return cls.get_completion_prompt(
elif model_mode == "chat": copy.deepcopy(BAICHUAN_CHAT_APP_COMPLETION_PROMPT_CONFIG),
return cls.get_chat_prompt( has_context,
copy.deepcopy(BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG), has_context, baichuan_context_prompt baichuan_context_prompt,
) )
elif app_mode == AppMode.COMPLETION: case "chat":
if model_mode == "completion": return cls.get_chat_prompt(
return cls.get_completion_prompt( copy.deepcopy(BAICHUAN_CHAT_APP_CHAT_PROMPT_CONFIG), has_context, baichuan_context_prompt
copy.deepcopy(BAICHUAN_COMPLETION_APP_COMPLETION_PROMPT_CONFIG), )
has_context, case _:
baichuan_context_prompt, pass
) case AppMode.COMPLETION:
elif model_mode == "chat": match model_mode:
return cls.get_chat_prompt( case "completion":
copy.deepcopy(BAICHUAN_COMPLETION_APP_CHAT_PROMPT_CONFIG), has_context, baichuan_context_prompt return cls.get_completion_prompt(
) copy.deepcopy(BAICHUAN_COMPLETION_APP_COMPLETION_PROMPT_CONFIG),
has_context,
baichuan_context_prompt,
)
case "chat":
return cls.get_chat_prompt(
copy.deepcopy(BAICHUAN_COMPLETION_APP_CHAT_PROMPT_CONFIG),
has_context,
baichuan_context_prompt,
)
case _:
pass
case _:
pass
# default return empty dict # default return empty dict
return {} return {}

View File

@ -7,11 +7,12 @@ from models.model import AppMode, AppModelConfigDict
class AppModelConfigService: class AppModelConfigService:
@classmethod @classmethod
def validate_configuration(cls, tenant_id: str, config: dict, app_mode: AppMode) -> AppModelConfigDict: def validate_configuration(cls, tenant_id: str, config: dict, app_mode: AppMode) -> AppModelConfigDict:
if app_mode == AppMode.CHAT: match app_mode:
return ChatAppConfigManager.config_validate(tenant_id, config) case AppMode.CHAT:
elif app_mode == AppMode.AGENT_CHAT: return ChatAppConfigManager.config_validate(tenant_id, config)
return AgentChatAppConfigManager.config_validate(tenant_id, config) case AppMode.AGENT_CHAT:
elif app_mode == AppMode.COMPLETION: return AgentChatAppConfigManager.config_validate(tenant_id, config)
return CompletionAppConfigManager.config_validate(tenant_id, config) case AppMode.COMPLETION:
else: return CompletionAppConfigManager.config_validate(tenant_id, config)
raise ValueError(f"Invalid app mode: {app_mode}") case AppMode.WORKFLOW | AppMode.ADVANCED_CHAT | AppMode.CHANNEL | AppMode.RAG_PIPELINE:
raise ValueError(f"Invalid app mode: {app_mode}")

View File

@ -170,34 +170,38 @@ class WorkflowConverter:
graph = self._append_node(graph, llm_node) graph = self._append_node(graph, llm_node)
if new_app_mode == AppMode.WORKFLOW:
# convert to end node by app mode
end_node = self._convert_to_end_node()
graph = self._append_node(graph, end_node)
else:
answer_node = self._convert_to_answer_node()
graph = self._append_node(graph, answer_node)
app_model_config_dict = app_config.app_model_config_dict app_model_config_dict = app_config.app_model_config_dict
# features match new_app_mode:
if new_app_mode == AppMode.ADVANCED_CHAT: case AppMode.WORKFLOW:
features = { end_node = self._convert_to_end_node()
"opening_statement": app_model_config_dict.get("opening_statement"), graph = self._append_node(graph, end_node)
"suggested_questions": app_model_config_dict.get("suggested_questions"), features = {
"suggested_questions_after_answer": app_model_config_dict.get("suggested_questions_after_answer"), "text_to_speech": app_model_config_dict.get("text_to_speech"),
"speech_to_text": app_model_config_dict.get("speech_to_text"), "file_upload": app_model_config_dict.get("file_upload"),
"text_to_speech": app_model_config_dict.get("text_to_speech"), "sensitive_word_avoidance": app_model_config_dict.get("sensitive_word_avoidance"),
"file_upload": app_model_config_dict.get("file_upload"), }
"sensitive_word_avoidance": app_model_config_dict.get("sensitive_word_avoidance"), case AppMode.ADVANCED_CHAT:
"retriever_resource": app_model_config_dict.get("retriever_resource"), answer_node = self._convert_to_answer_node()
} graph = self._append_node(graph, answer_node)
else: features = {
features = { "opening_statement": app_model_config_dict.get("opening_statement"),
"text_to_speech": app_model_config_dict.get("text_to_speech"), "suggested_questions": app_model_config_dict.get("suggested_questions"),
"file_upload": app_model_config_dict.get("file_upload"), "suggested_questions_after_answer": app_model_config_dict.get("suggested_questions_after_answer"),
"sensitive_word_avoidance": app_model_config_dict.get("sensitive_word_avoidance"), "speech_to_text": app_model_config_dict.get("speech_to_text"),
} "text_to_speech": app_model_config_dict.get("text_to_speech"),
"file_upload": app_model_config_dict.get("file_upload"),
"sensitive_word_avoidance": app_model_config_dict.get("sensitive_word_avoidance"),
"retriever_resource": app_model_config_dict.get("retriever_resource"),
}
case _:
answer_node = self._convert_to_answer_node()
graph = self._append_node(graph, answer_node)
features = {
"text_to_speech": app_model_config_dict.get("text_to_speech"),
"file_upload": app_model_config_dict.get("file_upload"),
"sensitive_word_avoidance": app_model_config_dict.get("sensitive_word_avoidance"),
}
# create workflow record # create workflow record
workflow = Workflow( workflow = Workflow(
@ -220,19 +224,23 @@ class WorkflowConverter:
def _convert_to_app_config(self, app_model: App, app_model_config: AppModelConfig) -> EasyUIBasedAppConfig: def _convert_to_app_config(self, app_model: App, app_model_config: AppModelConfig) -> EasyUIBasedAppConfig:
app_mode_enum = AppMode.value_of(app_model.mode) app_mode_enum = AppMode.value_of(app_model.mode)
app_config: EasyUIBasedAppConfig app_config: EasyUIBasedAppConfig
if app_mode_enum == AppMode.AGENT_CHAT or app_model.is_agent: effective_mode = (
app_model.mode = AppMode.AGENT_CHAT AppMode.AGENT_CHAT if app_model.is_agent and app_mode_enum != AppMode.AGENT_CHAT else app_mode_enum
app_config = AgentChatAppConfigManager.get_app_config( )
app_model=app_model, app_model_config=app_model_config match effective_mode:
) case AppMode.AGENT_CHAT:
elif app_mode_enum == AppMode.CHAT: app_model.mode = AppMode.AGENT_CHAT
app_config = ChatAppConfigManager.get_app_config(app_model=app_model, app_model_config=app_model_config) app_config = AgentChatAppConfigManager.get_app_config(
elif app_mode_enum == AppMode.COMPLETION: app_model=app_model, app_model_config=app_model_config
app_config = CompletionAppConfigManager.get_app_config( )
app_model=app_model, app_model_config=app_model_config case AppMode.CHAT:
) app_config = ChatAppConfigManager.get_app_config(app_model=app_model, app_model_config=app_model_config)
else: case AppMode.COMPLETION:
raise ValueError("Invalid app mode") app_config = CompletionAppConfigManager.get_app_config(
app_model=app_model, app_model_config=app_model_config
)
case _:
raise ValueError("Invalid app mode")
return app_config return app_config

View File

@ -1417,16 +1417,17 @@ class WorkflowService:
self._validate_human_input_node_data(node_data) self._validate_human_input_node_data(node_data)
def validate_features_structure(self, app_model: App, features: dict): def validate_features_structure(self, app_model: App, features: dict):
if app_model.mode == AppMode.ADVANCED_CHAT: match app_model.mode:
return AdvancedChatAppConfigManager.config_validate( case AppMode.ADVANCED_CHAT:
tenant_id=app_model.tenant_id, config=features, only_structure_validate=True return AdvancedChatAppConfigManager.config_validate(
) tenant_id=app_model.tenant_id, config=features, only_structure_validate=True
elif app_model.mode == AppMode.WORKFLOW: )
return WorkflowAppConfigManager.config_validate( case AppMode.WORKFLOW:
tenant_id=app_model.tenant_id, config=features, only_structure_validate=True return WorkflowAppConfigManager.config_validate(
) tenant_id=app_model.tenant_id, config=features, only_structure_validate=True
else: )
raise ValueError(f"Invalid app mode: {app_model.mode}") case _:
raise ValueError(f"Invalid app mode: {app_model.mode}")
def _validate_human_input_node_data(self, node_data: dict) -> None: def _validate_human_input_node_data(self, node_data: dict) -> None:
""" """