Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { appAction } from './app'
  2. import { knowledgeAction } from './knowledge'
  3. import { pluginAction } from './plugin'
  4. import { workflowNodesAction } from './workflow-nodes'
  5. import type { ActionItem, SearchResult } from './types'
  6. export const Actions = {
  7. app: appAction,
  8. knowledge: knowledgeAction,
  9. plugin: pluginAction,
  10. node: workflowNodesAction,
  11. }
  12. export const searchAnything = async (
  13. locale: string,
  14. query: string,
  15. actionItem?: ActionItem,
  16. ): Promise<SearchResult[]> => {
  17. if (actionItem) {
  18. const searchTerm = query.replace(actionItem.key, '').replace(actionItem.shortcut, '').trim()
  19. try {
  20. return await actionItem.search(query, searchTerm, locale)
  21. }
  22. catch (error) {
  23. console.warn(`Search failed for ${actionItem.key}:`, error)
  24. return []
  25. }
  26. }
  27. if (query.startsWith('@'))
  28. return []
  29. // Use Promise.allSettled to handle partial failures gracefully
  30. const searchPromises = Object.values(Actions).map(async (action) => {
  31. try {
  32. const results = await action.search(query, query, locale)
  33. return { success: true, data: results, actionType: action.key }
  34. }
  35. catch (error) {
  36. console.warn(`Search failed for ${action.key}:`, error)
  37. return { success: false, data: [], actionType: action.key, error }
  38. }
  39. })
  40. const settledResults = await Promise.allSettled(searchPromises)
  41. const allResults: SearchResult[] = []
  42. const failedActions: string[] = []
  43. settledResults.forEach((result, index) => {
  44. if (result.status === 'fulfilled' && result.value.success) {
  45. allResults.push(...result.value.data)
  46. }
  47. else {
  48. const actionKey = Object.values(Actions)[index]?.key || 'unknown'
  49. failedActions.push(actionKey)
  50. }
  51. })
  52. if (failedActions.length > 0)
  53. console.warn(`Some search actions failed: ${failedActions.join(', ')}`)
  54. return allResults
  55. }
  56. export const matchAction = (query: string, actions: Record<string, ActionItem>) => {
  57. return Object.values(actions).find((action) => {
  58. const reg = new RegExp(`^(${action.key}|${action.shortcut})(?:\\s|$)`)
  59. return reg.test(query)
  60. })
  61. }
  62. export * from './types'
  63. export { appAction, knowledgeAction, pluginAction, workflowNodesAction }