🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(model-switch-panel): cap pinned new models, unify zh-CN token (#20601)
YuTengjingefad80c0bed1
diff --git a/locales/zh-CN/components.json b/locales/zh-CN/components.jsonindex 01aefaaee88..8ae06e2a065 100644--- a/locales/zh-CN/components.json+++ b/locales/zh-CN/components.json@@ -171,13 +171,13 @@"ModelSwitchPanel.detail.context": "上下文长度","ModelSwitchPanel.detail.pricing": "价格","ModelSwitchPanel.detail.pricing.cachedInput": "缓存输入 ${{amount}}/百万",- "ModelSwitchPanel.detail.pricing.credits.cachedInput": "缓存输入 {{amount}} 积分/M 令牌",+ "ModelSwitchPanel.detail.pricing.credits.cachedInput": "缓存输入 {{amount}} 积分/M token","ModelSwitchPanel.detail.pricing.credits.image": "积分/图片",- "ModelSwitchPanel.detail.pricing.credits.input": "输入 {{amount}} 积分/M 令牌",+ "ModelSwitchPanel.detail.pricing.credits.input": "输入 {{amount}} 积分/M token","ModelSwitchPanel.detail.pricing.credits.megapixel": "积分/百万像素","ModelSwitchPanel.detail.pricing.credits.millionCharacters": "积分/百万字符",- "ModelSwitchPanel.detail.pricing.credits.millionTokens": "积分/百万令牌",- "ModelSwitchPanel.detail.pricing.credits.output": "输出 {{amount}} 积分/M 令牌",+ "ModelSwitchPanel.detail.pricing.credits.millionTokens": "积分/百万 token",+ "ModelSwitchPanel.detail.pricing.credits.output": "输出 {{amount}} 积分/M token","ModelSwitchPanel.detail.pricing.credits.perImage": "约 {{amount}} 积分 / 图片","ModelSwitchPanel.detail.pricing.credits.perVideo": "约 {{amount}} 积分 / 视频","ModelSwitchPanel.detail.pricing.credits.second": "积分/秒",diff --git a/src/features/ModelSwitchPanel/hooks/useBuildListItems.test.ts b/src/features/ModelSwitchPanel/hooks/useBuildListItems.test.tsindex e9d7bd7ffa5..a588dff5dc9 100644--- a/src/features/ModelSwitchPanel/hooks/useBuildListItems.test.ts+++ b/src/features/ModelSwitchPanel/hooks/useBuildListItems.test.ts@@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest';import type { EnabledProviderWithModels } from '@/types/aiProvider';-import { buildListItems } from './useBuildListItems';+import { buildListItems, MAX_PINNED_NEW_MODELS } from './useBuildListItems';const model = (id: string, displayName = id, releasedAt?: string) =>({ abilities: {}, displayName, id, releasedAt }) satisfies AiModelForSelect;@@ -110,4 +110,89 @@ describe('buildListItems', () => {),).toEqual(['gpt-6-astra', 'fable-5.1']);});+ it('should pin only the newest new models and keep the rest in catalog order', () => {+ expect(MAX_PINNED_NEW_MODELS).toBe(4);++ const items = buildListItems(+ [+ provider('lobehub', [+ model('deepseek-v4.1-flash', 'DeepSeek V4.1 Flash', daysAgo(200)),+ model('claude-sonnet-5.5', 'Claude Sonnet 5.5', daysAgo(6)),+ model('claude-sonnet-5', 'Claude Sonnet 5', daysAgo(100)),+ model('claude-haiku-5.5', 'Claude Haiku 5.5', daysAgo(2)),+ model('gpt-6.1-sol', 'GPT-6.1 Sol', daysAgo(5)),+ model('gpt-6-sol', 'GPT-6 Sol', daysAgo(10)),+ model('grok-4.7', 'Grok 4.7', daysAgo(1)),+ model('glm-5.3', 'GLM-5.3', daysAgo(3)),+ ]),+ ],+ 'byProvider',+ );++ expect(getProviderModelIds(items)).toEqual([+ 'grok-4.7',+ 'claude-haiku-5.5',+ 'glm-5.3',+ 'gpt-6.1-sol',+ 'deepseek-v4.1-flash',+ 'claude-sonnet-5.5',+ 'claude-sonnet-5',+ 'gpt-6-sol',+ ]);+ });++ it('should not spend pinned slots on models sorted last', () => {+ const items = buildListItems(+ [+ provider('lobehub', [+ model('legacy', 'Legacy', daysAgo(400)),+ model('pro-a', 'Pro A', daysAgo(1)),+ model('pro-b', 'Pro B', daysAgo(1)),+ model('pro-c', 'Pro C', daysAgo(1)),+ model('pro-d', 'Pro D', daysAgo(1)),+ model('new-free', 'New Free', daysAgo(5)),+ ]),+ ],+ 'byProvider',+ '',+ (modelId) => modelId.startsWith('pro-'),+ );++ expect(getProviderModelIds(items)).toEqual([+ 'new-free',+ 'legacy',+ 'pro-a',+ 'pro-b',+ 'pro-c',+ 'pro-d',+ ]);+ });+ it('should group pinned new models by vendor series, keeping catalog order inside a series', () => {+ const items = buildListItems(+ [+ provider('lobehub', [+ model('deepseek-v4.1-flash', 'DeepSeek V4.1 Flash', daysAgo(200)),+ model('claude-sonnet-5.5', 'Claude Sonnet 5.5', daysAgo(4)),+ model('claude-haiku-5.5', 'Claude Haiku 5.5', daysAgo(1)),+ model('gpt-6.1-sol', 'GPT-6.1 Sol', daysAgo(2)),+ model('grok-4.7', 'Grok 4.7', daysAgo(3)),+ ]),+ ],+ 'byModel',+ );++ expect(+ items.flatMap((item) =>+ item.type === 'model-item-single' || item.type === 'model-item-multiple'+ ? [item.data.model.id]+ : [],+ ),+ ).toEqual([+ 'claude-sonnet-5.5',+ 'claude-haiku-5.5',+ 'gpt-6.1-sol',+ 'grok-4.7',+ 'deepseek-v4.1-flash',+ ]);+ });});diff --git a/src/features/ModelSwitchPanel/hooks/useBuildListItems.ts b/src/features/ModelSwitchPanel/hooks/useBuildListItems.tsindex eabf98c236e..09a48d3a39b 100644--- a/src/features/ModelSwitchPanel/hooks/useBuildListItems.ts+++ b/src/features/ModelSwitchPanel/hooks/useBuildListItems.ts@@ -1,3 +1,4 @@+import { modelMappings } from '@lobehub/icons';import dayjs from 'dayjs';import { useMemo } from 'react';@@ -7,26 +8,79 @@ import { isNewReleaseDate } from '@/utils/time';import { type GroupMode, type ListItem, type ModelWithProviders } from '../types';/**- * Shares the exact rule behind `NewModelBadge`, so a model is pinned to the top only while its- * badge is still visible. Every renderer of this list must keep the badge on — otherwise models- * jump ahead with no visible explanation.+ * Shares the exact rule behind `NewModelBadge`. Every renderer of this list must keep the badge+ * on — otherwise pinned models jump ahead with no visible explanation.*/const isNewModel = (releasedAt?: string): boolean => !!releasedAt && isNewReleaseDate(releasedAt);/**- * Pins new models to the top, then orders them newest-first. Ranking the pinned models by- * `displayOrder` instead would surface whichever vendor happens to sit earliest in the catalog,- * so a model released days earlier could outrank today's launch under the same "new" badge.+ * Caps how many new models jump ahead of the catalog order. A busy launch week can badge many+ * models at once; pinning all of them would push every established model out of the first screen.+ * New models beyond the cap keep their badge but stay in their catalog position.+ */+export const MAX_PINNED_NEW_MODELS = 4;++/**+ * Resolves the vendor series a model belongs to with the same keyword table `ModelIcon` uses, so+ * "same series" always means "same row icon" to the user. Models without a matching icon form a+ * series of their own.+ */+const getModelSeries = (modelId: string): unknown => {+ const id = modelId.toLowerCase();+ const mapping = modelMappings.find((item) =>+ item.keywords.some((keyword) => new RegExp(keyword, 'i').test(id)),+ );++ return mapping?.Icon ?? modelId;+};++/**+ * Pins at most {@link MAX_PINNED_NEW_MODELS} new models to the top and keeps the remaining items+ * in catalog order.+ *+ * The pinned models are chosen newest-first, so a busy week keeps today's launch over one from+ * days earlier. They are then grouped by vendor series: series are ordered by their newest model,+ * and models within a series keep catalog order to match the list below. Ordering purely by+ * release date would scatter one vendor's launches across the pinned zone (e.g. Claude Haiku 5.5+ * first and Claude Sonnet 5.5 fourth); ordering purely by `displayOrder` would let whichever+ * vendor sits earliest in the catalog outrank a newer launch.*- * Same-day releases fall through to 0 and keep `displayOrder` via stable sort.+ * Items flagged by `isLast` sink below everything else and are never pinned.*/-const compareNewness = (a?: string, b?: string): number => {- const aNew = isNewModel(a);- const bNew = isNewModel(b);- if (aNew !== bNew) return aNew ? -1 : 1;- if (!aNew) return 0;+const sortWithPinnedNewModels = <T>(+ items: T[],+ getModel: (item: T) => { id: string; releasedAt?: string },+ isLast?: (item: T) => boolean,+): T[] => {+ const releasedAtOf = (item: T) => dayjs(getModel(item).releasedAt).valueOf();++ const pinnedSet = new Set(+ items+ .filter((item) => !isLast?.(item) && isNewModel(getModel(item).releasedAt))+ .toSorted((a, b) => releasedAtOf(b) - releasedAtOf(a))+ .slice(0, MAX_PINNED_NEW_MODELS),+ );- return dayjs(b).valueOf() - dayjs(a).valueOf();+ const seriesNewest = new Map<unknown, number>();+ for (const item of pinnedSet) {+ const series = getModelSeries(getModel(item).id);+ seriesNewest.set(series, Math.max(seriesNewest.get(series) ?? 0, releasedAtOf(item)));+ }+ // Same-newness series fall back to catalog order via stable sort.+ const pinned = items+ .filter((item) => pinnedSet.has(item))+ .toSorted(+ (a, b) =>+ seriesNewest.get(getModelSeries(getModel(b).id))! -+ seriesNewest.get(getModelSeries(getModel(a).id))!,+ );++ const rest = items.filter((item) => !pinnedSet.has(item));++ return [+ ...pinned,+ ...(isLast ? rest.toSorted((a, b) => Number(isLast(a)) - Number(isLast(b))) : rest),+ ];};export const buildListItems = (@@ -95,14 +149,12 @@ export const buildListItems = (});}- const sortedModels = modelArray.toSorted((a, b) => {- if (sortModelLast) {- const aLast = a.providers.every((provider) => sortModelLast(a.model.id, provider.id));- const bLast = b.providers.every((provider) => sortModelLast(b.model.id, provider.id));- if (aLast !== bLast) return Number(aLast) - Number(bLast);- }- return compareNewness(a.model.releasedAt, b.model.releasedAt);- });+ const sortedModels = sortWithPinnedNewModels(+ modelArray,+ (item) => item.model,+ sortModelLast &&+ ((item) => item.providers.every((provider) => sortModelLast(item.model.id, provider.id))),+ );return sortedModels.map((data) => ({data,@@ -119,15 +171,11 @@ export const buildListItems = ((modelItem) =>matchesSearch(modelItem.displayName || modelItem.id) || matchesSearch(providerItem.name),);- const sortedModels = filteredModels.toSorted((a, b) => {- if (sortModelLast) {- const diff =- Number(sortModelLast(a.id, providerItem.id)) -- Number(sortModelLast(b.id, providerItem.id));- if (diff !== 0) return diff;- }- return compareNewness(a.releasedAt, b.releasedAt);- });+ const sortedModels = sortWithPinnedNewModels(+ filteredModels,+ (item) => item,+ sortModelLast && ((item) => sortModelLast(item.id, providerItem.id)),+ );if (sortedModels.length > 0 || !searchKeyword.trim()) {items.push({ provider: providerItem, type: 'group-header' });