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
@@ -0,0 +1,81 @@
|
||||
import type { CheckDependenciesResult } from '@dify/contracts/api/console/apps/types.gen'
|
||||
import { act } from '@testing-library/react'
|
||||
import { createHttpError } from '@/service/http-error'
|
||||
import { renderHookWithConsoleQuery } from '@/test/console/query-data'
|
||||
import { usePluginDependencies } from '../hooks'
|
||||
import { useStore } from '../store'
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
request: vi.fn<(...args: unknown[]) => Promise<Response>>(),
|
||||
error: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/service/base', () => ({ request: mocks.request }))
|
||||
vi.mock('@/app/notifications', () => ({ toast: { error: mocks.error } }))
|
||||
|
||||
describe('usePluginDependencies', () => {
|
||||
beforeEach(() => {
|
||||
mocks.request.mockReset()
|
||||
useStore.getState().setDependencies([])
|
||||
})
|
||||
|
||||
it.each([
|
||||
{ resource: 'app', isPipeline: false, path: '/apps/imports/resource-1/check-dependencies' },
|
||||
{
|
||||
resource: 'pipeline',
|
||||
isPipeline: true,
|
||||
path: '/rag/pipelines/imports/resource-1/check-dependencies',
|
||||
},
|
||||
])(
|
||||
'stores $resource dependencies with nullable versions and clears an omitted list',
|
||||
async ({ isPipeline, path }) => {
|
||||
const dependency = {
|
||||
type: 'marketplace',
|
||||
value: { marketplace_plugin_unique_identifier: 'plugin-1', version: null },
|
||||
} satisfies NonNullable<CheckDependenciesResult['leaked_dependencies']>[number]
|
||||
mocks.request
|
||||
.mockResolvedValueOnce(Response.json({ leaked_dependencies: [dependency] }))
|
||||
.mockResolvedValueOnce(Response.json({}))
|
||||
const { result } = renderHookWithConsoleQuery(usePluginDependencies)
|
||||
await act(async () =>
|
||||
expect(await result.current.handleCheckPluginDependencies('resource-1', isPipeline)).toBe(
|
||||
true,
|
||||
),
|
||||
)
|
||||
expect(useStore.getState().dependencies).toEqual([
|
||||
{ ...dependency, value: { ...dependency.value, version: undefined } },
|
||||
])
|
||||
expect(mocks.request).toHaveBeenCalledWith(
|
||||
expect.stringContaining(path),
|
||||
expect.any(Object),
|
||||
expect.objectContaining({ fetchCompat: true, ...(!isPipeline && { silent: true }) }),
|
||||
)
|
||||
await act(async () => result.current.handleCheckPluginDependencies('resource-1', isPipeline))
|
||||
expect(useStore.getState().dependencies).toEqual([])
|
||||
},
|
||||
)
|
||||
|
||||
it.each([
|
||||
[new TypeError('Network unavailable'), 'Network unavailable'],
|
||||
[
|
||||
createHttpError(new Response(null, { status: 503 }), {
|
||||
message: 'Dependency check unavailable',
|
||||
}),
|
||||
'Dependency check unavailable',
|
||||
],
|
||||
])('reports app dependency errors and clears stale dependencies', async (error, description) => {
|
||||
useStore.getState().setDependencies([
|
||||
{
|
||||
type: 'marketplace',
|
||||
value: { marketplace_plugin_unique_identifier: 'previous-app-plugin' },
|
||||
},
|
||||
])
|
||||
mocks.request.mockRejectedValue(error)
|
||||
const { result } = renderHookWithConsoleQuery(usePluginDependencies)
|
||||
await act(async () =>
|
||||
expect(await result.current.handleCheckPluginDependencies('app-1')).toBe(false),
|
||||
)
|
||||
expect(mocks.error).toHaveBeenCalledExactlyOnceWith('common.error', { description })
|
||||
expect(useStore.getState().dependencies).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,74 @@
|
||||
import type { Dependency } from '@/app/components/plugins/types'
|
||||
import { act, screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { PermissionType } from '@/app/components/plugins/types'
|
||||
import { renderWithConsoleQuery } from '@/test/console/query-data'
|
||||
import PluginDependency from '../index'
|
||||
import { useStore } from '../store'
|
||||
|
||||
const marketplace = vi.hoisted(() => vi.fn())
|
||||
vi.mock('@/service/base', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('@/service/base')>()),
|
||||
postMarketplace: marketplace,
|
||||
}))
|
||||
vi.mock('@/service/console', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@/service/console')>()
|
||||
const { withConsoleResponses, withConsoleClientResponses } =
|
||||
await import('@/test/console/service-responses')
|
||||
return {
|
||||
...actual,
|
||||
consoleClient: withConsoleClientResponses(actual.consoleClient, {
|
||||
'workspaces.current.plugin.permission.fetch.get': async () => ({
|
||||
install_permission: PermissionType.everyone,
|
||||
debug_permission: PermissionType.everyone,
|
||||
}),
|
||||
'workspaces.current.plugin.tasks.get': async () => ({ tasks: [] }),
|
||||
}),
|
||||
consoleQuery: withConsoleResponses(actual.consoleQuery, {
|
||||
'workspaces.current.plugin.list.installations.ids.post': async () => ({ plugins: [] }),
|
||||
}),
|
||||
}
|
||||
})
|
||||
// Catalog refresh is outside cancellation of the dependency installation dialog.
|
||||
vi.mock('@/app/components/plugins/install-plugin/hooks/use-refresh-plugin-list', () => ({
|
||||
default: () => ({ refreshPluginList: vi.fn() }),
|
||||
}))
|
||||
|
||||
const dependency: Dependency = {
|
||||
type: 'marketplace',
|
||||
value: { marketplace_plugin_unique_identifier: 'langgenius/sample-plugin:1.0.0' },
|
||||
}
|
||||
|
||||
describe('plugin-dependency', () => {
|
||||
beforeEach(() => {
|
||||
useStore.setState({ dependencies: [] })
|
||||
marketplace.mockReset().mockReturnValue(new Promise(() => {}))
|
||||
})
|
||||
|
||||
it('opens for new dependencies and clears them when the installation is cancelled', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onInstallComplete = vi.fn()
|
||||
renderWithConsoleQuery(<PluginDependency onInstallComplete={onInstallComplete} />, {
|
||||
workspacePermissionKeys: ['plugin.install'],
|
||||
systemFeatures: {
|
||||
enable_marketplace: true,
|
||||
rbac_enabled: true,
|
||||
plugin_installation_permission: {
|
||||
plugin_installation_scope: 'all',
|
||||
restrict_to_marketplace_only: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
|
||||
|
||||
act(() => useStore.getState().setDependencies([dependency]))
|
||||
expect(
|
||||
await screen.findByRole('dialog', { name: 'plugin.installModal.installPlugin' }),
|
||||
).toBeInTheDocument()
|
||||
await user.click(screen.getByRole('button', { name: 'common.operation.cancel' }))
|
||||
|
||||
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
|
||||
expect(useStore.getState().dependencies).toEqual([])
|
||||
expect(onInstallComplete).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,60 @@
|
||||
import { useMutation } from '@tanstack/react-query'
|
||||
import { useCallback } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { selectPipelineDependencies } from '@/app/components/rag-pipeline/utils/query-select'
|
||||
import { toast } from '@/app/notifications'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import { getErrorMessage } from '@/utils/error-message'
|
||||
import { useStore as usePluginDependenciesStore } from './store'
|
||||
|
||||
export const usePluginDependencies = () => {
|
||||
const { t } = useTranslation(['common'])
|
||||
const { mutateAsync: checkWorkflowDependencies } = useMutation(
|
||||
consoleQuery.apps.imports.byAppId.checkDependencies.get.mutationOptions({
|
||||
context: { silent: true },
|
||||
}),
|
||||
)
|
||||
const { mutateAsync: checkPipelineDependencies } = useMutation(
|
||||
consoleQuery.rag.pipelines.imports.byPipelineId.checkDependencies.get.mutationOptions(),
|
||||
)
|
||||
|
||||
const handleCheckPluginDependencies = useCallback(
|
||||
async (id: string, isPipeline = false) => {
|
||||
const { setDependencies } = usePluginDependenciesStore.getState()
|
||||
if (isPipeline) {
|
||||
setDependencies(
|
||||
selectPipelineDependencies(
|
||||
await checkPipelineDependencies({ params: { pipeline_id: id } }),
|
||||
),
|
||||
)
|
||||
return true
|
||||
}
|
||||
|
||||
try {
|
||||
const { leaked_dependencies } = await checkWorkflowDependencies({ params: { app_id: id } })
|
||||
setDependencies(
|
||||
(leaked_dependencies ?? []).map((dependency) => ({
|
||||
...dependency,
|
||||
// The legacy dependency store represents an absent version with undefined.
|
||||
value: { ...dependency.value, version: dependency.value.version ?? undefined },
|
||||
})),
|
||||
)
|
||||
return true
|
||||
} catch (error) {
|
||||
setDependencies([])
|
||||
toast.error(
|
||||
t(($) => $.error, { ns: 'common' }),
|
||||
{
|
||||
description: getErrorMessage(error),
|
||||
},
|
||||
)
|
||||
return false
|
||||
}
|
||||
},
|
||||
[checkWorkflowDependencies, checkPipelineDependencies, t],
|
||||
)
|
||||
|
||||
return {
|
||||
handleCheckPluginDependencies,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
'use client'
|
||||
|
||||
import type { InstallBundleCompleteCallback } from '@/app/components/plugins/install-plugin/install-bundle'
|
||||
import { useCallback } from 'react'
|
||||
import InstallBundle from '@/app/components/plugins/install-plugin/install-bundle'
|
||||
import { useStore } from './store'
|
||||
|
||||
type Props = {
|
||||
onInstallComplete?: InstallBundleCompleteCallback
|
||||
}
|
||||
|
||||
const PluginDependency = ({ onInstallComplete }: Props) => {
|
||||
const dependencies = useStore((s) => s.dependencies)
|
||||
|
||||
const handleCancelInstallBundle = useCallback(() => {
|
||||
const { setDependencies } = useStore.getState()
|
||||
setDependencies([])
|
||||
}, [])
|
||||
|
||||
if (!dependencies.length) return null
|
||||
|
||||
return (
|
||||
<InstallBundle
|
||||
fromDSLPayload={dependencies}
|
||||
onClose={handleCancelInstallBundle}
|
||||
onInstallComplete={onInstallComplete}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export default PluginDependency
|
||||
@@ -0,0 +1,11 @@
|
||||
import type { Dependency } from '@/app/components/plugins/types'
|
||||
import { create } from 'zustand'
|
||||
|
||||
type Shape = {
|
||||
dependencies: Dependency[]
|
||||
setDependencies: (dependencies: Dependency[]) => void
|
||||
}
|
||||
export const useStore = create<Shape>((set) => ({
|
||||
dependencies: [],
|
||||
setDependencies: (dependencies) => set({ dependencies }),
|
||||
}))
|
||||
Reference in new issue
Block a user