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

@@ -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?)
}