chat.tsx 39 KB

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