chat.tsx 20 KB

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