feat: auto-save story characters as global NPCs on save
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
getStory,
|
||||
updateStory,
|
||||
getNPCCharacters,
|
||||
createNPCCharacter,
|
||||
} from "../services/api";
|
||||
import { generateAvatarUrl } from "../services/imageGen";
|
||||
import { useStoryGeneration } from "../hooks/useStoryGeneration";
|
||||
@@ -424,6 +425,8 @@ export default function CreateStoryPage() {
|
||||
allSettings.push(form.customSetting.trim());
|
||||
}
|
||||
|
||||
const storyCharacters = form.characters.filter((c) => c.name.trim());
|
||||
|
||||
const storyData = {
|
||||
title: form.title,
|
||||
description: form.description || `Исекай история: ${form.title}`,
|
||||
@@ -436,7 +439,7 @@ export default function CreateStoryPage() {
|
||||
summary: form.summary,
|
||||
plot: form.plot,
|
||||
firstMessage: form.firstMessage,
|
||||
characters: form.characters.filter((c) => c.name.trim()),
|
||||
characters: storyCharacters,
|
||||
isNsfw: form.isNsfw,
|
||||
temperature: form.temperature,
|
||||
narrativeRules: form.narrativeRules.trim() || undefined,
|
||||
@@ -447,14 +450,39 @@ export default function CreateStoryPage() {
|
||||
},
|
||||
};
|
||||
|
||||
// Helper function to save characters as global NPCs
|
||||
const saveCharactersAsGlobalNPCs = async () => {
|
||||
const existingNames = savedNPCs.map((npc) => npc.name.toLowerCase());
|
||||
|
||||
for (const char of storyCharacters) {
|
||||
// Skip if NPC with this name already exists
|
||||
if (existingNames.includes(char.name.toLowerCase())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Create global NPC from story character
|
||||
await createNPCCharacter({
|
||||
name: char.name,
|
||||
description: char.description,
|
||||
role: char.role,
|
||||
age: char.age || "adult",
|
||||
gender: char.gender || "female",
|
||||
isNsfw: form.isNsfw,
|
||||
avatarUrl: char.avatarUrl,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (isEditMode && id) {
|
||||
const success = await updateStory(id, storyData);
|
||||
if (success) {
|
||||
await saveCharactersAsGlobalNPCs();
|
||||
navigate(`/story/${id}`);
|
||||
}
|
||||
} else {
|
||||
const story = await createStory(storyData);
|
||||
if (story) {
|
||||
await saveCharactersAsGlobalNPCs();
|
||||
navigate(`/story/${story.id}`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user