feat(sync): add pre-delivery contract verification
This commit is contained in:
@@ -93,12 +93,12 @@ public class GuardedToolCallback implements ToolCallback {
|
||||
}
|
||||
|
||||
/**
|
||||
* SYNC 工具(*.changes.list)数据域硬约束:tenant_id/scope 入参一律 PERMISSION_DENIED。
|
||||
* SYNC 数据工具的数据域硬约束:tenant_id/scope 入参一律 PERMISSION_DENIED。
|
||||
* 工具方法签名本就不含这些参数,这里拦的是调用方在原始 JSON 里多传的键
|
||||
* (Spring AI 对未知参数默认宽松,不能依赖反序列化兜底)。
|
||||
*/
|
||||
private void rejectForbiddenSyncParams(String toolName, String toolInput) {
|
||||
if (!toolName.endsWith(IntegrationContextService.CHANGES_TOOL_SUFFIX)) {
|
||||
if (!IntegrationContextService.isSyncDataTool(toolName)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
|
||||
@@ -35,8 +35,13 @@ public class IntegrationContextService {
|
||||
public static final String CAPABILITY_QUERY = "QUERY";
|
||||
public static final String CAPABILITY_SYNC = "SYNC";
|
||||
|
||||
/** 增量同步工具命名约定:出现该后缀即视为具备 SYNC 能力(数据驱动,不硬编码工具清单) */
|
||||
/** 后台数据工具命名约定:变化流与发送前批量复核均属于 SYNC 能力。 */
|
||||
static final String CHANGES_TOOL_SUFFIX = ".changes.list";
|
||||
static final String CURRENT_BATCH_TOOL_SUFFIX = ".current.batch_get";
|
||||
|
||||
static boolean isSyncDataTool(String name) {
|
||||
return name.endsWith(CHANGES_TOOL_SUFFIX) || name.endsWith(CURRENT_BATCH_TOOL_SUFFIX);
|
||||
}
|
||||
|
||||
final TokenScopeMapper tokenScopeMapper;
|
||||
final AgentScopeMapper agentScopeMapper;
|
||||
@@ -93,10 +98,10 @@ public class IntegrationContextService {
|
||||
/** 能力等级:能力集合内含 *.changes.list 工具 → SYNC;含其他工具 → QUERY;无能力 → 空 */
|
||||
List<String> capabilityLevels(Collection<String> allowedTools) {
|
||||
List<String> levels = new ArrayList<>();
|
||||
if (allowedTools.stream().anyMatch(name -> !name.endsWith(CHANGES_TOOL_SUFFIX))) {
|
||||
if (allowedTools.stream().anyMatch(name -> !isSyncDataTool(name))) {
|
||||
levels.add(CAPABILITY_QUERY);
|
||||
}
|
||||
if (allowedTools.stream().anyMatch(name -> name.endsWith(CHANGES_TOOL_SUFFIX))) {
|
||||
if (allowedTools.stream().anyMatch(IntegrationContextService::isSyncDataTool)) {
|
||||
levels.add(CAPABILITY_SYNC);
|
||||
}
|
||||
return levels;
|
||||
|
||||
@@ -28,6 +28,9 @@ public enum McpErrorCode {
|
||||
/** 调用方声明的 schema_version 不受支持 */
|
||||
SCHEMA_VERSION_UNSUPPORTED,
|
||||
|
||||
/** 工具入参格式、数量或取值不符合公开契约 */
|
||||
INVALID_ARGUMENT,
|
||||
|
||||
/** 数据处理过程中发现不一致或未预期异常(兜底码,内部细节只落服务端日志) */
|
||||
DATA_INTEGRITY_ERROR
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import java.time.Instant;
|
||||
|
||||
/**
|
||||
* 工具调用限流(REQ-20260911-0013 资源隔离):按 Token 分档计数——
|
||||
* *.changes.list 走 SYNC 档位,其余走 QUERY 档位,互不占配额。
|
||||
* 后台数据工具走 SYNC 档位,其余走 QUERY 档位,互不占配额。
|
||||
* Redis 固定窗口(分钟),超额抛 RATE_LIMITED;Redis 不可用时放行并告警
|
||||
* (限流是保护措施,不应成为可用性单点)。
|
||||
* 配额 0 或负数 = 关闭该档位。
|
||||
@@ -38,7 +38,7 @@ public class RateLimitService {
|
||||
if (tokenId == null) {
|
||||
return;
|
||||
}
|
||||
boolean sync = toolName.endsWith(IntegrationContextService.CHANGES_TOOL_SUFFIX);
|
||||
boolean sync = IntegrationContextService.isSyncDataTool(toolName);
|
||||
int limit = sync ? syncPerMinute : queryPerMinute;
|
||||
if (limit <= 0) {
|
||||
return;
|
||||
|
||||
@@ -13,7 +13,9 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -76,6 +78,37 @@ public class ContractsSyncEntity extends AbstractSyncEntity implements SyncEntit
|
||||
return rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the current authoritative rows for pre-delivery verification.
|
||||
* Soft-deleted rows are intentionally retained so consumers can cancel a
|
||||
* stale recommendation instead of confusing deletion with transport loss.
|
||||
*/
|
||||
public List<Map<String, Object>> fetchCurrent(List<Long> contractIds, User user) {
|
||||
if (contractIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
LambdaQueryWrapper<ContractNew> wrapper = new LambdaQueryWrapper<ContractNew>()
|
||||
.in(ContractNew::getId, contractIds);
|
||||
if (user != null) {
|
||||
permissionService.applyContractRowFilter(wrapper, user);
|
||||
}
|
||||
List<ContractNew> contracts = contractNewMapper.selectList(wrapper);
|
||||
List<Long> propertyIds = contracts.stream().map(ContractNew::getPropertyId)
|
||||
.filter(Objects::nonNull).distinct().collect(Collectors.toList());
|
||||
Map<Long, String> customerIds = propertyIds.isEmpty() ? Map.of() : customerIdsByProperty(propertyIds);
|
||||
Map<Long, Integer> inputOrder = new HashMap<>();
|
||||
for (int index = 0; index < contractIds.size(); index++) {
|
||||
inputOrder.put(contractIds.get(index), index);
|
||||
}
|
||||
contracts.sort(Comparator.comparingInt(row -> inputOrder.getOrDefault(row.getId(), Integer.MAX_VALUE)));
|
||||
List<Map<String, Object>> items = new ArrayList<>(contracts.size());
|
||||
for (ContractNew contract : contracts) {
|
||||
items.add(toItem(contract, ChangesEngine.effectiveTs(contract.getModifyTime(), contract.getCreateTime()),
|
||||
customerIds));
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
private Map<String, Object> toItem(ContractNew row, long mt, Map<Long, String> customerIds) {
|
||||
boolean deleted = row.getSoftDelete() != null && row.getSoftDelete() == 1;
|
||||
Date effective = row.getModifyTime() != null ? row.getModifyTime() : row.getCreateTime();
|
||||
|
||||
@@ -1,15 +1,25 @@
|
||||
package me.iiv.agent.mcp.tool;
|
||||
|
||||
import me.iiv.agent.mcp.ToolObject;
|
||||
import me.iiv.agent.mcp.McpErrorCode;
|
||||
import me.iiv.agent.mcp.ToolCallException;
|
||||
import me.iiv.agent.mcp.XqTool;
|
||||
import me.iiv.agent.mcp.sync.ChangesEngine;
|
||||
import me.iiv.agent.mcp.sync.ContractsSyncEntity;
|
||||
import me.iiv.agent.mcp.sync.PropertiesSyncEntity;
|
||||
import me.iiv.agent.mcp.sync.UsersSyncEntity;
|
||||
import me.iiv.agent.security.AuthContext;
|
||||
import me.iiv.agent.security.AuthContextHolder;
|
||||
import org.springframework.ai.tool.annotation.Tool;
|
||||
import org.springframework.ai.tool.annotation.ToolParam;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -35,9 +45,11 @@ public class ChangesTools implements ToolObject {
|
||||
private static final String LIMIT_DESC = "每页条数,默认 100,上限 200(超出截断为 200 并在 notice 提示)";
|
||||
|
||||
final ChangesEngine changesEngine;
|
||||
final ContractsSyncEntity contractsSyncEntity;
|
||||
|
||||
public ChangesTools(ChangesEngine changesEngine) {
|
||||
public ChangesTools(ChangesEngine changesEngine, ContractsSyncEntity contractsSyncEntity) {
|
||||
this.changesEngine = changesEngine;
|
||||
this.contractsSyncEntity = contractsSyncEntity;
|
||||
}
|
||||
|
||||
@XqTool(category = "sync", kind = "read")
|
||||
@@ -75,4 +87,51 @@ public class ChangesTools implements ToolObject {
|
||||
@ToolParam(required = false, description = LIMIT_DESC) Integer limit) {
|
||||
return changesEngine.execute(ContractsSyncEntity.NAME, syncToken, cursor, limit);
|
||||
}
|
||||
|
||||
@XqTool(category = "sync", kind = "read")
|
||||
@Tool(name = "contracts.current.batch_get", description = "发送经营提醒前批量复核合同当前权威状态。"
|
||||
+ "仅接受合同稳定ID列表,最多200个;返回items及not_found_ids。"
|
||||
+ "items字段与contracts.changes.list一致,包含当前状态、到期日、负责人、客户、提醒配置、版本和删除标记。"
|
||||
+ "数据域由Token确定,调用方不得传tenant_id或scope。")
|
||||
public Map<String, Object> contractsCurrentBatchGet(
|
||||
@ToolParam(description = "合同稳定ID列表,1至200个;重复ID自动去重并保持首次出现顺序")
|
||||
List<String> contractIds) {
|
||||
List<Long> ids = parseContractIds(contractIds);
|
||||
AuthContext context = AuthContextHolder.get();
|
||||
List<Map<String, Object>> items = contractsSyncEntity.fetchCurrent(ids,
|
||||
context == null ? null : context.getUser());
|
||||
LinkedHashSet<String> found = new LinkedHashSet<>();
|
||||
for (Map<String, Object> item : items) {
|
||||
found.add(String.valueOf(item.get("contract_id")));
|
||||
}
|
||||
List<String> missing = ids.stream().map(String::valueOf).filter(id -> !found.contains(id)).toList();
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("checked_at", OffsetDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
|
||||
result.put("count", items.size());
|
||||
result.put("items", items);
|
||||
result.put("not_found_ids", missing);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<Long> parseContractIds(List<String> contractIds) {
|
||||
if (contractIds == null || contractIds.isEmpty()) {
|
||||
throw new ToolCallException(McpErrorCode.INVALID_ARGUMENT, "contractIds必须包含1至200个合同ID");
|
||||
}
|
||||
if (contractIds.size() > 200) {
|
||||
throw new ToolCallException(McpErrorCode.INVALID_ARGUMENT, "contractIds最多允许200个合同ID");
|
||||
}
|
||||
LinkedHashSet<Long> unique = new LinkedHashSet<>();
|
||||
for (String raw : contractIds) {
|
||||
try {
|
||||
long id = Long.parseLong(raw == null ? "" : raw.trim());
|
||||
if (id <= 0) {
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
unique.add(id);
|
||||
} catch (NumberFormatException error) {
|
||||
throw new ToolCallException(McpErrorCode.INVALID_ARGUMENT, "contractIds包含无效合同ID");
|
||||
}
|
||||
}
|
||||
return new ArrayList<>(unique);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user