fix:1.修复生成章节内容的风格获取

This commit is contained in:
xiamuceer
2025-11-28 20:24:17 +08:00
parent 3be62e1482
commit 45152a5694
5 changed files with 44 additions and 15 deletions
+8 -7
View File
@@ -444,6 +444,7 @@ async def build_smart_chapter_context(
.where(Chapter.content != "")
.order_by(Chapter.chapter_number)
)
all_chapters_info = all_chapters_result.all()
total_previous = len(all_chapters_info)
@@ -1054,10 +1055,10 @@ async def generate_chapter_content_stream(
)
style = style_result.scalar_one_or_none()
if style:
# 验证风格是否可用:全局预设风格(project_id为NULL)或者当前项目的自定义风格
if style.project_id is None or style.project_id == current_chapter.project_id:
# 验证风格是否可用:全局预设风格(user_id为NULL)或者当前用户的自定义风格
if style.user_id is None or style.user_id == current_user_id:
style_content = style.prompt_content or ""
style_type = "全局预设" if style.project_id is None else "项目自定义"
style_type = "全局预设" if style.user_id is None else "用户自定义"
logger.info(f"使用指定风格: {style.name} ({style_type})")
else:
logger.warning(f"风格 {style_id} 不属于当前项目,无法使用")
@@ -2338,7 +2339,7 @@ async def generate_single_chapter_for_batch(
)
style = style_result.scalar_one_or_none()
if style:
if style.project_id is None or style.project_id == chapter.project_id:
if style.user_id is None or style.user_id == user_id:
style_content = style.prompt_content or ""
# 构建智能上下文
@@ -2589,10 +2590,10 @@ async def regenerate_chapter_stream(
)
style = style_result.scalar_one_or_none()
if style:
# 验证风格是否可用:全局预设风格(project_id为NULL)或者当前项目的自定义风格
if style.project_id is None or style.project_id == chapter.project_id:
# 验证风格是否可用:全局预设风格(user_id为NULL)或者当前用户的自定义风格
if style.user_id is None or style.user_id == user_id:
style_content = style.prompt_content or ""
style_type = "全局预设" if style.project_id is None else "项目自定义"
style_type = "全局预设" if style.user_id is None else "用户自定义"
logger.info(f"✅ 使用写作风格: {style.name} ({style_type})")
else:
logger.warning(f"⚠️ 风格 {style_id} 不属于当前项目,跳过")
+1 -1
View File
@@ -229,7 +229,7 @@ async def world_building_generator(
try:
result = await db.execute(
select(WritingStyle).where(
WritingStyle.project_id.is_(None),
WritingStyle.user_id.is_(None),
WritingStyle.order_index == 1
).limit(1)
)
+2 -2
View File
@@ -311,7 +311,7 @@ async def _init_global_writing_styles(user_id: str):
async with AsyncSessionLocal() as session:
# 检查是否已存在全局预设风格
result = await session.execute(
select(WritingStyle).where(WritingStyle.project_id.is_(None))
select(WritingStyle).where(WritingStyle.user_id.is_(None))
)
existing = result.scalars().first()
@@ -326,7 +326,7 @@ async def _init_global_writing_styles(user_id: str):
for index, (preset_id, preset_data) in enumerate(presets.items(), start=1):
style = WritingStyle(
project_id=None, # NULL 表示全局预设
user_id=None, # NULL 表示全局预设
name=preset_data["name"],
style_type="preset",
preset_id=preset_id,
+32 -4
View File
@@ -340,10 +340,19 @@ class ImportExportService:
@staticmethod
async def _export_writing_styles(project_id: str, db: AsyncSession) -> List[WritingStyleExportData]:
"""导出写作风格"""
"""导出写作风格(用户自定义风格)"""
# 获取项目所属用户
project_result = await db.execute(
select(Project).where(Project.id == project_id)
)
project = project_result.scalar_one_or_none()
if not project:
return []
# 导出该用户的自定义风格(不包括全局预设)
result = await db.execute(
select(WritingStyle)
.where(WritingStyle.project_id == project_id)
.where(WritingStyle.user_id == project.user_id)
.order_by(WritingStyle.order_index)
)
styles = result.scalars().all()
@@ -797,11 +806,30 @@ class ImportExportService:
styles_data: List[Dict],
db: AsyncSession
) -> int:
"""导入写作风格"""
"""导入写作风格(用户自定义风格)"""
# 获取项目所属用户
project_result = await db.execute(
select(Project).where(Project.id == project_id)
)
project = project_result.scalar_one_or_none()
if not project:
return 0
count = 0
for style_data in styles_data:
# 检查是否已存在同名风格(避免重复导入)
existing = await db.execute(
select(WritingStyle).where(
WritingStyle.user_id == project.user_id,
WritingStyle.name == style_data.get("name")
)
)
if existing.scalar_one_or_none():
logger.debug(f"风格 {style_data.get('name')} 已存在,跳过导入")
continue
style = WritingStyle(
project_id=project_id,
user_id=project.user_id, # 使用 user_id 而不是 project_id
name=style_data.get("name"),
style_type=style_data.get("style_type"),
preset_id=style_data.get("preset_id"),