Pair black and bright defect evidence for priority products
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
"""Plan missing black/bright source-defect queries for the same product scope.
|
||||
|
||||
Only current-turn, release-bound evidence is passed in by the runtime. This
|
||||
module never estimates a missing stage or reuses another product's evidence.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _single_stage_requested(question: str, context: Any) -> bool:
|
||||
if isinstance(context, dict) and context.get("stage") in ("black", "bright"):
|
||||
return True
|
||||
text = question if isinstance(question, str) else ""
|
||||
if re.search(r"(?:只|仅)(?:看|查|分析|要|关注)?(?:黑片|亮片)", text):
|
||||
return True
|
||||
stages = [stage for label, stage in (("黑片", "black"), ("亮片", "bright")) if label in text]
|
||||
return len(stages) == 1 and not re.search(r"整体|总体|综合|全部|两阶段|重点产品|重点规格", text)
|
||||
|
||||
|
||||
def plan_missing_product_defect_stages(
|
||||
documents: list[dict[str, Any]], question: str, context: Any = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Return the missing counterpart queries; identical scope/period is required."""
|
||||
if _single_stage_requested(question, context):
|
||||
return []
|
||||
groups: dict[str, dict[str, Any]] = {}
|
||||
for document in documents:
|
||||
result = document.get("result") if isinstance(document, dict) else None
|
||||
if not isinstance(result, dict) or result.get("kind") != "quality_fine_defects":
|
||||
continue
|
||||
if result.get("dimension") != "defect" or result.get("stage") not in ("black", "bright"):
|
||||
continue
|
||||
filters = result.get("filters")
|
||||
product = filters.get("product") if isinstance(filters, dict) else None
|
||||
if not isinstance(product, str) or not product.strip():
|
||||
continue
|
||||
if result.get("category") is not None or result.get("defect") is not None:
|
||||
continue
|
||||
release = result.get("releaseId")
|
||||
period = result.get("period")
|
||||
comparison = result.get("comparisonPeriod")
|
||||
if (not isinstance(release, str) or not release
|
||||
or not isinstance(period, dict) or (comparison is not None and not isinstance(comparison, dict))):
|
||||
continue
|
||||
sort = result.get("sort")
|
||||
if sort not in ("scrap_delta", "current_scrap"):
|
||||
continue
|
||||
identity = json.dumps([release, period, comparison, filters, sort], ensure_ascii=False, sort_keys=True)
|
||||
group = groups.setdefault(identity, {"stages": set(), "source": result})
|
||||
group["stages"].add(result["stage"])
|
||||
|
||||
queries = []
|
||||
for group in groups.values():
|
||||
if group["stages"] == {"black", "bright"}:
|
||||
continue
|
||||
source = group["source"]
|
||||
other = "bright" if "black" in group["stages"] else "black"
|
||||
args = {"mode": "defects", "releaseId": source["releaseId"],
|
||||
"period": source["period"], "filters": source["filters"],
|
||||
"dimension": "defect", "stage": other, "sort": source["sort"], "limit": 20}
|
||||
if source.get("comparisonPeriod") is not None:
|
||||
args["comparisonPeriod"] = source["comparisonPeriod"]
|
||||
queries.append(args)
|
||||
return queries
|
||||
Reference in New Issue
Block a user