fix: 修复多个问题

- JSON解析器字符串状态追踪修复
- AI客户端流式响应异常处理
- 写作风格MultipleResultsFound错误
- 职业stages字段类型处理
- 章节分析任务状态同步
- 后台任务返回值修复
This commit is contained in:
xiamuceer
2025-12-31 12:02:36 +08:00
parent 30c044394f
commit fba6922a5c
10 changed files with 155 additions and 82 deletions
+6 -1
View File
@@ -483,7 +483,12 @@ async def update_career(
for field, value in update_data.items():
if field == "stages" and value is not None:
# 转换为JSON字符串
setattr(career, field, json.dumps([stage.model_dump() for stage in value], ensure_ascii=False))
# model_dump() 已经将嵌套模型转换为字典,所以 value 中的元素已经是 dict
stages_list = [
stage if isinstance(stage, dict) else stage.model_dump()
for stage in value
]
setattr(career, field, json.dumps(stages_list, ensure_ascii=False))
elif field == "attribute_bonuses" and value is not None:
# 转换为JSON字符串
setattr(career, field, json.dumps(value, ensure_ascii=False))
+3 -3
View File
@@ -865,7 +865,7 @@ async def analyze_chapter_background(
if not task:
logger.error(f"❌ 任务不存在: {task_id}")
return
return False
# 更新任务状态(写操作,需要锁)
async with write_lock:
@@ -886,7 +886,7 @@ async def analyze_chapter_background(
task.completed_at = datetime.now()
await db_session.commit()
logger.error(f"❌ 章节不存在或内容为空: {chapter_id}")
return
return False
async with write_lock:
task.progress = 20
@@ -908,7 +908,7 @@ async def analyze_chapter_background(
task.completed_at = datetime.now()
await db_session.commit()
logger.error(f"❌ AI分析失败: {chapter_id}")
return
return False
async with write_lock:
task.progress = 60
+1 -1
View File
@@ -845,7 +845,7 @@ async def _continue_outline(
try:
from app.services.auto_character_service import get_auto_character_service
logger.info(f"🔮 【预测模式】在生成大纲前预测是否需要新角色(需要用户确认)")
logger.info(f"🔮 【预测模式】在生成大纲前预测是否需要新角色")
# 构建已有章节概览
all_chapters_brief_for_analysis = ""
+2 -2
View File
@@ -1310,7 +1310,7 @@ async def outline_generator(
])
# 第一阶段:生成3个粗粒度大纲节点
yield await SSEResponse.send_progress(f"生成{outline_count}个大纲节点...", 20)
yield await SSEResponse.send_progress(f"生成{outline_count}个大纲节点...", 10)
outline_requirements = f"{requirements}\n\n【重要说明】这是小说的开局部分,请生成{outline_count}个大纲节点,重点关注:\n"
outline_requirements += "1. 引入主要角色和世界观设定\n"
@@ -1355,7 +1355,7 @@ async def outline_generator(
# 定期更新进度和字数(5-95%,AI生成占90%)
if chunk_count % 5 == 0:
progress = min(5 + (chunk_count // 3), 95)
progress = min(10 + (chunk_count // 3), 90)
yield await SSEResponse.send_progress(
f"生成大纲中... ({len(accumulated_text)}字符)",
progress
+6 -6
View File
@@ -285,11 +285,11 @@ async def get_writing_style(
if not style:
raise HTTPException(status_code=404, detail="写作风格不存在")
# 检查是否有项目将其设置为默认风格
# 检查是否有项目将其设置为默认风格(一个风格可能被多个项目使用,使用 first() 避免 MultipleResultsFound
result = await db.execute(
select(ProjectDefaultStyle).where(ProjectDefaultStyle.style_id == style_id)
)
is_default = result.scalar_one_or_none() is not None
is_default = result.scalars().first() is not None
# 返回包含 is_default 字段的字典
return {
@@ -351,11 +351,11 @@ async def update_writing_style(
await db.commit()
await db.refresh(style)
# 检查是否有项目将其设置为默认风格
# 检查是否有项目将其设置为默认风格(一个风格可能被多个项目使用,使用 first() 避免 MultipleResultsFound
result = await db.execute(
select(ProjectDefaultStyle).where(ProjectDefaultStyle.style_id == style_id)
)
is_default = result.scalar_one_or_none() is not None
is_default = result.scalars().first() is not None
# 返回包含 is_default 字段的字典
return {
@@ -405,11 +405,11 @@ async def delete_writing_style(
if style.user_id != user_id:
raise HTTPException(status_code=403, detail="无权删除其他用户的风格")
# 检查是否有项目将其设置为默认风格
# 检查是否有项目将其设置为默认风格(一个风格可能被多个项目使用,使用 first() 避免 MultipleResultsFound
result = await db.execute(
select(ProjectDefaultStyle).where(ProjectDefaultStyle.style_id == style_id)
)
default_relation = result.scalar_one_or_none()
default_relation = result.scalars().first()
if default_relation:
raise HTTPException(
status_code=400,