Repository navigation
feat: auto fetch upstream models - #2979
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughAdds a new upstream channel model update subsystem: background detection task, per-channel and batch detect/apply HTTP endpoints, DTO fields and persistence for detected/ignored models, notification delivery, unified upstream model ID fetching, and frontend UI (table indicators, modal, batch actions) plus related tests. Changes
Sequence DiagramsequenceDiagram
participant User as User (UI)
participant UI as Frontend
participant API as Backend API
participant Upstream as Upstream Source
participant DB as Database
participant Cache as Channel Cache
User->>UI: Click "Detect Upstream Updates"
UI->>API: POST /api/channel/upstream_updates/detect (channel)
API->>Upstream: fetchChannelUpstreamModelIDs(channel)
Upstream-->>API: model ID list
API->>DB: Read channel models/settings
DB-->>API: local models, ignored, mappings
API->>API: Compute pending adds/removes
API->>DB: Persist last-detected metadata
API->>Cache: Refresh channel runtime cache
API-->>UI: Return pending add/remove lists
User->>UI: Confirm selections in modal
UI->>API: POST /api/channel/upstream_updates/apply (selection)
API->>DB: Persist changes (add/remove/ignored)
DB-->>API: Success
API->>Cache: Refresh runtime cache
API->>API: Notify watchers if enabled
API-->>UI: Success
Estimated code review effort🎯 4 (Complex) | ⏱️ ~65 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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.
Actionable comments posted: 2
🧹 Nitpick comments (4)
controller/channel_upstream_update.go (2)
299-304: Dead code: Gemini branch inside the general path is unreachable.The Gemini channel type is already handled and returned early at lines 239-250. The
if channel.Type == constant.ChannelTypeGeminicheck at line 300 will never be true in this code path.🧹 Proposed fix
ids := lo.Map(result.Data, func(item OpenAIModel, _ int) string { - if channel.Type == constant.ChannelTypeGemini { - return strings.TrimPrefix(item.ID, "models/") - } return item.ID })🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@controller/channel_upstream_update.go` around lines 299 - 304, The branch inside the lo.Map call that checks channel.Type == constant.ChannelTypeGemini is dead code because Gemini is handled and returned earlier; remove that conditional and simplify the mapping to always return item.ID (referencing the ids variable, lo.Map function and OpenAIModel type) so the map is consistent with the early-return path, or if Gemini-specific behavior is actually intended, move that logic into the earlier Gemini handling code path instead.
645-651: Batch endpoints load all channels into memory at once — consider paging like the background task.Both
ApplyAllChannelUpstreamModelUpdates(line 646) andDetectAllChannelUpstreamModelUpdates(line 714) callmodel.GetAllChannels(0, 0, true, false), which loads every channel (including keys) into memory. The background task at line 394-443 correctly usesLIMIT/OFFSETbatching. Large deployments with many channels could experience memory pressure from the unbatched API endpoints.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@controller/channel_upstream_update.go` around lines 645 - 651, Both ApplyAllChannelUpstreamModelUpdates and DetectAllChannelUpstreamModelUpdates call model.GetAllChannels(0,0,true,false) which loads every channel (and keys) into memory; change these handlers to iterate in pages like the background task: call model.GetAllChannels with a fixed LIMIT and increasing OFFSET in a loop, process each batch (invoking the existing per-channel update/detect logic), and stop when a batch is empty; ensure you update both functions (ApplyAllChannelUpstreamModelUpdates and DetectAllChannelUpstreamModelUpdates) to avoid building full in-memory slices and preserve the existing response/error behavior per batch.web/src/components/table/channels/modals/ChannelUpstreamUpdateModal.jsx (1)
91-108: Minor:useEffectdeps may cause unexpected resets while the modal is open.The effect depends on
[visible, addTabEnabled, removeTabEnabled, preferredTab]. IfaddModelsorremoveModelsprops change while the modal is open (e.g., due to a background re-render of the parent), all selections are reset. In practice this is unlikely, but if it becomes an issue, consider splitting the reset logic into a separate effect that only fires onvisibletransitions.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@web/src/components/table/channels/modals/ChannelUpstreamUpdateModal.jsx` around lines 91 - 108, Split the current useEffect into two: 1) a dedicated effect that only resets selection state and keyword when the modal opens (run when visible becomes true) — move setSelectedAddModels([]), setSelectedRemoveModels([]) and setKeyword('') into a new useEffect with dependency [visible] and guard so it only runs on visible === true; 2) keep a separate effect that only determines the active tab (uses setActiveTab) and depends on [visible, addTabEnabled, removeTabEnabled, preferredTab] but no longer resets selections/keyword; update references to useEffect, setSelectedAddModels, setSelectedRemoveModels, setKeyword, setActiveTab, addTabEnabled, removeTabEnabled and preferredTab accordingly.web/src/components/table/channels/modals/EditChannelModal.jsx (1)
3134-3168: Consider disabling the ignored-models input when checks are off.
Auto-sync is disabled when detection is off, but the ignore list remains editable, which can be confusing—especially for unsupported types.💡 Optional UX refinement
<Form.Input field='upstream_model_update_ignored_models' label={t('手动忽略模型(逗号分隔)')} placeholder={t('例如:gpt-4.1-nano,gpt-4o-mini')} onChange={(value) => handleInputChange( 'upstream_model_update_ignored_models', value, ) } + disabled={ + !inputs.upstream_model_update_check_enabled || + !MODEL_FETCHABLE_CHANNEL_TYPES.has(inputs.type) + } showClear />🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@web/src/components/table/channels/modals/EditChannelModal.jsx` around lines 3134 - 3168, The ignored-models input remains editable even when detection is off; update the Form.Input with field 'upstream_model_update_ignored_models' to be disabled whenever detection is disabled by using the same flag as the switch (inputs.upstream_model_update_check_enabled) — i.e., add disabled={!inputs.upstream_model_update_check_enabled} to the Form.Input and keep using handleInputChange; this mirrors the Form.Switch ('upstream_model_update_auto_sync_enabled') behavior and prevents editing the ignore list when detection is off.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@web/src/components/table/channels/ChannelsColumnDefs.jsx`:
- Around line 277-323: getUpstreamUpdateMeta currently only parses
record.settings when it's a string, which causes object-form settings from the
API to be ignored; update the function to accept both string and object by
normalizing settings into a parsed object (e.g., if typeof record.settings ===
'string' JSON.parse it, otherwise treat record.settings as the parsed object)
and then derive enabled, pendingAddModels and pendingRemoveModels from that
parsed object with the same trimming/Set/dedup logic and same safe fallbacks in
the try/catch flow.
In `@web/src/hooks/channels/useChannelsData.jsx`:
- Around line 701-756: Wrap the network calls in applyUpstreamUpdates,
applyAllUpstreamUpdates, detectChannelUpstreamUpdates, and
detectAllUpstreamUpdates in try/catch blocks around the API.post(...) calls; on
catch call showError with a useful message (e.g. error.message or t('操作失败')),
ensure any UI cleanup like closeUpstreamUpdateModal() still runs (use finally or
call before return) and avoid letting the rejected promise propagate, and keep
existing success logic (showSuccess, refresh) in the try branch so failures
don't crash the caller.
---
Nitpick comments:
In `@controller/channel_upstream_update.go`:
- Around line 299-304: The branch inside the lo.Map call that checks
channel.Type == constant.ChannelTypeGemini is dead code because Gemini is
handled and returned earlier; remove that conditional and simplify the mapping
to always return item.ID (referencing the ids variable, lo.Map function and
OpenAIModel type) so the map is consistent with the early-return path, or if
Gemini-specific behavior is actually intended, move that logic into the earlier
Gemini handling code path instead.
- Around line 645-651: Both ApplyAllChannelUpstreamModelUpdates and
DetectAllChannelUpstreamModelUpdates call model.GetAllChannels(0,0,true,false)
which loads every channel (and keys) into memory; change these handlers to
iterate in pages like the background task: call model.GetAllChannels with a
fixed LIMIT and increasing OFFSET in a loop, process each batch (invoking the
existing per-channel update/detect logic), and stop when a batch is empty;
ensure you update both functions (ApplyAllChannelUpstreamModelUpdates and
DetectAllChannelUpstreamModelUpdates) to avoid building full in-memory slices
and preserve the existing response/error behavior per batch.
In `@web/src/components/table/channels/modals/ChannelUpstreamUpdateModal.jsx`:
- Around line 91-108: Split the current useEffect into two: 1) a dedicated
effect that only resets selection state and keyword when the modal opens (run
when visible becomes true) — move setSelectedAddModels([]),
setSelectedRemoveModels([]) and setKeyword('') into a new useEffect with
dependency [visible] and guard so it only runs on visible === true; 2) keep a
separate effect that only determines the active tab (uses setActiveTab) and
depends on [visible, addTabEnabled, removeTabEnabled, preferredTab] but no
longer resets selections/keyword; update references to useEffect,
setSelectedAddModels, setSelectedRemoveModels, setKeyword, setActiveTab,
addTabEnabled, removeTabEnabled and preferredTab accordingly.
In `@web/src/components/table/channels/modals/EditChannelModal.jsx`:
- Around line 3134-3168: The ignored-models input remains editable even when
detection is off; update the Form.Input with field
'upstream_model_update_ignored_models' to be disabled whenever detection is
disabled by using the same flag as the switch
(inputs.upstream_model_update_check_enabled) — i.e., add
disabled={!inputs.upstream_model_update_check_enabled} to the Form.Input and
keep using handleInputChange; this mirrors the Form.Switch
('upstream_model_update_auto_sync_enabled') behavior and prevents editing the
ignore list when detection is off.
| const getUpstreamUpdateMeta = (record) => { | ||
| const supported = | ||
| !!record && | ||
| record.children === undefined && | ||
| MODEL_FETCHABLE_CHANNEL_TYPES.has(record.type); | ||
| if (!record || record.children !== undefined) { | ||
| return { | ||
| supported: false, | ||
| enabled: false, | ||
| pendingAddModels: [], | ||
| pendingRemoveModels: [], | ||
| }; | ||
| } | ||
| if (!record.settings || typeof record.settings !== 'string') { | ||
| return { | ||
| supported, | ||
| enabled: false, | ||
| pendingAddModels: [], | ||
| pendingRemoveModels: [], | ||
| }; | ||
| } | ||
| try { | ||
| const parsed = JSON.parse(record.settings); | ||
| const enabled = parsed?.upstream_model_update_check_enabled === true; | ||
| const pendingAddModels = Array.from( | ||
| new Set( | ||
| (parsed?.upstream_model_update_last_detected_models || []) | ||
| .map((model) => String(model || '').trim()) | ||
| .filter(Boolean), | ||
| ), | ||
| ); | ||
| const pendingRemoveModels = Array.from( | ||
| new Set( | ||
| (parsed?.upstream_model_update_last_removed_models || []) | ||
| .map((model) => String(model || '').trim()) | ||
| .filter(Boolean), | ||
| ), | ||
| ); | ||
| return { supported, enabled, pendingAddModels, pendingRemoveModels }; | ||
| } catch (error) { | ||
| return { | ||
| supported, | ||
| enabled: false, | ||
| pendingAddModels: [], | ||
| pendingRemoveModels: [], | ||
| }; | ||
| } |
There was a problem hiding this comment.
Handle object-form settings to avoid disabling upstream update UI.
record.settings is treated as string-only; if the API returns an object, enabled/pending data is ignored and the UI appears disabled. Consider accepting an object as well.
🛠️ Suggested fix
- if (!record.settings || typeof record.settings !== 'string') {
+ const rawSettings = record.settings;
+ if (!rawSettings) {
return {
supported,
enabled: false,
pendingAddModels: [],
pendingRemoveModels: [],
};
}
- try {
- const parsed = JSON.parse(record.settings);
+ let parsed = null;
+ if (typeof rawSettings === 'string') {
+ try {
+ parsed = JSON.parse(rawSettings);
+ } catch (error) {
+ return {
+ supported,
+ enabled: false,
+ pendingAddModels: [],
+ pendingRemoveModels: [],
+ };
+ }
+ } else if (typeof rawSettings === 'object') {
+ parsed = rawSettings;
+ }
+ if (!parsed || typeof parsed !== 'object') {
+ return {
+ supported,
+ enabled: false,
+ pendingAddModels: [],
+ pendingRemoveModels: [],
+ };
+ }
+ try {
const enabled = parsed?.upstream_model_update_check_enabled === true;
const pendingAddModels = Array.from(
new Set(
(parsed?.upstream_model_update_last_detected_models || [])
.map((model) => String(model || '').trim())
.filter(Boolean),
),
);
const pendingRemoveModels = Array.from(
new Set(
(parsed?.upstream_model_update_last_removed_models || [])
.map((model) => String(model || '').trim())
.filter(Boolean),
),
);
return { supported, enabled, pendingAddModels, pendingRemoveModels };
} catch (error) {
return {
supported,
enabled: false,
pendingAddModels: [],
pendingRemoveModels: [],
};
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const getUpstreamUpdateMeta = (record) => { | |
| const supported = | |
| !!record && | |
| record.children === undefined && | |
| MODEL_FETCHABLE_CHANNEL_TYPES.has(record.type); | |
| if (!record || record.children !== undefined) { | |
| return { | |
| supported: false, | |
| enabled: false, | |
| pendingAddModels: [], | |
| pendingRemoveModels: [], | |
| }; | |
| } | |
| if (!record.settings || typeof record.settings !== 'string') { | |
| return { | |
| supported, | |
| enabled: false, | |
| pendingAddModels: [], | |
| pendingRemoveModels: [], | |
| }; | |
| } | |
| try { | |
| const parsed = JSON.parse(record.settings); | |
| const enabled = parsed?.upstream_model_update_check_enabled === true; | |
| const pendingAddModels = Array.from( | |
| new Set( | |
| (parsed?.upstream_model_update_last_detected_models || []) | |
| .map((model) => String(model || '').trim()) | |
| .filter(Boolean), | |
| ), | |
| ); | |
| const pendingRemoveModels = Array.from( | |
| new Set( | |
| (parsed?.upstream_model_update_last_removed_models || []) | |
| .map((model) => String(model || '').trim()) | |
| .filter(Boolean), | |
| ), | |
| ); | |
| return { supported, enabled, pendingAddModels, pendingRemoveModels }; | |
| } catch (error) { | |
| return { | |
| supported, | |
| enabled: false, | |
| pendingAddModels: [], | |
| pendingRemoveModels: [], | |
| }; | |
| } | |
| const getUpstreamUpdateMeta = (record) => { | |
| const supported = | |
| !!record && | |
| record.children === undefined && | |
| MODEL_FETCHABLE_CHANNEL_TYPES.has(record.type); | |
| if (!record || record.children !== undefined) { | |
| return { | |
| supported: false, | |
| enabled: false, | |
| pendingAddModels: [], | |
| pendingRemoveModels: [], | |
| }; | |
| } | |
| const rawSettings = record.settings; | |
| if (!rawSettings) { | |
| return { | |
| supported, | |
| enabled: false, | |
| pendingAddModels: [], | |
| pendingRemoveModels: [], | |
| }; | |
| } | |
| let parsed = null; | |
| if (typeof rawSettings === 'string') { | |
| try { | |
| parsed = JSON.parse(rawSettings); | |
| } catch (error) { | |
| return { | |
| supported, | |
| enabled: false, | |
| pendingAddModels: [], | |
| pendingRemoveModels: [], | |
| }; | |
| } | |
| } else if (typeof rawSettings === 'object') { | |
| parsed = rawSettings; | |
| } | |
| if (!parsed || typeof parsed !== 'object') { | |
| return { | |
| supported, | |
| enabled: false, | |
| pendingAddModels: [], | |
| pendingRemoveModels: [], | |
| }; | |
| } | |
| try { | |
| const enabled = parsed?.upstream_model_update_check_enabled === true; | |
| const pendingAddModels = Array.from( | |
| new Set( | |
| (parsed?.upstream_model_update_last_detected_models || []) | |
| .map((model) => String(model || '').trim()) | |
| .filter(Boolean), | |
| ), | |
| ); | |
| const pendingRemoveModels = Array.from( | |
| new Set( | |
| (parsed?.upstream_model_update_last_removed_models || []) | |
| .map((model) => String(model || '').trim()) | |
| .filter(Boolean), | |
| ), | |
| ); | |
| return { supported, enabled, pendingAddModels, pendingRemoveModels }; | |
| } catch (error) { | |
| return { | |
| supported, | |
| enabled: false, | |
| pendingAddModels: [], | |
| pendingRemoveModels: [], | |
| }; | |
| } | |
| }; |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@web/src/components/table/channels/ChannelsColumnDefs.jsx` around lines 277 -
323, getUpstreamUpdateMeta currently only parses record.settings when it's a
string, which causes object-form settings from the API to be ignored; update the
function to accept both string and object by normalizing settings into a parsed
object (e.g., if typeof record.settings === 'string' JSON.parse it, otherwise
treat record.settings as the parsed object) and then derive enabled,
pendingAddModels and pendingRemoveModels from that parsed object with the same
trimming/Set/dedup logic and same safe fallbacks in the try/catch flow.
| const applyUpstreamUpdates = async ({ | ||
| addModels: selectedAddModels = [], | ||
| removeModels: selectedRemoveModels = [], | ||
| } = {}) => { | ||
| if (!upstreamUpdateChannel?.id) { | ||
| closeUpstreamUpdateModal(); | ||
| return; | ||
| } | ||
|
|
||
| const normalizedSelectedAddModels = Array.from( | ||
| new Set( | ||
| (selectedAddModels || []) | ||
| .map((model) => String(model || '').trim()) | ||
| .filter(Boolean), | ||
| ), | ||
| ); | ||
| const normalizedSelectedRemoveModels = Array.from( | ||
| new Set( | ||
| (selectedRemoveModels || []) | ||
| .map((model) => String(model || '').trim()) | ||
| .filter(Boolean), | ||
| ), | ||
| ); | ||
| const selectedAddSet = new Set(normalizedSelectedAddModels); | ||
| const ignoreModels = upstreamUpdateAddModels.filter( | ||
| (model) => !selectedAddSet.has(model), | ||
| ); | ||
|
|
||
| const res = await API.post('/api/channel/upstream_updates/apply', { | ||
| id: upstreamUpdateChannel.id, | ||
| add_models: normalizedSelectedAddModels, | ||
| ignore_models: ignoreModels, | ||
| remove_models: normalizedSelectedRemoveModels, | ||
| }); | ||
| const { success, message, data } = res.data || {}; | ||
| if (!success) { | ||
| showError(message || t('操作失败')); | ||
| return; | ||
| } | ||
|
|
||
| const addedCount = data?.added_models?.length || 0; | ||
| const removedCount = data?.removed_models?.length || 0; | ||
| const ignoredCount = data?.ignored_models?.length || 0; | ||
| showSuccess( | ||
| t( | ||
| '已处理上游模型更新:加入 {{added}} 个,删除 {{removed}} 个,忽略 {{ignored}} 个', | ||
| { | ||
| added: addedCount, | ||
| removed: removedCount, | ||
| ignored: ignoredCount, | ||
| }, | ||
| ), | ||
| ); | ||
| closeUpstreamUpdateModal(); | ||
| await refresh(); | ||
| }; |
There was a problem hiding this comment.
Missing try/catch — unhandled network errors will crash the caller.
applyUpstreamUpdates, applyAllUpstreamUpdates, detectChannelUpstreamUpdates, and detectAllUpstreamUpdates all perform API.post(...) without a try/catch. If the request fails (e.g., network timeout), the rejected promise propagates unhandled and may result in an uncaught error. While some existing functions in this hook follow the same pattern, these new functions are called from UI event handlers where a showError fallback would be more user-friendly.
Example for applyUpstreamUpdates
const applyUpstreamUpdates = async ({
addModels: selectedAddModels = [],
removeModels: selectedRemoveModels = [],
} = {}) => {
if (!upstreamUpdateChannel?.id) {
closeUpstreamUpdateModal();
return;
}
-
- const normalizedSelectedAddModels = Array.from(
- ...
- );
+ try {
+ const normalizedSelectedAddModels = Array.from(
+ ...
+ );
...
- await refresh();
+ await refresh();
+ } catch (error) {
+ showError(error);
+ }
};🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@web/src/hooks/channels/useChannelsData.jsx` around lines 701 - 756, Wrap the
network calls in applyUpstreamUpdates, applyAllUpstreamUpdates,
detectChannelUpstreamUpdates, and detectAllUpstreamUpdates in try/catch blocks
around the API.post(...) calls; on catch call showError with a useful message
(e.g. error.message or t('操作失败')), ensure any UI cleanup like
closeUpstreamUpdateModal() still runs (use finally or call before return) and
avoid letting the rejected promise propagate, and keep existing success logic
(showSuccess, refresh) in the try branch so failures don't crash the caller.
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
controller/channel_upstream_update.go (1)
307-314: Dead code: Gemini branch insidelo.Mapis unreachable.The Gemini channel type returns early at Lines 248–259 before reaching the
lo.Mapcall, sochannel.Type == constant.ChannelTypeGeminiat Line 309 can never be true.♻️ Proposed fix — remove the dead branch
ids := lo.Map(result.Data, func(item OpenAIModel, _ int) string { - if channel.Type == constant.ChannelTypeGemini { - return strings.TrimPrefix(item.ID, "models/") - } return item.ID })🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@controller/channel_upstream_update.go` around lines 307 - 314, The conditional branch inside the lo.Map call is dead because the Gemini path already returns early, so update the mapping in the ids assignment (lo.Map over result.Data/OpenAIModel) to drop the unreachable channel.Type == constant.ChannelTypeGemini check and simply return item.ID (or perform any necessary normalizing outside this function), removing the redundant logic tied to channel.Type in ids.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@controller/channel_upstream_update.go`:
- Around line 489-497: runChannelUpstreamModelUpdateTaskOnce is selecting
channel rows but omits the model_mapping column so channel.ModelMapping is nil
and normalizeChannelModelMapping cannot populate redirectSourceSet, causing
false pending adds/removes; fix by including "model_mapping" in the GORM Select
call used in runChannelUpstreamModelUpdateTaskOnce (the query that builds
channels slice) so normalizeChannelModelMapping sees actual mappings and
redirectSourceSet/pendingRemoveModels and coveredUpstreamSet logic behave
correctly.
In `@controller/user.go`:
- Around line 1098-1111: The constructed settings in UpdateUserSetting currently
overwrites the whole user setting JSON and omits SidebarModules,
BillingPreference, and Language, causing data loss; when building the
dto.UserSetting before calling user.SetSetting(settings), copy these fields from
existingSettings (e.g., settings.SidebarModules =
existingSettings.SidebarModules, settings.BillingPreference =
existingSettings.BillingPreference, settings.Language =
existingSettings.Language) so unrelated preferences are preserved (retain the
existing handling for UpstreamModelUpdateNotifyEnabled and other updated
fields).
---
Nitpick comments:
In `@controller/channel_upstream_update.go`:
- Around line 307-314: The conditional branch inside the lo.Map call is dead
because the Gemini path already returns early, so update the mapping in the ids
assignment (lo.Map over result.Data/OpenAIModel) to drop the unreachable
channel.Type == constant.ChannelTypeGemini check and simply return item.ID (or
perform any necessary normalizing outside this function), removing the redundant
logic tied to channel.Type in ids.
| for { | ||
| var channels []*model.Channel | ||
| err := model.DB. | ||
| Select("id", "name", "type", "key", "status", "base_url", "models", "settings", "setting", "other", "group", "priority", "weight", "tag", "channel_info", "header_override"). | ||
| Where("status = ?", common.ChannelStatusEnabled). | ||
| Order("id asc"). | ||
| Limit(channelUpstreamModelUpdateTaskBatchSize). | ||
| Offset(offset). | ||
| Find(&channels).Error |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
# Confirm the exact SELECT statement and check for model_mapping
sed -n '489,497p' controller/channel_upstream_update.goRepository: QuantumNous/new-api
Length of output: 465
🏁 Script executed:
# Check the Channel model struct to confirm field naming
rg -A 50 "^type Channel struct" model/ --type=go | head -100Repository: QuantumNous/new-api
Length of output: 3937
🏁 Script executed:
# Verify the normalizeChannelModelMapping function
rg -B 3 -A 15 "func normalizeChannelModelMapping" controller/ --type=goRepository: QuantumNous/new-api
Length of output: 1448
🏁 Script executed:
# Search for model_mapping usage in SELECT statements elsewhere
rg "Select.*model_mapping" controller/ --type=go -iRepository: QuantumNous/new-api
Length of output: 45
🏁 Script executed:
# Verify the impact: check how normalizeChannelModelMapping is used
rg -B 5 -A 20 "func collectPendingUpstreamModelChangesFromModels" controller/channel_upstream_update.go --type=goRepository: QuantumNous/new-api
Length of output: 834
🏁 Script executed:
# Confirm redirectSourceSet/redirectTargetSet logic when mapping is nil
rg -B 3 -A 10 "redirectSourceSet\|redirectTargetSet" controller/channel_upstream_update.go --type=go | head -50Repository: QuantumNous/new-api
Length of output: 45
🏁 Script executed:
# Verify the exact line where normalizeChannelModelMapping is called in context
sed -n '205,225p' controller/channel_upstream_update.goRepository: QuantumNous/new-api
Length of output: 851
🏁 Script executed:
# Get the complete function collectPendingUpstreamModelChangesFromModels to see the impact
sed -n '127,180p' controller/channel_upstream_update.goRepository: QuantumNous/new-api
Length of output: 1744
🏁 Script executed:
# Check if there are other SELECT statements that DO include model_mapping
rg "Select\(" controller/ --type=go -A 1 | grep -i "model_mapping"Repository: QuantumNous/new-api
Length of output: 45
🏁 Script executed:
# Get the rest of collectPendingUpstreamModelChangesFromModels to see pending removal logic
sed -n '180,210p' controller/channel_upstream_update.goRepository: QuantumNous/new-api
Length of output: 1072
🏁 Script executed:
# Verify how pendingRemoveModels is constructed
rg -A 5 "pendingRemoveModels" controller/channel_upstream_update.go | head -30Repository: QuantumNous/new-api
Length of output: 1348
Add model_mapping to SELECT statement — missing field causes false-positive pending model changes for channels with aliases.
runChannelUpstreamModelUpdateTaskOnce omits model_mapping from its batch SELECT statement. When GORM loads channels without this column, channel.ModelMapping is always nil, causing normalizeChannelModelMapping to return nil. This breaks the alias (redirect mapping) logic downstream:
- False pending removals: redirect source models (e.g.,
"my-alias"→"gpt-4") are not protected by theredirectSourceSetcheck because the set is empty. Any source alias absent from the upstream list incorrectly lands inpendingRemoveModels. - False pending additions: redirect target models are not added to
coveredUpstreamSet, so they surface aspendingAddModels. For channels withUpstreamModelUpdateAutoSyncEnabled, targets could be auto-added as independent models.
The code comment at line 191–192 explicitly documents this intent: "Redirect source models are virtual aliases and should not be removed only because they are absent from upstream model list." The protection only works when redirectSourceSet is populated, which requires loading model_mapping.
Fix — add `model_mapping` to the SELECT
err := model.DB.
- Select("id", "name", "type", "key", "status", "base_url", "models", "settings", "setting", "other", "group", "priority", "weight", "tag", "channel_info", "header_override").
+ Select("id", "name", "type", "key", "status", "base_url", "models", "model_mapping", "settings", "setting", "other", "group", "priority", "weight", "tag", "channel_info", "header_override").
Where("status = ?", common.ChannelStatusEnabled).🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@controller/channel_upstream_update.go` around lines 489 - 497,
runChannelUpstreamModelUpdateTaskOnce is selecting channel rows but omits the
model_mapping column so channel.ModelMapping is nil and
normalizeChannelModelMapping cannot populate redirectSourceSet, causing false
pending adds/removes; fix by including "model_mapping" in the GORM Select call
used in runChannelUpstreamModelUpdateTaskOnce (the query that builds channels
slice) so normalizeChannelModelMapping sees actual mappings and
redirectSourceSet/pendingRemoveModels and coveredUpstreamSet logic behave
correctly.
| existingSettings := user.GetSetting() | ||
| upstreamModelUpdateNotifyEnabled := existingSettings.UpstreamModelUpdateNotifyEnabled | ||
| if user.Role >= common.RoleAdminUser && req.UpstreamModelUpdateNotifyEnabled != nil { | ||
| upstreamModelUpdateNotifyEnabled = *req.UpstreamModelUpdateNotifyEnabled | ||
| } | ||
|
|
||
| // 构建设置 | ||
| settings := dto.UserSetting{ | ||
| NotifyType: req.QuotaWarningType, | ||
| QuotaWarningThreshold: req.QuotaWarningThreshold, | ||
| AcceptUnsetRatioModel: req.AcceptUnsetModelRatioModel, | ||
| RecordIpLog: req.RecordIpLog, | ||
| NotifyType: req.QuotaWarningType, | ||
| QuotaWarningThreshold: req.QuotaWarningThreshold, | ||
| UpstreamModelUpdateNotifyEnabled: upstreamModelUpdateNotifyEnabled, | ||
| AcceptUnsetRatioModel: req.AcceptUnsetModelRatioModel, | ||
| RecordIpLog: req.RecordIpLog, | ||
| } |
There was a problem hiding this comment.
Data loss: SidebarModules, BillingPreference, and Language are silently cleared on every notification-settings save.
UpdateUserSetting now reads existingSettings to preserve UpstreamModelUpdateNotifyEnabled, but the settings struct it constructs does not carry over existingSettings.SidebarModules, existingSettings.BillingPreference, or existingSettings.Language. Because user.SetSetting(settings) serialises and replaces the entire setting JSON, any values those fields held (written via separate UpdateSelf / language-preference calls) are destroyed whenever the user saves their notification settings.
🛡️ Proposed fix — carry all unrelated fields forward from existingSettings
existingSettings := user.GetSetting()
upstreamModelUpdateNotifyEnabled := existingSettings.UpstreamModelUpdateNotifyEnabled
if user.Role >= common.RoleAdminUser && req.UpstreamModelUpdateNotifyEnabled != nil {
upstreamModelUpdateNotifyEnabled = *req.UpstreamModelUpdateNotifyEnabled
}
// 构建设置
settings := dto.UserSetting{
NotifyType: req.QuotaWarningType,
QuotaWarningThreshold: req.QuotaWarningThreshold,
UpstreamModelUpdateNotifyEnabled: upstreamModelUpdateNotifyEnabled,
AcceptUnsetRatioModel: req.AcceptUnsetModelRatioModel,
RecordIpLog: req.RecordIpLog,
+ // Preserve fields managed by other update paths
+ SidebarModules: existingSettings.SidebarModules,
+ BillingPreference: existingSettings.BillingPreference,
+ Language: existingSettings.Language,
}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@controller/user.go` around lines 1098 - 1111, The constructed settings in
UpdateUserSetting currently overwrites the whole user setting JSON and omits
SidebarModules, BillingPreference, and Language, causing data loss; when
building the dto.UserSetting before calling user.SetSetting(settings), copy
these fields from existingSettings (e.g., settings.SidebarModules =
existingSettings.SidebarModules, settings.BillingPreference =
existingSettings.BillingPreference, settings.Language =
existingSettings.Language) so unrelated preferences are preserved (retain the
existing handling for UpstreamModelUpdateNotifyEnabled and other updated
fields).
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
web/src/components/table/channels/modals/ChannelUpstreamUpdateModal.jsx (1)
119-121:checkedCountuses O(n×m)Array.includes— prefer aSetfor O(1) lookups.♻️ Proposed refactor
+ const currentSelectedSet = useMemo( + () => new Set(currentSelectedModels), + [currentSelectedModels], + ); - const checkedCount = currentModels.filter((model) => - currentSelectedModels.includes(model), - ).length; + const checkedCount = currentModels.filter((model) => + currentSelectedSet.has(model), + ).length;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@web/src/components/table/channels/modals/ChannelUpstreamUpdateModal.jsx` around lines 119 - 121, The current implementation of checkedCount iterates currentModels and calls currentSelectedModels.includes for each element causing O(n×m) complexity; fix by creating a Set from currentSelectedModels (e.g., selectedSet = new Set(currentSelectedModels)) and then compute checkedCount by testing membership with selectedSet.has for O(1) lookups when iterating currentModels (refer to checkedCount, currentModels, and currentSelectedModels to locate the change).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@web/src/components/table/channels/modals/ChannelUpstreamUpdateModal.jsx`:
- Around line 92-110: The effect currently resets selections whenever
addTabEnabled/removeTabEnabled change while visible is true; change it to only
run the reset when visible transitions from false to true by tracking previous
visible (e.g., a useRef prevVisible) and in the useEffect check if visible &&
!prevVisible.current before calling setSelectedAddModels,
setSelectedRemoveModels, setKeyword, setPartialSubmitConfirmed and setting the
active tab (use preferredTab/addTabEnabled/removeTabEnabled to choose the tab),
then update prevVisible.current = visible; keep the same dependency list so the
guard controls execution.
- Around line 30-33: Add the missing external dependency
`@douyinfe/semi-illustrations` (used by the imports IllustrationNoResult and
IllustrationNoResultDark in ChannelUpstreamUpdateModal.jsx) to the web project's
package.json dependencies and then run bun install to update lockfiles; ensure
the package is listed in "dependencies" (not just devDependencies) so the build
can resolve those imports at compile time.
---
Nitpick comments:
In `@web/src/components/table/channels/modals/ChannelUpstreamUpdateModal.jsx`:
- Around line 119-121: The current implementation of checkedCount iterates
currentModels and calls currentSelectedModels.includes for each element causing
O(n×m) complexity; fix by creating a Set from currentSelectedModels (e.g.,
selectedSet = new Set(currentSelectedModels)) and then compute checkedCount by
testing membership with selectedSet.has for O(1) lookups when iterating
currentModels (refer to checkedCount, currentModels, and currentSelectedModels
to locate the change).
| import { | ||
| IllustrationNoResult, | ||
| IllustrationNoResultDark, | ||
| } from '@douyinfe/semi-illustrations'; |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
cat web/package.json | jq '.dependencies, .devDependencies' | grep -A 2 -B 2 "semi-illustrations" || echo "Dependency not found in dependencies or devDependencies"Repository: QuantumNous/new-api
Length of output: 118
Add @douyinfe/semi-illustrations to web/package.json.
The import at lines 30-33 references @douyinfe/semi-illustrations, which is a separate package from @douyinfe/semi-ui and is not listed in dependencies or devDependencies. This will cause the build to fail. Add this package to web/package.json and run bun install.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@web/src/components/table/channels/modals/ChannelUpstreamUpdateModal.jsx`
around lines 30 - 33, Add the missing external dependency
`@douyinfe/semi-illustrations` (used by the imports IllustrationNoResult and
IllustrationNoResultDark in ChannelUpstreamUpdateModal.jsx) to the web project's
package.json dependencies and then run bun install to update lockfiles; ensure
the package is listed in "dependencies" (not just devDependencies) so the build
can resolve those imports at compile time.
| useEffect(() => { | ||
| if (!visible) { | ||
| return; | ||
| } | ||
| setSelectedAddModels([]); | ||
| setSelectedRemoveModels([]); | ||
| setKeyword(''); | ||
| setPartialSubmitConfirmed(false); | ||
| const normalizedPreferredTab = preferredTab === 'remove' ? 'remove' : 'add'; | ||
| if (normalizedPreferredTab === 'remove' && removeTabEnabled) { | ||
| setActiveTab('remove'); | ||
| return; | ||
| } | ||
| if (normalizedPreferredTab === 'add' && addTabEnabled) { | ||
| setActiveTab('add'); | ||
| return; | ||
| } | ||
| setActiveTab(addTabEnabled ? 'add' : 'remove'); | ||
| }, [visible, addTabEnabled, removeTabEnabled, preferredTab]); |
There was a problem hiding this comment.
State reset fires on addTabEnabled/removeTabEnabled changes while modal is already visible.
The useEffect resets all user selections and the keyword filter whenever addTabEnabled or removeTabEnabled changes — not just when visible transitions to true. If the parent ever updates addModels/removeModels while the modal is already open (e.g., a re-fetch in the background), any selections the user has made are silently wiped.
Guard the reset to only fire on the visible: false → true transition:
🛡️ Proposed fix
- useEffect(() => {
- if (!visible) {
- return;
- }
- setSelectedAddModels([]);
- setSelectedRemoveModels([]);
- setKeyword('');
- setPartialSubmitConfirmed(false);
- const normalizedPreferredTab = preferredTab === 'remove' ? 'remove' : 'add';
- if (normalizedPreferredTab === 'remove' && removeTabEnabled) {
- setActiveTab('remove');
- return;
- }
- if (normalizedPreferredTab === 'add' && addTabEnabled) {
- setActiveTab('add');
- return;
- }
- setActiveTab(addTabEnabled ? 'add' : 'remove');
- }, [visible, addTabEnabled, removeTabEnabled, preferredTab]);
+ useEffect(() => {
+ if (!visible) {
+ return;
+ }
+ setSelectedAddModels([]);
+ setSelectedRemoveModels([]);
+ setKeyword('');
+ setPartialSubmitConfirmed(false);
+ const normalizedPreferredTab = preferredTab === 'remove' ? 'remove' : 'add';
+ if (normalizedPreferredTab === 'remove' && removeTabEnabled) {
+ setActiveTab('remove');
+ return;
+ }
+ if (normalizedPreferredTab === 'add' && addTabEnabled) {
+ setActiveTab('add');
+ return;
+ }
+ setActiveTab(addTabEnabled ? 'add' : 'remove');
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [visible, preferredTab]);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@web/src/components/table/channels/modals/ChannelUpstreamUpdateModal.jsx`
around lines 92 - 110, The effect currently resets selections whenever
addTabEnabled/removeTabEnabled change while visible is true; change it to only
run the reset when visible transitions from false to true by tracking previous
visible (e.g., a useRef prevVisible) and in the useEffect check if visible &&
!prevVisible.current before calling setSelectedAddModels,
setSelectedRemoveModels, setKeyword, setPartialSubmitConfirmed and setting the
active tab (use preferredTab/addTabEnabled/removeTabEnabled to choose the tab),
then update prevVisible.current = visible; keep the same dependency list so the
guard controls execution.
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
web/src/components/table/channels/modals/EditChannelModal.jsx (2)
243-244:upstreamDetectedModelsOmittedCountcould be derived inside the preview memo for consistency
upstreamDetectedModelsOmittedCountis the only derived value in this trio that isn't wrapped inuseMemo. Since it's a primitive derived from two already-memoized.lengthvalues, there's no correctness issue, but it's stylistically inconsistent.♻️ Suggested consolidation
- const upstreamDetectedModelsPreview = useMemo( - () => upstreamDetectedModels.slice(0, UPSTREAM_DETECTED_MODEL_PREVIEW_LIMIT), - [upstreamDetectedModels], - ); - const upstreamDetectedModelsOmittedCount = - upstreamDetectedModels.length - upstreamDetectedModelsPreview.length; + const { upstreamDetectedModelsPreview, upstreamDetectedModelsOmittedCount } = + useMemo(() => { + const preview = upstreamDetectedModels.slice( + 0, + UPSTREAM_DETECTED_MODEL_PREVIEW_LIMIT, + ); + return { + upstreamDetectedModelsPreview: preview, + upstreamDetectedModelsOmittedCount: + upstreamDetectedModels.length - preview.length, + }; + }, [upstreamDetectedModels]);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@web/src/components/table/channels/modals/EditChannelModal.jsx` around lines 243 - 244, Move the primitive derivation for upstreamDetectedModelsOmittedCount into the same useMemo that creates upstreamDetectedModelsPreview so both derived values are computed together for consistency; update the memo (the useMemo that currently returns upstreamDetectedModelsPreview) to return an object or tuple containing preview and omittedCount (compute omittedCount as upstreamDetectedModels.length - upstreamDetectedModelsPreview.length inside that memo), then replace the standalone upstreamDetectedModelsOmittedCount const with a destructured value from the memo result where upstreamDetectedModelsPreview is used.
569-575:toLocaleString()without locale/options produces locale-dependent outputDifferent browsers/locales will render the timestamp differently (12h vs 24h, date order, separators). Consider passing explicit locale and format options if a consistent display is desired.
♻️ Suggested fix
- return new Date(value * 1000).toLocaleString(); + return new Date(value * 1000).toLocaleString('zh-CN', { + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@web/src/components/table/channels/modals/EditChannelModal.jsx` around lines 569 - 575, The formatUnixTime function currently calls new Date(...).toLocaleString() without locale or options; update formatUnixTime to format dates consistently by using Intl.DateTimeFormat (or toLocaleString with an explicit locale and options) when constructing the display string for the Date created in formatUnixTime so the output is deterministic across browsers/locales—locate the formatUnixTime arrow function and replace the plain toLocaleString call with a DateTimeFormat configured (e.g., explicit locale and time/date options or a chosen locale like 'en-GB'/'en-US' plus hour12 flag) to produce a consistent format.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@web/src/components/table/channels/modals/EditChannelModal.jsx`:
- Around line 3152-3212: The Advanced Settings UI for upstream models is
rendered for all channel types but should only appear for fetchable channels;
wrap the entire block that contains Form.Switch
field='upstream_model_update_auto_sync_enabled', Form.Input
field='upstream_model_update_ignored_models', and the detected-models display
(which uses upstreamDetectedModels and
upstreamDetectedModelsPreview/upstreamDetectedModelsOmittedCount) with the same
guard used for the check switch: MODEL_FETCHABLE_CHANNEL_TYPES.has(inputs.type).
This ensures the auto-sync switch (dependent on
upstream_model_update_check_enabled), ignored-models input, and "上次检测到可加入模型"
display are only shown for fetchable channel types.
---
Duplicate comments:
In `@web/src/components/table/channels/modals/ChannelUpstreamUpdateModal.jsx`:
- Around line 92-110: The effect in ChannelUpstreamUpdateModal.jsx currently
resets state whenever addTabEnabled/removeTabEnabled change; change it so the
reset runs only on the transition visible: false → true. Inside the existing
useEffect (the one referencing visible, addTabEnabled, removeTabEnabled,
preferredTab), track previous visible using a ref (e.g., prevVisibleRef) and
only perform the resets and activeTab logic when prevVisibleRef.current is false
and visible is true; update prevVisibleRef.current = visible at the end. This
preserves the setSelectedAddModels, setSelectedRemoveModels, setKeyword,
setPartialSubmitConfirmed, and setActiveTab logic but prevents resets while the
modal is already open.
- Around line 30-33: The import of IllustrationNoResult and
IllustrationNoResultDark in ChannelUpstreamUpdateModal.jsx requires adding the
missing dependency; add "@douyinfe/semi-illustrations" to web/package.json
(choose the project's preferred version or run the package manager to pick a
compatible version), run npm/yarn/pnpm install to update node_modules and the
lockfile, and commit the updated package.json and lockfile so CI/builds succeed.
---
Nitpick comments:
In `@web/src/components/table/channels/modals/EditChannelModal.jsx`:
- Around line 243-244: Move the primitive derivation for
upstreamDetectedModelsOmittedCount into the same useMemo that creates
upstreamDetectedModelsPreview so both derived values are computed together for
consistency; update the memo (the useMemo that currently returns
upstreamDetectedModelsPreview) to return an object or tuple containing preview
and omittedCount (compute omittedCount as upstreamDetectedModels.length -
upstreamDetectedModelsPreview.length inside that memo), then replace the
standalone upstreamDetectedModelsOmittedCount const with a destructured value
from the memo result where upstreamDetectedModelsPreview is used.
- Around line 569-575: The formatUnixTime function currently calls new
Date(...).toLocaleString() without locale or options; update formatUnixTime to
format dates consistently by using Intl.DateTimeFormat (or toLocaleString with
an explicit locale and options) when constructing the display string for the
Date created in formatUnixTime so the output is deterministic across
browsers/locales—locate the formatUnixTime arrow function and replace the plain
toLocaleString call with a DateTimeFormat configured (e.g., explicit locale and
time/date options or a chosen locale like 'en-GB'/'en-US' plus hour12 flag) to
produce a consistent format.
| <Form.Switch | ||
| field='upstream_model_update_auto_sync_enabled' | ||
| label={t('是否自动同步上游模型更新')} | ||
| checkedText={t('开')} | ||
| uncheckedText={t('关')} | ||
| disabled={!inputs.upstream_model_update_check_enabled} | ||
| onChange={(value) => | ||
| handleChannelOtherSettingsChange( | ||
| 'upstream_model_update_auto_sync_enabled', | ||
| value, | ||
| ) | ||
| } | ||
| extraText={t( | ||
| '开启后检测到新增模型会自动加入当前渠道模型列表', | ||
| )} | ||
| /> | ||
|
|
||
| <Form.Input | ||
| field='upstream_model_update_ignored_models' | ||
| label={t('手动忽略模型(逗号分隔)')} | ||
| placeholder={t('例如:gpt-4.1-nano,gpt-4o-mini')} | ||
| onChange={(value) => | ||
| handleInputChange( | ||
| 'upstream_model_update_ignored_models', | ||
| value, | ||
| ) | ||
| } | ||
| showClear | ||
| /> | ||
|
|
||
| <div className='text-xs text-gray-500 mb-3'> | ||
| {t('上次检测到可加入模型')}: | ||
| {upstreamDetectedModels.length === 0 ? ( | ||
| t('暂无') | ||
| ) : ( | ||
| <> | ||
| <Tooltip | ||
| position='topLeft' | ||
| content={ | ||
| <div className='max-w-[640px] break-all text-xs leading-5'> | ||
| {upstreamDetectedModels.join(', ')} | ||
| </div> | ||
| } | ||
| > | ||
| <span className='cursor-help break-all'> | ||
| {upstreamDetectedModelsPreview.join(', ')} | ||
| </span> | ||
| </Tooltip> | ||
| <span className='ml-1 text-gray-400'> | ||
| {upstreamDetectedModelsOmittedCount > 0 | ||
| ? t('(共 {{total}} 个,省略 {{omit}} 个)', { | ||
| total: upstreamDetectedModels.length, | ||
| omit: upstreamDetectedModelsOmittedCount, | ||
| }) | ||
| : t('(共 {{total}} 个)', { | ||
| total: upstreamDetectedModels.length, | ||
| })} | ||
| </span> | ||
| </> | ||
| )} | ||
| </div> |
There was a problem hiding this comment.
Upstream Advanced Settings block not gated by MODEL_FETCHABLE_CHANNEL_TYPES — causes permanently disabled/dead UI for non-fetchable channel types
The upstream_model_update_check_enabled switch in the Model Configuration card is correctly wrapped in MODEL_FETCHABLE_CHANNEL_TYPES.has(inputs.type), but the three related controls in Advanced Settings (auto-sync switch, ignored models input, detected models display) are rendered unconditionally for every channel type.
For non-fetchable types, the effect is:
- Auto-sync switch is always disabled (its dependency
check_enabledis never enabled because the enabling switch is hidden) with no contextual explanation for the user. - Ignored models input is fully editable and persists values, but the backend has no use for them.
- "上次检测到可加入模型" always shows "暂无" as dead noise.
🐛 Proposed fix — wrap the block in the same type guard
+ {MODEL_FETCHABLE_CHANNEL_TYPES.has(inputs.type) && (
+ <>
<Form.Switch
field='upstream_model_update_auto_sync_enabled'
label={t('是否自动同步上游模型更新')}
...
/>
<Form.Input
field='upstream_model_update_ignored_models'
...
/>
<div className='text-xs text-gray-500 mb-3'>
{t('上次检测到可加入模型')}:
...
</div>
+ </>
+ )}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@web/src/components/table/channels/modals/EditChannelModal.jsx` around lines
3152 - 3212, The Advanced Settings UI for upstream models is rendered for all
channel types but should only appear for fetchable channels; wrap the entire
block that contains Form.Switch field='upstream_model_update_auto_sync_enabled',
Form.Input field='upstream_model_update_ignored_models', and the detected-models
display (which uses upstreamDetectedModels and
upstreamDetectedModelsPreview/upstreamDetectedModelsOmittedCount) with the same
guard used for the check switch: MODEL_FETCHABLE_CHANNEL_TYPES.has(inputs.type).
This ensures the auto-sync switch (dependent on
upstream_model_update_check_enabled), ignored-models input, and "上次检测到可加入模型"
display are only shown for fetchable channel types.
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
controller/channel_upstream_update.go (2)
314-319: Dead code: unreachable Gemini branch inlo.Map.
channel.Type == constant.ChannelTypeGeminiis handled earlier with an earlyreturnat line 264, so the innerChannelTypeGeminicheck at line 315 can never be reached. Remove it.♻️ Proposed cleanup
ids := lo.Map(result.Data, func(item OpenAIModel, _ int) string { - if channel.Type == constant.ChannelTypeGemini { - return strings.TrimPrefix(item.ID, "models/") - } return item.ID })🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@controller/channel_upstream_update.go` around lines 314 - 319, The inner Gemini branch inside the lo.Map call is dead because channel.Type == constant.ChannelTypeGemini is already handled by an earlier early return; update the mapping to always return item.ID (remove the strings.TrimPrefix branch and the channel.Type check) so ids := lo.Map(result.Data, func(item OpenAIModel, _ int) string { return item.ID }) and remove any now-unused imports (e.g., strings) caused by the removed branch.
877-923:DetectAllChannelUpstreamModelUpdatesmakes unbounded synchronous upstream HTTP calls without any rate limiting.The background task (
runChannelUpstreamModelUpdateTaskOnce) sleepscommon.RequestIntervalbetween channel fetches. This HTTP handler callscheckAndPersistChannelUpstreamModelUpdates→fetchChannelUpstreamModelIDsin a tight loop with no delay or concurrency limit. For deployments with many enabled channels, this will:
- Exhaust upstream API rate limits.
- Hold the Gin handler goroutine for the full duration, risking client-side HTTP timeouts.
- Potentially cause cascading failures if upstream providers throttle or block.
Consider moving detection work to the background task with a manual trigger flag, or at minimum mirror the same
time.Sleep(common.RequestInterval)used inrunChannelUpstreamModelUpdateTaskOnce:♻️ Minimal rate-limiting fix (mirrors background task pattern)
modelsChanged, autoAdded, err := checkAndPersistChannelUpstreamModelUpdates(channel, &settings, true, false) if err != nil { failed = append(failed, channel.Id) continue } +if common.RequestInterval > 0 { + time.Sleep(common.RequestInterval) +}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@controller/channel_upstream_update.go` around lines 877 - 923, DetectAllChannelUpstreamModelUpdates currently calls checkAndPersistChannelUpstreamModelUpdates (which calls fetchChannelUpstreamModelIDs) in a tight loop for every enabled channel causing unbounded upstream requests and long-running handler execution; either convert this handler to only enqueue work for the existing background worker (runChannelUpstreamModelUpdateTaskOnce) or, at minimum, throttle the loop by adding rate-limiting identical to the background task (e.g. sleep time.Sleep(common.RequestInterval) between channel checks) and/or limit concurrency (worker pool with bounded goroutines) to ensure the handler returns quickly and upstream API rate limits are respected. Ensure any enqueuing uses the same settings.UpstreamModelUpdate… flags and that errors/failures are tracked as before so behavior remains consistent with checkAndPersistChannelUpstreamModelUpdates.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@controller/channel_upstream_update.go`:
- Line 517: The Select(...) call in channel_upstream_update.go uses raw reserved
SQL identifiers "key" and "group" which will break on MySQL because GORM v2
doesn't auto-quote; replace those literals with the already-defined quoted
variables commonKeyCol and commonGroupCol (from model/main.go) in the Select
list, and also add the missing model_mapping column to the selection so the
query returns that field; update the Select invocation that currently lists
"id", "name", "type", "key", "status", "base_url", "models", "settings",
"setting", "other", "group", "priority", "weight", "tag", "channel_info",
"header_override" to use commonKeyCol and commonGroupCol and include
model_mapping.
---
Duplicate comments:
In `@controller/channel_upstream_update.go`:
- Around line 516-522: The SELECT query that loads channels misses the
"model_mapping" column, so channel.ModelMapping stays nil and
normalizeChannelModelMapping / redirectSourceSet / redirectTargetSet behave as
if aliases have no mappings; update the channel load query (the chained DB call
that builds
Select(...).Where(...).Order(...).Limit(...).Offset(...).Find(&channels).Error)
to include "model_mapping" in the Select list so ModelMapping is populated and
the alias-protection logic (normalizeChannelModelMapping, redirectSourceSet,
redirectTargetSet) works correctly.
---
Nitpick comments:
In `@controller/channel_upstream_update.go`:
- Around line 314-319: The inner Gemini branch inside the lo.Map call is dead
because channel.Type == constant.ChannelTypeGemini is already handled by an
earlier early return; update the mapping to always return item.ID (remove the
strings.TrimPrefix branch and the channel.Type check) so ids :=
lo.Map(result.Data, func(item OpenAIModel, _ int) string { return item.ID }) and
remove any now-unused imports (e.g., strings) caused by the removed branch.
- Around line 877-923: DetectAllChannelUpstreamModelUpdates currently calls
checkAndPersistChannelUpstreamModelUpdates (which calls
fetchChannelUpstreamModelIDs) in a tight loop for every enabled channel causing
unbounded upstream requests and long-running handler execution; either convert
this handler to only enqueue work for the existing background worker
(runChannelUpstreamModelUpdateTaskOnce) or, at minimum, throttle the loop by
adding rate-limiting identical to the background task (e.g. sleep
time.Sleep(common.RequestInterval) between channel checks) and/or limit
concurrency (worker pool with bounded goroutines) to ensure the handler returns
quickly and upstream API rate limits are respected. Ensure any enqueuing uses
the same settings.UpstreamModelUpdate… flags and that errors/failures are
tracked as before so behavior remains consistent with
checkAndPersistChannelUpstreamModelUpdates.
| for { | ||
| var channels []*model.Channel | ||
| err := model.DB. | ||
| Select("id", "name", "type", "key", "status", "base_url", "models", "settings", "setting", "other", "group", "priority", "weight", "tag", "channel_info", "header_override"). |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Find commonGroupCol and commonKeyCol definitions in model/main.go
echo "=== Looking for commonGroupCol/commonKeyCol definitions ==="
rg -n "commonGroupCol|commonKeyCol" model/main.go -B2 -A2
# Find existing Select calls using these variables (reference usages)
echo -e "\n=== Existing usages of commonGroupCol/commonKeyCol in Select calls ==="
rg -n "commonGroupCol|commonKeyCol" --type=go -B1 -A1
# Check the specific file and line mentioned
echo -e "\n=== Checking controller/channel_upstream_update.go around line 517 ==="
if [ -f "controller/channel_upstream_update.go" ]; then
wc -l controller/channel_upstream_update.go
sed -n '510,525p' controller/channel_upstream_update.go
fi
# Check for Select calls with bare "key" or "group" strings
echo -e "\n=== Select calls with bare \"key\" strings ==="
rg -n 'Select\(' controller/channel_upstream_update.go -A1 | head -20
echo -e "\n=== Check if model_mapping is referenced elsewhere ==="
rg -n "model_mapping" --type=go | head -10Repository: QuantumNous/new-api
Length of output: 8036
Replace bare SQL reserved keywords "key" and "group" with their quoted equivalents from model/main.go
GORM v2 passes raw Select() strings directly to SQL without auto-quoting, so bare reserved keywords will fail on MySQL. Use commonGroupCol and commonKeyCol variables instead, which are conditionally initialized with proper quoting (backticks for MySQL/SQLite, double quotes for PostgreSQL).
Also add the missing model_mapping column to the selection.
🐛 Proposed fix
err := model.DB.
- Select("id", "name", "type", "key", "status", "base_url", "models", "settings", "setting", "other", "group", "priority", "weight", "tag", "channel_info", "header_override").
+ Select("id", "name", "type", commonKeyCol, "status", "base_url", "models", "model_mapping", "settings", "setting", "other", commonGroupCol, "priority", "weight", "tag", "channel_info", "header_override").
Where("status = ?", common.ChannelStatusEnabled).🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@controller/channel_upstream_update.go` at line 517, The Select(...) call in
channel_upstream_update.go uses raw reserved SQL identifiers "key" and "group"
which will break on MySQL because GORM v2 doesn't auto-quote; replace those
literals with the already-defined quoted variables commonKeyCol and
commonGroupCol (from model/main.go) in the Select list, and also add the missing
model_mapping column to the selection so the query returns that field; update
the Select invocation that currently lists "id", "name", "type", "key",
"status", "base_url", "models", "settings", "setting", "other", "group",
"priority", "weight", "tag", "channel_info", "header_override" to use
commonKeyCol and commonGroupCol and include model_mapping.
# Conflicts: # dto/channel_settings.go # main.go # web/src/components/table/channels/modals/EditChannelModal.jsx
* main: feat: auto fetch upstream models (QuantumNous#2979) feat: add AionUI to chat settings and built-in templates Revert "fix: aws text content blocks must be non-empty" Revert "Fix/aws non empty text" fix: tool responses Return error when model price/ratio unset Merge pull request QuantumNous#3066 from seefs001/fix/aws-header-override fix: handle rate limits and improve error response parsing in video task updates fix: default empty input_json_delta arguments to {} for tool call parsing fix: preserve tool_use on malformed tool arguments to keep tool_result pairing valid fix: aws text content blocks must be non-empty feat: add cc-switch integration and modal for token management fix: preserve explicit zero values in native relay requests fix: enhance migrateTokenModelLimitsToText function to return errors and improve migration checks fix: migrate model_limits column from varchar(1024) to text for existing tables fix: change token model_limits column from varchar(1024) to text # Conflicts: # web/src/i18n/locales/en.json # web/src/i18n/locales/fr.json # web/src/i18n/locales/ja.json # web/src/i18n/locales/ru.json # web/src/i18n/locales/vi.json
* feat: add upstream model update detection with scheduled sync and manual apply flows * feat: support upstream model removal sync and selectable deletes in update modal * feat: add detect-only upstream updates and show compact +/- model badges * feat: improve upstream model update UX * feat: improve upstream model update UX * fix: respect model_mapping in upstream update detection * feat: improve upstream update modal to prevent missed add/remove actions * feat: add admin upstream model update notifications with digest and truncation * fix: avoid repeated partial-submit confirmation in upstream update modal * feat: improve ui/ux * feat: suppress upstream update alerts for unchanged channel-count within 24h * fix: submit upstream update choices even when no models are selected * feat: improve upstream model update flow and split frontend updater * fix merge conflict
* feat: add upstream model update detection with scheduled sync and manual apply flows * feat: support upstream model removal sync and selectable deletes in update modal * feat: add detect-only upstream updates and show compact +/- model badges * feat: improve upstream model update UX * feat: improve upstream model update UX * fix: respect model_mapping in upstream update detection * feat: improve upstream update modal to prevent missed add/remove actions * feat: add admin upstream model update notifications with digest and truncation * fix: avoid repeated partial-submit confirmation in upstream update modal * feat: improve ui/ux * feat: suppress upstream update alerts for unchanged channel-count within 24h * fix: submit upstream update choices even when no models are selected * feat: improve upstream model update flow and split frontend updater * fix merge conflict
* feat: add upstream model update detection with scheduled sync and manual apply flows * feat: support upstream model removal sync and selectable deletes in update modal * feat: add detect-only upstream updates and show compact +/- model badges * feat: improve upstream model update UX * feat: improve upstream model update UX * fix: respect model_mapping in upstream update detection * feat: improve upstream update modal to prevent missed add/remove actions * feat: add admin upstream model update notifications with digest and truncation * fix: avoid repeated partial-submit confirmation in upstream update modal * feat: improve ui/ux * feat: suppress upstream update alerts for unchanged channel-count within 24h * fix: submit upstream update choices even when no models are selected * feat: improve upstream model update flow and split frontend updater * fix merge conflict
Summary by CodeRabbit
New Features
Settings