feat: add external api from the create external knowledge page
This commit is contained in:
parent
9c9352bc73
commit
c9e3a9e56a
@ -28,13 +28,14 @@ import { fetchDatasetApiBaseUrl } from '@/service/datasets'
|
|||||||
import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
|
import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
|
||||||
import { useStore as useTagStore } from '@/app/components/base/tag-management/store'
|
import { useStore as useTagStore } from '@/app/components/base/tag-management/store'
|
||||||
import { useAppContext } from '@/context/app-context'
|
import { useAppContext } from '@/context/app-context'
|
||||||
|
import { ExternalApiPanelProvider, useExternalApiPanel } from '@/context/external-api-panel-context'
|
||||||
|
|
||||||
const Container = () => {
|
const ContainerContent = () => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const { currentWorkspace } = useAppContext()
|
const { currentWorkspace } = useAppContext()
|
||||||
const showTagManagementModal = useTagStore(s => s.showTagManagementModal)
|
const showTagManagementModal = useTagStore(s => s.showTagManagementModal)
|
||||||
const [showExternalApiPanel, setShowExternalApiPanel] = useState(false)
|
const { showExternalApiPanel, setShowExternalApiPanel } = useExternalApiPanel()
|
||||||
|
|
||||||
const options = useMemo(() => {
|
const options = useMemo(() => {
|
||||||
return [
|
return [
|
||||||
@ -74,45 +75,51 @@ const Container = () => {
|
|||||||
}, [currentWorkspace, router])
|
}, [currentWorkspace, router])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ExternalKnowledgeApiProvider>
|
<div ref={containerRef} className='grow relative flex flex-col bg-gray-100 overflow-y-auto'>
|
||||||
<div ref={containerRef} className='grow relative flex flex-col bg-gray-100 overflow-y-auto'>
|
<div className='sticky top-0 flex justify-between pt-4 px-12 pb-2 leading-[56px] bg-gray-100 z-10 flex-wrap gap-y-2'>
|
||||||
<div className='sticky top-0 flex justify-between pt-4 px-12 pb-2 leading-[56px] bg-gray-100 z-10 flex-wrap gap-y-2'>
|
<TabSliderNew
|
||||||
<TabSliderNew
|
value={activeTab}
|
||||||
value={activeTab}
|
onChange={newActiveTab => setActiveTab(newActiveTab)}
|
||||||
onChange={newActiveTab => setActiveTab(newActiveTab)}
|
options={options}
|
||||||
options={options}
|
/>
|
||||||
/>
|
|
||||||
{activeTab === 'dataset' && (
|
|
||||||
<div className='flex items-center gap-2'>
|
|
||||||
<TagFilter type='knowledge' value={tagFilterValue} onChange={handleTagsChange} />
|
|
||||||
<SearchInput className='w-[200px]' value={keywords} onChange={handleKeywordsChange} />
|
|
||||||
<div className="w-[1px] h-4 bg-divider-regular" />
|
|
||||||
<Button
|
|
||||||
className='gap-0.5 shadows-shadow-xs'
|
|
||||||
onClick={() => setShowExternalApiPanel(true)}
|
|
||||||
>
|
|
||||||
<ApiConnectionMod className='w-4 h-4 text-components-button-secondary-text' />
|
|
||||||
<div className='flex px-0.5 justify-center items-center gap-1 text-components-button-secondary-text system-sm-medium'>{t('dataset.externalAPI')}</div>
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{activeTab === 'api' && data && <ApiServer apiBaseUrl={data.api_base_url || ''} />}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{activeTab === 'dataset' && (
|
{activeTab === 'dataset' && (
|
||||||
<>
|
<div className='flex items-center gap-2'>
|
||||||
<Datasets containerRef={containerRef} tags={tagIDs} keywords={searchKeywords} />
|
<TagFilter type='knowledge' value={tagFilterValue} onChange={handleTagsChange} />
|
||||||
<DatasetFooter />
|
<SearchInput className='w-[200px]' value={keywords} onChange={handleKeywordsChange} />
|
||||||
{showTagManagementModal && (
|
<div className="w-[1px] h-4 bg-divider-regular" />
|
||||||
<TagManagementModal type='knowledge' show={showTagManagementModal} />
|
<Button
|
||||||
)}
|
className='gap-0.5 shadows-shadow-xs'
|
||||||
</>
|
onClick={() => setShowExternalApiPanel(true)}
|
||||||
|
>
|
||||||
|
<ApiConnectionMod className='w-4 h-4 text-components-button-secondary-text' />
|
||||||
|
<div className='flex px-0.5 justify-center items-center gap-1 text-components-button-secondary-text system-sm-medium'>{t('dataset.externalAPI')}</div>
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{activeTab === 'api' && data && <ApiServer apiBaseUrl={data.api_base_url || ''} />}
|
||||||
{activeTab === 'api' && data && <Doc apiBaseUrl={data.api_base_url || ''} />}
|
|
||||||
|
|
||||||
{showExternalApiPanel && <ExternalAPIPanel onClose={() => setShowExternalApiPanel(false)} isShow={showExternalApiPanel} datasetBindings={[]} />}
|
|
||||||
</div>
|
</div>
|
||||||
|
{activeTab === 'dataset' && (
|
||||||
|
<>
|
||||||
|
<Datasets containerRef={containerRef} tags={tagIDs} keywords={searchKeywords} />
|
||||||
|
<DatasetFooter />
|
||||||
|
{showTagManagementModal && (
|
||||||
|
<TagManagementModal type='knowledge' show={showTagManagementModal} />
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{activeTab === 'api' && data && <Doc apiBaseUrl={data.api_base_url || ''} />}
|
||||||
|
|
||||||
|
{showExternalApiPanel && <ExternalAPIPanel onClose={() => setShowExternalApiPanel(false)} isShow={showExternalApiPanel} datasetBindings={[]} />}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const Container = () => {
|
||||||
|
return (
|
||||||
|
<ExternalKnowledgeApiProvider>
|
||||||
|
<ExternalApiPanelProvider>
|
||||||
|
<ContainerContent />
|
||||||
|
</ExternalApiPanelProvider>
|
||||||
</ExternalKnowledgeApiProvider>
|
</ExternalKnowledgeApiProvider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import ExternalKnowledgeBaseConnector from '@/app/components/datasets/external-knowledge-base/connector'
|
import ExternalKnowledgeBaseConnector from '@/app/components/datasets/external-knowledge-base/connector'
|
||||||
import { ExternalKnowledgeApiProvider } from '@/context/external-knowledge-api-context'
|
import { ExternalKnowledgeApiProvider } from '@/context/external-knowledge-api-context'
|
||||||
|
import { ExternalApiPanelProvider } from '@/context/external-api-panel-context'
|
||||||
|
|
||||||
const ExternalKnowledgeBaseCreation = async () => {
|
const ExternalKnowledgeBaseCreation = async () => {
|
||||||
return (
|
return (
|
||||||
<ExternalKnowledgeApiProvider>
|
<ExternalKnowledgeApiProvider>
|
||||||
<ExternalKnowledgeBaseConnector />
|
<ExternalApiPanelProvider>
|
||||||
|
<ExternalKnowledgeBaseConnector />
|
||||||
|
</ExternalApiPanelProvider>
|
||||||
</ExternalKnowledgeApiProvider>
|
</ExternalKnowledgeApiProvider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,9 @@ const ExternalKnowledgeBaseConnector = () => {
|
|||||||
}
|
}
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
return <ExternalKnowledgeBaseCreate onConnect={handleConnect} loading={loading} />
|
return (
|
||||||
|
<ExternalKnowledgeBaseCreate onConnect={handleConnect} loading={loading} />
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ExternalKnowledgeBaseConnector
|
export default ExternalKnowledgeBaseConnector
|
||||||
|
@ -0,0 +1,73 @@
|
|||||||
|
import React, { useState } from 'react'
|
||||||
|
import { RiArrowDownSLine } from '@remixicon/react'
|
||||||
|
import { useTranslation } from 'react-i18next'
|
||||||
|
import { ApiConnectionMod } from '@/app/components/base/icons/src/vender/solid/development'
|
||||||
|
|
||||||
|
type ApiItem = {
|
||||||
|
value: string
|
||||||
|
name: string
|
||||||
|
url: string
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExternalApiSelectProps = {
|
||||||
|
items: ApiItem[]
|
||||||
|
defaultValue?: string
|
||||||
|
onSelect: (item: ApiItem) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
const ExternalApiSelect: React.FC<ExternalApiSelectProps> = ({ items, defaultValue, onSelect }) => {
|
||||||
|
const { t } = useTranslation()
|
||||||
|
const [isOpen, setIsOpen] = useState(false)
|
||||||
|
const [selectedItem, setSelectedItem] = useState<ApiItem | null>(
|
||||||
|
items.find(item => item.value === defaultValue) || null,
|
||||||
|
)
|
||||||
|
|
||||||
|
const handleSelect = (item: ApiItem) => {
|
||||||
|
setSelectedItem(item)
|
||||||
|
onSelect(item)
|
||||||
|
setIsOpen(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="relative w-full">
|
||||||
|
<div
|
||||||
|
className={`flex items-center justify-between cursor-point px-2 py-1 gap-0.5 self-stretch rounded-lg
|
||||||
|
bg-components-input-bg-normal hover:bg-state-base-hover-alt ${isOpen && 'bg-state-base-hover-alt'}`}
|
||||||
|
onClick={() => setIsOpen(!isOpen)}
|
||||||
|
>
|
||||||
|
{selectedItem
|
||||||
|
? (
|
||||||
|
<div className="flex px-2 py-1 items-center gap-0.5 self-stretch rounded-lg">
|
||||||
|
<ApiConnectionMod className='text-text-secondary w-4 h-4' />
|
||||||
|
<div className='flex p-1 items-center flex-grow'>
|
||||||
|
<span className='text-components-input-text-filled text-ellipsis system-sm-regular overflow-hidden'>{selectedItem.name}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
: (
|
||||||
|
<span className='text-components-input-text-placeholder system-sm-regular'>{t('dataset.selectExternalKnowledgeAPI.placeholder')}</span>
|
||||||
|
)}
|
||||||
|
<RiArrowDownSLine className={`w-4 h-4 text-text-quaternary transition-transform ${isOpen ? 'text-text-secondary' : ''}`} />
|
||||||
|
</div>
|
||||||
|
{isOpen && (
|
||||||
|
<div className="absolute z-10 w-full mt-1 bg-components-panel-bg-blur border rounded-xl shadow-lg">
|
||||||
|
{items.map(item => (
|
||||||
|
<div
|
||||||
|
key={item.value}
|
||||||
|
className="flex p-1 items-center cursor-pointer"
|
||||||
|
onClick={() => handleSelect(item)}
|
||||||
|
>
|
||||||
|
<div className="flex p-2 items-center gap-2 self-stretch rounded-lg hover:bg-state-base-hover w-full">
|
||||||
|
<ApiConnectionMod className='text-text-secondary w-4 h-4' />
|
||||||
|
<span className='text-text-secondary text-ellipsis system-sm-medium overflow-hidden flex-grow'>{item.name}</span>
|
||||||
|
<span className='text-text-tertiary overflow-hidden text-right text-ellipsis system-xs-regular'>{item.url}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ExternalApiSelect
|
@ -1,8 +1,13 @@
|
|||||||
import React, { useEffect } from 'react'
|
import React, { useEffect } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import Select from '@/app/components/base/select'
|
import { RiAddLine } from '@remixicon/react'
|
||||||
|
import { useRouter } from 'next/navigation'
|
||||||
|
import ExternalApiSelect from './ExternalApiSelect'
|
||||||
import Input from '@/app/components/base/input'
|
import Input from '@/app/components/base/input'
|
||||||
|
import Button from '@/app/components/base/button'
|
||||||
import { useExternalKnowledgeApi } from '@/context/external-knowledge-api-context'
|
import { useExternalKnowledgeApi } from '@/context/external-knowledge-api-context'
|
||||||
|
import { useExternalApiPanel } from '@/context/external-api-panel-context'
|
||||||
|
|
||||||
type ExternalApiSelectionProps = {
|
type ExternalApiSelectionProps = {
|
||||||
external_knowledge_api_id: string
|
external_knowledge_api_id: string
|
||||||
external_knowledge_id: string
|
external_knowledge_id: string
|
||||||
@ -11,13 +16,23 @@ type ExternalApiSelectionProps = {
|
|||||||
|
|
||||||
const ExternalApiSelection: React.FC<ExternalApiSelectionProps> = ({ external_knowledge_api_id, external_knowledge_id, onChange }) => {
|
const ExternalApiSelection: React.FC<ExternalApiSelectionProps> = ({ external_knowledge_api_id, external_knowledge_id, onChange }) => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
|
const router = useRouter()
|
||||||
const { externalKnowledgeApiList } = useExternalKnowledgeApi()
|
const { externalKnowledgeApiList } = useExternalKnowledgeApi()
|
||||||
|
// const { setShowExternalApiPanel } = useExternalApiPanel()
|
||||||
|
|
||||||
const apiItems = externalKnowledgeApiList.map(api => ({
|
const apiItems = externalKnowledgeApiList.map(api => ({
|
||||||
value: api.id,
|
value: api.id,
|
||||||
name: api.name,
|
name: api.name,
|
||||||
|
url: api.settings.endpoint,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
const { showExternalApiPanel, setShowExternalApiPanel } = useExternalApiPanel()
|
||||||
|
|
||||||
|
const handleAddNewAPI = () => {
|
||||||
|
router.back()
|
||||||
|
setShowExternalApiPanel(true)
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!external_knowledge_api_id && apiItems.length > 0)
|
if (!external_knowledge_api_id && apiItems.length > 0)
|
||||||
onChange({ external_knowledge_api_id: apiItems[0].value, external_knowledge_id })
|
onChange({ external_knowledge_api_id: apiItems[0].value, external_knowledge_id })
|
||||||
@ -29,12 +44,17 @@ const ExternalApiSelection: React.FC<ExternalApiSelectionProps> = ({ external_kn
|
|||||||
<div className='flex flex-col self-stretch'>
|
<div className='flex flex-col self-stretch'>
|
||||||
<label className='text-text-secondary system-sm-semibold'>{t('dataset.externalAPIPanelTitle')}</label>
|
<label className='text-text-secondary system-sm-semibold'>{t('dataset.externalAPIPanelTitle')}</label>
|
||||||
</div>
|
</div>
|
||||||
<Select
|
{apiItems.length > 0
|
||||||
className='w-full'
|
? <ExternalApiSelect
|
||||||
items={apiItems}
|
items={apiItems}
|
||||||
defaultValue={apiItems.length > 0 ? apiItems[0].value : ''}
|
defaultValue={apiItems[0].value}
|
||||||
onSelect={e => onChange({ external_knowledge_api_id: e.value as string, external_knowledge_id })}
|
onSelect={e => onChange({ external_knowledge_api_id: e.value as string, external_knowledge_id })}
|
||||||
/>
|
/>
|
||||||
|
: <Button variant={'tertiary'} onClick={handleAddNewAPI} className='justify-start gap-0.5'>
|
||||||
|
<RiAddLine className='w-4 h-4 text-text-tertiary' />
|
||||||
|
<span className='text-text-tertiary system-sm-regular'>{t('dataset.noExternalKnowledge')}</span>
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
<div className='flex flex-col gap-1 self-stretch'>
|
<div className='flex flex-col gap-1 self-stretch'>
|
||||||
<div className='flex flex-col self-stretch'>
|
<div className='flex flex-col self-stretch'>
|
||||||
|
28
web/context/external-api-panel-context.tsx
Normal file
28
web/context/external-api-panel-context.tsx
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
'use client'
|
||||||
|
|
||||||
|
import React, { createContext, useContext, useState } from 'react'
|
||||||
|
|
||||||
|
type ExternalApiPanelContextType = {
|
||||||
|
showExternalApiPanel: boolean
|
||||||
|
setShowExternalApiPanel: (show: boolean) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
const ExternalApiPanelContext = createContext<ExternalApiPanelContextType | undefined>(undefined)
|
||||||
|
|
||||||
|
export const ExternalApiPanelProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||||
|
const [showExternalApiPanel, setShowExternalApiPanel] = useState(false)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ExternalApiPanelContext.Provider value={{ showExternalApiPanel, setShowExternalApiPanel }}>
|
||||||
|
{children}
|
||||||
|
</ExternalApiPanelContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useExternalApiPanel = () => {
|
||||||
|
const context = useContext(ExternalApiPanelContext)
|
||||||
|
if (context === undefined)
|
||||||
|
throw new Error('useExternalApiPanel must be used within an ExternalApiPanelProvider')
|
||||||
|
|
||||||
|
return context
|
||||||
|
}
|
@ -16,6 +16,7 @@ const translation = {
|
|||||||
wordCount: ' k words',
|
wordCount: ' k words',
|
||||||
appCount: ' linked apps',
|
appCount: ' linked apps',
|
||||||
createDataset: 'Create Knowledge',
|
createDataset: 'Create Knowledge',
|
||||||
|
noExternalKnowledge: 'There is no External Knowledge API yet, click here to create',
|
||||||
createExternalAPI: 'Add an External Knowledge API',
|
createExternalAPI: 'Add an External Knowledge API',
|
||||||
editExternalAPIFormTitle: 'Edit the External Knowledge API',
|
editExternalAPIFormTitle: 'Edit the External Knowledge API',
|
||||||
editExternalAPITooltipTitle: 'LINKED KNOWLEDGE',
|
editExternalAPITooltipTitle: 'LINKED KNOWLEDGE',
|
||||||
@ -38,6 +39,9 @@ const translation = {
|
|||||||
},
|
},
|
||||||
noConnectionContent: 'Are you sure to delete this API?',
|
noConnectionContent: 'Are you sure to delete this API?',
|
||||||
},
|
},
|
||||||
|
selectExternalKnowledgeAPI: {
|
||||||
|
placeholder: 'Choose an External Knowledge API',
|
||||||
|
},
|
||||||
connectDataset: 'Connect to an External Knowledge Base',
|
connectDataset: 'Connect to an External Knowledge Base',
|
||||||
connectDatasetIntro: {
|
connectDatasetIntro: {
|
||||||
title: 'How to Connect to an External Knowledge Base',
|
title: 'How to Connect to an External Knowledge Base',
|
||||||
|
@ -16,6 +16,7 @@ const translation = {
|
|||||||
wordCount: ' 千字符',
|
wordCount: ' 千字符',
|
||||||
appCount: ' 关联应用',
|
appCount: ' 关联应用',
|
||||||
createDataset: '创建知识库',
|
createDataset: '创建知识库',
|
||||||
|
noExternalKnowledge: '还没有外部知识库 API,点击此处创建',
|
||||||
createExternalAPI: '添加外部知识库 API',
|
createExternalAPI: '添加外部知识库 API',
|
||||||
editExternalAPIFormTitle: '编辑外部知识库 API',
|
editExternalAPIFormTitle: '编辑外部知识库 API',
|
||||||
editExternalAPITooltipTitle: '个关联知识库',
|
editExternalAPITooltipTitle: '个关联知识库',
|
||||||
@ -55,6 +56,9 @@ const translation = {
|
|||||||
datasetUsedByApp: '某些应用正在使用该知识库。应用将无法再使用该知识库,所有的提示配置和日志将被永久删除。',
|
datasetUsedByApp: '某些应用正在使用该知识库。应用将无法再使用该知识库,所有的提示配置和日志将被永久删除。',
|
||||||
datasetDeleted: '知识库已删除',
|
datasetDeleted: '知识库已删除',
|
||||||
datasetDeleteFailed: '删除知识库失败',
|
datasetDeleteFailed: '删除知识库失败',
|
||||||
|
selectExternalKnowledgeAPI: {
|
||||||
|
placeholder: '选择一个外部知识 API',
|
||||||
|
},
|
||||||
didYouKnow: '你知道吗?',
|
didYouKnow: '你知道吗?',
|
||||||
intro1: '知识库可以被集成到 Dify 应用中',
|
intro1: '知识库可以被集成到 Dify 应用中',
|
||||||
intro2: '作为上下文',
|
intro2: '作为上下文',
|
||||||
|
Loading…
Reference in New Issue
Block a user