feat: enforce typed page schema in AI drafts

This commit is contained in:
2026-06-17 09:15:10 +08:00
parent c5b7b68210
commit c1ff0db96b
9 changed files with 181 additions and 16 deletions

View File

@@ -57,6 +57,10 @@ import com.eip4.platform.ai.model.AiToolDefinition;
import com.eip4.platform.ai.model.AiEmployeeDefinition;
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.TypedPageSchemaIssue;
import com.eip4.platform.metadata.schema.TypedPageSchemaReport;
import com.eip4.platform.metadata.schema.TypedPageSchemaValidator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -81,6 +85,7 @@ public class AiAgentService {
private final AiAgentRepairExecutor agentRepairExecutor;
private final ObjectMapper objectMapper;
private final AiToolRegistry toolRegistry;
private final TypedPageSchemaValidator typedSchemaValidator;
private static volatile SchemaGate sharedSchemaGate;
private volatile SchemaGate schemaGate;
@@ -90,13 +95,23 @@ public class AiAgentService {
AiSmokeRunner smokeRunner, AiLocalReleaseExecutor localReleaseExecutor,
AiAgentRepairExecutor agentRepairExecutor, ObjectMapper objectMapper) {
this(store, featureDraftGenerator, smokeRunner, localReleaseExecutor, agentRepairExecutor,
objectMapper, AiToolRegistry.defaults());
objectMapper, AiToolRegistry.defaults(),
new TypedPageSchemaValidator(objectMapper, PageComponentRegistry.defaults()));
}
AiAgentService(AiAgentStore store, AiFeatureDraftGenerator featureDraftGenerator,
AiSmokeRunner smokeRunner, AiLocalReleaseExecutor localReleaseExecutor,
AiAgentRepairExecutor agentRepairExecutor, ObjectMapper objectMapper,
AiToolRegistry toolRegistry) {
this(store, featureDraftGenerator, smokeRunner, localReleaseExecutor, agentRepairExecutor,
objectMapper, toolRegistry,
new TypedPageSchemaValidator(objectMapper, PageComponentRegistry.defaults()));
}
AiAgentService(AiAgentStore store, AiFeatureDraftGenerator featureDraftGenerator,
AiSmokeRunner smokeRunner, AiLocalReleaseExecutor localReleaseExecutor,
AiAgentRepairExecutor agentRepairExecutor, ObjectMapper objectMapper,
AiToolRegistry toolRegistry, TypedPageSchemaValidator typedSchemaValidator) {
this.store = store;
this.featureDraftGenerator = featureDraftGenerator;
this.smokeRunner = smokeRunner;
@@ -104,6 +119,9 @@ public class AiAgentService {
this.agentRepairExecutor = agentRepairExecutor;
this.objectMapper = objectMapper;
this.toolRegistry = toolRegistry;
this.typedSchemaValidator = typedSchemaValidator == null
? new TypedPageSchemaValidator(objectMapper, PageComponentRegistry.defaults())
: typedSchemaValidator;
}
public AiTask createTask(String objective) {
@@ -704,10 +722,26 @@ public class AiAgentService {
}
public Map<String, Object> validateSchema(JsonNode schema) {
AiGateResult gate = validateSchemaGate(schema);
AiGateResult schemaGate = validateSchemaGate(schema);
TypedPageSchemaReport typedReport = validateTypedPageSchema(schema);
List<String> typedMessages = typedMessages(typedReport);
List<String> validationErrors = new ArrayList<>();
validationErrors.addAll(schemaGate.messages());
validationErrors.addAll(typedMessages);
Map<String, Object> resp = new LinkedHashMap<>();
resp.put("schemaValid", gate.passed());
resp.put("validationErrors", gate.messages());
resp.put("valid", schemaGate.passed() && typedReport.valid());
resp.put("schemaValid", schemaGate.passed());
resp.put("typedSchemaValid", typedReport.valid());
resp.put("validationErrors", validationErrors.stream().limit(40).toList());
resp.put("typedSchemaVersion", typedReport.schemaVersion());
resp.put("typedSchemaIssues", typedReport.issues().stream().limit(20)
.map(issue -> Map.of(
"path", issue.path(),
"code", issue.code(),
"message", issue.message()))
.toList());
resp.put("usedComponents", typedReport.usedComponents());
resp.put("usedActions", typedReport.usedActions());
return resp;
}
@@ -742,17 +776,44 @@ public class AiAgentService {
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 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) {
List<AiGateResult> gates = new ArrayList<>();
gates.add(requiredFieldsGate(type, targetId, payload));
gates.add(switch (type) {
case ENTITY -> validateEntityGate(payload);
case PAGE_SCHEMA -> validateSchemaGate(payload);
case SERVICEFLOW -> validateServiceFlowGate(payload);
case MENU -> validateMenuGate(payload);
case TEST -> validateTestGate(payload);
case CODE -> new AiGateResult("code-review-required", true, List.of("代码草稿需后续静态检查/单测门禁"));
});
switch (type) {
case PAGE_SCHEMA -> {
gates.add(validateSchemaGate(payload));
gates.add(validateTypedPageSchemaGate(payload));
}
case ENTITY -> gates.add(validateEntityGate(payload));
case SERVICEFLOW -> gates.add(validateServiceFlowGate(payload));
case MENU -> gates.add(validateMenuGate(payload));
case TEST -> gates.add(validateTestGate(payload));
case CODE -> gates.add(new AiGateResult("code-review-required", true, List.of("代码草稿需后续静态检查/单测门禁")));
}
return List.copyOf(gates);
}
@@ -1344,7 +1405,7 @@ public class AiAgentService {
INSERT INTO sys_serviceflow
(sys_serviceflow_id, displayname, contentxml, entityid, iscontrol, isupdateable,
isactive, createtime, updatetime, createby, updateby, version)
VALUES (%s, %s, %s, %s, %d, %d, 1, %d, %d, 'AI_AGENT', 'AI_AGENT', 0)
VALUES (%s, %s, %s::xml, %s, %d, %d, 1, %d, %d, 'AI_AGENT', 'AI_AGENT', 0)
ON CONFLICT (sys_serviceflow_id) DO UPDATE SET
displayname = EXCLUDED.displayname,
contentxml = EXCLUDED.contentxml,

View File

@@ -127,7 +127,7 @@ public class AiLocalReleaseExecutor {
INSERT INTO sys_serviceflow
(sys_serviceflow_id, displayname, contentxml, entityid, iscontrol, isupdateable,
isactive, createtime, updatetime, createby, updateby, version)
VALUES (?, ?, ?, ?, ?, ?, 1, ?, ?, 'AI_AGENT', 'AI_AGENT', 0)
VALUES (?, ?, ?::xml, ?, ?, ?, 1, ?, ?, 'AI_AGENT', 'AI_AGENT', 0)
ON CONFLICT (sys_serviceflow_id) DO UPDATE SET
displayname = EXCLUDED.displayname,
contentxml = EXCLUDED.contentxml,