chat.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. import { useDebounce, useDebouncedCallback } from "use-debounce";
  2. import { memo, useState, useRef, useEffect, useLayoutEffect } from "react";
  3. import SendWhiteIcon from "../icons/send-white.svg";
  4. import BrainIcon from "../icons/brain.svg";
  5. import ExportIcon from "../icons/export.svg";
  6. import ReturnIcon from "../icons/return.svg";
  7. import CopyIcon from "../icons/copy.svg";
  8. import DownloadIcon from "../icons/download.svg";
  9. import LoadingIcon from "../icons/three-dots.svg";
  10. import BotIcon from "../icons/bot.svg";
  11. import AddIcon from "../icons/add.svg";
  12. import DeleteIcon from "../icons/delete.svg";
  13. import {
  14. Message,
  15. SubmitKey,
  16. useChatStore,
  17. BOT_HELLO,
  18. ROLES,
  19. createMessage,
  20. useAccessStore,
  21. } from "../store";
  22. import {
  23. copyToClipboard,
  24. downloadAs,
  25. getEmojiUrl,
  26. isMobileScreen,
  27. selectOrCopy,
  28. autoGrowTextArea,
  29. } from "../utils";
  30. import dynamic from "next/dynamic";
  31. import { ControllerPool } from "../requests";
  32. import { Prompt, usePromptStore } from "../store/prompt";
  33. import Locale from "../locales";
  34. import { IconButton } from "./button";
  35. import styles from "./home.module.scss";
  36. import chatStyle from "./chat.module.scss";
  37. import { Input, Modal, showModal } from "./ui-lib";
  38. const Markdown = dynamic(
  39. async () => memo((await import("./markdown")).Markdown),
  40. {
  41. loading: () => <LoadingIcon />,
  42. },
  43. );
  44. const Emoji = dynamic(async () => (await import("emoji-picker-react")).Emoji, {
  45. loading: () => <LoadingIcon />,
  46. });
  47. export function Avatar(props: { role: Message["role"] }) {
  48. const config = useChatStore((state) => state.config);
  49. if (props.role !== "user") {
  50. return <BotIcon className={styles["user-avtar"]} />;
  51. }
  52. return (
  53. <div className={styles["user-avtar"]}>
  54. <Emoji unified={config.avatar} size={18} getEmojiUrl={getEmojiUrl} />
  55. </div>
  56. );
  57. }
  58. function exportMessages(messages: Message[], topic: string) {
  59. const mdText =
  60. `# ${topic}\n\n` +
  61. messages
  62. .map((m) => {
  63. return m.role === "user"
  64. ? `## ${Locale.Export.MessageFromYou}:\n${m.content}`
  65. : `## ${Locale.Export.MessageFromChatGPT}:\n${m.content.trim()}`;
  66. })
  67. .join("\n\n");
  68. const filename = `${topic}.md`;
  69. showModal({
  70. title: Locale.Export.Title,
  71. children: (
  72. <div className="markdown-body">
  73. <pre className={styles["export-content"]}>{mdText}</pre>
  74. </div>
  75. ),
  76. actions: [
  77. <IconButton
  78. key="copy"
  79. icon={<CopyIcon />}
  80. bordered
  81. text={Locale.Export.Copy}
  82. onClick={() => copyToClipboard(mdText)}
  83. />,
  84. <IconButton
  85. key="download"
  86. icon={<DownloadIcon />}
  87. bordered
  88. text={Locale.Export.Download}
  89. onClick={() => downloadAs(mdText, filename)}
  90. />,
  91. ],
  92. });
  93. }
  94. function PromptToast(props: {
  95. showToast?: boolean;
  96. showModal?: boolean;
  97. setShowModal: (_: boolean) => void;
  98. }) {
  99. const chatStore = useChatStore();
  100. const session = chatStore.currentSession();
  101. const context = session.context;
  102. const addContextPrompt = (prompt: Message) => {
  103. chatStore.updateCurrentSession((session) => {
  104. session.context.push(prompt);
  105. });
  106. };
  107. const removeContextPrompt = (i: number) => {
  108. chatStore.updateCurrentSession((session) => {
  109. session.context.splice(i, 1);
  110. });
  111. };
  112. const updateContextPrompt = (i: number, prompt: Message) => {
  113. chatStore.updateCurrentSession((session) => {
  114. session.context[i] = prompt;
  115. });
  116. };
  117. return (
  118. <div className={chatStyle["prompt-toast"]} key="prompt-toast">
  119. {props.showToast && (
  120. <div
  121. className={chatStyle["prompt-toast-inner"] + " clickable"}
  122. role="button"
  123. onClick={() => props.setShowModal(true)}
  124. >
  125. <BrainIcon />
  126. <span className={chatStyle["prompt-toast-content"]}>
  127. {Locale.Context.Toast(context.length)}
  128. </span>
  129. </div>
  130. )}
  131. {props.showModal && (
  132. <div className="modal-mask">
  133. <Modal
  134. title={Locale.Context.Edit}
  135. onClose={() => props.setShowModal(false)}
  136. actions={[
  137. <IconButton
  138. key="reset"
  139. icon={<CopyIcon />}
  140. bordered
  141. text={Locale.Memory.Reset}
  142. onClick={() =>
  143. confirm(Locale.Memory.ResetConfirm) &&
  144. chatStore.resetSession()
  145. }
  146. />,
  147. <IconButton
  148. key="copy"
  149. icon={<CopyIcon />}
  150. bordered
  151. text={Locale.Memory.Copy}
  152. onClick={() => copyToClipboard(session.memoryPrompt)}
  153. />,
  154. ]}
  155. >
  156. <>
  157. <div className={chatStyle["context-prompt"]}>
  158. {context.map((c, i) => (
  159. <div className={chatStyle["context-prompt-row"]} key={i}>
  160. <select
  161. value={c.role}
  162. className={chatStyle["context-role"]}
  163. onChange={(e) =>
  164. updateContextPrompt(i, {
  165. ...c,
  166. role: e.target.value as any,
  167. })
  168. }
  169. >
  170. {ROLES.map((r) => (
  171. <option key={r} value={r}>
  172. {r}
  173. </option>
  174. ))}
  175. </select>
  176. <Input
  177. value={c.content}
  178. type="text"
  179. className={chatStyle["context-content"]}
  180. rows={1}
  181. onInput={(e) =>
  182. updateContextPrompt(i, {
  183. ...c,
  184. content: e.currentTarget.value as any,
  185. })
  186. }
  187. />
  188. <IconButton
  189. icon={<DeleteIcon />}
  190. className={chatStyle["context-delete-button"]}
  191. onClick={() => removeContextPrompt(i)}
  192. bordered
  193. />
  194. </div>
  195. ))}
  196. <div className={chatStyle["context-prompt-row"]}>
  197. <IconButton
  198. icon={<AddIcon />}
  199. text={Locale.Context.Add}
  200. bordered
  201. className={chatStyle["context-prompt-button"]}
  202. onClick={() =>
  203. addContextPrompt({
  204. role: "system",
  205. content: "",
  206. date: "",
  207. })
  208. }
  209. />
  210. </div>
  211. </div>
  212. <div className={chatStyle["memory-prompt"]}>
  213. <div className={chatStyle["memory-prompt-title"]}>
  214. <span>
  215. {Locale.Memory.Title} ({session.lastSummarizeIndex} of{" "}
  216. {session.messages.length})
  217. </span>
  218. <label className={chatStyle["memory-prompt-action"]}>
  219. {Locale.Memory.Send}
  220. <input
  221. type="checkbox"
  222. checked={session.sendMemory}
  223. onChange={() =>
  224. chatStore.updateCurrentSession(
  225. (session) =>
  226. (session.sendMemory = !session.sendMemory),
  227. )
  228. }
  229. ></input>
  230. </label>
  231. </div>
  232. <div className={chatStyle["memory-prompt-content"]}>
  233. {session.memoryPrompt || Locale.Memory.EmptyContent}
  234. </div>
  235. </div>
  236. </>
  237. </Modal>
  238. </div>
  239. )}
  240. </div>
  241. );
  242. }
  243. function useSubmitHandler() {
  244. const config = useChatStore((state) => state.config);
  245. const submitKey = config.submitKey;
  246. const shouldSubmit = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  247. if (e.key !== "Enter") return false;
  248. if (e.key === "Enter" && e.nativeEvent.isComposing) return false;
  249. return (
  250. (config.submitKey === SubmitKey.AltEnter && e.altKey) ||
  251. (config.submitKey === SubmitKey.CtrlEnter && e.ctrlKey) ||
  252. (config.submitKey === SubmitKey.ShiftEnter && e.shiftKey) ||
  253. (config.submitKey === SubmitKey.MetaEnter && e.metaKey) ||
  254. (config.submitKey === SubmitKey.Enter &&
  255. !e.altKey &&
  256. !e.ctrlKey &&
  257. !e.shiftKey &&
  258. !e.metaKey)
  259. );
  260. };
  261. return {
  262. submitKey,
  263. shouldSubmit,
  264. };
  265. }
  266. export function PromptHints(props: {
  267. prompts: Prompt[];
  268. onPromptSelect: (prompt: Prompt) => void;
  269. }) {
  270. if (props.prompts.length === 0) return null;
  271. return (
  272. <div className={styles["prompt-hints"]}>
  273. {props.prompts.map((prompt, i) => (
  274. <div
  275. className={styles["prompt-hint"]}
  276. key={prompt.title + i.toString()}
  277. onClick={() => props.onPromptSelect(prompt)}
  278. >
  279. <div className={styles["hint-title"]}>{prompt.title}</div>
  280. <div className={styles["hint-content"]}>{prompt.content}</div>
  281. </div>
  282. ))}
  283. </div>
  284. );
  285. }
  286. function useScrollToBottom() {
  287. // for auto-scroll
  288. const scrollRef = useRef<HTMLDivElement>(null);
  289. const [autoScroll, setAutoScroll] = useState(true);
  290. // auto scroll
  291. useLayoutEffect(() => {
  292. const dom = scrollRef.current;
  293. if (dom && autoScroll) {
  294. setTimeout(() => (dom.scrollTop = dom.scrollHeight), 1);
  295. }
  296. });
  297. return {
  298. scrollRef,
  299. autoScroll,
  300. setAutoScroll,
  301. };
  302. }
  303. export function Chat(props: {
  304. showSideBar?: () => void;
  305. sideBarShowing?: boolean;
  306. }) {
  307. type RenderMessage = Message & { preview?: boolean };
  308. const chatStore = useChatStore();
  309. const [session, sessionIndex] = useChatStore((state) => [
  310. state.currentSession(),
  311. state.currentSessionIndex,
  312. ]);
  313. const fontSize = useChatStore((state) => state.config.fontSize);
  314. const inputRef = useRef<HTMLTextAreaElement>(null);
  315. const [userInput, setUserInput] = useState("");
  316. const [beforeInput, setBeforeInput] = useState("");
  317. const [isLoading, setIsLoading] = useState(false);
  318. const { submitKey, shouldSubmit } = useSubmitHandler();
  319. const { scrollRef, setAutoScroll } = useScrollToBottom();
  320. const [hitBottom, setHitBottom] = useState(false);
  321. const onChatBodyScroll = (e: HTMLElement) => {
  322. const isTouchBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 20;
  323. setHitBottom(isTouchBottom);
  324. };
  325. // prompt hints
  326. const promptStore = usePromptStore();
  327. const [promptHints, setPromptHints] = useState<Prompt[]>([]);
  328. const onSearch = useDebouncedCallback(
  329. (text: string) => {
  330. setPromptHints(promptStore.search(text));
  331. },
  332. 100,
  333. { leading: true, trailing: true },
  334. );
  335. const onPromptSelect = (prompt: Prompt) => {
  336. setUserInput(prompt.content);
  337. setPromptHints([]);
  338. inputRef.current?.focus();
  339. };
  340. const scrollInput = () => {
  341. const dom = inputRef.current;
  342. if (!dom) return;
  343. const paddingBottomNum: number = parseInt(
  344. window.getComputedStyle(dom).paddingBottom,
  345. 10,
  346. );
  347. dom.scrollTop = dom.scrollHeight - dom.offsetHeight + paddingBottomNum;
  348. };
  349. // auto grow input
  350. const [inputRows, setInputRows] = useState(2);
  351. const measure = useDebouncedCallback(
  352. () => {
  353. const rows = inputRef.current ? autoGrowTextArea(inputRef.current) : 1;
  354. const inputRows = Math.min(
  355. 5,
  356. Math.max(2 + Number(!isMobileScreen()), rows),
  357. );
  358. setInputRows(inputRows);
  359. },
  360. 100,
  361. {
  362. leading: true,
  363. trailing: true,
  364. },
  365. );
  366. // eslint-disable-next-line react-hooks/exhaustive-deps
  367. useEffect(measure, [userInput]);
  368. // only search prompts when user input is short
  369. const SEARCH_TEXT_LIMIT = 30;
  370. const onInput = (text: string) => {
  371. scrollInput();
  372. setUserInput(text);
  373. const n = text.trim().length;
  374. // clear search results
  375. if (n === 0) {
  376. setPromptHints([]);
  377. } else if (!chatStore.config.disablePromptHint && n < SEARCH_TEXT_LIMIT) {
  378. // check if need to trigger auto completion
  379. if (text.startsWith("/")) {
  380. let searchText = text.slice(1);
  381. onSearch(searchText);
  382. }
  383. }
  384. };
  385. // submit user input
  386. const onUserSubmit = () => {
  387. if (userInput.length <= 0) return;
  388. setIsLoading(true);
  389. chatStore.onUserInput(userInput).then(() => setIsLoading(false));
  390. setBeforeInput(userInput);
  391. setUserInput("");
  392. setPromptHints([]);
  393. if (!isMobileScreen()) inputRef.current?.focus();
  394. setAutoScroll(true);
  395. };
  396. // stop response
  397. const onUserStop = (messageId: number) => {
  398. ControllerPool.stop(sessionIndex, messageId);
  399. };
  400. // check if should send message
  401. const onInputKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  402. // if ArrowUp and no userInput
  403. if (e.key === "ArrowUp" && userInput.length <= 0) {
  404. setUserInput(beforeInput);
  405. e.preventDefault();
  406. return;
  407. }
  408. if (shouldSubmit(e)) {
  409. onUserSubmit();
  410. e.preventDefault();
  411. }
  412. };
  413. const onRightClick = (e: any, message: Message) => {
  414. // auto fill user input
  415. if (message.role === "user") {
  416. setUserInput(message.content);
  417. }
  418. // copy to clipboard
  419. if (selectOrCopy(e.currentTarget, message.content)) {
  420. e.preventDefault();
  421. }
  422. };
  423. const onResend = (botIndex: number) => {
  424. // find last user input message and resend
  425. for (let i = botIndex; i >= 0; i -= 1) {
  426. if (messages[i].role === "user") {
  427. setIsLoading(true);
  428. chatStore
  429. .onUserInput(messages[i].content)
  430. .then(() => setIsLoading(false));
  431. chatStore.updateCurrentSession((session) =>
  432. session.messages.splice(i, 2),
  433. );
  434. inputRef.current?.focus();
  435. return;
  436. }
  437. }
  438. };
  439. const config = useChatStore((state) => state.config);
  440. const context: RenderMessage[] = session.context.slice();
  441. const accessStore = useAccessStore();
  442. if (
  443. context.length === 0 &&
  444. session.messages.at(0)?.content !== BOT_HELLO.content
  445. ) {
  446. const copiedHello = Object.assign({}, BOT_HELLO);
  447. if (!accessStore.isAuthorized()) {
  448. copiedHello.content = Locale.Error.Unauthorized;
  449. }
  450. context.push(copiedHello);
  451. }
  452. // preview messages
  453. const messages = context
  454. .concat(session.messages as RenderMessage[])
  455. .concat(
  456. isLoading
  457. ? [
  458. {
  459. ...createMessage({
  460. role: "assistant",
  461. content: "……",
  462. }),
  463. preview: true,
  464. },
  465. ]
  466. : [],
  467. )
  468. .concat(
  469. userInput.length > 0 && config.sendPreviewBubble
  470. ? [
  471. {
  472. ...createMessage({
  473. role: "user",
  474. content: userInput,
  475. }),
  476. preview: true,
  477. },
  478. ]
  479. : [],
  480. );
  481. const [showPromptModal, setShowPromptModal] = useState(false);
  482. // Auto focus
  483. useEffect(() => {
  484. if (props.sideBarShowing && isMobileScreen()) return;
  485. inputRef.current?.focus();
  486. // eslint-disable-next-line react-hooks/exhaustive-deps
  487. }, []);
  488. return (
  489. <div className={styles.chat} key={session.id}>
  490. <div className={styles["window-header"]}>
  491. <div className={styles["window-header-title"]}>
  492. <div
  493. className={`${styles["window-header-main-title"]} ${styles["chat-body-title"]}`}
  494. onClickCapture={() => {
  495. const newTopic = prompt(Locale.Chat.Rename, session.topic);
  496. if (newTopic && newTopic !== session.topic) {
  497. chatStore.updateCurrentSession(
  498. (session) => (session.topic = newTopic!),
  499. );
  500. }
  501. }}
  502. >
  503. {session.topic}
  504. </div>
  505. <div className={styles["window-header-sub-title"]}>
  506. {Locale.Chat.SubTitle(session.messages.length)}
  507. </div>
  508. </div>
  509. <div className={styles["window-actions"]}>
  510. <div className={styles["window-action-button"] + " " + styles.mobile}>
  511. <IconButton
  512. icon={<ReturnIcon />}
  513. bordered
  514. title={Locale.Chat.Actions.ChatList}
  515. onClick={props?.showSideBar}
  516. />
  517. </div>
  518. <div className={styles["window-action-button"]}>
  519. <IconButton
  520. icon={<BrainIcon />}
  521. bordered
  522. title={Locale.Chat.Actions.CompressedHistory}
  523. onClick={() => {
  524. setShowPromptModal(true);
  525. }}
  526. />
  527. </div>
  528. <div className={styles["window-action-button"]}>
  529. <IconButton
  530. icon={<ExportIcon />}
  531. bordered
  532. title={Locale.Chat.Actions.Export}
  533. onClick={() => {
  534. exportMessages(
  535. session.messages.filter((msg) => !msg.isError),
  536. session.topic,
  537. );
  538. }}
  539. />
  540. </div>
  541. </div>
  542. <PromptToast
  543. showToast={!hitBottom}
  544. showModal={showPromptModal}
  545. setShowModal={setShowPromptModal}
  546. />
  547. </div>
  548. <div
  549. className={styles["chat-body"]}
  550. ref={scrollRef}
  551. onScroll={(e) => onChatBodyScroll(e.currentTarget)}
  552. onWheel={(e) => setAutoScroll(hitBottom && e.deltaY > 0)}
  553. onTouchStart={() => {
  554. inputRef.current?.blur();
  555. setAutoScroll(false);
  556. }}
  557. >
  558. {messages.map((message, i) => {
  559. const isUser = message.role === "user";
  560. return (
  561. <div
  562. key={i}
  563. className={
  564. isUser ? styles["chat-message-user"] : styles["chat-message"]
  565. }
  566. >
  567. <div className={styles["chat-message-container"]}>
  568. <div className={styles["chat-message-avatar"]}>
  569. <Avatar role={message.role} />
  570. </div>
  571. {(message.preview || message.streaming) && (
  572. <div className={styles["chat-message-status"]}>
  573. {Locale.Chat.Typing}
  574. </div>
  575. )}
  576. <div className={styles["chat-message-item"]}>
  577. {!isUser &&
  578. !(message.preview || message.content.length === 0) && (
  579. <div className={styles["chat-message-top-actions"]}>
  580. {message.streaming ? (
  581. <div
  582. className={styles["chat-message-top-action"]}
  583. onClick={() => onUserStop(message.id ?? i)}
  584. >
  585. {Locale.Chat.Actions.Stop}
  586. </div>
  587. ) : (
  588. <div
  589. className={styles["chat-message-top-action"]}
  590. onClick={() => onResend(i)}
  591. >
  592. {Locale.Chat.Actions.Retry}
  593. </div>
  594. )}
  595. <div
  596. className={styles["chat-message-top-action"]}
  597. onClick={() => copyToClipboard(message.content)}
  598. >
  599. {Locale.Chat.Actions.Copy}
  600. </div>
  601. </div>
  602. )}
  603. {(message.preview || message.content.length === 0) &&
  604. !isUser ? (
  605. <LoadingIcon />
  606. ) : (
  607. <div
  608. className="markdown-body"
  609. style={{ fontSize: `${fontSize}px` }}
  610. onContextMenu={(e) => onRightClick(e, message)}
  611. onDoubleClickCapture={() => {
  612. if (!isMobileScreen()) return;
  613. setUserInput(message.content);
  614. }}
  615. >
  616. <Markdown content={message.content} />
  617. </div>
  618. )}
  619. </div>
  620. {!isUser && !message.preview && (
  621. <div className={styles["chat-message-actions"]}>
  622. <div className={styles["chat-message-action-date"]}>
  623. {message.date.toLocaleString()}
  624. </div>
  625. </div>
  626. )}
  627. </div>
  628. </div>
  629. );
  630. })}
  631. </div>
  632. <div className={styles["chat-input-panel"]}>
  633. <PromptHints prompts={promptHints} onPromptSelect={onPromptSelect} />
  634. <div className={styles["chat-input-panel-inner"]}>
  635. <textarea
  636. ref={inputRef}
  637. className={styles["chat-input"]}
  638. placeholder={Locale.Chat.Input(submitKey)}
  639. onInput={(e) => onInput(e.currentTarget.value)}
  640. value={userInput}
  641. onKeyDown={onInputKeyDown}
  642. onFocus={() => setAutoScroll(true)}
  643. onBlur={() => {
  644. setAutoScroll(false);
  645. setTimeout(() => setPromptHints([]), 500);
  646. }}
  647. autoFocus={!props?.sideBarShowing}
  648. rows={inputRows}
  649. />
  650. <IconButton
  651. icon={<SendWhiteIcon />}
  652. text={Locale.Chat.Send}
  653. className={styles["chat-input-send"]}
  654. noDark
  655. onClick={onUserSubmit}
  656. />
  657. </div>
  658. </div>
  659. </div>
  660. );
  661. }