You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

model-config.ts 4.8KB

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