chat.tsx 56 KB

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