use-workflow-variables.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { useCallback } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useStore } from '../store'
  4. import { getVarType, toNodeAvailableVars } from '@/app/components/workflow/nodes/_base/components/variable/utils'
  5. import type {
  6. Node,
  7. NodeOutPutVar,
  8. ValueSelector,
  9. Var,
  10. } from '@/app/components/workflow/types'
  11. export const useWorkflowVariables = () => {
  12. const { t } = useTranslation()
  13. const environmentVariables = useStore(s => s.environmentVariables)
  14. const conversationVariables = useStore(s => s.conversationVariables)
  15. const getNodeAvailableVars = useCallback(({
  16. parentNode,
  17. beforeNodes,
  18. isChatMode,
  19. filterVar,
  20. hideEnv,
  21. hideChatVar,
  22. }: {
  23. parentNode?: Node | null
  24. beforeNodes: Node[]
  25. isChatMode: boolean
  26. filterVar: (payload: Var, selector: ValueSelector) => boolean
  27. hideEnv?: boolean
  28. hideChatVar?: boolean
  29. }): NodeOutPutVar[] => {
  30. return toNodeAvailableVars({
  31. parentNode,
  32. t,
  33. beforeNodes,
  34. isChatMode,
  35. environmentVariables: hideEnv ? [] : environmentVariables,
  36. conversationVariables: (isChatMode && !hideChatVar) ? conversationVariables : [],
  37. filterVar,
  38. })
  39. }, [conversationVariables, environmentVariables, t])
  40. const getCurrentVariableType = useCallback(({
  41. parentNode,
  42. valueSelector,
  43. isIterationItem,
  44. isLoopItem,
  45. availableNodes,
  46. isChatMode,
  47. isConstant,
  48. }: {
  49. valueSelector: ValueSelector
  50. parentNode?: Node | null
  51. isIterationItem?: boolean
  52. isLoopItem?: boolean
  53. availableNodes: any[]
  54. isChatMode: boolean
  55. isConstant?: boolean
  56. }) => {
  57. return getVarType({
  58. parentNode,
  59. valueSelector,
  60. isIterationItem,
  61. isLoopItem,
  62. availableNodes,
  63. isChatMode,
  64. isConstant,
  65. environmentVariables,
  66. conversationVariables,
  67. })
  68. }, [conversationVariables, environmentVariables])
  69. return {
  70. getNodeAvailableVars,
  71. getCurrentVariableType,
  72. }
  73. }