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-config.ts 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 { VarType } from '../../types'
  6. import type { DocExtractorNodeType } from './types'
  7. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  8. import {
  9. useIsChatMode,
  10. useNodesReadOnly,
  11. useWorkflow,
  12. useWorkflowVariables,
  13. } from '@/app/components/workflow/hooks'
  14. const useConfig = (id: string, payload: DocExtractorNodeType) => {
  15. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  16. const { inputs, setInputs } = useNodeCrud<DocExtractorNodeType>(id, payload)
  17. const filterVar = useCallback((varPayload: Var) => {
  18. return varPayload.type === VarType.file || varPayload.type === VarType.arrayFile
  19. }, [])
  20. const isChatMode = useIsChatMode()
  21. const store = useStoreApi()
  22. const { getBeforeNodesInSameBranch } = useWorkflow()
  23. const {
  24. getNodes,
  25. } = store.getState()
  26. const currentNode = getNodes().find(n => n.id === id)
  27. const isInIteration = payload.isInIteration
  28. const iterationNode = isInIteration ? getNodes().find(n => n.id === currentNode!.parentId) : null
  29. const isInLoop = payload.isInLoop
  30. const loopNode = isInLoop ? getNodes().find(n => n.id === currentNode!.parentId) : null
  31. const availableNodes = useMemo(() => {
  32. return getBeforeNodesInSameBranch(id)
  33. }, [getBeforeNodesInSameBranch, id])
  34. const { getCurrentVariableType } = useWorkflowVariables()
  35. const getType = useCallback((variable?: ValueSelector) => {
  36. const varType = getCurrentVariableType({
  37. parentNode: isInIteration ? iterationNode : loopNode,
  38. valueSelector: variable || [],
  39. availableNodes,
  40. isChatMode,
  41. isConstant: false,
  42. })
  43. return varType
  44. }, [getCurrentVariableType, isInIteration, availableNodes, isChatMode, iterationNode, loopNode])
  45. const handleVarChanges = useCallback((variable: ValueSelector | string) => {
  46. const newInputs = produce(inputs, (draft) => {
  47. draft.variable_selector = variable as ValueSelector
  48. draft.is_array_file = getType(draft.variable_selector) === VarType.arrayFile
  49. })
  50. setInputs(newInputs)
  51. }, [getType, inputs, setInputs])
  52. return {
  53. readOnly,
  54. inputs,
  55. filterVar,
  56. handleVarChanges,
  57. }
  58. }
  59. export default useConfig