ChatGPT-Next-Web/app/components/mask.tsx

541 lines
16 KiB
TypeScript
Raw Normal View History

2023-04-24 16:49:27 +00:00
import { IconButton } from "./button";
import { ErrorBoundary } from "./error";
import styles from "./mask.module.scss";
import DownloadIcon from "../icons/download.svg";
2023-04-26 17:16:21 +00:00
import UploadIcon from "../icons/upload.svg";
2023-04-24 16:49:27 +00:00
import EditIcon from "../icons/edit.svg";
import AddIcon from "../icons/add.svg";
import CloseIcon from "../icons/close.svg";
import DeleteIcon from "../icons/delete.svg";
2023-04-26 17:16:21 +00:00
import EyeIcon from "../icons/eye.svg";
2023-04-24 16:49:27 +00:00
import CopyIcon from "../icons/copy.svg";
2023-04-26 17:16:21 +00:00
import { DEFAULT_MASK_AVATAR, Mask, useMaskStore } from "../store/mask";
2023-07-09 11:37:42 +00:00
import {
ChatMessage,
createMessage,
ModelConfig,
useAppConfig,
useChatStore,
} from "../store";
import { ROLES } from "../client/api";
import {
Input,
List,
ListItem,
Modal,
Popover,
Select,
showConfirm,
} from "./ui-lib";
2023-04-26 17:16:21 +00:00
import { Avatar, AvatarPicker } from "./emoji";
import Locale, { AllLangs, ALL_LANG_OPTIONS, Lang } from "../locales";
2023-04-24 16:49:27 +00:00
import { useNavigate } from "react-router-dom";
import chatStyle from "./chat.module.scss";
import { useEffect, useState } from "react";
2023-07-05 15:19:54 +00:00
import { copyToClipboard, downloadAs, readFromFile } from "../utils";
import { Updater } from "../typing";
2023-04-24 16:49:27 +00:00
import { ModelConfigList } from "./model-config";
2023-04-26 17:16:21 +00:00
import { FileName, Path } from "../constant";
import { BUILTIN_MASK_STORE } from "../masks";
2023-07-09 11:37:42 +00:00
import { nanoid } from "nanoid";
2023-04-25 18:02:46 +00:00
export function MaskAvatar(props: { mask: Mask }) {
return props.mask.avatar !== DEFAULT_MASK_AVATAR ? (
<Avatar avatar={props.mask.avatar} />
) : (
<Avatar model={props.mask.modelConfig.model} />
);
}
2023-04-24 16:49:27 +00:00
export function MaskConfig(props: {
mask: Mask;
updateMask: Updater<Mask>;
extraListItems?: JSX.Element;
2023-04-26 17:16:21 +00:00
readonly?: boolean;
shouldSyncFromGlobal?: boolean;
2023-04-24 16:49:27 +00:00
}) {
const [showPicker, setShowPicker] = useState(false);
const updateConfig = (updater: (config: ModelConfig) => void) => {
2023-04-26 17:16:21 +00:00
if (props.readonly) return;
2023-04-24 16:49:27 +00:00
const config = { ...props.mask.modelConfig };
updater(config);
props.updateMask((mask) => {
mask.modelConfig = config;
// if user changed current session mask, it will disable auto sync
mask.syncGlobalConfig = false;
});
2023-04-24 16:49:27 +00:00
};
2023-07-05 15:19:54 +00:00
const copyMaskLink = () => {
const maskLink = `${location.protocol}//${location.host}/#${Path.NewChat}?mask=${props.mask.id}`;
copyToClipboard(maskLink);
};
const globalConfig = useAppConfig();
2023-04-24 16:49:27 +00:00
return (
<>
<ContextPrompts
context={props.mask.context}
updateContext={(updater) => {
const context = props.mask.context.slice();
updater(context);
props.updateMask((mask) => (mask.context = context));
}}
/>
<List>
2023-04-26 17:16:21 +00:00
<ListItem title={Locale.Mask.Config.Avatar}>
2023-04-24 16:49:27 +00:00
<Popover
content={
<AvatarPicker
onEmojiClick={(emoji) => {
props.updateMask((mask) => (mask.avatar = emoji));
setShowPicker(false);
}}
></AvatarPicker>
}
open={showPicker}
onClose={() => setShowPicker(false)}
>
<div
onClick={() => setShowPicker(true)}
style={{ cursor: "pointer" }}
>
2023-04-25 18:02:46 +00:00
<MaskAvatar mask={props.mask} />
2023-04-24 16:49:27 +00:00
</div>
</Popover>
</ListItem>
2023-04-26 17:16:21 +00:00
<ListItem title={Locale.Mask.Config.Name}>
2023-04-24 16:49:27 +00:00
<input
type="text"
value={props.mask.name}
onInput={(e) =>
props.updateMask((mask) => {
mask.name = e.currentTarget.value;
})
2023-04-24 16:49:27 +00:00
}
></input>
</ListItem>
<ListItem
title={Locale.Mask.Config.HideContext.Title}
subTitle={Locale.Mask.Config.HideContext.SubTitle}
>
<input
type="checkbox"
checked={props.mask.hideContext}
onChange={(e) => {
props.updateMask((mask) => {
mask.hideContext = e.currentTarget.checked;
});
}}
></input>
</ListItem>
2023-07-05 15:19:54 +00:00
{!props.shouldSyncFromGlobal ? (
<ListItem
title={Locale.Mask.Config.Share.Title}
subTitle={Locale.Mask.Config.Share.SubTitle}
>
<IconButton
icon={<CopyIcon />}
text={Locale.Mask.Config.Share.Action}
onClick={copyMaskLink}
/>
</ListItem>
) : null}
{props.shouldSyncFromGlobal ? (
<ListItem
title={Locale.Mask.Config.Sync.Title}
subTitle={Locale.Mask.Config.Sync.SubTitle}
>
<input
type="checkbox"
checked={props.mask.syncGlobalConfig}
onChange={async (e) => {
const checked = e.currentTarget.checked;
if (
checked &&
(await showConfirm(Locale.Mask.Config.Sync.Confirm))
) {
props.updateMask((mask) => {
mask.syncGlobalConfig = checked;
mask.modelConfig = { ...globalConfig.modelConfig };
});
} else if (!checked) {
props.updateMask((mask) => {
mask.syncGlobalConfig = checked;
});
}
}}
></input>
</ListItem>
) : null}
2023-04-24 16:49:27 +00:00
</List>
<List>
<ModelConfigList
modelConfig={{ ...props.mask.modelConfig }}
updateConfig={updateConfig}
/>
{props.extraListItems}
</List>
</>
);
}
function ContextPromptItem(props: {
prompt: ChatMessage;
update: (prompt: ChatMessage) => void;
remove: () => void;
}) {
const [focusingInput, setFocusingInput] = useState(false);
return (
<div className={chatStyle["context-prompt-row"]}>
{!focusingInput && (
<Select
value={props.prompt.role}
className={chatStyle["context-role"]}
onChange={(e) =>
props.update({
...props.prompt,
role: e.target.value as any,
})
}
>
{ROLES.map((r) => (
<option key={r} value={r}>
{r}
</option>
))}
</Select>
)}
<Input
value={props.prompt.content}
type="text"
className={chatStyle["context-content"]}
rows={focusingInput ? 5 : 1}
onFocus={() => setFocusingInput(true)}
onBlur={() => {
setFocusingInput(false);
2023-06-14 06:40:51 +00:00
// If the selection is not removed when the user loses focus, some
// extensions like "Translate" will always display a floating bar
window?.getSelection()?.removeAllRanges();
}}
onInput={(e) =>
props.update({
...props.prompt,
content: e.currentTarget.value as any,
})
}
/>
{!focusingInput && (
<IconButton
icon={<DeleteIcon />}
className={chatStyle["context-delete-button"]}
onClick={() => props.remove()}
bordered
/>
)}
</div>
);
}
2023-04-24 16:49:27 +00:00
export function ContextPrompts(props: {
context: ChatMessage[];
updateContext: (updater: (context: ChatMessage[]) => void) => void;
2023-04-24 16:49:27 +00:00
}) {
const context = props.context;
const addContextPrompt = (prompt: ChatMessage) => {
2023-04-24 16:49:27 +00:00
props.updateContext((context) => context.push(prompt));
};
const removeContextPrompt = (i: number) => {
props.updateContext((context) => context.splice(i, 1));
};
const updateContextPrompt = (i: number, prompt: ChatMessage) => {
2023-04-24 16:49:27 +00:00
props.updateContext((context) => (context[i] = prompt));
};
return (
<>
<div className={chatStyle["context-prompt"]} style={{ marginBottom: 20 }}>
{context.map((c, i) => (
<ContextPromptItem
key={i}
prompt={c}
update={(prompt) => updateContextPrompt(i, prompt)}
remove={() => removeContextPrompt(i)}
/>
2023-04-24 16:49:27 +00:00
))}
<div className={chatStyle["context-prompt-row"]}>
<IconButton
icon={<AddIcon />}
text={Locale.Context.Add}
bordered
className={chatStyle["context-prompt-button"]}
onClick={() =>
2023-07-09 11:37:42 +00:00
addContextPrompt(
createMessage({
role: "user",
content: "",
date: "",
}),
)
2023-04-24 16:49:27 +00:00
}
/>
</div>
</div>
</>
);
}
export function MaskPage() {
const navigate = useNavigate();
2023-04-25 18:02:46 +00:00
const maskStore = useMaskStore();
const chatStore = useChatStore();
2023-04-26 17:16:21 +00:00
const [filterLang, setFilterLang] = useState<Lang>();
const allMasks = maskStore
.getAll()
.filter((m) => !filterLang || m.lang === filterLang);
const [searchMasks, setSearchMasks] = useState<Mask[]>([]);
const [searchText, setSearchText] = useState("");
const masks = searchText.length > 0 ? searchMasks : allMasks;
// simple search, will refactor later
const onSearch = (text: string) => {
setSearchText(text);
if (text.length > 0) {
const result = allMasks.filter((m) => m.name.includes(text));
setSearchMasks(result);
} else {
setSearchMasks(allMasks);
}
};
2023-04-25 18:02:46 +00:00
2023-07-09 11:37:42 +00:00
const [editingMaskId, setEditingMaskId] = useState<string | undefined>();
2023-04-26 17:16:21 +00:00
const editingMask =
maskStore.get(editingMaskId) ?? BUILTIN_MASK_STORE.get(editingMaskId);
2023-04-25 18:02:46 +00:00
const closeMaskModal = () => setEditingMaskId(undefined);
2023-04-24 16:49:27 +00:00
2023-04-26 17:16:21 +00:00
const downloadAll = () => {
downloadAs(JSON.stringify(masks), FileName.Masks);
};
2023-05-04 14:33:13 +00:00
const importFromFile = () => {
readFromFile().then((content) => {
try {
const importMasks = JSON.parse(content);
if (Array.isArray(importMasks)) {
for (const mask of importMasks) {
if (mask.name) {
maskStore.create(mask);
}
}
2023-05-19 02:39:34 +00:00
return;
}
//if the content is a single mask.
if (importMasks.name) {
maskStore.create(importMasks);
2023-05-04 14:33:13 +00:00
}
} catch {}
});
};
2023-04-24 16:49:27 +00:00
return (
<ErrorBoundary>
<div className={styles["mask-page"]}>
<div className="window-header">
<div className="window-header-title">
2023-04-26 17:16:21 +00:00
<div className="window-header-main-title">
{Locale.Mask.Page.Title}
</div>
2023-04-25 18:02:46 +00:00
<div className="window-header-submai-title">
2023-04-26 17:16:21 +00:00
{Locale.Mask.Page.SubTitle(allMasks.length)}
2023-04-25 18:02:46 +00:00
</div>
2023-04-24 16:49:27 +00:00
</div>
<div className="window-actions">
<div className="window-action-button">
2023-04-25 18:02:46 +00:00
<IconButton
2023-04-26 17:16:21 +00:00
icon={<DownloadIcon />}
2023-04-25 18:02:46 +00:00
bordered
2023-04-26 17:16:21 +00:00
onClick={downloadAll}
2023-04-25 18:02:46 +00:00
/>
2023-04-24 16:49:27 +00:00
</div>
<div className="window-action-button">
2023-04-26 17:16:21 +00:00
<IconButton
icon={<UploadIcon />}
bordered
2023-05-04 14:33:13 +00:00
onClick={() => importFromFile()}
2023-04-26 17:16:21 +00:00
/>
2023-04-24 16:49:27 +00:00
</div>
<div className="window-action-button">
<IconButton
icon={<CloseIcon />}
bordered
onClick={() => navigate(-1)}
/>
</div>
</div>
</div>
<div className={styles["mask-page-body"]}>
2023-04-26 17:16:21 +00:00
<div className={styles["mask-filter"]}>
<input
type="text"
className={styles["search-bar"]}
placeholder={Locale.Mask.Page.Search}
autoFocus
onInput={(e) => onSearch(e.currentTarget.value)}
/>
<Select
2023-04-26 17:16:21 +00:00
className={styles["mask-filter-lang"]}
value={filterLang ?? Locale.Settings.Lang.All}
onChange={(e) => {
const value = e.currentTarget.value;
if (value === Locale.Settings.Lang.All) {
setFilterLang(undefined);
} else {
setFilterLang(value as Lang);
}
}}
>
<option key="all" value={Locale.Settings.Lang.All}>
{Locale.Settings.Lang.All}
</option>
{AllLangs.map((lang) => (
<option value={lang} key={lang}>
{ALL_LANG_OPTIONS[lang]}
2023-04-26 17:16:21 +00:00
</option>
))}
</Select>
2023-04-26 17:16:21 +00:00
2023-05-01 19:01:42 +00:00
<IconButton
className={styles["mask-create"]}
icon={<AddIcon />}
text={Locale.Mask.Page.Create}
bordered
onClick={() => {
const createdMask = maskStore.create();
setEditingMaskId(createdMask.id);
}}
/>
2023-04-26 17:16:21 +00:00
</div>
2023-04-24 16:49:27 +00:00
2023-04-25 18:02:46 +00:00
<div>
2023-04-24 16:49:27 +00:00
{masks.map((m) => (
2023-04-25 18:02:46 +00:00
<div className={styles["mask-item"]} key={m.id}>
<div className={styles["mask-header"]}>
2023-04-24 16:49:27 +00:00
<div className={styles["mask-icon"]}>
2023-04-25 18:02:46 +00:00
<MaskAvatar mask={m} />
</div>
<div className={styles["mask-title"]}>
<div className={styles["mask-name"]}>{m.name}</div>
<div className={styles["mask-info"] + " one-line"}>
2023-04-26 17:16:21 +00:00
{`${Locale.Mask.Item.Info(m.context.length)} / ${
ALL_LANG_OPTIONS[m.lang]
2023-04-25 18:02:46 +00:00
} / ${m.modelConfig.model}`}
</div>
2023-04-24 16:49:27 +00:00
</div>
2023-04-25 18:02:46 +00:00
</div>
2023-04-24 16:49:27 +00:00
<div className={styles["mask-actions"]}>
2023-04-25 18:02:46 +00:00
<IconButton
icon={<AddIcon />}
2023-04-26 17:16:21 +00:00
text={Locale.Mask.Item.Chat}
2023-04-25 18:02:46 +00:00
onClick={() => {
chatStore.newSession(m);
navigate(Path.Chat);
}}
/>
2023-04-26 17:16:21 +00:00
{m.builtin ? (
<IconButton
icon={<EyeIcon />}
text={Locale.Mask.Item.View}
onClick={() => setEditingMaskId(m.id)}
/>
) : (
<IconButton
icon={<EditIcon />}
text={Locale.Mask.Item.Edit}
onClick={() => setEditingMaskId(m.id)}
/>
)}
{!m.builtin && (
<IconButton
icon={<DeleteIcon />}
text={Locale.Mask.Item.Delete}
onClick={async () => {
if (await showConfirm(Locale.Mask.Item.DeleteConfirm)) {
2023-04-26 17:16:21 +00:00
maskStore.delete(m.id);
}
}}
/>
)}
2023-04-24 16:49:27 +00:00
</div>
2023-04-25 18:02:46 +00:00
</div>
2023-04-24 16:49:27 +00:00
))}
2023-04-25 18:02:46 +00:00
</div>
2023-04-24 16:49:27 +00:00
</div>
</div>
2023-04-25 18:02:46 +00:00
{editingMask && (
<div className="modal-mask">
2023-04-26 17:16:21 +00:00
<Modal
title={Locale.Mask.EditModal.Title(editingMask?.builtin)}
onClose={closeMaskModal}
actions={[
<IconButton
icon={<DownloadIcon />}
text={Locale.Mask.EditModal.Download}
key="export"
bordered
2023-04-28 11:25:03 +00:00
onClick={() =>
2023-05-04 14:33:13 +00:00
downloadAs(
JSON.stringify(editingMask),
`${editingMask.name}.json`,
)
2023-04-28 11:25:03 +00:00
}
2023-04-26 17:16:21 +00:00
/>,
<IconButton
key="copy"
icon={<CopyIcon />}
bordered
text={Locale.Mask.EditModal.Clone}
onClick={() => {
navigate(Path.Masks);
maskStore.create(editingMask);
setEditingMaskId(undefined);
}}
/>,
]}
>
2023-04-25 18:02:46 +00:00
<MaskConfig
2023-04-26 17:16:21 +00:00
mask={editingMask}
2023-04-25 18:02:46 +00:00
updateMask={(updater) =>
maskStore.update(editingMaskId!, updater)
}
2023-04-26 17:16:21 +00:00
readonly={editingMask.builtin}
2023-04-25 18:02:46 +00:00
/>
</Modal>
</div>
)}
2023-04-24 16:49:27 +00:00
</ErrorBoundary>
);
}