to-form-schema.ts 1.7 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. _type: parameter.type,
  21. show_on: [],
  22. options: parameter.options?.map((option) => {
  23. return {
  24. ...option,
  25. show_on: [],
  26. }
  27. }),
  28. tooltip: parameter.human_description,
  29. }
  30. })
  31. return formSchemas
  32. }
  33. export const toolCredentialToFormSchemas = (parameters: ToolCredential[]) => {
  34. if (!parameters)
  35. return []
  36. const formSchemas = parameters.map((parameter) => {
  37. return {
  38. ...parameter,
  39. variable: parameter.name,
  40. label: parameter.label,
  41. tooltip: parameter.help,
  42. show_on: [],
  43. options: parameter.options?.map((option) => {
  44. return {
  45. ...option,
  46. show_on: [],
  47. }
  48. }),
  49. }
  50. })
  51. return formSchemas
  52. }
  53. export const addDefaultValue = (value: Record<string, any>, formSchemas: { variable: string; default?: any }[]) => {
  54. const newValues = { ...value }
  55. formSchemas.forEach((formSchema) => {
  56. const itemValue = value[formSchema.variable]
  57. if ((formSchema.default !== undefined) && (value === undefined || itemValue === null || itemValue === '' || itemValue === undefined))
  58. newValues[formSchema.variable] = formSchema.default
  59. })
  60. return newValues
  61. }