You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-workflow-variables.ts 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. import { useIsChatMode } from './use-workflow'
  12. import { useStoreApi } from 'reactflow'
  13. import type { Type } from '../nodes/llm/types'
  14. export const useWorkflowVariables = () => {
  15. const { t } = useTranslation()
  16. const environmentVariables = useStore(s => s.environmentVariables)
  17. const conversationVariables = useStore(s => s.conversationVariables)
  18. const getNodeAvailableVars = useCallback(({
  19. parentNode,
  20. beforeNodes,
  21. isChatMode,
  22. filterVar,
  23. hideEnv,
  24. hideChatVar,
  25. }: {
  26. parentNode?: Node | null
  27. beforeNodes: Node[]
  28. isChatMode: boolean
  29. filterVar: (payload: Var, selector: ValueSelector) => boolean
  30. hideEnv?: boolean
  31. hideChatVar?: boolean
  32. }): NodeOutPutVar[] => {
  33. return toNodeAvailableVars({
  34. parentNode,
  35. t,
  36. beforeNodes,
  37. isChatMode,
  38. environmentVariables: hideEnv ? [] : environmentVariables,
  39. conversationVariables: (isChatMode && !hideChatVar) ? conversationVariables : [],
  40. filterVar,
  41. })
  42. }, [conversationVariables, environmentVariables, t])
  43. const getCurrentVariableType = useCallback(({
  44. parentNode,
  45. valueSelector,
  46. isIterationItem,
  47. isLoopItem,
  48. availableNodes,
  49. isChatMode,
  50. isConstant,
  51. }: {
  52. valueSelector: ValueSelector
  53. parentNode?: Node | null
  54. isIterationItem?: boolean
  55. isLoopItem?: boolean
  56. availableNodes: any[]
  57. isChatMode: boolean
  58. isConstant?: boolean
  59. }) => {
  60. return getVarType({
  61. parentNode,
  62. valueSelector,
  63. isIterationItem,
  64. isLoopItem,
  65. availableNodes,
  66. isChatMode,
  67. isConstant,
  68. environmentVariables,
  69. conversationVariables,
  70. })
  71. }, [conversationVariables, environmentVariables])
  72. return {
  73. getNodeAvailableVars,
  74. getCurrentVariableType,
  75. }
  76. }
  77. export const useWorkflowVariableType = () => {
  78. const store = useStoreApi()
  79. const {
  80. getNodes,
  81. } = store.getState()
  82. const { getCurrentVariableType } = useWorkflowVariables()
  83. const isChatMode = useIsChatMode()
  84. const getVarType = ({
  85. nodeId,
  86. valueSelector,
  87. }: {
  88. nodeId: string,
  89. valueSelector: ValueSelector,
  90. }) => {
  91. const node = getNodes().find(n => n.id === nodeId)
  92. const isInIteration = !!node?.data.isInIteration
  93. const iterationNode = isInIteration ? getNodes().find(n => n.id === node.parentId) : null
  94. const availableNodes = [node]
  95. const type = getCurrentVariableType({
  96. parentNode: iterationNode,
  97. valueSelector,
  98. availableNodes,
  99. isChatMode,
  100. isConstant: false,
  101. })
  102. return type as unknown as Type
  103. }
  104. return getVarType
  105. }