chat.tsx 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888
  1. import { useDebouncedCallback } from "use-debounce";
  2. import React, {
  3. useState,
  4. useRef,
  5. useEffect,
  6. useMemo,
  7. useCallback,
  8. Fragment,
  9. RefObject,
  10. } from "react";
  11. import SendWhiteIcon from "../icons/send-white.svg";
  12. import VoiceWhiteIcon from "../icons/voice-white.svg";
  13. import BrainIcon from "../icons/brain.svg";
  14. import RenameIcon from "../icons/rename.svg";
  15. import ExportIcon from "../icons/share.svg";
  16. import ReturnIcon from "../icons/return.svg";
  17. import CopyIcon from "../icons/copy.svg";
  18. import SpeakIcon from "../icons/speak.svg";
  19. import SpeakStopIcon from "../icons/speak-stop.svg";
  20. import LoadingIcon from "../icons/three-dots.svg";
  21. import LoadingButtonIcon from "../icons/loading.svg";
  22. import PromptIcon from "../icons/prompt.svg";
  23. import MaskIcon from "../icons/mask.svg";
  24. import MaxIcon from "../icons/max.svg";
  25. import MinIcon from "../icons/min.svg";
  26. import ResetIcon from "../icons/reload.svg";
  27. import BreakIcon from "../icons/break.svg";
  28. import SettingsIcon from "../icons/chat-settings.svg";
  29. import DeleteIcon from "../icons/clear.svg";
  30. import PinIcon from "../icons/pin.svg";
  31. import EditIcon from "../icons/rename.svg";
  32. import ConfirmIcon from "../icons/confirm.svg";
  33. import CancelIcon from "../icons/cancel.svg";
  34. import ImageIcon from "../icons/image.svg";
  35. import LightIcon from "../icons/light.svg";
  36. import DarkIcon from "../icons/dark.svg";
  37. import AutoIcon from "../icons/auto.svg";
  38. import BottomIcon from "../icons/bottom.svg";
  39. import StopIcon from "../icons/pause.svg";
  40. import RobotIcon from "../icons/robot.svg";
  41. import SizeIcon from "../icons/size.svg";
  42. import QualityIcon from "../icons/hd.svg";
  43. import StyleIcon from "../icons/palette.svg";
  44. import PluginIcon from "../icons/plugin.svg";
  45. import {
  46. ChatMessage,
  47. SubmitKey,
  48. useChatStore,
  49. BOT_HELLO,
  50. createMessage,
  51. useAccessStore,
  52. Theme,
  53. useAppConfig,
  54. DEFAULT_TOPIC,
  55. ModelType,
  56. } from "../store";
  57. import {
  58. copyToClipboard,
  59. selectOrCopy,
  60. autoGrowTextArea,
  61. useMobileScreen,
  62. getMessageTextContent,
  63. getMessageImages,
  64. isVisionModel,
  65. isDalle3,
  66. isFirefox,
  67. } from "../utils";
  68. import { uploadImage as uploadImageRemote } from "@/app/utils/chat";
  69. import dynamic from "next/dynamic";
  70. import { ChatControllerPool } from "../client/controller";
  71. import { DalleSize, DalleQuality, DalleStyle } from "../typing";
  72. import { Prompt, usePromptStore } from "../store/prompt";
  73. import Locale, { getLang, getSTTLang } from "../locales";
  74. import { IconButton } from "./button";
  75. import styles from "./chat.module.scss";
  76. import {
  77. List,
  78. ListItem,
  79. Modal,
  80. Selector,
  81. showConfirm,
  82. showPrompt,
  83. showToast,
  84. } from "./ui-lib";
  85. import { useNavigate } from "react-router-dom";
  86. import {
  87. CHAT_PAGE_SIZE,
  88. DEFAULT_STT_ENGINE,
  89. DEFAULT_TTS_ENGINE,
  90. FIREFOX_DEFAULT_STT_ENGINE,
  91. ModelProvider,
  92. LAST_INPUT_KEY,
  93. Path,
  94. REQUEST_TIMEOUT_MS,
  95. UNFINISHED_INPUT,
  96. ServiceProvider,
  97. Plugin,
  98. } from "../constant";
  99. import { Avatar } from "./emoji";
  100. import { ContextPrompts, MaskAvatar, MaskConfig } from "./mask";
  101. import { useMaskStore } from "../store/mask";
  102. import { ChatCommandPrefix, useChatCommand, useCommand } from "../command";
  103. import { prettyObject } from "../utils/format";
  104. import { ExportMessageModal } from "./exporter";
  105. import { getClientConfig } from "../config/client";
  106. import { useAllModels } from "../utils/hooks";
  107. import { MultimodalContent } from "../client/api";
  108. import { ClientApi } from "../client/api";
  109. import { createTTSPlayer } from "../utils/audio";
  110. import {
  111. OpenAITranscriptionApi,
  112. SpeechApi,
  113. WebTranscriptionApi,
  114. } from "../utils/speech";
  115. import { MsEdgeTTS, OUTPUT_FORMAT } from "../utils/ms_edge_tts";
  116. const ttsPlayer = createTTSPlayer();
  117. const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
  118. loading: () => <LoadingIcon />,
  119. });
  120. export function SessionConfigModel(props: { onClose: () => void }) {
  121. const chatStore = useChatStore();
  122. const session = chatStore.currentSession();
  123. const maskStore = useMaskStore();
  124. const navigate = useNavigate();
  125. return (
  126. <div className="modal-mask">
  127. <Modal
  128. title={Locale.Context.Edit}
  129. onClose={() => props.onClose()}
  130. actions={[
  131. <IconButton
  132. key="reset"
  133. icon={<ResetIcon />}
  134. bordered
  135. text={Locale.Chat.Config.Reset}
  136. onClick={async () => {
  137. if (await showConfirm(Locale.Memory.ResetConfirm)) {
  138. chatStore.updateCurrentSession(
  139. (session) => (session.memoryPrompt = ""),
  140. );
  141. }
  142. }}
  143. />,
  144. <IconButton
  145. key="copy"
  146. icon={<CopyIcon />}
  147. bordered
  148. text={Locale.Chat.Config.SaveAs}
  149. onClick={() => {
  150. navigate(Path.Masks);
  151. setTimeout(() => {
  152. maskStore.create(session.mask);
  153. }, 500);
  154. }}
  155. />,
  156. ]}
  157. >
  158. <MaskConfig
  159. mask={session.mask}
  160. updateMask={(updater) => {
  161. const mask = { ...session.mask };
  162. updater(mask);
  163. chatStore.updateCurrentSession((session) => (session.mask = mask));
  164. }}
  165. shouldSyncFromGlobal
  166. extraListItems={
  167. session.mask.modelConfig.sendMemory ? (
  168. <ListItem
  169. className="copyable"
  170. title={`${Locale.Memory.Title} (${session.lastSummarizeIndex} of ${session.messages.length})`}
  171. subTitle={session.memoryPrompt || Locale.Memory.EmptyContent}
  172. ></ListItem>
  173. ) : (
  174. <></>
  175. )
  176. }
  177. ></MaskConfig>
  178. </Modal>
  179. </div>
  180. );
  181. }
  182. function PromptToast(props: {
  183. showToast?: boolean;
  184. showModal?: boolean;
  185. setShowModal: (_: boolean) => void;
  186. }) {
  187. const chatStore = useChatStore();
  188. const session = chatStore.currentSession();
  189. const context = session.mask.context;
  190. return (
  191. <div className={styles["prompt-toast"]} key="prompt-toast">
  192. {props.showToast && (
  193. <div
  194. className={styles["prompt-toast-inner"] + " clickable"}
  195. role="button"
  196. onClick={() => props.setShowModal(true)}
  197. >
  198. <BrainIcon />
  199. <span className={styles["prompt-toast-content"]}>
  200. {Locale.Context.Toast(context.length)}
  201. </span>
  202. </div>
  203. )}
  204. {props.showModal && (
  205. <SessionConfigModel onClose={() => props.setShowModal(false)} />
  206. )}
  207. </div>
  208. );
  209. }
  210. function useSubmitHandler() {
  211. const config = useAppConfig();
  212. const submitKey = config.submitKey;
  213. const isComposing = useRef(false);
  214. useEffect(() => {
  215. const onCompositionStart = () => {
  216. isComposing.current = true;
  217. };
  218. const onCompositionEnd = () => {
  219. isComposing.current = false;
  220. };
  221. window.addEventListener("compositionstart", onCompositionStart);
  222. window.addEventListener("compositionend", onCompositionEnd);
  223. return () => {
  224. window.removeEventListener("compositionstart", onCompositionStart);
  225. window.removeEventListener("compositionend", onCompositionEnd);
  226. };
  227. }, []);
  228. const shouldSubmit = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  229. // Fix Chinese input method "Enter" on Safari
  230. if (e.keyCode == 229) return false;
  231. if (e.key !== "Enter") return false;
  232. if (e.key === "Enter" && (e.nativeEvent.isComposing || isComposing.current))
  233. return false;
  234. return (
  235. (config.submitKey === SubmitKey.AltEnter && e.altKey) ||
  236. (config.submitKey === SubmitKey.CtrlEnter && e.ctrlKey) ||
  237. (config.submitKey === SubmitKey.ShiftEnter && e.shiftKey) ||
  238. (config.submitKey === SubmitKey.MetaEnter && e.metaKey) ||
  239. (config.submitKey === SubmitKey.Enter &&
  240. !e.altKey &&
  241. !e.ctrlKey &&
  242. !e.shiftKey &&
  243. !e.metaKey)
  244. );
  245. };
  246. return {
  247. submitKey,
  248. shouldSubmit,
  249. };
  250. }
  251. export type RenderPrompt = Pick<Prompt, "title" | "content">;
  252. export function PromptHints(props: {
  253. prompts: RenderPrompt[];
  254. onPromptSelect: (prompt: RenderPrompt) => void;
  255. }) {
  256. const noPrompts = props.prompts.length === 0;
  257. const [selectIndex, setSelectIndex] = useState(0);
  258. const selectedRef = useRef<HTMLDivElement>(null);
  259. useEffect(() => {
  260. setSelectIndex(0);
  261. }, [props.prompts.length]);
  262. useEffect(() => {
  263. const onKeyDown = (e: KeyboardEvent) => {
  264. if (noPrompts || e.metaKey || e.altKey || e.ctrlKey) {
  265. return;
  266. }
  267. // arrow up / down to select prompt
  268. const changeIndex = (delta: number) => {
  269. e.stopPropagation();
  270. e.preventDefault();
  271. const nextIndex = Math.max(
  272. 0,
  273. Math.min(props.prompts.length - 1, selectIndex + delta),
  274. );
  275. setSelectIndex(nextIndex);
  276. selectedRef.current?.scrollIntoView({
  277. block: "center",
  278. });
  279. };
  280. if (e.key === "ArrowUp") {
  281. changeIndex(1);
  282. } else if (e.key === "ArrowDown") {
  283. changeIndex(-1);
  284. } else if (e.key === "Enter") {
  285. const selectedPrompt = props.prompts.at(selectIndex);
  286. if (selectedPrompt) {
  287. props.onPromptSelect(selectedPrompt);
  288. }
  289. }
  290. };
  291. window.addEventListener("keydown", onKeyDown);
  292. return () => window.removeEventListener("keydown", onKeyDown);
  293. // eslint-disable-next-line react-hooks/exhaustive-deps
  294. }, [props.prompts.length, selectIndex]);
  295. if (noPrompts) return null;
  296. return (
  297. <div className={styles["prompt-hints"]}>
  298. {props.prompts.map((prompt, i) => (
  299. <div
  300. ref={i === selectIndex ? selectedRef : null}
  301. className={
  302. styles["prompt-hint"] +
  303. ` ${i === selectIndex ? styles["prompt-hint-selected"] : ""}`
  304. }
  305. key={prompt.title + i.toString()}
  306. onClick={() => props.onPromptSelect(prompt)}
  307. onMouseEnter={() => setSelectIndex(i)}
  308. >
  309. <div className={styles["hint-title"]}>{prompt.title}</div>
  310. <div className={styles["hint-content"]}>{prompt.content}</div>
  311. </div>
  312. ))}
  313. </div>
  314. );
  315. }
  316. function ClearContextDivider() {
  317. const chatStore = useChatStore();
  318. return (
  319. <div
  320. className={styles["clear-context"]}
  321. onClick={() =>
  322. chatStore.updateCurrentSession(
  323. (session) => (session.clearContextIndex = undefined),
  324. )
  325. }
  326. >
  327. <div className={styles["clear-context-tips"]}>{Locale.Context.Clear}</div>
  328. <div className={styles["clear-context-revert-btn"]}>
  329. {Locale.Context.Revert}
  330. </div>
  331. </div>
  332. );
  333. }
  334. export function ChatAction(props: {
  335. text: string;
  336. icon: JSX.Element;
  337. onClick: () => void;
  338. }) {
  339. const iconRef = useRef<HTMLDivElement>(null);
  340. const textRef = useRef<HTMLDivElement>(null);
  341. const [width, setWidth] = useState({
  342. full: 16,
  343. icon: 16,
  344. });
  345. function updateWidth() {
  346. if (!iconRef.current || !textRef.current) return;
  347. const getWidth = (dom: HTMLDivElement) => dom.getBoundingClientRect().width;
  348. const textWidth = getWidth(textRef.current);
  349. const iconWidth = getWidth(iconRef.current);
  350. setWidth({
  351. full: textWidth + iconWidth,
  352. icon: iconWidth,
  353. });
  354. }
  355. return (
  356. <div
  357. className={`${styles["chat-input-action"]} clickable`}
  358. onClick={() => {
  359. props.onClick();
  360. setTimeout(updateWidth, 1);
  361. }}
  362. onMouseEnter={updateWidth}
  363. onTouchStart={updateWidth}
  364. style={
  365. {
  366. "--icon-width": `${width.icon}px`,
  367. "--full-width": `${width.full}px`,
  368. } as React.CSSProperties
  369. }
  370. >
  371. <div ref={iconRef} className={styles["icon"]}>
  372. {props.icon}
  373. </div>
  374. <div className={styles["text"]} ref={textRef}>
  375. {props.text}
  376. </div>
  377. </div>
  378. );
  379. }
  380. function useScrollToBottom(
  381. scrollRef: RefObject<HTMLDivElement>,
  382. detach: boolean = false,
  383. ) {
  384. // for auto-scroll
  385. const [autoScroll, setAutoScroll] = useState(true);
  386. function scrollDomToBottom() {
  387. const dom = scrollRef.current;
  388. if (dom) {
  389. requestAnimationFrame(() => {
  390. setAutoScroll(true);
  391. dom.scrollTo(0, dom.scrollHeight);
  392. });
  393. }
  394. }
  395. // auto scroll
  396. useEffect(() => {
  397. if (autoScroll && !detach) {
  398. scrollDomToBottom();
  399. }
  400. });
  401. return {
  402. scrollRef,
  403. autoScroll,
  404. setAutoScroll,
  405. scrollDomToBottom,
  406. };
  407. }
  408. export function ChatActions(props: {
  409. uploadImage: () => void;
  410. setAttachImages: (images: string[]) => void;
  411. setUploading: (uploading: boolean) => void;
  412. showPromptModal: () => void;
  413. scrollToBottom: () => void;
  414. showPromptHints: () => void;
  415. hitBottom: boolean;
  416. uploading: boolean;
  417. }) {
  418. const config = useAppConfig();
  419. const navigate = useNavigate();
  420. const chatStore = useChatStore();
  421. // switch themes
  422. const theme = config.theme;
  423. function nextTheme() {
  424. const themes = [Theme.Auto, Theme.Light, Theme.Dark];
  425. const themeIndex = themes.indexOf(theme);
  426. const nextIndex = (themeIndex + 1) % themes.length;
  427. const nextTheme = themes[nextIndex];
  428. config.update((config) => (config.theme = nextTheme));
  429. }
  430. // stop all responses
  431. const couldStop = ChatControllerPool.hasPending();
  432. const stopAll = () => ChatControllerPool.stopAll();
  433. // switch model
  434. const currentModel = chatStore.currentSession().mask.modelConfig.model;
  435. const currentProviderName =
  436. chatStore.currentSession().mask.modelConfig?.providerName ||
  437. ServiceProvider.OpenAI;
  438. const allModels = useAllModels();
  439. const models = useMemo(() => {
  440. const filteredModels = allModels.filter((m) => m.available);
  441. const defaultModel = filteredModels.find((m) => m.isDefault);
  442. if (defaultModel) {
  443. const arr = [
  444. defaultModel,
  445. ...filteredModels.filter((m) => m !== defaultModel),
  446. ];
  447. return arr;
  448. } else {
  449. return filteredModels;
  450. }
  451. }, [allModels]);
  452. const currentModelName = useMemo(() => {
  453. const model = models.find(
  454. (m) =>
  455. m.name == currentModel &&
  456. m?.provider?.providerName == currentProviderName,
  457. );
  458. return model?.displayName ?? "";
  459. }, [models, currentModel, currentProviderName]);
  460. const [showModelSelector, setShowModelSelector] = useState(false);
  461. const [showPluginSelector, setShowPluginSelector] = useState(false);
  462. const [showUploadImage, setShowUploadImage] = useState(false);
  463. const [showSizeSelector, setShowSizeSelector] = useState(false);
  464. const [showQualitySelector, setShowQualitySelector] = useState(false);
  465. const [showStyleSelector, setShowStyleSelector] = useState(false);
  466. const dalle3Sizes: DalleSize[] = ["1024x1024", "1792x1024", "1024x1792"];
  467. const dalle3Qualitys: DalleQuality[] = ["standard", "hd"];
  468. const dalle3Styles: DalleStyle[] = ["vivid", "natural"];
  469. const currentSize =
  470. chatStore.currentSession().mask.modelConfig?.size ?? "1024x1024";
  471. const currentQuality =
  472. chatStore.currentSession().mask.modelConfig?.quality ?? "standard";
  473. const currentStyle =
  474. chatStore.currentSession().mask.modelConfig?.style ?? "vivid";
  475. useEffect(() => {
  476. const show = isVisionModel(currentModel);
  477. setShowUploadImage(show);
  478. if (!show) {
  479. props.setAttachImages([]);
  480. props.setUploading(false);
  481. }
  482. // if current model is not available
  483. // switch to first available model
  484. const isUnavaliableModel = !models.some((m) => m.name === currentModel);
  485. if (isUnavaliableModel && models.length > 0) {
  486. // show next model to default model if exist
  487. let nextModel = models.find((model) => model.isDefault) || models[0];
  488. chatStore.updateCurrentSession((session) => {
  489. session.mask.modelConfig.model = nextModel.name;
  490. session.mask.modelConfig.providerName = nextModel?.provider
  491. ?.providerName as ServiceProvider;
  492. });
  493. showToast(
  494. nextModel?.provider?.providerName == "ByteDance"
  495. ? nextModel.displayName
  496. : nextModel.name,
  497. );
  498. }
  499. }, [chatStore, currentModel, models]);
  500. return (
  501. <div className={styles["chat-input-actions"]}>
  502. {couldStop && (
  503. <ChatAction
  504. onClick={stopAll}
  505. text={Locale.Chat.InputActions.Stop}
  506. icon={<StopIcon />}
  507. />
  508. )}
  509. {!props.hitBottom && (
  510. <ChatAction
  511. onClick={props.scrollToBottom}
  512. text={Locale.Chat.InputActions.ToBottom}
  513. icon={<BottomIcon />}
  514. />
  515. )}
  516. {props.hitBottom && (
  517. <ChatAction
  518. onClick={props.showPromptModal}
  519. text={Locale.Chat.InputActions.Settings}
  520. icon={<SettingsIcon />}
  521. />
  522. )}
  523. {showUploadImage && (
  524. <ChatAction
  525. onClick={props.uploadImage}
  526. text={Locale.Chat.InputActions.UploadImage}
  527. icon={props.uploading ? <LoadingButtonIcon /> : <ImageIcon />}
  528. />
  529. )}
  530. <ChatAction
  531. onClick={nextTheme}
  532. text={Locale.Chat.InputActions.Theme[theme]}
  533. icon={
  534. <>
  535. {theme === Theme.Auto ? (
  536. <AutoIcon />
  537. ) : theme === Theme.Light ? (
  538. <LightIcon />
  539. ) : theme === Theme.Dark ? (
  540. <DarkIcon />
  541. ) : null}
  542. </>
  543. }
  544. />
  545. <ChatAction
  546. onClick={props.showPromptHints}
  547. text={Locale.Chat.InputActions.Prompt}
  548. icon={<PromptIcon />}
  549. />
  550. <ChatAction
  551. onClick={() => {
  552. navigate(Path.Masks);
  553. }}
  554. text={Locale.Chat.InputActions.Masks}
  555. icon={<MaskIcon />}
  556. />
  557. <ChatAction
  558. text={Locale.Chat.InputActions.Clear}
  559. icon={<BreakIcon />}
  560. onClick={() => {
  561. chatStore.updateCurrentSession((session) => {
  562. if (session.clearContextIndex === session.messages.length) {
  563. session.clearContextIndex = undefined;
  564. } else {
  565. session.clearContextIndex = session.messages.length;
  566. session.memoryPrompt = ""; // will clear memory
  567. }
  568. });
  569. }}
  570. />
  571. <ChatAction
  572. onClick={() => setShowModelSelector(true)}
  573. text={currentModelName}
  574. icon={<RobotIcon />}
  575. />
  576. {showModelSelector && (
  577. <Selector
  578. defaultSelectedValue={`${currentModel}@${currentProviderName}`}
  579. items={models.map((m) => ({
  580. title: `${m.displayName}${
  581. m?.provider?.providerName
  582. ? "(" + m?.provider?.providerName + ")"
  583. : ""
  584. }`,
  585. value: `${m.name}@${m?.provider?.providerName}`,
  586. }))}
  587. onClose={() => setShowModelSelector(false)}
  588. onSelection={(s) => {
  589. if (s.length === 0) return;
  590. const [model, providerName] = s[0].split("@");
  591. chatStore.updateCurrentSession((session) => {
  592. session.mask.modelConfig.model = model as ModelType;
  593. session.mask.modelConfig.providerName =
  594. providerName as ServiceProvider;
  595. session.mask.syncGlobalConfig = false;
  596. });
  597. if (providerName == "ByteDance") {
  598. const selectedModel = models.find(
  599. (m) =>
  600. m.name == model && m?.provider?.providerName == providerName,
  601. );
  602. showToast(selectedModel?.displayName ?? "");
  603. } else {
  604. showToast(model);
  605. }
  606. }}
  607. />
  608. )}
  609. {isDalle3(currentModel) && (
  610. <ChatAction
  611. onClick={() => setShowSizeSelector(true)}
  612. text={currentSize}
  613. icon={<SizeIcon />}
  614. />
  615. )}
  616. {showSizeSelector && (
  617. <Selector
  618. defaultSelectedValue={currentSize}
  619. items={dalle3Sizes.map((m) => ({
  620. title: m,
  621. value: m,
  622. }))}
  623. onClose={() => setShowSizeSelector(false)}
  624. onSelection={(s) => {
  625. if (s.length === 0) return;
  626. const size = s[0];
  627. chatStore.updateCurrentSession((session) => {
  628. session.mask.modelConfig.size = size;
  629. });
  630. showToast(size);
  631. }}
  632. />
  633. )}
  634. {isDalle3(currentModel) && (
  635. <ChatAction
  636. onClick={() => setShowQualitySelector(true)}
  637. text={currentQuality}
  638. icon={<QualityIcon />}
  639. />
  640. )}
  641. {showQualitySelector && (
  642. <Selector
  643. defaultSelectedValue={currentQuality}
  644. items={dalle3Qualitys.map((m) => ({
  645. title: m,
  646. value: m,
  647. }))}
  648. onClose={() => setShowQualitySelector(false)}
  649. onSelection={(q) => {
  650. if (q.length === 0) return;
  651. const quality = q[0];
  652. chatStore.updateCurrentSession((session) => {
  653. session.mask.modelConfig.quality = quality;
  654. });
  655. showToast(quality);
  656. }}
  657. />
  658. )}
  659. {isDalle3(currentModel) && (
  660. <ChatAction
  661. onClick={() => setShowStyleSelector(true)}
  662. text={currentStyle}
  663. icon={<StyleIcon />}
  664. />
  665. )}
  666. {showStyleSelector && (
  667. <Selector
  668. defaultSelectedValue={currentStyle}
  669. items={dalle3Styles.map((m) => ({
  670. title: m,
  671. value: m,
  672. }))}
  673. onClose={() => setShowStyleSelector(false)}
  674. onSelection={(s) => {
  675. if (s.length === 0) return;
  676. const style = s[0];
  677. chatStore.updateCurrentSession((session) => {
  678. session.mask.modelConfig.style = style;
  679. });
  680. showToast(style);
  681. }}
  682. />
  683. )}
  684. <ChatAction
  685. onClick={() => setShowPluginSelector(true)}
  686. text={Locale.Plugin.Name}
  687. icon={<PluginIcon />}
  688. />
  689. {showPluginSelector && (
  690. <Selector
  691. multiple
  692. defaultSelectedValue={chatStore.currentSession().mask?.plugin}
  693. items={[
  694. {
  695. title: Locale.Plugin.Artifacts,
  696. value: Plugin.Artifacts,
  697. },
  698. ]}
  699. onClose={() => setShowPluginSelector(false)}
  700. onSelection={(s) => {
  701. const plugin = s[0];
  702. chatStore.updateCurrentSession((session) => {
  703. session.mask.plugin = s;
  704. });
  705. if (plugin) {
  706. showToast(plugin);
  707. }
  708. }}
  709. />
  710. )}
  711. </div>
  712. );
  713. }
  714. export function EditMessageModal(props: { onClose: () => void }) {
  715. const chatStore = useChatStore();
  716. const session = chatStore.currentSession();
  717. const [messages, setMessages] = useState(session.messages.slice());
  718. return (
  719. <div className="modal-mask">
  720. <Modal
  721. title={Locale.Chat.EditMessage.Title}
  722. onClose={props.onClose}
  723. actions={[
  724. <IconButton
  725. text={Locale.UI.Cancel}
  726. icon={<CancelIcon />}
  727. key="cancel"
  728. onClick={() => {
  729. props.onClose();
  730. }}
  731. />,
  732. <IconButton
  733. type="primary"
  734. text={Locale.UI.Confirm}
  735. icon={<ConfirmIcon />}
  736. key="ok"
  737. onClick={() => {
  738. chatStore.updateCurrentSession(
  739. (session) => (session.messages = messages),
  740. );
  741. props.onClose();
  742. }}
  743. />,
  744. ]}
  745. >
  746. <List>
  747. <ListItem
  748. title={Locale.Chat.EditMessage.Topic.Title}
  749. subTitle={Locale.Chat.EditMessage.Topic.SubTitle}
  750. >
  751. <input
  752. type="text"
  753. value={session.topic}
  754. onInput={(e) =>
  755. chatStore.updateCurrentSession(
  756. (session) => (session.topic = e.currentTarget.value),
  757. )
  758. }
  759. ></input>
  760. </ListItem>
  761. </List>
  762. <ContextPrompts
  763. context={messages}
  764. updateContext={(updater) => {
  765. const newMessages = messages.slice();
  766. updater(newMessages);
  767. setMessages(newMessages);
  768. }}
  769. />
  770. </Modal>
  771. </div>
  772. );
  773. }
  774. export function DeleteImageButton(props: { deleteImage: () => void }) {
  775. return (
  776. <div className={styles["delete-image"]} onClick={props.deleteImage}>
  777. <DeleteIcon />
  778. </div>
  779. );
  780. }
  781. function _Chat() {
  782. type RenderMessage = ChatMessage & { preview?: boolean };
  783. const chatStore = useChatStore();
  784. const session = chatStore.currentSession();
  785. const config = useAppConfig();
  786. const fontSize = config.fontSize;
  787. const fontFamily = config.fontFamily;
  788. const [showExport, setShowExport] = useState(false);
  789. const inputRef = useRef<HTMLTextAreaElement>(null);
  790. const [userInput, setUserInput] = useState("");
  791. const [isLoading, setIsLoading] = useState(false);
  792. const { submitKey, shouldSubmit } = useSubmitHandler();
  793. const scrollRef = useRef<HTMLDivElement>(null);
  794. const isScrolledToBottom = scrollRef?.current
  795. ? Math.abs(
  796. scrollRef.current.scrollHeight -
  797. (scrollRef.current.scrollTop + scrollRef.current.clientHeight),
  798. ) <= 1
  799. : false;
  800. const { setAutoScroll, scrollDomToBottom } = useScrollToBottom(
  801. scrollRef,
  802. isScrolledToBottom,
  803. );
  804. const [hitBottom, setHitBottom] = useState(true);
  805. const isMobileScreen = useMobileScreen();
  806. const navigate = useNavigate();
  807. const [attachImages, setAttachImages] = useState<string[]>([]);
  808. const [uploading, setUploading] = useState(false);
  809. // prompt hints
  810. const promptStore = usePromptStore();
  811. const [promptHints, setPromptHints] = useState<RenderPrompt[]>([]);
  812. const onSearch = useDebouncedCallback(
  813. (text: string) => {
  814. const matchedPrompts = promptStore.search(text);
  815. setPromptHints(matchedPrompts);
  816. },
  817. 100,
  818. { leading: true, trailing: true },
  819. );
  820. // auto grow input
  821. const [inputRows, setInputRows] = useState(2);
  822. const measure = useDebouncedCallback(
  823. () => {
  824. const rows = inputRef.current ? autoGrowTextArea(inputRef.current) : 1;
  825. const inputRows = Math.min(
  826. 20,
  827. Math.max(2 + Number(!isMobileScreen), rows),
  828. );
  829. setInputRows(inputRows);
  830. },
  831. 100,
  832. {
  833. leading: true,
  834. trailing: true,
  835. },
  836. );
  837. // eslint-disable-next-line react-hooks/exhaustive-deps
  838. useEffect(measure, [userInput]);
  839. // chat commands shortcuts
  840. const chatCommands = useChatCommand({
  841. new: () => chatStore.newSession(),
  842. newm: () => navigate(Path.NewChat),
  843. prev: () => chatStore.nextSession(-1),
  844. next: () => chatStore.nextSession(1),
  845. clear: () =>
  846. chatStore.updateCurrentSession(
  847. (session) => (session.clearContextIndex = session.messages.length),
  848. ),
  849. del: () => chatStore.deleteSession(chatStore.currentSessionIndex),
  850. });
  851. // only search prompts when user input is short
  852. const SEARCH_TEXT_LIMIT = 30;
  853. const onInput = (text: string) => {
  854. setUserInput(text);
  855. const n = text.trim().length;
  856. // clear search results
  857. if (n === 0) {
  858. setPromptHints([]);
  859. } else if (text.match(ChatCommandPrefix)) {
  860. setPromptHints(chatCommands.search(text));
  861. } else if (!config.disablePromptHint && n < SEARCH_TEXT_LIMIT) {
  862. // check if need to trigger auto completion
  863. if (text.startsWith("/")) {
  864. let searchText = text.slice(1);
  865. onSearch(searchText);
  866. }
  867. }
  868. };
  869. const [isListening, setIsListening] = useState(false);
  870. const [isTranscription, setIsTranscription] = useState(false);
  871. const [speechApi, setSpeechApi] = useState<any>(null);
  872. const startListening = async () => {
  873. if (speechApi) {
  874. await speechApi.start();
  875. setIsListening(true);
  876. }
  877. };
  878. const stopListening = async () => {
  879. if (speechApi) {
  880. if (config.sttConfig.engine !== DEFAULT_STT_ENGINE)
  881. setIsTranscription(true);
  882. await speechApi.stop();
  883. setIsListening(false);
  884. }
  885. };
  886. const onRecognitionEnd = (finalTranscript: string) => {
  887. console.log(finalTranscript);
  888. if (finalTranscript) setUserInput(finalTranscript);
  889. if (config.sttConfig.engine !== DEFAULT_STT_ENGINE)
  890. setIsTranscription(false);
  891. };
  892. const doSubmit = (userInput: string) => {
  893. if (userInput.trim() === "") return;
  894. const matchCommand = chatCommands.match(userInput);
  895. if (matchCommand.matched) {
  896. setUserInput("");
  897. setPromptHints([]);
  898. matchCommand.invoke();
  899. return;
  900. }
  901. setIsLoading(true);
  902. chatStore
  903. .onUserInput(userInput, attachImages)
  904. .then(() => setIsLoading(false));
  905. setAttachImages([]);
  906. localStorage.setItem(LAST_INPUT_KEY, userInput);
  907. setUserInput("");
  908. setPromptHints([]);
  909. if (!isMobileScreen) inputRef.current?.focus();
  910. setAutoScroll(true);
  911. };
  912. const onPromptSelect = (prompt: RenderPrompt) => {
  913. setTimeout(() => {
  914. setPromptHints([]);
  915. const matchedChatCommand = chatCommands.match(prompt.content);
  916. if (matchedChatCommand.matched) {
  917. // if user is selecting a chat command, just trigger it
  918. matchedChatCommand.invoke();
  919. setUserInput("");
  920. } else {
  921. // or fill the prompt
  922. setUserInput(prompt.content);
  923. }
  924. inputRef.current?.focus();
  925. }, 30);
  926. };
  927. // stop response
  928. const onUserStop = (messageId: string) => {
  929. ChatControllerPool.stop(session.id, messageId);
  930. };
  931. useEffect(() => {
  932. chatStore.updateCurrentSession((session) => {
  933. const stopTiming = Date.now() - REQUEST_TIMEOUT_MS;
  934. session.messages.forEach((m) => {
  935. // check if should stop all stale messages
  936. if (m.isError || new Date(m.date).getTime() < stopTiming) {
  937. if (m.streaming) {
  938. m.streaming = false;
  939. }
  940. if (m.content.length === 0) {
  941. m.isError = true;
  942. m.content = prettyObject({
  943. error: true,
  944. message: "empty response",
  945. });
  946. }
  947. }
  948. });
  949. // auto sync mask config from global config
  950. if (session.mask.syncGlobalConfig) {
  951. console.log("[Mask] syncing from global, name = ", session.mask.name);
  952. session.mask.modelConfig = { ...config.modelConfig };
  953. }
  954. });
  955. // eslint-disable-next-line react-hooks/exhaustive-deps
  956. if (isFirefox()) config.sttConfig.engine = FIREFOX_DEFAULT_STT_ENGINE;
  957. setSpeechApi(
  958. config.sttConfig.engine === DEFAULT_STT_ENGINE
  959. ? new WebTranscriptionApi((transcription) =>
  960. onRecognitionEnd(transcription),
  961. )
  962. : new OpenAITranscriptionApi((transcription) =>
  963. onRecognitionEnd(transcription),
  964. ),
  965. );
  966. }, []);
  967. // check if should send message
  968. const onInputKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  969. // if ArrowUp and no userInput, fill with last input
  970. if (
  971. e.key === "ArrowUp" &&
  972. userInput.length <= 0 &&
  973. !(e.metaKey || e.altKey || e.ctrlKey)
  974. ) {
  975. setUserInput(localStorage.getItem(LAST_INPUT_KEY) ?? "");
  976. e.preventDefault();
  977. return;
  978. }
  979. if (shouldSubmit(e) && promptHints.length === 0) {
  980. doSubmit(userInput);
  981. e.preventDefault();
  982. }
  983. };
  984. const onRightClick = (e: any, message: ChatMessage) => {
  985. // copy to clipboard
  986. if (selectOrCopy(e.currentTarget, getMessageTextContent(message))) {
  987. if (userInput.length === 0) {
  988. setUserInput(getMessageTextContent(message));
  989. }
  990. e.preventDefault();
  991. }
  992. };
  993. const deleteMessage = (msgId?: string) => {
  994. chatStore.updateCurrentSession(
  995. (session) =>
  996. (session.messages = session.messages.filter((m) => m.id !== msgId)),
  997. );
  998. };
  999. const onDelete = (msgId: string) => {
  1000. deleteMessage(msgId);
  1001. };
  1002. const onResend = (message: ChatMessage) => {
  1003. // when it is resending a message
  1004. // 1. for a user's message, find the next bot response
  1005. // 2. for a bot's message, find the last user's input
  1006. // 3. delete original user input and bot's message
  1007. // 4. resend the user's input
  1008. const resendingIndex = session.messages.findIndex(
  1009. (m) => m.id === message.id,
  1010. );
  1011. if (resendingIndex < 0 || resendingIndex >= session.messages.length) {
  1012. console.error("[Chat] failed to find resending message", message);
  1013. return;
  1014. }
  1015. let userMessage: ChatMessage | undefined;
  1016. let botMessage: ChatMessage | undefined;
  1017. if (message.role === "assistant") {
  1018. // if it is resending a bot's message, find the user input for it
  1019. botMessage = message;
  1020. for (let i = resendingIndex; i >= 0; i -= 1) {
  1021. if (session.messages[i].role === "user") {
  1022. userMessage = session.messages[i];
  1023. break;
  1024. }
  1025. }
  1026. } else if (message.role === "user") {
  1027. // if it is resending a user's input, find the bot's response
  1028. userMessage = message;
  1029. for (let i = resendingIndex; i < session.messages.length; i += 1) {
  1030. if (session.messages[i].role === "assistant") {
  1031. botMessage = session.messages[i];
  1032. break;
  1033. }
  1034. }
  1035. }
  1036. if (userMessage === undefined) {
  1037. console.error("[Chat] failed to resend", message);
  1038. return;
  1039. }
  1040. // delete the original messages
  1041. deleteMessage(userMessage.id);
  1042. deleteMessage(botMessage?.id);
  1043. // resend the message
  1044. setIsLoading(true);
  1045. const textContent = getMessageTextContent(userMessage);
  1046. const images = getMessageImages(userMessage);
  1047. chatStore.onUserInput(textContent, images).then(() => setIsLoading(false));
  1048. inputRef.current?.focus();
  1049. };
  1050. const onPinMessage = (message: ChatMessage) => {
  1051. chatStore.updateCurrentSession((session) =>
  1052. session.mask.context.push(message),
  1053. );
  1054. showToast(Locale.Chat.Actions.PinToastContent, {
  1055. text: Locale.Chat.Actions.PinToastAction,
  1056. onClick: () => {
  1057. setShowPromptModal(true);
  1058. },
  1059. });
  1060. };
  1061. const accessStore = useAccessStore();
  1062. const [speechStatus, setSpeechStatus] = useState(false);
  1063. const [speechLoading, setSpeechLoading] = useState(false);
  1064. async function openaiSpeech(text: string) {
  1065. if (speechStatus) {
  1066. ttsPlayer.stop();
  1067. setSpeechStatus(false);
  1068. } else {
  1069. var api: ClientApi;
  1070. api = new ClientApi(ModelProvider.GPT);
  1071. const config = useAppConfig.getState();
  1072. setSpeechLoading(true);
  1073. ttsPlayer.init();
  1074. let audioBuffer: ArrayBuffer;
  1075. const { markdownToTxt } = require("markdown-to-txt");
  1076. const textContent = markdownToTxt(text);
  1077. if (config.ttsConfig.engine !== DEFAULT_TTS_ENGINE) {
  1078. const edgeVoiceName = accessStore.edgeVoiceName();
  1079. const tts = new MsEdgeTTS();
  1080. await tts.setMetadata(
  1081. edgeVoiceName,
  1082. OUTPUT_FORMAT.AUDIO_24KHZ_96KBITRATE_MONO_MP3,
  1083. );
  1084. audioBuffer = await tts.toArrayBuffer(textContent);
  1085. } else {
  1086. audioBuffer = await api.llm.speech({
  1087. model: config.ttsConfig.model,
  1088. input: textContent,
  1089. voice: config.ttsConfig.voice,
  1090. speed: config.ttsConfig.speed,
  1091. });
  1092. }
  1093. setSpeechStatus(true);
  1094. ttsPlayer
  1095. .play(audioBuffer, () => {
  1096. setSpeechStatus(false);
  1097. })
  1098. .catch((e) => {
  1099. console.error("[OpenAI Speech]", e);
  1100. showToast(prettyObject(e));
  1101. setSpeechStatus(false);
  1102. })
  1103. .finally(() => setSpeechLoading(false));
  1104. }
  1105. }
  1106. const context: RenderMessage[] = useMemo(() => {
  1107. return session.mask.hideContext ? [] : session.mask.context.slice();
  1108. }, [session.mask.context, session.mask.hideContext]);
  1109. if (
  1110. context.length === 0 &&
  1111. session.messages.at(0)?.content !== BOT_HELLO.content
  1112. ) {
  1113. const copiedHello = Object.assign({}, BOT_HELLO);
  1114. if (!accessStore.isAuthorized()) {
  1115. copiedHello.content = Locale.Error.Unauthorized;
  1116. }
  1117. context.push(copiedHello);
  1118. }
  1119. // preview messages
  1120. const renderMessages = useMemo(() => {
  1121. return context
  1122. .concat(session.messages as RenderMessage[])
  1123. .concat(
  1124. isLoading
  1125. ? [
  1126. {
  1127. ...createMessage({
  1128. role: "assistant",
  1129. content: "……",
  1130. }),
  1131. preview: true,
  1132. },
  1133. ]
  1134. : [],
  1135. )
  1136. .concat(
  1137. userInput.length > 0 && config.sendPreviewBubble
  1138. ? [
  1139. {
  1140. ...createMessage({
  1141. role: "user",
  1142. content: userInput,
  1143. }),
  1144. preview: true,
  1145. },
  1146. ]
  1147. : [],
  1148. );
  1149. }, [
  1150. config.sendPreviewBubble,
  1151. context,
  1152. isLoading,
  1153. session.messages,
  1154. userInput,
  1155. ]);
  1156. const [msgRenderIndex, _setMsgRenderIndex] = useState(
  1157. Math.max(0, renderMessages.length - CHAT_PAGE_SIZE),
  1158. );
  1159. function setMsgRenderIndex(newIndex: number) {
  1160. newIndex = Math.min(renderMessages.length - CHAT_PAGE_SIZE, newIndex);
  1161. newIndex = Math.max(0, newIndex);
  1162. _setMsgRenderIndex(newIndex);
  1163. }
  1164. const messages = useMemo(() => {
  1165. const endRenderIndex = Math.min(
  1166. msgRenderIndex + 3 * CHAT_PAGE_SIZE,
  1167. renderMessages.length,
  1168. );
  1169. return renderMessages.slice(msgRenderIndex, endRenderIndex);
  1170. }, [msgRenderIndex, renderMessages]);
  1171. const onChatBodyScroll = (e: HTMLElement) => {
  1172. const bottomHeight = e.scrollTop + e.clientHeight;
  1173. const edgeThreshold = e.clientHeight;
  1174. const isTouchTopEdge = e.scrollTop <= edgeThreshold;
  1175. const isTouchBottomEdge = bottomHeight >= e.scrollHeight - edgeThreshold;
  1176. const isHitBottom =
  1177. bottomHeight >= e.scrollHeight - (isMobileScreen ? 4 : 10);
  1178. const prevPageMsgIndex = msgRenderIndex - CHAT_PAGE_SIZE;
  1179. const nextPageMsgIndex = msgRenderIndex + CHAT_PAGE_SIZE;
  1180. if (isTouchTopEdge && !isTouchBottomEdge) {
  1181. setMsgRenderIndex(prevPageMsgIndex);
  1182. } else if (isTouchBottomEdge) {
  1183. setMsgRenderIndex(nextPageMsgIndex);
  1184. }
  1185. setHitBottom(isHitBottom);
  1186. setAutoScroll(isHitBottom);
  1187. };
  1188. function scrollToBottom() {
  1189. setMsgRenderIndex(renderMessages.length - CHAT_PAGE_SIZE);
  1190. scrollDomToBottom();
  1191. }
  1192. // clear context index = context length + index in messages
  1193. const clearContextIndex =
  1194. (session.clearContextIndex ?? -1) >= 0
  1195. ? session.clearContextIndex! + context.length - msgRenderIndex
  1196. : -1;
  1197. const [showPromptModal, setShowPromptModal] = useState(false);
  1198. const clientConfig = useMemo(() => getClientConfig(), []);
  1199. const autoFocus = !isMobileScreen; // wont auto focus on mobile screen
  1200. const showMaxIcon = !isMobileScreen && !clientConfig?.isApp;
  1201. useCommand({
  1202. fill: setUserInput,
  1203. submit: (text) => {
  1204. doSubmit(text);
  1205. },
  1206. code: (text) => {
  1207. if (accessStore.disableFastLink) return;
  1208. console.log("[Command] got code from url: ", text);
  1209. showConfirm(Locale.URLCommand.Code + `code = ${text}`).then((res) => {
  1210. if (res) {
  1211. accessStore.update((access) => (access.accessCode = text));
  1212. }
  1213. });
  1214. },
  1215. settings: (text) => {
  1216. if (accessStore.disableFastLink) return;
  1217. try {
  1218. const payload = JSON.parse(text) as {
  1219. key?: string;
  1220. url?: string;
  1221. };
  1222. console.log("[Command] got settings from url: ", payload);
  1223. if (payload.key || payload.url) {
  1224. showConfirm(
  1225. Locale.URLCommand.Settings +
  1226. `\n${JSON.stringify(payload, null, 4)}`,
  1227. ).then((res) => {
  1228. if (!res) return;
  1229. if (payload.key) {
  1230. accessStore.update(
  1231. (access) => (access.openaiApiKey = payload.key!),
  1232. );
  1233. }
  1234. if (payload.url) {
  1235. accessStore.update((access) => (access.openaiUrl = payload.url!));
  1236. }
  1237. accessStore.update((access) => (access.useCustomConfig = true));
  1238. });
  1239. }
  1240. } catch {
  1241. console.error("[Command] failed to get settings from url: ", text);
  1242. }
  1243. },
  1244. });
  1245. // edit / insert message modal
  1246. const [isEditingMessage, setIsEditingMessage] = useState(false);
  1247. // remember unfinished input
  1248. useEffect(() => {
  1249. // try to load from local storage
  1250. const key = UNFINISHED_INPUT(session.id);
  1251. const mayBeUnfinishedInput = localStorage.getItem(key);
  1252. if (mayBeUnfinishedInput && userInput.length === 0) {
  1253. setUserInput(mayBeUnfinishedInput);
  1254. localStorage.removeItem(key);
  1255. }
  1256. const dom = inputRef.current;
  1257. return () => {
  1258. localStorage.setItem(key, dom?.value ?? "");
  1259. };
  1260. // eslint-disable-next-line react-hooks/exhaustive-deps
  1261. }, []);
  1262. const handlePaste = useCallback(
  1263. async (event: React.ClipboardEvent<HTMLTextAreaElement>) => {
  1264. const currentModel = chatStore.currentSession().mask.modelConfig.model;
  1265. if (!isVisionModel(currentModel)) {
  1266. return;
  1267. }
  1268. const items = (event.clipboardData || window.clipboardData).items;
  1269. for (const item of items) {
  1270. if (item.kind === "file" && item.type.startsWith("image/")) {
  1271. event.preventDefault();
  1272. const file = item.getAsFile();
  1273. if (file) {
  1274. const images: string[] = [];
  1275. images.push(...attachImages);
  1276. images.push(
  1277. ...(await new Promise<string[]>((res, rej) => {
  1278. setUploading(true);
  1279. const imagesData: string[] = [];
  1280. uploadImageRemote(file)
  1281. .then((dataUrl) => {
  1282. imagesData.push(dataUrl);
  1283. setUploading(false);
  1284. res(imagesData);
  1285. })
  1286. .catch((e) => {
  1287. setUploading(false);
  1288. rej(e);
  1289. });
  1290. })),
  1291. );
  1292. const imagesLength = images.length;
  1293. if (imagesLength > 3) {
  1294. images.splice(3, imagesLength - 3);
  1295. }
  1296. setAttachImages(images);
  1297. }
  1298. }
  1299. }
  1300. },
  1301. [attachImages, chatStore],
  1302. );
  1303. async function uploadImage() {
  1304. const images: string[] = [];
  1305. images.push(...attachImages);
  1306. images.push(
  1307. ...(await new Promise<string[]>((res, rej) => {
  1308. const fileInput = document.createElement("input");
  1309. fileInput.type = "file";
  1310. fileInput.accept =
  1311. "image/png, image/jpeg, image/webp, image/heic, image/heif";
  1312. fileInput.multiple = true;
  1313. fileInput.onchange = (event: any) => {
  1314. setUploading(true);
  1315. const files = event.target.files;
  1316. const imagesData: string[] = [];
  1317. for (let i = 0; i < files.length; i++) {
  1318. const file = event.target.files[i];
  1319. uploadImageRemote(file)
  1320. .then((dataUrl) => {
  1321. imagesData.push(dataUrl);
  1322. if (
  1323. imagesData.length === 3 ||
  1324. imagesData.length === files.length
  1325. ) {
  1326. setUploading(false);
  1327. res(imagesData);
  1328. }
  1329. })
  1330. .catch((e) => {
  1331. setUploading(false);
  1332. rej(e);
  1333. });
  1334. }
  1335. };
  1336. fileInput.click();
  1337. })),
  1338. );
  1339. const imagesLength = images.length;
  1340. if (imagesLength > 3) {
  1341. images.splice(3, imagesLength - 3);
  1342. }
  1343. setAttachImages(images);
  1344. }
  1345. return (
  1346. <div className={styles.chat} key={session.id}>
  1347. <div className="window-header" data-tauri-drag-region>
  1348. {isMobileScreen && (
  1349. <div className="window-actions">
  1350. <div className={"window-action-button"}>
  1351. <IconButton
  1352. icon={<ReturnIcon />}
  1353. bordered
  1354. title={Locale.Chat.Actions.ChatList}
  1355. onClick={() => navigate(Path.Home)}
  1356. />
  1357. </div>
  1358. </div>
  1359. )}
  1360. <div className={`window-header-title ${styles["chat-body-title"]}`}>
  1361. <div
  1362. className={`window-header-main-title ${styles["chat-body-main-title"]}`}
  1363. onClickCapture={() => setIsEditingMessage(true)}
  1364. >
  1365. {!session.topic ? DEFAULT_TOPIC : session.topic}
  1366. </div>
  1367. <div className="window-header-sub-title">
  1368. {Locale.Chat.SubTitle(session.messages.length)}
  1369. </div>
  1370. </div>
  1371. <div className="window-actions">
  1372. {!isMobileScreen && (
  1373. <div className="window-action-button">
  1374. <IconButton
  1375. icon={<RenameIcon />}
  1376. bordered
  1377. title={Locale.Chat.EditMessage.Title}
  1378. aria={Locale.Chat.EditMessage.Title}
  1379. onClick={() => setIsEditingMessage(true)}
  1380. />
  1381. </div>
  1382. )}
  1383. <div className="window-action-button">
  1384. <IconButton
  1385. icon={<ExportIcon />}
  1386. bordered
  1387. title={Locale.Chat.Actions.Export}
  1388. onClick={() => {
  1389. setShowExport(true);
  1390. }}
  1391. />
  1392. </div>
  1393. {showMaxIcon && (
  1394. <div className="window-action-button">
  1395. <IconButton
  1396. icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
  1397. bordered
  1398. title={Locale.Chat.Actions.FullScreen}
  1399. aria={Locale.Chat.Actions.FullScreen}
  1400. onClick={() => {
  1401. config.update(
  1402. (config) => (config.tightBorder = !config.tightBorder),
  1403. );
  1404. }}
  1405. />
  1406. </div>
  1407. )}
  1408. </div>
  1409. <PromptToast
  1410. showToast={!hitBottom}
  1411. showModal={showPromptModal}
  1412. setShowModal={setShowPromptModal}
  1413. />
  1414. </div>
  1415. <div
  1416. className={styles["chat-body"]}
  1417. ref={scrollRef}
  1418. onScroll={(e) => onChatBodyScroll(e.currentTarget)}
  1419. onMouseDown={() => inputRef.current?.blur()}
  1420. onTouchStart={() => {
  1421. inputRef.current?.blur();
  1422. setAutoScroll(false);
  1423. }}
  1424. >
  1425. {messages.map((message, i) => {
  1426. const isUser = message.role === "user";
  1427. const isContext = i < context.length;
  1428. const showActions =
  1429. i > 0 &&
  1430. !(message.preview || message.content.length === 0) &&
  1431. !isContext;
  1432. const showTyping = message.preview || message.streaming;
  1433. const shouldShowClearContextDivider = i === clearContextIndex - 1;
  1434. return (
  1435. <Fragment key={message.id}>
  1436. <div
  1437. className={
  1438. isUser ? styles["chat-message-user"] : styles["chat-message"]
  1439. }
  1440. >
  1441. <div className={styles["chat-message-container"]}>
  1442. <div className={styles["chat-message-header"]}>
  1443. <div className={styles["chat-message-avatar"]}>
  1444. <div className={styles["chat-message-edit"]}>
  1445. <IconButton
  1446. icon={<EditIcon />}
  1447. aria={Locale.Chat.Actions.Edit}
  1448. onClick={async () => {
  1449. const newMessage = await showPrompt(
  1450. Locale.Chat.Actions.Edit,
  1451. getMessageTextContent(message),
  1452. 10,
  1453. );
  1454. let newContent: string | MultimodalContent[] =
  1455. newMessage;
  1456. const images = getMessageImages(message);
  1457. if (images.length > 0) {
  1458. newContent = [{ type: "text", text: newMessage }];
  1459. for (let i = 0; i < images.length; i++) {
  1460. newContent.push({
  1461. type: "image_url",
  1462. image_url: {
  1463. url: images[i],
  1464. },
  1465. });
  1466. }
  1467. }
  1468. chatStore.updateCurrentSession((session) => {
  1469. const m = session.mask.context
  1470. .concat(session.messages)
  1471. .find((m) => m.id === message.id);
  1472. if (m) {
  1473. m.content = newContent;
  1474. }
  1475. });
  1476. }}
  1477. ></IconButton>
  1478. </div>
  1479. {isUser ? (
  1480. <Avatar avatar={config.avatar} />
  1481. ) : (
  1482. <>
  1483. {["system"].includes(message.role) ? (
  1484. <Avatar avatar="2699-fe0f" />
  1485. ) : (
  1486. <MaskAvatar
  1487. avatar={session.mask.avatar}
  1488. model={
  1489. message.model || session.mask.modelConfig.model
  1490. }
  1491. />
  1492. )}
  1493. </>
  1494. )}
  1495. </div>
  1496. {!isUser && (
  1497. <div className={styles["chat-model-name"]}>
  1498. {message.model}
  1499. </div>
  1500. )}
  1501. {showActions && (
  1502. <div className={styles["chat-message-actions"]}>
  1503. <div className={styles["chat-input-actions"]}>
  1504. {message.streaming ? (
  1505. <ChatAction
  1506. text={Locale.Chat.Actions.Stop}
  1507. icon={<StopIcon />}
  1508. onClick={() => onUserStop(message.id ?? i)}
  1509. />
  1510. ) : (
  1511. <>
  1512. <ChatAction
  1513. text={Locale.Chat.Actions.Retry}
  1514. icon={<ResetIcon />}
  1515. onClick={() => onResend(message)}
  1516. />
  1517. <ChatAction
  1518. text={Locale.Chat.Actions.Delete}
  1519. icon={<DeleteIcon />}
  1520. onClick={() => onDelete(message.id ?? i)}
  1521. />
  1522. <ChatAction
  1523. text={Locale.Chat.Actions.Pin}
  1524. icon={<PinIcon />}
  1525. onClick={() => onPinMessage(message)}
  1526. />
  1527. <ChatAction
  1528. text={Locale.Chat.Actions.Copy}
  1529. icon={<CopyIcon />}
  1530. onClick={() =>
  1531. copyToClipboard(
  1532. getMessageTextContent(message),
  1533. )
  1534. }
  1535. />
  1536. {config.ttsConfig.enable && (
  1537. <ChatAction
  1538. text={
  1539. speechStatus
  1540. ? Locale.Chat.Actions.StopSpeech
  1541. : Locale.Chat.Actions.Speech
  1542. }
  1543. loding={speechLoading}
  1544. icon={
  1545. speechStatus ? (
  1546. <SpeakStopIcon />
  1547. ) : (
  1548. <SpeakIcon />
  1549. )
  1550. }
  1551. onClick={() =>
  1552. openaiSpeech(getMessageTextContent(message))
  1553. }
  1554. />
  1555. )}
  1556. </>
  1557. )}
  1558. </div>
  1559. </div>
  1560. )}
  1561. </div>
  1562. {showTyping && (
  1563. <div className={styles["chat-message-status"]}>
  1564. {Locale.Chat.Typing}
  1565. </div>
  1566. )}
  1567. <div className={styles["chat-message-item"]}>
  1568. <Markdown
  1569. key={message.streaming ? "loading" : "done"}
  1570. content={getMessageTextContent(message)}
  1571. loading={
  1572. (message.preview || message.streaming) &&
  1573. message.content.length === 0 &&
  1574. !isUser
  1575. }
  1576. onContextMenu={(e) => onRightClick(e, message)}
  1577. onDoubleClickCapture={() => {
  1578. if (!isMobileScreen) return;
  1579. setUserInput(getMessageTextContent(message));
  1580. }}
  1581. fontSize={fontSize}
  1582. fontFamily={fontFamily}
  1583. parentRef={scrollRef}
  1584. defaultShow={i >= messages.length - 6}
  1585. />
  1586. {getMessageImages(message).length == 1 && (
  1587. <img
  1588. className={styles["chat-message-item-image"]}
  1589. src={getMessageImages(message)[0]}
  1590. alt=""
  1591. />
  1592. )}
  1593. {getMessageImages(message).length > 1 && (
  1594. <div
  1595. className={styles["chat-message-item-images"]}
  1596. style={
  1597. {
  1598. "--image-count": getMessageImages(message).length,
  1599. } as React.CSSProperties
  1600. }
  1601. >
  1602. {getMessageImages(message).map((image, index) => {
  1603. return (
  1604. <img
  1605. className={
  1606. styles["chat-message-item-image-multi"]
  1607. }
  1608. key={index}
  1609. src={image}
  1610. alt=""
  1611. />
  1612. );
  1613. })}
  1614. </div>
  1615. )}
  1616. </div>
  1617. <div className={styles["chat-message-action-date"]}>
  1618. {isContext
  1619. ? Locale.Chat.IsContext
  1620. : message.date.toLocaleString()}
  1621. </div>
  1622. </div>
  1623. </div>
  1624. {shouldShowClearContextDivider && <ClearContextDivider />}
  1625. </Fragment>
  1626. );
  1627. })}
  1628. </div>
  1629. <div className={styles["chat-input-panel"]}>
  1630. <PromptHints prompts={promptHints} onPromptSelect={onPromptSelect} />
  1631. <ChatActions
  1632. uploadImage={uploadImage}
  1633. setAttachImages={setAttachImages}
  1634. setUploading={setUploading}
  1635. showPromptModal={() => setShowPromptModal(true)}
  1636. scrollToBottom={scrollToBottom}
  1637. hitBottom={hitBottom}
  1638. uploading={uploading}
  1639. showPromptHints={() => {
  1640. // Click again to close
  1641. if (promptHints.length > 0) {
  1642. setPromptHints([]);
  1643. return;
  1644. }
  1645. inputRef.current?.focus();
  1646. setUserInput("/");
  1647. onSearch("");
  1648. }}
  1649. />
  1650. <label
  1651. className={`${styles["chat-input-panel-inner"]} ${
  1652. attachImages.length != 0
  1653. ? styles["chat-input-panel-inner-attach"]
  1654. : ""
  1655. }`}
  1656. htmlFor="chat-input"
  1657. >
  1658. <textarea
  1659. id="chat-input"
  1660. ref={inputRef}
  1661. className={styles["chat-input"]}
  1662. placeholder={Locale.Chat.Input(submitKey)}
  1663. onInput={(e) => onInput(e.currentTarget.value)}
  1664. value={userInput}
  1665. onKeyDown={onInputKeyDown}
  1666. onFocus={scrollToBottom}
  1667. onClick={scrollToBottom}
  1668. onPaste={handlePaste}
  1669. rows={inputRows}
  1670. autoFocus={autoFocus}
  1671. style={{
  1672. fontSize: config.fontSize,
  1673. fontFamily: config.fontFamily,
  1674. }}
  1675. />
  1676. {attachImages.length != 0 && (
  1677. <div className={styles["attach-images"]}>
  1678. {attachImages.map((image, index) => {
  1679. return (
  1680. <div
  1681. key={index}
  1682. className={styles["attach-image"]}
  1683. style={{ backgroundImage: `url("${image}")` }}
  1684. >
  1685. <div className={styles["attach-image-mask"]}>
  1686. <DeleteImageButton
  1687. deleteImage={() => {
  1688. setAttachImages(
  1689. attachImages.filter((_, i) => i !== index),
  1690. );
  1691. }}
  1692. />
  1693. </div>
  1694. </div>
  1695. );
  1696. })}
  1697. </div>
  1698. )}
  1699. {config.sttConfig.enable ? (
  1700. <IconButton
  1701. icon={<VoiceWhiteIcon />}
  1702. text={
  1703. isListening ? Locale.Chat.StopSpeak : Locale.Chat.StartSpeak
  1704. }
  1705. className={styles["chat-input-send"]}
  1706. type="primary"
  1707. onClick={async () =>
  1708. isListening ? await stopListening() : await startListening()
  1709. }
  1710. loding={isTranscription}
  1711. />
  1712. ) : (
  1713. <IconButton
  1714. icon={<SendWhiteIcon />}
  1715. text={Locale.Chat.Send}
  1716. className={styles["chat-input-send"]}
  1717. type="primary"
  1718. onClick={() => doSubmit(userInput)}
  1719. />
  1720. )}
  1721. {/* <IconButton
  1722. icon={<SendWhiteIcon />}
  1723. text={Locale.Chat.Send}
  1724. className={styles["chat-input-send"]}
  1725. type="primary"
  1726. onClick={() => doSubmit(userInput)}
  1727. /> */}
  1728. </label>
  1729. </div>
  1730. {showExport && (
  1731. <ExportMessageModal onClose={() => setShowExport(false)} />
  1732. )}
  1733. {isEditingMessage && (
  1734. <EditMessageModal
  1735. onClose={() => {
  1736. setIsEditingMessage(false);
  1737. }}
  1738. />
  1739. )}
  1740. </div>
  1741. );
  1742. }
  1743. export function Chat() {
  1744. const chatStore = useChatStore();
  1745. const sessionIndex = chatStore.currentSessionIndex;
  1746. return <_Chat key={sessionIndex}></_Chat>;
  1747. }