chat.tsx 21 KB

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