fix: refresh model list after api key changes (#944)
Browse files
app/components/chat/APIKeyManager.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import React, { useState } from 'react';
|
| 2 |
import { IconButton } from '~/components/ui/IconButton';
|
| 3 |
import type { ProviderInfo } from '~/types/model';
|
|
|
|
| 4 |
|
| 5 |
interface APIKeyManagerProps {
|
| 6 |
provider: ProviderInfo;
|
|
@@ -10,6 +11,23 @@ interface APIKeyManagerProps {
|
|
| 10 |
labelForGetApiKey?: string;
|
| 11 |
}
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
// eslint-disable-next-line @typescript-eslint/naming-convention
|
| 14 |
export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey, setApiKey }) => {
|
| 15 |
const [isEditing, setIsEditing] = useState(false);
|
|
|
|
| 1 |
import React, { useState } from 'react';
|
| 2 |
import { IconButton } from '~/components/ui/IconButton';
|
| 3 |
import type { ProviderInfo } from '~/types/model';
|
| 4 |
+
import Cookies from 'js-cookie';
|
| 5 |
|
| 6 |
interface APIKeyManagerProps {
|
| 7 |
provider: ProviderInfo;
|
|
|
|
| 11 |
labelForGetApiKey?: string;
|
| 12 |
}
|
| 13 |
|
| 14 |
+
const apiKeyMemoizeCache: { [k: string]: Record<string, string> } = {};
|
| 15 |
+
|
| 16 |
+
export function getApiKeysFromCookies() {
|
| 17 |
+
const storedApiKeys = Cookies.get('apiKeys');
|
| 18 |
+
let parsedKeys = {};
|
| 19 |
+
|
| 20 |
+
if (storedApiKeys) {
|
| 21 |
+
parsedKeys = apiKeyMemoizeCache[storedApiKeys];
|
| 22 |
+
|
| 23 |
+
if (!parsedKeys) {
|
| 24 |
+
parsedKeys = apiKeyMemoizeCache[storedApiKeys] = JSON.parse(storedApiKeys);
|
| 25 |
+
}
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
return parsedKeys;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
// eslint-disable-next-line @typescript-eslint/naming-convention
|
| 32 |
export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey, setApiKey }) => {
|
| 33 |
const [isEditing, setIsEditing] = useState(false);
|
app/components/chat/BaseChat.tsx
CHANGED
|
@@ -12,7 +12,7 @@ import { classNames } from '~/utils/classNames';
|
|
| 12 |
import { MODEL_LIST, PROVIDER_LIST, initializeModelList } from '~/utils/constants';
|
| 13 |
import { Messages } from './Messages.client';
|
| 14 |
import { SendButton } from './SendButton.client';
|
| 15 |
-
import { APIKeyManager } from './APIKeyManager';
|
| 16 |
import Cookies from 'js-cookie';
|
| 17 |
import * as Tooltip from '@radix-ui/react-tooltip';
|
| 18 |
|
|
@@ -125,52 +125,6 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
|
|
| 125 |
}, [transcript]);
|
| 126 |
|
| 127 |
useEffect(() => {
|
| 128 |
-
// Load API keys from cookies on component mount
|
| 129 |
-
|
| 130 |
-
let parsedApiKeys: Record<string, string> | undefined = {};
|
| 131 |
-
|
| 132 |
-
try {
|
| 133 |
-
const storedApiKeys = Cookies.get('apiKeys');
|
| 134 |
-
|
| 135 |
-
if (storedApiKeys) {
|
| 136 |
-
const parsedKeys = JSON.parse(storedApiKeys);
|
| 137 |
-
|
| 138 |
-
if (typeof parsedKeys === 'object' && parsedKeys !== null) {
|
| 139 |
-
setApiKeys(parsedKeys);
|
| 140 |
-
parsedApiKeys = parsedKeys;
|
| 141 |
-
}
|
| 142 |
-
}
|
| 143 |
-
} catch (error) {
|
| 144 |
-
console.error('Error loading API keys from cookies:', error);
|
| 145 |
-
|
| 146 |
-
// Clear invalid cookie data
|
| 147 |
-
Cookies.remove('apiKeys');
|
| 148 |
-
}
|
| 149 |
-
|
| 150 |
-
let providerSettings: Record<string, IProviderSetting> | undefined = undefined;
|
| 151 |
-
|
| 152 |
-
try {
|
| 153 |
-
const savedProviderSettings = Cookies.get('providers');
|
| 154 |
-
|
| 155 |
-
if (savedProviderSettings) {
|
| 156 |
-
const parsedProviderSettings = JSON.parse(savedProviderSettings);
|
| 157 |
-
|
| 158 |
-
if (typeof parsedProviderSettings === 'object' && parsedProviderSettings !== null) {
|
| 159 |
-
providerSettings = parsedProviderSettings;
|
| 160 |
-
}
|
| 161 |
-
}
|
| 162 |
-
} catch (error) {
|
| 163 |
-
console.error('Error loading Provider Settings from cookies:', error);
|
| 164 |
-
|
| 165 |
-
// Clear invalid cookie data
|
| 166 |
-
Cookies.remove('providers');
|
| 167 |
-
}
|
| 168 |
-
|
| 169 |
-
initializeModelList({ apiKeys: parsedApiKeys, providerSettings }).then((modelList) => {
|
| 170 |
-
console.log('Model List: ', modelList);
|
| 171 |
-
setModelList(modelList);
|
| 172 |
-
});
|
| 173 |
-
|
| 174 |
if (typeof window !== 'undefined' && ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window)) {
|
| 175 |
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 176 |
const recognition = new SpeechRecognition();
|
|
@@ -202,6 +156,44 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
|
|
| 202 |
}
|
| 203 |
}, []);
|
| 204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
const startListening = () => {
|
| 206 |
if (recognition) {
|
| 207 |
recognition.start();
|
|
|
|
| 12 |
import { MODEL_LIST, PROVIDER_LIST, initializeModelList } from '~/utils/constants';
|
| 13 |
import { Messages } from './Messages.client';
|
| 14 |
import { SendButton } from './SendButton.client';
|
| 15 |
+
import { APIKeyManager, getApiKeysFromCookies } from './APIKeyManager';
|
| 16 |
import Cookies from 'js-cookie';
|
| 17 |
import * as Tooltip from '@radix-ui/react-tooltip';
|
| 18 |
|
|
|
|
| 125 |
}, [transcript]);
|
| 126 |
|
| 127 |
useEffect(() => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
if (typeof window !== 'undefined' && ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window)) {
|
| 129 |
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 130 |
const recognition = new SpeechRecognition();
|
|
|
|
| 156 |
}
|
| 157 |
}, []);
|
| 158 |
|
| 159 |
+
useEffect(() => {
|
| 160 |
+
let providerSettings: Record<string, IProviderSetting> | undefined = undefined;
|
| 161 |
+
|
| 162 |
+
try {
|
| 163 |
+
const savedProviderSettings = Cookies.get('providers');
|
| 164 |
+
|
| 165 |
+
if (savedProviderSettings) {
|
| 166 |
+
const parsedProviderSettings = JSON.parse(savedProviderSettings);
|
| 167 |
+
|
| 168 |
+
if (typeof parsedProviderSettings === 'object' && parsedProviderSettings !== null) {
|
| 169 |
+
providerSettings = parsedProviderSettings;
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
} catch (error) {
|
| 173 |
+
console.error('Error loading Provider Settings from cookies:', error);
|
| 174 |
+
|
| 175 |
+
// Clear invalid cookie data
|
| 176 |
+
Cookies.remove('providers');
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
let parsedApiKeys: Record<string, string> | undefined = {};
|
| 180 |
+
|
| 181 |
+
try {
|
| 182 |
+
parsedApiKeys = getApiKeysFromCookies();
|
| 183 |
+
setApiKeys(parsedApiKeys);
|
| 184 |
+
} catch (error) {
|
| 185 |
+
console.error('Error loading API keys from cookies:', error);
|
| 186 |
+
|
| 187 |
+
// Clear invalid cookie data
|
| 188 |
+
Cookies.remove('apiKeys');
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
initializeModelList({ apiKeys: parsedApiKeys, providerSettings }).then((modelList) => {
|
| 192 |
+
console.log('Model List: ', modelList);
|
| 193 |
+
setModelList(modelList);
|
| 194 |
+
});
|
| 195 |
+
}, [apiKeys]);
|
| 196 |
+
|
| 197 |
const startListening = () => {
|
| 198 |
if (recognition) {
|
| 199 |
recognition.start();
|