chat.tsx 32 KB

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