新增: F-04多Provider负载均衡池(数据层+选择器+并发原语)+CR-52白项

This commit is contained in:
2026-06-17 02:04:58 +08:00
parent 31ea151bb2
commit 79b6a43095
12 changed files with 463 additions and 16 deletions

View File

@@ -1067,6 +1067,10 @@ fn ai_provider_from_row(row: &Row<'_>) -> std::result::Result<AiProviderRecord,
config: row.get("config")?,
created_at: row.get("created_at")?,
updated_at: row.get("updated_at")?,
// F-260614-04: enabled/weight 列老库经 v19 迁移补建,DEFAULT 1 / DEFAULT 50。
// from_row 按 i32 取列值兼容(SQLite 无真 BOOLEAN),0→false/非0→true。
enabled: row.get::<_, i32>("enabled").unwrap_or(1) != 0,
weight: row.get::<_, i32>("weight").unwrap_or(50).max(0) as u32,
})
}
@@ -1151,23 +1155,30 @@ impl_repo!(
let is_default = if rec.is_default { 1i32 } else { 0i32 };
// model_configs:Vec<ModelConfig> → JSON 字符串落 TEXT 列
let model_configs_json = serde_json::to_string(&rec.model_configs).unwrap_or_else(|_| "[]".into());
// F-260614-04: enabled/weight 落库(SQLite 无 BOOLEAN,i32 承载)。
let enabled_i = if rec.enabled { 1i32 } else { 0i32 };
let weight_i = rec.weight.min(100) as i32;
conn.execute(
"INSERT OR REPLACE INTO ai_providers (id, name, provider_type, api_key, base_url, default_model, models, model_configs, is_default, config, created_at, updated_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)",
"INSERT OR REPLACE INTO ai_providers (id, name, provider_type, api_key, base_url, default_model, models, model_configs, is_default, config, created_at, updated_at, enabled, weight)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14)",
params![
rec.id, rec.name, rec.provider_type, rec.api_key, rec.base_url,
rec.default_model, rec.models, model_configs_json, is_default, rec.config, rec.created_at, rec.updated_at
rec.default_model, rec.models, model_configs_json, is_default, rec.config, rec.created_at, rec.updated_at,
enabled_i, weight_i
],
)
},
update => |conn, rec| {
let is_default = if rec.is_default { 1i32 } else { 0i32 };
let model_configs_json = serde_json::to_string(&rec.model_configs).unwrap_or_else(|_| "[]".into());
let enabled_i = if rec.enabled { 1i32 } else { 0i32 };
let weight_i = rec.weight.min(100) as i32;
conn.execute(
"UPDATE ai_providers SET name = ?1, provider_type = ?2, api_key = ?3, base_url = ?4, default_model = ?5, models = ?6, model_configs = ?7, is_default = ?8, config = ?9, updated_at = ?10 WHERE id = ?11",
"UPDATE ai_providers SET name = ?1, provider_type = ?2, api_key = ?3, base_url = ?4, default_model = ?5, models = ?6, model_configs = ?7, is_default = ?8, config = ?9, updated_at = ?10, enabled = ?11, weight = ?12 WHERE id = ?13",
params![
rec.name, rec.provider_type, rec.api_key, rec.base_url,
rec.default_model, rec.models, model_configs_json, is_default, rec.config, rec.updated_at, rec.id
rec.default_model, rec.models, model_configs_json, is_default, rec.config, rec.updated_at,
enabled_i, weight_i, rec.id
],
)
}
@@ -1761,6 +1772,8 @@ mod tests {
config: None,
created_at: "0".into(),
updated_at: "0".into(),
enabled: true,
weight: 50,
};
repo.insert(rec).await.expect("insert");

View File

