Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

to-form-schema.ts 6.5KB

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