chat.tsx 32 KB

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