chat.tsx 38 KB

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