feat: chat in explore support agent (#647)
Co-authored-by: StyleZhang <jasonapring2015@outlook.com>
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import Image from 'next/image'
|
||||
import SerpapiLogo from '../../assets/serpapi.png'
|
||||
import KeyValidator from '../key-validator'
|
||||
import type { Form, ValidateValue } from '../key-validator/declarations'
|
||||
import { updatePluginKey, validatePluginKey } from './utils'
|
||||
import { useToastContext } from '@/app/components/base/toast'
|
||||
import type { PluginProvider } from '@/models/common'
|
||||
|
||||
type SerpapiPluginProps = {
|
||||
plugin: PluginProvider
|
||||
onUpdate: () => void
|
||||
}
|
||||
const SerpapiPlugin = ({
|
||||
plugin,
|
||||
onUpdate,
|
||||
}: SerpapiPluginProps) => {
|
||||
const { t } = useTranslation()
|
||||
const { notify } = useToastContext()
|
||||
|
||||
const forms: Form[] = [{
|
||||
key: 'api_key',
|
||||
title: t('common.plugin.serpapi.apiKey'),
|
||||
placeholder: t('common.plugin.serpapi.apiKeyPlaceholder'),
|
||||
value: plugin.credentials?.api_key,
|
||||
validate: {
|
||||
before: (v) => {
|
||||
if (v?.api_key)
|
||||
return true
|
||||
},
|
||||
run: async (v) => {
|
||||
return validatePluginKey('serpapi', {
|
||||
credentials: {
|
||||
api_key: v?.api_key,
|
||||
},
|
||||
})
|
||||
},
|
||||
},
|
||||
handleFocus: (v, dispatch) => {
|
||||
if (v.api_key === plugin.credentials?.api_key)
|
||||
dispatch({ ...v, api_key: '' })
|
||||
},
|
||||
}]
|
||||
|
||||
const handleSave = async (v: ValidateValue) => {
|
||||
if (!v?.api_key || v?.api_key === plugin.credentials?.api_key)
|
||||
return
|
||||
|
||||
const res = await updatePluginKey('serpapi', {
|
||||
credentials: {
|
||||
api_key: v?.api_key,
|
||||
},
|
||||
})
|
||||
|
||||
if (res.status === 'success') {
|
||||
notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
|
||||
onUpdate()
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<KeyValidator
|
||||
type='serpapi'
|
||||
title={<Image alt='serpapi logo' src={SerpapiLogo} width={64} />}
|
||||
status={plugin.credentials?.api_key ? 'success' : 'add'}
|
||||
forms={forms}
|
||||
keyFrom={{
|
||||
text: t('common.plugin.serpapi.keyFrom'),
|
||||
link: 'https://serpapi.com/manage-api-key',
|
||||
}}
|
||||
onSave={handleSave}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export default SerpapiPlugin
|
@@ -0,0 +1,38 @@
|
||||
import useSWR from 'swr'
|
||||
import { LockClosedIcon } from '@heroicons/react/24/solid'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import Link from 'next/link'
|
||||
import SerpapiPlugin from './SerpapiPlugin'
|
||||
import { fetchPluginProviders } from '@/service/common'
|
||||
import type { PluginProvider } from '@/models/common'
|
||||
|
||||
const PluginPage = () => {
|
||||
const { t } = useTranslation()
|
||||
const { data: plugins, mutate } = useSWR('/workspaces/current/tool-providers', fetchPluginProviders)
|
||||
|
||||
const Plugin_MAP: Record<string, any> = {
|
||||
serpapi: (plugin: PluginProvider) => <SerpapiPlugin key='serpapi' plugin={plugin} onUpdate={() => mutate()} />,
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='pb-7'>
|
||||
<div>
|
||||
{plugins?.map(plugin => Plugin_MAP[plugin.tool_name](plugin))}
|
||||
</div>
|
||||
<div className='fixed bottom-0 w-[472px] h-[42px] flex items-center bg-white text-xs text-gray-500'>
|
||||
<LockClosedIcon className='w-3 h-3 mr-1' />
|
||||
{t('common.provider.encrypted.front')}
|
||||
<Link
|
||||
className='text-primary-600 mx-1'
|
||||
target={'_blank'}
|
||||
href='https://pycryptodome.readthedocs.io/en/latest/src/cipher/oaep.html'
|
||||
>
|
||||
PKCS1_OAEP
|
||||
</Link>
|
||||
{t('common.provider.encrypted.back')}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default PluginPage
|
@@ -0,0 +1,34 @@
|
||||
import { ValidatedStatus } from '../key-validator/declarations'
|
||||
import { updatePluginProviderAIKey, validatePluginProviderKey } from '@/service/common'
|
||||
|
||||
export const validatePluginKey = async (pluginType: string, body: any) => {
|
||||
try {
|
||||
const res = await validatePluginProviderKey({
|
||||
url: `/workspaces/current/tool-providers/${pluginType}/credentials-validate`,
|
||||
body,
|
||||
})
|
||||
if (res.result === 'success')
|
||||
return Promise.resolve({ status: ValidatedStatus.Success })
|
||||
else
|
||||
return Promise.resolve({ status: ValidatedStatus.Error, message: res.error })
|
||||
}
|
||||
catch (e: any) {
|
||||
return Promise.resolve({ status: ValidatedStatus.Error, message: e.message })
|
||||
}
|
||||
}
|
||||
|
||||
export const updatePluginKey = async (pluginType: string, body: any) => {
|
||||
try {
|
||||
const res = await updatePluginProviderAIKey({
|
||||
url: `/workspaces/current/tool-providers/${pluginType}/credentials`,
|
||||
body,
|
||||
})
|
||||
if (res.result === 'success')
|
||||
return Promise.resolve({ status: ValidatedStatus.Success })
|
||||
else
|
||||
return Promise.resolve({ status: ValidatedStatus.Error, message: res.error })
|
||||
}
|
||||
catch (e: any) {
|
||||
return Promise.resolve({ status: ValidatedStatus.Error, message: e.message })
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user