Skip to content

Commit

Permalink
🐛 fix: fix api key in api key form (#5498)
Browse files Browse the repository at this point in the history
* fix api key

* fix client fetch
  • Loading branch information
arvinxx authored Jan 17, 2025
1 parent 261e14a commit b4a160b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import { useTranslation } from 'react-i18next';

import { useProviderName } from '@/hooks/useProviderName';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';
import { useUserStore } from '@/store/user';
import { keyVaultsConfigSelectors } from '@/store/user/selectors';
import { GlobalLLMProviderKey } from '@/types/user/settings';

import { FormAction } from '../style';
import { useApiKey } from './useApiKey';

interface ProviderApiKeyFormProps {
apiKeyPlaceholder?: string;
Expand All @@ -25,11 +24,7 @@ const ProviderApiKeyForm = memo<ProviderApiKeyFormProps>(
const { t: errorT } = useTranslation('error');
const [showProxy, setShow] = useState(false);

const [apiKey, proxyUrl, setConfig] = useUserStore((s) => [
keyVaultsConfigSelectors.getVaultByProvider(provider)(s)?.apiKey,
keyVaultsConfigSelectors.getVaultByProvider(provider)(s)?.baseURL,
s.updateKeyVaultConfig,
]);
const { apiKey, baseURL, setConfig } = useApiKey(provider);
const { showOpenAIProxyUrl } = useServerConfigStore(featureFlagsSelectors);
const providerName = useProviderName(provider);

Expand Down Expand Up @@ -58,7 +53,7 @@ const ProviderApiKeyForm = memo<ProviderApiKeyFormProps>(
}}
placeholder={'https://api.openai.com/v1'}
type={'block'}
value={proxyUrl}
value={baseURL}
/>
) : (
<Button
Expand Down
34 changes: 34 additions & 0 deletions src/features/Conversation/Error/APIKeyForm/useApiKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import isEqual from 'fast-deep-equal';

import { isDeprecatedEdition } from '@/const/version';
import { aiProviderSelectors, useAiInfraStore } from '@/store/aiInfra';
import { useUserStore } from '@/store/user';
import { keyVaultsConfigSelectors } from '@/store/user/selectors';

export const useApiKey = (provider: string) => {
const [apiKey, baseURL, setConfig] = useUserStore((s) => [
keyVaultsConfigSelectors.getVaultByProvider(provider as any)(s)?.apiKey,
keyVaultsConfigSelectors.getVaultByProvider(provider as any)(s)?.baseURL,
s.updateKeyVaultConfig,
]);

const updateAiProviderConfig = useAiInfraStore((s) => s.updateAiProviderConfig);
const data = useAiInfraStore(aiProviderSelectors.providerConfigById(provider), isEqual);

// TODO: remove this in V2
if (isDeprecatedEdition) return { apiKey, baseURL, setConfig };
//

return {
apiKey: data?.keyVaults.apiKey,
baseURL: data?.keyVaults?.baseURL,
setConfig: async (id: string, params: Record<string, string>) => {
await updateAiProviderConfig(id, {
keyVaults: {
...data?.keyVaults,
...params,
},
});
},
};
};
28 changes: 14 additions & 14 deletions src/store/aiInfra/slices/aiProvider/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ const isActiveProviderApiKeyNotEmpty = (s: AIProviderStoreState) => {
return !!vault?.apiKey || !!vault?.accessKeyId || !!vault?.secretAccessKey;
};

const providerConfigById =
(id: string) =>
(s: AIProviderStoreState): AiProviderRuntimeConfig | undefined => {
if (!id) return undefined;

return s.aiProviderRuntimeConfig?.[id];
};

const isProviderConfigUpdating = (id: string) => (s: AIProviderStoreState) =>
s.aiProviderConfigUpdatingIds.includes(id);

/**
* @description The conditions to enable client fetch
* 1. If no baseUrl and apikey input, force on Server.
Expand All @@ -46,7 +57,7 @@ const isActiveProviderApiKeyNotEmpty = (s: AIProviderStoreState) => {
*/
const isProviderFetchOnClient =
(provider: GlobalLLMProviderKey | string) => (s: AIProviderStoreState) => {
const config = activeProviderConfig(s);
const config = providerConfigById(provider)(s);

// If the provider already disable broswer request in model config, force on Server.
if (isProviderDisableBroswerRequest(provider)) return false;
Expand All @@ -56,8 +67,8 @@ const isProviderFetchOnClient =
return config?.fetchOnClient;

// 1. If no baseUrl and apikey input, force on Server.
const isProviderEndpointNotEmpty = isActiveProviderEndpointNotEmpty(s);
const isProviderApiKeyNotEmpty = isActiveProviderApiKeyNotEmpty(s);
const isProviderEndpointNotEmpty = !!config?.keyVaults.baseURL;
const isProviderApiKeyNotEmpty = !!config?.keyVaults.apiKey;
if (!isProviderEndpointNotEmpty && !isProviderApiKeyNotEmpty) return false;

// 2. If only contains baseUrl, force on Client
Expand All @@ -76,17 +87,6 @@ const providerKeyVaults = (provider: string | undefined) => (s: AIProviderStoreS
return s.aiProviderRuntimeConfig?.[provider]?.keyVaults;
};

const providerConfigById =
(id: string) =>
(s: AIProviderStoreState): AiProviderRuntimeConfig | undefined => {
if (!id) return undefined;

return s.aiProviderRuntimeConfig?.[id];
};

const isProviderConfigUpdating = (id: string) => (s: AIProviderStoreState) =>
s.aiProviderConfigUpdatingIds.includes(id);

export const aiProviderSelectors = {
activeProviderConfig,
disabledAiProviderList,
Expand Down
2 changes: 1 addition & 1 deletion src/types/aiProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export interface EnabledAiModel {

export interface AiProviderRuntimeConfig {
fetchOnClient?: boolean;
keyVaults: Record<string, object>;
keyVaults: Record<string, string>;
settings: AiProviderSettings;
}

Expand Down

0 comments on commit b4a160b

Please sign in to comment.