chat.tsx 33 KB

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