import { IconButton } from "./button"; import { ErrorBoundary } from "./error"; import styles from "./plugin.module.scss"; import DownloadIcon from "../icons/download.svg"; import UploadIcon from "../icons/upload.svg"; import EditIcon from "../icons/edit.svg"; import AddIcon from "../icons/add.svg"; import CloseIcon from "../icons/close.svg"; import DeleteIcon from "../icons/delete.svg"; import EyeIcon from "../icons/eye.svg"; import CopyIcon from "../icons/copy.svg"; import LeftIcon from "../icons/left.svg"; import { Plugin, usePluginStore } from "../store/plugin"; import { ChatMessage, createMessage, ModelConfig, useAppConfig, useChatStore, } from "../store"; import { ROLES } from "../client/api"; import { Input, List, ListItem, Modal, Popover, Select, showConfirm, } from "./ui-lib"; import { Avatar, AvatarPicker } from "./emoji"; import Locale, { AllLangs, ALL_LANG_OPTIONS, Lang } from "../locales"; import { useLocation, useNavigate } from "react-router-dom"; import chatStyle from "./chat.module.scss"; import { useEffect, useState } from "react"; import { copyToClipboard, downloadAs, readFromFile } from "../utils"; import { Updater } from "../typing"; import { ModelConfigList } from "./model-config"; import { FileName, Path } from "../constant"; import { BUILTIN_PLUGIN_STORE } from "../plugins"; import { nanoid } from "nanoid"; import { getISOLang, getLang } from "../locales"; // export function PluginConfig(props: { // plugin: Plugin; // updateMask: Updater; // extraListItems?: JSX.Element; // readonly?: boolean; // shouldSyncFromGlobal?: boolean; // }) { // const [showPicker, setShowPicker] = useState(false); // const updateConfig = (updater: (config: ModelConfig) => void) => { // if (props.readonly) return; // // 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; // }); // }; // const globalConfig = useAppConfig(); // return ( // <> // { // const context = props.mask.context.slice(); // updater(context); // props.updateMask((mask) => (mask.context = context)); // }} // /> // // // { // props.updateMask((mask) => (mask.avatar = emoji)); // setShowPicker(false); // }} // > // } // open={showPicker} // onClose={() => setShowPicker(false)} // > //
setShowPicker(true)} // style={{ cursor: "pointer" }} // > //
//
//
// // // props.updateMask((mask) => { // mask.name = e.currentTarget.value; // }) // } // > // // // { // props.updateMask((mask) => { // mask.hideContext = e.currentTarget.checked; // }); // }} // > // // {!props.shouldSyncFromGlobal ? ( // // } // text={Locale.Mask.Config.Share.Action} // onClick={copyMaskLink} // /> // // ) : null} // {props.shouldSyncFromGlobal ? ( // // { // 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; // }); // } // }} // > // // ) : null} //
// // // {props.extraListItems} // // // ); // } function ContextPromptItem(props: { prompt: ChatMessage; update: (prompt: ChatMessage) => void; remove: () => void; }) { const [focusingInput, setFocusingInput] = useState(false); return (
{!focusingInput && ( )} setFocusingInput(true)} onBlur={() => { setFocusingInput(false); // 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 && ( } className={chatStyle["context-delete-button"]} onClick={() => props.remove()} bordered /> )}
); } export function ContextPrompts(props: { context: ChatMessage[]; updateContext: (updater: (context: ChatMessage[]) => void) => void; }) { const context = props.context; const addContextPrompt = (prompt: ChatMessage) => { props.updateContext((context) => context.push(prompt)); }; const removeContextPrompt = (i: number) => { props.updateContext((context) => context.splice(i, 1)); }; const updateContextPrompt = (i: number, prompt: ChatMessage) => { props.updateContext((context) => (context[i] = prompt)); }; return ( <>
{context.map((c, i) => ( updateContextPrompt(i, prompt)} remove={() => removeContextPrompt(i)} /> ))}
} text={Locale.Context.Add} bordered className={chatStyle["context-prompt-button"]} onClick={() => addContextPrompt( createMessage({ role: "user", content: "", date: "", }), ) } />
); } export function PluginPage() { const navigate = useNavigate(); const pluginStore = usePluginStore(); const chatStore = useChatStore(); const allPlugins = pluginStore .getAll() .filter( (m) => !getLang() || m.lang === (getLang() == "cn" ? getLang() : "en"), ); const [searchPlugins, setSearchPlugins] = useState([]); const [searchText, setSearchText] = useState(""); const plugins = searchText.length > 0 ? searchPlugins : allPlugins; // simple search, will refactor later const onSearch = (text: string) => { setSearchText(text); if (text.length > 0) { const result = allPlugins.filter((m) => m.name.includes(text)); setSearchPlugins(result); } else { setSearchPlugins(allPlugins); } }; const [editingPluginId, setEditingPluginId] = useState(); const editingPlugin = pluginStore.get(editingPluginId) ?? BUILTIN_PLUGIN_STORE.get(editingPluginId); const closePluginModal = () => setEditingPluginId(undefined); const downloadAll = () => { downloadAs(JSON.stringify(plugins), FileName.Plugins); }; const updatePluginEnableStatus = (id: string, enable: boolean) => { console.log(enable); if (enable) pluginStore.enable(id); else pluginStore.disable(id); }; const importFromFile = () => { readFromFile().then((content) => { try { const importPlugins = JSON.parse(content); if (Array.isArray(importPlugins)) { for (const plugin of importPlugins) { if (plugin.name) { pluginStore.create(plugin); } } return; } if (importPlugins.name) { pluginStore.create(importPlugins); } } catch {} }); }; return (
} text={Locale.NewChat.Return} onClick={() => navigate(Path.Home)} >
{Locale.Plugin.Page.Title}
{Locale.Plugin.Page.SubTitle(allPlugins.length)}
{/*
} bordered onClick={downloadAll} />
} bordered onClick={() => importFromFile()} />
*/}
onSearch(e.currentTarget.value)} /> {/* } text={Locale.Mask.Page.Create} bordered onClick={() => { const createdMask = pluginStore.create(); setEditingMaskId(createdMask.id); }} /> */}
{plugins.map((m) => (
{m.name}
{/* 描述 */}
{`${m.description}`}
{ updatePluginEnableStatus(m.id, e.currentTarget.checked); }} > {/* {m.builtin ? ( } text={Locale.Mask.Item.View} onClick={() => setEditingMaskId(m.id)} /> ) : ( } text={Locale.Mask.Item.Edit} onClick={() => setEditingMaskId(m.id)} /> )} {!m.builtin && ( } text={Locale.Mask.Item.Delete} onClick={async () => { if (await showConfirm(Locale.Mask.Item.DeleteConfirm)) { maskStore.delete(m.id); } }} /> )} */}
))}
{editingPlugin && (
} text={Locale.Plugin.EditModal.Download} key="export" bordered onClick={() => downloadAs( JSON.stringify(editingPlugin), `${editingPlugin.name}.json`, ) } />, ]} > {/* pluginStore.update(editingPluginId!, updater) } readonly={editingPlugin.builtin} /> */}
)}
); }