chat.tsx 58 KB

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