feat: add AI page blueprint compiler

This commit is contained in:
2026-06-17 12:14:57 +08:00
parent c1ff0db96b
commit e55072598e
9 changed files with 489 additions and 118 deletions

View File

@@ -0,0 +1,32 @@
package com.eip4.platform.metadata.schema.blueprint;
import java.util.List;
/**
* EIP4 页面生成蓝图。
*
* <p>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<PageBlueprintField> 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);
}
}

View File

@@ -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();
}
}

View File

@@ -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) {
}

View File

@@ -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));
}
}