Repository navigation
新增:倍率 0 的限时免费模型豁免余额限制器 #596
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
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) | ||
| } | ||
|
|
||
| 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) { | ||
|
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. 免费模型的豁免不应被其他模型的 Token 命中挡住。
例如,某个 Skip 组有两个 OR 分支:
两个分支都成立时, 请在状态结果中按成立分支保存模型范围和读数。 仅过滤未成立的分支无法修复此例,因为两个分支都成立。 请补充测试:两个模型、两个 OR 分支同时命中。
|
||
| 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.
不要让未成立分支中的 Token 命中阻止豁免。
例如,规则是
余额≤0 OR(Tokens≥100 AND Requests≥100),读数分别为0、120、5。state()只认定余额分支成立,但这里仍会读到hit=true的 Token 条件。函数因此返回false,免费模型仍被拦截。请让
state()额外返回成立分支的条件索引,豁免判定只读取这些分支。保留原有完整读数,避免影响其他消费者。补充上述组合规则的回归测试。按路径说明,应“从更底层的数据源、公共组件或状态流转层面更优雅地解决”。
Source: Path instructions