feat(ai): support multiple configurations and models
This commit is contained in:
1 parent
5d2a363786
commit
dd3a2e30a4
29 files changed
+1728
-500
No files matched your search
@@ -8,6 +8,7 @@ use std::net::IpAddr;
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, LazyLock};
|
||||
use tokio::sync::{Notify, RwLock};
|
||||
use uuid::Uuid;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Stream cancel registry
|
||||
@@ -63,6 +64,22 @@ pub enum AiProvider {
|
||||
Custom,
|
||||
}
|
||||
|
||||
impl AiProvider {
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
AiProvider::Claude => "claude",
|
||||
AiProvider::Openai => "openai",
|
||||
AiProvider::Gemini => "gemini",
|
||||
AiProvider::Deepseek => "deepseek",
|
||||
AiProvider::Qwen => "qwen",
|
||||
AiProvider::Ollama => "ollama",
|
||||
AiProvider::OpenaiCompatible => "openai-compatible",
|
||||
AiProvider::CodexCli => "codex-cli",
|
||||
AiProvider::Custom => "custom",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum AiApiStyle {
|
||||
@@ -104,6 +121,31 @@ impl AiReasoningLevel {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AiConfigItem {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
#[serde(default)]
|
||||
pub is_default: bool,
|
||||
#[serde(flatten)]
|
||||
pub config: AiConfig,
|
||||
}
|
||||
|
||||
impl AiConfigItem {
|
||||
pub fn new_id() -> String {
|
||||
Uuid::new_v4().to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AiModelListItem {
|
||||
pub name: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub label: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AiConfig {
|
||||
@@ -117,6 +159,8 @@ pub struct AiConfig {
|
||||
#[serde(default)]
|
||||
pub model: String,
|
||||
#[serde(default)]
|
||||
pub models: Vec<AiModelListItem>,
|
||||
#[serde(default)]
|
||||
pub api_style: AiApiStyle,
|
||||
#[serde(default)]
|
||||
pub proxy_enabled: bool,
|
||||
@@ -2573,6 +2617,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
|
||||
model: "gpt-4o".to_string(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: true,
|
||||
proxy_url: "not a proxy url".to_string(),
|
||||
@@ -2596,6 +2641,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
|
||||
model: "gpt-4o".to_string(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: true,
|
||||
proxy_url: "127.0.0.1:7890".to_string(),
|
||||
@@ -2617,6 +2663,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: "http://127.0.0.1:3456/v1".to_string(),
|
||||
model: "gpt-4o".to_string(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: true,
|
||||
proxy_url: "not a proxy url".to_string(),
|
||||
@@ -2638,6 +2685,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::ApiKey,
|
||||
endpoint: "https://generativelanguage.googleapis.com".to_string(),
|
||||
model: "gemini-1.5-pro".to_string(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
@@ -2663,6 +2711,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: "http://localhost:11434/v1".to_string(),
|
||||
model: "llama3.1".to_string(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
@@ -2685,6 +2734,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
|
||||
model: String::new(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
@@ -2702,6 +2752,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::ApiKey,
|
||||
endpoint: "https://api.anthropic.com/v1/messages".to_string(),
|
||||
model: String::new(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
@@ -2722,6 +2773,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::ApiKey,
|
||||
endpoint: "https://gateway.example.com/anthropic/v1".to_string(),
|
||||
model: "claude-sonnet-4-20250514".to_string(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::AnthropicMessages,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
@@ -2766,6 +2818,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: "https://api.example.com".to_string(),
|
||||
model: "test-model".to_string(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
@@ -2822,6 +2875,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::ApiKey,
|
||||
endpoint: "https://api.anthropic.com/v1/messages".to_string(),
|
||||
model: "claude-sonnet-4-20250514".to_string(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
@@ -3110,6 +3164,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
|
||||
model: "gpt-5.5".to_string(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
@@ -3175,6 +3230,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: "https://api.moonshot.cn/v1".to_string(),
|
||||
model: "kimi-k2.5".to_string(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
@@ -3205,6 +3261,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
|
||||
model: "gpt-5".to_string(),
|
||||
models: vec![],
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
@@ -3241,6 +3298,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: "https://example.com/v1".to_string(),
|
||||
model: "qwen3".to_string(),
|
||||
models: vec![],
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
@@ -3271,6 +3329,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: "http://localhost:11434/v1".to_string(),
|
||||
model: "deepseek-r1:14b".to_string(),
|
||||
models: vec![],
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
|
||||
@@ -605,6 +605,7 @@ mod tests {
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: String::new(),
|
||||
model: model.to_string(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
|
||||
@@ -9,7 +9,7 @@ use reqwest::{header, Client, Method, StatusCode, Url};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::ai::AiConfig;
|
||||
use crate::ai::AiConfigItem;
|
||||
use crate::connection_secrets::{
|
||||
MQ_AUTH_API_KEY_VALUE_KEY, MQ_AUTH_CLIENT_SECRET_KEY, MQ_AUTH_PASSWORD_KEY, MQ_AUTH_TOKEN_KEY,
|
||||
MQ_TOKEN_SIGNING_KEY, NACOS_AUTH_PASSWORD_KEY,
|
||||
@@ -119,7 +119,12 @@ pub struct EncryptedSecretsBlob {
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SensitiveSyncPayload {
|
||||
pub connection_secrets: Vec<ConnectionSecretSnapshot>,
|
||||
pub ai_config: Option<AiConfig>,
|
||||
// None = legacy snapshot (fall through to ai_config migration),
|
||||
// Some(vec) = explicit state (empty vec means all configs were deleted)
|
||||
pub ai_configs: Option<Vec<AiConfigItem>>,
|
||||
/// Legacy field, used only for deserializing old data; not serialized
|
||||
#[serde(default, skip_serializing)]
|
||||
pub ai_config: Option<crate::ai::AiConfig>,
|
||||
/// Full tunnel profiles including their secrets.
|
||||
#[serde(default)]
|
||||
pub tunnel_profiles: Option<Vec<TransportLayerConfig>>,
|
||||
@@ -664,7 +669,8 @@ async fn build_sensitive_payload(
|
||||
|
||||
Ok(SensitiveSyncPayload {
|
||||
connection_secrets,
|
||||
ai_config: storage.load_ai_config().await?,
|
||||
ai_configs: Some(storage.load_ai_configs().await.unwrap_or_default()),
|
||||
ai_config: None,
|
||||
tunnel_profiles: Some(tunnel_profiles.to_vec()),
|
||||
})
|
||||
}
|
||||
@@ -784,8 +790,19 @@ async fn apply_sensitive_payload(storage: &Storage, payload: &SensitiveSyncPaylo
|
||||
}
|
||||
storage.set_secret(&secret.connection_id, &secret.key, &secret.secret).await?;
|
||||
}
|
||||
if let Some(ai_config) = &payload.ai_config {
|
||||
storage.save_ai_config(ai_config).await?;
|
||||
if let Some(configs) = &payload.ai_configs {
|
||||
// New format: save directly (empty = all configs were deleted)
|
||||
storage.save_ai_configs(configs).await?;
|
||||
} else if let Some(old_config) = &payload.ai_config {
|
||||
// Legacy format: migrate then save
|
||||
let provider_name = old_config.provider.as_str().to_string();
|
||||
let item = AiConfigItem {
|
||||
id: AiConfigItem::new_id(),
|
||||
name: provider_name,
|
||||
is_default: true,
|
||||
config: old_config.clone(),
|
||||
};
|
||||
storage.save_ai_config_item(&item).await?;
|
||||
}
|
||||
if let Some(profiles) = &payload.tunnel_profiles {
|
||||
storage.save_tunnel_profiles(profiles).await?;
|
||||
@@ -1024,18 +1041,43 @@ fn parent_collection_paths(remote_path: &str) -> Vec<String> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
apply_sync_snapshot, build_sync_snapshot, build_sync_snapshot_with_saved_secrets, decrypt_sensitive_payload,
|
||||
encrypt_sensitive_payload, forget_webdav_sync_secrets_passphrase, gitee_snippet_payload,
|
||||
normalized_remote_path, parent_collection_paths, resolve_webdav_sync_secrets_passphrase,
|
||||
apply_sensitive_payload, apply_sync_snapshot, build_sync_snapshot, build_sync_snapshot_with_saved_secrets,
|
||||
decrypt_sensitive_payload, encrypt_sensitive_payload, forget_webdav_sync_secrets_passphrase,
|
||||
gitee_snippet_payload, normalized_remote_path, parent_collection_paths, resolve_webdav_sync_secrets_passphrase,
|
||||
save_webdav_sync_secrets_preference, scrub_connection_secrets, snippet_file_content, snippet_response_id,
|
||||
webdav_sync_secrets_status, ApplySnapshotOptions, ConnectionSecretSnapshot, SensitiveSyncPayload,
|
||||
};
|
||||
use crate::ai::{AiApiStyle, AiAuthMethod, AiConfig, AiConfigItem};
|
||||
use crate::connection_secrets::NACOS_AUTH_PASSWORD_KEY;
|
||||
use crate::models::connection::{
|
||||
default_redis_key_separator, ConnectionConfig, DatabaseType, SshTunnelConfig, TransportLayerConfig,
|
||||
};
|
||||
use crate::storage::Storage;
|
||||
|
||||
fn make_test_config(name: &str, is_default: bool) -> AiConfigItem {
|
||||
AiConfigItem {
|
||||
id: format!("cfg-{name}"),
|
||||
name: name.to_string(),
|
||||
is_default,
|
||||
config: AiConfig {
|
||||
provider: crate::ai::AiProvider::Openai,
|
||||
api_key: String::new(),
|
||||
auth_method: AiAuthMethod::Bearer,
|
||||
endpoint: "https://api.openai.com/v1".to_string(),
|
||||
model: "gpt-4o-mini".to_string(),
|
||||
models: vec![],
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
enable_thinking: true,
|
||||
reasoning_level: crate::ai::AiReasoningLevel::Default,
|
||||
context_window: None,
|
||||
codex_cli_path: None,
|
||||
codex_cli_env: Default::default(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn temp_db_path(name: &str) -> std::path::PathBuf {
|
||||
std::env::temp_dir().join(format!("dbx-cloud-sync-{name}-{}.db", uuid::Uuid::new_v4()))
|
||||
}
|
||||
@@ -1310,6 +1352,7 @@ mod tests {
|
||||
secret: "hop-secret".to_string(),
|
||||
},
|
||||
],
|
||||
ai_configs: None,
|
||||
ai_config: None,
|
||||
};
|
||||
let encrypted = encrypt_sensitive_payload(&payload, "sync-pass").unwrap();
|
||||
@@ -1328,6 +1371,7 @@ mod tests {
|
||||
key: "password".to_string(),
|
||||
secret: "secret".to_string(),
|
||||
}],
|
||||
ai_configs: None,
|
||||
ai_config: None,
|
||||
};
|
||||
let encrypted = encrypt_sensitive_payload(&payload, "sync-pass").unwrap();
|
||||
@@ -1478,4 +1522,59 @@ mod tests {
|
||||
.unwrap();
|
||||
assert_eq!(target.load_tunnel_profiles().await.unwrap(), vec![profile]);
|
||||
}
|
||||
|
||||
// ---- AI configs sync tests ----
|
||||
|
||||
#[tokio::test]
|
||||
async fn sensitive_payload_ai_configs_none_falls_through_to_legacy() {
|
||||
let storage = Storage::open(&temp_db_path("ai-cfg-none")).await.unwrap();
|
||||
|
||||
// No ai_configs in payload — fall through to ai_config (legacy) branch
|
||||
let payload = SensitiveSyncPayload {
|
||||
connection_secrets: vec![],
|
||||
ai_configs: None,
|
||||
ai_config: None,
|
||||
tunnel_profiles: None,
|
||||
};
|
||||
apply_sensitive_payload(&storage, &payload).await.unwrap();
|
||||
let loaded = storage.load_ai_configs().await.unwrap();
|
||||
assert!(loaded.is_empty(), "None → no configs written");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn sensitive_payload_ai_configs_empty_clears_table() {
|
||||
let storage = Storage::open(&temp_db_path("ai-cfg-empty")).await.unwrap();
|
||||
|
||||
// Pre-populate with a config
|
||||
let cfg = make_test_config("to-be-cleared", true);
|
||||
storage.save_ai_config_item(&cfg).await.unwrap();
|
||||
|
||||
// Some([]) — explicit clear
|
||||
let payload = SensitiveSyncPayload {
|
||||
connection_secrets: vec![],
|
||||
ai_configs: Some(vec![]),
|
||||
ai_config: None,
|
||||
tunnel_profiles: None,
|
||||
};
|
||||
apply_sensitive_payload(&storage, &payload).await.unwrap();
|
||||
let loaded = storage.load_ai_configs().await.unwrap();
|
||||
assert!(loaded.is_empty(), "Some([]) → table cleared");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn sensitive_payload_ai_configs_some_saves_configs() {
|
||||
let storage = Storage::open(&temp_db_path("ai-cfg-some")).await.unwrap();
|
||||
|
||||
let cfg = make_test_config("synced", true);
|
||||
let payload = SensitiveSyncPayload {
|
||||
connection_secrets: vec![],
|
||||
ai_configs: Some(vec![cfg]),
|
||||
ai_config: None,
|
||||
tunnel_profiles: None,
|
||||
};
|
||||
apply_sensitive_payload(&storage, &payload).await.unwrap();
|
||||
let loaded = storage.load_ai_configs().await.unwrap();
|
||||
assert_eq!(loaded.len(), 1);
|
||||
assert_eq!(loaded[0].name, "synced");
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ use rusqlite::{params, params_from_iter, Connection, DatabaseName, OpenFlags, Op
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::ai::{AiChatMessage, AiConfig, AiConversation, AiProvider};
|
||||
use crate::ai::{AiChatMessage, AiConfig, AiConfigItem, AiConversation, AiProvider};
|
||||
use crate::connection_secrets::{
|
||||
MQ_AUTH_API_KEY_VALUE_KEY, MQ_AUTH_CLIENT_SECRET_KEY, MQ_AUTH_PASSWORD_KEY, MQ_AUTH_SECRET_PREFIX,
|
||||
MQ_AUTH_TOKEN_KEY, MQ_TOKEN_SIGNING_KEY, MQ_TOKEN_SIGNING_SECRET_PREFIX, NACOS_AUTH_PASSWORD_KEY,
|
||||
@@ -332,6 +332,14 @@ const SCHEMA_STATEMENTS: &[&str] = &[
|
||||
created_at TEXT NOT NULL DEFAULT '',
|
||||
updated_at TEXT NOT NULL DEFAULT ''
|
||||
)",
|
||||
"CREATE TABLE IF NOT EXISTS ai_configs (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
model TEXT NOT NULL DEFAULT '',
|
||||
models TEXT NOT NULL DEFAULT '[]',
|
||||
config_json TEXT NOT NULL,
|
||||
is_default INTEGER NOT NULL DEFAULT 0
|
||||
)",
|
||||
];
|
||||
|
||||
impl Storage {
|
||||
@@ -351,6 +359,7 @@ impl Storage {
|
||||
ensure_history_columns_sync(conn)?;
|
||||
ensure_saved_sql_columns_sync(conn)?;
|
||||
ensure_tab_runtime_cache_columns_sync(conn)?;
|
||||
ensure_ai_configs_columns_sync(conn)?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
@@ -484,6 +493,24 @@ fn unix_timestamp_millis() -> i64 {
|
||||
SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis().min(i64::MAX as u128) as i64
|
||||
}
|
||||
|
||||
fn ensure_ai_configs_columns_sync(conn: &Connection) -> Result<(), String> {
|
||||
const COLUMNS: &[(&str, &str)] = &[
|
||||
("model", "TEXT NOT NULL DEFAULT ''"),
|
||||
("models", "TEXT NOT NULL DEFAULT '[]'"),
|
||||
("is_default", "INTEGER NOT NULL DEFAULT 0"),
|
||||
];
|
||||
|
||||
ensure_table_columns(conn, "ai_configs", COLUMNS)?;
|
||||
|
||||
// Create partial unique index (if not exists)
|
||||
conn.execute(
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS idx_ai_configs_default ON ai_configs(is_default) WHERE is_default = 1",
|
||||
[],
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
fn ensure_table_columns(conn: &Connection, table_name: &str, columns: &[(&str, &str)]) -> Result<(), String> {
|
||||
let mut stmt =
|
||||
conn.prepare(&format!("SELECT name FROM pragma_table_info('{table_name}')")).map_err(|e| e.to_string())?;
|
||||
@@ -815,6 +842,136 @@ impl Storage {
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn save_ai_configs(&self, configs: &[AiConfigItem]) -> Result<(), String> {
|
||||
let configs = configs.to_vec();
|
||||
self.with_conn(move |conn| {
|
||||
let tx = conn.transaction().map_err(|e| e.to_string())?;
|
||||
tx.execute("DELETE FROM ai_configs", []).map_err(|e| e.to_string())?;
|
||||
for config in &configs {
|
||||
let json = serde_json::to_string(&config.config).map_err(|e| e.to_string())?;
|
||||
let models_json = serde_json::to_string(&config.config.models).map_err(|e| e.to_string())?;
|
||||
tx.execute(
|
||||
"INSERT OR REPLACE INTO ai_configs (id, name, model, models, config_json, is_default) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
|
||||
params![config.id, config.name, config.config.model, models_json, json, config.is_default as i32],
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
// Clear old single-config tables — migration is complete, avoids re-migration on empty ai_configs
|
||||
tx.execute("DELETE FROM ai_config", []).map_err(|e| e.to_string())?;
|
||||
tx.execute("DELETE FROM ai_provider_configs", []).map_err(|e| e.to_string())?;
|
||||
tx.commit().map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn load_ai_configs(&self) -> Result<Vec<AiConfigItem>, String> {
|
||||
self.with_conn(|conn| {
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT id, name, model, models, config_json, is_default FROM ai_configs")
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rows = stmt
|
||||
.query_map([], |row| {
|
||||
Ok((
|
||||
row.get::<_, String>(0)?,
|
||||
row.get::<_, String>(1)?,
|
||||
row.get::<_, String>(2)?,
|
||||
row.get::<_, String>(3)?,
|
||||
row.get::<_, String>(4)?,
|
||||
row.get::<_, bool>(5)?,
|
||||
))
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut configs = Vec::new();
|
||||
for row in rows {
|
||||
let (id, name, model_col, models_json_col, json, is_default_col) = row.map_err(|e| e.to_string())?;
|
||||
match serde_json::from_str::<AiConfig>(&json) {
|
||||
Ok(mut config) => {
|
||||
// 优先使用列值,如果列值为空则从 config_json 回退读取
|
||||
if model_col.is_empty() {
|
||||
// config.model 已经从 json 解析出来了
|
||||
} else {
|
||||
config.model = model_col;
|
||||
}
|
||||
if models_json_col.is_empty() || models_json_col == "[]" {
|
||||
// config.models 已经从 json 解析出来了
|
||||
} else {
|
||||
config.models = serde_json::from_str(&models_json_col).unwrap_or_default();
|
||||
}
|
||||
let is_default = is_default_col;
|
||||
configs.push(AiConfigItem { id, name, is_default, config });
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to deserialize AI config item '{}': {}", id, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(configs)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn set_default_ai_config(&self, config_id: &str) -> Result<(), String> {
|
||||
let config_id = config_id.to_string();
|
||||
self.with_conn(move |conn| {
|
||||
let tx = conn.transaction().map_err(|e| e.to_string())?;
|
||||
tx.execute("UPDATE ai_configs SET is_default = 0 WHERE is_default = 1", []).map_err(|e| e.to_string())?;
|
||||
tx.execute("UPDATE ai_configs SET is_default = 1 WHERE id = ?1", params![config_id])
|
||||
.map_err(|e| e.to_string())?;
|
||||
tx.commit().map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn save_ai_config_item(&self, config: &AiConfigItem) -> Result<(), String> {
|
||||
let config = config.clone();
|
||||
self.with_conn(move |conn| {
|
||||
let json = serde_json::to_string(&config.config).map_err(|e| e.to_string())?;
|
||||
let models_json = serde_json::to_string(&config.config.models).map_err(|e| e.to_string())?;
|
||||
let tx = conn.transaction().map_err(|e| e.to_string())?;
|
||||
|
||||
// 如果设该配置为默认,先清除其他默认,避免与 idx_ai_configs_default 冲突
|
||||
if config.is_default {
|
||||
tx.execute(
|
||||
"UPDATE ai_configs SET is_default = 0 WHERE is_default = 1 AND id != ?1",
|
||||
params![config.id],
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
tx.execute(
|
||||
"INSERT INTO ai_configs (id, name, model, models, config_json, is_default)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6)
|
||||
ON CONFLICT(id) DO UPDATE SET name = excluded.name, model = excluded.model,
|
||||
models = excluded.models, config_json = excluded.config_json, is_default = excluded.is_default",
|
||||
params![config.id, config.name, config.config.model, models_json, json, config.is_default as i32],
|
||||
)
|
||||
.map_err(|e| {
|
||||
let msg = e.to_string();
|
||||
// SQLite UNIQUE constraint error contains the table and column name
|
||||
if msg.contains("UNIQUE constraint failed") && msg.contains("ai_configs.name") {
|
||||
format!("ai.configNameExists:{}", config.name)
|
||||
} else {
|
||||
msg
|
||||
}
|
||||
})?;
|
||||
|
||||
tx.commit().map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_ai_config(&self, config_id: &str) -> Result<(), String> {
|
||||
let config_id = config_id.to_string();
|
||||
self.with_conn(move |conn| {
|
||||
conn.execute("DELETE FROM ai_configs WHERE id = ?1", params![config_id]).map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
// Tunnel profiles — shared transport-layer configurations managed in
|
||||
@@ -3592,4 +3749,194 @@ mod tests {
|
||||
assert_eq!(loaded.open_count, 1);
|
||||
assert_eq!(loaded.sql, "SELECT 1;");
|
||||
}
|
||||
|
||||
// ---- AI Config tests ----
|
||||
|
||||
use crate::ai::{AiApiStyle, AiAuthMethod, AiConfig, AiConfigItem, AiProvider, AiReasoningLevel};
|
||||
|
||||
fn make_ai_config(name: &str, is_default: bool) -> AiConfigItem {
|
||||
AiConfigItem {
|
||||
id: format!("cfg-{name}"),
|
||||
name: name.to_string(),
|
||||
is_default,
|
||||
config: AiConfig {
|
||||
provider: AiProvider::Openai,
|
||||
api_key: "sk-test".to_string(),
|
||||
auth_method: AiAuthMethod::ApiKey,
|
||||
endpoint: "https://api.openai.com/v1".to_string(),
|
||||
model: "gpt-4o".to_string(),
|
||||
models: Vec::new(),
|
||||
api_style: AiApiStyle::Completions,
|
||||
proxy_enabled: false,
|
||||
proxy_url: String::new(),
|
||||
enable_thinking: true,
|
||||
reasoning_level: AiReasoningLevel::Default,
|
||||
context_window: None,
|
||||
codex_cli_path: None,
|
||||
codex_cli_env: std::collections::HashMap::new(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ai_config_save_load_roundtrip() {
|
||||
let db = temp_db_path("ai-roundtrip");
|
||||
let storage = Storage::open(&db).await.unwrap();
|
||||
|
||||
let cfg = make_ai_config("test-config", true);
|
||||
storage.save_ai_config_item(&cfg).await.unwrap();
|
||||
|
||||
let loaded = storage.load_ai_configs().await.unwrap();
|
||||
assert_eq!(loaded.len(), 1);
|
||||
assert_eq!(loaded[0].id, "cfg-test-config");
|
||||
assert_eq!(loaded[0].name, "test-config");
|
||||
assert!(loaded[0].is_default);
|
||||
assert_eq!(loaded[0].config.model, "gpt-4o");
|
||||
|
||||
std::fs::remove_file(&db).ok();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ai_config_only_one_default() {
|
||||
let db = temp_db_path("ai-one-default");
|
||||
let storage = Storage::open(&db).await.unwrap();
|
||||
|
||||
let cfg1 = make_ai_config("config-a", true);
|
||||
let cfg2 = make_ai_config("config-b", true);
|
||||
storage.save_ai_config_item(&cfg1).await.unwrap();
|
||||
|
||||
// Second default config should succeed and cascade-clear the first
|
||||
storage.save_ai_config_item(&cfg2).await.unwrap();
|
||||
|
||||
let loaded = storage.load_ai_configs().await.unwrap();
|
||||
let defaults: Vec<_> = loaded.iter().filter(|c| c.is_default).collect();
|
||||
assert_eq!(defaults.len(), 1);
|
||||
assert_eq!(defaults[0].id, "cfg-config-b");
|
||||
|
||||
std::fs::remove_file(&db).ok();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ai_config_update_existing_to_default() {
|
||||
let db = temp_db_path("ai-update-default");
|
||||
let storage = Storage::open(&db).await.unwrap();
|
||||
|
||||
let cfg1 = make_ai_config("config-a", true);
|
||||
let cfg2 = make_ai_config("config-b", false);
|
||||
storage.save_ai_config_item(&cfg1).await.unwrap();
|
||||
storage.save_ai_config_item(&cfg2).await.unwrap();
|
||||
assert_eq!(storage.load_ai_configs().await.unwrap().iter().filter(|c| c.is_default).count(), 1);
|
||||
|
||||
// Update cfg-b to be default via save_ai_config_item — should succeed and clear cfg-a
|
||||
let cfg2 = make_ai_config("config-b", true);
|
||||
storage.save_ai_config_item(&cfg2).await.unwrap();
|
||||
|
||||
let loaded = storage.load_ai_configs().await.unwrap();
|
||||
let defaults: Vec<_> = loaded.iter().filter(|c| c.is_default).collect();
|
||||
assert_eq!(defaults.len(), 1);
|
||||
assert_eq!(defaults[0].id, "cfg-config-b");
|
||||
|
||||
std::fs::remove_file(&db).ok();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ai_config_duplicate_name_error() {
|
||||
let db = temp_db_path("ai-dup-name");
|
||||
let storage = Storage::open(&db).await.unwrap();
|
||||
|
||||
let cfg1 = make_ai_config("same-name", false);
|
||||
storage.save_ai_config_item(&cfg1).await.unwrap();
|
||||
|
||||
// Different id, same name → should fail with name conflict
|
||||
let mut cfg2 = make_ai_config("same-name", false);
|
||||
cfg2.id = "cfg-other".to_string();
|
||||
let err = storage.save_ai_config_item(&cfg2).await.unwrap_err();
|
||||
assert!(err.contains("ai.configNameExists"), "Expected name conflict error, got: {err}");
|
||||
|
||||
std::fs::remove_file(&db).ok();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ai_config_set_default_switches() {
|
||||
let db = temp_db_path("ai-set-default");
|
||||
let storage = Storage::open(&db).await.unwrap();
|
||||
|
||||
let cfg1 = make_ai_config("first", true);
|
||||
let cfg2 = make_ai_config("second", false);
|
||||
storage.save_ai_config_item(&cfg1).await.unwrap();
|
||||
storage.save_ai_config_item(&cfg2).await.unwrap();
|
||||
|
||||
// Switch default to second
|
||||
storage.set_default_ai_config("cfg-second").await.unwrap();
|
||||
|
||||
let loaded = storage.load_ai_configs().await.unwrap();
|
||||
let first = loaded.iter().find(|c| c.id == "cfg-first").unwrap();
|
||||
let second = loaded.iter().find(|c| c.id == "cfg-second").unwrap();
|
||||
assert!(!first.is_default);
|
||||
assert!(second.is_default);
|
||||
|
||||
std::fs::remove_file(&db).ok();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ai_config_delete_default_no_cascade() {
|
||||
let db = temp_db_path("ai-delete-default");
|
||||
let storage = Storage::open(&db).await.unwrap();
|
||||
|
||||
let cfg1 = make_ai_config("default-one", true);
|
||||
let cfg2 = make_ai_config("other", false);
|
||||
storage.save_ai_config_item(&cfg1).await.unwrap();
|
||||
storage.save_ai_config_item(&cfg2).await.unwrap();
|
||||
|
||||
// Delete the default config
|
||||
storage.delete_ai_config("cfg-default-one").await.unwrap();
|
||||
|
||||
let loaded = storage.load_ai_configs().await.unwrap();
|
||||
assert_eq!(loaded.len(), 1);
|
||||
// Remaining config should NOT be auto-promoted to default
|
||||
assert!(!loaded[0].is_default);
|
||||
|
||||
std::fs::remove_file(&db).ok();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ai_config_save_configs_batch() {
|
||||
let db = temp_db_path("ai-batch");
|
||||
let storage = Storage::open(&db).await.unwrap();
|
||||
|
||||
let configs =
|
||||
vec![make_ai_config("batch-a", true), make_ai_config("batch-b", false), make_ai_config("batch-c", false)];
|
||||
storage.save_ai_configs(&configs).await.unwrap();
|
||||
|
||||
let loaded = storage.load_ai_configs().await.unwrap();
|
||||
assert_eq!(loaded.len(), 3);
|
||||
|
||||
std::fs::remove_file(&db).ok();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ai_config_save_configs_clears_old_tables() {
|
||||
let db = temp_db_path("ai-clear-old");
|
||||
let storage = Storage::open(&db).await.unwrap();
|
||||
|
||||
// Pre-populate old tables as if migration hasn't run yet
|
||||
storage.save_ai_config(&make_ai_config("legacy-active", false).config).await.unwrap();
|
||||
storage.save_ai_provider_config("openai", &make_ai_config("legacy-openai", false).config).await.unwrap();
|
||||
|
||||
// save_ai_configs should clear old tables
|
||||
let configs = vec![make_ai_config("new-a", true)];
|
||||
storage.save_ai_configs(&configs).await.unwrap();
|
||||
|
||||
// New table has the saved config
|
||||
let loaded = storage.load_ai_configs().await.unwrap();
|
||||
assert_eq!(loaded.len(), 1);
|
||||
assert_eq!(loaded[0].name, "new-a");
|
||||
|
||||
// Old tables are cleared — prevents re-migration on restart
|
||||
assert!(storage.load_ai_config().await.unwrap().is_none(), "ai_config should be deleted");
|
||||
let old_providers = storage.load_ai_provider_configs().await.unwrap();
|
||||
assert!(old_providers.is_empty(), "ai_provider_configs should be deleted");
|
||||
|
||||
std::fs::remove_file(&db).ok();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user