use-config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 availableNodes = useMemo(() => {
  31. return getBeforeNodesInSameBranch(id)
  32. }, [getBeforeNodesInSameBranch, id])
  33. const { getCurrentVariableType } = useWorkflowVariables()
  34. const getType = useCallback((variable?: ValueSelector) => {
  35. const varType = getCurrentVariableType({
  36. parentNode: iterationNode,
  37. valueSelector: variable || [],
  38. availableNodes,
  39. isChatMode,
  40. isConstant: false,
  41. })
  42. return varType
  43. }, [getCurrentVariableType, availableNodes, isChatMode, iterationNode])
  44. const handleVarChanges = useCallback((variable: ValueSelector | string) => {
  45. const newInputs = produce(inputs, (draft) => {
  46. draft.variable_selector = variable as ValueSelector
  47. draft.is_array_file = getType(draft.variable_selector) === VarType.arrayFile
  48. })
  49. setInputs(newInputs)
  50. }, [getType, inputs, setInputs])
  51. // single run
  52. const {
  53. isShowSingleRun,
  54. hideSingleRun,
  55. runningStatus,
  56. isCompleted,
  57. handleRun,
  58. handleStop,
  59. runInputData,
  60. setRunInputData,
  61. runResult,
  62. } = useOneStepRun<DocExtractorNodeType>({
  63. id,
  64. data: inputs,
  65. defaultRunInputData: { files: [] },
  66. })
  67. const varInputs = [{
  68. label: inputs.title,
  69. variable: 'files',
  70. type: InputVarType.multiFiles,
  71. required: true,
  72. }]
  73. const files = runInputData.files
  74. const setFiles = useCallback((newFiles: []) => {
  75. setRunInputData({
  76. ...runInputData,
  77. files: newFiles,
  78. })
  79. }, [runInputData, setRunInputData])
  80. return {
  81. readOnly,
  82. inputs,
  83. filterVar,
  84. handleVarChanges,
  85. // single run
  86. isShowSingleRun,
  87. hideSingleRun,
  88. runningStatus,
  89. isCompleted,
  90. handleRun,
  91. handleStop,
  92. varInputs,
  93. files,
  94. setFiles,
  95. runResult,
  96. }
  97. }
  98. export default useConfig