use-config.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { useCallback, useMemo } from 'react'
  2. import produce from 'immer'
  3. import { useStoreApi } from 'reactflow'
  4. import { VarType } from '../../types'
  5. import type { ValueSelector, Var } from '../../types'
  6. import { WriteMode } from './types'
  7. import type { AssignerNodeOperation, AssignerNodeType } from './types'
  8. import { useGetAvailableVars } from './hooks'
  9. import { convertV1ToV2 } from './utils'
  10. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  11. import {
  12. useIsChatMode,
  13. useNodesReadOnly,
  14. useWorkflow,
  15. useWorkflowVariables,
  16. } from '@/app/components/workflow/hooks'
  17. const useConfig = (id: string, rawPayload: AssignerNodeType) => {
  18. const payload = useMemo(() => convertV1ToV2(rawPayload), [rawPayload])
  19. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  20. const isChatMode = useIsChatMode()
  21. const getAvailableVars = useGetAvailableVars()
  22. const filterVar = (varType: VarType) => {
  23. return (v: Var) => {
  24. if (varType === VarType.any)
  25. return true
  26. if (v.type === VarType.any)
  27. return true
  28. return v.type === varType
  29. }
  30. }
  31. const store = useStoreApi()
  32. const { getBeforeNodesInSameBranch } = useWorkflow()
  33. const {
  34. getNodes,
  35. } = store.getState()
  36. const currentNode = getNodes().find(n => n.id === id)
  37. const isInIteration = payload.isInIteration
  38. const iterationNode = isInIteration ? getNodes().find(n => n.id === currentNode!.parentId) : null
  39. const availableNodes = useMemo(() => {
  40. return getBeforeNodesInSameBranch(id)
  41. }, [getBeforeNodesInSameBranch, id])
  42. const { inputs, setInputs } = useNodeCrud<AssignerNodeType>(id, payload)
  43. const newSetInputs = useCallback((newInputs: AssignerNodeType) => {
  44. const finalInputs = produce(newInputs, (draft) => {
  45. if (draft.version !== '2')
  46. draft.version = '2'
  47. })
  48. setInputs(finalInputs)
  49. }, [setInputs])
  50. const { getCurrentVariableType } = useWorkflowVariables()
  51. const getAssignedVarType = useCallback((valueSelector: ValueSelector) => {
  52. return getCurrentVariableType({
  53. parentNode: iterationNode,
  54. valueSelector: valueSelector || [],
  55. availableNodes,
  56. isChatMode,
  57. isConstant: false,
  58. })
  59. }, [getCurrentVariableType, iterationNode, availableNodes, isChatMode])
  60. const handleOperationListChanges = useCallback((items: AssignerNodeOperation[]) => {
  61. const newInputs = produce(inputs, (draft) => {
  62. draft.items = [...items]
  63. })
  64. newSetInputs(newInputs)
  65. }, [inputs, newSetInputs])
  66. const writeModeTypesArr = [WriteMode.overwrite, WriteMode.clear, WriteMode.append, WriteMode.extend]
  67. const writeModeTypes = [WriteMode.overwrite, WriteMode.clear, WriteMode.set]
  68. const writeModeTypesNum = [WriteMode.increment, WriteMode.decrement, WriteMode.multiply, WriteMode.divide]
  69. const getToAssignedVarType = useCallback((assignedVarType: VarType, write_mode: WriteMode) => {
  70. if (write_mode === WriteMode.overwrite || write_mode === WriteMode.increment || write_mode === WriteMode.decrement
  71. || write_mode === WriteMode.multiply || write_mode === WriteMode.divide || write_mode === WriteMode.extend)
  72. return assignedVarType
  73. if (write_mode === WriteMode.append) {
  74. if (assignedVarType === VarType.arrayString)
  75. return VarType.string
  76. if (assignedVarType === VarType.arrayNumber)
  77. return VarType.number
  78. if (assignedVarType === VarType.arrayObject)
  79. return VarType.object
  80. }
  81. return VarType.string
  82. }, [])
  83. const filterAssignedVar = useCallback((varPayload: Var, selector: ValueSelector) => {
  84. return selector.join('.').startsWith('conversation')
  85. }, [])
  86. const filterToAssignedVar = useCallback((varPayload: Var, assignedVarType: VarType, write_mode: WriteMode) => {
  87. if (write_mode === WriteMode.overwrite || write_mode === WriteMode.extend || write_mode === WriteMode.increment
  88. || write_mode === WriteMode.decrement || write_mode === WriteMode.multiply || write_mode === WriteMode.divide) {
  89. return varPayload.type === assignedVarType
  90. }
  91. else if (write_mode === WriteMode.append) {
  92. switch (assignedVarType) {
  93. case VarType.arrayString:
  94. return varPayload.type === VarType.string
  95. case VarType.arrayNumber:
  96. return varPayload.type === VarType.number
  97. case VarType.arrayObject:
  98. return varPayload.type === VarType.object
  99. default:
  100. return false
  101. }
  102. }
  103. return true
  104. }, [])
  105. return {
  106. readOnly,
  107. inputs,
  108. handleOperationListChanges,
  109. getAssignedVarType,
  110. getToAssignedVarType,
  111. writeModeTypes,
  112. writeModeTypesArr,
  113. writeModeTypesNum,
  114. filterAssignedVar,
  115. filterToAssignedVar,
  116. getAvailableVars,
  117. filterVar,
  118. }
  119. }
  120. export default useConfig