您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

use-workflow-variables.ts 3.1KB

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