From a713e8823c5774f899b932589f2c98f7d4925414 Mon Sep 17 00:00:00 2001 From: Peanut Date: Mon, 29 Jun 2026 22:50:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20loadMessages=20=E5=AF=B9=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E5=89=A7=E6=9C=AC=20plotJson=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A6=96=E6=9D=A1=20AI=20=E6=B6=88?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mini-program/src/pages/main/ScriptView.vue | 46 +++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/mini-program/src/pages/main/ScriptView.vue b/mini-program/src/pages/main/ScriptView.vue index 384661d..f1887ac 100644 --- a/mini-program/src/pages/main/ScriptView.vue +++ b/mini-program/src/pages/main/ScriptView.vue @@ -663,7 +663,16 @@ const loadMessages = async () => { try { const res = await listMessagesByConversation({ conversationId: conversationId.value, includeVersions: false }) - messages.value = res.data || [] + let loaded = res.data || [] + + // 历史剧本兼容:若 API 返回空且 plotJson 有内容,自动初始化首条 AI 消息 + if (loaded.length === 0 && currentResult.value?.plotJson?.fullContent) { + await initializeFirstAIMessage() + const retryRes = await listMessagesByConversation({ conversationId: conversationId.value, includeVersions: false }) + loaded = retryRes.data || [] + } + + messages.value = loaded const scriptMessages = messages.value.filter(m => m.type === 'script') if (scriptMessages.length > 0) { const rootMessage = scriptMessages[0] @@ -675,6 +684,41 @@ const loadMessages = async () => { } } +/** + * 历史剧本兼容:基于 plotJson 初始化首条 AI 剧本消息 + * 触发条件:loadMessages 返回空列表,且 currentResult.plotJson.fullContent 存在 + */ +const initializeFirstAIMessage = async () => { + const content = currentResult.value?.plotJson?.fullContent || '' + if (!content) return + + try { + // 先保存用户心愿消息(theme) + const theme = currentResult.value?.theme || currentResult.value?.title || '' + if (theme) { + await createMessage({ + conversationId: conversationId.value, + content: theme, + sender: 'user', + type: 'chat' + }) + } + + // 再保存 AI 剧本消息 + await createMessage({ + conversationId: conversationId.value, + content, + sender: 'assistant', + type: 'script', + metadata: JSON.stringify(currentResult.value?.plotJson || {}), + versionNumber: 1 + }) + } catch (error) { + // 初始化失败不阻塞页面加载,仅日志 + console.warn('[ScriptView] initializeFirstAIMessage failed:', error) + } +} + const sendChat = async () => { if (!chatInput.value.trim() || generating.value) return const content = chatInput.value.trim()