fix: 搜索结果持久化保存,刷新页面后历史对话也显示搜索结果
- 用户消息 extra_data 存储搜索结果 - API 返回消息时包含 extra_data - 前端 displayHistory 处理历史搜索结果显示
This commit is contained in:
@@ -648,6 +648,7 @@ async def get_messages(conversation_id: str, db: Session = Depends(get_db)):
|
||||
"role": m.role,
|
||||
"content": m.content,
|
||||
"thinking_content": m.thinking_content, # v2新增
|
||||
"extra_data": m.extra_data, # 包含搜索结果等
|
||||
"source": m.source,
|
||||
"agent_id": m.agent_id, # v2新增
|
||||
"model_used": m.model_used, # v2新增
|
||||
@@ -807,6 +808,7 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
|
||||
|
||||
# 4. 执行搜索并发送搜索结果
|
||||
search_context = None
|
||||
search_results_for_client = None # 用于发送给前端和保存
|
||||
logger.info(f"检查搜索条件: agent_tools={agent_tools}, disabled_tools={disabled_tools}")
|
||||
|
||||
if 'search' in agent_tools and 'search' not in disabled_tools:
|
||||
@@ -888,12 +890,13 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
|
||||
'duration_ms': duration_ms
|
||||
})
|
||||
|
||||
# 5. 保存用户消息到数据库
|
||||
# 5. 保存用户消息到数据库(包含搜索结果)
|
||||
user_msg = conv_service.add_message(
|
||||
conversation_id=conversation.id,
|
||||
role='user',
|
||||
content=message,
|
||||
source='web'
|
||||
source='web',
|
||||
extra_data={'search_results': search_results_for_client, 'search_query': message} if search_results_for_client else None
|
||||
)
|
||||
|
||||
# 6. 获取对话历史(包含刚保存的用户消息)
|
||||
|
||||
@@ -622,7 +622,28 @@
|
||||
function displayHistory(messages) {
|
||||
const container = document.getElementById('messagesContainer');
|
||||
container.innerHTML = '';
|
||||
messages.forEach(m => appendMessage(m.role, m.content, m.thinking_content));
|
||||
messages.forEach(m => {
|
||||
appendMessage(m.role, m.content, m.thinking_content);
|
||||
// 如果用户消息有搜索结果,追加显示
|
||||
if (m.role === 'user' && m.extra_data && m.extra_data.search_results) {
|
||||
displaySearchResultsForHistory(m.extra_data.search_results, m.extra_data.search_query || m.content);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function displaySearchResultsForHistory(results, query) {
|
||||
if (!results || results.length === 0) return;
|
||||
|
||||
const container = document.getElementById('messagesContainer');
|
||||
const userMessages = container.querySelectorAll('.message.user');
|
||||
const lastUserMsg = userMessages[userMessages.length - 1];
|
||||
|
||||
if (lastUserMsg) {
|
||||
const msgBody = lastUserMsg.querySelector('.message-body');
|
||||
if (msgBody) {
|
||||
msgBody.innerHTML += buildSearchResultsHtml(results, query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function clearMessages() {
|
||||
|
||||
Reference in New Issue
Block a user