Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix: fix model select not auto update and sort issue #5330

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const nextConfig: NextConfig = {
source: '/welcome',
},
],
serverExternalPackages: ['@electric-sql/pglite', 'shiki/wasm', 'sharp'],
serverExternalPackages: ['@electric-sql/pglite', 'sharp'],

transpilePackages: ['pdfjs-dist', 'mermaid'],

Expand Down
25 changes: 18 additions & 7 deletions src/components/ModelSelect/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IconAvatarProps, ModelIcon, ProviderIcon } from '@lobehub/icons';
import { Icon, Tooltip } from '@lobehub/ui';
import { Avatar, Icon, Tooltip } from '@lobehub/ui';
import { Typography } from 'antd';
import { createStyles } from 'antd-style';
import { Infinity, LucideEye, LucidePaperclip, ToyBrick } from 'lucide-react';
Expand All @@ -10,6 +10,7 @@ import { useTranslation } from 'react-i18next';
import { Center, Flexbox } from 'react-layout-kit';

import { ModelAbilities } from '@/types/aiModel';
import { AiProviderSourceType } from '@/types/aiProvider';
import { ChatModelCard } from '@/types/llm';
import { formatTokenNumber } from '@/utils/format';

Expand Down Expand Up @@ -153,16 +154,26 @@ export const ModelItemRender = memo<ModelItemRenderProps>(({ showInfoTag = true,
});

interface ProviderItemRenderProps {
logo?: string;
name: string;
provider: string;
source?: AiProviderSourceType;
}

export const ProviderItemRender = memo<ProviderItemRenderProps>(({ provider, name }) => (
<Flexbox align={'center'} gap={4} horizontal>
<ProviderIcon provider={provider} size={20} type={'mono'} />
{name}
</Flexbox>
));
export const ProviderItemRender = memo<ProviderItemRenderProps>(
({ provider, name, source, logo }) => {
return (
<Flexbox align={'center'} gap={4} horizontal>
{source === 'custom' && !!logo ? (
<Avatar avatar={logo} size={20} style={{ filter: 'grayscale(1)' }} title={name} />
) : (
<ProviderIcon provider={provider} size={20} type={'mono'} />
)}
{name}
</Flexbox>
);
},
);

interface LabelRendererProps {
Icon: FC<IconAvatarProps>;
Expand Down
6 changes: 4 additions & 2 deletions src/database/repositories/aiInfra/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class AiInfraRepos {
return list
.filter((item) => item.enabled)
.sort((a, b) => a.sort! - b.sort!)
.map((item) => ({ id: item.id, name: item.name, source: item.source }));
.map((item) => ({ id: item.id, logo: item.logo, name: item.name, source: item.source }));
};

getEnabledModels = async () => {
Expand All @@ -86,7 +86,9 @@ export class AiInfraRepos {
{ concurrency: 10 },
);

return [...modelList.flat(), ...userEnabledModels] as EnabledAiModel[];
return [...modelList.flat(), ...userEnabledModels].sort(
(a, b) => (a?.sort || -1) - (b?.sort || -1),
) as EnabledAiModel[];
};

getAiProviderModelList = async (providerId: string) => {
Expand Down
1 change: 1 addition & 0 deletions src/database/server/models/aiModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class AiModelModel {
enabled: aiModels.enabled,
id: aiModels.id,
providerId: aiModels.providerId,
sort: aiModels.sort,
source: aiModels.source,
type: aiModels.type,
})
Expand Down
9 changes: 8 additions & 1 deletion src/features/ModelSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ const ModelSelect = memo<ModelSelectProps>(({ value, onChange, showAbility = tru
}

return enabledList.map((provider) => ({
label: <ProviderItemRender name={provider.name} provider={provider.id} />,
label: (
<ProviderItemRender
logo={provider.logo}
name={provider.name}
provider={provider.id}
source={provider.source}
/>
),
options: getChatModels(provider),
}));
}, [enabledList]);
Expand Down
9 changes: 8 additions & 1 deletion src/features/ModelSwitchPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ const ModelSwitchPanel = memo<PropsWithChildren>(({ children }) => {
return enabledList.map((provider) => ({
children: getModelItems(provider),
key: provider.id,
label: <ProviderItemRender name={provider.name} provider={provider.id} />,
label: (
<ProviderItemRender
logo={provider.logo}
name={provider.name}
provider={provider.id}
source={provider.source}
/>
),
type: 'group',
}));
}, [enabledList]);
Expand Down
4 changes: 2 additions & 2 deletions src/store/aiInfra/slices/aiProvider/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const createAiProviderSlice: StateCreator<
await get().refreshAiProviderRuntimeState();
},
refreshAiProviderRuntimeState: async () => {
await mutate(AiProviderSwrKey.fetchAiProviderRuntimeState);
await mutate([AiProviderSwrKey.fetchAiProviderRuntimeState, true]);
},
removeAiProvider: async (id) => {
await aiProviderService.deleteAiProvider(id);
Expand Down Expand Up @@ -187,8 +187,8 @@ export const createAiProviderSlice: StateCreator<

// 3. 组装最终数据结构
const enabledChatModelList = data.enabledAiProviders.map((provider) => ({
...provider,
children: getModelListByType(provider.id, 'chat'),
id: provider.id,
name: provider.name || provider.id,
}));

Expand Down
1 change: 1 addition & 0 deletions src/store/user/slices/modelList/selectors/modelProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const modelProviderListForModelSelect = (s: UserStore): EnabledProviderWithModel
displayName: m.displayName,
id: m.id,
})),
source: 'builtin',
}));

const getModelCardById = (id: string) => (s: UserStore) => {
Expand Down
4 changes: 4 additions & 0 deletions src/types/aiModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { z } from 'zod';

import { AiProviderSourceType } from '@/types/aiProvider';

export type ModelPriceCurrency = 'CNY' | 'USD';

export const AiModelSourceEnum = {
Expand Down Expand Up @@ -312,5 +314,7 @@ interface AiModelForSelect {
export interface EnabledProviderWithModels {
children: AiModelForSelect[];
id: string;
logo?: string;
name: string;
source: AiProviderSourceType;
}
6 changes: 4 additions & 2 deletions src/types/aiProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod';

import { AiModelConfig, AiModelSourceType, AiModelType, ModelAbilities } from '@/types/aiModel';
import { AiModelConfig, AiModelType, ModelAbilities } from '@/types/aiModel';
import { SmoothingParams } from '@/types/llm';

export const AiProviderSourceEnum = {
Expand Down Expand Up @@ -182,8 +182,9 @@ export interface AiProviderSortMap {

export interface EnabledProvider {
id: string;
logo?: string;
name?: string;
source: AiModelSourceType;
source: AiProviderSourceType;
}

export interface EnabledAiModel {
Expand All @@ -193,6 +194,7 @@ export interface EnabledAiModel {
displayName?: string;
id: string;
providerId: string;
sort?: number;
type: AiModelType;
}

Expand Down
Loading