diff --git a/crates/df-storage/src/crud.rs b/crates/df-storage/src/crud.rs index 80d3c28..fee1a64 100644 --- a/crates/df-storage/src/crud.rs +++ b/crates/df-storage/src/crud.rs @@ -1339,6 +1339,28 @@ impl KnowledgeEventsRepo { // AiConversationRepo 的整体更新已由 impl_repo! 宏统一生成的 update_full 提供。 impl AiConversationRepo { + /// 清空对话消息内容(保留 conversation 记录本身,只清 messages JSON + 清零 token 计数) + /// + /// "清空对话"语义:对话壳保留(侧栏仍可见,可继续在该对话内聊),仅清空历史消息。 + /// messages 是 ai_conversations 表内的 JSON 列而非独立行,故"删 messages"= 置空该列。 + pub async fn clear_messages(&self, id: &str) -> Result { + let conn = self.conn.clone(); + let id = id.to_owned(); + let now = now_millis_str(); + tokio::task::spawn_blocking(move || { + let guard = conn.blocking_lock(); + let affected = guard + .execute( + "UPDATE ai_conversations SET messages = '[]', prompt_tokens = 0, completion_tokens = 0, updated_at = ?1 WHERE id = ?2", + params![now, id], + ) + .map_err(|e| Error::Storage(e.to_string()))?; + Ok(affected > 0) + }) + .await + .map_err(|e| Error::Storage(e.to_string()))? + } + /// 设置归档标记(仅改 archived,不动 updated_at) /// /// 区别于 update_field(后者强制 SET updated_at=now,会把归档/取消归档误判为内容更新, diff --git a/src-tauri/src/commands/ai/commands.rs b/src-tauri/src/commands/ai/commands.rs index d6baec9..8bcf19a 100644 --- a/src-tauri/src/commands/ai/commands.rs +++ b/src-tauri/src/commands/ai/commands.rs @@ -214,8 +214,19 @@ pub async fn ai_pending_tool_calls( #[tauri::command] pub async fn ai_chat_clear(state: State<'_, AppState>) -> Result<(), String> { let mut session = state.ai_session.lock().await; + // 取活跃对话 id 后释放锁(避免持 session 锁调 DB) + let active_id = session.active_conversation_id.clone(); session.messages.clear(); session.pending_approvals.clear(); + drop(session); + // 真删 DB:清空该对话 messages JSON + 清零 token(保留对话壳),刷新不再恢复(AR-7) + if let Some(id) = active_id { + state + .ai_conversations + .clear_messages(&id) + .await + .map_err(|e| e.to_string())?; + } Ok(()) } diff --git a/src/components/AiChat.vue b/src/components/AiChat.vue index 92ff771..f6b8fe8 100644 --- a/src/components/AiChat.vue +++ b/src/components/AiChat.vue @@ -103,6 +103,10 @@ + +