mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-08-02 02:05:57 +08:00
style: format Rust code with cargo fmt
This commit is contained in:
@@ -260,7 +260,9 @@ pub async fn set_common_config_snippet(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if matches!(app_type.as_str(), "claude" | "codex" | "gemini") {
|
if matches!(app_type.as_str(), "claude" | "codex" | "gemini") {
|
||||||
if let Some(legacy_snippet) = old_snippet.as_deref().filter(|value| !value.trim().is_empty())
|
if let Some(legacy_snippet) = old_snippet
|
||||||
|
.as_deref()
|
||||||
|
.filter(|value| !value.trim().is_empty())
|
||||||
{
|
{
|
||||||
let app = AppType::from_str(&app_type).map_err(|e| e.to_string())?;
|
let app = AppType::from_str(&app_type).map_err(|e| e.to_string())?;
|
||||||
crate::services::provider::ProviderService::migrate_legacy_common_config_usage(
|
crate::services::provider::ProviderService::migrate_legacy_common_config_usage(
|
||||||
|
|||||||
@@ -198,7 +198,10 @@ pub struct ProviderMeta {
|
|||||||
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
|
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
|
||||||
pub custom_endpoints: HashMap<String, crate::settings::CustomEndpoint>,
|
pub custom_endpoints: HashMap<String, crate::settings::CustomEndpoint>,
|
||||||
/// 是否在写入 live 时应用通用配置片段
|
/// 是否在写入 live 时应用通用配置片段
|
||||||
#[serde(rename = "commonConfigEnabled", skip_serializing_if = "Option::is_none")]
|
#[serde(
|
||||||
|
rename = "commonConfigEnabled",
|
||||||
|
skip_serializing_if = "Option::is_none"
|
||||||
|
)]
|
||||||
pub common_config_enabled: Option<bool>,
|
pub common_config_enabled: Option<bool>,
|
||||||
/// 用量查询脚本配置
|
/// 用量查询脚本配置
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
|||||||
@@ -111,8 +111,9 @@ impl TokenUsage {
|
|||||||
}
|
}
|
||||||
// 从 message_delta 中处理缓存命中(cache_read_input_tokens)
|
// 从 message_delta 中处理缓存命中(cache_read_input_tokens)
|
||||||
if usage.cache_read_tokens == 0 {
|
if usage.cache_read_tokens == 0 {
|
||||||
if let Some(cache_read) =
|
if let Some(cache_read) = delta_usage
|
||||||
delta_usage.get("cache_read_input_tokens").and_then(|v| v.as_u64())
|
.get("cache_read_input_tokens")
|
||||||
|
.and_then(|v| v.as_u64())
|
||||||
{
|
{
|
||||||
usage.cache_read_tokens = cache_read as u32;
|
usage.cache_read_tokens = cache_read as u32;
|
||||||
}
|
}
|
||||||
@@ -120,8 +121,9 @@ impl TokenUsage {
|
|||||||
// 从 message_delta 中处理缓存创建(cache_creation_input_tokens)
|
// 从 message_delta 中处理缓存创建(cache_creation_input_tokens)
|
||||||
// 注: 现在 zhipu 没有返回 cache_creation_input_tokens 字段
|
// 注: 现在 zhipu 没有返回 cache_creation_input_tokens 字段
|
||||||
if usage.cache_creation_tokens == 0 {
|
if usage.cache_creation_tokens == 0 {
|
||||||
if let Some(cache_creation) =
|
if let Some(cache_creation) = delta_usage
|
||||||
delta_usage.get("cache_creation_input_tokens").and_then(|v| v.as_u64())
|
.get("cache_creation_input_tokens")
|
||||||
|
.and_then(|v| v.as_u64())
|
||||||
{
|
{
|
||||||
usage.cache_creation_tokens = cache_creation as u32;
|
usage.cache_creation_tokens = cache_creation as u32;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,11 +39,11 @@ fn json_is_subset(target: &Value, source: &Value) -> bool {
|
|||||||
let Some(target_map) = target.as_object() else {
|
let Some(target_map) = target.as_object() else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
source_map
|
source_map.iter().all(|(key, source_value)| {
|
||||||
.iter()
|
target_map
|
||||||
.all(|(key, source_value)| target_map.get(key).is_some_and(|target_value| {
|
.get(key)
|
||||||
json_is_subset(target_value, source_value)
|
.is_some_and(|target_value| json_is_subset(target_value, source_value))
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
Value::Array(source_arr) => {
|
Value::Array(source_arr) => {
|
||||||
let Some(target_arr) = target.as_array() else {
|
let Some(target_arr) = target.as_array() else {
|
||||||
@@ -163,9 +163,13 @@ fn toml_array_contains_subset(target: &toml_edit::Array, source: &toml_edit::Arr
|
|||||||
let target_items: Vec<&toml_edit::Value> = target.iter().collect();
|
let target_items: Vec<&toml_edit::Value> = target.iter().collect();
|
||||||
|
|
||||||
source.iter().all(|source_item| {
|
source.iter().all(|source_item| {
|
||||||
if let Some((index, _)) = target_items.iter().enumerate().find(|(index, target_item)| {
|
if let Some((index, _)) = target_items
|
||||||
!matched[*index] && toml_value_is_subset(target_item, source_item)
|
.iter()
|
||||||
}) {
|
.enumerate()
|
||||||
|
.find(|(index, target_item)| {
|
||||||
|
!matched[*index] && toml_value_is_subset(target_item, source_item)
|
||||||
|
})
|
||||||
|
{
|
||||||
matched[index] = true;
|
matched[index] = true;
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
@@ -204,7 +208,9 @@ fn toml_item_is_subset(target: &Item, source: &Item) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
match (target.as_value(), source.as_value()) {
|
match (target.as_value(), source.as_value()) {
|
||||||
(Some(target_value), Some(source_value)) => toml_value_is_subset(target_value, source_value),
|
(Some(target_value), Some(source_value)) => {
|
||||||
|
toml_value_is_subset(target_value, source_value)
|
||||||
|
}
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -251,7 +257,9 @@ fn remove_toml_item(target: &mut Item, source: &Item) {
|
|||||||
toml_remove_array_items(target_arr, source_arr);
|
toml_remove_array_items(target_arr, source_arr);
|
||||||
remove_item = target_arr.is_empty();
|
remove_item = target_arr.is_empty();
|
||||||
}
|
}
|
||||||
(target_value, source_value) if toml_value_is_subset(target_value, source_value) => {
|
(target_value, source_value)
|
||||||
|
if toml_value_is_subset(target_value, source_value) =>
|
||||||
|
{
|
||||||
remove_item = true;
|
remove_item = true;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
@@ -370,7 +378,9 @@ pub(crate) fn remove_common_config_from_settings(
|
|||||||
DocumentMut::new()
|
DocumentMut::new()
|
||||||
} else {
|
} else {
|
||||||
config_toml.parse::<DocumentMut>().map_err(|e| {
|
config_toml.parse::<DocumentMut>().map_err(|e| {
|
||||||
AppError::Message(format!("Invalid Codex config.toml while removing common config: {e}"))
|
AppError::Message(format!(
|
||||||
|
"Invalid Codex config.toml while removing common config: {e}"
|
||||||
|
))
|
||||||
})?
|
})?
|
||||||
};
|
};
|
||||||
let source_doc = trimmed.parse::<DocumentMut>().map_err(|e| {
|
let source_doc = trimmed.parse::<DocumentMut>().map_err(|e| {
|
||||||
@@ -421,7 +431,9 @@ fn apply_common_config_to_settings(
|
|||||||
DocumentMut::new()
|
DocumentMut::new()
|
||||||
} else {
|
} else {
|
||||||
config_toml.parse::<DocumentMut>().map_err(|e| {
|
config_toml.parse::<DocumentMut>().map_err(|e| {
|
||||||
AppError::Message(format!("Invalid Codex config.toml while applying common config: {e}"))
|
AppError::Message(format!(
|
||||||
|
"Invalid Codex config.toml while applying common config: {e}"
|
||||||
|
))
|
||||||
})?
|
})?
|
||||||
};
|
};
|
||||||
let source_doc = trimmed.parse::<DocumentMut>().map_err(|e| {
|
let source_doc = trimmed.parse::<DocumentMut>().map_err(|e| {
|
||||||
@@ -791,7 +803,8 @@ pub(crate) fn sync_current_provider_for_app_to_live(
|
|||||||
if app_type.is_additive_mode() {
|
if app_type.is_additive_mode() {
|
||||||
sync_all_providers_to_live(state, app_type)?;
|
sync_all_providers_to_live(state, app_type)?;
|
||||||
} else {
|
} else {
|
||||||
let current_id = match crate::settings::get_effective_current_provider(&state.db, app_type)? {
|
let current_id = match crate::settings::get_effective_current_provider(&state.db, app_type)?
|
||||||
|
{
|
||||||
Some(id) => id,
|
Some(id) => id,
|
||||||
None => return Ok(()),
|
None => return Ok(()),
|
||||||
};
|
};
|
||||||
@@ -1326,8 +1339,7 @@ mod tests {
|
|||||||
});
|
});
|
||||||
let snippet = "[shared]\nreasoning = \"medium\"\n";
|
let snippet = "[shared]\nreasoning = \"medium\"\n";
|
||||||
|
|
||||||
let applied =
|
let applied = apply_common_config_to_settings(&AppType::Codex, &settings, snippet).unwrap();
|
||||||
apply_common_config_to_settings(&AppType::Codex, &settings, snippet).unwrap();
|
|
||||||
let applied_config = applied["config"].as_str().unwrap_or_default();
|
let applied_config = applied["config"].as_str().unwrap_or_default();
|
||||||
assert!(applied_config.contains("[shared]"));
|
assert!(applied_config.contains("[shared]"));
|
||||||
assert!(applied_config.contains("reasoning = \"medium\""));
|
assert!(applied_config.contains("reasoning = \"medium\""));
|
||||||
|
|||||||
@@ -565,12 +565,13 @@ impl ProviderService {
|
|||||||
// Only backfill when switching to a different provider
|
// Only backfill when switching to a different provider
|
||||||
if let Ok(live_config) = read_live_settings(app_type.clone()) {
|
if let Ok(live_config) = read_live_settings(app_type.clone()) {
|
||||||
if let Some(mut current_provider) = providers.get(¤t_id).cloned() {
|
if let Some(mut current_provider) = providers.get(¤t_id).cloned() {
|
||||||
current_provider.settings_config = strip_common_config_from_live_settings(
|
current_provider.settings_config =
|
||||||
state.db.as_ref(),
|
strip_common_config_from_live_settings(
|
||||||
&app_type,
|
state.db.as_ref(),
|
||||||
¤t_provider,
|
&app_type,
|
||||||
live_config,
|
¤t_provider,
|
||||||
);
|
live_config,
|
||||||
|
);
|
||||||
if let Err(e) =
|
if let Err(e) =
|
||||||
state.db.save_provider(app_type.as_str(), ¤t_provider)
|
state.db.save_provider(app_type.as_str(), ¤t_provider)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user