feat: 结局AI改写支持pathHistory回放和完成通知

This commit is contained in:
wangwuww111
2026-03-10 14:19:13 +08:00
parent aa23db8a89
commit 2b941cc4e0
5 changed files with 119 additions and 29 deletions

View File

@@ -39,6 +39,7 @@ class CreateEndingDraftRequest(BaseModel):
endingName: str
endingContent: str
prompt: str
pathHistory: list = [] # 游玩路径历史(可选)
class ContinueEndingDraftRequest(BaseModel):
@@ -48,6 +49,7 @@ class ContinueEndingDraftRequest(BaseModel):
endingName: str
endingContent: str
prompt: str
pathHistory: list = [] # 游玩路径历史(可选)
class DraftResponse(BaseModel):
@@ -169,10 +171,9 @@ async def process_ai_rewrite_ending(draft_id: int):
await db.commit()
return
# 从 path_history 获取结局信息
ending_info = draft.path_history or {}
ending_name = ending_info.get("endingName", "未知结局")
ending_content = ending_info.get("endingContent", "")
# 从草稿字段获取结局信息
ending_name = draft.current_node_key or "未知结局"
ending_content = draft.current_content or ""
# 调用AI服务改写结局
ai_result = await ai_service.rewrite_ending(
@@ -201,16 +202,17 @@ async def process_ai_rewrite_ending(draft_id: int):
except (json.JSONDecodeError, AttributeError):
pass
# 成功 - 存储为单节点结局格式
# 成功 - 存储为对象格式(与故事节点格式一致)
draft.status = DraftStatus.completed
draft.ai_nodes = [{
"nodeKey": "ending_rewrite",
"content": content,
"speaker": "旁白",
"isEnding": True,
"endingName": new_ending_name,
"endingType": "rewrite"
}]
draft.ai_nodes = {
"ending_rewrite": {
"content": content,
"speaker": "旁白",
"is_ending": True,
"ending_name": new_ending_name,
"ending_type": "rewrite"
}
}
draft.entry_node_key = "ending_rewrite"
draft.tokens_used = ai_result.get("tokens_used", 0)
draft.title = f"{story.title}-{new_ending_name}"
@@ -264,10 +266,9 @@ async def process_ai_continue_ending(draft_id: int):
await db.commit()
return
# 从 path_history 获取结局信息
ending_info = draft.path_history or {}
ending_name = ending_info.get("endingName", "未知结局")
ending_content = ending_info.get("endingContent", "")
# 从草稿字段获取结局信息
ending_name = draft.current_node_key or "未知结局"
ending_content = draft.current_content or ""
# 调用AI服务续写结局
ai_result = await ai_service.continue_ending(
@@ -376,14 +377,14 @@ async def create_ending_draft(
if not story:
raise HTTPException(status_code=404, detail="故事不存在")
# 创建草稿记录,将结局信息存在 path_history
# 创建草稿记录,保存游玩路径和结局信息
draft = StoryDraft(
user_id=request.userId,
story_id=request.storyId,
title=f"{story.title}-结局改写",
path_history={"endingName": request.endingName, "endingContent": request.endingContent},
current_node_key="ending",
current_content=request.endingContent,
path_history=request.pathHistory, # 保存游玩路径
current_node_key=request.endingName, # 保存结局名称
current_content=request.endingContent, # 保存结局内容
user_prompt=request.prompt,
status=DraftStatus.pending
)
@@ -421,14 +422,14 @@ async def create_continue_ending_draft(
if not story:
raise HTTPException(status_code=404, detail="故事不存在")
# 创建草稿记录,将结局信息存在 path_history
# 创建草稿记录,保存游玩路径和结局信息
draft = StoryDraft(
user_id=request.userId,
story_id=request.storyId,
title=f"{story.title}-结局续写",
path_history={"endingName": request.endingName, "endingContent": request.endingContent},
current_node_key="ending",
current_content=request.endingContent,
path_history=request.pathHistory, # 保存游玩路径
current_node_key=request.endingName, # 保存结局名称
current_content=request.endingContent, # 保存结局内容
user_prompt=request.prompt,
status=DraftStatus.pending
)