mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-08-02 10:21:16 +08:00
style: format code with cargo fmt and prettier
This commit is contained in:
@@ -275,10 +275,7 @@ fn schema_create_tables_include_pricing_model_columns() {
|
|||||||
let multiplier = get_column_info(&conn, "proxy_config", "default_cost_multiplier");
|
let multiplier = get_column_info(&conn, "proxy_config", "default_cost_multiplier");
|
||||||
assert_eq!(multiplier.r#type, "TEXT");
|
assert_eq!(multiplier.r#type, "TEXT");
|
||||||
assert_eq!(multiplier.notnull, 1);
|
assert_eq!(multiplier.notnull, 1);
|
||||||
assert_eq!(
|
assert_eq!(normalize_default(&multiplier.default).as_deref(), Some("1"));
|
||||||
normalize_default(&multiplier.default).as_deref(),
|
|
||||||
Some("1")
|
|
||||||
);
|
|
||||||
|
|
||||||
let pricing_source = get_column_info(&conn, "proxy_config", "pricing_model_source");
|
let pricing_source = get_column_info(&conn, "proxy_config", "pricing_model_source");
|
||||||
assert_eq!(pricing_source.r#type, "TEXT");
|
assert_eq!(pricing_source.r#type, "TEXT");
|
||||||
@@ -310,10 +307,7 @@ fn schema_migration_v4_adds_pricing_model_columns() {
|
|||||||
let multiplier = get_column_info(&conn, "proxy_config", "default_cost_multiplier");
|
let multiplier = get_column_info(&conn, "proxy_config", "default_cost_multiplier");
|
||||||
assert_eq!(multiplier.r#type, "TEXT");
|
assert_eq!(multiplier.r#type, "TEXT");
|
||||||
assert_eq!(multiplier.notnull, 1);
|
assert_eq!(multiplier.notnull, 1);
|
||||||
assert_eq!(
|
assert_eq!(normalize_default(&multiplier.default).as_deref(), Some("1"));
|
||||||
normalize_default(&multiplier.default).as_deref(),
|
|
||||||
Some("1")
|
|
||||||
);
|
|
||||||
|
|
||||||
let pricing_source = get_column_info(&conn, "proxy_config", "pricing_model_source");
|
let pricing_source = get_column_info(&conn, "proxy_config", "pricing_model_source");
|
||||||
assert_eq!(pricing_source.r#type, "TEXT");
|
assert_eq!(pricing_source.r#type, "TEXT");
|
||||||
|
|||||||
@@ -216,10 +216,7 @@ pub struct ProviderMeta {
|
|||||||
#[serde(rename = "costMultiplier", skip_serializing_if = "Option::is_none")]
|
#[serde(rename = "costMultiplier", skip_serializing_if = "Option::is_none")]
|
||||||
pub cost_multiplier: Option<String>,
|
pub cost_multiplier: Option<String>,
|
||||||
/// 计费模式来源(response/request)
|
/// 计费模式来源(response/request)
|
||||||
#[serde(
|
#[serde(rename = "pricingModelSource", skip_serializing_if = "Option::is_none")]
|
||||||
rename = "pricingModelSource",
|
|
||||||
skip_serializing_if = "Option::is_none"
|
|
||||||
)]
|
|
||||||
pub pricing_model_source: Option<String>,
|
pub pricing_model_source: Option<String>,
|
||||||
/// 每日消费限额(USD)
|
/// 每日消费限额(USD)
|
||||||
#[serde(rename = "limitDailyUsd", skip_serializing_if = "Option::is_none")]
|
#[serde(rename = "limitDailyUsd", skip_serializing_if = "Option::is_none")]
|
||||||
@@ -688,9 +685,7 @@ mod tests {
|
|||||||
json!({ "env": {} }),
|
json!({ "env": {} }),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
manager
|
manager.providers.insert("provider-1".to_string(), provider);
|
||||||
.providers
|
|
||||||
.insert("provider-1".to_string(), provider);
|
|
||||||
|
|
||||||
assert_eq!(manager.get_all_providers().len(), 1);
|
assert_eq!(manager.get_all_providers().len(), 1);
|
||||||
assert!(manager.get_all_providers().contains_key("provider-1"));
|
assert!(manager.get_all_providers().contains_key("provider-1"));
|
||||||
@@ -713,9 +708,7 @@ mod tests {
|
|||||||
opus_model: Some("claude-opus".to_string()),
|
opus_model: Some("claude-opus".to_string()),
|
||||||
});
|
});
|
||||||
|
|
||||||
let provider = universal
|
let provider = universal.to_claude_provider().expect("claude provider");
|
||||||
.to_claude_provider()
|
|
||||||
.expect("claude provider");
|
|
||||||
|
|
||||||
assert_eq!(provider.id, "universal-claude-u1");
|
assert_eq!(provider.id, "universal-claude-u1");
|
||||||
assert_eq!(provider.name, "Universal");
|
assert_eq!(provider.name, "Universal");
|
||||||
|
|||||||
@@ -452,9 +452,9 @@ async fn log_usage(
|
|||||||
use super::usage::logger::UsageLogger;
|
use super::usage::logger::UsageLogger;
|
||||||
|
|
||||||
let logger = UsageLogger::new(&state.db);
|
let logger = UsageLogger::new(&state.db);
|
||||||
let (multiplier, pricing_model_source) = logger
|
|
||||||
.resolve_pricing_config(provider_id, app_type)
|
let (multiplier, pricing_model_source) =
|
||||||
.await;
|
logger.resolve_pricing_config(provider_id, app_type).await;
|
||||||
let pricing_model = if pricing_model_source == "request" {
|
let pricing_model = if pricing_model_source == "request" {
|
||||||
request_model
|
request_model
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -402,9 +402,8 @@ async fn log_usage_internal(
|
|||||||
use super::usage::logger::UsageLogger;
|
use super::usage::logger::UsageLogger;
|
||||||
|
|
||||||
let logger = UsageLogger::new(&state.db);
|
let logger = UsageLogger::new(&state.db);
|
||||||
let (multiplier, pricing_model_source) = logger
|
let (multiplier, pricing_model_source) =
|
||||||
.resolve_pricing_config(provider_id, app_type)
|
logger.resolve_pricing_config(provider_id, app_type).await;
|
||||||
.await;
|
|
||||||
let pricing_model = if pricing_model_source == "request" {
|
let pricing_model = if pricing_model_source == "request" {
|
||||||
request_model
|
request_model
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -209,9 +209,7 @@ impl<'a> UsageLogger<'a> {
|
|||||||
let default_multiplier_raw = match self.db.get_default_cost_multiplier(app_type).await {
|
let default_multiplier_raw = match self.db.get_default_cost_multiplier(app_type).await {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::warn!(
|
log::warn!("[USG-003] 获取默认倍率失败 (app_type={app_type}): {e}");
|
||||||
"[USG-003] 获取默认倍率失败 (app_type={app_type}): {e}"
|
|
||||||
);
|
|
||||||
"1".to_string()
|
"1".to_string()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -228,23 +226,19 @@ impl<'a> UsageLogger<'a> {
|
|||||||
let default_pricing_source_raw = match self.db.get_pricing_model_source(app_type).await {
|
let default_pricing_source_raw = match self.db.get_pricing_model_source(app_type).await {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::warn!(
|
log::warn!("[USG-003] 获取默认计费模式失败 (app_type={app_type}): {e}");
|
||||||
"[USG-003] 获取默认计费模式失败 (app_type={app_type}): {e}"
|
|
||||||
);
|
|
||||||
"response".to_string()
|
"response".to_string()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let default_pricing_source = if matches!(
|
let default_pricing_source =
|
||||||
default_pricing_source_raw.as_str(),
|
if matches!(default_pricing_source_raw.as_str(), "response" | "request") {
|
||||||
"response" | "request"
|
default_pricing_source_raw
|
||||||
) {
|
} else {
|
||||||
default_pricing_source_raw
|
log::warn!(
|
||||||
} else {
|
|
||||||
log::warn!(
|
|
||||||
"[USG-003] 默认计费模式无效 (app_type={app_type}): {default_pricing_source_raw}"
|
"[USG-003] 默认计费模式无效 (app_type={app_type}): {default_pricing_source_raw}"
|
||||||
);
|
);
|
||||||
"response".to_string()
|
"response".to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
let provider = self
|
let provider = self
|
||||||
.db
|
.db
|
||||||
@@ -279,9 +273,7 @@ impl<'a> UsageLogger<'a> {
|
|||||||
let pricing_model_source = match provider_pricing_source {
|
let pricing_model_source = match provider_pricing_source {
|
||||||
Some(value) if matches!(value, "response" | "request") => value.to_string(),
|
Some(value) if matches!(value, "response" | "request") => value.to_string(),
|
||||||
Some(value) => {
|
Some(value) => {
|
||||||
log::warn!(
|
log::warn!("[USG-003] 供应商计费模式无效 (provider_id={provider_id}): {value}");
|
||||||
"[USG-003] 供应商计费模式无效 (provider_id={provider_id}): {value}"
|
|
||||||
);
|
|
||||||
default_pricing_source.clone()
|
default_pricing_source.clone()
|
||||||
}
|
}
|
||||||
None => default_pricing_source.clone(),
|
None => default_pricing_source.clone(),
|
||||||
@@ -312,9 +304,7 @@ impl<'a> UsageLogger<'a> {
|
|||||||
let pricing = self.get_model_pricing(&pricing_model)?;
|
let pricing = self.get_model_pricing(&pricing_model)?;
|
||||||
|
|
||||||
if pricing.is_none() {
|
if pricing.is_none() {
|
||||||
log::warn!(
|
log::warn!("[USG-002] 模型定价未找到,成本将记录为 0: {pricing_model}");
|
||||||
"[USG-002] 模型定价未找到,成本将记录为 0: {pricing_model}"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let cost = CostCalculator::try_calculate(&usage, pricing.as_ref(), cost_multiplier);
|
let cost = CostCalculator::try_calculate(&usage, pricing.as_ref(), cost_multiplier);
|
||||||
|
|||||||
@@ -124,9 +124,7 @@ interface ProviderFormProps {
|
|||||||
showButtons?: boolean;
|
showButtons?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const normalizePricingSource = (
|
const normalizePricingSource = (value?: string): PricingModelSourceOption =>
|
||||||
value?: string,
|
|
||||||
): PricingModelSourceOption =>
|
|
||||||
value === "request" || value === "response" ? value : "inherit";
|
value === "request" || value === "response" ? value : "inherit";
|
||||||
|
|
||||||
export function ProviderForm({
|
export function ProviderForm({
|
||||||
@@ -970,7 +968,9 @@ export function ProviderForm({
|
|||||||
// 添加高级配置
|
// 添加高级配置
|
||||||
testConfig: testConfig.enabled ? testConfig : undefined,
|
testConfig: testConfig.enabled ? testConfig : undefined,
|
||||||
proxyConfig: proxyConfig.enabled ? proxyConfig : undefined,
|
proxyConfig: proxyConfig.enabled ? proxyConfig : undefined,
|
||||||
costMultiplier: pricingConfig.enabled ? pricingConfig.costMultiplier : undefined,
|
costMultiplier: pricingConfig.enabled
|
||||||
|
? pricingConfig.costMultiplier
|
||||||
|
: undefined,
|
||||||
pricingModelSource:
|
pricingModelSource:
|
||||||
pricingConfig.enabled && pricingConfig.pricingModelSource !== "inherit"
|
pricingConfig.enabled && pricingConfig.pricingModelSource !== "inherit"
|
||||||
? pricingConfig.pricingModelSource
|
? pricingConfig.pricingModelSource
|
||||||
|
|||||||
@@ -89,7 +89,9 @@ export function PricingConfigPanel() {
|
|||||||
return {
|
return {
|
||||||
app,
|
app,
|
||||||
multiplier,
|
multiplier,
|
||||||
source: (source === "request" ? "request" : "response") as PricingModelSource,
|
source: (source === "request"
|
||||||
|
? "request"
|
||||||
|
: "response") as PricingModelSource,
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@@ -153,7 +155,10 @@ export function PricingConfigPanel() {
|
|||||||
try {
|
try {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
PRICING_APPS.flatMap((app) => [
|
PRICING_APPS.flatMap((app) => [
|
||||||
proxyApi.setDefaultCostMultiplier(app, appConfigs[app].multiplier.trim()),
|
proxyApi.setDefaultCostMultiplier(
|
||||||
|
app,
|
||||||
|
appConfigs[app].multiplier.trim(),
|
||||||
|
),
|
||||||
proxyApi.setPricingModelSource(app, appConfigs[app].source),
|
proxyApi.setPricingModelSource(app, appConfigs[app].source),
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
@@ -308,10 +313,14 @@ export function PricingConfigPanel() {
|
|||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
<SelectContent>
|
<SelectContent>
|
||||||
<SelectItem value="response">
|
<SelectItem value="response">
|
||||||
{t("settings.globalProxy.pricingModelSourceResponse")}
|
{t(
|
||||||
|
"settings.globalProxy.pricingModelSourceResponse",
|
||||||
|
)}
|
||||||
</SelectItem>
|
</SelectItem>
|
||||||
<SelectItem value="request">
|
<SelectItem value="request">
|
||||||
{t("settings.globalProxy.pricingModelSourceRequest")}
|
{t(
|
||||||
|
"settings.globalProxy.pricingModelSourceRequest",
|
||||||
|
)}
|
||||||
</SelectItem>
|
</SelectItem>
|
||||||
</SelectContent>
|
</SelectContent>
|
||||||
</Select>
|
</Select>
|
||||||
|
|||||||
@@ -101,7 +101,10 @@ export const proxyApi = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// 设置默认成本倍率
|
// 设置默认成本倍率
|
||||||
async setDefaultCostMultiplier(appType: string, value: string): Promise<void> {
|
async setDefaultCostMultiplier(
|
||||||
|
appType: string,
|
||||||
|
value: string,
|
||||||
|
): Promise<void> {
|
||||||
return invoke("set_default_cost_multiplier", { appType, value });
|
return invoke("set_default_cost_multiplier", { appType, value });
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user