fix: persist model on conversation send, fallback resolution, carry model on new conversation

This commit is contained in:
Xianren Studio
2026-08-13 23:44:27 +08:00
parent 00624d81cf
commit 95d7ff1299
6 changed files with 36 additions and 12 deletions
+1
View File
@@ -29,6 +29,7 @@ reqwest.workspace = true
futures.workspace = true
uuid.workspace = true
dirs.workspace = true
rusqlite.workspace = true
xianren-core = { path = "../../crates/core" }
xianren-engine = { path = "../../crates/engine" }
xianren-download = { path = "../../crates/download" }
+23 -9
View File
@@ -217,10 +217,12 @@ pub fn list_conversations(state: State<'_, App>) -> Result<Vec<xianren_core::Con
pub fn create_conversation(
state: State<'_, App>,
title: String,
model_id: Option<String>,
) -> Result<xianren_core::Conversation, String> {
let id = uuid::Uuid::new_v4().to_string();
let db = state.core.db.lock().unwrap();
sessions_db::create_conversation(&db, &id, &title, None, None).map_err(|e| e.to_string())?;
sessions_db::create_conversation(&db, &id, &title, model_id.as_deref(), None)
.map_err(|e| e.to_string())?;
sessions_db::get_conversation(&db, &id)
.map_err(|e| e.to_string())?
.ok_or_else(|| "conversation not found".to_string())
@@ -481,6 +483,9 @@ pub async fn chat_send(
)
.map_err(|e| e.to_string())?;
}
// 始终把当前使用的模型写回会话,编辑/重新生成依赖它
sessions_db::update_conversation_model(&db, &payload.conversation_id, &payload.model_id)
.map_err(|e| e.to_string())?;
let user_message_id = sessions_db::add_message(
&db,
&payload.conversation_id,
@@ -724,6 +729,21 @@ fn estimate_tokens(text: &str) -> usize {
score / 4 + 1
}
/// 解析会话使用的模型:优先会话记录;未记录时若本地只有一个模型则自动使用,否则提示。
fn resolve_conversation_model(
db: &rusqlite::Connection,
conversation: &xianren_core::Conversation,
) -> Result<String, String> {
if let Some(id) = &conversation.model_id {
return Ok(id.clone());
}
let all = models_db::list(db).map_err(|e| e.to_string())?;
if all.len() == 1 {
return Ok(all[0].id.clone());
}
Err("会话未记录模型:请先在该会话发送一条消息,或重新选择模型后重试".into())
}
/// 重新生成某条助手回复:旧内容存入版本历史,截断后重新生成。
#[tauri::command]
pub async fn regenerate_message(
@@ -768,10 +788,7 @@ pub async fn regenerate_message(
images: m.images.clone(),
})
.collect::<Vec<_>>();
let model_id = conversation
.model_id
.clone()
.ok_or_else(|| "conversation has no model".to_string())?;
let model_id = resolve_conversation_model(&db, &conversation)?;
let model = models_db::get(&db, &model_id)
.map_err(|e| e.to_string())?
.ok_or_else(|| "model not found".to_string())?;
@@ -835,10 +852,7 @@ pub async fn edit_message(
images: m.images,
})
.collect::<Vec<_>>();
let model_id = conversation
.model_id
.clone()
.ok_or_else(|| "conversation has no model".to_string())?;
let model_id = resolve_conversation_model(&db, &conversation)?;
let model = models_db::get(&db, &model_id)
.map_err(|e| e.to_string())?
.ok_or_else(|| "model not found".to_string())?;