index.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import Item from './item'
  6. import FeaturePanel from '@/app/components/app/configuration/base/feature-panel'
  7. import { Google, WebReader, Wikipedia } from '@/app/components/base/icons/src/public/plugins'
  8. import { getToolProviders } from '@/service/explore'
  9. import Loading from '@/app/components/base/loading'
  10. import AccountSetting from '@/app/components/header/account-setting'
  11. export type IPluginsProps = {
  12. readonly?: boolean
  13. config: Record<string, boolean>
  14. onChange?: (key: string, value: boolean) => void
  15. }
  16. const plugins = [
  17. { key: 'google_search', icon: <Google /> },
  18. { key: 'web_reader', icon: <WebReader /> },
  19. { key: 'wikipedia', icon: <Wikipedia /> },
  20. ]
  21. const Plugins: FC<IPluginsProps> = ({
  22. readonly,
  23. config,
  24. onChange,
  25. }) => {
  26. const { t } = useTranslation()
  27. const [isLoading, setIsLoading] = React.useState(!readonly)
  28. const [isSerpApiValid, setIsSerpApiValid] = React.useState(false)
  29. const checkSerpApiKey = async () => {
  30. if (readonly)
  31. return
  32. const provides: any = await getToolProviders()
  33. const isSerpApiValid = !!provides.find((v: any) => v.tool_name === 'serpapi' && v.is_enabled)
  34. setIsSerpApiValid(isSerpApiValid)
  35. setIsLoading(false)
  36. }
  37. useEffect(() => {
  38. checkSerpApiKey()
  39. }, [])
  40. const [showSetSerpAPIKeyModal, setShowSetAPIKeyModal] = React.useState(false)
  41. const itemConfigs = plugins.map((plugin) => {
  42. const res: Record<string, any> = { ...plugin }
  43. const { key } = plugin
  44. res.name = t(`explore.universalChat.plugins.${key}.name`)
  45. if (key === 'web_reader')
  46. res.description = t(`explore.universalChat.plugins.${key}.description`)
  47. if (key === 'google_search' && !isSerpApiValid && !readonly) {
  48. res.readonly = true
  49. res.more = (
  50. <div className='border-t border-[#FEF0C7] flex items-center h-[34px] pl-2 bg-[#FFFAEB] text-gray-700 text-xs '>
  51. <span className='whitespace-pre'>{t('explore.universalChat.plugins.google_search.more.left')}</span>
  52. <span className='cursor-pointer text-[#155EEF]' onClick={() => setShowSetAPIKeyModal(true)}>{t('explore.universalChat.plugins.google_search.more.link')}</span>
  53. <span className='whitespace-pre'>{t('explore.universalChat.plugins.google_search.more.right')}</span>
  54. </div>
  55. )
  56. }
  57. return res
  58. })
  59. const enabledPluginNum = Object.values(config).filter(v => v).length
  60. return (
  61. <>
  62. <FeaturePanel
  63. className='mt-3'
  64. title={
  65. <div className='flex space-x-1'>
  66. <div>{t('explore.universalChat.plugins.name')}</div>
  67. <div className='text-[13px] font-normal text-gray-500'>({enabledPluginNum}/{plugins.length})</div>
  68. </div>}
  69. hasHeaderBottomBorder={false}
  70. >
  71. {isLoading
  72. ? (
  73. <div className='flex items-center h-[166px]'>
  74. <Loading type='area' />
  75. </div>
  76. )
  77. : (<div className='space-y-2'>
  78. {itemConfigs.map(item => (
  79. <Item
  80. key={item.key}
  81. icon={item.icon}
  82. name={item.name}
  83. description={item.description}
  84. more={item.more}
  85. enabled={config[item.key]}
  86. onChange={enabled => onChange?.(item.key, enabled)}
  87. readonly={readonly || item.readonly}
  88. />
  89. ))}
  90. </div>)}
  91. </FeaturePanel>
  92. {
  93. showSetSerpAPIKeyModal && (
  94. <AccountSetting activeTab="plugin" onCancel={async () => {
  95. setShowSetAPIKeyModal(false)
  96. await checkSerpApiKey()
  97. }} />
  98. )
  99. }
  100. </>
  101. )
  102. }
  103. export default React.memo(Plugins)