package com.vincent.rsf.server.ai.tool; import com.vincent.rsf.framework.exception.CoolException; import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.List; public final class BuiltinToolGovernanceSupport { private BuiltinToolGovernanceSupport() { } public static int normalizeLimit(Integer limit, int defaultValue, int maxValue) { if (limit == null) { return defaultValue; } if (limit < 1 || limit > maxValue) { throw new CoolException("limit 必须在 1 到 " + maxValue + " 之间"); } return limit; } public static void requireAnyFilter(String message, String... values) { if (values == null || values.length == 0) { throw new CoolException(message); } for (String value : values) { if (StringUtils.hasText(value)) { return; } } throw new CoolException(message); } public static String sanitizeQueryText(String value, String fieldLabel, int maxLength) { if (!StringUtils.hasText(value)) { return null; } String normalized = value.trim(); if (normalized.length() > maxLength) { throw new CoolException(fieldLabel + "长度不能超过 " + maxLength); } return normalized; } public static List sanitizeStringList(List values, String fieldLabel, int maxSize, int maxItemLength) { if (values == null || values.isEmpty()) { throw new CoolException(fieldLabel + "不能为空"); } if (values.size() > maxSize) { throw new CoolException(fieldLabel + "数量不能超过 " + maxSize); } List result = new ArrayList<>(); for (String value : values) { String normalized = sanitizeQueryText(value, fieldLabel + "项", maxItemLength); if (!StringUtils.hasText(normalized)) { continue; } result.add(normalized); } if (result.isEmpty()) { throw new CoolException(fieldLabel + "不能为空"); } return result; } }