SchemaForm.vue 3.14 KB
<!-- 按后端返回的 JSON Schema(pydantic model_json_schema)自动渲染参数表单:
  boolean → 开关行;integer / number → 数字框(上下限取 minimum / maximum,单位取 unit);string → 输入框;
  string[] → 可新建的标签选择。标题 / 说明取 title / description。新技能加参数,页面不用改。 -->
<script setup lang="ts">
import { computed } from 'vue'
import HelpTip from './HelpTip.vue'
import SwitchRow from './SwitchRow.vue'

const props = defineProps<{ schema: any; modelValue: Record<string, any>; disabled?: boolean }>()
const emit = defineEmits<{ 'update:modelValue': [v: Record<string, any>] }>()

type Field = { key: string; kind: 'bool' | 'int' | 'num' | 'str' | 'list' | 'other'; p: any }
const fields = computed<Field[]>(() => Object.entries(props.schema?.properties || {}).map(([key, p]: [string, any]) => {
  const t = p.type
  const kind = t === 'boolean' ? 'bool' : t === 'integer' ? 'int' : t === 'number' ? 'num' : t === 'string' ? 'str'
    : t === 'array' && p.items?.type === 'string' ? 'list' : 'other'
  return { key, kind, p }
}))
function set(key: string, val: any) {
  emit('update:modelValue', { ...props.modelValue, [key]: val })
}
</script>

<template>
  <div class="schema-form">
    <template v-for="f in fields" :key="f.key">
      <SwitchRow v-if="f.kind === 'bool'" :label="f.p.title || f.key" :hint="f.p.description">
        <el-switch :model-value="!!modelValue[f.key]" :disabled="disabled" @update:model-value="(v: any) => set(f.key, v)" />
      </SwitchRow>
      <div v-else-if="f.kind !== 'other'" class="sf-item">
        <div class="sf-label">{{ f.p.title || f.key }}<HelpTip v-if="f.p.description" :text="f.p.description" /></div>
        <div class="sf-ctrl">
          <template v-if="f.kind === 'int' || f.kind === 'num'">
            <el-input-number :model-value="modelValue[f.key]" :min="f.p.minimum ?? f.p.exclusiveMinimum" :max="f.p.maximum ?? f.p.exclusiveMaximum"
                             :step="f.kind === 'int' ? 1 : 0.1" :precision="f.kind === 'int' ? 0 : undefined" :disabled="disabled"
                             @update:model-value="(v: any) => set(f.key, v)" />
            <span v-if="f.p.unit" class="unit">{{ f.p.unit }}</span>
          </template>
          <el-input v-else-if="f.kind === 'str'" :model-value="modelValue[f.key]" :maxlength="f.p.maxLength" :disabled="disabled"
                    @update:model-value="(v: any) => set(f.key, v)" />
          <el-select v-else :model-value="modelValue[f.key] || []" multiple filterable allow-create default-first-option
                     :disabled="disabled" placeholder="输入后回车添加" @update:model-value="(v: any) => set(f.key, v)" />
        </div>
      </div>
    </template>
  </div>
</template>

<style scoped>
.schema-form { display: grid; gap: var(--sp-3); }
.sf-item { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-4); flex-wrap: wrap; }
.sf-label { font-weight: 600; font-size: 13.5px; color: var(--c-text); display: flex; align-items: center; }
.sf-ctrl { display: flex; align-items: center; gap: var(--sp-2); }
.unit { color: var(--c-text-3); font-size: 13px; }
</style>