优化: R-PD-10 commands err_str helper 统一错误格式化(87 处)
mod.rs 加 pub fn err_str<E: ToString>,commands/ 10 文件 87 处 .map_err(|e| e.to_string()) → .map_err(err_str),残留 0(10 处复杂表达式 e 用于 format!/anyhow 保留)。行为零变化,统一入口为未来加日志/分类留点。批5,cargo workspace 0
This commit is contained in:
@@ -10,7 +10,7 @@ use df_core::types::new_id;
|
||||
use df_storage::models::AiProviderRecord;
|
||||
|
||||
use crate::state::AppState;
|
||||
use crate::commands::now_millis;
|
||||
use crate::commands::{err_str, now_millis};
|
||||
|
||||
use super::agentic::{run_agentic_loop, try_continue_agent_loop};
|
||||
use super::audit::audit_finalize;
|
||||
@@ -230,7 +230,7 @@ pub async fn ai_chat_clear(state: State<'_, AppState>) -> Result<(), String> {
|
||||
.ai_conversations
|
||||
.clear_messages(&id)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
.map_err(err_str)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -311,7 +311,7 @@ fn mask_api_key(key: &str) -> String {
|
||||
/// 列出所有已配置的 AI 提供商(is_default 真相源为 DB,重启不丢)
|
||||
#[tauri::command]
|
||||
pub async fn ai_list_providers(state: State<'_, AppState>) -> Result<Vec<AiProviderRecord>, String> {
|
||||
let mut providers = state.ai_providers.list_all().await.map_err(|e| e.to_string())?;
|
||||
let mut providers = state.ai_providers.list_all().await.map_err(err_str)?;
|
||||
// IPC 不传明文 api_key(FR-S1):前端编辑用空 apiKey 表示不改,mask 后前端 realm 不持有明文。
|
||||
// 迁移后 DB api_key 空 → 从 keyring 取真实密钥再 mask(前端看到 mask 但不持有明文)
|
||||
for p in &mut providers {
|
||||
@@ -339,7 +339,7 @@ pub async fn ai_save_provider(
|
||||
// 编辑已有提供商时保留原 created_at,避免被覆盖
|
||||
let created_at = match &id {
|
||||
Some(pid) => state.ai_providers.get_by_id(pid).await
|
||||
.map_err(|e| e.to_string())?
|
||||
.map_err(err_str)?
|
||||
.map(|p| p.created_at)
|
||||
.unwrap_or_else(now_millis),
|
||||
None => now_millis(),
|
||||
@@ -347,11 +347,11 @@ pub async fn ai_save_provider(
|
||||
// is_default:编辑保留原值;新建时若全表尚无默认则设为默认(首个自动默认,避免无默认可用)
|
||||
let is_default = match &id {
|
||||
Some(pid) => state.ai_providers.get_by_id(pid).await
|
||||
.map_err(|e| e.to_string())?
|
||||
.map_err(err_str)?
|
||||
.map(|p| p.is_default)
|
||||
.unwrap_or(false),
|
||||
None => !state.ai_providers.list_all().await
|
||||
.map_err(|e| e.to_string())?
|
||||
.map_err(err_str)?
|
||||
.iter().any(|p| p.is_default),
|
||||
};
|
||||
// FR-S1:密钥存 OS keyring,DB api_key 列恒空(不入明文)。
|
||||
@@ -369,7 +369,7 @@ pub async fn ai_save_provider(
|
||||
// 兜底:发现未迁移态先即时迁移补密钥,迁移成功后再让下方清 DB 明文(收敛到迁移完成态);
|
||||
// 迁移失败则 Err 阻断保存且 INSERT OR REPLACE 不执行 → DB 明文保留,绝不劣化现状。
|
||||
let old = state.ai_providers.get_by_id(pid).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
.map_err(err_str)?;
|
||||
if let Some(old) = old {
|
||||
if !old.api_key.is_empty()
|
||||
&& super::secret::get_provider_secret(pid).is_none()
|
||||
@@ -409,7 +409,7 @@ pub async fn ai_save_provider(
|
||||
.ai_providers
|
||||
.insert(record)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
.map_err(err_str)?;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
@@ -424,18 +424,18 @@ pub async fn ai_set_provider(
|
||||
.ai_providers
|
||||
.get_by_id(&provider_id)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
.map_err(err_str)?
|
||||
.ok_or_else(|| format!("提供商不存在: {}", provider_id))?;
|
||||
|
||||
// 互斥写 DB:目标 is_default=true,其余=false。仅写变化的记录。
|
||||
let providers = state.ai_providers.list_all().await.map_err(|e| e.to_string())?;
|
||||
let providers = state.ai_providers.list_all().await.map_err(err_str)?;
|
||||
for p in &providers {
|
||||
let should = p.id == provider_id;
|
||||
if p.is_default != should {
|
||||
let mut updated = p.clone();
|
||||
updated.is_default = should;
|
||||
updated.updated_at = now_millis();
|
||||
state.ai_providers.update_full(&updated).await.map_err(|e| e.to_string())?;
|
||||
state.ai_providers.update_full(&updated).await.map_err(err_str)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -450,7 +450,7 @@ pub async fn ai_delete_provider(
|
||||
state: State<'_, AppState>,
|
||||
provider_id: String,
|
||||
) -> Result<(), String> {
|
||||
state.ai_providers.delete(&provider_id).await.map_err(|e| e.to_string())?;
|
||||
state.ai_providers.delete(&provider_id).await.map_err(err_str)?;
|
||||
// CR-260615-01:DB 已删则清 keyring 残留密钥(失败仅 warn 不阻断——无 DB 消费方,
|
||||
// 残留 keyring 不可复活;同 id 复用也不会读到旧密钥,因 set 覆盖写)
|
||||
if let Err(e) = super::secret::delete_provider_secret(&provider_id) {
|
||||
@@ -518,7 +518,7 @@ pub async fn ai_conversation_list(
|
||||
) -> Result<Vec<serde_json::Value>, String> {
|
||||
let limit = limit.unwrap_or(50);
|
||||
let include_archived = include_archived.unwrap_or(false);
|
||||
let records = state.ai_conversations.list_all().await.map_err(|e| e.to_string())?;
|
||||
let records = state.ai_conversations.list_all().await.map_err(err_str)?;
|
||||
// list_all 已按 created_at DESC(最新在前);默认排除归档 + 截断 limit
|
||||
let summaries: Vec<serde_json::Value> = records.iter()
|
||||
.filter(|r| include_archived || !r.archived)
|
||||
@@ -551,7 +551,7 @@ pub async fn ai_conversation_switch(
|
||||
conversation_id: String,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
let record = state.ai_conversations.get_by_id(&conversation_id).await
|
||||
.map_err(|e| e.to_string())?
|
||||
.map_err(err_str)?
|
||||
.ok_or_else(|| format!("对话不存在: {}", conversation_id))?;
|
||||
|
||||
let messages: Vec<ChatMessage> = serde_json::from_str(&record.messages)
|
||||
@@ -591,7 +591,7 @@ pub async fn ai_conversation_delete(
|
||||
state: State<'_, AppState>,
|
||||
conversation_id: String,
|
||||
) -> Result<(), String> {
|
||||
state.ai_conversations.delete(&conversation_id).await.map_err(|e| e.to_string())?;
|
||||
state.ai_conversations.delete(&conversation_id).await.map_err(err_str)?;
|
||||
|
||||
let mut session = state.ai_session.lock().await;
|
||||
if session.active_conversation_id.as_deref() == Some(&conversation_id) {
|
||||
@@ -614,7 +614,7 @@ pub async fn ai_conversation_rename(
|
||||
return Err("标题不能为空".to_string());
|
||||
}
|
||||
state.ai_conversations.update_field(&conversation_id, "title", &title)
|
||||
.await.map_err(|e| e.to_string())?;
|
||||
.await.map_err(err_str)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -628,7 +628,7 @@ pub async fn ai_conversation_archive(
|
||||
state.ai_conversations
|
||||
.set_archived(&conversation_id, archived)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
.map_err(err_str)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user