chat.tsx 36 KB

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