'use client' import type { FC } from 'react' import type { Credential, CustomCollectionBackend, CustomParamSchema, } from '@/app/components/tools/types' import { Button } from '@langgenius/dify-ui/button' import { cn } from '@langgenius/dify-ui/cn' import { Drawer, DrawerBackdrop, DrawerCloseButton, DrawerContent, DrawerPopup, DrawerPortal, DrawerTitle, DrawerViewport, } from '@langgenius/dify-ui/drawer' import { RiSettings2Line } from '@remixicon/react' import * as React from 'react' import { useState } from 'react' import { useTranslation } from 'react-i18next' import Input from '@/app/components/base/input' import { AuthType } from '@/app/components/tools/types' import { useLocale } from '@/context/i18n' import { getLanguage } from '@/i18n-config/language' import { testAPIAvailable } from '@/service/tools' import ConfigCredentials from './config-credentials' type Props = Readonly<{ positionCenter?: boolean customCollection: CustomCollectionBackend tool: CustomParamSchema onHide: () => void }> const TestApi: FC = ({ positionCenter, customCollection, tool, onHide }) => { const { t } = useTranslation() const locale = useLocale() const language = getLanguage(locale) const [credentialsModalShow, setCredentialsModalShow] = useState(false) const [tempCredential, setTempCredential] = React.useState( customCollection.credentials, ) const [testing, setTesting] = useState(false) const [result, setResult] = useState('') const { operation_id: toolName, parameters } = tool const [parametersValue, setParametersValue] = useState>({}) const handleTest = async () => { if (testing) return setTesting(true) // clone test schema const credentials = JSON.parse(JSON.stringify(tempCredential)) as Credential if (credentials.auth_type === AuthType.none) { delete credentials.api_key_header_prefix delete credentials.api_key_header delete credentials.api_key_value } const data = { provider_name: customCollection.provider, tool_name: toolName, credentials, schema_type: customCollection.schema_type, schema: customCollection.schema, parameters: parametersValue, } const res = (await testAPIAvailable(data)) as any setResult(res.error || res.result) setTesting(false) } return ( <> { if (!open) onHide() }} >
{`${t(($) => $['test.title'], { ns: 'tools' })} ${toolName}`} $['operation.close'], { ns: 'common' })} className="size-6 rounded-md" />
{t(($) => $['createTool.authMethod.title'], { ns: 'tools' })}
setCredentialsModalShow(true)} >
{t(($) => $[`createTool.authMethod.types.${tempCredential.auth_type}`], { ns: 'tools', })}
{t(($) => $['test.parametersValue'], { ns: 'tools' })}
{parameters.map((item, index) => ( ))}
{t(($) => $['test.parameters'], { ns: 'tools' })} {t(($) => $['test.value'], { ns: 'tools' })}
{item.label[language]} setParametersValue({ ...parametersValue, [item.name]: e.target.value, }) } type="text" className="!hover:border-transparent !hover:bg-transparent !focus:border-transparent !focus:bg-transparent border-transparent! bg-transparent!" />
{t(($) => $['test.testResult'], { ns: 'tools' })}
{result || ( {t(($) => $['test.testResultPlaceholder'], { ns: 'tools' })} )}
{credentialsModalShow && ( setCredentialsModalShow(false)} /> )} ) } export default React.memo(TestApi)