model-config.ts 3.5 KB

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