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:
未来
2026-04-29 08:31:07 +08:00
parent 1f80a58994
commit 2bd8b61e91
20 changed files with 2873 additions and 151 deletions
+17 -2
View File
@@ -29,7 +29,21 @@ async def lifespan(app: FastAPI):
"""应用生命周期管理"""
# 注册MCP状态同步服务
register_status_sync()
# 安全保障:确保后台任务表存在(兼容未执行Alembic迁移的旧部署)
try:
from app.database import get_engine
from app.models.background_task import BackgroundTask
_startup_engine = await get_engine("system")
async with _startup_engine.begin() as conn:
# 仅创建 background_tasks 表(如果不存在),不影响其他表
await conn.run_sync(
lambda sync_conn: BackgroundTask.__table__.create(sync_conn, checkfirst=True)
)
logger.info("后台任务表检查完成")
except Exception as e:
logger.warning(f"后台任务表检查失败(不影响启动): {e}")
logger.info("应用启动完成")
yield
@@ -133,7 +147,7 @@ from app.api import (
auth, users, settings, writing_styles, memories,
mcp_plugins, admin, inspiration, prompt_templates,
changelog, careers, foreshadows, prompt_workshop, book_import,
project_covers
project_covers, tasks
)
app.include_router(auth.router, prefix="/api")
@@ -159,6 +173,7 @@ app.include_router(prompt_templates.router, prefix="/api") # 提示词模板管
app.include_router(changelog.router, prefix="/api") # 更新日志API
app.include_router(prompt_workshop.router, prefix="/api") # 提示词工坊API
app.include_router(book_import.router, prefix="/api") # 拆书导入API
app.include_router(tasks.router, prefix="/api") # 后台任务API
static_dir = Path(__file__).parent.parent / "static"
generated_assets_root_dir = Path(__file__).parent.parent / "storage"