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
+250
@@ -0,0 +1,250 @@
|
||||
import type { ExternalKnowledgeApiResponse } from '@dify/contracts/api/console/datasets/types.gen'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { act, screen, waitFor, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import {
|
||||
checkUsageExternalAPI,
|
||||
deleteExternalAPI,
|
||||
fetchExternalAPI,
|
||||
updateExternalAPI,
|
||||
} from '@/service/datasets'
|
||||
import { renderWithConfigurationModals } from '@/test/configuration-modals'
|
||||
import { createConsoleQueryClient } from '@/test/console/query-data'
|
||||
import { createDeferred } from '@/test/deferred'
|
||||
import ExternalKnowledgeAPICard from '../index'
|
||||
|
||||
vi.mock('@/service/datasets', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('@/service/datasets')>()),
|
||||
fetchExternalAPI: vi.fn(),
|
||||
updateExternalAPI: vi.fn(),
|
||||
deleteExternalAPI: vi.fn(),
|
||||
checkUsageExternalAPI: vi.fn(),
|
||||
}))
|
||||
const { getApis } = vi.hoisted(() => ({ getApis: 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': getApis,
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
||||
const api = {
|
||||
id: 'api-123',
|
||||
tenant_id: 'tenant-1',
|
||||
name: 'Test External API',
|
||||
description: 'Test API description',
|
||||
settings: { endpoint: 'https://api.example.com/knowledge', api_key: 'secret-key-123' },
|
||||
dataset_bindings: [],
|
||||
created_by: 'user-1',
|
||||
created_at: '2021-01-01T00:00:00Z',
|
||||
} satisfies ExternalKnowledgeApiResponse
|
||||
const listResponse = (data: ExternalKnowledgeApiResponse[]) => ({
|
||||
data,
|
||||
has_more: false,
|
||||
page: 1,
|
||||
limit: 20,
|
||||
total: data.length,
|
||||
})
|
||||
const listOptions = consoleQuery.datasets.externalKnowledgeApi.get.queryOptions({ input: {} })
|
||||
function setup({ apis = [api], canManage = true } = {}) {
|
||||
const queryClient = createConsoleQueryClient()
|
||||
queryClient.setQueryData(listOptions.queryKey, listResponse(apis))
|
||||
function ApiCards() {
|
||||
const { data } = useQuery(listOptions)
|
||||
return data?.data.map((item, index) => (
|
||||
<ExternalKnowledgeAPICard
|
||||
key={item.id}
|
||||
api={item}
|
||||
position={index + 1}
|
||||
canManageExternalKnowledgeApi={canManage}
|
||||
/>
|
||||
))
|
||||
}
|
||||
return renderWithConfigurationModals(<ApiCards />, { queryClient })
|
||||
}
|
||||
const edit = () =>
|
||||
screen.getByRole('button', {
|
||||
name: `common.operation.edit ${api.name} ${api.settings.endpoint} 1`,
|
||||
})
|
||||
const remove = () =>
|
||||
screen.getByRole('button', {
|
||||
name: `common.operation.delete ${api.name} ${api.settings.endpoint} 1`,
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
vi.mocked(fetchExternalAPI).mockReset().mockResolvedValue(api)
|
||||
vi.mocked(updateExternalAPI).mockReset().mockResolvedValue(api)
|
||||
vi.mocked(deleteExternalAPI).mockReset().mockResolvedValue({ result: 'success' })
|
||||
vi.mocked(checkUsageExternalAPI).mockReset().mockResolvedValue({ is_using: false, count: 0 })
|
||||
getApis.mockReset().mockResolvedValue(listResponse([api]))
|
||||
})
|
||||
|
||||
it('targets the correct API when cards have duplicate names and endpoints', async () => {
|
||||
const user = userEvent.setup()
|
||||
setup({ apis: [api, { ...api, id: 'api-456' }] })
|
||||
await user.click(
|
||||
screen.getByRole('button', {
|
||||
name: `common.operation.delete ${api.name} ${api.settings.endpoint} 2`,
|
||||
}),
|
||||
)
|
||||
expect(checkUsageExternalAPI).toHaveBeenCalledWith({ apiTemplateId: 'api-456' })
|
||||
expect(await screen.findByRole('alertdialog')).toBeVisible()
|
||||
})
|
||||
|
||||
it('hides management actions without permission', () => {
|
||||
setup({ canManage: false })
|
||||
expect(screen.getByText(api.name)).toBeVisible()
|
||||
expect(
|
||||
screen.queryByRole('button', { name: /common.operation.(edit|delete)/ }),
|
||||
).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('loads API details and bindings into the real edit dialog', async () => {
|
||||
const user = userEvent.setup()
|
||||
vi.mocked(fetchExternalAPI).mockResolvedValue({
|
||||
...api,
|
||||
dataset_bindings: [{ id: 'ds-1', name: 'Dataset 1' }],
|
||||
})
|
||||
setup()
|
||||
await user.click(edit())
|
||||
const dialog = await screen.findByRole('dialog', { name: 'dataset.editExternalAPIFormTitle' })
|
||||
expect(fetchExternalAPI).toHaveBeenCalledWith({ apiTemplateId: api.id })
|
||||
expect(within(dialog).getByLabelText(/Name/)).toHaveValue(api.name)
|
||||
expect(within(dialog).getByLabelText(/API Endpoint/)).toHaveValue(api.settings.endpoint)
|
||||
expect(within(dialog).getByLabelText(/API Key/)).toHaveValue(api.settings.api_key)
|
||||
expect(dialog).toHaveTextContent('1 dataset.editExternalAPIFormWarning.end')
|
||||
})
|
||||
|
||||
it('saves edits through the modal and refreshes the displayed card', async () => {
|
||||
const user = userEvent.setup()
|
||||
const updated = {
|
||||
...api,
|
||||
name: 'Updated External API',
|
||||
settings: { endpoint: 'https://updated.example.com/knowledge', api_key: 'updated-secret-key' },
|
||||
}
|
||||
getApis.mockResolvedValue(listResponse([updated]))
|
||||
setup()
|
||||
await user.click(edit())
|
||||
const dialog = await screen.findByRole('dialog', { name: 'dataset.editExternalAPIFormTitle' })
|
||||
for (const [label, value] of [
|
||||
[/Name/, updated.name],
|
||||
[/API Endpoint/, updated.settings.endpoint],
|
||||
[/API Key/, updated.settings.api_key],
|
||||
] as const) {
|
||||
await user.clear(within(dialog).getByLabelText(label))
|
||||
await user.type(within(dialog).getByLabelText(label), value)
|
||||
}
|
||||
await user.click(within(dialog).getByRole('button', { name: 'dataset.externalAPIForm.save' }))
|
||||
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
|
||||
expect(updateExternalAPI).toHaveBeenCalledWith({
|
||||
apiTemplateId: api.id,
|
||||
body: { name: updated.name, settings: updated.settings },
|
||||
})
|
||||
expect(await screen.findByText(updated.name)).toBeVisible()
|
||||
expect(screen.getByText(updated.settings.endpoint)).toBeVisible()
|
||||
expect(screen.queryByText(api.name)).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('shows the usage count before deleting a connected API', async () => {
|
||||
const user = userEvent.setup()
|
||||
vi.mocked(checkUsageExternalAPI).mockResolvedValue({ is_using: true, count: 3 })
|
||||
setup()
|
||||
await user.click(remove())
|
||||
expect(await screen.findByRole('alertdialog')).toHaveTextContent('3')
|
||||
expect(deleteExternalAPI).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('deletes a confirmed API and removes its card after refreshing the list', async () => {
|
||||
const user = userEvent.setup()
|
||||
getApis.mockResolvedValue(listResponse([]))
|
||||
setup()
|
||||
await user.click(remove())
|
||||
await user.click(
|
||||
within(await screen.findByRole('alertdialog')).getByRole('button', {
|
||||
name: 'common.operation.confirm',
|
||||
}),
|
||||
)
|
||||
await waitFor(() => expect(screen.queryByText(api.name)).not.toBeInTheDocument())
|
||||
expect(deleteExternalAPI).toHaveBeenCalledWith({ apiTemplateId: api.id })
|
||||
expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('keeps deletion pending in the dialog and prevents duplicate requests', async () => {
|
||||
const user = userEvent.setup()
|
||||
const request = createDeferred<{ result: string }>()
|
||||
vi.mocked(deleteExternalAPI).mockReturnValueOnce(request.promise)
|
||||
getApis.mockResolvedValue(listResponse([]))
|
||||
setup()
|
||||
await user.click(remove())
|
||||
const dialog = await screen.findByRole('alertdialog')
|
||||
const confirm = within(dialog).getByRole('button', { name: 'common.operation.confirm' })
|
||||
const cancel = within(dialog).getByRole('button', { name: 'common.operation.cancel' })
|
||||
|
||||
await user.click(confirm)
|
||||
expect(confirm).toHaveAttribute('aria-disabled', 'true')
|
||||
expect(cancel).toBeDisabled()
|
||||
expect(deleteExternalAPI).toHaveBeenCalledTimes(1)
|
||||
|
||||
await user.click(confirm)
|
||||
await user.keyboard('{Escape}')
|
||||
expect(screen.getByRole('alertdialog')).toBeVisible()
|
||||
expect(deleteExternalAPI).toHaveBeenCalledTimes(1)
|
||||
|
||||
await act(async () => request.resolve({ result: 'success' }))
|
||||
await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument())
|
||||
expect(screen.queryByText(api.name)).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('cancels deletion without changing the list', async () => {
|
||||
const user = userEvent.setup()
|
||||
setup()
|
||||
await user.click(remove())
|
||||
await user.click(
|
||||
within(await screen.findByRole('alertdialog')).getByRole('button', {
|
||||
name: 'common.operation.cancel',
|
||||
}),
|
||||
)
|
||||
await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument())
|
||||
expect(screen.getByText(api.name)).toBeVisible()
|
||||
expect(deleteExternalAPI).not.toHaveBeenCalled()
|
||||
expect(getApis).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it.each(['fetch', 'usage', 'delete'] as const)(
|
||||
'preserves the card when the %s request fails',
|
||||
async (operation) => {
|
||||
const user = userEvent.setup()
|
||||
const failure = new Error('Request failed')
|
||||
const service =
|
||||
operation === 'fetch'
|
||||
? fetchExternalAPI
|
||||
: operation === 'usage'
|
||||
? checkUsageExternalAPI
|
||||
: deleteExternalAPI
|
||||
const request = createDeferred<never>()
|
||||
vi.mocked(service).mockReturnValueOnce(request.promise)
|
||||
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
try {
|
||||
setup()
|
||||
await user.click(operation === 'fetch' ? edit() : remove())
|
||||
if (operation === 'delete')
|
||||
await user.click(
|
||||
within(await screen.findByRole('alertdialog')).getByRole('button', {
|
||||
name: 'common.operation.confirm',
|
||||
}),
|
||||
)
|
||||
await act(async () => request.reject(failure))
|
||||
expect(screen.getByText(api.name)).toBeVisible()
|
||||
expect(getApis).not.toHaveBeenCalled()
|
||||
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
|
||||
if (operation !== 'delete') expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()
|
||||
} finally {
|
||||
consoleError.mockRestore()
|
||||
}
|
||||
},
|
||||
)
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
import type { ExternalKnowledgeApiResponse } from '@dify/contracts/api/console/datasets/types.gen'
|
||||
import type { CreateExternalAPIReq } from '../declarations'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogCancelButton,
|
||||
AlertDialogConfirmButton,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@langgenius/dify-ui/alert-dialog'
|
||||
import { IconButton } from '@langgenius/dify-ui/icon-button'
|
||||
import { useQueryClient } from '@tanstack/react-query'
|
||||
import * as React from 'react'
|
||||
import { useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useModalContext } from '@/context/modal-context'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import {
|
||||
checkUsageExternalAPI,
|
||||
deleteExternalAPI,
|
||||
fetchExternalAPI,
|
||||
updateExternalAPI,
|
||||
} from '@/service/datasets'
|
||||
|
||||
type ExternalKnowledgeAPICardProps = {
|
||||
api: ExternalKnowledgeApiResponse
|
||||
canManageExternalKnowledgeApi: boolean
|
||||
position: number
|
||||
}
|
||||
|
||||
const ExternalKnowledgeAPICard: React.FC<ExternalKnowledgeAPICardProps> = ({
|
||||
api,
|
||||
canManageExternalKnowledgeApi,
|
||||
position,
|
||||
}) => {
|
||||
const { setShowExternalKnowledgeAPIModal } = useModalContext()
|
||||
const [showConfirm, setShowConfirm] = useState(false)
|
||||
const [isDeleting, setIsDeleting] = useState(false)
|
||||
const [isHovered, setIsHovered] = useState(false)
|
||||
const [usageCount, setUsageCount] = useState(0)
|
||||
const queryClient = useQueryClient()
|
||||
const externalKnowledgeApiQueryKey = consoleQuery.datasets.externalKnowledgeApi.get.queryOptions({
|
||||
input: {},
|
||||
}).queryKey
|
||||
const endpoint =
|
||||
api.settings && typeof api.settings.endpoint === 'string' ? api.settings.endpoint : ''
|
||||
|
||||
const { t } = useTranslation(['common', 'dataset'])
|
||||
|
||||
const handleEditClick = async () => {
|
||||
if (!canManageExternalKnowledgeApi) return
|
||||
|
||||
try {
|
||||
const response = await fetchExternalAPI({ apiTemplateId: api.id })
|
||||
const formValue: CreateExternalAPIReq = {
|
||||
name: response.name,
|
||||
settings: {
|
||||
endpoint: response.settings?.endpoint ?? '',
|
||||
api_key: response.settings?.api_key ?? '',
|
||||
},
|
||||
}
|
||||
|
||||
setShowExternalKnowledgeAPIModal({
|
||||
payload: formValue,
|
||||
isEditMode: true,
|
||||
datasetBindings: response.dataset_bindings,
|
||||
onEditCallback: async (updatedData: CreateExternalAPIReq) => {
|
||||
try {
|
||||
await updateExternalAPI({
|
||||
apiTemplateId: api.id,
|
||||
body: { name: updatedData.name, settings: updatedData.settings },
|
||||
})
|
||||
await queryClient.invalidateQueries({ queryKey: externalKnowledgeApiQueryKey })
|
||||
} catch (error) {
|
||||
console.error('Error updating external knowledge API:', error)
|
||||
}
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error fetching external knowledge API data:', error)
|
||||
}
|
||||
}
|
||||
|
||||
const handleDeleteClick = async () => {
|
||||
if (!canManageExternalKnowledgeApi) return
|
||||
|
||||
try {
|
||||
const usage = await checkUsageExternalAPI({ apiTemplateId: api.id })
|
||||
if (usage.is_using) setUsageCount(usage.count)
|
||||
|
||||
setShowConfirm(true)
|
||||
} catch (error) {
|
||||
console.error('Error checking external API usage:', error)
|
||||
}
|
||||
}
|
||||
|
||||
const handleConfirmDelete = async () => {
|
||||
if (!canManageExternalKnowledgeApi || isDeleting) return
|
||||
|
||||
setIsDeleting(true)
|
||||
try {
|
||||
await deleteExternalAPI({ apiTemplateId: api.id })
|
||||
setShowConfirm(false)
|
||||
await queryClient.invalidateQueries({ queryKey: externalKnowledgeApiQueryKey })
|
||||
} catch (error) {
|
||||
console.error('Error deleting external knowledge API:', error)
|
||||
} finally {
|
||||
setIsDeleting(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div
|
||||
className={`flex items-start self-stretch rounded-lg border-[0.5px] border-components-panel-border-subtle bg-components-panel-on-panel-item-bg p-2 ps-3 ${isHovered ? 'border-state-destructive-border bg-state-destructive-hover' : ''}`}
|
||||
>
|
||||
<div className="flex grow flex-col items-start justify-center gap-1.5 py-1">
|
||||
<div className="flex items-center gap-1 self-stretch text-text-secondary">
|
||||
<span
|
||||
aria-hidden
|
||||
className="i-custom-vender-solid-development-api-connection-mod size-4"
|
||||
/>
|
||||
<div className="system-sm-medium">{api.name}</div>
|
||||
</div>
|
||||
<div className="self-stretch system-xs-regular text-text-tertiary">{endpoint}</div>
|
||||
</div>
|
||||
{canManageExternalKnowledgeApi && (
|
||||
<div className="flex items-start gap-1">
|
||||
<IconButton
|
||||
aria-label={`${t(($) => $['operation.edit'], { ns: 'common' })} ${api.name} ${endpoint} ${position}`}
|
||||
onClick={handleEditClick}
|
||||
>
|
||||
<span
|
||||
aria-hidden
|
||||
className="i-ri-edit-line size-4 text-text-tertiary hover:text-text-secondary"
|
||||
/>
|
||||
</IconButton>
|
||||
<IconButton
|
||||
aria-label={`${t(($) => $['operation.delete'], { ns: 'common' })} ${api.name} ${endpoint} ${position}`}
|
||||
tone="destructive"
|
||||
onClick={handleDeleteClick}
|
||||
onMouseEnter={() => setIsHovered(true)}
|
||||
onMouseLeave={() => setIsHovered(false)}
|
||||
>
|
||||
<span aria-hidden className="i-ri-delete-bin-line size-4" />
|
||||
</IconButton>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<AlertDialog
|
||||
open={showConfirm}
|
||||
onOpenChange={(open, details) => {
|
||||
if (!open && isDeleting) details.cancel()
|
||||
else setShowConfirm(open)
|
||||
}}
|
||||
>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>
|
||||
{`${t(($) => $['deleteExternalAPIConfirmWarningContent.title.front'], { ns: 'dataset' })} ${api.name}${t(($) => $['deleteExternalAPIConfirmWarningContent.title.end'], { ns: 'dataset' })}`}
|
||||
</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
{usageCount > 0
|
||||
? `${t(($) => $['deleteExternalAPIConfirmWarningContent.content.front'], { ns: 'dataset' })} ${usageCount} ${t(($) => $['deleteExternalAPIConfirmWarningContent.content.end'], { ns: 'dataset' })}`
|
||||
: t(($) => $['deleteExternalAPIConfirmWarningContent.noConnectionContent'], {
|
||||
ns: 'dataset',
|
||||
})}
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancelButton disabled={isDeleting}>
|
||||
{t(($) => $['operation.cancel'], { ns: 'common' })}
|
||||
</AlertDialogCancelButton>
|
||||
<AlertDialogConfirmButton loading={isDeleting} onClick={handleConfirmDelete}>
|
||||
{t(($) => $['operation.confirm'], { ns: 'common' })}
|
||||
</AlertDialogConfirmButton>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default ExternalKnowledgeAPICard
|
||||
Reference in new issue
Block a user