chat.tsx 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. import { useDebouncedCallback } from "use-debounce";
  2. import React, {
  3. useState,
  4. useRef,
  5. useEffect,
  6. useMemo,
  7. useCallback,
  8. Fragment,
  9. } from "react";
  10. import SendWhiteIcon from "../icons/send-white.svg";
  11. import BrainIcon from "../icons/brain.svg";
  12. import RenameIcon from "../icons/rename.svg";
  13. import ExportIcon from "../icons/share.svg";
  14. import ReturnIcon from "../icons/return.svg";
  15. import CopyIcon from "../icons/copy.svg";
  16. import LoadingIcon from "../icons/three-dots.svg";
  17. import PromptIcon from "../icons/prompt.svg";
  18. import MaskIcon from "../icons/mask.svg";
  19. import MaxIcon from "../icons/max.svg";
  20. import MinIcon from "../icons/min.svg";
  21. import ResetIcon from "../icons/reload.svg";
  22. import BreakIcon from "../icons/break.svg";
  23. import SettingsIcon from "../icons/chat-settings.svg";
  24. import DeleteIcon from "../icons/clear.svg";
  25. import PinIcon from "../icons/pin.svg";
  26. import EditIcon from "../icons/rename.svg";
  27. import ConfirmIcon from "../icons/confirm.svg";
  28. import CancelIcon from "../icons/cancel.svg";
  29. import LightIcon from "../icons/light.svg";
  30. import DarkIcon from "../icons/dark.svg";
  31. import AutoIcon from "../icons/auto.svg";
  32. import BottomIcon from "../icons/bottom.svg";
  33. import StopIcon from "../icons/pause.svg";
  34. import RobotIcon from "../icons/robot.svg";
  35. import {
  36. ChatMessage,
  37. SubmitKey,
  38. useChatStore,
  39. BOT_HELLO,
  40. createMessage,
  41. useAccessStore,
  42. Theme,
  43. useAppConfig,
  44. DEFAULT_TOPIC,
  45. ModelType,
  46. } from "../store";
  47. import {
  48. copyToClipboard,
  49. selectOrCopy,
  50. autoGrowTextArea,
  51. useMobileScreen,
  52. } from "../utils";
  53. import dynamic from "next/dynamic";
  54. import { ChatControllerPool } from "../client/controller";
  55. import { Prompt, usePromptStore } from "../store/prompt";
  56. import Locale from "../locales";
  57. import { IconButton } from "./button";
  58. import styles from "./chat.module.scss";
  59. import {
  60. List,
  61. ListItem,
  62. Modal,
  63. Selector,
  64. showConfirm,
  65. showPrompt,
  66. showToast,
  67. } from "./ui-lib";
  68. import { useNavigate } from "react-router-dom";
  69. import {
  70. CHAT_PAGE_SIZE,
  71. LAST_INPUT_KEY,
  72. Path,
  73. REQUEST_TIMEOUT_MS,
  74. UNFINISHED_INPUT,
  75. } from "../constant";
  76. import { Avatar } from "./emoji";
  77. import { ContextPrompts, MaskAvatar, MaskConfig } from "./mask";
  78. import { useMaskStore } from "../store/mask";
  79. import { ChatCommandPrefix, useChatCommand, useCommand } from "../command";
  80. import { prettyObject } from "../utils/format";
  81. import { ExportMessageModal } from "./exporter";
  82. import { getClientConfig } from "../config/client";
  83. import { useAllModels } from "../utils/hooks";
  84. const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
  85. loading: () => <LoadingIcon />,
  86. });
  87. export function SessionConfigModel(props: { onClose: () => void }) {
  88. const chatStore = useChatStore();
  89. const session = chatStore.currentSession();
  90. const maskStore = useMaskStore();
  91. const navigate = useNavigate();
  92. return (
  93. <div className="modal-mask">
  94. <Modal
  95. title={Locale.Context.Edit}
  96. onClose={() => props.onClose()}
  97. actions={[
  98. <IconButton
  99. key="reset"
  100. icon={<ResetIcon />}
  101. bordered
  102. text={Locale.Chat.Config.Reset}
  103. onClick={async () => {
  104. if (await showConfirm(Locale.Memory.ResetConfirm)) {
  105. chatStore.updateCurrentSession(
  106. (session) => (session.memoryPrompt = ""),
  107. );
  108. }
  109. }}
  110. />,
  111. <IconButton
  112. key="copy"
  113. icon={<CopyIcon />}
  114. bordered
  115. text={Locale.Chat.Config.SaveAs}
  116. onClick={() => {
  117. navigate(Path.Masks);
  118. setTimeout(() => {
  119. maskStore.create(session.mask);
  120. }, 500);
  121. }}
  122. />,
  123. ]}
  124. >
  125. <MaskConfig
  126. mask={session.mask}
  127. updateMask={(updater) => {
  128. const mask = { ...session.mask };
  129. updater(mask);
  130. chatStore.updateCurrentSession((session) => (session.mask = mask));
  131. }}
  132. shouldSyncFromGlobal
  133. extraListItems={
  134. session.mask.modelConfig.sendMemory ? (
  135. <ListItem
  136. className="copyable"
  137. title={`${Locale.Memory.Title} (${session.lastSummarizeIndex} of ${session.messages.length})`}
  138. subTitle={session.memoryPrompt || Locale.Memory.EmptyContent}
  139. ></ListItem>
  140. ) : (
  141. <></>
  142. )
  143. }
  144. ></MaskConfig>
  145. </Modal>
  146. </div>
  147. );
  148. }
  149. function PromptToast(props: {
  150. showToast?: boolean;
  151. showModal?: boolean;
  152. setShowModal: (_: boolean) => void;
  153. }) {
  154. const chatStore = useChatStore();
  155. const session = chatStore.currentSession();
  156. const context = session.mask.context;
  157. return (
  158. <div className={styles["prompt-toast"]} key="prompt-toast">
  159. {props.showToast && (
  160. <div
  161. className={styles["prompt-toast-inner"] + " clickable"}
  162. role="button"
  163. onClick={() => props.setShowModal(true)}
  164. >
  165. <BrainIcon />
  166. <span className={styles["prompt-toast-content"]}>
  167. {Locale.Context.Toast(context.length)}
  168. </span>
  169. </div>
  170. )}
  171. {props.showModal && (
  172. <SessionConfigModel onClose={() => props.setShowModal(false)} />
  173. )}
  174. </div>
  175. );
  176. }
  177. function useSubmitHandler() {
  178. const config = useAppConfig();
  179. const submitKey = config.submitKey;
  180. const isComposing = useRef(false);
  181. useEffect(() => {
  182. const onCompositionStart = () => {
  183. isComposing.current = true;
  184. };
  185. const onCompositionEnd = () => {
  186. isComposing.current = false;
  187. };
  188. window.addEventListener("compositionstart", onCompositionStart);
  189. window.addEventListener("compositionend", onCompositionEnd);
  190. return () => {
  191. window.removeEventListener("compositionstart", onCompositionStart);
  192. window.removeEventListener("compositionend", onCompositionEnd);
  193. };
  194. }, []);
  195. const shouldSubmit = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  196. if (e.key !== "Enter") return false;
  197. if (e.key === "Enter" && (e.nativeEvent.isComposing || isComposing.current))
  198. return false;
  199. return (
  200. (config.submitKey === SubmitKey.AltEnter && e.altKey) ||
  201. (config.submitKey === SubmitKey.CtrlEnter && e.ctrlKey) ||
  202. (config.submitKey === SubmitKey.ShiftEnter && e.shiftKey) ||
  203. (config.submitKey === SubmitKey.MetaEnter && e.metaKey) ||
  204. (config.submitKey === SubmitKey.Enter &&
  205. !e.altKey &&
  206. !e.ctrlKey &&
  207. !e.shiftKey &&
  208. !e.metaKey)
  209. );
  210. };
  211. return {
  212. submitKey,
  213. shouldSubmit,
  214. };
  215. }
  216. export type RenderPompt = Pick<Prompt, "title" | "content">;
  217. export function PromptHints(props: {
  218. prompts: RenderPompt[];
  219. onPromptSelect: (prompt: RenderPompt) => void;
  220. }) {
  221. const noPrompts = props.prompts.length === 0;
  222. const [selectIndex, setSelectIndex] = useState(0);
  223. const selectedRef = useRef<HTMLDivElement>(null);
  224. useEffect(() => {
  225. setSelectIndex(0);
  226. }, [props.prompts.length]);
  227. useEffect(() => {
  228. const onKeyDown = (e: KeyboardEvent) => {
  229. if (noPrompts || e.metaKey || e.altKey || e.ctrlKey) {
  230. return;
  231. }
  232. // arrow up / down to select prompt
  233. const changeIndex = (delta: number) => {
  234. e.stopPropagation();
  235. e.preventDefault();
  236. const nextIndex = Math.max(
  237. 0,
  238. Math.min(props.prompts.length - 1, selectIndex + delta),
  239. );
  240. setSelectIndex(nextIndex);
  241. selectedRef.current?.scrollIntoView({
  242. block: "center",
  243. });
  244. };
  245. if (e.key === "ArrowUp") {
  246. changeIndex(1);
  247. } else if (e.key === "ArrowDown") {
  248. changeIndex(-1);
  249. } else if (e.key === "Enter") {
  250. const selectedPrompt = props.prompts.at(selectIndex);
  251. if (selectedPrompt) {
  252. props.onPromptSelect(selectedPrompt);
  253. }
  254. }
  255. };
  256. window.addEventListener("keydown", onKeyDown);
  257. return () => window.removeEventListener("keydown", onKeyDown);
  258. // eslint-disable-next-line react-hooks/exhaustive-deps
  259. }, [props.prompts.length, selectIndex]);
  260. if (noPrompts) return null;
  261. return (
  262. <div className={styles["prompt-hints"]}>
  263. {props.prompts.map((prompt, i) => (
  264. <div
  265. ref={i === selectIndex ? selectedRef : null}
  266. className={
  267. styles["prompt-hint"] +
  268. ` ${i === selectIndex ? styles["prompt-hint-selected"] : ""}`
  269. }
  270. key={prompt.title + i.toString()}
  271. onClick={() => props.onPromptSelect(prompt)}
  272. onMouseEnter={() => setSelectIndex(i)}
  273. >
  274. <div className={styles["hint-title"]}>{prompt.title}</div>
  275. <div className={styles["hint-content"]}>{prompt.content}</div>
  276. </div>
  277. ))}
  278. </div>
  279. );
  280. }
  281. function ClearContextDivider() {
  282. const chatStore = useChatStore();
  283. return (
  284. <div
  285. className={styles["clear-context"]}
  286. onClick={() =>
  287. chatStore.updateCurrentSession(
  288. (session) => (session.clearContextIndex = undefined),
  289. )
  290. }
  291. >
  292. <div className={styles["clear-context-tips"]}>{Locale.Context.Clear}</div>
  293. <div className={styles["clear-context-revert-btn"]}>
  294. {Locale.Context.Revert}
  295. </div>
  296. </div>
  297. );
  298. }
  299. function ChatAction(props: {
  300. text: string;
  301. icon: JSX.Element;
  302. onClick: () => void;
  303. }) {
  304. const iconRef = useRef<HTMLDivElement>(null);
  305. const textRef = useRef<HTMLDivElement>(null);
  306. const [width, setWidth] = useState({
  307. full: 16,
  308. icon: 16,
  309. });
  310. function updateWidth() {
  311. if (!iconRef.current || !textRef.current) return;
  312. const getWidth = (dom: HTMLDivElement) => dom.getBoundingClientRect().width;
  313. const textWidth = getWidth(textRef.current);
  314. const iconWidth = getWidth(iconRef.current);
  315. setWidth({
  316. full: textWidth + iconWidth,
  317. icon: iconWidth,
  318. });
  319. }
  320. return (
  321. <div
  322. className={`${styles["chat-input-action"]} clickable`}
  323. onClick={() => {
  324. props.onClick();
  325. setTimeout(updateWidth, 1);
  326. }}
  327. onMouseEnter={updateWidth}
  328. onTouchStart={updateWidth}
  329. style={
  330. {
  331. "--icon-width": `${width.icon}px`,
  332. "--full-width": `${width.full}px`,
  333. } as React.CSSProperties
  334. }
  335. >
  336. <div ref={iconRef} className={styles["icon"]}>
  337. {props.icon}
  338. </div>
  339. <div className={styles["text"]} ref={textRef}>
  340. {props.text}
  341. </div>
  342. </div>
  343. );
  344. }
  345. function useScrollToBottom() {
  346. // for auto-scroll
  347. const scrollRef = useRef<HTMLDivElement>(null);
  348. const [autoScroll, setAutoScroll] = useState(true);
  349. function scrollDomToBottom() {
  350. const dom = scrollRef.current;
  351. if (dom) {
  352. requestAnimationFrame(() => {
  353. setAutoScroll(true);
  354. dom.scrollTo(0, dom.scrollHeight);
  355. });
  356. }
  357. }
  358. // auto scroll
  359. useEffect(() => {
  360. if (autoScroll) {
  361. scrollDomToBottom();
  362. }
  363. });
  364. return {
  365. scrollRef,
  366. autoScroll,
  367. setAutoScroll,
  368. scrollDomToBottom,
  369. };
  370. }
  371. export function ChatActions(props: {
  372. showPromptModal: () => void;
  373. scrollToBottom: () => void;
  374. showPromptHints: () => void;
  375. hitBottom: boolean;
  376. }) {
  377. const config = useAppConfig();
  378. const navigate = useNavigate();
  379. const chatStore = useChatStore();
  380. // switch themes
  381. const theme = config.theme;
  382. function nextTheme() {
  383. const themes = [Theme.Auto, Theme.Light, Theme.Dark];
  384. const themeIndex = themes.indexOf(theme);
  385. const nextIndex = (themeIndex + 1) % themes.length;
  386. const nextTheme = themes[nextIndex];
  387. config.update((config) => (config.theme = nextTheme));
  388. }
  389. // stop all responses
  390. const couldStop = ChatControllerPool.hasPending();
  391. const stopAll = () => ChatControllerPool.stopAll();
  392. // switch model
  393. const currentModel = chatStore.currentSession().mask.modelConfig.model;
  394. const allModels = useAllModels();
  395. const models = useMemo(
  396. () => allModels.filter((m) => m.available),
  397. [allModels],
  398. );
  399. const [showModelSelector, setShowModelSelector] = useState(false);
  400. useEffect(() => {
  401. // if current model is not available
  402. // switch to first available model
  403. const isUnavaliableModel = !models.some((m) => m.name === currentModel);
  404. if (isUnavaliableModel && models.length > 0) {
  405. const nextModel = models[0].name as ModelType;
  406. chatStore.updateCurrentSession(
  407. (session) => (session.mask.modelConfig.model = nextModel),
  408. );
  409. showToast(nextModel);
  410. }
  411. }, [chatStore, currentModel, models]);
  412. return (
  413. <div className={styles["chat-input-actions"]}>
  414. {couldStop && (
  415. <ChatAction
  416. onClick={stopAll}
  417. text={Locale.Chat.InputActions.Stop}
  418. icon={<StopIcon />}
  419. />
  420. )}
  421. {!props.hitBottom && (
  422. <ChatAction
  423. onClick={props.scrollToBottom}
  424. text={Locale.Chat.InputActions.ToBottom}
  425. icon={<BottomIcon />}
  426. />
  427. )}
  428. {props.hitBottom && (
  429. <ChatAction
  430. onClick={props.showPromptModal}
  431. text={Locale.Chat.InputActions.Settings}
  432. icon={<SettingsIcon />}
  433. />
  434. )}
  435. <ChatAction
  436. onClick={nextTheme}
  437. text={Locale.Chat.InputActions.Theme[theme]}
  438. icon={
  439. <>
  440. {theme === Theme.Auto ? (
  441. <AutoIcon />
  442. ) : theme === Theme.Light ? (
  443. <LightIcon />
  444. ) : theme === Theme.Dark ? (
  445. <DarkIcon />
  446. ) : null}
  447. </>
  448. }
  449. />
  450. <ChatAction
  451. onClick={props.showPromptHints}
  452. text={Locale.Chat.InputActions.Prompt}
  453. icon={<PromptIcon />}
  454. />
  455. <ChatAction
  456. onClick={() => {
  457. navigate(Path.Masks);
  458. }}
  459. text={Locale.Chat.InputActions.Masks}
  460. icon={<MaskIcon />}
  461. />
  462. <ChatAction
  463. text={Locale.Chat.InputActions.Clear}
  464. icon={<BreakIcon />}
  465. onClick={() => {
  466. chatStore.updateCurrentSession((session) => {
  467. if (session.clearContextIndex === session.messages.length) {
  468. session.clearContextIndex = undefined;
  469. } else {
  470. session.clearContextIndex = session.messages.length;
  471. session.memoryPrompt = ""; // will clear memory
  472. }
  473. });
  474. }}
  475. />
  476. <ChatAction
  477. onClick={() => setShowModelSelector(true)}
  478. text={currentModel}
  479. icon={<RobotIcon />}
  480. />
  481. {showModelSelector && (
  482. <Selector
  483. defaultSelectedValue={currentModel}
  484. items={models.map((m) => ({
  485. title: m.displayName,
  486. value: m.name,
  487. }))}
  488. onClose={() => setShowModelSelector(false)}
  489. onSelection={(s) => {
  490. if (s.length === 0) return;
  491. chatStore.updateCurrentSession((session) => {
  492. session.mask.modelConfig.model = s[0] as ModelType;
  493. session.mask.syncGlobalConfig = false;
  494. });
  495. showToast(s[0]);
  496. }}
  497. />
  498. )}
  499. </div>
  500. );
  501. }
  502. export function EditMessageModal(props: { onClose: () => void }) {
  503. const chatStore = useChatStore();
  504. const session = chatStore.currentSession();
  505. const [messages, setMessages] = useState(session.messages.slice());
  506. return (
  507. <div className="modal-mask">
  508. <Modal
  509. title={Locale.Chat.EditMessage.Title}
  510. onClose={props.onClose}
  511. actions={[
  512. <IconButton
  513. text={Locale.UI.Cancel}
  514. icon={<CancelIcon />}
  515. key="cancel"
  516. onClick={() => {
  517. props.onClose();
  518. }}
  519. />,
  520. <IconButton
  521. type="primary"
  522. text={Locale.UI.Confirm}
  523. icon={<ConfirmIcon />}
  524. key="ok"
  525. onClick={() => {
  526. chatStore.updateCurrentSession(
  527. (session) => (session.messages = messages),
  528. );
  529. props.onClose();
  530. }}
  531. />,
  532. ]}
  533. >
  534. <List>
  535. <ListItem
  536. title={Locale.Chat.EditMessage.Topic.Title}
  537. subTitle={Locale.Chat.EditMessage.Topic.SubTitle}
  538. >
  539. <input
  540. type="text"
  541. value={session.topic}
  542. onInput={(e) =>
  543. chatStore.updateCurrentSession(
  544. (session) => (session.topic = e.currentTarget.value),
  545. )
  546. }
  547. ></input>
  548. </ListItem>
  549. </List>
  550. <ContextPrompts
  551. context={messages}
  552. updateContext={(updater) => {
  553. const newMessages = messages.slice();
  554. updater(newMessages);
  555. setMessages(newMessages);
  556. }}
  557. />
  558. </Modal>
  559. </div>
  560. );
  561. }
  562. function _Chat() {
  563. type RenderMessage = ChatMessage & { preview?: boolean };
  564. const chatStore = useChatStore();
  565. const session = chatStore.currentSession();
  566. const config = useAppConfig();
  567. const fontSize = config.fontSize;
  568. const [showExport, setShowExport] = useState(false);
  569. const inputRef = useRef<HTMLTextAreaElement>(null);
  570. const [userInput, setUserInput] = useState("");
  571. const [isLoading, setIsLoading] = useState(false);
  572. const { submitKey, shouldSubmit } = useSubmitHandler();
  573. const { scrollRef, setAutoScroll, scrollDomToBottom } = useScrollToBottom();
  574. const [hitBottom, setHitBottom] = useState(true);
  575. const isMobileScreen = useMobileScreen();
  576. const navigate = useNavigate();
  577. // prompt hints
  578. const promptStore = usePromptStore();
  579. const [promptHints, setPromptHints] = useState<RenderPompt[]>([]);
  580. const onSearch = useDebouncedCallback(
  581. (text: string) => {
  582. const matchedPrompts = promptStore.search(text);
  583. setPromptHints(matchedPrompts);
  584. },
  585. 100,
  586. { leading: true, trailing: true },
  587. );
  588. // auto grow input
  589. const [inputRows, setInputRows] = useState(2);
  590. const measure = useDebouncedCallback(
  591. () => {
  592. const rows = inputRef.current ? autoGrowTextArea(inputRef.current) : 1;
  593. const inputRows = Math.min(
  594. 20,
  595. Math.max(2 + Number(!isMobileScreen), rows),
  596. );
  597. setInputRows(inputRows);
  598. },
  599. 100,
  600. {
  601. leading: true,
  602. trailing: true,
  603. },
  604. );
  605. // eslint-disable-next-line react-hooks/exhaustive-deps
  606. useEffect(measure, [userInput]);
  607. // chat commands shortcuts
  608. const chatCommands = useChatCommand({
  609. new: () => chatStore.newSession(),
  610. newm: () => navigate(Path.NewChat),
  611. prev: () => chatStore.nextSession(-1),
  612. next: () => chatStore.nextSession(1),
  613. clear: () =>
  614. chatStore.updateCurrentSession(
  615. (session) => (session.clearContextIndex = session.messages.length),
  616. ),
  617. del: () => chatStore.deleteSession(chatStore.currentSessionIndex),
  618. });
  619. // only search prompts when user input is short
  620. const SEARCH_TEXT_LIMIT = 30;
  621. const onInput = (text: string) => {
  622. setUserInput(text);
  623. const n = text.trim().length;
  624. // clear search results
  625. if (n === 0) {
  626. setPromptHints([]);
  627. } else if (text.startsWith(ChatCommandPrefix)) {
  628. setPromptHints(chatCommands.search(text));
  629. } else if (!config.disablePromptHint && n < SEARCH_TEXT_LIMIT) {
  630. // check if need to trigger auto completion
  631. if (text.startsWith("/")) {
  632. let searchText = text.slice(1);
  633. onSearch(searchText);
  634. }
  635. }
  636. };
  637. const doSubmit = (userInput: string) => {
  638. if (userInput.trim() === "") return;
  639. const matchCommand = chatCommands.match(userInput);
  640. if (matchCommand.matched) {
  641. setUserInput("");
  642. setPromptHints([]);
  643. matchCommand.invoke();
  644. return;
  645. }
  646. setIsLoading(true);
  647. chatStore.onUserInput(userInput).then(() => setIsLoading(false));
  648. localStorage.setItem(LAST_INPUT_KEY, userInput);
  649. setUserInput("");
  650. setPromptHints([]);
  651. if (!isMobileScreen) inputRef.current?.focus();
  652. setAutoScroll(true);
  653. };
  654. const onPromptSelect = (prompt: RenderPompt) => {
  655. setTimeout(() => {
  656. setPromptHints([]);
  657. const matchedChatCommand = chatCommands.match(prompt.content);
  658. if (matchedChatCommand.matched) {
  659. // if user is selecting a chat command, just trigger it
  660. matchedChatCommand.invoke();
  661. setUserInput("");
  662. } else {
  663. // or fill the prompt
  664. setUserInput(prompt.content);
  665. }
  666. inputRef.current?.focus();
  667. }, 30);
  668. };
  669. // stop response
  670. const onUserStop = (messageId: string) => {
  671. ChatControllerPool.stop(session.id, messageId);
  672. };
  673. useEffect(() => {
  674. chatStore.updateCurrentSession((session) => {
  675. const stopTiming = Date.now() - REQUEST_TIMEOUT_MS;
  676. session.messages.forEach((m) => {
  677. // check if should stop all stale messages
  678. if (m.isError || new Date(m.date).getTime() < stopTiming) {
  679. if (m.streaming) {
  680. m.streaming = false;
  681. }
  682. if (m.content.length === 0) {
  683. m.isError = true;
  684. m.content = prettyObject({
  685. error: true,
  686. message: "empty response",
  687. });
  688. }
  689. }
  690. });
  691. // auto sync mask config from global config
  692. if (session.mask.syncGlobalConfig) {
  693. console.log("[Mask] syncing from global, name = ", session.mask.name);
  694. session.mask.modelConfig = { ...config.modelConfig };
  695. }
  696. });
  697. // eslint-disable-next-line react-hooks/exhaustive-deps
  698. }, []);
  699. // check if should send message
  700. const onInputKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  701. // if ArrowUp and no userInput, fill with last input
  702. if (
  703. e.key === "ArrowUp" &&
  704. userInput.length <= 0 &&
  705. !(e.metaKey || e.altKey || e.ctrlKey)
  706. ) {
  707. setUserInput(localStorage.getItem(LAST_INPUT_KEY) ?? "");
  708. e.preventDefault();
  709. return;
  710. }
  711. if (shouldSubmit(e) && promptHints.length === 0) {
  712. doSubmit(userInput);
  713. e.preventDefault();
  714. }
  715. };
  716. const onRightClick = (e: any, message: ChatMessage) => {
  717. // copy to clipboard
  718. if (selectOrCopy(e.currentTarget, message.content)) {
  719. if (userInput.length === 0) {
  720. setUserInput(message.content);
  721. }
  722. e.preventDefault();
  723. }
  724. };
  725. const deleteMessage = (msgId?: string) => {
  726. chatStore.updateCurrentSession(
  727. (session) =>
  728. (session.messages = session.messages.filter((m) => m.id !== msgId)),
  729. );
  730. };
  731. const onDelete = (msgId: string) => {
  732. deleteMessage(msgId);
  733. };
  734. const onResend = (message: ChatMessage) => {
  735. // when it is resending a message
  736. // 1. for a user's message, find the next bot response
  737. // 2. for a bot's message, find the last user's input
  738. // 3. delete original user input and bot's message
  739. // 4. resend the user's input
  740. const resendingIndex = session.messages.findIndex(
  741. (m) => m.id === message.id,
  742. );
  743. if (resendingIndex < 0 || resendingIndex >= session.messages.length) {
  744. console.error("[Chat] failed to find resending message", message);
  745. return;
  746. }
  747. let userMessage: ChatMessage | undefined;
  748. let botMessage: ChatMessage | undefined;
  749. if (message.role === "assistant") {
  750. // if it is resending a bot's message, find the user input for it
  751. botMessage = message;
  752. for (let i = resendingIndex; i >= 0; i -= 1) {
  753. if (session.messages[i].role === "user") {
  754. userMessage = session.messages[i];
  755. break;
  756. }
  757. }
  758. } else if (message.role === "user") {
  759. // if it is resending a user's input, find the bot's response
  760. userMessage = message;
  761. for (let i = resendingIndex; i < session.messages.length; i += 1) {
  762. if (session.messages[i].role === "assistant") {
  763. botMessage = session.messages[i];
  764. break;
  765. }
  766. }
  767. }
  768. if (userMessage === undefined) {
  769. console.error("[Chat] failed to resend", message);
  770. return;
  771. }
  772. // delete the original messages
  773. deleteMessage(userMessage.id);
  774. deleteMessage(botMessage?.id);
  775. // resend the message
  776. setIsLoading(true);
  777. chatStore.onUserInput(userMessage.content).then(() => setIsLoading(false));
  778. inputRef.current?.focus();
  779. };
  780. const onPinMessage = (message: ChatMessage) => {
  781. chatStore.updateCurrentSession((session) =>
  782. session.mask.context.push(message),
  783. );
  784. showToast(Locale.Chat.Actions.PinToastContent, {
  785. text: Locale.Chat.Actions.PinToastAction,
  786. onClick: () => {
  787. setShowPromptModal(true);
  788. },
  789. });
  790. };
  791. const context: RenderMessage[] = useMemo(() => {
  792. return session.mask.hideContext ? [] : session.mask.context.slice();
  793. }, [session.mask.context, session.mask.hideContext]);
  794. const accessStore = useAccessStore();
  795. if (
  796. context.length === 0 &&
  797. session.messages.at(0)?.content !== BOT_HELLO.content
  798. ) {
  799. const copiedHello = Object.assign({}, BOT_HELLO);
  800. if (!accessStore.isAuthorized()) {
  801. copiedHello.content = Locale.Error.Unauthorized;
  802. }
  803. context.push(copiedHello);
  804. }
  805. // preview messages
  806. const renderMessages = useMemo(() => {
  807. return context
  808. .concat(session.messages as RenderMessage[])
  809. .concat(
  810. isLoading
  811. ? [
  812. {
  813. ...createMessage({
  814. role: "assistant",
  815. content: "……",
  816. }),
  817. preview: true,
  818. },
  819. ]
  820. : [],
  821. )
  822. .concat(
  823. userInput.length > 0 && config.sendPreviewBubble
  824. ? [
  825. {
  826. ...createMessage({
  827. role: "user",
  828. content: userInput,
  829. }),
  830. preview: true,
  831. },
  832. ]
  833. : [],
  834. );
  835. }, [
  836. config.sendPreviewBubble,
  837. context,
  838. isLoading,
  839. session.messages,
  840. userInput,
  841. ]);
  842. const [msgRenderIndex, _setMsgRenderIndex] = useState(
  843. Math.max(0, renderMessages.length - CHAT_PAGE_SIZE),
  844. );
  845. function setMsgRenderIndex(newIndex: number) {
  846. newIndex = Math.min(renderMessages.length - CHAT_PAGE_SIZE, newIndex);
  847. newIndex = Math.max(0, newIndex);
  848. _setMsgRenderIndex(newIndex);
  849. }
  850. const messages = useMemo(() => {
  851. const endRenderIndex = Math.min(
  852. msgRenderIndex + 3 * CHAT_PAGE_SIZE,
  853. renderMessages.length,
  854. );
  855. return renderMessages.slice(msgRenderIndex, endRenderIndex);
  856. }, [msgRenderIndex, renderMessages]);
  857. const onChatBodyScroll = (e: HTMLElement) => {
  858. const bottomHeight = e.scrollTop + e.clientHeight;
  859. const edgeThreshold = e.clientHeight;
  860. const isTouchTopEdge = e.scrollTop <= edgeThreshold;
  861. const isTouchBottomEdge = bottomHeight >= e.scrollHeight - edgeThreshold;
  862. const isHitBottom =
  863. bottomHeight >= e.scrollHeight - (isMobileScreen ? 4 : 10);
  864. const prevPageMsgIndex = msgRenderIndex - CHAT_PAGE_SIZE;
  865. const nextPageMsgIndex = msgRenderIndex + CHAT_PAGE_SIZE;
  866. if (isTouchTopEdge && !isTouchBottomEdge) {
  867. setMsgRenderIndex(prevPageMsgIndex);
  868. } else if (isTouchBottomEdge) {
  869. setMsgRenderIndex(nextPageMsgIndex);
  870. }
  871. setHitBottom(isHitBottom);
  872. setAutoScroll(isHitBottom);
  873. };
  874. function scrollToBottom() {
  875. setMsgRenderIndex(renderMessages.length - CHAT_PAGE_SIZE);
  876. scrollDomToBottom();
  877. }
  878. // clear context index = context length + index in messages
  879. const clearContextIndex =
  880. (session.clearContextIndex ?? -1) >= 0
  881. ? session.clearContextIndex! + context.length - msgRenderIndex
  882. : -1;
  883. const [showPromptModal, setShowPromptModal] = useState(false);
  884. const clientConfig = useMemo(() => getClientConfig(), []);
  885. const autoFocus = !isMobileScreen; // wont auto focus on mobile screen
  886. const showMaxIcon = !isMobileScreen && !clientConfig?.isApp;
  887. useCommand({
  888. fill: setUserInput,
  889. submit: (text) => {
  890. doSubmit(text);
  891. },
  892. code: (text) => {
  893. if (accessStore.disableFastLink) return;
  894. console.log("[Command] got code from url: ", text);
  895. showConfirm(Locale.URLCommand.Code + `code = ${text}`).then((res) => {
  896. if (res) {
  897. accessStore.update((access) => (access.accessCode = text));
  898. }
  899. });
  900. },
  901. settings: (text) => {
  902. if (accessStore.disableFastLink) return;
  903. try {
  904. const payload = JSON.parse(text) as {
  905. key?: string;
  906. url?: string;
  907. };
  908. console.log("[Command] got settings from url: ", payload);
  909. if (payload.key || payload.url) {
  910. showConfirm(
  911. Locale.URLCommand.Settings +
  912. `\n${JSON.stringify(payload, null, 4)}`,
  913. ).then((res) => {
  914. if (!res) return;
  915. if (payload.key) {
  916. accessStore.update(
  917. (access) => (access.openaiApiKey = payload.key!),
  918. );
  919. }
  920. if (payload.url) {
  921. accessStore.update((access) => (access.openaiUrl = payload.url!));
  922. }
  923. });
  924. }
  925. } catch {
  926. console.error("[Command] failed to get settings from url: ", text);
  927. }
  928. },
  929. });
  930. // edit / insert message modal
  931. const [isEditingMessage, setIsEditingMessage] = useState(false);
  932. // remember unfinished input
  933. useEffect(() => {
  934. // try to load from local storage
  935. const key = UNFINISHED_INPUT(session.id);
  936. const mayBeUnfinishedInput = localStorage.getItem(key);
  937. if (mayBeUnfinishedInput && userInput.length === 0) {
  938. setUserInput(mayBeUnfinishedInput);
  939. localStorage.removeItem(key);
  940. }
  941. const dom = inputRef.current;
  942. return () => {
  943. localStorage.setItem(key, dom?.value ?? "");
  944. };
  945. // eslint-disable-next-line react-hooks/exhaustive-deps
  946. }, []);
  947. return (
  948. <div className={styles.chat} key={session.id}>
  949. <div className="window-header" data-tauri-drag-region>
  950. {isMobileScreen && (
  951. <div className="window-actions">
  952. <div className={"window-action-button"}>
  953. <IconButton
  954. icon={<ReturnIcon />}
  955. bordered
  956. title={Locale.Chat.Actions.ChatList}
  957. onClick={() => navigate(Path.Home)}
  958. />
  959. </div>
  960. </div>
  961. )}
  962. <div className={`window-header-title ${styles["chat-body-title"]}`}>
  963. <div
  964. className={`window-header-main-title ${styles["chat-body-main-title"]}`}
  965. onClickCapture={() => setIsEditingMessage(true)}
  966. >
  967. {!session.topic ? DEFAULT_TOPIC : session.topic}
  968. </div>
  969. <div className="window-header-sub-title">
  970. {Locale.Chat.SubTitle(session.messages.length)}
  971. </div>
  972. </div>
  973. <div className="window-actions">
  974. {!isMobileScreen && (
  975. <div className="window-action-button">
  976. <IconButton
  977. icon={<RenameIcon />}
  978. bordered
  979. onClick={() => setIsEditingMessage(true)}
  980. />
  981. </div>
  982. )}
  983. <div className="window-action-button">
  984. <IconButton
  985. icon={<ExportIcon />}
  986. bordered
  987. title={Locale.Chat.Actions.Export}
  988. onClick={() => {
  989. setShowExport(true);
  990. }}
  991. />
  992. </div>
  993. {showMaxIcon && (
  994. <div className="window-action-button">
  995. <IconButton
  996. icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
  997. bordered
  998. onClick={() => {
  999. config.update(
  1000. (config) => (config.tightBorder = !config.tightBorder),
  1001. );
  1002. }}
  1003. />
  1004. </div>
  1005. )}
  1006. </div>
  1007. <PromptToast
  1008. showToast={!hitBottom}
  1009. showModal={showPromptModal}
  1010. setShowModal={setShowPromptModal}
  1011. />
  1012. </div>
  1013. <div
  1014. className={styles["chat-body"]}
  1015. ref={scrollRef}
  1016. onScroll={(e) => onChatBodyScroll(e.currentTarget)}
  1017. onMouseDown={() => inputRef.current?.blur()}
  1018. onTouchStart={() => {
  1019. inputRef.current?.blur();
  1020. setAutoScroll(false);
  1021. }}
  1022. >
  1023. {messages.map((message, i) => {
  1024. const isUser = message.role === "user";
  1025. const isContext = i < context.length;
  1026. const showActions =
  1027. i > 0 &&
  1028. !(message.preview || message.content.length === 0) &&
  1029. !isContext;
  1030. const showTyping = message.preview || message.streaming;
  1031. const shouldShowClearContextDivider = i === clearContextIndex - 1;
  1032. return (
  1033. <Fragment key={message.id}>
  1034. <div
  1035. className={
  1036. isUser ? styles["chat-message-user"] : styles["chat-message"]
  1037. }
  1038. >
  1039. <div className={styles["chat-message-container"]}>
  1040. <div className={styles["chat-message-header"]}>
  1041. <div className={styles["chat-message-avatar"]}>
  1042. <div className={styles["chat-message-edit"]}>
  1043. <IconButton
  1044. icon={<EditIcon />}
  1045. onClick={async () => {
  1046. const newMessage = await showPrompt(
  1047. Locale.Chat.Actions.Edit,
  1048. message.content,
  1049. 10,
  1050. );
  1051. chatStore.updateCurrentSession((session) => {
  1052. const m = session.mask.context
  1053. .concat(session.messages)
  1054. .find((m) => m.id === message.id);
  1055. if (m) {
  1056. m.content = newMessage;
  1057. }
  1058. });
  1059. }}
  1060. ></IconButton>
  1061. </div>
  1062. {isUser ? (
  1063. <Avatar avatar={config.avatar} />
  1064. ) : (
  1065. <>
  1066. {["system"].includes(message.role) ? (
  1067. <Avatar avatar="2699-fe0f" />
  1068. ) : (
  1069. <MaskAvatar
  1070. avatar={session.mask.avatar}
  1071. model={
  1072. message.model || session.mask.modelConfig.model
  1073. }
  1074. />
  1075. )}
  1076. </>
  1077. )}
  1078. </div>
  1079. {showActions && (
  1080. <div className={styles["chat-message-actions"]}>
  1081. <div className={styles["chat-input-actions"]}>
  1082. {message.streaming ? (
  1083. <ChatAction
  1084. text={Locale.Chat.Actions.Stop}
  1085. icon={<StopIcon />}
  1086. onClick={() => onUserStop(message.id ?? i)}
  1087. />
  1088. ) : (
  1089. <>
  1090. <ChatAction
  1091. text={Locale.Chat.Actions.Retry}
  1092. icon={<ResetIcon />}
  1093. onClick={() => onResend(message)}
  1094. />
  1095. <ChatAction
  1096. text={Locale.Chat.Actions.Delete}
  1097. icon={<DeleteIcon />}
  1098. onClick={() => onDelete(message.id ?? i)}
  1099. />
  1100. <ChatAction
  1101. text={Locale.Chat.Actions.Pin}
  1102. icon={<PinIcon />}
  1103. onClick={() => onPinMessage(message)}
  1104. />
  1105. <ChatAction
  1106. text={Locale.Chat.Actions.Copy}
  1107. icon={<CopyIcon />}
  1108. onClick={() => copyToClipboard(message.content)}
  1109. />
  1110. </>
  1111. )}
  1112. </div>
  1113. </div>
  1114. )}
  1115. </div>
  1116. {showTyping && (
  1117. <div className={styles["chat-message-status"]}>
  1118. {Locale.Chat.Typing}
  1119. </div>
  1120. )}
  1121. <div className={styles["chat-message-item"]}>
  1122. <Markdown
  1123. content={message.content}
  1124. loading={
  1125. (message.preview || message.streaming) &&
  1126. message.content.length === 0 &&
  1127. !isUser
  1128. }
  1129. onContextMenu={(e) => onRightClick(e, message)}
  1130. onDoubleClickCapture={() => {
  1131. if (!isMobileScreen) return;
  1132. setUserInput(message.content);
  1133. }}
  1134. fontSize={fontSize}
  1135. parentRef={scrollRef}
  1136. defaultShow={i >= messages.length - 6}
  1137. />
  1138. </div>
  1139. <div className={styles["chat-message-action-date"]}>
  1140. {isContext
  1141. ? Locale.Chat.IsContext
  1142. : message.date.toLocaleString()}
  1143. </div>
  1144. </div>
  1145. </div>
  1146. {shouldShowClearContextDivider && <ClearContextDivider />}
  1147. </Fragment>
  1148. );
  1149. })}
  1150. </div>
  1151. <div className={styles["chat-input-panel"]}>
  1152. <PromptHints prompts={promptHints} onPromptSelect={onPromptSelect} />
  1153. <ChatActions
  1154. showPromptModal={() => setShowPromptModal(true)}
  1155. scrollToBottom={scrollToBottom}
  1156. hitBottom={hitBottom}
  1157. showPromptHints={() => {
  1158. // Click again to close
  1159. if (promptHints.length > 0) {
  1160. setPromptHints([]);
  1161. return;
  1162. }
  1163. inputRef.current?.focus();
  1164. setUserInput("/");
  1165. onSearch("");
  1166. }}
  1167. />
  1168. <div className={styles["chat-input-panel-inner"]}>
  1169. <textarea
  1170. ref={inputRef}
  1171. className={styles["chat-input"]}
  1172. placeholder={Locale.Chat.Input(submitKey)}
  1173. onInput={(e) => onInput(e.currentTarget.value)}
  1174. value={userInput}
  1175. onKeyDown={onInputKeyDown}
  1176. onFocus={scrollToBottom}
  1177. onClick={scrollToBottom}
  1178. rows={inputRows}
  1179. autoFocus={autoFocus}
  1180. style={{
  1181. fontSize: config.fontSize,
  1182. }}
  1183. />
  1184. <IconButton
  1185. icon={<SendWhiteIcon />}
  1186. text={Locale.Chat.Send}
  1187. className={styles["chat-input-send"]}
  1188. type="primary"
  1189. onClick={() => doSubmit(userInput)}
  1190. />
  1191. </div>
  1192. </div>
  1193. {showExport && (
  1194. <ExportMessageModal onClose={() => setShowExport(false)} />
  1195. )}
  1196. {isEditingMessage && (
  1197. <EditMessageModal
  1198. onClose={() => {
  1199. setIsEditingMessage(false);
  1200. }}
  1201. />
  1202. )}
  1203. </div>
  1204. );
  1205. }
  1206. export function Chat() {
  1207. const chatStore = useChatStore();
  1208. const sessionIndex = chatStore.currentSessionIndex;
  1209. return <_Chat key={sessionIndex}></_Chat>;
  1210. }