import { Button, Textarea } from "@mui/joy"; import { reverse } from "lodash-es"; import { useEffect, useState } from "react"; import * as api from "../helpers/api"; import useLoading from "../hooks/useLoading"; import { marked } from "../labs/marked"; import Icon from "./Icon"; import { generateDialog } from "./Dialog"; import toastHelper from "./Toast"; import showSettingDialog from "./SettingDialog"; type Props = DialogProps; interface History { question: string; answer: string; } const AskAIDialog: React.FC = (props: Props) => { const { destroy, hide } = props; const fetchingState = useLoading(false); const [historyList, setHistoryList] = useState([]); const [isEnabled, setIsEnabled] = useState(true); useEffect(() => { api.checkOpenAIEnabled().then(({ data }) => { const { data: enabled } = data; setIsEnabled(enabled); }); }, []); const handleGotoSystemSetting = () => { showSettingDialog("system"); destroy(); }; const handleQuestionTextareaKeyDown = async (event: React.KeyboardEvent) => { if (event.key === "Enter") { event.preventDefault(); const question = event.currentTarget.value; event.currentTarget.value = ""; fetchingState.setLoading(); try { await askQuestion(question); } catch (error: any) { console.error(error); toastHelper.error(error.response.data.error); } fetchingState.setFinish(); } }; const askQuestion = async (question: string) => { if (question === "") { return; } const { data: { data: answer }, } = await api.postChatCompletion(question); setHistoryList([ ...historyList, { question, answer: answer.replace(/^\n\n/, ""), }, ]); }; return ( <>

Ask AI