"""Require a narrow answer to contain the model-resolved target's verified facts.""" LABELS = {'group':'业务分组','department':'产业部','product':'产品规格','defect':'具体缺陷'} def answer_focus(facts, task): if (not task or task.get('queryPlan') or task.get('kind') != 'analysis' or len(task.get('dimensions', [])) != 1): return None dimension = task['dimensions'][0] if dimension not in LABELS: return None from nao_agent_task import dimension_stages selected_stages = dimension_stages(task, dimension) candidates = [fact for fact in facts if (fact.get('kind') == dimension and fact.get('dimension', dimension) == dimension) or dimension == 'product' and fact.get('kind') == 'product_rate_signal' or dimension == 'department' and fact.get('kind') == 'group' and fact.get('dimension') == dimension] declared = (task.get('targetEntities') or {}).get(dimension, []) declared = list(dict.fromkeys(name for name in declared if isinstance(name, str) and name)) if isinstance(declared,list) else [] if len(declared) >= 2: candidates = [fact for fact in candidates if fact.get('label') in declared] required = [] for stage in selected_stages: rows = [fact for fact in candidates if fact.get('stage') == stage] # A pair is necessary for an object comparison. Keep a bounded reading # path for large rankings, without requiring every returned row. required.extend(fact['id'] for fact in (rows if len(declared) >= 2 else rows[:2])) priority = None # Compare server-returned changes, without recalculating rates or claiming # a contribution to the parent's composite rate. Conflicting signals do # not support one unconditional priority. combined = [row for row in candidates if row.get('stage') is None and all(type(row.get(key)) in (int,float) for key in ('rateDrop','scrapDelta'))] if dimension == 'group' and selected_stages == [None] and len(combined) > 1: for row in combined: others = [other for other in combined if other is not row] if (row['rateDrop'] > 0 and row['scrapDelta'] > 0 and all(row['rateDrop'] > other['rateDrop'] and row['scrapDelta'] > other['scrapDelta'] for other in others)): priority = {'factId':row['id'], 'label':row['label'], 'evidenceRef':row['evidenceRef']} required.append(row['id']) break missing_entities = ([name for name in declared if any(not any(fact.get('label') == name and fact.get('stage') == stage for fact in candidates) for stage in selected_stages)] if len(declared) >= 2 else []) if missing_entities: priority = None return {'dimension':dimension,'label':LABELS[dimension], 'requiredFactIds':list(dict.fromkeys(required)), 'available':bool(required) and not missing_entities, 'missingEntities':missing_entities,'priority':priority} def with_focus_conclusion(answer, focus): priority = (focus or {}).get('priority') if not priority: return answer return ('按本轮可比分组的变化,优先关注'+priority['label']+':合格率降幅与报废增量均较大。' '这是分组变化线索,不代表对总体下降的精确贡献。['+priority['evidenceRef']+']\n\n'+answer) def missing_focus(selected, focus): return bool(focus and (not focus['available'] or not set(focus['requiredFactIds']) <= set(selected))) def focus_fallback(report, task, missing): focus = answer_focus(report['facts'], task) if focus is None: return None selected = focus['requiredFactIds']; known = {fact['id']:fact for fact in report['facts']} if selected: lines = [known[key]['text'].rstrip()+'['+known[key]['evidenceRef']+']' for key in selected] answer = '\n\n'.join(lines) else: answer = '本轮需要回答'+focus['label']+';当前尚无同期间、同范围的有效对比依据,暂不能作出判断。' if missing: answer += '\n\n未覆盖内容:'+';'.join(missing) return with_focus_conclusion(answer, focus), selected