"""Exact presentation for a single completed calculation, independent of business domain.""" from app.modules.work_assistant.schemas import ContinueDecision from app.modules.work_assistant.presentation import display_note, numeric_display def precise_answer(payload): if len(payload.root_queries)!=1 or len(payload.evidence)!=1: return None if payload.question_spec.get('requested_assessments') or payload.question_spec.get('limitation'): return None query=payload.root_queries[0] item=payload.evidence[0] if item.status!='ok' or item.resource!=query.resource: return None progress = task_progress_answer(payload, query, item) if progress is not None: return progress computed=bool(query.analysis and item.computation and item.computation.get('complete')) if not (query.operation==item.operation=='count' or computed): return None if computed and query.analysis.metric=='none': return None # Filtered prose/detail still needs relevance selection and explanation. from app.modules.work_assistant.answers import _conditions definitions={r['id']:r for r in payload.resources} conditions=_conditions(item,definitions) references=['result:0'] if not computed: lines=[f'{item.title}:**{item.total} 项**。'] else: names=[f for f in item.fields if f not in {'id','analysis_rows','analysis_nonempty'}] binding=definitions.get(item.resource,{}).get('subject_binding',{}) if binding.get('name_field') in names and binding.get('id_field') in names: names.remove(binding['id_field']) if 'analysis_value' not in names: return None metric_field = query.analysis.metric_field unit = definitions.get(item.resource, {}).get('field_units', {}).get(metric_field, '') if query.analysis.metric in {'count', 'distinct_count'}: unit = '' label = item.fields['analysis_value'] if not unit: import re match = re.search(r'[((](万元|元)[))]', label) unit = match.group(1) if match else '' scalar = not query.analysis.group_by and item.total == 1 and len(item.rows) == 1 if scalar: value = numeric_display(item.rows[0].get('analysis_value'), unit) scalar_label = label.replace(f'({unit})', '').replace(f'({unit})', '') if unit else label lines = [f'{scalar_label}:**{value}**。'] else: lines=[f'### {item.title}','',f'共 **{item.total} 组**,本次展示 {len(item.rows)} 组。',''] def cell(value): return str(value if value not in (None,'') else '未提供').replace('|','/').replace('\n',' ') if not scalar: lines+=['| '+' | '.join(cell(item.fields[f]) for f in names)+' |', '| '+' | '.join('---' for _ in names)+' |'] for row in item.rows: if any(f not in row for f in names): return None if not scalar: lines.append('| '+' | '.join(cell(numeric_display(row[f], unit) if f == 'analysis_value' else row[f]) for f in names)+' |') references.append(f"{item.resource}:{row['id']}") if conditions: lines+=['',conditions] note = display_note(item.note, definitions.get(item.resource)) if note: lines+=['','> '+note.replace('\n','\n> ')] return ContinueDecision(answer='\n'.join(lines),source_ids=references) def task_progress_answer(payload, query, item): """Summarize registered task fields without inventing completion dates/causes. Only the ordinary single-task-list progress question takes this path. Other analysis, requested evidence and missing fields stay with the bounded agent. """ import re from datetime import date if (query.resource != 'tasks' or query.operation != 'list' or query.analysis or query.include or not item.rows or not re.search(r'任务.*(?:进度|进展|完成情况)', payload.message) or re.search(r'原因|为什么|反馈|对比|相比|评分|评价|质量|完成率|风险', payload.message) or any(not row.get('status') or not (row.get('name') or row.get('title')) for row in item.rows)): return None from app.modules.work_assistant.answers import _conditions definitions = {r['id']: r for r in payload.resources} conditions = _conditions(item, definitions) complete = item.offset == 0 and item.total == len(item.rows) done = sum(row['status'] == '已完成' for row in item.rows) active = sum(row['status'] == '进行中' for row in item.rows) other = len(item.rows) - done - active count_scope = '其中' if complete else f'本页展示 {len(item.rows)} 项,其中' lines = [f'共 **{item.total} 项任务**。{count_scope}已完成 **{done} 项**,进行中 **{active} 项**' + (f',其他状态 **{other} 项**。' if other else '。')] if conditions: lines += ['', conditions] lines += ['', '| 任务名称 | 状态 | 进度 | 截止日期 |', '| --- | --- | --- | --- |'] def cell(value): return str(value if value not in (None, '') else '未提供').replace('|', '/').replace('\n', ' ') refs = ['result:0'] overdue = [] for row in item.rows: name = row.get('name') or row.get('title') progress = row.get('progress') lines.append('| ' + ' | '.join(cell(v) for v in (name, row['status'], f'{progress}%' if progress not in (None, '') else None, row.get('deadline'))) + ' |') if row.get('id') is not None: refs.append(f"tasks:{row['id']}") if row['status'] == '进行中' and row.get('deadline') and payload.today: try: days = (date.fromisoformat(payload.today) - date.fromisoformat(str(row['deadline'])[:10])).days if days > 0: overdue.append((days, name)) except ValueError: pass if overdue: lines += ['', ('建议优先核对:' if re.search(r'优先|关注|建议|安排', payload.message) else '已过截止日期的进行中任务:') + ';'.join(f'{name}({days} 天)' for days, name in sorted(overdue, key=lambda x: -x[0])) + '。'] if not complete: lines += ['', '状态数量仅对应本页,未展示记录的完成情况尚未统计。'] note = display_note(item.note, definitions.get(item.resource)) if note: lines += ['', note] return ContinueDecision(answer='\n'.join(lines), source_ids=refs)