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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 { useStore } from '@/app/components/workflow/store'
  14. import type { Type } from '../nodes/llm/types'
  15. import useMatchSchemaType from '../nodes/_base/components/variable/use-match-schema-type'
  16. export const useWorkflowVariables = () => {
  17. const { t } = useTranslation()
  18. const workflowStore = useWorkflowStore()
  19. const { schemaTypeDefinitions } = useMatchSchemaType()
  20. const buildInTools = useStore(s => s.buildInTools)
  21. const customTools = useStore(s => s.customTools)
  22. const workflowTools = useStore(s => s.workflowTools)
  23. const mcpTools = useStore(s => s.mcpTools)
  24. const dataSourceList = useStore(s => s.dataSourceList)
  25. const getNodeAvailableVars = useCallback(({
  26. parentNode,
  27. beforeNodes,
  28. isChatMode,
  29. filterVar,
  30. hideEnv,
  31. hideChatVar,
  32. }: {
  33. parentNode?: Node | null
  34. beforeNodes: Node[]
  35. isChatMode: boolean
  36. filterVar: (payload: Var, selector: ValueSelector) => boolean
  37. hideEnv?: boolean
  38. hideChatVar?: boolean
  39. }): NodeOutPutVar[] => {
  40. const {
  41. conversationVariables,
  42. environmentVariables,
  43. ragPipelineVariables,
  44. } = workflowStore.getState()
  45. return toNodeAvailableVars({
  46. parentNode,
  47. t,
  48. beforeNodes,
  49. isChatMode,
  50. environmentVariables: hideEnv ? [] : environmentVariables,
  51. conversationVariables: (isChatMode && !hideChatVar) ? conversationVariables : [],
  52. ragVariables: ragPipelineVariables,
  53. filterVar,
  54. allPluginInfoList: {
  55. buildInTools,
  56. customTools,
  57. workflowTools,
  58. mcpTools,
  59. dataSourceList: dataSourceList ?? [],
  60. },
  61. schemaTypeDefinitions,
  62. })
  63. }, [t, workflowStore, schemaTypeDefinitions, buildInTools])
  64. const getCurrentVariableType = useCallback(({
  65. parentNode,
  66. valueSelector,
  67. isIterationItem,
  68. isLoopItem,
  69. availableNodes,
  70. isChatMode,
  71. isConstant,
  72. preferSchemaType,
  73. }: {
  74. valueSelector: ValueSelector
  75. parentNode?: Node | null
  76. isIterationItem?: boolean
  77. isLoopItem?: boolean
  78. availableNodes: any[]
  79. isChatMode: boolean
  80. isConstant?: boolean
  81. preferSchemaType?: boolean
  82. }) => {
  83. const {
  84. conversationVariables,
  85. environmentVariables,
  86. ragPipelineVariables,
  87. buildInTools,
  88. customTools,
  89. workflowTools,
  90. mcpTools,
  91. dataSourceList,
  92. } = workflowStore.getState()
  93. return getVarType({
  94. parentNode,
  95. valueSelector,
  96. isIterationItem,
  97. isLoopItem,
  98. availableNodes,
  99. isChatMode,
  100. isConstant,
  101. environmentVariables,
  102. conversationVariables,
  103. ragVariables: ragPipelineVariables,
  104. allPluginInfoList: {
  105. buildInTools,
  106. customTools,
  107. workflowTools,
  108. mcpTools,
  109. dataSourceList: dataSourceList ?? [],
  110. },
  111. schemaTypeDefinitions,
  112. preferSchemaType,
  113. })
  114. }, [workflowStore, getVarType, schemaTypeDefinitions])
  115. return {
  116. getNodeAvailableVars,
  117. getCurrentVariableType,
  118. }
  119. }
  120. export const useWorkflowVariableType = () => {
  121. const store = useStoreApi()
  122. const {
  123. getNodes,
  124. } = store.getState()
  125. const { getCurrentVariableType } = useWorkflowVariables()
  126. const isChatMode = useIsChatMode()
  127. const getVarType = ({
  128. nodeId,
  129. valueSelector,
  130. }: {
  131. nodeId: string,
  132. valueSelector: ValueSelector,
  133. }) => {
  134. const node = getNodes().find(n => n.id === nodeId)
  135. const isInIteration = !!node?.data.isInIteration
  136. const iterationNode = isInIteration ? getNodes().find(n => n.id === node.parentId) : null
  137. const availableNodes = [node]
  138. const type = getCurrentVariableType({
  139. parentNode: iterationNode,
  140. valueSelector,
  141. availableNodes,
  142. isChatMode,
  143. isConstant: false,
  144. })
  145. return type as unknown as Type
  146. }
  147. return getVarType
  148. }