diff --git a/docs/AI_AGENT_AUTO_DEVELOPMENT.md b/docs/AI_AGENT_AUTO_DEVELOPMENT.md index f7690af..c81ab7b 100644 --- a/docs/AI_AGENT_AUTO_DEVELOPMENT.md +++ b/docs/AI_AGENT_AUTO_DEVELOPMENT.md @@ -102,8 +102,9 @@ curl -X POST http://localhost:8093/api/ai/features/auto-develop \ ## 自动链路 1. 创建 `AiTask`。 -2. 生成 `ENTITY`、`PAGE_SCHEMA`、`SERVICEFLOW`、`MENU`、`TEST` 草稿。 -3. 对草稿执行基础门禁,包括 amis schema、服务流 XML、实体字段和 smoke 计划。 +2. 生成 `ENTITY`、`PAGE_SCHEMA`、`SERVICEFLOW`、`MENU`、`TEST` 草稿。`PAGE_SCHEMA` + 先生成 EIP4 PageBlueprint,再由平台编译器输出 amis schema;草稿 metadata 保留 blueprint 证据。 +3. 对草稿执行基础门禁,包括 amis schema、typed page schema、服务流 XML、实体字段和 smoke 计划。 4. 生成待人审 `AiChangeSet`。 5. 生成 release plan。 6. 本地 apply:实体走本机 `/api/meta/entity`,页面/服务流写本地库;如果本地最小库缺少 `sys_menu`,菜单发布标记为 `SKIPPED`,不阻断本地 gate,部署包仍保留菜单 SQL 供线上人审。 diff --git a/docs/AI_AGENT_PAGE_BLUEPRINT_CONTRACT.md b/docs/AI_AGENT_PAGE_BLUEPRINT_CONTRACT.md new file mode 100644 index 0000000..3715951 --- /dev/null +++ b/docs/AI_AGENT_PAGE_BLUEPRINT_CONTRACT.md @@ -0,0 +1,82 @@ +# AI Agent Page Blueprint Contract + +日期:2026-06-17 + +## 目标 + +EIP4 的 AI Agent 不直接自由生成 amis JSON。Agent 应先生成 EIP4 PageBlueprint,再由平台编译器输出 amis schema,并继续通过 typed page schema、amis schema 和真实浏览器 E2E。 + +这条路径借鉴 NocoBase 的做法:AI 操作受控配置模型,平台负责规范化、校验、模板复用和版本证据。 + +## 当前已落地对象 + +代码位置: + +- `platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprint.java` +- `platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintField.java` +- `platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintCompiler.java` +- `platform-ai/src/main/resources/ai/page-recipes/maintenance-crud-v1.yaml` + +AI 自动开发链路中的 `PAGE_SCHEMA` 草稿仍然保存编译后的 amis schema,保持现有落库和前端渲染契约不变;草稿 metadata 额外保存: + +- `pageRecipe` +- `pageBlueprintVersion` +- `pageBlueprint` + +## Recipe: maintenance-crud-v1 + +适用范围: + +- 普通实体维护页 +- 单主表列表 +- 新增、编辑、删除 +- 本地 smoke 可覆盖列表、新增、服务流搜索 + +标准输出结构: + +- 顶层 `type=page` +- `body[0]` 为 `crud` +- `crud.name` 固定为 `{lowerCamel(tableName)}Crud` +- `crud.syncLocation=false` +- 列表 API 固定为 `/api/entity/{table}/list?page=${page}&perPage=${perPage}` +- 新增按钮位于 `headerToolbar[0]` +- 新增按钮使用 `level=primary`、`icon=fa fa-plus`、`actionType=dialog` +- 新增和编辑表单提交到 `post:/api/entity/{table}/save` +- 编辑按钮位于操作列第一位 +- 删除按钮位于操作列第二位 +- 删除按钮使用 `className=text-danger`、`actionType=ajax`、`confirmText` +- 删除 API 固定为 `/api/entity/{table}/delete` +- 页大小默认 `20` +- `footerToolbar` 固定包含 `statistics` 和 `pagination` + +字段映射: + +- `string` -> `input-text` +- `integer` / `number` -> `input-number` +- `date` -> `input-date` +- `datetime` -> `input-datetime` +- `boolean` -> `switch` +- 主键不出现在新增表单,编辑表单最后追加 `hidden` + +## 当前边界 + +本阶段只解决“AI 生成维护类 CRUD 页时走受控 recipe”。还未覆盖: + +- 主子表页面 +- 树表页面 +- 审批办理页面 +- 看板、日历、图表、报表页 +- 复杂字段分组 +- 条件联动 +- 引用选择器策略 +- 权限按钮策略 + +这些类型必须作为新的 recipe 增量加入,不能让 Agent 直接绕过 blueprint 生成最终 amis JSON。 + +## 后续演进 + +1. 将 `maintenance-crud-v1.yaml` 中的规则接入 UI lint。 +2. 增加 `master-detail-v1`、`tree-crud-v1`、`approval-task-v1` recipe。 +3. 让设计器 overlay 保存也能记录对应 blueprint 或 template 来源。 +4. 在功能包中导出 blueprint,线上部署包继续导出编译后的 amis schema。 +5. Playwright E2E 从 recipe 反推出应验证的 UI 行为。 diff --git a/platform-ai/src/main/java/com/eip4/platform/ai/service/AiFeatureDraftGenerator.java b/platform-ai/src/main/java/com/eip4/platform/ai/service/AiFeatureDraftGenerator.java index 5c15e76..978fcdb 100644 --- a/platform-ai/src/main/java/com/eip4/platform/ai/service/AiFeatureDraftGenerator.java +++ b/platform-ai/src/main/java/com/eip4/platform/ai/service/AiFeatureDraftGenerator.java @@ -20,6 +20,9 @@ import com.eip4.platform.ai.model.AiArtifactType; import com.eip4.platform.ai.model.AiFeatureField; import com.eip4.platform.ai.model.AiFeatureRequest; import com.eip4.platform.ai.model.AiGeneratedDraft; +import com.eip4.platform.metadata.schema.blueprint.PageBlueprint; +import com.eip4.platform.metadata.schema.blueprint.PageBlueprintCompiler; +import com.eip4.platform.metadata.schema.blueprint.PageBlueprintField; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -32,18 +35,21 @@ public class AiFeatureDraftGenerator { "VERSION", "CREATETIME", "UPDATETIME", "CREATEBY", "UPDATEBY", "ISACTIVE"); private final ObjectMapper objectMapper; + private final PageBlueprintCompiler pageBlueprintCompiler; public AiFeatureDraftGenerator(ObjectMapper objectMapper) { this.objectMapper = objectMapper; + this.pageBlueprintCompiler = new PageBlueprintCompiler(objectMapper); } public List generate(AiFeatureRequest request) { FeatureSpec spec = normalize(request); + PageBlueprint pageBlueprint = pageBlueprint(spec); List drafts = new ArrayList<>(); drafts.add(new AiGeneratedDraft(AiArtifactType.ENTITY, spec.tableName(), entityPayload(spec), metadata("entity-meta", "/api/meta/entity"))); drafts.add(new AiGeneratedDraft(AiArtifactType.PAGE_SCHEMA, spec.templateId(), - pageSchema(spec), metadata("page-schema", "sys_page_schema"))); + pageBlueprintCompiler.compile(pageBlueprint), pageSchemaMetadata(pageBlueprint))); drafts.add(new AiGeneratedDraft(AiArtifactType.SERVICEFLOW, spec.tableName() + "_DEFAULT_FLOWS", serviceFlowPayload(spec), metadata("serviceflow", "sys_serviceflow"))); drafts.add(new AiGeneratedDraft(AiArtifactType.MENU, spec.templateId(), @@ -72,102 +78,16 @@ public class AiFeatureDraftGenerator { return root; } - private ObjectNode pageSchema(FeatureSpec spec) { - ObjectNode page = objectMapper.createObjectNode(); - page.put("type", "page"); - page.put("title", spec.featureName()); - ArrayNode body = page.putArray("body"); - ObjectNode crud = body.addObject(); - crud.put("type", "crud"); - crud.put("name", lowerCamel(spec.tableName()) + "Crud"); - crud.put("syncLocation", false); - ObjectNode api = crud.putObject("api"); - api.put("method", "get"); - api.put("url", "/api/entity/" + spec.tableName() + "/list?page=${page}&perPage=${perPage}"); - api.put("adaptor", "return payload;"); - ArrayNode headerToolbar = crud.putArray("headerToolbar"); - ObjectNode addButton = headerToolbar.addObject(); - addButton.put("type", "button"); - addButton.put("label", "新增"); - addButton.put("level", "primary"); - addButton.put("icon", "fa fa-plus"); - addButton.put("actionType", "dialog"); - ObjectNode addDialog = addButton.putObject("dialog"); - addDialog.put("title", "新增" + spec.entityName()); - ObjectNode addForm = addDialog.putObject("body"); - addForm.put("type", "form"); - addForm.put("api", "post:/api/entity/" + spec.tableName() + "/save"); - addForm.set("body", formControls(spec, false)); - headerToolbar.add("pagination"); - ArrayNode footerToolbar = crud.putArray("footerToolbar"); - footerToolbar.add("statistics"); - footerToolbar.add("pagination"); - crud.put("perPage", 20); - crud.set("columns", columns(spec)); - return page; - } - - private ArrayNode columns(FeatureSpec spec) { - ArrayNode columns = objectMapper.createArrayNode(); + private PageBlueprint pageBlueprint(FeatureSpec spec) { + List fields = new ArrayList<>(); for (FieldSpec field : spec.fields()) { - ObjectNode column = columns.addObject(); - column.put("name", field.column()); - column.put("label", field.label()); + fields.add(new PageBlueprintField(field.column(), field.label(), field.type(), + field.required(), true, true)); } - ObjectNode op = columns.addObject(); - op.put("type", "operation"); - op.put("label", "操作"); - op.put("width", 180); - ArrayNode buttons = op.putArray("buttons"); - ObjectNode edit = buttons.addObject(); - edit.put("type", "button"); - edit.put("label", "编辑"); - edit.put("level", "link"); - edit.put("actionType", "dialog"); - ObjectNode editDialog = edit.putObject("dialog"); - editDialog.put("title", "编辑${" + displayColumn(spec) + "}"); - ObjectNode editForm = editDialog.putObject("body"); - editForm.put("type", "form"); - editForm.put("api", "post:/api/entity/" + spec.tableName() + "/save"); - ArrayNode editControls = formControls(spec, true); - ObjectNode hiddenPk = objectMapper.createObjectNode(); - hiddenPk.put("type", "hidden"); - hiddenPk.put("name", spec.tableName() + "_ID"); - hiddenPk.put("value", "${" + spec.tableName() + "_ID}"); - editControls.add(hiddenPk); - editForm.set("body", editControls); - - ObjectNode del = buttons.addObject(); - del.put("type", "button"); - del.put("label", "删除"); - del.put("level", "link"); - del.put("className", "text-danger"); - del.put("actionType", "ajax"); - del.put("confirmText", "确认删除该记录?"); - ObjectNode delApi = del.putObject("api"); - delApi.put("method", "post"); - delApi.put("url", "/api/entity/" + spec.tableName() + "/delete"); - ObjectNode data = delApi.putObject("data"); - data.put("id", "${" + spec.tableName() + "_ID}"); - del.put("reload", lowerCamel(spec.tableName()) + "Crud"); - return columns; - } - - private ArrayNode formControls(FeatureSpec spec, boolean bindCurrentRow) { - ArrayNode controls = objectMapper.createArrayNode(); - for (FieldSpec field : spec.fields()) { - ObjectNode c = controls.addObject(); - c.put("type", controlType(field.type())); - c.put("name", field.column()); - c.put("label", field.label()); - if (field.required()) { - c.put("required", true); - } - if (bindCurrentRow) { - c.put("value", "${" + field.column() + "}"); - } - } - return controls; + return new PageBlueprint(PageBlueprint.VERSION, PageBlueprint.RECIPE_MAINTENANCE_CRUD, + spec.templateId(), spec.featureName(), spec.tableName(), spec.entityName(), + spec.tableName() + "_ID", displayColumn(spec), fields, + true, true, true, 20); } private ObjectNode serviceFlowPayload(FeatureSpec spec) { @@ -332,16 +252,6 @@ public class AiFeatureDraftGenerator { return Math.min(length, 2000); } - private static String controlType(String fieldType) { - return switch (fieldType) { - case "integer", "number" -> "input-number"; - case "date" -> "input-date"; - case "datetime" -> "input-datetime"; - case "boolean" -> "switch"; - default -> "input-text"; - }; - } - private static String displayColumn(FeatureSpec spec) { return spec.fields().stream() .filter(f -> "NAME".equals(f.column()) || f.required()) @@ -350,17 +260,6 @@ public class AiFeatureDraftGenerator { .orElse(spec.fields().get(0).column()); } - private static String lowerCamel(String table) { - String[] parts = table.toLowerCase(Locale.ROOT).split("_+"); - StringBuilder sb = new StringBuilder(parts[0]); - for (int i = 1; i < parts.length; i++) { - if (!parts[i].isBlank()) { - sb.append(Character.toUpperCase(parts[i].charAt(0))).append(parts[i].substring(1)); - } - } - return sb.toString(); - } - private static String firstText(String... values) { for (String value : values) { if (value != null && !value.isBlank()) { @@ -379,6 +278,15 @@ public class AiFeatureDraftGenerator { return metadata; } + @SuppressWarnings("unchecked") + private Map pageSchemaMetadata(PageBlueprint pageBlueprint) { + Map metadata = metadata("page-schema", "sys_page_schema"); + metadata.put("pageRecipe", pageBlueprint.recipe()); + metadata.put("pageBlueprintVersion", pageBlueprint.schemaVersion()); + metadata.put("pageBlueprint", objectMapper.convertValue(pageBlueprint, Map.class)); + return metadata; + } + private static String sha(String text) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); diff --git a/platform-ai/src/main/resources/ai/page-recipes/maintenance-crud-v1.yaml b/platform-ai/src/main/resources/ai/page-recipes/maintenance-crud-v1.yaml new file mode 100644 index 0000000..13bc47a --- /dev/null +++ b/platform-ai/src/main/resources/ai/page-recipes/maintenance-crud-v1.yaml @@ -0,0 +1,49 @@ +schemaVersion: 1 +recipe: maintenance-crud-v1 +title: EIP4 maintenance CRUD page +purpose: 普通实体维护页,覆盖列表、新增、编辑、删除。 +compiler: com.eip4.platform.metadata.schema.blueprint.PageBlueprintCompiler +amis: + rootType: page + body: + - type: crud + syncLocation: false + perPage: 20 + apiPattern: /api/entity/{tableName}/list?page=${page}&perPage=${perPage} + headerToolbar: + - addButton: + label: 新增 + level: primary + icon: fa fa-plus + actionType: dialog + formApiPattern: post:/api/entity/{tableName}/save + - pagination + footerToolbar: + - statistics + - pagination + operationColumn: + label: 操作 + width: 180 + actions: + - edit: + level: link + actionType: dialog + formApiPattern: post:/api/entity/{tableName}/save + - delete: + level: link + className: text-danger + actionType: ajax + confirmText: 确认删除该记录? + apiPattern: /api/entity/{tableName}/delete +fieldMappings: + string: input-text + integer: input-number + number: input-number + date: input-date + datetime: input-datetime + boolean: switch +gates: + - typed-page-schema + - amis-schema + - local-smoke + - playwright-render diff --git a/platform-ai/src/test/java/com/eip4/platform/ai/service/AiFeatureDraftGeneratorTest.java b/platform-ai/src/test/java/com/eip4/platform/ai/service/AiFeatureDraftGeneratorTest.java index 6ac0e6d..f72dd2e 100644 --- a/platform-ai/src/test/java/com/eip4/platform/ai/service/AiFeatureDraftGeneratorTest.java +++ b/platform-ai/src/test/java/com/eip4/platform/ai/service/AiFeatureDraftGeneratorTest.java @@ -6,6 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; +import java.util.Map; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -39,6 +40,15 @@ class AiFeatureDraftGeneratorTest { assertTrue(drafts.stream().anyMatch(d -> d.artifactType() == AiArtifactType.SERVICEFLOW)); assertTrue(drafts.stream().anyMatch(d -> d.artifactType() == AiArtifactType.MENU)); assertTrue(drafts.stream().anyMatch(d -> d.artifactType() == AiArtifactType.TEST)); + var pageSchema = drafts.stream().filter(d -> d.artifactType() == AiArtifactType.PAGE_SCHEMA) + .findFirst().orElseThrow(); + assertEquals("maintenance-crud-v1", pageSchema.metadata().get("pageRecipe")); + assertEquals(1, pageSchema.metadata().get("pageBlueprintVersion")); + assertEquals("crud", pageSchema.payload().path("body").get(0).path("type").asText()); + @SuppressWarnings("unchecked") + Map blueprint = (Map) pageSchema.metadata().get("pageBlueprint"); + assertEquals("EIP4AI_CONTRACT_TYPE", blueprint.get("tableName")); + assertEquals("NAME", blueprint.get("displayField")); var serviceFlow = drafts.stream().filter(d -> d.artifactType() == AiArtifactType.SERVICEFLOW) .findFirst().orElseThrow(); assertEquals(5, serviceFlow.payload().path("flows").size()); diff --git a/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprint.java b/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprint.java new file mode 100644 index 0000000..8906476 --- /dev/null +++ b/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprint.java @@ -0,0 +1,32 @@ +package com.eip4.platform.metadata.schema.blueprint; + +import java.util.List; + +/** + * EIP4 页面生成蓝图。 + * + *

