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