Repository navigation
Fix/transient 5修复:上游瞬态 5xx 不再绕过账号/模型顺延xx failover #592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: beta
Are you sure you want to change the base?
Changes from all commits
bbf960d
7733f20
a13e4ac
5a11543
3194dae
317c9df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| use serde_json::Value; | ||
|
|
||
| use super::{counter, groups, sources, types::*}; | ||
| use crate::server::core::{custom_providers, providers}; | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "evaluate_tests.rs"] | ||
|
|
@@ -125,12 +126,118 @@ pub fn state(account: &Value, group: &Group, facts: &sources::Facts, counts: &co | |
| GroupState { hit, account: hit && (group.target == Target::Account || account_hit), models, conditions: readings } | ||
| } | ||
|
|
||
| /// 模型条目里的倍率读数;`None` = 目录没有可判定的计费信息。 | ||
| /// | ||
| /// 字段优先级:`billingEffectiveMultiplier` > `billingMultiplier` > | ||
| /// `pointsMultiplier` > `multiplier` > `rate` > `credits` 展示串。 | ||
| /// 0 与负数都按「免费」处理(负数只可能是上游异常读数,按不消耗积分更安全); | ||
| /// 非有限值直接丢弃,避免把 NaN / Infinity 当成免费。 | ||
| fn multiplier_of(model: &Value) -> Option<f64> { | ||
| for key in [ | ||
| "billingEffectiveMultiplier", | ||
| "billingMultiplier", | ||
| "pointsMultiplier", | ||
| "multiplier", | ||
| "rate", | ||
| ] { | ||
| if let Some(value) = model.get(key).and_then(Value::as_f64) { | ||
| if value.is_finite() { | ||
| return Some(value); | ||
| } | ||
| } | ||
| } | ||
| // 展示串形态:`x0.00 credits` / `x0.0` / `x0.5 credits` / `0.7x` / `免费`。 | ||
| // `creditMultiplier` 是 Tabbit 的字段名;`credits` 是其余各家的通用字段。 | ||
| for key in ["credits", "creditMultiplier"] { | ||
| let Some(value) = model.get(key) else { continue }; | ||
| if let Some(number) = value.as_f64() { | ||
| if number.is_finite() { | ||
| return Some(number); | ||
| } | ||
| } | ||
| let Some(text) = value.as_str() else { continue }; | ||
| let text = text.trim(); | ||
| if text == "免费" { | ||
| return Some(0.0); | ||
| } | ||
| let text = text | ||
| .strip_prefix('x') | ||
| .or_else(|| text.strip_prefix('X')) | ||
| .unwrap_or(text) | ||
| .trim(); | ||
| let Some(number) = text.split_whitespace().next() else { continue }; | ||
| let number = number | ||
| .strip_suffix('x') | ||
| .or_else(|| number.strip_suffix('X')) | ||
| .unwrap_or(number); | ||
| if let Ok(value) = number.parse::<f64>() { | ||
| if value.is_finite() { | ||
| return Some(value); | ||
| } | ||
| } | ||
| } | ||
| None | ||
| } | ||
|
|
||
| /// 某家当前生效的模型清单:自定义家走自己的模型表,其余走提供商目录。 | ||
| fn manifest(provider: &str) -> Vec<Value> { | ||
| if custom_providers::is_custom_provider_id(provider) { | ||
| custom_providers::models_of(provider) | ||
| } else { | ||
| providers::catalog::manifest_for_provider(provider) | ||
| } | ||
| } | ||
|
|
||
| /// 某家上游模型当前的倍率;目录里找不到该模型时返回 `None`。 | ||
| fn model_multiplier(provider: &str, model: &str) -> Option<f64> { | ||
| if provider.is_empty() || model.is_empty() { | ||
| return None; | ||
| } | ||
| let manifest = manifest(provider); | ||
| let entry = manifest.iter().find(|entry| { | ||
| entry | ||
| .get("id") | ||
| .and_then(Value::as_str) | ||
| .is_some_and(|id| id.eq_ignore_ascii_case(model)) | ||
| || entry | ||
| .get("name") | ||
| .and_then(Value::as_str) | ||
| .is_some_and(|name| name.eq_ignore_ascii_case(model)) | ||
| })?; | ||
| multiplier_of(entry) | ||
| } | ||
|
|
||
| /// 倍率 0 的限时免费模型:余额/积分限制不应生效。 | ||
| fn is_free_model(provider: &str, model: &str) -> bool { | ||
| model_multiplier(provider, model).is_some_and(|multiplier| multiplier <= 0.0) | ||
| } | ||
|
|
||
| /// 命中里是否只有余额/额度条件。 | ||
| /// | ||
| /// `local = true` 的是 Token / 请求数这类**本地消耗保护**,不是账号积分; | ||
| /// 免费模型可以豁免前者,但不该豁免后者。 | ||
| fn only_balance_conditions(conditions: &[Reading]) -> bool { | ||
| conditions.iter().filter(|reading| reading.hit).all(|reading| !reading.local) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 只检查对当前模型实际生效的条件。 例如,规则为“余额不足 OR(本地请求数超限 AND 另一额度不足)”,三个条件依次为 true、true、false。 请让
|
||
| } | ||
|
|
||
| pub fn blocked(account: &Value, model: &str, facts: &sources::Facts, counts: &counter::Facts, now: i64) -> bool { | ||
| let provider = crate::server::core::routing::provider_of(account); | ||
| // 目录查询只在「确实有余额条件命中」时发生一次:多数账号不会走到这里。 | ||
| let mut free: Option<bool> = None; | ||
| groups::effective_groups(account).iter().any(|group| { | ||
| if group.action != Action::Skip { return false; } | ||
| let state = state(account, group, facts, counts, now); | ||
| if !state.hit { return false; } | ||
| state.account || (!model.is_empty() && state.models.iter().any(|wanted| wanted.eq_ignore_ascii_case(model))) | ||
| let matched = state.account | ||
| || (!model.is_empty() && state.models.iter().any(|wanted| wanted.eq_ignore_ascii_case(model))); | ||
| if !matched { return false; } | ||
| // 倍率 0 的限时免费模型不消耗账号积分:纯余额/额度条件的命中不生效。 | ||
| // Token / 请求数限制仍然生效(那是本地保护,与积分无关)。 | ||
| if !model.is_empty() && only_balance_conditions(&state.conditions) { | ||
| let is_free = *free.get_or_insert_with(|| is_free_model(provider, model)); | ||
| if is_free { return false; } | ||
| } | ||
| true | ||
| }) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with_breaker没有处理账号列表的信封结构,熔断状态无法注入。list_visible_accounts()返回{ currentAccountId, currentAccountIds, accounts: [...], … }形式的对象,而不是顶层数组。with_breaker只处理顶层数组,因此as_array_mut()返回None,循环不执行。结果是GET /api/accounts的每个账号都缺少breakerOpenUntil,账号页无法显示「熔断」徽章和清除入口。会话路径也使用同一个函数,同样受影响。请在公共出口改为读取
accounts字段,并保留原信封:建议修改
Committable suggestion
Source: Path instructions