model-config.ts 2.0 KB

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