chat.tsx 34 KB

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