chat.tsx 70 KB

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