2023-05-23 13:58:18 -04:00
|
|
|
import { useEffect, useState } from 'react';
|
2023-04-10 00:41:34 +08:00
|
|
|
import DialogTemplate from '../../ui/DialogTemplate';
|
|
|
|
|
import { Dialog } from '../../ui/Dialog.tsx';
|
2023-05-18 04:51:30 +05:30
|
|
|
import * as Checkbox from '@radix-ui/react-checkbox';
|
|
|
|
|
import { CheckIcon } from '@radix-ui/react-icons';
|
2023-05-13 16:29:06 -04:00
|
|
|
import FileUpload from '../NewConversationMenu/FileUpload';
|
2023-04-10 00:41:34 +08:00
|
|
|
import store from '~/store';
|
2023-05-18 04:51:30 +05:30
|
|
|
import InputWithLabel from './InputWithLabel';
|
|
|
|
|
|
|
|
|
|
function isJson(str) {
|
|
|
|
|
try {
|
|
|
|
|
JSON.parse(str);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-04-10 00:41:34 +08:00
|
|
|
|
|
|
|
|
const SetTokenDialog = ({ open, onOpenChange, endpoint }) => {
|
|
|
|
|
const [token, setToken] = useState('');
|
2023-05-18 04:51:30 +05:30
|
|
|
const [showPanel, setShowPanel] = useState(false);
|
2023-04-10 00:41:34 +08:00
|
|
|
const { getToken, saveToken } = store.useToken(endpoint);
|
|
|
|
|
|
|
|
|
|
const submit = () => {
|
|
|
|
|
saveToken(token);
|
|
|
|
|
onOpenChange(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-05-18 04:51:30 +05:30
|
|
|
let oldToken = getToken();
|
|
|
|
|
if (isJson(token)) {
|
|
|
|
|
setShowPanel(true);
|
|
|
|
|
}
|
|
|
|
|
setToken(oldToken ?? '');
|
2023-04-10 00:41:34 +08:00
|
|
|
}, [open]);
|
|
|
|
|
|
2023-05-18 04:51:30 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (!showPanel && isJson(token)) {
|
|
|
|
|
setToken('');
|
|
|
|
|
}
|
|
|
|
|
}, [showPanel]);
|
|
|
|
|
|
2023-04-10 00:41:34 +08:00
|
|
|
const helpText = {
|
|
|
|
|
bingAI: (
|
|
|
|
|
<small className="break-all text-gray-600">
|
2023-05-31 00:41:01 -04:00
|
|
|
{`To get your Access token for Bing, login to `}
|
|
|
|
|
<a
|
|
|
|
|
target="_blank"
|
|
|
|
|
href="https://www.bing.com"
|
|
|
|
|
rel="noreferrer"
|
|
|
|
|
className="text-blue-600 underline"
|
|
|
|
|
>
|
|
|
|
|
https://www.bing.com
|
|
|
|
|
</a>
|
|
|
|
|
{`. Use dev tools or an extension while logged into the site to copy the content of the _U cookie.
|
2023-05-29 11:00:51 -04:00
|
|
|
If this fails, follow these `}
|
2023-05-23 13:58:18 -04:00
|
|
|
<a
|
|
|
|
|
target="_blank"
|
|
|
|
|
href="https://github.com/waylaidwanderer/node-chatgpt-api/issues/378#issuecomment-1559868368"
|
|
|
|
|
rel="noreferrer"
|
|
|
|
|
className="text-blue-600 underline"
|
|
|
|
|
>
|
2023-05-29 11:00:51 -04:00
|
|
|
instructions
|
2023-05-23 13:58:18 -04:00
|
|
|
</a>
|
2023-05-29 11:00:51 -04:00
|
|
|
{` to provide the full cookie strings.`}
|
2023-04-10 00:41:34 +08:00
|
|
|
</small>
|
|
|
|
|
),
|
|
|
|
|
chatGPTBrowser: (
|
|
|
|
|
<small className="break-all text-gray-600">
|
2023-05-23 13:58:18 -04:00
|
|
|
{`To get your Access token For ChatGPT 'Free Version', login to `}
|
2023-04-10 00:41:34 +08:00
|
|
|
<a
|
|
|
|
|
target="_blank"
|
|
|
|
|
href="https://chat.openai.com"
|
|
|
|
|
rel="noreferrer"
|
|
|
|
|
className="text-blue-600 underline"
|
|
|
|
|
>
|
|
|
|
|
https://chat.openai.com
|
|
|
|
|
</a>
|
|
|
|
|
, then visit{' '}
|
|
|
|
|
<a
|
|
|
|
|
target="_blank"
|
|
|
|
|
href="https://chat.openai.com/api/auth/session"
|
|
|
|
|
rel="noreferrer"
|
|
|
|
|
className="text-blue-600 underline"
|
|
|
|
|
>
|
|
|
|
|
https://chat.openai.com/api/auth/session
|
|
|
|
|
</a>
|
|
|
|
|
. Copy access token.
|
|
|
|
|
</small>
|
2023-05-13 16:29:06 -04:00
|
|
|
),
|
|
|
|
|
google: (
|
|
|
|
|
<small className="break-all text-gray-600">
|
|
|
|
|
You need to{' '}
|
|
|
|
|
<a
|
|
|
|
|
target="_blank"
|
|
|
|
|
href="https://console.cloud.google.com/vertex-ai"
|
|
|
|
|
rel="noreferrer"
|
|
|
|
|
className="text-blue-600 underline"
|
|
|
|
|
>
|
|
|
|
|
Enable Vertex AI
|
|
|
|
|
</a>{' '}
|
|
|
|
|
API on Google Cloud, then{' '}
|
|
|
|
|
<a
|
|
|
|
|
target="_blank"
|
|
|
|
|
href="https://console.cloud.google.com/projectselector/iam-admin/serviceaccounts/create?walkthrough_id=iam--create-service-account#step_index=1"
|
|
|
|
|
rel="noreferrer"
|
|
|
|
|
className="text-blue-600 underline"
|
|
|
|
|
>
|
|
|
|
|
Create a Service Account
|
|
|
|
|
</a>
|
2023-05-23 13:58:18 -04:00
|
|
|
{`. Make sure to click 'Create and Continue' to give at least the 'Vertex AI User' role.
|
|
|
|
|
Lastly, create a JSON key to import here.`}
|
2023-05-13 16:29:06 -04:00
|
|
|
</small>
|
2023-04-10 00:41:34 +08:00
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-18 04:51:30 +05:30
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 00:41:34 +08:00
|
|
|
return (
|
2023-05-18 11:09:31 -07:00
|
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
2023-04-10 00:41:34 +08:00
|
|
|
<DialogTemplate
|
|
|
|
|
title={`Set Token of ${endpoint}`}
|
|
|
|
|
main={
|
|
|
|
|
<div className="grid w-full items-center gap-2">
|
2023-05-13 16:29:06 -04:00
|
|
|
{endpoint === 'google' ? (
|
|
|
|
|
<FileUpload
|
2023-05-18 04:51:30 +05:30
|
|
|
id="googleKey"
|
2023-05-13 16:29:06 -04:00
|
|
|
className="w-full"
|
|
|
|
|
text="Import Service Account JSON Key"
|
|
|
|
|
successText="Successfully Imported Service Account JSON Key"
|
|
|
|
|
invalidText="Invalid Service Account JSON Key, Did you import the correct file?"
|
2023-05-18 11:09:31 -07:00
|
|
|
validator={(credentials) => {
|
2023-05-13 16:29:06 -04:00
|
|
|
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;
|
|
|
|
|
}}
|
2023-05-18 11:09:31 -07:00
|
|
|
onFileSelected={(data) => {
|
2023-05-13 16:29:06 -04:00
|
|
|
setToken(JSON.stringify(data));
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2023-05-18 04:51:30 +05:30
|
|
|
) : endpoint === 'openAI' ? (
|
|
|
|
|
<>
|
|
|
|
|
{!showPanel ? (
|
|
|
|
|
<>
|
|
|
|
|
<InputWithLabel
|
|
|
|
|
id={'chatGPTLabel'}
|
|
|
|
|
value={token || ''}
|
2023-05-18 11:44:07 -07:00
|
|
|
onChange={(e) => setToken(e.target.value || '')}
|
2023-05-18 04:51:30 +05:30
|
|
|
label={'OpenAI API Key'}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<InputWithLabel
|
|
|
|
|
id={'instanceNameLabel'}
|
|
|
|
|
value={getAzure('instanceName') || ''}
|
2023-05-18 11:44:07 -07:00
|
|
|
onChange={(e) => setAzure('instanceName', e.target.value || '')}
|
2023-05-18 04:51:30 +05:30
|
|
|
label={'Azure OpenAI Instance Name'}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<InputWithLabel
|
|
|
|
|
id={'deploymentNameLabel'}
|
|
|
|
|
value={getAzure('deploymentName') || ''}
|
2023-05-18 11:44:07 -07:00
|
|
|
onChange={(e) => setAzure('deploymentName', e.target.value || '')}
|
2023-05-18 04:51:30 +05:30
|
|
|
label={'Azure OpenAI Deployment Name'}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<InputWithLabel
|
|
|
|
|
id={'versionLabel'}
|
|
|
|
|
value={getAzure('version') || ''}
|
2023-05-18 11:44:07 -07:00
|
|
|
onChange={(e) => setAzure('version', e.target.value || '')}
|
2023-05-18 04:51:30 +05:30
|
|
|
label={'Azure OpenAI API Version'}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<InputWithLabel
|
|
|
|
|
id={'apiKeyLabel'}
|
|
|
|
|
value={getAzure('apiKey') || ''}
|
2023-05-18 11:44:07 -07:00
|
|
|
onChange={(e) => setAzure('apiKey', e.target.value || '')}
|
2023-05-18 04:51:30 +05:30
|
|
|
label={'Azure OpenAI API Key'}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
2023-05-13 16:29:06 -04:00
|
|
|
)}
|
2023-05-18 04:51:30 +05:30
|
|
|
<div className="flex items-center">
|
|
|
|
|
<Checkbox.Root
|
|
|
|
|
className="flex h-[20px] w-[20px] appearance-none items-center justify-center rounded-[4px] bg-gray-100 text-white outline-none hover:bg-gray-200 dark:bg-gray-700 dark:hover:bg-gray-900"
|
|
|
|
|
id="azureOpenAI"
|
|
|
|
|
checked={showPanel}
|
|
|
|
|
onCheckedChange={() => setShowPanel(!showPanel)}
|
|
|
|
|
>
|
|
|
|
|
<Checkbox.Indicator className="flex h-[20px] w-[20px] items-center justify-center rounded-[3.5px] bg-green-600">
|
|
|
|
|
<CheckIcon />
|
|
|
|
|
</Checkbox.Indicator>
|
|
|
|
|
</Checkbox.Root>
|
|
|
|
|
|
|
|
|
|
<label
|
|
|
|
|
className="pl-[8px] text-[15px] leading-none dark:text-white"
|
|
|
|
|
htmlFor="azureOpenAI"
|
|
|
|
|
>
|
|
|
|
|
Use Azure OpenAI.
|
|
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<InputWithLabel
|
|
|
|
|
id={'chatGPTLabel'}
|
|
|
|
|
value={token || ''}
|
2023-05-18 11:44:07 -07:00
|
|
|
onChange={(e) => setToken(e.target.value || '')}
|
2023-05-18 04:51:30 +05:30
|
|
|
label={'Token Name'}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
2023-05-13 16:29:06 -04:00
|
|
|
)}
|
2023-05-18 11:09:31 -07:00
|
|
|
<small className="text-red-600">
|
|
|
|
|
Your token will be sent to the server, but not saved.
|
|
|
|
|
</small>
|
2023-04-10 00:41:34 +08:00
|
|
|
{helpText?.[endpoint]}
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
selection={{
|
|
|
|
|
selectHandler: submit,
|
|
|
|
|
selectClasses: 'bg-green-600 hover:bg-green-700 dark:hover:bg-green-800 text-white',
|
|
|
|
|
selectText: 'Submit'
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SetTokenDialog;
|