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
+274
@@ -0,0 +1,274 @@
|
||||
import type { ExternalKnowledgeApiResponse } from '@dify/contracts/api/console/datasets/types.gen'
|
||||
import { ORPCError } from '@orpc/client'
|
||||
import { act, screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { createExternalKnowledgeBase } from '@/service/datasets'
|
||||
import { renderWithConsoleQuery as render } from '@/test/console/query-data'
|
||||
import { createDeferred } from '@/test/deferred'
|
||||
import { createDatasetFixture } from '@/test/fixtures/dataset'
|
||||
import ExternalKnowledgeBaseConnector from '../index'
|
||||
|
||||
const mockRouterBack = vi.fn()
|
||||
const mockReplace = vi.fn()
|
||||
vi.mock('@/next/navigation', () => ({
|
||||
useRouter: () => ({
|
||||
back: mockRouterBack,
|
||||
replace: mockReplace,
|
||||
push: vi.fn(),
|
||||
refresh: vi.fn(),
|
||||
}),
|
||||
}))
|
||||
|
||||
// Mock useDocLink hook
|
||||
vi.mock('@/context/i18n', () => ({
|
||||
useDocLink: () => (path?: string) => `https://docs.dify.ai/en${path || ''}`,
|
||||
}))
|
||||
|
||||
const mockToastSuccess = vi.hoisted(() => vi.fn())
|
||||
const mockToastError = vi.hoisted(() => vi.fn())
|
||||
vi.mock('@/app/notifications', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@/app/notifications')>()
|
||||
return {
|
||||
...actual,
|
||||
toast: {
|
||||
...actual.toast,
|
||||
success: mockToastSuccess,
|
||||
error: mockToastError,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
// Mock modal context
|
||||
vi.mock('@/context/modal-context', () => ({
|
||||
useModalContext: () => ({
|
||||
setShowExternalKnowledgeAPIModal: vi.fn(),
|
||||
}),
|
||||
}))
|
||||
|
||||
// Mock API service
|
||||
vi.mock('@/service/datasets', () => ({
|
||||
createExternalKnowledgeBase: vi.fn(),
|
||||
}))
|
||||
|
||||
// Factory function to create mock ExternalAPIItem
|
||||
const createMockExternalAPIItem = (
|
||||
overrides: Partial<ExternalKnowledgeApiResponse> = {},
|
||||
): ExternalKnowledgeApiResponse => ({
|
||||
id: 'api-default',
|
||||
tenant_id: 'tenant-1',
|
||||
name: 'Default API',
|
||||
description: 'Default API description',
|
||||
settings: {
|
||||
endpoint: 'https://api.example.com',
|
||||
api_key: 'test-api-key',
|
||||
},
|
||||
dataset_bindings: [],
|
||||
created_by: 'user-1',
|
||||
created_at: '2024-01-01T00:00:00Z',
|
||||
...overrides,
|
||||
})
|
||||
|
||||
// Default mock API list
|
||||
const createDefaultMockApiList = (): ExternalKnowledgeApiResponse[] => [
|
||||
createMockExternalAPIItem({
|
||||
id: 'api-1',
|
||||
name: 'Test API 1',
|
||||
settings: { endpoint: 'https://api1.example.com', api_key: 'key-1' },
|
||||
}),
|
||||
createMockExternalAPIItem({
|
||||
id: 'api-2',
|
||||
name: 'Test API 2',
|
||||
settings: { endpoint: 'https://api2.example.com', api_key: 'key-2' },
|
||||
}),
|
||||
]
|
||||
|
||||
let mockExternalKnowledgeApiList: ExternalKnowledgeApiResponse[] = createDefaultMockApiList()
|
||||
|
||||
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, {
|
||||
'datasets.externalKnowledgeApi.get': () => ({ data: mockExternalKnowledgeApiList }),
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
||||
// Suppress console.error helper
|
||||
const suppressConsoleError = () => vi.spyOn(console, 'error').mockImplementation(vi.fn())
|
||||
|
||||
// Helper to fill required form fields and submit
|
||||
async function fillFormAndSubmit(user: ReturnType<typeof userEvent.setup>) {
|
||||
const nameInput = screen.getByPlaceholderText('dataset.externalKnowledgeNamePlaceholder')
|
||||
const knowledgeIdInput = screen.getByPlaceholderText('dataset.externalKnowledgeIdPlaceholder')
|
||||
|
||||
await user.type(nameInput, 'Test Knowledge Base')
|
||||
await user.type(knowledgeIdInput, 'kb-123')
|
||||
|
||||
// Wait for button to be enabled
|
||||
await waitFor(() => {
|
||||
const connectButton = screen
|
||||
.getByText('dataset.externalKnowledgeForm.connect')
|
||||
.closest('button')
|
||||
expect(connectButton).not.toBeDisabled()
|
||||
})
|
||||
|
||||
const connectButton = screen.getByText('dataset.externalKnowledgeForm.connect').closest('button')
|
||||
await user.click(connectButton!)
|
||||
}
|
||||
|
||||
describe('ExternalKnowledgeBaseConnector', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mockExternalKnowledgeApiList = createDefaultMockApiList()
|
||||
vi.mocked(createExternalKnowledgeBase).mockResolvedValue(
|
||||
createDatasetFixture({ id: 'new-kb-id' }),
|
||||
)
|
||||
})
|
||||
|
||||
// Tests for rendering with real ExternalKnowledgeBaseCreate component
|
||||
|
||||
// Tests for API success flow
|
||||
describe('API Success Flow', () => {
|
||||
it('should call API and show success notification when form is submitted', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<ExternalKnowledgeBaseConnector />)
|
||||
|
||||
await fillFormAndSubmit(user)
|
||||
|
||||
// Verify API was called with form data
|
||||
await waitFor(() => {
|
||||
expect(createExternalKnowledgeBase).toHaveBeenCalledWith(
|
||||
{
|
||||
body: expect.objectContaining({
|
||||
name: 'Test Knowledge Base',
|
||||
external_knowledge_id: 'kb-123',
|
||||
external_knowledge_api_id: 'api-1',
|
||||
provider: 'external',
|
||||
external_retrieval_model: {
|
||||
top_k: 4,
|
||||
score_threshold: 0.5,
|
||||
score_threshold_enabled: false,
|
||||
},
|
||||
}),
|
||||
},
|
||||
{ silent: true },
|
||||
)
|
||||
})
|
||||
|
||||
// Verify success notification
|
||||
expect(mockToastSuccess).toHaveBeenCalledWith(
|
||||
'dataset.externalKnowledgeForm.connectedSuccess',
|
||||
)
|
||||
|
||||
// Verify navigation back
|
||||
expect(mockRouterBack).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
|
||||
// Tests for API error flow
|
||||
describe('API Error Flow', () => {
|
||||
it.each([
|
||||
{
|
||||
scenario: 'the selected API template no longer exists',
|
||||
error: new ORPCError('NOT_FOUND', { status: 404, message: 'API template not found.' }),
|
||||
message: 'API template not found.',
|
||||
},
|
||||
{
|
||||
scenario: 'the failure has no error message',
|
||||
// oxlint-disable-next-line unicorn/error-message -- Exercise the fallback for an exception without a useful message.
|
||||
error: new Error(''),
|
||||
message: 'dataset.externalKnowledgeForm.connectedFailed',
|
||||
},
|
||||
])('shows one error and keeps the form open when $scenario', async ({ error, message }) => {
|
||||
const user = userEvent.setup()
|
||||
const consoleErrorSpy = suppressConsoleError()
|
||||
vi.mocked(createExternalKnowledgeBase).mockRejectedValue(error)
|
||||
|
||||
render(<ExternalKnowledgeBaseConnector />)
|
||||
|
||||
await fillFormAndSubmit(user)
|
||||
|
||||
// Verify error notification
|
||||
await waitFor(() => {
|
||||
expect(mockToastError).toHaveBeenCalledExactlyOnceWith(message)
|
||||
})
|
||||
|
||||
// Verify no navigation
|
||||
expect(mockRouterBack).not.toHaveBeenCalled()
|
||||
|
||||
consoleErrorSpy.mockRestore()
|
||||
})
|
||||
})
|
||||
|
||||
// Tests for loading state
|
||||
describe('Loading State', () => {
|
||||
it('should show loading state during API call', async () => {
|
||||
const user = userEvent.setup()
|
||||
|
||||
// Create a promise that won't resolve immediately
|
||||
const { promise, resolve: resolvePromise } =
|
||||
createDeferred<Awaited<ReturnType<typeof createExternalKnowledgeBase>>>()
|
||||
vi.mocked(createExternalKnowledgeBase).mockReturnValue(promise)
|
||||
|
||||
render(<ExternalKnowledgeBaseConnector />)
|
||||
|
||||
// Fill form
|
||||
const nameInput = screen.getByPlaceholderText('dataset.externalKnowledgeNamePlaceholder')
|
||||
const knowledgeIdInput = screen.getByPlaceholderText('dataset.externalKnowledgeIdPlaceholder')
|
||||
await user.type(nameInput, 'Test')
|
||||
await user.type(knowledgeIdInput, 'kb-1')
|
||||
|
||||
await waitFor(() => {
|
||||
const connectButton = screen
|
||||
.getByText('dataset.externalKnowledgeForm.connect')
|
||||
.closest('button')
|
||||
expect(connectButton).not.toBeDisabled()
|
||||
})
|
||||
|
||||
const connectButton = screen
|
||||
.getByText('dataset.externalKnowledgeForm.connect')
|
||||
.closest('button')
|
||||
await user.click(connectButton!)
|
||||
|
||||
// Button should show loading (the real Button component has loading prop)
|
||||
await waitFor(() => {
|
||||
expect(createExternalKnowledgeBase).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
expect(connectButton).toHaveAttribute('aria-disabled', 'true')
|
||||
await user.click(connectButton!)
|
||||
expect(createExternalKnowledgeBase).toHaveBeenCalledOnce()
|
||||
await act(async () => resolvePromise(createDatasetFixture({ id: 'new-id' })))
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockToastSuccess).toHaveBeenCalledWith(
|
||||
'dataset.externalKnowledgeForm.connectedSuccess',
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// Tests for form validation (integration with real create component)
|
||||
|
||||
// Tests for user interactions
|
||||
describe('User Interactions', () => {
|
||||
it('should link back to the dataset list from cancel', () => {
|
||||
render(<ExternalKnowledgeBaseConnector />)
|
||||
|
||||
expect(
|
||||
screen.getByRole('link', { name: 'dataset.externalKnowledgeForm.cancel' }),
|
||||
).toHaveAttribute('href', '/datasets')
|
||||
})
|
||||
|
||||
it('should handle back button click', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<ExternalKnowledgeBaseConnector />)
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'common.operation.back' }))
|
||||
|
||||
expect(mockReplace).toHaveBeenCalledWith('/datasets')
|
||||
})
|
||||
})
|
||||
})
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
'use client'
|
||||
|
||||
import type { CreateKnowledgeBaseReq } from '@/app/components/datasets/external-knowledge-base/create/declarations'
|
||||
import * as React from 'react'
|
||||
import { useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { trackEvent } from '@/app/components/base/amplitude'
|
||||
import ExternalKnowledgeBaseCreate from '@/app/components/datasets/external-knowledge-base/create'
|
||||
import { toast } from '@/app/notifications'
|
||||
import useDocumentTitle from '@/hooks/use-document-title'
|
||||
import { useRouter } from '@/next/navigation'
|
||||
import { createExternalKnowledgeBase } from '@/service/datasets'
|
||||
import { getErrorMessage } from '@/utils/error-message'
|
||||
|
||||
const ExternalKnowledgeBaseConnector = () => {
|
||||
const [loading, setLoading] = useState(false)
|
||||
const router = useRouter()
|
||||
const { t } = useTranslation(['dataset', 'onboarding'])
|
||||
useDocumentTitle(
|
||||
t(($) => $['stepByStepTour.guides.knowledge.empty.connect.title'], { ns: 'onboarding' }),
|
||||
)
|
||||
|
||||
const handleConnect = async (formValue: CreateKnowledgeBaseReq) => {
|
||||
try {
|
||||
setLoading(true)
|
||||
const result = await createExternalKnowledgeBase({ body: formValue }, { silent: true })
|
||||
if (result && result.id) {
|
||||
toast.success(t(($) => $['externalKnowledgeForm.connectedSuccess'], { ns: 'dataset' }))
|
||||
trackEvent('create_external_knowledge_base', {
|
||||
provider: formValue.provider,
|
||||
name: formValue.name,
|
||||
})
|
||||
router.back()
|
||||
} else {
|
||||
throw new Error('Failed to create external knowledge base')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error creating external knowledge base:', error)
|
||||
toast.error(
|
||||
getErrorMessage(error) ??
|
||||
t(($) => $['externalKnowledgeForm.connectedFailed'], { ns: 'dataset' }),
|
||||
)
|
||||
}
|
||||
setLoading(false)
|
||||
}
|
||||
return <ExternalKnowledgeBaseCreate onConnect={handleConnect} loading={loading} />
|
||||
}
|
||||
|
||||
export default ExternalKnowledgeBaseConnector
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
import { RiAddLine, RiArrowDownSLine } from '@remixicon/react'
|
||||
import { useQueryClient } from '@tanstack/react-query'
|
||||
import * as React from 'react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useModalContext } from '@/context/modal-context'
|
||||
import { useRouter } from '@/next/navigation'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
|
||||
type ApiItem = {
|
||||
value: string
|
||||
name: string
|
||||
url: string
|
||||
}
|
||||
|
||||
type ExternalApiSelectProps = {
|
||||
'aria-labelledby'?: string
|
||||
items: ApiItem[]
|
||||
value?: string
|
||||
onSelect: (item: ApiItem) => void
|
||||
}
|
||||
|
||||
const ExternalApiSelect: React.FC<ExternalApiSelectProps> = ({
|
||||
'aria-labelledby': ariaLabelledBy,
|
||||
items,
|
||||
value,
|
||||
onSelect,
|
||||
}) => {
|
||||
const { t } = useTranslation(['dataset'])
|
||||
const [isOpen, setIsOpen] = useState(false)
|
||||
const [selectedItem, setSelectedItem] = useState<ApiItem | null>(
|
||||
items.find((item) => item.value === value) || null,
|
||||
)
|
||||
const { setShowExternalKnowledgeAPIModal } = useModalContext()
|
||||
const queryClient = useQueryClient()
|
||||
const externalKnowledgeApiQueryKey = consoleQuery.datasets.externalKnowledgeApi.get.queryOptions({
|
||||
input: {},
|
||||
}).queryKey
|
||||
const router = useRouter()
|
||||
|
||||
useEffect(() => {
|
||||
const newSelectedItem = items.find((item) => item.value === value) || null
|
||||
setSelectedItem(newSelectedItem)
|
||||
}, [value, items])
|
||||
|
||||
const handleAddNewAPI = () => {
|
||||
setShowExternalKnowledgeAPIModal({
|
||||
payload: { name: '', settings: { endpoint: '', api_key: '' } },
|
||||
onSaveCallback: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: externalKnowledgeApiQueryKey })
|
||||
router.refresh()
|
||||
},
|
||||
isEditMode: false,
|
||||
})
|
||||
}
|
||||
|
||||
const handleSelect = (item: ApiItem) => {
|
||||
setSelectedItem(item)
|
||||
onSelect(item)
|
||||
setIsOpen(false)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="relative w-full">
|
||||
<button
|
||||
type="button"
|
||||
aria-labelledby={ariaLabelledBy}
|
||||
className={`flex w-full cursor-pointer appearance-none items-center justify-between gap-0.5 self-stretch rounded-lg border-0 bg-components-input-bg-normal px-2 py-1 text-left hover:bg-state-base-hover-alt ${isOpen && 'bg-state-base-hover-alt'}`}
|
||||
onClick={() => setIsOpen(!isOpen)}
|
||||
>
|
||||
{selectedItem ? (
|
||||
<span className="flex items-center gap-2 self-stretch rounded-lg p-1">
|
||||
<span
|
||||
aria-hidden
|
||||
className="i-custom-vender-solid-development-api-connection-mod size-4 text-text-secondary"
|
||||
/>
|
||||
<span className="flex grow items-center">
|
||||
<span className="overflow-hidden system-sm-regular text-ellipsis text-components-input-text-filled">
|
||||
{selectedItem.name}
|
||||
</span>
|
||||
</span>
|
||||
</span>
|
||||
) : (
|
||||
<span className="system-sm-regular text-components-input-text-placeholder">
|
||||
{t(($) => $['selectExternalKnowledgeAPI.placeholder'], { ns: 'dataset' })}
|
||||
</span>
|
||||
)}
|
||||
<RiArrowDownSLine
|
||||
className={`size-4 text-text-quaternary transition-transform ${isOpen ? 'text-text-secondary' : ''}`}
|
||||
/>
|
||||
</button>
|
||||
{isOpen && (
|
||||
<div className="absolute z-10 mt-1 w-full rounded-xl border border-components-panel-border bg-components-panel-bg-blur shadow-lg">
|
||||
{items.map((item) => (
|
||||
<button
|
||||
type="button"
|
||||
key={item.value}
|
||||
className="flex w-full cursor-pointer appearance-none items-center border-0 bg-transparent p-1 text-left"
|
||||
onClick={() => handleSelect(item)}
|
||||
>
|
||||
<span className="flex w-full items-center gap-2 self-stretch rounded-lg p-2 hover:bg-state-base-hover">
|
||||
<span
|
||||
aria-hidden
|
||||
className="i-custom-vender-solid-development-api-connection-mod size-4 text-text-secondary"
|
||||
/>
|
||||
<span className="grow overflow-hidden system-sm-medium text-ellipsis text-text-secondary">
|
||||
{item.name}
|
||||
</span>
|
||||
<span className="overflow-hidden text-right system-xs-regular text-ellipsis text-text-tertiary">
|
||||
{item.url}
|
||||
</span>
|
||||
</span>
|
||||
</button>
|
||||
))}
|
||||
<div className="flex flex-col items-start self-stretch p-1">
|
||||
<button
|
||||
type="button"
|
||||
className="flex w-full cursor-pointer appearance-none items-center gap-2 self-stretch rounded-lg border-0 bg-transparent p-2 text-left hover:bg-state-base-hover"
|
||||
onClick={handleAddNewAPI}
|
||||
>
|
||||
<RiAddLine className="size-4 text-text-secondary" />
|
||||
<span className="grow overflow-hidden system-sm-medium text-ellipsis text-text-secondary">
|
||||
{t(($) => $.createNewExternalAPI, { ns: 'dataset' })}
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ExternalApiSelect
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
'use client'
|
||||
|
||||
import { Button } from '@langgenius/dify-ui/button'
|
||||
import { Input } from '@langgenius/dify-ui/input'
|
||||
import { RiAddLine } from '@remixicon/react'
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import * as React from 'react'
|
||||
import { useEffect, useId, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useModalContext } from '@/context/modal-context'
|
||||
import { useRouter } from '@/next/navigation'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import ExternalApiSelect from './ExternalApiSelect'
|
||||
|
||||
type ExternalApiSelectionProps = {
|
||||
external_knowledge_api_id: string
|
||||
external_knowledge_id: string
|
||||
onChange: (data: { external_knowledge_api_id?: string; external_knowledge_id?: string }) => void
|
||||
}
|
||||
|
||||
const ExternalApiSelection: React.FC<ExternalApiSelectionProps> = ({
|
||||
external_knowledge_api_id,
|
||||
external_knowledge_id,
|
||||
onChange,
|
||||
}) => {
|
||||
const { t } = useTranslation(['dataset'])
|
||||
const router = useRouter()
|
||||
const queryClient = useQueryClient()
|
||||
const externalKnowledgeApiQueryOptions =
|
||||
consoleQuery.datasets.externalKnowledgeApi.get.queryOptions({ input: {} })
|
||||
const { data } = useQuery(externalKnowledgeApiQueryOptions)
|
||||
const externalKnowledgeApiList = data?.data ?? []
|
||||
const externalKnowledgeApiLabelId = useId()
|
||||
const externalKnowledgeIdInputId = useId()
|
||||
const [selectedApiId, setSelectedApiId] = useState(external_knowledge_api_id)
|
||||
const { setShowExternalKnowledgeAPIModal } = useModalContext()
|
||||
|
||||
const apiItems = externalKnowledgeApiList.map((api) => ({
|
||||
value: api.id,
|
||||
name: api.name,
|
||||
url: api.settings && typeof api.settings.endpoint === 'string' ? api.settings.endpoint : '',
|
||||
}))
|
||||
|
||||
useEffect(() => {
|
||||
if (apiItems.length > 0) {
|
||||
const newSelectedId = external_knowledge_api_id || apiItems[0]!.value
|
||||
setSelectedApiId(newSelectedId)
|
||||
if (newSelectedId !== external_knowledge_api_id)
|
||||
onChange({ external_knowledge_api_id: newSelectedId, external_knowledge_id })
|
||||
}
|
||||
}, [apiItems, external_knowledge_api_id, external_knowledge_id, onChange])
|
||||
|
||||
const handleAddNewAPI = () => {
|
||||
setShowExternalKnowledgeAPIModal({
|
||||
payload: { name: '', settings: { endpoint: '', api_key: '' } },
|
||||
onSaveCallback: async () => {
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: externalKnowledgeApiQueryOptions.queryKey,
|
||||
})
|
||||
router.refresh()
|
||||
},
|
||||
isEditMode: false,
|
||||
})
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!external_knowledge_api_id && apiItems.length > 0)
|
||||
onChange({ external_knowledge_api_id: apiItems[0]!.value, external_knowledge_id })
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<form className="flex flex-col gap-4 self-stretch">
|
||||
<div className="flex flex-col gap-1 self-stretch">
|
||||
<div className="flex flex-col self-stretch">
|
||||
<span id={externalKnowledgeApiLabelId} className="system-sm-semibold text-text-secondary">
|
||||
{t(($) => $.externalAPIPanelTitle, { ns: 'dataset' })}
|
||||
</span>
|
||||
</div>
|
||||
{apiItems.length > 0 ? (
|
||||
<ExternalApiSelect
|
||||
aria-labelledby={externalKnowledgeApiLabelId}
|
||||
items={apiItems}
|
||||
value={selectedApiId}
|
||||
onSelect={(e) => {
|
||||
setSelectedApiId(e.value)
|
||||
onChange({ external_knowledge_api_id: e.value, external_knowledge_id })
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<Button variant="tertiary" onClick={handleAddNewAPI} className="justify-start">
|
||||
<RiAddLine className="size-4 text-text-tertiary" />
|
||||
<span className="system-sm-regular text-text-tertiary">
|
||||
{t(($) => $.noExternalKnowledge, { ns: 'dataset' })}
|
||||
</span>
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex flex-col gap-1 self-stretch">
|
||||
<div className="flex flex-col self-stretch">
|
||||
<label
|
||||
htmlFor={externalKnowledgeIdInputId}
|
||||
className="system-sm-semibold text-text-secondary"
|
||||
>
|
||||
{t(($) => $.externalKnowledgeId, { ns: 'dataset' })}
|
||||
</label>
|
||||
</div>
|
||||
<Input
|
||||
id={externalKnowledgeIdInputId}
|
||||
value={external_knowledge_id}
|
||||
onValueChange={(value) =>
|
||||
onChange({ external_knowledge_id: value, external_knowledge_api_id })
|
||||
}
|
||||
placeholder={t(($) => $.externalKnowledgeIdPlaceholder, { ns: 'dataset' })}
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
|
||||
export default ExternalApiSelection
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
import { RiBookOpenLine } from '@remixicon/react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useDocLink } from '@/context/i18n'
|
||||
|
||||
const InfoPanel = () => {
|
||||
const { t } = useTranslation(['dataset'])
|
||||
const docLink = useDocLink()
|
||||
|
||||
return (
|
||||
<div className="flex w-90 flex-col items-start pt-27 pr-8 pb-2">
|
||||
<div className="flex w-full min-w-60 flex-col items-start gap-3 self-stretch rounded-xl bg-background-section p-6">
|
||||
<div className="flex h-10 w-10 grow items-center justify-center gap-2 self-stretch rounded-lg border-[0.5px] border-components-card-border bg-components-card-bg p-1">
|
||||
<RiBookOpenLine className="size-5 text-text-accent" />
|
||||
</div>
|
||||
<p className="flex flex-col items-start gap-2 self-stretch">
|
||||
<span className="self-stretch system-xl-semibold text-text-secondary">
|
||||
{t(($) => $['connectDatasetIntro.title'], { ns: 'dataset' })}
|
||||
</span>
|
||||
<span className="system-sm-regular text-text-tertiary">
|
||||
{t(($) => $['connectDatasetIntro.content.front'], { ns: 'dataset' })}
|
||||
<a
|
||||
className="ml-1 system-sm-regular text-text-accent"
|
||||
href={docLink('/use-dify/knowledge/external-knowledge-api')}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
{t(($) => $['connectDatasetIntro.content.link'], { ns: 'dataset' })}
|
||||
</a>
|
||||
{t(($) => $['connectDatasetIntro.content.end'], { ns: 'dataset' })}
|
||||
</span>
|
||||
<a
|
||||
className="self-stretch system-sm-regular text-text-accent"
|
||||
href={docLink('/use-dify/knowledge/connect-external-knowledge-base')}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
{t(($) => $['connectDatasetIntro.learnMore'], { ns: 'dataset' })}
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default InfoPanel
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
import { Input } from '@langgenius/dify-ui/input'
|
||||
import * as React from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
type KnowledgeBaseInfoProps = {
|
||||
name: string
|
||||
description?: string
|
||||
onChange: (data: { name?: string; description?: string }) => void
|
||||
}
|
||||
|
||||
const KnowledgeBaseInfo: React.FC<KnowledgeBaseInfoProps> = ({ name, description, onChange }) => {
|
||||
const { t } = useTranslation(['dataset'])
|
||||
const nameInputId = React.useId()
|
||||
const descriptionInputId = React.useId()
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4 self-stretch">
|
||||
<div className="flex flex-col gap-4 self-stretch">
|
||||
<div className="flex flex-col gap-1 self-stretch">
|
||||
<div className="flex flex-col justify-center self-stretch">
|
||||
<label htmlFor={nameInputId} className="system-sm-semibold text-text-secondary">
|
||||
{t(($) => $.externalKnowledgeName, { ns: 'dataset' })}
|
||||
</label>
|
||||
</div>
|
||||
<Input
|
||||
id={nameInputId}
|
||||
value={name}
|
||||
onValueChange={(nextValue) => onChange({ name: nextValue })}
|
||||
placeholder={t(($) => $.externalKnowledgeNamePlaceholder, { ns: 'dataset' })}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex flex-col gap-1 self-stretch">
|
||||
<div className="flex flex-col justify-center self-stretch">
|
||||
<label htmlFor={descriptionInputId} className="system-sm-semibold text-text-secondary">
|
||||
{t(($) => $.externalKnowledgeDescription, { ns: 'dataset' })}
|
||||
</label>
|
||||
</div>
|
||||
<div className="flex flex-col gap-1 self-stretch">
|
||||
<textarea
|
||||
id={descriptionInputId}
|
||||
value={description}
|
||||
onChange={(e) => onChange({ description: e.target.value })}
|
||||
placeholder={t(($) => $.externalKnowledgeDescriptionPlaceholder, { ns: 'dataset' })}
|
||||
className={`flex h-20 items-start self-stretch rounded-lg bg-components-input-bg-normal p-3 py-2 ${description ? 'text-components-input-text-filled' : 'text-components-input-text-placeholder'} system-sm-regular`}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default KnowledgeBaseInfo
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
import type { FC } from 'react'
|
||||
import { cn } from '@langgenius/dify-ui/cn'
|
||||
import * as React from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import ScoreThresholdItem from '@/app/components/base/param-item/score-threshold-item'
|
||||
import TopKItem from '@/app/components/base/param-item/top-k-item'
|
||||
|
||||
type RetrievalSettingsProps = {
|
||||
topK: number
|
||||
scoreThreshold: number
|
||||
scoreThresholdEnabled: boolean
|
||||
isInHitTesting?: boolean
|
||||
isInRetrievalSetting?: boolean
|
||||
readonly?: boolean
|
||||
onChange: (data: {
|
||||
top_k?: number
|
||||
score_threshold?: number
|
||||
score_threshold_enabled?: boolean
|
||||
}) => void
|
||||
}
|
||||
|
||||
const RetrievalSettings: FC<RetrievalSettingsProps> = ({
|
||||
topK,
|
||||
scoreThreshold,
|
||||
scoreThresholdEnabled,
|
||||
onChange,
|
||||
isInHitTesting = false,
|
||||
isInRetrievalSetting = false,
|
||||
readonly = false,
|
||||
}) => {
|
||||
const { t } = useTranslation(['dataset'])
|
||||
|
||||
const handleScoreThresholdChange = (enabled: boolean) => {
|
||||
onChange({ score_threshold_enabled: enabled })
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn('flex flex-col gap-2 self-stretch', isInRetrievalSetting && 'w-full max-w-120')}
|
||||
>
|
||||
{!isInHitTesting && !isInRetrievalSetting && (
|
||||
<div className="flex h-7 flex-col gap-2 self-stretch pt-1">
|
||||
<label className="system-sm-semibold text-text-secondary">
|
||||
{t(($) => $.retrievalSettings, { ns: 'dataset' })}
|
||||
</label>
|
||||
</div>
|
||||
)}
|
||||
<div
|
||||
className={cn('flex gap-4 self-stretch', {
|
||||
'flex-col': isInHitTesting,
|
||||
'flex-row': isInRetrievalSetting,
|
||||
'flex-col sm:flex-row': !isInHitTesting && !isInRetrievalSetting,
|
||||
})}
|
||||
>
|
||||
<div className="flex grow flex-col gap-1">
|
||||
<TopKItem
|
||||
className="grow"
|
||||
value={topK}
|
||||
onChange={(_key, v) => onChange({ top_k: v })}
|
||||
enable={true}
|
||||
disabled={readonly}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex grow flex-col gap-1">
|
||||
<ScoreThresholdItem
|
||||
className="grow"
|
||||
value={scoreThreshold}
|
||||
onChange={(_key, v) => onChange({ score_threshold: v })}
|
||||
enable={scoreThresholdEnabled}
|
||||
hasSwitch={true}
|
||||
onSwitchChange={(_key, v) => handleScoreThresholdChange(v)}
|
||||
disabled={readonly}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default RetrievalSettings
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
import type { ExternalKnowledgeApiListResponse } from '@dify/contracts/api/console/datasets/types.gen'
|
||||
import { screen, waitFor, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { useState } from 'react'
|
||||
import { createExternalAPI } from '@/service/datasets'
|
||||
import { renderWithConfigurationModals as render } from '@/test/configuration-modals'
|
||||
import ExternalApiSelection from '../ExternalApiSelection'
|
||||
|
||||
const mocks = vi.hoisted(() => ({ getApis: vi.fn(), refresh: vi.fn() }))
|
||||
vi.mock('@/next/navigation', () => ({ useRouter: () => ({ refresh: mocks.refresh }) }))
|
||||
vi.mock('@/service/datasets', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('@/service/datasets')>()),
|
||||
createExternalAPI: 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, {
|
||||
'datasets.externalKnowledgeApi.get': mocks.getApis,
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
||||
const response: ExternalKnowledgeApiListResponse = {
|
||||
data: ['One', 'Two'].map((name, index) => ({
|
||||
id: `api-${index + 1}`,
|
||||
name: `API ${name}`,
|
||||
description: '',
|
||||
settings: { endpoint: `https://api${index + 1}.com` },
|
||||
created_at: '2026-01-01',
|
||||
created_by: 'user',
|
||||
tenant_id: 'workspace',
|
||||
dataset_bindings: [],
|
||||
})),
|
||||
has_more: false,
|
||||
page: 1,
|
||||
limit: 20,
|
||||
total: 2,
|
||||
}
|
||||
|
||||
function Harness() {
|
||||
const [value, setValue] = useState({ external_knowledge_api_id: '', external_knowledge_id: '' })
|
||||
return (
|
||||
<>
|
||||
<ExternalApiSelection
|
||||
{...value}
|
||||
onChange={(next) => {
|
||||
setValue((current) => ({ ...current, ...next }))
|
||||
}}
|
||||
/>
|
||||
<output aria-label="Knowledge connection">{JSON.stringify(value)}</output>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
mocks.getApis.mockResolvedValue(response)
|
||||
vi.mocked(createExternalAPI).mockResolvedValue(response.data[0]!)
|
||||
})
|
||||
|
||||
it('defaults to the first API and keeps the chosen API while entering the knowledge ID', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<Harness />)
|
||||
const selector = await screen.findByRole('button', { name: 'dataset.externalAPIPanelTitle' })
|
||||
await waitFor(() => expect(selector).toHaveTextContent('API One'))
|
||||
await user.click(selector)
|
||||
await user.click(screen.getByRole('button', { name: 'API Two https://api2.com' }))
|
||||
expect(selector).toHaveTextContent('API Two')
|
||||
expect(screen.queryByText('https://api2.com')).not.toBeInTheDocument()
|
||||
await user.type(screen.getByRole('textbox', { name: 'dataset.externalKnowledgeId' }), 'kb-123')
|
||||
expect(JSON.parse(screen.getByLabelText('Knowledge connection').textContent!)).toEqual({
|
||||
external_knowledge_api_id: 'api-2',
|
||||
external_knowledge_id: 'kb-123',
|
||||
})
|
||||
})
|
||||
|
||||
it.each(['empty list', 'selector'] as const)(
|
||||
'refreshes APIs after creation from the %s',
|
||||
async (entry) => {
|
||||
const user = userEvent.setup()
|
||||
mocks.getApis.mockResolvedValueOnce({
|
||||
...response,
|
||||
data: entry === 'empty list' ? [] : [response.data[1]!],
|
||||
total: entry === 'empty list' ? 0 : 1,
|
||||
})
|
||||
render(<Harness />)
|
||||
if (entry === 'selector') {
|
||||
await user.click(await screen.findByRole('button', { name: 'dataset.externalAPIPanelTitle' }))
|
||||
await user.click(screen.getByRole('button', { name: 'dataset.createNewExternalAPI' }))
|
||||
} else {
|
||||
await user.click(await screen.findByRole('button', { name: 'dataset.noExternalKnowledge' }))
|
||||
}
|
||||
const dialog = await screen.findByRole('dialog', { name: 'dataset.createExternalAPI' })
|
||||
await user.type(within(dialog).getByLabelText(/Name/), 'API One')
|
||||
await user.type(within(dialog).getByLabelText(/API Endpoint/), 'https://api1.com')
|
||||
await user.type(within(dialog).getByLabelText(/API Key/), 'secret-key')
|
||||
await user.click(within(dialog).getByRole('button', { name: 'dataset.externalAPIForm.save' }))
|
||||
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
|
||||
expect(createExternalAPI).toHaveBeenCalledWith({
|
||||
body: { name: 'API One', settings: { endpoint: 'https://api1.com', api_key: 'secret-key' } },
|
||||
})
|
||||
await waitFor(() => expect(mocks.getApis).toHaveBeenCalledTimes(2))
|
||||
expect(mocks.refresh).toHaveBeenCalledOnce()
|
||||
const selector = screen.getByRole('button', { name: 'dataset.externalAPIPanelTitle' })
|
||||
if (entry === 'empty list') await waitFor(() => expect(selector).toHaveTextContent('API One'))
|
||||
else {
|
||||
expect(selector).toHaveTextContent('API Two')
|
||||
expect(await screen.findByRole('button', { name: 'API One https://api1.com' })).toBeVisible()
|
||||
}
|
||||
},
|
||||
)
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import RetrievalSettings from '../RetrievalSettings'
|
||||
|
||||
const defaultProps = {
|
||||
topK: 3,
|
||||
scoreThreshold: 0.5,
|
||||
scoreThresholdEnabled: false,
|
||||
onChange: vi.fn(),
|
||||
}
|
||||
|
||||
describe('RetrievalSettings', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('updates top K', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<RetrievalSettings {...defaultProps} />)
|
||||
|
||||
await user.clear(screen.getByRole('textbox', { name: 'appDebug.datasetConfig.top_k' }))
|
||||
await user.type(screen.getByRole('textbox', { name: 'appDebug.datasetConfig.top_k' }), '8')
|
||||
|
||||
expect(defaultProps.onChange).toHaveBeenCalledWith({ top_k: 8 })
|
||||
})
|
||||
|
||||
it('updates the score threshold', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<RetrievalSettings {...defaultProps} scoreThresholdEnabled />)
|
||||
|
||||
await user.clear(
|
||||
screen.getByRole('textbox', { name: 'appDebug.datasetConfig.score_threshold' }),
|
||||
)
|
||||
await user.type(
|
||||
screen.getByRole('textbox', { name: 'appDebug.datasetConfig.score_threshold' }),
|
||||
'0.9',
|
||||
)
|
||||
|
||||
expect(defaultProps.onChange).toHaveBeenCalledWith({ score_threshold: 0.9 })
|
||||
})
|
||||
|
||||
it('enables score threshold filtering', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<RetrievalSettings {...defaultProps} />)
|
||||
|
||||
await user.click(screen.getByRole('switch', { name: 'appDebug.datasetConfig.score_threshold' }))
|
||||
|
||||
expect(defaultProps.onChange).toHaveBeenCalledWith({ score_threshold_enabled: true })
|
||||
})
|
||||
})
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
import type { ExternalKnowledgeApiResponse } from '@dify/contracts/api/console/datasets/types.gen'
|
||||
import { screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import { createConsoleQueryClient, renderWithConsoleQuery } from '@/test/console/query-data'
|
||||
import ExternalKnowledgeBaseCreate from '../index'
|
||||
|
||||
const { replace, showApiModal } = vi.hoisted(() => ({ replace: vi.fn(), showApiModal: vi.fn() }))
|
||||
vi.mock('@/next/navigation', () => ({ useRouter: () => ({ replace }) }))
|
||||
vi.mock('@/context/modal-context', () => ({
|
||||
useModalContext: () => ({ setShowExternalKnowledgeAPIModal: showApiModal }),
|
||||
}))
|
||||
const apis: ExternalKnowledgeApiResponse[] = ['First', 'Second'].map((name, index) => ({
|
||||
id: `api-${index + 1}`,
|
||||
tenant_id: 'tenant-1',
|
||||
name,
|
||||
description: '',
|
||||
settings: { endpoint: `https://api${index + 1}.example.com`, api_key: 'secret' },
|
||||
dataset_bindings: [],
|
||||
created_by: 'user-1',
|
||||
created_at: '2024-01-01T00:00:00Z',
|
||||
}))
|
||||
function setup({ loading = false, available = true } = {}) {
|
||||
const onConnect = vi.fn()
|
||||
const queryClient = createConsoleQueryClient()
|
||||
queryClient.setQueryData(
|
||||
consoleQuery.datasets.externalKnowledgeApi.get.queryOptions({ input: {} }).queryKey,
|
||||
{
|
||||
data: available ? apis : [],
|
||||
has_more: false,
|
||||
page: 1,
|
||||
limit: 20,
|
||||
total: available ? apis.length : 0,
|
||||
},
|
||||
)
|
||||
const view = renderWithConsoleQuery(
|
||||
<ExternalKnowledgeBaseCreate loading={loading} onConnect={onConnect} />,
|
||||
{ queryClient },
|
||||
)
|
||||
return { ...view, onConnect }
|
||||
}
|
||||
const connectButton = () =>
|
||||
screen.getByRole('button', { name: 'dataset.externalKnowledgeForm.connect' })
|
||||
async function fillForm(user: ReturnType<typeof userEvent.setup>, name = 'Knowledge', id = 'kb-1') {
|
||||
if (name)
|
||||
await user.type(screen.getByRole('textbox', { name: 'dataset.externalKnowledgeName' }), name)
|
||||
if (id) await user.type(screen.getByPlaceholderText('dataset.externalKnowledgeIdPlaceholder'), id)
|
||||
}
|
||||
beforeEach(() => vi.clearAllMocks())
|
||||
|
||||
it('submits the first available API with default retrieval settings and an optional empty description', async () => {
|
||||
const user = userEvent.setup()
|
||||
const { onConnect } = setup()
|
||||
await fillForm(user)
|
||||
await user.click(connectButton())
|
||||
expect(onConnect).toHaveBeenCalledExactlyOnceWith({
|
||||
name: 'Knowledge',
|
||||
description: '',
|
||||
provider: 'external',
|
||||
external_knowledge_id: 'kb-1',
|
||||
external_knowledge_api_id: 'api-1',
|
||||
external_retrieval_model: { top_k: 4, score_threshold: 0.5, score_threshold_enabled: false },
|
||||
})
|
||||
})
|
||||
|
||||
it('submits the selected API and merges changes to all retrieval fields', async () => {
|
||||
const user = userEvent.setup()
|
||||
const { onConnect } = setup()
|
||||
await fillForm(user, 'Custom knowledge', 'remote-id')
|
||||
await user.type(
|
||||
screen.getByRole('textbox', { name: 'dataset.externalKnowledgeDescription' }),
|
||||
'Reference documents',
|
||||
)
|
||||
await user.click(await screen.findByText('First'))
|
||||
await user.click(screen.getByText('Second'))
|
||||
await user.click(screen.getByRole('textbox', { name: 'appDebug.datasetConfig.top_k' }))
|
||||
await user.keyboard('{ArrowUp}')
|
||||
await user.click(screen.getByRole('switch', { name: 'appDebug.datasetConfig.score_threshold' }))
|
||||
await user.click(screen.getByRole('textbox', { name: 'appDebug.datasetConfig.score_threshold' }))
|
||||
await user.keyboard('{ArrowUp}')
|
||||
await user.click(connectButton())
|
||||
expect(onConnect).toHaveBeenCalledExactlyOnceWith({
|
||||
name: 'Custom knowledge',
|
||||
description: 'Reference documents',
|
||||
provider: 'external',
|
||||
external_knowledge_id: 'remote-id',
|
||||
external_knowledge_api_id: 'api-2',
|
||||
external_retrieval_model: { top_k: 5, score_threshold: 0.51, score_threshold_enabled: true },
|
||||
})
|
||||
})
|
||||
|
||||
it.each([
|
||||
{ name: '', id: 'kb-1', available: true },
|
||||
{ name: ' ', id: 'kb-1', available: true },
|
||||
{ name: 'Knowledge', id: '', available: true },
|
||||
{ name: 'Knowledge', id: 'kb-1', available: false },
|
||||
])('prevents submission with incomplete required fields: %j', async ({ name, id, available }) => {
|
||||
const user = userEvent.setup()
|
||||
const { onConnect } = setup({ available })
|
||||
await fillForm(user, name, id)
|
||||
expect(connectButton()).toBeDisabled()
|
||||
await user.click(connectButton())
|
||||
expect(onConnect).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('blocks duplicate connections while loading and allows submission after loading ends', async () => {
|
||||
const user = userEvent.setup()
|
||||
const { onConnect, rerender } = setup({ loading: true })
|
||||
await fillForm(user)
|
||||
await user.click(connectButton())
|
||||
expect(onConnect).not.toHaveBeenCalled()
|
||||
rerender(<ExternalKnowledgeBaseCreate loading={false} onConnect={onConnect} />)
|
||||
await user.click(connectButton())
|
||||
expect(onConnect).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('navigates back and links cancel to the dataset list', async () => {
|
||||
const user = userEvent.setup()
|
||||
const { onConnect } = setup()
|
||||
expect(
|
||||
screen.getByRole('link', { name: 'dataset.externalKnowledgeForm.cancel' }),
|
||||
).toHaveAttribute('href', '/datasets')
|
||||
await user.click(screen.getByRole('button', { name: 'common.operation.back' }))
|
||||
expect(replace).toHaveBeenCalledExactlyOnceWith('/datasets')
|
||||
expect(onConnect).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('offers API creation when there are no APIs to connect', async () => {
|
||||
const user = userEvent.setup()
|
||||
setup({ available: false })
|
||||
await user.click(screen.getByRole('button', { name: 'dataset.noExternalKnowledge' }))
|
||||
await waitFor(() =>
|
||||
expect(showApiModal).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
isEditMode: false,
|
||||
payload: { name: '', settings: { endpoint: '', api_key: '' } },
|
||||
}),
|
||||
),
|
||||
)
|
||||
})
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
export type CreateKnowledgeBaseReq = {
|
||||
name: string
|
||||
description?: string
|
||||
external_knowledge_api_id: string
|
||||
provider: 'external'
|
||||
external_knowledge_id: string
|
||||
external_retrieval_model: {
|
||||
top_k: number
|
||||
score_threshold: number
|
||||
score_threshold_enabled: boolean
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
'use client'
|
||||
|
||||
import type { CreateKnowledgeBaseReq } from './declarations'
|
||||
import { Button, buttonVariants } from '@langgenius/dify-ui/button'
|
||||
import { IconButton } from '@langgenius/dify-ui/icon-button'
|
||||
import { Separator } from '@langgenius/dify-ui/separator'
|
||||
import { useCallback, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useDocLink } from '@/context/i18n'
|
||||
import Link from '@/next/link'
|
||||
import { useRouter } from '@/next/navigation'
|
||||
import ExternalApiSelection from './ExternalApiSelection'
|
||||
import InfoPanel from './InfoPanel'
|
||||
import KnowledgeBaseInfo from './KnowledgeBaseInfo'
|
||||
import RetrievalSettings from './RetrievalSettings'
|
||||
|
||||
type ExternalKnowledgeBaseCreateProps = {
|
||||
onConnect: (formValue: CreateKnowledgeBaseReq) => void
|
||||
loading: boolean
|
||||
}
|
||||
|
||||
const ExternalKnowledgeBaseCreate: React.FC<ExternalKnowledgeBaseCreateProps> = ({
|
||||
onConnect,
|
||||
loading,
|
||||
}) => {
|
||||
const { t } = useTranslation(['common', 'dataset'])
|
||||
const docLink = useDocLink()
|
||||
const router = useRouter()
|
||||
const [formData, setFormData] = useState<CreateKnowledgeBaseReq>({
|
||||
name: '',
|
||||
description: '',
|
||||
external_knowledge_api_id: '',
|
||||
external_knowledge_id: '',
|
||||
external_retrieval_model: {
|
||||
top_k: 4,
|
||||
score_threshold: 0.5,
|
||||
score_threshold_enabled: false,
|
||||
},
|
||||
provider: 'external',
|
||||
})
|
||||
|
||||
const navBackHandle = useCallback(() => {
|
||||
router.replace('/datasets')
|
||||
}, [router])
|
||||
|
||||
const handleFormChange = (newData: CreateKnowledgeBaseReq) => {
|
||||
setFormData(newData)
|
||||
}
|
||||
|
||||
const isFormValid =
|
||||
formData.name.trim() !== '' &&
|
||||
formData.external_knowledge_api_id !== '' &&
|
||||
formData.external_knowledge_id !== '' &&
|
||||
formData.external_retrieval_model.top_k !== undefined &&
|
||||
formData.external_retrieval_model.score_threshold !== undefined
|
||||
|
||||
return (
|
||||
<div className="flex grow flex-col self-stretch rounded-t-2xl border-t border-effects-highlight bg-components-panel-bg">
|
||||
<div className="flex grow justify-center self-stretch">
|
||||
<div className="flex w-full max-w-240 flex-col items-center px-14 py-0">
|
||||
<div className="flex w-full max-w-160 grow flex-col items-center gap-4 pt-6 pb-8">
|
||||
<div className="relative flex flex-col items-center gap-0.5 self-stretch py-2">
|
||||
<div className="grow self-stretch system-xl-semibold text-text-primary">
|
||||
{t(($) => $.connectDataset, { ns: 'dataset' })}
|
||||
</div>
|
||||
<p className="system-sm-regular text-text-tertiary">
|
||||
<span>{t(($) => $['connectHelper.helper1'], { ns: 'dataset' })}</span>
|
||||
<span className="system-sm-medium text-text-secondary">
|
||||
{t(($) => $['connectHelper.helper2'], { ns: 'dataset' })}
|
||||
</span>
|
||||
<span>{t(($) => $['connectHelper.helper3'], { ns: 'dataset' })}</span>
|
||||
<a
|
||||
className="self-stretch system-sm-regular text-text-accent"
|
||||
href={docLink('/use-dify/knowledge/connect-external-knowledge-base')}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
{t(($) => $['connectHelper.helper4'], { ns: 'dataset' })}
|
||||
</a>
|
||||
<span>{t(($) => $['connectHelper.helper5'], { ns: 'dataset' })} </span>
|
||||
</p>
|
||||
<IconButton
|
||||
aria-label={t(($) => $['operation.back'], { ns: 'common' })}
|
||||
className="absolute top-1 -left-11 rounded-full"
|
||||
variant="tertiary"
|
||||
size="lg"
|
||||
onClick={navBackHandle}
|
||||
>
|
||||
<span aria-hidden className="i-ri-arrow-left-line size-4 text-text-tertiary" />
|
||||
</IconButton>
|
||||
</div>
|
||||
<KnowledgeBaseInfo
|
||||
name={formData.name}
|
||||
description={formData.description ?? ''}
|
||||
onChange={(data) =>
|
||||
handleFormChange({
|
||||
...formData,
|
||||
...data,
|
||||
})
|
||||
}
|
||||
/>
|
||||
<Separator className="my-2 h-[0.5px]" />
|
||||
<ExternalApiSelection
|
||||
external_knowledge_api_id={formData.external_knowledge_api_id}
|
||||
external_knowledge_id={formData.external_knowledge_id}
|
||||
onChange={(data) =>
|
||||
handleFormChange({
|
||||
...formData,
|
||||
...data,
|
||||
})
|
||||
}
|
||||
/>
|
||||
<RetrievalSettings
|
||||
topK={formData.external_retrieval_model.top_k}
|
||||
scoreThreshold={formData.external_retrieval_model.score_threshold}
|
||||
scoreThresholdEnabled={formData.external_retrieval_model.score_threshold_enabled}
|
||||
onChange={(data) =>
|
||||
handleFormChange({
|
||||
...formData,
|
||||
external_retrieval_model: {
|
||||
...formData.external_retrieval_model,
|
||||
...data,
|
||||
},
|
||||
})
|
||||
}
|
||||
/>
|
||||
<div className="flex items-center justify-end gap-2 self-stretch py-2">
|
||||
<Link href="/datasets" replace className={buttonVariants({ variant: 'secondary' })}>
|
||||
<div className="system-sm-medium text-components-button-secondary-text">
|
||||
{t(($) => $['externalKnowledgeForm.cancel'], { ns: 'dataset' })}
|
||||
</div>
|
||||
</Link>
|
||||
<Button
|
||||
variant="primary"
|
||||
onClick={() => {
|
||||
onConnect(formData)
|
||||
}}
|
||||
disabled={!isFormValid}
|
||||
loading={loading}
|
||||
>
|
||||
<div className="system-sm-medium text-components-button-primary-text">
|
||||
{t(($) => $['externalKnowledgeForm.connect'], { ns: 'dataset' })}
|
||||
</div>
|
||||
<span
|
||||
aria-hidden
|
||||
className="i-ri-arrow-right-line size-4 text-components-button-primary-text"
|
||||
/>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<InfoPanel />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ExternalKnowledgeBaseCreate
|
||||
Reference in new issue
Block a user