chat.tsx 19 KB

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