chat.tsx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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 and resend
  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. let content = message.content;
  670. if (message.role === "assistant" && message.id) {
  671. const userIndex = findLastUserIndex(message.id);
  672. if (userIndex) {
  673. content = session.messages.at(userIndex)?.content ?? content;
  674. }
  675. }
  676. setIsLoading(true);
  677. chatStore.onUserInput(content).then(() => setIsLoading(false));
  678. inputRef.current?.focus();
  679. };
  680. const onPinMessage = (message: ChatMessage) => {
  681. chatStore.updateCurrentSession((session) =>
  682. session.mask.context.push(message),
  683. );
  684. showToast(Locale.Chat.Actions.PinToastContent, {
  685. text: Locale.Chat.Actions.PinToastAction,
  686. onClick: () => {
  687. setShowPromptModal(true);
  688. },
  689. });
  690. };
  691. const context: RenderMessage[] = session.mask.hideContext
  692. ? []
  693. : session.mask.context.slice();
  694. const accessStore = useAccessStore();
  695. if (
  696. context.length === 0 &&
  697. session.messages.at(0)?.content !== BOT_HELLO.content
  698. ) {
  699. const copiedHello = Object.assign({}, BOT_HELLO);
  700. if (!accessStore.isAuthorized()) {
  701. copiedHello.content = Locale.Error.Unauthorized;
  702. }
  703. context.push(copiedHello);
  704. }
  705. // clear context index = context length + index in messages
  706. const clearContextIndex =
  707. (session.clearContextIndex ?? -1) >= 0
  708. ? session.clearContextIndex! + context.length
  709. : -1;
  710. // preview messages
  711. const messages = context
  712. .concat(session.messages as RenderMessage[])
  713. .concat(
  714. isLoading
  715. ? [
  716. {
  717. ...createMessage({
  718. role: "assistant",
  719. content: "……",
  720. }),
  721. preview: true,
  722. },
  723. ]
  724. : [],
  725. )
  726. .concat(
  727. userInput.length > 0 && config.sendPreviewBubble
  728. ? [
  729. {
  730. ...createMessage({
  731. role: "user",
  732. content: userInput,
  733. }),
  734. preview: true,
  735. },
  736. ]
  737. : [],
  738. );
  739. const [showPromptModal, setShowPromptModal] = useState(false);
  740. const renameSession = () => {
  741. showPrompt(Locale.Chat.Rename, session.topic).then((newTopic) => {
  742. if (newTopic && newTopic !== session.topic) {
  743. chatStore.updateCurrentSession(
  744. (session) => (session.topic = newTopic!),
  745. );
  746. }
  747. });
  748. };
  749. const clientConfig = useMemo(() => getClientConfig(), []);
  750. const location = useLocation();
  751. const isChat = location.pathname === Path.Chat;
  752. const autoFocus = !isMobileScreen || isChat; // only focus in chat page
  753. const showMaxIcon = !isMobileScreen && !clientConfig?.isApp;
  754. useCommand({
  755. fill: setUserInput,
  756. submit: (text) => {
  757. doSubmit(text);
  758. },
  759. });
  760. return (
  761. <div className={styles.chat} key={session.id}>
  762. <div className="window-header" data-tauri-drag-region>
  763. {isMobileScreen && (
  764. <div className="window-actions">
  765. <div className={"window-action-button"}>
  766. <IconButton
  767. icon={<ReturnIcon />}
  768. bordered
  769. title={Locale.Chat.Actions.ChatList}
  770. onClick={() => navigate(Path.Home)}
  771. />
  772. </div>
  773. </div>
  774. )}
  775. <div className={`window-header-title ${styles["chat-body-title"]}`}>
  776. <div
  777. className={`window-header-main-title ${styles["chat-body-main-title"]}`}
  778. onClickCapture={renameSession}
  779. >
  780. {!session.topic ? DEFAULT_TOPIC : session.topic}
  781. </div>
  782. <div className="window-header-sub-title">
  783. {Locale.Chat.SubTitle(session.messages.length)}
  784. </div>
  785. </div>
  786. <div className="window-actions">
  787. {!isMobileScreen && (
  788. <div className="window-action-button">
  789. <IconButton
  790. icon={<RenameIcon />}
  791. bordered
  792. onClick={renameSession}
  793. />
  794. </div>
  795. )}
  796. <div className="window-action-button">
  797. <IconButton
  798. icon={<ExportIcon />}
  799. bordered
  800. title={Locale.Chat.Actions.Export}
  801. onClick={() => {
  802. setShowExport(true);
  803. }}
  804. />
  805. </div>
  806. {showMaxIcon && (
  807. <div className="window-action-button">
  808. <IconButton
  809. icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
  810. bordered
  811. onClick={() => {
  812. config.update(
  813. (config) => (config.tightBorder = !config.tightBorder),
  814. );
  815. }}
  816. />
  817. </div>
  818. )}
  819. </div>
  820. <PromptToast
  821. showToast={!hitBottom}
  822. showModal={showPromptModal}
  823. setShowModal={setShowPromptModal}
  824. />
  825. </div>
  826. <div
  827. className={styles["chat-body"]}
  828. ref={scrollRef}
  829. onScroll={(e) => onChatBodyScroll(e.currentTarget)}
  830. onMouseDown={() => inputRef.current?.blur()}
  831. onWheel={(e) => setAutoScroll(hitBottom && e.deltaY > 0)}
  832. onTouchStart={() => {
  833. inputRef.current?.blur();
  834. setAutoScroll(false);
  835. }}
  836. >
  837. {messages.map((message, i) => {
  838. const isUser = message.role === "user";
  839. const isContext = i < context.length;
  840. const showActions =
  841. i > 0 &&
  842. !(message.preview || message.content.length === 0) &&
  843. !isContext;
  844. const showTyping = message.preview || message.streaming;
  845. const shouldShowClearContextDivider = i === clearContextIndex - 1;
  846. return (
  847. <>
  848. <div
  849. key={i}
  850. className={
  851. isUser ? styles["chat-message-user"] : styles["chat-message"]
  852. }
  853. >
  854. <div className={styles["chat-message-container"]}>
  855. <div className={styles["chat-message-header"]}>
  856. <div className={styles["chat-message-avatar"]}>
  857. <div className={styles["chat-message-edit"]}>
  858. <IconButton
  859. icon={<EditIcon />}
  860. onClick={async () => {
  861. const newMessage = await showPrompt(
  862. Locale.Chat.Actions.Edit,
  863. message.content,
  864. 10,
  865. );
  866. chatStore.updateCurrentSession((session) => {
  867. const m = session.messages.find(
  868. (m) => m.id === message.id,
  869. );
  870. if (m) {
  871. m.content = newMessage;
  872. }
  873. });
  874. }}
  875. ></IconButton>
  876. </div>
  877. {isUser ? (
  878. <Avatar avatar={config.avatar} />
  879. ) : (
  880. <MaskAvatar mask={session.mask} />
  881. )}
  882. </div>
  883. {showActions && (
  884. <div className={styles["chat-message-actions"]}>
  885. <div className={styles["chat-input-actions"]}>
  886. {message.streaming ? (
  887. <ChatAction
  888. text={Locale.Chat.Actions.Stop}
  889. icon={<StopIcon />}
  890. onClick={() => onUserStop(message.id ?? i)}
  891. />
  892. ) : (
  893. <>
  894. <ChatAction
  895. text={Locale.Chat.Actions.Retry}
  896. icon={<ResetIcon />}
  897. onClick={() => onResend(message)}
  898. />
  899. <ChatAction
  900. text={Locale.Chat.Actions.Delete}
  901. icon={<DeleteIcon />}
  902. onClick={() => onDelete(message.id ?? i)}
  903. />
  904. <ChatAction
  905. text={Locale.Chat.Actions.Pin}
  906. icon={<PinIcon />}
  907. onClick={() => onPinMessage(message)}
  908. />
  909. <ChatAction
  910. text={Locale.Chat.Actions.Copy}
  911. icon={<CopyIcon />}
  912. onClick={() => copyToClipboard(message.content)}
  913. />
  914. </>
  915. )}
  916. </div>
  917. </div>
  918. )}
  919. </div>
  920. {showTyping && (
  921. <div className={styles["chat-message-status"]}>
  922. {Locale.Chat.Typing}
  923. </div>
  924. )}
  925. <div className={styles["chat-message-item"]}>
  926. <Markdown
  927. content={message.content}
  928. loading={
  929. (message.preview || message.content.length === 0) &&
  930. !isUser
  931. }
  932. onContextMenu={(e) => onRightClick(e, message)}
  933. onDoubleClickCapture={() => {
  934. if (!isMobileScreen) return;
  935. setUserInput(message.content);
  936. }}
  937. fontSize={fontSize}
  938. parentRef={scrollRef}
  939. defaultShow={i >= messages.length - 10}
  940. />
  941. </div>
  942. <div className={styles["chat-message-action-date"]}>
  943. {isContext
  944. ? Locale.Chat.IsContext
  945. : message.date.toLocaleString()}
  946. </div>
  947. </div>
  948. </div>
  949. {shouldShowClearContextDivider && <ClearContextDivider />}
  950. </>
  951. );
  952. })}
  953. </div>
  954. <div className={styles["chat-input-panel"]}>
  955. <PromptHints prompts={promptHints} onPromptSelect={onPromptSelect} />
  956. <ChatActions
  957. showPromptModal={() => setShowPromptModal(true)}
  958. scrollToBottom={scrollToBottom}
  959. hitBottom={hitBottom}
  960. showPromptHints={() => {
  961. // Click again to close
  962. if (promptHints.length > 0) {
  963. setPromptHints([]);
  964. return;
  965. }
  966. inputRef.current?.focus();
  967. setUserInput("/");
  968. onSearch("");
  969. }}
  970. />
  971. <div className={styles["chat-input-panel-inner"]}>
  972. <textarea
  973. ref={inputRef}
  974. className={styles["chat-input"]}
  975. placeholder={Locale.Chat.Input(submitKey)}
  976. onInput={(e) => onInput(e.currentTarget.value)}
  977. value={userInput}
  978. onKeyDown={onInputKeyDown}
  979. onFocus={() => setAutoScroll(true)}
  980. onBlur={() => setAutoScroll(false)}
  981. rows={inputRows}
  982. autoFocus={autoFocus}
  983. style={{
  984. fontSize: config.fontSize,
  985. }}
  986. />
  987. <IconButton
  988. icon={<SendWhiteIcon />}
  989. text={Locale.Chat.Send}
  990. className={styles["chat-input-send"]}
  991. type="primary"
  992. onClick={() => doSubmit(userInput)}
  993. />
  994. </div>
  995. </div>
  996. {showExport && (
  997. <ExportMessageModal onClose={() => setShowExport(false)} />
  998. )}
  999. </div>
  1000. );
  1001. }