chat.tsx 31 KB

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