feat: add AI tool access registry
This commit is contained in:
@@ -93,6 +93,13 @@ curl http://localhost:8084/api/page/schema/components
|
|||||||
curl -X POST http://localhost:8084/api/page/schema/validate \
|
curl -X POST http://localhost:8084/api/page/schema/validate \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
--data-binary @target/ai-runs/page-schema.json
|
--data-binary @target/ai-runs/page-schema.json
|
||||||
|
|
||||||
|
# AI tools / employees / ACL
|
||||||
|
curl http://localhost:8084/api/ai/tools
|
||||||
|
curl http://localhost:8084/api/ai/employees
|
||||||
|
curl -X POST http://localhost:8084/api/ai/tools/access-check \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"employeeId":"agent-developer","toolName":"local.apply","grantedScopes":["ai:local:apply"],"targetEnv":"local","productionWrite":false}'
|
||||||
```
|
```
|
||||||
|
|
||||||
## tools/
|
## tools/
|
||||||
|
|||||||
@@ -165,6 +165,8 @@ EIP4 当前已经有实体、页面 schema、服务流、菜单、smoke、部署
|
|||||||
|
|
||||||
### P5:多 Agent 协作和 AI 员工
|
### P5:多 Agent 协作和 AI 员工
|
||||||
|
|
||||||
|
状态:第一版完成。
|
||||||
|
|
||||||
目标:
|
目标:
|
||||||
|
|
||||||
- 在平台内提供面向业务用户的 AI 助手能力,而不只是在开发侧使用 Agent。
|
- 在平台内提供面向业务用户的 AI 助手能力,而不只是在开发侧使用 Agent。
|
||||||
@@ -182,6 +184,13 @@ EIP4 当前已经有实体、页面 schema、服务流、菜单、smoke、部署
|
|||||||
- 每个 Agent 有独立权限、审计、工具白名单
|
- 每个 Agent 有独立权限、审计、工具白名单
|
||||||
- 业务侧操作和开发侧操作共用平台审计模型
|
- 业务侧操作和开发侧操作共用平台审计模型
|
||||||
|
|
||||||
|
第一版已落地:
|
||||||
|
|
||||||
|
- `GET /api/ai/tools` 下发工具注册表和 scope 要求。
|
||||||
|
- `GET /api/ai/employees` 下发 AI employee 定义。
|
||||||
|
- `POST /api/ai/tools/access-check` 统一检查 employee/tool/scope/targetEnv/productionWrite。
|
||||||
|
- 生产写默认阻断;当前只开放本地开发、校验、smoke、功能包和发布准备工具。
|
||||||
|
|
||||||
## 四、不照搬的部分
|
## 四、不照搬的部分
|
||||||
|
|
||||||
NocoBase 是 TypeScript/Yarn workspace/插件包体系。EIP4 是 Java/Spring/Maven,并且必须兼容 EIP3 的实体、服务流和 XML 模板迁移路径。直接照搬 NocoBase 插件运行时会引入过高迁移成本。
|
NocoBase 是 TypeScript/Yarn workspace/插件包体系。EIP4 是 Java/Spring/Maven,并且必须兼容 EIP3 的实体、服务流和 XML 模板迁移路径。直接照搬 NocoBase 插件运行时会引入过高迁移成本。
|
||||||
@@ -203,4 +212,4 @@ EIP4 应该借鉴的是边界和流程:
|
|||||||
| P2 | 第一版完成 | 已新增功能包快照、里程碑 revision、CLI 命令和持久化表 |
|
| P2 | 第一版完成 | 已新增功能包快照、里程碑 revision、CLI 命令和持久化表 |
|
||||||
| P3 | 进行中 | 已完成 env registry/current env/cross-env guard/release verify/export/import-preview/diff/rollback-plan 第一版 |
|
| P3 | 进行中 | 已完成 env registry/current env/cross-env guard/release verify/export/import-preview/diff/rollback-plan 第一版 |
|
||||||
| P4 | 第一版完成 | 已新增组件 registry、typed schema validator、校验 API 和 overlay 保存前门禁 |
|
| P4 | 第一版完成 | 已新增组件 registry、typed schema validator、校验 API 和 overlay 保存前门禁 |
|
||||||
| P5 | 待开始 | 依赖权限、审计、工具白名单成熟 |
|
| P5 | 第一版完成 | 已新增 AI tool registry、AI employee registry 和 access-check 门禁 |
|
||||||
|
|||||||
@@ -115,9 +115,22 @@
|
|||||||
|
|
||||||
### P5:AI tools/skills/employee/ACL
|
### P5:AI tools/skills/employee/ACL
|
||||||
|
|
||||||
|
状态:第一版完成。
|
||||||
|
|
||||||
目标:
|
目标:
|
||||||
|
|
||||||
- AI tool registry
|
- AI tool registry
|
||||||
|
- AI employee registry
|
||||||
|
- ACL/scope check
|
||||||
|
- 生产写默认阻断
|
||||||
|
|
||||||
|
第一版已落地:
|
||||||
|
|
||||||
|
- `AiToolRegistry` 注册工具、操作入口、所需 scope、本地限定和生产写边界。
|
||||||
|
- `GET /api/ai/tools` 返回 AI 工具注册表。
|
||||||
|
- `GET /api/ai/employees` 返回 AI employee 定义。
|
||||||
|
- `POST /api/ai/tools/access-check` 只做权限判定,不执行工具;`local.apply` 等本地工具在 prod target 会被阻断。
|
||||||
|
- 当前所有工具都不允许生产写;线上部署仍由人工拿发布包执行。
|
||||||
- Skills CLI check/update
|
- Skills CLI check/update
|
||||||
- AI Agent 权限和审计
|
- AI Agent 权限和审计
|
||||||
- MCP 工具白名单
|
- MCP 工具白名单
|
||||||
|
|||||||
@@ -70,6 +70,19 @@ curl -X POST http://localhost:8084/api/page/schema/validate \
|
|||||||
|
|
||||||
设计器保存 overlay 时也会执行同一套 typed schema 校验;未注册组件或不支持的 `actionType` 会被拒绝。
|
设计器保存 overlay 时也会执行同一套 typed schema 校验;未注册组件或不支持的 `actionType` 会被拒绝。
|
||||||
|
|
||||||
|
## AI 工具权限检查
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl http://localhost:8084/api/ai/tools
|
||||||
|
curl http://localhost:8084/api/ai/employees
|
||||||
|
|
||||||
|
curl -X POST http://localhost:8084/api/ai/tools/access-check \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"employeeId":"agent-developer","toolName":"local.apply","grantedScopes":["ai:local:apply"],"targetEnv":"local","productionWrite":false}'
|
||||||
|
```
|
||||||
|
|
||||||
|
本地 apply、smoke 等工具只允许本地目标;生产写默认被阻断,线上部署仍走人工发布。
|
||||||
|
|
||||||
## 失败处理
|
## 失败处理
|
||||||
|
|
||||||
1. 查看输出 JSON 中的 apply/smoke/repair evidence。
|
1. 查看输出 JSON 中的 apply/smoke/repair evidence。
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
}
|
||||||
@@ -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) {
|
||||||
|
}
|
||||||
@@ -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) {
|
||||||
|
}
|
||||||
@@ -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) {
|
||||||
|
}
|
||||||
@@ -18,6 +18,7 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
|
||||||
|
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;
|
||||||
@@ -50,6 +51,10 @@ import com.eip4.platform.ai.model.AiRunRecord;
|
|||||||
import com.eip4.platform.ai.model.AiSmokeResult;
|
import com.eip4.platform.ai.model.AiSmokeResult;
|
||||||
import com.eip4.platform.ai.model.AiSmokeRun;
|
import com.eip4.platform.ai.model.AiSmokeRun;
|
||||||
import com.eip4.platform.ai.model.AiTask;
|
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.ai.store.AiAgentStore;
|
||||||
import com.eip4.platform.converter.SchemaGate;
|
import com.eip4.platform.converter.SchemaGate;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
@@ -75,19 +80,30 @@ public class AiAgentService {
|
|||||||
private final AiLocalReleaseExecutor localReleaseExecutor;
|
private final AiLocalReleaseExecutor localReleaseExecutor;
|
||||||
private final AiAgentRepairExecutor agentRepairExecutor;
|
private final AiAgentRepairExecutor agentRepairExecutor;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
private final AiToolRegistry toolRegistry;
|
||||||
|
|
||||||
private static volatile SchemaGate sharedSchemaGate;
|
private static volatile SchemaGate sharedSchemaGate;
|
||||||
private volatile SchemaGate schemaGate;
|
private volatile SchemaGate schemaGate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
public AiAgentService(AiAgentStore store, AiFeatureDraftGenerator featureDraftGenerator,
|
public AiAgentService(AiAgentStore store, AiFeatureDraftGenerator featureDraftGenerator,
|
||||||
AiSmokeRunner smokeRunner, AiLocalReleaseExecutor localReleaseExecutor,
|
AiSmokeRunner smokeRunner, AiLocalReleaseExecutor localReleaseExecutor,
|
||||||
AiAgentRepairExecutor agentRepairExecutor, ObjectMapper objectMapper) {
|
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.store = store;
|
||||||
this.featureDraftGenerator = featureDraftGenerator;
|
this.featureDraftGenerator = featureDraftGenerator;
|
||||||
this.smokeRunner = smokeRunner;
|
this.smokeRunner = smokeRunner;
|
||||||
this.localReleaseExecutor = localReleaseExecutor;
|
this.localReleaseExecutor = localReleaseExecutor;
|
||||||
this.agentRepairExecutor = agentRepairExecutor;
|
this.agentRepairExecutor = agentRepairExecutor;
|
||||||
this.objectMapper = objectMapper;
|
this.objectMapper = objectMapper;
|
||||||
|
this.toolRegistry = toolRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AiTask createTask(String objective) {
|
public AiTask createTask(String objective) {
|
||||||
@@ -695,6 +711,18 @@ public class AiAgentService {
|
|||||||
return resp;
|
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) {
|
private AiArtifactType parseType(String type) {
|
||||||
if (type == null || type.isBlank()) {
|
if (type == null || type.isBlank()) {
|
||||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "artifactType 不能为空");
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "artifactType 不能为空");
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,6 +28,10 @@ import com.eip4.platform.ai.model.AiRevision;
|
|||||||
import com.eip4.platform.ai.model.AiRunRecord;
|
import com.eip4.platform.ai.model.AiRunRecord;
|
||||||
import com.eip4.platform.ai.model.AiSmokeRun;
|
import com.eip4.platform.ai.model.AiSmokeRun;
|
||||||
import com.eip4.platform.ai.model.AiTask;
|
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.eip4.platform.ai.service.AiAgentService;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
|
||||||
@@ -191,6 +195,24 @@ public class AiAgentController {
|
|||||||
return service.validateSchema(schema);
|
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) {
|
public record CreateTaskRequest(String objective) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package com.eip4.platform.ai.service;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import com.eip4.platform.ai.model.AiToolAccessRequest;
|
||||||
|
|
||||||
|
class AiToolRegistryTest {
|
||||||
|
|
||||||
|
private final AiToolRegistry registry = AiToolRegistry.defaults();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void agentDeveloperCanUseLocalApplyWithScopeInLocalEnv() {
|
||||||
|
var decision = registry.check(new AiToolAccessRequest(
|
||||||
|
"agent-developer",
|
||||||
|
"local.apply",
|
||||||
|
List.of("ai:local:apply"),
|
||||||
|
"local",
|
||||||
|
false));
|
||||||
|
|
||||||
|
assertTrue(decision.allowed());
|
||||||
|
assertTrue(decision.blockers().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void localOnlyToolIsBlockedForProductionTarget() {
|
||||||
|
var decision = registry.check(new AiToolAccessRequest(
|
||||||
|
"agent-developer",
|
||||||
|
"local.apply",
|
||||||
|
List.of("ai:local:apply", "human:approval"),
|
||||||
|
"prod",
|
||||||
|
true));
|
||||||
|
|
||||||
|
assertFalse(decision.allowed());
|
||||||
|
assertTrue(decision.blockers().stream().anyMatch(b -> b.contains("local-only")));
|
||||||
|
assertTrue(decision.blockers().stream().anyMatch(b -> b.contains("production writes")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void missingScopesAreReported() {
|
||||||
|
var decision = registry.check(new AiToolAccessRequest(
|
||||||
|
"release-auditor",
|
||||||
|
"release.prepare",
|
||||||
|
List.of(),
|
||||||
|
"local",
|
||||||
|
false));
|
||||||
|
|
||||||
|
assertFalse(decision.allowed());
|
||||||
|
assertTrue(decision.missingScopes().contains("ai:release:prepare"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void employeeCannotUseUnassignedTool() {
|
||||||
|
var decision = registry.check(new AiToolAccessRequest(
|
||||||
|
"support-diagnoser",
|
||||||
|
"feature.package",
|
||||||
|
List.of("ai:package:create"),
|
||||||
|
"local",
|
||||||
|
false));
|
||||||
|
|
||||||
|
assertFalse(decision.allowed());
|
||||||
|
assertTrue(decision.blockers().stream().anyMatch(b -> b.contains("not allowed")));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import static org.mockito.ArgumentMatchers.any;
|
|||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
@@ -44,6 +45,10 @@ import com.eip4.platform.ai.model.AiRunRecord;
|
|||||||
import com.eip4.platform.ai.model.AiSmokeResult;
|
import com.eip4.platform.ai.model.AiSmokeResult;
|
||||||
import com.eip4.platform.ai.model.AiSmokeRun;
|
import com.eip4.platform.ai.model.AiSmokeRun;
|
||||||
import com.eip4.platform.ai.model.AiTask;
|
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.eip4.platform.ai.service.AiAgentService;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
@@ -147,6 +152,63 @@ class AiAgentControllerTest {
|
|||||||
assertEquals("page", captor.getValue().path("type").asText());
|
assertEquals("page", captor.getValue().path("type").asText());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 工具注册表接口返回工具列表() throws Exception {
|
||||||
|
when(service.listTools()).thenReturn(List.of(new AiToolDefinition(
|
||||||
|
"schema.validate", "validation", "validate schema",
|
||||||
|
List.of("POST /api/page/schema/validate"),
|
||||||
|
List.of("ai:schema:validate"), false, false)));
|
||||||
|
|
||||||
|
mvc.perform(get("/api/ai/tools"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$[0].name").value("schema.validate"))
|
||||||
|
.andExpect(jsonPath("$[0].requiredScopes[0]").value("ai:schema:validate"));
|
||||||
|
|
||||||
|
verify(service).listTools();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void ai员工接口返回员工定义() throws Exception {
|
||||||
|
when(service.listEmployees()).thenReturn(List.of(new AiEmployeeDefinition(
|
||||||
|
"agent-developer", "Agent Developer", "Develops features",
|
||||||
|
List.of("feature.draft.generate"),
|
||||||
|
List.of("ai:feature:draft"),
|
||||||
|
List.of("Local apply only"))));
|
||||||
|
|
||||||
|
mvc.perform(get("/api/ai/employees"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$[0].id").value("agent-developer"))
|
||||||
|
.andExpect(jsonPath("$[0].toolNames[0]").value("feature.draft.generate"));
|
||||||
|
|
||||||
|
verify(service).listEmployees();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 工具访问检查接口不执行工具只返回判定() throws Exception {
|
||||||
|
var request = new AiToolAccessRequest("agent-developer", "local.apply",
|
||||||
|
List.of("ai:local:apply"), "prod", true);
|
||||||
|
when(service.checkToolAccess(any(AiToolAccessRequest.class))).thenReturn(new AiToolAccessDecision(
|
||||||
|
false,
|
||||||
|
List.of(),
|
||||||
|
List.of("Tool local.apply is not allowed to perform production writes"),
|
||||||
|
new AiToolDefinition("local.apply", "local-gate", "local apply",
|
||||||
|
List.of("POST /local-apply"), List.of("ai:local:apply"), true, false),
|
||||||
|
new AiEmployeeDefinition("agent-developer", "Agent Developer", "Develops features",
|
||||||
|
List.of("local.apply"), List.of("ai:local:apply"), List.of())));
|
||||||
|
|
||||||
|
mvc.perform(post("/api/ai/tools/access-check")
|
||||||
|
.contentType("application/json")
|
||||||
|
.content(objectMapper.writeValueAsString(request)))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.allowed").value(false))
|
||||||
|
.andExpect(jsonPath("$.blockers[0]").value("Tool local.apply is not allowed to perform production writes"));
|
||||||
|
|
||||||
|
ArgumentCaptor<AiToolAccessRequest> captor = ArgumentCaptor.forClass(AiToolAccessRequest.class);
|
||||||
|
verify(service).checkToolAccess(captor.capture());
|
||||||
|
assertEquals("prod", captor.getValue().targetEnv());
|
||||||
|
assertTrue(captor.getValue().productionWrite());
|
||||||
|
}
|
||||||
|
|
||||||
private AiAutoDevelopmentRun autoDevelopmentRun() {
|
private AiAutoDevelopmentRun autoDevelopmentRun() {
|
||||||
AiFeatureDevelopment development = new AiFeatureDevelopment(task(), List.of(draft()), changeSet());
|
AiFeatureDevelopment development = new AiFeatureDevelopment(task(), List.of(draft()), changeSet());
|
||||||
AiReleasePlan releasePlan = new AiReleasePlan("ait-1", "aics-1", "READY",
|
AiReleasePlan releasePlan = new AiReleasePlan("ait-1", "aics-1", "READY",
|
||||||
|
|||||||
Reference in New Issue
Block a user