-
-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from lobehub/feat/plugin-dev
✨ feat: 插件开发者模式
- Loading branch information
Showing
22 changed files
with
941 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Button, Switch } from 'antd'; | ||
import isEqual from 'fast-deep-equal'; | ||
import { memo, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { Flexbox } from 'react-layout-kit'; | ||
import DevModal from 'src/features/PluginDevModal'; | ||
|
||
import { pluginSelectors, usePluginStore } from '@/store/plugin'; | ||
|
||
import { useStore } from '../store'; | ||
|
||
const MarketList = memo<{ id: string }>(({ id }) => { | ||
const { t } = useTranslation('plugin'); | ||
|
||
const [showModal, setModal] = useState(false); | ||
|
||
const updateConfig = useStore((s) => s.toggleAgentPlugin); | ||
const [plugins, hasPlugin] = useStore((s) => [s.config.plugins || [], !!s.config.plugins]); | ||
|
||
const [useFetchPluginList, fetchPluginManifest, dispatchDevPluginList] = usePluginStore((s) => [ | ||
s.useFetchPluginList, | ||
s.fetchPluginManifest, | ||
s.dispatchDevPluginList, | ||
]); | ||
|
||
const pluginManifestLoading = usePluginStore((s) => s.pluginManifestLoading, isEqual); | ||
const devPlugin = usePluginStore(pluginSelectors.getDevPluginById(id), isEqual); | ||
|
||
useFetchPluginList(); | ||
|
||
return ( | ||
<> | ||
<DevModal | ||
onDelete={() => { | ||
dispatchDevPluginList({ id, type: 'deleteItem' }); | ||
}} | ||
onOpenChange={setModal} | ||
onSave={(value) => { | ||
dispatchDevPluginList({ id, plugin: value, type: 'updateItem' }); | ||
}} | ||
open={showModal} | ||
showDelete | ||
value={devPlugin} | ||
/> | ||
|
||
<Flexbox align={'center'} gap={8} horizontal> | ||
<Switch | ||
checked={ | ||
// 如果在加载中,说明激活了 | ||
pluginManifestLoading[id] || !hasPlugin ? false : plugins.includes(id) | ||
} | ||
loading={pluginManifestLoading[id]} | ||
onChange={(checked) => { | ||
updateConfig(id); | ||
if (checked) { | ||
fetchPluginManifest(id); | ||
} | ||
}} | ||
/> | ||
<Button | ||
onClick={() => { | ||
setModal(true); | ||
}} | ||
> | ||
{t('list.item.local.config')} | ||
</Button> | ||
</Flexbox> | ||
</> | ||
); | ||
}); | ||
|
||
export default MarketList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { Avatar, Form, Icon, Tooltip } from '@lobehub/ui'; | ||
import { Button, Skeleton, Switch, Tag } from 'antd'; | ||
import { createStyles } from 'antd-style'; | ||
import isEqual from 'fast-deep-equal'; | ||
import { LucideBlocks, LucideStore } from 'lucide-react'; | ||
import { memo, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { Flexbox } from 'react-layout-kit'; | ||
import DevModal from 'src/features/PluginDevModal'; | ||
|
||
import { FORM_STYLE } from '@/const/layoutTokens'; | ||
import { pluginHelpers, usePluginStore } from '@/store/plugin'; | ||
|
||
import { useStore } from '../store'; | ||
import LocalPluginItem from './LocalPluginItem'; | ||
|
||
const useStyles = createStyles(({ css }) => ({ | ||
avatar: css` | ||
.ant-skeleton-header { | ||
padding-right: 0; | ||
} | ||
`, | ||
label: css` | ||
li { | ||
height: 14px !important; | ||
} | ||
li + li { | ||
margin-block-start: 12px !important; | ||
} | ||
`, | ||
})); | ||
|
||
const MarketList = memo(() => { | ||
const { t } = useTranslation('setting'); | ||
const { styles } = useStyles(); | ||
|
||
const [showModal, setModal] = useState(false); | ||
|
||
// useEffect(() => { | ||
// setModal(true); | ||
// }, []); | ||
|
||
const updateConfig = useStore((s) => s.toggleAgentPlugin); | ||
const [plugins, hasPlugin] = useStore((s) => [s.config.plugins || [], !!s.config.plugins]); | ||
|
||
const [useFetchPluginList, fetchPluginManifest, saveToDevList, updateNewDevPlugin] = | ||
usePluginStore((s) => [ | ||
s.useFetchPluginList, | ||
s.fetchPluginManifest, | ||
s.saveToDevList, | ||
s.updateNewDevPlugin, | ||
]); | ||
const pluginManifestLoading = usePluginStore((s) => s.pluginManifestLoading, isEqual); | ||
const pluginList = usePluginStore((s) => s.pluginList, isEqual); | ||
const devPluginList = usePluginStore((s) => s.devPluginList, isEqual); | ||
|
||
useFetchPluginList(); | ||
|
||
const loadingItem = { | ||
avatar: ( | ||
<Skeleton | ||
active | ||
avatar={{ size: 40 }} | ||
className={styles.avatar} | ||
paragraph={false} | ||
title={false} | ||
/> | ||
), | ||
label: ( | ||
<Skeleton | ||
active | ||
avatar={false} | ||
paragraph={{ className: styles.label, style: { marginBottom: 0 } }} | ||
style={{ width: 300 }} | ||
title={false} | ||
/> | ||
), | ||
}; | ||
|
||
const loadingList = [loadingItem, loadingItem, loadingItem]; | ||
|
||
const isEmpty = pluginList.length === 0; | ||
|
||
const list = pluginList.map(({ identifier, meta }) => ({ | ||
avatar: <Avatar avatar={meta.avatar} />, | ||
children: ( | ||
<Switch | ||
checked={ | ||
// 如果在加载中,说明激活了 | ||
pluginManifestLoading[identifier] || !hasPlugin ? false : plugins.includes(identifier) | ||
} | ||
loading={pluginManifestLoading[identifier]} | ||
onChange={(checked) => { | ||
updateConfig(identifier); | ||
if (checked) { | ||
fetchPluginManifest(identifier); | ||
} | ||
}} | ||
/> | ||
), | ||
desc: pluginHelpers.getPluginDesc(meta), | ||
label: pluginHelpers.getPluginTitle(meta), | ||
minWidth: undefined, | ||
tag: identifier, | ||
})); | ||
|
||
const devList = devPluginList.map(({ identifier, meta }) => ({ | ||
avatar: <Avatar avatar={pluginHelpers.getPluginAvatar(meta) || '🧩'} />, | ||
children: <LocalPluginItem id={identifier} />, | ||
desc: pluginHelpers.getPluginDesc(meta), | ||
label: ( | ||
<Flexbox align={'center'} gap={8} horizontal> | ||
{pluginHelpers.getPluginTitle(meta)} | ||
<Tag bordered={false} color={'gold'}> | ||
{t('list.item.local.title', { ns: 'plugin' })} | ||
</Tag> | ||
</Flexbox> | ||
), | ||
minWidth: undefined, | ||
tag: identifier, | ||
})); | ||
|
||
return ( | ||
<> | ||
<DevModal | ||
onOpenChange={setModal} | ||
onSave={saveToDevList} | ||
onValueChange={updateNewDevPlugin} | ||
open={showModal} | ||
/> | ||
<Form | ||
items={[ | ||
{ | ||
children: isEmpty ? loadingList : [...devList, ...list], | ||
extra: ( | ||
<Tooltip title={t('settingPlugin.addTooltip')}> | ||
<Button | ||
icon={<Icon icon={LucideBlocks} />} | ||
onClick={(e) => { | ||
e.stopPropagation(); | ||
setModal(true); | ||
}} | ||
size={'small'} | ||
> | ||
{t('settingPlugin.add')} | ||
</Button> | ||
</Tooltip> | ||
), | ||
icon: LucideStore, | ||
title: t('settingPlugin.title'), | ||
}, | ||
]} | ||
{...FORM_STYLE} | ||
/>{' '} | ||
</> | ||
); | ||
}); | ||
|
||
export default MarketList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Form, ItemGroup, Markdown } from '@lobehub/ui'; | ||
import { createStyles } from 'antd-style'; | ||
import isEqual from 'fast-deep-equal'; | ||
import { ToyBrick } from 'lucide-react'; | ||
import { memo } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { FORM_STYLE } from '@/const/layoutTokens'; | ||
import { transformPluginSettings } from '@/features/PluginSettings'; | ||
import PluginSettingRender from '@/features/PluginSettings/PluginSettingRender'; | ||
import { pluginHelpers, pluginSelectors, usePluginStore } from '@/store/plugin'; | ||
|
||
import { useStore } from '../store'; | ||
|
||
const useStyles = createStyles(({ css, token }) => ({ | ||
md: css` | ||
p { | ||
color: ${token.colorTextDescription}; | ||
} | ||
`, | ||
})); | ||
|
||
const PluginSettings = memo(() => { | ||
const { t } = useTranslation('setting'); | ||
const { styles } = useStyles(); | ||
|
||
const [plugins] = useStore((s) => [s.config.plugins || [], !!s.config.plugins]); | ||
|
||
const [useFetchPluginList, updatePluginSettings] = usePluginStore((s) => [ | ||
s.useFetchPluginList, | ||
s.updatePluginSettings, | ||
]); | ||
const pluginList = usePluginStore(pluginSelectors.pluginList); | ||
|
||
const pluginManifestMap = usePluginStore((s) => s.pluginManifestMap, isEqual); | ||
const pluginsSettings = usePluginStore((s) => s.pluginsSettings, isEqual); | ||
useFetchPluginList(); | ||
|
||
const isEmpty = pluginList.length === 0; | ||
|
||
const pluginsConfig = isEmpty | ||
? [] | ||
: (plugins | ||
.map((identifier) => { | ||
const item = pluginList.find((i) => i.identifier === identifier); | ||
|
||
if (!item) return null; | ||
const manifest = pluginManifestMap[identifier]; | ||
|
||
if (!manifest?.settings) return null; | ||
|
||
return { | ||
children: transformPluginSettings(manifest.settings).map((item) => ({ | ||
...item, | ||
children: ( | ||
<PluginSettingRender | ||
defaultValue={pluginsSettings[identifier]?.[item.name]} | ||
format={item.format} | ||
onChange={(value) => { | ||
updatePluginSettings(identifier, { [item.name]: value }); | ||
}} | ||
type={item.type as any} | ||
/> | ||
), | ||
desc: item.desc && <Markdown className={styles.md}>{item.desc}</Markdown>, | ||
})), | ||
icon: ToyBrick, | ||
title: t('settingPlugin.config', { id: pluginHelpers.getPluginTitle(item.meta) }), | ||
}; | ||
}) | ||
.filter(Boolean) as unknown as ItemGroup[]); | ||
|
||
return <Form items={pluginsConfig} {...FORM_STYLE} />; | ||
}); | ||
|
||
export default PluginSettings; |
Oops, something went wrong.