chat.tsx 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139
  1. import { useDebouncedCallback } from "use-debounce";
  2. import React, {
  3. Fragment,
  4. RefObject,
  5. useCallback,
  6. useEffect,
  7. useMemo,
  8. useRef,
  9. useState,
  10. } from "react";
  11. import SendWhiteIcon from "../icons/send-white.svg";
  12. import BrainIcon from "../icons/brain.svg";
  13. import RenameIcon from "../icons/rename.svg";
  14. import EditIcon from "../icons/rename.svg";
  15. import ExportIcon from "../icons/share.svg";
  16. import ReturnIcon from "../icons/return.svg";
  17. import CopyIcon from "../icons/copy.svg";
  18. import SpeakIcon from "../icons/speak.svg";
  19. import SpeakStopIcon from "../icons/speak-stop.svg";
  20. import LoadingIcon from "../icons/three-dots.svg";
  21. import LoadingButtonIcon from "../icons/loading.svg";
  22. import PromptIcon from "../icons/prompt.svg";
  23. import MaskIcon from "../icons/mask.svg";
  24. import MaxIcon from "../icons/max.svg";
  25. import MinIcon from "../icons/min.svg";
  26. import ResetIcon from "../icons/reload.svg";
  27. import ReloadIcon from "../icons/reload.svg";
  28. import BreakIcon from "../icons/break.svg";
  29. import SettingsIcon from "../icons/chat-settings.svg";
  30. import DeleteIcon from "../icons/clear.svg";
  31. import PinIcon from "../icons/pin.svg";
  32. import ConfirmIcon from "../icons/confirm.svg";
  33. import CloseIcon from "../icons/close.svg";
  34. import CancelIcon from "../icons/cancel.svg";
  35. import ImageIcon from "../icons/image.svg";
  36. import LightIcon from "../icons/light.svg";
  37. import DarkIcon from "../icons/dark.svg";
  38. import AutoIcon from "../icons/auto.svg";
  39. import BottomIcon from "../icons/bottom.svg";
  40. import StopIcon from "../icons/pause.svg";
  41. import RobotIcon from "../icons/robot.svg";
  42. import SizeIcon from "../icons/size.svg";
  43. import QualityIcon from "../icons/hd.svg";
  44. import StyleIcon from "../icons/palette.svg";
  45. import PluginIcon from "../icons/plugin.svg";
  46. import ShortcutkeyIcon from "../icons/shortcutkey.svg";
  47. import McpToolIcon from "../icons/tool.svg";
  48. import HeadphoneIcon from "../icons/headphone.svg";
  49. import {
  50. BOT_HELLO,
  51. ChatMessage,
  52. createMessage,
  53. DEFAULT_TOPIC,
  54. ModelType,
  55. SubmitKey,
  56. Theme,
  57. useAccessStore,
  58. useAppConfig,
  59. useChatStore,
  60. usePluginStore,
  61. } from "../store";
  62. import {
  63. autoGrowTextArea,
  64. copyToClipboard,
  65. getMessageImages,
  66. getMessageTextContent,
  67. isDalle3,
  68. isVisionModel,
  69. safeLocalStorage,
  70. selectOrCopy,
  71. showPlugins,
  72. useMobileScreen,
  73. } from "../utils";
  74. import { uploadImage as uploadImageRemote } from "@/app/utils/chat";
  75. import dynamic from "next/dynamic";
  76. import { ChatControllerPool } from "../client/controller";
  77. import { DalleQuality, DalleSize, DalleStyle } from "../typing";
  78. import { Prompt, usePromptStore } from "../store/prompt";
  79. import Locale from "../locales";
  80. import { IconButton } from "./button";
  81. import styles from "./chat.module.scss";
  82. import {
  83. List,
  84. ListItem,
  85. Modal,
  86. Selector,
  87. showConfirm,
  88. showPrompt,
  89. showToast,
  90. } from "./ui-lib";
  91. import { useNavigate } from "react-router-dom";
  92. import {
  93. CHAT_PAGE_SIZE,
  94. DEFAULT_TTS_ENGINE,
  95. ModelProvider,
  96. Path,
  97. REQUEST_TIMEOUT_MS,
  98. ServiceProvider,
  99. UNFINISHED_INPUT,
  100. } from "../constant";
  101. import { Avatar } from "./emoji";
  102. import { ContextPrompts, MaskAvatar, MaskConfig } from "./mask";
  103. import { useMaskStore } from "../store/mask";
  104. import { ChatCommandPrefix, useChatCommand, useCommand } from "../command";
  105. import { prettyObject } from "../utils/format";
  106. import { ExportMessageModal } from "./exporter";
  107. import { getClientConfig } from "../config/client";
  108. import { useAllModels } from "../utils/hooks";
  109. import { ClientApi, MultimodalContent } from "../client/api";
  110. import { createTTSPlayer } from "../utils/audio";
  111. import { MsEdgeTTS, OUTPUT_FORMAT } from "../utils/ms_edge_tts";
  112. import { isEmpty } from "lodash-es";
  113. import { getModelProvider } from "../utils/model";
  114. import { RealtimeChat } from "@/app/components/realtime-chat";
  115. import clsx from "clsx";
  116. import { getAvailableClientsCount } from "../mcp/actions";
  117. const localStorage = safeLocalStorage();
  118. const ttsPlayer = createTTSPlayer();
  119. const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
  120. loading: () => <LoadingIcon />,
  121. });
  122. const MCPAction = () => {
  123. const navigate = useNavigate();
  124. const [count, setCount] = useState<number>(0);
  125. useEffect(() => {
  126. const loadCount = async () => {
  127. const count = await getAvailableClientsCount();
  128. setCount(count);
  129. };
  130. loadCount();
  131. }, []);
  132. return (
  133. <ChatAction
  134. onClick={() => navigate(Path.McpMarket)}
  135. text={`MCP${count ? ` (${count})` : ""}`}
  136. icon={<McpToolIcon />}
  137. />
  138. );
  139. };
  140. export function SessionConfigModel(props: { onClose: () => void }) {
  141. const chatStore = useChatStore();
  142. const session = chatStore.currentSession();
  143. const maskStore = useMaskStore();
  144. const navigate = useNavigate();
  145. return (
  146. <div className="modal-mask">
  147. <Modal
  148. title={Locale.Context.Edit}
  149. onClose={() => props.onClose()}
  150. actions={[
  151. <IconButton
  152. key="reset"
  153. icon={<ResetIcon />}
  154. bordered
  155. text={Locale.Chat.Config.Reset}
  156. onClick={async () => {
  157. if (await showConfirm(Locale.Memory.ResetConfirm)) {
  158. chatStore.updateTargetSession(
  159. session,
  160. (session) => (session.memoryPrompt = ""),
  161. );
  162. }
  163. }}
  164. />,
  165. <IconButton
  166. key="copy"
  167. icon={<CopyIcon />}
  168. bordered
  169. text={Locale.Chat.Config.SaveAs}
  170. onClick={() => {
  171. navigate(Path.Masks);
  172. setTimeout(() => {
  173. maskStore.create(session.mask);
  174. }, 500);
  175. }}
  176. />,
  177. ]}
  178. >
  179. <MaskConfig
  180. mask={session.mask}
  181. updateMask={(updater) => {
  182. const mask = { ...session.mask };
  183. updater(mask);
  184. chatStore.updateTargetSession(
  185. session,
  186. (session) => (session.mask = mask),
  187. );
  188. }}
  189. shouldSyncFromGlobal
  190. extraListItems={
  191. session.mask.modelConfig.sendMemory ? (
  192. <ListItem
  193. className="copyable"
  194. title={`${Locale.Memory.Title} (${session.lastSummarizeIndex} of ${session.messages.length})`}
  195. subTitle={session.memoryPrompt || Locale.Memory.EmptyContent}
  196. ></ListItem>
  197. ) : (
  198. <></>
  199. )
  200. }
  201. ></MaskConfig>
  202. </Modal>
  203. </div>
  204. );
  205. }
  206. function PromptToast(props: {
  207. showToast?: boolean;
  208. showModal?: boolean;
  209. setShowModal: (_: boolean) => void;
  210. }) {
  211. const chatStore = useChatStore();
  212. const session = chatStore.currentSession();
  213. const context = session.mask.context;
  214. return (
  215. <div className={styles["prompt-toast"]} key="prompt-toast">
  216. {props.showToast && context.length > 0 && (
  217. <div
  218. className={clsx(styles["prompt-toast-inner"], "clickable")}
  219. role="button"
  220. onClick={() => props.setShowModal(true)}
  221. >
  222. <BrainIcon />
  223. <span className={styles["prompt-toast-content"]}>
  224. {Locale.Context.Toast(context.length)}
  225. </span>
  226. </div>
  227. )}
  228. {props.showModal && (
  229. <SessionConfigModel onClose={() => props.setShowModal(false)} />
  230. )}
  231. </div>
  232. );
  233. }
  234. function useSubmitHandler() {
  235. const config = useAppConfig();
  236. const submitKey = config.submitKey;
  237. const isComposing = useRef(false);
  238. useEffect(() => {
  239. const onCompositionStart = () => {
  240. isComposing.current = true;
  241. };
  242. const onCompositionEnd = () => {
  243. isComposing.current = false;
  244. };
  245. window.addEventListener("compositionstart", onCompositionStart);
  246. window.addEventListener("compositionend", onCompositionEnd);
  247. return () => {
  248. window.removeEventListener("compositionstart", onCompositionStart);
  249. window.removeEventListener("compositionend", onCompositionEnd);
  250. };
  251. }, []);
  252. const shouldSubmit = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  253. // Fix Chinese input method "Enter" on Safari
  254. if (e.keyCode == 229) return false;
  255. if (e.key !== "Enter") return false;
  256. if (e.key === "Enter" && (e.nativeEvent.isComposing || isComposing.current))
  257. return false;
  258. return (
  259. (config.submitKey === SubmitKey.AltEnter && e.altKey) ||
  260. (config.submitKey === SubmitKey.CtrlEnter && e.ctrlKey) ||
  261. (config.submitKey === SubmitKey.ShiftEnter && e.shiftKey) ||
  262. (config.submitKey === SubmitKey.MetaEnter && e.metaKey) ||
  263. (config.submitKey === SubmitKey.Enter &&
  264. !e.altKey &&
  265. !e.ctrlKey &&
  266. !e.shiftKey &&
  267. !e.metaKey)
  268. );
  269. };
  270. return {
  271. submitKey,
  272. shouldSubmit,
  273. };
  274. }
  275. export type RenderPrompt = Pick<Prompt, "title" | "content">;
  276. export function PromptHints(props: {
  277. prompts: RenderPrompt[];
  278. onPromptSelect: (prompt: RenderPrompt) => void;
  279. }) {
  280. const noPrompts = props.prompts.length === 0;
  281. const [selectIndex, setSelectIndex] = useState(0);
  282. const selectedRef = useRef<HTMLDivElement>(null);
  283. useEffect(() => {
  284. setSelectIndex(0);
  285. }, [props.prompts.length]);
  286. useEffect(() => {
  287. const onKeyDown = (e: KeyboardEvent) => {
  288. if (noPrompts || e.metaKey || e.altKey || e.ctrlKey) {
  289. return;
  290. }
  291. // arrow up / down to select prompt
  292. const changeIndex = (delta: number) => {
  293. e.stopPropagation();
  294. e.preventDefault();
  295. const nextIndex = Math.max(
  296. 0,
  297. Math.min(props.prompts.length - 1, selectIndex + delta),
  298. );
  299. setSelectIndex(nextIndex);
  300. selectedRef.current?.scrollIntoView({
  301. block: "center",
  302. });
  303. };
  304. if (e.key === "ArrowUp") {
  305. changeIndex(1);
  306. } else if (e.key === "ArrowDown") {
  307. changeIndex(-1);
  308. } else if (e.key === "Enter") {
  309. const selectedPrompt = props.prompts.at(selectIndex);
  310. if (selectedPrompt) {
  311. props.onPromptSelect(selectedPrompt);
  312. }
  313. }
  314. };
  315. window.addEventListener("keydown", onKeyDown);
  316. return () => window.removeEventListener("keydown", onKeyDown);
  317. // eslint-disable-next-line react-hooks/exhaustive-deps
  318. }, [props.prompts.length, selectIndex]);
  319. if (noPrompts) return null;
  320. return (
  321. <div className={styles["prompt-hints"]}>
  322. {props.prompts.map((prompt, i) => (
  323. <div
  324. ref={i === selectIndex ? selectedRef : null}
  325. className={clsx(styles["prompt-hint"], {
  326. [styles["prompt-hint-selected"]]: i === selectIndex,
  327. })}
  328. key={prompt.title + i.toString()}
  329. onClick={() => props.onPromptSelect(prompt)}
  330. onMouseEnter={() => setSelectIndex(i)}
  331. >
  332. <div className={styles["hint-title"]}>{prompt.title}</div>
  333. <div className={styles["hint-content"]}>{prompt.content}</div>
  334. </div>
  335. ))}
  336. </div>
  337. );
  338. }
  339. function ClearContextDivider() {
  340. const chatStore = useChatStore();
  341. const session = chatStore.currentSession();
  342. return (
  343. <div
  344. className={styles["clear-context"]}
  345. onClick={() =>
  346. chatStore.updateTargetSession(
  347. session,
  348. (session) => (session.clearContextIndex = undefined),
  349. )
  350. }
  351. >
  352. <div className={styles["clear-context-tips"]}>{Locale.Context.Clear}</div>
  353. <div className={styles["clear-context-revert-btn"]}>
  354. {Locale.Context.Revert}
  355. </div>
  356. </div>
  357. );
  358. }
  359. export function ChatAction(props: {
  360. text: string;
  361. icon: JSX.Element;
  362. onClick: () => void;
  363. }) {
  364. const iconRef = useRef<HTMLDivElement>(null);
  365. const textRef = useRef<HTMLDivElement>(null);
  366. const [width, setWidth] = useState({
  367. full: 16,
  368. icon: 16,
  369. });
  370. function updateWidth() {
  371. if (!iconRef.current || !textRef.current) return;
  372. const getWidth = (dom: HTMLDivElement) => dom.getBoundingClientRect().width;
  373. const textWidth = getWidth(textRef.current);
  374. const iconWidth = getWidth(iconRef.current);
  375. setWidth({
  376. full: textWidth + iconWidth,
  377. icon: iconWidth,
  378. });
  379. }
  380. return (
  381. <div
  382. className={clsx(styles["chat-input-action"], "clickable")}
  383. onClick={() => {
  384. props.onClick();
  385. setTimeout(updateWidth, 1);
  386. }}
  387. onMouseEnter={updateWidth}
  388. onTouchStart={updateWidth}
  389. style={
  390. {
  391. "--icon-width": `${width.icon}px`,
  392. "--full-width": `${width.full}px`,
  393. } as React.CSSProperties
  394. }
  395. >
  396. <div ref={iconRef} className={styles["icon"]}>
  397. {props.icon}
  398. </div>
  399. <div className={styles["text"]} ref={textRef}>
  400. {props.text}
  401. </div>
  402. </div>
  403. );
  404. }
  405. function useScrollToBottom(
  406. scrollRef: RefObject<HTMLDivElement>,
  407. detach: boolean = false,
  408. messages: ChatMessage[],
  409. ) {
  410. // for auto-scroll
  411. const [autoScroll, setAutoScroll] = useState(true);
  412. const scrollDomToBottom = useCallback(() => {
  413. const dom = scrollRef.current;
  414. if (dom) {
  415. requestAnimationFrame(() => {
  416. setAutoScroll(true);
  417. dom.scrollTo(0, dom.scrollHeight);
  418. });
  419. }
  420. }, [scrollRef]);
  421. // auto scroll
  422. useEffect(() => {
  423. if (autoScroll && !detach) {
  424. scrollDomToBottom();
  425. }
  426. });
  427. // auto scroll when messages length changes
  428. const lastMessagesLength = useRef(messages.length);
  429. useEffect(() => {
  430. if (messages.length > lastMessagesLength.current && !detach) {
  431. scrollDomToBottom();
  432. }
  433. lastMessagesLength.current = messages.length;
  434. }, [messages.length, detach, scrollDomToBottom]);
  435. return {
  436. scrollRef,
  437. autoScroll,
  438. setAutoScroll,
  439. scrollDomToBottom,
  440. };
  441. }
  442. export function ChatActions(props: {
  443. uploadImage: () => void;
  444. setAttachImages: (images: string[]) => void;
  445. setUploading: (uploading: boolean) => void;
  446. showPromptModal: () => void;
  447. scrollToBottom: () => void;
  448. showPromptHints: () => void;
  449. hitBottom: boolean;
  450. uploading: boolean;
  451. setShowShortcutKeyModal: React.Dispatch<React.SetStateAction<boolean>>;
  452. setUserInput: (input: string) => void;
  453. setShowChatSidePanel: React.Dispatch<React.SetStateAction<boolean>>;
  454. }) {
  455. const config = useAppConfig();
  456. const navigate = useNavigate();
  457. const chatStore = useChatStore();
  458. const pluginStore = usePluginStore();
  459. const session = chatStore.currentSession();
  460. // switch themes
  461. const theme = config.theme;
  462. function nextTheme() {
  463. const themes = [Theme.Auto, Theme.Light, Theme.Dark];
  464. const themeIndex = themes.indexOf(theme);
  465. const nextIndex = (themeIndex + 1) % themes.length;
  466. const nextTheme = themes[nextIndex];
  467. config.update((config) => (config.theme = nextTheme));
  468. }
  469. // stop all responses
  470. const couldStop = ChatControllerPool.hasPending();
  471. const stopAll = () => ChatControllerPool.stopAll();
  472. // switch model
  473. const currentModel = session.mask.modelConfig.model;
  474. const currentProviderName =
  475. session.mask.modelConfig?.providerName || ServiceProvider.OpenAI;
  476. const allModels = useAllModels();
  477. const models = useMemo(() => {
  478. const filteredModels = allModels.filter((m) => m.available);
  479. const defaultModel = filteredModels.find((m) => m.isDefault);
  480. if (defaultModel) {
  481. const arr = [
  482. defaultModel,
  483. ...filteredModels.filter((m) => m !== defaultModel),
  484. ];
  485. return arr;
  486. } else {
  487. return filteredModels;
  488. }
  489. }, [allModels]);
  490. const currentModelName = useMemo(() => {
  491. const model = models.find(
  492. (m) =>
  493. m.name == currentModel &&
  494. m?.provider?.providerName == currentProviderName,
  495. );
  496. return model?.displayName ?? "";
  497. }, [models, currentModel, currentProviderName]);
  498. const [showModelSelector, setShowModelSelector] = useState(false);
  499. const [showPluginSelector, setShowPluginSelector] = useState(false);
  500. const [showUploadImage, setShowUploadImage] = useState(false);
  501. const [showSizeSelector, setShowSizeSelector] = useState(false);
  502. const [showQualitySelector, setShowQualitySelector] = useState(false);
  503. const [showStyleSelector, setShowStyleSelector] = useState(false);
  504. const dalle3Sizes: DalleSize[] = ["1024x1024", "1792x1024", "1024x1792"];
  505. const dalle3Qualitys: DalleQuality[] = ["standard", "hd"];
  506. const dalle3Styles: DalleStyle[] = ["vivid", "natural"];
  507. const currentSize = session.mask.modelConfig?.size ?? "1024x1024";
  508. const currentQuality = session.mask.modelConfig?.quality ?? "standard";
  509. const currentStyle = session.mask.modelConfig?.style ?? "vivid";
  510. const isMobileScreen = useMobileScreen();
  511. useEffect(() => {
  512. const show = isVisionModel(currentModel);
  513. setShowUploadImage(show);
  514. if (!show) {
  515. props.setAttachImages([]);
  516. props.setUploading(false);
  517. }
  518. // if current model is not available
  519. // switch to first available model
  520. const isUnavailableModel = !models.some((m) => m.name === currentModel);
  521. if (isUnavailableModel && models.length > 0) {
  522. // show next model to default model if exist
  523. let nextModel = models.find((model) => model.isDefault) || models[0];
  524. chatStore.updateTargetSession(session, (session) => {
  525. session.mask.modelConfig.model = nextModel.name;
  526. session.mask.modelConfig.providerName = nextModel?.provider
  527. ?.providerName as ServiceProvider;
  528. });
  529. showToast(
  530. nextModel?.provider?.providerName == "ByteDance"
  531. ? nextModel.displayName
  532. : nextModel.name,
  533. );
  534. }
  535. }, [chatStore, currentModel, models, session]);
  536. return (
  537. <div className={styles["chat-input-actions"]}>
  538. <>
  539. {couldStop && (
  540. <ChatAction
  541. onClick={stopAll}
  542. text={Locale.Chat.InputActions.Stop}
  543. icon={<StopIcon />}
  544. />
  545. )}
  546. {!props.hitBottom && (
  547. <ChatAction
  548. onClick={props.scrollToBottom}
  549. text={Locale.Chat.InputActions.ToBottom}
  550. icon={<BottomIcon />}
  551. />
  552. )}
  553. {props.hitBottom && (
  554. <ChatAction
  555. onClick={props.showPromptModal}
  556. text={Locale.Chat.InputActions.Settings}
  557. icon={<SettingsIcon />}
  558. />
  559. )}
  560. {showUploadImage && (
  561. <ChatAction
  562. onClick={props.uploadImage}
  563. text={Locale.Chat.InputActions.UploadImage}
  564. icon={props.uploading ? <LoadingButtonIcon /> : <ImageIcon />}
  565. />
  566. )}
  567. <ChatAction
  568. onClick={nextTheme}
  569. text={Locale.Chat.InputActions.Theme[theme]}
  570. icon={
  571. <>
  572. {theme === Theme.Auto ? (
  573. <AutoIcon />
  574. ) : theme === Theme.Light ? (
  575. <LightIcon />
  576. ) : theme === Theme.Dark ? (
  577. <DarkIcon />
  578. ) : null}
  579. </>
  580. }
  581. />
  582. <ChatAction
  583. onClick={props.showPromptHints}
  584. text={Locale.Chat.InputActions.Prompt}
  585. icon={<PromptIcon />}
  586. />
  587. <ChatAction
  588. onClick={() => {
  589. navigate(Path.Masks);
  590. }}
  591. text={Locale.Chat.InputActions.Masks}
  592. icon={<MaskIcon />}
  593. />
  594. <ChatAction
  595. text={Locale.Chat.InputActions.Clear}
  596. icon={<BreakIcon />}
  597. onClick={() => {
  598. chatStore.updateTargetSession(session, (session) => {
  599. if (session.clearContextIndex === session.messages.length) {
  600. session.clearContextIndex = undefined;
  601. } else {
  602. session.clearContextIndex = session.messages.length;
  603. session.memoryPrompt = ""; // will clear memory
  604. }
  605. });
  606. }}
  607. />
  608. <ChatAction
  609. onClick={() => setShowModelSelector(true)}
  610. text={currentModelName}
  611. icon={<RobotIcon />}
  612. />
  613. {showModelSelector && (
  614. <Selector
  615. defaultSelectedValue={`${currentModel}@${currentProviderName}`}
  616. items={models.map((m) => ({
  617. title: `${m.displayName}${
  618. m?.provider?.providerName
  619. ? " (" + m?.provider?.providerName + ")"
  620. : ""
  621. }`,
  622. value: `${m.name}@${m?.provider?.providerName}`,
  623. }))}
  624. onClose={() => setShowModelSelector(false)}
  625. onSelection={(s) => {
  626. if (s.length === 0) return;
  627. const [model, providerName] = getModelProvider(s[0]);
  628. chatStore.updateTargetSession(session, (session) => {
  629. session.mask.modelConfig.model = model as ModelType;
  630. session.mask.modelConfig.providerName =
  631. providerName as ServiceProvider;
  632. session.mask.syncGlobalConfig = false;
  633. });
  634. if (providerName == "ByteDance") {
  635. const selectedModel = models.find(
  636. (m) =>
  637. m.name == model &&
  638. m?.provider?.providerName == providerName,
  639. );
  640. showToast(selectedModel?.displayName ?? "");
  641. } else {
  642. showToast(model);
  643. }
  644. }}
  645. />
  646. )}
  647. {isDalle3(currentModel) && (
  648. <ChatAction
  649. onClick={() => setShowSizeSelector(true)}
  650. text={currentSize}
  651. icon={<SizeIcon />}
  652. />
  653. )}
  654. {showSizeSelector && (
  655. <Selector
  656. defaultSelectedValue={currentSize}
  657. items={dalle3Sizes.map((m) => ({
  658. title: m,
  659. value: m,
  660. }))}
  661. onClose={() => setShowSizeSelector(false)}
  662. onSelection={(s) => {
  663. if (s.length === 0) return;
  664. const size = s[0];
  665. chatStore.updateTargetSession(session, (session) => {
  666. session.mask.modelConfig.size = size;
  667. });
  668. showToast(size);
  669. }}
  670. />
  671. )}
  672. {isDalle3(currentModel) && (
  673. <ChatAction
  674. onClick={() => setShowQualitySelector(true)}
  675. text={currentQuality}
  676. icon={<QualityIcon />}
  677. />
  678. )}
  679. {showQualitySelector && (
  680. <Selector
  681. defaultSelectedValue={currentQuality}
  682. items={dalle3Qualitys.map((m) => ({
  683. title: m,
  684. value: m,
  685. }))}
  686. onClose={() => setShowQualitySelector(false)}
  687. onSelection={(q) => {
  688. if (q.length === 0) return;
  689. const quality = q[0];
  690. chatStore.updateTargetSession(session, (session) => {
  691. session.mask.modelConfig.quality = quality;
  692. });
  693. showToast(quality);
  694. }}
  695. />
  696. )}
  697. {isDalle3(currentModel) && (
  698. <ChatAction
  699. onClick={() => setShowStyleSelector(true)}
  700. text={currentStyle}
  701. icon={<StyleIcon />}
  702. />
  703. )}
  704. {showStyleSelector && (
  705. <Selector
  706. defaultSelectedValue={currentStyle}
  707. items={dalle3Styles.map((m) => ({
  708. title: m,
  709. value: m,
  710. }))}
  711. onClose={() => setShowStyleSelector(false)}
  712. onSelection={(s) => {
  713. if (s.length === 0) return;
  714. const style = s[0];
  715. chatStore.updateTargetSession(session, (session) => {
  716. session.mask.modelConfig.style = style;
  717. });
  718. showToast(style);
  719. }}
  720. />
  721. )}
  722. {showPlugins(currentProviderName, currentModel) && (
  723. <ChatAction
  724. onClick={() => {
  725. if (pluginStore.getAll().length == 0) {
  726. navigate(Path.Plugins);
  727. } else {
  728. setShowPluginSelector(true);
  729. }
  730. }}
  731. text={Locale.Plugin.Name}
  732. icon={<PluginIcon />}
  733. />
  734. )}
  735. {showPluginSelector && (
  736. <Selector
  737. multiple
  738. defaultSelectedValue={chatStore.currentSession().mask?.plugin}
  739. items={pluginStore.getAll().map((item) => ({
  740. title: `${item?.title}@${item?.version}`,
  741. value: item?.id,
  742. }))}
  743. onClose={() => setShowPluginSelector(false)}
  744. onSelection={(s) => {
  745. chatStore.updateTargetSession(session, (session) => {
  746. session.mask.plugin = s as string[];
  747. });
  748. }}
  749. />
  750. )}
  751. {!isMobileScreen && (
  752. <ChatAction
  753. onClick={() => props.setShowShortcutKeyModal(true)}
  754. text={Locale.Chat.ShortcutKey.Title}
  755. icon={<ShortcutkeyIcon />}
  756. />
  757. )}
  758. {!isMobileScreen && <MCPAction />}
  759. </>
  760. <div className={styles["chat-input-actions-end"]}>
  761. {config.realtimeConfig.enable && (
  762. <ChatAction
  763. onClick={() => props.setShowChatSidePanel(true)}
  764. text={"Realtime Chat"}
  765. icon={<HeadphoneIcon />}
  766. />
  767. )}
  768. </div>
  769. </div>
  770. );
  771. }
  772. export function EditMessageModal(props: { onClose: () => void }) {
  773. const chatStore = useChatStore();
  774. const session = chatStore.currentSession();
  775. const [messages, setMessages] = useState(session.messages.slice());
  776. return (
  777. <div className="modal-mask">
  778. <Modal
  779. title={Locale.Chat.EditMessage.Title}
  780. onClose={props.onClose}
  781. actions={[
  782. <IconButton
  783. text={Locale.UI.Cancel}
  784. icon={<CancelIcon />}
  785. key="cancel"
  786. onClick={() => {
  787. props.onClose();
  788. }}
  789. />,
  790. <IconButton
  791. type="primary"
  792. text={Locale.UI.Confirm}
  793. icon={<ConfirmIcon />}
  794. key="ok"
  795. onClick={() => {
  796. chatStore.updateTargetSession(
  797. session,
  798. (session) => (session.messages = messages),
  799. );
  800. props.onClose();
  801. }}
  802. />,
  803. ]}
  804. >
  805. <List>
  806. <ListItem
  807. title={Locale.Chat.EditMessage.Topic.Title}
  808. subTitle={Locale.Chat.EditMessage.Topic.SubTitle}
  809. >
  810. <input
  811. type="text"
  812. value={session.topic}
  813. onInput={(e) =>
  814. chatStore.updateTargetSession(
  815. session,
  816. (session) => (session.topic = e.currentTarget.value),
  817. )
  818. }
  819. ></input>
  820. </ListItem>
  821. </List>
  822. <ContextPrompts
  823. context={messages}
  824. updateContext={(updater) => {
  825. const newMessages = messages.slice();
  826. updater(newMessages);
  827. setMessages(newMessages);
  828. }}
  829. />
  830. </Modal>
  831. </div>
  832. );
  833. }
  834. export function DeleteImageButton(props: { deleteImage: () => void }) {
  835. return (
  836. <div className={styles["delete-image"]} onClick={props.deleteImage}>
  837. <DeleteIcon />
  838. </div>
  839. );
  840. }
  841. export function ShortcutKeyModal(props: { onClose: () => void }) {
  842. const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0;
  843. const shortcuts = [
  844. {
  845. title: Locale.Chat.ShortcutKey.newChat,
  846. keys: isMac ? ["⌘", "Shift", "O"] : ["Ctrl", "Shift", "O"],
  847. },
  848. { title: Locale.Chat.ShortcutKey.focusInput, keys: ["Shift", "Esc"] },
  849. {
  850. title: Locale.Chat.ShortcutKey.copyLastCode,
  851. keys: isMac ? ["⌘", "Shift", ";"] : ["Ctrl", "Shift", ";"],
  852. },
  853. {
  854. title: Locale.Chat.ShortcutKey.copyLastMessage,
  855. keys: isMac ? ["⌘", "Shift", "C"] : ["Ctrl", "Shift", "C"],
  856. },
  857. {
  858. title: Locale.Chat.ShortcutKey.showShortcutKey,
  859. keys: isMac ? ["⌘", "/"] : ["Ctrl", "/"],
  860. },
  861. ];
  862. return (
  863. <div className="modal-mask">
  864. <Modal
  865. title={Locale.Chat.ShortcutKey.Title}
  866. onClose={props.onClose}
  867. actions={[
  868. <IconButton
  869. type="primary"
  870. text={Locale.UI.Confirm}
  871. icon={<ConfirmIcon />}
  872. key="ok"
  873. onClick={() => {
  874. props.onClose();
  875. }}
  876. />,
  877. ]}
  878. >
  879. <div className={styles["shortcut-key-container"]}>
  880. <div className={styles["shortcut-key-grid"]}>
  881. {shortcuts.map((shortcut, index) => (
  882. <div key={index} className={styles["shortcut-key-item"]}>
  883. <div className={styles["shortcut-key-title"]}>
  884. {shortcut.title}
  885. </div>
  886. <div className={styles["shortcut-key-keys"]}>
  887. {shortcut.keys.map((key, i) => (
  888. <div key={i} className={styles["shortcut-key"]}>
  889. <span>{key}</span>
  890. </div>
  891. ))}
  892. </div>
  893. </div>
  894. ))}
  895. </div>
  896. </div>
  897. </Modal>
  898. </div>
  899. );
  900. }
  901. function _Chat() {
  902. type RenderMessage = ChatMessage & { preview?: boolean };
  903. const chatStore = useChatStore();
  904. const session = chatStore.currentSession();
  905. const config = useAppConfig();
  906. const fontSize = config.fontSize;
  907. const fontFamily = config.fontFamily;
  908. const [showExport, setShowExport] = useState(false);
  909. const inputRef = useRef<HTMLTextAreaElement>(null);
  910. const [userInput, setUserInput] = useState("");
  911. const [isLoading, setIsLoading] = useState(false);
  912. const { submitKey, shouldSubmit } = useSubmitHandler();
  913. const scrollRef = useRef<HTMLDivElement>(null);
  914. const isScrolledToBottom = scrollRef?.current
  915. ? Math.abs(
  916. scrollRef.current.scrollHeight -
  917. (scrollRef.current.scrollTop + scrollRef.current.clientHeight),
  918. ) <= 1
  919. : false;
  920. const isAttachWithTop = useMemo(() => {
  921. const lastMessage = scrollRef.current?.lastElementChild as HTMLElement;
  922. // if scrolllRef is not ready or no message, return false
  923. if (!scrollRef?.current || !lastMessage) return false;
  924. const topDistance =
  925. lastMessage!.getBoundingClientRect().top -
  926. scrollRef.current.getBoundingClientRect().top;
  927. // leave some space for user question
  928. return topDistance < 100;
  929. }, [scrollRef?.current?.scrollHeight]);
  930. const isTyping = userInput !== "";
  931. // if user is typing, should auto scroll to bottom
  932. // if user is not typing, should auto scroll to bottom only if already at bottom
  933. const { setAutoScroll, scrollDomToBottom } = useScrollToBottom(
  934. scrollRef,
  935. (isScrolledToBottom || isAttachWithTop) && !isTyping,
  936. session.messages,
  937. );
  938. const [hitBottom, setHitBottom] = useState(true);
  939. const isMobileScreen = useMobileScreen();
  940. const navigate = useNavigate();
  941. const [attachImages, setAttachImages] = useState<string[]>([]);
  942. const [uploading, setUploading] = useState(false);
  943. // prompt hints
  944. const promptStore = usePromptStore();
  945. const [promptHints, setPromptHints] = useState<RenderPrompt[]>([]);
  946. const onSearch = useDebouncedCallback(
  947. (text: string) => {
  948. const matchedPrompts = promptStore.search(text);
  949. setPromptHints(matchedPrompts);
  950. },
  951. 100,
  952. { leading: true, trailing: true },
  953. );
  954. // auto grow input
  955. const [inputRows, setInputRows] = useState(2);
  956. const measure = useDebouncedCallback(
  957. () => {
  958. const rows = inputRef.current ? autoGrowTextArea(inputRef.current) : 1;
  959. const inputRows = Math.min(
  960. 20,
  961. Math.max(2 + Number(!isMobileScreen), rows),
  962. );
  963. setInputRows(inputRows);
  964. },
  965. 100,
  966. {
  967. leading: true,
  968. trailing: true,
  969. },
  970. );
  971. // eslint-disable-next-line react-hooks/exhaustive-deps
  972. useEffect(measure, [userInput]);
  973. // chat commands shortcuts
  974. const chatCommands = useChatCommand({
  975. new: () => chatStore.newSession(),
  976. newm: () => navigate(Path.NewChat),
  977. prev: () => chatStore.nextSession(-1),
  978. next: () => chatStore.nextSession(1),
  979. clear: () =>
  980. chatStore.updateTargetSession(
  981. session,
  982. (session) => (session.clearContextIndex = session.messages.length),
  983. ),
  984. fork: () => chatStore.forkSession(),
  985. del: () => chatStore.deleteSession(chatStore.currentSessionIndex),
  986. });
  987. // only search prompts when user input is short
  988. const SEARCH_TEXT_LIMIT = 30;
  989. const onInput = (text: string) => {
  990. setUserInput(text);
  991. const n = text.trim().length;
  992. // clear search results
  993. if (n === 0) {
  994. setPromptHints([]);
  995. } else if (text.match(ChatCommandPrefix)) {
  996. setPromptHints(chatCommands.search(text));
  997. } else if (!config.disablePromptHint && n < SEARCH_TEXT_LIMIT) {
  998. // check if need to trigger auto completion
  999. if (text.startsWith("/")) {
  1000. let searchText = text.slice(1);
  1001. onSearch(searchText);
  1002. }
  1003. }
  1004. };
  1005. const doSubmit = (userInput: string) => {
  1006. if (userInput.trim() === "" && isEmpty(attachImages)) return;
  1007. const matchCommand = chatCommands.match(userInput);
  1008. if (matchCommand.matched) {
  1009. setUserInput("");
  1010. setPromptHints([]);
  1011. matchCommand.invoke();
  1012. return;
  1013. }
  1014. setIsLoading(true);
  1015. chatStore
  1016. .onUserInput(userInput, attachImages)
  1017. .then(() => setIsLoading(false));
  1018. setAttachImages([]);
  1019. chatStore.setLastInput(userInput);
  1020. setUserInput("");
  1021. setPromptHints([]);
  1022. if (!isMobileScreen) inputRef.current?.focus();
  1023. setAutoScroll(true);
  1024. };
  1025. const onPromptSelect = (prompt: RenderPrompt) => {
  1026. setTimeout(() => {
  1027. setPromptHints([]);
  1028. const matchedChatCommand = chatCommands.match(prompt.content);
  1029. if (matchedChatCommand.matched) {
  1030. // if user is selecting a chat command, just trigger it
  1031. matchedChatCommand.invoke();
  1032. setUserInput("");
  1033. } else {
  1034. // or fill the prompt
  1035. setUserInput(prompt.content);
  1036. }
  1037. inputRef.current?.focus();
  1038. }, 30);
  1039. };
  1040. // stop response
  1041. const onUserStop = (messageId: string) => {
  1042. ChatControllerPool.stop(session.id, messageId);
  1043. };
  1044. useEffect(() => {
  1045. chatStore.updateTargetSession(session, (session) => {
  1046. const stopTiming = Date.now() - REQUEST_TIMEOUT_MS;
  1047. session.messages.forEach((m) => {
  1048. // check if should stop all stale messages
  1049. if (m.isError || new Date(m.date).getTime() < stopTiming) {
  1050. if (m.streaming) {
  1051. m.streaming = false;
  1052. }
  1053. if (m.content.length === 0) {
  1054. m.isError = true;
  1055. m.content = prettyObject({
  1056. error: true,
  1057. message: "empty response",
  1058. });
  1059. }
  1060. }
  1061. });
  1062. // auto sync mask config from global config
  1063. if (session.mask.syncGlobalConfig) {
  1064. console.log("[Mask] syncing from global, name = ", session.mask.name);
  1065. session.mask.modelConfig = { ...config.modelConfig };
  1066. }
  1067. });
  1068. // eslint-disable-next-line react-hooks/exhaustive-deps
  1069. }, [session]);
  1070. // check if should send message
  1071. const onInputKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
  1072. // if ArrowUp and no userInput, fill with last input
  1073. if (
  1074. e.key === "ArrowUp" &&
  1075. userInput.length <= 0 &&
  1076. !(e.metaKey || e.altKey || e.ctrlKey)
  1077. ) {
  1078. setUserInput(chatStore.lastInput ?? "");
  1079. e.preventDefault();
  1080. return;
  1081. }
  1082. if (shouldSubmit(e) && promptHints.length === 0) {
  1083. doSubmit(userInput);
  1084. e.preventDefault();
  1085. }
  1086. };
  1087. const onRightClick = (e: any, message: ChatMessage) => {
  1088. // copy to clipboard
  1089. if (selectOrCopy(e.currentTarget, getMessageTextContent(message))) {
  1090. if (userInput.length === 0) {
  1091. setUserInput(getMessageTextContent(message));
  1092. }
  1093. e.preventDefault();
  1094. }
  1095. };
  1096. const deleteMessage = (msgId?: string) => {
  1097. chatStore.updateTargetSession(
  1098. session,
  1099. (session) =>
  1100. (session.messages = session.messages.filter((m) => m.id !== msgId)),
  1101. );
  1102. };
  1103. const onDelete = (msgId: string) => {
  1104. deleteMessage(msgId);
  1105. };
  1106. const onResend = (message: ChatMessage) => {
  1107. // when it is resending a message
  1108. // 1. for a user's message, find the next bot response
  1109. // 2. for a bot's message, find the last user's input
  1110. // 3. delete original user input and bot's message
  1111. // 4. resend the user's input
  1112. const resendingIndex = session.messages.findIndex(
  1113. (m) => m.id === message.id,
  1114. );
  1115. if (resendingIndex < 0 || resendingIndex >= session.messages.length) {
  1116. console.error("[Chat] failed to find resending message", message);
  1117. return;
  1118. }
  1119. let userMessage: ChatMessage | undefined;
  1120. let botMessage: ChatMessage | undefined;
  1121. if (message.role === "assistant") {
  1122. // if it is resending a bot's message, find the user input for it
  1123. botMessage = message;
  1124. for (let i = resendingIndex; i >= 0; i -= 1) {
  1125. if (session.messages[i].role === "user") {
  1126. userMessage = session.messages[i];
  1127. break;
  1128. }
  1129. }
  1130. } else if (message.role === "user") {
  1131. // if it is resending a user's input, find the bot's response
  1132. userMessage = message;
  1133. for (let i = resendingIndex; i < session.messages.length; i += 1) {
  1134. if (session.messages[i].role === "assistant") {
  1135. botMessage = session.messages[i];
  1136. break;
  1137. }
  1138. }
  1139. }
  1140. if (userMessage === undefined) {
  1141. console.error("[Chat] failed to resend", message);
  1142. return;
  1143. }
  1144. // delete the original messages
  1145. deleteMessage(userMessage.id);
  1146. deleteMessage(botMessage?.id);
  1147. // resend the message
  1148. setIsLoading(true);
  1149. const textContent = getMessageTextContent(userMessage);
  1150. const images = getMessageImages(userMessage);
  1151. chatStore.onUserInput(textContent, images).then(() => setIsLoading(false));
  1152. inputRef.current?.focus();
  1153. };
  1154. const onPinMessage = (message: ChatMessage) => {
  1155. chatStore.updateTargetSession(session, (session) =>
  1156. session.mask.context.push(message),
  1157. );
  1158. showToast(Locale.Chat.Actions.PinToastContent, {
  1159. text: Locale.Chat.Actions.PinToastAction,
  1160. onClick: () => {
  1161. setShowPromptModal(true);
  1162. },
  1163. });
  1164. };
  1165. const accessStore = useAccessStore();
  1166. const [speechStatus, setSpeechStatus] = useState(false);
  1167. const [speechLoading, setSpeechLoading] = useState(false);
  1168. async function openaiSpeech(text: string) {
  1169. if (speechStatus) {
  1170. ttsPlayer.stop();
  1171. setSpeechStatus(false);
  1172. } else {
  1173. var api: ClientApi;
  1174. api = new ClientApi(ModelProvider.GPT);
  1175. const config = useAppConfig.getState();
  1176. setSpeechLoading(true);
  1177. ttsPlayer.init();
  1178. let audioBuffer: ArrayBuffer;
  1179. const { markdownToTxt } = require("markdown-to-txt");
  1180. const textContent = markdownToTxt(text);
  1181. if (config.ttsConfig.engine !== DEFAULT_TTS_ENGINE) {
  1182. const edgeVoiceName = accessStore.edgeVoiceName();
  1183. const tts = new MsEdgeTTS();
  1184. await tts.setMetadata(
  1185. edgeVoiceName,
  1186. OUTPUT_FORMAT.AUDIO_24KHZ_96KBITRATE_MONO_MP3,
  1187. );
  1188. audioBuffer = await tts.toArrayBuffer(textContent);
  1189. } else {
  1190. audioBuffer = await api.llm.speech({
  1191. model: config.ttsConfig.model,
  1192. input: textContent,
  1193. voice: config.ttsConfig.voice,
  1194. speed: config.ttsConfig.speed,
  1195. });
  1196. }
  1197. setSpeechStatus(true);
  1198. ttsPlayer
  1199. .play(audioBuffer, () => {
  1200. setSpeechStatus(false);
  1201. })
  1202. .catch((e) => {
  1203. console.error("[OpenAI Speech]", e);
  1204. showToast(prettyObject(e));
  1205. setSpeechStatus(false);
  1206. })
  1207. .finally(() => setSpeechLoading(false));
  1208. }
  1209. }
  1210. const context: RenderMessage[] = useMemo(() => {
  1211. return session.mask.hideContext ? [] : session.mask.context.slice();
  1212. }, [session.mask.context, session.mask.hideContext]);
  1213. if (
  1214. context.length === 0 &&
  1215. session.messages.at(0)?.content !== BOT_HELLO.content
  1216. ) {
  1217. const copiedHello = Object.assign({}, BOT_HELLO);
  1218. if (!accessStore.isAuthorized()) {
  1219. copiedHello.content = Locale.Error.Unauthorized;
  1220. }
  1221. context.push(copiedHello);
  1222. }
  1223. // preview messages
  1224. const renderMessages = useMemo(() => {
  1225. return context
  1226. .concat(session.messages as RenderMessage[])
  1227. .concat(
  1228. isLoading
  1229. ? [
  1230. {
  1231. ...createMessage({
  1232. role: "assistant",
  1233. content: "……",
  1234. }),
  1235. preview: true,
  1236. },
  1237. ]
  1238. : [],
  1239. )
  1240. .concat(
  1241. userInput.length > 0 && config.sendPreviewBubble
  1242. ? [
  1243. {
  1244. ...createMessage({
  1245. role: "user",
  1246. content: userInput,
  1247. }),
  1248. preview: true,
  1249. },
  1250. ]
  1251. : [],
  1252. );
  1253. }, [
  1254. config.sendPreviewBubble,
  1255. context,
  1256. isLoading,
  1257. session.messages,
  1258. userInput,
  1259. ]);
  1260. const [msgRenderIndex, _setMsgRenderIndex] = useState(
  1261. Math.max(0, renderMessages.length - CHAT_PAGE_SIZE),
  1262. );
  1263. function setMsgRenderIndex(newIndex: number) {
  1264. newIndex = Math.min(renderMessages.length - CHAT_PAGE_SIZE, newIndex);
  1265. newIndex = Math.max(0, newIndex);
  1266. _setMsgRenderIndex(newIndex);
  1267. }
  1268. const messages = useMemo(() => {
  1269. const endRenderIndex = Math.min(
  1270. msgRenderIndex + 3 * CHAT_PAGE_SIZE,
  1271. renderMessages.length,
  1272. );
  1273. return renderMessages.slice(msgRenderIndex, endRenderIndex);
  1274. }, [msgRenderIndex, renderMessages]);
  1275. const onChatBodyScroll = (e: HTMLElement) => {
  1276. const bottomHeight = e.scrollTop + e.clientHeight;
  1277. const edgeThreshold = e.clientHeight;
  1278. const isTouchTopEdge = e.scrollTop <= edgeThreshold;
  1279. const isTouchBottomEdge = bottomHeight >= e.scrollHeight - edgeThreshold;
  1280. const isHitBottom =
  1281. bottomHeight >= e.scrollHeight - (isMobileScreen ? 4 : 10);
  1282. const prevPageMsgIndex = msgRenderIndex - CHAT_PAGE_SIZE;
  1283. const nextPageMsgIndex = msgRenderIndex + CHAT_PAGE_SIZE;
  1284. if (isTouchTopEdge && !isTouchBottomEdge) {
  1285. setMsgRenderIndex(prevPageMsgIndex);
  1286. } else if (isTouchBottomEdge) {
  1287. setMsgRenderIndex(nextPageMsgIndex);
  1288. }
  1289. setHitBottom(isHitBottom);
  1290. setAutoScroll(isHitBottom);
  1291. };
  1292. function scrollToBottom() {
  1293. setMsgRenderIndex(renderMessages.length - CHAT_PAGE_SIZE);
  1294. scrollDomToBottom();
  1295. }
  1296. // clear context index = context length + index in messages
  1297. const clearContextIndex =
  1298. (session.clearContextIndex ?? -1) >= 0
  1299. ? session.clearContextIndex! + context.length - msgRenderIndex
  1300. : -1;
  1301. const [showPromptModal, setShowPromptModal] = useState(false);
  1302. const clientConfig = useMemo(() => getClientConfig(), []);
  1303. const autoFocus = !isMobileScreen; // wont auto focus on mobile screen
  1304. const showMaxIcon = !isMobileScreen && !clientConfig?.isApp;
  1305. useCommand({
  1306. fill: setUserInput,
  1307. submit: (text) => {
  1308. doSubmit(text);
  1309. },
  1310. code: (text) => {
  1311. if (accessStore.disableFastLink) return;
  1312. console.log("[Command] got code from url: ", text);
  1313. showConfirm(Locale.URLCommand.Code + `code = ${text}`).then((res) => {
  1314. if (res) {
  1315. accessStore.update((access) => (access.accessCode = text));
  1316. }
  1317. });
  1318. },
  1319. settings: (text) => {
  1320. if (accessStore.disableFastLink) return;
  1321. try {
  1322. const payload = JSON.parse(text) as {
  1323. key?: string;
  1324. url?: string;
  1325. };
  1326. console.log("[Command] got settings from url: ", payload);
  1327. if (payload.key || payload.url) {
  1328. showConfirm(
  1329. Locale.URLCommand.Settings +
  1330. `\n${JSON.stringify(payload, null, 4)}`,
  1331. ).then((res) => {
  1332. if (!res) return;
  1333. if (payload.key) {
  1334. accessStore.update(
  1335. (access) => (access.openaiApiKey = payload.key!),
  1336. );
  1337. }
  1338. if (payload.url) {
  1339. accessStore.update((access) => (access.openaiUrl = payload.url!));
  1340. }
  1341. accessStore.update((access) => (access.useCustomConfig = true));
  1342. });
  1343. }
  1344. } catch {
  1345. console.error("[Command] failed to get settings from url: ", text);
  1346. }
  1347. },
  1348. });
  1349. // edit / insert message modal
  1350. const [isEditingMessage, setIsEditingMessage] = useState(false);
  1351. // remember unfinished input
  1352. useEffect(() => {
  1353. // try to load from local storage
  1354. const key = UNFINISHED_INPUT(session.id);
  1355. const mayBeUnfinishedInput = localStorage.getItem(key);
  1356. if (mayBeUnfinishedInput && userInput.length === 0) {
  1357. setUserInput(mayBeUnfinishedInput);
  1358. localStorage.removeItem(key);
  1359. }
  1360. const dom = inputRef.current;
  1361. return () => {
  1362. localStorage.setItem(key, dom?.value ?? "");
  1363. };
  1364. // eslint-disable-next-line react-hooks/exhaustive-deps
  1365. }, []);
  1366. const handlePaste = useCallback(
  1367. async (event: React.ClipboardEvent<HTMLTextAreaElement>) => {
  1368. const currentModel = chatStore.currentSession().mask.modelConfig.model;
  1369. if (!isVisionModel(currentModel)) {
  1370. return;
  1371. }
  1372. const items = (event.clipboardData || window.clipboardData).items;
  1373. for (const item of items) {
  1374. if (item.kind === "file" && item.type.startsWith("image/")) {
  1375. event.preventDefault();
  1376. const file = item.getAsFile();
  1377. if (file) {
  1378. const images: string[] = [];
  1379. images.push(...attachImages);
  1380. images.push(
  1381. ...(await new Promise<string[]>((res, rej) => {
  1382. setUploading(true);
  1383. const imagesData: string[] = [];
  1384. uploadImageRemote(file)
  1385. .then((dataUrl) => {
  1386. imagesData.push(dataUrl);
  1387. setUploading(false);
  1388. res(imagesData);
  1389. })
  1390. .catch((e) => {
  1391. setUploading(false);
  1392. rej(e);
  1393. });
  1394. })),
  1395. );
  1396. const imagesLength = images.length;
  1397. if (imagesLength > 3) {
  1398. images.splice(3, imagesLength - 3);
  1399. }
  1400. setAttachImages(images);
  1401. }
  1402. }
  1403. }
  1404. },
  1405. [attachImages, chatStore],
  1406. );
  1407. async function uploadImage() {
  1408. const images: string[] = [];
  1409. images.push(...attachImages);
  1410. images.push(
  1411. ...(await new Promise<string[]>((res, rej) => {
  1412. const fileInput = document.createElement("input");
  1413. fileInput.type = "file";
  1414. fileInput.accept =
  1415. "image/png, image/jpeg, image/webp, image/heic, image/heif";
  1416. fileInput.multiple = true;
  1417. fileInput.onchange = (event: any) => {
  1418. setUploading(true);
  1419. const files = event.target.files;
  1420. const imagesData: string[] = [];
  1421. for (let i = 0; i < files.length; i++) {
  1422. const file = event.target.files[i];
  1423. uploadImageRemote(file)
  1424. .then((dataUrl) => {
  1425. imagesData.push(dataUrl);
  1426. if (
  1427. imagesData.length === 3 ||
  1428. imagesData.length === files.length
  1429. ) {
  1430. setUploading(false);
  1431. res(imagesData);
  1432. }
  1433. })
  1434. .catch((e) => {
  1435. setUploading(false);
  1436. rej(e);
  1437. });
  1438. }
  1439. };
  1440. fileInput.click();
  1441. })),
  1442. );
  1443. const imagesLength = images.length;
  1444. if (imagesLength > 3) {
  1445. images.splice(3, imagesLength - 3);
  1446. }
  1447. setAttachImages(images);
  1448. }
  1449. // 快捷键 shortcut keys
  1450. const [showShortcutKeyModal, setShowShortcutKeyModal] = useState(false);
  1451. useEffect(() => {
  1452. const handleKeyDown = (event: any) => {
  1453. // 打开新聊天 command + shift + o
  1454. if (
  1455. (event.metaKey || event.ctrlKey) &&
  1456. event.shiftKey &&
  1457. event.key.toLowerCase() === "o"
  1458. ) {
  1459. event.preventDefault();
  1460. setTimeout(() => {
  1461. chatStore.newSession();
  1462. navigate(Path.Chat);
  1463. }, 10);
  1464. }
  1465. // 聚焦聊天输入 shift + esc
  1466. else if (event.shiftKey && event.key.toLowerCase() === "escape") {
  1467. event.preventDefault();
  1468. inputRef.current?.focus();
  1469. }
  1470. // 复制最后一个代码块 command + shift + ;
  1471. else if (
  1472. (event.metaKey || event.ctrlKey) &&
  1473. event.shiftKey &&
  1474. event.code === "Semicolon"
  1475. ) {
  1476. event.preventDefault();
  1477. const copyCodeButton =
  1478. document.querySelectorAll<HTMLElement>(".copy-code-button");
  1479. if (copyCodeButton.length > 0) {
  1480. copyCodeButton[copyCodeButton.length - 1].click();
  1481. }
  1482. }
  1483. // 复制最后一个回复 command + shift + c
  1484. else if (
  1485. (event.metaKey || event.ctrlKey) &&
  1486. event.shiftKey &&
  1487. event.key.toLowerCase() === "c"
  1488. ) {
  1489. event.preventDefault();
  1490. const lastNonUserMessage = messages
  1491. .filter((message) => message.role !== "user")
  1492. .pop();
  1493. if (lastNonUserMessage) {
  1494. const lastMessageContent = getMessageTextContent(lastNonUserMessage);
  1495. copyToClipboard(lastMessageContent);
  1496. }
  1497. }
  1498. // 展示快捷键 command + /
  1499. else if ((event.metaKey || event.ctrlKey) && event.key === "/") {
  1500. event.preventDefault();
  1501. setShowShortcutKeyModal(true);
  1502. }
  1503. };
  1504. window.addEventListener("keydown", handleKeyDown);
  1505. return () => {
  1506. window.removeEventListener("keydown", handleKeyDown);
  1507. };
  1508. }, [messages, chatStore, navigate]);
  1509. const [showChatSidePanel, setShowChatSidePanel] = useState(false);
  1510. return (
  1511. <>
  1512. <div className={styles.chat} key={session.id}>
  1513. <div className="window-header" data-tauri-drag-region>
  1514. {isMobileScreen && (
  1515. <div className="window-actions">
  1516. <div className={"window-action-button"}>
  1517. <IconButton
  1518. icon={<ReturnIcon />}
  1519. bordered
  1520. title={Locale.Chat.Actions.ChatList}
  1521. onClick={() => navigate(Path.Home)}
  1522. />
  1523. </div>
  1524. </div>
  1525. )}
  1526. <div
  1527. className={clsx("window-header-title", styles["chat-body-title"])}
  1528. >
  1529. <div
  1530. className={clsx(
  1531. "window-header-main-title",
  1532. styles["chat-body-main-title"],
  1533. )}
  1534. onClickCapture={() => setIsEditingMessage(true)}
  1535. >
  1536. {!session.topic ? DEFAULT_TOPIC : session.topic}
  1537. </div>
  1538. <div className="window-header-sub-title">
  1539. {Locale.Chat.SubTitle(session.messages.length)}
  1540. </div>
  1541. </div>
  1542. <div className="window-actions">
  1543. <div className="window-action-button">
  1544. <IconButton
  1545. icon={<ReloadIcon />}
  1546. bordered
  1547. title={Locale.Chat.Actions.RefreshTitle}
  1548. onClick={() => {
  1549. showToast(Locale.Chat.Actions.RefreshToast);
  1550. chatStore.summarizeSession(true, session);
  1551. }}
  1552. />
  1553. </div>
  1554. {!isMobileScreen && (
  1555. <div className="window-action-button">
  1556. <IconButton
  1557. icon={<RenameIcon />}
  1558. bordered
  1559. title={Locale.Chat.EditMessage.Title}
  1560. aria={Locale.Chat.EditMessage.Title}
  1561. onClick={() => setIsEditingMessage(true)}
  1562. />
  1563. </div>
  1564. )}
  1565. <div className="window-action-button">
  1566. <IconButton
  1567. icon={<ExportIcon />}
  1568. bordered
  1569. title={Locale.Chat.Actions.Export}
  1570. onClick={() => {
  1571. setShowExport(true);
  1572. }}
  1573. />
  1574. </div>
  1575. {showMaxIcon && (
  1576. <div className="window-action-button">
  1577. <IconButton
  1578. icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
  1579. bordered
  1580. title={Locale.Chat.Actions.FullScreen}
  1581. aria={Locale.Chat.Actions.FullScreen}
  1582. onClick={() => {
  1583. config.update(
  1584. (config) => (config.tightBorder = !config.tightBorder),
  1585. );
  1586. }}
  1587. />
  1588. </div>
  1589. )}
  1590. </div>
  1591. <PromptToast
  1592. showToast={!hitBottom}
  1593. showModal={showPromptModal}
  1594. setShowModal={setShowPromptModal}
  1595. />
  1596. </div>
  1597. <div className={styles["chat-main"]}>
  1598. <div className={styles["chat-body-container"]}>
  1599. <div
  1600. className={styles["chat-body"]}
  1601. ref={scrollRef}
  1602. onScroll={(e) => onChatBodyScroll(e.currentTarget)}
  1603. onMouseDown={() => inputRef.current?.blur()}
  1604. onTouchStart={() => {
  1605. inputRef.current?.blur();
  1606. setAutoScroll(false);
  1607. }}
  1608. >
  1609. {messages
  1610. // TODO
  1611. // .filter((m) => !m.isMcpResponse)
  1612. .map((message, i) => {
  1613. const isUser = message.role === "user";
  1614. const isContext = i < context.length;
  1615. const showActions =
  1616. i > 0 &&
  1617. !(message.preview || message.content.length === 0) &&
  1618. !isContext;
  1619. const showTyping = message.preview || message.streaming;
  1620. const shouldShowClearContextDivider =
  1621. i === clearContextIndex - 1;
  1622. return (
  1623. <Fragment key={message.id}>
  1624. <div
  1625. className={
  1626. isUser
  1627. ? styles["chat-message-user"]
  1628. : styles["chat-message"]
  1629. }
  1630. >
  1631. <div className={styles["chat-message-container"]}>
  1632. <div className={styles["chat-message-header"]}>
  1633. <div className={styles["chat-message-avatar"]}>
  1634. <div className={styles["chat-message-edit"]}>
  1635. <IconButton
  1636. icon={<EditIcon />}
  1637. aria={Locale.Chat.Actions.Edit}
  1638. onClick={async () => {
  1639. const newMessage = await showPrompt(
  1640. Locale.Chat.Actions.Edit,
  1641. getMessageTextContent(message),
  1642. 10,
  1643. );
  1644. let newContent:
  1645. | string
  1646. | MultimodalContent[] = newMessage;
  1647. const images = getMessageImages(message);
  1648. if (images.length > 0) {
  1649. newContent = [
  1650. { type: "text", text: newMessage },
  1651. ];
  1652. for (let i = 0; i < images.length; i++) {
  1653. newContent.push({
  1654. type: "image_url",
  1655. image_url: {
  1656. url: images[i],
  1657. },
  1658. });
  1659. }
  1660. }
  1661. chatStore.updateTargetSession(
  1662. session,
  1663. (session) => {
  1664. const m = session.mask.context
  1665. .concat(session.messages)
  1666. .find((m) => m.id === message.id);
  1667. if (m) {
  1668. m.content = newContent;
  1669. }
  1670. },
  1671. );
  1672. }}
  1673. ></IconButton>
  1674. </div>
  1675. {isUser ? (
  1676. <Avatar avatar={config.avatar} />
  1677. ) : (
  1678. <>
  1679. {["system"].includes(message.role) ? (
  1680. <Avatar avatar="2699-fe0f" />
  1681. ) : (
  1682. <MaskAvatar
  1683. avatar={session.mask.avatar}
  1684. model={
  1685. message.model ||
  1686. session.mask.modelConfig.model
  1687. }
  1688. />
  1689. )}
  1690. </>
  1691. )}
  1692. </div>
  1693. {!isUser && (
  1694. <div className={styles["chat-model-name"]}>
  1695. {message.model}
  1696. </div>
  1697. )}
  1698. {showActions && (
  1699. <div className={styles["chat-message-actions"]}>
  1700. <div className={styles["chat-input-actions"]}>
  1701. {message.streaming ? (
  1702. <ChatAction
  1703. text={Locale.Chat.Actions.Stop}
  1704. icon={<StopIcon />}
  1705. onClick={() =>
  1706. onUserStop(message.id ?? i)
  1707. }
  1708. />
  1709. ) : (
  1710. <>
  1711. <ChatAction
  1712. text={Locale.Chat.Actions.Retry}
  1713. icon={<ResetIcon />}
  1714. onClick={() => onResend(message)}
  1715. />
  1716. <ChatAction
  1717. text={Locale.Chat.Actions.Delete}
  1718. icon={<DeleteIcon />}
  1719. onClick={() =>
  1720. onDelete(message.id ?? i)
  1721. }
  1722. />
  1723. <ChatAction
  1724. text={Locale.Chat.Actions.Pin}
  1725. icon={<PinIcon />}
  1726. onClick={() => onPinMessage(message)}
  1727. />
  1728. <ChatAction
  1729. text={Locale.Chat.Actions.Copy}
  1730. icon={<CopyIcon />}
  1731. onClick={() =>
  1732. copyToClipboard(
  1733. getMessageTextContent(message),
  1734. )
  1735. }
  1736. />
  1737. {config.ttsConfig.enable && (
  1738. <ChatAction
  1739. text={
  1740. speechStatus
  1741. ? Locale.Chat.Actions.StopSpeech
  1742. : Locale.Chat.Actions.Speech
  1743. }
  1744. icon={
  1745. speechStatus ? (
  1746. <SpeakStopIcon />
  1747. ) : (
  1748. <SpeakIcon />
  1749. )
  1750. }
  1751. onClick={() =>
  1752. openaiSpeech(
  1753. getMessageTextContent(message),
  1754. )
  1755. }
  1756. />
  1757. )}
  1758. </>
  1759. )}
  1760. </div>
  1761. </div>
  1762. )}
  1763. </div>
  1764. {message?.tools?.length == 0 && showTyping && (
  1765. <div className={styles["chat-message-status"]}>
  1766. {Locale.Chat.Typing}
  1767. </div>
  1768. )}
  1769. {/*@ts-ignore*/}
  1770. {message?.tools?.length > 0 && (
  1771. <div className={styles["chat-message-tools"]}>
  1772. {message?.tools?.map((tool) => (
  1773. <div
  1774. key={tool.id}
  1775. title={tool?.errorMsg}
  1776. className={styles["chat-message-tool"]}
  1777. >
  1778. {tool.isError === false ? (
  1779. <ConfirmIcon />
  1780. ) : tool.isError === true ? (
  1781. <CloseIcon />
  1782. ) : (
  1783. <LoadingButtonIcon />
  1784. )}
  1785. <span>{tool?.function?.name}</span>
  1786. </div>
  1787. ))}
  1788. </div>
  1789. )}
  1790. <div className={styles["chat-message-item"]}>
  1791. <Markdown
  1792. key={message.streaming ? "loading" : "done"}
  1793. content={getMessageTextContent(message)}
  1794. loading={
  1795. (message.preview || message.streaming) &&
  1796. message.content.length === 0 &&
  1797. !isUser
  1798. }
  1799. // onContextMenu={(e) => onRightClick(e, message)} // hard to use
  1800. onDoubleClickCapture={() => {
  1801. if (!isMobileScreen) return;
  1802. setUserInput(getMessageTextContent(message));
  1803. }}
  1804. fontSize={fontSize}
  1805. fontFamily={fontFamily}
  1806. parentRef={scrollRef}
  1807. defaultShow={i >= messages.length - 6}
  1808. />
  1809. {getMessageImages(message).length == 1 && (
  1810. <img
  1811. className={styles["chat-message-item-image"]}
  1812. src={getMessageImages(message)[0]}
  1813. alt=""
  1814. />
  1815. )}
  1816. {getMessageImages(message).length > 1 && (
  1817. <div
  1818. className={styles["chat-message-item-images"]}
  1819. style={
  1820. {
  1821. "--image-count":
  1822. getMessageImages(message).length,
  1823. } as React.CSSProperties
  1824. }
  1825. >
  1826. {getMessageImages(message).map(
  1827. (image, index) => {
  1828. return (
  1829. <img
  1830. className={
  1831. styles[
  1832. "chat-message-item-image-multi"
  1833. ]
  1834. }
  1835. key={index}
  1836. src={image}
  1837. alt=""
  1838. />
  1839. );
  1840. },
  1841. )}
  1842. </div>
  1843. )}
  1844. </div>
  1845. {message?.audio_url && (
  1846. <div className={styles["chat-message-audio"]}>
  1847. <audio src={message.audio_url} controls />
  1848. </div>
  1849. )}
  1850. <div className={styles["chat-message-action-date"]}>
  1851. {isContext
  1852. ? Locale.Chat.IsContext
  1853. : message.date.toLocaleString()}
  1854. </div>
  1855. </div>
  1856. </div>
  1857. {shouldShowClearContextDivider && <ClearContextDivider />}
  1858. </Fragment>
  1859. );
  1860. })}
  1861. </div>
  1862. <div className={styles["chat-input-panel"]}>
  1863. <PromptHints
  1864. prompts={promptHints}
  1865. onPromptSelect={onPromptSelect}
  1866. />
  1867. <ChatActions
  1868. uploadImage={uploadImage}
  1869. setAttachImages={setAttachImages}
  1870. setUploading={setUploading}
  1871. showPromptModal={() => setShowPromptModal(true)}
  1872. scrollToBottom={scrollToBottom}
  1873. hitBottom={hitBottom}
  1874. uploading={uploading}
  1875. showPromptHints={() => {
  1876. // Click again to close
  1877. if (promptHints.length > 0) {
  1878. setPromptHints([]);
  1879. return;
  1880. }
  1881. inputRef.current?.focus();
  1882. setUserInput("/");
  1883. onSearch("");
  1884. }}
  1885. setShowShortcutKeyModal={setShowShortcutKeyModal}
  1886. setUserInput={setUserInput}
  1887. setShowChatSidePanel={setShowChatSidePanel}
  1888. />
  1889. <label
  1890. className={clsx(styles["chat-input-panel-inner"], {
  1891. [styles["chat-input-panel-inner-attach"]]:
  1892. attachImages.length !== 0,
  1893. })}
  1894. htmlFor="chat-input"
  1895. >
  1896. <textarea
  1897. id="chat-input"
  1898. ref={inputRef}
  1899. className={styles["chat-input"]}
  1900. placeholder={Locale.Chat.Input(submitKey)}
  1901. onInput={(e) => onInput(e.currentTarget.value)}
  1902. value={userInput}
  1903. onKeyDown={onInputKeyDown}
  1904. onFocus={scrollToBottom}
  1905. onClick={scrollToBottom}
  1906. onPaste={handlePaste}
  1907. rows={inputRows}
  1908. autoFocus={autoFocus}
  1909. style={{
  1910. fontSize: config.fontSize,
  1911. fontFamily: config.fontFamily,
  1912. }}
  1913. />
  1914. {attachImages.length != 0 && (
  1915. <div className={styles["attach-images"]}>
  1916. {attachImages.map((image, index) => {
  1917. return (
  1918. <div
  1919. key={index}
  1920. className={styles["attach-image"]}
  1921. style={{ backgroundImage: `url("${image}")` }}
  1922. >
  1923. <div className={styles["attach-image-mask"]}>
  1924. <DeleteImageButton
  1925. deleteImage={() => {
  1926. setAttachImages(
  1927. attachImages.filter((_, i) => i !== index),
  1928. );
  1929. }}
  1930. />
  1931. </div>
  1932. </div>
  1933. );
  1934. })}
  1935. </div>
  1936. )}
  1937. <IconButton
  1938. icon={<SendWhiteIcon />}
  1939. text={Locale.Chat.Send}
  1940. className={styles["chat-input-send"]}
  1941. type="primary"
  1942. onClick={() => doSubmit(userInput)}
  1943. />
  1944. </label>
  1945. </div>
  1946. </div>
  1947. <div
  1948. className={clsx(styles["chat-side-panel"], {
  1949. [styles["mobile"]]: isMobileScreen,
  1950. [styles["chat-side-panel-show"]]: showChatSidePanel,
  1951. })}
  1952. >
  1953. {showChatSidePanel && (
  1954. <RealtimeChat
  1955. onClose={() => {
  1956. setShowChatSidePanel(false);
  1957. }}
  1958. onStartVoice={async () => {
  1959. console.log("start voice");
  1960. }}
  1961. />
  1962. )}
  1963. </div>
  1964. </div>
  1965. </div>
  1966. {showExport && (
  1967. <ExportMessageModal onClose={() => setShowExport(false)} />
  1968. )}
  1969. {isEditingMessage && (
  1970. <EditMessageModal
  1971. onClose={() => {
  1972. setIsEditingMessage(false);
  1973. }}
  1974. />
  1975. )}
  1976. {showShortcutKeyModal && (
  1977. <ShortcutKeyModal onClose={() => setShowShortcutKeyModal(false)} />
  1978. )}
  1979. </>
  1980. );
  1981. }
  1982. export function Chat() {
  1983. const chatStore = useChatStore();
  1984. const session = chatStore.currentSession();
  1985. return <_Chat key={session.id}></_Chat>;
  1986. }