From cba9f5417d5af7f4a11a77f83c9ed279e1246825 Mon Sep 17 00:00:00 2001 From: xiamuceer Date: Sun, 14 Dec 2025 15:28:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:1.=E4=BF=AE=E5=A4=8D=E7=AB=A0=E8=8A=82?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E7=94=9F=E6=88=90=E8=BF=9B=E5=BA=A6=E5=BC=B9?= =?UTF-8?q?=E7=AA=97=E4=B8=AD=EF=BC=8C=E8=BF=9B=E5=BA=A6=E5=A7=8B=E7=BB=88?= =?UTF-8?q?=E6=98=BE=E7=A4=BA0%=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/chapters.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/backend/app/api/chapters.py b/backend/app/api/chapters.py index 8dc109c..bd9d72e 100644 --- a/backend/app/api/chapters.py +++ b/backend/app/api/chapters.py @@ -1194,6 +1194,9 @@ async def generate_chapter_content_stream( # 发送开始事件 yield f"data: {json.dumps({'type': 'start', 'message': '开始AI创作...'}, ensure_ascii=False)}\n\n" + # 发送初始进度0% + yield f"data: {json.dumps({'type': 'progress', 'progress': 0, 'message': '准备生成...', 'status': 'processing'}, ensure_ascii=False)}\n\n" + # 🔧 MCP工具增强:收集章节参考资料(优化版) mcp_reference_materials = "" if enable_mcp and current_user_id: @@ -1253,10 +1256,15 @@ async def generate_chapter_content_stream( yield f"data: {json.dumps({'type': 'progress', 'message': 'ℹ️ MCP未使用工具,继续', 'progress': 32}, ensure_ascii=False)}\n\n" else: logger.debug(f"用户 {current_user_id} 未启用MCP工具,跳过MCP增强") + # 未启用MCP时也发送进度,保持连贯性 + yield f"data: {json.dumps({'type': 'progress', 'message': '准备生成内容...', 'progress': 10}, ensure_ascii=False)}\n\n" except Exception as e: logger.warning(f"⚠️ MCP工具调用失败,降级为基础模式: {str(e)}") - yield f"data: {json.dumps({'type': 'progress', 'message': '⚠️ MCP工具暂时不可用,使用基础模式', 'progress': 32}, ensure_ascii=False)}\n\n" + yield f"data: {json.dumps({'type': 'progress', 'message': '⚠️ MCP工具暂时不可用,使用基础模式', 'progress': 10}, ensure_ascii=False)}\n\n" + else: + # 如果未启用MCP,也发送基础进度 + yield f"data: {json.dumps({'type': 'progress', 'message': '开始构建创作上下文...', 'progress': 10}, ensure_ascii=False)}\n\n" # 🎭 确定使用的叙事人称(临时指定 > 项目默认 > 系统默认) chapter_perspective = ( @@ -1382,6 +1390,9 @@ async def generate_chapter_content_stream( logger.info(f"开始AI流式创作章节 {chapter_id}") + # 发送开始生成的进度 + yield f"data: {json.dumps({'type': 'progress', 'progress': 35, 'message': '开始AI创作...', 'status': 'processing'}, ensure_ascii=False)}\n\n" + # 准备生成参数 generate_kwargs = {"prompt": prompt} if custom_model: @@ -1402,11 +1413,11 @@ async def generate_chapter_content_stream( # 发送内容块 yield f"data: {json.dumps({'type': 'content', 'content': chunk}, ensure_ascii=False)}\n\n" - # 每50个chunk发送一次进度更新(估算) - if chunk_count % 50 == 0: + # 每20个chunk发送一次进度更新(提高频率) + if chunk_count % 20 == 0: current_word_count = len(full_content) - # 根据目标字数估算进度(35%起步,最高95%,为后续保存留5%) - estimated_progress = min(95, 35 + int((current_word_count / target_word_count) * 60)) + # 根据目标字数估算进度(40%起步,最高95%,为后续保存留5%) + estimated_progress = min(95, 40 + int((current_word_count / target_word_count) * 55)) # 只在进度变化时发送 if estimated_progress > last_progress: