修复:DSH 自主查询、图片直显与报告稳定性

允许 Agent 在循环中查目录、修正查询和提问,保留实时权限检查及模型原始回答。
统一相同查询证据与图表资源;排名补齐阶段指标,修正 OR 重量筛选,并限制模型明细输入。
增加运行恢复、图表自适应坐标轴与两位小数,修复报告主图选择、重复图及边缘标签。

验证:独立副本通过 162 项 Python、73 项前端及 21 项后端测试,前端构建通过。
线上已用原 8 题复测,并追加两轮双期对比;TOP100 与组合筛选分别核对 100 行和 20 行。
This commit is contained in:
2026-11-12 16:14:20 +08:00
parent f1a2fbd494
commit bc19475b29
35 changed files with 983 additions and 114 deletions
+73 -1
View File
@@ -5,6 +5,29 @@ import copy
import math
import re
def presentation_context(context):
"""历史展示语法仅投影为输入标题,不修改会话存储或用户问题。"""
result = copy.deepcopy(context)
def project(value, key=''):
if isinstance(value, dict):
return {name: project(item, name) for name, item in value.items()}
if isinstance(value, list):
return [project(item, key) for item in value]
if isinstance(value, str) and key in ('answer', 'modelAnswer', 'reportMarkdown'):
return re.sub(r'!\[([^\]\n]*)\]\(chart:[^\n)]*\)', lambda match: match[1], value)
return value
for key in ('_serverHistory', 'historySnapshot'):
if key in result:
result[key] = project(result[key])
result['presentation'] = {
'charts': 'platform_managed', 'imageResources': [],
'guidance': '平台使用本轮正式数据绘制真实图表。初始无图片资源;查询工具结果中的presentation.images会提供真实src和标题,可用![标题](src)直接展示贴题图表。只使用工具明确提供的src,不自行编造ID或地址,不重复插图。没有可用资源时直接写文字和必要表格。历史图表标题只是上下文,不是本轮可用资源。',
}
return result
DIRECT_PLANNING_PROMPT = '''你只理解本轮质量分析目标与范围,返回一个JSON对象,不设计查询步骤、不计算、不回答业务结论。
默认新业务任务使用kind=analysis、executionMode=native_agent。不要输出queryPlan。DSH会自行决定查询与排名。
必填:goal(80字内概括,不复述原问题)、kind、freshQuery(bool)、needsClarification(bool)、verifySource(bool)、fullDetails(bool)、filterUpdates(对象)、fields(数组)、stages(数组)、dimensions(数组)。原始问题会完整交给DSH。
@@ -267,6 +290,9 @@ def canonical_proposal(value, *, question='', context=None):
updates.update(department=None, group=None)
value['dimensions'] = list(dict.fromkeys(['department', *[d for d in value.get('dimensions', []) if d not in ('group', 'department')]]))
value['dimensionStages'] = {'department': list(value.get('stages') or [])}
if (re.search(r'(?:总体|综合|整体)质量(?:分析)?报告', question)
and not re.search(r'只(?:看|分析|展示).{0,8}(?:投入|报废)重量', question)):
value['fields'] = list(dict.fromkeys(['rate', *value.get('fields', [])]))
if (value.get('comparisonRequired') or value.get('comparisonPeriod')):
declared = value.get('aggregateSelection')
if isinstance(declared, dict) and declared.get('metric') == 'rate' and not declared.get('weightFilter'):
@@ -301,6 +327,50 @@ def compact_tool_result(result, *, task=None):
if view.get('kind') == 'quality_catalog' and isinstance(task, dict) and not (task.get('verifySource') or task.get('fullDetails')):
view.pop('sourceFields', None)
view.pop('investigation', None)
if (view.get('kind') == 'quality_comparison' and (task or {}).get('fields') == ['rate']
and not (task or {}).get('verifySource') and not (task or {}).get('fullDetails')):
def rate_point(point):
if not isinstance(point, dict):
return point
return {**{key: point[key] for key in ('period','compositeRate','coverage') if key in point},
**{stage: {'rate': point[stage].get('rate')} for stage in ('black','bright')
if isinstance(point.get(stage), dict)}}
view['current'] = rate_point(view.get('current'))
view['comparison'] = rate_point(view.get('comparison'))
for key in ('calculationRules',):
view.pop(key, None)
current, previous = view.get('current') or {}, view.get('comparison') or {}
view['comparisonAnswer'] = {'currentPeriod': current.get('period'),
'comparisonPeriod': previous.get('period'), 'rows': [
{'metric': '综合合格率', 'current': current.get('compositeRate'),
'comparison': previous.get('compositeRate'), 'changePP': (view.get('delta') or {}).get('percentagePoints')},
*[{'metric': label, 'current': (current.get(stage) or {}).get('rate'),
'comparison': (previous.get(stage) or {}).get('rate'),
'changePP': (view.get('stageRateChangesPP') or {}).get(stage)}
for stage, label in (('black','黑片合格率'),('bright','亮片合格率'))]],
'guidance': '三项正式率值和变化已在同一双期查询中取得,直接按这些值回答;无需再查询相同期间确认。'}
if view.get('kind') == 'quality_metrics' and view.get('view') in ('product','department','group'):
# Keep the complete signed result out of the model context. Pagination
# affects this projection only; aggregate selection already ran on all rows.
rows = view.get('rows', [])
page = (task or {}).get('modelPage') or {}
offset = page.get('offset', 0)
limit = min(100, max(1, page.get('limit', 100)))
total = len(rows)
def slim(row):
item = {key: row[key] for key in ('key','label','rank','selectedRate','compositeRate') if key in row}
for stage in ('black', 'bright'):
if isinstance(row.get(stage), dict):
item[stage] = {key: value for key, value in row[stage].items()
if key in ('rate','scrapRate','totalWeight','scrapWeight')}
return item
view['rows'] = [slim(row) for row in rows[offset:offset + limit]]
view['rowsReturned'] = len(view['rows'])
view['modelPage'] = {'offset': offset, 'limit': limit, 'totalRows': total,
'returnedRows': len(view['rows']), 'hasMore': offset + limit < total,
'nextOffset': offset + limit if offset + limit < total else None,
'completeSource': view.get('rowsTruncated') is False}
view['rowsTruncated'] = bool(view.get('rowsTruncated') or total > len(view['rows']))
view['numericPresentation'] = {
'ratios': 'rate/compositeRate/scrapRate/share为比例,展示时乘100并保留2位小数,百分号%;不是已乘100的数值。',
'weights': '重量单位kg,展示保留2位小数;原数值保留用于计算。',
@@ -310,7 +380,9 @@ def compact_tool_result(result, *, task=None):
if view.get('kind') == 'quality_weight_ranking':
view['rankingDisplay'] = {
'dimension': view.get('dimension'), 'returnedRows': view.get('returnedRows'),
'complete': view.get('truncated') is False,
'complete': view.get('rankingCoverage', {}).get('completeRanking') is True,
'returnedTopComplete': view.get('returnedRows') == len(view.get('rows', [])),
'rateField': 'rows[].metrics.rate为所选阶段合格率;不是综合合格率。',
'quantityRule': 'Top N是该维度最多N项。完整榜少于N项就按实际数量回答;不能用业务分组、产品或其他维度补足,不扩大limit重查完整榜。',
}
if view.get('kind') == 'quality_fine_defects':