mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-06-26 18:26:38 +00:00
fix: refresh model list after api key changes (#944)
Some checks are pending
Update Stable Branch / prepare-release (push) Waiting to run
Some checks are pending
Update Stable Branch / prepare-release (push) Waiting to run
This commit is contained in:
parent
e00264236e
commit
55cfd5d4ee
@ -1,6 +1,7 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { IconButton } from '~/components/ui/IconButton';
|
import { IconButton } from '~/components/ui/IconButton';
|
||||||
import type { ProviderInfo } from '~/types/model';
|
import type { ProviderInfo } from '~/types/model';
|
||||||
|
import Cookies from 'js-cookie';
|
||||||
|
|
||||||
interface APIKeyManagerProps {
|
interface APIKeyManagerProps {
|
||||||
provider: ProviderInfo;
|
provider: ProviderInfo;
|
||||||
@ -10,6 +11,23 @@ interface APIKeyManagerProps {
|
|||||||
labelForGetApiKey?: string;
|
labelForGetApiKey?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const apiKeyMemoizeCache: { [k: string]: Record<string, string> } = {};
|
||||||
|
|
||||||
|
export function getApiKeysFromCookies() {
|
||||||
|
const storedApiKeys = Cookies.get('apiKeys');
|
||||||
|
let parsedKeys = {};
|
||||||
|
|
||||||
|
if (storedApiKeys) {
|
||||||
|
parsedKeys = apiKeyMemoizeCache[storedApiKeys];
|
||||||
|
|
||||||
|
if (!parsedKeys) {
|
||||||
|
parsedKeys = apiKeyMemoizeCache[storedApiKeys] = JSON.parse(storedApiKeys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsedKeys;
|
||||||
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey, setApiKey }) => {
|
export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey, setApiKey }) => {
|
||||||
const [isEditing, setIsEditing] = useState(false);
|
const [isEditing, setIsEditing] = useState(false);
|
||||||
|
|||||||
@ -12,7 +12,7 @@ import { classNames } from '~/utils/classNames';
|
|||||||
import { MODEL_LIST, PROVIDER_LIST, initializeModelList } from '~/utils/constants';
|
import { MODEL_LIST, PROVIDER_LIST, initializeModelList } from '~/utils/constants';
|
||||||
import { Messages } from './Messages.client';
|
import { Messages } from './Messages.client';
|
||||||
import { SendButton } from './SendButton.client';
|
import { SendButton } from './SendButton.client';
|
||||||
import { APIKeyManager } from './APIKeyManager';
|
import { APIKeyManager, getApiKeysFromCookies } from './APIKeyManager';
|
||||||
import Cookies from 'js-cookie';
|
import Cookies from 'js-cookie';
|
||||||
import * as Tooltip from '@radix-ui/react-tooltip';
|
import * as Tooltip from '@radix-ui/react-tooltip';
|
||||||
|
|
||||||
@ -125,52 +125,6 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
|
|||||||
}, [transcript]);
|
}, [transcript]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Load API keys from cookies on component mount
|
|
||||||
|
|
||||||
let parsedApiKeys: Record<string, string> | undefined = {};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const storedApiKeys = Cookies.get('apiKeys');
|
|
||||||
|
|
||||||
if (storedApiKeys) {
|
|
||||||
const parsedKeys = JSON.parse(storedApiKeys);
|
|
||||||
|
|
||||||
if (typeof parsedKeys === 'object' && parsedKeys !== null) {
|
|
||||||
setApiKeys(parsedKeys);
|
|
||||||
parsedApiKeys = parsedKeys;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error loading API keys from cookies:', error);
|
|
||||||
|
|
||||||
// Clear invalid cookie data
|
|
||||||
Cookies.remove('apiKeys');
|
|
||||||
}
|
|
||||||
|
|
||||||
let providerSettings: Record<string, IProviderSetting> | undefined = undefined;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const savedProviderSettings = Cookies.get('providers');
|
|
||||||
|
|
||||||
if (savedProviderSettings) {
|
|
||||||
const parsedProviderSettings = JSON.parse(savedProviderSettings);
|
|
||||||
|
|
||||||
if (typeof parsedProviderSettings === 'object' && parsedProviderSettings !== null) {
|
|
||||||
providerSettings = parsedProviderSettings;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error loading Provider Settings from cookies:', error);
|
|
||||||
|
|
||||||
// Clear invalid cookie data
|
|
||||||
Cookies.remove('providers');
|
|
||||||
}
|
|
||||||
|
|
||||||
initializeModelList({ apiKeys: parsedApiKeys, providerSettings }).then((modelList) => {
|
|
||||||
console.log('Model List: ', modelList);
|
|
||||||
setModelList(modelList);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (typeof window !== 'undefined' && ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window)) {
|
if (typeof window !== 'undefined' && ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window)) {
|
||||||
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
||||||
const recognition = new SpeechRecognition();
|
const recognition = new SpeechRecognition();
|
||||||
@ -202,6 +156,44 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
|
|||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let providerSettings: Record<string, IProviderSetting> | undefined = undefined;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const savedProviderSettings = Cookies.get('providers');
|
||||||
|
|
||||||
|
if (savedProviderSettings) {
|
||||||
|
const parsedProviderSettings = JSON.parse(savedProviderSettings);
|
||||||
|
|
||||||
|
if (typeof parsedProviderSettings === 'object' && parsedProviderSettings !== null) {
|
||||||
|
providerSettings = parsedProviderSettings;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error loading Provider Settings from cookies:', error);
|
||||||
|
|
||||||
|
// Clear invalid cookie data
|
||||||
|
Cookies.remove('providers');
|
||||||
|
}
|
||||||
|
|
||||||
|
let parsedApiKeys: Record<string, string> | undefined = {};
|
||||||
|
|
||||||
|
try {
|
||||||
|
parsedApiKeys = getApiKeysFromCookies();
|
||||||
|
setApiKeys(parsedApiKeys);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error loading API keys from cookies:', error);
|
||||||
|
|
||||||
|
// Clear invalid cookie data
|
||||||
|
Cookies.remove('apiKeys');
|
||||||
|
}
|
||||||
|
|
||||||
|
initializeModelList({ apiKeys: parsedApiKeys, providerSettings }).then((modelList) => {
|
||||||
|
console.log('Model List: ', modelList);
|
||||||
|
setModelList(modelList);
|
||||||
|
});
|
||||||
|
}, [apiKeys]);
|
||||||
|
|
||||||
const startListening = () => {
|
const startListening = () => {
|
||||||
if (recognition) {
|
if (recognition) {
|
||||||
recognition.start();
|
recognition.start();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user