chat.tsx 19 KB

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