model-config.ts 3.5 KB

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