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
+72
@@ -0,0 +1,72 @@
|
||||
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
||||
import { act, render, screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { createRef } from 'react'
|
||||
import * as z from 'zod'
|
||||
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
||||
import Form from '../form'
|
||||
|
||||
const { toastError } = vi.hoisted(() => ({ toastError: vi.fn() }))
|
||||
vi.mock('@/app/notifications', () => ({ toast: { error: toastError } }))
|
||||
|
||||
const configurations: BaseConfiguration[] = [
|
||||
{
|
||||
type: BaseFieldType.textInput,
|
||||
variable: 'separator',
|
||||
label: 'Separator',
|
||||
required: false,
|
||||
showConditions: [],
|
||||
placeholder: '',
|
||||
tooltip: '',
|
||||
},
|
||||
]
|
||||
|
||||
const setup = () => {
|
||||
const ref = createRef<{ submit: () => void }>()
|
||||
const onSubmit = vi.fn()
|
||||
render(
|
||||
<Form
|
||||
ref={ref}
|
||||
initialData={{ separator: '---' }}
|
||||
configurations={configurations}
|
||||
schema={z.object({ separator: z.string().min(1, 'is required') })}
|
||||
onSubmit={onSubmit}
|
||||
onPreview={vi.fn()}
|
||||
isRunning={false}
|
||||
/>,
|
||||
)
|
||||
return { ref, onSubmit }
|
||||
}
|
||||
|
||||
// ProcessDocuments owns action routing and running/loading gates. These cases
|
||||
// cover the form's value reset and schema validation with real inputs.
|
||||
describe('Processing form', () => {
|
||||
it('resets edited fields to the initial values before submission', async () => {
|
||||
const user = userEvent.setup()
|
||||
const { ref, onSubmit } = setup()
|
||||
const input = screen.getByRole('textbox')
|
||||
const reset = screen.getByRole('button', { name: 'common.operation.reset' })
|
||||
expect(reset).toBeDisabled()
|
||||
await user.clear(input)
|
||||
await user.type(input, '|||')
|
||||
expect(reset).toBeEnabled()
|
||||
await user.click(reset)
|
||||
expect(input).toHaveValue('---')
|
||||
expect(reset).toBeDisabled()
|
||||
await act(async () => ref.current!.submit())
|
||||
await waitFor(() => expect(onSubmit).toHaveBeenCalledWith({ separator: '---' }))
|
||||
})
|
||||
|
||||
it('rejects an invalid field and submits after the user corrects it', async () => {
|
||||
const user = userEvent.setup()
|
||||
const { ref, onSubmit } = setup()
|
||||
const input = screen.getByRole('textbox')
|
||||
await user.clear(input)
|
||||
await act(async () => ref.current!.submit())
|
||||
await waitFor(() => expect(toastError).toHaveBeenCalledWith('"separator" is required'))
|
||||
expect(onSubmit).not.toHaveBeenCalled()
|
||||
await user.type(input, '|||')
|
||||
await act(async () => ref.current!.submit())
|
||||
await waitFor(() => expect(onSubmit).toHaveBeenCalledWith({ separator: '|||' }))
|
||||
})
|
||||
})
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
||||
import { screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { useRef } from 'react'
|
||||
import { z } from 'zod'
|
||||
import { renderWithConsoleQuery as render } from '@/test/console/query-data'
|
||||
import Form from '../form'
|
||||
|
||||
const { toastError } = vi.hoisted(() => ({ toastError: vi.fn() }))
|
||||
vi.mock('@/app/notifications', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@/app/notifications')>()
|
||||
return { ...actual, toast: { ...actual.toast, error: toastError } }
|
||||
})
|
||||
const configurations: BaseConfiguration[] = [
|
||||
{ variable: 'name', type: 'text-input', label: 'Name', required: true, showConditions: [] },
|
||||
]
|
||||
const schema = z.object({ name: z.string().min(1, 'Name is required') })
|
||||
function Harness({ onSubmit = vi.fn(), onPreview = vi.fn(), isRunning = false }) {
|
||||
const ref = useRef<{ submit: () => void }>(null)
|
||||
return (
|
||||
<>
|
||||
<Form
|
||||
ref={ref}
|
||||
initialData={{ name: 'original' }}
|
||||
configurations={configurations}
|
||||
schema={schema}
|
||||
onSubmit={onSubmit}
|
||||
onPreview={onPreview}
|
||||
isRunning={isRunning}
|
||||
/>
|
||||
<button onClick={() => ref.current?.submit()}>Process documents</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
it('submits the edited configuration through the process action and rejects missing required values', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onSubmit = vi.fn()
|
||||
render(<Harness onSubmit={onSubmit} />)
|
||||
const name = screen.getByRole('textbox')
|
||||
await user.clear(name)
|
||||
await user.click(screen.getByRole('button', { name: 'Process documents' }))
|
||||
await waitFor(() => expect(toastError).toHaveBeenCalledWith('"name" Name is required'))
|
||||
expect(onSubmit).not.toHaveBeenCalled()
|
||||
await user.type(name, 'Updated')
|
||||
await user.click(screen.getByRole('button', { name: 'Process documents' }))
|
||||
await waitFor(() => expect(onSubmit).toHaveBeenCalledWith({ name: 'Updated' }))
|
||||
})
|
||||
|
||||
it('resets edited configuration and prevents preview while processing', async () => {
|
||||
const user = userEvent.setup()
|
||||
const onPreview = vi.fn()
|
||||
const { rerender } = render(<Harness onPreview={onPreview} />)
|
||||
const reset = screen.getByRole('button', { name: 'common.operation.reset' })
|
||||
expect(reset).toBeDisabled()
|
||||
await user.type(screen.getByRole('textbox'), ' draft')
|
||||
await user.click(reset)
|
||||
expect(screen.getByRole('textbox')).toHaveValue('original')
|
||||
expect(reset).toBeDisabled()
|
||||
const preview = screen.getByRole('button', {
|
||||
name: 'datasetPipeline.addDocuments.stepTwo.previewChunks',
|
||||
})
|
||||
await user.click(preview)
|
||||
expect(onPreview).toHaveBeenCalledOnce()
|
||||
rerender(<Harness onPreview={onPreview} isRunning />)
|
||||
expect(preview).toBeDisabled()
|
||||
await user.click(preview)
|
||||
expect(onPreview).toHaveBeenCalledOnce()
|
||||
})
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
import { act, waitFor } from '@testing-library/react'
|
||||
import { renderHookWithConsoleQuery as renderHook } from '@/test/console/query-data'
|
||||
import { createDeferred } from '@/test/deferred'
|
||||
import { useInputVariables } from '../hooks'
|
||||
|
||||
const { getParams } = vi.hoisted(() => ({ getParams: vi.fn() }))
|
||||
vi.mock('@/context/dataset-detail', () => ({
|
||||
useDatasetDetailContextWithSelector: (
|
||||
selector: (state: { dataset: { pipeline_id: string } }) => unknown,
|
||||
) => selector({ dataset: { pipeline_id: 'pipeline-1' } }),
|
||||
}))
|
||||
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.byPipelineId.workflows.published.processing.parameters.get': getParams,
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
||||
const response = {
|
||||
variables: [
|
||||
{
|
||||
variable: 'chunkSize',
|
||||
label: 'Chunk size',
|
||||
type: 'number',
|
||||
required: true,
|
||||
default_value: 100,
|
||||
},
|
||||
],
|
||||
}
|
||||
const renderVariables = () => renderHook(() => useInputVariables('node-1'))
|
||||
|
||||
describe('useInputVariables', () => {
|
||||
it('loads the current pipeline and node parameters before exposing the configuration', async () => {
|
||||
const pending = createDeferred<typeof response>()
|
||||
getParams.mockReturnValue(pending.promise)
|
||||
const { result } = renderVariables()
|
||||
expect(result.current.isFetchingParams).toBe(true)
|
||||
expect(result.current.paramsConfig).toBeUndefined()
|
||||
await act(async () => pending.resolve(response))
|
||||
await waitFor(() => expect(result.current.isFetchingParams).toBe(false))
|
||||
expect(result.current.paramsConfig?.variables).toEqual(response.variables)
|
||||
expect(getParams).toHaveBeenCalledWith({
|
||||
params: { pipeline_id: 'pipeline-1' },
|
||||
query: { node_id: 'node-1' },
|
||||
})
|
||||
})
|
||||
})
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
import { act, screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { createRef } from 'react'
|
||||
import DatasetDetailContext from '@/context/dataset-detail'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import { createConsoleQueryClient, renderWithConsoleQuery } from '@/test/console/query-data'
|
||||
import { createDeferred } from '@/test/deferred'
|
||||
import { createDatasetFixture } from '@/test/fixtures/dataset'
|
||||
import ProcessDocuments from '../index'
|
||||
|
||||
const getParameters = vi.hoisted(() => vi.fn())
|
||||
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.byPipelineId.workflows.published.processing.parameters.get': getParameters,
|
||||
}),
|
||||
}
|
||||
})
|
||||
const parameters = {
|
||||
variables: [
|
||||
{
|
||||
type: 'text-input',
|
||||
belong_to_node_id: 'node-1',
|
||||
variable: 'separator',
|
||||
label: 'Separator',
|
||||
required: false,
|
||||
default_value: '---',
|
||||
},
|
||||
],
|
||||
}
|
||||
function renderProcess({ loading = false, isRunning = false } = {}) {
|
||||
const queryClient = createConsoleQueryClient()
|
||||
if (!loading)
|
||||
queryClient.setQueryData(
|
||||
consoleQuery.rag.pipelines.byPipelineId.workflows.published.processing.parameters.get.queryKey(
|
||||
{ input: { params: { pipeline_id: 'pipeline-1' }, query: { node_id: 'node-1' } } },
|
||||
),
|
||||
parameters,
|
||||
)
|
||||
const ref = createRef<{ submit: () => void }>()
|
||||
const callbacks = { onProcess: vi.fn(), onPreview: vi.fn(), onBack: vi.fn(), onSubmit: vi.fn() }
|
||||
renderWithConsoleQuery(
|
||||
<DatasetDetailContext.Provider
|
||||
value={{ dataset: createDatasetFixture({ pipeline_id: 'pipeline-1' }) }}
|
||||
>
|
||||
<ProcessDocuments ref={ref} dataSourceNodeId="node-1" isRunning={isRunning} {...callbacks} />
|
||||
</DatasetDetailContext.Provider>,
|
||||
{ queryClient },
|
||||
)
|
||||
return { ref, ...callbacks }
|
||||
}
|
||||
it.each([
|
||||
{ action: 'onProcess', name: 'datasetPipeline.operations.saveAndProcess' },
|
||||
{ action: 'onBack', name: 'datasetPipeline.operations.dataSource' },
|
||||
{ action: 'onPreview', name: 'datasetPipeline.addDocuments.stepTwo.previewChunks' },
|
||||
] as const)('routes $action from the real processing form', async ({ action, name }) => {
|
||||
const user = userEvent.setup()
|
||||
const callbacks = renderProcess()
|
||||
await user.click(screen.getByRole('button', { name }))
|
||||
expect(callbacks[action]).toHaveBeenCalledOnce()
|
||||
})
|
||||
it.each([false, true])(
|
||||
'disables processing while parameters load (running=%s)',
|
||||
async (isRunning) => {
|
||||
const pending = createDeferred<typeof parameters>()
|
||||
getParameters.mockReturnValueOnce(pending.promise)
|
||||
renderProcess({ loading: true, isRunning })
|
||||
const process = screen.getByRole('button', {
|
||||
name: 'datasetPipeline.operations.saveAndProcess',
|
||||
})
|
||||
expect(process).toBeDisabled()
|
||||
await act(async () => pending.resolve(parameters))
|
||||
await waitFor(() =>
|
||||
expect(getParameters).toHaveBeenCalledWith({
|
||||
params: { pipeline_id: 'pipeline-1' },
|
||||
query: { node_id: 'node-1' },
|
||||
}),
|
||||
)
|
||||
if (isRunning) expect(process).toBeDisabled()
|
||||
else await waitFor(() => expect(process).toBeEnabled())
|
||||
},
|
||||
)
|
||||
it('converts published variables to editable defaults and submits the edited values', async () => {
|
||||
const user = userEvent.setup()
|
||||
const { ref, onSubmit } = renderProcess()
|
||||
const input = screen.getByRole('textbox', { name: 'Separator' })
|
||||
expect(input).toHaveValue('---')
|
||||
await user.clear(input)
|
||||
await user.type(screen.getByRole('textbox', { name: 'Separator' }), '|||')
|
||||
await act(async () => ref.current!.submit())
|
||||
await waitFor(() => expect(onSubmit).toHaveBeenCalledExactlyOnceWith({ separator: '|||' }))
|
||||
})
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
import { Button } from '@langgenius/dify-ui/button'
|
||||
import { RiArrowLeftLine } from '@remixicon/react'
|
||||
import * as React from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
type ActionsProps = {
|
||||
onBack: () => void
|
||||
runDisabled?: boolean
|
||||
onProcess: () => void
|
||||
}
|
||||
|
||||
const Actions = ({ onBack, runDisabled, onProcess }: ActionsProps) => {
|
||||
const { t } = useTranslation(['datasetPipeline'])
|
||||
|
||||
return (
|
||||
<div className="flex items-center justify-between">
|
||||
<Button variant="secondary" onClick={onBack}>
|
||||
<RiArrowLeftLine className="size-4" />
|
||||
<span>{t(($) => $['operations.dataSource'], { ns: 'datasetPipeline' })}</span>
|
||||
</Button>
|
||||
<Button variant="primary" disabled={runDisabled} onClick={onProcess}>
|
||||
{t(($) => $['operations.saveAndProcess'], { ns: 'datasetPipeline' })}
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(Actions)
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
import type { ZodType } from 'zod'
|
||||
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
||||
import { useCallback, useImperativeHandle } from 'react'
|
||||
import { useAppForm } from '@/app/components/base/form'
|
||||
import BaseField from '@/app/components/base/form/form-scenarios/base/field'
|
||||
import { toast } from '@/app/notifications'
|
||||
import Header from './header'
|
||||
|
||||
type OptionsProps = {
|
||||
initialData: Record<string, any>
|
||||
configurations: BaseConfiguration[]
|
||||
schema: ZodType
|
||||
onSubmit: (data: Record<string, any>) => void
|
||||
onPreview: () => void
|
||||
ref: React.RefObject<any>
|
||||
isRunning: boolean
|
||||
}
|
||||
|
||||
const Form = ({
|
||||
initialData,
|
||||
configurations,
|
||||
schema,
|
||||
onSubmit,
|
||||
onPreview,
|
||||
ref,
|
||||
isRunning,
|
||||
}: OptionsProps) => {
|
||||
const form = useAppForm({
|
||||
defaultValues: initialData,
|
||||
validators: {
|
||||
onSubmit: ({ value }) => {
|
||||
const result = schema.safeParse(value)
|
||||
if (!result.success) {
|
||||
const issues = result.error.issues
|
||||
const firstIssue = issues[0]
|
||||
const errorMessage = `"${firstIssue!.path.join('.')}" ${firstIssue!.message}`
|
||||
toast.error(errorMessage)
|
||||
return errorMessage
|
||||
}
|
||||
return undefined
|
||||
},
|
||||
},
|
||||
onSubmit: ({ value }) => {
|
||||
onSubmit(value)
|
||||
},
|
||||
})
|
||||
|
||||
useImperativeHandle(ref, () => {
|
||||
return {
|
||||
submit: () => {
|
||||
form.handleSubmit()
|
||||
},
|
||||
}
|
||||
}, [form])
|
||||
|
||||
const handleReset = useCallback(() => {
|
||||
form.reset()
|
||||
}, [form])
|
||||
|
||||
return (
|
||||
<form
|
||||
className="flex w-full flex-col rounded-lg border border-components-panel-border bg-components-panel-bg"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
form.handleSubmit()
|
||||
}}
|
||||
>
|
||||
<form.Subscribe
|
||||
selector={(state) => state.isDirty}
|
||||
children={(isDirty) => (
|
||||
<Header
|
||||
onReset={handleReset}
|
||||
resetDisabled={!isDirty}
|
||||
onPreview={onPreview}
|
||||
previewDisabled={isRunning}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<div className="flex flex-col gap-3 border-t border-divider-subtle px-4 py-3">
|
||||
{configurations.map((config, index) => {
|
||||
const FieldComponent = BaseField({
|
||||
initialData,
|
||||
config,
|
||||
})
|
||||
return <FieldComponent key={index} form={form} />
|
||||
})}
|
||||
</div>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
|
||||
export default Form
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
import { Button } from '@langgenius/dify-ui/button'
|
||||
import { RiSearchEyeLine } from '@remixicon/react'
|
||||
import * as React from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
type HeaderProps = {
|
||||
onReset: () => void
|
||||
resetDisabled: boolean
|
||||
previewDisabled: boolean
|
||||
onPreview?: () => void
|
||||
}
|
||||
|
||||
const Header = ({ onReset, resetDisabled, previewDisabled, onPreview }: HeaderProps) => {
|
||||
const { t } = useTranslation(['common', 'datasetPipeline'])
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-x-1 px-4 py-2">
|
||||
<div className="grow system-sm-semibold-uppercase text-text-secondary">
|
||||
{t(($) => $['addDocuments.stepTwo.chunkSettings'], { ns: 'datasetPipeline' })}
|
||||
</div>
|
||||
<Button variant="ghost" disabled={resetDisabled} onClick={onReset}>
|
||||
{t(($) => $['operation.reset'], { ns: 'common' })}
|
||||
</Button>
|
||||
<Button variant="secondary-accent" onClick={onPreview} disabled={previewDisabled}>
|
||||
<RiSearchEyeLine className="size-4" />
|
||||
<span>{t(($) => $['addDocuments.stepTwo.previewChunks'], { ns: 'datasetPipeline' })}</span>
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(Header)
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { selectPipelineStepParameters } from '@/app/components/rag-pipeline/utils/query-select'
|
||||
import { useDatasetDetailContextWithSelector } from '@/context/dataset-detail'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
|
||||
export const useInputVariables = (datasourceNodeId: string) => {
|
||||
const pipelineId = useDatasetDetailContextWithSelector((state) => state.dataset?.pipeline_id)
|
||||
const { data: paramsConfig, isFetching: isFetchingParams } = useQuery(
|
||||
consoleQuery.rag.pipelines.byPipelineId.workflows.published.processing.parameters.get.queryOptions(
|
||||
{
|
||||
input: { params: { pipeline_id: pipelineId! }, query: { node_id: datasourceNodeId } },
|
||||
select: selectPipelineStepParameters,
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
return {
|
||||
paramsConfig,
|
||||
isFetchingParams,
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
import * as React from 'react'
|
||||
import { generateZodSchema } from '@/app/components/base/form/form-scenarios/base/utils'
|
||||
import {
|
||||
useConfigurations,
|
||||
useInitialData,
|
||||
} from '@/app/components/rag-pipeline/hooks/use-input-fields'
|
||||
import Actions from './actions'
|
||||
import Form from './form'
|
||||
import { useInputVariables } from './hooks'
|
||||
|
||||
type ProcessDocumentsProps = {
|
||||
dataSourceNodeId: string
|
||||
ref: React.RefObject<any>
|
||||
isRunning: boolean
|
||||
onProcess: () => void
|
||||
onPreview: () => void
|
||||
onSubmit: (data: Record<string, any>) => void
|
||||
onBack: () => void
|
||||
}
|
||||
|
||||
const ProcessDocuments = ({
|
||||
dataSourceNodeId,
|
||||
isRunning,
|
||||
onProcess,
|
||||
onPreview,
|
||||
onSubmit,
|
||||
onBack,
|
||||
ref,
|
||||
}: ProcessDocumentsProps) => {
|
||||
const { isFetchingParams, paramsConfig } = useInputVariables(dataSourceNodeId)
|
||||
const initialData = useInitialData(paramsConfig?.variables || [])
|
||||
const configurations = useConfigurations(paramsConfig?.variables || [])
|
||||
const schema = generateZodSchema(configurations)
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-y-4 pt-4">
|
||||
<Form
|
||||
ref={ref}
|
||||
initialData={initialData}
|
||||
configurations={configurations}
|
||||
schema={schema}
|
||||
onSubmit={onSubmit}
|
||||
onPreview={onPreview}
|
||||
isRunning={isRunning}
|
||||
/>
|
||||
<Actions runDisabled={isFetchingParams || isRunning} onBack={onBack} onProcess={onProcess} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default React.memo(ProcessDocuments)
|
||||
Reference in new issue
Block a user