马一丁

Optimize the Progress Bar Display in Report Engine

... ... @@ -386,6 +386,7 @@ class ReportAgent:
'template': template_result.get('template_name'),
'reason': template_result.get('selection_reason')
})
emit('progress', {'progress': 10, 'message': '模板选择完成'})
sections = self._slice_template(template_result.get('template_content', ''))
if not sections:
raise ValueError("模板无法解析出章节,请检查模板内容。")
... ... @@ -407,6 +408,7 @@ class ReportAgent:
'title': layout_design.get('title'),
'toc': layout_design.get('tocTitle')
})
emit('progress', {'progress': 15, 'message': '文档标题/目录设计完成'})
# 使用刚生成的设计稿对全书进行篇幅规划,约束各章字数与重点
word_plan = self.word_budget_node.run(
sections,
... ... @@ -420,6 +422,7 @@ class ReportAgent:
'stage': 'word_plan_ready',
'chapter_targets': len(word_plan.get('chapters', []))
})
emit('progress', {'progress': 20, 'message': '章节字数规划已生成'})
# 记录每个章节的目标字数/强调点,后续传给章节LLM
chapter_targets = {
entry.get("chapterId"): entry
... ... @@ -472,6 +475,9 @@ class ReportAgent:
chapter_max_attempts = max(
self._CONTENT_SPARSE_MIN_ATTEMPTS, self.config.CHAPTER_JSON_MAX_ATTEMPTS
)
total_chapters = len(sections) # 总章节数
completed_chapters = 0 # 已完成章节数
for section in sections:
logger.info(f"生成章节: {section.title}")
emit('chapter_status', {
... ... @@ -587,6 +593,13 @@ class ReportAgent:
f"{section.title} 章节JSON在 {chapter_max_attempts} 次尝试后仍无法解析"
)
chapters.append(chapter_payload)
completed_chapters += 1 # 更新已完成章节数
# 计算当前进度:20% + 80% * (已完成章节数 / 总章节数),四舍五入
chapter_progress = 20 + round(80 * completed_chapters / total_chapters)
emit('progress', {
'progress': chapter_progress,
'message': f'章节 {completed_chapters}/{total_chapters} 已完成'
})
completion_status = {
'chapterId': section.chapter_id,
'title': section.title,
... ...
... ... @@ -349,8 +349,11 @@ def run_report_generation(task: ReportTask, query: str, custom_template: str = "
def stream_handler(event_type: str, payload: Dict[str, Any]):
"""所有阶段事件都通过同一个接口分发,保证日志一致。"""
task.publish_event(event_type, payload)
# 如果事件包含进度信息,同步更新任务进度
if event_type == 'progress' and 'progress' in payload:
task.update_status("running", payload['progress'])
task.update_status("running", 10)
task.update_status("running", 5)
task.publish_event('stage', {'message': '任务已启动,正在检查输入文件', 'stage': 'prepare'})
# 检查输入文件
... ... @@ -365,14 +368,10 @@ def run_report_generation(task: ReportTask, query: str, custom_template: str = "
'files': check_result.get('latest_files', {})
})
task.update_status("running", 30)
# 加载输入文件
content = report_agent.load_input_files(check_result['latest_files'])
task.publish_event('stage', {'message': '源数据加载完成,启动生成流程', 'stage': 'data_loaded'})
task.update_status("running", 50)
# 生成报告(附带兜底重试,缓解瞬时网络抖动)
for attempt in range(1, 3):
try:
... ... @@ -432,7 +431,6 @@ def run_report_generation(task: ReportTask, query: str, custom_template: str = "
else:
html_report = generation_result
task.update_status("running", 90)
task.publish_event('stage', {'message': '报告生成完毕,准备持久化', 'stage': 'persist'})
# 保存结果
... ...