Skip to content

feat(ratio-sync): support models.dev ratio sync and fix Gemini cache ratios - #2973

Merged
seefs001 merged 2 commits into
QuantumNous:mainfrom
RedwindA:feat/modelsdotdev
Feb 21, 2026
Merged

seefs001 merged 2 commits into
QuantumNous:mainfrom
RedwindA:feat/modelsdotdev

Conversation

@RedwindA

@RedwindA RedwindA commented Feb 20, 2026 •

Copy link
Copy Markdown
Contributor

PR 类型

  • Bug 修复
  • 新功能
  • 文档更新
  • 其他

PR 是否包含破坏性更新?

  • 是
  • 否

PR 描述

close #2827
支持从 models.dev 同步模型倍率,并修复 Gemini 缓存倍率默认值。

主要改动如下:

  1. 新增 models.dev 价格预设(ID = -101),可在倍率同步时直接使用 https://models.dev/api.json。
  2. 后端同步逻辑新增 models.dev 端点识别与解析流程,并转换为系统内部的 model_ratio、completion_ratio、cache_ratio 结构。
  3. 对 models.dev 同名模型跨 provider 的冲突,采用“优先最便宜非零 input 成本;若仅有零成本则保留 0”策略。
  4. 前端同步页面与渠道选择器支持识别和展示 models.dev 预设。
  5. 修复 Gemini 默认缓存倍率:将 gemini-3-flash-preview、gemini-3-pro-preview 调整为 0.1,并新增 gemini-3.1-pro-preview = 0.1。

Summary by CodeRabbit

  • New Features

    • Added models.dev as a new upstream pricing data source for model cost synchronization
    • Introduced official and models.dev presets for streamlined channel configuration and data synchronization
  • Updates

    • Updated default cache ratio values for Gemini model variants
    • Enhanced channel synchronization with improved preset detection and endpoint routing

@coderabbitai

coderabbitai Bot commented Feb 20, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

This PR adds models.dev as a new upstream source for ratio synchronization alongside existing OpenRouter and type-based formats. It introduces data structures, parsing logic, and endpoint detection for models.dev APIs, extends upstream response processing to support multiple parse paths, and registers two new public presets (official and models.dev) in channel synchronization data.

Changes

Cohort / File(s) Summary
Backend Ratio Sync Logic
controller/ratio_sync.go
Adds models.dev upstream support with endpoint detection, API response parsing, data structures for provider/model/cost handling, and conflict resolution logic. Introduces roundRatioValue normalization helper and extends response body reading to support multiple downstream parse paths (OpenRouter, models.dev, type1/type2). Updates channel synchronization with two new presets: officialRatioPreset and modelsDevPreset.
Cache Ratio Configuration
setting/ratio_setting/cache_ratio.go
Updates default cache ratio values: gemini-3-flash-preview and gemini-3-pro-preview reduced from 0.25 to 0.1, and adds new entry for gemini-3.1-pro-preview with 0.1 ratio.
Frontend Preset Handling
web/src/components/settings/ChannelSelectorModal.jsx, web/src/pages/Setting/Ratio/UpstreamRatioSync.jsx
Introduces constants for preset identification (OFFICIAL_RATIO_PRESET_ID, MODELS_DEV_PRESET_ID and corresponding names/base URLs). Updates isOfficialChannel logic to use constants instead of hard-coded checks. Adds preset classification logic to assign channel endpoints with prioritization: models.dev preset → official preset → openrouter → default endpoint.

Sequence Diagram

sequenceDiagram
    participant UI as Frontend<br/>(UpstreamRatioSync)
    participant Ctrl as Controller<br/>(ratio_sync)
    participant ModDev as models.dev<br/>API
    participant DB as Local Storage

    UI->>Ctrl: Request ratio sync for channel
    Ctrl->>Ctrl: Identify channel preset type
    alt is models.dev preset
        Ctrl->>Ctrl: Set endpoint to models.dev API
    else is official preset
        Ctrl->>Ctrl: Set endpoint to basellm
    else
        Ctrl->>Ctrl: Set endpoint to openrouter/default
    end
    
    Ctrl->>ModDev: GET /api.json
    ModDev-->>Ctrl: Return pricing data
    
    Ctrl->>Ctrl: Parse response body
    Ctrl->>Ctrl: Extract provider/model/cost
    Ctrl->>Ctrl: Select cheapest input cost
    Ctrl->>Ctrl: Normalize ratios (roundRatioValue)
    Ctrl->>Ctrl: Convert to local format<br/>(model_ratio, completion_ratio)
    
    Ctrl->>DB: Store computed ratios
    DB-->>Ctrl: Confirmed
    Ctrl-->>UI: Sync complete
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • seefs001

