fix: 修复多轮RAG查询卡住问题
This commit is contained in:
parent
35b7a489c2
commit
dca2c15bb9
|
|
@ -145,8 +145,26 @@ impl ConversationRepository {
|
||||||
let conn = self.connection.lock().unwrap();
|
let conn = self.connection.lock().unwrap();
|
||||||
|
|
||||||
// 获取会话信息
|
// 获取会话信息
|
||||||
let session = self.get_session(&query.session_id)?
|
let session = match self.get_session(&query.session_id)? {
|
||||||
.ok_or_else(|| anyhow::anyhow!("Session not found: {}", query.session_id))?;
|
Some(s) => s,
|
||||||
|
None => {
|
||||||
|
// 如果会话不存在,创建一个临时会话对象并返回空消息列表
|
||||||
|
println!("⚠️ 会话不存在: {},返回空历史", query.session_id);
|
||||||
|
let empty_session = ConversationSession {
|
||||||
|
id: query.session_id.clone(),
|
||||||
|
title: Some("临时会话".to_string()),
|
||||||
|
created_at: chrono::Utc::now(),
|
||||||
|
updated_at: chrono::Utc::now(),
|
||||||
|
is_active: true,
|
||||||
|
metadata: None,
|
||||||
|
};
|
||||||
|
return Ok(ConversationHistory {
|
||||||
|
session: empty_session,
|
||||||
|
messages: Vec::new(),
|
||||||
|
total_count: 0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// 构建消息查询
|
// 构建消息查询
|
||||||
let mut sql = "SELECT id, session_id, role, content, timestamp, metadata
|
let mut sql = "SELECT id, session_id, role, content, timestamp, metadata
|
||||||
|
|
|
||||||
|
|
@ -1058,17 +1058,8 @@ impl GeminiService {
|
||||||
println!("✅ 会话已存在");
|
println!("✅ 会话已存在");
|
||||||
}
|
}
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
println!("🆕 会话不存在,创建新会话...");
|
println!("🆕 会话不存在,跳过历史消息获取...");
|
||||||
// 创建新会话
|
// 对于新会话,没有历史消息,直接跳过
|
||||||
use crate::data::models::conversation::CreateConversationSessionRequest;
|
|
||||||
let create_request = CreateConversationSessionRequest {
|
|
||||||
title: Some("RAG对话会话".to_string()),
|
|
||||||
metadata: None,
|
|
||||||
};
|
|
||||||
match repo.create_session(create_request) {
|
|
||||||
Ok(_) => println!("✅ 新会话创建成功"),
|
|
||||||
Err(e) => println!("⚠️ 创建会话失败: {}", e),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("⚠️ 检查会话失败: {}", e);
|
println!("⚠️ 检查会话失败: {}", e);
|
||||||
|
|
@ -1084,7 +1075,13 @@ impl GeminiService {
|
||||||
context_length: 0,
|
context_length: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
println!("🔍 检查是否需要获取历史消息...");
|
||||||
|
println!(" - 会话仓库存在: {}", conversation_repo.is_some());
|
||||||
|
println!(" - 包含历史消息: {}", request.include_history.unwrap_or(false));
|
||||||
|
|
||||||
if let (Some(repo), true) = (&conversation_repo, request.include_history.unwrap_or(false)) {
|
if let (Some(repo), true) = (&conversation_repo, request.include_history.unwrap_or(false)) {
|
||||||
|
println!("📚 开始获取历史消息...");
|
||||||
|
println!("🔄 调用get_conversation_history方法...");
|
||||||
match self.get_conversation_history(&repo, &session_id, request.max_history_messages.unwrap_or(10)).await {
|
match self.get_conversation_history(&repo, &session_id, request.max_history_messages.unwrap_or(10)).await {
|
||||||
Ok(history_messages) => {
|
Ok(history_messages) => {
|
||||||
conversation_context.total_messages = history_messages.len() as u32;
|
conversation_context.total_messages = history_messages.len() as u32;
|
||||||
|
|
@ -1166,6 +1163,10 @@ impl GeminiService {
|
||||||
session_id: &str,
|
session_id: &str,
|
||||||
max_messages: u32,
|
max_messages: u32,
|
||||||
) -> Result<Vec<ConversationMessage>> {
|
) -> Result<Vec<ConversationMessage>> {
|
||||||
|
println!("📋 构建历史查询参数...");
|
||||||
|
println!(" - session_id: {}", session_id);
|
||||||
|
println!(" - max_messages: {}", max_messages);
|
||||||
|
|
||||||
let query = ConversationHistoryQuery {
|
let query = ConversationHistoryQuery {
|
||||||
session_id: session_id.to_string(),
|
session_id: session_id.to_string(),
|
||||||
limit: Some(max_messages),
|
limit: Some(max_messages),
|
||||||
|
|
@ -1173,9 +1174,16 @@ impl GeminiService {
|
||||||
include_system_messages: Some(false),
|
include_system_messages: Some(false),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
println!("🔍 调用repo.get_conversation_history...");
|
||||||
match repo.get_conversation_history(query) {
|
match repo.get_conversation_history(query) {
|
||||||
Ok(history) => Ok(history.messages),
|
Ok(history) => {
|
||||||
Err(e) => Err(anyhow!("获取会话历史失败: {}", e)),
|
println!("✅ 历史查询成功,获取到 {} 条消息", history.messages.len());
|
||||||
|
Ok(history.messages)
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
println!("❌ 历史查询失败: {}", e);
|
||||||
|
Err(anyhow!("获取会话历史失败: {}", e))
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue