chat.tsx 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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 = useRef(true);
  341. const scrollToBottom = useCallback(() => {
  342. const dom = scrollRef.current;
  343. if (dom) {
  344. requestAnimationFrame(() => dom.scrollTo(0, dom.scrollHeight));
  345. }
  346. }, []);
  347. const setAutoScroll = (enable: boolean) => {
  348. autoScroll.current = enable;
  349. };
  350. // auto scroll
  351. useEffect(() => {
  352. const intervalId = setInterval(() => {
  353. if (autoScroll.current) {
  354. scrollToBottom();
  355. }
  356. }, 30);
  357. return () => clearInterval(intervalId);
  358. // eslint-disable-next-line react-hooks/exhaustive-deps
  359. }, []);
  360. return {
  361. scrollRef,
  362. autoScroll,
  363. setAutoScroll,
  364. scrollToBottom,
  365. };
  366. }
  367. export function ChatActions(props: {
  368. showPromptModal: () => void;
  369. scrollToBottom: () => void;
  370. showPromptHints: () => void;
  371. hitBottom: boolean;
  372. }) {
  373. const config = useAppConfig();
  374. const navigate = useNavigate();
  375. const chatStore = useChatStore();
  376. // switch themes
  377. const theme = config.theme;
  378. function nextTheme() {
  379. const themes = [Theme.Auto, Theme.Light, Theme.Dark];
  380. const themeIndex = themes.indexOf(theme);
  381. const nextIndex = (themeIndex + 1) % themes.length;
  382. const nextTheme = themes[nextIndex];
  383. config.update((config) => (config.theme = nextTheme));
  384. }
  385. // stop all responses
  386. const couldStop = ChatControllerPool.hasPending();
  387. const stopAll = () => ChatControllerPool.stopAll();
  388. // switch model
  389. const currentModel = chatStore.currentSession().mask.modelConfig.model;
  390. const models = useMemo(
  391. () =>
  392. config
  393. .allModels()
  394. .filter((m) => m.available)
  395. .map((m) => m.name),
  396. [config],
  397. );
  398. const [showModelSelector, setShowModelSelector] = useState(false);
  399. return (
  400. <div className={styles["chat-input-actions"]}>
  401. {couldStop && (
  402. <ChatAction
  403. onClick={stopAll}
  404. text={Locale.Chat.InputActions.Stop}
  405. icon={<StopIcon />}
  406. />
  407. )}
  408. {!props.hitBottom && (
  409. <ChatAction
  410. onClick={props.scrollToBottom}
  411. text={Locale.Chat.InputActions.ToBottom}
  412. icon={<BottomIcon />}
  413. />
  414. )}
  415. {props.hitBottom && (
  416. <ChatAction
  417. onClick={props.showPromptModal}
  418. text={Locale.Chat.InputActions.Settings}
  419. icon={<SettingsIcon />}
  420. />
  421. )}
  422. <ChatAction
  423. onClick={nextTheme}
  424. text={Locale.Chat.InputActions.Theme[theme]}
  425. icon={
  426. <>
  427. {theme === Theme.Auto ? (
  428. <AutoIcon />
  429. ) : theme === Theme.Light ? (
  430. <LightIcon />
  431. ) : theme === Theme.Dark ? (
  432. <DarkIcon />
  433. ) : null}
  434. </>
  435. }
  436. />
  437. <ChatAction
  438. onClick={props.showPromptHints}
  439. text={Locale.Chat.InputActions.Prompt}
  440. icon={<PromptIcon />}
  441. />
  442. <ChatAction
  443. onClick={() => {
  444. navigate(Path.Masks);
  445. }}
  446. text={Locale.Chat.InputActions.Masks}
  447. icon={<MaskIcon />}
  448. />
  449. <ChatAction
  450. text={Locale.Chat.InputActions.Clear}
  451. icon={<BreakIcon />}
  452. onClick={() => {
  453. chatStore.updateCurrentSession((session) => {
  454. if (session.clearContextIndex === session.messages.length) {
  455. session.clearContextIndex = undefined;
  456. } else {
  457. session.clearContextIndex = session.messages.length;
  458. session.memoryPrompt = ""; // will clear memory
  459. }
  460. });
  461. }}
  462. />
  463. <ChatAction
  464. onClick={() => setShowModelSelector(true)}
  465. text={currentModel}
  466. icon={<RobotIcon />}
  467. />
  468. {showModelSelector && (
  469. <Selector
  470. defaultSelectedValue={currentModel}
  471. items={models.map((m) => ({
  472. title: m,
  473. value: m,
  474. }))}
  475. onClose={() => setShowModelSelector(false)}
  476. onSelection={(s) => {
  477. if (s.length === 0) return;
  478. chatStore.updateCurrentSession((session) => {
  479. session.mask.modelConfig.model = s[0] as ModelType;
  480. session.mask.syncGlobalConfig = false;
  481. });
  482. showToast(s[0]);
  483. }}
  484. />
  485. )}
  486. </div>
  487. );
  488. }
  489. export function EditMessageModal(props: { onClose: () => void }) {
  490. const chatStore = useChatStore();
  491. const session = chatStore.currentSession();
  492. const [messages, setMessages] = useState(session.messages.slice());
  493. return (
  494. <div className="modal-mask">
  495. <Modal
  496. title={Locale.Chat.EditMessage.Title}
  497. onClose={props.onClose}
  498. actions={[
  499. <IconButton
  500. text={Locale.UI.Cancel}
  501. icon={<CancelIcon />}
  502. key="cancel"
  503. onClick={() => {
  504. props.onClose();
  505. }}
  506. />,
  507. <IconButton
  508. type="primary"
  509. text={Locale.UI.Confirm}
  510. icon={<ConfirmIcon />}
  511. key="ok"
  512. onClick={() => {
  513. chatStore.updateCurrentSession(
  514. (session) => (session.messages = messages),
  515. );
  516. props.onClose();
  517. }}
  518. />,
  519. ]}
  520. >
  521. <List>
  522. <ListItem
  523. title={Locale.Chat.EditMessage.Topic.Title}
  524. subTitle={Locale.Chat.EditMessage.Topic.SubTitle}
  525. >
  526. <input
  527. type="text"
  528. value={session.topic}
  529. onInput={(e) =>
  530. chatStore.updateCurrentSession(
  531. (session) => (session.topic = e.currentTarget.value),
  532. )
  533. }
  534. ></input>
  535. </ListItem>
  536. </List>
  537. <ContextPrompts
  538. context={messages}
  539. updateContext={(updater) => {
  540. const newMessages = messages.slice();
  541. updater(newMessages);
  542. setMessages(newMessages);
  543. }}
  544. />
  545. </Modal>
  546. </div>
  547. );
  548. }
  549. export function Chat() {
  550. type RenderMessage = ChatMessage & { preview?: boolean };
  551. const chatStore = useChatStore();
  552. const session = chatStore.currentSession();
  553. const config = useAppConfig();
  554. const fontSize = config.fontSize;
  555. const [showExport, setShowExport] = useState(false);
  556. const inputRef = useRef<HTMLTextAreaElement>(null);
  557. const [userInput, setUserInput] = useState("");
  558. const [isLoading, setIsLoading] = useState(false);
  559. const { submitKey, shouldSubmit } = useSubmitHandler();
  560. const { scrollRef, setAutoScroll, scrollToBottom } = useScrollToBottom();
  561. const [hitBottom, setHitBottom] = useState(true);
  562. const isMobileScreen = useMobileScreen();
  563. const navigate = useNavigate();
  564. const lastBodyScroolTop = useRef(0);
  565. const onChatBodyScroll = (e: HTMLElement) => {
  566. const isTouchBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 10;
  567. setHitBottom(isTouchBottom);
  568. // only enable auto scroll when scroll down and touched bottom
  569. setAutoScroll(e.scrollTop >= lastBodyScroolTop.current && isTouchBottom);
  570. lastBodyScroolTop.current = e.scrollTop;
  571. };
  572. // prompt hints
  573. const promptStore = usePromptStore();
  574. const [promptHints, setPromptHints] = useState<RenderPompt[]>([]);
  575. const onSearch = useDebouncedCallback(
  576. (text: string) => {
  577. const matchedPrompts = promptStore.search(text);
  578. setPromptHints(matchedPrompts);
  579. },
  580. 100,
  581. { leading: true, trailing: true },
  582. );
  583. // auto grow input
  584. const [inputRows, setInputRows] = useState(2);
  585. const measure = useDebouncedCallback(
  586. () => {
  587. const rows = inputRef.current ? autoGrowTextArea(inputRef.current) : 1;
  588. const inputRows = Math.min(
  589. 20,
  590. Math.max(2 + Number(!isMobileScreen), rows),
  591. );
  592. setInputRows(inputRows);
  593. },
  594. 100,
  595. {
  596. leading: true,
  597. trailing: true,
  598. },
  599. );
  600. // eslint-disable-next-line react-hooks/exhaustive-deps
  601. useEffect(measure, [userInput]);
  602. // chat commands shortcuts
  603. const chatCommands = useChatCommand({
  604. new: () => chatStore.newSession(),
  605. newm: () => navigate(Path.NewChat),
  606. prev: () => chatStore.nextSession(-1),
  607. next: () => chatStore.nextSession(1),
  608. clear: () =>
  609. chatStore.updateCurrentSession(
  610. (session) => (session.clearContextIndex = session.messages.length),
  611. ),
  612. del: () => chatStore.deleteSession(chatStore.currentSessionIndex),
  613. });
  614. // only search prompts when user input is short
  615. const SEARCH_TEXT_LIMIT = 30;
  616. const onInput = (text: string) => {
  617. setUserInput(text);
  618. const n = text.trim().length;
  619. // clear search results
  620. if (n === 0) {
  621. setPromptHints([]);
  622. } else if (text.startsWith(ChatCommandPrefix)) {
  623. setPromptHints(chatCommands.search(text));
  624. } else if (!config.disablePromptHint && n < SEARCH_TEXT_LIMIT) {
  625. // check if need to trigger auto completion
  626. if (text.startsWith("/")) {
  627. let searchText = text.slice(1);
  628. onSearch(searchText);
  629. }
  630. }
  631. };
  632. const doSubmit = (userInput: string) => {
  633. if (userInput.trim() === "") return;
  634. const matchCommand = chatCommands.match(userInput);
  635. if (matchCommand.matched) {
  636. setUserInput("");
  637. setPromptHints([]);
  638. matchCommand.invoke();
  639. return;
  640. }
  641. setIsLoading(true);
  642. chatStore.onUserInput(userInput).then(() => setIsLoading(false));
  643. localStorage.setItem(LAST_INPUT_KEY, userInput);
  644. setUserInput("");
  645. setPromptHints([]);
  646. if (!isMobileScreen) inputRef.current?.focus();
  647. setAutoScroll(true);
  648. };
  649. const onPromptSelect = (prompt: RenderPompt) => {
  650. setTimeout(() => {
  651. setPromptHints([]);
  652. const matchedChatCommand = chatCommands.match(prompt.content);
  653. if (matchedChatCommand.matched) {
  654. // if user is selecting a chat command, just trigger it
  655. matchedChatCommand.invoke();
  656. setUserInput("");
  657. } else {
  658. // or fill the prompt
  659. setUserInput(prompt.content);
  660. }
  661. inputRef.current?.focus();
  662. }, 30);
  663. };
  664. // stop response
  665. const onUserStop = (messageId: string) => {
  666. ChatControllerPool.stop(session.id, messageId);
  667. };
  668. useEffect(() => {
  669. chatStore.updateCurrentSession((session) => {
  670. const stopTiming = Date.now() - REQUEST_TIMEOUT_MS;
  671. session.messages.forEach((m) => {
  672. // check if should stop all stale messages
  673. if (m.isError || new Date(m.date).getTime() < stopTiming) {
  674. if (m.streaming) {
  675. m.streaming = false;
  676. }
  677. if (m.content.length === 0) {
  678. m.isError = true;
  679. m.content = prettyObject({
  680. error: true,
  681. message: "empty response",
  682. });
  683. }
  684. }
  685. });
  686. // auto sync mask config from global config
  687. if (session.mask.syncGlobalConfig) {
  688. console.log("[Mask] syncing from global, name = ", session.mask.name);
  689. session.mask.modelConfig = { ...config.modelConfig };
  690. }
  691. });
  692. // eslint-disable-next-line react-hooks/exhaustive-deps
  693. }, []);
  694. // check if should send message
  695. const onInputKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  696. // if ArrowUp and no userInput, fill with last input
  697. if (
  698. e.key === "ArrowUp" &&
  699. userInput.length <= 0 &&
  700. !(e.metaKey || e.altKey || e.ctrlKey)
  701. ) {
  702. setUserInput(localStorage.getItem(LAST_INPUT_KEY) ?? "");
  703. e.preventDefault();
  704. return;
  705. }
  706. if (shouldSubmit(e) && promptHints.length === 0) {
  707. doSubmit(userInput);
  708. e.preventDefault();
  709. }
  710. };
  711. const onRightClick = (e: any, message: ChatMessage) => {
  712. // copy to clipboard
  713. if (selectOrCopy(e.currentTarget, message.content)) {
  714. if (userInput.length === 0) {
  715. setUserInput(message.content);
  716. }
  717. e.preventDefault();
  718. }
  719. };
  720. const deleteMessage = (msgId?: string) => {
  721. chatStore.updateCurrentSession(
  722. (session) =>
  723. (session.messages = session.messages.filter((m) => m.id !== msgId)),
  724. );
  725. };
  726. const onDelete = (msgId: string) => {
  727. deleteMessage(msgId);
  728. };
  729. const onResend = (message: ChatMessage) => {
  730. // when it is resending a message
  731. // 1. for a user's message, find the next bot response
  732. // 2. for a bot's message, find the last user's input
  733. // 3. delete original user input and bot's message
  734. // 4. resend the user's input
  735. const resendingIndex = session.messages.findIndex(
  736. (m) => m.id === message.id,
  737. );
  738. if (resendingIndex <= 0 || resendingIndex >= session.messages.length) {
  739. console.error("[Chat] failed to find resending message", message);
  740. return;
  741. }
  742. let userMessage: ChatMessage | undefined;
  743. let botMessage: ChatMessage | undefined;
  744. if (message.role === "assistant") {
  745. // if it is resending a bot's message, find the user input for it
  746. botMessage = message;
  747. for (let i = resendingIndex; i >= 0; i -= 1) {
  748. if (session.messages[i].role === "user") {
  749. userMessage = session.messages[i];
  750. break;
  751. }
  752. }
  753. } else if (message.role === "user") {
  754. // if it is resending a user's input, find the bot's response
  755. userMessage = message;
  756. for (let i = resendingIndex; i < session.messages.length; i += 1) {
  757. if (session.messages[i].role === "assistant") {
  758. botMessage = session.messages[i];
  759. break;
  760. }
  761. }
  762. }
  763. if (userMessage === undefined) {
  764. console.error("[Chat] failed to resend", message);
  765. return;
  766. }
  767. // delete the original messages
  768. deleteMessage(userMessage.id);
  769. deleteMessage(botMessage?.id);
  770. // resend the message
  771. setIsLoading(true);
  772. chatStore.onUserInput(userMessage.content).then(() => setIsLoading(false));
  773. inputRef.current?.focus();
  774. };
  775. const onPinMessage = (message: ChatMessage) => {
  776. chatStore.updateCurrentSession((session) =>
  777. session.mask.context.push(message),
  778. );
  779. showToast(Locale.Chat.Actions.PinToastContent, {
  780. text: Locale.Chat.Actions.PinToastAction,
  781. onClick: () => {
  782. setShowPromptModal(true);
  783. },
  784. });
  785. };
  786. const context: RenderMessage[] = session.mask.hideContext
  787. ? []
  788. : session.mask.context.slice();
  789. const accessStore = useAccessStore();
  790. if (
  791. context.length === 0 &&
  792. session.messages.at(0)?.content !== BOT_HELLO.content
  793. ) {
  794. const copiedHello = Object.assign({}, BOT_HELLO);
  795. if (!accessStore.isAuthorized()) {
  796. copiedHello.content = Locale.Error.Unauthorized;
  797. }
  798. context.push(copiedHello);
  799. }
  800. // clear context index = context length + index in messages
  801. const clearContextIndex =
  802. (session.clearContextIndex ?? -1) >= 0
  803. ? session.clearContextIndex! + context.length
  804. : -1;
  805. // preview messages
  806. const messages = context
  807. .concat(session.messages as RenderMessage[])
  808. .concat(
  809. isLoading
  810. ? [
  811. {
  812. ...createMessage({
  813. role: "assistant",
  814. content: "……",
  815. }),
  816. preview: true,
  817. },
  818. ]
  819. : [],
  820. )
  821. .concat(
  822. userInput.length > 0 && config.sendPreviewBubble
  823. ? [
  824. {
  825. ...createMessage({
  826. role: "user",
  827. content: userInput,
  828. }),
  829. preview: true,
  830. },
  831. ]
  832. : [],
  833. );
  834. const [showPromptModal, setShowPromptModal] = useState(false);
  835. const clientConfig = useMemo(() => getClientConfig(), []);
  836. const location = useLocation();
  837. const isChat = location.pathname === Path.Chat;
  838. const autoFocus = !isMobileScreen || isChat; // only focus in chat page
  839. const showMaxIcon = !isMobileScreen && !clientConfig?.isApp;
  840. useCommand({
  841. fill: setUserInput,
  842. submit: (text) => {
  843. doSubmit(text);
  844. },
  845. code: (text) => {
  846. console.log("[Command] got code from url: ", text);
  847. showConfirm(Locale.URLCommand.Code + `code = ${text}`).then((res) => {
  848. if (res) {
  849. accessStore.updateCode(text);
  850. }
  851. });
  852. },
  853. settings: (text) => {
  854. try {
  855. const payload = JSON.parse(text) as {
  856. key?: string;
  857. url?: string;
  858. };
  859. console.log("[Command] got settings from url: ", payload);
  860. if (payload.key || payload.url) {
  861. showConfirm(
  862. Locale.URLCommand.Settings +
  863. `\n${JSON.stringify(payload, null, 4)}`,
  864. ).then((res) => {
  865. if (!res) return;
  866. if (payload.key) {
  867. accessStore.updateToken(payload.key);
  868. }
  869. if (payload.url) {
  870. accessStore.updateOpenAiUrl(payload.url);
  871. }
  872. });
  873. }
  874. } catch {
  875. console.error("[Command] failed to get settings from url: ", text);
  876. }
  877. },
  878. });
  879. // edit / insert message modal
  880. const [isEditingMessage, setIsEditingMessage] = useState(false);
  881. return (
  882. <div className={styles.chat} key={session.id}>
  883. <div className="window-header" data-tauri-drag-region>
  884. {isMobileScreen && (
  885. <div className="window-actions">
  886. <div className={"window-action-button"}>
  887. <IconButton
  888. icon={<ReturnIcon />}
  889. bordered
  890. title={Locale.Chat.Actions.ChatList}
  891. onClick={() => navigate(Path.Home)}
  892. />
  893. </div>
  894. </div>
  895. )}
  896. <div className={`window-header-title ${styles["chat-body-title"]}`}>
  897. <div
  898. className={`window-header-main-title ${styles["chat-body-main-title"]}`}
  899. onClickCapture={() => setIsEditingMessage(true)}
  900. >
  901. {!session.topic ? DEFAULT_TOPIC : session.topic}
  902. </div>
  903. <div className="window-header-sub-title">
  904. {Locale.Chat.SubTitle(session.messages.length)}
  905. </div>
  906. </div>
  907. <div className="window-actions">
  908. {!isMobileScreen && (
  909. <div className="window-action-button">
  910. <IconButton
  911. icon={<RenameIcon />}
  912. bordered
  913. onClick={() => setIsEditingMessage(true)}
  914. />
  915. </div>
  916. )}
  917. <div className="window-action-button">
  918. <IconButton
  919. icon={<ExportIcon />}
  920. bordered
  921. title={Locale.Chat.Actions.Export}
  922. onClick={() => {
  923. setShowExport(true);
  924. }}
  925. />
  926. </div>
  927. {showMaxIcon && (
  928. <div className="window-action-button">
  929. <IconButton
  930. icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
  931. bordered
  932. onClick={() => {
  933. config.update(
  934. (config) => (config.tightBorder = !config.tightBorder),
  935. );
  936. }}
  937. />
  938. </div>
  939. )}
  940. </div>
  941. <PromptToast
  942. showToast={!hitBottom}
  943. showModal={showPromptModal}
  944. setShowModal={setShowPromptModal}
  945. />
  946. </div>
  947. <div
  948. className={styles["chat-body"]}
  949. ref={scrollRef}
  950. onScroll={(e) => onChatBodyScroll(e.currentTarget)}
  951. onMouseDown={() => inputRef.current?.blur()}
  952. onTouchStart={() => {
  953. inputRef.current?.blur();
  954. setAutoScroll(false);
  955. }}
  956. >
  957. {messages.map((message, i) => {
  958. const isUser = message.role === "user";
  959. const isContext = i < context.length;
  960. const showActions =
  961. i > 0 &&
  962. !(message.preview || message.content.length === 0) &&
  963. !isContext;
  964. const showTyping = message.preview || message.streaming;
  965. const shouldShowClearContextDivider = i === clearContextIndex - 1;
  966. return (
  967. <Fragment key={i}>
  968. <div
  969. className={
  970. isUser ? styles["chat-message-user"] : styles["chat-message"]
  971. }
  972. >
  973. <div className={styles["chat-message-container"]}>
  974. <div className={styles["chat-message-header"]}>
  975. <div className={styles["chat-message-avatar"]}>
  976. <div className={styles["chat-message-edit"]}>
  977. <IconButton
  978. icon={<EditIcon />}
  979. onClick={async () => {
  980. const newMessage = await showPrompt(
  981. Locale.Chat.Actions.Edit,
  982. message.content,
  983. 10,
  984. );
  985. chatStore.updateCurrentSession((session) => {
  986. const m = session.messages.find(
  987. (m) => m.id === message.id,
  988. );
  989. if (m) {
  990. m.content = newMessage;
  991. }
  992. });
  993. }}
  994. ></IconButton>
  995. </div>
  996. {isUser ? (
  997. <Avatar avatar={config.avatar} />
  998. ) : (
  999. <MaskAvatar mask={session.mask} />
  1000. )}
  1001. </div>
  1002. {showActions && (
  1003. <div className={styles["chat-message-actions"]}>
  1004. <div className={styles["chat-input-actions"]}>
  1005. {message.streaming ? (
  1006. <ChatAction
  1007. text={Locale.Chat.Actions.Stop}
  1008. icon={<StopIcon />}
  1009. onClick={() => onUserStop(message.id ?? i)}
  1010. />
  1011. ) : (
  1012. <>
  1013. <ChatAction
  1014. text={Locale.Chat.Actions.Retry}
  1015. icon={<ResetIcon />}
  1016. onClick={() => onResend(message)}
  1017. />
  1018. <ChatAction
  1019. text={Locale.Chat.Actions.Delete}
  1020. icon={<DeleteIcon />}
  1021. onClick={() => onDelete(message.id ?? i)}
  1022. />
  1023. <ChatAction
  1024. text={Locale.Chat.Actions.Pin}
  1025. icon={<PinIcon />}
  1026. onClick={() => onPinMessage(message)}
  1027. />
  1028. <ChatAction
  1029. text={Locale.Chat.Actions.Copy}
  1030. icon={<CopyIcon />}
  1031. onClick={() => copyToClipboard(message.content)}
  1032. />
  1033. </>
  1034. )}
  1035. </div>
  1036. </div>
  1037. )}
  1038. </div>
  1039. {showTyping && (
  1040. <div className={styles["chat-message-status"]}>
  1041. {Locale.Chat.Typing}
  1042. </div>
  1043. )}
  1044. <div className={styles["chat-message-item"]}>
  1045. <Markdown
  1046. content={message.content}
  1047. loading={
  1048. (message.preview || message.content.length === 0) &&
  1049. !isUser
  1050. }
  1051. onContextMenu={(e) => onRightClick(e, message)}
  1052. onDoubleClickCapture={() => {
  1053. if (!isMobileScreen) return;
  1054. setUserInput(message.content);
  1055. }}
  1056. fontSize={fontSize}
  1057. parentRef={scrollRef}
  1058. defaultShow={i >= messages.length - 6}
  1059. />
  1060. </div>
  1061. <div className={styles["chat-message-action-date"]}>
  1062. {isContext
  1063. ? Locale.Chat.IsContext
  1064. : message.date.toLocaleString()}
  1065. </div>
  1066. </div>
  1067. </div>
  1068. {shouldShowClearContextDivider && <ClearContextDivider />}
  1069. </Fragment>
  1070. );
  1071. })}
  1072. </div>
  1073. <div className={styles["chat-input-panel"]}>
  1074. <PromptHints prompts={promptHints} onPromptSelect={onPromptSelect} />
  1075. <ChatActions
  1076. showPromptModal={() => setShowPromptModal(true)}
  1077. scrollToBottom={scrollToBottom}
  1078. hitBottom={hitBottom}
  1079. showPromptHints={() => {
  1080. // Click again to close
  1081. if (promptHints.length > 0) {
  1082. setPromptHints([]);
  1083. return;
  1084. }
  1085. inputRef.current?.focus();
  1086. setUserInput("/");
  1087. onSearch("");
  1088. }}
  1089. />
  1090. <div className={styles["chat-input-panel-inner"]}>
  1091. <textarea
  1092. ref={inputRef}
  1093. className={styles["chat-input"]}
  1094. placeholder={Locale.Chat.Input(submitKey)}
  1095. onInput={(e) => onInput(e.currentTarget.value)}
  1096. value={userInput}
  1097. onKeyDown={onInputKeyDown}
  1098. onFocus={() => setAutoScroll(true)}
  1099. rows={inputRows}
  1100. autoFocus={autoFocus}
  1101. style={{
  1102. fontSize: config.fontSize,
  1103. }}
  1104. />
  1105. <IconButton
  1106. icon={<SendWhiteIcon />}
  1107. text={Locale.Chat.Send}
  1108. className={styles["chat-input-send"]}
  1109. type="primary"
  1110. onClick={() => doSubmit(userInput)}
  1111. />
  1112. </div>
  1113. </div>
  1114. {showExport && (
  1115. <ExportMessageModal onClose={() => setShowExport(false)} />
  1116. )}
  1117. {isEditingMessage && (
  1118. <EditMessageModal
  1119. onClose={() => {
  1120. setIsEditingMessage(false);
  1121. }}
  1122. />
  1123. )}
  1124. </div>
  1125. );
  1126. }