Poem

🐰 A rabbit hops through pricing tiers,
models.dev brings cheer, away with fears!
New ratios flow like morning dew,
Upstream sync made fresh and new! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the two main changes: adding models.dev ratio sync support and fixing Gemini cache ratios.
Linked Issues check ✅ Passed All changes align with issue #2827 requirements: models.dev is added as upstream source with parsing logic, conflict resolution, and frontend updates to recognize the new preset.
Out of Scope Changes check ✅ Passed Cache ratio fixes for Gemini models appear tangentially related to ratio handling but align with the PR's ratio-sync scope and related issue closure.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
web/src/components/settings/ChannelSelectorModal.jsx (1)

75-86: ⚠️ Potential issue | 🟡 Minor

isOfficialChannel now also matches models.dev — the "官方" tag may mislead users.

This function returns true for both the official preset and the models.dev preset. On line 214, channels matching this check are rendered with a <Tag color='green'>官方</Tag> ("Official") badge. Labelling models.dev as "官方" (official) could confuse users since it's an independent third-party source, not the project's own preset. Consider either:

  1. Renaming to isPresetChannel and differentiating the tag label (e.g., "预设" for both, or distinct labels per preset), or
  2. Keeping the "官方" tag only for the official preset and showing a different tag for models.dev.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@web/src/components/settings/ChannelSelectorModal.jsx` around lines 75 - 86,
isOfficialChannel currently returns true for both the project's official preset
and the models.dev preset (it checks OFFICIAL_RATIO_PRESET_ID and
MODELS_DEV_PRESET_ID), causing the UI to render a "官方" tag for models.dev;
update the logic so only the official preset constants
(OFFICIAL_RATIO_PRESET_ID, OFFICIAL_RATIO_PRESET_BASE_URL,
OFFICIAL_RATIO_PRESET_NAME) cause the "官方" tag, and add a separate check (e.g.,
isModelsDevChannel or isPresetChannel) that detects
MODELS_DEV_PRESET_ID/BASE_URL/NAME and maps it to a different tag label (e.g.,
"预设" or "第三方") in the rendering code that currently uses <Tag
color='green'>官方</Tag>, or alternatively rename isOfficialChannel to
isPresetChannel and implement distinct boolean helpers for official vs
models.dev and use those to choose the correct tag text.
🧹 Nitpick comments (4)
web/src/components/settings/ChannelSelectorModal.jsx (1)

38-43: Duplicate constants across files — extract to a shared module.

These six constants (OFFICIAL_RATIO_PRESET_ID, MODELS_DEV_PRESET_ID, OFFICIAL_RATIO_PRESET_NAME, MODELS_DEV_PRESET_NAME, OFFICIAL_RATIO_PRESET_BASE_URL, MODELS_DEV_PRESET_BASE_URL) are identically defined in web/src/pages/Setting/Ratio/UpstreamRatioSync.jsx (lines 56-64). Extracting them into a shared constants file (e.g., alongside DEFAULT_ENDPOINT in constants) would eliminate duplication and reduce the risk of the two copies drifting apart.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@web/src/components/settings/ChannelSelectorModal.jsx` around lines 38 - 43,
Extract the six duplicated constants (OFFICIAL_RATIO_PRESET_ID,
MODELS_DEV_PRESET_ID, OFFICIAL_RATIO_PRESET_NAME, MODELS_DEV_PRESET_NAME,
OFFICIAL_RATIO_PRESET_BASE_URL, MODELS_DEV_PRESET_BASE_URL) into a shared
constants module (e.g., add exports next to DEFAULT_ENDPOINT in the existing
constants file), export them by name, then replace the in-file definitions in
ChannelSelectorModal.jsx and UpstreamRatioSync.jsx with named imports of those
symbols; ensure both files import the exact exported identifiers and remove the
original local constant declarations so there is a single source of truth.
controller/ratio_sync.go (2)

