chat.tsx 62 KB

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