chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动

演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
wuzilongandCursor committed 2026-11-18 13:59:25 +08:00
1 parent 60158935f4
commit 6519682885
15831 files changed
+3063428

No files matched your search

@@ -0,0 +1,83 @@
import type { DataSourceNodeType } from '@/app/components/workflow/nodes/data-source/types'
import type { Node } from '@/app/components/workflow/types'
import { screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { BlockEnum } from '@/app/components/workflow/types'
import { renderWithConsoleQuery as render } from '@/test/console/query-data'
import DataSourceOptions from '../index'
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, {
'rag.pipelines.datasourcePlugins.get': () => Promise.resolve([]),
}),
}
})
const createNode = (id: string, title: string): Node<DataSourceNodeType> => ({
id,
type: 'custom',
position: { x: 0, y: 0 },
data: {
title,
desc: '',
type: BlockEnum.DataSource,
plugin_id: `plugin-${id}`,
provider_type: 'local_file',
provider_name: 'Test Provider',
datasource_name: `datasource-${id}`,
datasource_label: title,
datasource_parameters: {},
datasource_configurations: {},
},
})
const nodes = [createNode('node-1', 'Local files'), createNode('node-2', 'Notion')]
describe('DataSourceOptions', () => {
it('selects the first data source when none is selected', () => {
const onSelect = vi.fn()
render(<DataSourceOptions pipelineNodes={nodes} datasourceNodeId="" onSelect={onSelect} />)
expect(onSelect).toHaveBeenCalledWith({
nodeId: 'node-1',
nodeData: nodes[0]!.data,
})
})
it('keeps an existing data source selection on mount', () => {
const onSelect = vi.fn()
render(
<DataSourceOptions pipelineNodes={nodes} datasourceNodeId="node-2" onSelect={onSelect} />,
)
expect(onSelect).not.toHaveBeenCalled()
})
it('returns the selected data source', async () => {
const user = userEvent.setup()
const onSelect = vi.fn()
render(
<DataSourceOptions pipelineNodes={nodes} datasourceNodeId="node-1" onSelect={onSelect} />,
)
await user.click(screen.getByText('Notion'))
expect(onSelect).toHaveBeenCalledWith({
nodeId: 'node-2',
nodeData: nodes[1]!.data,
})
})
it('does not select anything when the pipeline has no data sources', () => {
const onSelect = vi.fn()
render(<DataSourceOptions pipelineNodes={[]} datasourceNodeId="" onSelect={onSelect} />)
expect(onSelect).not.toHaveBeenCalled()
})
})
@@ -0,0 +1,105 @@
import type { PropsWithChildren } from 'react'
import type { DataSourceNodeType } from '@/app/components/workflow/nodes/data-source/types'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen, waitFor } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vite-plus/test'
import { createDatasourceProvider } from '@/app/components/rag-pipeline/__tests__/datasource-fixtures'
import { BlockEnum } from '@/app/components/workflow/types'
import { consoleQuery } from '@/service/console'
import OptionCard from '../option-card'
const request = vi.hoisted(() => vi.fn<(url: string) => Promise<Response>>())
vi.mock('@/service/base', () => ({ request }))
vi.mock('@/utils/var', async (importOriginal) => ({
...(await importOriginal<typeof import('@/utils/var')>()),
basePath: '/base',
}))
const node: DataSourceNodeType = {
plugin_id: 'langgenius/file',
provider_type: 'local_file',
provider_name: 'file',
datasource_name: 'local-file',
datasource_label: 'Local File',
datasource_parameters: {},
datasource_configurations: {},
title: 'DataSource',
desc: '',
type: BlockEnum.DataSource,
}
describe('OptionCard', () => {
let client: QueryClient
const key = consoleQuery.rag.pipelines.datasourcePlugins.get.queryKey()
const wrapper = ({ children }: PropsWithChildren) => (
<QueryClientProvider client={client}>{children}</QueryClientProvider>
)
beforeEach(() => {
vi.clearAllMocks()
client = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: Infinity } } })
request.mockResolvedValue(Response.json([]))
})
afterEach(() => client.clear())
it('resolves an installed provider icon without mutating the shared catalog response', async () => {
const icon = '/icon.png'
const expected = '/base/icon.png'
const provider = createDatasourceProvider()
provider.declaration.identity.icon = icon
request.mockResolvedValue(Response.json([provider]))
const { container } = render(<OptionCard label="Local File" selected nodeData={node} />, {
wrapper,
})
await waitFor(() =>
expect(container.querySelector('.bg-cover')).toHaveStyle({
backgroundImage: `url("${expected}")`,
}),
)
expect(client.getQueryData(key)).toEqual([provider])
expect(request).toHaveBeenCalledOnce()
})
it('renders a cached icon while revalidating the catalog', () => {
client.setQueryData(key, [createDatasourceProvider()])
request.mockReturnValue(new Promise(() => {}))
const { container } = render(<OptionCard label="Local File" selected nodeData={node} />, {
wrapper,
})
expect(container.querySelector('.bg-cover')).toHaveStyle({
backgroundImage: 'url("/base/datasource.svg")',
})
})
it('keeps a missing or pending provider icon empty instead of constructing a URL', async () => {
const { container } = render(
<OptionCard label="Local File" selected={false} nodeData={node} />,
{ wrapper },
)
expect(container.querySelector<HTMLElement>('.bg-cover')?.style.backgroundImage).toBe('')
await waitFor(() => expect(client.getQueryState(key)?.status).toBe('success'))
expect(container.querySelector<HTMLElement>('.bg-cover')?.style.backgroundImage).toBe('')
expect(screen.getByText('Local File')).toBeInTheDocument()
})
it('resolves an older saved provider without plugin metadata', async () => {
const provider = createDatasourceProvider()
request.mockResolvedValue(Response.json([provider]))
const { container } = render(
<OptionCard label="Local File" selected={false} nodeData={{ ...node, plugin_id: '' }} />,
{ wrapper },
)
await waitFor(() =>
expect(container.querySelector('.bg-cover')).toHaveStyle({
backgroundImage: 'url("/base/datasource.svg")',
}),
)
expect(client.getQueryData(key)).toEqual([provider])
})
})
@@ -0,0 +1,36 @@
import type { FC } from 'react'
import { cn } from '@langgenius/dify-ui/cn'
import { memo } from 'react'
type DatasourceIconProps = {
size?: string
className?: string
iconUrl?: string
}
const ICON_CONTAINER_CLASSNAME_SIZE_MAP: Record<string, string> = {
xs: 'w-4 h-4 rounded-[5px] shadow-xs',
sm: 'w-5 h-5 rounded-md shadow-xs',
md: 'w-6 h-6 rounded-lg shadow-md',
}
const DatasourceIcon: FC<DatasourceIconProps> = ({ size = 'sm', className, iconUrl }) => {
return (
<div
className={cn(
'flex items-center justify-center shadow-none',
ICON_CONTAINER_CLASSNAME_SIZE_MAP[size],
className,
)}
>
<div
className="size-full shrink-0 rounded-md bg-cover bg-center"
style={{
backgroundImage: iconUrl ? `url(${iconUrl})` : undefined,
}}
/>
</div>
)
}
export default memo(DatasourceIcon)
@@ -0,0 +1,53 @@
import type { Datasource } from '@/app/components/rag-pipeline/components/panel/test-run/types'
import type { DataSourceNodeType } from '@/app/components/workflow/nodes/data-source/types'
import type { Node } from '@/app/components/workflow/types'
import { useCallback, useEffect } from 'react'
import { useDatasourceOptions } from '../hooks'
import OptionCard from './option-card'
type DataSourceOptionsProps = {
pipelineNodes: Node<DataSourceNodeType>[]
datasourceNodeId: string
onSelect: (option: Datasource) => void
}
const DataSourceOptions = ({
pipelineNodes,
datasourceNodeId,
onSelect,
}: DataSourceOptionsProps) => {
const options = useDatasourceOptions(pipelineNodes)
const handelSelect = useCallback(
(value: string) => {
const selectedOption = options.find((option) => option.value === value)
if (!selectedOption) return
const datasource = {
nodeId: selectedOption.value,
nodeData: selectedOption.data,
}
onSelect(datasource)
},
[onSelect, options],
)
useEffect(() => {
if (options.length > 0 && !datasourceNodeId) handelSelect(options[0]!.value)
}, [])
return (
<div className="grid w-full grid-cols-4 gap-1">
{options.map((option) => (
<OptionCard
key={option.value}
label={option.label}
selected={datasourceNodeId === option.value}
nodeData={option.data}
onClick={handelSelect.bind(null, option.value)}
/>
))}
</div>
)
}
export default DataSourceOptions
@@ -0,0 +1,50 @@
import type { DataSourceNodeType } from '@/app/components/workflow/nodes/data-source/types'
import { cn } from '@langgenius/dify-ui/cn'
import { useQuery } from '@tanstack/react-query'
import * as React from 'react'
import { resolveDatasourceIcon } from '@/app/components/rag-pipeline/utils/datasource-icon'
import { matchDataSource } from '@/app/components/workflow/utils/plugin-install-check'
import { consoleQuery } from '@/service/console'
import DatasourceIcon from './datasource-icon'
type OptionCardProps = {
label: string
selected: boolean
nodeData: DataSourceNodeType
onClick?: () => void
}
const OptionCard = ({ label, selected, nodeData, onClick }: OptionCardProps) => {
const { data: dataSourceList } = useQuery(
consoleQuery.rag.pipelines.datasourcePlugins.get.queryOptions(),
)
const provider = dataSourceList && matchDataSource(dataSourceList, nodeData)
const iconUrl = provider && resolveDatasourceIcon(provider.declaration.identity.icon)
return (
<div
className={cn(
'flex cursor-pointer items-center gap-2 rounded-xl border border-components-option-card-option-border bg-components-option-card-option-bg p-3 shadow-shadow-shadow-3',
selected
? 'border-components-option-card-option-selected-border bg-components-option-card-option-selected-bg shadow-xs inset-ring-[0.5px] inset-ring-components-option-card-option-selected-border'
: 'hover:border-components-option-card-option-border-hover hover:shadow-xs',
)}
onClick={onClick}
>
<div className="flex size-8 shrink-0 items-center justify-center rounded-lg border-[0.5px] border-components-panel-border bg-background-default-dodge p-1.5">
<DatasourceIcon iconUrl={iconUrl} />
</div>
<div
className={cn(
'line-clamp-2 grow system-sm-medium text-text-secondary',
selected && 'text-text-primary',
)}
title={label}
>
{label}
</div>
</div>
)
}
export default React.memo(OptionCard)