chat.tsx 63 KB

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