| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import { appAction } from './app'
- import { knowledgeAction } from './knowledge'
- import { pluginAction } from './plugin'
- import { workflowNodesAction } from './workflow-nodes'
- import type { ActionItem, SearchResult } from './types'
-
- export const Actions = {
- app: appAction,
- knowledge: knowledgeAction,
- plugin: pluginAction,
- node: workflowNodesAction,
- }
-
- export const searchAnything = async (
- locale: string,
- query: string,
- actionItem?: ActionItem,
- ): Promise<SearchResult[]> => {
- if (actionItem) {
- const searchTerm = query.replace(actionItem.key, '').replace(actionItem.shortcut, '').trim()
- try {
- return await actionItem.search(query, searchTerm, locale)
- }
- catch (error) {
- console.warn(`Search failed for ${actionItem.key}:`, error)
- return []
- }
- }
-
- if (query.startsWith('@'))
- return []
-
- // Use Promise.allSettled to handle partial failures gracefully
- const searchPromises = Object.values(Actions).map(async (action) => {
- try {
- const results = await action.search(query, query, locale)
- return { success: true, data: results, actionType: action.key }
- }
- catch (error) {
- console.warn(`Search failed for ${action.key}:`, error)
- return { success: false, data: [], actionType: action.key, error }
- }
- })
-
- const settledResults = await Promise.allSettled(searchPromises)
-
- const allResults: SearchResult[] = []
- const failedActions: string[] = []
-
- settledResults.forEach((result, index) => {
- if (result.status === 'fulfilled' && result.value.success) {
- allResults.push(...result.value.data)
- }
- else {
- const actionKey = Object.values(Actions)[index]?.key || 'unknown'
- failedActions.push(actionKey)
- }
- })
-
- if (failedActions.length > 0)
- console.warn(`Some search actions failed: ${failedActions.join(', ')}`)
-
- return allResults
- }
-
- export const matchAction = (query: string, actions: Record<string, ActionItem>) => {
- return Object.values(actions).find((action) => {
- const reg = new RegExp(`^(${action.key}|${action.shortcut})(?:\\s|$)`)
- return reg.test(query)
- })
- }
-
- export * from './types'
- export { appAction, knowledgeAction, pluginAction, workflowNodesAction }
|