mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-08-04 19:45:34 +08:00
fix: sync openclaw and hermes live provider updates (#5098)
* fix: sync openclaw live provider updates * fix: sync hermes live provider updates * fix test: hermes live import stores models as array after denormalize The test import_hermes_providers_from_live_updates_existing_provider_from_live seeded models as a dict, but import_hermes_providers_from_live reads via get_providers() which calls denormalize_provider_models_for_read(), converting models from YAML dict to UI-friendly array. The test assertion accessed models as dict -> Null -> assertion failure. Fix: access models as array by index, matching the actual storage format after live import. Also verify the id field is preserved in the denormalized output.
This commit is contained in:
@@ -1479,6 +1479,7 @@ pub fn import_openclaw_providers_from_live(state: &AppState) -> Result<usize, Ap
|
||||
}
|
||||
|
||||
let mut imported = 0;
|
||||
let mut updated = 0;
|
||||
let existing_ids = state.db.get_provider_ids("openclaw")?;
|
||||
|
||||
for (id, config) in providers {
|
||||
@@ -1492,12 +1493,6 @@ pub fn import_openclaw_providers_from_live(state: &AppState) -> Result<usize, Ap
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip if already exists in database
|
||||
if existing_ids.contains(&id) {
|
||||
log::debug!("OpenClaw provider '{id}' already exists in database, skipping");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Convert to Value for settings_config
|
||||
let settings_config = match serde_json::to_value(&config) {
|
||||
Ok(v) => v,
|
||||
@@ -1507,6 +1502,30 @@ pub fn import_openclaw_providers_from_live(state: &AppState) -> Result<usize, Ap
|
||||
}
|
||||
};
|
||||
|
||||
if existing_ids.contains(&id) {
|
||||
match state.db.get_provider_by_id(&id, "openclaw") {
|
||||
Ok(Some(existing)) => {
|
||||
if existing.settings_config != settings_config {
|
||||
let mut provider = existing;
|
||||
provider.settings_config = settings_config;
|
||||
if let Err(e) = state.db.save_provider("openclaw", &provider) {
|
||||
log::warn!(
|
||||
"Failed to update OpenClaw provider '{id}' from live config: {e}"
|
||||
);
|
||||
} else {
|
||||
updated += 1;
|
||||
log::info!("Updated OpenClaw provider '{id}' from live config");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
log::warn!("OpenClaw provider '{id}' disappeared while importing live config")
|
||||
}
|
||||
Err(e) => log::warn!("Failed to look up OpenClaw provider '{id}': {e}"),
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Determine display name: use first model name if available, otherwise use id
|
||||
let display_name = config
|
||||
.models
|
||||
@@ -1531,7 +1550,7 @@ pub fn import_openclaw_providers_from_live(state: &AppState) -> Result<usize, Ap
|
||||
log::info!("Imported OpenClaw provider '{id}' from live config");
|
||||
}
|
||||
|
||||
Ok(imported)
|
||||
Ok(imported + updated)
|
||||
}
|
||||
|
||||
/// Import all providers from Hermes live config to database
|
||||
@@ -1548,6 +1567,7 @@ pub fn import_hermes_providers_from_live(state: &AppState) -> Result<usize, AppE
|
||||
}
|
||||
|
||||
let mut imported = 0;
|
||||
let mut updated = 0;
|
||||
let existing_ids = state.db.get_provider_ids("hermes")?;
|
||||
|
||||
for (name, config) in providers {
|
||||
@@ -1557,9 +1577,27 @@ pub fn import_hermes_providers_from_live(state: &AppState) -> Result<usize, AppE
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip if already exists in database
|
||||
if existing_ids.contains(&name) {
|
||||
log::debug!("Hermes provider '{name}' already exists in database, skipping");
|
||||
match state.db.get_provider_by_id(&name, "hermes") {
|
||||
Ok(Some(existing)) => {
|
||||
if existing.settings_config != config {
|
||||
let mut provider = existing;
|
||||
provider.settings_config = config;
|
||||
if let Err(e) = state.db.save_provider("hermes", &provider) {
|
||||
log::warn!(
|
||||
"Failed to update Hermes provider '{name}' from live config: {e}"
|
||||
);
|
||||
} else {
|
||||
updated += 1;
|
||||
log::info!("Updated Hermes provider '{name}' from live config");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
log::warn!("Hermes provider '{name}' disappeared while importing live config")
|
||||
}
|
||||
Err(e) => log::warn!("Failed to look up Hermes provider '{name}': {e}"),
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1580,7 +1618,7 @@ pub fn import_hermes_providers_from_live(state: &AppState) -> Result<usize, AppE
|
||||
log::info!("Imported Hermes provider '{name}' from live config");
|
||||
}
|
||||
|
||||
Ok(imported)
|
||||
Ok(imported + updated)
|
||||
}
|
||||
|
||||
/// Remove a Hermes provider from live config
|
||||
|
||||
Reference in New Issue
Block a user