zhou zhou
13 小时以前 d5884d0974d17d96225a5d80e432de33a5ee6552
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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<String> sanitizeStringList(List<String> 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<String> 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;
    }
}