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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. return key && key.trim() && name && name.trim()
  103. }).forEach((item: any) => {
  104. if (item.type === 'string' || item.type === 'paragraph') {
  105. userInputs.push({
  106. [item.type === 'string' ? 'text-input' : 'paragraph']: {
  107. label: item.name,
  108. variable: item.key,
  109. required: item.required !== false, // default true
  110. max_length: item.max_length,
  111. default: '',
  112. },
  113. } as any)
  114. return
  115. }
  116. if (item.type === 'number') {
  117. userInputs.push({
  118. number: {
  119. label: item.name,
  120. variable: item.key,
  121. required: item.required !== false, // default true
  122. default: '',
  123. },
  124. } as any)
  125. }
  126. else if (item.type === 'select') {
  127. userInputs.push({
  128. select: {
  129. label: item.name,
  130. variable: item.key,
  131. required: item.required !== false, // default true
  132. options: item.options,
  133. default: '',
  134. },
  135. } as any)
  136. }
  137. else {
  138. userInputs.push({
  139. external_data_tool: {
  140. label: item.name,
  141. variable: item.key,
  142. enabled: item.enabled,
  143. type: item.type,
  144. config: item.config,
  145. required: item.required,
  146. icon: item.icon,
  147. icon_background: item.icon_background,
  148. },
  149. } as any)
  150. }
  151. })
  152. return userInputs
  153. }