model-config.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.number)
  15. return ['number', item.number]
  16. if (item.file)
  17. return ['file', item.file]
  18. if (item['file-list'])
  19. return ['file-list', item['file-list']]
  20. if (item.external_data_tool)
  21. return [item.external_data_tool.type, item.external_data_tool]
  22. return ['select', item.select || {}]
  23. })()
  24. const is_context_var = dataset_query_variable === content?.variable
  25. if (type === 'string' || type === 'paragraph') {
  26. promptVariables.push({
  27. key: content.variable,
  28. name: content.label,
  29. required: content.required,
  30. type,
  31. max_length: content.max_length,
  32. options: [],
  33. is_context_var,
  34. })
  35. }
  36. else if (type === 'number') {
  37. promptVariables.push({
  38. key: content.variable,
  39. name: content.label,
  40. required: content.required,
  41. type,
  42. options: [],
  43. })
  44. }
  45. else if (type === 'select') {
  46. promptVariables.push({
  47. key: content.variable,
  48. name: content.label,
  49. required: content.required,
  50. type: 'select',
  51. options: content.options,
  52. is_context_var,
  53. })
  54. }
  55. else if (type === 'file') {
  56. promptVariables.push({
  57. key: content.variable,
  58. name: content.label,
  59. required: content.required,
  60. type,
  61. config: {
  62. allowed_file_types: content.allowed_file_types,
  63. allowed_file_extensions: content.allowed_file_extensions,
  64. allowed_file_upload_methods: content.allowed_file_upload_methods,
  65. number_limits: 1,
  66. },
  67. })
  68. }
  69. else if (type === 'file-list') {
  70. promptVariables.push({
  71. key: content.variable,
  72. name: content.label,
  73. required: content.required,
  74. type,
  75. config: {
  76. allowed_file_types: content.allowed_file_types,
  77. allowed_file_extensions: content.allowed_file_extensions,
  78. allowed_file_upload_methods: content.allowed_file_upload_methods,
  79. number_limits: content.max_length,
  80. },
  81. })
  82. }
  83. else {
  84. promptVariables.push({
  85. key: content.variable,
  86. name: content.label,
  87. required: content.required,
  88. type: content.type,
  89. enabled: content.enabled,
  90. config: content.config,
  91. icon: content.icon,
  92. icon_background: content.icon_background,
  93. is_context_var,
  94. })
  95. }
  96. })
  97. return promptVariables
  98. }
  99. export const promptVariablesToUserInputsForm = (promptVariables: PromptVariable[]) => {
  100. const userInputs: UserInputFormItem[] = []
  101. promptVariables.filter(({ key, name }) => {
  102. if (key && key.trim() && name && name.trim())
  103. return true
  104. return false
  105. }).forEach((item: any) => {
  106. if (item.type === 'string' || item.type === 'paragraph') {
  107. userInputs.push({
  108. [item.type === 'string' ? 'text-input' : 'paragraph']: {
  109. label: item.name,
  110. variable: item.key,
  111. required: item.required !== false, // default true
  112. max_length: item.max_length,
  113. default: '',
  114. },
  115. } as any)
  116. return
  117. }
  118. if (item.type === 'number') {
  119. userInputs.push({
  120. number: {
  121. label: item.name,
  122. variable: item.key,
  123. required: item.required !== false, // default true
  124. default: '',
  125. },
  126. } as any)
  127. }
  128. else if (item.type === 'select') {
  129. userInputs.push({
  130. select: {
  131. label: item.name,
  132. variable: item.key,
  133. required: item.required !== false, // default true
  134. options: item.options,
  135. default: '',
  136. },
  137. } as any)
  138. }
  139. else {
  140. userInputs.push({
  141. external_data_tool: {
  142. label: item.name,
  143. variable: item.key,
  144. enabled: item.enabled,
  145. type: item.type,
  146. config: item.config,
  147. required: item.required,
  148. icon: item.icon,
  149. icon_background: item.icon_background,
  150. },
  151. } as any)
  152. }
  153. })
  154. return userInputs
  155. }