chat.tsx 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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 LightIcon from "../icons/light.svg";
  26. import DarkIcon from "../icons/dark.svg";
  27. import AutoIcon from "../icons/auto.svg";
  28. import BottomIcon from "../icons/bottom.svg";
  29. import StopIcon from "../icons/pause.svg";
  30. import RobotIcon from "../icons/robot.svg";
  31. import {
  32. ChatMessage,
  33. SubmitKey,
  34. useChatStore,
  35. BOT_HELLO,
  36. createMessage,
  37. useAccessStore,
  38. Theme,
  39. useAppConfig,
  40. DEFAULT_TOPIC,
  41. ALL_MODELS,
  42. } from "../store";
  43. import {
  44. copyToClipboard,
  45. downloadAs,
  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, 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 = ALL_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;
  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. const newTopic = prompt(Locale.Chat.Rename, session.topic);
  705. if (newTopic && newTopic !== session.topic) {
  706. chatStore.updateCurrentSession((session) => (session.topic = newTopic!));
  707. }
  708. };
  709. const clientConfig = useMemo(() => getClientConfig(), []);
  710. const location = useLocation();
  711. const isChat = location.pathname === Path.Chat;
  712. const autoFocus = !isMobileScreen || isChat; // only focus in chat page
  713. const showMaxIcon = !isMobileScreen && !clientConfig?.isApp;
  714. useCommand({
  715. fill: setUserInput,
  716. submit: (text) => {
  717. doSubmit(text);
  718. },
  719. });
  720. return (
  721. <div className={styles.chat} key={session.id}>
  722. <div className="window-header" data-tauri-drag-region>
  723. {isMobileScreen && (
  724. <div className="window-actions">
  725. <div className={"window-action-button"}>
  726. <IconButton
  727. icon={<ReturnIcon />}
  728. bordered
  729. title={Locale.Chat.Actions.ChatList}
  730. onClick={() => navigate(Path.Home)}
  731. />
  732. </div>
  733. </div>
  734. )}
  735. <div className={`window-header-title ${styles["chat-body-title"]}`}>
  736. <div
  737. className={`window-header-main-title ${styles["chat-body-main-title"]}`}
  738. onClickCapture={renameSession}
  739. >
  740. {!session.topic ? DEFAULT_TOPIC : session.topic}
  741. </div>
  742. <div className="window-header-sub-title">
  743. {Locale.Chat.SubTitle(session.messages.length)}
  744. </div>
  745. </div>
  746. <div className="window-actions">
  747. {!isMobileScreen && (
  748. <div className="window-action-button">
  749. <IconButton
  750. icon={<RenameIcon />}
  751. bordered
  752. onClick={renameSession}
  753. />
  754. </div>
  755. )}
  756. <div className="window-action-button">
  757. <IconButton
  758. icon={<ExportIcon />}
  759. bordered
  760. title={Locale.Chat.Actions.Export}
  761. onClick={() => {
  762. setShowExport(true);
  763. }}
  764. />
  765. </div>
  766. {showMaxIcon && (
  767. <div className="window-action-button">
  768. <IconButton
  769. icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
  770. bordered
  771. onClick={() => {
  772. config.update(
  773. (config) => (config.tightBorder = !config.tightBorder),
  774. );
  775. }}
  776. />
  777. </div>
  778. )}
  779. </div>
  780. <PromptToast
  781. showToast={!hitBottom}
  782. showModal={showPromptModal}
  783. setShowModal={setShowPromptModal}
  784. />
  785. </div>
  786. <div
  787. className={styles["chat-body"]}
  788. ref={scrollRef}
  789. onScroll={(e) => onChatBodyScroll(e.currentTarget)}
  790. onMouseDown={() => inputRef.current?.blur()}
  791. onWheel={(e) => setAutoScroll(hitBottom && e.deltaY > 0)}
  792. onTouchStart={() => {
  793. inputRef.current?.blur();
  794. setAutoScroll(false);
  795. }}
  796. >
  797. {messages.map((message, i) => {
  798. const isUser = message.role === "user";
  799. const showActions =
  800. !isUser &&
  801. i > 0 &&
  802. !(message.preview || message.content.length === 0);
  803. const showTyping = message.preview || message.streaming;
  804. const shouldShowClearContextDivider = i === clearContextIndex - 1;
  805. return (
  806. <>
  807. <div
  808. key={i}
  809. className={
  810. isUser ? styles["chat-message-user"] : styles["chat-message"]
  811. }
  812. >
  813. <div className={styles["chat-message-container"]}>
  814. <div className={styles["chat-message-avatar"]}>
  815. {message.role === "user" ? (
  816. <Avatar avatar={config.avatar} />
  817. ) : (
  818. <MaskAvatar mask={session.mask} />
  819. )}
  820. </div>
  821. {showTyping && (
  822. <div className={styles["chat-message-status"]}>
  823. {Locale.Chat.Typing}
  824. </div>
  825. )}
  826. <div className={styles["chat-message-item"]}>
  827. <Markdown
  828. content={message.content}
  829. loading={
  830. (message.preview || message.content.length === 0) &&
  831. !isUser
  832. }
  833. onContextMenu={(e) => onRightClick(e, message)}
  834. onDoubleClickCapture={() => {
  835. if (!isMobileScreen) return;
  836. setUserInput(message.content);
  837. }}
  838. fontSize={fontSize}
  839. parentRef={scrollRef}
  840. defaultShow={i >= messages.length - 10}
  841. />
  842. {showActions && (
  843. <div className={styles["chat-message-actions"]}>
  844. <div
  845. className={styles["chat-input-actions"]}
  846. style={{
  847. marginTop: 10,
  848. marginBottom: 0,
  849. }}
  850. >
  851. {message.streaming ? (
  852. <ChatAction
  853. text={Locale.Chat.Actions.Stop}
  854. icon={<StopIcon />}
  855. onClick={() => onUserStop(message.id ?? i)}
  856. />
  857. ) : (
  858. <>
  859. <ChatAction
  860. text={Locale.Chat.Actions.Retry}
  861. icon={<ResetIcon />}
  862. onClick={() => onResend(message.id ?? i)}
  863. />
  864. <ChatAction
  865. text={Locale.Chat.Actions.Delete}
  866. icon={<DeleteIcon />}
  867. onClick={() => onDelete(message.id ?? i)}
  868. />
  869. <ChatAction
  870. text={Locale.Chat.Actions.Pin}
  871. icon={<PinIcon />}
  872. onClick={() => onPinMessage(message)}
  873. />
  874. <ChatAction
  875. text={Locale.Chat.Actions.Copy}
  876. icon={<CopyIcon />}
  877. onClick={() => copyToClipboard(message.content)}
  878. />
  879. </>
  880. )}
  881. </div>
  882. <div className={styles["chat-message-action-date"]}>
  883. {message.date.toLocaleString()}
  884. </div>
  885. </div>
  886. )}
  887. </div>
  888. </div>
  889. </div>
  890. {shouldShowClearContextDivider && <ClearContextDivider />}
  891. </>
  892. );
  893. })}
  894. </div>
  895. <div className={styles["chat-input-panel"]}>
  896. <PromptHints prompts={promptHints} onPromptSelect={onPromptSelect} />
  897. <ChatActions
  898. showPromptModal={() => setShowPromptModal(true)}
  899. scrollToBottom={scrollToBottom}
  900. hitBottom={hitBottom}
  901. showPromptHints={() => {
  902. // Click again to close
  903. if (promptHints.length > 0) {
  904. setPromptHints([]);
  905. return;
  906. }
  907. inputRef.current?.focus();
  908. setUserInput("/");
  909. onSearch("");
  910. }}
  911. />
  912. <div className={styles["chat-input-panel-inner"]}>
  913. <textarea
  914. ref={inputRef}
  915. className={styles["chat-input"]}
  916. placeholder={Locale.Chat.Input(submitKey)}
  917. onInput={(e) => onInput(e.currentTarget.value)}
  918. value={userInput}
  919. onKeyDown={onInputKeyDown}
  920. onFocus={() => setAutoScroll(true)}
  921. onBlur={() => setAutoScroll(false)}
  922. rows={inputRows}
  923. autoFocus={autoFocus}
  924. style={{
  925. fontSize: config.fontSize,
  926. }}
  927. />
  928. <IconButton
  929. icon={<SendWhiteIcon />}
  930. text={Locale.Chat.Send}
  931. className={styles["chat-input-send"]}
  932. type="primary"
  933. onClick={() => doSubmit(userInput)}
  934. />
  935. </div>
  936. </div>
  937. {showExport && (
  938. <ExportMessageModal onClose={() => setShowExport(false)} />
  939. )}
  940. </div>
  941. );
  942. }