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 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import type { MutableRefObject } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import type { InputVar, Variable } from '@/app/components/workflow/types'
  4. import { InputVarType } from '@/app/components/workflow/types'
  5. import { useCallback, useMemo } from 'react'
  6. import type { KnowledgeRetrievalNodeType } from './types'
  7. const i18nPrefix = 'workflow.nodes.knowledgeRetrieval'
  8. type Params = {
  9. id: string,
  10. payload: KnowledgeRetrievalNodeType
  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. payload,
  19. runInputData,
  20. setRunInputData,
  21. }: Params) => {
  22. const { t } = useTranslation()
  23. const query = runInputData.query
  24. const setQuery = useCallback((newQuery: string) => {
  25. setRunInputData({
  26. ...runInputData,
  27. query: newQuery,
  28. })
  29. }, [runInputData, setRunInputData])
  30. const forms = useMemo(() => {
  31. return [
  32. {
  33. inputs: [{
  34. label: t(`${i18nPrefix}.queryVariable`)!,
  35. variable: 'query',
  36. type: InputVarType.paragraph,
  37. required: true,
  38. }],
  39. values: { query },
  40. onChange: (keyValue: Record<string, any>) => setQuery(keyValue.query),
  41. },
  42. ]
  43. }, [query, setQuery, t])
  44. const getDependentVars = () => {
  45. return [payload.query_variable_selector]
  46. }
  47. const getDependentVar = (variable: string) => {
  48. if(variable === 'query')
  49. return payload.query_variable_selector
  50. }
  51. return {
  52. forms,
  53. getDependentVars,
  54. getDependentVar,
  55. }
  56. }
  57. export default useSingleRunFormParams