import { themeChange } from "theme-change";
import { useRef } from "react";
import { useContext, useEffect, useState, Dispatch } from "react";
import { clearTotalCost, getTotalCost } from "@/utils/totalCost";
import {
ChatStore,
TemplateChatStore,
TemplateAPI,
TemplateTools,
} from "@/types/chatstore";
import { models } from "@/types/models";
import { tr, Tr, langCodeContext, LANG_OPTIONS } from "@/translate";
import { isVailedJSON } from "@/utils/isVailedJSON";
import { SetAPIsTemplate } from "@/setAPIsTemplate";
import { autoHeight } from "@/utils/textAreaHelp";
import { getDefaultParams } from "@/utils/getDefaultParam";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Sheet,
SheetClose,
SheetContent,
SheetDescription,
SheetFooter,
SheetHeader,
SheetTitle,
SheetTrigger,
} from "@/components/ui/sheet";
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Checkbox } from "@/components/ui/checkbox";
import { Textarea } from "@/components/ui/textarea";
import {
BanIcon,
CheckIcon,
CircleEllipsisIcon,
CogIcon,
Ellipsis,
EyeIcon,
InfoIcon,
KeyIcon,
ListIcon,
MoveHorizontalIcon,
TriangleAlertIcon,
} from "lucide-react";
import { Separator } from "@/components/ui/separator";
import { Slider } from "@/components/ui/slider";
import { NonOverflowScrollArea, ScrollArea } from "@/components/ui/scroll-area";
import { AppContext } from "@/pages/App";
const TTS_VOICES: string[] = [
"alloy",
"echo",
"fable",
"onyx",
"nova",
"shimmer",
];
const TTS_FORMAT: string[] = ["mp3", "opus", "aac", "flac"];
const Help = (props: { children: any; help: string; field: string }) => {
return (
);
};
const SelectModel = (props: { help: string }) => {
const ctx = useContext(AppContext);
if (ctx === null) return <>>;
let shouldIUseCustomModel: boolean = true;
for (const model in models) {
if (ctx.chatStore.model === model) {
shouldIUseCustomModel = false;
}
}
const [useCustomModel, setUseCustomModel] = useState(shouldIUseCustomModel);
return (
setUseCustomModel(!useCustomModel)}
/>
{useCustomModel ? (
) => {
ctx.chatStore.model = e.target.value;
ctx.setChatStore({ ...ctx.chatStore });
}}
/>
) : (
)}
);
};
const LongInput = (props: {
field: "systemMessageContent" | "toolsString";
label: string;
help: string;
}) => {
const ctx = useContext(AppContext);
if (ctx === null) return <>>;
return (
);
};
const InputField = (props: {
field:
| "apiKey"
| "apiEndpoint"
| "whisper_api"
| "whisper_key"
| "tts_api"
| "tts_key"
| "image_gen_api"
| "image_gen_key";
help: string;
}) => {
const ctx = useContext(AppContext);
if (ctx === null) return <>>;
const [hideInput, setHideInput] = useState(true);
return (
<>
>
);
};
const Slicer = (props: {
field: "temperature" | "top_p" | "tts_speed";
help: string;
min: number;
max: number;
}) => {
const ctx = useContext(AppContext);
if (ctx === null) return <>>;
const enable_filed_name: "temperature_enabled" | "top_p_enabled" =
`${props.field}_enabled` as any;
const enabled = ctx.chatStore[enable_filed_name];
if (enabled === null || enabled === undefined) {
if (props.field === "temperature") {
ctx.chatStore[enable_filed_name] = true;
}
if (props.field === "top_p") {
ctx.chatStore[enable_filed_name] = false;
}
}
const setEnabled = (state: boolean) => {
ctx.chatStore[enable_filed_name] = state;
ctx.setChatStore({ ...ctx.chatStore });
};
return (
{enabled && (
)}
);
};
const Number = (props: {
field:
| "totalTokens"
| "maxTokens"
| "maxGenTokens"
| "tokenMargin"
| "postBeginIndex"
| "presence_penalty"
| "frequency_penalty";
readOnly: boolean;
help: string;
}) => {
const ctx = useContext(AppContext);
if (ctx === null) return <>>;
return (
) => {
let newNumber = parseFloat(event.target.value);
if (newNumber < 0) newNumber = 0;
ctx.chatStore[props.field] = newNumber;
ctx.setChatStore({ ...ctx.chatStore });
}}
/>
);
};
const Choice = (props: {
field: "streamMode" | "develop_mode" | "json_mode" | "logprobs";
help: string;
}) => {
const ctx = useContext(AppContext);
if (ctx === null) return <>>;
return (
{
ctx.chatStore[props.field] = checked;
ctx.setChatStore({ ...ctx.chatStore });
}}
/>
);
};
export default (props: {}) => {
const ctx = useContext(AppContext);
if (ctx === null) return <>>;
let link =
location.protocol +
"//" +
location.host +
location.pathname +
`?key=${encodeURIComponent(ctx.chatStore.apiKey)}&api=${encodeURIComponent(
ctx.chatStore.apiEndpoint
)}&mode=${ctx.chatStore.streamMode ? "stream" : "fetch"}&model=${
ctx.chatStore.model
}&sys=${encodeURIComponent(ctx.chatStore.systemMessageContent)}`;
if (ctx.chatStore.develop_mode) {
link = link + `&dev=true`;
}
const importFileRef = useRef(null);
const [totalCost, setTotalCost] = useState(getTotalCost());
// @ts-ignore
const { langCode, setLangCode } = useContext(langCodeContext);
const [open, setOpen] = useState(false);
useEffect(() => {
themeChange(false);
const handleKeyPress = (event: any) => {
if (event.keyCode === 27) {
// keyCode for ESC key is 27
setOpen(false);
}
};
document.addEventListener("keydown", handleKeyPress);
return () => {
document.removeEventListener("keydown", handleKeyPress);
};
}, []); // The empty dependency array ensures that the effect runs only once
return (
{Tr("Settings")}
You can customize the settings here.
Session
Session Cost
Cost of the current session.
$ USD
{ctx.chatStore.cost?.toFixed(4)}
JSON Check:{" "}
{isVailedJSON(ctx.chatStore.toolsString) ? (
) : (
)}
{ctx.chatStore.toolsString.trim() && (
)}
System
<>
Accumulated Cost
in all sessions
$ USD
{totalCost.toFixed(4)}
{
const file = importFileRef.current.files[0];
if (!file || file.type !== "application/json") {
alert(tr("Please select a json file", langCode));
return;
}
const reader = new FileReader();
reader.onload = () => {
if (!reader) {
alert(tr("Empty file", langCode));
return;
}
try {
const newChatStore: ChatStore = JSON.parse(
reader.result as string
);
if (!newChatStore.chatgpt_api_web_version) {
throw tr(
"This is not an exported chatgpt-api-web chatstore file. The key 'chatgpt_api_web_version' is missing!",
langCode
);
}
ctx.setChatStore({ ...newChatStore });
} catch (e) {
alert(
tr(
`Import error on parsing json:`,
langCode
) + `${e}`
);
}
};
reader.readAsText(file);
}}
/>
>
Chat
Chat API
Configure the LLM API settings
Speech Recognition
Whisper API
Configure speech recognition settings
TTS
TTS API
Configure text-to-speech settings
Image Generation
Image Generation API
Configure image generation settings
);
};