refactor: unify common config hooks with generic base hook and adapters

- Create useCommonConfigBase generic hook (~300 lines)
- Create commonConfigAdapters for Claude (JSON), Codex (TOML), Gemini (ENV/JSON)
- Refactor three hooks from ~1370 lines to ~430 lines (-940 lines)
- Extract useDarkMode hook from three ConfigSections components
- Remove dead code: backend _str functions, frontend JSON/TOML unused exports
- Deduplicate deepClone/deepMerge utilities
- Fix duplicate mod tests in provider.rs
This commit is contained in:
YoVinchen
2026-01-29 22:35:52 +08:00
parent 982f78af0f
commit 3b61fab4b5
14 changed files with 1045 additions and 1505 deletions
-98
View File
@@ -6,9 +6,6 @@
//!
//! Supports JSON (Claude, Gemini) and TOML (Codex) formats.
// Allow dead code as this is a utility module with functions available for future use
#![allow(dead_code)]
use serde_json::{Map, Value as JsonValue};
use toml::Value as TomlValue;
@@ -94,55 +91,6 @@ pub fn compute_final_json_config(
result
}
/// Compute final JSON config from strings.
///
/// # Arguments
/// * `custom_config_json` - Custom configuration as JSON string
/// * `common_config_json` - Common configuration as JSON string
/// * `enabled` - Whether common config is enabled
///
/// # Returns
/// Tuple of (final_config_json, error_message)
pub fn compute_final_json_config_str(
custom_config_json: &str,
common_config_json: &str,
enabled: bool,
) -> (String, Option<String>) {
let custom_config: JsonValue = match serde_json::from_str(custom_config_json) {
Ok(v) => v,
Err(_) => {
return (
custom_config_json.to_string(),
Some("Failed to parse custom config JSON".to_string()),
)
}
};
let common_config: JsonValue = if common_config_json.trim().is_empty() {
JsonValue::Object(Map::new())
} else {
match serde_json::from_str(common_config_json) {
Ok(v) => v,
Err(_) => {
return (
custom_config_json.to_string(),
Some("Failed to parse common config JSON".to_string()),
)
}
}
};
let final_config = compute_final_json_config(&custom_config, &common_config, enabled);
match serde_json::to_string_pretty(&final_config) {
Ok(s) => (s, None),
Err(e) => (
custom_config_json.to_string(),
Some(format!("Failed to serialize: {e}")),
),
}
}
/// Check if two JSON values are deeply equal.
fn json_deep_equal(a: &JsonValue, b: &JsonValue) -> bool {
match (a, b) {
@@ -247,52 +195,6 @@ pub fn extract_json_difference(
(JsonValue::Object(custom_config), has_common_keys)
}
/// Extract difference from JSON strings.
///
/// # Returns
/// Tuple of (custom_config_json, has_common_keys, error_message)
pub fn extract_json_difference_str(
live_config_json: &str,
common_config_json: &str,
) -> (String, bool, Option<String>) {
let live_config: JsonValue = match serde_json::from_str(live_config_json) {
Ok(v) => v,
Err(_) => {
return (
live_config_json.to_string(),
false,
Some("Failed to parse live config JSON".to_string()),
)
}
};
let common_config: JsonValue = if common_config_json.trim().is_empty() {
JsonValue::Object(Map::new())
} else {
match serde_json::from_str(common_config_json) {
Ok(v) => v,
Err(_) => {
return (
live_config_json.to_string(),
false,
Some("Failed to parse common config JSON".to_string()),
)
}
}
};
let (custom_config, has_common_keys) = extract_json_difference(&live_config, &common_config);
match serde_json::to_string_pretty(&custom_config) {
Ok(s) => (s, has_common_keys, None),
Err(e) => (
live_config_json.to_string(),
false,
Some(format!("Failed to serialize: {e}")),
),
}
}
// ============================================================================
// TOML Configuration Merge Functions
// ============================================================================
+1 -1
View File
@@ -697,7 +697,7 @@ pub struct OpenCodeModelLimit {
}
#[cfg(test)]
mod tests {
mod provider_tests {
use super::{
ClaudeModelConfig, CodexModelConfig, GeminiModelConfig, OpenCodeProviderConfig, Provider,
ProviderManager, ProviderMeta, UniversalProvider,