utils.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import type { InputForm } from './type'
  2. import { InputVarType } from '@/app/components/workflow/types'
  3. import { getProcessedFiles } from '@/app/components/base/file-uploader/utils'
  4. export const processOpeningStatement = (openingStatement: string, inputs: Record<string, any>, inputsForm: InputForm[]) => {
  5. if (!openingStatement)
  6. return openingStatement
  7. return openingStatement.replace(/\{\{([^}]+)\}\}/g, (match, key) => {
  8. const name = inputs[key]
  9. if (name) { // has set value
  10. return name
  11. }
  12. const valueObj = inputsForm.find(v => v.variable === key)
  13. return valueObj ? `{{${valueObj.label}}}` : match
  14. })
  15. }
  16. export const processInputFileFromServer = (fileItem: Record<string, any>) => {
  17. return {
  18. type: fileItem.type,
  19. transfer_method: fileItem.transfer_method,
  20. url: fileItem.remote_url,
  21. upload_file_id: fileItem.related_id,
  22. }
  23. }
  24. export const getProcessedInputs = (inputs: Record<string, any>, inputsForm: InputForm[]) => {
  25. const processedInputs = { ...inputs }
  26. inputsForm.forEach((item) => {
  27. const inputValue = inputs[item.variable]
  28. if (!inputValue)
  29. return
  30. if (item.type === InputVarType.singleFile) {
  31. if ('transfer_method' in inputValue)
  32. processedInputs[item.variable] = processInputFileFromServer(inputValue)
  33. else
  34. processedInputs[item.variable] = getProcessedFiles([inputValue])[0]
  35. }
  36. else if (item.type === InputVarType.multiFiles) {
  37. if ('transfer_method' in inputValue[0])
  38. processedInputs[item.variable] = inputValue.map(processInputFileFromServer)
  39. else
  40. processedInputs[item.variable] = getProcessedFiles(inputValue)
  41. }
  42. })
  43. return processedInputs
  44. }