use-config.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { useCallback, useMemo } from 'react'
  2. import produce from 'immer'
  3. import { useStoreApi } from 'reactflow'
  4. import type { ValueSelector, Var } from '../../types'
  5. import { InputVarType, VarType } from '../../types'
  6. import type { DocExtractorNodeType } from './types'
  7. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  8. import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
  9. import {
  10. useIsChatMode,
  11. useNodesReadOnly,
  12. useWorkflow,
  13. useWorkflowVariables,
  14. } from '@/app/components/workflow/hooks'
  15. const useConfig = (id: string, payload: DocExtractorNodeType) => {
  16. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  17. const { inputs, setInputs } = useNodeCrud<DocExtractorNodeType>(id, payload)
  18. const filterVar = useCallback((varPayload: Var) => {
  19. return varPayload.type === VarType.file || varPayload.type === VarType.arrayFile
  20. }, [])
  21. const isChatMode = useIsChatMode()
  22. const store = useStoreApi()
  23. const { getBeforeNodesInSameBranch } = useWorkflow()
  24. const {
  25. getNodes,
  26. } = store.getState()
  27. const currentNode = getNodes().find(n => n.id === id)
  28. const isInIteration = payload.isInIteration
  29. const iterationNode = isInIteration ? getNodes().find(n => n.id === currentNode!.parentId) : null
  30. const isInLoop = payload.isInLoop
  31. const loopNode = isInLoop ? getNodes().find(n => n.id === currentNode!.parentId) : null
  32. const availableNodes = useMemo(() => {
  33. return getBeforeNodesInSameBranch(id)
  34. }, [getBeforeNodesInSameBranch, id])
  35. const { getCurrentVariableType } = useWorkflowVariables()
  36. const getType = useCallback((variable?: ValueSelector) => {
  37. const varType = getCurrentVariableType({
  38. parentNode: isInIteration ? iterationNode : loopNode,
  39. valueSelector: variable || [],
  40. availableNodes,
  41. isChatMode,
  42. isConstant: false,
  43. })
  44. return varType
  45. }, [getCurrentVariableType, isInIteration, availableNodes, isChatMode, iterationNode, loopNode])
  46. const handleVarChanges = useCallback((variable: ValueSelector | string) => {
  47. const newInputs = produce(inputs, (draft) => {
  48. draft.variable_selector = variable as ValueSelector
  49. draft.is_array_file = getType(draft.variable_selector) === VarType.arrayFile
  50. })
  51. setInputs(newInputs)
  52. }, [getType, inputs, setInputs])
  53. // single run
  54. const {
  55. isShowSingleRun,
  56. hideSingleRun,
  57. runningStatus,
  58. isCompleted,
  59. handleRun,
  60. handleStop,
  61. runInputData,
  62. setRunInputData,
  63. runResult,
  64. } = useOneStepRun<DocExtractorNodeType>({
  65. id,
  66. data: inputs,
  67. defaultRunInputData: { files: [] },
  68. })
  69. const varInputs = [{
  70. label: inputs.title,
  71. variable: 'files',
  72. type: InputVarType.multiFiles,
  73. required: true,
  74. }]
  75. const files = runInputData.files
  76. const setFiles = useCallback((newFiles: []) => {
  77. setRunInputData({
  78. ...runInputData,
  79. files: newFiles,
  80. })
  81. }, [runInputData, setRunInputData])
  82. return {
  83. readOnly,
  84. inputs,
  85. filterVar,
  86. handleVarChanges,
  87. // single run
  88. isShowSingleRun,
  89. hideSingleRun,
  90. runningStatus,
  91. isCompleted,
  92. handleRun,
  93. handleStop,
  94. varInputs,
  95. files,
  96. setFiles,
  97. runResult,
  98. }
  99. }
  100. export default useConfig