use-workflow-variables.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. availableNodes,
  45. isChatMode,
  46. isConstant,
  47. }: {
  48. valueSelector: ValueSelector
  49. parentNode?: Node | null
  50. isIterationItem?: boolean
  51. availableNodes: any[]
  52. isChatMode: boolean
  53. isConstant?: boolean
  54. }) => {
  55. return getVarType({
  56. parentNode,
  57. valueSelector,
  58. isIterationItem,
  59. availableNodes,
  60. isChatMode,
  61. isConstant,
  62. environmentVariables,
  63. conversationVariables,
  64. })
  65. }, [conversationVariables, environmentVariables])
  66. return {
  67. getNodeAvailableVars,
  68. getCurrentVariableType,
  69. }
  70. }