fix: unified error handling for GotoAnything search actions (#23715)

This commit is contained in:
lyzno1
2025-08-11 11:57:06 +08:00
committed by GitHub
parent ff791efe18
commit 0c5e66bccb
7 changed files with 285 additions and 67 deletions

View File

@@ -38,16 +38,21 @@ export const appAction: ActionItem = {
title: 'Search Applications',
description: 'Search and navigate to your applications',
// action,
search: async (_, searchTerm = '', locale) => {
const response = (await fetchAppList({
url: 'apps',
params: {
page: 1,
name: searchTerm,
},
}))
const apps = response.data || []
return parser(apps)
search: async (_, searchTerm = '', _locale) => {
try {
const response = await fetchAppList({
url: 'apps',
params: {
page: 1,
name: searchTerm,
},
})
const apps = response?.data || []
return parser(apps)
}
catch (error) {
console.warn('App search failed:', error)
return []
}
},
}

View File

@@ -18,7 +18,13 @@ export const searchAnything = async (
): Promise<SearchResult[]> => {
if (actionItem) {
const searchTerm = query.replace(actionItem.key, '').replace(actionItem.shortcut, '').trim()
return await actionItem.search(query, searchTerm, locale)
try {
return await actionItem.search(query, searchTerm, locale)
}
catch (error) {
console.warn(`Search failed for ${actionItem.key}:`, error)
return []
}
}
if (query.startsWith('@'))

View File

@@ -35,16 +35,22 @@ export const knowledgeAction: ActionItem = {
title: 'Search Knowledge Bases',
description: 'Search and navigate to your knowledge bases',
// action,
search: async (_, searchTerm = '', locale) => {
const response = await fetchDatasets({
url: '/datasets',
params: {
page: 1,
limit: 10,
keyword: searchTerm,
},
})
return parser(response.data)
search: async (_, searchTerm = '', _locale) => {
try {
const response = await fetchDatasets({
url: '/datasets',
params: {
page: 1,
limit: 10,
keyword: searchTerm,
},
})
const datasets = response?.data || []
return parser(datasets)
}
catch (error) {
console.warn('Knowledge search failed:', error)
return []
}
},
}

View File

@@ -24,18 +24,30 @@ export const pluginAction: ActionItem = {
title: 'Search Plugins',
description: 'Search and navigate to your plugins',
search: async (_, searchTerm = '', locale) => {
const response = await postMarketplace<{ data: PluginsFromMarketplaceResponse }>('/plugins/search/advanced', {
body: {
page: 1,
page_size: 10,
query: searchTerm,
type: 'plugin',
},
})
const list = (response.data.plugins || []).map(plugin => ({
...plugin,
icon: getPluginIconInMarketplace(plugin),
}))
return parser(list, locale!)
try {
const response = await postMarketplace<{ data: PluginsFromMarketplaceResponse }>('/plugins/search/advanced', {
body: {
page: 1,
page_size: 10,
query: searchTerm,
type: 'plugin',
},
})
if (!response?.data?.plugins) {
console.warn('Plugin search: Unexpected response structure', response)
return []
}
const list = response.data.plugins.map(plugin => ({
...plugin,
icon: getPluginIconInMarketplace(plugin),
}))
return parser(list, locale!)
}
catch (error) {
console.warn('Plugin search failed:', error)
return []
}
},
}

View File

@@ -1,6 +1,4 @@
import type { ActionItem } from './types'
import { BoltIcon } from '@heroicons/react/24/outline'
import i18n from 'i18next'
// Create the workflow nodes action
export const workflowNodesAction: ActionItem = {
@@ -12,32 +10,14 @@ export const workflowNodesAction: ActionItem = {
search: async (_, searchTerm = '', locale) => {
try {
// Use the searchFn if available (set by useWorkflowSearch hook)
if (workflowNodesAction.searchFn) {
// searchFn already returns SearchResult[] type, no need to use parser
if (workflowNodesAction.searchFn)
return workflowNodesAction.searchFn(searchTerm)
}
// If not in workflow context or search function not registered
if (!searchTerm.trim()) {
return [{
id: 'help',
title: i18n.t('app.gotoAnything.actions.searchWorkflowNodes', { lng: locale }),
description: i18n.t('app.gotoAnything.actions.searchWorkflowNodesHelp', { lng: locale }),
type: 'workflow-node',
path: '#',
data: {} as any,
icon: (
<div className="flex h-8 w-8 shrink-0 items-center justify-center rounded-md bg-blue-50 text-blue-600">
<BoltIcon className="h-5 w-5" />
</div>
),
}]
}
// If not in workflow context, return empty array
return []
}
catch (error) {
console.error('Error searching workflow nodes:', error)
catch (error) {
console.warn('Workflow nodes search failed:', error)
return []
}
},

View File

@@ -46,9 +46,9 @@ export const useWorkflowSearch = () => {
// Create search function for workflow nodes
const searchWorkflowNodes = useCallback((query: string) => {
if (!searchableNodes.length || !query.trim()) return []
if (!searchableNodes.length) return []
const searchTerm = query.toLowerCase()
const searchTerm = query.toLowerCase().trim()
const results = searchableNodes
.map((node) => {
@@ -58,11 +58,18 @@ export const useWorkflowSearch = () => {
let score = 0
if (titleMatch.startsWith(searchTerm)) score += 100
else if (titleMatch.includes(searchTerm)) score += 50
else if (typeMatch === searchTerm) score += 80
else if (typeMatch.includes(searchTerm)) score += 30
else if (descMatch.includes(searchTerm)) score += 20
// If no search term, show all nodes with base score
if (!searchTerm) {
score = 1
}
else {
// Score based on search relevance
if (titleMatch.startsWith(searchTerm)) score += 100
else if (titleMatch.includes(searchTerm)) score += 50
else if (typeMatch === searchTerm) score += 80
else if (typeMatch.includes(searchTerm)) score += 30
else if (descMatch.includes(searchTerm)) score += 20
}
return score > 0
? {
@@ -89,6 +96,11 @@ export const useWorkflowSearch = () => {
})
.filter((node): node is NonNullable<typeof node> => node !== null)
.sort((a, b) => {
// If no search term, sort alphabetically
if (!searchTerm)
return a.title.localeCompare(b.title)
// Sort by relevance when searching
const aTitle = a.title.toLowerCase()
const bTitle = b.title.toLowerCase()