修复语音/通话聊天记录无法显示在历史面板的问题

根因:chatStore 的 currentBackgroundId 从 localStorage('selectedBackground') 读取,
但前端从未写入该 key,导致语音/通话会话的 bg_id 始终为默认值 1,
与历史面板查询的实际 backgroundId 不匹配。

修复:
- ChatView 新增 cacheSelectedBackground(),在初始化和加载背景时同步到 localStorage
- chatStore voice/call 初始化函数支持 config.background_id 显式覆盖
- ChatView 中 callConfig 补充 background_id 字段
- 后端 _persist_voice_session_history 异常日志升级为 error + traceback

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
wsb1224 2026-06-07 14:30:04 +08:00
parent 779dac49c8
commit a649712fdb
3 changed files with 16 additions and 5 deletions

View File

@ -11,6 +11,7 @@ import asyncio
import base64
import json
import logging
import traceback
import uuid
from datetime import datetime
from typing import Dict, Optional, Any
@ -824,7 +825,7 @@ async def _persist_voice_session_history(
))
await db.commit()
except Exception as e:
logger.warning(f"Failed to persist voice session history: user_id={user_id}, mode={mode}, error={e}")
logger.error(f"Failed to persist voice session history: user_id={user_id}, mode={mode}, error={e}\n{traceback.format_exc()}")
async def _append_voice_log_chunk(

View File

@ -679,7 +679,7 @@ export const useChatStore = defineStore('chat', () => {
...currentSessionConfig.value,
...petConfig,
...config,
background_id: currentBackgroundId.value
background_id: config.background_id || currentBackgroundId.value
}
voiceSessionReady.value = false
@ -1110,7 +1110,7 @@ export const useChatStore = defineStore('chat', () => {
...currentSessionConfig.value,
...petConfig,
...config,
background_id: currentBackgroundId.value
background_id: config.background_id || currentBackgroundId.value
}
const sent = sendRealtimeMessage(callConfig)

View File

@ -1313,6 +1313,7 @@ export default {
console.log('[ChatView] Final petPosition:', petPosition.value)
cacheSelectedPet(currentPet.value)
cacheSelectedBackground(currentBackground.value)
saveChatConfig()
syncStoreState()
await chatStore.fetchUserBalance()
@ -1329,6 +1330,7 @@ export default {
const response = await backgroundAPI.detail(backgroundId)
console.log('[ChatView] loadBackground response:', response.data)
currentBackground.value = response.data
cacheSelectedBackground(currentBackground.value)
console.log('[ChatView] currentBackground set to:', currentBackground.value)
} catch (err) {
console.error('Failed to load background:', err)
@ -1342,6 +1344,7 @@ export default {
console.log('[ChatView] loadDefaultBackground response:', response.data)
if (response.data.data && response.data.data.length > 0) {
currentBackground.value = response.data.data[0]
cacheSelectedBackground(currentBackground.value)
console.log('[ChatView] currentBackground set to:', currentBackground.value)
}
} catch (err) {
@ -1500,6 +1503,11 @@ export default {
localStorage.setItem('selectedPet', JSON.stringify(pet))
}
const cacheSelectedBackground = (background) => {
if (!background || !background.id) return
localStorage.setItem('selectedBackground', JSON.stringify(background))
}
const refreshActiveVoiceModeWithCurrentPet = async () => {
// /使
if (chatMode.value === 'call' && isInCall.value) {
@ -1513,7 +1521,8 @@ export default {
system_role: currentPet.value?.volcano_system_role || currentPet.value?.global_prompt || '你是一个友善的AI助手用简洁自然的语言和用户交流。',
character_manifest: currentPet.value?.volcano_character_manifest || '',
speaking_style: '语气自然亲切,回复简洁',
hello_text: `你好!我是${currentPet.value?.volcano_bot_name || currentPet.value?.name || 'AI助手'},有什么可以帮助你的吗?`
hello_text: `你好!我是${currentPet.value?.volcano_bot_name || currentPet.value?.name || 'AI助手'},有什么可以帮助你的吗?`,
background_id: currentBackground.value?.id
}
const modeWhenReconnect = chatMode.value
@ -1849,7 +1858,8 @@ export default {
system_role: currentPet.value?.volcano_system_role || currentPet.value?.global_prompt || '你是一个友善的AI助手用简洁自然的语言和用户交流。',
character_manifest: currentPet.value?.volcano_character_manifest || '',
speaking_style: '语气自然亲切,回复简洁',
hello_text: `你好!我是${currentPet.value?.volcano_bot_name || currentPet.value?.name || 'AI助手'},有什么可以帮助你的吗?`
hello_text: `你好!我是${currentPet.value?.volcano_bot_name || currentPet.value?.name || 'AI助手'},有什么可以帮助你的吗?`,
background_id: currentBackground.value?.id
}
const success = await chatStore.enterRealtimeCallMode(callConfig)