feat: update JSON schema configuration to include localized help documentation and adjust boolean type handling

This commit is contained in:
twwu 2025-03-27 12:54:33 +08:00
parent b52a0d6fa5
commit d29b3292f0
5 changed files with 34 additions and 23 deletions

View File

@ -14,6 +14,7 @@ import { MittProvider, VisualEditorContextProvider } from './visual-editor/conte
import ErrorMessage from './error-message'
import { useVisualEditorStore } from './visual-editor/store'
import Toast from '@/app/components/base/toast'
import { useGetLanguage } from '@/context/i18n'
type JsonSchemaConfigProps = {
defaultSchema?: SchemaRoot
@ -38,12 +39,21 @@ const DEFAULT_SCHEMA: SchemaRoot = {
additionalProperties: false,
}
const HELP_DOC_URL = {
zh_Hans: 'https://docs.dify.ai/zh-hans/guides/workflow/structured-outputs',
en_US: 'https://docs.dify.ai/guides/workflow/structured-outputs',
ja_JP: 'https://docs.dify.ai/ja-jp/guides/workflow/structured-outputs',
}
type LocaleKey = keyof typeof HELP_DOC_URL
const JsonSchemaConfig: FC<JsonSchemaConfigProps> = ({
defaultSchema,
onSave,
onClose,
}) => {
const { t } = useTranslation()
const locale = useGetLanguage() as LocaleKey
const [currentTab, setCurrentTab] = useState(SchemaView.VisualEditor)
const [jsonSchema, setJsonSchema] = useState(defaultSchema || DEFAULT_SCHEMA)
const [json, setJson] = useState(JSON.stringify(jsonSchema, null, 2))
@ -227,7 +237,7 @@ const JsonSchemaConfig: FC<JsonSchemaConfigProps> = ({
<div className='flex items-center gap-x-2 p-6 pt-5'>
<a
className='flex grow items-center gap-x-1 text-text-accent'
href='https://json-schema.org/' // todo: replace with documentation link
href={HELP_DOC_URL[locale]}
target='_blank'
rel='noopener noreferrer'
>

View File

@ -38,11 +38,11 @@ type EditCardProps = {
const TYPE_OPTIONS = [
{ value: Type.string, text: 'string' },
{ value: Type.number, text: 'number' },
{ value: Type.boolean, text: 'boolean' },
// { value: Type.boolean, text: 'boolean' },
{ value: Type.object, text: 'object' },
{ value: ArrayType.string, text: 'array[string]' },
{ value: ArrayType.number, text: 'array[number]' },
{ value: ArrayType.boolean, text: 'array[boolean]' },
// { value: ArrayType.boolean, text: 'array[boolean]' },
{ value: ArrayType.object, text: 'array[object]' },
]

View File

@ -130,12 +130,12 @@ export const useSchemaNodeOperations = (props: VisualEditorProps) => {
type: Type.number,
}
break
case ArrayType.boolean:
schema.type = Type.array
schema.items = {
type: Type.boolean,
}
break
// case ArrayType.boolean:
// schema.type = Type.array
// schema.items = {
// type: Type.boolean,
// }
// break
case ArrayType.object:
schema.type = Type.array
schema.items = {
@ -304,12 +304,12 @@ export const useSchemaNodeOperations = (props: VisualEditorProps) => {
type: Type.number,
}
break
case ArrayType.boolean:
schema.type = Type.array
schema.items = {
type: Type.boolean,
}
break
// case ArrayType.boolean:
// schema.type = Type.array
// schema.items = {
// type: Type.boolean,
// }
// break
case ArrayType.object:
schema.type = Type.array
schema.items = {
@ -393,12 +393,12 @@ export const useSchemaNodeOperations = (props: VisualEditorProps) => {
type: Type.number,
}
break
case ArrayType.boolean:
schema.type = Type.array
schema.items = {
type: Type.boolean,
}
break
// case ArrayType.boolean:
// schema.type = Type.array
// schema.items = {
// type: Type.boolean,
// }
// break
case ArrayType.object:
schema.type = Type.array
schema.items = {

View File

@ -28,7 +28,8 @@ export const getHasChildren = (schema: Field) => {
export const inferType = (value: any): Type => {
if (Array.isArray(value)) return Type.array
if (typeof value === 'boolean') return Type.boolean
// type boolean will be treated as string
if (typeof value === 'boolean') return Type.string
if (typeof value === 'number') return Type.number
if (typeof value === 'string') return Type.string
if (typeof value === 'object') return Type.object

View File

@ -33,7 +33,7 @@ export const languages = data.languages
export const LanguagesSupported = languages.filter(item => item.supported).map(item => item.value)
export const getLanguage = (locale: string) => {
if (locale === 'zh-Hans')
if (['zh-Hans', 'ja-JP'].includes(locale))
return locale.replace('-', '_')
return LanguagesSupported[0].replace('-', '_')