chat.tsx 68 KB

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