to-form-schema.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import type { ToolCredential, ToolParameter } from '../types'
  2. const toType = (type: string) => {
  3. switch (type) {
  4. case 'string':
  5. return 'text-input'
  6. case 'number':
  7. return 'number-input'
  8. default:
  9. return type
  10. }
  11. }
  12. export const toolParametersToFormSchemas = (parameters: ToolParameter[]) => {
  13. if (!parameters)
  14. return []
  15. const formSchemas = parameters.map((parameter) => {
  16. return {
  17. ...parameter,
  18. variable: parameter.name,
  19. type: toType(parameter.type),
  20. show_on: [],
  21. options: parameter.options?.map((option) => {
  22. return {
  23. ...option,
  24. show_on: [],
  25. }
  26. }),
  27. tooltip: parameter.human_description,
  28. }
  29. })
  30. return formSchemas
  31. }
  32. export const toolCredentialToFormSchemas = (parameters: ToolCredential[]) => {
  33. if (!parameters)
  34. return []
  35. const formSchemas = parameters.map((parameter) => {
  36. return {
  37. ...parameter,
  38. variable: parameter.name,
  39. label: parameter.label,
  40. tooltip: parameter.help,
  41. show_on: [],
  42. options: parameter.options?.map((option) => {
  43. return {
  44. ...option,
  45. show_on: [],
  46. }
  47. }),
  48. }
  49. })
  50. return formSchemas
  51. }
  52. export const addDefaultValue = (value: Record<string, any>, formSchemas: { variable: string; default?: any }[]) => {
  53. const newValues = { ...value }
  54. formSchemas.forEach((formSchema) => {
  55. const itemValue = value[formSchema.variable]
  56. if ((formSchema.default !== undefined) && (value === undefined || itemValue === null || itemValue === '' || itemValue === undefined))
  57. newValues[formSchema.variable] = formSchema.default
  58. })
  59. return newValues
  60. }