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.

to-form-schema.ts 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import type { ToolCredential, ToolParameter } from '../types'
  2. import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  3. import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types'
  4. export const toType = (type: string) => {
  5. switch (type) {
  6. case 'string':
  7. return 'text-input'
  8. case 'number':
  9. return 'number-input'
  10. default:
  11. return type
  12. }
  13. }
  14. export const toolParametersToFormSchemas = (parameters: ToolParameter[]) => {
  15. if (!parameters)
  16. return []
  17. const formSchemas = parameters.map((parameter) => {
  18. return {
  19. ...parameter,
  20. variable: parameter.name,
  21. type: toType(parameter.type),
  22. _type: parameter.type,
  23. show_on: [],
  24. options: parameter.options?.map((option) => {
  25. return {
  26. ...option,
  27. show_on: [],
  28. }
  29. }),
  30. tooltip: parameter.human_description,
  31. }
  32. })
  33. return formSchemas
  34. }
  35. export const toolCredentialToFormSchemas = (parameters: ToolCredential[]) => {
  36. if (!parameters)
  37. return []
  38. const formSchemas = parameters.map((parameter) => {
  39. return {
  40. ...parameter,
  41. variable: parameter.name,
  42. label: parameter.label,
  43. tooltip: parameter.help,
  44. show_on: [],
  45. options: parameter.options?.map((option) => {
  46. return {
  47. ...option,
  48. show_on: [],
  49. }
  50. }),
  51. }
  52. })
  53. return formSchemas
  54. }
  55. export const addDefaultValue = (value: Record<string, any>, formSchemas: { variable: string; type: string; default?: any }[]) => {
  56. const newValues = { ...value }
  57. formSchemas.forEach((formSchema) => {
  58. const itemValue = value[formSchema.variable]
  59. if ((formSchema.default !== undefined) && (value === undefined || itemValue === null || itemValue === '' || itemValue === undefined))
  60. newValues[formSchema.variable] = formSchema.default
  61. })
  62. return newValues
  63. }
  64. const correctInitialData = (type: string, target: any, defaultValue: any) => {
  65. if (type === 'text-input' || type === 'secret-input')
  66. target.type = 'mixed'
  67. if (type === 'boolean') {
  68. if (typeof defaultValue === 'string')
  69. target.value = defaultValue === 'true' || defaultValue === '1'
  70. if (typeof defaultValue === 'boolean')
  71. target.value = defaultValue
  72. if (typeof defaultValue === 'number')
  73. target.value = defaultValue === 1
  74. }
  75. if (type === 'number-input') {
  76. if (typeof defaultValue === 'string' && defaultValue !== '')
  77. target.value = Number.parseFloat(defaultValue)
  78. }
  79. if (type === 'app-selector' || type === 'model-selector')
  80. target.value = defaultValue
  81. return target
  82. }
  83. export const generateFormValue = (value: Record<string, any>, formSchemas: { variable: string; default?: any; type: string }[], isReasoning = false) => {
  84. const newValues = {} as any
  85. formSchemas.forEach((formSchema) => {
  86. const itemValue = value[formSchema.variable]
  87. if ((formSchema.default !== undefined) && (value === undefined || itemValue === null || itemValue === '' || itemValue === undefined)) {
  88. const value = formSchema.default
  89. newValues[formSchema.variable] = {
  90. value: {
  91. type: 'constant',
  92. value: formSchema.default,
  93. },
  94. ...(isReasoning ? { auto: 1, value: null } : {}),
  95. }
  96. if (!isReasoning)
  97. newValues[formSchema.variable].value = correctInitialData(formSchema.type, newValues[formSchema.variable].value, value)
  98. }
  99. })
  100. return newValues
  101. }
  102. export const getPlainValue = (value: Record<string, any>) => {
  103. const plainValue = { ...value }
  104. Object.keys(plainValue).forEach((key) => {
  105. plainValue[key] = {
  106. ...value[key].value,
  107. }
  108. })
  109. return plainValue
  110. }
  111. export const getStructureValue = (value: Record<string, any>) => {
  112. const newValue = { ...value } as any
  113. Object.keys(newValue).forEach((key) => {
  114. newValue[key] = {
  115. value: value[key],
  116. }
  117. })
  118. return newValue
  119. }
  120. export const getConfiguredValue = (value: Record<string, any>, formSchemas: { variable: string; type: string; default?: any }[]) => {
  121. const newValues = { ...value }
  122. formSchemas.forEach((formSchema) => {
  123. const itemValue = value[formSchema.variable]
  124. if ((formSchema.default !== undefined) && (value === undefined || itemValue === null || itemValue === '' || itemValue === undefined)) {
  125. const value = formSchema.default
  126. newValues[formSchema.variable] = {
  127. type: 'constant',
  128. value: formSchema.default,
  129. }
  130. newValues[formSchema.variable] = correctInitialData(formSchema.type, newValues[formSchema.variable], value)
  131. }
  132. })
  133. return newValues
  134. }
  135. const getVarKindType = (type: FormTypeEnum) => {
  136. if (type === FormTypeEnum.file || type === FormTypeEnum.files)
  137. return VarKindType.variable
  138. if (type === FormTypeEnum.select || type === FormTypeEnum.boolean || type === FormTypeEnum.textNumber)
  139. return VarKindType.constant
  140. if (type === FormTypeEnum.textInput || type === FormTypeEnum.secretInput)
  141. return VarKindType.mixed
  142. }
  143. export const generateAgentToolValue = (value: Record<string, any>, formSchemas: { variable: string; default?: any; type: string }[], isReasoning = false) => {
  144. const newValues = {} as any
  145. if (!isReasoning) {
  146. formSchemas.forEach((formSchema) => {
  147. const itemValue = value[formSchema.variable]
  148. newValues[formSchema.variable] = {
  149. value: {
  150. type: 'constant',
  151. value: itemValue.value,
  152. },
  153. }
  154. newValues[formSchema.variable].value = correctInitialData(formSchema.type, newValues[formSchema.variable].value, itemValue.value)
  155. })
  156. }
  157. else {
  158. formSchemas.forEach((formSchema) => {
  159. const itemValue = value[formSchema.variable]
  160. if (itemValue.auto === 1) {
  161. newValues[formSchema.variable] = {
  162. auto: 1,
  163. value: null,
  164. }
  165. }
  166. else {
  167. newValues[formSchema.variable] = {
  168. auto: 0,
  169. value: itemValue.value || {
  170. type: getVarKindType(formSchema.type as FormTypeEnum),
  171. value: null,
  172. },
  173. }
  174. }
  175. })
  176. }
  177. return newValues
  178. }