| import { useMemo, useEffect } from 'react'; |
| import { MODEL_CONFIG } from '../config/modelConfig'; |
| import { useAccountStore } from '../stores/useAccountStore'; |
| import { Bot } from 'lucide-react'; |
| import { useTranslation } from 'react-i18next'; |
|
|
| export const useProxyModels = () => { |
| const { t } = useTranslation(); |
| const { accounts, fetchAccounts } = useAccountStore(); |
|
|
| |
| useEffect(() => { |
| if (accounts.length === 0) { |
| fetchAccounts(); |
| } |
| }, []); |
|
|
| const models = useMemo(() => { |
| |
| |
| const dynamicMap = new Map<string, { name: string; display_name?: string }>(); |
| for (const account of accounts) { |
| for (const m of account.quota?.models ?? []) { |
| const key = m.name.toLowerCase(); |
| if (!dynamicMap.has(key) || m.display_name) { |
| dynamicMap.set(key, { name: m.name, display_name: m.display_name }); |
| } |
| } |
| } |
|
|
| const result = []; |
| const seenIds = new Set<string>(); |
|
|
| |
| for (const [key, m] of dynamicMap) { |
| if (seenIds.has(key)) continue; |
| seenIds.add(key); |
|
|
| |
| const cfgEntry = Object.entries(MODEL_CONFIG).find( |
| ([cfgId, cfg]) => |
| cfgId.toLowerCase() === key || |
| (cfg.protectedKey && cfg.protectedKey.toLowerCase() === key) |
| ); |
|
|
| const primaryName = m.display_name || m.name; |
| const CfgIcon = cfgEntry?.[1].Icon; |
| const icon = CfgIcon |
| ? <CfgIcon size={16} /> |
| : <Bot size={16} className="text-gray-400 dark:text-gray-500" />; |
| const group = cfgEntry ? (cfgEntry[1].group || 'Other') : 'Dynamic'; |
|
|
| result.push({ |
| id: m.name, |
| name: primaryName, |
| desc: primaryName, |
| group, |
| icon, |
| }); |
| } |
|
|
| |
| const addedLabels = new Set<string>(); |
| for (const [id, config] of Object.entries(MODEL_CONFIG)) { |
| const key = id.toLowerCase(); |
| if (seenIds.has(key)) { |
| addedLabels.add((config.shortLabel || config.label).toLowerCase()); |
| continue; |
| } |
| |
| if (key.includes('-thinking')) continue; |
| |
| const labelKey = (config.shortLabel || config.label).toLowerCase(); |
| if (addedLabels.has(labelKey)) continue; |
| addedLabels.add(labelKey); |
| seenIds.add(key); |
|
|
| const displayName = config.i18nKey ? t(config.i18nKey, config.label) : config.label; |
| result.push({ |
| id, |
| name: displayName, |
| desc: displayName, |
| group: config.group || 'Other', |
| icon: <config.Icon size={16} />, |
| }); |
| } |
|
|
| return result; |
| }, [accounts, t]); |
|
|
| return { models }; |
| }; |
|
|