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 5.6KB

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