fix: loadMessages 对历史剧本 plotJson 自动初始化首条 AI 消息
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user