chat.tsx 32 KB

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