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 api key input issue #6112

Merged
merged 2 commits into from
Feb 13, 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
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,3 @@ const Page = async (props: PagePropsWithId) => {
};

export default Page;

export const dynamic = 'auto';
11 changes: 11 additions & 0 deletions src/features/Conversation/Error/APIKeyForm/LoadingContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createContext } from 'react';

interface LoadingContextValue {
loading: boolean;
setLoading: (loading: boolean) => void;
}

export const LoadingContext = createContext<LoadingContextValue>({
loading: false,
setLoading: () => {},
});
25 changes: 14 additions & 11 deletions src/features/Conversation/Error/APIKeyForm/ProviderApiKeyForm.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Icon } from '@lobehub/ui';
import { Button, Input } from 'antd';
import { Network } from 'lucide-react';
import { ReactNode, memo, useState } from 'react';
import { Button } from 'antd';
import { Loader2Icon, Network } from 'lucide-react';
import { ReactNode, memo, useContext, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { FormInput, FormPassword } from '@/components/FormInput';
import { LoadingContext } from '@/features/Conversation/Error/APIKeyForm/LoadingContext';
import { useProviderName } from '@/hooks/useProviderName';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';
import { GlobalLLMProviderKey } from '@/types/user/settings';
Expand All @@ -27,32 +29,33 @@ const ProviderApiKeyForm = memo<ProviderApiKeyFormProps>(
const { apiKey, baseURL, setConfig } = useApiKey(provider);
const { showOpenAIProxyUrl } = useServerConfigStore(featureFlagsSelectors);
const providerName = useProviderName(provider);
const { loading } = useContext(LoadingContext);

return (
<FormAction
avatar={avatar}
description={t(`unlock.apiKey.description`, { name: providerName, ns: 'error' })}
title={t(`unlock.apiKey.title`, { name: providerName, ns: 'error' })}
>
<Input.Password
<FormPassword
autoComplete={'new-password'}
onChange={(e) => {
setConfig(provider, { apiKey: e.target.value });
onChange={(value) => {
setConfig(provider, { apiKey: value });
}}
placeholder={apiKeyPlaceholder || 'sk-***********************'}
type={'block'}
suffix={<div>{loading && <Icon icon={Loader2Icon} spin />}</div>}
value={apiKey}
/>

{showEndpoint &&
showOpenAIProxyUrl &&
(showProxy ? (
<Input
onChange={(e) => {
setConfig(provider, { baseURL: e.target.value });
<FormInput
onChange={(value) => {
setConfig(provider, { baseURL: value });
}}
placeholder={'https://api.openai.com/v1'}
type={'block'}
suffix={<div>{loading && <Icon icon={Loader2Icon} spin />}</div>}
value={baseURL}
/>
) : (
Expand Down
71 changes: 38 additions & 33 deletions src/features/Conversation/Error/APIKeyForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ProviderIcon } from '@lobehub/icons';
import { Button } from 'antd';
import { memo, useMemo } from 'react';
import { memo, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Center, Flexbox } from 'react-layout-kit';

Expand All @@ -9,6 +9,7 @@ import { useChatStore } from '@/store/chat';
import { GlobalLLMProviderKey } from '@/types/user/settings';

import BedrockForm from './Bedrock';
import { LoadingContext } from './LoadingContext';
import ProviderApiKeyForm from './ProviderApiKeyForm';

interface APIKeyFormProps {
Expand All @@ -18,6 +19,7 @@ interface APIKeyFormProps {

const APIKeyForm = memo<APIKeyFormProps>(({ id, provider }) => {
const { t } = useTranslation('error');
const [loading, setLoading] = useState(false);

const [resend, deleteMessage] = useChatStore((s) => [s.regenerateMessage, s.deleteMessage]);

Expand Down Expand Up @@ -62,38 +64,41 @@ const APIKeyForm = memo<APIKeyFormProps>(({ id, provider }) => {
}, [provider]);

return (
<Center gap={16} style={{ maxWidth: 300 }}>
{provider === ModelProvider.Bedrock ? (
<BedrockForm />
) : (
<ProviderApiKeyForm
apiKeyPlaceholder={apiKeyPlaceholder}
avatar={<ProviderIcon provider={provider} size={80} type={'avatar'} />}
provider={provider as GlobalLLMProviderKey}
showEndpoint={provider === ModelProvider.OpenAI}
/>
)}
<Flexbox gap={12} width={'100%'}>
<Button
block
onClick={() => {
resend(id);
deleteMessage(id);
}}
style={{ marginTop: 8 }}
type={'primary'}
>
{t('unlock.confirm')}
</Button>
<Button
onClick={() => {
deleteMessage(id);
}}
>
{t('unlock.closeMessage')}
</Button>
</Flexbox>
</Center>
<LoadingContext value={{ loading, setLoading }}>
<Center gap={16} style={{ maxWidth: 300 }}>
{provider === ModelProvider.Bedrock ? (
<BedrockForm />
) : (
<ProviderApiKeyForm
apiKeyPlaceholder={apiKeyPlaceholder}
avatar={<ProviderIcon provider={provider} size={80} type={'avatar'} />}
provider={provider as GlobalLLMProviderKey}
showEndpoint={provider === ModelProvider.OpenAI}
/>
)}
<Flexbox gap={12} width={'100%'}>
<Button
block
disabled={loading}
onClick={() => {
resend(id);
deleteMessage(id);
}}
style={{ marginTop: 8 }}
type={'primary'}
>
{t('unlock.confirm')}
</Button>
<Button
onClick={() => {
deleteMessage(id);
}}
>
{t('unlock.closeMessage')}
</Button>
</Flexbox>
</Center>
</LoadingContext>
);
});

Expand Down
14 changes: 9 additions & 5 deletions src/features/Conversation/Error/APIKeyForm/useApiKey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import isEqual from 'fast-deep-equal';
import { useContext } from 'react';

import { isDeprecatedEdition } from '@/const/version';
import { LoadingContext } from '@/features/Conversation/Error/APIKeyForm/LoadingContext';
import { aiProviderSelectors, useAiInfraStore } from '@/store/aiInfra';
import { useUserStore } from '@/store/user';
import { keyVaultsConfigSelectors } from '@/store/user/selectors';
Expand All @@ -11,7 +13,7 @@ export const useApiKey = (provider: string) => {
keyVaultsConfigSelectors.getVaultByProvider(provider as any)(s)?.baseURL,
s.updateKeyVaultConfig,
]);

const { setLoading } = useContext(LoadingContext);
const updateAiProviderConfig = useAiInfraStore((s) => s.updateAiProviderConfig);
const data = useAiInfraStore(aiProviderSelectors.providerConfigById(provider), isEqual);

Expand All @@ -23,12 +25,14 @@ export const useApiKey = (provider: string) => {
apiKey: data?.keyVaults.apiKey,
baseURL: data?.keyVaults?.baseURL,
setConfig: async (id: string, params: Record<string, string>) => {
const next = { ...data?.keyVaults, ...params };
if (isEqual(data?.keyVaults, next)) return;

setLoading(true);
await updateAiProviderConfig(id, {
keyVaults: {
...data?.keyVaults,
...params,
},
keyVaults: { ...data?.keyVaults, ...params },
});
setLoading(false);
},
};
};