chat.tsx 19 KB

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