chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
+130
@@ -0,0 +1,130 @@
|
||||
import type { consoleClient } from '@/service/console'
|
||||
import { screen, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { useState } from 'react'
|
||||
import { PluginCategoryEnum } from '@/app/components/plugins/types'
|
||||
import { MARKETPLACE_API_PREFIX } from '@/features/marketplace/api'
|
||||
import { renderWithConsoleQuery as render } from '@/test/console/query-data'
|
||||
import { createInstalledPluginFixture } from '@/test/fixtures/plugin'
|
||||
import PluginsPicker from '../plugins-picker'
|
||||
import { AUTO_UPDATE_MODE } from '../types'
|
||||
|
||||
const plugin = (id: string, label: string, category: PluginCategoryEnum) => {
|
||||
const entity = createInstalledPluginFixture({ id, plugin_id: id })
|
||||
return {
|
||||
...entity,
|
||||
plugin_id: id,
|
||||
declaration: { ...entity.declaration, category, label: { en_US: label }, tags: [] },
|
||||
}
|
||||
}
|
||||
const plugins = [
|
||||
plugin('dify/model-plugin', 'Model plugin', PluginCategoryEnum.model),
|
||||
plugin('dify/tool-plugin', 'Tool plugin', PluginCategoryEnum.tool),
|
||||
plugin('dify/new-tool-plugin', 'New tool plugin', PluginCategoryEnum.tool),
|
||||
plugin('dify/datasource-plugin', 'Datasource plugin', PluginCategoryEnum.datasource),
|
||||
]
|
||||
|
||||
const installedPlugins = vi.hoisted(() =>
|
||||
vi.fn<typeof consoleClient.workspaces.current.plugin.list.get>(),
|
||||
)
|
||||
|
||||
vi.mock('@/service/console', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@/service/console')>()
|
||||
const { withConsoleResponses } = await import('@/test/console/service-responses')
|
||||
return {
|
||||
...actual,
|
||||
consoleQuery: withConsoleResponses(actual.consoleQuery, {
|
||||
'workspaces.current.plugin.list.get': installedPlugins,
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
installedPlugins.mockReset().mockResolvedValue({ plugins, total: plugins.length })
|
||||
})
|
||||
|
||||
it('opens the installed catalog and updates the selection when a plugin is checked and unchecked', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onChange = vi.fn()
|
||||
function Picker() {
|
||||
const [value, setValue] = useState<string[]>([])
|
||||
return (
|
||||
<PluginsPicker
|
||||
updateMode={AUTO_UPDATE_MODE.partial}
|
||||
value={value}
|
||||
onChange={(next) => {
|
||||
setValue(next)
|
||||
onChange(next)
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
render(<Picker />)
|
||||
|
||||
expect(screen.getByText('plugin.autoUpdate.upgradeModePlaceholder.partial')).toBeInTheDocument()
|
||||
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
|
||||
await user.click(screen.getByRole('button', { name: 'plugin.autoUpdate.operation.select' }))
|
||||
const dialog = await screen.findByRole('dialog')
|
||||
const checkbox = await within(dialog).findByRole('checkbox', { name: 'Tool plugin' })
|
||||
expect(
|
||||
within(dialog)
|
||||
.getAllByRole('presentation')
|
||||
.map((image) => image.getAttribute('src')),
|
||||
).toContain(`${MARKETPLACE_API_PREFIX}/plugins/dify/tool-plugin/icon`)
|
||||
await user.click(checkbox)
|
||||
expect(checkbox).toBeChecked()
|
||||
expect(onChange).toHaveBeenCalledExactlyOnceWith(['dify/tool-plugin'])
|
||||
await user.click(checkbox)
|
||||
expect(checkbox).not.toBeChecked()
|
||||
expect(onChange).toHaveBeenLastCalledWith([])
|
||||
})
|
||||
|
||||
it('summarizes excluded plugins and clears all selections without a category scope', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onChange = vi.fn()
|
||||
render(
|
||||
<PluginsPicker
|
||||
updateMode={AUTO_UPDATE_MODE.exclude}
|
||||
value={['dify/model-plugin', 'dify/tool-plugin']}
|
||||
onChange={onChange}
|
||||
/>,
|
||||
)
|
||||
|
||||
expect(
|
||||
screen.getByText('plugin.autoUpdate.excludeUpdate:{"count":2,"num":2}'),
|
||||
).toBeInTheDocument()
|
||||
await user.click(screen.getByRole('button', { name: 'plugin.autoUpdate.operation.clearAll' }))
|
||||
expect(onChange).toHaveBeenCalledExactlyOnceWith([])
|
||||
})
|
||||
|
||||
it('edits only the current category and preserves hidden selections', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onChange = vi.fn()
|
||||
render(
|
||||
<PluginsPicker
|
||||
updateMode={AUTO_UPDATE_MODE.exclude}
|
||||
value={['dify/model-plugin', 'dify/tool-plugin', 'dify/datasource-plugin']}
|
||||
onChange={onChange}
|
||||
integrationCategory={PluginCategoryEnum.tool}
|
||||
/>,
|
||||
)
|
||||
|
||||
expect(
|
||||
await screen.findByText('plugin.autoUpdate.excludeUpdate:{"count":1,"num":1}'),
|
||||
).toBeInTheDocument()
|
||||
await user.click(screen.getByRole('button', { name: 'plugin.autoUpdate.operation.select' }))
|
||||
const dialog = within(await screen.findByRole('dialog'))
|
||||
expect(dialog.getByRole('checkbox', { name: 'Tool plugin' })).toBeChecked()
|
||||
expect(dialog.queryByRole('checkbox', { name: 'Model plugin' })).not.toBeInTheDocument()
|
||||
expect(dialog.queryByRole('checkbox', { name: 'Datasource plugin' })).not.toBeInTheDocument()
|
||||
await user.click(dialog.getByRole('checkbox', { name: 'New tool plugin' }))
|
||||
expect(onChange).toHaveBeenCalledWith([
|
||||
'dify/model-plugin',
|
||||
'dify/datasource-plugin',
|
||||
'dify/tool-plugin',
|
||||
'dify/new-tool-plugin',
|
||||
])
|
||||
await user.keyboard('{Escape}')
|
||||
await user.click(screen.getByRole('button', { name: 'plugin.autoUpdate.operation.clearAll' }))
|
||||
expect(onChange).toHaveBeenLastCalledWith(['dify/model-plugin', 'dify/datasource-plugin'])
|
||||
})
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
import type { consoleClient } from '@/service/console'
|
||||
import { screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vite-plus/test'
|
||||
import { PluginCategoryEnum, PluginSource } from '@/app/components/plugins/types'
|
||||
import { renderWithConsoleQuery as render } from '@/test/console/query-data'
|
||||
import { createInstalledPluginFixture } from '@/test/fixtures/plugin'
|
||||
import ToolPicker from '../tool-picker'
|
||||
|
||||
const installedPlugins = vi.hoisted(() =>
|
||||
vi.fn<typeof consoleClient.workspaces.current.plugin.list.get>(),
|
||||
)
|
||||
|
||||
vi.mock('@/service/console', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@/service/console')>()
|
||||
const { withConsoleResponses } = await import('@/test/console/service-responses')
|
||||
return {
|
||||
...actual,
|
||||
consoleQuery: withConsoleResponses(actual.consoleQuery, {
|
||||
'workspaces.current.plugin.list.get': installedPlugins,
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
||||
const createPlugin = (
|
||||
pluginId: string,
|
||||
source: PluginSource,
|
||||
category: PluginCategoryEnum,
|
||||
tags: string[],
|
||||
) => {
|
||||
const plugin = createInstalledPluginFixture({ id: pluginId, plugin_id: pluginId })
|
||||
return {
|
||||
...plugin,
|
||||
plugin_id: pluginId,
|
||||
source,
|
||||
declaration: {
|
||||
...plugin.declaration,
|
||||
category,
|
||||
tags,
|
||||
label: { ...plugin.declaration.label, en_US: pluginId },
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
describe('ToolPicker', () => {
|
||||
beforeEach(() => {
|
||||
installedPlugins.mockReset().mockResolvedValue({ plugins: [], total: 0 })
|
||||
})
|
||||
|
||||
it('shows the empty installed-plugin state', async () => {
|
||||
render(
|
||||
<ToolPicker
|
||||
trigger={<button>trigger</button>}
|
||||
value={[]}
|
||||
onChange={vi.fn()}
|
||||
isShow
|
||||
onShowChange={vi.fn()}
|
||||
/>,
|
||||
)
|
||||
expect(
|
||||
await screen.findByText('plugin.autoUpdate.noPluginPlaceholder.noInstalled'),
|
||||
).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('filters by plugin type, tags, and query', async () => {
|
||||
const user = userEvent.setup()
|
||||
installedPlugins.mockResolvedValue({
|
||||
plugins: [
|
||||
createPlugin('tool-search', PluginSource.marketplace, PluginCategoryEnum.tool, ['search']),
|
||||
createPlugin('tool-rag', PluginSource.marketplace, PluginCategoryEnum.tool, ['rag']),
|
||||
createPlugin('model-agent', PluginSource.marketplace, PluginCategoryEnum.model, ['agent']),
|
||||
createPlugin('github-tool', PluginSource.github, PluginCategoryEnum.tool, ['rag']),
|
||||
],
|
||||
total: 4,
|
||||
})
|
||||
|
||||
render(
|
||||
<ToolPicker
|
||||
trigger={<button type="button">trigger</button>}
|
||||
value={[]}
|
||||
onChange={vi.fn()}
|
||||
isShow
|
||||
onShowChange={vi.fn()}
|
||||
/>,
|
||||
)
|
||||
|
||||
expect(
|
||||
await screen.findAllByRole('checkbox', { name: /^(tool-|model-|datasource-)/ }),
|
||||
).toHaveLength(3)
|
||||
expect(screen.queryByText('github-tool')).not.toBeInTheDocument()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'plugin.category.models' }))
|
||||
expect(screen.getByRole('button', { name: 'plugin.category.models' })).toHaveAttribute(
|
||||
'aria-pressed',
|
||||
'true',
|
||||
)
|
||||
expect(screen.getAllByRole('checkbox', { name: /^(tool-|model-|datasource-)/ })).toHaveLength(1)
|
||||
expect(screen.getByText('model-agent')).toBeInTheDocument()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'plugin.category.tools' }))
|
||||
expect(screen.getAllByRole('checkbox', { name: /^(tool-|model-|datasource-)/ })).toHaveLength(2)
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'pluginTags.allTags' }))
|
||||
await user.click(screen.getByRole('checkbox', { name: 'pluginTags.tags.rag' }))
|
||||
expect(screen.getAllByRole('checkbox', { name: /^(tool-|model-|datasource-)/ })).toHaveLength(1)
|
||||
expect(screen.getByText('tool-rag')).toBeInTheDocument()
|
||||
await user.keyboard('{Escape}')
|
||||
|
||||
const search = screen.getByRole('searchbox', { name: 'plugin.searchTools' })
|
||||
await user.type(search, 'tool-rag')
|
||||
expect(screen.getAllByRole('checkbox', { name: /^(tool-|model-|datasource-)/ })).toHaveLength(1)
|
||||
expect(search).toHaveValue('tool-rag')
|
||||
await user.clear(search)
|
||||
await user.type(search, 'not-installed')
|
||||
expect(screen.queryByRole('checkbox', { name: 'tool-rag' })).not.toBeInTheDocument()
|
||||
expect(screen.getByText('plugin.autoUpdate.noPluginPlaceholder.noFound')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('limits selectable integrations to the provided integration category', async () => {
|
||||
installedPlugins.mockResolvedValue({
|
||||
plugins: [
|
||||
createPlugin('model-openai', PluginSource.marketplace, PluginCategoryEnum.model, ['llm']),
|
||||
createPlugin('tool-rag', PluginSource.marketplace, PluginCategoryEnum.tool, ['rag']),
|
||||
createPlugin('datasource-notion', PluginSource.marketplace, PluginCategoryEnum.datasource, [
|
||||
'docs',
|
||||
]),
|
||||
],
|
||||
total: 3,
|
||||
})
|
||||
|
||||
render(
|
||||
<ToolPicker
|
||||
trigger={<button type="button">trigger</button>}
|
||||
value={[]}
|
||||
onChange={vi.fn()}
|
||||
isShow
|
||||
onShowChange={vi.fn()}
|
||||
integrationCategory={PluginCategoryEnum.model}
|
||||
/>,
|
||||
)
|
||||
|
||||
expect(await screen.findByRole('checkbox', { name: 'model-openai' })).toBeInTheDocument()
|
||||
expect(screen.getByRole('button', { name: 'plugin.category.models' })).toHaveAttribute(
|
||||
'aria-pressed',
|
||||
'true',
|
||||
)
|
||||
expect(screen.queryByRole('button', { name: 'plugin.category.all' })).not.toBeInTheDocument()
|
||||
expect(screen.queryByRole('button', { name: 'plugin.category.tools' })).not.toBeInTheDocument()
|
||||
expect(screen.getAllByRole('checkbox', { name: /^(tool-|model-|datasource-)/ })).toHaveLength(1)
|
||||
expect(screen.getByText('model-openai')).toBeInTheDocument()
|
||||
expect(screen.queryByText('tool-rag')).not.toBeInTheDocument()
|
||||
expect(screen.queryByText('datasource-notion')).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
import { convertLocalSecondsToUTCDaySeconds, convertUTCDaySecondsToLocalSeconds } from '../utils'
|
||||
|
||||
describe('auto-update daily clock conversion', () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
vi.setSystemTime(new Date('2026-01-15T12:00:00Z'))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('should convert local seconds to UTC day seconds correctly', () => {
|
||||
const localTimezone = 'Asia/Shanghai'
|
||||
const utcSeconds = convertLocalSecondsToUTCDaySeconds(0, localTimezone)
|
||||
expect(utcSeconds).toBe((24 - 8) * 3600)
|
||||
})
|
||||
|
||||
it('should convert local seconds to UTC day seconds for a specific time', () => {
|
||||
const localTimezone = 'Asia/Shanghai'
|
||||
expect(
|
||||
convertUTCDaySecondsToLocalSeconds(
|
||||
convertLocalSecondsToUTCDaySeconds(0, localTimezone),
|
||||
localTimezone,
|
||||
),
|
||||
).toBe(0)
|
||||
})
|
||||
|
||||
it.each([
|
||||
['2026-11-02T12:00:00Z', 16200],
|
||||
['2026-03-09T12:00:00Z', 12600],
|
||||
])('displays 23:30 across the DST transition preceding %s', (now, utcSeconds) => {
|
||||
vi.setSystemTime(new Date(now))
|
||||
expect(convertUTCDaySecondsToLocalSeconds(utcSeconds, 'America/New_York')).toBe(84600)
|
||||
})
|
||||
|
||||
it.each([
|
||||
['2026-11-01T12:00:00Z', 70200],
|
||||
['2026-03-08T12:00:00Z', 66600],
|
||||
])('saves and restores a 14:30 wall time on the DST transition at %s', (now, utcSeconds) => {
|
||||
vi.setSystemTime(new Date(now))
|
||||
const saved = convertLocalSecondsToUTCDaySeconds(52200, 'America/New_York')
|
||||
expect(saved).toBe(utcSeconds)
|
||||
expect(convertUTCDaySecondsToLocalSeconds(saved, 'America/New_York')).toBe(52200)
|
||||
})
|
||||
|
||||
it.each([
|
||||
['2026-11-01T12:00:00Z', 16200],
|
||||
['2026-03-08T12:00:00Z', 12600],
|
||||
])('preserves 23:30 when saving crosses the UTC date boundary at %s', (now, utcSeconds) => {
|
||||
vi.setSystemTime(new Date(now))
|
||||
const saved = convertLocalSecondsToUTCDaySeconds(84600, 'America/New_York')
|
||||
expect(saved).toBe(utcSeconds)
|
||||
expect(convertUTCDaySecondsToLocalSeconds(saved, 'America/New_York')).toBe(84600)
|
||||
})
|
||||
})
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import { cn } from '@langgenius/dify-ui/cn'
|
||||
import * as React from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
type Props = Readonly<{
|
||||
className: string
|
||||
noPlugins?: boolean
|
||||
}>
|
||||
|
||||
const NoDataPlaceholder: FC<Props> = ({ className, noPlugins }) => {
|
||||
const { t } = useTranslation(['plugin'])
|
||||
const icon = noPlugins ? (
|
||||
<span aria-hidden className="i-custom-vender-other-group size-6 text-text-quaternary" />
|
||||
) : (
|
||||
<span
|
||||
aria-hidden
|
||||
className="i-custom-vender-line-general-search-menu size-8 text-text-tertiary"
|
||||
/>
|
||||
)
|
||||
const text = t(
|
||||
($) => $[`autoUpdate.noPluginPlaceholder.${noPlugins ? 'noInstalled' : 'noFound'}`],
|
||||
{ ns: 'plugin' },
|
||||
)
|
||||
return (
|
||||
<div className={cn('flex items-center justify-center', className)}>
|
||||
<div className="flex flex-col items-center">
|
||||
{icon}
|
||||
<div className="mt-2 system-sm-regular text-text-tertiary">{text}</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(NoDataPlaceholder)
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import * as React from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { AUTO_UPDATE_MODE } from './types'
|
||||
|
||||
type Props = Readonly<{
|
||||
updateMode: AUTO_UPDATE_MODE
|
||||
}>
|
||||
|
||||
const NoPluginSelected: FC<Props> = ({ updateMode }) => {
|
||||
const { t } = useTranslation(['plugin'])
|
||||
const text = `${t(($) => $[`autoUpdate.upgradeModePlaceholder.${updateMode === AUTO_UPDATE_MODE.partial ? 'partial' : 'exclude'}`], { ns: 'plugin' })}`
|
||||
return <div className="text-center system-xs-regular text-text-tertiary">{text}</div>
|
||||
}
|
||||
export default React.memo(NoPluginSelected)
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import type { PluginCategoryEnum } from '../../types'
|
||||
import { Button } from '@langgenius/dify-ui/button'
|
||||
import { RiAddLine } from '@remixicon/react'
|
||||
import { useBoolean } from 'ahooks'
|
||||
import * as React from 'react'
|
||||
import { useCallback, useMemo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useInstalledPluginList } from '@/service/use-plugins'
|
||||
import NoPluginSelected from './no-plugin-selected'
|
||||
import PluginsSelected from './plugins-selected'
|
||||
import ToolPicker from './tool-picker'
|
||||
import { AUTO_UPDATE_MODE } from './types'
|
||||
|
||||
const i18nPrefix = 'autoUpdate'
|
||||
|
||||
type Props = Readonly<{
|
||||
updateMode: AUTO_UPDATE_MODE
|
||||
value: string[] // plugin ids
|
||||
onChange: (value: string[]) => void
|
||||
integrationCategory?: PluginCategoryEnum
|
||||
}>
|
||||
|
||||
const PluginsPicker: FC<Props> = ({ updateMode, value, onChange, integrationCategory }) => {
|
||||
const { t } = useTranslation(['plugin'])
|
||||
const { data } = useInstalledPluginList()
|
||||
const pluginCategoryById = useMemo(() => {
|
||||
return new Map(data?.plugins.map((plugin) => [plugin.plugin_id, plugin.declaration.category]))
|
||||
}, [data?.plugins])
|
||||
const isCurrentCategoryPlugin = useCallback(
|
||||
(pluginId: string) => {
|
||||
if (!integrationCategory) return true
|
||||
|
||||
return pluginCategoryById.get(pluginId) === integrationCategory
|
||||
},
|
||||
[integrationCategory, pluginCategoryById],
|
||||
)
|
||||
const visiblePlugins = useMemo(() => {
|
||||
return value.filter(isCurrentCategoryPlugin)
|
||||
}, [isCurrentCategoryPlugin, value])
|
||||
const hiddenPlugins = useMemo(() => {
|
||||
if (!integrationCategory) return []
|
||||
|
||||
return value.filter((plugin) => !isCurrentCategoryPlugin(plugin))
|
||||
}, [integrationCategory, isCurrentCategoryPlugin, value])
|
||||
const hasSelected = visiblePlugins.length > 0
|
||||
const isExcludeMode = updateMode === AUTO_UPDATE_MODE.exclude
|
||||
const handleClear = () => {
|
||||
onChange(hiddenPlugins)
|
||||
}
|
||||
const handleVisiblePluginsChange = useCallback(
|
||||
(newVisiblePlugins: string[]) => {
|
||||
onChange(
|
||||
integrationCategory
|
||||
? [
|
||||
...hiddenPlugins,
|
||||
...newVisiblePlugins.filter((plugin) => !hiddenPlugins.includes(plugin)),
|
||||
]
|
||||
: newVisiblePlugins,
|
||||
)
|
||||
},
|
||||
[hiddenPlugins, integrationCategory, onChange],
|
||||
)
|
||||
|
||||
const [isShowToolPicker, { set: setToolPicker }] = useBoolean(false)
|
||||
return (
|
||||
<div className="mt-2 flex w-full flex-col gap-2 rounded-[10px] bg-background-section-burn p-2.5">
|
||||
{hasSelected ? (
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="min-w-0 flex-1 system-xs-medium text-text-tertiary">
|
||||
{t(($) => $[`${i18nPrefix}.${isExcludeMode ? 'excludeUpdate' : 'partialUPdate'}`], {
|
||||
ns: 'plugin',
|
||||
count: visiblePlugins.length,
|
||||
num: visiblePlugins.length,
|
||||
})}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="shrink-0 cursor-pointer border-none bg-transparent p-0 text-left system-xs-medium text-text-tertiary hover:text-text-secondary focus-visible:ring-1 focus-visible:ring-components-input-border-active focus-visible:outline-hidden"
|
||||
onClick={handleClear}
|
||||
>
|
||||
{t(($) => $[`${i18nPrefix}.operation.clearAll`], { ns: 'plugin' })}
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<NoPluginSelected updateMode={updateMode} />
|
||||
)}
|
||||
|
||||
{hasSelected && <PluginsSelected className="h-6 w-full gap-1" plugins={visiblePlugins} />}
|
||||
|
||||
<ToolPicker
|
||||
trigger={
|
||||
<Button className="h-6 w-full" size="small" variant="secondary-accent">
|
||||
<RiAddLine className="size-3.5" />
|
||||
{t(($) => $[`${i18nPrefix}.operation.select`], { ns: 'plugin' })}
|
||||
</Button>
|
||||
}
|
||||
value={visiblePlugins}
|
||||
onChange={handleVisiblePluginsChange}
|
||||
isShow={isShowToolPicker}
|
||||
onShowChange={setToolPicker}
|
||||
integrationCategory={integrationCategory}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
export default React.memo(PluginsPicker)
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import { cn } from '@langgenius/dify-ui/cn'
|
||||
import * as React from 'react'
|
||||
import Icon from '@/app/components/plugins/card/base/card-icon'
|
||||
import { getMarketplacePluginIconUrl } from '@/features/marketplace/api'
|
||||
|
||||
const MAX_DISPLAY_COUNT = 14
|
||||
type Props = Readonly<{
|
||||
className?: string
|
||||
plugins: string[]
|
||||
}>
|
||||
|
||||
const PluginsSelected: FC<Props> = ({ className, plugins }) => {
|
||||
const displayPlugins = plugins.slice(0, MAX_DISPLAY_COUNT)
|
||||
const hiddenPluginsCount = plugins.length - displayPlugins.length
|
||||
return (
|
||||
<div className={cn('flex min-w-0 items-center overflow-hidden', className)}>
|
||||
{displayPlugins.map((plugin) => (
|
||||
<Icon key={plugin} size="tiny" src={getMarketplacePluginIconUrl(plugin)} />
|
||||
))}
|
||||
{hiddenPluginsCount > 0 && (
|
||||
<div className="shrink-0 system-xs-medium text-text-tertiary">+{hiddenPluginsCount}</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
export default React.memo(PluginsSelected)
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import type { PluginDetail } from '@/app/components/plugins/types'
|
||||
import { Checkbox } from '@langgenius/dify-ui/checkbox'
|
||||
import * as React from 'react'
|
||||
import Icon from '@/app/components/plugins/card/base/card-icon'
|
||||
import { useGetLanguage } from '@/context/i18n'
|
||||
import { getMarketplacePluginIconUrl } from '@/features/marketplace/api'
|
||||
import { renderI18nObject } from '@/i18n/metadata'
|
||||
|
||||
type Props = Readonly<{
|
||||
payload: PluginDetail
|
||||
isChecked?: boolean
|
||||
onCheckChange: () => void
|
||||
}>
|
||||
|
||||
const ToolItem: FC<Props> = ({ payload, isChecked, onCheckChange }) => {
|
||||
const language = useGetLanguage()
|
||||
|
||||
const { plugin_id, declaration } = payload
|
||||
const { label, author: org } = declaration
|
||||
return (
|
||||
<div className="flex w-full items-center gap-1 rounded-lg py-1 pr-2 pl-3 select-none hover:bg-state-base-hover">
|
||||
<div className="flex min-w-0 grow items-center gap-2 pr-2">
|
||||
<Icon size="tiny" src={getMarketplacePluginIconUrl(plugin_id)} />
|
||||
<div className="min-w-0 truncate system-sm-medium text-text-secondary">
|
||||
{renderI18nObject(label, language)}
|
||||
</div>
|
||||
<div className="min-w-0 truncate system-xs-regular text-text-quaternary">{org}</div>
|
||||
</div>
|
||||
<Checkbox
|
||||
checked={isChecked}
|
||||
onCheckedChange={() => onCheckChange()}
|
||||
className="shrink-0"
|
||||
aria-label={renderI18nObject(label, language)}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
export default React.memo(ToolItem)
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
'use client'
|
||||
import type { FC } from 'react'
|
||||
import type { ActivePluginType } from '../../marketplace/constants'
|
||||
import type { PluginCategoryEnum } from '../../types'
|
||||
import { cn } from '@langgenius/dify-ui/cn'
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '@langgenius/dify-ui/popover'
|
||||
import * as React from 'react'
|
||||
import { useMemo, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { LoadingPlaceholder } from '@/app/components/base/loading-placeholder'
|
||||
import { PluginSearchInput } from '@/app/components/plugins/plugin-search-input'
|
||||
import { useInstalledPluginList } from '@/service/use-plugins'
|
||||
import { PLUGIN_TYPE_SEARCH_MAP } from '../../marketplace/constants'
|
||||
import { PluginSource } from '../../types'
|
||||
import NoDataPlaceholder from './no-data-placeholder'
|
||||
import ToolItem from './tool-item'
|
||||
|
||||
type Props = Readonly<{
|
||||
trigger: React.ReactNode
|
||||
value: string[]
|
||||
onChange: (value: string[]) => void
|
||||
isShow: boolean
|
||||
onShowChange: (isShow: boolean) => void
|
||||
integrationCategory?: PluginCategoryEnum
|
||||
}>
|
||||
|
||||
const ToolPicker: FC<Props> = ({
|
||||
trigger,
|
||||
value,
|
||||
onChange,
|
||||
isShow,
|
||||
onShowChange,
|
||||
integrationCategory,
|
||||
}) => {
|
||||
const { t } = useTranslation(['plugin'])
|
||||
|
||||
const allTabs = [
|
||||
{ key: PLUGIN_TYPE_SEARCH_MAP.all, name: t(($) => $['category.all'], { ns: 'plugin' }) },
|
||||
{ key: PLUGIN_TYPE_SEARCH_MAP.model, name: t(($) => $['category.models'], { ns: 'plugin' }) },
|
||||
{ key: PLUGIN_TYPE_SEARCH_MAP.tool, name: t(($) => $['category.tools'], { ns: 'plugin' }) },
|
||||
{ key: PLUGIN_TYPE_SEARCH_MAP.agent, name: t(($) => $['category.agents'], { ns: 'plugin' }) },
|
||||
{
|
||||
key: PLUGIN_TYPE_SEARCH_MAP.extension,
|
||||
name: t(($) => $['category.extensions'], { ns: 'plugin' }),
|
||||
},
|
||||
{
|
||||
key: PLUGIN_TYPE_SEARCH_MAP.datasource,
|
||||
name: t(($) => $['category.datasources'], { ns: 'plugin' }),
|
||||
},
|
||||
{
|
||||
key: PLUGIN_TYPE_SEARCH_MAP.trigger,
|
||||
name: t(($) => $['category.triggers'], { ns: 'plugin' }),
|
||||
},
|
||||
{ key: PLUGIN_TYPE_SEARCH_MAP.bundle, name: t(($) => $['category.bundles'], { ns: 'plugin' }) },
|
||||
]
|
||||
const tabs = integrationCategory
|
||||
? allTabs.filter((tab) => tab.key === integrationCategory)
|
||||
: allTabs
|
||||
|
||||
const [pluginType, setPluginType] = useState<ActivePluginType>(PLUGIN_TYPE_SEARCH_MAP.all)
|
||||
const effectivePluginType = integrationCategory ?? pluginType
|
||||
const [query, setQuery] = useState('')
|
||||
const [tags, setTags] = useState<string[]>([])
|
||||
const { data, isLoading } = useInstalledPluginList()
|
||||
const filteredList = useMemo(() => {
|
||||
const list = data ? data.plugins : []
|
||||
return list.filter((plugin) => {
|
||||
const isFromMarketPlace = plugin.source === PluginSource.marketplace
|
||||
return (
|
||||
isFromMarketPlace &&
|
||||
(effectivePluginType === PLUGIN_TYPE_SEARCH_MAP.all ||
|
||||
plugin.declaration.category === effectivePluginType) &&
|
||||
(tags.length === 0 || tags.some((tag) => plugin.declaration.tags.includes(tag))) &&
|
||||
(query === '' || plugin.plugin_id.toLowerCase().includes(query.toLowerCase()))
|
||||
)
|
||||
})
|
||||
}, [data, effectivePluginType, query, tags])
|
||||
|
||||
const handleCheckChange = (pluginId: string) => {
|
||||
const newValue = value.includes(pluginId)
|
||||
? value.filter((id) => id !== pluginId)
|
||||
: [...value, pluginId]
|
||||
onChange(newValue)
|
||||
}
|
||||
|
||||
const listContent = (
|
||||
<div className="max-h-99 overflow-y-auto p-1">
|
||||
{filteredList.map((item) => (
|
||||
<ToolItem
|
||||
key={item.plugin_id}
|
||||
payload={item}
|
||||
isChecked={value.includes(item.plugin_id)}
|
||||
onCheckChange={() => handleCheckChange(item.plugin_id)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
|
||||
const loadingContent = (
|
||||
<div className="flex h-99 items-center justify-center">
|
||||
<LoadingPlaceholder />
|
||||
</div>
|
||||
)
|
||||
|
||||
const noData = <NoDataPlaceholder className="h-99" noPlugins={!query} />
|
||||
|
||||
const resolvedTrigger = React.isValidElement(trigger) ? trigger : <div>{trigger}</div>
|
||||
|
||||
return (
|
||||
<Popover open={isShow} onOpenChange={onShowChange}>
|
||||
<PopoverTrigger render={resolvedTrigger} />
|
||||
<PopoverContent
|
||||
side="top"
|
||||
sideOffset={4}
|
||||
className="border-none bg-transparent p-0 shadow-none backdrop-blur-none"
|
||||
>
|
||||
<div className="relative min-h-20 w-108 max-w-[calc(100vw-32px)] overflow-hidden rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg shadow-lg backdrop-blur-xs">
|
||||
<div className="flex flex-col overflow-hidden rounded-t-lg border-b border-divider-subtle bg-background-section-burn">
|
||||
<div className="bg-components-panel-bg p-2">
|
||||
<PluginSearchInput
|
||||
value={query}
|
||||
onValueChange={setQuery}
|
||||
tags={tags}
|
||||
onTagsChange={setTags}
|
||||
placeholder={t(($) => $.searchTools, { ns: 'plugin' })!}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex items-center justify-between bg-components-panel-bg px-3 pb-2">
|
||||
<div className="flex min-w-0 items-center gap-0.5 overflow-x-auto">
|
||||
{tabs.map((tab) => (
|
||||
<button
|
||||
type="button"
|
||||
aria-pressed={effectivePluginType === tab.key}
|
||||
className={cn(
|
||||
'flex h-6 shrink-0 cursor-pointer items-center rounded-md px-2 system-xs-medium text-text-tertiary hover:bg-state-base-hover focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-state-accent-solid',
|
||||
effectivePluginType === tab.key &&
|
||||
'bg-state-base-hover-alt system-xs-semibold text-text-primary',
|
||||
)}
|
||||
key={tab.key}
|
||||
onClick={() => setPluginType(tab.key)}
|
||||
>
|
||||
{tab.name}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{!isLoading && filteredList.length > 0 && listContent}
|
||||
{!isLoading && filteredList.length === 0 && noData}
|
||||
{isLoading && loadingContent}
|
||||
</div>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(ToolPicker)
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
export const AUTO_UPDATE_STRATEGY = {
|
||||
fixOnly: 'fix_only',
|
||||
disabled: 'disabled',
|
||||
latest: 'latest',
|
||||
} as const
|
||||
|
||||
export type AUTO_UPDATE_STRATEGY = (typeof AUTO_UPDATE_STRATEGY)[keyof typeof AUTO_UPDATE_STRATEGY]
|
||||
|
||||
export const AUTO_UPDATE_MODE = {
|
||||
partial: 'partial',
|
||||
exclude: 'exclude',
|
||||
update_all: 'all',
|
||||
} as const
|
||||
|
||||
export type AUTO_UPDATE_MODE = (typeof AUTO_UPDATE_MODE)[keyof typeof AUTO_UPDATE_MODE]
|
||||
|
||||
export type AutoUpdateConfig = {
|
||||
strategy_setting: AUTO_UPDATE_STRATEGY
|
||||
upgrade_time_of_day: number
|
||||
upgrade_mode: AUTO_UPDATE_MODE
|
||||
exclude_plugins: string[]
|
||||
include_plugins: string[]
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
import type { Dayjs } from 'dayjs'
|
||||
import dayjs from 'dayjs'
|
||||
import timezone from 'dayjs/plugin/timezone'
|
||||
import utc from 'dayjs/plugin/utc'
|
||||
|
||||
dayjs.extend(utc)
|
||||
dayjs.extend(timezone)
|
||||
|
||||
export const timeOfDayToDayjs = (timeOfDay: number): Dayjs => {
|
||||
const hours = Math.floor(timeOfDay / 3600)
|
||||
const minutes = (timeOfDay - hours * 3600) / 60
|
||||
const res = dayjs().startOf('day').hour(hours).minute(minutes)
|
||||
return res
|
||||
}
|
||||
|
||||
export const convertLocalSecondsToUTCDaySeconds = (
|
||||
secondsInDay: number,
|
||||
localTimezone: string,
|
||||
): number => {
|
||||
// The API stores a daily UTC clock, not a date. Use the same current offset in both directions.
|
||||
const offset = dayjs().tz(localTimezone).utcOffset()
|
||||
const utcTargetTime = dayjs
|
||||
.utc()
|
||||
.startOf('day')
|
||||
.add(secondsInDay - offset * 60, 'second')
|
||||
return utcTargetTime.hour() * 3600 + utcTargetTime.minute() * 60 + utcTargetTime.second()
|
||||
}
|
||||
|
||||
export const convertUTCDaySecondsToLocalSeconds = (
|
||||
utcDaySeconds: number,
|
||||
localTimezone: string,
|
||||
): number => {
|
||||
const utcDayStart = dayjs().utc().startOf('day')
|
||||
const utcTargetTime = utcDayStart.add(utcDaySeconds, 'second')
|
||||
const localTargetTime = utcTargetTime.utcOffset(dayjs().tz(localTimezone).utcOffset())
|
||||
return localTargetTime.hour() * 3600 + localTargetTime.minute() * 60 + localTargetTime.second()
|
||||
}
|
||||
Reference in new issue
Block a user