chat.tsx 37 KB

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