AI 和设计器应优先产出本对象,再由平台编译成 amis schema。这样 UI/UE 规则集中在 + * recipe/compiler/validator 中,而不是散落在 Agent 提示词里。 + */ +public record PageBlueprint( + int schemaVersion, + String recipe, + String templateId, + String title, + String tableName, + String entityName, + String primaryKey, + String displayField, + List fields, + boolean allowCreate, + boolean allowEdit, + boolean allowDelete, + int pageSize) { + + public static final int VERSION = 1; + public static final String RECIPE_MAINTENANCE_CRUD = "maintenance-crud-v1"; + + public PageBlueprint { + fields = fields == null ? List.of() : List.copyOf(fields); + } +} diff --git a/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintCompiler.java b/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintCompiler.java new file mode 100644 index 0000000..a39ee5e --- /dev/null +++ b/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintCompiler.java @@ -0,0 +1,201 @@ +package com.eip4.platform.metadata.schema.blueprint; + +import java.util.Locale; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; + +/** + * 把 EIP4 受控 PageBlueprint 编译成当前前端可消费的 amis schema。 + */ +public final class PageBlueprintCompiler { + + private final ObjectMapper objectMapper; + + public PageBlueprintCompiler(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + } + + public ObjectNode compile(PageBlueprint blueprint) { + validate(blueprint); + if (PageBlueprint.RECIPE_MAINTENANCE_CRUD.equals(blueprint.recipe())) { + return maintenanceCrud(blueprint); + } + throw new IllegalArgumentException("Unsupported page recipe: " + blueprint.recipe()); + } + + private ObjectNode maintenanceCrud(PageBlueprint blueprint) { + ObjectNode page = objectMapper.createObjectNode(); + page.put("type", "page"); + page.put("title", blueprint.title()); + ArrayNode body = page.putArray("body"); + ObjectNode crud = body.addObject(); + String crudName = lowerCamel(blueprint.tableName()) + "Crud"; + crud.put("type", "crud"); + crud.put("name", crudName); + crud.put("syncLocation", false); + ObjectNode api = crud.putObject("api"); + api.put("method", "get"); + api.put("url", "/api/entity/" + blueprint.tableName() + "/list?page=${page}&perPage=${perPage}"); + api.put("adaptor", "return payload;"); + + ArrayNode headerToolbar = crud.putArray("headerToolbar"); + if (blueprint.allowCreate()) { + ObjectNode addButton = headerToolbar.addObject(); + addButton.put("type", "button"); + addButton.put("label", "新增"); + addButton.put("level", "primary"); + addButton.put("icon", "fa fa-plus"); + addButton.put("actionType", "dialog"); + ObjectNode addDialog = addButton.putObject("dialog"); + addDialog.put("title", "新增" + blueprint.entityName()); + ObjectNode addForm = addDialog.putObject("body"); + addForm.put("type", "form"); + addForm.put("api", "post:/api/entity/" + blueprint.tableName() + "/save"); + addForm.set("body", formControls(blueprint, false)); + } + headerToolbar.add("pagination"); + + ArrayNode footerToolbar = crud.putArray("footerToolbar"); + footerToolbar.add("statistics"); + footerToolbar.add("pagination"); + crud.put("perPage", blueprint.pageSize()); + crud.set("columns", columns(blueprint, crudName)); + return page; + } + + private ArrayNode columns(PageBlueprint blueprint, String crudName) { + ArrayNode columns = objectMapper.createArrayNode(); + for (PageBlueprintField field : blueprint.fields()) { + if (!field.listVisible()) { + continue; + } + ObjectNode column = columns.addObject(); + column.put("name", field.name()); + column.put("label", field.label()); + } + if (!blueprint.allowEdit() && !blueprint.allowDelete()) { + return columns; + } + ObjectNode op = columns.addObject(); + op.put("type", "operation"); + op.put("label", "操作"); + op.put("width", 180); + ArrayNode buttons = op.putArray("buttons"); + if (blueprint.allowEdit()) { + addEditAction(blueprint, buttons); + } + if (blueprint.allowDelete()) { + addDeleteAction(blueprint, buttons, crudName); + } + return columns; + } + + private void addEditAction(PageBlueprint blueprint, ArrayNode buttons) { + ObjectNode edit = buttons.addObject(); + edit.put("type", "button"); + edit.put("label", "编辑"); + edit.put("level", "link"); + edit.put("actionType", "dialog"); + ObjectNode editDialog = edit.putObject("dialog"); + editDialog.put("title", "编辑${" + blueprint.displayField() + "}"); + ObjectNode editForm = editDialog.putObject("body"); + editForm.put("type", "form"); + editForm.put("api", "post:/api/entity/" + blueprint.tableName() + "/save"); + ArrayNode editControls = formControls(blueprint, true); + ObjectNode hiddenPk = objectMapper.createObjectNode(); + hiddenPk.put("type", "hidden"); + hiddenPk.put("name", blueprint.primaryKey()); + hiddenPk.put("value", "${" + blueprint.primaryKey() + "}"); + editControls.add(hiddenPk); + editForm.set("body", editControls); + } + + private void addDeleteAction(PageBlueprint blueprint, ArrayNode buttons, String crudName) { + ObjectNode del = buttons.addObject(); + del.put("type", "button"); + del.put("label", "删除"); + del.put("level", "link"); + del.put("className", "text-danger"); + del.put("actionType", "ajax"); + del.put("confirmText", "确认删除该记录?"); + ObjectNode delApi = del.putObject("api"); + delApi.put("method", "post"); + delApi.put("url", "/api/entity/" + blueprint.tableName() + "/delete"); + ObjectNode data = delApi.putObject("data"); + data.put("id", "${" + blueprint.primaryKey() + "}"); + del.put("reload", crudName); + } + + private ArrayNode formControls(PageBlueprint blueprint, boolean bindCurrentRow) { + ArrayNode controls = objectMapper.createArrayNode(); + for (PageBlueprintField field : blueprint.fields()) { + if (!field.formVisible()) { + continue; + } + ObjectNode c = controls.addObject(); + c.put("type", controlType(field.type())); + c.put("name", field.name()); + c.put("label", field.label()); + if (field.required()) { + c.put("required", true); + } + if (bindCurrentRow) { + c.put("value", "${" + field.name() + "}"); + } + } + return controls; + } + + private void validate(PageBlueprint blueprint) { + if (blueprint == null) { + throw new IllegalArgumentException("Page blueprint is required"); + } + if (blueprint.schemaVersion() != PageBlueprint.VERSION) { + throw new IllegalArgumentException("Unsupported page blueprint version: " + blueprint.schemaVersion()); + } + if (!PageBlueprint.RECIPE_MAINTENANCE_CRUD.equals(blueprint.recipe())) { + throw new IllegalArgumentException("Unsupported page recipe: " + blueprint.recipe()); + } + required(blueprint.templateId(), "templateId"); + required(blueprint.title(), "title"); + required(blueprint.tableName(), "tableName"); + required(blueprint.entityName(), "entityName"); + required(blueprint.primaryKey(), "primaryKey"); + required(blueprint.displayField(), "displayField"); + if (blueprint.fields().isEmpty()) { + throw new IllegalArgumentException("fields must not be empty"); + } + if (blueprint.pageSize() < 1 || blueprint.pageSize() > 200) { + throw new IllegalArgumentException("pageSize must be between 1 and 200"); + } + } + + private static void required(String value, String field) { + if (value == null || value.isBlank()) { + throw new IllegalArgumentException(field + " is required"); + } + } + + private static String controlType(String fieldType) { + return switch (fieldType) { + case "integer", "number" -> "input-number"; + case "date" -> "input-date"; + case "datetime" -> "input-datetime"; + case "boolean" -> "switch"; + default -> "input-text"; + }; + } + + private static String lowerCamel(String table) { + String[] parts = table.toLowerCase(Locale.ROOT).split("_+"); + StringBuilder sb = new StringBuilder(parts[0]); + for (int i = 1; i < parts.length; i++) { + if (!parts[i].isBlank()) { + sb.append(Character.toUpperCase(parts[i].charAt(0))).append(parts[i].substring(1)); + } + } + return sb.toString(); + } +} diff --git a/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintField.java b/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintField.java new file mode 100644 index 0000000..759731f --- /dev/null +++ b/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintField.java @@ -0,0 +1,10 @@ +package com.eip4.platform.metadata.schema.blueprint; + +public record PageBlueprintField( + String name, + String label, + String type, + boolean required, + boolean listVisible, + boolean formVisible) { +} diff --git a/platform-metadata/src/test/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintCompilerTest.java b/platform-metadata/src/test/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintCompilerTest.java new file mode 100644 index 0000000..984cff8 --- /dev/null +++ b/platform-metadata/src/test/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintCompilerTest.java @@ -0,0 +1,78 @@ +package com.eip4.platform.metadata.schema.blueprint; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.List; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; + +class PageBlueprintCompilerTest { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + @Test + @DisplayName("maintenance-crud-v1 编译为 EIP4 标准 amis CRUD 页") + void compilesMaintenanceCrudRecipe() { + PageBlueprint blueprint = new PageBlueprint( + PageBlueprint.VERSION, + PageBlueprint.RECIPE_MAINTENANCE_CRUD, + "eip4ai-contract-type", + "合同类型维护", + "EIP4AI_CONTRACT_TYPE", + "合同类型", + "EIP4AI_CONTRACT_TYPE_ID", + "NAME", + List.of( + new PageBlueprintField("NAME", "名称", "string", true, true, true), + new PageBlueprintField("ENABLED", "启用", "boolean", false, true, true)), + true, + true, + true, + 20); + + ObjectNode schema = new PageBlueprintCompiler(MAPPER).compile(blueprint); + + assertEquals("page", schema.path("type").asText()); + assertEquals("合同类型维护", schema.path("title").asText()); + ObjectNode crud = (ObjectNode) schema.path("body").get(0); + assertEquals("crud", crud.path("type").asText()); + assertEquals("eip4aiContractTypeCrud", crud.path("name").asText()); + assertEquals("/api/entity/EIP4AI_CONTRACT_TYPE/list?page=${page}&perPage=${perPage}", + crud.path("api").path("url").asText()); + assertEquals("新增", crud.path("headerToolbar").get(0).path("label").asText()); + assertEquals("post:/api/entity/EIP4AI_CONTRACT_TYPE/save", + crud.path("headerToolbar").get(0).path("dialog").path("body").path("api").asText()); + assertEquals("switch", crud.path("headerToolbar").get(0).path("dialog").path("body") + .path("body").get(1).path("type").asText()); + assertEquals("hidden", crud.path("columns").get(2).path("buttons").get(0).path("dialog") + .path("body").path("body").get(2).path("type").asText()); + assertEquals("text-danger", crud.path("columns").get(2).path("buttons").get(1) + .path("className").asText()); + } + + @Test + @DisplayName("不支持的 recipe 会被拒绝") + void rejectsUnsupportedRecipe() { + PageBlueprint blueprint = new PageBlueprint( + PageBlueprint.VERSION, + "dashboard-v9", + "tpl", + "标题", + "EIP4AI_T", + "实体", + "EIP4AI_T_ID", + "NAME", + List.of(new PageBlueprintField("NAME", "名称", "string", true, true, true)), + true, + true, + true, + 20); + + assertThrows(IllegalArgumentException.class, () -> new PageBlueprintCompiler(MAPPER).compile(blueprint)); + } +}