Repository navigation
feat(ratio-sync): support models.dev ratio sync and fix Gemini cache ratios - #2973
Conversation
WalkthroughThis 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
Sequence DiagramsequenceDiagram
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
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
isOfficialChannelnow also matches models.dev — the "官方" tag may mislead users.This function returns
truefor 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. Labellingmodels.devas "官方" (official) could confuse users since it's an independent third-party source, not the project's own preset. Consider either:
- Renaming to
isPresetChanneland differentiating the tag label (e.g., "预设" for both, or distinct labels per preset), or- 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 inweb/src/pages/Setting/Ratio/UpstreamRatioSync.jsx(lines 56-64). Extracting them into a shared constants file (e.g., alongsideDEFAULT_ENDPOINTinconstants) 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 theTypefield (defaults to0).The
officialRatioPresetandmodelsDevPresetare appended withStatus: 1but without settingType. In the frontend,Typeis only checked against20(OpenRouter), so0won't cause incorrect endpoint assignment. However, a distinct sentinelTypevalue 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/jsonimported directly — only used forjson.RawMessagetype on line 274.The coding guidelines prohibit direct use of
encoding/jsonfor marshal/unmarshal. Here the import is only needed for thejson.RawMessagetype alias (line 274), not for any marshal/unmarshal call — actual decoding usescommon.DecodeJsonandcommon.Unmarshal. This is technically compliant, but worth noting: if a future contributor sees the import, they may assume directjson.Unmarshalis acceptable in this file. Consider adding a brief comment or using the raw type ([]byte) withcommon.Unmarshalto avoid the import entirely.As per coding guidelines: "Do NOT directly import or call
encoding/jsonfor 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 withChannelSelectorModal.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_ENDPOINTandMODELS_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.
feat(ratio-sync): support models.dev ratio sync and fix Gemini cache ratios
feat(ratio-sync): support models.dev ratio sync and fix Gemini cache ratios
feat(ratio-sync): support models.dev ratio sync and fix Gemini cache ratios
PR 类型
PR 是否包含破坏性更新?
PR 描述
close #2827
支持从 models.dev 同步模型倍率,并修复 Gemini 缓存倍率默认值。
主要改动如下:
ID = -101),可在倍率同步时直接使用https://models.dev/api.json。model_ratio、completion_ratio、cache_ratio结构。gemini-3-flash-preview、gemini-3-pro-preview调整为0.1,并新增gemini-3.1-pro-preview = 0.1。Summary by CodeRabbit
New Features
Updates