feat: add AI tool access registry

This commit is contained in:
2026-06-17 08:45:56 +08:00
parent 5eb95a4ef4
commit 8bdde45e89
13 changed files with 436 additions and 1 deletions

View File

@@ -0,0 +1,12 @@
package com.eip4.platform.ai.model;
import java.util.List;
public record AiEmployeeDefinition(
String id,
String name,
String role,
List<String> toolNames,
List<String> requiredScopes,
List<String> policies) {
}

View File

@@ -0,0 +1,11 @@
package com.eip4.platform.ai.model;
import java.util.List;
public record AiToolAccessDecision(
boolean allowed,
List<String> missingScopes,
List<String> blockers,
AiToolDefinition tool,
AiEmployeeDefinition employee) {
}

View File

@@ -0,0 +1,11 @@
package com.eip4.platform.ai.model;
import java.util.List;
public record AiToolAccessRequest(
String employeeId,
String toolName,
List<String> grantedScopes,
String targetEnv,
boolean productionWrite) {
}

View File

@@ -0,0 +1,13 @@
package com.eip4.platform.ai.model;
import java.util.List;
public record AiToolDefinition(
String name,
String category,
String description,
List<String> operations,
List<String> requiredScopes,
boolean localOnly,
boolean productionWriteAllowed) {
}

View File

