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.

utils.ts 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { z } from 'zod'
  2. import { ArrayType, Type } from './types'
  3. import type { ArrayItems, Field, LLMNodeType } from './types'
  4. import { draft07Validator, forbidBooleanProperties } from '@/utils/validators'
  5. import type { ValidationError } from 'jsonschema'
  6. export const checkNodeValid = (_payload: LLMNodeType) => {
  7. return true
  8. }
  9. export const getFieldType = (field: Field) => {
  10. const { type, items } = field
  11. if (type !== Type.array || !items)
  12. return type
  13. return ArrayType[items.type as keyof typeof ArrayType]
  14. }
  15. export const getHasChildren = (schema: Field) => {
  16. const complexTypes = [Type.object, Type.array]
  17. if (!complexTypes.includes(schema.type))
  18. return false
  19. if (schema.type === Type.object)
  20. return schema.properties && Object.keys(schema.properties).length > 0
  21. if (schema.type === Type.array)
  22. return schema.items && schema.items.type === Type.object && schema.items.properties && Object.keys(schema.items.properties).length > 0
  23. }
  24. export const getTypeOf = (target: any) => {
  25. if (target === null) return 'null'
  26. if (typeof target !== 'object') {
  27. return typeof target
  28. }
  29. else {
  30. return Object.prototype.toString
  31. .call(target)
  32. .slice(8, -1)
  33. .toLocaleLowerCase()
  34. }
  35. }
  36. export const inferType = (value: any): Type => {
  37. const type = getTypeOf(value)
  38. if (type === 'array') return Type.array
  39. // type boolean will be treated as string
  40. if (type === 'boolean') return Type.string
  41. if (type === 'number') return Type.number
  42. if (type === 'string') return Type.string
  43. if (type === 'object') return Type.object
  44. return Type.string
  45. }
  46. export const jsonToSchema = (json: any): Field => {
  47. const schema: Field = {
  48. type: inferType(json),
  49. }
  50. if (schema.type === Type.object) {
  51. schema.properties = {}
  52. schema.required = []
  53. schema.additionalProperties = false
  54. Object.entries(json).forEach(([key, value]) => {
  55. schema.properties![key] = jsonToSchema(value)
  56. schema.required!.push(key)
  57. })
  58. }
  59. else if (schema.type === Type.array) {
  60. schema.items = jsonToSchema(json[0]) as ArrayItems
  61. }
  62. return schema
  63. }
  64. export const checkJsonDepth = (json: any) => {
  65. if (!json || getTypeOf(json) !== 'object')
  66. return 0
  67. let maxDepth = 0
  68. if (getTypeOf(json) === 'array') {
  69. if (json[0] && getTypeOf(json[0]) === 'object')
  70. maxDepth = checkJsonDepth(json[0])
  71. }
  72. else if (getTypeOf(json) === 'object') {
  73. const propertyDepths = Object.values(json).map(value => checkJsonDepth(value))
  74. maxDepth = propertyDepths.length ? Math.max(...propertyDepths) + 1 : 1
  75. }
  76. return maxDepth
  77. }
  78. export const checkJsonSchemaDepth = (schema: Field) => {
  79. if (!schema || getTypeOf(schema) !== 'object')
  80. return 0
  81. let maxDepth = 0
  82. if (schema.type === Type.object && schema.properties) {
  83. const propertyDepths = Object.values(schema.properties).map(value => checkJsonSchemaDepth(value))
  84. maxDepth = propertyDepths.length ? Math.max(...propertyDepths) + 1 : 1
  85. }
  86. else if (schema.type === Type.array && schema.items && schema.items.type === Type.object) {
  87. maxDepth = checkJsonSchemaDepth(schema.items) + 1
  88. }
  89. return maxDepth
  90. }
  91. export const findPropertyWithPath = (target: any, path: string[]) => {
  92. let current = target
  93. for (const key of path)
  94. current = current[key]
  95. return current
  96. }
  97. export const validateSchemaAgainstDraft7 = (schemaToValidate: any) => {
  98. // First check against Draft-07
  99. const result = draft07Validator(schemaToValidate)
  100. // Then apply custom rule
  101. const customErrors = forbidBooleanProperties(schemaToValidate)
  102. return [...result.errors, ...customErrors]
  103. }
  104. export const getValidationErrorMessage = (errors: Array<ValidationError | string>) => {
  105. const message = errors.map((error) => {
  106. if (typeof error === 'string')
  107. return error
  108. else
  109. return `Error: ${error.stack}\n`
  110. }).join('')
  111. return message
  112. }
  113. // Previous Not support boolean type, so transform boolean to string when paste it into schema editor
  114. export const convertBooleanToString = (schema: any) => {
  115. if (schema.type === Type.boolean)
  116. schema.type = Type.string
  117. if (schema.type === Type.array && schema.items && schema.items.type === Type.boolean)
  118. schema.items.type = Type.string
  119. if (schema.type === Type.object) {
  120. schema.properties = Object.entries(schema.properties).reduce((acc, [key, value]) => {
  121. acc[key] = convertBooleanToString(value)
  122. return acc
  123. }, {} as any)
  124. }
  125. if (schema.type === Type.array && schema.items && schema.items.type === Type.object) {
  126. schema.items.properties = Object.entries(schema.items.properties).reduce((acc, [key, value]) => {
  127. acc[key] = convertBooleanToString(value)
  128. return acc
  129. }, {} as any)
  130. }
  131. return schema
  132. }
  133. const schemaRootObject = z.object({
  134. type: z.literal('object'),
  135. properties: z.record(z.string(), z.any()),
  136. required: z.array(z.string()),
  137. additionalProperties: z.boolean().optional(),
  138. })
  139. export const preValidateSchema = (schema: any) => {
  140. const result = schemaRootObject.safeParse(schema)
  141. return result
  142. }