895-907: Both presets omit the Type field (defaults to 0).

The officialRatioPreset and modelsDevPreset are appended with Status: 1 but without setting Type. In the frontend, Type is only checked against 20 (OpenRouter), so 0 won't cause incorrect endpoint assignment. However, a distinct sentinel Type value for presets could help the frontend distinguish them from user-created channels more robustly in the future.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@controller/ratio_sync.go` around lines 895 - 907, The two preset entries
appended to syncableChannels (the dto.SyncableChannel objects created for
officialRatioPresetID and modelsDevPresetID) omit the Type field; add a distinct
sentinel Type (e.g., dto.ChannelTypePreset) to both objects and define that
constant in the dto package (choose a value unlikely to collide, e.g., 99) so
the frontend can reliably distinguish presets from user channels; update the two
append calls to include Type: dto.ChannelTypePreset and add the new constant
(ChannelTypePreset) to dto.

3-6: encoding/json imported directly — only used for json.RawMessage type on line 274.

The coding guidelines prohibit direct use of encoding/json for marshal/unmarshal. Here the import is only needed for the json.RawMessage type alias (line 274), not for any marshal/unmarshal call — actual decoding uses common.DecodeJson and common.Unmarshal. This is technically compliant, but worth noting: if a future contributor sees the import, they may assume direct json.Unmarshal is acceptable in this file. Consider adding a brief comment or using the raw type ([]byte) with common.Unmarshal to avoid the import entirely.

