fix: preserve gene-family stage intent and completed results

This commit is contained in:
wtx
2026-08-24 09:34:13 +08:00
parent 311a49110f
commit 4a5fdff6f0
10 changed files with 910 additions and 20 deletions
+52 -2
View File
@@ -1344,6 +1344,46 @@ def _build_msa_region_plot_message(params: Dict) -> str:
return "我理解你想对 MSA 指定区间进行局部绘图;请在确认弹窗中填写 alignment 起止列。"
def _stage_keyword_pattern(keyword: str) -> str:
"""Build a boundary-safe regex for one existing stage keyword."""
escaped = re.escape(keyword.lower())
if keyword and keyword[0].isascii() and keyword[0].isalnum():
escaped = rf"(?<![a-z0-9_]){escaped}"
if keyword and keyword[-1].isascii() and keyword[-1].isalnum():
escaped = rf"{escaped}(?![a-z0-9_])"
return escaped
def extract_negated_target_stages(query: str) -> List[str]:
"""Return explicitly excluded ordinary gene-family/RNA-seq target stages."""
query_lower = str(query or "").lower()
if any(marker in query_lower for marker in (
"亚家族", "subfamily", "reference_classification", "subfamily_confirm",
"build_subfamily_evidence", "subfamily_evidence", "subfamily_prediction",
)):
return []
if re.search(r"(?:不是|并非)\s*(?:不执行|不单独执行|不要|不用|无需|不做|跳过|排除|去掉)", query_lower):
return []
eligible = set(CORE_STAGES) | set(RNASEQ_STAGES) | {"expression", "annotation", "msa"}
chinese_prefix = r"(?:不单独执行|不执行|不要(?:执行|做|进行)?|不用(?:执行|做|进行)?|无需(?:执行|做|进行)?|不做|跳过|排除|去掉)\s*"
english_prefix = r"(?:without(?:\s+(?:running|executing|performing))?|skip|exclude|do\s+not\s+(?:run|execute|perform)?)\s+"
excluded = []
for stage, keywords in STAGE_KEYWORDS.items():
if stage not in eligible:
continue
for keyword in sorted(keywords, key=len, reverse=True):
keyword_pattern = _stage_keyword_pattern(keyword)
if (
re.search(chinese_prefix + keyword_pattern, query_lower)
or re.search(english_prefix + keyword_pattern, query_lower)
or re.search(r"(?:^|[\s,,;;。::])-\s*" + keyword_pattern, query_lower)
):
excluded.append(stage)
break
return list(dict.fromkeys(excluded))
def _handle_stage_resolution(query: str, query_lower: str, stages: List[str]) -> Dict:
"""
方案B: 处理阶段解析逻辑
@@ -1352,11 +1392,15 @@ def _handle_stage_resolution(query: str, query_lower: str, stages: List[str]) ->
- 有"分析"但不明确 → 引导澄清
- 完全无法识别 → 引导澄清
"""
excluded_stages = set(extract_negated_target_stages(query))
stages = [stage for stage in stages if stage not in excluded_stages]
# 情况1: 明确指定了具体阶段 → 直接执行。
# 如果同时出现“完整/全流程”等词,则以基础基因家族链路为主体,
# 再追加用户明确点名的 Expression/KaKs/PPI/种间共线性等可选阶段。
if stages:
resolved_stages = list(dict.fromkeys([*CORE_STAGES, *stages])) if _is_complete_workflow_query(query_lower) else stages
resolved_stages = [stage for stage in resolved_stages if stage not in excluded_stages]
return {
"stages": resolved_stages,
"need_clarify": False,
@@ -1368,7 +1412,13 @@ def _handle_stage_resolution(query: str, query_lower: str, stages: List[str]) ->
if any(marker in query_lower for marker in RNASEQ_MARKERS):
if any(kw in query_lower for kw in ["分析", "执行", "运行", "开始", "流程", "pipeline"]):
return {
"stages": ["rnaseq_de", "rnaseq_go_enrich", "rnaseq_kegg_enrich", "rnaseq_fgsea", "rnaseq_wgcna", "rnaseq_candidate_family"],
"stages": [
stage for stage in (
"rnaseq_de", "rnaseq_go_enrich", "rnaseq_kegg_enrich",
"rnaseq_fgsea", "rnaseq_wgcna", "rnaseq_candidate_family",
)
if stage not in excluded_stages
],
"need_clarify": False,
"confidence": 0.9,
"note": "generic_rnaseq_workflow"
@@ -1377,7 +1427,7 @@ def _handle_stage_resolution(query: str, query_lower: str, stages: List[str]) ->
# 情况2: "基因家族分析" = 基础基因家族链路
if _is_complete_workflow_query(query_lower):
return {
"stages": CORE_STAGES,
"stages": [stage for stage in CORE_STAGES if stage not in excluded_stages],
"need_clarify": False,
"confidence": 0.9,
"note": "完整流程"