mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-18 02:35:56 +08:00
feat: update JSON Schema handling and improve UI interactions
This commit is contained in:
parent
b305c17bc3
commit
1305e5f8c1
@ -184,7 +184,7 @@ const JsonImporter: FC<JsonImporterProps> = ({
|
|||||||
{t('common.operation.cancel')}
|
{t('common.operation.cancel')}
|
||||||
</Button>
|
</Button>
|
||||||
<Button variant='primary' onClick={handleSubmit}>
|
<Button variant='primary' onClick={handleSubmit}>
|
||||||
{t('common.operation.confirm')}
|
{t('common.operation.submit')}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -14,6 +14,7 @@ import { useMittContext } from './context'
|
|||||||
import type { EditData } from './visual-editor/edit-card'
|
import type { EditData } from './visual-editor/edit-card'
|
||||||
import produce from 'immer'
|
import produce from 'immer'
|
||||||
import Toast from '@/app/components/base/toast'
|
import Toast from '@/app/components/base/toast'
|
||||||
|
import { jsonToSchema } from '../../utils'
|
||||||
|
|
||||||
type JsonSchemaConfigProps = {
|
type JsonSchemaConfigProps = {
|
||||||
defaultSchema?: SchemaRoot
|
defaultSchema?: SchemaRoot
|
||||||
@ -470,9 +471,14 @@ const JsonSchemaConfig: FC<JsonSchemaConfigProps> = ({
|
|||||||
setBtnWidth(width + 32)
|
setBtnWidth(width + 32)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const handleApplySchema = useCallback(() => {}, [])
|
const handleApplySchema = useCallback((schema: SchemaRoot) => {
|
||||||
|
setJsonSchema(schema)
|
||||||
|
}, [])
|
||||||
|
|
||||||
const handleSubmit = useCallback(() => {}, [])
|
const handleSubmit = useCallback((schema: string) => {
|
||||||
|
const jsonSchema = jsonToSchema(schema) as SchemaRoot
|
||||||
|
setJsonSchema(jsonSchema)
|
||||||
|
}, [])
|
||||||
|
|
||||||
const handleSchemaEditorUpdate = useCallback((schema: string) => {
|
const handleSchemaEditorUpdate = useCallback((schema: string) => {
|
||||||
setJson(schema)
|
setJson(schema)
|
||||||
@ -541,7 +547,7 @@ const JsonSchemaConfig: FC<JsonSchemaConfigProps> = ({
|
|||||||
<div className='flex items-center p-6 pt-5 gap-x-2'>
|
<div className='flex items-center p-6 pt-5 gap-x-2'>
|
||||||
<a
|
<a
|
||||||
className='flex items-center gap-x-1 grow text-text-accent'
|
className='flex items-center gap-x-1 grow text-text-accent'
|
||||||
href='https://json-schema.org/'
|
href='https://json-schema.org/' // todo: replace with documentation link
|
||||||
target='_blank'
|
target='_blank'
|
||||||
rel='noopener noreferrer'
|
rel='noopener noreferrer'
|
||||||
>
|
>
|
||||||
|
@ -19,7 +19,7 @@ const Card: FC<CardProps> = ({
|
|||||||
return (
|
return (
|
||||||
<div className='flex flex-col py-0.5'>
|
<div className='flex flex-col py-0.5'>
|
||||||
<div className='flex items-center gap-x-1 p-0.5 pl-1'>
|
<div className='flex items-center gap-x-1 p-0.5 pl-1'>
|
||||||
<div className='px-1 py-0.5 text-text-primary system-sm-semibold'>
|
<div className='px-1 py-0.5 text-text-primary system-sm-semibold truncate'>
|
||||||
{name}
|
{name}
|
||||||
</div>
|
</div>
|
||||||
<div className='px-1 py-0.5 text-text-tertiary system-xs-medium'>
|
<div className='px-1 py-0.5 text-text-tertiary system-xs-medium'>
|
||||||
@ -35,7 +35,7 @@ const Card: FC<CardProps> = ({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{description && (
|
{description && (
|
||||||
<div className='px-2 pb-1 text-text-tertiary system-xs-regular'>
|
<div className='px-2 pb-1 text-text-tertiary system-xs-regular truncate'>
|
||||||
{description}
|
{description}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
import type { Field } from '../../../types'
|
import type { SchemaRoot } from '../../../types'
|
||||||
import SchemaNode from './schema-node'
|
import SchemaNode from './schema-node'
|
||||||
|
|
||||||
type VisualEditorProps = {
|
type VisualEditorProps = {
|
||||||
schema: Field
|
schema: SchemaRoot
|
||||||
}
|
}
|
||||||
|
|
||||||
const VisualEditor: FC<VisualEditorProps> = ({
|
const VisualEditor: FC<VisualEditorProps> = ({
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { ArrayType, Type } from './types'
|
import { ArrayType, Type } from './types'
|
||||||
import type { Field, LLMNodeType } from './types'
|
import type { ArrayItems, Field, LLMNodeType } from './types'
|
||||||
|
|
||||||
export const checkNodeValid = (payload: LLMNodeType) => {
|
export const checkNodeValid = (payload: LLMNodeType) => {
|
||||||
return true
|
return true
|
||||||
@ -22,3 +22,34 @@ export const getHasChildren = (schema: Field) => {
|
|||||||
if (schema.type === Type.array)
|
if (schema.type === Type.array)
|
||||||
return schema.items && schema.items.type === Type.object && schema.items.properties && Object.keys(schema.items.properties).length > 0
|
return schema.items && schema.items.type === Type.object && schema.items.properties && Object.keys(schema.items.properties).length > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const inferType = (value: any): Type => {
|
||||||
|
if (Array.isArray(value)) return Type.array
|
||||||
|
if (typeof value === 'boolean') return Type.boolean
|
||||||
|
if (typeof value === 'number') return Type.number
|
||||||
|
if (typeof value === 'string') return Type.string
|
||||||
|
if (typeof value === 'object') return Type.object
|
||||||
|
return Type.string
|
||||||
|
}
|
||||||
|
|
||||||
|
export function jsonToSchema(json: any): Field {
|
||||||
|
const schema: Field = {
|
||||||
|
type: inferType(json),
|
||||||
|
}
|
||||||
|
|
||||||
|
if (schema.type === 'object') {
|
||||||
|
schema.properties = {}
|
||||||
|
schema.required = []
|
||||||
|
schema.additionalProperties = false
|
||||||
|
|
||||||
|
Object.entries(json).forEach(([key, value]) => {
|
||||||
|
schema.properties![key] = jsonToSchema(value)
|
||||||
|
schema.required!.push(key)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else if (schema.type === Type.array && json.length > 0) {
|
||||||
|
schema.items = jsonToSchema(json[0]) as ArrayItems
|
||||||
|
}
|
||||||
|
|
||||||
|
return schema
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user