As per coding guidelines: "Do NOT directly import or call encoding/json for marshal/unmarshal in business code."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@controller/ratio_sync.go` around lines 3 - 6, The import of encoding/json is
only used for the json.RawMessage type; remove the encoding/json import and
replace any use of json.RawMessage with a raw []byte payload (e.g., change
field/type from json.RawMessage to []byte) and continue to decode with
common.DecodeJson/common.Unmarshal, or if you must keep RawMessage, add a short
comment beside the import explaining it's intentionally only used as a type
alias (no direct marshal/unmarshal) to avoid confusion; update all references to
json.RawMessage accordingly so the file no longer imports encoding/json for
marshal/unmarshal.
web/src/pages/Setting/Ratio/UpstreamRatioSync.jsx (1)

56-64: Constants partially duplicated with ChannelSelectorModal.jsx.

The ID, name, and base URL constants are identical copies of those in ChannelSelectorModal.jsx (lines 38-43). The endpoint constants (OFFICIAL_RATIO_PRESET_ENDPOINT and MODELS_DEV_PRESET_ENDPOINT) are unique to this file. Consider extracting the shared subset to a common constants module and keeping only the endpoint constants here.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@web/src/pages/Setting/Ratio/UpstreamRatioSync.jsx` around lines 56 - 64, The
file duplicates several preset constants already defined in
ChannelSelectorModal.jsx; extract the shared subset (OFFICIAL_RATIO_PRESET_ID,
OFFICIAL_RATIO_PRESET_NAME, OFFICIAL_RATIO_PRESET_BASE_URL,
MODELS_DEV_PRESET_ID, MODELS_DEV_PRESET_NAME, MODELS_DEV_PRESET_BASE_URL) into a
new common constants module (e.g., presetConstants.js), export them, then
replace the inline definitions in UpstreamRatioSync.jsx and
ChannelSelectorModal.jsx with imports from that module while leaving the
file-local endpoint constants (OFFICIAL_RATIO_PRESET_ENDPOINT,
MODELS_DEV_PRESET_ENDPOINT) in UpstreamRatioSync.jsx; update any import paths
and usages accordingly to ensure no references remain to the duplicated
definitions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@web/src/components/settings/ChannelSelectorModal.jsx`:
- Around line 75-86: isOfficialChannel currently returns true for both the
project's official preset and the models.dev preset (it checks
OFFICIAL_RATIO_PRESET_ID and MODELS_DEV_PRESET_ID), causing the UI to render a
"官方" tag for models.dev; update the logic so only the official preset constants
(OFFICIAL_RATIO_PRESET_ID, OFFICIAL_RATIO_PRESET_BASE_URL,
OFFICIAL_RATIO_PRESET_NAME) cause the "官方" tag, and add a separate check (e.g.,
isModelsDevChannel or isPresetChannel) that detects
MODELS_DEV_PRESET_ID/BASE_URL/NAME and maps it to a different tag label (e.g.,
"预设" or "第三方") in the rendering code that currently uses <Tag
color='green'>官方</Tag>, or alternatively rename isOfficialChannel to
isPresetChannel and implement distinct boolean helpers for official vs
models.dev and use those to choose the correct tag text.

---

Nitpick comments:
In `@controller/ratio_sync.go`:
- Around line 895-907: The two preset entries appended to syncableChannels (the
dto.SyncableChannel objects created for officialRatioPresetID and
modelsDevPresetID) omit the Type field; add a distinct sentinel Type (e.g.,
dto.ChannelTypePreset) to both objects and define that constant in the dto
package (choose a value unlikely to collide, e.g., 99) so the frontend can
reliably distinguish presets from user channels; update the two append calls to
include Type: dto.ChannelTypePreset and add the new constant (ChannelTypePreset)
to dto.
- Around line 3-6: The import of encoding/json is only used for the
json.RawMessage type; remove the encoding/json import and replace any use of
json.RawMessage with a raw []byte payload (e.g., change field/type from
json.RawMessage to []byte) and continue to decode with
common.DecodeJson/common.Unmarshal, or if you must keep RawMessage, add a short
comment beside the import explaining it's intentionally only used as a type
alias (no direct marshal/unmarshal) to avoid confusion; update all references to
json.RawMessage accordingly so the file no longer imports encoding/json for
marshal/unmarshal.

In `@web/src/components/settings/ChannelSelectorModal.jsx`:
- Around line 38-43: Extract the six duplicated constants
(OFFICIAL_RATIO_PRESET_ID, MODELS_DEV_PRESET_ID, OFFICIAL_RATIO_PRESET_NAME,
MODELS_DEV_PRESET_NAME, OFFICIAL_RATIO_PRESET_BASE_URL,
MODELS_DEV_PRESET_BASE_URL) into a shared constants module (e.g., add exports
next to DEFAULT_ENDPOINT in the existing constants file), export them by name,
then replace the in-file definitions in ChannelSelectorModal.jsx and
UpstreamRatioSync.jsx with named imports of those symbols; ensure both files
import the exact exported identifiers and remove the original local constant
declarations so there is a single source of truth.

In `@web/src/pages/Setting/Ratio/UpstreamRatioSync.jsx`:
- Around line 56-64: The file duplicates several preset constants already
defined in ChannelSelectorModal.jsx; extract the shared subset
(OFFICIAL_RATIO_PRESET_ID, OFFICIAL_RATIO_PRESET_NAME,
OFFICIAL_RATIO_PRESET_BASE_URL, MODELS_DEV_PRESET_ID, MODELS_DEV_PRESET_NAME,
MODELS_DEV_PRESET_BASE_URL) into a new common constants module (e.g.,
presetConstants.js), export them, then replace the inline definitions in
UpstreamRatioSync.jsx and ChannelSelectorModal.jsx with imports from that module
while leaving the file-local endpoint constants (OFFICIAL_RATIO_PRESET_ENDPOINT,
MODELS_DEV_PRESET_ENDPOINT) in UpstreamRatioSync.jsx; update any import paths
and usages accordingly to ensure no references remain to the duplicated
definitions.

@seefs001
seefs001 merged commit 31deb0d into QuantumNous:main Feb 21, 2026
1 check passed
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
feat(ratio-sync): support models.dev ratio sync and fix Gemini cache ratios
@RedwindA
RedwindA deleted the feat/modelsdotdev branch July 23, 2026 07:04
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
feat(ratio-sync): support models.dev ratio sync and fix Gemini cache ratios
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
feat(ratio-sync): support models.dev ratio sync and fix Gemini cache ratios
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add models.dev as baseline/basellm upstream for ratio sync

3 participants