Files
company-ai-platform/app/modules/work_assistant/precise_answers.py
T
2026-11-02 09:28:08 +08:00

66 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""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
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)