Files
lyun f1a2fbd494 统一:DSH 智能分析、Top100 与图表静默恢复
接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。
补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。
优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。

验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。
仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
2026-11-12 11:57:41 +08:00

123 lines
4.5 KiB
Python

"""Preserve assistant prose independently of evidence and presentation validation."""
from contextvars import ContextVar
import json
import re
import threading
import time
_CURRENT = ContextVar('quality_original_answer', default=None)
def current():
return _CURRENT.get()
def capture(content, *, phase='native_synthesis'):
store = current()
if store is not None and phase == 'native_synthesis':
store.capture_composition(content)
def capture_envelope(raw, *, phase='native_synthesis'):
"""Capture only provider assistant content, including an interrupted finish."""
try:
document = json.loads(raw)
content = document['choices'][0]['message']['content']
except (ValueError, TypeError, KeyError, IndexError):
return
capture(content, phase=phase)
def business_text(content):
"""Unwrap business text fields without rewriting the model's strings."""
if not isinstance(content, str):
return '', None
try:
document = json.loads(content)
except (ValueError, TypeError):
return content, None
if not isinstance(document, dict):
return content, None
title = document.get('reportTitle')
for field in ('blocks', 'answerBlocks'):
blocks = document.get(field)
if isinstance(blocks, list):
texts = [block['text'] for block in blocks
if isinstance(block, dict) and isinstance(block.get('text'), str)]
if texts:
return '\n\n'.join(texts), title if isinstance(title, str) else None
for field in ('answer', 'markdown', 'content', 'text'):
if isinstance(document.get(field), str):
return document[field], title if isinstance(title, str) else None
return content, title if isinstance(title, str) else None
class OriginalAnswer:
def __init__(self, emit):
self.emit = emit
self.text = ''
self.title = None
self.source = None
self.lock = threading.Lock()
self.last_time = float('-inf')
self.token = None
def __enter__(self):
self.token = _CURRENT.set(self)
return self
def __exit__(self, *_args):
_CURRENT.reset(self.token)
def capture_native(self, text):
heading = re.search(r'^#{1,6} +([^\n]+)', text, re.M) if isinstance(text, str) else None
self._capture(text, 'dsh_assistant', title=heading[1] if heading else None, force=False)
def capture_composition(self, content):
with self.lock:
# Review or retry output cannot replace the first business answer.
if self.source is not None:
return
text, title = business_text(content)
self._capture(text, 'model_business_text', title=title, force=True)
def _capture(self, text, source, *, title=None, force):
if not isinstance(text, str) or not text:
return
with self.lock:
self.text, self.source = text, source
if title is not None or source == 'dsh_assistant':
self.title = title
now = time.monotonic()
publish = force or now - self.last_time >= 0.25
if publish:
self.last_time = now
if publish:
self.emit({'type': 'progress', 'modelAnswer': text})
def settle(self, event):
"""Publish raw prose for every terminal status, with validation metadata."""
if self.source is None and event.get('completionMethod') == 'direct_conversation':
self.capture_composition(event.get('answer'))
if self.source is None and event.get('status') == 'NEEDS_CLARIFICATION':
task = event.get('agentTask') or {}
if isinstance(task.get('clarification'), str):
self.capture_composition(task['clarification'])
event['modelAnswer'] = self.text
event['answer'] = self.text
event['answerOrigin'] = self.source or 'no_model_text'
event.pop('readingSummary', None)
event.pop('reportHtml', None)
event.pop('_verifiedReportNarrative', None)
if self.title is not None:
event['reportTitle'] = self.title
elif self.source == 'dsh_assistant':
event.pop('reportTitle', None)
event['answerValidation'] = {
'status': event.get('status'),
'stopReason': event.get('stopReason'),
'answerQuality': event.get('answerQuality'),
'evidenceCompletion': event.get('evidenceCompletion'),
'originalPreserved': True,
}