| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793 |
- import {useDebouncedCallback} from "use-debounce";
- import React, {
- useState,
- useRef,
- useEffect,
- useMemo,
- useCallback,
- Fragment,
- RefObject,
- } from "react";
- import SendWhiteIcon from "../icons/send-white.svg";
- import BrainIcon from "../icons/brain.svg";
- import RenameIcon from "../icons/rename.svg";
- import ExportIcon from "../icons/share.svg";
- import ReturnIcon from "../icons/return.svg";
- import CopyIcon from "../icons/copy.svg";
- import faviconSrc from "../icons/favicon.png";
- import Favicon from "../icons/favicon.svg";
- import LoadingIcon from "../icons/three-dots.svg";
- import LoadingButtonIcon from "../icons/loading.svg";
- import PromptIcon from "../icons/prompt.svg";
- import MaskIcon from "../icons/mask.svg";
- import MaxIcon from "../icons/max.svg";
- import MinIcon from "../icons/min.svg";
- import ResetIcon from "../icons/reload.svg";
- import BreakIcon from "../icons/break.svg";
- import SettingsIcon from "../icons/chat-settings.svg";
- import DeleteIcon from "../icons/clear.svg";
- import PinIcon from "../icons/pin.svg";
- import EditIcon from "../icons/rename.svg";
- import ConfirmIcon from "../icons/confirm.svg";
- import CancelIcon from "../icons/cancel.svg";
- import ImageIcon from "../icons/image.svg";
- import LightIcon from "../icons/light.svg";
- import DarkIcon from "../icons/dark.svg";
- import AutoIcon from "../icons/auto.svg";
- import BottomIcon from "../icons/bottom.svg";
- import StopIcon from "../icons/pause.svg";
- import RobotIcon from "../icons/robot.svg";
- import AddIcon from "../icons/add.svg";
- import SizeIcon from "../icons/size.svg";
- import PluginIcon from "../icons/plugin.svg";
- import {
- ChatMessage,
- SubmitKey,
- useChatStore,
- BOT_HELLO,
- createMessage,
- useAccessStore,
- Theme,
- useAppConfig,
- DEFAULT_TOPIC,
- ModelType,
- } from "../store";
- import {
- copyToClipboard,
- selectOrCopy,
- autoGrowTextArea,
- useMobileScreen,
- getMessageTextContent,
- getMessageImages,
- isVisionModel,
- isDalle3,
- } from "../utils";
- import {uploadImage as uploadImageRemote} from "@/app/utils/chat";
- import dynamic from "next/dynamic";
- import {ChatControllerPool} from "../client/controller";
- import {DalleSize} from "../typing";
- import {Prompt, usePromptStore} from "../store/prompt";
- import Locale from "../locales";
- import {IconButton} from "./button";
- import styles from "./chat.module.scss";
- import {
- List,
- ListItem,
- Modal,
- Selector,
- showConfirm,
- showPrompt,
- showToast,
- } from "./ui-lib";
- import {useNavigate} from "react-router-dom";
- import {
- CHAT_PAGE_SIZE,
- LAST_INPUT_KEY,
- Path,
- REQUEST_TIMEOUT_MS,
- UNFINISHED_INPUT,
- ServiceProvider,
- Plugin,
- } from "../constant";
- import {Avatar} from "./emoji";
- import {ContextPrompts, MaskAvatar, MaskConfig} from "./mask";
- import {useMaskStore} from "../store/mask";
- import {ChatCommandPrefix, useChatCommand, useCommand} from "../command";
- import {prettyObject} from "../utils/format";
- import {ExportMessageModal} from "./exporter";
- import {getClientConfig} from "../config/client";
- import {useAllModels} from "../utils/hooks";
- import {MultimodalContent} from "../client/api";
- const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
- loading: () => <LoadingIcon/>,
- });
- export function SessionConfigModel(props: { onClose: () => void }) {
- const chatStore = useChatStore();
- const session = chatStore.currentSession();
- const maskStore = useMaskStore();
- const navigate = useNavigate();
-
- return (
- <div className="modal-mask">
- <Modal
- title={Locale.Context.Edit}
- onClose={() => props.onClose()}
- actions={[
- <IconButton
- key="reset"
- icon={<ResetIcon/>}
- bordered
- text={Locale.Chat.Config.Reset}
- onClick={async () => {
- if (await showConfirm(Locale.Memory.ResetConfirm)) {
- chatStore.updateCurrentSession(
- (session) => (session.memoryPrompt = ""),
- );
- }
- }}
- />,
- <IconButton
- key="copy"
- icon={<CopyIcon/>}
- bordered
- text={Locale.Chat.Config.SaveAs}
- onClick={() => {
- navigate(Path.Masks);
- setTimeout(() => {
- maskStore.create(session.mask);
- }, 500);
- }}
- />,
- ]}
- >
- <MaskConfig
- mask={session.mask}
- updateMask={(updater) => {
- const mask = {...session.mask};
- updater(mask);
- chatStore.updateCurrentSession((session) => (session.mask = mask));
- }}
- shouldSyncFromGlobal
- extraListItems={
- session.mask.modelConfig.sendMemory ? (
- <ListItem
- className="copyable"
- title={`${Locale.Memory.Title} (${session.lastSummarizeIndex} of ${session.messages.length})`}
- subTitle={session.memoryPrompt || Locale.Memory.EmptyContent}
- ></ListItem>
- ) : (
- <></>
- )
- }
- ></MaskConfig>
- </Modal>
- </div>
- );
- }
- // 提示词
- const CallWord = (props: {
- setUserInput: (value: string) => void,
- doSubmit: (userInput: string) => void,
- }) => {
- const {setUserInput, doSubmit} = props
- const list = [
- {
- title: '信息公布',
- // text: '在哪里查看招聘信息?',
- text: '今年上海建科工程咨询的校园招聘什么时候开始?如何查阅相关招聘信息?',
- },
- {
- title: '招聘岗位',
- // text: '今年招聘的岗位有哪些?',
- text: '今年招聘的岗位有哪些?',
- },
- {
- title: '专业要求',
- // text: '招聘的岗位有什么专业要求?',
- text: '招聘的岗位有什么专业要求?',
- },
- {
- title: '工作地点',
- // text: '全国都有工作地点吗?',
- text: '工作地点是如何确定的?',
- },
- {
- title: '薪资待遇',
- // text: '企业可提供的薪资与福利待遇如何?',
- text: '企业可提供的薪资与福利待遇如何?',
- },
- {
- title: '职业发展',
- // text: '我应聘贵单位,你们能提供怎样的职业发展规划?',
- text: '公司有哪些职业发展通道?',
- },
- {
- title: '落户政策',
- // text: '公司是否能协助我落户?',
- text: '关于落户支持?',
- }
- ]
-
- return (
- <>
- {
- list.map((item, index) => {
- return <span
- key={index}
- style={{
- padding: '5px 10px',
- background: '#f6f7f8',
- color: '#5e5e66',
- borderRadius: 4,
- margin: '0 5px 10px 0',
- cursor: 'pointer',
- fontSize: 12
- }}
- onClick={() => {
- const plan: string = '2';
- if (plan === '1') {
- // 方案1.点击后出现在输入框内,用户自己点击发送
- setUserInput(item.text);
- } else {
- // 方案2.点击后直接发送
- doSubmit(item.text)
- }
- }}
- >
- {item.title}
- </span>
- })
- }
- </>
- )
- }
- function PromptToast(props: {
- showToast?: boolean;
- showModal?: boolean;
- setShowModal: (_: boolean) => void;
- }) {
- const chatStore = useChatStore();
- const session = chatStore.currentSession();
- const context = session.mask.context;
-
- return (
- <div className={styles["prompt-toast"]} key="prompt-toast">
- {props.showToast && (
- <div
- className={styles["prompt-toast-inner"] + " clickable"}
- role="button"
- onClick={() => props.setShowModal(true)}
- >
- <BrainIcon/>
- <span className={styles["prompt-toast-content"]}>
- {Locale.Context.Toast(context.length)}
- </span>
- </div>
- )}
- {props.showModal && (
- <SessionConfigModel onClose={() => props.setShowModal(false)}/>
- )}
- </div>
- );
- }
- function useSubmitHandler() {
- const config = useAppConfig();
- const submitKey = config.submitKey;
- const isComposing = useRef(false);
-
- useEffect(() => {
- const onCompositionStart = () => {
- isComposing.current = true;
- };
- const onCompositionEnd = () => {
- isComposing.current = false;
- };
-
- window.addEventListener("compositionstart", onCompositionStart);
- window.addEventListener("compositionend", onCompositionEnd);
-
- return () => {
- window.removeEventListener("compositionstart", onCompositionStart);
- window.removeEventListener("compositionend", onCompositionEnd);
- };
- }, []);
-
- const shouldSubmit = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
- // Fix Chinese input method "Enter" on Safari
- if (e.keyCode == 229) return false;
- if (e.key !== "Enter") return false;
- if (e.key === "Enter" && (e.nativeEvent.isComposing || isComposing.current))
- return false;
- return (
- (config.submitKey === SubmitKey.AltEnter && e.altKey) ||
- (config.submitKey === SubmitKey.CtrlEnter && e.ctrlKey) ||
- (config.submitKey === SubmitKey.ShiftEnter && e.shiftKey) ||
- (config.submitKey === SubmitKey.MetaEnter && e.metaKey) ||
- (config.submitKey === SubmitKey.Enter &&
- !e.altKey &&
- !e.ctrlKey &&
- !e.shiftKey &&
- !e.metaKey)
- );
- };
-
- return {
- submitKey,
- shouldSubmit,
- };
- }
- export type RenderPrompt = Pick<Prompt, "title" | "content">;
- export function PromptHints(props: {
- prompts: RenderPrompt[];
- onPromptSelect: (prompt: RenderPrompt) => void;
- }) {
- const noPrompts = props.prompts.length === 0;
- const [selectIndex, setSelectIndex] = useState(0);
- const selectedRef = useRef<HTMLDivElement>(null);
-
- useEffect(() => {
- setSelectIndex(0);
- }, [props.prompts.length]);
-
- useEffect(() => {
- const onKeyDown = (e: KeyboardEvent) => {
- if (noPrompts || e.metaKey || e.altKey || e.ctrlKey) {
- return;
- }
- // arrow up / down to select prompt
- const changeIndex = (delta: number) => {
- e.stopPropagation();
- e.preventDefault();
- const nextIndex = Math.max(
- 0,
- Math.min(props.prompts.length - 1, selectIndex + delta),
- );
- setSelectIndex(nextIndex);
- selectedRef.current?.scrollIntoView({
- block: "center",
- });
- };
-
- if (e.key === "ArrowUp") {
- changeIndex(1);
- } else if (e.key === "ArrowDown") {
- changeIndex(-1);
- } else if (e.key === "Enter") {
- const selectedPrompt = props.prompts.at(selectIndex);
- if (selectedPrompt) {
- props.onPromptSelect(selectedPrompt);
- }
- }
- };
-
- window.addEventListener("keydown", onKeyDown);
-
- return () => window.removeEventListener("keydown", onKeyDown);
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [props.prompts.length, selectIndex]);
-
- if (noPrompts) return null;
- return (
- <div className={styles["prompt-hints"]}>
- {props.prompts.map((prompt, i) => (
- <div
- ref={i === selectIndex ? selectedRef : null}
- className={
- styles["prompt-hint"] +
- ` ${i === selectIndex ? styles["prompt-hint-selected"] : ""}`
- }
- key={prompt.title + i.toString()}
- onClick={() => props.onPromptSelect(prompt)}
- onMouseEnter={() => setSelectIndex(i)}
- >
- <div className={styles["hint-title"]}>{prompt.title}</div>
- <div className={styles["hint-content"]}>{prompt.content}</div>
- </div>
- ))}
- </div>
- );
- }
- function ClearContextDivider() {
- const chatStore = useChatStore();
-
- return (
- <div
- className={styles["clear-context"]}
- onClick={() =>
- chatStore.updateCurrentSession(
- (session) => (session.clearContextIndex = undefined),
- )
- }
- >
- <div className={styles["clear-context-tips"]}>{Locale.Context.Clear}</div>
- <div className={styles["clear-context-revert-btn"]}>
- {Locale.Context.Revert}
- </div>
- </div>
- );
- }
- export function ChatAction(props: {
- text: string;
- icon: JSX.Element;
- onClick: () => void;
- }) {
- const iconRef = useRef<HTMLDivElement>(null);
- const textRef = useRef<HTMLDivElement>(null);
- const [width, setWidth] = useState({
- full: 16,
- icon: 16,
- });
-
- function updateWidth() {
- if (!iconRef.current || !textRef.current) return;
- const getWidth = (dom: HTMLDivElement) => dom.getBoundingClientRect().width;
- const textWidth = getWidth(textRef.current);
- const iconWidth = getWidth(iconRef.current);
- setWidth({
- full: textWidth + iconWidth,
- icon: iconWidth,
- });
- }
-
- return (
- <div
- className={`${styles["chat-input-action"]} clickable`}
- onClick={() => {
- props.onClick();
- setTimeout(updateWidth, 1);
- }}
- onMouseEnter={updateWidth}
- onTouchStart={updateWidth}
- style={
- {
- "--icon-width": `${width.icon}px`,
- "--full-width": `${width.full}px`,
- } as React.CSSProperties
- }
- >
- <div ref={iconRef} className={styles["icon"]}>
- {props.icon}
- </div>
- <div className={styles["text"]} ref={textRef}>
- {props.text}
- </div>
- </div>
- );
- }
- function useScrollToBottom(
- scrollRef: RefObject<HTMLDivElement>,
- detach: boolean = false,
- ) {
- // for auto-scroll
-
- const [autoScroll, setAutoScroll] = useState(true);
-
- function scrollDomToBottom() {
- const dom = scrollRef.current;
- if (dom) {
- requestAnimationFrame(() => {
- setAutoScroll(true);
- dom.scrollTo(0, dom.scrollHeight);
- });
- }
- }
-
- // auto scroll
- useEffect(() => {
- if (autoScroll && !detach) {
- scrollDomToBottom();
- }
- });
-
- return {
- scrollRef,
- autoScroll,
- setAutoScroll,
- scrollDomToBottom,
- };
- }
- export function ChatActions(props: {
- setUserInput: (value: string) => void;
- doSubmit: (userInput: string) => void;
- uploadImage: () => void;
- setAttachImages: (images: string[]) => void;
- setUploading: (uploading: boolean) => void;
- showPromptModal: () => void;
- scrollToBottom: () => void;
- showPromptHints: () => void;
- hitBottom: boolean;
- uploading: boolean;
- }) {
- const config = useAppConfig();
- const navigate = useNavigate();
- const chatStore = useChatStore();
-
- // switch themes
- const theme = config.theme;
-
- function nextTheme() {
- const themes = [Theme.Auto, Theme.Light, Theme.Dark];
- const themeIndex = themes.indexOf(theme);
- const nextIndex = (themeIndex + 1) % themes.length;
- const nextTheme = themes[nextIndex];
- config.update((config) => (config.theme = nextTheme));
- }
-
- // stop all responses
- const couldStop = ChatControllerPool.hasPending();
- const stopAll = () => ChatControllerPool.stopAll();
-
- // switch model
- const currentModel = chatStore.currentSession().mask.modelConfig.model;
- const currentProviderName =
- chatStore.currentSession().mask.modelConfig?.providerName ||
- ServiceProvider.OpenAI;
- const allModels = useAllModels();
- const models = useMemo(() => {
- const filteredModels = allModels.filter((m) => m.available);
- const defaultModel = filteredModels.find((m) => m.isDefault);
-
- if (defaultModel) {
- const arr = [
- defaultModel,
- ...filteredModels.filter((m) => m !== defaultModel),
- ];
- return arr;
- } else {
- return filteredModels;
- }
- }, [allModels]);
- const currentModelName = useMemo(() => {
- const model = models.find(
- (m) =>
- m.name == currentModel &&
- m?.provider?.providerName == currentProviderName,
- );
- return model?.displayName ?? "";
- }, [models, currentModel, currentProviderName]);
- const [showModelSelector, setShowModelSelector] = useState(false);
- const [showPluginSelector, setShowPluginSelector] = useState(false);
- const [showUploadImage, setShowUploadImage] = useState(false);
-
- const [showSizeSelector, setShowSizeSelector] = useState(false);
- const dalle3Sizes: DalleSize[] = ["1024x1024", "1792x1024", "1024x1792"];
- const currentSize =
- chatStore.currentSession().mask.modelConfig?.size ?? "1024x1024";
-
- useEffect(() => {
- const show = isVisionModel(currentModel);
- setShowUploadImage(show);
- if (!show) {
- props.setAttachImages([]);
- props.setUploading(false);
- }
-
- // if current model is not available
- // switch to first available model
- const isUnavaliableModel = !models.some((m) => m.name === currentModel);
- if (isUnavaliableModel && models.length > 0) {
- // show next model to default model if exist
- let nextModel = models.find((model) => model.isDefault) || models[0];
- chatStore.updateCurrentSession((session) => {
- session.mask.modelConfig.model = nextModel.name;
- session.mask.modelConfig.providerName = nextModel?.provider?.providerName as ServiceProvider;
- });
- showToast(
- nextModel?.provider?.providerName == "ByteDance"
- ? nextModel.displayName
- : nextModel.name,
- );
- }
- }, [chatStore, currentModel, models]);
-
- return (
- <div className={styles["chat-input-actions"]}>
- {/* {couldStop && (
- <ChatAction
- onClick={stopAll}
- text={Locale.Chat.InputActions.Stop}
- icon={<StopIcon />}
- />
- )} */}
-
- {!props.hitBottom && (
- <ChatAction
- onClick={props.scrollToBottom}
- text={Locale.Chat.InputActions.ToBottom}
- icon={<BottomIcon/>}
- />
- )}
-
- {/* {props.hitBottom && (
- <ChatAction
- onClick={props.showPromptModal}
- text={Locale.Chat.InputActions.Settings}
- icon={<SettingsIcon />}
- />
- )} */}
-
- {showUploadImage && (
- <ChatAction
- onClick={props.uploadImage}
- text={Locale.Chat.InputActions.UploadImage}
- icon={props.uploading ? <LoadingButtonIcon/> : <ImageIcon/>}
- />
- )}
-
- {/* <ChatAction
- onClick={nextTheme}
- text={Locale.Chat.InputActions.Theme[theme]}
- icon={
- <>
- {theme === Theme.Auto ? (
- <AutoIcon />
- ) : theme === Theme.Light ? (
- <LightIcon />
- ) : theme === Theme.Dark ? (
- <DarkIcon />
- ) : null}
- </>
- }
- /> */}
-
- <CallWord
- setUserInput={props.setUserInput}
- doSubmit={props.doSubmit}
- />
-
- {/* <ChatAction
- onClick={props.showPromptHints}
- text={Locale.Chat.InputActions.Prompt}
- icon={<PromptIcon />}
- /> */}
-
- {/* <ChatAction
- onClick={() => {
- navigate(Path.Masks);
- }}
- text={Locale.Chat.InputActions.Masks}
- icon={<MaskIcon />}
- /> */}
-
- {/* <ChatAction
- text={Locale.Chat.InputActions.Clear}
- icon={<BreakIcon />}
- onClick={() => {
- chatStore.updateCurrentSession((session) => {
- if (session.clearContextIndex === session.messages.length) {
- session.clearContextIndex = undefined;
- } else {
- session.clearContextIndex = session.messages.length;
- session.memoryPrompt = ""; // will clear memory
- }
- });
- }}
- /> */}
-
- {/* <ChatAction
- onClick={() => setShowModelSelector(true)}
- text={currentModelName}
- icon={<RobotIcon />}
- /> */}
-
- {showModelSelector && (
- <Selector
- defaultSelectedValue={`${currentModel}@${currentProviderName}`}
- items={models.map((m) => ({
- title: `${m.displayName}${m?.provider?.providerName
- ? "(" + m?.provider?.providerName + ")"
- : ""
- }`,
- value: `${m.name}@${m?.provider?.providerName}`,
- }))}
- onClose={() => setShowModelSelector(false)}
- onSelection={(s) => {
- if (s.length === 0) return;
- const [model, providerName] = s[0].split("@");
- chatStore.updateCurrentSession((session) => {
- session.mask.modelConfig.model = model as ModelType;
- session.mask.modelConfig.providerName =
- providerName as ServiceProvider;
- session.mask.syncGlobalConfig = false;
- });
- if (providerName == "ByteDance") {
- const selectedModel = models.find(
- (m) =>
- m.name == model && m?.provider?.providerName == providerName,
- );
- showToast(selectedModel?.displayName ?? "");
- } else {
- showToast(model);
- }
- }}
- />
- )}
-
- {isDalle3(currentModel) && (
- <ChatAction
- onClick={() => setShowSizeSelector(true)}
- text={currentSize}
- icon={<SizeIcon/>}
- />
- )}
-
- {showSizeSelector && (
- <Selector
- defaultSelectedValue={currentSize}
- items={dalle3Sizes.map((m) => ({
- title: m,
- value: m,
- }))}
- onClose={() => setShowSizeSelector(false)}
- onSelection={(s) => {
- if (s.length === 0) return;
- const size = s[0];
- chatStore.updateCurrentSession((session) => {
- session.mask.modelConfig.size = size;
- });
- showToast(size);
- }}
- />
- )}
-
- {/* <ChatAction
- onClick={() => setShowPluginSelector(true)}
- text={Locale.Plugin.Name}
- icon={<PluginIcon />}
- /> */}
-
- {showPluginSelector && (
- <Selector
- multiple
- defaultSelectedValue={chatStore.currentSession().mask?.plugin}
- items={[
- {
- title: Locale.Plugin.Artifacts,
- value: Plugin.Artifacts,
- },
- ]}
- onClose={() => setShowPluginSelector(false)}
- onSelection={(s) => {
- const plugin = s[0];
- chatStore.updateCurrentSession((session) => {
- session.mask.plugin = s;
- });
- if (plugin) {
- showToast(plugin);
- }
- }}
- />
- )}
- </div>
- );
- }
- export function EditMessageModal(props: { onClose: () => void }) {
- const chatStore = useChatStore();
- const session = chatStore.currentSession();
- const [messages, setMessages] = useState(session.messages.slice());
-
- return (
- <div className="modal-mask">
- <Modal
- title={Locale.Chat.EditMessage.Title}
- onClose={props.onClose}
- actions={[
- <IconButton
- text={Locale.UI.Cancel}
- icon={<CancelIcon/>}
- key="cancel"
- onClick={() => {
- props.onClose();
- }}
- />,
- <IconButton
- type="primary"
- text={Locale.UI.Confirm}
- icon={<ConfirmIcon/>}
- key="ok"
- onClick={() => {
- chatStore.updateCurrentSession(
- (session) => (session.messages = messages),
- );
- props.onClose();
- }}
- />,
- ]}
- >
- <List>
- <ListItem
- title={Locale.Chat.EditMessage.Topic.Title}
- subTitle={Locale.Chat.EditMessage.Topic.SubTitle}
- >
- <input
- type="text"
- value={session.topic}
- onInput={(e) =>
- chatStore.updateCurrentSession(
- (session) => (session.topic = e.currentTarget.value),
- )
- }
- ></input>
- </ListItem>
- </List>
- <ContextPrompts
- context={messages}
- updateContext={(updater) => {
- const newMessages = messages.slice();
- updater(newMessages);
- setMessages(newMessages);
- }}
- />
- </Modal>
- </div>
- );
- }
- export function DeleteImageButton(props: { deleteImage: () => void }) {
- return (
- <div className={styles["delete-image"]} onClick={props.deleteImage}>
- <DeleteIcon/>
- </div>
- );
- }
- function _Chat() {
- type RenderMessage = ChatMessage & { preview?: boolean };
-
- const chatStore = useChatStore();
- const session = chatStore.currentSession();
- const config = useAppConfig();
- const fontSize = config.fontSize;
- const fontFamily = config.fontFamily;
-
- const [showExport, setShowExport] = useState(false);
-
- const inputRef = useRef<HTMLTextAreaElement>(null);
- const [userInput, setUserInput] = useState("");
- const [isLoading, setIsLoading] = useState(false);
- const {submitKey, shouldSubmit} = useSubmitHandler();
- const scrollRef = useRef<HTMLDivElement>(null);
- const isScrolledToBottom = scrollRef?.current
- ? Math.abs(
- scrollRef.current.scrollHeight -
- (scrollRef.current.scrollTop + scrollRef.current.clientHeight),
- ) <= 1
- : false;
- const {setAutoScroll, scrollDomToBottom} = useScrollToBottom(
- scrollRef,
- isScrolledToBottom,
- );
- const [hitBottom, setHitBottom] = useState(true);
- const isMobileScreen = useMobileScreen();
- const navigate = useNavigate();
- const [attachImages, setAttachImages] = useState<string[]>([]);
- const [uploading, setUploading] = useState(false);
-
- // prompt hints
- const promptStore = usePromptStore();
- const [promptHints, setPromptHints] = useState<RenderPrompt[]>([]);
- const onSearch = useDebouncedCallback(
- (text: string) => {
- const matchedPrompts = promptStore.search(text);
- setPromptHints(matchedPrompts);
- },
- 100,
- {leading: true, trailing: true},
- );
-
- // auto grow input
- const [inputRows, setInputRows] = useState(2);
- const measure = useDebouncedCallback(
- () => {
- const rows = inputRef.current ? autoGrowTextArea(inputRef.current) : 1;
- const inputRows = Math.min(
- 20,
- Math.max(2 + Number(!isMobileScreen), rows),
- );
- setInputRows(inputRows);
- },
- 100,
- {
- leading: true,
- trailing: true,
- },
- );
-
- // eslint-disable-next-line react-hooks/exhaustive-deps
- useEffect(measure, [userInput]);
-
- // chat commands shortcuts
- const chatCommands = useChatCommand({
- new: () => chatStore.newSession(),
- newm: () => navigate(Path.NewChat),
- prev: () => chatStore.nextSession(-1),
- next: () => chatStore.nextSession(1),
- clear: () =>
- chatStore.updateCurrentSession(
- (session) => (session.clearContextIndex = session.messages.length),
- ),
- del: () => chatStore.deleteSession(chatStore.currentSessionIndex),
- });
-
- // only search prompts when user input is short
- const SEARCH_TEXT_LIMIT = 30;
- const onInput = (text: string) => {
- setUserInput(text);
- const n = text.trim().length;
-
- // clear search results
- if (n === 0) {
- setPromptHints([]);
- } else if (text.match(ChatCommandPrefix)) {
- setPromptHints(chatCommands.search(text));
- } else if (!config.disablePromptHint && n < SEARCH_TEXT_LIMIT) {
- // check if need to trigger auto completion
- if (text.startsWith("/")) {
- let searchText = text.slice(1);
- onSearch(searchText);
- }
- }
- };
-
- const doSubmit = (userInput: string) => {
- if (userInput.trim() === "") return;
- const matchCommand = chatCommands.match(userInput);
- if (matchCommand.matched) {
- setUserInput("");
- setPromptHints([]);
- matchCommand.invoke();
- return;
- }
- setIsLoading(true);
- chatStore.onUserInput(userInput, attachImages).then(() => setIsLoading(false));
- setAttachImages([]);
- localStorage.setItem(LAST_INPUT_KEY, userInput);
- setUserInput("");
- setPromptHints([]);
- if (!isMobileScreen) inputRef.current?.focus();
- setAutoScroll(true);
- };
-
- const onPromptSelect = (prompt: RenderPrompt) => {
- setTimeout(() => {
- setPromptHints([]);
-
- const matchedChatCommand = chatCommands.match(prompt.content);
- if (matchedChatCommand.matched) {
- // if user is selecting a chat command, just trigger it
- matchedChatCommand.invoke();
- setUserInput("");
- } else {
- // or fill the prompt
- setUserInput(prompt.content);
- }
- inputRef.current?.focus();
- }, 30);
- };
-
- // stop response
- const onUserStop = (messageId: string) => {
- ChatControllerPool.stop(session.id, messageId);
- };
-
- useEffect(() => {
- chatStore.updateCurrentSession((session) => {
- const stopTiming = Date.now() - REQUEST_TIMEOUT_MS;
- session.messages.forEach((m) => {
- // check if should stop all stale messages
- if (m.isError || new Date(m.date).getTime() < stopTiming) {
- if (m.streaming) {
- m.streaming = false;
- }
-
- if (m.content.length === 0) {
- m.isError = true;
- m.content = prettyObject({
- error: true,
- message: "empty response",
- });
- }
- }
- });
-
- // auto sync mask config from global config
- if (session.mask.syncGlobalConfig) {
- console.log("[Mask] syncing from global, name = ", session.mask.name);
- session.mask.modelConfig = {...config.modelConfig};
- }
- });
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, []);
-
- // check if should send message
- const onInputKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
- if (
- e.key === "ArrowUp" &&
- userInput.length <= 0 &&
- !(e.metaKey || e.altKey || e.ctrlKey)
- ) {
- setUserInput(localStorage.getItem(LAST_INPUT_KEY) ?? "");
- e.preventDefault();
- return;
- }
- if (shouldSubmit(e) && promptHints.length === 0) {
- doSubmit(userInput);
- e.preventDefault();
- }
- };
-
- const onRightClick = (e: any, message: ChatMessage) => {
- // copy to clipboard
- if (selectOrCopy(e.currentTarget, getMessageTextContent(message))) {
- if (userInput.length === 0) {
- setUserInput(getMessageTextContent(message));
- }
-
- e.preventDefault();
- }
- };
-
- const deleteMessage = (msgId?: string) => {
- chatStore.updateCurrentSession(
- (session) =>
- (session.messages = session.messages.filter((m) => m.id !== msgId)),
- );
- };
-
- const onDelete = (msgId: string) => {
- deleteMessage(msgId);
- };
-
- const onResend = (message: ChatMessage) => {
- // when it is resending a message
- // 1. for a user's message, find the next bot response
- // 2. for a bot's message, find the last user's input
- // 3. delete original user input and bot's message
- // 4. resend the user's input
-
- const resendingIndex = session.messages.findIndex(
- (m) => m.id === message.id,
- );
-
- if (resendingIndex < 0 || resendingIndex >= session.messages.length) {
- console.error("[Chat] failed to find resending message", message);
- return;
- }
-
- let userMessage: ChatMessage | undefined;
- let botMessage: ChatMessage | undefined;
-
- if (message.role === "assistant") {
- // if it is resending a bot's message, find the user input for it
- botMessage = message;
- for (let i = resendingIndex; i >= 0; i -= 1) {
- if (session.messages[i].role === "user") {
- userMessage = session.messages[i];
- break;
- }
- }
- } else if (message.role === "user") {
- // if it is resending a user's input, find the bot's response
- userMessage = message;
- for (let i = resendingIndex; i < session.messages.length; i += 1) {
- if (session.messages[i].role === "assistant") {
- botMessage = session.messages[i];
- break;
- }
- }
- }
-
- if (userMessage === undefined) {
- console.error("[Chat] failed to resend", message);
- return;
- }
-
- // delete the original messages
- deleteMessage(userMessage.id);
- deleteMessage(botMessage?.id);
-
- // resend the message
- setIsLoading(true);
- const textContent = getMessageTextContent(userMessage);
- const images = getMessageImages(userMessage);
- chatStore.onUserInput(textContent, images).then(() => setIsLoading(false));
- inputRef.current?.focus();
- };
-
- const onPinMessage = (message: ChatMessage) => {
- chatStore.updateCurrentSession((session) =>
- session.mask.context.push(message),
- );
-
- showToast(Locale.Chat.Actions.PinToastContent, {
- text: Locale.Chat.Actions.PinToastAction,
- onClick: () => {
- setShowPromptModal(true);
- },
- });
- };
-
- const context: RenderMessage[] = useMemo(() => {
- return session.mask.hideContext ? [] : session.mask.context.slice();
- }, [session.mask.context, session.mask.hideContext]);
- const accessStore = useAccessStore();
-
- if (
- context.length === 0 &&
- session.messages.at(0)?.content !== BOT_HELLO.content
- ) {
- const copiedHello = Object.assign({}, BOT_HELLO);
- if (!accessStore.isAuthorized()) {
- copiedHello.content = Locale.Error.Unauthorized;
- }
- context.push(copiedHello);
- }
-
- // preview messages
- const renderMessages = useMemo(() => {
- return context.concat(session.messages as RenderMessage[]).concat(
- isLoading
- ? [
- {
- ...createMessage({
- role: "assistant",
- content: "……",
- }),
- preview: true,
- },
- ]
- : [],
- ).concat(
- userInput.length > 0 && config.sendPreviewBubble
- ? [
- {
- ...createMessage({
- role: "user",
- content: userInput,
- }),
- preview: true,
- },
- ]
- : [],
- );
- }, [
- config.sendPreviewBubble,
- context,
- isLoading,
- session.messages,
- userInput,
- ]);
-
- const [msgRenderIndex, _setMsgRenderIndex] = useState(
- Math.max(0, renderMessages.length - CHAT_PAGE_SIZE),
- );
-
- function setMsgRenderIndex(newIndex: number) {
- newIndex = Math.min(renderMessages.length - CHAT_PAGE_SIZE, newIndex);
- newIndex = Math.max(0, newIndex);
- _setMsgRenderIndex(newIndex);
- }
-
- const messages = useMemo(() => {
- const endRenderIndex = Math.min(
- msgRenderIndex + 3 * CHAT_PAGE_SIZE,
- renderMessages.length,
- );
- return renderMessages.slice(msgRenderIndex, endRenderIndex);
- }, [msgRenderIndex, renderMessages]);
-
- const onChatBodyScroll = (e: HTMLElement) => {
- const bottomHeight = e.scrollTop + e.clientHeight;
- const edgeThreshold = e.clientHeight;
-
- const isTouchTopEdge = e.scrollTop <= edgeThreshold;
- const isTouchBottomEdge = bottomHeight >= e.scrollHeight - edgeThreshold;
- const isHitBottom =
- bottomHeight >= e.scrollHeight - (isMobileScreen ? 4 : 10);
-
- const prevPageMsgIndex = msgRenderIndex - CHAT_PAGE_SIZE;
- const nextPageMsgIndex = msgRenderIndex + CHAT_PAGE_SIZE;
-
- if (isTouchTopEdge && !isTouchBottomEdge) {
- setMsgRenderIndex(prevPageMsgIndex);
- } else if (isTouchBottomEdge) {
- setMsgRenderIndex(nextPageMsgIndex);
- }
-
- setHitBottom(isHitBottom);
- setAutoScroll(isHitBottom);
- };
-
- function scrollToBottom() {
- setMsgRenderIndex(renderMessages.length - CHAT_PAGE_SIZE);
- scrollDomToBottom();
- }
-
- // clear context index = context length + index in messages
- const clearContextIndex =
- (session.clearContextIndex ?? -1) >= 0
- ? session.clearContextIndex! + context.length - msgRenderIndex
- : -1;
-
- const [showPromptModal, setShowPromptModal] = useState(false);
-
- const clientConfig = useMemo(() => getClientConfig(), []);
-
- const autoFocus = !isMobileScreen; // wont auto focus on mobile screen
- const showMaxIcon = !isMobileScreen && !clientConfig?.isApp;
-
- useCommand({
- fill: setUserInput,
- submit: (text) => {
- doSubmit(text);
- },
- code: (text) => {
- if (accessStore.disableFastLink) return;
- console.log("[Command] got code from url: ", text);
- showConfirm(Locale.URLCommand.Code + `code = ${text}`).then((res) => {
- if (res) {
- accessStore.update((access) => (access.accessCode = text));
- }
- });
- },
- settings: (text) => {
- if (accessStore.disableFastLink) return;
-
- try {
- const payload = JSON.parse(text) as {
- key?: string;
- url?: string;
- };
-
- console.log("[Command] got settings from url: ", payload);
-
- if (payload.key || payload.url) {
- showConfirm(
- Locale.URLCommand.Settings +
- `\n${JSON.stringify(payload, null, 4)}`,
- ).then((res) => {
- if (!res) return;
- if (payload.key) {
- accessStore.update(
- (access) => (access.openaiApiKey = payload.key!),
- );
- }
- if (payload.url) {
- accessStore.update((access) => (access.openaiUrl = payload.url!));
- }
- accessStore.update((access) => (access.useCustomConfig = true));
- });
- }
- } catch {
- console.error("[Command] failed to get settings from url: ", text);
- }
- },
- });
-
- // edit / insert message modal
- const [isEditingMessage, setIsEditingMessage] = useState(false);
-
- // remember unfinished input
- useEffect(() => {
- // try to load from local storage
- const key = UNFINISHED_INPUT(session.id);
- const mayBeUnfinishedInput = localStorage.getItem(key);
- if (mayBeUnfinishedInput && userInput.length === 0) {
- setUserInput(mayBeUnfinishedInput);
- localStorage.removeItem(key);
- }
-
- const dom = inputRef.current;
- return () => {
- localStorage.setItem(key, dom?.value ?? "");
- };
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, []);
-
- const handlePaste = useCallback(
- async (event: React.ClipboardEvent<HTMLTextAreaElement>) => {
- const currentModel = chatStore.currentSession().mask.modelConfig.model;
- if (!isVisionModel(currentModel)) {
- return;
- }
- const items = (event.clipboardData || window.clipboardData).items;
- for (const item of items) {
- if (item.kind === "file" && item.type.startsWith("image/")) {
- event.preventDefault();
- const file = item.getAsFile();
- if (file) {
- const images: string[] = [];
- images.push(...attachImages);
- images.push(
- ...(await new Promise<string[]>((res, rej) => {
- setUploading(true);
- const imagesData: string[] = [];
- uploadImageRemote(file).then((dataUrl) => {
- imagesData.push(dataUrl);
- setUploading(false);
- res(imagesData);
- }).catch((e) => {
- setUploading(false);
- rej(e);
- });
- })),
- );
- const imagesLength = images.length;
-
- if (imagesLength > 3) {
- images.splice(3, imagesLength - 3);
- }
- setAttachImages(images);
- }
- }
- }
- },
- [attachImages, chatStore],
- );
-
- async function uploadImage() {
- const images: string[] = [];
- images.push(...attachImages);
-
- images.push(
- ...(await new Promise<string[]>((res, rej) => {
- const fileInput = document.createElement("input");
- fileInput.type = "file";
- fileInput.accept =
- "image/png, image/jpeg, image/webp, image/heic, image/heif";
- fileInput.multiple = true;
- fileInput.onchange = (event: any) => {
- setUploading(true);
- const files = event.target.files;
- const imagesData: string[] = [];
- for (let i = 0; i < files.length; i++) {
- const file = event.target.files[i];
- uploadImageRemote(file).then((dataUrl) => {
- imagesData.push(dataUrl);
- if (
- imagesData.length === 3 ||
- imagesData.length === files.length
- ) {
- setUploading(false);
- res(imagesData);
- }
- }).catch((e) => {
- setUploading(false);
- rej(e);
- });
- }
- };
- fileInput.click();
- })),
- );
-
- const imagesLength = images.length;
- if (imagesLength > 3) {
- images.splice(3, imagesLength - 3);
- }
- setAttachImages(images);
- }
-
- return (
- <div className={styles.chat} key={session.id}>
- <div className="window-header" data-tauri-drag-region>
- {/* {isMobileScreen && (
- <div className="window-actions">
- <div className={"window-action-button"}>
- <IconButton
- icon={<ReturnIcon />}
- bordered
- title={Locale.Chat.Actions.ChatList}
- onClick={() => navigate(Path.Home)}
- />
- </div>
- </div>
- )} */}
- <div style={{display: 'flex', alignItems: 'center'}}
- className={`window-header-title ${styles["chat-body-title"]}`}>
- <div>
- <img style={{ marginTop: '10%', marginRight : '10px', height:42 }} src={faviconSrc.src} />
- {/* <Favicon className="window-header-icon" /> */}
- </div>
- <div>
- <div
- className={`window-header-main-title ${styles["chat-body-main-title"]}`}
- // onClickCapture={() => setIsEditingMessage(true)}
- >
- 建科小K
- </div>
- <div className="window-header-sub-title">
- {Locale.Chat.SubTitle(session.messages.length)}
- </div>
- </div>
- </div>
- <div className="window-actions">
- <IconButton
- icon={<AddIcon/>}
- bordered
- title='新建对话'
- aria='新建对话'
- onClick={() => {
- localStorage.clear()
- location.reload()
- }}
- />
- {/* {!isMobileScreen && (
- <div className="window-action-button">
- <IconButton
- icon={<RenameIcon />}
- bordered
- title={Locale.Chat.EditMessage.Title}
- aria={Locale.Chat.EditMessage.Title}
- onClick={() => setIsEditingMessage(true)}
- />
- </div>
- )} */}
- {/* <div className="window-action-button">
- <IconButton
- icon={<ExportIcon />}
- bordered
- title={Locale.Chat.Actions.Export}
- onClick={() => {
- setShowExport(true);
- }}
- />
- </div> */}
- {/* {showMaxIcon && (
- <div className="window-action-button">
- <IconButton
- icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
- bordered
- title={Locale.Chat.Actions.FullScreen}
- aria={Locale.Chat.Actions.FullScreen}
- onClick={() => {
- config.update(
- (config) => (config.tightBorder = !config.tightBorder),
- );
- }}
- />
- </div>
- )} */}
- </div>
-
- {/* <PromptToast
- showToast={!hitBottom}
- showModal={showPromptModal}
- setShowModal={setShowPromptModal}
- /> */}
- </div>
-
- <div
- className={styles["chat-body"]}
- ref={scrollRef}
- onScroll={(e) => onChatBodyScroll(e.currentTarget)}
- onMouseDown={() => inputRef.current?.blur()}
- onTouchStart={() => {
- inputRef.current?.blur();
- setAutoScroll(false);
- }}
- >
- {messages.map((message, i) => {
- const isUser = message.role === "user";
- const isContext = i < context.length;
- const showActions =
- i > 0 &&
- !(message.preview || message.content.length === 0) &&
- !isContext;
- const showTyping = message.preview || message.streaming;
-
- const shouldShowClearContextDivider = i === clearContextIndex - 1;
-
- return (
- <Fragment key={message.id}>
- <div
- className={
- isUser ? styles["chat-message-user"] : styles["chat-message"]
- }
- >
- <div className={styles["chat-message-container"]}>
- <div className={styles["chat-message-header"]}>
- <div className={styles["chat-message-avatar"]}>
- {/* <div className={styles["chat-message-edit"]}>
- <IconButton
- icon={<EditIcon />}
- aria={Locale.Chat.Actions.Edit}
- onClick={async () => {
- const newMessage = await showPrompt(
- Locale.Chat.Actions.Edit,
- getMessageTextContent(message),
- 10,
- );
- let newContent: string | MultimodalContent[] =
- newMessage;
- const images = getMessageImages(message);
- if (images.length > 0) {
- newContent = [{ type: "text", text: newMessage }];
- for (let i = 0; i < images.length; i++) {
- newContent.push({
- type: "image_url",
- image_url: {
- url: images[i],
- },
- });
- }
- }
- chatStore.updateCurrentSession((session) => {
- const m = session.mask.context
- .concat(session.messages)
- .find((m) => m.id === message.id);
- if (m) {
- m.content = newContent;
- }
- });
- }}
- ></IconButton>
- </div> */}
- {isUser ? (
- // 在这里换头像
- <div style={{position: 'relative'}}>
- <MaskAvatar
- avatar={session.mask.avatar}
- />
- <div
- style={{
- position: 'absolute',
- zIndex: 2,
- top: '50%',
- left: '50%',
- transform: ' translate(-50%, -50%)',
- fontSize: 14
- }}>
- 我
- </div>
- </div>
- ) : (
- <>
- {["system"].includes(message.role) ? (
- <Avatar avatar="2699-fe0f"/>
- ) : (
- <MaskAvatar
- avatar={session.mask.avatar}
- model={
- message.model || session.mask.modelConfig.model
- }
- />
- )}
- </>
- )}
- </div>
- {/* {showActions && (
- <div className={styles["chat-message-actions"]}>
- <div className={styles["chat-input-actions"]}>
- {message.streaming ? (
- <ChatAction
- text={Locale.Chat.Actions.Stop}
- icon={<StopIcon />}
- onClick={() => onUserStop(message.id ?? i)}
- />
- ) : (
- <>
- <ChatAction
- text={Locale.Chat.Actions.Retry}
- icon={<ResetIcon />}
- onClick={() => onResend(message)}
- />
- <ChatAction
- text={Locale.Chat.Actions.Delete}
- icon={<DeleteIcon />}
- onClick={() => onDelete(message.id ?? i)}
- />
- <ChatAction
- text={Locale.Chat.Actions.Pin}
- icon={<PinIcon />}
- onClick={() => onPinMessage(message)}
- />
- <ChatAction
- text={Locale.Chat.Actions.Copy}
- icon={<CopyIcon />}
- onClick={() =>
- copyToClipboard(
- getMessageTextContent(message),
- )
- }
- />
- </>
- )}
- </div>
- </div>
- )} */}
- </div>
- {showTyping && (
- <div className={styles["chat-message-status"]}>
- {Locale.Chat.Typing}
- </div>
- )}
- <div className={styles["chat-message-item"]}>
- <Markdown
- key={message.streaming ? "loading" : "done"}
- content={getMessageTextContent(message)}
- loading={
- (message.preview || message.streaming) &&
- message.content.length === 0 &&
- !isUser
- }
- // onContextMenu={(e) => onRightClick(e, message)}
- onDoubleClickCapture={() => {
- if (!isMobileScreen) return;
- setUserInput(getMessageTextContent(message));
- }}
- fontSize={fontSize}
- fontFamily={fontFamily}
- parentRef={scrollRef}
- defaultShow={i >= messages.length - 6}
- />
- {getMessageImages(message).length == 1 && (
- <img
- className={styles["chat-message-item-image"]}
- src={getMessageImages(message)[0]}
- alt=""
- />
- )}
- {getMessageImages(message).length > 1 && (
- <div
- className={styles["chat-message-item-images"]}
- style={
- {
- "--image-count": getMessageImages(message).length,
- } as React.CSSProperties
- }
- >
- {getMessageImages(message).map((image, index) => {
- return (
- <img
- className={
- styles["chat-message-item-image-multi"]
- }
- key={index}
- src={image}
- alt=""
- />
- );
- })}
- </div>
- )}
- </div>
- {/* <div className={styles["chat-message-action-date"]}>
- {isContext
- ? Locale.Chat.IsContext
- : message.date.toLocaleString()}
- </div> */}
- </div>
- </div>
- {shouldShowClearContextDivider && <ClearContextDivider/>}
- </Fragment>
- );
- })}
- </div>
-
- <div className={styles["chat-input-panel"]}>
- {/* <PromptHints prompts={promptHints} onPromptSelect={onPromptSelect} /> */}
-
- <ChatActions
- setUserInput={setUserInput}
- doSubmit={doSubmit}
- uploadImage={uploadImage}
- setAttachImages={setAttachImages}
- setUploading={setUploading}
- showPromptModal={() => setShowPromptModal(true)}
- scrollToBottom={scrollToBottom}
- hitBottom={hitBottom}
- uploading={uploading}
- showPromptHints={() => {
- // Click again to close
- if (promptHints.length > 0) {
- setPromptHints([]);
- return;
- }
-
- inputRef.current?.focus();
- setUserInput("/");
- onSearch("");
- }}
- />
- <label
- className={`${styles["chat-input-panel-inner"]} ${attachImages.length != 0
- ? styles["chat-input-panel-inner-attach"]
- : ""
- }`}
- htmlFor="chat-input"
- >
- <textarea
- id="chat-input"
- ref={inputRef}
- className={styles["chat-input"]}
- placeholder={Locale.Chat.Input(submitKey)}
- onInput={(e) => onInput(e.currentTarget.value)}
- value={userInput}
- onKeyDown={onInputKeyDown}
- onFocus={scrollToBottom}
- onClick={scrollToBottom}
- onPaste={handlePaste}
- rows={inputRows}
- autoFocus={autoFocus}
- style={{
- fontSize: config.fontSize,
- fontFamily: config.fontFamily,
- }}
- />
- {attachImages.length != 0 && (
- <div className={styles["attach-images"]}>
- {attachImages.map((image, index) => {
- return (
- <div
- key={index}
- className={styles["attach-image"]}
- style={{backgroundImage: `url("${image}")`}}
- >
- <div className={styles["attach-image-mask"]}>
- <DeleteImageButton
- deleteImage={() => {
- setAttachImages(
- attachImages.filter((_, i) => i !== index),
- );
- }}
- />
- </div>
- </div>
- );
- })}
- </div>
- )}
- <IconButton
- icon={<SendWhiteIcon/>}
- text={Locale.Chat.Send}
- className={styles["chat-input-send"]}
- type="primary"
- onClick={() => doSubmit(userInput)}
- />
- </label>
- </div>
-
- {showExport && (
- <ExportMessageModal onClose={() => setShowExport(false)}/>
- )}
-
- {isEditingMessage && (
- <EditMessageModal
- onClose={() => {
- setIsEditingMessage(false);
- }}
- />
- )}
- </div>
- );
- }
- export function Chat() {
- const chatStore = useChatStore();
- const sessionIndex = chatStore.currentSessionIndex;
- return <_Chat key={sessionIndex}></_Chat>;
- }
|