feat: enforce AI page recipe gate
This commit is contained in:
@@ -61,6 +61,10 @@ 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.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;
|
||||
@@ -86,6 +90,7 @@ public class AiAgentService {
|
||||
private final ObjectMapper objectMapper;
|
||||
private final AiToolRegistry toolRegistry;
|
||||
private final TypedPageSchemaValidator typedSchemaValidator;
|
||||
private final PageRecipeValidator pageRecipeValidator;
|
||||
|
||||
private static volatile SchemaGate sharedSchemaGate;
|
||||
private volatile SchemaGate schemaGate;
|
||||
@@ -122,6 +127,7 @@ public class AiAgentService {
|
||||
this.typedSchemaValidator = typedSchemaValidator == null
|
||||
? new TypedPageSchemaValidator(objectMapper, PageComponentRegistry.defaults())
|
||||
: typedSchemaValidator;
|
||||
this.pageRecipeValidator = new PageRecipeValidator(objectMapper, PageRecipeRegistry.defaults());
|
||||
}
|
||||
|
||||
public AiTask createTask(String objective) {
|
||||
@@ -154,7 +160,8 @@ public class AiAgentService {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "payload 不能为空");
|
||||
}
|
||||
|
||||
List<AiGateResult> gates = validateDraft(artifactType, target, payload);
|
||||
Map<String, Object> safeMetadata = metadata == null ? Map.of() : metadata;
|
||||
List<AiGateResult> gates = validateDraft(artifactType, target, payload, safeMetadata);
|
||||
AiDraft draft = new AiDraft(
|
||||
"aid-" + UUID.randomUUID(),
|
||||
taskId,
|
||||
@@ -163,7 +170,7 @@ public class AiAgentService {
|
||||
payload,
|
||||
System.currentTimeMillis(),
|
||||
gates,
|
||||
metadata == null ? Map.of() : Map.copyOf(metadata));
|
||||
Map.copyOf(safeMetadata));
|
||||
store.saveDraft(draft);
|
||||
audit(taskId, "draft.saved", "agent", Map.of(
|
||||
"draftId", draft.id(),
|
||||
@@ -781,6 +788,49 @@ public class AiAgentService {
|
||||
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));
|
||||
@@ -800,13 +850,15 @@ public class AiAgentService {
|
||||
return issue.path() + " " + issue.code() + ": " + issue.message();
|
||||
}
|
||||
|
||||
private List<AiGateResult> validateDraft(AiArtifactType type, String targetId, JsonNode payload) {
|
||||
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));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.eip4.platform.ai.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -251,7 +252,7 @@ public class DeterministicAiAgentRepairExecutor implements AiAgentRepairExecutor
|
||||
AiArtifactType.PAGE_SCHEMA,
|
||||
draft.targetId(),
|
||||
copy,
|
||||
Map.of(
|
||||
repairMetadata(draft,
|
||||
"source", "deterministic-agent-repair",
|
||||
"repairActionId", action.id(),
|
||||
"supersedesDraftId", draft.id())));
|
||||
@@ -318,4 +319,15 @@ public class DeterministicAiAgentRepairExecutor implements AiAgentRepairExecutor
|
||||
Object value = action.details() == null ? null : action.details().get(key);
|
||||
return value == null ? "" : String.valueOf(value);
|
||||
}
|
||||
|
||||
private static Map<String, Object> repairMetadata(AiDraft draft, Object... entries) {
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
if (draft.metadata() != null) {
|
||||
out.putAll(draft.metadata());
|
||||
}
|
||||
for (int i = 0; i + 1 < entries.length; i += 2) {
|
||||
out.put(String.valueOf(entries[i]), entries[i + 1]);
|
||||
}
|
||||
return Map.copyOf(out);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ fieldMappings:
|
||||
datetime: input-datetime
|
||||
boolean: switch
|
||||
gates:
|
||||
- page-recipe
|
||||
- typed-page-schema
|
||||
- amis-schema
|
||||
- local-smoke
|
||||
|
||||
Reference in New Issue
Block a user