feat: 后台任务系统 + JSON容错解析 + SSE心跳保活 + 多项Bug修复
新功能: - 大纲/章节生成改为服务端后台任务,支持断线续传 - 后台任务队列排队执行,按用户排队(同用户串行不同用户并发) - 章节管理页面添加后台任务列表弹窗和进度面板 - 章节状态添加 pending(待处理)选项 - 集成json5容错解析器 + 上下文感知JSON修复 - SSE流式生成添加心跳保活,防止连接超时 - SSEPostClient添加credentials:include修复network error - 每章最大伏笔数从2调整为5 - 添加大纲读区伏笔的功能 Bug修复: - 修复AI生成JSON中未转义引号/中文标点/多对象属性值未合并 - 修复JSON非法转义字符清洗和中文引号处理 - 修复MCP插件TimeoutError/连接失败上下文清理 - MCP插件后台注册添加重试机制 - 续写模式添加缺失的mcp_references参数 - 修复Alembic迁移链分叉 - 使用torch CPU版本加速Docker构建
This commit is contained in:
@@ -27,6 +27,7 @@ from app.models.analysis_task import AnalysisTask
|
||||
from app.models.memory import PlotAnalysis, StoryMemory
|
||||
from app.models.batch_generation_task import BatchGenerationTask
|
||||
from app.models.regeneration_task import RegenerationTask
|
||||
from app.models.background_task import BackgroundTask
|
||||
from app.schemas.chapter import (
|
||||
ChapterCreate,
|
||||
ChapterUpdate,
|
||||
@@ -1815,6 +1816,450 @@ async def generate_chapter_content_stream(
|
||||
return create_sse_response(event_generator())
|
||||
|
||||
|
||||
@router.post("/{chapter_id}/generate-background", summary="AI创作章节内容(后台任务)")
|
||||
async def generate_chapter_content_background(
|
||||
chapter_id: str,
|
||||
request: Request,
|
||||
generate_request: ChapterGenerateRequest = ChapterGenerateRequest(),
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
创建后台任务来生成章节内容。
|
||||
任务创建后立即返回task_id,前端通过 GET /api/tasks/{task_id} 轮询进度。
|
||||
关闭浏览器不影响生成,生成完成后内容自动保存到数据库。
|
||||
"""
|
||||
user_id = getattr(request.state, 'user_id', None)
|
||||
if not user_id:
|
||||
raise HTTPException(status_code=401, detail="未登录")
|
||||
|
||||
# 验证章节存在
|
||||
result = await db.execute(
|
||||
select(Chapter).where(Chapter.id == chapter_id)
|
||||
)
|
||||
chapter = result.scalar_one_or_none()
|
||||
if not chapter:
|
||||
raise HTTPException(status_code=404, detail="章节不存在")
|
||||
|
||||
# 验证项目权限
|
||||
project = await verify_project_access(chapter.project_id, user_id, db)
|
||||
|
||||
# 检查前置条件
|
||||
can_generate, error_msg, _ = await check_prerequisites(db, chapter)
|
||||
if not can_generate:
|
||||
raise HTTPException(status_code=400, detail=error_msg)
|
||||
|
||||
# 创建后台任务
|
||||
from app.services.background_task_service import background_task_service, TaskProgressTracker
|
||||
task = await background_task_service.create_task(
|
||||
user_id=user_id,
|
||||
project_id=chapter.project_id,
|
||||
task_type="chapter_generate",
|
||||
task_input={
|
||||
"chapter_id": chapter_id,
|
||||
"style_id": generate_request.style_id,
|
||||
"target_word_count": generate_request.target_word_count or 3000,
|
||||
"enable_mcp": generate_request.enable_mcp,
|
||||
"model": generate_request.model,
|
||||
"narrative_perspective": generate_request.narrative_perspective,
|
||||
},
|
||||
db=db
|
||||
)
|
||||
|
||||
# 后台执行的函数
|
||||
async def _run_chapter_generation(task_id: str, bg_user_id: str):
|
||||
from app.database import get_engine
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession as BgAsyncSession
|
||||
|
||||
engine = await get_engine(bg_user_id)
|
||||
AsyncSessionLocal = async_sessionmaker(engine, class_=BgAsyncSession, expire_on_commit=False)
|
||||
|
||||
async with AsyncSessionLocal() as bg_db:
|
||||
tracker = TaskProgressTracker(task_id, bg_user_id, "章节")
|
||||
try:
|
||||
await tracker.start()
|
||||
|
||||
# 获取AI服务
|
||||
from app.api.settings import get_user_ai_service_from_db
|
||||
bg_ai_service = await get_user_ai_service_from_db(bg_user_id, bg_db)
|
||||
|
||||
await _run_chapter_generation_bg(
|
||||
task_input={
|
||||
"chapter_id": chapter_id,
|
||||
"style_id": generate_request.style_id,
|
||||
"target_word_count": generate_request.target_word_count or 3000,
|
||||
"enable_mcp": generate_request.enable_mcp,
|
||||
"model": generate_request.model,
|
||||
"narrative_perspective": generate_request.narrative_perspective,
|
||||
},
|
||||
db=bg_db,
|
||||
ai_service=bg_ai_service,
|
||||
tracker=tracker,
|
||||
user_id=bg_user_id,
|
||||
task_id=task_id,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"❌ 后台章节生成失败: {e}", exc_info=True)
|
||||
await tracker.error(str(e))
|
||||
|
||||
await background_task_service.spawn_background_task(
|
||||
task.id, user_id, _run_chapter_generation
|
||||
)
|
||||
|
||||
return {
|
||||
"task_id": task.id,
|
||||
"task_type": "chapter_generate",
|
||||
"status": "pending",
|
||||
"message": "任务已创建,请通过 GET /api/tasks/{task_id} 查询进度"
|
||||
}
|
||||
|
||||
|
||||
async def _run_chapter_generation_bg(
|
||||
task_input: dict,
|
||||
db: AsyncSession,
|
||||
ai_service: AIService,
|
||||
tracker,
|
||||
user_id: str,
|
||||
task_id: str,
|
||||
):
|
||||
"""后台执行章节生成(不使用SSE,直接生成并保存)"""
|
||||
from app.services.chapter_context_service import (
|
||||
OneToManyContextBuilder,
|
||||
OneToOneContextBuilder
|
||||
)
|
||||
|
||||
chapter_id = task_input["chapter_id"]
|
||||
style_id = task_input.get("style_id")
|
||||
target_word_count = task_input.get("target_word_count", 3000)
|
||||
custom_model = task_input.get("model")
|
||||
temp_narrative_perspective = task_input.get("narrative_perspective")
|
||||
write_lock = await get_db_write_lock(user_id)
|
||||
|
||||
# === 加载阶段 ===
|
||||
await tracker.loading("加载章节信息...", 0.2)
|
||||
|
||||
chapter_result = await db.execute(
|
||||
select(Chapter).where(Chapter.id == chapter_id)
|
||||
)
|
||||
current_chapter = chapter_result.scalar_one_or_none()
|
||||
if not current_chapter:
|
||||
await tracker.error("章节不存在")
|
||||
return
|
||||
|
||||
await tracker.loading("加载项目信息...", 0.4)
|
||||
|
||||
project_result = await db.execute(
|
||||
select(Project).where(Project.id == current_chapter.project_id)
|
||||
)
|
||||
project = project_result.scalar_one_or_none()
|
||||
if not project:
|
||||
await tracker.error("项目不存在")
|
||||
return
|
||||
|
||||
outline_mode = project.outline_mode if project else 'one-to-many'
|
||||
|
||||
# 获取大纲
|
||||
if current_chapter.outline_id:
|
||||
outline_result = await db.execute(
|
||||
select(Outline).where(Outline.id == current_chapter.outline_id)
|
||||
)
|
||||
else:
|
||||
outline_result = await db.execute(
|
||||
select(Outline)
|
||||
.where(Outline.project_id == current_chapter.project_id)
|
||||
.where(Outline.order_index == current_chapter.chapter_number)
|
||||
)
|
||||
outline = outline_result.scalar_one_or_none()
|
||||
|
||||
# 获取写作风格
|
||||
style_content = ""
|
||||
if style_id:
|
||||
style_result = await db.execute(
|
||||
select(WritingStyle).where(WritingStyle.id == style_id)
|
||||
)
|
||||
style = style_result.scalar_one_or_none()
|
||||
if style and (style.user_id is None or style.user_id == user_id):
|
||||
style_content = style.prompt_content or ""
|
||||
|
||||
# === 构建上下文 ===
|
||||
if outline_mode == 'one-to-one':
|
||||
context_builder = OneToOneContextBuilder(
|
||||
memory_service=memory_service,
|
||||
foreshadow_service=foreshadow_service
|
||||
)
|
||||
chapter_context = await context_builder.build(
|
||||
chapter=current_chapter,
|
||||
project=project,
|
||||
outline=outline,
|
||||
user_id=user_id,
|
||||
db=db,
|
||||
target_word_count=target_word_count
|
||||
)
|
||||
else:
|
||||
context_builder = OneToManyContextBuilder(
|
||||
memory_service=memory_service,
|
||||
foreshadow_service=foreshadow_service
|
||||
)
|
||||
chapter_context = await context_builder.build(
|
||||
chapter=current_chapter,
|
||||
project=project,
|
||||
outline=outline,
|
||||
user_id=user_id,
|
||||
db=db,
|
||||
style_content=style_content,
|
||||
target_word_count=target_word_count,
|
||||
temp_narrative_perspective=temp_narrative_perspective
|
||||
)
|
||||
|
||||
await tracker.loading("上下文构建完成", 0.8)
|
||||
|
||||
# 确定叙事人称
|
||||
chapter_perspective = (
|
||||
temp_narrative_perspective or
|
||||
project.narrative_perspective or
|
||||
'第三人称'
|
||||
)
|
||||
|
||||
# === 准备提示词 ===
|
||||
if outline_mode == 'one-to-one':
|
||||
if chapter_context.continuation_point:
|
||||
template = await PromptService.get_template("CHAPTER_GENERATION_ONE_TO_ONE_NEXT", user_id, db)
|
||||
base_prompt = PromptService.format_prompt(
|
||||
template,
|
||||
project_title=project.title,
|
||||
chapter_number=current_chapter.chapter_number,
|
||||
chapter_title=current_chapter.title,
|
||||
chapter_outline=chapter_context.chapter_outline,
|
||||
target_word_count=target_word_count,
|
||||
genre=project.genre or '未设定',
|
||||
narrative_perspective=chapter_perspective,
|
||||
previous_chapter_content=chapter_context.continuation_point,
|
||||
previous_chapter_summary=chapter_context.previous_chapter_summary or '(无上一章摘要)',
|
||||
characters_info=chapter_context.chapter_characters or '暂无角色信息',
|
||||
chapter_careers=chapter_context.chapter_careers or '暂无职业信息',
|
||||
foreshadow_reminders=chapter_context.foreshadow_reminders or '暂无需要关注的伏笔',
|
||||
relevant_memories=chapter_context.relevant_memories or '暂无相关记忆'
|
||||
)
|
||||
else:
|
||||
template = await PromptService.get_template("CHAPTER_GENERATION_ONE_TO_ONE", user_id, db)
|
||||
base_prompt = PromptService.format_prompt(
|
||||
template,
|
||||
project_title=project.title,
|
||||
chapter_number=current_chapter.chapter_number,
|
||||
chapter_title=current_chapter.title,
|
||||
chapter_outline=chapter_context.chapter_outline,
|
||||
target_word_count=target_word_count,
|
||||
genre=project.genre or '未设定',
|
||||
narrative_perspective=chapter_perspective,
|
||||
characters_info=chapter_context.chapter_characters or '暂无角色信息',
|
||||
chapter_careers=chapter_context.chapter_careers or '暂无职业信息',
|
||||
foreshadow_reminders=chapter_context.foreshadow_reminders or '暂无需要关注的伏笔',
|
||||
relevant_memories=chapter_context.relevant_memories or '暂无相关记忆'
|
||||
)
|
||||
else:
|
||||
if chapter_context.continuation_point:
|
||||
previous_summary = chapter_context.previous_chapter_summary or "(无上一章摘要,请根据锚点续写)"
|
||||
template = await PromptService.get_template("CHAPTER_GENERATION_ONE_TO_MANY_NEXT", user_id, db)
|
||||
base_prompt = PromptService.format_prompt(
|
||||
template,
|
||||
project_title=project.title,
|
||||
chapter_number=current_chapter.chapter_number,
|
||||
chapter_title=current_chapter.title,
|
||||
chapter_outline=chapter_context.chapter_outline,
|
||||
target_word_count=target_word_count,
|
||||
continuation_point=chapter_context.continuation_point,
|
||||
genre=project.genre or '未设定',
|
||||
narrative_perspective=chapter_perspective,
|
||||
characters_info=chapter_context.chapter_characters or '暂无角色信息',
|
||||
chapter_careers=chapter_context.chapter_careers or '暂无职业信息',
|
||||
foreshadow_reminders=chapter_context.foreshadow_reminders or '暂无需要关注的伏笔',
|
||||
previous_chapter_summary=previous_summary,
|
||||
recent_chapters_context=chapter_context.recent_chapters_context or '',
|
||||
relevant_memories=chapter_context.relevant_memories or ''
|
||||
)
|
||||
else:
|
||||
template = await PromptService.get_template("CHAPTER_GENERATION_ONE_TO_MANY", user_id, db)
|
||||
base_prompt = PromptService.format_prompt(
|
||||
template,
|
||||
project_title=project.title,
|
||||
chapter_number=current_chapter.chapter_number,
|
||||
chapter_title=current_chapter.title,
|
||||
chapter_outline=chapter_context.chapter_outline,
|
||||
target_word_count=target_word_count,
|
||||
genre=project.genre or '未设定',
|
||||
narrative_perspective=chapter_perspective,
|
||||
characters_info=chapter_context.chapter_characters or '暂无角色信息',
|
||||
chapter_careers=chapter_context.chapter_careers or '暂无职业信息',
|
||||
foreshadow_reminders=chapter_context.foreshadow_reminders or '暂无需要关注的伏笔',
|
||||
relevant_memories=chapter_context.relevant_memories or '暂无相关记忆'
|
||||
)
|
||||
|
||||
# 应用写作风格
|
||||
if style_content:
|
||||
prompt = WritingStyleManager.apply_style_to_prompt(base_prompt, style_content)
|
||||
else:
|
||||
prompt = base_prompt
|
||||
|
||||
# === 准备阶段 ===
|
||||
await tracker.preparing("准备AI提示词...")
|
||||
|
||||
system_prompt_with_style = None
|
||||
if style_content:
|
||||
system_prompt_with_style = f"""【🎨 写作风格要求 - 最高优先级】
|
||||
|
||||
{style_content}
|
||||
|
||||
⚠️ 请严格遵循上述写作风格要求进行创作,这是最重要的指令!
|
||||
确保在整个章节创作过程中始终保持风格的一致性。"""
|
||||
|
||||
calculated_max_tokens = int(target_word_count * 3)
|
||||
calculated_max_tokens = max(2000, min(calculated_max_tokens, 16000))
|
||||
|
||||
generate_kwargs = {
|
||||
"prompt": prompt,
|
||||
"system_prompt": system_prompt_with_style,
|
||||
"tool_choice": "required",
|
||||
"max_tokens": calculated_max_tokens
|
||||
}
|
||||
if custom_model:
|
||||
generate_kwargs["model"] = custom_model
|
||||
|
||||
# === 生成阶段 ===
|
||||
full_content = ""
|
||||
chunk_count = 0
|
||||
|
||||
await tracker.generating(
|
||||
current_chars=0,
|
||||
estimated_total=target_word_count
|
||||
)
|
||||
|
||||
async for chunk in ai_service.generate_text_stream(**generate_kwargs):
|
||||
# 检查是否被取消
|
||||
if chunk_count % 10 == 0 and await tracker.check_cancelled():
|
||||
logger.info(f"🚫 后台章节生成被取消: {chapter_id}")
|
||||
return
|
||||
|
||||
full_content += chunk
|
||||
chunk_count += 1
|
||||
|
||||
# 每10个chunk更新一次进度
|
||||
if chunk_count % 10 == 0:
|
||||
await tracker.generating(
|
||||
current_chars=len(full_content),
|
||||
estimated_total=target_word_count,
|
||||
message=f'正在创作中... 已生成 {len(full_content)} 字'
|
||||
)
|
||||
|
||||
await asyncio.sleep(0)
|
||||
|
||||
# === 保存阶段 ===
|
||||
await tracker.saving("正在保存章节...", 0.3)
|
||||
|
||||
async with write_lock:
|
||||
# 重新获取章节(确保最新状态)
|
||||
chapter_result = await db.execute(
|
||||
select(Chapter).where(Chapter.id == chapter_id)
|
||||
)
|
||||
current_chapter = chapter_result.scalar_one_or_none()
|
||||
if not current_chapter:
|
||||
await tracker.error("保存时章节不存在")
|
||||
return
|
||||
|
||||
old_word_count = current_chapter.word_count or 0
|
||||
current_chapter.content = full_content
|
||||
new_word_count = len(full_content)
|
||||
current_chapter.word_count = new_word_count
|
||||
current_chapter.status = "completed"
|
||||
|
||||
# 更新项目字数
|
||||
project_result = await db.execute(
|
||||
select(Project).where(Project.id == current_chapter.project_id)
|
||||
)
|
||||
project_obj = project_result.scalar_one_or_none()
|
||||
if project_obj:
|
||||
project_obj.current_words = (project_obj.current_words or 0) - old_word_count + new_word_count
|
||||
|
||||
# 记录生成历史
|
||||
history = GenerationHistory(
|
||||
project_id=current_chapter.project_id,
|
||||
chapter_id=current_chapter.id,
|
||||
prompt=f"创作章节: 第{current_chapter.chapter_number}章 {current_chapter.title}",
|
||||
generated_content=full_content[:500] if len(full_content) > 500 else full_content,
|
||||
model="default"
|
||||
)
|
||||
db.add(history)
|
||||
|
||||
await db.commit()
|
||||
|
||||
logger.info(f"✅ 后台创作章节 {chapter_id} 完成,共 {new_word_count} 字")
|
||||
|
||||
# 🔮 自动标记伏笔
|
||||
try:
|
||||
plant_result = await foreshadow_service.auto_plant_pending_foreshadows(
|
||||
db=db,
|
||||
project_id=current_chapter.project_id,
|
||||
chapter_id=chapter_id,
|
||||
chapter_number=current_chapter.chapter_number,
|
||||
chapter_content=full_content
|
||||
)
|
||||
if plant_result.get('planted_count', 0) > 0:
|
||||
logger.info(f"🔮 自动标记伏笔已埋入: {plant_result['planted_count']}个")
|
||||
except Exception as plant_error:
|
||||
logger.warning(f"⚠️ 自动标记伏笔埋入失败: {str(plant_error)}")
|
||||
|
||||
# 创建分析任务
|
||||
analysis_task = AnalysisTask(
|
||||
chapter_id=chapter_id,
|
||||
user_id=user_id,
|
||||
project_id=current_chapter.project_id,
|
||||
status='pending',
|
||||
progress=0
|
||||
)
|
||||
db.add(analysis_task)
|
||||
await db.commit()
|
||||
await db.refresh(analysis_task)
|
||||
|
||||
logger.info(f"📋 后台生成:已创建分析任务: {analysis_task.id}")
|
||||
|
||||
await asyncio.sleep(0.05)
|
||||
|
||||
# 启动后台分析
|
||||
asyncio.create_task(
|
||||
analyze_chapter_background(
|
||||
chapter_id=chapter_id,
|
||||
user_id=user_id,
|
||||
project_id=current_chapter.project_id,
|
||||
task_id=analysis_task.id,
|
||||
ai_service=ai_service
|
||||
)
|
||||
)
|
||||
|
||||
# === 完成 ===
|
||||
await tracker.complete(f"创作完成!共 {new_word_count} 字")
|
||||
|
||||
# 更新任务结果
|
||||
from app.services.background_task_service import background_task_service
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession as BgAsyncSession
|
||||
from app.database import get_engine as bg_get_engine
|
||||
try:
|
||||
engine = await bg_get_engine(user_id)
|
||||
AsyncSessionLocal = async_sessionmaker(engine, class_=BgAsyncSession, expire_on_commit=False)
|
||||
async with AsyncSessionLocal() as result_db:
|
||||
from sqlalchemy import update as sql_update
|
||||
await result_db.execute(
|
||||
sql_update(BackgroundTask)
|
||||
.where(BackgroundTask.id == task_id)
|
||||
.values(task_result={
|
||||
"chapter_id": chapter_id,
|
||||
"word_count": new_word_count,
|
||||
"analysis_task_id": analysis_task.id
|
||||
})
|
||||
)
|
||||
await result_db.commit()
|
||||
except Exception as e:
|
||||
logger.warning(f"⚠️ 更新任务结果失败: {e}")
|
||||
|
||||
|
||||
def _build_analysis_task_status_payload(
|
||||
chapter_id: str,
|
||||
task: Optional[AnalysisTask],
|
||||
|
||||
Reference in New Issue
Block a user