chat.tsx 70 KB

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