Commit b29011ef authored by yaoke.yk's avatar yaoke.yk

fix(storyboard): harden prompt persistence and invalidation

parent a19ab7b9
......@@ -23,6 +23,7 @@ import com.yaoai.pipeline.service.StoryboardPipelineService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -193,6 +194,7 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
}
@Override
@Transactional
public Storyboard update(Long id, Long tenantId, Storyboard patch) {
Storyboard existing = storyboardMapper.selectById(id);
if (existing == null || !tenantId.equals(existing.getTenantId())) {
......@@ -202,7 +204,10 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
boolean bodyFieldsChanged = hasStoryboardBodyChanges(existing, patch);
patch.setId(id);
patch.setTenantId(tenantId);
storyboardMapper.updateById(patch);
int updatedRows = storyboardMapper.updateById(patch);
if (updatedRows != 1) {
throw new BizException(ErrorCode.INTERNAL_ERROR, "Failed to update storyboard");
}
if (bodyFieldsChanged) {
clearVideoPrompt(id, tenantId);
}
......@@ -226,6 +231,7 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
}
@Override
@Transactional
public String generatePrompt(Long storyboardId, Long tenantId) {
Storyboard storyboard = storyboardMapper.selectById(storyboardId);
if (storyboard == null || !tenantId.equals(storyboard.getTenantId())) {
......@@ -271,7 +277,7 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
userMsg.append(String.format(
"- @%s (%s): %s%n",
nullSafe(scene.getName()),
"indoor".equals(scene.getSceneType()) ? "indoor" : "outdoor",
sceneTypeLabel(scene.getSceneType()),
nullSafe(scene.getDescription())
));
}
......@@ -331,11 +337,6 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
String prompt = llmService.chat(request);
Storyboard promptUpdate = new Storyboard();
promptUpdate.setId(storyboard.getId());
promptUpdate.setVideoPrompt(prompt);
storyboardMapper.updateById(promptUpdate);
billingService.charge(BillingChargeRequest.builder()
.tenantId(tenantId)
.userId(UserContext.require())
......@@ -348,6 +349,14 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
.unitCount(1)
.refId(String.valueOf(storyboardId))
.build());
Storyboard promptUpdate = new Storyboard();
promptUpdate.setId(storyboard.getId());
promptUpdate.setVideoPrompt(prompt);
int updatedRows = storyboardMapper.updateById(promptUpdate);
if (updatedRows != 1) {
throw new BizException(ErrorCode.INTERNAL_ERROR, "Failed to persist storyboard prompt");
}
return prompt;
}
......@@ -388,7 +397,7 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
context.append(String.format(
"- @%s (%s): %s%n",
nullSafe(scene.getName()),
"indoor".equals(scene.getSceneType()) ? "indoor" : "outdoor",
sceneTypeLabel(scene.getSceneType()),
nullSafe(scene.getDescription())
));
}
......@@ -412,17 +421,19 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
private StoryboardRefs splitStoryboardRefs(String rawRefs, Set<String> sceneRefs) {
List<String> refs = parseRefs(rawRefs);
List<String> characterRefs = new ArrayList<>();
List<String> matchedSceneRefs = new ArrayList<>();
String sceneRef = null;
for (String ref : refs) {
if (sceneRefs.contains(ref)) {
matchedSceneRefs.add(ref);
if (sceneRef == null) {
sceneRef = ref;
}
} else {
characterRefs.add(ref);
}
}
return new StoryboardRefs(joinRefs(characterRefs), joinRefs(matchedSceneRefs));
return new StoryboardRefs(joinRefs(characterRefs), sceneRef);
}
private StoryboardRefs promptRefsForCurrentShot(Storyboard storyboard, List<Scene> projectScenes) {
......@@ -459,13 +470,16 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
}
private void clearVideoPrompt(Long storyboardId, Long tenantId) {
storyboardMapper.update(
int updatedRows = storyboardMapper.update(
null,
new UpdateWrapper<Storyboard>()
.eq("id", storyboardId)
.eq("tenant_id", tenantId)
.set("video_prompt", null)
);
if (updatedRows != 1) {
throw new BizException(ErrorCode.INTERNAL_ERROR, "Failed to clear storyboard prompt");
}
}
private boolean hasStoryboardBodyChanges(Storyboard existing, Storyboard patch) {
......@@ -508,6 +522,16 @@ public class StoryboardPipelineServiceImpl implements StoryboardPipelineService
return value != null ? value : "";
}
private String sceneTypeLabel(String sceneType) {
if ("indoor".equals(sceneType)) {
return "indoor";
}
if ("outdoor".equals(sceneType)) {
return "outdoor";
}
return "";
}
private record StoryboardRefs(String characters, String sceneRef) {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment