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
+109
@@ -0,0 +1,109 @@
|
||||
import type { ExternalKnowledgeApiListResponse } from '@dify/contracts/api/console/datasets/types.gen'
|
||||
import { act, screen, waitFor, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vite-plus/test'
|
||||
import { createExternalAPI } from '@/service/datasets'
|
||||
import { renderWithConfigurationModals as render } from '@/test/configuration-modals'
|
||||
import ExternalAPIPanel from '../index'
|
||||
|
||||
const { getApis } = vi.hoisted(() => ({ getApis: vi.fn() }))
|
||||
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': getApis,
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
||||
const response: ExternalKnowledgeApiListResponse = {
|
||||
data: [
|
||||
{
|
||||
id: 'api-1',
|
||||
tenant_id: 'tenant-1',
|
||||
name: 'Product knowledge',
|
||||
description: '',
|
||||
settings: { endpoint: 'https://knowledge.example.com', api_key: 'key' },
|
||||
dataset_bindings: [],
|
||||
created_by: 'user-1',
|
||||
created_at: '2021-01-01T00:00:00Z',
|
||||
},
|
||||
],
|
||||
has_more: false,
|
||||
limit: 20,
|
||||
page: 1,
|
||||
total: 1,
|
||||
}
|
||||
|
||||
describe('ExternalAPIPanel', () => {
|
||||
beforeEach(() => {
|
||||
getApis.mockResolvedValue(response)
|
||||
vi.mocked(createExternalAPI).mockResolvedValue(response.data[0]!)
|
||||
})
|
||||
|
||||
it('replaces loading with API cards and allows the panel to close', async () => {
|
||||
const user = userEvent.setup()
|
||||
let resolve!: (value: ExternalKnowledgeApiListResponse) => void
|
||||
getApis.mockReturnValue(
|
||||
new Promise<ExternalKnowledgeApiListResponse>((done) => {
|
||||
resolve = done
|
||||
}),
|
||||
)
|
||||
const onClose = vi.fn()
|
||||
render(<ExternalAPIPanel canManageExternalKnowledgeApi onClose={onClose} />)
|
||||
expect(screen.getByLabelText('common.loading')).toBeInTheDocument()
|
||||
await act(async () => resolve(response))
|
||||
expect(await screen.findByText('Product knowledge')).toBeInTheDocument()
|
||||
expect(screen.getByText('https://knowledge.example.com')).toBeInTheDocument()
|
||||
expect(screen.queryByLabelText('common.loading')).not.toBeInTheDocument()
|
||||
await user.click(screen.getByRole('button', { name: 'common.operation.close' }))
|
||||
expect(onClose).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('shows read-only API cards when management is unavailable', async () => {
|
||||
render(<ExternalAPIPanel canManageExternalKnowledgeApi={false} onClose={vi.fn()} />)
|
||||
expect(await screen.findByText('Product knowledge')).toBeInTheDocument()
|
||||
expect(
|
||||
screen.queryByRole('button', { name: 'dataset.createExternalAPI' }),
|
||||
).not.toBeInTheDocument()
|
||||
expect(screen.queryByRole('button', { name: /common.operation.edit/ })).not.toBeInTheDocument()
|
||||
expect(
|
||||
screen.queryByRole('button', { name: /common.operation.delete/ }),
|
||||
).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('opens a blank creation form and refreshes the actual list after save', async () => {
|
||||
const user = userEvent.setup()
|
||||
getApis.mockResolvedValueOnce({ ...response, data: [], total: 0 })
|
||||
render(<ExternalAPIPanel canManageExternalKnowledgeApi onClose={vi.fn()} />)
|
||||
await user.click(screen.getByRole('button', { name: 'dataset.createExternalAPI' }))
|
||||
const dialog = await screen.findByRole('dialog', { name: 'dataset.createExternalAPI' })
|
||||
expect(within(dialog).getByLabelText(/Name/)).toHaveValue('')
|
||||
expect(within(dialog).getByLabelText(/API Endpoint/)).toHaveValue('')
|
||||
expect(within(dialog).getByLabelText(/API Key/)).toHaveValue('')
|
||||
await user.type(within(dialog).getByLabelText(/Name/), 'Product knowledge')
|
||||
await user.type(within(dialog).getByLabelText(/API Endpoint/), 'https://knowledge.example.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: 'Product knowledge',
|
||||
settings: { endpoint: 'https://knowledge.example.com', api_key: 'secret-key' },
|
||||
},
|
||||
})
|
||||
expect(await screen.findByText('Product knowledge')).toBeInTheDocument()
|
||||
expect(
|
||||
screen.getByRole('button', { name: /common.operation.edit Product knowledge/ }),
|
||||
).toBeInTheDocument()
|
||||
expect(
|
||||
screen.getByRole('button', { name: /common.operation.delete Product knowledge/ }),
|
||||
).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,115 @@
|
||||
import { Button } from '@langgenius/dify-ui/button'
|
||||
import { cn } from '@langgenius/dify-ui/cn'
|
||||
import { IconButton } from '@langgenius/dify-ui/icon-button'
|
||||
import { RiAddLine, RiBookOpenLine, RiCloseLine } from '@remixicon/react'
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import * as React from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { LoadingPlaceholder } from '@/app/components/base/loading-placeholder'
|
||||
import { useDocLink } from '@/context/i18n'
|
||||
import { useModalContext } from '@/context/modal-context'
|
||||
import { consoleQuery } from '@/service/console'
|
||||
import ExternalKnowledgeAPICard from '../external-knowledge-api-card'
|
||||
|
||||
type ExternalAPIPanelProps = {
|
||||
canManageExternalKnowledgeApi: boolean
|
||||
onClose: () => void
|
||||
}
|
||||
|
||||
const ExternalAPIPanel: React.FC<ExternalAPIPanelProps> = ({
|
||||
canManageExternalKnowledgeApi,
|
||||
onClose,
|
||||
}) => {
|
||||
const { t } = useTranslation(['common', 'dataset'])
|
||||
const docLink = useDocLink()
|
||||
const { setShowExternalKnowledgeAPIModal } = useModalContext()
|
||||
const queryClient = useQueryClient()
|
||||
const externalKnowledgeApiQueryOptions =
|
||||
consoleQuery.datasets.externalKnowledgeApi.get.queryOptions({ input: {} })
|
||||
const { data, isLoading } = useQuery(externalKnowledgeApiQueryOptions)
|
||||
const externalKnowledgeApiList = data?.data ?? []
|
||||
|
||||
const handleOpenExternalAPIModal = () => {
|
||||
if (!canManageExternalKnowledgeApi) return
|
||||
|
||||
setShowExternalKnowledgeAPIModal({
|
||||
payload: { name: '', settings: { endpoint: '', api_key: '' } },
|
||||
datasetBindings: [],
|
||||
onSaveCallback: () => {
|
||||
void queryClient.invalidateQueries({
|
||||
queryKey: externalKnowledgeApiQueryOptions.queryKey,
|
||||
})
|
||||
},
|
||||
isEditMode: false,
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<div tabIndex={-1} className={cn('absolute top-14 right-0 bottom-2 z-10 flex outline-hidden')}>
|
||||
<div
|
||||
className={cn(
|
||||
'relative flex h-full w-105 flex-col rounded-l-2xl border border-components-panel-border bg-components-panel-bg-alt',
|
||||
)}
|
||||
>
|
||||
<div className="flex items-start self-stretch p-4 pb-0">
|
||||
<div className="flex grow flex-col items-start gap-1">
|
||||
<div className="self-stretch system-xl-semibold text-text-primary">
|
||||
{t(($) => $.externalAPIPanelTitle, { ns: 'dataset' })}
|
||||
</div>
|
||||
<div className="self-stretch body-xs-regular text-text-tertiary">
|
||||
{t(($) => $.externalAPIPanelDescription, { ns: 'dataset' })}
|
||||
</div>
|
||||
<a
|
||||
className="flex cursor-pointer items-center justify-center gap-1 self-stretch"
|
||||
href={docLink('/use-dify/knowledge/external-knowledge-api')}
|
||||
target="_blank"
|
||||
>
|
||||
<RiBookOpenLine className="size-3 text-text-accent" />
|
||||
<div className="grow body-xs-regular text-text-accent">
|
||||
{t(($) => $.externalAPIPanelDocumentation, { ns: 'dataset' })}
|
||||
</div>
|
||||
</a>
|
||||
</div>
|
||||
<div className="flex items-center">
|
||||
<IconButton
|
||||
aria-label={t(($) => $['operation.close'], { ns: 'common' })}
|
||||
onClick={() => onClose()}
|
||||
>
|
||||
<RiCloseLine aria-hidden className="size-4 text-text-tertiary" />
|
||||
</IconButton>
|
||||
</div>
|
||||
</div>
|
||||
{canManageExternalKnowledgeApi && (
|
||||
<div className="flex flex-col items-start justify-center gap-2 self-stretch px-4 py-3">
|
||||
<Button
|
||||
variant="primary"
|
||||
className="flex items-center justify-center px-3 py-2"
|
||||
onClick={handleOpenExternalAPIModal}
|
||||
>
|
||||
<RiAddLine className="size-4 text-components-button-primary-text" />
|
||||
<div className="system-sm-medium text-components-button-primary-text">
|
||||
{t(($) => $.createExternalAPI, { ns: 'dataset' })}
|
||||
</div>
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
<div className="flex grow flex-col items-start gap-1 self-stretch px-4 py-0">
|
||||
{isLoading ? (
|
||||
<LoadingPlaceholder />
|
||||
) : (
|
||||
externalKnowledgeApiList.map((api, index) => (
|
||||
<ExternalKnowledgeAPICard
|
||||
key={api.id}
|
||||
api={api}
|
||||
canManageExternalKnowledgeApi={canManageExternalKnowledgeApi}
|
||||
position={index + 1}
|
||||
/>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ExternalAPIPanel
|
||||
Reference in new issue
Block a user