chat.tsx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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 RenameIcon from "../icons/rename.svg";
  6. import ExportIcon from "../icons/share.svg";
  7. import ReturnIcon from "../icons/return.svg";
  8. import CopyIcon from "../icons/copy.svg";
  9. import DownloadIcon from "../icons/download.svg";
  10. import LoadingIcon from "../icons/three-dots.svg";
  11. import PromptIcon from "../icons/prompt.svg";
  12. import MaskIcon from "../icons/mask.svg";
  13. import MaxIcon from "../icons/max.svg";
  14. import MinIcon from "../icons/min.svg";
  15. import ResetIcon from "../icons/reload.svg";
  16. import LightIcon from "../icons/light.svg";
  17. import DarkIcon from "../icons/dark.svg";
  18. import AutoIcon from "../icons/auto.svg";
  19. import BottomIcon from "../icons/bottom.svg";
  20. import StopIcon from "../icons/pause.svg";
  21. import {
  22. Message,
  23. SubmitKey,
  24. useChatStore,
  25. BOT_HELLO,
  26. ROLES,
  27. createMessage,
  28. useAccessStore,
  29. Theme,
  30. useAppConfig,
  31. ModelConfig,
  32. DEFAULT_TOPIC,
  33. } from "../store";
  34. import {
  35. copyToClipboard,
  36. downloadAs,
  37. selectOrCopy,
  38. autoGrowTextArea,
  39. useMobileScreen,
  40. } from "../utils";
  41. import dynamic from "next/dynamic";
  42. import { ControllerPool } from "../requests";
  43. import { Prompt, usePromptStore } from "../store/prompt";
  44. import Locale from "../locales";
  45. import { IconButton } from "./button";
  46. import styles from "./home.module.scss";
  47. import chatStyle from "./chat.module.scss";
  48. import { Input, List, ListItem, Modal, Popover, showModal } from "./ui-lib";
  49. import { useNavigate } from "react-router-dom";
  50. import { Path } from "../constant";
  51. import { ModelConfigList } from "./model-config";
  52. import { Avatar, AvatarPicker } from "./emoji";
  53. import { MaskConfig } from "./mask";
  54. import { DEFAULT_MASK_ID, useMaskStore } from "../store/mask";
  55. const Markdown = dynamic(
  56. async () => memo((await import("./markdown")).Markdown),
  57. {
  58. loading: () => <LoadingIcon />,
  59. },
  60. );
  61. function exportMessages(messages: Message[], topic: string) {
  62. const mdText =
  63. `# ${topic}\n\n` +
  64. messages
  65. .map((m) => {
  66. return m.role === "user"
  67. ? `## ${Locale.Export.MessageFromYou}:\n${m.content}`
  68. : `## ${Locale.Export.MessageFromChatGPT}:\n${m.content.trim()}`;
  69. })
  70. .join("\n\n");
  71. const filename = `${topic}.md`;
  72. showModal({
  73. title: Locale.Export.Title,
  74. children: (
  75. <div className="markdown-body">
  76. <pre className={styles["export-content"]}>{mdText}</pre>
  77. </div>
  78. ),
  79. actions: [
  80. <IconButton
  81. key="copy"
  82. icon={<CopyIcon />}
  83. bordered
  84. text={Locale.Export.Copy}
  85. onClick={() => copyToClipboard(mdText)}
  86. />,
  87. <IconButton
  88. key="download"
  89. icon={<DownloadIcon />}
  90. bordered
  91. text={Locale.Export.Download}
  92. onClick={() => downloadAs(mdText, filename)}
  93. />,
  94. ],
  95. });
  96. }
  97. export function SessionConfigModel(props: { onClose: () => void }) {
  98. const chatStore = useChatStore();
  99. const session = chatStore.currentSession();
  100. const maskStore = useMaskStore();
  101. const navigate = useNavigate();
  102. return (
  103. <div className="modal-mask">
  104. <Modal
  105. title={Locale.Context.Edit}
  106. onClose={() => props.onClose()}
  107. actions={[
  108. <IconButton
  109. key="reset"
  110. icon={<ResetIcon />}
  111. bordered
  112. text={Locale.Chat.Config.Reset}
  113. onClick={() =>
  114. confirm(Locale.Memory.ResetConfirm) && chatStore.resetSession()
  115. }
  116. />,
  117. <IconButton
  118. key="copy"
  119. icon={<CopyIcon />}
  120. bordered
  121. text={Locale.Chat.Config.SaveAs}
  122. onClick={() => {
  123. navigate(Path.Masks);
  124. setTimeout(() => {
  125. maskStore.create(session.mask);
  126. }, 500);
  127. }}
  128. />,
  129. ]}
  130. >
  131. <MaskConfig
  132. mask={session.mask}
  133. updateMask={(updater) => {
  134. const mask = { ...session.mask };
  135. updater(mask);
  136. chatStore.updateCurrentSession((session) => (session.mask = mask));
  137. }}
  138. extraListItems={
  139. session.mask.modelConfig.sendMemory ? (
  140. <ListItem
  141. title={`${Locale.Memory.Title} (${session.lastSummarizeIndex} of ${session.messages.length})`}
  142. subTitle={session.memoryPrompt || Locale.Memory.EmptyContent}
  143. ></ListItem>
  144. ) : (
  145. <></>
  146. )
  147. }
  148. ></MaskConfig>
  149. </Modal>
  150. </div>
  151. );
  152. }
  153. function PromptToast(props: {
  154. showToast?: boolean;
  155. showModal?: boolean;
  156. setShowModal: (_: boolean) => void;
  157. }) {
  158. const chatStore = useChatStore();
  159. const session = chatStore.currentSession();
  160. const context = session.mask.context;
  161. return (
  162. <div className={chatStyle["prompt-toast"]} key="prompt-toast">
  163. {props.showToast && (
  164. <div
  165. className={chatStyle["prompt-toast-inner"] + " clickable"}
  166. role="button"
  167. onClick={() => props.setShowModal(true)}
  168. >
  169. <BrainIcon />
  170. <span className={chatStyle["prompt-toast-content"]}>
  171. {Locale.Context.Toast(context.length)}
  172. </span>
  173. </div>
  174. )}
  175. {props.showModal && (
  176. <SessionConfigModel onClose={() => props.setShowModal(false)} />
  177. )}
  178. </div>
  179. );
  180. }
  181. function useSubmitHandler() {
  182. const config = useAppConfig();
  183. const submitKey = config.submitKey;
  184. const shouldSubmit = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  185. if (e.key !== "Enter") return false;
  186. if (e.key === "Enter" && e.nativeEvent.isComposing) return false;
  187. return (
  188. (config.submitKey === SubmitKey.AltEnter && e.altKey) ||
  189. (config.submitKey === SubmitKey.CtrlEnter && e.ctrlKey) ||
  190. (config.submitKey === SubmitKey.ShiftEnter && e.shiftKey) ||
  191. (config.submitKey === SubmitKey.MetaEnter && e.metaKey) ||
  192. (config.submitKey === SubmitKey.Enter &&
  193. !e.altKey &&
  194. !e.ctrlKey &&
  195. !e.shiftKey &&
  196. !e.metaKey)
  197. );
  198. };
  199. return {
  200. submitKey,
  201. shouldSubmit,
  202. };
  203. }
  204. export function PromptHints(props: {
  205. prompts: Prompt[];
  206. onPromptSelect: (prompt: Prompt) => void;
  207. }) {
  208. if (props.prompts.length === 0) return null;
  209. return (
  210. <div className={styles["prompt-hints"]}>
  211. {props.prompts.map((prompt, i) => (
  212. <div
  213. className={styles["prompt-hint"]}
  214. key={prompt.title + i.toString()}
  215. onClick={() => props.onPromptSelect(prompt)}
  216. >
  217. <div className={styles["hint-title"]}>{prompt.title}</div>
  218. <div className={styles["hint-content"]}>{prompt.content}</div>
  219. </div>
  220. ))}
  221. </div>
  222. );
  223. }
  224. function useScrollToBottom() {
  225. // for auto-scroll
  226. const scrollRef = useRef<HTMLDivElement>(null);
  227. const [autoScroll, setAutoScroll] = useState(true);
  228. const scrollToBottom = () => {
  229. const dom = scrollRef.current;
  230. if (dom) {
  231. setTimeout(() => (dom.scrollTop = dom.scrollHeight), 1);
  232. }
  233. };
  234. // auto scroll
  235. useLayoutEffect(() => {
  236. autoScroll && scrollToBottom();
  237. });
  238. return {
  239. scrollRef,
  240. autoScroll,
  241. setAutoScroll,
  242. scrollToBottom,
  243. };
  244. }
  245. export function ChatActions(props: {
  246. showPromptModal: () => void;
  247. scrollToBottom: () => void;
  248. showPromptHints: () => void;
  249. hitBottom: boolean;
  250. }) {
  251. const config = useAppConfig();
  252. const navigate = useNavigate();
  253. // switch themes
  254. const theme = config.theme;
  255. function nextTheme() {
  256. const themes = [Theme.Auto, Theme.Light, Theme.Dark];
  257. const themeIndex = themes.indexOf(theme);
  258. const nextIndex = (themeIndex + 1) % themes.length;
  259. const nextTheme = themes[nextIndex];
  260. config.update((config) => (config.theme = nextTheme));
  261. }
  262. // stop all responses
  263. const couldStop = ControllerPool.hasPending();
  264. const stopAll = () => ControllerPool.stopAll();
  265. return (
  266. <div className={chatStyle["chat-input-actions"]}>
  267. {couldStop && (
  268. <div
  269. className={`${chatStyle["chat-input-action"]} clickable`}
  270. onClick={stopAll}
  271. >
  272. <StopIcon />
  273. </div>
  274. )}
  275. {!props.hitBottom && (
  276. <div
  277. className={`${chatStyle["chat-input-action"]} clickable`}
  278. onClick={props.scrollToBottom}
  279. >
  280. <BottomIcon />
  281. </div>
  282. )}
  283. {props.hitBottom && (
  284. <div
  285. className={`${chatStyle["chat-input-action"]} clickable`}
  286. onClick={props.showPromptModal}
  287. >
  288. <BrainIcon />
  289. </div>
  290. )}
  291. <div
  292. className={`${chatStyle["chat-input-action"]} clickable`}
  293. onClick={nextTheme}
  294. >
  295. {theme === Theme.Auto ? (
  296. <AutoIcon />
  297. ) : theme === Theme.Light ? (
  298. <LightIcon />
  299. ) : theme === Theme.Dark ? (
  300. <DarkIcon />
  301. ) : null}
  302. </div>
  303. <div
  304. className={`${chatStyle["chat-input-action"]} clickable`}
  305. onClick={props.showPromptHints}
  306. >
  307. <PromptIcon />
  308. </div>
  309. <div
  310. className={`${chatStyle["chat-input-action"]} clickable`}
  311. onClick={() => {
  312. navigate(Path.Masks);
  313. }}
  314. >
  315. <MaskIcon />
  316. </div>
  317. </div>
  318. );
  319. }
  320. export function Chat() {
  321. type RenderMessage = Message & { preview?: boolean };
  322. const chatStore = useChatStore();
  323. const [session, sessionIndex] = useChatStore((state) => [
  324. state.currentSession(),
  325. state.currentSessionIndex,
  326. ]);
  327. const config = useAppConfig();
  328. const fontSize = config.fontSize;
  329. const inputRef = useRef<HTMLTextAreaElement>(null);
  330. const [userInput, setUserInput] = useState("");
  331. const [beforeInput, setBeforeInput] = useState("");
  332. const [isLoading, setIsLoading] = useState(false);
  333. const { submitKey, shouldSubmit } = useSubmitHandler();
  334. const { scrollRef, setAutoScroll, scrollToBottom } = useScrollToBottom();
  335. const [hitBottom, setHitBottom] = useState(false);
  336. const isMobileScreen = useMobileScreen();
  337. const navigate = useNavigate();
  338. const onChatBodyScroll = (e: HTMLElement) => {
  339. const isTouchBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 20;
  340. setHitBottom(isTouchBottom);
  341. };
  342. // prompt hints
  343. const promptStore = usePromptStore();
  344. const [promptHints, setPromptHints] = useState<Prompt[]>([]);
  345. const onSearch = useDebouncedCallback(
  346. (text: string) => {
  347. setPromptHints(promptStore.search(text));
  348. },
  349. 100,
  350. { leading: true, trailing: true },
  351. );
  352. const onPromptSelect = (prompt: Prompt) => {
  353. setPromptHints([]);
  354. inputRef.current?.focus();
  355. setUserInput(prompt.content);
  356. };
  357. // auto grow input
  358. const [inputRows, setInputRows] = useState(2);
  359. const measure = useDebouncedCallback(
  360. () => {
  361. const rows = inputRef.current ? autoGrowTextArea(inputRef.current) : 1;
  362. const inputRows = Math.min(
  363. 5,
  364. Math.max(2 + Number(!isMobileScreen), rows),
  365. );
  366. setInputRows(inputRows);
  367. },
  368. 100,
  369. {
  370. leading: true,
  371. trailing: true,
  372. },
  373. );
  374. // eslint-disable-next-line react-hooks/exhaustive-deps
  375. useEffect(measure, [userInput]);
  376. // only search prompts when user input is short
  377. const SEARCH_TEXT_LIMIT = 30;
  378. const onInput = (text: string) => {
  379. setUserInput(text);
  380. const n = text.trim().length;
  381. // clear search results
  382. if (n === 0) {
  383. setPromptHints([]);
  384. } else if (!config.disablePromptHint && n < SEARCH_TEXT_LIMIT) {
  385. // check if need to trigger auto completion
  386. if (text.startsWith("/")) {
  387. let searchText = text.slice(1);
  388. onSearch(searchText);
  389. }
  390. }
  391. };
  392. // submit user input
  393. const onUserSubmit = () => {
  394. if (userInput.length <= 0) return;
  395. setIsLoading(true);
  396. chatStore.onUserInput(userInput).then(() => setIsLoading(false));
  397. setBeforeInput(userInput);
  398. setUserInput("");
  399. setPromptHints([]);
  400. if (!isMobileScreen) inputRef.current?.focus();
  401. setAutoScroll(true);
  402. };
  403. // stop response
  404. const onUserStop = (messageId: number) => {
  405. ControllerPool.stop(sessionIndex, messageId);
  406. };
  407. // check if should send message
  408. const onInputKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  409. // if ArrowUp and no userInput
  410. if (e.key === "ArrowUp" && userInput.length <= 0) {
  411. setUserInput(beforeInput);
  412. e.preventDefault();
  413. return;
  414. }
  415. if (shouldSubmit(e)) {
  416. onUserSubmit();
  417. e.preventDefault();
  418. }
  419. };
  420. const onRightClick = (e: any, message: Message) => {
  421. // auto fill user input
  422. if (message.role === "user") {
  423. setUserInput(message.content);
  424. }
  425. // copy to clipboard
  426. if (selectOrCopy(e.currentTarget, message.content)) {
  427. e.preventDefault();
  428. }
  429. };
  430. const findLastUesrIndex = (messageId: number) => {
  431. // find last user input message and resend
  432. let lastUserMessageIndex: number | null = null;
  433. for (let i = 0; i < session.messages.length; i += 1) {
  434. const message = session.messages[i];
  435. if (message.id === messageId) {
  436. break;
  437. }
  438. if (message.role === "user") {
  439. lastUserMessageIndex = i;
  440. }
  441. }
  442. return lastUserMessageIndex;
  443. };
  444. const deleteMessage = (userIndex: number) => {
  445. chatStore.updateCurrentSession((session) =>
  446. session.messages.splice(userIndex, 2),
  447. );
  448. };
  449. const onDelete = (botMessageId: number) => {
  450. const userIndex = findLastUesrIndex(botMessageId);
  451. if (userIndex === null) return;
  452. deleteMessage(userIndex);
  453. };
  454. const onResend = (botMessageId: number) => {
  455. // find last user input message and resend
  456. const userIndex = findLastUesrIndex(botMessageId);
  457. if (userIndex === null) return;
  458. setIsLoading(true);
  459. const content = session.messages[userIndex].content;
  460. deleteMessage(userIndex);
  461. chatStore.onUserInput(content).then(() => setIsLoading(false));
  462. inputRef.current?.focus();
  463. };
  464. const context: RenderMessage[] = session.mask.context.slice();
  465. const accessStore = useAccessStore();
  466. if (
  467. context.length === 0 &&
  468. session.messages.at(0)?.content !== BOT_HELLO.content
  469. ) {
  470. const copiedHello = Object.assign({}, BOT_HELLO);
  471. if (!accessStore.isAuthorized()) {
  472. copiedHello.content = Locale.Error.Unauthorized;
  473. }
  474. context.push(copiedHello);
  475. }
  476. // preview messages
  477. const messages = context
  478. .concat(session.messages as RenderMessage[])
  479. .concat(
  480. isLoading
  481. ? [
  482. {
  483. ...createMessage({
  484. role: "assistant",
  485. content: "……",
  486. }),
  487. preview: true,
  488. },
  489. ]
  490. : [],
  491. )
  492. .concat(
  493. userInput.length > 0 && config.sendPreviewBubble
  494. ? [
  495. {
  496. ...createMessage({
  497. role: "user",
  498. content: userInput,
  499. }),
  500. preview: true,
  501. },
  502. ]
  503. : [],
  504. );
  505. const [showPromptModal, setShowPromptModal] = useState(false);
  506. const renameSession = () => {
  507. const newTopic = prompt(Locale.Chat.Rename, session.topic);
  508. if (newTopic && newTopic !== session.topic) {
  509. chatStore.updateCurrentSession((session) => (session.topic = newTopic!));
  510. }
  511. };
  512. // Auto focus
  513. useEffect(() => {
  514. if (isMobileScreen) return;
  515. inputRef.current?.focus();
  516. // eslint-disable-next-line react-hooks/exhaustive-deps
  517. }, []);
  518. return (
  519. <div className={styles.chat} key={session.id}>
  520. <div className="window-header">
  521. <div className="window-header-title">
  522. <div
  523. className={`window-header-main-title " ${styles["chat-body-title"]}`}
  524. onClickCapture={renameSession}
  525. >
  526. {!session.topic ? DEFAULT_TOPIC : session.topic}
  527. </div>
  528. <div className="window-header-sub-title">
  529. {Locale.Chat.SubTitle(session.messages.length)}
  530. </div>
  531. </div>
  532. <div className="window-actions">
  533. <div className={"window-action-button" + " " + styles.mobile}>
  534. <IconButton
  535. icon={<ReturnIcon />}
  536. bordered
  537. title={Locale.Chat.Actions.ChatList}
  538. onClick={() => navigate(Path.Home)}
  539. />
  540. </div>
  541. <div className="window-action-button">
  542. <IconButton
  543. icon={<RenameIcon />}
  544. bordered
  545. onClick={renameSession}
  546. />
  547. </div>
  548. <div className="window-action-button">
  549. <IconButton
  550. icon={<ExportIcon />}
  551. bordered
  552. title={Locale.Chat.Actions.Export}
  553. onClick={() => {
  554. exportMessages(
  555. session.messages.filter((msg) => !msg.isError),
  556. session.topic,
  557. );
  558. }}
  559. />
  560. </div>
  561. {!isMobileScreen && (
  562. <div className="window-action-button">
  563. <IconButton
  564. icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
  565. bordered
  566. onClick={() => {
  567. config.update(
  568. (config) => (config.tightBorder = !config.tightBorder),
  569. );
  570. }}
  571. />
  572. </div>
  573. )}
  574. </div>
  575. <PromptToast
  576. showToast={!hitBottom}
  577. showModal={showPromptModal}
  578. setShowModal={setShowPromptModal}
  579. />
  580. </div>
  581. <div
  582. className={styles["chat-body"]}
  583. ref={scrollRef}
  584. onScroll={(e) => onChatBodyScroll(e.currentTarget)}
  585. onMouseDown={() => inputRef.current?.blur()}
  586. onWheel={(e) => setAutoScroll(hitBottom && e.deltaY > 0)}
  587. onTouchStart={() => {
  588. inputRef.current?.blur();
  589. setAutoScroll(false);
  590. }}
  591. >
  592. {messages.map((message, i) => {
  593. const isUser = message.role === "user";
  594. const showActions =
  595. !isUser &&
  596. i > 0 &&
  597. !(message.preview || message.content.length === 0);
  598. const showTyping = message.preview || message.streaming;
  599. return (
  600. <div
  601. key={i}
  602. className={
  603. isUser ? styles["chat-message-user"] : styles["chat-message"]
  604. }
  605. >
  606. <div className={styles["chat-message-container"]}>
  607. <div className={styles["chat-message-avatar"]}>
  608. {message.role === "user" ? (
  609. <Avatar avatar={config.avatar} />
  610. ) : session.mask.id === DEFAULT_MASK_ID ? (
  611. <Avatar model={message.model ?? "gpt-3.5-turbo"} />
  612. ) : (
  613. <Avatar avatar={session.mask.avatar} />
  614. )}
  615. </div>
  616. {showTyping && (
  617. <div className={styles["chat-message-status"]}>
  618. {Locale.Chat.Typing}
  619. </div>
  620. )}
  621. <div className={styles["chat-message-item"]}>
  622. {showActions && (
  623. <div className={styles["chat-message-top-actions"]}>
  624. {message.streaming ? (
  625. <div
  626. className={styles["chat-message-top-action"]}
  627. onClick={() => onUserStop(message.id ?? i)}
  628. >
  629. {Locale.Chat.Actions.Stop}
  630. </div>
  631. ) : (
  632. <>
  633. <div
  634. className={styles["chat-message-top-action"]}
  635. onClick={() => onDelete(message.id ?? i)}
  636. >
  637. {Locale.Chat.Actions.Delete}
  638. </div>
  639. <div
  640. className={styles["chat-message-top-action"]}
  641. onClick={() => onResend(message.id ?? i)}
  642. >
  643. {Locale.Chat.Actions.Retry}
  644. </div>
  645. </>
  646. )}
  647. <div
  648. className={styles["chat-message-top-action"]}
  649. onClick={() => copyToClipboard(message.content)}
  650. >
  651. {Locale.Chat.Actions.Copy}
  652. </div>
  653. </div>
  654. )}
  655. <Markdown
  656. content={message.content}
  657. loading={
  658. (message.preview || message.content.length === 0) &&
  659. !isUser
  660. }
  661. onContextMenu={(e) => onRightClick(e, message)}
  662. onDoubleClickCapture={() => {
  663. if (!isMobileScreen) return;
  664. setUserInput(message.content);
  665. }}
  666. fontSize={fontSize}
  667. parentRef={scrollRef}
  668. />
  669. </div>
  670. {!isUser && !message.preview && (
  671. <div className={styles["chat-message-actions"]}>
  672. <div className={styles["chat-message-action-date"]}>
  673. {message.date.toLocaleString()}
  674. </div>
  675. </div>
  676. )}
  677. </div>
  678. </div>
  679. );
  680. })}
  681. </div>
  682. <div className={styles["chat-input-panel"]}>
  683. <PromptHints prompts={promptHints} onPromptSelect={onPromptSelect} />
  684. <ChatActions
  685. showPromptModal={() => setShowPromptModal(true)}
  686. scrollToBottom={scrollToBottom}
  687. hitBottom={hitBottom}
  688. showPromptHints={() => {
  689. inputRef.current?.focus();
  690. onSearch("");
  691. }}
  692. />
  693. <div className={styles["chat-input-panel-inner"]}>
  694. <textarea
  695. ref={inputRef}
  696. className={styles["chat-input"]}
  697. placeholder={Locale.Chat.Input(submitKey)}
  698. onInput={(e) => onInput(e.currentTarget.value)}
  699. value={userInput}
  700. onKeyDown={onInputKeyDown}
  701. onFocus={() => setAutoScroll(true)}
  702. onBlur={() => {
  703. setTimeout(() => {
  704. if (document.activeElement !== inputRef.current) {
  705. setAutoScroll(false);
  706. setPromptHints([]);
  707. }
  708. }, 100);
  709. }}
  710. autoFocus
  711. rows={inputRows}
  712. />
  713. <IconButton
  714. icon={<SendWhiteIcon />}
  715. text={Locale.Chat.Send}
  716. className={styles["chat-input-send"]}
  717. noDark
  718. onClick={onUserSubmit}
  719. />
  720. </div>
  721. </div>
  722. </div>
  723. );
  724. }