feat: 支持按模型配置 token 价格,聚合 API 模型同步时自动创建零价格规则#279
Open
panzeyu2013 wants to merge 1 commit into
Open
Conversation
…egate API models
- Auto-create model_price_rules (price=0) when syncing aggregate API models
- Only insert on first sync, never overwrite existing user-set prices
- Add RPC endpoints: quota/modelPriceRules/list, read, upsert
- Add price input fields (input/cached/output per 1M tokens) to model edit modal
- Register Tauri commands for desktop support
- User-created rules use priority=20000,ID=user-{slug} to override official seeds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
变更摘要
问题:聚合 API(如 DeepSeek、Mistral)的模型同步后缺少
model_price_rules记录,导致定价引擎匹配失败,estimated_cost_usd始终为null(前端显示price_status: "missing"),钱包不扣费,成本统计无法正常工作。根因:
auto_associate_source_models(crates/service/src/apikey/apikey_models.rs:821)在同步聚合 API 模型时只创建了 platform model 目录条目和model_source_mapping路由映射,没有创建价格规则。定价引擎estimate_cost_usd_for_log先查 DB rules → 未命中 → fallback 硬编码PRICE_SEEDS→ 也未命中 → 返回None → 0.0。方案:同步时自动为聚合 API 模型创建价格规则(价格均为 0),并支持用户在模型编辑弹窗中手动设置价格。用户规则
priority=20000,确定性 ID=user-{slug},确保覆盖所有自动创建和官方 seed 规则。改动范围
主要文件
Rust 后端(4 个文件)
crates/core/src/rpc/types.rsModelPriceRuleEntryModelPriceRuleListResultModelPriceRuleUpsertInputcrates/service/src/quota/read.rslist_model_price_rulesread_model_price_ruleupsert_model_price_ruleprice_rule_entry辅助函数crates/service/src/rpc_dispatch/quota.rsquota/modelPriceRules/listquota/modelPriceRule/readquota/modelPriceRule/upsertcrates/service/src/apikey/apikey_models.rsensure_model_price_rules_for_aggregate_apiexisting_patternsHashSet先查后插for source_model in source_models的 borrow-after-move 编译错误前端(5 个文件)
apps/src/lib/api/account-client.tsModelPriceRuleEntryModelPriceRuleUpsertPayloadlistModelPriceRulesreadModelPriceRuleupsertModelPriceRuleapps/src/lib/api/transport-web-commands.tsservice_model_price_rules_listservice_model_price_rule_readservice_model_price_rule_upsertapps/src/components/modals/model-catalog-modal.tsxonSavePriceRule回调同步写入规则apps/src/hooks/useManagedModels.tssaveModelPriceRulehook 方法apps/src/app/models/page.tsxonSavePriceRule桌面端 Tauri(2 个文件)
apps/src-tauri/src/commands/apikey.rsservice_model_price_rules_listservice_model_price_rule_readservice_model_price_rule_upsert#[tauri::command]apps/src-tauri/src/commands/registry.rsinvoke_handler!宏中注册上述三个命令验证
pnpm -C apps run testpnpm -C apps run buildpnpm -C apps run test:uicargo test --workspace已执行的实际验证:
Rust 编译检查
cargo check --workspace
结果:0 errors, 0 warnings (0.82s)
Rust 全部测试
cargo test --workspace
结果:1122 tests passed, 0 failed, 0 ignored
前端构建
pnpm -C apps run build
结果:12 static pages, 0 errors
桌面端构建(含 Tauri)
pnpm -C apps run build:desktop
结果:12 static pages, Tauri commands registered, 0 errors
风险与影响面
直接影响
定价引擎:
model_pricing.rs不变)请求日志:
request_log.rs、aggregate_api.rs、proxy.rs等均不变)OpenAI account 同步:
source_kind == "aggregate_api"触发)官方模型定价:
priority=20000priority=-10priority≈999920000 > 9999 > -10边界注意
修复前:
cost=0修复后:
cost仍为 0base_cost_usd <= 0.0跳过)重同步保护:
existing_patterns包含所有已启用规则的model_pattern编辑已有模型:
备注
自动创建的 rule 标识:
source: "aggregate_api_sync"priority=-10官方 seed:
source: "official_seed"用户规则:
source: "custom"priority=20000分层关系清晰
用户 upsert 使用 ID:
user-{model_pattern}特性: