chat.tsx 62 KB

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