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-single-run-form-params.ts 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import type { MutableRefObject } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
  4. import type { ValueSelector } from '@/app/components/workflow/types'
  5. import { type InputVar, InputVarType, type Variable } from '@/app/components/workflow/types'
  6. import type { StartNodeType } from './types'
  7. import { useIsChatMode } from '../../hooks'
  8. type Params = {
  9. id: string,
  10. payload: StartNodeType,
  11. runInputData: Record<string, any>
  12. runInputDataRef: MutableRefObject<Record<string, any>>
  13. getInputVars: (textList: string[]) => InputVar[]
  14. setRunInputData: (data: Record<string, any>) => void
  15. toVarInputs: (variables: Variable[]) => InputVar[]
  16. }
  17. const useSingleRunFormParams = ({
  18. id,
  19. payload,
  20. runInputData,
  21. setRunInputData,
  22. }: Params) => {
  23. const { t } = useTranslation()
  24. const isChatMode = useIsChatMode()
  25. const forms = (() => {
  26. const forms: FormProps[] = []
  27. const inputs: InputVar[] = payload.variables.map((item) => {
  28. return {
  29. ...item,
  30. getVarValueFromDependent: true,
  31. }
  32. })
  33. if (isChatMode) {
  34. inputs.push({
  35. label: 'sys.query',
  36. variable: '#sys.query#',
  37. type: InputVarType.textInput,
  38. required: true,
  39. })
  40. }
  41. inputs.push({
  42. label: 'sys.files',
  43. variable: '#sys.files#',
  44. type: InputVarType.multiFiles,
  45. required: false,
  46. })
  47. forms.push(
  48. {
  49. label: t('workflow.nodes.llm.singleRun.variable')!,
  50. inputs,
  51. values: runInputData,
  52. onChange: setRunInputData,
  53. },
  54. )
  55. return forms
  56. })()
  57. const getDependentVars = () => {
  58. const inputVars = payload.variables.map((item) => {
  59. return [id, item.variable]
  60. })
  61. const vars: ValueSelector[] = [...inputVars, ['sys', 'files']]
  62. if (isChatMode)
  63. vars.push(['sys', 'query'])
  64. return vars
  65. }
  66. const getDependentVar = (variable: string) => {
  67. return [id, variable]
  68. }
  69. return {
  70. forms,
  71. getDependentVars,
  72. getDependentVar,
  73. }
  74. }
  75. export default useSingleRunFormParams