model-config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import type { UserInputFormItem } from '@/types/app'
  2. import type { PromptVariable } from '@/models/debug'
  3. export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null, dataset_query_variable?: string) => {
  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. if (item.external_data_tool)
  15. return ['api', item.external_data_tool]
  16. return ['select', item.select]
  17. })()
  18. const is_context_var = dataset_query_variable === content.variable
  19. if (type === 'string' || type === 'paragraph') {
  20. promptVariables.push({
  21. key: content.variable,
  22. name: content.label,
  23. required: content.required,
  24. type,
  25. max_length: content.max_length,
  26. options: [],
  27. is_context_var,
  28. })
  29. }
  30. else if (type === 'api') {
  31. promptVariables.push({
  32. key: content.variable,
  33. name: content.label,
  34. required: content.required,
  35. type: content.type,
  36. enabled: content.enabled,
  37. config: content.config,
  38. icon: content.icon,
  39. icon_background: content.icon_background,
  40. is_context_var,
  41. })
  42. }
  43. else {
  44. promptVariables.push({
  45. key: content.variable,
  46. name: content.label,
  47. required: content.required,
  48. type: 'select',
  49. options: content.options,
  50. is_context_var,
  51. })
  52. }
  53. })
  54. return promptVariables
  55. }
  56. export const promptVariablesToUserInputsForm = (promptVariables: PromptVariable[]) => {
  57. const userInputs: UserInputFormItem[] = []
  58. promptVariables.filter(({ key, name }) => {
  59. if (key && key.trim() && name && name.trim())
  60. return true
  61. return false
  62. }).forEach((item: any) => {
  63. if (item.type === 'string' || item.type === 'paragraph') {
  64. userInputs.push({
  65. [item.type === 'string' ? 'text-input' : 'paragraph']: {
  66. label: item.name,
  67. variable: item.key,
  68. required: item.required !== false, // default true
  69. max_length: item.max_length,
  70. default: '',
  71. },
  72. } as any)
  73. }
  74. else if (item.type === 'api') {
  75. userInputs.push({
  76. external_data_tool: {
  77. label: item.name,
  78. variable: item.key,
  79. enabled: item.enabled,
  80. type: item.type,
  81. config: item.config,
  82. required: item.required,
  83. icon: item.icon,
  84. icon_background: item.icon_background,
  85. },
  86. } as any)
  87. }
  88. else {
  89. userInputs.push({
  90. select: {
  91. label: item.name,
  92. variable: item.key,
  93. required: item.required !== false, // default true
  94. options: item.options,
  95. default: '',
  96. },
  97. } as any)
  98. }
  99. })
  100. return userInputs
  101. }