refactor(ai): 抽 AiDraftGateValidator 收窄 AiAgentService(§10.2 SRP slice 2)
把按 artifact 类型分发的门禁电池(validateDraft + 必填/amis-schema/typed-page-schema/page-recipe/实体/服务流XML/菜单/smoke-plan/render-plan 各 gate + SchemaGate 懒加载状态 + 4 个正则常量 + XML/数组校验 helper)逐方法移入新 AiDraftGateValidator;判定逻辑零变更(D3 红线焊在门禁里,搬迁不动阈值/正则/分支)。AiAgentService 1677→1329 行(累计自 1846 -28%),委托 validateDraft/validateSchemaGate/validateTypedPageSchema/typedMessages。新增 AiDraftGateValidatorTest 7 用例直测各类 pass/fail;AiAgentServiceTest 22/22 + AiAgentControllerTest 11/11 + pipeline 行为不变,platform-ai 79 全绿,全 reactor 绿。slice 3(AiRunStore)经评估丢弃:持久化已由现有 AiAgentStore 承担,另抽会重复。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -10,15 +10,12 @@ import java.util.Optional;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.server.ResponseStatusException;
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
import org.xml.sax.InputSource;
|
|
||||||
|
|
||||||
import com.eip4.platform.ai.model.AiApplyRun;
|
import com.eip4.platform.ai.model.AiApplyRun;
|
||||||
import com.eip4.platform.ai.model.AiApplyStepResult;
|
import com.eip4.platform.ai.model.AiApplyStepResult;
|
||||||
@@ -52,17 +49,12 @@ import com.eip4.platform.ai.model.AiToolAccessRequest;
|
|||||||
import com.eip4.platform.ai.model.AiToolDefinition;
|
import com.eip4.platform.ai.model.AiToolDefinition;
|
||||||
import com.eip4.platform.ai.model.AiEmployeeDefinition;
|
import com.eip4.platform.ai.model.AiEmployeeDefinition;
|
||||||
import com.eip4.platform.ai.store.AiAgentStore;
|
import com.eip4.platform.ai.store.AiAgentStore;
|
||||||
import com.eip4.platform.converter.SchemaGate;
|
|
||||||
import com.eip4.platform.metadata.schema.PageComponentRegistry;
|
import com.eip4.platform.metadata.schema.PageComponentRegistry;
|
||||||
import com.eip4.platform.metadata.schema.TypedPageSchemaIssue;
|
|
||||||
import com.eip4.platform.metadata.schema.TypedPageSchemaReport;
|
import com.eip4.platform.metadata.schema.TypedPageSchemaReport;
|
||||||
import com.eip4.platform.metadata.schema.TypedPageSchemaValidator;
|
import com.eip4.platform.metadata.schema.TypedPageSchemaValidator;
|
||||||
import com.eip4.platform.metadata.schema.blueprint.PageBlueprint;
|
|
||||||
import com.eip4.platform.metadata.schema.blueprint.PageRecipeDefinition;
|
import com.eip4.platform.metadata.schema.blueprint.PageRecipeDefinition;
|
||||||
import com.eip4.platform.metadata.schema.blueprint.PageRecipeRegistry;
|
import com.eip4.platform.metadata.schema.blueprint.PageRecipeRegistry;
|
||||||
import com.eip4.platform.metadata.schema.blueprint.PageRecipeValidationReport;
|
|
||||||
import com.eip4.platform.metadata.schema.blueprint.PageRecipeValidator;
|
import com.eip4.platform.metadata.schema.blueprint.PageRecipeValidator;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
@@ -74,10 +66,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
@Service
|
@Service
|
||||||
public class AiAgentService {
|
public class AiAgentService {
|
||||||
|
|
||||||
private static final Pattern TABLE_NAME = Pattern.compile("[A-Z][A-Z0-9_]{2,40}");
|
|
||||||
private static final Pattern FLOW_KEY = Pattern.compile("[A-Z][A-Z0-9_]{2,80}");
|
|
||||||
private static final Pattern COLUMN_NAME = Pattern.compile("[A-Z][A-Z0-9_]{0,39}");
|
|
||||||
private static final Pattern HTTP_METHOD = Pattern.compile("GET|POST|PUT|DELETE|PATCH");
|
|
||||||
|
|
||||||
private final AiAgentStore store;
|
private final AiAgentStore store;
|
||||||
private final AiFeatureDraftGenerator featureDraftGenerator;
|
private final AiFeatureDraftGenerator featureDraftGenerator;
|
||||||
@@ -91,9 +79,9 @@ public class AiAgentService {
|
|||||||
private final PageRecipeValidator pageRecipeValidator;
|
private final PageRecipeValidator pageRecipeValidator;
|
||||||
/** 部署/回滚 SQL 生成已抽出(§10.2 SRP slice 1);本类只编排,不再内联 SQL 文本。 */
|
/** 部署/回滚 SQL 生成已抽出(§10.2 SRP slice 1);本类只编排,不再内联 SQL 文本。 */
|
||||||
private final AiDeploymentSqlBuilder sqlBuilder;
|
private final AiDeploymentSqlBuilder sqlBuilder;
|
||||||
|
/** 草稿门禁电池已抽出(§10.2 SRP slice 2);本类只编排,不再内联各类 validate*Gate 判定。 */
|
||||||
|
private final AiDraftGateValidator gateValidator;
|
||||||
|
|
||||||
private static volatile SchemaGate sharedSchemaGate;
|
|
||||||
private volatile SchemaGate schemaGate;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public AiAgentService(AiAgentStore store, AiFeatureDraftGenerator featureDraftGenerator,
|
public AiAgentService(AiAgentStore store, AiFeatureDraftGenerator featureDraftGenerator,
|
||||||
@@ -130,6 +118,8 @@ public class AiAgentService {
|
|||||||
this.pageRecipeRegistry = PageRecipeRegistry.defaults();
|
this.pageRecipeRegistry = PageRecipeRegistry.defaults();
|
||||||
this.pageRecipeValidator = new PageRecipeValidator(objectMapper, pageRecipeRegistry);
|
this.pageRecipeValidator = new PageRecipeValidator(objectMapper, pageRecipeRegistry);
|
||||||
this.sqlBuilder = new AiDeploymentSqlBuilder(objectMapper);
|
this.sqlBuilder = new AiDeploymentSqlBuilder(objectMapper);
|
||||||
|
this.gateValidator = new AiDraftGateValidator(
|
||||||
|
objectMapper, this.typedSchemaValidator, this.pageRecipeRegistry, this.pageRecipeValidator);
|
||||||
}
|
}
|
||||||
|
|
||||||
public AiTask createTask(String objective) {
|
public AiTask createTask(String objective) {
|
||||||
@@ -163,7 +153,7 @@ public class AiAgentService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Map<String, Object> safeMetadata = metadata == null ? Map.of() : metadata;
|
Map<String, Object> safeMetadata = metadata == null ? Map.of() : metadata;
|
||||||
List<AiGateResult> gates = validateDraft(artifactType, target, payload, safeMetadata);
|
List<AiGateResult> gates = gateValidator.validateDraft(artifactType, target, payload, safeMetadata);
|
||||||
AiDraft draft = new AiDraft(
|
AiDraft draft = new AiDraft(
|
||||||
"aid-" + UUID.randomUUID(),
|
"aid-" + UUID.randomUUID(),
|
||||||
taskId,
|
taskId,
|
||||||
@@ -741,9 +731,9 @@ public class AiAgentService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Object> validateSchema(JsonNode schema) {
|
public Map<String, Object> validateSchema(JsonNode schema) {
|
||||||
AiGateResult schemaGate = validateSchemaGate(schema);
|
AiGateResult schemaGate = gateValidator.validateSchemaGate(schema);
|
||||||
TypedPageSchemaReport typedReport = validateTypedPageSchema(schema);
|
TypedPageSchemaReport typedReport = gateValidator.validateTypedPageSchema(schema);
|
||||||
List<String> typedMessages = typedMessages(typedReport);
|
List<String> typedMessages = AiDraftGateValidator.typedMessages(typedReport);
|
||||||
List<String> validationErrors = new ArrayList<>();
|
List<String> validationErrors = new ArrayList<>();
|
||||||
validationErrors.addAll(schemaGate.messages());
|
validationErrors.addAll(schemaGate.messages());
|
||||||
validationErrors.addAll(typedMessages);
|
validationErrors.addAll(typedMessages);
|
||||||
@@ -797,342 +787,6 @@ public class AiAgentService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private AiGateResult validateSchemaGate(JsonNode schema) {
|
|
||||||
if (schema == null || !schema.isObject()) {
|
|
||||||
return new AiGateResult("amis-schema", false, List.of("schema 顶层必须是 JSON object"));
|
|
||||||
}
|
|
||||||
List<String> errors = getSchemaGate().validate(schema);
|
|
||||||
return new AiGateResult("amis-schema", errors.isEmpty(), errors.stream().limit(20).toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
private AiGateResult validateTypedPageSchemaGate(JsonNode schema) {
|
|
||||||
TypedPageSchemaReport report = validateTypedPageSchema(schema);
|
|
||||||
return new AiGateResult("typed-page-schema", report.valid(), typedMessages(report));
|
|
||||||
}
|
|
||||||
|
|
||||||
private AiGateResult validatePageRecipeGate(String targetId, JsonNode schema, Map<String, Object> metadata) {
|
|
||||||
boolean hasRecipe = metadata.containsKey("pageRecipe");
|
|
||||||
boolean hasBlueprint = metadata.containsKey("pageBlueprint");
|
|
||||||
if (!hasRecipe && !hasBlueprint) {
|
|
||||||
return new AiGateResult("page-recipe", true, List.of("未声明 pageRecipe,按手工 PAGE_SCHEMA 草稿跳过 recipe 门禁"));
|
|
||||||
}
|
|
||||||
List<String> messages = new ArrayList<>();
|
|
||||||
Object recipeValue = metadata.get("pageRecipe");
|
|
||||||
Object blueprintValue = metadata.get("pageBlueprint");
|
|
||||||
Object versionValue = metadata.get("pageBlueprintVersion");
|
|
||||||
if (!(recipeValue instanceof String recipe) || recipe.isBlank()) {
|
|
||||||
messages.add("pageRecipe metadata 不能为空");
|
|
||||||
}
|
|
||||||
if (blueprintValue == null) {
|
|
||||||
messages.add("pageBlueprint metadata 不能为空");
|
|
||||||
}
|
|
||||||
if (!(versionValue instanceof Number)) {
|
|
||||||
messages.add("pageBlueprintVersion metadata 必须是数字");
|
|
||||||
}
|
|
||||||
if (!messages.isEmpty()) {
|
|
||||||
return new AiGateResult("page-recipe", false, messages);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
PageBlueprint blueprint = objectMapper.convertValue(blueprintValue, PageBlueprint.class);
|
|
||||||
if (!recipeValue.equals(blueprint.recipe())) {
|
|
||||||
messages.add("pageRecipe 与 pageBlueprint.recipe 不一致");
|
|
||||||
}
|
|
||||||
if (!targetId.equals(blueprint.templateId())) {
|
|
||||||
messages.add("targetId 与 pageBlueprint.templateId 不一致");
|
|
||||||
}
|
|
||||||
if (((Number) versionValue).intValue() != blueprint.schemaVersion()) {
|
|
||||||
messages.add("pageBlueprintVersion 与 pageBlueprint.schemaVersion 不一致");
|
|
||||||
}
|
|
||||||
if (!messages.isEmpty()) {
|
|
||||||
return new AiGateResult("page-recipe", false, messages);
|
|
||||||
}
|
|
||||||
PageRecipeValidationReport report = pageRecipeValidator.validate(blueprint, schema);
|
|
||||||
return new AiGateResult("page-recipe", report.valid(), report.messages());
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
return new AiGateResult("page-recipe", false, List.of("pageBlueprint metadata 解析失败: " + ex.getMessage()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private TypedPageSchemaReport validateTypedPageSchema(JsonNode schema) {
|
|
||||||
try {
|
|
||||||
return typedSchemaValidator.validate(schema == null ? "null" : objectMapper.writeValueAsString(schema));
|
|
||||||
} catch (JsonProcessingException ex) {
|
|
||||||
return typedSchemaValidator.validate("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<String> typedMessages(TypedPageSchemaReport report) {
|
|
||||||
return report.issues().stream()
|
|
||||||
.limit(20)
|
|
||||||
.map(AiAgentService::typedMessage)
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String typedMessage(TypedPageSchemaIssue issue) {
|
|
||||||
return issue.path() + " " + issue.code() + ": " + issue.message();
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<AiGateResult> validateDraft(AiArtifactType type, String targetId, JsonNode payload,
|
|
||||||
Map<String, Object> metadata) {
|
|
||||||
List<AiGateResult> gates = new ArrayList<>();
|
|
||||||
gates.add(requiredFieldsGate(type, targetId, payload));
|
|
||||||
switch (type) {
|
|
||||||
case PAGE_SCHEMA -> {
|
|
||||||
gates.add(validateSchemaGate(payload));
|
|
||||||
gates.add(validateTypedPageSchemaGate(payload));
|
|
||||||
gates.add(validatePageRecipeGate(targetId, payload, metadata));
|
|
||||||
}
|
|
||||||
case ENTITY -> gates.add(validateEntityGate(payload));
|
|
||||||
case SERVICEFLOW -> gates.add(validateServiceFlowGate(payload));
|
|
||||||
case MENU -> gates.add(validateMenuGate(payload));
|
|
||||||
case TEST -> {
|
|
||||||
gates.add(validateTestGate(payload));
|
|
||||||
gates.add(validateRenderPlanGate(payload));
|
|
||||||
}
|
|
||||||
case CODE -> gates.add(new AiGateResult("code-review-required", true, List.of("代码草稿需后续静态检查/单测门禁")));
|
|
||||||
}
|
|
||||||
return List.copyOf(gates);
|
|
||||||
}
|
|
||||||
|
|
||||||
private AiGateResult requiredFieldsGate(AiArtifactType type, String targetId, JsonNode payload) {
|
|
||||||
List<String> messages = new ArrayList<>();
|
|
||||||
if (targetId == null || targetId.isBlank()) {
|
|
||||||
messages.add("targetId 不能为空");
|
|
||||||
}
|
|
||||||
if (payload == null || !payload.isObject()) {
|
|
||||||
messages.add(type.name() + " payload 顶层必须是 JSON object");
|
|
||||||
}
|
|
||||||
return new AiGateResult("draft-required-fields", messages.isEmpty(), List.copyOf(messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
private AiGateResult validateEntityGate(JsonNode payload) {
|
|
||||||
List<String> messages = new ArrayList<>();
|
|
||||||
String table = payload.path("tablename").asText("");
|
|
||||||
if (!TABLE_NAME.matcher(table).matches()) {
|
|
||||||
messages.add("tablename 必须是 3-40 位大写字母数字下划线,且以字母开头");
|
|
||||||
}
|
|
||||||
if (payload.path("name").asText("").isBlank()) {
|
|
||||||
messages.add("name 不能为空");
|
|
||||||
}
|
|
||||||
JsonNode attrs = payload.path("attributes");
|
|
||||||
if (!attrs.isArray() || attrs.isEmpty()) {
|
|
||||||
messages.add("attributes 至少需要 1 个业务字段");
|
|
||||||
} else {
|
|
||||||
for (JsonNode attr : attrs) {
|
|
||||||
String col = attr.path("column").asText("");
|
|
||||||
if (!COLUMN_NAME.matcher(col).matches()) {
|
|
||||||
messages.add("字段 column 不合法: " + col);
|
|
||||||
}
|
|
||||||
String type = attr.path("type").asText("");
|
|
||||||
if (!List.of("string", "integer", "number", "date", "datetime", "boolean").contains(type)) {
|
|
||||||
messages.add("字段 " + col + " type 不支持: " + type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new AiGateResult("entity-meta", messages.isEmpty(), messages.stream().limit(20).toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
private AiGateResult validateServiceFlowGate(JsonNode payload) {
|
|
||||||
List<String> messages = new ArrayList<>();
|
|
||||||
JsonNode flows = payload.path("flows");
|
|
||||||
if (!flows.isArray() || flows.isEmpty()) {
|
|
||||||
messages.add("flows 至少需要 1 个服务流定义");
|
|
||||||
} else {
|
|
||||||
for (JsonNode flow : flows) {
|
|
||||||
String key = flow.path("key").asText("");
|
|
||||||
if (!FLOW_KEY.matcher(key).matches()) {
|
|
||||||
messages.add("服务流 key 不合法: " + key);
|
|
||||||
}
|
|
||||||
String xml = flow.path("contentXml").asText("");
|
|
||||||
if (!isValidFlowXml(xml)) {
|
|
||||||
messages.add("服务流 XML 不合法: " + key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new AiGateResult("serviceflow-xml", messages.isEmpty(), messages.stream().limit(20).toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
private AiGateResult validateMenuGate(JsonNode payload) {
|
|
||||||
List<String> messages = new ArrayList<>();
|
|
||||||
if (payload.path("name").asText("").isBlank()) {
|
|
||||||
messages.add("菜单 name 不能为空");
|
|
||||||
}
|
|
||||||
if (payload.path("viewTemplateId").asText("").isBlank()) {
|
|
||||||
messages.add("菜单 viewTemplateId 不能为空");
|
|
||||||
}
|
|
||||||
return new AiGateResult("menu-binding", messages.isEmpty(), List.copyOf(messages));
|
|
||||||
}
|
|
||||||
|
|
||||||
private AiGateResult validateTestGate(JsonNode payload) {
|
|
||||||
List<String> messages = new ArrayList<>();
|
|
||||||
JsonNode tests = payload.path("smokeTests");
|
|
||||||
if (!tests.isArray() || tests.isEmpty()) {
|
|
||||||
messages.add("smokeTests 至少需要 1 条");
|
|
||||||
} else {
|
|
||||||
for (JsonNode test : tests) {
|
|
||||||
String method = test.path("method").asText("");
|
|
||||||
String url = test.path("url").asText("");
|
|
||||||
if (!HTTP_METHOD.matcher(method).matches()) {
|
|
||||||
messages.add("smoke method 不合法: " + method);
|
|
||||||
}
|
|
||||||
if (!url.startsWith("/api/")) {
|
|
||||||
messages.add("smoke url 必须以 /api/ 开头: " + url);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new AiGateResult("smoke-plan", messages.isEmpty(), messages.stream().limit(20).toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
private AiGateResult validateRenderPlanGate(JsonNode payload) {
|
|
||||||
List<String> messages = new ArrayList<>();
|
|
||||||
JsonNode tests = payload.path("renderTests");
|
|
||||||
if (!tests.isArray() || tests.isEmpty()) {
|
|
||||||
messages.add("renderTests 至少需要 1 条");
|
|
||||||
} else {
|
|
||||||
for (JsonNode test : tests) {
|
|
||||||
String name = test.path("name").asText("");
|
|
||||||
String recipe = test.path("recipe").asText("");
|
|
||||||
String schemaUrl = test.path("schemaUrl").asText("");
|
|
||||||
String previewUrl = test.path("previewUrl").asText("");
|
|
||||||
String rootType = test.path("expectedRootType").asText("");
|
|
||||||
String bodyType = test.path("expectedBodyType").asText("");
|
|
||||||
String crudName = test.path("expectedCrudName").asText("");
|
|
||||||
String expectedApi = test.path("expectedApi").asText("");
|
|
||||||
JsonNode components = test.path("expectedComponents");
|
|
||||||
JsonNode columns = test.path("expectedColumns");
|
|
||||||
JsonNode actions = test.path("expectedActions");
|
|
||||||
JsonNode texts = test.path("expectedTexts");
|
|
||||||
if (name.isBlank()) {
|
|
||||||
messages.add("render name 不能为空");
|
|
||||||
}
|
|
||||||
if (pageRecipeRegistry.find(recipe).isEmpty()) {
|
|
||||||
messages.add("render recipe 未注册: " + recipe);
|
|
||||||
}
|
|
||||||
if (!schemaUrl.startsWith("/api/page/schema/")) {
|
|
||||||
messages.add("render schemaUrl 必须以 /api/page/schema/ 开头: " + schemaUrl);
|
|
||||||
}
|
|
||||||
if (!previewUrl.startsWith("/amis.html?schema=")
|
|
||||||
&& !previewUrl.startsWith("/amis.html?templateId=")
|
|
||||||
&& !previewUrl.startsWith("/amis.html?schemaUrl=")) {
|
|
||||||
messages.add("render previewUrl 必须指向可解析 schema 的 /amis.html: " + previewUrl);
|
|
||||||
}
|
|
||||||
if (!"page".equals(rootType)) {
|
|
||||||
messages.add("render expectedRootType 必须是 page");
|
|
||||||
}
|
|
||||||
if (!"crud".equals(bodyType)) {
|
|
||||||
messages.add("render expectedBodyType 必须是 crud");
|
|
||||||
}
|
|
||||||
if (crudName.isBlank()) {
|
|
||||||
messages.add("render expectedCrudName 不能为空");
|
|
||||||
}
|
|
||||||
// house-style 对齐:单表维护页列表走保真层服务流 _SEARCH(/api/flow),
|
|
||||||
// 树页仍走 /api/entity 树接口。
|
|
||||||
if (PageBlueprint.RECIPE_TREE_CRUD.equals(recipe)) {
|
|
||||||
if (!expectedApi.startsWith("/api/entity/") || !expectedApi.contains("/tree?")) {
|
|
||||||
messages.add("tree-crud-v1 render expectedApi 必须是 /api/entity 树接口: " + expectedApi);
|
|
||||||
}
|
|
||||||
} else if (PageBlueprint.RECIPE_MAINTENANCE_CRUD.equals(recipe)) {
|
|
||||||
if (!expectedApi.startsWith("/api/flow/") || !expectedApi.endsWith("_SEARCH")) {
|
|
||||||
messages.add("maintenance-crud-v1 render expectedApi 必须是 /api/flow 服务流 _SEARCH 接口: "
|
|
||||||
+ expectedApi);
|
|
||||||
}
|
|
||||||
} else if (!expectedApi.startsWith("/api/")) {
|
|
||||||
messages.add("render expectedApi 必须指向 /api/: " + expectedApi);
|
|
||||||
}
|
|
||||||
if (!components.isArray() || components.isEmpty()) {
|
|
||||||
messages.add("render expectedComponents 至少需要 1 项");
|
|
||||||
} else {
|
|
||||||
if (!arrayContainsText(components, "page")) {
|
|
||||||
messages.add("render expectedComponents 必须包含 page");
|
|
||||||
}
|
|
||||||
if (!arrayContainsText(components, "crud")) {
|
|
||||||
messages.add("render expectedComponents 必须包含 crud");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!columns.isArray() || columns.isEmpty()) {
|
|
||||||
messages.add("render expectedColumns 至少需要 1 项");
|
|
||||||
} else {
|
|
||||||
for (JsonNode column : columns) {
|
|
||||||
if (column.path("name").asText("").isBlank()) {
|
|
||||||
messages.add("render expectedColumns.name 不能为空");
|
|
||||||
}
|
|
||||||
if (column.path("label").asText("").isBlank()) {
|
|
||||||
messages.add("render expectedColumns.label 不能为空");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!actions.isArray() || actions.isEmpty()) {
|
|
||||||
messages.add("render expectedActions 至少需要 1 项");
|
|
||||||
} else {
|
|
||||||
if (!arrayContainsText(actions, "新增")
|
|
||||||
&& !arrayContainsTextStartingWith(actions, "新增顶级")) {
|
|
||||||
messages.add("render expectedActions 必须包含新增动作");
|
|
||||||
}
|
|
||||||
if (!arrayContainsText(actions, "编辑")) {
|
|
||||||
messages.add("render expectedActions 必须包含编辑动作");
|
|
||||||
}
|
|
||||||
if (!arrayContainsText(actions, "删除")) {
|
|
||||||
messages.add("render expectedActions 必须包含删除动作");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!texts.isArray() || texts.isEmpty()) {
|
|
||||||
messages.add("render expectedTexts 至少需要 1 项");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new AiGateResult("render-plan", messages.isEmpty(), messages.stream().limit(20).toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean arrayContainsText(JsonNode array, String value) {
|
|
||||||
for (JsonNode item : array) {
|
|
||||||
if (value.equals(item.asText(""))) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean arrayContainsTextStartingWith(JsonNode array, String prefix) {
|
|
||||||
for (JsonNode item : array) {
|
|
||||||
if (item.asText("").startsWith(prefix)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean isValidFlowXml(String xml) {
|
|
||||||
if (xml == null || xml.isBlank()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
||||||
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
|
||||||
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
|
||||||
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
|
||||||
factory.setExpandEntityReferences(false);
|
|
||||||
var builder = factory.newDocumentBuilder();
|
|
||||||
var doc = builder.parse(new InputSource(new java.io.StringReader(xml)));
|
|
||||||
return "flow".equals(doc.getDocumentElement().getNodeName())
|
|
||||||
&& doc.getElementsByTagName("input").getLength() > 0
|
|
||||||
&& doc.getElementsByTagName("transition").getLength() > 0;
|
|
||||||
} catch (Exception ex) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private SchemaGate getSchemaGate() {
|
|
||||||
if (schemaGate == null) {
|
|
||||||
synchronized (AiAgentService.class) {
|
|
||||||
if (sharedSchemaGate == null) {
|
|
||||||
sharedSchemaGate = new SchemaGate();
|
|
||||||
}
|
|
||||||
schemaGate = sharedSchemaGate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return schemaGate;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void audit(String taskId, String action, String actor, Map<String, Object> details) {
|
private void audit(String taskId, String action, String actor, Map<String, Object> details) {
|
||||||
AiAuditEntry entry = new AiAuditEntry(
|
AiAuditEntry entry = new AiAuditEntry(
|
||||||
"aia-" + UUID.randomUUID(),
|
"aia-" + UUID.randomUUID(),
|
||||||
|
|||||||
@@ -0,0 +1,395 @@
|
|||||||
|
package com.eip4.platform.ai.service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
|
||||||
|
import org.xml.sax.InputSource;
|
||||||
|
|
||||||
|
import com.eip4.platform.ai.model.AiArtifactType;
|
||||||
|
import com.eip4.platform.ai.model.AiGateResult;
|
||||||
|
import com.eip4.platform.converter.SchemaGate;
|
||||||
|
import com.eip4.platform.metadata.schema.TypedPageSchemaIssue;
|
||||||
|
import com.eip4.platform.metadata.schema.TypedPageSchemaReport;
|
||||||
|
import com.eip4.platform.metadata.schema.TypedPageSchemaValidator;
|
||||||
|
import com.eip4.platform.metadata.schema.blueprint.PageBlueprint;
|
||||||
|
import com.eip4.platform.metadata.schema.blueprint.PageRecipeRegistry;
|
||||||
|
import com.eip4.platform.metadata.schema.blueprint.PageRecipeValidationReport;
|
||||||
|
import com.eip4.platform.metadata.schema.blueprint.PageRecipeValidator;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AI Agent 草稿门禁校验器(自 {@code AiAgentService} 抽出,§10.2 SRP 收敛 slice 2)。
|
||||||
|
*
|
||||||
|
* <p>把按 artifact 类型分发的门禁电池(必填字段 / amis-schema / typed-page-schema / page-recipe /
|
||||||
|
* 实体元数据 / 服务流 XML / 菜单绑定 / smoke-plan / render-plan)从编排类剥离,集中在此。
|
||||||
|
* <strong>逐方法从原 AiAgentService 搬迁、判定逻辑零变更</strong>——D3 红线(每类草稿在落库/发布前必过
|
||||||
|
* 对应门禁、终点恒人审)焊在这些判定里,搬迁不动任何阈值/正则/分支。{@code AiAgentServiceTest} +
|
||||||
|
* {@code AiAgentControllerTest} 的现有用例即行为安全网。
|
||||||
|
*
|
||||||
|
* <p>{@link SchemaGate} 的懒加载状态(进程级共享单例)随门禁一并移入本类。
|
||||||
|
*/
|
||||||
|
public class AiDraftGateValidator {
|
||||||
|
|
||||||
|
private static final Pattern TABLE_NAME = Pattern.compile("[A-Z][A-Z0-9_]{2,40}");
|
||||||
|
private static final Pattern FLOW_KEY = Pattern.compile("[A-Z][A-Z0-9_]{2,80}");
|
||||||
|
private static final Pattern COLUMN_NAME = Pattern.compile("[A-Z][A-Z0-9_]{0,39}");
|
||||||
|
private static final Pattern HTTP_METHOD = Pattern.compile("GET|POST|PUT|DELETE|PATCH");
|
||||||
|
|
||||||
|
private static volatile SchemaGate sharedSchemaGate;
|
||||||
|
private volatile SchemaGate schemaGate;
|
||||||
|
|
||||||
|
private final ObjectMapper objectMapper;
|
||||||
|
private final TypedPageSchemaValidator typedSchemaValidator;
|
||||||
|
private final PageRecipeRegistry pageRecipeRegistry;
|
||||||
|
private final PageRecipeValidator pageRecipeValidator;
|
||||||
|
|
||||||
|
public AiDraftGateValidator(ObjectMapper objectMapper, TypedPageSchemaValidator typedSchemaValidator,
|
||||||
|
PageRecipeRegistry pageRecipeRegistry, PageRecipeValidator pageRecipeValidator) {
|
||||||
|
this.objectMapper = objectMapper;
|
||||||
|
this.typedSchemaValidator = typedSchemaValidator;
|
||||||
|
this.pageRecipeRegistry = pageRecipeRegistry;
|
||||||
|
this.pageRecipeValidator = pageRecipeValidator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<AiGateResult> validateDraft(AiArtifactType type, String targetId, JsonNode payload,
|
||||||
|
Map<String, Object> metadata) {
|
||||||
|
List<AiGateResult> gates = new ArrayList<>();
|
||||||
|
gates.add(requiredFieldsGate(type, targetId, payload));
|
||||||
|
switch (type) {
|
||||||
|
case PAGE_SCHEMA -> {
|
||||||
|
gates.add(validateSchemaGate(payload));
|
||||||
|
gates.add(validateTypedPageSchemaGate(payload));
|
||||||
|
gates.add(validatePageRecipeGate(targetId, payload, metadata));
|
||||||
|
}
|
||||||
|
case ENTITY -> gates.add(validateEntityGate(payload));
|
||||||
|
case SERVICEFLOW -> gates.add(validateServiceFlowGate(payload));
|
||||||
|
case MENU -> gates.add(validateMenuGate(payload));
|
||||||
|
case TEST -> {
|
||||||
|
gates.add(validateTestGate(payload));
|
||||||
|
gates.add(validateRenderPlanGate(payload));
|
||||||
|
}
|
||||||
|
case CODE -> gates.add(new AiGateResult("code-review-required", true, List.of("代码草稿需后续静态检查/单测门禁")));
|
||||||
|
}
|
||||||
|
return List.copyOf(gates);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AiGateResult validateSchemaGate(JsonNode schema) {
|
||||||
|
if (schema == null || !schema.isObject()) {
|
||||||
|
return new AiGateResult("amis-schema", false, List.of("schema 顶层必须是 JSON object"));
|
||||||
|
}
|
||||||
|
List<String> errors = getSchemaGate().validate(schema);
|
||||||
|
return new AiGateResult("amis-schema", errors.isEmpty(), errors.stream().limit(20).toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private AiGateResult validateTypedPageSchemaGate(JsonNode schema) {
|
||||||
|
TypedPageSchemaReport report = validateTypedPageSchema(schema);
|
||||||
|
return new AiGateResult("typed-page-schema", report.valid(), typedMessages(report));
|
||||||
|
}
|
||||||
|
|
||||||
|
private AiGateResult validatePageRecipeGate(String targetId, JsonNode schema, Map<String, Object> metadata) {
|
||||||
|
boolean hasRecipe = metadata.containsKey("pageRecipe");
|
||||||
|
boolean hasBlueprint = metadata.containsKey("pageBlueprint");
|
||||||
|
if (!hasRecipe && !hasBlueprint) {
|
||||||
|
return new AiGateResult("page-recipe", true, List.of("未声明 pageRecipe,按手工 PAGE_SCHEMA 草稿跳过 recipe 门禁"));
|
||||||
|
}
|
||||||
|
List<String> messages = new ArrayList<>();
|
||||||
|
Object recipeValue = metadata.get("pageRecipe");
|
||||||
|
Object blueprintValue = metadata.get("pageBlueprint");
|
||||||
|
Object versionValue = metadata.get("pageBlueprintVersion");
|
||||||
|
if (!(recipeValue instanceof String recipe) || recipe.isBlank()) {
|
||||||
|
messages.add("pageRecipe metadata 不能为空");
|
||||||
|
}
|
||||||
|
if (blueprintValue == null) {
|
||||||
|
messages.add("pageBlueprint metadata 不能为空");
|
||||||
|
}
|
||||||
|
if (!(versionValue instanceof Number)) {
|
||||||
|
messages.add("pageBlueprintVersion metadata 必须是数字");
|
||||||
|
}
|
||||||
|
if (!messages.isEmpty()) {
|
||||||
|
return new AiGateResult("page-recipe", false, messages);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
PageBlueprint blueprint = objectMapper.convertValue(blueprintValue, PageBlueprint.class);
|
||||||
|
if (!recipeValue.equals(blueprint.recipe())) {
|
||||||
|
messages.add("pageRecipe 与 pageBlueprint.recipe 不一致");
|
||||||
|
}
|
||||||
|
if (!targetId.equals(blueprint.templateId())) {
|
||||||
|
messages.add("targetId 与 pageBlueprint.templateId 不一致");
|
||||||
|
}
|
||||||
|
if (((Number) versionValue).intValue() != blueprint.schemaVersion()) {
|
||||||
|
messages.add("pageBlueprintVersion 与 pageBlueprint.schemaVersion 不一致");
|
||||||
|
}
|
||||||
|
if (!messages.isEmpty()) {
|
||||||
|
return new AiGateResult("page-recipe", false, messages);
|
||||||
|
}
|
||||||
|
PageRecipeValidationReport report = pageRecipeValidator.validate(blueprint, schema);
|
||||||
|
return new AiGateResult("page-recipe", report.valid(), report.messages());
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
return new AiGateResult("page-recipe", false, List.of("pageBlueprint metadata 解析失败: " + ex.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypedPageSchemaReport validateTypedPageSchema(JsonNode schema) {
|
||||||
|
try {
|
||||||
|
return typedSchemaValidator.validate(schema == null ? "null" : objectMapper.writeValueAsString(schema));
|
||||||
|
} catch (JsonProcessingException ex) {
|
||||||
|
return typedSchemaValidator.validate("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> typedMessages(TypedPageSchemaReport report) {
|
||||||
|
return report.issues().stream()
|
||||||
|
.limit(20)
|
||||||
|
.map(AiDraftGateValidator::typedMessage)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String typedMessage(TypedPageSchemaIssue issue) {
|
||||||
|
return issue.path() + " " + issue.code() + ": " + issue.message();
|
||||||
|
}
|
||||||
|
|
||||||
|
private AiGateResult requiredFieldsGate(AiArtifactType type, String targetId, JsonNode payload) {
|
||||||
|
List<String> messages = new ArrayList<>();
|
||||||
|
if (targetId == null || targetId.isBlank()) {
|
||||||
|
messages.add("targetId 不能为空");
|
||||||
|
}
|
||||||
|
if (payload == null || !payload.isObject()) {
|
||||||
|
messages.add(type.name() + " payload 顶层必须是 JSON object");
|
||||||
|
}
|
||||||
|
return new AiGateResult("draft-required-fields", messages.isEmpty(), List.copyOf(messages));
|
||||||
|
}
|
||||||
|
|
||||||
|
private AiGateResult validateEntityGate(JsonNode payload) {
|
||||||
|
List<String> messages = new ArrayList<>();
|
||||||
|
String table = payload.path("tablename").asText("");
|
||||||
|
if (!TABLE_NAME.matcher(table).matches()) {
|
||||||
|
messages.add("tablename 必须是 3-40 位大写字母数字下划线,且以字母开头");
|
||||||
|
}
|
||||||
|
if (payload.path("name").asText("").isBlank()) {
|
||||||
|
messages.add("name 不能为空");
|
||||||
|
}
|
||||||
|
JsonNode attrs = payload.path("attributes");
|
||||||
|
if (!attrs.isArray() || attrs.isEmpty()) {
|
||||||
|
messages.add("attributes 至少需要 1 个业务字段");
|
||||||
|
} else {
|
||||||
|
for (JsonNode attr : attrs) {
|
||||||
|
String col = attr.path("column").asText("");
|
||||||
|
if (!COLUMN_NAME.matcher(col).matches()) {
|
||||||
|
messages.add("字段 column 不合法: " + col);
|
||||||
|
}
|
||||||
|
String type = attr.path("type").asText("");
|
||||||
|
if (!List.of("string", "integer", "number", "date", "datetime", "boolean").contains(type)) {
|
||||||
|
messages.add("字段 " + col + " type 不支持: " + type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new AiGateResult("entity-meta", messages.isEmpty(), messages.stream().limit(20).toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private AiGateResult validateServiceFlowGate(JsonNode payload) {
|
||||||
|
List<String> messages = new ArrayList<>();
|
||||||
|
JsonNode flows = payload.path("flows");
|
||||||
|
if (!flows.isArray() || flows.isEmpty()) {
|
||||||
|
messages.add("flows 至少需要 1 个服务流定义");
|
||||||
|
} else {
|
||||||
|
for (JsonNode flow : flows) {
|
||||||
|
String key = flow.path("key").asText("");
|
||||||
|
if (!FLOW_KEY.matcher(key).matches()) {
|
||||||
|
messages.add("服务流 key 不合法: " + key);
|
||||||
|
}
|
||||||
|
String xml = flow.path("contentXml").asText("");
|
||||||
|
if (!isValidFlowXml(xml)) {
|
||||||
|
messages.add("服务流 XML 不合法: " + key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new AiGateResult("serviceflow-xml", messages.isEmpty(), messages.stream().limit(20).toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private AiGateResult validateMenuGate(JsonNode payload) {
|
||||||
|
List<String> messages = new ArrayList<>();
|
||||||
|
if (payload.path("name").asText("").isBlank()) {
|
||||||
|
messages.add("菜单 name 不能为空");
|
||||||
|
}
|
||||||
|
if (payload.path("viewTemplateId").asText("").isBlank()) {
|
||||||
|
messages.add("菜单 viewTemplateId 不能为空");
|
||||||
|
}
|
||||||
|
return new AiGateResult("menu-binding", messages.isEmpty(), List.copyOf(messages));
|
||||||
|
}
|
||||||
|
|
||||||
|
private AiGateResult validateTestGate(JsonNode payload) {
|
||||||
|
List<String> messages = new ArrayList<>();
|
||||||
|
JsonNode tests = payload.path("smokeTests");
|
||||||
|
if (!tests.isArray() || tests.isEmpty()) {
|
||||||
|
messages.add("smokeTests 至少需要 1 条");
|
||||||
|
} else {
|
||||||
|
for (JsonNode test : tests) {
|
||||||
|
String method = test.path("method").asText("");
|
||||||
|
String url = test.path("url").asText("");
|
||||||
|
if (!HTTP_METHOD.matcher(method).matches()) {
|
||||||
|
messages.add("smoke method 不合法: " + method);
|
||||||
|
}
|
||||||
|
if (!url.startsWith("/api/")) {
|
||||||
|
messages.add("smoke url 必须以 /api/ 开头: " + url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new AiGateResult("smoke-plan", messages.isEmpty(), messages.stream().limit(20).toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private AiGateResult validateRenderPlanGate(JsonNode payload) {
|
||||||
|
List<String> messages = new ArrayList<>();
|
||||||
|
JsonNode tests = payload.path("renderTests");
|
||||||
|
if (!tests.isArray() || tests.isEmpty()) {
|
||||||
|
messages.add("renderTests 至少需要 1 条");
|
||||||
|
} else {
|
||||||
|
for (JsonNode test : tests) {
|
||||||
|
String name = test.path("name").asText("");
|
||||||
|
String recipe = test.path("recipe").asText("");
|
||||||
|
String schemaUrl = test.path("schemaUrl").asText("");
|
||||||
|
String previewUrl = test.path("previewUrl").asText("");
|
||||||
|
String rootType = test.path("expectedRootType").asText("");
|
||||||
|
String bodyType = test.path("expectedBodyType").asText("");
|
||||||
|
String crudName = test.path("expectedCrudName").asText("");
|
||||||
|
String expectedApi = test.path("expectedApi").asText("");
|
||||||
|
JsonNode components = test.path("expectedComponents");
|
||||||
|
JsonNode columns = test.path("expectedColumns");
|
||||||
|
JsonNode actions = test.path("expectedActions");
|
||||||
|
JsonNode texts = test.path("expectedTexts");
|
||||||
|
if (name.isBlank()) {
|
||||||
|
messages.add("render name 不能为空");
|
||||||
|
}
|
||||||
|
if (pageRecipeRegistry.find(recipe).isEmpty()) {
|
||||||
|
messages.add("render recipe 未注册: " + recipe);
|
||||||
|
}
|
||||||
|
if (!schemaUrl.startsWith("/api/page/schema/")) {
|
||||||
|
messages.add("render schemaUrl 必须以 /api/page/schema/ 开头: " + schemaUrl);
|
||||||
|
}
|
||||||
|
if (!previewUrl.startsWith("/amis.html?schema=")
|
||||||
|
&& !previewUrl.startsWith("/amis.html?templateId=")
|
||||||
|
&& !previewUrl.startsWith("/amis.html?schemaUrl=")) {
|
||||||
|
messages.add("render previewUrl 必须指向可解析 schema 的 /amis.html: " + previewUrl);
|
||||||
|
}
|
||||||
|
if (!"page".equals(rootType)) {
|
||||||
|
messages.add("render expectedRootType 必须是 page");
|
||||||
|
}
|
||||||
|
if (!"crud".equals(bodyType)) {
|
||||||
|
messages.add("render expectedBodyType 必须是 crud");
|
||||||
|
}
|
||||||
|
if (crudName.isBlank()) {
|
||||||
|
messages.add("render expectedCrudName 不能为空");
|
||||||
|
}
|
||||||
|
// house-style 对齐:单表维护页列表走保真层服务流 _SEARCH(/api/flow),
|
||||||
|
// 树页仍走 /api/entity 树接口。
|
||||||
|
if (PageBlueprint.RECIPE_TREE_CRUD.equals(recipe)) {
|
||||||
|
if (!expectedApi.startsWith("/api/entity/") || !expectedApi.contains("/tree?")) {
|
||||||
|
messages.add("tree-crud-v1 render expectedApi 必须是 /api/entity 树接口: " + expectedApi);
|
||||||
|
}
|
||||||
|
} else if (PageBlueprint.RECIPE_MAINTENANCE_CRUD.equals(recipe)) {
|
||||||
|
if (!expectedApi.startsWith("/api/flow/") || !expectedApi.endsWith("_SEARCH")) {
|
||||||
|
messages.add("maintenance-crud-v1 render expectedApi 必须是 /api/flow 服务流 _SEARCH 接口: "
|
||||||
|
+ expectedApi);
|
||||||
|
}
|
||||||
|
} else if (!expectedApi.startsWith("/api/")) {
|
||||||
|
messages.add("render expectedApi 必须指向 /api/: " + expectedApi);
|
||||||
|
}
|
||||||
|
if (!components.isArray() || components.isEmpty()) {
|
||||||
|
messages.add("render expectedComponents 至少需要 1 项");
|
||||||
|
} else {
|
||||||
|
if (!arrayContainsText(components, "page")) {
|
||||||
|
messages.add("render expectedComponents 必须包含 page");
|
||||||
|
}
|
||||||
|
if (!arrayContainsText(components, "crud")) {
|
||||||
|
messages.add("render expectedComponents 必须包含 crud");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!columns.isArray() || columns.isEmpty()) {
|
||||||
|
messages.add("render expectedColumns 至少需要 1 项");
|
||||||
|
} else {
|
||||||
|
for (JsonNode column : columns) {
|
||||||
|
if (column.path("name").asText("").isBlank()) {
|
||||||
|
messages.add("render expectedColumns.name 不能为空");
|
||||||
|
}
|
||||||
|
if (column.path("label").asText("").isBlank()) {
|
||||||
|
messages.add("render expectedColumns.label 不能为空");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!actions.isArray() || actions.isEmpty()) {
|
||||||
|
messages.add("render expectedActions 至少需要 1 项");
|
||||||
|
} else {
|
||||||
|
if (!arrayContainsText(actions, "新增")
|
||||||
|
&& !arrayContainsTextStartingWith(actions, "新增顶级")) {
|
||||||
|
messages.add("render expectedActions 必须包含新增动作");
|
||||||
|
}
|
||||||
|
if (!arrayContainsText(actions, "编辑")) {
|
||||||
|
messages.add("render expectedActions 必须包含编辑动作");
|
||||||
|
}
|
||||||
|
if (!arrayContainsText(actions, "删除")) {
|
||||||
|
messages.add("render expectedActions 必须包含删除动作");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!texts.isArray() || texts.isEmpty()) {
|
||||||
|
messages.add("render expectedTexts 至少需要 1 项");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new AiGateResult("render-plan", messages.isEmpty(), messages.stream().limit(20).toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean arrayContainsText(JsonNode array, String value) {
|
||||||
|
for (JsonNode item : array) {
|
||||||
|
if (value.equals(item.asText(""))) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean arrayContainsTextStartingWith(JsonNode array, String prefix) {
|
||||||
|
for (JsonNode item : array) {
|
||||||
|
if (item.asText("").startsWith(prefix)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isValidFlowXml(String xml) {
|
||||||
|
if (xml == null || xml.isBlank()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
|
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||||
|
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
||||||
|
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
||||||
|
factory.setExpandEntityReferences(false);
|
||||||
|
var builder = factory.newDocumentBuilder();
|
||||||
|
var doc = builder.parse(new InputSource(new java.io.StringReader(xml)));
|
||||||
|
return "flow".equals(doc.getDocumentElement().getNodeName())
|
||||||
|
&& doc.getElementsByTagName("input").getLength() > 0
|
||||||
|
&& doc.getElementsByTagName("transition").getLength() > 0;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private SchemaGate getSchemaGate() {
|
||||||
|
if (schemaGate == null) {
|
||||||
|
synchronized (AiDraftGateValidator.class) {
|
||||||
|
if (sharedSchemaGate == null) {
|
||||||
|
sharedSchemaGate = new SchemaGate();
|
||||||
|
}
|
||||||
|
schemaGate = sharedSchemaGate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return schemaGate;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
package com.eip4.platform.ai.service;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import com.eip4.platform.ai.model.AiArtifactType;
|
||||||
|
import com.eip4.platform.ai.model.AiGateResult;
|
||||||
|
import com.eip4.platform.metadata.schema.PageComponentRegistry;
|
||||||
|
import com.eip4.platform.metadata.schema.TypedPageSchemaValidator;
|
||||||
|
import com.eip4.platform.metadata.schema.blueprint.PageRecipeRegistry;
|
||||||
|
import com.eip4.platform.metadata.schema.blueprint.PageRecipeValidator;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AiDraftGateValidator 直测(§10.2 SRP slice 2 抽取的门禁簇的独立契约测试)。
|
||||||
|
*
|
||||||
|
* <p>门禁逐方法从 AiAgentService 搬迁、判定零变更;此处直接对验证器钉死各类草稿的关键 pass/fail 判定,
|
||||||
|
* 与经 {@code AiAgentServiceTest.addDraft} 的传递覆盖互补。红线:每类草稿落库/发布前必过对应门禁。
|
||||||
|
*/
|
||||||
|
class AiDraftGateValidatorTest {
|
||||||
|
|
||||||
|
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||||
|
|
||||||
|
private AiDraftGateValidator validator() {
|
||||||
|
PageRecipeRegistry registry = PageRecipeRegistry.defaults();
|
||||||
|
return new AiDraftGateValidator(
|
||||||
|
MAPPER,
|
||||||
|
new TypedPageSchemaValidator(MAPPER, PageComponentRegistry.defaults()),
|
||||||
|
registry,
|
||||||
|
new PageRecipeValidator(MAPPER, registry));
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonNode json(String s) {
|
||||||
|
try {
|
||||||
|
return MAPPER.readTree(s);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private AiGateResult gate(List<AiGateResult> gates, String name) {
|
||||||
|
return gates.stream().filter(g -> g.name().equals(name)).findFirst().orElseThrow();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void requiredFields_targetId空被拒() {
|
||||||
|
List<AiGateResult> g = validator().validateDraft(AiArtifactType.ENTITY, "", json("{}"), Map.of());
|
||||||
|
assertFalse(gate(g, "draft-required-fields").passed());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void entity_合法通过_非法tablename被拒() {
|
||||||
|
String ok = "{\"tablename\":\"OA_DEMO\",\"name\":\"演示\",\"attributes\":[{\"column\":\"TITLE\",\"type\":\"string\"}]}";
|
||||||
|
assertTrue(gate(validator().validateDraft(AiArtifactType.ENTITY, "OA_DEMO", json(ok), Map.of()),
|
||||||
|
"entity-meta").passed());
|
||||||
|
String bad = "{\"tablename\":\"oa demo\",\"name\":\"\",\"attributes\":[]}";
|
||||||
|
assertFalse(gate(validator().validateDraft(AiArtifactType.ENTITY, "X", json(bad), Map.of()),
|
||||||
|
"entity-meta").passed());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void serviceFlow_非法key与xml被拒() {
|
||||||
|
String bad = "{\"flows\":[{\"key\":\"bad key\",\"contentXml\":\"<notflow/>\"}]}";
|
||||||
|
assertFalse(gate(validator().validateDraft(AiArtifactType.SERVICEFLOW, "F", json(bad), Map.of()),
|
||||||
|
"serviceflow-xml").passed());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void menu_缺name或viewTemplateId被拒() {
|
||||||
|
assertFalse(gate(validator().validateDraft(AiArtifactType.MENU, "M", json("{\"name\":\"\"}"), Map.of()),
|
||||||
|
"menu-binding").passed());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void code_恒通过() {
|
||||||
|
assertTrue(gate(validator().validateDraft(AiArtifactType.CODE, "C", json("{}"), Map.of()),
|
||||||
|
"code-review-required").passed());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void pageSchema_顶层非object的amisSchema门被拒() {
|
||||||
|
AiGateResult r = validator().validateSchemaGate(MAPPER.createArrayNode());
|
||||||
|
assertFalse(r.passed());
|
||||||
|
assertEquals("amis-schema", r.name());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void pageRecipe_未声明recipe时跳过门() {
|
||||||
|
// 无 pageRecipe/pageBlueprint metadata → page-recipe 门以通过(跳过)放行手工草稿
|
||||||
|
String schema = "{\"type\":\"page\"}";
|
||||||
|
List<AiGateResult> g = validator().validateDraft(AiArtifactType.PAGE_SCHEMA, "PG", json(schema), Map.of());
|
||||||
|
assertTrue(gate(g, "page-recipe").passed());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user