model-config.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { UserInputFormItem, } from '@/types/app'
  2. import { PromptVariable } from '@/models/debug'
  3. export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null) => {
  4. if (!useInputs) return []
  5. const promptVariables: PromptVariable[] = []
  6. useInputs.forEach((item: any) => {
  7. const type = item['text-input'] ? 'string' : 'select'
  8. const content = type === 'string' ? item['text-input'] : item['select']
  9. if (type === 'string') {
  10. promptVariables.push({
  11. key: content.variable,
  12. name: content.label,
  13. required: content.required,
  14. type: 'string',
  15. max_length: content.max_length,
  16. options: [],
  17. })
  18. } else {
  19. promptVariables.push({
  20. key: content.variable,
  21. name: content.label,
  22. required: content.required,
  23. type: 'select',
  24. options: content.options,
  25. })
  26. }
  27. })
  28. return promptVariables
  29. }
  30. export const promptVariablesToUserInputsForm = (promptVariables: PromptVariable[]) => {
  31. const userInputs: UserInputFormItem[] = []
  32. promptVariables.filter(({ key, name }) => {
  33. if (key && key.trim() && name && name.trim()) {
  34. return true
  35. }
  36. return false
  37. }).forEach((item: any) => {
  38. if (item.type === 'string') {
  39. userInputs.push({
  40. 'text-input': {
  41. label: item.name,
  42. variable: item.key,
  43. required: item.required === false ? false : true, // default true
  44. max_length: item.max_length,
  45. default: ''
  46. },
  47. } as any)
  48. } else {
  49. userInputs.push({
  50. 'select': {
  51. label: item.name,
  52. variable: item.key,
  53. required: item.required === false ? false : true, // default true
  54. options: item.options,
  55. default: ''
  56. },
  57. } as any)
  58. }
  59. })
  60. return userInputs
  61. }