import { useEffect, useState } from 'react'; import DialogTemplate from '../../ui/DialogTemplate'; import { Dialog } from '../../ui/Dialog.tsx'; import * as Checkbox from '@radix-ui/react-checkbox'; import { CheckIcon } from '@radix-ui/react-icons'; import FileUpload from '../NewConversationMenu/FileUpload'; import store from '~/store'; import InputWithLabel from './InputWithLabel'; function isJson(str) { try { JSON.parse(str); } catch (e) { return false; } return true; } const SetTokenDialog = ({ open, onOpenChange, endpoint }) => { const [token, setToken] = useState(''); const [showPanel, setShowPanel] = useState(false); const { getToken, saveToken } = store.useToken(endpoint); const submit = () => { saveToken(token); onOpenChange(false); }; useEffect(() => { let oldToken = getToken(); if (isJson(token)) { setShowPanel(true); } setToken(oldToken ?? ''); }, [open]); useEffect(() => { if (!showPanel && isJson(token)) { setToken(''); } }, [showPanel]); const helpText = { bingAI: ( {`As of 5/23/23, to use Bing, you will need your full cookie string from bing.com. Use dev tools or an extension while logged into the site to view it in your network request Cookie header value. For full instructions, see my `} comment here ), chatGPTBrowser: ( {`To get your Access token For ChatGPT 'Free Version', login to `} https://chat.openai.com , then visit{' '} https://chat.openai.com/api/auth/session . Copy access token. ), google: ( You need to{' '} Enable Vertex AI {' '} API on Google Cloud, then{' '} Create a Service Account {`. Make sure to click 'Create and Continue' to give at least the 'Vertex AI User' role. Lastly, create a JSON key to import here.`} ) }; function getAzure(name) { if (isJson(token)) { let newToken = JSON.parse(token); return newToken[name]; } else { return ''; } } function setAzure(name, value) { let newToken = {}; if (isJson(token)) { newToken = JSON.parse(token); } newToken[name] = value; setToken(JSON.stringify(newToken)); } return ( {endpoint === 'google' ? ( { if (!credentials) { return false; } if ( !credentials.client_email || typeof credentials.client_email !== 'string' || credentials.client_email.length <= 2 ) { return false; } if ( !credentials.project_id || typeof credentials.project_id !== 'string' || credentials.project_id.length <= 2 ) { return false; } if ( !credentials.private_key || typeof credentials.private_key !== 'string' || credentials.private_key.length <= 600 ) { return false; } return true; }} onFileSelected={(data) => { setToken(JSON.stringify(data)); }} /> ) : endpoint === 'openAI' ? ( <> {!showPanel ? ( <> setToken(e.target.value || '')} label={'OpenAI API Key'} /> ) : ( <> setAzure('instanceName', e.target.value || '')} label={'Azure OpenAI Instance Name'} /> setAzure('deploymentName', e.target.value || '')} label={'Azure OpenAI Deployment Name'} /> setAzure('version', e.target.value || '')} label={'Azure OpenAI API Version'} /> setAzure('apiKey', e.target.value || '')} label={'Azure OpenAI API Key'} /> )}
setShowPanel(!showPanel)} >
) : ( <> setToken(e.target.value || '')} label={'Token Name'} /> )} Your token will be sent to the server, but not saved. {helpText?.[endpoint]} } selection={{ selectHandler: submit, selectClasses: 'bg-green-600 hover:bg-green-700 dark:hover:bg-green-800 text-white', selectText: 'Submit' }} />
); }; export default SetTokenDialog;