chat.tsx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. import { useDebouncedCallback } from "use-debounce";
  2. import React, {
  3. useState,
  4. useRef,
  5. useEffect,
  6. useMemo,
  7. useCallback,
  8. } from "react";
  9. import SendWhiteIcon from "../icons/send-white.svg";
  10. import BrainIcon from "../icons/brain.svg";
  11. import RenameIcon from "../icons/rename.svg";
  12. import ExportIcon from "../icons/share.svg";
  13. import ReturnIcon from "../icons/return.svg";
  14. import CopyIcon from "../icons/copy.svg";
  15. import LoadingIcon from "../icons/three-dots.svg";
  16. import PromptIcon from "../icons/prompt.svg";
  17. import MaskIcon from "../icons/mask.svg";
  18. import MaxIcon from "../icons/max.svg";
  19. import MinIcon from "../icons/min.svg";
  20. import ResetIcon from "../icons/reload.svg";
  21. import BreakIcon from "../icons/break.svg";
  22. import SettingsIcon from "../icons/chat-settings.svg";
  23. import LightIcon from "../icons/light.svg";
  24. import DarkIcon from "../icons/dark.svg";
  25. import AutoIcon from "../icons/auto.svg";
  26. import BottomIcon from "../icons/bottom.svg";
  27. import StopIcon from "../icons/pause.svg";
  28. import RobotIcon from "../icons/robot.svg";
  29. import {
  30. ChatMessage,
  31. SubmitKey,
  32. useChatStore,
  33. BOT_HELLO,
  34. createMessage,
  35. useAccessStore,
  36. Theme,
  37. useAppConfig,
  38. DEFAULT_TOPIC,
  39. ALL_MODELS,
  40. } from "../store";
  41. import {
  42. copyToClipboard,
  43. downloadAs,
  44. selectOrCopy,
  45. autoGrowTextArea,
  46. useMobileScreen,
  47. } from "../utils";
  48. import dynamic from "next/dynamic";
  49. import { ChatControllerPool } from "../client/controller";
  50. import { Prompt, usePromptStore } from "../store/prompt";
  51. import Locale from "../locales";
  52. import { IconButton } from "./button";
  53. import styles from "./home.module.scss";
  54. import chatStyle from "./chat.module.scss";
  55. import { ListItem, Modal } from "./ui-lib";
  56. import { useLocation, useNavigate } from "react-router-dom";
  57. import { LAST_INPUT_KEY, Path, REQUEST_TIMEOUT_MS } from "../constant";
  58. import { Avatar } from "./emoji";
  59. import { MaskAvatar, MaskConfig } from "./mask";
  60. import { useMaskStore } from "../store/mask";
  61. import { ChatCommandPrefix, useChatCommand, useCommand } from "../command";
  62. import { prettyObject } from "../utils/format";
  63. import { ExportMessageModal } from "./exporter";
  64. import { getClientConfig } from "../config/client";
  65. const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
  66. loading: () => <LoadingIcon />,
  67. });
  68. export function SessionConfigModel(props: { onClose: () => void }) {
  69. const chatStore = useChatStore();
  70. const session = chatStore.currentSession();
  71. const maskStore = useMaskStore();
  72. const navigate = useNavigate();
  73. return (
  74. <div className="modal-mask">
  75. <Modal
  76. title={Locale.Context.Edit}
  77. onClose={() => props.onClose()}
  78. actions={[
  79. <IconButton
  80. key="reset"
  81. icon={<ResetIcon />}
  82. bordered
  83. text={Locale.Chat.Config.Reset}
  84. onClick={() => {
  85. if (confirm(Locale.Memory.ResetConfirm)) {
  86. chatStore.updateCurrentSession(
  87. (session) => (session.memoryPrompt = ""),
  88. );
  89. }
  90. }}
  91. />,
  92. <IconButton
  93. key="copy"
  94. icon={<CopyIcon />}
  95. bordered
  96. text={Locale.Chat.Config.SaveAs}
  97. onClick={() => {
  98. navigate(Path.Masks);
  99. setTimeout(() => {
  100. maskStore.create(session.mask);
  101. }, 500);
  102. }}
  103. />,
  104. ]}
  105. >
  106. <MaskConfig
  107. mask={session.mask}
  108. updateMask={(updater) => {
  109. const mask = { ...session.mask };
  110. updater(mask);
  111. chatStore.updateCurrentSession((session) => (session.mask = mask));
  112. }}
  113. shouldSyncFromGlobal
  114. extraListItems={
  115. session.mask.modelConfig.sendMemory ? (
  116. <ListItem
  117. title={`${Locale.Memory.Title} (${session.lastSummarizeIndex} of ${session.messages.length})`}
  118. subTitle={session.memoryPrompt || Locale.Memory.EmptyContent}
  119. ></ListItem>
  120. ) : (
  121. <></>
  122. )
  123. }
  124. ></MaskConfig>
  125. </Modal>
  126. </div>
  127. );
  128. }
  129. function PromptToast(props: {
  130. showToast?: boolean;
  131. showModal?: boolean;
  132. setShowModal: (_: boolean) => void;
  133. }) {
  134. const chatStore = useChatStore();
  135. const session = chatStore.currentSession();
  136. const context = session.mask.context;
  137. return (
  138. <div className={chatStyle["prompt-toast"]} key="prompt-toast">
  139. {props.showToast && (
  140. <div
  141. className={chatStyle["prompt-toast-inner"] + " clickable"}
  142. role="button"
  143. onClick={() => props.setShowModal(true)}
  144. >
  145. <BrainIcon />
  146. <span className={chatStyle["prompt-toast-content"]}>
  147. {Locale.Context.Toast(context.length)}
  148. </span>
  149. </div>
  150. )}
  151. {props.showModal && (
  152. <SessionConfigModel onClose={() => props.setShowModal(false)} />
  153. )}
  154. </div>
  155. );
  156. }
  157. function useSubmitHandler() {
  158. const config = useAppConfig();
  159. const submitKey = config.submitKey;
  160. const shouldSubmit = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  161. if (e.key !== "Enter") return false;
  162. if (e.key === "Enter" && e.nativeEvent.isComposing) return false;
  163. return (
  164. (config.submitKey === SubmitKey.AltEnter && e.altKey) ||
  165. (config.submitKey === SubmitKey.CtrlEnter && e.ctrlKey) ||
  166. (config.submitKey === SubmitKey.ShiftEnter && e.shiftKey) ||
  167. (config.submitKey === SubmitKey.MetaEnter && e.metaKey) ||
  168. (config.submitKey === SubmitKey.Enter &&
  169. !e.altKey &&
  170. !e.ctrlKey &&
  171. !e.shiftKey &&
  172. !e.metaKey)
  173. );
  174. };
  175. return {
  176. submitKey,
  177. shouldSubmit,
  178. };
  179. }
  180. export function PromptHints(props: {
  181. prompts: Prompt[];
  182. onPromptSelect: (prompt: Prompt) => void;
  183. }) {
  184. const noPrompts = props.prompts.length === 0;
  185. const [selectIndex, setSelectIndex] = useState(0);
  186. const selectedRef = useRef<HTMLDivElement>(null);
  187. useEffect(() => {
  188. setSelectIndex(0);
  189. }, [props.prompts.length]);
  190. useEffect(() => {
  191. const onKeyDown = (e: KeyboardEvent) => {
  192. if (noPrompts || e.metaKey || e.altKey || e.ctrlKey) {
  193. return;
  194. }
  195. // arrow up / down to select prompt
  196. const changeIndex = (delta: number) => {
  197. e.stopPropagation();
  198. e.preventDefault();
  199. const nextIndex = Math.max(
  200. 0,
  201. Math.min(props.prompts.length - 1, selectIndex + delta),
  202. );
  203. setSelectIndex(nextIndex);
  204. selectedRef.current?.scrollIntoView({
  205. block: "center",
  206. });
  207. };
  208. if (e.key === "ArrowUp") {
  209. changeIndex(1);
  210. } else if (e.key === "ArrowDown") {
  211. changeIndex(-1);
  212. } else if (e.key === "Enter") {
  213. const selectedPrompt = props.prompts.at(selectIndex);
  214. if (selectedPrompt) {
  215. props.onPromptSelect(selectedPrompt);
  216. }
  217. }
  218. };
  219. window.addEventListener("keydown", onKeyDown);
  220. return () => window.removeEventListener("keydown", onKeyDown);
  221. // eslint-disable-next-line react-hooks/exhaustive-deps
  222. }, [props.prompts.length, selectIndex]);
  223. if (noPrompts) return null;
  224. return (
  225. <div className={styles["prompt-hints"]}>
  226. {props.prompts.map((prompt, i) => (
  227. <div
  228. ref={i === selectIndex ? selectedRef : null}
  229. className={
  230. styles["prompt-hint"] +
  231. ` ${i === selectIndex ? styles["prompt-hint-selected"] : ""}`
  232. }
  233. key={prompt.title + i.toString()}
  234. onClick={() => props.onPromptSelect(prompt)}
  235. onMouseEnter={() => setSelectIndex(i)}
  236. >
  237. <div className={styles["hint-title"]}>{prompt.title}</div>
  238. <div className={styles["hint-content"]}>{prompt.content}</div>
  239. </div>
  240. ))}
  241. </div>
  242. );
  243. }
  244. function ClearContextDivider() {
  245. const chatStore = useChatStore();
  246. return (
  247. <div
  248. className={chatStyle["clear-context"]}
  249. onClick={() =>
  250. chatStore.updateCurrentSession(
  251. (session) => (session.clearContextIndex = undefined),
  252. )
  253. }
  254. >
  255. <div className={chatStyle["clear-context-tips"]}>
  256. {Locale.Context.Clear}
  257. </div>
  258. <div className={chatStyle["clear-context-revert-btn"]}>
  259. {Locale.Context.Revert}
  260. </div>
  261. </div>
  262. );
  263. }
  264. function ChatAction(props: {
  265. text: string;
  266. icon: JSX.Element;
  267. onClick: () => void;
  268. }) {
  269. const iconRef = useRef<HTMLDivElement>(null);
  270. const textRef = useRef<HTMLDivElement>(null);
  271. const [width, setWidth] = useState({
  272. full: 20,
  273. icon: 20,
  274. });
  275. function updateWidth() {
  276. if (!iconRef.current || !textRef.current) return;
  277. const getWidth = (dom: HTMLDivElement) => dom.getBoundingClientRect().width;
  278. const textWidth = getWidth(textRef.current);
  279. const iconWidth = getWidth(iconRef.current);
  280. setWidth({
  281. full: textWidth + iconWidth,
  282. icon: iconWidth,
  283. });
  284. }
  285. useEffect(() => {
  286. updateWidth();
  287. }, []);
  288. return (
  289. <div
  290. className={`${chatStyle["chat-input-action"]} clickable`}
  291. onClick={() => {
  292. props.onClick();
  293. setTimeout(updateWidth, 1);
  294. }}
  295. style={
  296. {
  297. "--icon-width": `${width.icon}px`,
  298. "--full-width": `${width.full}px`,
  299. } as React.CSSProperties
  300. }
  301. >
  302. <div ref={iconRef} className={chatStyle["icon"]}>
  303. {props.icon}
  304. </div>
  305. <div className={chatStyle["text"]} ref={textRef}>
  306. {props.text}
  307. </div>
  308. </div>
  309. );
  310. }
  311. function useScrollToBottom() {
  312. // for auto-scroll
  313. const scrollRef = useRef<HTMLDivElement>(null);
  314. const [autoScroll, setAutoScroll] = useState(true);
  315. const scrollToBottom = useCallback(() => {
  316. const dom = scrollRef.current;
  317. if (dom) {
  318. requestAnimationFrame(() => dom.scrollTo(0, dom.scrollHeight));
  319. }
  320. }, []);
  321. // auto scroll
  322. useEffect(() => {
  323. autoScroll && scrollToBottom();
  324. });
  325. return {
  326. scrollRef,
  327. autoScroll,
  328. setAutoScroll,
  329. scrollToBottom,
  330. };
  331. }
  332. export function ChatActions(props: {
  333. showPromptModal: () => void;
  334. scrollToBottom: () => void;
  335. showPromptHints: () => void;
  336. hitBottom: boolean;
  337. }) {
  338. const config = useAppConfig();
  339. const navigate = useNavigate();
  340. const chatStore = useChatStore();
  341. // switch themes
  342. const theme = config.theme;
  343. function nextTheme() {
  344. const themes = [Theme.Auto, Theme.Light, Theme.Dark];
  345. const themeIndex = themes.indexOf(theme);
  346. const nextIndex = (themeIndex + 1) % themes.length;
  347. const nextTheme = themes[nextIndex];
  348. config.update((config) => (config.theme = nextTheme));
  349. }
  350. // stop all responses
  351. const couldStop = ChatControllerPool.hasPending();
  352. const stopAll = () => ChatControllerPool.stopAll();
  353. // switch model
  354. const currentModel = chatStore.currentSession().mask.modelConfig.model;
  355. function nextModel() {
  356. const models = ALL_MODELS.filter((m) => m.available).map((m) => m.name);
  357. const modelIndex = models.indexOf(currentModel);
  358. const nextIndex = (modelIndex + 1) % models.length;
  359. const nextModel = models[nextIndex];
  360. chatStore.updateCurrentSession((session) => {
  361. session.mask.modelConfig.model = nextModel;
  362. session.mask.syncGlobalConfig = false;
  363. });
  364. }
  365. return (
  366. <div className={chatStyle["chat-input-actions"]}>
  367. {couldStop && (
  368. <ChatAction
  369. onClick={stopAll}
  370. text={Locale.Chat.InputActions.Stop}
  371. icon={<StopIcon />}
  372. />
  373. )}
  374. {!props.hitBottom && (
  375. <ChatAction
  376. onClick={props.scrollToBottom}
  377. text={Locale.Chat.InputActions.ToBottom}
  378. icon={<BottomIcon />}
  379. />
  380. )}
  381. {props.hitBottom && (
  382. <ChatAction
  383. onClick={props.showPromptModal}
  384. text={Locale.Chat.InputActions.Settings}
  385. icon={<SettingsIcon />}
  386. />
  387. )}
  388. <ChatAction
  389. onClick={nextTheme}
  390. text={Locale.Chat.InputActions.Theme[theme]}
  391. icon={
  392. <>
  393. {theme === Theme.Auto ? (
  394. <AutoIcon />
  395. ) : theme === Theme.Light ? (
  396. <LightIcon />
  397. ) : theme === Theme.Dark ? (
  398. <DarkIcon />
  399. ) : null}
  400. </>
  401. }
  402. />
  403. <ChatAction
  404. onClick={props.showPromptHints}
  405. text={Locale.Chat.InputActions.Prompt}
  406. icon={<PromptIcon />}
  407. />
  408. <ChatAction
  409. onClick={() => {
  410. navigate(Path.Masks);
  411. }}
  412. text={Locale.Chat.InputActions.Masks}
  413. icon={<MaskIcon />}
  414. />
  415. <ChatAction
  416. text={Locale.Chat.InputActions.Clear}
  417. icon={<BreakIcon />}
  418. onClick={() => {
  419. chatStore.updateCurrentSession((session) => {
  420. if (session.clearContextIndex === session.messages.length) {
  421. session.clearContextIndex = undefined;
  422. } else {
  423. session.clearContextIndex = session.messages.length;
  424. session.memoryPrompt = ""; // will clear memory
  425. }
  426. });
  427. }}
  428. />
  429. <ChatAction
  430. onClick={nextModel}
  431. text={currentModel}
  432. icon={<RobotIcon />}
  433. />
  434. </div>
  435. );
  436. }
  437. export function Chat() {
  438. type RenderMessage = ChatMessage & { preview?: boolean };
  439. const chatStore = useChatStore();
  440. const [session, sessionIndex] = useChatStore((state) => [
  441. state.currentSession(),
  442. state.currentSessionIndex,
  443. ]);
  444. const config = useAppConfig();
  445. const fontSize = config.fontSize;
  446. const [showExport, setShowExport] = useState(false);
  447. const inputRef = useRef<HTMLTextAreaElement>(null);
  448. const [userInput, setUserInput] = useState("");
  449. const [isLoading, setIsLoading] = useState(false);
  450. const { submitKey, shouldSubmit } = useSubmitHandler();
  451. const { scrollRef, setAutoScroll, scrollToBottom } = useScrollToBottom();
  452. const [hitBottom, setHitBottom] = useState(true);
  453. const isMobileScreen = useMobileScreen();
  454. const navigate = useNavigate();
  455. const onChatBodyScroll = (e: HTMLElement) => {
  456. const isTouchBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 10;
  457. setHitBottom(isTouchBottom);
  458. };
  459. // prompt hints
  460. const promptStore = usePromptStore();
  461. const [promptHints, setPromptHints] = useState<Prompt[]>([]);
  462. const onSearch = useDebouncedCallback(
  463. (text: string) => {
  464. const matchedPrompts = promptStore.search(text);
  465. setPromptHints(matchedPrompts);
  466. },
  467. 100,
  468. { leading: true, trailing: true },
  469. );
  470. const onPromptSelect = (prompt: Prompt) => {
  471. setTimeout(() => {
  472. setPromptHints([]);
  473. setUserInput(prompt.content);
  474. inputRef.current?.focus();
  475. }, 30);
  476. };
  477. // auto grow input
  478. const [inputRows, setInputRows] = useState(2);
  479. const measure = useDebouncedCallback(
  480. () => {
  481. const rows = inputRef.current ? autoGrowTextArea(inputRef.current) : 1;
  482. const inputRows = Math.min(
  483. 20,
  484. Math.max(2 + Number(!isMobileScreen), rows),
  485. );
  486. setInputRows(inputRows);
  487. },
  488. 100,
  489. {
  490. leading: true,
  491. trailing: true,
  492. },
  493. );
  494. // eslint-disable-next-line react-hooks/exhaustive-deps
  495. useEffect(measure, [userInput]);
  496. // chat commands shortcuts
  497. const chatCommands = useChatCommand({
  498. new: () => chatStore.newSession(),
  499. newm: () => navigate(Path.NewChat),
  500. prev: () => chatStore.nextSession(-1),
  501. next: () => chatStore.nextSession(1),
  502. clear: () =>
  503. chatStore.updateCurrentSession(
  504. (session) => (session.clearContextIndex = session.messages.length),
  505. ),
  506. del: () => chatStore.deleteSession(chatStore.currentSessionIndex),
  507. });
  508. // only search prompts when user input is short
  509. const SEARCH_TEXT_LIMIT = 30;
  510. const onInput = (text: string) => {
  511. setUserInput(text);
  512. const n = text.trim().length;
  513. // clear search results
  514. if (n === 0) {
  515. setPromptHints([]);
  516. } else if (text.startsWith(ChatCommandPrefix)) {
  517. setPromptHints(chatCommands.search(text));
  518. } else if (!config.disablePromptHint && n < SEARCH_TEXT_LIMIT) {
  519. // check if need to trigger auto completion
  520. if (text.startsWith("/")) {
  521. let searchText = text.slice(1);
  522. onSearch(searchText);
  523. }
  524. }
  525. };
  526. const doSubmit = (userInput: string) => {
  527. if (userInput.trim() === "") return;
  528. const matchCommand = chatCommands.match(userInput);
  529. if (matchCommand.matched) {
  530. setUserInput("");
  531. setPromptHints([]);
  532. matchCommand.invoke();
  533. return;
  534. }
  535. setIsLoading(true);
  536. chatStore.onUserInput(userInput).then(() => setIsLoading(false));
  537. localStorage.setItem(LAST_INPUT_KEY, userInput);
  538. setUserInput("");
  539. setPromptHints([]);
  540. if (!isMobileScreen) inputRef.current?.focus();
  541. setAutoScroll(true);
  542. };
  543. // stop response
  544. const onUserStop = (messageId: number) => {
  545. ChatControllerPool.stop(sessionIndex, messageId);
  546. };
  547. useEffect(() => {
  548. chatStore.updateCurrentSession((session) => {
  549. const stopTiming = Date.now() - REQUEST_TIMEOUT_MS;
  550. session.messages.forEach((m) => {
  551. // check if should stop all stale messages
  552. if (m.isError || new Date(m.date).getTime() < stopTiming) {
  553. if (m.streaming) {
  554. m.streaming = false;
  555. }
  556. if (m.content.length === 0) {
  557. m.isError = true;
  558. m.content = prettyObject({
  559. error: true,
  560. message: "empty response",
  561. });
  562. }
  563. }
  564. });
  565. // auto sync mask config from global config
  566. if (session.mask.syncGlobalConfig) {
  567. console.log("[Mask] syncing from global, name = ", session.mask.name);
  568. session.mask.modelConfig = { ...config.modelConfig };
  569. }
  570. });
  571. // eslint-disable-next-line react-hooks/exhaustive-deps
  572. }, []);
  573. // check if should send message
  574. const onInputKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  575. // if ArrowUp and no userInput, fill with last input
  576. if (
  577. e.key === "ArrowUp" &&
  578. userInput.length <= 0 &&
  579. !(e.metaKey || e.altKey || e.ctrlKey)
  580. ) {
  581. setUserInput(localStorage.getItem(LAST_INPUT_KEY) ?? "");
  582. e.preventDefault();
  583. return;
  584. }
  585. if (shouldSubmit(e) && promptHints.length === 0) {
  586. doSubmit(userInput);
  587. e.preventDefault();
  588. }
  589. };
  590. const onRightClick = (e: any, message: ChatMessage) => {
  591. // copy to clipboard
  592. if (selectOrCopy(e.currentTarget, message.content)) {
  593. if (userInput.length === 0) {
  594. setUserInput(message.content);
  595. }
  596. e.preventDefault();
  597. }
  598. };
  599. const findLastUserIndex = (messageId: number) => {
  600. // find last user input message and resend
  601. let lastUserMessageIndex: number | null = null;
  602. for (let i = 0; i < session.messages.length; i += 1) {
  603. const message = session.messages[i];
  604. if (message.id === messageId) {
  605. break;
  606. }
  607. if (message.role === "user") {
  608. lastUserMessageIndex = i;
  609. }
  610. }
  611. return lastUserMessageIndex;
  612. };
  613. const deleteMessage = (userIndex: number) => {
  614. chatStore.updateCurrentSession((session) =>
  615. session.messages.splice(userIndex, 2),
  616. );
  617. };
  618. const onDelete = (botMessageId: number) => {
  619. const userIndex = findLastUserIndex(botMessageId);
  620. if (userIndex === null) return;
  621. deleteMessage(userIndex);
  622. };
  623. const onResend = (botMessageId: number) => {
  624. // find last user input message and resend
  625. const userIndex = findLastUserIndex(botMessageId);
  626. if (userIndex === null) return;
  627. setIsLoading(true);
  628. const content = session.messages[userIndex].content;
  629. deleteMessage(userIndex);
  630. chatStore.onUserInput(content).then(() => setIsLoading(false));
  631. inputRef.current?.focus();
  632. };
  633. const context: RenderMessage[] = session.mask.hideContext
  634. ? []
  635. : session.mask.context.slice();
  636. const accessStore = useAccessStore();
  637. if (
  638. context.length === 0 &&
  639. session.messages.at(0)?.content !== BOT_HELLO.content
  640. ) {
  641. const copiedHello = Object.assign({}, BOT_HELLO);
  642. if (!accessStore.isAuthorized()) {
  643. copiedHello.content = Locale.Error.Unauthorized;
  644. }
  645. context.push(copiedHello);
  646. }
  647. // clear context index = context length + index in messages
  648. const clearContextIndex =
  649. (session.clearContextIndex ?? -1) >= 0
  650. ? session.clearContextIndex! + context.length
  651. : -1;
  652. // preview messages
  653. const messages = context
  654. .concat(session.messages as RenderMessage[])
  655. .concat(
  656. isLoading
  657. ? [
  658. {
  659. ...createMessage({
  660. role: "assistant",
  661. content: "……",
  662. }),
  663. preview: true,
  664. },
  665. ]
  666. : [],
  667. )
  668. .concat(
  669. userInput.length > 0 && config.sendPreviewBubble
  670. ? [
  671. {
  672. ...createMessage({
  673. role: "user",
  674. content: userInput,
  675. }),
  676. preview: true,
  677. },
  678. ]
  679. : [],
  680. );
  681. const [showPromptModal, setShowPromptModal] = useState(false);
  682. const renameSession = () => {
  683. const newTopic = prompt(Locale.Chat.Rename, session.topic);
  684. if (newTopic && newTopic !== session.topic) {
  685. chatStore.updateCurrentSession((session) => (session.topic = newTopic!));
  686. }
  687. };
  688. const clientConfig = useMemo(() => getClientConfig(), []);
  689. const location = useLocation();
  690. const isChat = location.pathname === Path.Chat;
  691. const autoFocus = !isMobileScreen || isChat; // only focus in chat page
  692. const showMaxIcon = !isMobileScreen && !clientConfig?.isApp;
  693. useCommand({
  694. fill: setUserInput,
  695. submit: (text) => {
  696. doSubmit(text);
  697. },
  698. });
  699. return (
  700. <div className={styles.chat} key={session.id}>
  701. <div className="window-header" data-tauri-drag-region>
  702. <div className="window-header-title">
  703. <div
  704. className={`window-header-main-title " ${styles["chat-body-title"]}`}
  705. onClickCapture={renameSession}
  706. >
  707. {!session.topic ? DEFAULT_TOPIC : session.topic}
  708. </div>
  709. <div className="window-header-sub-title">
  710. {Locale.Chat.SubTitle(session.messages.length)}
  711. </div>
  712. </div>
  713. <div className="window-actions">
  714. <div className={"window-action-button" + " " + styles.mobile}>
  715. <IconButton
  716. icon={<ReturnIcon />}
  717. bordered
  718. title={Locale.Chat.Actions.ChatList}
  719. onClick={() => navigate(Path.Home)}
  720. />
  721. </div>
  722. <div className="window-action-button">
  723. <IconButton
  724. icon={<RenameIcon />}
  725. bordered
  726. onClick={renameSession}
  727. />
  728. </div>
  729. <div className="window-action-button">
  730. <IconButton
  731. icon={<ExportIcon />}
  732. bordered
  733. title={Locale.Chat.Actions.Export}
  734. onClick={() => {
  735. setShowExport(true);
  736. }}
  737. />
  738. </div>
  739. {showMaxIcon && (
  740. <div className="window-action-button">
  741. <IconButton
  742. icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
  743. bordered
  744. onClick={() => {
  745. config.update(
  746. (config) => (config.tightBorder = !config.tightBorder),
  747. );
  748. }}
  749. />
  750. </div>
  751. )}
  752. </div>
  753. <PromptToast
  754. showToast={!hitBottom}
  755. showModal={showPromptModal}
  756. setShowModal={setShowPromptModal}
  757. />
  758. </div>
  759. <div
  760. className={styles["chat-body"]}
  761. ref={scrollRef}
  762. onScroll={(e) => onChatBodyScroll(e.currentTarget)}
  763. onMouseDown={() => inputRef.current?.blur()}
  764. onWheel={(e) => setAutoScroll(hitBottom && e.deltaY > 0)}
  765. onTouchStart={() => {
  766. inputRef.current?.blur();
  767. setAutoScroll(false);
  768. }}
  769. >
  770. {messages.map((message, i) => {
  771. const isUser = message.role === "user";
  772. const showActions =
  773. !isUser &&
  774. i > 0 &&
  775. !(message.preview || message.content.length === 0);
  776. const showTyping = message.preview || message.streaming;
  777. const shouldShowClearContextDivider = i === clearContextIndex - 1;
  778. return (
  779. <>
  780. <div
  781. key={i}
  782. className={
  783. isUser ? styles["chat-message-user"] : styles["chat-message"]
  784. }
  785. >
  786. <div className={styles["chat-message-container"]}>
  787. <div className={styles["chat-message-avatar"]}>
  788. {message.role === "user" ? (
  789. <Avatar avatar={config.avatar} />
  790. ) : (
  791. <MaskAvatar mask={session.mask} />
  792. )}
  793. </div>
  794. {showTyping && (
  795. <div className={styles["chat-message-status"]}>
  796. {Locale.Chat.Typing}
  797. </div>
  798. )}
  799. <div className={styles["chat-message-item"]}>
  800. {showActions && (
  801. <div className={styles["chat-message-top-actions"]}>
  802. {message.streaming ? (
  803. <div
  804. className={styles["chat-message-top-action"]}
  805. onClick={() => onUserStop(message.id ?? i)}
  806. >
  807. {Locale.Chat.Actions.Stop}
  808. </div>
  809. ) : (
  810. <>
  811. <div
  812. className={styles["chat-message-top-action"]}
  813. onClick={() => onDelete(message.id ?? i)}
  814. >
  815. {Locale.Chat.Actions.Delete}
  816. </div>
  817. <div
  818. className={styles["chat-message-top-action"]}
  819. onClick={() => onResend(message.id ?? i)}
  820. >
  821. {Locale.Chat.Actions.Retry}
  822. </div>
  823. </>
  824. )}
  825. <div
  826. className={styles["chat-message-top-action"]}
  827. onClick={() => copyToClipboard(message.content)}
  828. >
  829. {Locale.Chat.Actions.Copy}
  830. </div>
  831. </div>
  832. )}
  833. <Markdown
  834. content={message.content}
  835. loading={
  836. (message.preview || message.content.length === 0) &&
  837. !isUser
  838. }
  839. onContextMenu={(e) => onRightClick(e, message)}
  840. onDoubleClickCapture={() => {
  841. if (!isMobileScreen) return;
  842. setUserInput(message.content);
  843. }}
  844. fontSize={fontSize}
  845. parentRef={scrollRef}
  846. defaultShow={i >= messages.length - 10}
  847. />
  848. </div>
  849. {!isUser && !message.preview && (
  850. <div className={styles["chat-message-actions"]}>
  851. <div className={styles["chat-message-action-date"]}>
  852. {message.date.toLocaleString()}
  853. </div>
  854. </div>
  855. )}
  856. </div>
  857. </div>
  858. {shouldShowClearContextDivider && <ClearContextDivider />}
  859. </>
  860. );
  861. })}
  862. </div>
  863. <div className={styles["chat-input-panel"]}>
  864. <PromptHints prompts={promptHints} onPromptSelect={onPromptSelect} />
  865. <ChatActions
  866. showPromptModal={() => setShowPromptModal(true)}
  867. scrollToBottom={scrollToBottom}
  868. hitBottom={hitBottom}
  869. showPromptHints={() => {
  870. // Click again to close
  871. if (promptHints.length > 0) {
  872. setPromptHints([]);
  873. return;
  874. }
  875. inputRef.current?.focus();
  876. setUserInput("/");
  877. onSearch("");
  878. }}
  879. />
  880. <div className={styles["chat-input-panel-inner"]}>
  881. <textarea
  882. ref={inputRef}
  883. className={styles["chat-input"]}
  884. placeholder={Locale.Chat.Input(submitKey)}
  885. onInput={(e) => onInput(e.currentTarget.value)}
  886. value={userInput}
  887. onKeyDown={onInputKeyDown}
  888. onFocus={() => setAutoScroll(true)}
  889. onBlur={() => setAutoScroll(false)}
  890. rows={inputRows}
  891. autoFocus={autoFocus}
  892. style={{
  893. fontSize: config.fontSize,
  894. }}
  895. />
  896. <IconButton
  897. icon={<SendWhiteIcon />}
  898. text={Locale.Chat.Send}
  899. className={styles["chat-input-send"]}
  900. type="primary"
  901. onClick={() => doSubmit(userInput)}
  902. />
  903. </div>
  904. </div>
  905. {showExport && (
  906. <ExportMessageModal onClose={() => setShowExport(false)} />
  907. )}
  908. </div>
  909. );
  910. }