马一丁

Optimize the Progress Bar Display in Report Engine

@@ -386,6 +386,7 @@ class ReportAgent: @@ -386,6 +386,7 @@ class ReportAgent:
386 'template': template_result.get('template_name'), 386 'template': template_result.get('template_name'),
387 'reason': template_result.get('selection_reason') 387 'reason': template_result.get('selection_reason')
388 }) 388 })
  389 + emit('progress', {'progress': 10, 'message': '模板选择完成'})
389 sections = self._slice_template(template_result.get('template_content', '')) 390 sections = self._slice_template(template_result.get('template_content', ''))
390 if not sections: 391 if not sections:
391 raise ValueError("模板无法解析出章节,请检查模板内容。") 392 raise ValueError("模板无法解析出章节,请检查模板内容。")
@@ -407,6 +408,7 @@ class ReportAgent: @@ -407,6 +408,7 @@ class ReportAgent:
407 'title': layout_design.get('title'), 408 'title': layout_design.get('title'),
408 'toc': layout_design.get('tocTitle') 409 'toc': layout_design.get('tocTitle')
409 }) 410 })
  411 + emit('progress', {'progress': 15, 'message': '文档标题/目录设计完成'})
410 # 使用刚生成的设计稿对全书进行篇幅规划,约束各章字数与重点 412 # 使用刚生成的设计稿对全书进行篇幅规划,约束各章字数与重点
411 word_plan = self.word_budget_node.run( 413 word_plan = self.word_budget_node.run(
412 sections, 414 sections,
@@ -420,6 +422,7 @@ class ReportAgent: @@ -420,6 +422,7 @@ class ReportAgent:
420 'stage': 'word_plan_ready', 422 'stage': 'word_plan_ready',
421 'chapter_targets': len(word_plan.get('chapters', [])) 423 'chapter_targets': len(word_plan.get('chapters', []))
422 }) 424 })
  425 + emit('progress', {'progress': 20, 'message': '章节字数规划已生成'})
423 # 记录每个章节的目标字数/强调点,后续传给章节LLM 426 # 记录每个章节的目标字数/强调点,后续传给章节LLM
424 chapter_targets = { 427 chapter_targets = {
425 entry.get("chapterId"): entry 428 entry.get("chapterId"): entry
@@ -472,6 +475,9 @@ class ReportAgent: @@ -472,6 +475,9 @@ class ReportAgent:
472 chapter_max_attempts = max( 475 chapter_max_attempts = max(
473 self._CONTENT_SPARSE_MIN_ATTEMPTS, self.config.CHAPTER_JSON_MAX_ATTEMPTS 476 self._CONTENT_SPARSE_MIN_ATTEMPTS, self.config.CHAPTER_JSON_MAX_ATTEMPTS
474 ) 477 )
  478 + total_chapters = len(sections) # 总章节数
  479 + completed_chapters = 0 # 已完成章节数
  480 +
475 for section in sections: 481 for section in sections:
476 logger.info(f"生成章节: {section.title}") 482 logger.info(f"生成章节: {section.title}")
477 emit('chapter_status', { 483 emit('chapter_status', {
@@ -587,6 +593,13 @@ class ReportAgent: @@ -587,6 +593,13 @@ class ReportAgent:
587 f"{section.title} 章节JSON在 {chapter_max_attempts} 次尝试后仍无法解析" 593 f"{section.title} 章节JSON在 {chapter_max_attempts} 次尝试后仍无法解析"
588 ) 594 )
589 chapters.append(chapter_payload) 595 chapters.append(chapter_payload)
  596 + completed_chapters += 1 # 更新已完成章节数
  597 + # 计算当前进度:20% + 80% * (已完成章节数 / 总章节数),四舍五入
  598 + chapter_progress = 20 + round(80 * completed_chapters / total_chapters)
  599 + emit('progress', {
  600 + 'progress': chapter_progress,
  601 + 'message': f'章节 {completed_chapters}/{total_chapters} 已完成'
  602 + })
590 completion_status = { 603 completion_status = {
591 'chapterId': section.chapter_id, 604 'chapterId': section.chapter_id,
592 'title': section.title, 605 'title': section.title,
@@ -349,8 +349,11 @@ def run_report_generation(task: ReportTask, query: str, custom_template: str = " @@ -349,8 +349,11 @@ def run_report_generation(task: ReportTask, query: str, custom_template: str = "
349 def stream_handler(event_type: str, payload: Dict[str, Any]): 349 def stream_handler(event_type: str, payload: Dict[str, Any]):
350 """所有阶段事件都通过同一个接口分发,保证日志一致。""" 350 """所有阶段事件都通过同一个接口分发,保证日志一致。"""
351 task.publish_event(event_type, payload) 351 task.publish_event(event_type, payload)
  352 + # 如果事件包含进度信息,同步更新任务进度
  353 + if event_type == 'progress' and 'progress' in payload:
  354 + task.update_status("running", payload['progress'])
352 355
353 - task.update_status("running", 10) 356 + task.update_status("running", 5)
354 task.publish_event('stage', {'message': '任务已启动,正在检查输入文件', 'stage': 'prepare'}) 357 task.publish_event('stage', {'message': '任务已启动,正在检查输入文件', 'stage': 'prepare'})
355 358
356 # 检查输入文件 359 # 检查输入文件
@@ -365,14 +368,10 @@ def run_report_generation(task: ReportTask, query: str, custom_template: str = " @@ -365,14 +368,10 @@ def run_report_generation(task: ReportTask, query: str, custom_template: str = "
365 'files': check_result.get('latest_files', {}) 368 'files': check_result.get('latest_files', {})
366 }) 369 })
367 370
368 - task.update_status("running", 30)  
369 -  
370 # 加载输入文件 371 # 加载输入文件
371 content = report_agent.load_input_files(check_result['latest_files']) 372 content = report_agent.load_input_files(check_result['latest_files'])
372 task.publish_event('stage', {'message': '源数据加载完成,启动生成流程', 'stage': 'data_loaded'}) 373 task.publish_event('stage', {'message': '源数据加载完成,启动生成流程', 'stage': 'data_loaded'})
373 374
374 - task.update_status("running", 50)  
375 -  
376 # 生成报告(附带兜底重试,缓解瞬时网络抖动) 375 # 生成报告(附带兜底重试,缓解瞬时网络抖动)
377 for attempt in range(1, 3): 376 for attempt in range(1, 3):
378 try: 377 try:
@@ -432,7 +431,6 @@ def run_report_generation(task: ReportTask, query: str, custom_template: str = " @@ -432,7 +431,6 @@ def run_report_generation(task: ReportTask, query: str, custom_template: str = "
432 else: 431 else:
433 html_report = generation_result 432 html_report = generation_result
434 433
435 - task.update_status("running", 90)  
436 task.publish_event('stage', {'message': '报告生成完毕,准备持久化', 'stage': 'persist'}) 434 task.publish_event('stage', {'message': '报告生成完毕,准备持久化', 'stage': 'persist'})
437 435
438 # 保存结果 436 # 保存结果