@@ -18,6 +18,7 @@ import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
@@ -50,6 +51,10 @@ import com.eip4.platform.ai.model.AiRunRecord;
import com.eip4.platform.ai.model.AiSmokeResult;
import com.eip4.platform.ai.model.AiSmokeRun;
import com.eip4.platform.ai.model.AiTask;
import com.eip4.platform.ai.model.AiToolAccessDecision;
import com.eip4.platform.ai.model.AiToolAccessRequest;
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.fasterxml.jackson.core.JsonProcessingException;
@@ -75,19 +80,30 @@ public class AiAgentService {
private final AiLocalReleaseExecutor localReleaseExecutor;
private final AiAgentRepairExecutor agentRepairExecutor;
private final ObjectMapper objectMapper;
private final AiToolRegistry toolRegistry;
private static volatile SchemaGate sharedSchemaGate;
private volatile SchemaGate schemaGate;
@Autowired
public AiAgentService(AiAgentStore store, AiFeatureDraftGenerator featureDraftGenerator,
AiSmokeRunner smokeRunner, AiLocalReleaseExecutor localReleaseExecutor,
AiAgentRepairExecutor agentRepairExecutor, ObjectMapper objectMapper) {
this(store, featureDraftGenerator, smokeRunner, localReleaseExecutor, agentRepairExecutor,
objectMapper, AiToolRegistry.defaults());
}
AiAgentService(AiAgentStore store, AiFeatureDraftGenerator featureDraftGenerator,
AiSmokeRunner smokeRunner, AiLocalReleaseExecutor localReleaseExecutor,
AiAgentRepairExecutor agentRepairExecutor, ObjectMapper objectMapper,
AiToolRegistry toolRegistry) {
this.store = store;
this.featureDraftGenerator = featureDraftGenerator;
this.smokeRunner = smokeRunner;
this.localReleaseExecutor = localReleaseExecutor;
this.agentRepairExecutor = agentRepairExecutor;
this.objectMapper = objectMapper;
this.toolRegistry = toolRegistry;
}
public AiTask createTask(String objective) {
@@ -695,6 +711,18 @@ public class AiAgentService {
return resp;
}
public List<AiToolDefinition> listTools() {
return toolRegistry.tools();
}
public List<AiEmployeeDefinition> listEmployees() {
return toolRegistry.employees();
}
public AiToolAccessDecision checkToolAccess(AiToolAccessRequest request) {
return toolRegistry.check(request);
}
private AiArtifactType parseType(String type) {
if (type == null || type.isBlank()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "artifactType 不能为空");

View File

@@ -0,0 +1,166 @@
package com.eip4.platform.ai.service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import com.eip4.platform.ai.model.AiEmployeeDefinition;
import com.eip4.platform.ai.model.AiToolAccessDecision;
import com.eip4.platform.ai.model.AiToolAccessRequest;
import com.eip4.platform.ai.model.AiToolDefinition;
public final class AiToolRegistry {
private final Map<String, AiToolDefinition> tools;
private final Map<String, AiEmployeeDefinition> employees;
private AiToolRegistry(Map<String, AiToolDefinition> tools, Map<String, AiEmployeeDefinition> employees) {
this.tools = Collections.unmodifiableMap(new LinkedHashMap<>(tools));
this.employees = Collections.unmodifiableMap(new LinkedHashMap<>(employees));
}
public static AiToolRegistry defaults() {
Map<String, AiToolDefinition> tools = new LinkedHashMap<>();
registerTool(tools, "feature.draft.generate", "development",
"Generate entity, page schema, serviceflow, menu and smoke drafts.",
List.of("POST /api/ai/features/develop", "POST /api/ai/features/auto-develop"),
List.of("ai:feature:draft"), false, false);
registerTool(tools, "schema.validate", "validation",
"Validate AMIS and typed page schema before saving or packaging.",
List.of("POST /api/ai/tools/validate-schema", "POST /api/page/schema/validate"),
List.of("ai:schema:validate"), false, false);
registerTool(tools, "local.apply", "local-gate",
"Apply a change set to a local EIP4 environment only.",
List.of("POST /api/ai/tasks/{taskId}/change-sets/{changeSetId}/local-apply"),
List.of("ai:local:apply"), true, false);
registerTool(tools, "smoke.run", "local-gate",
"Run local smoke checks against a local EIP4 baseUrl.",
List.of("POST /api/ai/tasks/{taskId}/change-sets/{changeSetId}/smoke"),
List.of("ai:smoke:run"), true, false);
registerTool(tools, "feature.package", "release-prep",
"Create feature packages, revisions and release evidence for human review.",
List.of("POST /api/ai/tasks/{taskId}/change-sets/{changeSetId}/feature-package",
"POST /api/ai/tasks/{taskId}/feature-packages/{packageId}/revisions"),
List.of("ai:package:create"), false, false);
registerTool(tools, "release.prepare", "release-prep",
"Export, verify, diff and prepare rollback plans without production execution.",
List.of("eip4 release verify", "eip4 release export", "eip4 release diff",
"eip4 release rollback-plan"),
List.of("ai:release:prepare"), false, false);
Map<String, AiEmployeeDefinition> employees = new LinkedHashMap<>();
registerEmployee(employees, "agent-developer", "Agent Developer",
"Develops EIP4 feature packages in local environments.",
List.of("feature.draft.generate", "schema.validate", "local.apply", "smoke.run",
"feature.package", "release.prepare"),
List.of("ai:feature:draft", "ai:schema:validate", "ai:local:apply", "ai:smoke:run",
"ai:package:create", "ai:release:prepare"),
List.of("Local apply only", "Production deployment requires human operator"));
registerEmployee(employees, "release-auditor", "Release Auditor",
"Reviews packages, diffs and rollback plans before human deployment.",
List.of("schema.validate", "feature.package", "release.prepare"),
List.of("ai:schema:validate", "ai:package:create", "ai:release:prepare"),
List.of("Read-only release preparation", "No environment writes"));
registerEmployee(employees, "support-diagnoser", "Support Diagnoser",
"Reads run evidence and prepares repair plans for local reproduction.",
List.of("schema.validate", "smoke.run"),
List.of("ai:schema:validate", "ai:smoke:run"),
List.of("Local diagnostics only"));
return new AiToolRegistry(tools, employees);
}
public List<AiToolDefinition> tools() {
return List.copyOf(tools.values());
}
public List<AiEmployeeDefinition> employees() {
return List.copyOf(employees.values());
}
public AiToolAccessDecision check(AiToolAccessRequest request) {
String employeeId = text(request == null ? null : request.employeeId());
String toolName = text(request == null ? null : request.toolName());
AiEmployeeDefinition employee = employees.get(employeeId);
AiToolDefinition tool = tools.get(toolName);
List<String> blockers = new ArrayList<>();
if (employee == null) {
blockers.add("Unknown AI employee: " + employeeId);
}
if (tool == null) {
blockers.add("Unknown AI tool: " + toolName);
}
if (employee == null || tool == null) {
return new AiToolAccessDecision(false, List.of(), List.copyOf(blockers), tool, employee);
}
if (!employee.toolNames().contains(tool.name())) {
blockers.add("Employee " + employee.id() + " is not allowed to use tool " + tool.name());
}
Set<String> granted = new LinkedHashSet<>(request.grantedScopes() == null
? List.of() : request.grantedScopes());
List<String> missingScopes = new ArrayList<>();
for (String scope : tool.requiredScopes()) {
if (!granted.contains(scope)) {
missingScopes.add(scope);
}
}
for (String scope : employee.requiredScopes()) {
if (tool.requiredScopes().contains(scope) && !granted.contains(scope) && !missingScopes.contains(scope)) {
missingScopes.add(scope);
}
}
if (!missingScopes.isEmpty()) {
blockers.add("Missing scopes: " + String.join(",", missingScopes));
}
String targetEnv = text(request.targetEnv()).toLowerCase(Locale.ROOT);
if (tool.localOnly() && !isLocalEnv(targetEnv)) {
blockers.add("Tool " + tool.name() + " is local-only, targetEnv=" + targetEnv);
}
if (request.productionWrite() || isProductionEnv(targetEnv)) {
if (!tool.productionWriteAllowed()) {
blockers.add("Tool " + tool.name() + " is not allowed to perform production writes");
}
if (!granted.contains("human:approval")) {
blockers.add("Production write requires human:approval scope");
}
}
return new AiToolAccessDecision(blockers.isEmpty(), List.copyOf(missingScopes), List.copyOf(blockers),
tool, employee);
}
private static void registerTool(Map<String, AiToolDefinition> tools, String name, String category,
String description, List<String> operations, List<String> requiredScopes,
boolean localOnly, boolean productionWriteAllowed) {
tools.put(name, new AiToolDefinition(name, category, description, List.copyOf(operations),
List.copyOf(requiredScopes), localOnly, productionWriteAllowed));
}
private static void registerEmployee(Map<String, AiEmployeeDefinition> employees, String id, String name,
String role, List<String> toolNames, List<String> requiredScopes,
List<String> policies) {
employees.put(id, new AiEmployeeDefinition(id, name, role, List.copyOf(toolNames),
List.copyOf(requiredScopes), List.copyOf(policies)));
}
private static boolean isLocalEnv(String targetEnv) {
return targetEnv.isBlank()
|| targetEnv.equals("local")
|| targetEnv.equals("dev")
|| targetEnv.startsWith("localhost")
|| targetEnv.startsWith("127.0.0.1")
|| targetEnv.startsWith("http://localhost")
|| targetEnv.startsWith("http://127.0.0.1");
}
private static boolean isProductionEnv(String targetEnv) {
return targetEnv.equals("prod") || targetEnv.equals("production") || targetEnv.contains("prod");
}
private static String text(String value) {
return value == null ? "" : value.trim();
}
}

View File

@@ -28,6 +28,10 @@ import com.eip4.platform.ai.model.AiRevision;
import com.eip4.platform.ai.model.AiRunRecord;
import com.eip4.platform.ai.model.AiSmokeRun;
import com.eip4.platform.ai.model.AiTask;
import com.eip4.platform.ai.model.AiToolAccessDecision;
import com.eip4.platform.ai.model.AiToolAccessRequest;
import com.eip4.platform.ai.model.AiToolDefinition;
import com.eip4.platform.ai.model.AiEmployeeDefinition;
import com.eip4.platform.ai.service.AiAgentService;
import com.fasterxml.jackson.databind.JsonNode;
@@ -191,6 +195,24 @@ public class AiAgentController {
return service.validateSchema(schema);
}
@Operation(summary = "列出 AI 工具注册表", description = "返回工具、操作入口、所需 scope 和生产写边界")
@GetMapping("/tools")
public List<AiToolDefinition> tools() {
return service.listTools();
}
@Operation(summary = "列出 AI 员工定义", description = "返回 AI employee 可用工具、所需 scope 和策略")
@GetMapping("/employees")
public List<AiEmployeeDefinition> employees() {
return service.listEmployees();
}
@Operation(summary = "检查 AI 工具访问权限", description = "只做权限判定,不执行工具;生产写默认阻断")
@PostMapping("/tools/access-check")
public AiToolAccessDecision checkToolAccess(@RequestBody(required = false) AiToolAccessRequest request) {
return service.checkToolAccess(request);
}
public record CreateTaskRequest(String objective) {
}