feat(settings): add model provider management with full CRUD support

Add a new settings page for managing API model providers (name, API URL,
API key, applicable agent types). Includes database migration, SeaORM
entity, backend CRUD commands/handlers, frontend settings UI with agent
type filter, add/edit/delete dialogs, and i18n support for all 10 locales.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
xintaofei
2026-04-05 16:35:14 +08:00
parent 6359651247
commit ba19299696
32 changed files with 1501 additions and 11 deletions

View File

@@ -4,6 +4,7 @@ pub mod conversations;
pub mod folder_commands;
pub mod folders;
pub mod mcp;
pub mod model_provider;
pub mod project_boot;
pub mod system_settings;
pub mod terminal;

View File

@@ -0,0 +1,171 @@
use crate::app_error::AppCommandError;
use crate::db::service::model_provider_service;
use crate::db::AppDatabase;
use crate::models::agent::AgentType;
use crate::models::model_provider::ModelProviderInfo;
// ---------------------------------------------------------------------------
// Shared core functions (used by both Tauri commands and web handlers)
// ---------------------------------------------------------------------------
fn validate_agent_types(agent_types: &[String]) -> Result<(), AppCommandError> {
if agent_types.is_empty() {
return Err(AppCommandError::invalid_input(
"At least one agent type is required",
));
}
for at in agent_types {
let _: AgentType = serde_json::from_value(serde_json::Value::String(at.clone()))
.map_err(|_| {
AppCommandError::invalid_input(format!("Invalid agent type: {at}"))
})?;
}
Ok(())
}
fn validate_fields(
name: Option<&str>,
api_url: Option<&str>,
api_key: Option<&str>,
) -> Result<(), AppCommandError> {
if let Some(n) = name {
if n.len() > 256 {
return Err(AppCommandError::invalid_input("Name must be 256 characters or less"));
}
}
if let Some(u) = api_url {
if u.len() > 2048 {
return Err(AppCommandError::invalid_input("API URL must be 2048 characters or less"));
}
if !u.starts_with("http://") && !u.starts_with("https://") {
return Err(AppCommandError::invalid_input("API URL must start with http:// or https://"));
}
}
if let Some(k) = api_key {
if k.len() > 4096 {
return Err(AppCommandError::invalid_input("API Key must be 4096 characters or less"));
}
}
Ok(())
}
pub async fn list_model_providers_core(
db: &AppDatabase,
) -> Result<Vec<ModelProviderInfo>, AppCommandError> {
let rows = model_provider_service::list_all(&db.conn)
.await
.map_err(AppCommandError::from)?;
Ok(rows.into_iter().map(ModelProviderInfo::from).collect())
}
pub async fn create_model_provider_core(
db: &AppDatabase,
name: String,
api_url: String,
api_key: String,
agent_types: Vec<String>,
) -> Result<ModelProviderInfo, AppCommandError> {
validate_fields(Some(&name), Some(&api_url), Some(&api_key))?;
validate_agent_types(&agent_types)?;
let agent_types_json = serde_json::to_string(&agent_types)
.map_err(|e| AppCommandError::invalid_input(e.to_string()))?;
let model = model_provider_service::create(
&db.conn,
name,
api_url,
api_key,
agent_types_json,
)
.await
.map_err(AppCommandError::from)?;
Ok(ModelProviderInfo::from(model))
}
pub async fn update_model_provider_core(
db: &AppDatabase,
id: i32,
name: Option<String>,
api_url: Option<String>,
api_key: Option<String>,
agent_types: Option<Vec<String>>,
) -> Result<ModelProviderInfo, AppCommandError> {
validate_fields(name.as_deref(), api_url.as_deref(), api_key.as_deref())?;
let agent_types_json = if let Some(ref ats) = agent_types {
validate_agent_types(ats)?;
Some(
serde_json::to_string(ats)
.map_err(|e| AppCommandError::invalid_input(e.to_string()))?,
)
} else {
None
};
let model = model_provider_service::update(
&db.conn,
id,
name,
api_url,
api_key,
agent_types_json,
)
.await
.map_err(AppCommandError::from)?;
Ok(ModelProviderInfo::from(model))
}
pub async fn delete_model_provider_core(
db: &AppDatabase,
id: i32,
) -> Result<(), AppCommandError> {
model_provider_service::delete(&db.conn, id)
.await
.map_err(AppCommandError::from)?;
Ok(())
}
// ---------------------------------------------------------------------------
// Tauri commands
// ---------------------------------------------------------------------------
#[cfg(feature = "tauri-runtime")]
#[tauri::command]
pub async fn list_model_providers(
db: tauri::State<'_, AppDatabase>,
) -> Result<Vec<ModelProviderInfo>, AppCommandError> {
list_model_providers_core(&db).await
}
#[cfg(feature = "tauri-runtime")]
#[tauri::command]
pub async fn create_model_provider(
db: tauri::State<'_, AppDatabase>,
name: String,
api_url: String,
api_key: String,
agent_types: Vec<String>,
) -> Result<ModelProviderInfo, AppCommandError> {
create_model_provider_core(&db, name, api_url, api_key, agent_types).await
}
#[cfg(feature = "tauri-runtime")]
#[tauri::command]
pub async fn update_model_provider(
db: tauri::State<'_, AppDatabase>,
id: i32,
name: Option<String>,
api_url: Option<String>,
api_key: Option<String>,
agent_types: Option<Vec<String>>,
) -> Result<ModelProviderInfo, AppCommandError> {
update_model_provider_core(&db, id, name, api_url, api_key, agent_types).await
}
#[cfg(feature = "tauri-runtime")]
#[tauri::command]
pub async fn delete_model_provider(
db: tauri::State<'_, AppDatabase>,
id: i32,
) -> Result<(), AppCommandError> {
delete_model_provider_core(&db, id).await
}

