Team Ai
Apppublic

Underground-Digital/Workflow-Engine

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
model-config.ts168 linesDownload Raw Back to utils
1import type { UserInputFormItem } from '@/types/app'2import type { PromptVariable } from '@/models/debug'3 4export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null, dataset_query_variable?: string) => {5  if (!useInputs)6    return []7  const promptVariables: PromptVariable[] = []8  useInputs.forEach((item: any) => {9    const isParagraph = !!item.paragraph10 11    const [type, content] = (() => {12      if (isParagraph)13        return ['paragraph', item.paragraph]14 15      if (item['text-input'])16        return ['string', item['text-input']]17 18      if (item.number)19        return ['number', item.number]20 21      if (item.file)22        return ['file', item.file]23 24      if (item['file-list'])25        return ['file-list', item['file-list']]26 27      if (item.external_data_tool)28        return [item.external_data_tool.type, item.external_data_tool]29 30      return ['select', item.select || {}]31    })()32    const is_context_var = dataset_query_variable === content?.variable33 34    if (type === 'string' || type === 'paragraph') {35      promptVariables.push({36        key: content.variable,37        name: content.label,38        required: content.required,39        type,40        max_length: content.max_length,41        options: [],42        is_context_var,43      })44    }45    else if (type === 'number') {46      promptVariables.push({47        key: content.variable,48        name: content.label,49        required: content.required,50        type,51        options: [],52      })53    }54    else if (type === 'select') {55      promptVariables.push({56        key: content.variable,57        name: content.label,58        required: content.required,59        type: 'select',60        options: content.options,61        is_context_var,62      })63    }64    else if (type === 'file') {65      promptVariables.push({66        key: content.variable,67        name: content.label,68        required: content.required,69        type,70        config: {71          allowed_file_types: content.allowed_file_types,72          allowed_file_extensions: content.allowed_file_extensions,73          allowed_file_upload_methods: content.allowed_file_upload_methods,74          number_limits: 1,75        },76      })77    }78    else if (type === 'file-list') {79      promptVariables.push({80        key: content.variable,81        name: content.label,82        required: content.required,83        type,84        config: {85          allowed_file_types: content.allowed_file_types,86          allowed_file_extensions: content.allowed_file_extensions,87          allowed_file_upload_methods: content.allowed_file_upload_methods,88          number_limits: content.max_length,89        },90      })91    }92    else {93      promptVariables.push({94        key: content.variable,95        name: content.label,96        required: content.required,97        type: content.type,98        enabled: content.enabled,99        config: content.config,100        icon: content.icon,101        icon_background: content.icon_background,102        is_context_var,103      })104    }105  })106  return promptVariables107}108 109export const promptVariablesToUserInputsForm = (promptVariables: PromptVariable[]) => {110  const userInputs: UserInputFormItem[] = []111  promptVariables.filter(({ key, name }) => {112    if (key && key.trim() && name && name.trim())113      return true114 115    return false116  }).forEach((item: any) => {117    if (item.type === 'string' || item.type === 'paragraph') {118      userInputs.push({119        [item.type === 'string' ? 'text-input' : 'paragraph']: {120          label: item.name,121          variable: item.key,122          required: item.required !== false, // default true123          max_length: item.max_length,124          default: '',125        },126      } as any)127      return128    }129    if (item.type === 'number') {130      userInputs.push({131        number: {132          label: item.name,133          variable: item.key,134          required: item.required !== false, // default true135          default: '',136        },137      } as any)138    }139    else if (item.type === 'select') {140      userInputs.push({141        select: {142          label: item.name,143          variable: item.key,144          required: item.required !== false, // default true145          options: item.options,146          default: '',147        },148      } as any)149    }150    else {151      userInputs.push({152        external_data_tool: {153          label: item.name,154          variable: item.key,155          enabled: item.enabled,156          type: item.type,157          config: item.config,158          required: item.required,159          icon: item.icon,160          icon_background: item.icon_background,161        },162      } as any)163    }164  })165 166  return userInputs167}168