Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

use-single-run-form-params.ts 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import type { MutableRefObject } from 'react'
  2. import type { InputVar, Variable } from '@/app/components/workflow/types'
  3. import { useCallback, useMemo, useState } from 'react'
  4. import useNodeCrud from '../_base/hooks/use-node-crud'
  5. import { type ToolNodeType, VarType } from './types'
  6. import type { ValueSelector } from '@/app/components/workflow/types'
  7. import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
  8. import produce from 'immer'
  9. import type { NodeTracing } from '@/types/workflow'
  10. import { useTranslation } from 'react-i18next'
  11. import formatToTracingNodeList from '@/app/components/workflow/run/utils/format-log'
  12. import { useToolIcon } from '../../hooks'
  13. type Params = {
  14. id: string,
  15. payload: ToolNodeType,
  16. runInputData: Record<string, any>
  17. runInputDataRef: MutableRefObject<Record<string, any>>
  18. getInputVars: (textList: string[]) => InputVar[]
  19. setRunInputData: (data: Record<string, any>) => void
  20. toVarInputs: (variables: Variable[]) => InputVar[]
  21. runResult: NodeTracing
  22. }
  23. const useSingleRunFormParams = ({
  24. id,
  25. payload,
  26. getInputVars,
  27. setRunInputData,
  28. runResult,
  29. }: Params) => {
  30. const { t } = useTranslation()
  31. const { inputs } = useNodeCrud<ToolNodeType>(id, payload)
  32. const hadVarParams = Object.keys(inputs.tool_parameters)
  33. .filter(key => inputs.tool_parameters[key].type !== VarType.constant)
  34. .map(k => inputs.tool_parameters[k])
  35. const hadVarSettings = Object.keys(inputs.tool_configurations)
  36. .filter(key => typeof inputs.tool_configurations[key] === 'object' && inputs.tool_configurations[key].type && inputs.tool_configurations[key].type !== VarType.constant)
  37. .map(k => inputs.tool_configurations[k])
  38. const varInputs = getInputVars([...hadVarParams, ...hadVarSettings].map((p) => {
  39. if (p.type === VarType.variable) {
  40. // handle the old wrong value not crash the page
  41. if (!(p.value as any).join)
  42. return `{{#${p.value}#}}`
  43. return `{{#${(p.value as ValueSelector).join('.')}#}}`
  44. }
  45. return p.value as string
  46. }))
  47. const [inputVarValues, doSetInputVarValues] = useState<Record<string, any>>({})
  48. const setInputVarValues = useCallback((value: Record<string, any>) => {
  49. doSetInputVarValues(value)
  50. setRunInputData(value)
  51. }, [setRunInputData])
  52. const inputVarValuesWithConstantValue = useCallback(() => {
  53. const res = produce(inputVarValues, (draft) => {
  54. Object.keys(inputs.tool_parameters).forEach((key: string) => {
  55. const { type, value } = inputs.tool_parameters[key]
  56. if (type === VarType.constant && (value === undefined || value === null)) {
  57. if(!draft.tool_parameters || !draft.tool_parameters[key])
  58. return
  59. draft[key] = value
  60. }
  61. })
  62. })
  63. return res
  64. }, [inputs.tool_parameters, inputVarValues])
  65. const forms = useMemo(() => {
  66. const forms: FormProps[] = [{
  67. inputs: varInputs,
  68. values: inputVarValuesWithConstantValue(),
  69. onChange: setInputVarValues,
  70. }]
  71. return forms
  72. }, [inputVarValuesWithConstantValue, setInputVarValues, varInputs])
  73. const nodeInfo = useMemo(() => {
  74. if (!runResult)
  75. return null
  76. return formatToTracingNodeList([runResult], t)[0]
  77. }, [runResult, t])
  78. const toolIcon = useToolIcon(payload)
  79. const getDependentVars = () => {
  80. return varInputs.map(item => item.variable.slice(1, -1).split('.'))
  81. }
  82. return {
  83. forms,
  84. nodeInfo,
  85. toolIcon,
  86. getDependentVars,
  87. }
  88. }
  89. export default useSingleRunFormParams