@@ -34,7 +34,7 @@ pub fn run(conn: &Connection) -> Result<()> {
// 迁移步骤链: 顺序执行,跳过已应用的版本(current_version < N 才跑)。
// 新增版本时,在此数组追加一项 (N, migrate_vN) 即可,无需改逻辑。
let steps: [(i32, fn(&Connection) -> Result<()>); 18] = [
let steps: [(i32, fn(&Connection) -> Result<()>); 19] = [
(1, migrate_v1),
(2, migrate_v2),
(3, migrate_v3),
@@ -53,6 +53,7 @@ pub fn run(conn: &Connection) -> Result<()> {
(16, migrate_v16),
(17, migrate_v17),
(18, migrate_v18),
(19, migrate_v19),
];
for (version, migrate_fn) in steps {
@@ -328,6 +329,36 @@ fn migrate_v18(conn: &Connection) -> Result<()> {
Ok(())
}
/// V19: 幂等补 ai_providers.enabled + ai_providers.weight 列(F-260614-04 多 Provider 负载均衡池)
///
/// - `enabled INTEGER NOT NULL DEFAULT 1`:provider 是否进入负载均衡池。
/// 老库行迁移后默认 1(所有现存 provider 默认启用,单 provider 路径零变化)。
/// is_default 仍保留作启动兜底(get_active_provider 无 active_provider_id 时取 is_default)。
/// - `weight INTEGER NOT NULL DEFAULT 50`:provider 在池中的选择权重(0-100)。
/// 高权重 provider 优先被选为主;同权重时退化近似轮询。
///
/// 向后兼容:老库行 ALTER 后取 DEFAULT,from_row 经 i32→bool / i32→u32 解析。
/// 用 PRAGMA 探测列存在性,缺失才 ALTER(同 v17/v18 模式),对新库/老库均安全。
fn migrate_v19(conn: &Connection) -> Result<()> {
if !column_exists(conn, "ai_providers", "enabled") {
conn.execute(
"ALTER TABLE ai_providers ADD COLUMN enabled INTEGER NOT NULL DEFAULT 1",
[],
)?;
tracing::info!("v19: 补建 ai_providers.enabled 列(多 Provider 负载均衡池,F-260614-04)");
}
if !column_exists(conn, "ai_providers", "weight") {
conn.execute(
"ALTER TABLE ai_providers ADD COLUMN weight INTEGER NOT NULL DEFAULT 50",
[],
)?;
tracing::info!("v19: 补建 ai_providers.weight 列(多 Provider 负载均衡池,F-260614-04)");
}
conn.execute("INSERT INTO schema_version (version) VALUES (?)", [19])?;
tracing::info!("迁移 v19 完成");
Ok(())
}
/// V1 建表 SQL
const V1_SQL: &str = "
-- 想法表
@@ -512,7 +543,10 @@ CREATE TABLE IF NOT EXISTS ai_providers (
is_default INTEGER NOT NULL DEFAULT 0,
config TEXT,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
updated_at TEXT NOT NULL,
model_configs TEXT,
enabled INTEGER NOT NULL DEFAULT 1,
weight INTEGER NOT NULL DEFAULT 50
);
CREATE TABLE IF NOT EXISTS ai_tool_executions (

View File

@@ -162,6 +162,24 @@ pub struct AiProviderRecord {
pub config: Option<String>, // JSON extra config
pub created_at: String,
pub updated_at: String,
/// provider 是否进入负载均衡池(F-260614-04)。false = 仅作为配置存在,不参与主链路由/选池。
/// 老库迁移默认 1(单 provider 路径零变化)。
#[serde(default = "default_enabled")]
pub enabled: bool,
/// provider 在负载均衡池中的选择权重(0-100,F-260614-04)。高权重优先被选为主;
/// 同权重时退化近似轮询。老库迁移默认 50。
#[serde(default = "default_weight")]
pub weight: u32,
}
/// AiProviderRecord.enabled 的 serde 默认(true)。老库/缺字段 JSON → enabled。
fn default_enabled() -> bool {
true
}
/// AiProviderRecord.weight 的 serde 默认(50)。老库/缺字段 JSON → 50。
fn default_weight() -> u32 {
50
}
/// AI 对话记录

View File

@@ -211,6 +211,7 @@ mod tests {
api_key: "sk-db-fallback".into(), base_url: "https://x".into(),
default_model: "m".into(), models: None, model_configs: Vec::new(), is_default: false,
config: None, created_at: "0".into(), updated_at: "0".into(),
enabled: true, weight: 50,
};
assert_eq!(resolve_provider_secret(&rec), "sk-db-fallback");
}