feat: support tool search also can search toolProvider's name (#10518)

This commit is contained in:
非法操作 2024-11-11 11:32:41 +08:00 committed by GitHub
parent 451ccb778d
commit 0587e24fdb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 12 deletions

View File

@ -15,7 +15,6 @@ import Category from './category'
import Tools from './tools' import Tools from './tools'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import I18n from '@/context/i18n' import I18n from '@/context/i18n'
import { getLanguage } from '@/i18n/language'
import Drawer from '@/app/components/base/drawer' import Drawer from '@/app/components/base/drawer'
import Button from '@/app/components/base/button' import Button from '@/app/components/base/button'
import Loading from '@/app/components/base/loading' import Loading from '@/app/components/base/loading'
@ -44,13 +43,15 @@ const AddToolModal: FC<Props> = ({
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
const { locale } = useContext(I18n) const { locale } = useContext(I18n)
const language = getLanguage(locale)
const [currentType, setCurrentType] = useState('builtin') const [currentType, setCurrentType] = useState('builtin')
const [currentCategory, setCurrentCategory] = useState('') const [currentCategory, setCurrentCategory] = useState('')
const [keywords, setKeywords] = useState<string>('') const [keywords, setKeywords] = useState<string>('')
const handleKeywordsChange = (value: string) => { const handleKeywordsChange = (value: string) => {
setKeywords(value) setKeywords(value)
} }
const isMatchingKeywords = (text: string, keywords: string) => {
return text.toLowerCase().includes(keywords.toLowerCase())
}
const [toolList, setToolList] = useState<ToolWithProvider[]>([]) const [toolList, setToolList] = useState<ToolWithProvider[]>([])
const [listLoading, setListLoading] = useState(true) const [listLoading, setListLoading] = useState(true)
const getAllTools = async () => { const getAllTools = async () => {
@ -82,13 +83,16 @@ const AddToolModal: FC<Props> = ({
else else
return toolWithProvider.labels.includes(currentCategory) return toolWithProvider.labels.includes(currentCategory)
}).filter((toolWithProvider) => { }).filter((toolWithProvider) => {
return toolWithProvider.tools.some((tool) => { return (
return Object.values(tool.label).some((label) => { isMatchingKeywords(toolWithProvider.name, keywords)
return label.toLowerCase().includes(keywords.toLowerCase()) || toolWithProvider.tools.some((tool) => {
return Object.values(tool.label).some((label) => {
return isMatchingKeywords(label, keywords)
})
}) })
}) )
}) })
}, [currentType, currentCategory, toolList, keywords, language]) }, [currentType, currentCategory, toolList, keywords])
const { const {
modelConfig, modelConfig,

View File

@ -11,7 +11,6 @@ import { ToolTypeEnum } from './types'
import Tools from './tools' import Tools from './tools'
import { useToolTabs } from './hooks' import { useToolTabs } from './hooks'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import { useGetLanguage } from '@/context/i18n'
type AllToolsProps = { type AllToolsProps = {
searchText: string searchText: string
@ -21,13 +20,16 @@ const AllTools = ({
searchText, searchText,
onSelect, onSelect,
}: AllToolsProps) => { }: AllToolsProps) => {
const language = useGetLanguage()
const tabs = useToolTabs() const tabs = useToolTabs()
const [activeTab, setActiveTab] = useState(ToolTypeEnum.All) const [activeTab, setActiveTab] = useState(ToolTypeEnum.All)
const buildInTools = useStore(s => s.buildInTools) const buildInTools = useStore(s => s.buildInTools)
const customTools = useStore(s => s.customTools) const customTools = useStore(s => s.customTools)
const workflowTools = useStore(s => s.workflowTools) const workflowTools = useStore(s => s.workflowTools)
const isMatchingKeywords = (text: string, keywords: string) => {
return text.toLowerCase().includes(keywords.toLowerCase())
}
const tools = useMemo(() => { const tools = useMemo(() => {
let mergedTools: ToolWithProvider[] = [] let mergedTools: ToolWithProvider[] = []
if (activeTab === ToolTypeEnum.All) if (activeTab === ToolTypeEnum.All)
@ -40,11 +42,14 @@ const AllTools = ({
mergedTools = workflowTools mergedTools = workflowTools
return mergedTools.filter((toolWithProvider) => { return mergedTools.filter((toolWithProvider) => {
return toolWithProvider.tools.some((tool) => { return isMatchingKeywords(toolWithProvider.name, searchText)
return tool.label[language].toLowerCase().includes(searchText.toLowerCase()) || toolWithProvider.tools.some((tool) => {
return Object.values(tool.label).some((label) => {
return isMatchingKeywords(label, searchText)
})
}) })
}) })
}, [activeTab, buildInTools, customTools, workflowTools, searchText, language]) }, [activeTab, buildInTools, customTools, workflowTools, searchText])
return ( return (
<div> <div>
<div className='flex items-center px-3 h-8 space-x-1 bg-gray-25 border-b-[0.5px] border-black/[0.08] shadow-xs'> <div className='flex items-center px-3 h-8 space-x-1 bg-gray-25 border-b-[0.5px] border-black/[0.08] shadow-xs'>