View File

@@ -7,4 +7,5 @@ pub mod conversation;
pub mod folder;
pub mod folder_command;
pub mod folder_opened_conversation;
pub mod model_provider;
pub mod prelude;

View File

@@ -0,0 +1,19 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model_provider")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub name: String,
pub api_url: String,
pub api_key: String,
pub agent_types_json: String,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -9,3 +9,4 @@ pub use super::conversation::Entity as Conversation;
pub use super::folder::Entity as Folder;
pub use super::folder_command::Entity as FolderCommand;
pub use super::folder_opened_conversation::Entity as FolderOpenedConversation;
pub use super::model_provider::Entity as ModelProvider;

View File

@@ -0,0 +1,61 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(ModelProvider::Table)
.if_not_exists()
.col(
ColumnDef::new(ModelProvider::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(ModelProvider::Name).string().not_null())
.col(ColumnDef::new(ModelProvider::ApiUrl).text().not_null())
.col(ColumnDef::new(ModelProvider::ApiKey).text().not_null())
.col(
ColumnDef::new(ModelProvider::AgentTypesJson)
.text()
.not_null(),
)
.col(
ColumnDef::new(ModelProvider::CreatedAt)
.timestamp_with_time_zone()
.not_null(),
)
.col(
ColumnDef::new(ModelProvider::UpdatedAt)
.timestamp_with_time_zone()
.not_null(),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(ModelProvider::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum ModelProvider {
Table,
Id,
Name,
ApiUrl,
ApiKey,
AgentTypesJson,
CreatedAt,
UpdatedAt,
}

View File

@@ -7,6 +7,7 @@ mod m20260226_000001_agent_setting;
mod m20260227_000001_folder_parent_branch;
mod m20260330_000001_chat_channel;
mod m20260401_000001_chat_channel_sender_context;
mod m20260404_000001_model_provider;
pub struct Migrator;
#[async_trait::async_trait]
@@ -20,6 +21,7 @@ impl MigratorTrait for Migrator {
Box::new(m20260227_000001_folder_parent_branch::Migration),
Box::new(m20260330_000001_chat_channel::Migration),
Box::new(m20260401_000001_chat_channel_sender_context::Migration),
Box::new(m20260404_000001_model_provider::Migration),
]
}
}

View File

@@ -6,4 +6,5 @@ pub mod conversation_service;
pub mod folder_command_service;
pub mod folder_service;
pub mod import_service;
pub mod model_provider_service;
pub mod sender_context_service;

View File

@@ -0,0 +1,77 @@
use chrono::Utc;
use sea_orm::{
ActiveModelTrait, ActiveValue::NotSet, DatabaseConnection, EntityTrait, IntoActiveModel,
QueryOrder, Set,
};
use crate::db::entities::model_provider;
use crate::db::error::DbError;
pub async fn create(
conn: &DatabaseConnection,
name: String,
api_url: String,
api_key: String,
agent_types_json: String,
) -> Result<model_provider::Model, DbError> {
let now = Utc::now();
let active = model_provider::ActiveModel {
id: NotSet,
name: Set(name),
api_url: Set(api_url),
api_key: Set(api_key),
agent_types_json: Set(agent_types_json),
created_at: Set(now),
updated_at: Set(now),
};
Ok(active.insert(conn).await?)
}
pub async fn update(
conn: &DatabaseConnection,
id: i32,
name: Option<String>,
api_url: Option<String>,
api_key: Option<String>,
agent_types_json: Option<String>,
) -> Result<model_provider::Model, DbError> {
let model = model_provider::Entity::find_by_id(id)
.one(conn)
.await?
.ok_or_else(|| DbError::Migration(format!("model provider not found: {id}")))?;
let mut active = model.into_active_model();
if let Some(v) = name {
active.name = Set(v);
}
if let Some(v) = api_url {
active.api_url = Set(v);
}
if let Some(v) = api_key {
active.api_key = Set(v);
}
if let Some(v) = agent_types_json {
active.agent_types_json = Set(v);
}
active.updated_at = Set(Utc::now());
Ok(active.update(conn).await?)
}
pub async fn delete(conn: &DatabaseConnection, id: i32) -> Result<(), DbError> {
model_provider::Entity::delete_by_id(id).exec(conn).await?;
Ok(())
}
pub async fn get_by_id(
conn: &DatabaseConnection,
id: i32,
) -> Result<Option<model_provider::Model>, DbError> {
Ok(model_provider::Entity::find_by_id(id).one(conn).await?)
}
pub async fn list_all(conn: &DatabaseConnection) -> Result<Vec<model_provider::Model>, DbError> {
Ok(model_provider::Entity::find()
.order_by_asc(model_provider::Column::Id)
.all(conn)
.await?)
}

View File

@@ -21,7 +21,8 @@ mod tauri_app {
use crate::chat_channel::manager::ChatChannelManager;
use crate::commands::{
acp as acp_commands, chat_channel as chat_channel_commands, conversations, folder_commands,
folders, mcp as mcp_commands, notification, project_boot, system_settings,
folders, mcp as mcp_commands, model_provider as model_provider_commands, notification,
project_boot, system_settings,
terminal as terminal_commands, version_control, windows,
};
use crate::terminal::manager::TerminalManager;
@@ -391,6 +392,10 @@ mod tauri_app {
chat_channel_commands::set_chat_message_language,
chat_channel_commands::weixin_get_qrcode,
chat_channel_commands::weixin_check_qrcode,
model_provider_commands::list_model_providers,
model_provider_commands::create_model_provider,
model_provider_commands::update_model_provider,
model_provider_commands::delete_model_provider,
web::start_web_server,
web::stop_web_server,
web::get_web_server_status,

View File

@@ -2,6 +2,7 @@ pub mod agent;
pub mod chat_channel;
pub mod conversation;
pub mod folder;
pub mod model_provider;
pub mod message;
pub mod system;

View File

@@ -0,0 +1,42 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelProviderInfo {
pub id: i32,
pub name: String,
pub api_url: String,
pub api_key_masked: String,
pub agent_types: Vec<String>,
pub created_at: String,
pub updated_at: String,
}
fn mask_api_key(key: &str) -> String {
let len = key.len();
if len <= 8 {
"\u{2022}".repeat(len)
} else {
format!(
"{}{}{}",
&key[..4],
"\u{2022}".repeat(len.min(20) - 8),
&key[len - 4..]
)
}
}
impl From<crate::db::entities::model_provider::Model> for ModelProviderInfo {
fn from(m: crate::db::entities::model_provider::Model) -> Self {
let agent_types: Vec<String> =
serde_json::from_str(&m.agent_types_json).unwrap_or_default();
Self {
id: m.id,
name: m.name,
api_url: m.api_url,
api_key_masked: mask_api_key(&m.api_key),
agent_types,
created_at: m.created_at.to_rfc3339(),
updated_at: m.updated_at.to_rfc3339(),
}
}
}

View File

@@ -7,6 +7,7 @@ pub mod folder_commands;
pub mod folders;
pub mod git;
pub mod mcp;
pub mod model_provider;
pub mod project_boot;
pub mod system_settings;
pub mod terminal;

View File

@@ -0,0 +1,88 @@
use std::sync::Arc;
use axum::{extract::Extension, Json};
use serde::Deserialize;
use crate::app_error::AppCommandError;
use crate::app_state::AppState;
use crate::commands::model_provider as mp_commands;
use crate::models::model_provider::ModelProviderInfo;
// ---------------------------------------------------------------------------
// Param structs
// ---------------------------------------------------------------------------
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateModelProviderParams {
pub name: String,
pub api_url: String,
pub api_key: String,
pub agent_types: Vec<String>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateModelProviderParams {
pub id: i32,
pub name: Option<String>,
pub api_url: Option<String>,
pub api_key: Option<String>,
pub agent_types: Option<Vec<String>>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ModelProviderIdParams {
pub id: i32,
}
// ---------------------------------------------------------------------------
// Handlers
// ---------------------------------------------------------------------------
pub async fn list_model_providers(
Extension(state): Extension<Arc<AppState>>,
) -> Result<Json<Vec<ModelProviderInfo>>, AppCommandError> {
let result = mp_commands::list_model_providers_core(&state.db).await?;
Ok(Json(result))
}
pub async fn create_model_provider(
Extension(state): Extension<Arc<AppState>>,
Json(params): Json<CreateModelProviderParams>,
) -> Result<Json<ModelProviderInfo>, AppCommandError> {
let result = mp_commands::create_model_provider_core(
&state.db,
params.name,
params.api_url,
params.api_key,
params.agent_types,
)
.await?;
Ok(Json(result))
}
pub async fn update_model_provider(
Extension(state): Extension<Arc<AppState>>,
Json(params): Json<UpdateModelProviderParams>,
) -> Result<Json<ModelProviderInfo>, AppCommandError> {
let result = mp_commands::update_model_provider_core(
&state.db,
params.id,
params.name,
params.api_url,
params.api_key,
params.agent_types,
)
.await?;
Ok(Json(result))
}
pub async fn delete_model_provider(
Extension(state): Extension<Arc<AppState>>,
Json(params): Json<ModelProviderIdParams>,
) -> Result<Json<()>, AppCommandError> {
mp_commands::delete_model_provider_core(&state.db, params.id).await?;
Ok(Json(()))
}

View File

@@ -202,6 +202,11 @@ pub fn build_router(state: Arc<AppState>, token: String, static_dir: std::path::
.route("/set_chat_message_language", post(handlers::chat_channel::set_chat_message_language))
.route("/weixin_get_qrcode", post(handlers::chat_channel::weixin_get_qrcode))
.route("/weixin_check_qrcode", post(handlers::chat_channel::weixin_check_qrcode))
// ─── Model Providers ───
.route("/list_model_providers", post(handlers::model_provider::list_model_providers))
.route("/create_model_provider", post(handlers::model_provider::create_model_provider))
.route("/update_model_provider", post(handlers::model_provider::update_model_provider))
.route("/delete_model_provider", post(handlers::model_provider::delete_model_provider))
// ─── Terminal ───
.route("/terminal_spawn", post(handlers::terminal::terminal_spawn))
.route("/terminal_write", post(handlers::terminal::terminal_write))