update: 更新伏笔管理删除和同步逻辑,避免重复引入

This commit is contained in:
xiamuceer
2026-04-09 09:15:11 +08:00
parent 5968a3d29e
commit 425aab9eec
6 changed files with 357 additions and 19 deletions
+82 -9
View File
@@ -145,12 +145,31 @@ async def analyze_chapter(
chapter_id,
chapter.chapter_number
)
# 重新分析前,先清理该章节旧记忆(关系库 + 向量库)
old_memories_result = await db.execute(
select(StoryMemory).where(StoryMemory.chapter_id == chapter_id)
)
old_memories = old_memories_result.scalars().all()
for old_mem in old_memories:
await db.delete(old_mem)
await db.flush()
if user_id:
try:
await memory_service.delete_chapter_memories(
user_id=user_id,
project_id=project_id,
chapter_id=chapter_id
)
except Exception as vector_delete_error:
logger.warning(f"⚠️ 清理章节向量记忆失败(继续分析): {str(vector_delete_error)}")
# 保存记忆到数据库和向量库
saved_count = 0
for mem_data in memories_data:
memory_id = str(uuid.uuid4())
# 保存到关系数据库
memory = StoryMemory(
id=memory_id,
@@ -164,7 +183,7 @@ async def analyze_chapter(
**mem_data['metadata']
)
db.add(memory)
# 保存到向量库
await memory_service.add_memory(
user_id=user_id,
@@ -175,13 +194,66 @@ async def analyze_chapter(
metadata=mem_data['metadata']
)
saved_count += 1
await db.commit()
entity_changes = {
"careers": {"updated_count": 0, "changes": []},
"character_states": {
"state_updated_count": 0,
"relationship_created_count": 0,
"relationship_updated_count": 0,
"org_updated_count": 0,
"changes": []
},
"organization_states": {"updated_count": 0, "changes": []}
}
# 更新角色职业 / 角色状态关系 / 组织状态
if analysis_result.get('character_states'):
try:
from app.services.career_update_service import CareerUpdateService
career_update_result = await CareerUpdateService.update_careers_from_analysis(
db=db,
project_id=project_id,
character_states=analysis_result.get('character_states', []),
chapter_id=chapter_id,
chapter_number=chapter.chapter_number
)
entity_changes["careers"] = career_update_result
except Exception as career_error:
logger.error(f"⚠️ 更新角色职业失败(不影响分析结果): {str(career_error)}", exc_info=True)
try:
from app.services.character_state_update_service import CharacterStateUpdateService
state_update_result = await CharacterStateUpdateService.update_from_analysis(
db=db,
project_id=project_id,
character_states=analysis_result.get('character_states', []),
chapter_id=chapter_id,
chapter_number=chapter.chapter_number
)
entity_changes["character_states"] = state_update_result
except Exception as state_error:
logger.error(f"⚠️ 更新角色状态、关系和组织成员失败(不影响分析结果): {str(state_error)}", exc_info=True)
if analysis_result.get('organization_states'):
try:
from app.services.character_state_update_service import CharacterStateUpdateService
org_state_result = await CharacterStateUpdateService.update_organization_states(
db=db,
project_id=project_id,
organization_states=analysis_result.get('organization_states', []),
chapter_number=chapter.chapter_number
)
entity_changes["organization_states"] = org_state_result
except Exception as org_state_error:
logger.error(f"⚠️ 更新组织自身状态失败(不影响分析结果): {str(org_state_error)}", exc_info=True)
# 【新增】自动更新伏笔状态
foreshadow_stats = {"planted_count": 0, "resolved_count": 0, "created_count": 0}
analysis_foreshadows = analysis_result.get('foreshadows', [])
if analysis_foreshadows:
try:
foreshadow_stats = await foreshadow_service.auto_update_from_analysis(
@@ -194,15 +266,16 @@ async def analyze_chapter(
logger.info(f"📊 伏笔自动更新: 埋入{foreshadow_stats['planted_count']}个, 回收{foreshadow_stats['resolved_count']}")
except Exception as fs_error:
logger.error(f"⚠️ 伏笔自动更新失败(不影响分析结果): {str(fs_error)}")
logger.info(f"✅ 章节分析完成: 保存{saved_count}条记忆")
return {
"success": True,
"message": f"分析完成,提取了{saved_count}条记忆",
"analysis": plot_analysis.to_dict(),
"memories_count": saved_count,
"foreshadow_stats": foreshadow_stats
"foreshadow_stats": foreshadow_stats,
"entity_changes": entity_changes
}
except HTTPException: