fix:修复小说保存与列表刷新
- ShortNovelServiceImpl:累积 novel_delta 事件,novel_done 未携带 full_text 时用累积内容兜底 - ScriptLibraryView:onMounted 主动拉取最新剧本,解决生成后列表不刷新问题
This commit is contained in:
@@ -134,7 +134,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { computed, ref, watch } from 'vue'
|
import { computed, ref, watch, onMounted } from 'vue'
|
||||||
import { useAppStore } from '../../stores/app.js'
|
import { useAppStore } from '../../stores/app.js'
|
||||||
import { toggleFavoriteScript, checkFavoriteScript } from '../../services/epicScript.js'
|
import { toggleFavoriteScript, checkFavoriteScript } from '../../services/epicScript.js'
|
||||||
|
|
||||||
@@ -148,6 +148,12 @@ const activeMenuId = ref('')
|
|||||||
const deleteTarget = ref(null)
|
const deleteTarget = ref(null)
|
||||||
const deletingScript = ref(false)
|
const deletingScript = ref(false)
|
||||||
|
|
||||||
|
// 每次打开历史列表(组件挂载)时主动拉取最新剧本,避免显示 store 旧缓存
|
||||||
|
// 解决:生成完小说后打开历史列表看不到最新记录的问题
|
||||||
|
onMounted(() => {
|
||||||
|
store.fetchScripts()
|
||||||
|
})
|
||||||
|
|
||||||
// 固定 5 个分类标签:全部 / 短篇 / 中篇 / 长篇 / 收藏夹
|
// 固定 5 个分类标签:全部 / 短篇 / 中篇 / 长篇 / 收藏夹
|
||||||
const typeTabs = [
|
const typeTabs = [
|
||||||
{ label: '全部', value: 'all' },
|
{ label: '全部', value: 'all' },
|
||||||
|
|||||||
@@ -191,6 +191,11 @@ public class ShortNovelServiceImpl implements ShortNovelService {
|
|||||||
final java.util.concurrent.atomic.AtomicReference<JSONObject> stageMetaRef =
|
final java.util.concurrent.atomic.AtomicReference<JSONObject> stageMetaRef =
|
||||||
new java.util.concurrent.atomic.AtomicReference<>(new JSONObject());
|
new java.util.concurrent.atomic.AtomicReference<>(new JSONObject());
|
||||||
|
|
||||||
|
// 累积 novel_delta 事件的文本内容
|
||||||
|
// 上游服务通过 novel_delta 流式发送小说内容,novel_done 事件可能不含 full_text
|
||||||
|
// 因此需要在后端累积所有 delta,在 novel_done 时使用累积的完整文本保存到数据库
|
||||||
|
final StringBuilder novelContentAccumulator = new StringBuilder();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 构建 OkHttp 请求
|
// 构建 OkHttp 请求
|
||||||
Request okhttpRequest = new Request.Builder()
|
Request okhttpRequest = new Request.Builder()
|
||||||
@@ -257,7 +262,7 @@ public class ShortNovelServiceImpl implements ShortNovelService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 累积中间步骤元数据(用于 novel_done 时一并持久化)
|
// 累积中间步骤元数据(用于 novel_done 时一并持久化)+ 累积小说正文
|
||||||
JSONObject stagePayload = event.getJSONObject("payload");
|
JSONObject stagePayload = event.getJSONObject("payload");
|
||||||
if (stagePayload != null) {
|
if (stagePayload != null) {
|
||||||
if ("outline_created".equals(type)) {
|
if ("outline_created".equals(type)) {
|
||||||
@@ -270,6 +275,12 @@ public class ShortNovelServiceImpl implements ShortNovelService {
|
|||||||
if (card != null) {
|
if (card != null) {
|
||||||
stageMetaRef.get().put("clarification", card);
|
stageMetaRef.get().put("clarification", card);
|
||||||
}
|
}
|
||||||
|
} else if ("novel_delta".equals(type)) {
|
||||||
|
// 累积小说正文片段:上游通过 novel_delta 逐段发送内容
|
||||||
|
String delta = stagePayload.getString("delta");
|
||||||
|
if (delta != null) {
|
||||||
|
novelContentAccumulator.append(delta);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,7 +291,13 @@ public class ShortNovelServiceImpl implements ShortNovelService {
|
|||||||
originalQuery, payload != null);
|
originalQuery, payload != null);
|
||||||
if (payload != null && originalQuery != null && !originalQuery.trim().isEmpty()) {
|
if (payload != null && originalQuery != null && !originalQuery.trim().isEmpty()) {
|
||||||
String fullText = payload.getString("full_text");
|
String fullText = payload.getString("full_text");
|
||||||
if (fullText != null) {
|
// 兜底:如果上游 novel_done 未携带 full_text,使用已累积的 novel_delta 内容
|
||||||
|
if (fullText == null || fullText.isEmpty()) {
|
||||||
|
fullText = novelContentAccumulator.toString();
|
||||||
|
log.info("[ShortNovel SSE] novel_done 未携带 full_text,使用累积内容: length={}",
|
||||||
|
fullText.length());
|
||||||
|
}
|
||||||
|
if (fullText != null && !fullText.isEmpty()) {
|
||||||
Map<String, Object> metadata = new HashMap<>();
|
Map<String, Object> metadata = new HashMap<>();
|
||||||
if (payload.get("title") != null) {
|
if (payload.get("title") != null) {
|
||||||
metadata.put("title", payload.get("title"));
|
metadata.put("title", payload.get("title"));
|
||||||
|
|||||||
Reference in New Issue
Block a user