feat: AI中间章节改写功能 + 滚动优化

This commit is contained in:
wangwuww111
2026-03-06 13:16:54 +08:00
parent 66d4bd60c1
commit bbdccfa843
9 changed files with 602 additions and 21 deletions

View File

@@ -9,6 +9,7 @@ export default class StoryManager {
this.currentStory = null;
this.currentNodeKey = 'start';
this.categories = [];
this.pathHistory = []; // 记录用户走过的路径
}
/**
@@ -56,6 +57,7 @@ export default class StoryManager {
try {
this.currentStory = await get(`/stories/${storyId}`);
this.currentNodeKey = 'start';
this.pathHistory = []; // 重置路径历史
// 记录游玩次数
await post(`/stories/${storyId}/play`);
@@ -85,6 +87,14 @@ export default class StoryManager {
}
const choice = currentNode.choices[choiceIndex];
// 记录路径历史
this.pathHistory.push({
nodeKey: this.currentNodeKey,
content: currentNode.content,
choice: choice.text
});
this.currentNodeKey = choice.nextNodeKey;
return this.getCurrentNode();
@@ -145,6 +155,39 @@ export default class StoryManager {
}
}
/**
* AI改写中间章节生成新的剧情分支
* @returns {Object|null} 成功返回新节点,失败返回 null不改变当前状态
*/
async rewriteBranch(storyId, prompt, userId) {
try {
const currentNode = this.getCurrentNode();
const result = await post(`/stories/${storyId}/rewrite-branch`, {
userId: userId,
currentNodeKey: this.currentNodeKey,
pathHistory: this.pathHistory,
currentContent: currentNode?.content || '',
prompt: prompt
}, { timeout: 300000 }); // 5分钟超时AI生成需要较长时间
// 检查是否有有效的 nodes
if (result && result.nodes) {
// AI 成功,将新分支合并到当前故事中
Object.assign(this.currentStory.nodes, result.nodes);
// 跳转到新分支的入口节点
this.currentNodeKey = result.entryNodeKey || 'branch_1';
return this.getCurrentNode();
}
// AI 失败,返回 null
console.log('AI服务不可用:', result?.error || '未知错误');
return null;
} catch (error) {
console.error('AI改写分支失败:', error?.errMsg || error?.message || JSON.stringify(error));
return null;
}
}
/**
* AI续写故事
*/