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.0KB

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